blob: b271b5623d2845a370e025e3a042b6c10828e770 [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 Benjamincfa9de82016-03-14 14:19:41 -040059#include <assert.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/err.h>
64#include <openssl/mem.h>
Brian Smith054e6822015-03-27 21:12:01 -100065#include <openssl/thread.h>
Adam Langley95c29f32014-06-20 12:00:00 -070066
67#include "internal.h"
Adam Langley683d7bd2015-04-13 11:04:14 -070068#include "../internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -070069
70
Brian Smith625475f2016-01-12 10:47:25 -100071static int check_modulus_and_exponent_sizes(const RSA *rsa) {
72 unsigned rsa_bits = BN_num_bits(rsa->n);
David Benjamincfa9de82016-03-14 14:19:41 -040073
Brian Smith625475f2016-01-12 10:47:25 -100074 if (rsa_bits > 16 * 1024) {
75 OPENSSL_PUT_ERROR(RSA, RSA_R_MODULUS_TOO_LARGE);
76 return 0;
77 }
Adam Langley95c29f32014-06-20 12:00:00 -070078
David Benjamincfa9de82016-03-14 14:19:41 -040079 /* Mitigate DoS attacks by limiting the exponent size. 33 bits was chosen as
80 * the limit based on the recommendations in [1] and [2]. Windows CryptoAPI
81 * doesn't support values larger than 32 bits [3], so it is unlikely that
82 * exponents larger than 32 bits are being used for anything Windows commonly
83 * does.
84 *
85 * [1] https://www.imperialviolet.org/2012/03/16/rsae.html
86 * [2] https://www.imperialviolet.org/2012/03/17/rsados.html
87 * [3] https://msdn.microsoft.com/en-us/library/aa387685(VS.85).aspx */
88 static const unsigned kMaxExponentBits = 33;
89
90 if (BN_num_bits(rsa->e) > kMaxExponentBits) {
Brian Smith625475f2016-01-12 10:47:25 -100091 OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_E_VALUE);
92 return 0;
93 }
94
David Benjamincfa9de82016-03-14 14:19:41 -040095 /* Verify |n > e|. Comparing |rsa_bits| to |kMaxExponentBits| is a small
96 * shortcut to comparing |n| and |e| directly. In reality, |kMaxExponentBits|
97 * is much smaller than the minimum RSA key size that any application should
98 * accept. */
99 if (rsa_bits <= kMaxExponentBits) {
100 OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL);
Brian Smith625475f2016-01-12 10:47:25 -1000101 return 0;
102 }
David Benjamincfa9de82016-03-14 14:19:41 -0400103 assert(BN_ucmp(rsa->n, rsa->e) > 0);
Brian Smith625475f2016-01-12 10:47:25 -1000104
105 return 1;
106}
Adam Langley95c29f32014-06-20 12:00:00 -0700107
David Benjamind93831d2015-10-29 13:19:12 -0400108size_t rsa_default_size(const RSA *rsa) {
David Benjamin925fee32014-07-11 14:14:08 -0400109 return BN_num_bytes(rsa->n);
110}
111
David Benjamind93831d2015-10-29 13:19:12 -0400112int rsa_default_encrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
113 const uint8_t *in, size_t in_len, int padding) {
Adam Langley95c29f32014-06-20 12:00:00 -0700114 const unsigned rsa_size = RSA_size(rsa);
Adam Langley95c29f32014-06-20 12:00:00 -0700115 BIGNUM *f, *result;
116 uint8_t *buf = NULL;
117 BN_CTX *ctx = NULL;
Adam Langley6887edb2014-06-20 12:00:00 -0700118 int i, ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700119
Adam Langley95c29f32014-06-20 12:00:00 -0700120 if (max_out < rsa_size) {
David Benjamin3570d732015-06-29 00:28:17 -0400121 OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
Adam Langley95c29f32014-06-20 12:00:00 -0700122 return 0;
123 }
124
Brian Smith625475f2016-01-12 10:47:25 -1000125 if (!check_modulus_and_exponent_sizes(rsa)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700126 return 0;
127 }
128
129 ctx = BN_CTX_new();
130 if (ctx == NULL) {
131 goto err;
132 }
133
134 BN_CTX_start(ctx);
135 f = BN_CTX_get(ctx);
136 result = BN_CTX_get(ctx);
137 buf = OPENSSL_malloc(rsa_size);
138 if (!f || !result || !buf) {
David Benjamin3570d732015-06-29 00:28:17 -0400139 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700140 goto err;
141 }
142
143 switch (padding) {
144 case RSA_PKCS1_PADDING:
145 i = RSA_padding_add_PKCS1_type_2(buf, rsa_size, in, in_len);
146 break;
147 case RSA_PKCS1_OAEP_PADDING:
Adam Langley6887edb2014-06-20 12:00:00 -0700148 /* Use the default parameters: SHA-1 for both hashes and no label. */
149 i = RSA_padding_add_PKCS1_OAEP_mgf1(buf, rsa_size, in, in_len,
150 NULL, 0, NULL, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700151 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700152 case RSA_NO_PADDING:
153 i = RSA_padding_add_none(buf, rsa_size, in, in_len);
154 break;
155 default:
David Benjamin3570d732015-06-29 00:28:17 -0400156 OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_PADDING_TYPE);
Adam Langley95c29f32014-06-20 12:00:00 -0700157 goto err;
158 }
159
160 if (i <= 0) {
161 goto err;
162 }
163
164 if (BN_bin2bn(buf, rsa_size, f) == NULL) {
165 goto err;
166 }
167
168 if (BN_ucmp(f, rsa->n) >= 0) {
169 /* usually the padding functions would catch this */
David Benjamin3570d732015-06-29 00:28:17 -0400170 OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
Adam Langley95c29f32014-06-20 12:00:00 -0700171 goto err;
172 }
173
Brian Smith24493a42016-03-25 09:12:48 -1000174 if (BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx) == NULL ||
175 !BN_mod_exp_mont(result, f, rsa->e, rsa->n, ctx, rsa->mont_n)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700176 goto err;
177 }
178
179 /* put in leading 0 bytes if the number is less than the length of the
180 * modulus */
Adam Langley6887edb2014-06-20 12:00:00 -0700181 if (!BN_bn2bin_padded(out, rsa_size, result)) {
David Benjamin3570d732015-06-29 00:28:17 -0400182 OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
Adam Langley6887edb2014-06-20 12:00:00 -0700183 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700184 }
185
186 *out_len = rsa_size;
187 ret = 1;
188
189err:
190 if (ctx != NULL) {
191 BN_CTX_end(ctx);
192 BN_CTX_free(ctx);
193 }
194 if (buf != NULL) {
195 OPENSSL_cleanse(buf, rsa_size);
196 OPENSSL_free(buf);
197 }
198
199 return ret;
200}
201
202/* MAX_BLINDINGS_PER_RSA defines the maximum number of cached BN_BLINDINGs per
203 * RSA*. Then this limit is exceeded, BN_BLINDING objects will be created and
204 * destroyed as needed. */
205#define MAX_BLINDINGS_PER_RSA 1024
206
207/* rsa_blinding_get returns a BN_BLINDING to use with |rsa|. It does this by
208 * allocating one of the cached BN_BLINDING objects in |rsa->blindings|. If
209 * none are free, the cache will be extended by a extra element and the new
210 * BN_BLINDING is returned.
211 *
212 * On success, the index of the assigned BN_BLINDING is written to
213 * |*index_used| and must be passed to |rsa_blinding_release| when finished. */
214static BN_BLINDING *rsa_blinding_get(RSA *rsa, unsigned *index_used,
215 BN_CTX *ctx) {
Brian Smithcbf56a52016-03-21 11:25:39 -1000216 assert(rsa->mont_n != NULL);
217
Adam Langley95c29f32014-06-20 12:00:00 -0700218 BN_BLINDING *ret = NULL;
219 BN_BLINDING **new_blindings;
220 uint8_t *new_blindings_inuse;
221 char overflow = 0;
222
Adam Langley683d7bd2015-04-13 11:04:14 -0700223 CRYPTO_MUTEX_lock_write(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700224
Adam Langley33672732015-03-31 18:55:53 -0700225 unsigned i;
226 for (i = 0; i < rsa->num_blindings; i++) {
227 if (rsa->blindings_inuse[i] == 0) {
228 rsa->blindings_inuse[i] = 1;
229 ret = rsa->blindings[i];
230 *index_used = i;
231 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700232 }
233 }
234
235 if (ret != NULL) {
Adam Langley683d7bd2015-04-13 11:04:14 -0700236 CRYPTO_MUTEX_unlock(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700237 return ret;
238 }
239
240 overflow = rsa->num_blindings >= MAX_BLINDINGS_PER_RSA;
241
242 /* We didn't find a free BN_BLINDING to use so increase the length of
243 * the arrays by one and use the newly created element. */
244
Adam Langley683d7bd2015-04-13 11:04:14 -0700245 CRYPTO_MUTEX_unlock(&rsa->lock);
Brian Smithcbf56a52016-03-21 11:25:39 -1000246 ret = BN_BLINDING_new(rsa, ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700247 if (ret == NULL) {
248 return NULL;
249 }
250
251 if (overflow) {
252 /* We cannot add any more cached BN_BLINDINGs so we use |ret|
253 * and mark it for destruction in |rsa_blinding_release|. */
254 *index_used = MAX_BLINDINGS_PER_RSA;
255 return ret;
256 }
257
Adam Langley683d7bd2015-04-13 11:04:14 -0700258 CRYPTO_MUTEX_lock_write(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700259
260 new_blindings =
261 OPENSSL_malloc(sizeof(BN_BLINDING *) * (rsa->num_blindings + 1));
262 if (new_blindings == NULL) {
263 goto err1;
264 }
265 memcpy(new_blindings, rsa->blindings,
266 sizeof(BN_BLINDING *) * rsa->num_blindings);
267 new_blindings[rsa->num_blindings] = ret;
268
269 new_blindings_inuse = OPENSSL_malloc(rsa->num_blindings + 1);
270 if (new_blindings_inuse == NULL) {
271 goto err2;
272 }
273 memcpy(new_blindings_inuse, rsa->blindings_inuse, rsa->num_blindings);
274 new_blindings_inuse[rsa->num_blindings] = 1;
275 *index_used = rsa->num_blindings;
276
David Benjamind8b65c82015-04-22 16:09:09 -0400277 OPENSSL_free(rsa->blindings);
Adam Langley95c29f32014-06-20 12:00:00 -0700278 rsa->blindings = new_blindings;
David Benjamind8b65c82015-04-22 16:09:09 -0400279 OPENSSL_free(rsa->blindings_inuse);
Adam Langley95c29f32014-06-20 12:00:00 -0700280 rsa->blindings_inuse = new_blindings_inuse;
281 rsa->num_blindings++;
282
Adam Langley683d7bd2015-04-13 11:04:14 -0700283 CRYPTO_MUTEX_unlock(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700284 return ret;
285
286err2:
287 OPENSSL_free(new_blindings);
288
289err1:
Adam Langley683d7bd2015-04-13 11:04:14 -0700290 CRYPTO_MUTEX_unlock(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700291 BN_BLINDING_free(ret);
292 return NULL;
293}
294
295/* rsa_blinding_release marks the cached BN_BLINDING at the given index as free
296 * for other threads to use. */
297static void rsa_blinding_release(RSA *rsa, BN_BLINDING *blinding,
298 unsigned blinding_index) {
299 if (blinding_index == MAX_BLINDINGS_PER_RSA) {
300 /* This blinding wasn't cached. */
301 BN_BLINDING_free(blinding);
302 return;
303 }
304
Adam Langley683d7bd2015-04-13 11:04:14 -0700305 CRYPTO_MUTEX_lock_write(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700306 rsa->blindings_inuse[blinding_index] = 0;
Adam Langley683d7bd2015-04-13 11:04:14 -0700307 CRYPTO_MUTEX_unlock(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700308}
309
310/* signing */
David Benjamind93831d2015-10-29 13:19:12 -0400311int rsa_default_sign_raw(RSA *rsa, size_t *out_len, uint8_t *out,
312 size_t max_out, const uint8_t *in, size_t in_len,
313 int padding) {
Adam Langley95c29f32014-06-20 12:00:00 -0700314 const unsigned rsa_size = RSA_size(rsa);
Adam Langley95c29f32014-06-20 12:00:00 -0700315 uint8_t *buf = NULL;
Adam Langley6887edb2014-06-20 12:00:00 -0700316 int i, ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700317
318 if (max_out < rsa_size) {
David Benjamin3570d732015-06-29 00:28:17 -0400319 OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
Adam Langley95c29f32014-06-20 12:00:00 -0700320 return 0;
321 }
322
Adam Langley95c29f32014-06-20 12:00:00 -0700323 buf = OPENSSL_malloc(rsa_size);
Adam Langley6bc658d2014-08-18 13:29:45 -0700324 if (buf == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400325 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700326 goto err;
327 }
328
329 switch (padding) {
330 case RSA_PKCS1_PADDING:
331 i = RSA_padding_add_PKCS1_type_1(buf, rsa_size, in, in_len);
332 break;
333 case RSA_NO_PADDING:
334 i = RSA_padding_add_none(buf, rsa_size, in, in_len);
335 break;
336 default:
David Benjamin3570d732015-06-29 00:28:17 -0400337 OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_PADDING_TYPE);
Adam Langley95c29f32014-06-20 12:00:00 -0700338 goto err;
339 }
Adam Langley6bc658d2014-08-18 13:29:45 -0700340
Adam Langley95c29f32014-06-20 12:00:00 -0700341 if (i <= 0) {
342 goto err;
343 }
344
Adam Langley6bc658d2014-08-18 13:29:45 -0700345 if (!RSA_private_transform(rsa, out, buf, rsa_size)) {
Adam Langley5f5bf6f2015-02-24 13:49:41 -0800346 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700347 }
348
349 *out_len = rsa_size;
350 ret = 1;
351
352err:
Adam Langley95c29f32014-06-20 12:00:00 -0700353 if (buf != NULL) {
354 OPENSSL_cleanse(buf, rsa_size);
355 OPENSSL_free(buf);
356 }
Adam Langley95c29f32014-06-20 12:00:00 -0700357
358 return ret;
359}
360
David Benjamind93831d2015-10-29 13:19:12 -0400361int rsa_default_decrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
362 const uint8_t *in, size_t in_len, int padding) {
Adam Langley95c29f32014-06-20 12:00:00 -0700363 const unsigned rsa_size = RSA_size(rsa);
Adam Langley95c29f32014-06-20 12:00:00 -0700364 int r = -1;
365 uint8_t *buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700366 int ret = 0;
367
368 if (max_out < rsa_size) {
David Benjamin3570d732015-06-29 00:28:17 -0400369 OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
Adam Langley95c29f32014-06-20 12:00:00 -0700370 return 0;
371 }
372
David Benjamin74279b62014-07-24 13:09:19 -0400373 if (padding == RSA_NO_PADDING) {
374 buf = out;
375 } else {
376 /* Allocate a temporary buffer to hold the padded plaintext. */
377 buf = OPENSSL_malloc(rsa_size);
378 if (buf == NULL) {
379 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
380 goto err;
381 }
Adam Langley95c29f32014-06-20 12:00:00 -0700382 }
383
Adam Langley6bc658d2014-08-18 13:29:45 -0700384 if (in_len != rsa_size) {
David Benjamin3570d732015-06-29 00:28:17 -0400385 OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_LEN_NOT_EQUAL_TO_MOD_LEN);
Adam Langley95c29f32014-06-20 12:00:00 -0700386 goto err;
387 }
388
Adam Langley6bc658d2014-08-18 13:29:45 -0700389 if (!RSA_private_transform(rsa, buf, in, rsa_size)) {
Adam Langley6887edb2014-06-20 12:00:00 -0700390 goto err;
391 }
Adam Langley95c29f32014-06-20 12:00:00 -0700392
393 switch (padding) {
394 case RSA_PKCS1_PADDING:
Adam Langley6887edb2014-06-20 12:00:00 -0700395 r = RSA_padding_check_PKCS1_type_2(out, rsa_size, buf, rsa_size);
Adam Langley95c29f32014-06-20 12:00:00 -0700396 break;
397 case RSA_PKCS1_OAEP_PADDING:
Adam Langley6887edb2014-06-20 12:00:00 -0700398 /* Use the default parameters: SHA-1 for both hashes and no label. */
399 r = RSA_padding_check_PKCS1_OAEP_mgf1(out, rsa_size, buf, rsa_size,
400 NULL, 0, NULL, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700401 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700402 case RSA_NO_PADDING:
David Benjamin74279b62014-07-24 13:09:19 -0400403 r = rsa_size;
Adam Langley95c29f32014-06-20 12:00:00 -0700404 break;
405 default:
David Benjamin3570d732015-06-29 00:28:17 -0400406 OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_PADDING_TYPE);
Adam Langley95c29f32014-06-20 12:00:00 -0700407 goto err;
408 }
409
410 if (r < 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400411 OPENSSL_PUT_ERROR(RSA, RSA_R_PADDING_CHECK_FAILED);
Adam Langley95c29f32014-06-20 12:00:00 -0700412 } else {
413 *out_len = r;
414 ret = 1;
415 }
416
417err:
David Benjamin74279b62014-07-24 13:09:19 -0400418 if (padding != RSA_NO_PADDING && buf != NULL) {
Adam Langley95c29f32014-06-20 12:00:00 -0700419 OPENSSL_cleanse(buf, rsa_size);
420 OPENSSL_free(buf);
421 }
Adam Langley95c29f32014-06-20 12:00:00 -0700422
423 return ret;
424}
425
David Benjamind93831d2015-10-29 13:19:12 -0400426int rsa_default_verify_raw(RSA *rsa, size_t *out_len, uint8_t *out,
427 size_t max_out, const uint8_t *in, size_t in_len,
428 int padding) {
Adam Langley95c29f32014-06-20 12:00:00 -0700429 const unsigned rsa_size = RSA_size(rsa);
430 BIGNUM *f, *result;
431 int ret = 0;
Adam Langley6887edb2014-06-20 12:00:00 -0700432 int r = -1;
Adam Langley95c29f32014-06-20 12:00:00 -0700433 uint8_t *buf = NULL;
434 BN_CTX *ctx = NULL;
435
Adam Langley95c29f32014-06-20 12:00:00 -0700436 if (max_out < rsa_size) {
David Benjamin3570d732015-06-29 00:28:17 -0400437 OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
Adam Langley95c29f32014-06-20 12:00:00 -0700438 return 0;
439 }
440
Brian Smith625475f2016-01-12 10:47:25 -1000441 if (!check_modulus_and_exponent_sizes(rsa)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700442 return 0;
443 }
444
445 ctx = BN_CTX_new();
446 if (ctx == NULL) {
447 goto err;
448 }
449
450 BN_CTX_start(ctx);
451 f = BN_CTX_get(ctx);
452 result = BN_CTX_get(ctx);
David Benjamin74279b62014-07-24 13:09:19 -0400453 if (padding == RSA_NO_PADDING) {
454 buf = out;
455 } else {
456 /* Allocate a temporary buffer to hold the padded plaintext. */
457 buf = OPENSSL_malloc(rsa_size);
458 if (buf == NULL) {
459 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
460 goto err;
461 }
462 }
463 if (!f || !result) {
David Benjamin3570d732015-06-29 00:28:17 -0400464 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700465 goto err;
466 }
467
Adam Langley6bc658d2014-08-18 13:29:45 -0700468 if (in_len != rsa_size) {
David Benjamin3570d732015-06-29 00:28:17 -0400469 OPENSSL_PUT_ERROR(RSA, 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) {
David Benjamin3570d732015-06-29 00:28:17 -0400478 OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
Adam Langley95c29f32014-06-20 12:00:00 -0700479 goto err;
480 }
481
Brian Smith24493a42016-03-25 09:12:48 -1000482 if (BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx) == NULL ||
483 !BN_mod_exp_mont(result, f, rsa->e, rsa->n, ctx, rsa->mont_n)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700484 goto err;
485 }
486
Adam Langley6887edb2014-06-20 12:00:00 -0700487 if (!BN_bn2bin_padded(buf, rsa_size, result)) {
David Benjamin3570d732015-06-29 00:28:17 -0400488 OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
Adam Langley6887edb2014-06-20 12:00:00 -0700489 goto err;
490 }
Adam Langley95c29f32014-06-20 12:00:00 -0700491
492 switch (padding) {
493 case RSA_PKCS1_PADDING:
Adam Langley6887edb2014-06-20 12:00:00 -0700494 r = RSA_padding_check_PKCS1_type_1(out, rsa_size, buf, rsa_size);
Adam Langley95c29f32014-06-20 12:00:00 -0700495 break;
496 case RSA_NO_PADDING:
David Benjamin74279b62014-07-24 13:09:19 -0400497 r = rsa_size;
Adam Langley95c29f32014-06-20 12:00:00 -0700498 break;
499 default:
David Benjamin3570d732015-06-29 00:28:17 -0400500 OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_PADDING_TYPE);
Adam Langley95c29f32014-06-20 12:00:00 -0700501 goto err;
502 }
503
504 if (r < 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400505 OPENSSL_PUT_ERROR(RSA, RSA_R_PADDING_CHECK_FAILED);
Adam Langley95c29f32014-06-20 12:00:00 -0700506 } else {
507 *out_len = r;
508 ret = 1;
509 }
510
511err:
512 if (ctx != NULL) {
513 BN_CTX_end(ctx);
514 BN_CTX_free(ctx);
515 }
David Benjamin74279b62014-07-24 13:09:19 -0400516 if (padding != RSA_NO_PADDING && buf != NULL) {
Adam Langley95c29f32014-06-20 12:00:00 -0700517 OPENSSL_cleanse(buf, rsa_size);
518 OPENSSL_free(buf);
519 }
520 return ret;
521}
522
David Benjamind93831d2015-10-29 13:19:12 -0400523int rsa_default_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in,
524 size_t len) {
Adam Langley6bc658d2014-08-18 13:29:45 -0700525 BIGNUM *f, *result;
526 BN_CTX *ctx = NULL;
527 unsigned blinding_index = 0;
528 BN_BLINDING *blinding = NULL;
529 int ret = 0;
530
531 ctx = BN_CTX_new();
532 if (ctx == NULL) {
533 goto err;
534 }
535 BN_CTX_start(ctx);
536 f = BN_CTX_get(ctx);
537 result = BN_CTX_get(ctx);
538
539 if (f == NULL || result == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400540 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
Adam Langley6bc658d2014-08-18 13:29:45 -0700541 goto err;
542 }
543
544 if (BN_bin2bn(in, len, f) == NULL) {
545 goto err;
546 }
547
548 if (BN_ucmp(f, rsa->n) >= 0) {
549 /* Usually the padding functions would catch this. */
David Benjamin3570d732015-06-29 00:28:17 -0400550 OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
Adam Langley6bc658d2014-08-18 13:29:45 -0700551 goto err;
552 }
553
554 if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
Brian Smithcbf56a52016-03-21 11:25:39 -1000555 if (BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx) == NULL) {
556 OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
557 goto err;
558 }
559
Adam Langley6bc658d2014-08-18 13:29:45 -0700560 blinding = rsa_blinding_get(rsa, &blinding_index, ctx);
561 if (blinding == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400562 OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
Adam Langley6bc658d2014-08-18 13:29:45 -0700563 goto err;
564 }
Brian Smith617804a2016-02-08 20:36:51 -1000565 if (!BN_BLINDING_convert(f, blinding, ctx, rsa->mont_n)) {
Adam Langley6bc658d2014-08-18 13:29:45 -0700566 goto err;
567 }
568 }
569
570 if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
571 ((rsa->p != NULL) && (rsa->q != NULL) && (rsa->dmp1 != NULL) &&
572 (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
573 if (!rsa->meth->mod_exp(result, f, rsa, ctx)) {
574 goto err;
575 }
576 } else {
577 BIGNUM local_d;
578 BIGNUM *d = NULL;
579
580 BN_init(&local_d);
581 d = &local_d;
582 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
583
Brian Smith24493a42016-03-25 09:12:48 -1000584 if (BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx) == NULL ||
585 !BN_mod_exp_mont_consttime(result, f, d, rsa->n, ctx, rsa->mont_n)) {
Adam Langley6bc658d2014-08-18 13:29:45 -0700586 goto err;
587 }
588 }
589
590 if (blinding) {
Brian Smith642b0b82016-01-24 23:41:56 -1000591 if (!BN_BLINDING_invert(result, blinding, ctx)) {
Adam Langley6bc658d2014-08-18 13:29:45 -0700592 goto err;
593 }
594 }
595
596 if (!BN_bn2bin_padded(out, len, result)) {
David Benjamin3570d732015-06-29 00:28:17 -0400597 OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
Adam Langley6bc658d2014-08-18 13:29:45 -0700598 goto err;
599 }
600
601 ret = 1;
602
603err:
604 if (ctx != NULL) {
605 BN_CTX_end(ctx);
606 BN_CTX_free(ctx);
607 }
608 if (blinding != NULL) {
609 rsa_blinding_release(rsa, blinding, blinding_index);
610 }
611
612 return ret;
613}
614
Adam Langley95c29f32014-06-20 12:00:00 -0700615static int mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) {
616 BIGNUM *r1, *m1, *vrfy;
617 BIGNUM local_dmp1, local_dmq1, local_c, local_r1;
618 BIGNUM *dmp1, *dmq1, *c, *pr1;
619 int ret = 0;
Adam Langley839b8812015-05-26 11:36:46 -0700620 size_t i, num_additional_primes = 0;
621
622 if (rsa->additional_primes != NULL) {
623 num_additional_primes = sk_RSA_additional_prime_num(rsa->additional_primes);
624 }
Adam Langley95c29f32014-06-20 12:00:00 -0700625
626 BN_CTX_start(ctx);
627 r1 = BN_CTX_get(ctx);
628 m1 = BN_CTX_get(ctx);
629 vrfy = BN_CTX_get(ctx);
Brian Smith7cf60852016-03-19 22:39:37 -1000630 if (r1 == NULL ||
631 m1 == NULL ||
632 vrfy == NULL) {
633 goto err;
634 }
Adam Langley95c29f32014-06-20 12:00:00 -0700635
636 {
637 BIGNUM local_p, local_q;
638 BIGNUM *p = NULL, *q = NULL;
639
640 /* Make sure BN_mod_inverse in Montgomery intialization uses the
Brian Smith60a45aa2015-11-18 17:44:11 -1000641 * BN_FLG_CONSTTIME flag. */
Adam Langley95c29f32014-06-20 12:00:00 -0700642 BN_init(&local_p);
643 p = &local_p;
644 BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
645
646 BN_init(&local_q);
647 q = &local_q;
648 BN_with_flags(q, rsa->q, BN_FLG_CONSTTIME);
649
Brian Smith24493a42016-03-25 09:12:48 -1000650 if (BN_MONT_CTX_set_locked(&rsa->mont_p, &rsa->lock, p, ctx) == NULL ||
651 BN_MONT_CTX_set_locked(&rsa->mont_q, &rsa->lock, q, ctx) == NULL) {
652 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700653 }
654 }
655
Brian Smith24493a42016-03-25 09:12:48 -1000656 if (BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx) == NULL) {
657 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700658 }
659
660 /* compute I mod q */
661 c = &local_c;
662 BN_with_flags(c, I, BN_FLG_CONSTTIME);
663 if (!BN_mod(r1, c, rsa->q, ctx)) {
664 goto err;
665 }
666
667 /* compute r1^dmq1 mod q */
668 dmq1 = &local_dmq1;
669 BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
Brian Smith617804a2016-02-08 20:36:51 -1000670 if (!BN_mod_exp_mont_consttime(m1, r1, dmq1, rsa->q, ctx, rsa->mont_q)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700671 goto err;
672 }
673
674 /* compute I mod p */
675 c = &local_c;
676 BN_with_flags(c, I, BN_FLG_CONSTTIME);
677 if (!BN_mod(r1, c, rsa->p, ctx)) {
678 goto err;
679 }
680
681 /* compute r1^dmp1 mod p */
682 dmp1 = &local_dmp1;
683 BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
Brian Smith617804a2016-02-08 20:36:51 -1000684 if (!BN_mod_exp_mont_consttime(r0, r1, dmp1, rsa->p, ctx, rsa->mont_p)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700685 goto err;
686 }
687
688 if (!BN_sub(r0, r0, m1)) {
689 goto err;
690 }
691 /* This will help stop the size of r0 increasing, which does
692 * affect the multiply if it optimised for a power of 2 size */
693 if (BN_is_negative(r0)) {
694 if (!BN_add(r0, r0, rsa->p)) {
695 goto err;
696 }
697 }
698
699 if (!BN_mul(r1, r0, rsa->iqmp, ctx)) {
700 goto err;
701 }
702
703 /* Turn BN_FLG_CONSTTIME flag on before division operation */
704 pr1 = &local_r1;
705 BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
706
707 if (!BN_mod(r0, pr1, rsa->p, ctx)) {
708 goto err;
709 }
710
711 /* If p < q it is occasionally possible for the correction of
712 * adding 'p' if r0 is negative above to leave the result still
713 * negative. This can break the private key operations: the following
714 * second correction should *always* correct this rare occurrence.
715 * This will *never* happen with OpenSSL generated keys because
716 * they ensure p > q [steve] */
717 if (BN_is_negative(r0)) {
718 if (!BN_add(r0, r0, rsa->p)) {
719 goto err;
720 }
721 }
722 if (!BN_mul(r1, r0, rsa->q, ctx)) {
723 goto err;
724 }
725 if (!BN_add(r0, r1, m1)) {
726 goto err;
727 }
728
Adam Langley839b8812015-05-26 11:36:46 -0700729 for (i = 0; i < num_additional_primes; i++) {
730 /* multi-prime RSA. */
731 BIGNUM local_exp, local_prime;
732 BIGNUM *exp = &local_exp, *prime = &local_prime;
733 RSA_additional_prime *ap =
734 sk_RSA_additional_prime_value(rsa->additional_primes, i);
735
736 BN_with_flags(exp, ap->exp, BN_FLG_CONSTTIME);
737 BN_with_flags(prime, ap->prime, BN_FLG_CONSTTIME);
738
739 /* c will already point to a BIGNUM with the correct flags. */
740 if (!BN_mod(r1, c, prime, ctx)) {
741 goto err;
742 }
743
Brian Smith24493a42016-03-25 09:12:48 -1000744 if (BN_MONT_CTX_set_locked(&ap->mont, &rsa->lock, prime, ctx) == NULL ||
745 !BN_mod_exp_mont_consttime(m1, r1, exp, prime, ctx, ap->mont)) {
Adam Langley839b8812015-05-26 11:36:46 -0700746 goto err;
747 }
748
749 BN_set_flags(m1, BN_FLG_CONSTTIME);
750
751 if (!BN_sub(m1, m1, r0) ||
752 !BN_mul(m1, m1, ap->coeff, ctx) ||
753 !BN_mod(m1, m1, prime, ctx) ||
754 (BN_is_negative(m1) && !BN_add(m1, m1, prime)) ||
755 !BN_mul(m1, m1, ap->r, ctx) ||
756 !BN_add(r0, r0, m1)) {
757 goto err;
758 }
759 }
760
Adam Langley95c29f32014-06-20 12:00:00 -0700761 if (rsa->e && rsa->n) {
Brian Smith617804a2016-02-08 20:36:51 -1000762 if (!BN_mod_exp_mont(vrfy, r0, rsa->e, rsa->n, ctx, rsa->mont_n)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700763 goto err;
764 }
765 /* If 'I' was greater than (or equal to) rsa->n, the operation
766 * will be equivalent to using 'I mod n'. However, the result of
767 * the verify will *always* be less than 'n' so we don't check
768 * for absolute equality, just congruency. */
769 if (!BN_sub(vrfy, vrfy, I)) {
770 goto err;
771 }
772 if (!BN_mod(vrfy, vrfy, rsa->n, ctx)) {
773 goto err;
774 }
775 if (BN_is_negative(vrfy)) {
776 if (!BN_add(vrfy, vrfy, rsa->n)) {
777 goto err;
778 }
779 }
780 if (!BN_is_zero(vrfy)) {
781 /* 'I' and 'vrfy' aren't congruent mod n. Don't leak
782 * miscalculated CRT output, just do a raw (slower)
783 * mod_exp and return that instead. */
784
785 BIGNUM local_d;
786 BIGNUM *d = NULL;
787
788 d = &local_d;
789 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
Brian Smith617804a2016-02-08 20:36:51 -1000790 if (!BN_mod_exp_mont_consttime(r0, I, d, rsa->n, ctx, rsa->mont_n)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700791 goto err;
792 }
793 }
794 }
795 ret = 1;
796
797err:
798 BN_CTX_end(ctx);
799 return ret;
800}
801
David Benjamind93831d2015-10-29 13:19:12 -0400802int rsa_default_multi_prime_keygen(RSA *rsa, int bits, int num_primes,
803 BIGNUM *e_value, BN_GENCB *cb) {
Adam Langley95c29f32014-06-20 12:00:00 -0700804 BIGNUM *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL, *tmp;
805 BIGNUM local_r0, local_d, local_p;
806 BIGNUM *pr0, *d, *p;
Adam Langley839b8812015-05-26 11:36:46 -0700807 int prime_bits, ok = -1, n = 0, i, j;
Adam Langley95c29f32014-06-20 12:00:00 -0700808 BN_CTX *ctx = NULL;
Adam Langley839b8812015-05-26 11:36:46 -0700809 STACK_OF(RSA_additional_prime) *additional_primes = NULL;
810
811 if (num_primes < 2) {
812 ok = 0; /* we set our own err */
David Benjamin3570d732015-06-29 00:28:17 -0400813 OPENSSL_PUT_ERROR(RSA, RSA_R_MUST_HAVE_AT_LEAST_TWO_PRIMES);
Adam Langley839b8812015-05-26 11:36:46 -0700814 goto err;
815 }
Adam Langley95c29f32014-06-20 12:00:00 -0700816
817 ctx = BN_CTX_new();
818 if (ctx == NULL) {
819 goto err;
820 }
821 BN_CTX_start(ctx);
822 r0 = BN_CTX_get(ctx);
823 r1 = BN_CTX_get(ctx);
824 r2 = BN_CTX_get(ctx);
825 r3 = BN_CTX_get(ctx);
Brian Smithf4bbc2a2015-08-06 10:42:27 -0400826 if (r0 == NULL || r1 == NULL || r2 == NULL || r3 == NULL) {
Adam Langley95c29f32014-06-20 12:00:00 -0700827 goto err;
828 }
829
Adam Langley839b8812015-05-26 11:36:46 -0700830 if (num_primes > 2) {
831 additional_primes = sk_RSA_additional_prime_new_null();
832 if (additional_primes == NULL) {
833 goto err;
834 }
835 }
836
837 for (i = 2; i < num_primes; i++) {
838 RSA_additional_prime *ap = OPENSSL_malloc(sizeof(RSA_additional_prime));
839 if (ap == NULL) {
840 goto err;
841 }
842 memset(ap, 0, sizeof(RSA_additional_prime));
843 ap->prime = BN_new();
844 ap->exp = BN_new();
845 ap->coeff = BN_new();
846 ap->r = BN_new();
847 if (ap->prime == NULL ||
848 ap->exp == NULL ||
849 ap->coeff == NULL ||
850 ap->r == NULL ||
851 !sk_RSA_additional_prime_push(additional_primes, ap)) {
852 RSA_additional_prime_free(ap);
853 goto err;
854 }
855 }
Adam Langley95c29f32014-06-20 12:00:00 -0700856
857 /* We need the RSA components non-NULL */
David Benjamin6eb000d2015-02-11 01:17:41 -0500858 if (!rsa->n && ((rsa->n = BN_new()) == NULL)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700859 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500860 }
861 if (!rsa->d && ((rsa->d = BN_new()) == NULL)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700862 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500863 }
864 if (!rsa->e && ((rsa->e = BN_new()) == NULL)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700865 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500866 }
867 if (!rsa->p && ((rsa->p = BN_new()) == NULL)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700868 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500869 }
870 if (!rsa->q && ((rsa->q = BN_new()) == NULL)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700871 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500872 }
873 if (!rsa->dmp1 && ((rsa->dmp1 = BN_new()) == NULL)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700874 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500875 }
876 if (!rsa->dmq1 && ((rsa->dmq1 = BN_new()) == NULL)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700877 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500878 }
879 if (!rsa->iqmp && ((rsa->iqmp = BN_new()) == NULL)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700880 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500881 }
Adam Langley95c29f32014-06-20 12:00:00 -0700882
David Benjamin1c703cb2015-06-11 21:42:14 -0400883 if (!BN_copy(rsa->e, e_value)) {
884 goto err;
885 }
Adam Langley95c29f32014-06-20 12:00:00 -0700886
887 /* generate p and q */
Adam Langley839b8812015-05-26 11:36:46 -0700888 prime_bits = (bits + (num_primes - 1)) / num_primes;
Adam Langley95c29f32014-06-20 12:00:00 -0700889 for (;;) {
Adam Langley839b8812015-05-26 11:36:46 -0700890 if (!BN_generate_prime_ex(rsa->p, prime_bits, 0, NULL, NULL, cb) ||
David Benjamin6eb000d2015-02-11 01:17:41 -0500891 !BN_sub(r2, rsa->p, BN_value_one()) ||
892 !BN_gcd(r1, r2, rsa->e, ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700893 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500894 }
895 if (BN_is_one(r1)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700896 break;
David Benjamin6eb000d2015-02-11 01:17:41 -0500897 }
898 if (!BN_GENCB_call(cb, 2, n++)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700899 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500900 }
Adam Langley95c29f32014-06-20 12:00:00 -0700901 }
David Benjamin6eb000d2015-02-11 01:17:41 -0500902 if (!BN_GENCB_call(cb, 3, 0)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700903 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500904 }
Adam Langley839b8812015-05-26 11:36:46 -0700905 prime_bits = ((bits - prime_bits) + (num_primes - 2)) / (num_primes - 1);
Adam Langley95c29f32014-06-20 12:00:00 -0700906 for (;;) {
907 /* When generating ridiculously small keys, we can get stuck
908 * continually regenerating the same prime values. Check for
909 * this and bail if it happens 3 times. */
910 unsigned int degenerate = 0;
911 do {
Adam Langley839b8812015-05-26 11:36:46 -0700912 if (!BN_generate_prime_ex(rsa->q, prime_bits, 0, NULL, NULL, cb)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700913 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500914 }
Adam Langley95c29f32014-06-20 12:00:00 -0700915 } while ((BN_cmp(rsa->p, rsa->q) == 0) && (++degenerate < 3));
916 if (degenerate == 3) {
917 ok = 0; /* we set our own err */
David Benjamin3570d732015-06-29 00:28:17 -0400918 OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL);
Adam Langley95c29f32014-06-20 12:00:00 -0700919 goto err;
920 }
David Benjamin6eb000d2015-02-11 01:17:41 -0500921 if (!BN_sub(r2, rsa->q, BN_value_one()) ||
922 !BN_gcd(r1, r2, rsa->e, ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700923 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500924 }
925 if (BN_is_one(r1)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700926 break;
David Benjamin6eb000d2015-02-11 01:17:41 -0500927 }
928 if (!BN_GENCB_call(cb, 2, n++)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700929 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500930 }
Adam Langley95c29f32014-06-20 12:00:00 -0700931 }
Adam Langley839b8812015-05-26 11:36:46 -0700932
933 if (!BN_GENCB_call(cb, 3, 1) ||
934 !BN_mul(rsa->n, rsa->p, rsa->q, ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700935 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500936 }
Adam Langley839b8812015-05-26 11:36:46 -0700937
938 for (i = 2; i < num_primes; i++) {
939 RSA_additional_prime *ap =
940 sk_RSA_additional_prime_value(additional_primes, i - 2);
941 prime_bits = ((bits - BN_num_bits(rsa->n)) + (num_primes - (i + 1))) /
942 (num_primes - i);
943
944 for (;;) {
945 if (!BN_generate_prime_ex(ap->prime, prime_bits, 0, NULL, NULL, cb)) {
946 goto err;
947 }
948 if (BN_cmp(rsa->p, ap->prime) == 0 ||
949 BN_cmp(rsa->q, ap->prime) == 0) {
950 continue;
951 }
952
953 for (j = 0; j < i - 2; j++) {
954 if (BN_cmp(sk_RSA_additional_prime_value(additional_primes, j)->prime,
955 ap->prime) == 0) {
956 break;
957 }
958 }
959 if (j != i - 2) {
960 continue;
961 }
962
963 if (!BN_sub(r2, ap->prime, BN_value_one()) ||
964 !BN_gcd(r1, r2, rsa->e, ctx)) {
965 goto err;
966 }
967
968 if (!BN_is_one(r1)) {
969 continue;
970 }
971 if (i != num_primes - 1) {
972 break;
973 }
974
975 /* For the last prime we'll check that it makes n large enough. In the
976 * two prime case this isn't a problem because we generate primes with
977 * the top two bits set and so the product is always of the expected
978 * size. In the multi prime case, this doesn't follow. */
979 if (!BN_mul(r1, rsa->n, ap->prime, ctx)) {
980 goto err;
981 }
Adam Langley96c2a282015-06-02 14:16:44 -0700982 if (BN_num_bits(r1) == (unsigned) bits) {
Adam Langley839b8812015-05-26 11:36:46 -0700983 break;
984 }
985
986 if (!BN_GENCB_call(cb, 2, n++)) {
987 goto err;
988 }
989 }
990
991 /* ap->r is is the product of all the primes prior to the current one
992 * (including p and q). */
993 if (!BN_copy(ap->r, rsa->n)) {
994 goto err;
995 }
996 if (i == num_primes - 1) {
997 /* In the case of the last prime, we calculated n as |r1| in the loop
998 * above. */
999 if (!BN_copy(rsa->n, r1)) {
1000 goto err;
1001 }
1002 } else if (!BN_mul(rsa->n, rsa->n, ap->prime, ctx)) {
1003 goto err;
1004 }
1005
1006 if (!BN_GENCB_call(cb, 3, 1)) {
1007 goto err;
1008 }
1009 }
1010
Adam Langley95c29f32014-06-20 12:00:00 -07001011 if (BN_cmp(rsa->p, rsa->q) < 0) {
1012 tmp = rsa->p;
1013 rsa->p = rsa->q;
1014 rsa->q = tmp;
1015 }
1016
Adam Langley95c29f32014-06-20 12:00:00 -07001017 /* calculate d */
David Benjamin6eb000d2015-02-11 01:17:41 -05001018 if (!BN_sub(r1, rsa->p, BN_value_one())) {
Adam Langley95c29f32014-06-20 12:00:00 -07001019 goto err; /* p-1 */
David Benjamin6eb000d2015-02-11 01:17:41 -05001020 }
1021 if (!BN_sub(r2, rsa->q, BN_value_one())) {
Adam Langley95c29f32014-06-20 12:00:00 -07001022 goto err; /* q-1 */
David Benjamin6eb000d2015-02-11 01:17:41 -05001023 }
1024 if (!BN_mul(r0, r1, r2, ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -07001025 goto err; /* (p-1)(q-1) */
David Benjamin6eb000d2015-02-11 01:17:41 -05001026 }
Adam Langley839b8812015-05-26 11:36:46 -07001027 for (i = 2; i < num_primes; i++) {
1028 RSA_additional_prime *ap =
1029 sk_RSA_additional_prime_value(additional_primes, i - 2);
1030 if (!BN_sub(r3, ap->prime, BN_value_one()) ||
1031 !BN_mul(r0, r0, r3, ctx)) {
1032 goto err;
1033 }
1034 }
Adam Langley95c29f32014-06-20 12:00:00 -07001035 pr0 = &local_r0;
1036 BN_with_flags(pr0, r0, BN_FLG_CONSTTIME);
David Benjamin6eb000d2015-02-11 01:17:41 -05001037 if (!BN_mod_inverse(rsa->d, rsa->e, pr0, ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -07001038 goto err; /* d */
David Benjamin6eb000d2015-02-11 01:17:41 -05001039 }
Adam Langley95c29f32014-06-20 12:00:00 -07001040
1041 /* set up d for correct BN_FLG_CONSTTIME flag */
1042 d = &local_d;
1043 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
1044
1045 /* calculate d mod (p-1) */
David Benjamin6eb000d2015-02-11 01:17:41 -05001046 if (!BN_mod(rsa->dmp1, d, r1, ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -07001047 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -05001048 }
Adam Langley95c29f32014-06-20 12:00:00 -07001049
1050 /* calculate d mod (q-1) */
David Benjamin6eb000d2015-02-11 01:17:41 -05001051 if (!BN_mod(rsa->dmq1, d, r2, ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -07001052 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -05001053 }
Adam Langley95c29f32014-06-20 12:00:00 -07001054
1055 /* calculate inverse of q mod p */
1056 p = &local_p;
1057 BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
1058
David Benjamin6eb000d2015-02-11 01:17:41 -05001059 if (!BN_mod_inverse(rsa->iqmp, rsa->q, p, ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -07001060 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -05001061 }
Adam Langley95c29f32014-06-20 12:00:00 -07001062
Adam Langley839b8812015-05-26 11:36:46 -07001063 for (i = 2; i < num_primes; i++) {
1064 RSA_additional_prime *ap =
1065 sk_RSA_additional_prime_value(additional_primes, i - 2);
1066 if (!BN_sub(ap->exp, ap->prime, BN_value_one()) ||
1067 !BN_mod(ap->exp, rsa->d, ap->exp, ctx) ||
1068 !BN_mod_inverse(ap->coeff, ap->r, ap->prime, ctx)) {
1069 goto err;
1070 }
1071 }
1072
Adam Langley95c29f32014-06-20 12:00:00 -07001073 ok = 1;
Adam Langley839b8812015-05-26 11:36:46 -07001074 rsa->additional_primes = additional_primes;
1075 additional_primes = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001076
1077err:
1078 if (ok == -1) {
David Benjamin3570d732015-06-29 00:28:17 -04001079 OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
Adam Langley95c29f32014-06-20 12:00:00 -07001080 ok = 0;
1081 }
1082 if (ctx != NULL) {
1083 BN_CTX_end(ctx);
1084 BN_CTX_free(ctx);
1085 }
Adam Langley839b8812015-05-26 11:36:46 -07001086 sk_RSA_additional_prime_pop_free(additional_primes,
1087 RSA_additional_prime_free);
Adam Langley95c29f32014-06-20 12:00:00 -07001088 return ok;
1089}
1090
David Benjamind93831d2015-10-29 13:19:12 -04001091int rsa_default_keygen(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) {
1092 return rsa_default_multi_prime_keygen(rsa, bits, 2 /* num primes */, e_value,
1093 cb);
Adam Langley839b8812015-05-26 11:36:46 -07001094}
1095
David Benjamind93831d2015-10-29 13:19:12 -04001096/* Many of these methods are NULL to more easily drop unused functions. The
1097 * wrapper functions will select the appropriate |rsa_default_*| for all
1098 * methods. */
1099const RSA_METHOD RSA_default_method = {
Adam Langley95c29f32014-06-20 12:00:00 -07001100 {
1101 0 /* references */,
1102 1 /* is_static */,
1103 },
1104 NULL /* app_data */,
1105
1106 NULL /* init */,
David Benjamind93831d2015-10-29 13:19:12 -04001107 NULL /* finish (defaults to rsa_default_finish) */,
Adam Langley95c29f32014-06-20 12:00:00 -07001108
David Benjamind93831d2015-10-29 13:19:12 -04001109 NULL /* size (defaults to rsa_default_size) */,
David Benjamin925fee32014-07-11 14:14:08 -04001110
Adam Langley95c29f32014-06-20 12:00:00 -07001111 NULL /* sign */,
1112 NULL /* verify */,
1113
David Benjamind93831d2015-10-29 13:19:12 -04001114 NULL /* encrypt (defaults to rsa_default_encrypt) */,
1115 NULL /* sign_raw (defaults to rsa_default_sign_raw) */,
1116 NULL /* decrypt (defaults to rsa_default_decrypt) */,
1117 NULL /* verify_raw (defaults to rsa_default_verify_raw) */,
Adam Langley95c29f32014-06-20 12:00:00 -07001118
David Benjamind93831d2015-10-29 13:19:12 -04001119 NULL /* private_transform (defaults to rsa_default_private_transform) */,
Adam Langley6bc658d2014-08-18 13:29:45 -07001120
David Benjamind93831d2015-10-29 13:19:12 -04001121 mod_exp,
Brian Smith617804a2016-02-08 20:36:51 -10001122 NULL /* bn_mod_exp */,
Adam Langley95c29f32014-06-20 12:00:00 -07001123
1124 RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE,
1125
David Benjamind93831d2015-10-29 13:19:12 -04001126 NULL /* keygen (defaults to rsa_default_keygen) */,
1127 NULL /* multi_prime_keygen (defaults to rsa_default_multi_prime_keygen) */,
Adam Langley626c6862015-09-11 16:17:44 -07001128
1129 NULL /* supports_digest */,
Adam Langley95c29f32014-06-20 12:00:00 -07001130};