blob: b26737dd56d375566d973bac9138b41dff7089e6 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
56
57#include <openssl/rsa.h>
58
David Benjamin79c59a32015-09-19 13:35:39 -040059#include <limits.h>
Adam Langley2b2d66d2015-01-30 17:08:37 -080060#include <string.h>
61
Adam Langley95c29f32014-06-20 12:00:00 -070062#include <openssl/bn.h>
63#include <openssl/engine.h>
64#include <openssl/err.h>
65#include <openssl/ex_data.h>
66#include <openssl/mem.h>
David Benjamin98193672016-03-25 18:07:11 -040067#include <openssl/nid.h>
Brian Smith054e6822015-03-27 21:12:01 -100068#include <openssl/thread.h>
Adam Langley95c29f32014-06-20 12:00:00 -070069
70#include "internal.h"
Adam Langley683d7bd2015-04-13 11:04:14 -070071#include "../internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -070072
73
David Benjamin9f33fc62015-04-15 17:29:53 -040074static CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT;
75
Adam Langley95c29f32014-06-20 12:00:00 -070076RSA *RSA_new(void) { return RSA_new_method(NULL); }
77
78RSA *RSA_new_method(const ENGINE *engine) {
Brian Smith5ba06892016-02-07 09:36:04 -100079 RSA *rsa = OPENSSL_malloc(sizeof(RSA));
Adam Langley95c29f32014-06-20 12:00:00 -070080 if (rsa == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -040081 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -070082 return NULL;
83 }
84
David Benjamin17cf2cb2016-12-13 01:07:13 -050085 OPENSSL_memset(rsa, 0, sizeof(RSA));
Adam Langley95c29f32014-06-20 12:00:00 -070086
87 if (engine) {
88 rsa->meth = ENGINE_get_RSA_method(engine);
89 }
90
91 if (rsa->meth == NULL) {
92 rsa->meth = (RSA_METHOD*) &RSA_default_method;
93 }
94 METHOD_ref(rsa->meth);
95
96 rsa->references = 1;
97 rsa->flags = rsa->meth->flags;
Adam Langley683d7bd2015-04-13 11:04:14 -070098 CRYPTO_MUTEX_init(&rsa->lock);
David Benjamin8a589332015-12-04 23:14:35 -050099 CRYPTO_new_ex_data(&rsa->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700100
101 if (rsa->meth->init && !rsa->meth->init(rsa)) {
David Benjamin9f33fc62015-04-15 17:29:53 -0400102 CRYPTO_free_ex_data(&g_ex_data_class, rsa, &rsa->ex_data);
David Benjamin79680ff2015-10-23 18:46:16 -0400103 CRYPTO_MUTEX_cleanup(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700104 METHOD_unref(rsa->meth);
105 OPENSSL_free(rsa);
106 return NULL;
107 }
108
109 return rsa;
110}
111
Adam Langley839b8812015-05-26 11:36:46 -0700112void RSA_additional_prime_free(RSA_additional_prime *ap) {
113 if (ap == NULL) {
114 return;
115 }
116
117 BN_clear_free(ap->prime);
118 BN_clear_free(ap->exp);
119 BN_clear_free(ap->coeff);
120 BN_clear_free(ap->r);
David Benjamin8fb0f522015-11-03 18:24:07 -0500121 BN_MONT_CTX_free(ap->mont);
Adam Langley839b8812015-05-26 11:36:46 -0700122 OPENSSL_free(ap);
123}
124
Adam Langley95c29f32014-06-20 12:00:00 -0700125void RSA_free(RSA *rsa) {
126 unsigned u;
127
128 if (rsa == NULL) {
129 return;
130 }
131
Adam Langley0da323a2015-05-15 12:49:30 -0700132 if (!CRYPTO_refcount_dec_and_test_zero(&rsa->references)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700133 return;
134 }
135
David Benjamin8fb0f522015-11-03 18:24:07 -0500136 if (rsa->meth->finish) {
Adam Langley95c29f32014-06-20 12:00:00 -0700137 rsa->meth->finish(rsa);
138 }
139 METHOD_unref(rsa->meth);
140
David Benjamin9f33fc62015-04-15 17:29:53 -0400141 CRYPTO_free_ex_data(&g_ex_data_class, rsa, &rsa->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700142
David Benjamind8b65c82015-04-22 16:09:09 -0400143 BN_clear_free(rsa->n);
144 BN_clear_free(rsa->e);
145 BN_clear_free(rsa->d);
146 BN_clear_free(rsa->p);
147 BN_clear_free(rsa->q);
148 BN_clear_free(rsa->dmp1);
149 BN_clear_free(rsa->dmq1);
150 BN_clear_free(rsa->iqmp);
David Benjamin8fb0f522015-11-03 18:24:07 -0500151 BN_MONT_CTX_free(rsa->mont_n);
152 BN_MONT_CTX_free(rsa->mont_p);
153 BN_MONT_CTX_free(rsa->mont_q);
Adam Langley95c29f32014-06-20 12:00:00 -0700154 for (u = 0; u < rsa->num_blindings; u++) {
155 BN_BLINDING_free(rsa->blindings[u]);
156 }
David Benjamind8b65c82015-04-22 16:09:09 -0400157 OPENSSL_free(rsa->blindings);
158 OPENSSL_free(rsa->blindings_inuse);
Adam Langley839b8812015-05-26 11:36:46 -0700159 if (rsa->additional_primes != NULL) {
160 sk_RSA_additional_prime_pop_free(rsa->additional_primes,
161 RSA_additional_prime_free);
162 }
Adam Langley683d7bd2015-04-13 11:04:14 -0700163 CRYPTO_MUTEX_cleanup(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700164 OPENSSL_free(rsa);
165}
166
167int RSA_up_ref(RSA *rsa) {
Adam Langley0da323a2015-05-15 12:49:30 -0700168 CRYPTO_refcount_inc(&rsa->references);
Adam Langley95c29f32014-06-20 12:00:00 -0700169 return 1;
170}
171
David Benjamin5a915032016-08-09 11:04:24 -0400172void RSA_get0_key(const RSA *rsa, const BIGNUM **out_n, const BIGNUM **out_e,
173 const BIGNUM **out_d) {
174 if (out_n != NULL) {
175 *out_n = rsa->n;
176 }
177 if (out_e != NULL) {
178 *out_e = rsa->e;
179 }
180 if (out_d != NULL) {
181 *out_d = rsa->d;
182 }
183}
184
185void RSA_get0_factors(const RSA *rsa, const BIGNUM **out_p,
186 const BIGNUM **out_q) {
187 if (out_p != NULL) {
188 *out_p = rsa->p;
189 }
190 if (out_q != NULL) {
191 *out_q = rsa->q;
192 }
193}
194
195void RSA_get0_crt_params(const RSA *rsa, const BIGNUM **out_dmp1,
196 const BIGNUM **out_dmq1, const BIGNUM **out_iqmp) {
197 if (out_dmp1 != NULL) {
198 *out_dmp1 = rsa->dmp1;
199 }
200 if (out_dmq1 != NULL) {
201 *out_dmq1 = rsa->dmq1;
202 }
203 if (out_iqmp != NULL) {
204 *out_iqmp = rsa->iqmp;
205 }
206}
207
Adam Langley95c29f32014-06-20 12:00:00 -0700208int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) {
209 if (rsa->meth->keygen) {
210 return rsa->meth->keygen(rsa, bits, e_value, cb);
211 }
212
David Benjamind93831d2015-10-29 13:19:12 -0400213 return rsa_default_keygen(rsa, bits, e_value, cb);
Adam Langley95c29f32014-06-20 12:00:00 -0700214}
215
Adam Langley839b8812015-05-26 11:36:46 -0700216int RSA_generate_multi_prime_key(RSA *rsa, int bits, int num_primes,
217 BIGNUM *e_value, BN_GENCB *cb) {
218 if (rsa->meth->multi_prime_keygen) {
219 return rsa->meth->multi_prime_keygen(rsa, bits, num_primes, e_value, cb);
220 }
221
David Benjamind93831d2015-10-29 13:19:12 -0400222 return rsa_default_multi_prime_keygen(rsa, bits, num_primes, e_value, cb);
Adam Langley839b8812015-05-26 11:36:46 -0700223}
224
Adam Langley95c29f32014-06-20 12:00:00 -0700225int RSA_encrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
226 const uint8_t *in, size_t in_len, int padding) {
227 if (rsa->meth->encrypt) {
228 return rsa->meth->encrypt(rsa, out_len, out, max_out, in, in_len, padding);
229 }
230
David Benjamind93831d2015-10-29 13:19:12 -0400231 return rsa_default_encrypt(rsa, out_len, out, max_out, in, in_len, padding);
Adam Langley95c29f32014-06-20 12:00:00 -0700232}
233
Matt Braithwaite978f16e2015-10-19 13:38:36 -0700234int RSA_public_encrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700235 int padding) {
236 size_t out_len;
237
238 if (!RSA_encrypt(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
239 return -1;
240 }
241
Matt Braithwaite978f16e2015-10-19 13:38:36 -0700242 if (out_len > INT_MAX) {
243 OPENSSL_PUT_ERROR(RSA, ERR_R_OVERFLOW);
244 return -1;
245 }
Adam Langley95c29f32014-06-20 12:00:00 -0700246 return out_len;
247}
248
249int RSA_sign_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
250 const uint8_t *in, size_t in_len, int padding) {
251 if (rsa->meth->sign_raw) {
252 return rsa->meth->sign_raw(rsa, out_len, out, max_out, in, in_len, padding);
253 }
254
David Benjamind93831d2015-10-29 13:19:12 -0400255 return rsa_default_sign_raw(rsa, out_len, out, max_out, in, in_len, padding);
Adam Langley95c29f32014-06-20 12:00:00 -0700256}
257
Matt Braithwaite978f16e2015-10-19 13:38:36 -0700258int RSA_private_encrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700259 int padding) {
260 size_t out_len;
261
262 if (!RSA_sign_raw(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
263 return -1;
264 }
265
Matt Braithwaite978f16e2015-10-19 13:38:36 -0700266 if (out_len > INT_MAX) {
267 OPENSSL_PUT_ERROR(RSA, ERR_R_OVERFLOW);
268 return -1;
269 }
Adam Langley95c29f32014-06-20 12:00:00 -0700270 return out_len;
271}
272
273int RSA_decrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
274 const uint8_t *in, size_t in_len, int padding) {
275 if (rsa->meth->decrypt) {
276 return rsa->meth->decrypt(rsa, out_len, out, max_out, in, in_len, padding);
277 }
278
David Benjamind93831d2015-10-29 13:19:12 -0400279 return rsa_default_decrypt(rsa, out_len, out, max_out, in, in_len, padding);
Adam Langley95c29f32014-06-20 12:00:00 -0700280}
281
David Benjamin79c59a32015-09-19 13:35:39 -0400282int RSA_private_decrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700283 int padding) {
284 size_t out_len;
285
286 if (!RSA_decrypt(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
287 return -1;
288 }
289
David Benjamin79c59a32015-09-19 13:35:39 -0400290 if (out_len > INT_MAX) {
291 OPENSSL_PUT_ERROR(RSA, ERR_R_OVERFLOW);
292 return -1;
293 }
Adam Langley95c29f32014-06-20 12:00:00 -0700294 return out_len;
295}
296
Matt Braithwaite978f16e2015-10-19 13:38:36 -0700297int RSA_public_decrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700298 int padding) {
299 size_t out_len;
300
301 if (!RSA_verify_raw(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
302 return -1;
303 }
304
Matt Braithwaite978f16e2015-10-19 13:38:36 -0700305 if (out_len > INT_MAX) {
306 OPENSSL_PUT_ERROR(RSA, ERR_R_OVERFLOW);
307 return -1;
308 }
Adam Langley95c29f32014-06-20 12:00:00 -0700309 return out_len;
310}
311
312unsigned RSA_size(const RSA *rsa) {
David Benjamin925fee32014-07-11 14:14:08 -0400313 if (rsa->meth->size) {
314 return rsa->meth->size(rsa);
315 }
316
David Benjamind93831d2015-10-29 13:19:12 -0400317 return rsa_default_size(rsa);
Adam Langley95c29f32014-06-20 12:00:00 -0700318}
319
David Benjaminecc0ce72014-07-18 18:39:42 -0400320int RSA_is_opaque(const RSA *rsa) {
321 return rsa->meth && (rsa->meth->flags & RSA_FLAG_OPAQUE);
322}
323
David Benjamin8a589332015-12-04 23:14:35 -0500324int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
Adam Langley95c29f32014-06-20 12:00:00 -0700325 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -0400326 int index;
David Benjamin8a589332015-12-04 23:14:35 -0500327 if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp, dup_func,
328 free_func)) {
David Benjamin9f33fc62015-04-15 17:29:53 -0400329 return -1;
330 }
331 return index;
Adam Langley95c29f32014-06-20 12:00:00 -0700332}
333
334int RSA_set_ex_data(RSA *d, int idx, void *arg) {
335 return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
336}
337
338void *RSA_get_ex_data(const RSA *d, int idx) {
339 return CRYPTO_get_ex_data(&d->ex_data, idx);
340}
341
342/* SSL_SIG_LENGTH is the size of an SSL/TLS (prior to TLS 1.2) signature: it's
343 * the length of an MD5 and SHA1 hash. */
344static const unsigned SSL_SIG_LENGTH = 36;
345
346/* pkcs1_sig_prefix contains the ASN.1, DER encoded prefix for a hash that is
347 * to be signed with PKCS#1. */
348struct pkcs1_sig_prefix {
349 /* nid identifies the hash function. */
350 int nid;
351 /* len is the number of bytes of |bytes| which are valid. */
352 uint8_t len;
353 /* bytes contains the DER bytes. */
354 uint8_t bytes[19];
355};
356
357/* kPKCS1SigPrefixes contains the ASN.1 prefixes for PKCS#1 signatures with
358 * different hash functions. */
359static const struct pkcs1_sig_prefix kPKCS1SigPrefixes[] = {
360 {
361 NID_md5,
362 18,
363 {0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
364 0x02, 0x05, 0x05, 0x00, 0x04, 0x10},
365 },
366 {
367 NID_sha1,
368 15,
369 {0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
370 0x00, 0x04, 0x14},
371 },
372 {
373 NID_sha224,
374 19,
375 {0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
376 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1c},
377 },
378 {
379 NID_sha256,
380 19,
381 {0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
382 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20},
383 },
384 {
385 NID_sha384,
386 19,
387 {0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
388 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30},
389 },
390 {
391 NID_sha512,
392 19,
393 {0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
394 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40},
395 },
396 {
Adam Langley95c29f32014-06-20 12:00:00 -0700397 NID_undef, 0, {0},
398 },
399};
400
David Benjaminb0acb772015-05-28 16:53:31 -0400401int RSA_add_pkcs1_prefix(uint8_t **out_msg, size_t *out_msg_len,
402 int *is_alloced, int hash_nid, const uint8_t *msg,
403 size_t msg_len) {
Adam Langley95c29f32014-06-20 12:00:00 -0700404 unsigned i;
Adam Langley95c29f32014-06-20 12:00:00 -0700405
406 if (hash_nid == NID_md5_sha1) {
407 /* Special case: SSL signature, just check the length. */
408 if (msg_len != SSL_SIG_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400409 OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH);
Adam Langley95c29f32014-06-20 12:00:00 -0700410 return 0;
411 }
412
413 *out_msg = (uint8_t*) msg;
414 *out_msg_len = SSL_SIG_LENGTH;
415 *is_alloced = 0;
416 return 1;
417 }
418
419 for (i = 0; kPKCS1SigPrefixes[i].nid != NID_undef; i++) {
420 const struct pkcs1_sig_prefix *sig_prefix = &kPKCS1SigPrefixes[i];
Brian Smitha039d702015-01-29 15:03:18 -0800421 if (sig_prefix->nid != hash_nid) {
422 continue;
Adam Langley95c29f32014-06-20 12:00:00 -0700423 }
Brian Smitha039d702015-01-29 15:03:18 -0800424
425 const uint8_t* prefix = sig_prefix->bytes;
426 unsigned prefix_len = sig_prefix->len;
427 unsigned signed_msg_len;
428 uint8_t *signed_msg;
429
430 signed_msg_len = prefix_len + msg_len;
431 if (signed_msg_len < prefix_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400432 OPENSSL_PUT_ERROR(RSA, RSA_R_TOO_LONG);
Brian Smitha039d702015-01-29 15:03:18 -0800433 return 0;
434 }
435
436 signed_msg = OPENSSL_malloc(signed_msg_len);
437 if (!signed_msg) {
David Benjamin3570d732015-06-29 00:28:17 -0400438 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
Brian Smitha039d702015-01-29 15:03:18 -0800439 return 0;
440 }
441
David Benjamin17cf2cb2016-12-13 01:07:13 -0500442 OPENSSL_memcpy(signed_msg, prefix, prefix_len);
443 OPENSSL_memcpy(signed_msg + prefix_len, msg, msg_len);
Brian Smitha039d702015-01-29 15:03:18 -0800444
445 *out_msg = signed_msg;
446 *out_msg_len = signed_msg_len;
447 *is_alloced = 1;
448
449 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700450 }
451
David Benjamin3570d732015-06-29 00:28:17 -0400452 OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_ALGORITHM_TYPE);
Brian Smitha039d702015-01-29 15:03:18 -0800453 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700454}
455
456int RSA_sign(int hash_nid, const uint8_t *in, unsigned in_len, uint8_t *out,
457 unsigned *out_len, RSA *rsa) {
458 const unsigned rsa_size = RSA_size(rsa);
459 int ret = 0;
460 uint8_t *signed_msg;
461 size_t signed_msg_len;
462 int signed_msg_is_alloced = 0;
463 size_t size_t_out_len;
464
465 if (rsa->meth->sign) {
466 return rsa->meth->sign(hash_nid, in, in_len, out, out_len, rsa);
467 }
468
David Benjaminb0acb772015-05-28 16:53:31 -0400469 if (!RSA_add_pkcs1_prefix(&signed_msg, &signed_msg_len,
470 &signed_msg_is_alloced, hash_nid, in, in_len)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700471 return 0;
472 }
473
474 if (rsa_size < RSA_PKCS1_PADDING_SIZE ||
475 signed_msg_len > rsa_size - RSA_PKCS1_PADDING_SIZE) {
David Benjamin3570d732015-06-29 00:28:17 -0400476 OPENSSL_PUT_ERROR(RSA, RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
Adam Langley95c29f32014-06-20 12:00:00 -0700477 goto finish;
478 }
479
480 if (RSA_sign_raw(rsa, &size_t_out_len, out, rsa_size, signed_msg,
481 signed_msg_len, RSA_PKCS1_PADDING)) {
482 *out_len = size_t_out_len;
483 ret = 1;
484 }
485
486finish:
487 if (signed_msg_is_alloced) {
488 OPENSSL_free(signed_msg);
489 }
490 return ret;
491}
492
493int RSA_verify(int hash_nid, const uint8_t *msg, size_t msg_len,
494 const uint8_t *sig, size_t sig_len, RSA *rsa) {
Brian Smithc0b196d2016-03-04 08:54:07 -1000495 if (rsa->n == NULL || rsa->e == NULL) {
496 OPENSSL_PUT_ERROR(RSA, RSA_R_VALUE_MISSING);
497 return 0;
498 }
499
Adam Langley95c29f32014-06-20 12:00:00 -0700500 const size_t rsa_size = RSA_size(rsa);
501 uint8_t *buf = NULL;
502 int ret = 0;
503 uint8_t *signed_msg = NULL;
504 size_t signed_msg_len, len;
505 int signed_msg_is_alloced = 0;
506
Adam Langley95c29f32014-06-20 12:00:00 -0700507 if (hash_nid == NID_md5_sha1 && msg_len != SSL_SIG_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400508 OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH);
Adam Langley95c29f32014-06-20 12:00:00 -0700509 return 0;
510 }
511
512 buf = OPENSSL_malloc(rsa_size);
513 if (!buf) {
David Benjamin3570d732015-06-29 00:28:17 -0400514 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700515 return 0;
516 }
517
518 if (!RSA_verify_raw(rsa, &len, buf, rsa_size, sig, sig_len,
519 RSA_PKCS1_PADDING)) {
520 goto out;
521 }
522
David Benjaminb0acb772015-05-28 16:53:31 -0400523 if (!RSA_add_pkcs1_prefix(&signed_msg, &signed_msg_len,
524 &signed_msg_is_alloced, hash_nid, msg, msg_len)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700525 goto out;
526 }
527
Steven Valdezc1966802017-04-10 15:52:19 -0400528 /* Check that no other information follows the hash value (FIPS 186-4 Section
529 * 5.5) and it matches the expected hash. */
David Benjamin17cf2cb2016-12-13 01:07:13 -0500530 if (len != signed_msg_len || OPENSSL_memcmp(buf, signed_msg, len) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400531 OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_SIGNATURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700532 goto out;
533 }
534
535 ret = 1;
536
537out:
David Benjamind8b65c82015-04-22 16:09:09 -0400538 OPENSSL_free(buf);
Adam Langley95c29f32014-06-20 12:00:00 -0700539 if (signed_msg_is_alloced) {
540 OPENSSL_free(signed_msg);
541 }
542 return ret;
543}
Adam Langley409766d2014-06-20 12:00:00 -0700544
545static void bn_free_and_null(BIGNUM **bn) {
Adam Langley409766d2014-06-20 12:00:00 -0700546 BN_free(*bn);
547 *bn = NULL;
548}
549
Adam Langley05b73772014-07-25 12:03:51 -0700550int RSA_check_key(const RSA *key) {
Brian Smith7241ca52016-07-25 16:01:10 -1000551 BIGNUM n, pm1, qm1, lcm, gcd, de, dmp1, dmq1, iqmp_times_q;
Adam Langley05b73772014-07-25 12:03:51 -0700552 BN_CTX *ctx;
553 int ok = 0, has_crt_values;
554
555 if (RSA_is_opaque(key)) {
556 /* Opaque keys can't be checked. */
557 return 1;
558 }
559
560 if ((key->p != NULL) != (key->q != NULL)) {
David Benjamin3570d732015-06-29 00:28:17 -0400561 OPENSSL_PUT_ERROR(RSA, RSA_R_ONLY_ONE_OF_P_Q_GIVEN);
Adam Langley05b73772014-07-25 12:03:51 -0700562 return 0;
563 }
564
565 if (!key->n || !key->e) {
David Benjamin3570d732015-06-29 00:28:17 -0400566 OPENSSL_PUT_ERROR(RSA, RSA_R_VALUE_MISSING);
Adam Langley05b73772014-07-25 12:03:51 -0700567 return 0;
568 }
569
570 if (!key->d || !key->p) {
571 /* For a public key, or without p and q, there's nothing that can be
572 * checked. */
573 return 1;
574 }
575
576 ctx = BN_CTX_new();
577 if (ctx == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400578 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
Adam Langley05b73772014-07-25 12:03:51 -0700579 return 0;
580 }
581
582 BN_init(&n);
583 BN_init(&pm1);
584 BN_init(&qm1);
585 BN_init(&lcm);
586 BN_init(&gcd);
587 BN_init(&de);
588 BN_init(&dmp1);
589 BN_init(&dmq1);
Brian Smith7241ca52016-07-25 16:01:10 -1000590 BN_init(&iqmp_times_q);
Adam Langley05b73772014-07-25 12:03:51 -0700591
Adam Langley839b8812015-05-26 11:36:46 -0700592 if (!BN_mul(&n, key->p, key->q, ctx) ||
593 /* lcm = lcm(prime-1, for all primes) */
Adam Langley05b73772014-07-25 12:03:51 -0700594 !BN_sub(&pm1, key->p, BN_value_one()) ||
595 !BN_sub(&qm1, key->q, BN_value_one()) ||
596 !BN_mul(&lcm, &pm1, &qm1, ctx) ||
Adam Langley839b8812015-05-26 11:36:46 -0700597 !BN_gcd(&gcd, &pm1, &qm1, ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -0400598 OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
Adam Langley839b8812015-05-26 11:36:46 -0700599 goto out;
600 }
601
602 size_t num_additional_primes = 0;
603 if (key->additional_primes != NULL) {
604 num_additional_primes = sk_RSA_additional_prime_num(key->additional_primes);
605 }
606
David Benjamin54091232016-09-05 12:47:25 -0400607 for (size_t i = 0; i < num_additional_primes; i++) {
Adam Langley839b8812015-05-26 11:36:46 -0700608 const RSA_additional_prime *ap =
609 sk_RSA_additional_prime_value(key->additional_primes, i);
610 if (!BN_mul(&n, &n, ap->prime, ctx) ||
611 !BN_sub(&pm1, ap->prime, BN_value_one()) ||
612 !BN_mul(&lcm, &lcm, &pm1, ctx) ||
613 !BN_gcd(&gcd, &gcd, &pm1, ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -0400614 OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
Adam Langley839b8812015-05-26 11:36:46 -0700615 goto out;
616 }
617 }
618
619 if (!BN_div(&lcm, NULL, &lcm, &gcd, ctx) ||
Adam Langley05b73772014-07-25 12:03:51 -0700620 !BN_gcd(&gcd, &pm1, &qm1, ctx) ||
Adam Langley839b8812015-05-26 11:36:46 -0700621 /* de = d*e mod lcm(prime-1, for all primes). */
Adam Langley05b73772014-07-25 12:03:51 -0700622 !BN_mod_mul(&de, key->d, key->e, &lcm, ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -0400623 OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
Adam Langley05b73772014-07-25 12:03:51 -0700624 goto out;
625 }
626
627 if (BN_cmp(&n, key->n) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400628 OPENSSL_PUT_ERROR(RSA, RSA_R_N_NOT_EQUAL_P_Q);
Adam Langley05b73772014-07-25 12:03:51 -0700629 goto out;
630 }
631
632 if (!BN_is_one(&de)) {
David Benjamin3570d732015-06-29 00:28:17 -0400633 OPENSSL_PUT_ERROR(RSA, RSA_R_D_E_NOT_CONGRUENT_TO_1);
Adam Langley05b73772014-07-25 12:03:51 -0700634 goto out;
635 }
636
637 has_crt_values = key->dmp1 != NULL;
638 if (has_crt_values != (key->dmq1 != NULL) ||
639 has_crt_values != (key->iqmp != NULL)) {
David Benjamin3570d732015-06-29 00:28:17 -0400640 OPENSSL_PUT_ERROR(RSA, RSA_R_INCONSISTENT_SET_OF_CRT_VALUES);
Adam Langley05b73772014-07-25 12:03:51 -0700641 goto out;
642 }
643
Adam Langley839b8812015-05-26 11:36:46 -0700644 if (has_crt_values && num_additional_primes == 0) {
Adam Langley05b73772014-07-25 12:03:51 -0700645 if (/* dmp1 = d mod (p-1) */
646 !BN_mod(&dmp1, key->d, &pm1, ctx) ||
647 /* dmq1 = d mod (q-1) */
648 !BN_mod(&dmq1, key->d, &qm1, ctx) ||
649 /* iqmp = q^-1 mod p */
Brian Smith7241ca52016-07-25 16:01:10 -1000650 !BN_mod_mul(&iqmp_times_q, key->iqmp, key->q, key->p, ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -0400651 OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
Adam Langley05b73772014-07-25 12:03:51 -0700652 goto out;
653 }
654
655 if (BN_cmp(&dmp1, key->dmp1) != 0 ||
656 BN_cmp(&dmq1, key->dmq1) != 0 ||
Brian Smith7241ca52016-07-25 16:01:10 -1000657 BN_cmp(key->iqmp, key->p) >= 0 ||
658 !BN_is_one(&iqmp_times_q)) {
David Benjamin3570d732015-06-29 00:28:17 -0400659 OPENSSL_PUT_ERROR(RSA, RSA_R_CRT_VALUES_INCORRECT);
Adam Langley05b73772014-07-25 12:03:51 -0700660 goto out;
661 }
662 }
663
664 ok = 1;
665
666out:
667 BN_free(&n);
668 BN_free(&pm1);
669 BN_free(&qm1);
670 BN_free(&lcm);
671 BN_free(&gcd);
672 BN_free(&de);
673 BN_free(&dmp1);
674 BN_free(&dmq1);
Brian Smith7241ca52016-07-25 16:01:10 -1000675 BN_free(&iqmp_times_q);
Adam Langley05b73772014-07-25 12:03:51 -0700676 BN_CTX_free(ctx);
677
678 return ok;
679}
680
Adam Langley409766d2014-06-20 12:00:00 -0700681int RSA_recover_crt_params(RSA *rsa) {
682 BN_CTX *ctx;
683 BIGNUM *totient, *rem, *multiple, *p_plus_q, *p_minus_q;
684 int ok = 0;
685
686 if (rsa->n == NULL || rsa->e == NULL || rsa->d == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400687 OPENSSL_PUT_ERROR(RSA, RSA_R_EMPTY_PUBLIC_KEY);
Adam Langley409766d2014-06-20 12:00:00 -0700688 return 0;
689 }
690
691 if (rsa->p || rsa->q || rsa->dmp1 || rsa->dmq1 || rsa->iqmp) {
David Benjamin3570d732015-06-29 00:28:17 -0400692 OPENSSL_PUT_ERROR(RSA, RSA_R_CRT_PARAMS_ALREADY_GIVEN);
Adam Langley409766d2014-06-20 12:00:00 -0700693 return 0;
694 }
695
Adam Langley839b8812015-05-26 11:36:46 -0700696 if (rsa->additional_primes != NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400697 OPENSSL_PUT_ERROR(RSA, RSA_R_CANNOT_RECOVER_MULTI_PRIME_KEY);
Adam Langley839b8812015-05-26 11:36:46 -0700698 return 0;
699 }
700
Adam Langley409766d2014-06-20 12:00:00 -0700701 /* This uses the algorithm from section 9B of the RSA paper:
702 * http://people.csail.mit.edu/rivest/Rsapaper.pdf */
703
704 ctx = BN_CTX_new();
705 if (ctx == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400706 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
Adam Langley409766d2014-06-20 12:00:00 -0700707 return 0;
708 }
709
710 BN_CTX_start(ctx);
711 totient = BN_CTX_get(ctx);
712 rem = BN_CTX_get(ctx);
713 multiple = BN_CTX_get(ctx);
714 p_plus_q = BN_CTX_get(ctx);
715 p_minus_q = BN_CTX_get(ctx);
716
717 if (totient == NULL || rem == NULL || multiple == NULL || p_plus_q == NULL ||
718 p_minus_q == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400719 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
Adam Langley409766d2014-06-20 12:00:00 -0700720 goto err;
721 }
722
723 /* ed-1 is a small multiple of φ(n). */
724 if (!BN_mul(totient, rsa->e, rsa->d, ctx) ||
725 !BN_sub_word(totient, 1) ||
726 /* φ(n) =
727 * pq - p - q + 1 =
728 * n - (p + q) + 1
729 *
730 * Thus n is a reasonable estimate for φ(n). So, (ed-1)/n will be very
731 * close. But, when we calculate the quotient, we'll be truncating it
732 * because we discard the remainder. Thus (ed-1)/multiple will be >= n,
733 * which the totient cannot be. So we add one to the estimate.
734 *
735 * Consider ed-1 as:
736 *
737 * multiple * (n - (p+q) + 1) =
738 * multiple*n - multiple*(p+q) + multiple
739 *
740 * When we divide by n, the first term becomes multiple and, since
741 * multiple and p+q is tiny compared to n, the second and third terms can
742 * be ignored. Thus I claim that subtracting one from the estimate is
743 * sufficient. */
744 !BN_div(multiple, NULL, totient, rsa->n, ctx) ||
745 !BN_add_word(multiple, 1) ||
746 !BN_div(totient, rem, totient, multiple, ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -0400747 OPENSSL_PUT_ERROR(RSA, ERR_R_BN_LIB);
Adam Langley409766d2014-06-20 12:00:00 -0700748 goto err;
749 }
750
751 if (!BN_is_zero(rem)) {
David Benjamin3570d732015-06-29 00:28:17 -0400752 OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_RSA_PARAMETERS);
Adam Langley409766d2014-06-20 12:00:00 -0700753 goto err;
754 }
755
756 rsa->p = BN_new();
757 rsa->q = BN_new();
758 rsa->dmp1 = BN_new();
759 rsa->dmq1 = BN_new();
760 rsa->iqmp = BN_new();
761 if (rsa->p == NULL || rsa->q == NULL || rsa->dmp1 == NULL || rsa->dmq1 ==
762 NULL || rsa->iqmp == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400763 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
Adam Langley409766d2014-06-20 12:00:00 -0700764 goto err;
765 }
766
767 /* φ(n) = n - (p + q) + 1 =>
768 * n - totient + 1 = p + q */
769 if (!BN_sub(p_plus_q, rsa->n, totient) ||
770 !BN_add_word(p_plus_q, 1) ||
771 /* p - q = sqrt((p+q)^2 - 4n) */
772 !BN_sqr(rem, p_plus_q, ctx) ||
773 !BN_lshift(multiple, rsa->n, 2) ||
774 !BN_sub(rem, rem, multiple) ||
775 !BN_sqrt(p_minus_q, rem, ctx) ||
776 /* q is 1/2 (p+q)-(p-q) */
777 !BN_sub(rsa->q, p_plus_q, p_minus_q) ||
778 !BN_rshift1(rsa->q, rsa->q) ||
779 !BN_div(rsa->p, NULL, rsa->n, rsa->q, ctx) ||
780 !BN_mul(multiple, rsa->p, rsa->q, ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -0400781 OPENSSL_PUT_ERROR(RSA, ERR_R_BN_LIB);
Adam Langley409766d2014-06-20 12:00:00 -0700782 goto err;
783 }
784
785 if (BN_cmp(multiple, rsa->n) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400786 OPENSSL_PUT_ERROR(RSA, RSA_R_INTERNAL_ERROR);
Adam Langley409766d2014-06-20 12:00:00 -0700787 goto err;
788 }
789
790 if (!BN_sub(rem, rsa->p, BN_value_one()) ||
791 !BN_mod(rsa->dmp1, rsa->d, rem, ctx) ||
792 !BN_sub(rem, rsa->q, BN_value_one()) ||
793 !BN_mod(rsa->dmq1, rsa->d, rem, ctx) ||
794 !BN_mod_inverse(rsa->iqmp, rsa->q, rsa->p, ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -0400795 OPENSSL_PUT_ERROR(RSA, ERR_R_BN_LIB);
Adam Langley409766d2014-06-20 12:00:00 -0700796 goto err;
797 }
798
799 ok = 1;
800
801err:
802 BN_CTX_end(ctx);
803 BN_CTX_free(ctx);
804 if (!ok) {
805 bn_free_and_null(&rsa->p);
806 bn_free_and_null(&rsa->q);
807 bn_free_and_null(&rsa->dmp1);
808 bn_free_and_null(&rsa->dmq1);
809 bn_free_and_null(&rsa->iqmp);
810 }
811 return ok;
812}
Adam Langley6bc658d2014-08-18 13:29:45 -0700813
814int RSA_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in,
815 size_t len) {
816 if (rsa->meth->private_transform) {
817 return rsa->meth->private_transform(rsa, out, in, len);
818 }
819
David Benjamind93831d2015-10-29 13:19:12 -0400820 return rsa_default_private_transform(rsa, out, in, len);
Adam Langley6bc658d2014-08-18 13:29:45 -0700821}
Adam Langleyc3ef76f2015-04-13 14:34:17 -0700822
823int RSA_blinding_on(RSA *rsa, BN_CTX *ctx) {
824 return 1;
825}