blob: 0d946cdbd63f334bbe15cdbf7c728f73744fe1d4 [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
Adam Langley2b2d66d2015-01-30 17:08:37 -080059#include <string.h>
60
Adam Langley95c29f32014-06-20 12:00:00 -070061#include <openssl/bn.h>
62#include <openssl/err.h>
63#include <openssl/mem.h>
Brian Smith054e6822015-03-27 21:12:01 -100064#include <openssl/thread.h>
Adam Langley95c29f32014-06-20 12:00:00 -070065
66#include "internal.h"
Adam Langley683d7bd2015-04-13 11:04:14 -070067#include "../internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -070068
69
70#define OPENSSL_RSA_MAX_MODULUS_BITS 16384
71#define OPENSSL_RSA_SMALL_MODULUS_BITS 3072
72#define OPENSSL_RSA_MAX_PUBEXP_BITS \
73 64 /* exponent limit enforced for "large" modulus only */
74
75
76static int finish(RSA *rsa) {
David Benjamind8b65c82015-04-22 16:09:09 -040077 BN_MONT_CTX_free(rsa->_method_mod_n);
78 BN_MONT_CTX_free(rsa->_method_mod_p);
79 BN_MONT_CTX_free(rsa->_method_mod_q);
Adam Langley95c29f32014-06-20 12:00:00 -070080
Adam Langley839b8812015-05-26 11:36:46 -070081 if (rsa->additional_primes != NULL) {
82 size_t i;
83 for (i = 0; i < sk_RSA_additional_prime_num(rsa->additional_primes); i++) {
84 RSA_additional_prime *ap =
85 sk_RSA_additional_prime_value(rsa->additional_primes, i);
86 BN_MONT_CTX_free(ap->method_mod);
87 }
88 }
89
Adam Langley95c29f32014-06-20 12:00:00 -070090 return 1;
91}
92
David Benjamin925fee32014-07-11 14:14:08 -040093static size_t size(const RSA *rsa) {
94 return BN_num_bytes(rsa->n);
95}
96
Adam Langley95c29f32014-06-20 12:00:00 -070097static int encrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
98 const uint8_t *in, size_t in_len, int padding) {
99 const unsigned rsa_size = RSA_size(rsa);
Adam Langley95c29f32014-06-20 12:00:00 -0700100 BIGNUM *f, *result;
101 uint8_t *buf = NULL;
102 BN_CTX *ctx = NULL;
Adam Langley6887edb2014-06-20 12:00:00 -0700103 int i, ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700104
105 if (rsa_size > OPENSSL_RSA_MAX_MODULUS_BITS) {
106 OPENSSL_PUT_ERROR(RSA, encrypt, RSA_R_MODULUS_TOO_LARGE);
107 return 0;
108 }
109
110 if (max_out < rsa_size) {
111 OPENSSL_PUT_ERROR(RSA, encrypt, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
112 return 0;
113 }
114
115 if (BN_ucmp(rsa->n, rsa->e) <= 0) {
116 OPENSSL_PUT_ERROR(RSA, encrypt, RSA_R_BAD_E_VALUE);
117 return 0;
118 }
119
120 /* for large moduli, enforce exponent limit */
121 if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS &&
122 BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
123 OPENSSL_PUT_ERROR(RSA, encrypt, RSA_R_BAD_E_VALUE);
124 return 0;
125 }
126
127 ctx = BN_CTX_new();
128 if (ctx == NULL) {
129 goto err;
130 }
131
132 BN_CTX_start(ctx);
133 f = BN_CTX_get(ctx);
134 result = BN_CTX_get(ctx);
135 buf = OPENSSL_malloc(rsa_size);
136 if (!f || !result || !buf) {
137 OPENSSL_PUT_ERROR(RSA, encrypt, ERR_R_MALLOC_FAILURE);
138 goto err;
139 }
140
141 switch (padding) {
142 case RSA_PKCS1_PADDING:
143 i = RSA_padding_add_PKCS1_type_2(buf, rsa_size, in, in_len);
144 break;
145 case RSA_PKCS1_OAEP_PADDING:
Adam Langley6887edb2014-06-20 12:00:00 -0700146 /* Use the default parameters: SHA-1 for both hashes and no label. */
147 i = RSA_padding_add_PKCS1_OAEP_mgf1(buf, rsa_size, in, in_len,
148 NULL, 0, NULL, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700149 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700150 case RSA_NO_PADDING:
151 i = RSA_padding_add_none(buf, rsa_size, in, in_len);
152 break;
153 default:
154 OPENSSL_PUT_ERROR(RSA, encrypt, RSA_R_UNKNOWN_PADDING_TYPE);
155 goto err;
156 }
157
158 if (i <= 0) {
159 goto err;
160 }
161
162 if (BN_bin2bn(buf, rsa_size, f) == NULL) {
163 goto err;
164 }
165
166 if (BN_ucmp(f, rsa->n) >= 0) {
167 /* usually the padding functions would catch this */
168 OPENSSL_PUT_ERROR(RSA, encrypt, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
169 goto err;
170 }
171
172 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) {
Adam Langley683d7bd2015-04-13 11:04:14 -0700173 if (BN_MONT_CTX_set_locked(&rsa->_method_mod_n, &rsa->lock, rsa->n, ctx) ==
174 NULL) {
Adam Langley95c29f32014-06-20 12:00:00 -0700175 goto err;
176 }
177 }
178
Adam Langley683d7bd2015-04-13 11:04:14 -0700179 if (!rsa->meth->bn_mod_exp(result, f, rsa->e, rsa->n, ctx,
180 rsa->_method_mod_n)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700181 goto err;
182 }
183
184 /* put in leading 0 bytes if the number is less than the length of the
185 * modulus */
Adam Langley6887edb2014-06-20 12:00:00 -0700186 if (!BN_bn2bin_padded(out, rsa_size, result)) {
187 OPENSSL_PUT_ERROR(RSA, encrypt, ERR_R_INTERNAL_ERROR);
188 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700189 }
190
191 *out_len = rsa_size;
192 ret = 1;
193
194err:
195 if (ctx != NULL) {
196 BN_CTX_end(ctx);
197 BN_CTX_free(ctx);
198 }
199 if (buf != NULL) {
200 OPENSSL_cleanse(buf, rsa_size);
201 OPENSSL_free(buf);
202 }
203
204 return ret;
205}
206
207/* MAX_BLINDINGS_PER_RSA defines the maximum number of cached BN_BLINDINGs per
208 * RSA*. Then this limit is exceeded, BN_BLINDING objects will be created and
209 * destroyed as needed. */
210#define MAX_BLINDINGS_PER_RSA 1024
211
212/* rsa_blinding_get returns a BN_BLINDING to use with |rsa|. It does this by
213 * allocating one of the cached BN_BLINDING objects in |rsa->blindings|. If
214 * none are free, the cache will be extended by a extra element and the new
215 * BN_BLINDING is returned.
216 *
217 * On success, the index of the assigned BN_BLINDING is written to
218 * |*index_used| and must be passed to |rsa_blinding_release| when finished. */
219static BN_BLINDING *rsa_blinding_get(RSA *rsa, unsigned *index_used,
220 BN_CTX *ctx) {
221 BN_BLINDING *ret = NULL;
222 BN_BLINDING **new_blindings;
223 uint8_t *new_blindings_inuse;
224 char overflow = 0;
225
Adam Langley683d7bd2015-04-13 11:04:14 -0700226 CRYPTO_MUTEX_lock_write(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700227
Adam Langley33672732015-03-31 18:55:53 -0700228 unsigned i;
229 for (i = 0; i < rsa->num_blindings; i++) {
230 if (rsa->blindings_inuse[i] == 0) {
231 rsa->blindings_inuse[i] = 1;
232 ret = rsa->blindings[i];
233 *index_used = i;
234 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700235 }
236 }
237
238 if (ret != NULL) {
Adam Langley683d7bd2015-04-13 11:04:14 -0700239 CRYPTO_MUTEX_unlock(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700240 return ret;
241 }
242
243 overflow = rsa->num_blindings >= MAX_BLINDINGS_PER_RSA;
244
245 /* We didn't find a free BN_BLINDING to use so increase the length of
246 * the arrays by one and use the newly created element. */
247
Adam Langley683d7bd2015-04-13 11:04:14 -0700248 CRYPTO_MUTEX_unlock(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700249 ret = rsa_setup_blinding(rsa, ctx);
250 if (ret == NULL) {
251 return NULL;
252 }
253
254 if (overflow) {
255 /* We cannot add any more cached BN_BLINDINGs so we use |ret|
256 * and mark it for destruction in |rsa_blinding_release|. */
257 *index_used = MAX_BLINDINGS_PER_RSA;
258 return ret;
259 }
260
Adam Langley683d7bd2015-04-13 11:04:14 -0700261 CRYPTO_MUTEX_lock_write(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700262
263 new_blindings =
264 OPENSSL_malloc(sizeof(BN_BLINDING *) * (rsa->num_blindings + 1));
265 if (new_blindings == NULL) {
266 goto err1;
267 }
268 memcpy(new_blindings, rsa->blindings,
269 sizeof(BN_BLINDING *) * rsa->num_blindings);
270 new_blindings[rsa->num_blindings] = ret;
271
272 new_blindings_inuse = OPENSSL_malloc(rsa->num_blindings + 1);
273 if (new_blindings_inuse == NULL) {
274 goto err2;
275 }
276 memcpy(new_blindings_inuse, rsa->blindings_inuse, rsa->num_blindings);
277 new_blindings_inuse[rsa->num_blindings] = 1;
278 *index_used = rsa->num_blindings;
279
David Benjamind8b65c82015-04-22 16:09:09 -0400280 OPENSSL_free(rsa->blindings);
Adam Langley95c29f32014-06-20 12:00:00 -0700281 rsa->blindings = new_blindings;
David Benjamind8b65c82015-04-22 16:09:09 -0400282 OPENSSL_free(rsa->blindings_inuse);
Adam Langley95c29f32014-06-20 12:00:00 -0700283 rsa->blindings_inuse = new_blindings_inuse;
284 rsa->num_blindings++;
285
Adam Langley683d7bd2015-04-13 11:04:14 -0700286 CRYPTO_MUTEX_unlock(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700287 return ret;
288
289err2:
290 OPENSSL_free(new_blindings);
291
292err1:
Adam Langley683d7bd2015-04-13 11:04:14 -0700293 CRYPTO_MUTEX_unlock(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700294 BN_BLINDING_free(ret);
295 return NULL;
296}
297
298/* rsa_blinding_release marks the cached BN_BLINDING at the given index as free
299 * for other threads to use. */
300static void rsa_blinding_release(RSA *rsa, BN_BLINDING *blinding,
301 unsigned blinding_index) {
302 if (blinding_index == MAX_BLINDINGS_PER_RSA) {
303 /* This blinding wasn't cached. */
304 BN_BLINDING_free(blinding);
305 return;
306 }
307
Adam Langley683d7bd2015-04-13 11:04:14 -0700308 CRYPTO_MUTEX_lock_write(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700309 rsa->blindings_inuse[blinding_index] = 0;
Adam Langley683d7bd2015-04-13 11:04:14 -0700310 CRYPTO_MUTEX_unlock(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700311}
312
313/* signing */
314static int sign_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
315 const uint8_t *in, size_t in_len, int padding) {
316 const unsigned rsa_size = RSA_size(rsa);
Adam Langley95c29f32014-06-20 12:00:00 -0700317 uint8_t *buf = NULL;
Adam Langley6887edb2014-06-20 12:00:00 -0700318 int i, ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700319
320 if (max_out < rsa_size) {
321 OPENSSL_PUT_ERROR(RSA, sign_raw, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
322 return 0;
323 }
324
Adam Langley95c29f32014-06-20 12:00:00 -0700325 buf = OPENSSL_malloc(rsa_size);
Adam Langley6bc658d2014-08-18 13:29:45 -0700326 if (buf == NULL) {
Adam Langley95c29f32014-06-20 12:00:00 -0700327 OPENSSL_PUT_ERROR(RSA, sign_raw, ERR_R_MALLOC_FAILURE);
328 goto err;
329 }
330
331 switch (padding) {
332 case RSA_PKCS1_PADDING:
333 i = RSA_padding_add_PKCS1_type_1(buf, rsa_size, in, in_len);
334 break;
335 case RSA_NO_PADDING:
336 i = RSA_padding_add_none(buf, rsa_size, in, in_len);
337 break;
338 default:
339 OPENSSL_PUT_ERROR(RSA, sign_raw, RSA_R_UNKNOWN_PADDING_TYPE);
340 goto err;
341 }
Adam Langley6bc658d2014-08-18 13:29:45 -0700342
Adam Langley95c29f32014-06-20 12:00:00 -0700343 if (i <= 0) {
344 goto err;
345 }
346
Adam Langley6bc658d2014-08-18 13:29:45 -0700347 if (!RSA_private_transform(rsa, out, buf, rsa_size)) {
Adam Langley5f5bf6f2015-02-24 13:49:41 -0800348 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700349 }
350
351 *out_len = rsa_size;
352 ret = 1;
353
354err:
Adam Langley95c29f32014-06-20 12:00:00 -0700355 if (buf != NULL) {
356 OPENSSL_cleanse(buf, rsa_size);
357 OPENSSL_free(buf);
358 }
Adam Langley95c29f32014-06-20 12:00:00 -0700359
360 return ret;
361}
362
363static int decrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
364 const uint8_t *in, size_t in_len, int padding) {
365 const unsigned rsa_size = RSA_size(rsa);
Adam Langley95c29f32014-06-20 12:00:00 -0700366 int r = -1;
367 uint8_t *buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700368 int ret = 0;
369
370 if (max_out < rsa_size) {
371 OPENSSL_PUT_ERROR(RSA, decrypt, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
372 return 0;
373 }
374
Adam Langley95c29f32014-06-20 12:00:00 -0700375 buf = OPENSSL_malloc(rsa_size);
Adam Langley6bc658d2014-08-18 13:29:45 -0700376 if (buf == NULL) {
Adam Langley95c29f32014-06-20 12:00:00 -0700377 OPENSSL_PUT_ERROR(RSA, decrypt, ERR_R_MALLOC_FAILURE);
378 goto err;
379 }
380
Adam Langley6bc658d2014-08-18 13:29:45 -0700381 if (in_len != rsa_size) {
382 OPENSSL_PUT_ERROR(RSA, decrypt, RSA_R_DATA_LEN_NOT_EQUAL_TO_MOD_LEN);
Adam Langley95c29f32014-06-20 12:00:00 -0700383 goto err;
384 }
385
Adam Langley6bc658d2014-08-18 13:29:45 -0700386 if (!RSA_private_transform(rsa, buf, in, rsa_size)) {
Adam Langley6887edb2014-06-20 12:00:00 -0700387 goto err;
388 }
Adam Langley95c29f32014-06-20 12:00:00 -0700389
390 switch (padding) {
391 case RSA_PKCS1_PADDING:
Adam Langley6887edb2014-06-20 12:00:00 -0700392 r = RSA_padding_check_PKCS1_type_2(out, rsa_size, buf, rsa_size);
Adam Langley95c29f32014-06-20 12:00:00 -0700393 break;
394 case RSA_PKCS1_OAEP_PADDING:
Adam Langley6887edb2014-06-20 12:00:00 -0700395 /* Use the default parameters: SHA-1 for both hashes and no label. */
396 r = RSA_padding_check_PKCS1_OAEP_mgf1(out, rsa_size, buf, rsa_size,
397 NULL, 0, NULL, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700398 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700399 case RSA_NO_PADDING:
Adam Langley6887edb2014-06-20 12:00:00 -0700400 r = RSA_padding_check_none(out, rsa_size, buf, rsa_size);
Adam Langley95c29f32014-06-20 12:00:00 -0700401 break;
402 default:
403 OPENSSL_PUT_ERROR(RSA, decrypt, RSA_R_UNKNOWN_PADDING_TYPE);
404 goto err;
405 }
406
407 if (r < 0) {
408 OPENSSL_PUT_ERROR(RSA, decrypt, RSA_R_PADDING_CHECK_FAILED);
409 } else {
410 *out_len = r;
411 ret = 1;
412 }
413
414err:
Adam Langley95c29f32014-06-20 12:00:00 -0700415 if (buf != NULL) {
416 OPENSSL_cleanse(buf, rsa_size);
417 OPENSSL_free(buf);
418 }
Adam Langley95c29f32014-06-20 12:00:00 -0700419
420 return ret;
421}
422
423static int verify_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
424 const uint8_t *in, size_t in_len, int padding) {
425 const unsigned rsa_size = RSA_size(rsa);
426 BIGNUM *f, *result;
427 int ret = 0;
Adam Langley6887edb2014-06-20 12:00:00 -0700428 int r = -1;
Adam Langley95c29f32014-06-20 12:00:00 -0700429 uint8_t *buf = NULL;
430 BN_CTX *ctx = NULL;
431
432 if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
433 OPENSSL_PUT_ERROR(RSA, verify_raw, RSA_R_MODULUS_TOO_LARGE);
434 return 0;
435 }
436
437 if (BN_ucmp(rsa->n, rsa->e) <= 0) {
438 OPENSSL_PUT_ERROR(RSA, verify_raw, RSA_R_BAD_E_VALUE);
439 return 0;
440 }
441
442 if (max_out < rsa_size) {
443 OPENSSL_PUT_ERROR(RSA, verify_raw, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
444 return 0;
445 }
446
447 /* for large moduli, enforce exponent limit */
448 if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS &&
449 BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
450 OPENSSL_PUT_ERROR(RSA, verify_raw, RSA_R_BAD_E_VALUE);
451 return 0;
452 }
453
454 ctx = BN_CTX_new();
455 if (ctx == NULL) {
456 goto err;
457 }
458
459 BN_CTX_start(ctx);
460 f = BN_CTX_get(ctx);
461 result = BN_CTX_get(ctx);
462 buf = OPENSSL_malloc(rsa_size);
463 if (!f || !result || !buf) {
464 OPENSSL_PUT_ERROR(RSA, verify_raw, ERR_R_MALLOC_FAILURE);
465 goto err;
466 }
467
Adam Langley6bc658d2014-08-18 13:29:45 -0700468 if (in_len != rsa_size) {
469 OPENSSL_PUT_ERROR(RSA, verify_raw, RSA_R_DATA_LEN_NOT_EQUAL_TO_MOD_LEN);
Adam Langley95c29f32014-06-20 12:00:00 -0700470 goto err;
471 }
472
473 if (BN_bin2bn(in, in_len, f) == NULL) {
474 goto err;
475 }
476
477 if (BN_ucmp(f, rsa->n) >= 0) {
478 OPENSSL_PUT_ERROR(RSA, verify_raw, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
479 goto err;
480 }
481
482 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) {
Adam Langley683d7bd2015-04-13 11:04:14 -0700483 if (BN_MONT_CTX_set_locked(&rsa->_method_mod_n, &rsa->lock, rsa->n, ctx) ==
484 NULL) {
Adam Langley95c29f32014-06-20 12:00:00 -0700485 goto err;
486 }
487 }
488
489 if (!rsa->meth->bn_mod_exp(result, f, rsa->e, rsa->n, ctx,
490 rsa->_method_mod_n)) {
491 goto err;
492 }
493
Adam Langley6887edb2014-06-20 12:00:00 -0700494 if (!BN_bn2bin_padded(buf, rsa_size, result)) {
495 OPENSSL_PUT_ERROR(RSA, verify_raw, ERR_R_INTERNAL_ERROR);
496 goto err;
497 }
Adam Langley95c29f32014-06-20 12:00:00 -0700498
499 switch (padding) {
500 case RSA_PKCS1_PADDING:
Adam Langley6887edb2014-06-20 12:00:00 -0700501 r = RSA_padding_check_PKCS1_type_1(out, rsa_size, buf, rsa_size);
Adam Langley95c29f32014-06-20 12:00:00 -0700502 break;
503 case RSA_NO_PADDING:
Adam Langley6887edb2014-06-20 12:00:00 -0700504 r = RSA_padding_check_none(out, rsa_size, buf, rsa_size);
Adam Langley95c29f32014-06-20 12:00:00 -0700505 break;
506 default:
507 OPENSSL_PUT_ERROR(RSA, verify_raw, RSA_R_UNKNOWN_PADDING_TYPE);
508 goto err;
509 }
510
511 if (r < 0) {
512 OPENSSL_PUT_ERROR(RSA, verify_raw, RSA_R_PADDING_CHECK_FAILED);
513 } else {
514 *out_len = r;
515 ret = 1;
516 }
517
518err:
519 if (ctx != NULL) {
520 BN_CTX_end(ctx);
521 BN_CTX_free(ctx);
522 }
523 if (buf != NULL) {
524 OPENSSL_cleanse(buf, rsa_size);
525 OPENSSL_free(buf);
526 }
527 return ret;
528}
529
Adam Langley6bc658d2014-08-18 13:29:45 -0700530static int private_transform(RSA *rsa, uint8_t *out, const uint8_t *in,
531 size_t len) {
532 BIGNUM *f, *result;
533 BN_CTX *ctx = NULL;
534 unsigned blinding_index = 0;
535 BN_BLINDING *blinding = NULL;
536 int ret = 0;
537
538 ctx = BN_CTX_new();
539 if (ctx == NULL) {
540 goto err;
541 }
542 BN_CTX_start(ctx);
543 f = BN_CTX_get(ctx);
544 result = BN_CTX_get(ctx);
545
546 if (f == NULL || result == NULL) {
547 OPENSSL_PUT_ERROR(RSA, private_transform, ERR_R_MALLOC_FAILURE);
548 goto err;
549 }
550
551 if (BN_bin2bn(in, len, f) == NULL) {
552 goto err;
553 }
554
555 if (BN_ucmp(f, rsa->n) >= 0) {
556 /* Usually the padding functions would catch this. */
557 OPENSSL_PUT_ERROR(RSA, private_transform, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
558 goto err;
559 }
560
561 if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
562 blinding = rsa_blinding_get(rsa, &blinding_index, ctx);
563 if (blinding == NULL) {
564 OPENSSL_PUT_ERROR(RSA, private_transform, ERR_R_INTERNAL_ERROR);
565 goto err;
566 }
567 if (!BN_BLINDING_convert_ex(f, NULL, blinding, ctx)) {
568 goto err;
569 }
570 }
571
572 if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
573 ((rsa->p != NULL) && (rsa->q != NULL) && (rsa->dmp1 != NULL) &&
574 (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
575 if (!rsa->meth->mod_exp(result, f, rsa, ctx)) {
576 goto err;
577 }
578 } else {
579 BIGNUM local_d;
580 BIGNUM *d = NULL;
581
582 BN_init(&local_d);
583 d = &local_d;
584 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
585
586 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) {
Adam Langley683d7bd2015-04-13 11:04:14 -0700587 if (BN_MONT_CTX_set_locked(&rsa->_method_mod_n, &rsa->lock, rsa->n,
588 ctx) == NULL) {
Adam Langley6bc658d2014-08-18 13:29:45 -0700589 goto err;
590 }
591 }
592
593 if (!rsa->meth->bn_mod_exp(result, f, d, rsa->n, ctx, rsa->_method_mod_n)) {
594 goto err;
595 }
596 }
597
598 if (blinding) {
599 if (!BN_BLINDING_invert_ex(result, NULL, blinding, ctx)) {
600 goto err;
601 }
602 }
603
604 if (!BN_bn2bin_padded(out, len, result)) {
605 OPENSSL_PUT_ERROR(RSA, private_transform, ERR_R_INTERNAL_ERROR);
606 goto err;
607 }
608
609 ret = 1;
610
611err:
612 if (ctx != NULL) {
613 BN_CTX_end(ctx);
614 BN_CTX_free(ctx);
615 }
616 if (blinding != NULL) {
617 rsa_blinding_release(rsa, blinding, blinding_index);
618 }
619
620 return ret;
621}
622
Adam Langley95c29f32014-06-20 12:00:00 -0700623static int mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) {
624 BIGNUM *r1, *m1, *vrfy;
625 BIGNUM local_dmp1, local_dmq1, local_c, local_r1;
626 BIGNUM *dmp1, *dmq1, *c, *pr1;
627 int ret = 0;
Adam Langley839b8812015-05-26 11:36:46 -0700628 size_t i, num_additional_primes = 0;
629
630 if (rsa->additional_primes != NULL) {
631 num_additional_primes = sk_RSA_additional_prime_num(rsa->additional_primes);
632 }
Adam Langley95c29f32014-06-20 12:00:00 -0700633
634 BN_CTX_start(ctx);
635 r1 = BN_CTX_get(ctx);
636 m1 = BN_CTX_get(ctx);
637 vrfy = BN_CTX_get(ctx);
638
639 {
640 BIGNUM local_p, local_q;
641 BIGNUM *p = NULL, *q = NULL;
642
643 /* Make sure BN_mod_inverse in Montgomery intialization uses the
644 * BN_FLG_CONSTTIME flag (unless RSA_FLAG_NO_CONSTTIME is set) */
645 BN_init(&local_p);
646 p = &local_p;
647 BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
648
649 BN_init(&local_q);
650 q = &local_q;
651 BN_with_flags(q, rsa->q, BN_FLG_CONSTTIME);
652
653 if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {
Adam Langley683d7bd2015-04-13 11:04:14 -0700654 if (BN_MONT_CTX_set_locked(&rsa->_method_mod_p, &rsa->lock, p, ctx) ==
655 NULL) {
Adam Langley95c29f32014-06-20 12:00:00 -0700656 goto err;
657 }
Adam Langley683d7bd2015-04-13 11:04:14 -0700658 if (BN_MONT_CTX_set_locked(&rsa->_method_mod_q, &rsa->lock, q, ctx) ==
659 NULL) {
Adam Langley95c29f32014-06-20 12:00:00 -0700660 goto err;
661 }
662 }
663 }
664
665 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) {
Adam Langley683d7bd2015-04-13 11:04:14 -0700666 if (BN_MONT_CTX_set_locked(&rsa->_method_mod_n, &rsa->lock, rsa->n, ctx) ==
667 NULL) {
Adam Langley95c29f32014-06-20 12:00:00 -0700668 goto err;
669 }
670 }
671
672 /* compute I mod q */
673 c = &local_c;
674 BN_with_flags(c, I, BN_FLG_CONSTTIME);
675 if (!BN_mod(r1, c, rsa->q, ctx)) {
676 goto err;
677 }
678
679 /* compute r1^dmq1 mod q */
680 dmq1 = &local_dmq1;
681 BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
682 if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx, rsa->_method_mod_q)) {
683 goto err;
684 }
685
686 /* compute I mod p */
687 c = &local_c;
688 BN_with_flags(c, I, BN_FLG_CONSTTIME);
689 if (!BN_mod(r1, c, rsa->p, ctx)) {
690 goto err;
691 }
692
693 /* compute r1^dmp1 mod p */
694 dmp1 = &local_dmp1;
695 BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
696 if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx, rsa->_method_mod_p)) {
697 goto err;
698 }
699
700 if (!BN_sub(r0, r0, m1)) {
701 goto err;
702 }
703 /* This will help stop the size of r0 increasing, which does
704 * affect the multiply if it optimised for a power of 2 size */
705 if (BN_is_negative(r0)) {
706 if (!BN_add(r0, r0, rsa->p)) {
707 goto err;
708 }
709 }
710
711 if (!BN_mul(r1, r0, rsa->iqmp, ctx)) {
712 goto err;
713 }
714
715 /* Turn BN_FLG_CONSTTIME flag on before division operation */
716 pr1 = &local_r1;
717 BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
718
719 if (!BN_mod(r0, pr1, rsa->p, ctx)) {
720 goto err;
721 }
722
723 /* If p < q it is occasionally possible for the correction of
724 * adding 'p' if r0 is negative above to leave the result still
725 * negative. This can break the private key operations: the following
726 * second correction should *always* correct this rare occurrence.
727 * This will *never* happen with OpenSSL generated keys because
728 * they ensure p > q [steve] */
729 if (BN_is_negative(r0)) {
730 if (!BN_add(r0, r0, rsa->p)) {
731 goto err;
732 }
733 }
734 if (!BN_mul(r1, r0, rsa->q, ctx)) {
735 goto err;
736 }
737 if (!BN_add(r0, r1, m1)) {
738 goto err;
739 }
740
Adam Langley839b8812015-05-26 11:36:46 -0700741 for (i = 0; i < num_additional_primes; i++) {
742 /* multi-prime RSA. */
743 BIGNUM local_exp, local_prime;
744 BIGNUM *exp = &local_exp, *prime = &local_prime;
745 RSA_additional_prime *ap =
746 sk_RSA_additional_prime_value(rsa->additional_primes, i);
747
748 BN_with_flags(exp, ap->exp, BN_FLG_CONSTTIME);
749 BN_with_flags(prime, ap->prime, BN_FLG_CONSTTIME);
750
751 /* c will already point to a BIGNUM with the correct flags. */
752 if (!BN_mod(r1, c, prime, ctx)) {
753 goto err;
754 }
755
756 if ((rsa->flags & RSA_FLAG_CACHE_PRIVATE) &&
757 !BN_MONT_CTX_set_locked(&ap->method_mod, &rsa->lock, prime, ctx)) {
758 goto err;
759 }
760
761 if (!rsa->meth->bn_mod_exp(m1, r1, exp, prime, ctx, ap->method_mod)) {
762 goto err;
763 }
764
765 BN_set_flags(m1, BN_FLG_CONSTTIME);
766
767 if (!BN_sub(m1, m1, r0) ||
768 !BN_mul(m1, m1, ap->coeff, ctx) ||
769 !BN_mod(m1, m1, prime, ctx) ||
770 (BN_is_negative(m1) && !BN_add(m1, m1, prime)) ||
771 !BN_mul(m1, m1, ap->r, ctx) ||
772 !BN_add(r0, r0, m1)) {
773 goto err;
774 }
775 }
776
Adam Langley95c29f32014-06-20 12:00:00 -0700777 if (rsa->e && rsa->n) {
778 if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,
779 rsa->_method_mod_n)) {
780 goto err;
781 }
782 /* If 'I' was greater than (or equal to) rsa->n, the operation
783 * will be equivalent to using 'I mod n'. However, the result of
784 * the verify will *always* be less than 'n' so we don't check
785 * for absolute equality, just congruency. */
786 if (!BN_sub(vrfy, vrfy, I)) {
787 goto err;
788 }
789 if (!BN_mod(vrfy, vrfy, rsa->n, ctx)) {
790 goto err;
791 }
792 if (BN_is_negative(vrfy)) {
793 if (!BN_add(vrfy, vrfy, rsa->n)) {
794 goto err;
795 }
796 }
797 if (!BN_is_zero(vrfy)) {
798 /* 'I' and 'vrfy' aren't congruent mod n. Don't leak
799 * miscalculated CRT output, just do a raw (slower)
800 * mod_exp and return that instead. */
801
802 BIGNUM local_d;
803 BIGNUM *d = NULL;
804
805 d = &local_d;
806 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
807 if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx, rsa->_method_mod_n)) {
808 goto err;
809 }
810 }
811 }
812 ret = 1;
813
814err:
815 BN_CTX_end(ctx);
816 return ret;
817}
818
Adam Langley839b8812015-05-26 11:36:46 -0700819static int keygen_multiprime(RSA *rsa, int bits, int num_primes,
820 BIGNUM *e_value, BN_GENCB *cb) {
Adam Langley95c29f32014-06-20 12:00:00 -0700821 BIGNUM *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL, *tmp;
822 BIGNUM local_r0, local_d, local_p;
823 BIGNUM *pr0, *d, *p;
Adam Langley839b8812015-05-26 11:36:46 -0700824 int prime_bits, ok = -1, n = 0, i, j;
Adam Langley95c29f32014-06-20 12:00:00 -0700825 BN_CTX *ctx = NULL;
Adam Langley839b8812015-05-26 11:36:46 -0700826 STACK_OF(RSA_additional_prime) *additional_primes = NULL;
827
828 if (num_primes < 2) {
829 ok = 0; /* we set our own err */
830 OPENSSL_PUT_ERROR(RSA, keygen_multiprime, RSA_R_MUST_HAVE_AT_LEAST_TWO_PRIMES);
831 goto err;
832 }
Adam Langley95c29f32014-06-20 12:00:00 -0700833
834 ctx = BN_CTX_new();
835 if (ctx == NULL) {
836 goto err;
837 }
838 BN_CTX_start(ctx);
839 r0 = BN_CTX_get(ctx);
840 r1 = BN_CTX_get(ctx);
841 r2 = BN_CTX_get(ctx);
842 r3 = BN_CTX_get(ctx);
843 if (r3 == NULL) {
844 goto err;
845 }
846
Adam Langley839b8812015-05-26 11:36:46 -0700847 if (num_primes > 2) {
848 additional_primes = sk_RSA_additional_prime_new_null();
849 if (additional_primes == NULL) {
850 goto err;
851 }
852 }
853
854 for (i = 2; i < num_primes; i++) {
855 RSA_additional_prime *ap = OPENSSL_malloc(sizeof(RSA_additional_prime));
856 if (ap == NULL) {
857 goto err;
858 }
859 memset(ap, 0, sizeof(RSA_additional_prime));
860 ap->prime = BN_new();
861 ap->exp = BN_new();
862 ap->coeff = BN_new();
863 ap->r = BN_new();
864 if (ap->prime == NULL ||
865 ap->exp == NULL ||
866 ap->coeff == NULL ||
867 ap->r == NULL ||
868 !sk_RSA_additional_prime_push(additional_primes, ap)) {
869 RSA_additional_prime_free(ap);
870 goto err;
871 }
872 }
Adam Langley95c29f32014-06-20 12:00:00 -0700873
874 /* We need the RSA components non-NULL */
David Benjamin6eb000d2015-02-11 01:17:41 -0500875 if (!rsa->n && ((rsa->n = BN_new()) == NULL)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700876 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500877 }
878 if (!rsa->d && ((rsa->d = BN_new()) == NULL)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700879 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500880 }
881 if (!rsa->e && ((rsa->e = BN_new()) == NULL)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700882 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500883 }
884 if (!rsa->p && ((rsa->p = BN_new()) == NULL)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700885 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500886 }
887 if (!rsa->q && ((rsa->q = BN_new()) == NULL)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700888 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500889 }
890 if (!rsa->dmp1 && ((rsa->dmp1 = BN_new()) == NULL)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700891 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500892 }
893 if (!rsa->dmq1 && ((rsa->dmq1 = BN_new()) == NULL)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700894 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500895 }
896 if (!rsa->iqmp && ((rsa->iqmp = BN_new()) == NULL)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700897 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500898 }
Adam Langley95c29f32014-06-20 12:00:00 -0700899
David Benjamin1c703cb2015-06-11 21:42:14 -0400900 if (!BN_copy(rsa->e, e_value)) {
901 goto err;
902 }
Adam Langley95c29f32014-06-20 12:00:00 -0700903
904 /* generate p and q */
Adam Langley839b8812015-05-26 11:36:46 -0700905 prime_bits = (bits + (num_primes - 1)) / num_primes;
Adam Langley95c29f32014-06-20 12:00:00 -0700906 for (;;) {
Adam Langley839b8812015-05-26 11:36:46 -0700907 if (!BN_generate_prime_ex(rsa->p, prime_bits, 0, NULL, NULL, cb) ||
David Benjamin6eb000d2015-02-11 01:17:41 -0500908 !BN_sub(r2, rsa->p, BN_value_one()) ||
909 !BN_gcd(r1, r2, rsa->e, ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700910 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500911 }
912 if (BN_is_one(r1)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700913 break;
David Benjamin6eb000d2015-02-11 01:17:41 -0500914 }
915 if (!BN_GENCB_call(cb, 2, n++)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700916 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500917 }
Adam Langley95c29f32014-06-20 12:00:00 -0700918 }
David Benjamin6eb000d2015-02-11 01:17:41 -0500919 if (!BN_GENCB_call(cb, 3, 0)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700920 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500921 }
Adam Langley839b8812015-05-26 11:36:46 -0700922 prime_bits = ((bits - prime_bits) + (num_primes - 2)) / (num_primes - 1);
Adam Langley95c29f32014-06-20 12:00:00 -0700923 for (;;) {
924 /* When generating ridiculously small keys, we can get stuck
925 * continually regenerating the same prime values. Check for
926 * this and bail if it happens 3 times. */
927 unsigned int degenerate = 0;
928 do {
Adam Langley839b8812015-05-26 11:36:46 -0700929 if (!BN_generate_prime_ex(rsa->q, prime_bits, 0, NULL, NULL, cb)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700930 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500931 }
Adam Langley95c29f32014-06-20 12:00:00 -0700932 } while ((BN_cmp(rsa->p, rsa->q) == 0) && (++degenerate < 3));
933 if (degenerate == 3) {
934 ok = 0; /* we set our own err */
Adam Langley839b8812015-05-26 11:36:46 -0700935 OPENSSL_PUT_ERROR(RSA, keygen_multiprime, RSA_R_KEY_SIZE_TOO_SMALL);
Adam Langley95c29f32014-06-20 12:00:00 -0700936 goto err;
937 }
David Benjamin6eb000d2015-02-11 01:17:41 -0500938 if (!BN_sub(r2, rsa->q, BN_value_one()) ||
939 !BN_gcd(r1, r2, rsa->e, ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700940 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500941 }
942 if (BN_is_one(r1)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700943 break;
David Benjamin6eb000d2015-02-11 01:17:41 -0500944 }
945 if (!BN_GENCB_call(cb, 2, n++)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700946 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500947 }
Adam Langley95c29f32014-06-20 12:00:00 -0700948 }
Adam Langley839b8812015-05-26 11:36:46 -0700949
950 if (!BN_GENCB_call(cb, 3, 1) ||
951 !BN_mul(rsa->n, rsa->p, rsa->q, ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700952 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500953 }
Adam Langley839b8812015-05-26 11:36:46 -0700954
955 for (i = 2; i < num_primes; i++) {
956 RSA_additional_prime *ap =
957 sk_RSA_additional_prime_value(additional_primes, i - 2);
958 prime_bits = ((bits - BN_num_bits(rsa->n)) + (num_primes - (i + 1))) /
959 (num_primes - i);
960
961 for (;;) {
962 if (!BN_generate_prime_ex(ap->prime, prime_bits, 0, NULL, NULL, cb)) {
963 goto err;
964 }
965 if (BN_cmp(rsa->p, ap->prime) == 0 ||
966 BN_cmp(rsa->q, ap->prime) == 0) {
967 continue;
968 }
969
970 for (j = 0; j < i - 2; j++) {
971 if (BN_cmp(sk_RSA_additional_prime_value(additional_primes, j)->prime,
972 ap->prime) == 0) {
973 break;
974 }
975 }
976 if (j != i - 2) {
977 continue;
978 }
979
980 if (!BN_sub(r2, ap->prime, BN_value_one()) ||
981 !BN_gcd(r1, r2, rsa->e, ctx)) {
982 goto err;
983 }
984
985 if (!BN_is_one(r1)) {
986 continue;
987 }
988 if (i != num_primes - 1) {
989 break;
990 }
991
992 /* For the last prime we'll check that it makes n large enough. In the
993 * two prime case this isn't a problem because we generate primes with
994 * the top two bits set and so the product is always of the expected
995 * size. In the multi prime case, this doesn't follow. */
996 if (!BN_mul(r1, rsa->n, ap->prime, ctx)) {
997 goto err;
998 }
999 if (BN_num_bits(r1) == bits) {
1000 break;
1001 }
1002
1003 if (!BN_GENCB_call(cb, 2, n++)) {
1004 goto err;
1005 }
1006 }
1007
1008 /* ap->r is is the product of all the primes prior to the current one
1009 * (including p and q). */
1010 if (!BN_copy(ap->r, rsa->n)) {
1011 goto err;
1012 }
1013 if (i == num_primes - 1) {
1014 /* In the case of the last prime, we calculated n as |r1| in the loop
1015 * above. */
1016 if (!BN_copy(rsa->n, r1)) {
1017 goto err;
1018 }
1019 } else if (!BN_mul(rsa->n, rsa->n, ap->prime, ctx)) {
1020 goto err;
1021 }
1022
1023 if (!BN_GENCB_call(cb, 3, 1)) {
1024 goto err;
1025 }
1026 }
1027
Adam Langley95c29f32014-06-20 12:00:00 -07001028 if (BN_cmp(rsa->p, rsa->q) < 0) {
1029 tmp = rsa->p;
1030 rsa->p = rsa->q;
1031 rsa->q = tmp;
1032 }
1033
Adam Langley95c29f32014-06-20 12:00:00 -07001034 /* calculate d */
David Benjamin6eb000d2015-02-11 01:17:41 -05001035 if (!BN_sub(r1, rsa->p, BN_value_one())) {
Adam Langley95c29f32014-06-20 12:00:00 -07001036 goto err; /* p-1 */
David Benjamin6eb000d2015-02-11 01:17:41 -05001037 }
1038 if (!BN_sub(r2, rsa->q, BN_value_one())) {
Adam Langley95c29f32014-06-20 12:00:00 -07001039 goto err; /* q-1 */
David Benjamin6eb000d2015-02-11 01:17:41 -05001040 }
1041 if (!BN_mul(r0, r1, r2, ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -07001042 goto err; /* (p-1)(q-1) */
David Benjamin6eb000d2015-02-11 01:17:41 -05001043 }
Adam Langley839b8812015-05-26 11:36:46 -07001044 for (i = 2; i < num_primes; i++) {
1045 RSA_additional_prime *ap =
1046 sk_RSA_additional_prime_value(additional_primes, i - 2);
1047 if (!BN_sub(r3, ap->prime, BN_value_one()) ||
1048 !BN_mul(r0, r0, r3, ctx)) {
1049 goto err;
1050 }
1051 }
Adam Langley95c29f32014-06-20 12:00:00 -07001052 pr0 = &local_r0;
1053 BN_with_flags(pr0, r0, BN_FLG_CONSTTIME);
David Benjamin6eb000d2015-02-11 01:17:41 -05001054 if (!BN_mod_inverse(rsa->d, rsa->e, pr0, ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -07001055 goto err; /* d */
David Benjamin6eb000d2015-02-11 01:17:41 -05001056 }
Adam Langley95c29f32014-06-20 12:00:00 -07001057
1058 /* set up d for correct BN_FLG_CONSTTIME flag */
1059 d = &local_d;
1060 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
1061
1062 /* calculate d mod (p-1) */
David Benjamin6eb000d2015-02-11 01:17:41 -05001063 if (!BN_mod(rsa->dmp1, d, r1, ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -07001064 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -05001065 }
Adam Langley95c29f32014-06-20 12:00:00 -07001066
1067 /* calculate d mod (q-1) */
David Benjamin6eb000d2015-02-11 01:17:41 -05001068 if (!BN_mod(rsa->dmq1, d, r2, ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -07001069 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -05001070 }
Adam Langley95c29f32014-06-20 12:00:00 -07001071
1072 /* calculate inverse of q mod p */
1073 p = &local_p;
1074 BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
1075
David Benjamin6eb000d2015-02-11 01:17:41 -05001076 if (!BN_mod_inverse(rsa->iqmp, rsa->q, p, ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -07001077 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -05001078 }
Adam Langley95c29f32014-06-20 12:00:00 -07001079
Adam Langley839b8812015-05-26 11:36:46 -07001080 for (i = 2; i < num_primes; i++) {
1081 RSA_additional_prime *ap =
1082 sk_RSA_additional_prime_value(additional_primes, i - 2);
1083 if (!BN_sub(ap->exp, ap->prime, BN_value_one()) ||
1084 !BN_mod(ap->exp, rsa->d, ap->exp, ctx) ||
1085 !BN_mod_inverse(ap->coeff, ap->r, ap->prime, ctx)) {
1086 goto err;
1087 }
1088 }
1089
Adam Langley95c29f32014-06-20 12:00:00 -07001090 ok = 1;
Adam Langley839b8812015-05-26 11:36:46 -07001091 rsa->additional_primes = additional_primes;
1092 additional_primes = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001093
1094err:
1095 if (ok == -1) {
Adam Langley839b8812015-05-26 11:36:46 -07001096 OPENSSL_PUT_ERROR(RSA, keygen_multiprime, ERR_LIB_BN);
Adam Langley95c29f32014-06-20 12:00:00 -07001097 ok = 0;
1098 }
1099 if (ctx != NULL) {
1100 BN_CTX_end(ctx);
1101 BN_CTX_free(ctx);
1102 }
Adam Langley839b8812015-05-26 11:36:46 -07001103 sk_RSA_additional_prime_pop_free(additional_primes,
1104 RSA_additional_prime_free);
Adam Langley95c29f32014-06-20 12:00:00 -07001105 return ok;
1106}
1107
Adam Langley839b8812015-05-26 11:36:46 -07001108static int keygen(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) {
1109 return keygen_multiprime(rsa, bits, 2 /* num primes */, e_value, cb);
1110}
1111
Adam Langley95c29f32014-06-20 12:00:00 -07001112const struct rsa_meth_st RSA_default_method = {
1113 {
1114 0 /* references */,
1115 1 /* is_static */,
1116 },
1117 NULL /* app_data */,
1118
1119 NULL /* init */,
1120 finish,
1121
David Benjamin925fee32014-07-11 14:14:08 -04001122 size,
1123
Adam Langley95c29f32014-06-20 12:00:00 -07001124 NULL /* sign */,
1125 NULL /* verify */,
1126
1127 encrypt,
1128 sign_raw,
1129 decrypt,
1130 verify_raw,
1131
Adam Langley6bc658d2014-08-18 13:29:45 -07001132 private_transform,
1133
Adam Langley95c29f32014-06-20 12:00:00 -07001134 mod_exp /* mod_exp */,
1135 BN_mod_exp_mont /* bn_mod_exp */,
1136
1137 RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE,
1138
1139 keygen,
Adam Langley839b8812015-05-26 11:36:46 -07001140 keygen_multiprime,
Adam Langley95c29f32014-06-20 12:00:00 -07001141};