blob: ed0b114610245f34cbcc5ae503d53a44bcfc6acd [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) {
216 BN_BLINDING *ret = NULL;
217 BN_BLINDING **new_blindings;
218 uint8_t *new_blindings_inuse;
219 char overflow = 0;
220
Adam Langley683d7bd2015-04-13 11:04:14 -0700221 CRYPTO_MUTEX_lock_write(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700222
Adam Langley33672732015-03-31 18:55:53 -0700223 unsigned i;
224 for (i = 0; i < rsa->num_blindings; i++) {
225 if (rsa->blindings_inuse[i] == 0) {
226 rsa->blindings_inuse[i] = 1;
227 ret = rsa->blindings[i];
228 *index_used = i;
229 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700230 }
231 }
232
233 if (ret != NULL) {
Adam Langley683d7bd2015-04-13 11:04:14 -0700234 CRYPTO_MUTEX_unlock(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700235 return ret;
236 }
237
238 overflow = rsa->num_blindings >= MAX_BLINDINGS_PER_RSA;
239
240 /* We didn't find a free BN_BLINDING to use so increase the length of
241 * the arrays by one and use the newly created element. */
242
Adam Langley683d7bd2015-04-13 11:04:14 -0700243 CRYPTO_MUTEX_unlock(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700244 ret = rsa_setup_blinding(rsa, ctx);
245 if (ret == NULL) {
246 return NULL;
247 }
248
249 if (overflow) {
250 /* We cannot add any more cached BN_BLINDINGs so we use |ret|
251 * and mark it for destruction in |rsa_blinding_release|. */
252 *index_used = MAX_BLINDINGS_PER_RSA;
253 return ret;
254 }
255
Adam Langley683d7bd2015-04-13 11:04:14 -0700256 CRYPTO_MUTEX_lock_write(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700257
258 new_blindings =
259 OPENSSL_malloc(sizeof(BN_BLINDING *) * (rsa->num_blindings + 1));
260 if (new_blindings == NULL) {
261 goto err1;
262 }
263 memcpy(new_blindings, rsa->blindings,
264 sizeof(BN_BLINDING *) * rsa->num_blindings);
265 new_blindings[rsa->num_blindings] = ret;
266
267 new_blindings_inuse = OPENSSL_malloc(rsa->num_blindings + 1);
268 if (new_blindings_inuse == NULL) {
269 goto err2;
270 }
271 memcpy(new_blindings_inuse, rsa->blindings_inuse, rsa->num_blindings);
272 new_blindings_inuse[rsa->num_blindings] = 1;
273 *index_used = rsa->num_blindings;
274
David Benjamind8b65c82015-04-22 16:09:09 -0400275 OPENSSL_free(rsa->blindings);
Adam Langley95c29f32014-06-20 12:00:00 -0700276 rsa->blindings = new_blindings;
David Benjamind8b65c82015-04-22 16:09:09 -0400277 OPENSSL_free(rsa->blindings_inuse);
Adam Langley95c29f32014-06-20 12:00:00 -0700278 rsa->blindings_inuse = new_blindings_inuse;
279 rsa->num_blindings++;
280
Adam Langley683d7bd2015-04-13 11:04:14 -0700281 CRYPTO_MUTEX_unlock(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700282 return ret;
283
284err2:
285 OPENSSL_free(new_blindings);
286
287err1:
Adam Langley683d7bd2015-04-13 11:04:14 -0700288 CRYPTO_MUTEX_unlock(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700289 BN_BLINDING_free(ret);
290 return NULL;
291}
292
293/* rsa_blinding_release marks the cached BN_BLINDING at the given index as free
294 * for other threads to use. */
295static void rsa_blinding_release(RSA *rsa, BN_BLINDING *blinding,
296 unsigned blinding_index) {
297 if (blinding_index == MAX_BLINDINGS_PER_RSA) {
298 /* This blinding wasn't cached. */
299 BN_BLINDING_free(blinding);
300 return;
301 }
302
Adam Langley683d7bd2015-04-13 11:04:14 -0700303 CRYPTO_MUTEX_lock_write(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700304 rsa->blindings_inuse[blinding_index] = 0;
Adam Langley683d7bd2015-04-13 11:04:14 -0700305 CRYPTO_MUTEX_unlock(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700306}
307
308/* signing */
David Benjamind93831d2015-10-29 13:19:12 -0400309int rsa_default_sign_raw(RSA *rsa, size_t *out_len, uint8_t *out,
310 size_t max_out, const uint8_t *in, size_t in_len,
311 int padding) {
Adam Langley95c29f32014-06-20 12:00:00 -0700312 const unsigned rsa_size = RSA_size(rsa);
Adam Langley95c29f32014-06-20 12:00:00 -0700313 uint8_t *buf = NULL;
Adam Langley6887edb2014-06-20 12:00:00 -0700314 int i, ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700315
316 if (max_out < rsa_size) {
David Benjamin3570d732015-06-29 00:28:17 -0400317 OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
Adam Langley95c29f32014-06-20 12:00:00 -0700318 return 0;
319 }
320
Adam Langley95c29f32014-06-20 12:00:00 -0700321 buf = OPENSSL_malloc(rsa_size);
Adam Langley6bc658d2014-08-18 13:29:45 -0700322 if (buf == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400323 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700324 goto err;
325 }
326
327 switch (padding) {
328 case RSA_PKCS1_PADDING:
329 i = RSA_padding_add_PKCS1_type_1(buf, rsa_size, in, in_len);
330 break;
331 case RSA_NO_PADDING:
332 i = RSA_padding_add_none(buf, rsa_size, in, in_len);
333 break;
334 default:
David Benjamin3570d732015-06-29 00:28:17 -0400335 OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_PADDING_TYPE);
Adam Langley95c29f32014-06-20 12:00:00 -0700336 goto err;
337 }
Adam Langley6bc658d2014-08-18 13:29:45 -0700338
Adam Langley95c29f32014-06-20 12:00:00 -0700339 if (i <= 0) {
340 goto err;
341 }
342
Adam Langley6bc658d2014-08-18 13:29:45 -0700343 if (!RSA_private_transform(rsa, out, buf, rsa_size)) {
Adam Langley5f5bf6f2015-02-24 13:49:41 -0800344 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700345 }
346
347 *out_len = rsa_size;
348 ret = 1;
349
350err:
Adam Langley95c29f32014-06-20 12:00:00 -0700351 if (buf != NULL) {
352 OPENSSL_cleanse(buf, rsa_size);
353 OPENSSL_free(buf);
354 }
Adam Langley95c29f32014-06-20 12:00:00 -0700355
356 return ret;
357}
358
David Benjamind93831d2015-10-29 13:19:12 -0400359int rsa_default_decrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
360 const uint8_t *in, size_t in_len, int padding) {
Adam Langley95c29f32014-06-20 12:00:00 -0700361 const unsigned rsa_size = RSA_size(rsa);
Adam Langley95c29f32014-06-20 12:00:00 -0700362 int r = -1;
363 uint8_t *buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700364 int ret = 0;
365
366 if (max_out < rsa_size) {
David Benjamin3570d732015-06-29 00:28:17 -0400367 OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
Adam Langley95c29f32014-06-20 12:00:00 -0700368 return 0;
369 }
370
David Benjamin74279b62014-07-24 13:09:19 -0400371 if (padding == RSA_NO_PADDING) {
372 buf = out;
373 } else {
374 /* Allocate a temporary buffer to hold the padded plaintext. */
375 buf = OPENSSL_malloc(rsa_size);
376 if (buf == NULL) {
377 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
378 goto err;
379 }
Adam Langley95c29f32014-06-20 12:00:00 -0700380 }
381
Adam Langley6bc658d2014-08-18 13:29:45 -0700382 if (in_len != rsa_size) {
David Benjamin3570d732015-06-29 00:28:17 -0400383 OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_LEN_NOT_EQUAL_TO_MOD_LEN);
Adam Langley95c29f32014-06-20 12:00:00 -0700384 goto err;
385 }
386
Adam Langley6bc658d2014-08-18 13:29:45 -0700387 if (!RSA_private_transform(rsa, buf, in, rsa_size)) {
Adam Langley6887edb2014-06-20 12:00:00 -0700388 goto err;
389 }
Adam Langley95c29f32014-06-20 12:00:00 -0700390
391 switch (padding) {
392 case RSA_PKCS1_PADDING:
Adam Langley6887edb2014-06-20 12:00:00 -0700393 r = RSA_padding_check_PKCS1_type_2(out, rsa_size, buf, rsa_size);
Adam Langley95c29f32014-06-20 12:00:00 -0700394 break;
395 case RSA_PKCS1_OAEP_PADDING:
Adam Langley6887edb2014-06-20 12:00:00 -0700396 /* Use the default parameters: SHA-1 for both hashes and no label. */
397 r = RSA_padding_check_PKCS1_OAEP_mgf1(out, rsa_size, buf, rsa_size,
398 NULL, 0, NULL, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700399 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700400 case RSA_NO_PADDING:
David Benjamin74279b62014-07-24 13:09:19 -0400401 r = rsa_size;
Adam Langley95c29f32014-06-20 12:00:00 -0700402 break;
403 default:
David Benjamin3570d732015-06-29 00:28:17 -0400404 OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_PADDING_TYPE);
Adam Langley95c29f32014-06-20 12:00:00 -0700405 goto err;
406 }
407
408 if (r < 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400409 OPENSSL_PUT_ERROR(RSA, RSA_R_PADDING_CHECK_FAILED);
Adam Langley95c29f32014-06-20 12:00:00 -0700410 } else {
411 *out_len = r;
412 ret = 1;
413 }
414
415err:
David Benjamin74279b62014-07-24 13:09:19 -0400416 if (padding != RSA_NO_PADDING && buf != NULL) {
Adam Langley95c29f32014-06-20 12:00:00 -0700417 OPENSSL_cleanse(buf, rsa_size);
418 OPENSSL_free(buf);
419 }
Adam Langley95c29f32014-06-20 12:00:00 -0700420
421 return ret;
422}
423
David Benjamind93831d2015-10-29 13:19:12 -0400424int rsa_default_verify_raw(RSA *rsa, size_t *out_len, uint8_t *out,
425 size_t max_out, const uint8_t *in, size_t in_len,
426 int padding) {
Adam Langley95c29f32014-06-20 12:00:00 -0700427 const unsigned rsa_size = RSA_size(rsa);
428 BIGNUM *f, *result;
429 int ret = 0;
Adam Langley6887edb2014-06-20 12:00:00 -0700430 int r = -1;
Adam Langley95c29f32014-06-20 12:00:00 -0700431 uint8_t *buf = NULL;
432 BN_CTX *ctx = NULL;
433
Adam Langley95c29f32014-06-20 12:00:00 -0700434 if (max_out < rsa_size) {
David Benjamin3570d732015-06-29 00:28:17 -0400435 OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
Adam Langley95c29f32014-06-20 12:00:00 -0700436 return 0;
437 }
438
Brian Smith625475f2016-01-12 10:47:25 -1000439 if (!check_modulus_and_exponent_sizes(rsa)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700440 return 0;
441 }
442
443 ctx = BN_CTX_new();
444 if (ctx == NULL) {
445 goto err;
446 }
447
448 BN_CTX_start(ctx);
449 f = BN_CTX_get(ctx);
450 result = BN_CTX_get(ctx);
David Benjamin74279b62014-07-24 13:09:19 -0400451 if (padding == RSA_NO_PADDING) {
452 buf = out;
453 } else {
454 /* Allocate a temporary buffer to hold the padded plaintext. */
455 buf = OPENSSL_malloc(rsa_size);
456 if (buf == NULL) {
457 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
458 goto err;
459 }
460 }
461 if (!f || !result) {
David Benjamin3570d732015-06-29 00:28:17 -0400462 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700463 goto err;
464 }
465
Adam Langley6bc658d2014-08-18 13:29:45 -0700466 if (in_len != rsa_size) {
David Benjamin3570d732015-06-29 00:28:17 -0400467 OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_LEN_NOT_EQUAL_TO_MOD_LEN);
Adam Langley95c29f32014-06-20 12:00:00 -0700468 goto err;
469 }
470
471 if (BN_bin2bn(in, in_len, f) == NULL) {
472 goto err;
473 }
474
475 if (BN_ucmp(f, rsa->n) >= 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400476 OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
Adam Langley95c29f32014-06-20 12:00:00 -0700477 goto err;
478 }
479
Brian Smith24493a42016-03-25 09:12:48 -1000480 if (BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx) == NULL ||
481 !BN_mod_exp_mont(result, f, rsa->e, rsa->n, ctx, rsa->mont_n)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700482 goto err;
483 }
484
Adam Langley6887edb2014-06-20 12:00:00 -0700485 if (!BN_bn2bin_padded(buf, rsa_size, result)) {
David Benjamin3570d732015-06-29 00:28:17 -0400486 OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
Adam Langley6887edb2014-06-20 12:00:00 -0700487 goto err;
488 }
Adam Langley95c29f32014-06-20 12:00:00 -0700489
490 switch (padding) {
491 case RSA_PKCS1_PADDING:
Adam Langley6887edb2014-06-20 12:00:00 -0700492 r = RSA_padding_check_PKCS1_type_1(out, rsa_size, buf, rsa_size);
Adam Langley95c29f32014-06-20 12:00:00 -0700493 break;
494 case RSA_NO_PADDING:
David Benjamin74279b62014-07-24 13:09:19 -0400495 r = rsa_size;
Adam Langley95c29f32014-06-20 12:00:00 -0700496 break;
497 default:
David Benjamin3570d732015-06-29 00:28:17 -0400498 OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_PADDING_TYPE);
Adam Langley95c29f32014-06-20 12:00:00 -0700499 goto err;
500 }
501
502 if (r < 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400503 OPENSSL_PUT_ERROR(RSA, RSA_R_PADDING_CHECK_FAILED);
Adam Langley95c29f32014-06-20 12:00:00 -0700504 } else {
505 *out_len = r;
506 ret = 1;
507 }
508
509err:
510 if (ctx != NULL) {
511 BN_CTX_end(ctx);
512 BN_CTX_free(ctx);
513 }
David Benjamin74279b62014-07-24 13:09:19 -0400514 if (padding != RSA_NO_PADDING && buf != NULL) {
Adam Langley95c29f32014-06-20 12:00:00 -0700515 OPENSSL_cleanse(buf, rsa_size);
516 OPENSSL_free(buf);
517 }
518 return ret;
519}
520
David Benjamind93831d2015-10-29 13:19:12 -0400521int rsa_default_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in,
522 size_t len) {
Adam Langley6bc658d2014-08-18 13:29:45 -0700523 BIGNUM *f, *result;
524 BN_CTX *ctx = NULL;
525 unsigned blinding_index = 0;
526 BN_BLINDING *blinding = NULL;
527 int ret = 0;
528
529 ctx = BN_CTX_new();
530 if (ctx == NULL) {
531 goto err;
532 }
533 BN_CTX_start(ctx);
534 f = BN_CTX_get(ctx);
535 result = BN_CTX_get(ctx);
536
537 if (f == NULL || result == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400538 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
Adam Langley6bc658d2014-08-18 13:29:45 -0700539 goto err;
540 }
541
542 if (BN_bin2bn(in, len, f) == NULL) {
543 goto err;
544 }
545
546 if (BN_ucmp(f, rsa->n) >= 0) {
547 /* Usually the padding functions would catch this. */
David Benjamin3570d732015-06-29 00:28:17 -0400548 OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
Adam Langley6bc658d2014-08-18 13:29:45 -0700549 goto err;
550 }
551
552 if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
553 blinding = rsa_blinding_get(rsa, &blinding_index, ctx);
554 if (blinding == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400555 OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
Adam Langley6bc658d2014-08-18 13:29:45 -0700556 goto err;
557 }
Brian Smith617804a2016-02-08 20:36:51 -1000558 if (!BN_BLINDING_convert(f, blinding, ctx, rsa->mont_n)) {
Adam Langley6bc658d2014-08-18 13:29:45 -0700559 goto err;
560 }
561 }
562
563 if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
564 ((rsa->p != NULL) && (rsa->q != NULL) && (rsa->dmp1 != NULL) &&
565 (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
566 if (!rsa->meth->mod_exp(result, f, rsa, ctx)) {
567 goto err;
568 }
569 } else {
570 BIGNUM local_d;
571 BIGNUM *d = NULL;
572
573 BN_init(&local_d);
574 d = &local_d;
575 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
576
Brian Smith24493a42016-03-25 09:12:48 -1000577 if (BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx) == NULL ||
578 !BN_mod_exp_mont_consttime(result, f, d, rsa->n, ctx, rsa->mont_n)) {
Adam Langley6bc658d2014-08-18 13:29:45 -0700579 goto err;
580 }
581 }
582
583 if (blinding) {
Brian Smith642b0b82016-01-24 23:41:56 -1000584 if (!BN_BLINDING_invert(result, blinding, ctx)) {
Adam Langley6bc658d2014-08-18 13:29:45 -0700585 goto err;
586 }
587 }
588
589 if (!BN_bn2bin_padded(out, len, result)) {
David Benjamin3570d732015-06-29 00:28:17 -0400590 OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
Adam Langley6bc658d2014-08-18 13:29:45 -0700591 goto err;
592 }
593
594 ret = 1;
595
596err:
597 if (ctx != NULL) {
598 BN_CTX_end(ctx);
599 BN_CTX_free(ctx);
600 }
601 if (blinding != NULL) {
602 rsa_blinding_release(rsa, blinding, blinding_index);
603 }
604
605 return ret;
606}
607
Adam Langley95c29f32014-06-20 12:00:00 -0700608static int mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) {
609 BIGNUM *r1, *m1, *vrfy;
610 BIGNUM local_dmp1, local_dmq1, local_c, local_r1;
611 BIGNUM *dmp1, *dmq1, *c, *pr1;
612 int ret = 0;
Adam Langley839b8812015-05-26 11:36:46 -0700613 size_t i, num_additional_primes = 0;
614
615 if (rsa->additional_primes != NULL) {
616 num_additional_primes = sk_RSA_additional_prime_num(rsa->additional_primes);
617 }
Adam Langley95c29f32014-06-20 12:00:00 -0700618
619 BN_CTX_start(ctx);
620 r1 = BN_CTX_get(ctx);
621 m1 = BN_CTX_get(ctx);
622 vrfy = BN_CTX_get(ctx);
Brian Smith7cf60852016-03-19 22:39:37 -1000623 if (r1 == NULL ||
624 m1 == NULL ||
625 vrfy == NULL) {
626 goto err;
627 }
Adam Langley95c29f32014-06-20 12:00:00 -0700628
629 {
630 BIGNUM local_p, local_q;
631 BIGNUM *p = NULL, *q = NULL;
632
633 /* Make sure BN_mod_inverse in Montgomery intialization uses the
Brian Smith60a45aa2015-11-18 17:44:11 -1000634 * BN_FLG_CONSTTIME flag. */
Adam Langley95c29f32014-06-20 12:00:00 -0700635 BN_init(&local_p);
636 p = &local_p;
637 BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
638
639 BN_init(&local_q);
640 q = &local_q;
641 BN_with_flags(q, rsa->q, BN_FLG_CONSTTIME);
642
Brian Smith24493a42016-03-25 09:12:48 -1000643 if (BN_MONT_CTX_set_locked(&rsa->mont_p, &rsa->lock, p, ctx) == NULL ||
644 BN_MONT_CTX_set_locked(&rsa->mont_q, &rsa->lock, q, ctx) == NULL) {
645 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700646 }
647 }
648
Brian Smith24493a42016-03-25 09:12:48 -1000649 if (BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx) == NULL) {
650 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700651 }
652
653 /* compute I mod q */
654 c = &local_c;
655 BN_with_flags(c, I, BN_FLG_CONSTTIME);
656 if (!BN_mod(r1, c, rsa->q, ctx)) {
657 goto err;
658 }
659
660 /* compute r1^dmq1 mod q */
661 dmq1 = &local_dmq1;
662 BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
Brian Smith617804a2016-02-08 20:36:51 -1000663 if (!BN_mod_exp_mont_consttime(m1, r1, dmq1, rsa->q, ctx, rsa->mont_q)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700664 goto err;
665 }
666
667 /* compute I mod p */
668 c = &local_c;
669 BN_with_flags(c, I, BN_FLG_CONSTTIME);
670 if (!BN_mod(r1, c, rsa->p, ctx)) {
671 goto err;
672 }
673
674 /* compute r1^dmp1 mod p */
675 dmp1 = &local_dmp1;
676 BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
Brian Smith617804a2016-02-08 20:36:51 -1000677 if (!BN_mod_exp_mont_consttime(r0, r1, dmp1, rsa->p, ctx, rsa->mont_p)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700678 goto err;
679 }
680
681 if (!BN_sub(r0, r0, m1)) {
682 goto err;
683 }
684 /* This will help stop the size of r0 increasing, which does
685 * affect the multiply if it optimised for a power of 2 size */
686 if (BN_is_negative(r0)) {
687 if (!BN_add(r0, r0, rsa->p)) {
688 goto err;
689 }
690 }
691
692 if (!BN_mul(r1, r0, rsa->iqmp, ctx)) {
693 goto err;
694 }
695
696 /* Turn BN_FLG_CONSTTIME flag on before division operation */
697 pr1 = &local_r1;
698 BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
699
700 if (!BN_mod(r0, pr1, rsa->p, ctx)) {
701 goto err;
702 }
703
704 /* If p < q it is occasionally possible for the correction of
705 * adding 'p' if r0 is negative above to leave the result still
706 * negative. This can break the private key operations: the following
707 * second correction should *always* correct this rare occurrence.
708 * This will *never* happen with OpenSSL generated keys because
709 * they ensure p > q [steve] */
710 if (BN_is_negative(r0)) {
711 if (!BN_add(r0, r0, rsa->p)) {
712 goto err;
713 }
714 }
715 if (!BN_mul(r1, r0, rsa->q, ctx)) {
716 goto err;
717 }
718 if (!BN_add(r0, r1, m1)) {
719 goto err;
720 }
721
Adam Langley839b8812015-05-26 11:36:46 -0700722 for (i = 0; i < num_additional_primes; i++) {
723 /* multi-prime RSA. */
724 BIGNUM local_exp, local_prime;
725 BIGNUM *exp = &local_exp, *prime = &local_prime;
726 RSA_additional_prime *ap =
727 sk_RSA_additional_prime_value(rsa->additional_primes, i);
728
729 BN_with_flags(exp, ap->exp, BN_FLG_CONSTTIME);
730 BN_with_flags(prime, ap->prime, BN_FLG_CONSTTIME);
731
732 /* c will already point to a BIGNUM with the correct flags. */
733 if (!BN_mod(r1, c, prime, ctx)) {
734 goto err;
735 }
736
Brian Smith24493a42016-03-25 09:12:48 -1000737 if (BN_MONT_CTX_set_locked(&ap->mont, &rsa->lock, prime, ctx) == NULL ||
738 !BN_mod_exp_mont_consttime(m1, r1, exp, prime, ctx, ap->mont)) {
Adam Langley839b8812015-05-26 11:36:46 -0700739 goto err;
740 }
741
742 BN_set_flags(m1, BN_FLG_CONSTTIME);
743
744 if (!BN_sub(m1, m1, r0) ||
745 !BN_mul(m1, m1, ap->coeff, ctx) ||
746 !BN_mod(m1, m1, prime, ctx) ||
747 (BN_is_negative(m1) && !BN_add(m1, m1, prime)) ||
748 !BN_mul(m1, m1, ap->r, ctx) ||
749 !BN_add(r0, r0, m1)) {
750 goto err;
751 }
752 }
753
Adam Langley95c29f32014-06-20 12:00:00 -0700754 if (rsa->e && rsa->n) {
Brian Smith617804a2016-02-08 20:36:51 -1000755 if (!BN_mod_exp_mont(vrfy, r0, rsa->e, rsa->n, ctx, rsa->mont_n)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700756 goto err;
757 }
758 /* If 'I' was greater than (or equal to) rsa->n, the operation
759 * will be equivalent to using 'I mod n'. However, the result of
760 * the verify will *always* be less than 'n' so we don't check
761 * for absolute equality, just congruency. */
762 if (!BN_sub(vrfy, vrfy, I)) {
763 goto err;
764 }
765 if (!BN_mod(vrfy, vrfy, rsa->n, ctx)) {
766 goto err;
767 }
768 if (BN_is_negative(vrfy)) {
769 if (!BN_add(vrfy, vrfy, rsa->n)) {
770 goto err;
771 }
772 }
773 if (!BN_is_zero(vrfy)) {
774 /* 'I' and 'vrfy' aren't congruent mod n. Don't leak
775 * miscalculated CRT output, just do a raw (slower)
776 * mod_exp and return that instead. */
777
778 BIGNUM local_d;
779 BIGNUM *d = NULL;
780
781 d = &local_d;
782 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
Brian Smith617804a2016-02-08 20:36:51 -1000783 if (!BN_mod_exp_mont_consttime(r0, I, d, rsa->n, ctx, rsa->mont_n)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700784 goto err;
785 }
786 }
787 }
788 ret = 1;
789
790err:
791 BN_CTX_end(ctx);
792 return ret;
793}
794
David Benjamind93831d2015-10-29 13:19:12 -0400795int rsa_default_multi_prime_keygen(RSA *rsa, int bits, int num_primes,
796 BIGNUM *e_value, BN_GENCB *cb) {
Adam Langley95c29f32014-06-20 12:00:00 -0700797 BIGNUM *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL, *tmp;
798 BIGNUM local_r0, local_d, local_p;
799 BIGNUM *pr0, *d, *p;
Adam Langley839b8812015-05-26 11:36:46 -0700800 int prime_bits, ok = -1, n = 0, i, j;
Adam Langley95c29f32014-06-20 12:00:00 -0700801 BN_CTX *ctx = NULL;
Adam Langley839b8812015-05-26 11:36:46 -0700802 STACK_OF(RSA_additional_prime) *additional_primes = NULL;
803
804 if (num_primes < 2) {
805 ok = 0; /* we set our own err */
David Benjamin3570d732015-06-29 00:28:17 -0400806 OPENSSL_PUT_ERROR(RSA, RSA_R_MUST_HAVE_AT_LEAST_TWO_PRIMES);
Adam Langley839b8812015-05-26 11:36:46 -0700807 goto err;
808 }
Adam Langley95c29f32014-06-20 12:00:00 -0700809
810 ctx = BN_CTX_new();
811 if (ctx == NULL) {
812 goto err;
813 }
814 BN_CTX_start(ctx);
815 r0 = BN_CTX_get(ctx);
816 r1 = BN_CTX_get(ctx);
817 r2 = BN_CTX_get(ctx);
818 r3 = BN_CTX_get(ctx);
Brian Smithf4bbc2a2015-08-06 10:42:27 -0400819 if (r0 == NULL || r1 == NULL || r2 == NULL || r3 == NULL) {
Adam Langley95c29f32014-06-20 12:00:00 -0700820 goto err;
821 }
822
Adam Langley839b8812015-05-26 11:36:46 -0700823 if (num_primes > 2) {
824 additional_primes = sk_RSA_additional_prime_new_null();
825 if (additional_primes == NULL) {
826 goto err;
827 }
828 }
829
830 for (i = 2; i < num_primes; i++) {
831 RSA_additional_prime *ap = OPENSSL_malloc(sizeof(RSA_additional_prime));
832 if (ap == NULL) {
833 goto err;
834 }
835 memset(ap, 0, sizeof(RSA_additional_prime));
836 ap->prime = BN_new();
837 ap->exp = BN_new();
838 ap->coeff = BN_new();
839 ap->r = BN_new();
840 if (ap->prime == NULL ||
841 ap->exp == NULL ||
842 ap->coeff == NULL ||
843 ap->r == NULL ||
844 !sk_RSA_additional_prime_push(additional_primes, ap)) {
845 RSA_additional_prime_free(ap);
846 goto err;
847 }
848 }
Adam Langley95c29f32014-06-20 12:00:00 -0700849
850 /* We need the RSA components non-NULL */
David Benjamin6eb000d2015-02-11 01:17:41 -0500851 if (!rsa->n && ((rsa->n = BN_new()) == NULL)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700852 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500853 }
854 if (!rsa->d && ((rsa->d = BN_new()) == NULL)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700855 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500856 }
857 if (!rsa->e && ((rsa->e = BN_new()) == NULL)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700858 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500859 }
860 if (!rsa->p && ((rsa->p = BN_new()) == NULL)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700861 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500862 }
863 if (!rsa->q && ((rsa->q = BN_new()) == NULL)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700864 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500865 }
866 if (!rsa->dmp1 && ((rsa->dmp1 = BN_new()) == NULL)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700867 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500868 }
869 if (!rsa->dmq1 && ((rsa->dmq1 = BN_new()) == NULL)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700870 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500871 }
872 if (!rsa->iqmp && ((rsa->iqmp = BN_new()) == NULL)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700873 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500874 }
Adam Langley95c29f32014-06-20 12:00:00 -0700875
David Benjamin1c703cb2015-06-11 21:42:14 -0400876 if (!BN_copy(rsa->e, e_value)) {
877 goto err;
878 }
Adam Langley95c29f32014-06-20 12:00:00 -0700879
880 /* generate p and q */
Adam Langley839b8812015-05-26 11:36:46 -0700881 prime_bits = (bits + (num_primes - 1)) / num_primes;
Adam Langley95c29f32014-06-20 12:00:00 -0700882 for (;;) {
Adam Langley839b8812015-05-26 11:36:46 -0700883 if (!BN_generate_prime_ex(rsa->p, prime_bits, 0, NULL, NULL, cb) ||
David Benjamin6eb000d2015-02-11 01:17:41 -0500884 !BN_sub(r2, rsa->p, BN_value_one()) ||
885 !BN_gcd(r1, r2, rsa->e, ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700886 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500887 }
888 if (BN_is_one(r1)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700889 break;
David Benjamin6eb000d2015-02-11 01:17:41 -0500890 }
891 if (!BN_GENCB_call(cb, 2, n++)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700892 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500893 }
Adam Langley95c29f32014-06-20 12:00:00 -0700894 }
David Benjamin6eb000d2015-02-11 01:17:41 -0500895 if (!BN_GENCB_call(cb, 3, 0)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700896 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500897 }
Adam Langley839b8812015-05-26 11:36:46 -0700898 prime_bits = ((bits - prime_bits) + (num_primes - 2)) / (num_primes - 1);
Adam Langley95c29f32014-06-20 12:00:00 -0700899 for (;;) {
900 /* When generating ridiculously small keys, we can get stuck
901 * continually regenerating the same prime values. Check for
902 * this and bail if it happens 3 times. */
903 unsigned int degenerate = 0;
904 do {
Adam Langley839b8812015-05-26 11:36:46 -0700905 if (!BN_generate_prime_ex(rsa->q, prime_bits, 0, NULL, NULL, cb)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700906 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500907 }
Adam Langley95c29f32014-06-20 12:00:00 -0700908 } while ((BN_cmp(rsa->p, rsa->q) == 0) && (++degenerate < 3));
909 if (degenerate == 3) {
910 ok = 0; /* we set our own err */
David Benjamin3570d732015-06-29 00:28:17 -0400911 OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL);
Adam Langley95c29f32014-06-20 12:00:00 -0700912 goto err;
913 }
David Benjamin6eb000d2015-02-11 01:17:41 -0500914 if (!BN_sub(r2, rsa->q, BN_value_one()) ||
915 !BN_gcd(r1, r2, rsa->e, ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700916 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500917 }
918 if (BN_is_one(r1)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700919 break;
David Benjamin6eb000d2015-02-11 01:17:41 -0500920 }
921 if (!BN_GENCB_call(cb, 2, n++)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700922 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500923 }
Adam Langley95c29f32014-06-20 12:00:00 -0700924 }
Adam Langley839b8812015-05-26 11:36:46 -0700925
926 if (!BN_GENCB_call(cb, 3, 1) ||
927 !BN_mul(rsa->n, rsa->p, rsa->q, ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700928 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500929 }
Adam Langley839b8812015-05-26 11:36:46 -0700930
931 for (i = 2; i < num_primes; i++) {
932 RSA_additional_prime *ap =
933 sk_RSA_additional_prime_value(additional_primes, i - 2);
934 prime_bits = ((bits - BN_num_bits(rsa->n)) + (num_primes - (i + 1))) /
935 (num_primes - i);
936
937 for (;;) {
938 if (!BN_generate_prime_ex(ap->prime, prime_bits, 0, NULL, NULL, cb)) {
939 goto err;
940 }
941 if (BN_cmp(rsa->p, ap->prime) == 0 ||
942 BN_cmp(rsa->q, ap->prime) == 0) {
943 continue;
944 }
945
946 for (j = 0; j < i - 2; j++) {
947 if (BN_cmp(sk_RSA_additional_prime_value(additional_primes, j)->prime,
948 ap->prime) == 0) {
949 break;
950 }
951 }
952 if (j != i - 2) {
953 continue;
954 }
955
956 if (!BN_sub(r2, ap->prime, BN_value_one()) ||
957 !BN_gcd(r1, r2, rsa->e, ctx)) {
958 goto err;
959 }
960
961 if (!BN_is_one(r1)) {
962 continue;
963 }
964 if (i != num_primes - 1) {
965 break;
966 }
967
968 /* For the last prime we'll check that it makes n large enough. In the
969 * two prime case this isn't a problem because we generate primes with
970 * the top two bits set and so the product is always of the expected
971 * size. In the multi prime case, this doesn't follow. */
972 if (!BN_mul(r1, rsa->n, ap->prime, ctx)) {
973 goto err;
974 }
Adam Langley96c2a282015-06-02 14:16:44 -0700975 if (BN_num_bits(r1) == (unsigned) bits) {
Adam Langley839b8812015-05-26 11:36:46 -0700976 break;
977 }
978
979 if (!BN_GENCB_call(cb, 2, n++)) {
980 goto err;
981 }
982 }
983
984 /* ap->r is is the product of all the primes prior to the current one
985 * (including p and q). */
986 if (!BN_copy(ap->r, rsa->n)) {
987 goto err;
988 }
989 if (i == num_primes - 1) {
990 /* In the case of the last prime, we calculated n as |r1| in the loop
991 * above. */
992 if (!BN_copy(rsa->n, r1)) {
993 goto err;
994 }
995 } else if (!BN_mul(rsa->n, rsa->n, ap->prime, ctx)) {
996 goto err;
997 }
998
999 if (!BN_GENCB_call(cb, 3, 1)) {
1000 goto err;
1001 }
1002 }
1003
Adam Langley95c29f32014-06-20 12:00:00 -07001004 if (BN_cmp(rsa->p, rsa->q) < 0) {
1005 tmp = rsa->p;
1006 rsa->p = rsa->q;
1007 rsa->q = tmp;
1008 }
1009
Adam Langley95c29f32014-06-20 12:00:00 -07001010 /* calculate d */
David Benjamin6eb000d2015-02-11 01:17:41 -05001011 if (!BN_sub(r1, rsa->p, BN_value_one())) {
Adam Langley95c29f32014-06-20 12:00:00 -07001012 goto err; /* p-1 */
David Benjamin6eb000d2015-02-11 01:17:41 -05001013 }
1014 if (!BN_sub(r2, rsa->q, BN_value_one())) {
Adam Langley95c29f32014-06-20 12:00:00 -07001015 goto err; /* q-1 */
David Benjamin6eb000d2015-02-11 01:17:41 -05001016 }
1017 if (!BN_mul(r0, r1, r2, ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -07001018 goto err; /* (p-1)(q-1) */
David Benjamin6eb000d2015-02-11 01:17:41 -05001019 }
Adam Langley839b8812015-05-26 11:36:46 -07001020 for (i = 2; i < num_primes; i++) {
1021 RSA_additional_prime *ap =
1022 sk_RSA_additional_prime_value(additional_primes, i - 2);
1023 if (!BN_sub(r3, ap->prime, BN_value_one()) ||
1024 !BN_mul(r0, r0, r3, ctx)) {
1025 goto err;
1026 }
1027 }
Adam Langley95c29f32014-06-20 12:00:00 -07001028 pr0 = &local_r0;
1029 BN_with_flags(pr0, r0, BN_FLG_CONSTTIME);
David Benjamin6eb000d2015-02-11 01:17:41 -05001030 if (!BN_mod_inverse(rsa->d, rsa->e, pr0, ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -07001031 goto err; /* d */
David Benjamin6eb000d2015-02-11 01:17:41 -05001032 }
Adam Langley95c29f32014-06-20 12:00:00 -07001033
1034 /* set up d for correct BN_FLG_CONSTTIME flag */
1035 d = &local_d;
1036 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
1037
1038 /* calculate d mod (p-1) */
David Benjamin6eb000d2015-02-11 01:17:41 -05001039 if (!BN_mod(rsa->dmp1, d, r1, ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -07001040 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -05001041 }
Adam Langley95c29f32014-06-20 12:00:00 -07001042
1043 /* calculate d mod (q-1) */
David Benjamin6eb000d2015-02-11 01:17:41 -05001044 if (!BN_mod(rsa->dmq1, d, r2, ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -07001045 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -05001046 }
Adam Langley95c29f32014-06-20 12:00:00 -07001047
1048 /* calculate inverse of q mod p */
1049 p = &local_p;
1050 BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
1051
David Benjamin6eb000d2015-02-11 01:17:41 -05001052 if (!BN_mod_inverse(rsa->iqmp, rsa->q, p, ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -07001053 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -05001054 }
Adam Langley95c29f32014-06-20 12:00:00 -07001055
Adam Langley839b8812015-05-26 11:36:46 -07001056 for (i = 2; i < num_primes; i++) {
1057 RSA_additional_prime *ap =
1058 sk_RSA_additional_prime_value(additional_primes, i - 2);
1059 if (!BN_sub(ap->exp, ap->prime, BN_value_one()) ||
1060 !BN_mod(ap->exp, rsa->d, ap->exp, ctx) ||
1061 !BN_mod_inverse(ap->coeff, ap->r, ap->prime, ctx)) {
1062 goto err;
1063 }
1064 }
1065
Adam Langley95c29f32014-06-20 12:00:00 -07001066 ok = 1;
Adam Langley839b8812015-05-26 11:36:46 -07001067 rsa->additional_primes = additional_primes;
1068 additional_primes = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001069
1070err:
1071 if (ok == -1) {
David Benjamin3570d732015-06-29 00:28:17 -04001072 OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
Adam Langley95c29f32014-06-20 12:00:00 -07001073 ok = 0;
1074 }
1075 if (ctx != NULL) {
1076 BN_CTX_end(ctx);
1077 BN_CTX_free(ctx);
1078 }
Adam Langley839b8812015-05-26 11:36:46 -07001079 sk_RSA_additional_prime_pop_free(additional_primes,
1080 RSA_additional_prime_free);
Adam Langley95c29f32014-06-20 12:00:00 -07001081 return ok;
1082}
1083
David Benjamind93831d2015-10-29 13:19:12 -04001084int rsa_default_keygen(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) {
1085 return rsa_default_multi_prime_keygen(rsa, bits, 2 /* num primes */, e_value,
1086 cb);
Adam Langley839b8812015-05-26 11:36:46 -07001087}
1088
David Benjamind93831d2015-10-29 13:19:12 -04001089/* Many of these methods are NULL to more easily drop unused functions. The
1090 * wrapper functions will select the appropriate |rsa_default_*| for all
1091 * methods. */
1092const RSA_METHOD RSA_default_method = {
Adam Langley95c29f32014-06-20 12:00:00 -07001093 {
1094 0 /* references */,
1095 1 /* is_static */,
1096 },
1097 NULL /* app_data */,
1098
1099 NULL /* init */,
David Benjamind93831d2015-10-29 13:19:12 -04001100 NULL /* finish (defaults to rsa_default_finish) */,
Adam Langley95c29f32014-06-20 12:00:00 -07001101
David Benjamind93831d2015-10-29 13:19:12 -04001102 NULL /* size (defaults to rsa_default_size) */,
David Benjamin925fee32014-07-11 14:14:08 -04001103
Adam Langley95c29f32014-06-20 12:00:00 -07001104 NULL /* sign */,
1105 NULL /* verify */,
1106
David Benjamind93831d2015-10-29 13:19:12 -04001107 NULL /* encrypt (defaults to rsa_default_encrypt) */,
1108 NULL /* sign_raw (defaults to rsa_default_sign_raw) */,
1109 NULL /* decrypt (defaults to rsa_default_decrypt) */,
1110 NULL /* verify_raw (defaults to rsa_default_verify_raw) */,
Adam Langley95c29f32014-06-20 12:00:00 -07001111
David Benjamind93831d2015-10-29 13:19:12 -04001112 NULL /* private_transform (defaults to rsa_default_private_transform) */,
Adam Langley6bc658d2014-08-18 13:29:45 -07001113
David Benjamind93831d2015-10-29 13:19:12 -04001114 mod_exp,
Brian Smith617804a2016-02-08 20:36:51 -10001115 NULL /* bn_mod_exp */,
Adam Langley95c29f32014-06-20 12:00:00 -07001116
1117 RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE,
1118
David Benjamind93831d2015-10-29 13:19:12 -04001119 NULL /* keygen (defaults to rsa_default_keygen) */,
1120 NULL /* multi_prime_keygen (defaults to rsa_default_multi_prime_keygen) */,
Adam Langley626c6862015-09-11 16:17:44 -07001121
1122 NULL /* supports_digest */,
Adam Langley95c29f32014-06-20 12:00:00 -07001123};