blob: 012a9849633d8516b3689ef6edaa0252c7d1ca50 [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>
David Benjaminfb8b7632017-04-10 18:35:22 -040060#include <limits.h>
Adam Langley2b2d66d2015-01-30 17:08:37 -080061#include <string.h>
62
Adam Langley95c29f32014-06-20 12:00:00 -070063#include <openssl/bn.h>
64#include <openssl/err.h>
65#include <openssl/mem.h>
Brian Smith054e6822015-03-27 21:12:01 -100066#include <openssl/thread.h>
David Benjaminfb8b7632017-04-10 18:35:22 -040067#include <openssl/type_check.h>
Adam Langley95c29f32014-06-20 12:00:00 -070068
69#include "internal.h"
Adam Langley5c38c052017-04-28 14:47:06 -070070#include "../fipsmodule/bn/internal.h"
Adam Langley683d7bd2015-04-13 11:04:14 -070071#include "../internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -070072
73
Brian Smith625475f2016-01-12 10:47:25 -100074static int check_modulus_and_exponent_sizes(const RSA *rsa) {
75 unsigned rsa_bits = BN_num_bits(rsa->n);
David Benjamincfa9de82016-03-14 14:19:41 -040076
Brian Smith625475f2016-01-12 10:47:25 -100077 if (rsa_bits > 16 * 1024) {
78 OPENSSL_PUT_ERROR(RSA, RSA_R_MODULUS_TOO_LARGE);
79 return 0;
80 }
Adam Langley95c29f32014-06-20 12:00:00 -070081
David Benjamincfa9de82016-03-14 14:19:41 -040082 /* Mitigate DoS attacks by limiting the exponent size. 33 bits was chosen as
83 * the limit based on the recommendations in [1] and [2]. Windows CryptoAPI
84 * doesn't support values larger than 32 bits [3], so it is unlikely that
85 * exponents larger than 32 bits are being used for anything Windows commonly
86 * does.
87 *
88 * [1] https://www.imperialviolet.org/2012/03/16/rsae.html
89 * [2] https://www.imperialviolet.org/2012/03/17/rsados.html
90 * [3] https://msdn.microsoft.com/en-us/library/aa387685(VS.85).aspx */
91 static const unsigned kMaxExponentBits = 33;
92
93 if (BN_num_bits(rsa->e) > kMaxExponentBits) {
Brian Smith625475f2016-01-12 10:47:25 -100094 OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_E_VALUE);
95 return 0;
96 }
97
David Benjamincfa9de82016-03-14 14:19:41 -040098 /* Verify |n > e|. Comparing |rsa_bits| to |kMaxExponentBits| is a small
99 * shortcut to comparing |n| and |e| directly. In reality, |kMaxExponentBits|
100 * is much smaller than the minimum RSA key size that any application should
101 * accept. */
102 if (rsa_bits <= kMaxExponentBits) {
103 OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL);
Brian Smith625475f2016-01-12 10:47:25 -1000104 return 0;
105 }
David Benjamincfa9de82016-03-14 14:19:41 -0400106 assert(BN_ucmp(rsa->n, rsa->e) > 0);
Brian Smith625475f2016-01-12 10:47:25 -1000107
108 return 1;
109}
Adam Langley95c29f32014-06-20 12:00:00 -0700110
David Benjamind93831d2015-10-29 13:19:12 -0400111size_t rsa_default_size(const RSA *rsa) {
David Benjamin925fee32014-07-11 14:14:08 -0400112 return BN_num_bytes(rsa->n);
113}
114
David Benjamin073391f2017-05-03 15:03:35 -0400115int RSA_encrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
116 const uint8_t *in, size_t in_len, int padding) {
117 if (rsa->n == NULL || rsa->e == NULL) {
118 OPENSSL_PUT_ERROR(RSA, RSA_R_VALUE_MISSING);
119 return 0;
120 }
121
Adam Langley95c29f32014-06-20 12:00:00 -0700122 const unsigned rsa_size = RSA_size(rsa);
Adam Langley95c29f32014-06-20 12:00:00 -0700123 BIGNUM *f, *result;
124 uint8_t *buf = NULL;
125 BN_CTX *ctx = NULL;
Adam Langley6887edb2014-06-20 12:00:00 -0700126 int i, ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700127
Adam Langley95c29f32014-06-20 12:00:00 -0700128 if (max_out < rsa_size) {
David Benjamin3570d732015-06-29 00:28:17 -0400129 OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
Adam Langley95c29f32014-06-20 12:00:00 -0700130 return 0;
131 }
132
Brian Smith625475f2016-01-12 10:47:25 -1000133 if (!check_modulus_and_exponent_sizes(rsa)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700134 return 0;
135 }
136
137 ctx = BN_CTX_new();
138 if (ctx == NULL) {
139 goto err;
140 }
141
142 BN_CTX_start(ctx);
143 f = BN_CTX_get(ctx);
144 result = BN_CTX_get(ctx);
145 buf = OPENSSL_malloc(rsa_size);
146 if (!f || !result || !buf) {
David Benjamin3570d732015-06-29 00:28:17 -0400147 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700148 goto err;
149 }
150
151 switch (padding) {
152 case RSA_PKCS1_PADDING:
153 i = RSA_padding_add_PKCS1_type_2(buf, rsa_size, in, in_len);
154 break;
155 case RSA_PKCS1_OAEP_PADDING:
Adam Langley6887edb2014-06-20 12:00:00 -0700156 /* Use the default parameters: SHA-1 for both hashes and no label. */
157 i = RSA_padding_add_PKCS1_OAEP_mgf1(buf, rsa_size, in, in_len,
158 NULL, 0, NULL, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700159 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700160 case RSA_NO_PADDING:
161 i = RSA_padding_add_none(buf, rsa_size, in, in_len);
162 break;
163 default:
David Benjamin3570d732015-06-29 00:28:17 -0400164 OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_PADDING_TYPE);
Adam Langley95c29f32014-06-20 12:00:00 -0700165 goto err;
166 }
167
168 if (i <= 0) {
169 goto err;
170 }
171
172 if (BN_bin2bn(buf, rsa_size, f) == NULL) {
173 goto err;
174 }
175
176 if (BN_ucmp(f, rsa->n) >= 0) {
177 /* usually the padding functions would catch this */
David Benjamin3570d732015-06-29 00:28:17 -0400178 OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
Adam Langley95c29f32014-06-20 12:00:00 -0700179 goto err;
180 }
181
Brian Smithd0357302016-03-25 10:11:04 -1000182 if (!BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx) ||
Brian Smith24493a42016-03-25 09:12:48 -1000183 !BN_mod_exp_mont(result, f, rsa->e, rsa->n, ctx, rsa->mont_n)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700184 goto err;
185 }
186
187 /* put in leading 0 bytes if the number is less than the length of the
188 * modulus */
Adam Langley6887edb2014-06-20 12:00:00 -0700189 if (!BN_bn2bin_padded(out, rsa_size, result)) {
David Benjamin3570d732015-06-29 00:28:17 -0400190 OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
Adam Langley6887edb2014-06-20 12:00:00 -0700191 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700192 }
193
194 *out_len = rsa_size;
195 ret = 1;
196
197err:
198 if (ctx != NULL) {
199 BN_CTX_end(ctx);
200 BN_CTX_free(ctx);
201 }
202 if (buf != NULL) {
203 OPENSSL_cleanse(buf, rsa_size);
204 OPENSSL_free(buf);
205 }
206
207 return ret;
208}
209
210/* MAX_BLINDINGS_PER_RSA defines the maximum number of cached BN_BLINDINGs per
211 * RSA*. Then this limit is exceeded, BN_BLINDING objects will be created and
212 * destroyed as needed. */
213#define MAX_BLINDINGS_PER_RSA 1024
214
215/* rsa_blinding_get returns a BN_BLINDING to use with |rsa|. It does this by
216 * allocating one of the cached BN_BLINDING objects in |rsa->blindings|. If
217 * none are free, the cache will be extended by a extra element and the new
218 * BN_BLINDING is returned.
219 *
220 * On success, the index of the assigned BN_BLINDING is written to
221 * |*index_used| and must be passed to |rsa_blinding_release| when finished. */
222static BN_BLINDING *rsa_blinding_get(RSA *rsa, unsigned *index_used,
223 BN_CTX *ctx) {
Brian Smith3426d102016-03-17 16:10:04 -1000224 assert(ctx != NULL);
Brian Smithcbf56a52016-03-21 11:25:39 -1000225 assert(rsa->mont_n != NULL);
226
Adam Langley95c29f32014-06-20 12:00:00 -0700227 BN_BLINDING *ret = NULL;
228 BN_BLINDING **new_blindings;
229 uint8_t *new_blindings_inuse;
230 char overflow = 0;
231
Adam Langley683d7bd2015-04-13 11:04:14 -0700232 CRYPTO_MUTEX_lock_write(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700233
Adam Langley33672732015-03-31 18:55:53 -0700234 unsigned i;
235 for (i = 0; i < rsa->num_blindings; i++) {
236 if (rsa->blindings_inuse[i] == 0) {
237 rsa->blindings_inuse[i] = 1;
238 ret = rsa->blindings[i];
239 *index_used = i;
240 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700241 }
242 }
243
244 if (ret != NULL) {
David Benjamin29270de2016-05-24 15:28:36 +0000245 CRYPTO_MUTEX_unlock_write(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700246 return ret;
247 }
248
249 overflow = rsa->num_blindings >= MAX_BLINDINGS_PER_RSA;
250
251 /* We didn't find a free BN_BLINDING to use so increase the length of
252 * the arrays by one and use the newly created element. */
253
David Benjamin29270de2016-05-24 15:28:36 +0000254 CRYPTO_MUTEX_unlock_write(&rsa->lock);
Brian Smith86361a32016-03-26 19:42:31 -1000255 ret = BN_BLINDING_new();
Adam Langley95c29f32014-06-20 12:00:00 -0700256 if (ret == NULL) {
257 return NULL;
258 }
259
260 if (overflow) {
261 /* We cannot add any more cached BN_BLINDINGs so we use |ret|
262 * and mark it for destruction in |rsa_blinding_release|. */
263 *index_used = MAX_BLINDINGS_PER_RSA;
264 return ret;
265 }
266
Adam Langley683d7bd2015-04-13 11:04:14 -0700267 CRYPTO_MUTEX_lock_write(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700268
269 new_blindings =
270 OPENSSL_malloc(sizeof(BN_BLINDING *) * (rsa->num_blindings + 1));
271 if (new_blindings == NULL) {
272 goto err1;
273 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500274 OPENSSL_memcpy(new_blindings, rsa->blindings,
Adam Langley95c29f32014-06-20 12:00:00 -0700275 sizeof(BN_BLINDING *) * rsa->num_blindings);
276 new_blindings[rsa->num_blindings] = ret;
277
278 new_blindings_inuse = OPENSSL_malloc(rsa->num_blindings + 1);
279 if (new_blindings_inuse == NULL) {
280 goto err2;
281 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500282 OPENSSL_memcpy(new_blindings_inuse, rsa->blindings_inuse, rsa->num_blindings);
Adam Langley95c29f32014-06-20 12:00:00 -0700283 new_blindings_inuse[rsa->num_blindings] = 1;
284 *index_used = rsa->num_blindings;
285
David Benjamind8b65c82015-04-22 16:09:09 -0400286 OPENSSL_free(rsa->blindings);
Adam Langley95c29f32014-06-20 12:00:00 -0700287 rsa->blindings = new_blindings;
David Benjamind8b65c82015-04-22 16:09:09 -0400288 OPENSSL_free(rsa->blindings_inuse);
Adam Langley95c29f32014-06-20 12:00:00 -0700289 rsa->blindings_inuse = new_blindings_inuse;
290 rsa->num_blindings++;
291
David Benjamin29270de2016-05-24 15:28:36 +0000292 CRYPTO_MUTEX_unlock_write(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700293 return ret;
294
295err2:
296 OPENSSL_free(new_blindings);
297
298err1:
David Benjamin29270de2016-05-24 15:28:36 +0000299 CRYPTO_MUTEX_unlock_write(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700300 BN_BLINDING_free(ret);
301 return NULL;
302}
303
304/* rsa_blinding_release marks the cached BN_BLINDING at the given index as free
305 * for other threads to use. */
306static void rsa_blinding_release(RSA *rsa, BN_BLINDING *blinding,
307 unsigned blinding_index) {
308 if (blinding_index == MAX_BLINDINGS_PER_RSA) {
309 /* This blinding wasn't cached. */
310 BN_BLINDING_free(blinding);
311 return;
312 }
313
Adam Langley683d7bd2015-04-13 11:04:14 -0700314 CRYPTO_MUTEX_lock_write(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700315 rsa->blindings_inuse[blinding_index] = 0;
David Benjamin29270de2016-05-24 15:28:36 +0000316 CRYPTO_MUTEX_unlock_write(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700317}
318
319/* signing */
David Benjamind93831d2015-10-29 13:19:12 -0400320int rsa_default_sign_raw(RSA *rsa, size_t *out_len, uint8_t *out,
321 size_t max_out, const uint8_t *in, size_t in_len,
322 int padding) {
Adam Langley95c29f32014-06-20 12:00:00 -0700323 const unsigned rsa_size = RSA_size(rsa);
Adam Langley95c29f32014-06-20 12:00:00 -0700324 uint8_t *buf = NULL;
Adam Langley6887edb2014-06-20 12:00:00 -0700325 int i, ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700326
327 if (max_out < rsa_size) {
David Benjamin3570d732015-06-29 00:28:17 -0400328 OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
Adam Langley95c29f32014-06-20 12:00:00 -0700329 return 0;
330 }
331
Adam Langley95c29f32014-06-20 12:00:00 -0700332 buf = OPENSSL_malloc(rsa_size);
Adam Langley6bc658d2014-08-18 13:29:45 -0700333 if (buf == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400334 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700335 goto err;
336 }
337
338 switch (padding) {
339 case RSA_PKCS1_PADDING:
340 i = RSA_padding_add_PKCS1_type_1(buf, rsa_size, in, in_len);
341 break;
342 case RSA_NO_PADDING:
343 i = RSA_padding_add_none(buf, rsa_size, in, in_len);
344 break;
345 default:
David Benjamin3570d732015-06-29 00:28:17 -0400346 OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_PADDING_TYPE);
Adam Langley95c29f32014-06-20 12:00:00 -0700347 goto err;
348 }
Adam Langley6bc658d2014-08-18 13:29:45 -0700349
Adam Langley95c29f32014-06-20 12:00:00 -0700350 if (i <= 0) {
351 goto err;
352 }
353
Adam Langley6bc658d2014-08-18 13:29:45 -0700354 if (!RSA_private_transform(rsa, out, buf, rsa_size)) {
Adam Langley5f5bf6f2015-02-24 13:49:41 -0800355 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700356 }
357
358 *out_len = rsa_size;
359 ret = 1;
360
361err:
Adam Langley95c29f32014-06-20 12:00:00 -0700362 if (buf != NULL) {
363 OPENSSL_cleanse(buf, rsa_size);
364 OPENSSL_free(buf);
365 }
Adam Langley95c29f32014-06-20 12:00:00 -0700366
367 return ret;
368}
369
David Benjamind93831d2015-10-29 13:19:12 -0400370int rsa_default_decrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
371 const uint8_t *in, size_t in_len, int padding) {
Adam Langley95c29f32014-06-20 12:00:00 -0700372 const unsigned rsa_size = RSA_size(rsa);
Adam Langley95c29f32014-06-20 12:00:00 -0700373 uint8_t *buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700374 int ret = 0;
375
376 if (max_out < rsa_size) {
David Benjamin3570d732015-06-29 00:28:17 -0400377 OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
Adam Langley95c29f32014-06-20 12:00:00 -0700378 return 0;
379 }
380
David Benjamin74279b62014-07-24 13:09:19 -0400381 if (padding == RSA_NO_PADDING) {
382 buf = out;
383 } else {
384 /* Allocate a temporary buffer to hold the padded plaintext. */
385 buf = OPENSSL_malloc(rsa_size);
386 if (buf == NULL) {
387 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
388 goto err;
389 }
Adam Langley95c29f32014-06-20 12:00:00 -0700390 }
391
Adam Langley6bc658d2014-08-18 13:29:45 -0700392 if (in_len != rsa_size) {
David Benjamin3570d732015-06-29 00:28:17 -0400393 OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_LEN_NOT_EQUAL_TO_MOD_LEN);
Adam Langley95c29f32014-06-20 12:00:00 -0700394 goto err;
395 }
396
Adam Langley6bc658d2014-08-18 13:29:45 -0700397 if (!RSA_private_transform(rsa, buf, in, rsa_size)) {
Adam Langley6887edb2014-06-20 12:00:00 -0700398 goto err;
399 }
Adam Langley95c29f32014-06-20 12:00:00 -0700400
401 switch (padding) {
402 case RSA_PKCS1_PADDING:
David Benjaminb0ad3d72017-03-16 13:15:31 -0400403 ret =
404 RSA_padding_check_PKCS1_type_2(out, out_len, rsa_size, buf, rsa_size);
Adam Langley95c29f32014-06-20 12:00:00 -0700405 break;
406 case RSA_PKCS1_OAEP_PADDING:
Adam Langley6887edb2014-06-20 12:00:00 -0700407 /* Use the default parameters: SHA-1 for both hashes and no label. */
David Benjaminb0ad3d72017-03-16 13:15:31 -0400408 ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, out_len, rsa_size, buf,
409 rsa_size, NULL, 0, NULL, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700410 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700411 case RSA_NO_PADDING:
David Benjaminb0ad3d72017-03-16 13:15:31 -0400412 *out_len = rsa_size;
413 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700414 break;
415 default:
David Benjamin3570d732015-06-29 00:28:17 -0400416 OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_PADDING_TYPE);
Adam Langley95c29f32014-06-20 12:00:00 -0700417 goto err;
418 }
419
David Benjaminb0ad3d72017-03-16 13:15:31 -0400420 if (!ret) {
David Benjamin3570d732015-06-29 00:28:17 -0400421 OPENSSL_PUT_ERROR(RSA, RSA_R_PADDING_CHECK_FAILED);
Adam Langley95c29f32014-06-20 12:00:00 -0700422 }
423
424err:
David Benjamin74279b62014-07-24 13:09:19 -0400425 if (padding != RSA_NO_PADDING && buf != NULL) {
Adam Langley95c29f32014-06-20 12:00:00 -0700426 OPENSSL_cleanse(buf, rsa_size);
427 OPENSSL_free(buf);
428 }
Adam Langley95c29f32014-06-20 12:00:00 -0700429
430 return ret;
431}
432
Brian Smithf08c1c62016-03-25 13:24:46 -1000433static int mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
434
Brian Smithc0b196d2016-03-04 08:54:07 -1000435int RSA_verify_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
436 const uint8_t *in, size_t in_len, int padding) {
437 if (rsa->n == NULL || rsa->e == NULL) {
438 OPENSSL_PUT_ERROR(RSA, RSA_R_VALUE_MISSING);
439 return 0;
440 }
441
Adam Langley95c29f32014-06-20 12:00:00 -0700442 const unsigned rsa_size = RSA_size(rsa);
443 BIGNUM *f, *result;
Adam Langley95c29f32014-06-20 12:00:00 -0700444
Adam Langley95c29f32014-06-20 12:00:00 -0700445 if (max_out < rsa_size) {
David Benjamin3570d732015-06-29 00:28:17 -0400446 OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
Adam Langley95c29f32014-06-20 12:00:00 -0700447 return 0;
448 }
449
Brian Smith99022622016-03-04 09:20:07 -1000450 if (in_len != rsa_size) {
451 OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_LEN_NOT_EQUAL_TO_MOD_LEN);
452 return 0;
453 }
454
Brian Smith625475f2016-01-12 10:47:25 -1000455 if (!check_modulus_and_exponent_sizes(rsa)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700456 return 0;
457 }
458
Brian Smith2a920312016-03-04 13:42:47 -1000459 BN_CTX *ctx = BN_CTX_new();
Adam Langley95c29f32014-06-20 12:00:00 -0700460 if (ctx == NULL) {
Brian Smith2a920312016-03-04 13:42:47 -1000461 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700462 }
463
Brian Smith2a920312016-03-04 13:42:47 -1000464 int ret = 0;
465 uint8_t *buf = NULL;
466
Adam Langley95c29f32014-06-20 12:00:00 -0700467 BN_CTX_start(ctx);
468 f = BN_CTX_get(ctx);
469 result = BN_CTX_get(ctx);
Brian Smith2a920312016-03-04 13:42:47 -1000470 if (f == NULL || result == NULL) {
471 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
472 goto err;
473 }
474
David Benjamin74279b62014-07-24 13:09:19 -0400475 if (padding == RSA_NO_PADDING) {
476 buf = out;
477 } else {
478 /* Allocate a temporary buffer to hold the padded plaintext. */
479 buf = OPENSSL_malloc(rsa_size);
480 if (buf == NULL) {
481 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
482 goto err;
483 }
484 }
Adam Langley95c29f32014-06-20 12:00:00 -0700485
Adam Langley95c29f32014-06-20 12:00:00 -0700486 if (BN_bin2bn(in, in_len, f) == NULL) {
487 goto err;
488 }
489
490 if (BN_ucmp(f, rsa->n) >= 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400491 OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
Adam Langley95c29f32014-06-20 12:00:00 -0700492 goto err;
493 }
494
Brian Smithd0357302016-03-25 10:11:04 -1000495 if (!BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx) ||
Brian Smith24493a42016-03-25 09:12:48 -1000496 !BN_mod_exp_mont(result, f, rsa->e, rsa->n, ctx, rsa->mont_n)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700497 goto err;
498 }
499
Adam Langley6887edb2014-06-20 12:00:00 -0700500 if (!BN_bn2bin_padded(buf, rsa_size, result)) {
David Benjamin3570d732015-06-29 00:28:17 -0400501 OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
Adam Langley6887edb2014-06-20 12:00:00 -0700502 goto err;
503 }
Adam Langley95c29f32014-06-20 12:00:00 -0700504
505 switch (padding) {
506 case RSA_PKCS1_PADDING:
David Benjamin43ea2042017-03-16 11:54:11 -0400507 ret =
508 RSA_padding_check_PKCS1_type_1(out, out_len, rsa_size, buf, rsa_size);
Adam Langley95c29f32014-06-20 12:00:00 -0700509 break;
510 case RSA_NO_PADDING:
David Benjamin43ea2042017-03-16 11:54:11 -0400511 ret = 1;
512 *out_len = rsa_size;
Adam Langley95c29f32014-06-20 12:00:00 -0700513 break;
514 default:
David Benjamin3570d732015-06-29 00:28:17 -0400515 OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_PADDING_TYPE);
Adam Langley95c29f32014-06-20 12:00:00 -0700516 goto err;
517 }
518
David Benjamin43ea2042017-03-16 11:54:11 -0400519 if (!ret) {
David Benjamin3570d732015-06-29 00:28:17 -0400520 OPENSSL_PUT_ERROR(RSA, RSA_R_PADDING_CHECK_FAILED);
David Benjamin43ea2042017-03-16 11:54:11 -0400521 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700522 }
523
524err:
Brian Smith2a920312016-03-04 13:42:47 -1000525 BN_CTX_end(ctx);
526 BN_CTX_free(ctx);
527 if (buf != out) {
Adam Langley95c29f32014-06-20 12:00:00 -0700528 OPENSSL_free(buf);
529 }
530 return ret;
531}
532
David Benjamind93831d2015-10-29 13:19:12 -0400533int rsa_default_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in,
534 size_t len) {
Adam Langley6bc658d2014-08-18 13:29:45 -0700535 BIGNUM *f, *result;
536 BN_CTX *ctx = NULL;
537 unsigned blinding_index = 0;
538 BN_BLINDING *blinding = NULL;
539 int ret = 0;
540
541 ctx = BN_CTX_new();
542 if (ctx == NULL) {
543 goto err;
544 }
545 BN_CTX_start(ctx);
546 f = BN_CTX_get(ctx);
547 result = BN_CTX_get(ctx);
548
549 if (f == NULL || result == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400550 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
Adam Langley6bc658d2014-08-18 13:29:45 -0700551 goto err;
552 }
553
554 if (BN_bin2bn(in, len, f) == NULL) {
555 goto err;
556 }
557
558 if (BN_ucmp(f, rsa->n) >= 0) {
559 /* Usually the padding functions would catch this. */
David Benjamin3570d732015-06-29 00:28:17 -0400560 OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
Adam Langley6bc658d2014-08-18 13:29:45 -0700561 goto err;
562 }
563
Brian Smith86080c32016-03-25 12:23:16 -1000564 if (!BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx)) {
565 OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
566 goto err;
567 }
568
Brian Smith598e55a2016-03-26 20:17:37 -1000569 /* We cannot do blinding or verification without |e|, and continuing without
570 * those countermeasures is dangerous. However, the Java/Android RSA API
571 * requires support for keys where only |d| and |n| (and not |e|) are known.
572 * The callers that require that bad behavior set |RSA_FLAG_NO_BLINDING|. */
573 int disable_security = (rsa->flags & RSA_FLAG_NO_BLINDING) && rsa->e == NULL;
574
575 if (!disable_security) {
Brian Smith86361a32016-03-26 19:42:31 -1000576 /* Keys without public exponents must have blinding explicitly disabled to
577 * be used. */
578 if (rsa->e == NULL) {
579 OPENSSL_PUT_ERROR(RSA, RSA_R_NO_PUBLIC_EXPONENT);
580 goto err;
581 }
582
Adam Langley6bc658d2014-08-18 13:29:45 -0700583 blinding = rsa_blinding_get(rsa, &blinding_index, ctx);
584 if (blinding == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400585 OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
Adam Langley6bc658d2014-08-18 13:29:45 -0700586 goto err;
587 }
Brian Smith86361a32016-03-26 19:42:31 -1000588 if (!BN_BLINDING_convert(f, blinding, rsa->e, rsa->mont_n, ctx)) {
Adam Langley6bc658d2014-08-18 13:29:45 -0700589 goto err;
590 }
591 }
592
Brian Smith51b0d5b2016-03-25 13:15:39 -1000593 if (rsa->p != NULL && rsa->q != NULL && rsa->e != NULL && rsa->dmp1 != NULL &&
594 rsa->dmq1 != NULL && rsa->iqmp != NULL) {
Brian Smithf08c1c62016-03-25 13:24:46 -1000595 if (!mod_exp(result, f, rsa, ctx)) {
Adam Langley6bc658d2014-08-18 13:29:45 -0700596 goto err;
597 }
Brian Smith9f05de42016-08-02 18:21:18 -1000598 } else if (!BN_mod_exp_mont_consttime(result, f, rsa->d, rsa->n, ctx,
599 rsa->mont_n)) {
600 goto err;
Brian Smith86080c32016-03-25 12:23:16 -1000601 }
602
603 /* Verify the result to protect against fault attacks as described in the
604 * 1997 paper "On the Importance of Checking Cryptographic Protocols for
605 * Faults" by Dan Boneh, Richard A. DeMillo, and Richard J. Lipton. Some
606 * implementations do this only when the CRT is used, but we do it in all
607 * cases. Section 6 of the aforementioned paper describes an attack that
608 * works when the CRT isn't used. That attack is much less likely to succeed
609 * than the CRT attack, but there have likely been improvements since 1997.
610 *
Brian Smith598e55a2016-03-26 20:17:37 -1000611 * This check is cheap assuming |e| is small; it almost always is. */
612 if (!disable_security) {
Brian Smith86080c32016-03-25 12:23:16 -1000613 BIGNUM *vrfy = BN_CTX_get(ctx);
614 if (vrfy == NULL ||
615 !BN_mod_exp_mont(vrfy, result, rsa->e, rsa->n, ctx, rsa->mont_n) ||
616 !BN_equal_consttime(vrfy, f)) {
617 OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
Adam Langley6bc658d2014-08-18 13:29:45 -0700618 goto err;
619 }
Adam Langley6bc658d2014-08-18 13:29:45 -0700620
Brian Smith3426d102016-03-17 16:10:04 -1000621 if (!BN_BLINDING_invert(result, blinding, rsa->mont_n, ctx)) {
Adam Langley6bc658d2014-08-18 13:29:45 -0700622 goto err;
623 }
624 }
625
626 if (!BN_bn2bin_padded(out, len, result)) {
David Benjamin3570d732015-06-29 00:28:17 -0400627 OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
Adam Langley6bc658d2014-08-18 13:29:45 -0700628 goto err;
629 }
630
631 ret = 1;
632
633err:
634 if (ctx != NULL) {
635 BN_CTX_end(ctx);
636 BN_CTX_free(ctx);
637 }
638 if (blinding != NULL) {
639 rsa_blinding_release(rsa, blinding, blinding_index);
640 }
641
642 return ret;
643}
644
Adam Langley95c29f32014-06-20 12:00:00 -0700645static int mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) {
Brian Smithf08c1c62016-03-25 13:24:46 -1000646 assert(ctx != NULL);
647
Brian Smith51b0d5b2016-03-25 13:15:39 -1000648 assert(rsa->n != NULL);
649 assert(rsa->e != NULL);
650 assert(rsa->d != NULL);
651 assert(rsa->p != NULL);
652 assert(rsa->q != NULL);
653 assert(rsa->dmp1 != NULL);
654 assert(rsa->dmq1 != NULL);
655 assert(rsa->iqmp != NULL);
656
Adam Langley95c29f32014-06-20 12:00:00 -0700657 BIGNUM *r1, *m1, *vrfy;
Adam Langley95c29f32014-06-20 12:00:00 -0700658 int ret = 0;
659
660 BN_CTX_start(ctx);
661 r1 = BN_CTX_get(ctx);
662 m1 = BN_CTX_get(ctx);
663 vrfy = BN_CTX_get(ctx);
Brian Smith7cf60852016-03-19 22:39:37 -1000664 if (r1 == NULL ||
665 m1 == NULL ||
666 vrfy == NULL) {
667 goto err;
668 }
Adam Langley95c29f32014-06-20 12:00:00 -0700669
Brian Smith9f05de42016-08-02 18:21:18 -1000670 if (!BN_MONT_CTX_set_locked(&rsa->mont_p, &rsa->lock, rsa->p, ctx) ||
671 !BN_MONT_CTX_set_locked(&rsa->mont_q, &rsa->lock, rsa->q, ctx)) {
672 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700673 }
674
Brian Smithd0357302016-03-25 10:11:04 -1000675 if (!BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx)) {
Brian Smith24493a42016-03-25 09:12:48 -1000676 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700677 }
678
679 /* compute I mod q */
Brian Smith9f05de42016-08-02 18:21:18 -1000680 if (!BN_mod(r1, I, rsa->q, ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700681 goto err;
682 }
683
684 /* compute r1^dmq1 mod q */
Brian Smith9f05de42016-08-02 18:21:18 -1000685 if (!BN_mod_exp_mont_consttime(m1, r1, rsa->dmq1, rsa->q, ctx, rsa->mont_q)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700686 goto err;
687 }
688
689 /* compute I mod p */
Brian Smith9f05de42016-08-02 18:21:18 -1000690 if (!BN_mod(r1, I, rsa->p, ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700691 goto err;
692 }
693
694 /* compute r1^dmp1 mod p */
Brian Smith9f05de42016-08-02 18:21:18 -1000695 if (!BN_mod_exp_mont_consttime(r0, r1, rsa->dmp1, rsa->p, ctx, rsa->mont_p)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700696 goto err;
697 }
698
699 if (!BN_sub(r0, r0, m1)) {
700 goto err;
701 }
702 /* This will help stop the size of r0 increasing, which does
703 * affect the multiply if it optimised for a power of 2 size */
704 if (BN_is_negative(r0)) {
705 if (!BN_add(r0, r0, rsa->p)) {
706 goto err;
707 }
708 }
709
710 if (!BN_mul(r1, r0, rsa->iqmp, ctx)) {
711 goto err;
712 }
713
Brian Smith9f05de42016-08-02 18:21:18 -1000714 if (!BN_mod(r0, r1, rsa->p, ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700715 goto err;
716 }
717
718 /* If p < q it is occasionally possible for the correction of
719 * adding 'p' if r0 is negative above to leave the result still
720 * negative. This can break the private key operations: the following
721 * second correction should *always* correct this rare occurrence.
722 * This will *never* happen with OpenSSL generated keys because
723 * they ensure p > q [steve] */
724 if (BN_is_negative(r0)) {
725 if (!BN_add(r0, r0, rsa->p)) {
726 goto err;
727 }
728 }
729 if (!BN_mul(r1, r0, rsa->q, ctx)) {
730 goto err;
731 }
732 if (!BN_add(r0, r1, m1)) {
733 goto err;
734 }
735
Adam Langley95c29f32014-06-20 12:00:00 -0700736 ret = 1;
737
738err:
739 BN_CTX_end(ctx);
740 return ret;
741}
742
David Benjamin43780cb2017-04-10 18:05:19 -0400743static int ensure_bignum(BIGNUM **out) {
744 if (*out == NULL) {
745 *out = BN_new();
746 }
747 return *out != NULL;
748}
749
David Benjaminfb8b7632017-04-10 18:35:22 -0400750/* kBoringSSLRSASqrtTwo is the BIGNUM representation of ⌊2¹⁵³⁵×√2⌋. This is
751 * chosen to give enough precision for 3072-bit RSA, the largest key size FIPS
752 * specifies. Key sizes beyond this will round up.
753 *
754 * To verify this number, check that n² < 2³⁰⁷¹ < (n+1)², where n is value
755 * represented here. Note the components are listed in little-endian order. Here
756 * is some sample Python code to check:
757 *
758 * >>> TOBN = lambda a, b: a << 32 | b
759 * >>> l = [ <paste the contents of kSqrtTwo> ]
760 * >>> n = sum(a * 2**(64*i) for i, a in enumerate(l))
761 * >>> n**2 < 2**3071 < (n+1)**2
762 * True
763 */
764const BN_ULONG kBoringSSLRSASqrtTwo[] = {
765 TOBN(0xdea06241, 0xf7aa81c2), TOBN(0xf6a1be3f, 0xca221307),
766 TOBN(0x332a5e9f, 0x7bda1ebf), TOBN(0x0104dc01, 0xfe32352f),
767 TOBN(0xb8cf341b, 0x6f8236c7), TOBN(0x4264dabc, 0xd528b651),
768 TOBN(0xf4d3a02c, 0xebc93e0c), TOBN(0x81394ab6, 0xd8fd0efd),
769 TOBN(0xeaa4a089, 0x9040ca4a), TOBN(0xf52f120f, 0x836e582e),
770 TOBN(0xcb2a6343, 0x31f3c84d), TOBN(0xc6d5a8a3, 0x8bb7e9dc),
771 TOBN(0x460abc72, 0x2f7c4e33), TOBN(0xcab1bc91, 0x1688458a),
772 TOBN(0x53059c60, 0x11bc337b), TOBN(0xd2202e87, 0x42af1f4e),
773 TOBN(0x78048736, 0x3dfa2768), TOBN(0x0f74a85e, 0x439c7b4a),
774 TOBN(0xa8b1fe6f, 0xdc83db39), TOBN(0x4afc8304, 0x3ab8a2c3),
775 TOBN(0xed17ac85, 0x83339915), TOBN(0x1d6f60ba, 0x893ba84c),
776 TOBN(0x597d89b3, 0x754abe9f), TOBN(0xb504f333, 0xf9de6484),
777};
778const size_t kBoringSSLRSASqrtTwoLen = OPENSSL_ARRAY_SIZE(kBoringSSLRSASqrtTwo);
779
780int rsa_less_than_words(const BN_ULONG *a, const BN_ULONG *b, size_t len) {
Adam Langley518ba072017-04-20 13:51:11 -0700781 OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
782 crypto_word_t_too_small);
David Benjaminfb8b7632017-04-10 18:35:22 -0400783 int ret = 0;
784 /* Process the words in little-endian order. */
785 for (size_t i = 0; i < len; i++) {
Adam Langley518ba072017-04-20 13:51:11 -0700786 crypto_word_t eq = constant_time_eq_w(a[i], b[i]);
787 crypto_word_t lt = constant_time_lt_w(a[i], b[i]);
David Benjaminfb8b7632017-04-10 18:35:22 -0400788 ret = constant_time_select_int(eq, ret, constant_time_select_int(lt, 1, 0));
789 }
790 return ret;
791}
792
793int rsa_greater_than_pow2(const BIGNUM *b, int n) {
794 if (BN_is_negative(b) || n == INT_MAX) {
795 return 0;
796 }
797
798 int b_bits = BN_num_bits(b);
799 return b_bits > n + 1 || (b_bits == n + 1 && !BN_is_pow2(b));
800}
801
802/* generate_prime sets |out| to a prime with length |bits| such that |out|-1 is
803 * relatively prime to |e|. If |p| is non-NULL, |out| will also not be close to
804 * |p|. */
805static int generate_prime(BIGNUM *out, int bits, const BIGNUM *e,
806 const BIGNUM *p, BN_CTX *ctx, BN_GENCB *cb) {
807 if (bits < 128 || (bits % BN_BITS2) != 0) {
808 OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
809 return 0;
810 }
811
812 /* Ensure the bound on |tries| does not overflow. */
813 if (bits >= INT_MAX/5) {
814 OPENSSL_PUT_ERROR(RSA, RSA_R_MODULUS_TOO_LARGE);
815 return 0;
816 }
817
818 int ret = 0, tries = 0, rand_tries = 0;
819 BN_CTX_start(ctx);
820 BIGNUM *tmp = BN_CTX_get(ctx);
821 if (tmp == NULL) {
822 goto err;
823 }
824
825 /* See FIPS 186-4 appendix B.3.3, steps 4 and 5. Note |bits| here is
826 * nlen/2. */
827 for (;;) {
828 /* Generate a random number of length |bits| where the bottom bit is set
829 * (steps 4.2, 4.3, 5.2 and 5.3) and the top bit is set (implied by the
830 * bound checked below in steps 4.4 and 5.5). */
831 if (!BN_rand(out, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD) ||
832 !BN_GENCB_call(cb, BN_GENCB_GENERATED, rand_tries++)) {
833 goto err;
834 }
835
836 if (p != NULL) {
837 /* If |p| and |out| are too close, try again (step 5.4). */
838 if (!BN_sub(tmp, out, p)) {
839 goto err;
840 }
841 BN_set_negative(tmp, 0);
842 if (!rsa_greater_than_pow2(tmp, bits - 100)) {
843 continue;
844 }
845 }
846
847 /* If out < 2^(bits-1)×√2, try again (steps 4.4 and 5.5).
848 *
849 * We check the most significant words, so we retry if ⌊out/2^k⌋ <= ⌊b/2^k⌋,
850 * where b = 2^(bits-1)×√2 and k = max(0, bits - 1536). For key sizes up to
851 * 3072 (bits = 1536), k = 0, so we are testing that ⌊out⌋ <= ⌊b⌋. out is an
852 * integer and b is not, so this is equivalent to out < b. That is, the
853 * comparison is exact for FIPS key sizes.
854 *
855 * For larger keys, the comparison is approximate, leaning towards
856 * retrying. That is, we reject a negligible fraction of primes that are
857 * within the FIPS bound, but we will never accept a prime outside the
858 * bound, ensuring the resulting RSA key is the right size. Specifically, if
859 * the FIPS bound holds, we have ⌊out/2^k⌋ < out/2^k < b/2^k. This implies
860 * ⌊out/2^k⌋ <= ⌊b/2^k⌋. That is, the FIPS bound implies our bound and so we
861 * are slightly tighter. */
862 size_t out_len = (size_t)out->top;
863 assert(out_len == (size_t)bits / BN_BITS2);
864 size_t to_check = kBoringSSLRSASqrtTwoLen;
865 if (to_check > out_len) {
866 to_check = out_len;
867 }
868 if (!rsa_less_than_words(
869 kBoringSSLRSASqrtTwo + kBoringSSLRSASqrtTwoLen - to_check,
870 out->d + out_len - to_check, to_check)) {
871 continue;
872 }
873
874 /* Check gcd(out-1, e) is one (steps 4.5 and 5.6). */
875 if (!BN_sub(tmp, out, BN_value_one()) ||
876 !BN_gcd(tmp, tmp, e, ctx)) {
877 goto err;
878 }
879 if (BN_is_one(tmp)) {
880 /* Test |out| for primality (steps 4.5.1 and 5.6.1).
881 * TODO(davidben): Align the primality test with FIPS 186-4. */
882 int is_probable_prime;
883 if (!BN_primality_test(&is_probable_prime, out, BN_prime_checks, ctx, 1,
884 cb)) {
885 goto err;
886 }
887 if (is_probable_prime) {
888 ret = 1;
889 goto err;
890 }
891 }
892
893 /* If we've tried too many times to find a prime, abort (steps 4.7 and
894 * 5.8). */
895 tries++;
896 if (tries >= bits * 5) {
897 OPENSSL_PUT_ERROR(RSA, RSA_R_TOO_MANY_ITERATIONS);
898 goto err;
899 }
900 if (!BN_GENCB_call(cb, 2, tries)) {
901 goto err;
902 }
903 }
904
905err:
906 BN_CTX_end(ctx);
907 return ret;
908}
909
David Benjamin073391f2017-05-03 15:03:35 -0400910int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) {
David Benjaminfb8b7632017-04-10 18:35:22 -0400911 /* See FIPS 186-4 appendix B.3. This function implements a generalized version
912 * of the FIPS algorithm. For FIPS compliance, the caller is responsible for
913 * passing in 2048 or 3072 to |bits| and 65537 for |e_value|. */
914
David Benjaminb7ded432017-04-11 13:24:31 -0400915 /* Always generate RSA keys which are a multiple of 128 bits. Round |bits|
916 * down as needed. */
917 bits &= ~127;
918
919 /* Reject excessively small keys. */
920 if (bits < 256) {
921 OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL);
922 return 0;
923 }
924
David Benjaminfb8b7632017-04-10 18:35:22 -0400925 int ret = 0;
926 BN_CTX *ctx = BN_CTX_new();
Adam Langley95c29f32014-06-20 12:00:00 -0700927 if (ctx == NULL) {
David Benjaminfb8b7632017-04-10 18:35:22 -0400928 goto bn_err;
Adam Langley95c29f32014-06-20 12:00:00 -0700929 }
930 BN_CTX_start(ctx);
David Benjaminfb8b7632017-04-10 18:35:22 -0400931 BIGNUM *r0 = BN_CTX_get(ctx);
932 BIGNUM *r1 = BN_CTX_get(ctx);
933 BIGNUM *r2 = BN_CTX_get(ctx);
934 BIGNUM *r3 = BN_CTX_get(ctx);
Brian Smithf4bbc2a2015-08-06 10:42:27 -0400935 if (r0 == NULL || r1 == NULL || r2 == NULL || r3 == NULL) {
David Benjaminfb8b7632017-04-10 18:35:22 -0400936 goto bn_err;
Adam Langley95c29f32014-06-20 12:00:00 -0700937 }
938
David Benjaminfb8b7632017-04-10 18:35:22 -0400939 /* We need the RSA components non-NULL. */
David Benjamin43780cb2017-04-10 18:05:19 -0400940 if (!ensure_bignum(&rsa->n) ||
941 !ensure_bignum(&rsa->d) ||
942 !ensure_bignum(&rsa->e) ||
943 !ensure_bignum(&rsa->p) ||
944 !ensure_bignum(&rsa->q) ||
945 !ensure_bignum(&rsa->dmp1) ||
946 !ensure_bignum(&rsa->dmq1) ||
947 !ensure_bignum(&rsa->iqmp)) {
David Benjaminfb8b7632017-04-10 18:35:22 -0400948 goto bn_err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500949 }
Adam Langley95c29f32014-06-20 12:00:00 -0700950
David Benjamin1c703cb2015-06-11 21:42:14 -0400951 if (!BN_copy(rsa->e, e_value)) {
David Benjaminfb8b7632017-04-10 18:35:22 -0400952 goto bn_err;
David Benjamin1c703cb2015-06-11 21:42:14 -0400953 }
Adam Langley95c29f32014-06-20 12:00:00 -0700954
David Benjaminfb8b7632017-04-10 18:35:22 -0400955 int prime_bits = bits / 2;
956 do {
957 /* Generate p and q, each of size |prime_bits|, using the steps outlined in
958 * appendix FIPS 186-4 appendix B.3.3. */
959 if (!generate_prime(rsa->p, prime_bits, rsa->e, NULL, ctx, cb) ||
960 !BN_GENCB_call(cb, 3, 0) ||
961 !generate_prime(rsa->q, prime_bits, rsa->e, rsa->p, ctx, cb) ||
962 !BN_GENCB_call(cb, 3, 1)) {
963 goto bn_err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500964 }
David Benjaminfb8b7632017-04-10 18:35:22 -0400965
966 if (BN_cmp(rsa->p, rsa->q) < 0) {
967 BIGNUM *tmp = rsa->p;
968 rsa->p = rsa->q;
969 rsa->q = tmp;
David Benjamin6eb000d2015-02-11 01:17:41 -0500970 }
David Benjaminfb8b7632017-04-10 18:35:22 -0400971
972 /* Calculate d. */
973 if (!BN_sub(r1 /* p-1 */, rsa->p, BN_value_one()) ||
974 !BN_sub(r2 /* q-1 */, rsa->q, BN_value_one()) ||
975 !BN_mul(r0 /* (p-1)(q-1) */, r1, r2, ctx) ||
976 !BN_mod_inverse(rsa->d, rsa->e, r0, ctx)) {
977 goto bn_err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500978 }
David Benjaminfb8b7632017-04-10 18:35:22 -0400979
980 /* Check that |rsa->d| > 2^|prime_bits| and try again if it fails. See
981 * appendix B.3.1's guidance on values for d. */
982 } while (!rsa_greater_than_pow2(rsa->d, prime_bits));
983
984 if (/* Calculate n. */
985 !BN_mul(rsa->n, rsa->p, rsa->q, ctx) ||
986 /* Calculate d mod (p-1). */
987 !BN_mod(rsa->dmp1, rsa->d, r1, ctx) ||
988 /* Calculate d mod (q-1) */
989 !BN_mod(rsa->dmq1, rsa->d, r2, ctx)) {
990 goto bn_err;
Adam Langley95c29f32014-06-20 12:00:00 -0700991 }
Adam Langley839b8812015-05-26 11:36:46 -0700992
David Benjaminfb8b7632017-04-10 18:35:22 -0400993 /* Sanity-check that |rsa->n| has the specified size. This is implied by
994 * |generate_prime|'s bounds. */
995 if (BN_num_bits(rsa->n) != (unsigned)bits) {
996 OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -0700997 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500998 }
Adam Langley95c29f32014-06-20 12:00:00 -0700999
David Benjamin0b8dc302016-12-17 14:27:16 -05001000 /* Calculate inverse of q mod p. Note that although RSA key generation is far
1001 * from constant-time, |bn_mod_inverse_secret_prime| uses the same modular
1002 * exponentation logic as in RSA private key operations and, if the RSAZ-1024
1003 * code is enabled, will be optimized for common RSA prime sizes. */
David Benjamin0b8dc302016-12-17 14:27:16 -05001004 if (!BN_MONT_CTX_set_locked(&rsa->mont_p, &rsa->lock, rsa->p, ctx) ||
David Benjamin0a211df2016-12-17 15:25:55 -05001005 !bn_mod_inverse_secret_prime(rsa->iqmp, rsa->q, rsa->p, ctx,
1006 rsa->mont_p)) {
David Benjaminfb8b7632017-04-10 18:35:22 -04001007 goto bn_err;
David Benjamin6eb000d2015-02-11 01:17:41 -05001008 }
Adam Langley95c29f32014-06-20 12:00:00 -07001009
Brian Smithfebf7712016-03-21 13:47:32 -10001010 /* The key generation process is complex and thus error-prone. It could be
1011 * disastrous to generate and then use a bad key so double-check that the key
1012 * makes sense. */
David Benjaminfb8b7632017-04-10 18:35:22 -04001013 if (!RSA_check_key(rsa)) {
Brian Smithfebf7712016-03-21 13:47:32 -10001014 OPENSSL_PUT_ERROR(RSA, RSA_R_INTERNAL_ERROR);
David Benjaminfb8b7632017-04-10 18:35:22 -04001015 goto err;
Brian Smithfebf7712016-03-21 13:47:32 -10001016 }
1017
David Benjaminfb8b7632017-04-10 18:35:22 -04001018 ret = 1;
1019
1020bn_err:
1021 if (!ret) {
David Benjamin3570d732015-06-29 00:28:17 -04001022 OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
Adam Langley95c29f32014-06-20 12:00:00 -07001023 }
David Benjaminfb8b7632017-04-10 18:35:22 -04001024err:
Adam Langley95c29f32014-06-20 12:00:00 -07001025 if (ctx != NULL) {
1026 BN_CTX_end(ctx);
1027 BN_CTX_free(ctx);
1028 }
David Benjaminfb8b7632017-04-10 18:35:22 -04001029 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07001030}
1031
Brian Smithf08c1c62016-03-25 13:24:46 -10001032/* All of the methods are NULL to make it easier for the compiler/linker to drop
1033 * unused functions. The wrapper functions will select the appropriate
1034 * |rsa_default_*| implementation. */
David Benjamind93831d2015-10-29 13:19:12 -04001035const RSA_METHOD RSA_default_method = {
Adam Langley95c29f32014-06-20 12:00:00 -07001036 {
1037 0 /* references */,
1038 1 /* is_static */,
1039 },
1040 NULL /* app_data */,
1041
1042 NULL /* init */,
David Benjamind93831d2015-10-29 13:19:12 -04001043 NULL /* finish (defaults to rsa_default_finish) */,
Adam Langley95c29f32014-06-20 12:00:00 -07001044
David Benjamind93831d2015-10-29 13:19:12 -04001045 NULL /* size (defaults to rsa_default_size) */,
David Benjamin925fee32014-07-11 14:14:08 -04001046
Adam Langley95c29f32014-06-20 12:00:00 -07001047 NULL /* sign */,
1048 NULL /* verify */,
1049
David Benjamin073391f2017-05-03 15:03:35 -04001050 NULL /* encrypt (ignored) */,
David Benjamind93831d2015-10-29 13:19:12 -04001051 NULL /* sign_raw (defaults to rsa_default_sign_raw) */,
1052 NULL /* decrypt (defaults to rsa_default_decrypt) */,
David Benjamin073391f2017-05-03 15:03:35 -04001053 NULL /* verify_raw (ignored) */,
Adam Langley95c29f32014-06-20 12:00:00 -07001054
David Benjamind93831d2015-10-29 13:19:12 -04001055 NULL /* private_transform (defaults to rsa_default_private_transform) */,
Adam Langley6bc658d2014-08-18 13:29:45 -07001056
Brian Smithf08c1c62016-03-25 13:24:46 -10001057 NULL /* mod_exp (ignored) */,
1058 NULL /* bn_mod_exp (ignored) */,
Adam Langley95c29f32014-06-20 12:00:00 -07001059
1060 RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE,
1061
David Benjamin073391f2017-05-03 15:03:35 -04001062 NULL /* keygen (ignored) */,
David Benjamin4a2cc282017-04-10 18:20:55 -04001063 NULL /* multi_prime_keygen (ignored) */,
Adam Langley626c6862015-09-11 16:17:44 -07001064
David Benjamin073391f2017-05-03 15:03:35 -04001065 NULL /* supports_digest (ignored) */,
Adam Langley95c29f32014-06-20 12:00:00 -07001066};