blob: af55c1daceac1bbf4c788c5b99a53bd5b7f607fd [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 Smith3426d102016-03-17 16:10:04 -1000216 assert(ctx != NULL);
Brian Smithcbf56a52016-03-21 11:25:39 -1000217 assert(rsa->mont_n != NULL);
218
Adam Langley95c29f32014-06-20 12:00:00 -0700219 BN_BLINDING *ret = NULL;
220 BN_BLINDING **new_blindings;
221 uint8_t *new_blindings_inuse;
222 char overflow = 0;
223
Adam Langley683d7bd2015-04-13 11:04:14 -0700224 CRYPTO_MUTEX_lock_write(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700225
Adam Langley33672732015-03-31 18:55:53 -0700226 unsigned i;
227 for (i = 0; i < rsa->num_blindings; i++) {
228 if (rsa->blindings_inuse[i] == 0) {
229 rsa->blindings_inuse[i] = 1;
230 ret = rsa->blindings[i];
231 *index_used = i;
232 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700233 }
234 }
235
236 if (ret != NULL) {
Adam Langley683d7bd2015-04-13 11:04:14 -0700237 CRYPTO_MUTEX_unlock(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700238 return ret;
239 }
240
241 overflow = rsa->num_blindings >= MAX_BLINDINGS_PER_RSA;
242
243 /* We didn't find a free BN_BLINDING to use so increase the length of
244 * the arrays by one and use the newly created element. */
245
Adam Langley683d7bd2015-04-13 11:04:14 -0700246 CRYPTO_MUTEX_unlock(&rsa->lock);
Brian Smithcbf56a52016-03-21 11:25:39 -1000247 ret = BN_BLINDING_new(rsa, ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700248 if (ret == NULL) {
249 return NULL;
250 }
251
252 if (overflow) {
253 /* We cannot add any more cached BN_BLINDINGs so we use |ret|
254 * and mark it for destruction in |rsa_blinding_release|. */
255 *index_used = MAX_BLINDINGS_PER_RSA;
256 return ret;
257 }
258
Adam Langley683d7bd2015-04-13 11:04:14 -0700259 CRYPTO_MUTEX_lock_write(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700260
261 new_blindings =
262 OPENSSL_malloc(sizeof(BN_BLINDING *) * (rsa->num_blindings + 1));
263 if (new_blindings == NULL) {
264 goto err1;
265 }
266 memcpy(new_blindings, rsa->blindings,
267 sizeof(BN_BLINDING *) * rsa->num_blindings);
268 new_blindings[rsa->num_blindings] = ret;
269
270 new_blindings_inuse = OPENSSL_malloc(rsa->num_blindings + 1);
271 if (new_blindings_inuse == NULL) {
272 goto err2;
273 }
274 memcpy(new_blindings_inuse, rsa->blindings_inuse, rsa->num_blindings);
275 new_blindings_inuse[rsa->num_blindings] = 1;
276 *index_used = rsa->num_blindings;
277
David Benjamind8b65c82015-04-22 16:09:09 -0400278 OPENSSL_free(rsa->blindings);
Adam Langley95c29f32014-06-20 12:00:00 -0700279 rsa->blindings = new_blindings;
David Benjamind8b65c82015-04-22 16:09:09 -0400280 OPENSSL_free(rsa->blindings_inuse);
Adam Langley95c29f32014-06-20 12:00:00 -0700281 rsa->blindings_inuse = new_blindings_inuse;
282 rsa->num_blindings++;
283
Adam Langley683d7bd2015-04-13 11:04:14 -0700284 CRYPTO_MUTEX_unlock(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700285 return ret;
286
287err2:
288 OPENSSL_free(new_blindings);
289
290err1:
Adam Langley683d7bd2015-04-13 11:04:14 -0700291 CRYPTO_MUTEX_unlock(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700292 BN_BLINDING_free(ret);
293 return NULL;
294}
295
296/* rsa_blinding_release marks the cached BN_BLINDING at the given index as free
297 * for other threads to use. */
298static void rsa_blinding_release(RSA *rsa, BN_BLINDING *blinding,
299 unsigned blinding_index) {
300 if (blinding_index == MAX_BLINDINGS_PER_RSA) {
301 /* This blinding wasn't cached. */
302 BN_BLINDING_free(blinding);
303 return;
304 }
305
Adam Langley683d7bd2015-04-13 11:04:14 -0700306 CRYPTO_MUTEX_lock_write(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700307 rsa->blindings_inuse[blinding_index] = 0;
Adam Langley683d7bd2015-04-13 11:04:14 -0700308 CRYPTO_MUTEX_unlock(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700309}
310
311/* signing */
David Benjamind93831d2015-10-29 13:19:12 -0400312int rsa_default_sign_raw(RSA *rsa, size_t *out_len, uint8_t *out,
313 size_t max_out, const uint8_t *in, size_t in_len,
314 int padding) {
Adam Langley95c29f32014-06-20 12:00:00 -0700315 const unsigned rsa_size = RSA_size(rsa);
Adam Langley95c29f32014-06-20 12:00:00 -0700316 uint8_t *buf = NULL;
Adam Langley6887edb2014-06-20 12:00:00 -0700317 int i, ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700318
319 if (max_out < rsa_size) {
David Benjamin3570d732015-06-29 00:28:17 -0400320 OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
Adam Langley95c29f32014-06-20 12:00:00 -0700321 return 0;
322 }
323
Adam Langley95c29f32014-06-20 12:00:00 -0700324 buf = OPENSSL_malloc(rsa_size);
Adam Langley6bc658d2014-08-18 13:29:45 -0700325 if (buf == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400326 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700327 goto err;
328 }
329
330 switch (padding) {
331 case RSA_PKCS1_PADDING:
332 i = RSA_padding_add_PKCS1_type_1(buf, rsa_size, in, in_len);
333 break;
334 case RSA_NO_PADDING:
335 i = RSA_padding_add_none(buf, rsa_size, in, in_len);
336 break;
337 default:
David Benjamin3570d732015-06-29 00:28:17 -0400338 OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_PADDING_TYPE);
Adam Langley95c29f32014-06-20 12:00:00 -0700339 goto err;
340 }
Adam Langley6bc658d2014-08-18 13:29:45 -0700341
Adam Langley95c29f32014-06-20 12:00:00 -0700342 if (i <= 0) {
343 goto err;
344 }
345
Adam Langley6bc658d2014-08-18 13:29:45 -0700346 if (!RSA_private_transform(rsa, out, buf, rsa_size)) {
Adam Langley5f5bf6f2015-02-24 13:49:41 -0800347 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700348 }
349
350 *out_len = rsa_size;
351 ret = 1;
352
353err:
Adam Langley95c29f32014-06-20 12:00:00 -0700354 if (buf != NULL) {
355 OPENSSL_cleanse(buf, rsa_size);
356 OPENSSL_free(buf);
357 }
Adam Langley95c29f32014-06-20 12:00:00 -0700358
359 return ret;
360}
361
David Benjamind93831d2015-10-29 13:19:12 -0400362int rsa_default_decrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
363 const uint8_t *in, size_t in_len, int padding) {
Adam Langley95c29f32014-06-20 12:00:00 -0700364 const unsigned rsa_size = RSA_size(rsa);
Adam Langley95c29f32014-06-20 12:00:00 -0700365 int r = -1;
366 uint8_t *buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700367 int ret = 0;
368
369 if (max_out < rsa_size) {
David Benjamin3570d732015-06-29 00:28:17 -0400370 OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
Adam Langley95c29f32014-06-20 12:00:00 -0700371 return 0;
372 }
373
David Benjamin74279b62014-07-24 13:09:19 -0400374 if (padding == RSA_NO_PADDING) {
375 buf = out;
376 } else {
377 /* Allocate a temporary buffer to hold the padded plaintext. */
378 buf = OPENSSL_malloc(rsa_size);
379 if (buf == NULL) {
380 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
381 goto err;
382 }
Adam Langley95c29f32014-06-20 12:00:00 -0700383 }
384
Adam Langley6bc658d2014-08-18 13:29:45 -0700385 if (in_len != rsa_size) {
David Benjamin3570d732015-06-29 00:28:17 -0400386 OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_LEN_NOT_EQUAL_TO_MOD_LEN);
Adam Langley95c29f32014-06-20 12:00:00 -0700387 goto err;
388 }
389
Adam Langley6bc658d2014-08-18 13:29:45 -0700390 if (!RSA_private_transform(rsa, buf, in, rsa_size)) {
Adam Langley6887edb2014-06-20 12:00:00 -0700391 goto err;
392 }
Adam Langley95c29f32014-06-20 12:00:00 -0700393
394 switch (padding) {
395 case RSA_PKCS1_PADDING:
Adam Langley6887edb2014-06-20 12:00:00 -0700396 r = RSA_padding_check_PKCS1_type_2(out, rsa_size, buf, rsa_size);
Adam Langley95c29f32014-06-20 12:00:00 -0700397 break;
398 case RSA_PKCS1_OAEP_PADDING:
Adam Langley6887edb2014-06-20 12:00:00 -0700399 /* Use the default parameters: SHA-1 for both hashes and no label. */
400 r = RSA_padding_check_PKCS1_OAEP_mgf1(out, rsa_size, buf, rsa_size,
401 NULL, 0, NULL, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700402 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700403 case RSA_NO_PADDING:
David Benjamin74279b62014-07-24 13:09:19 -0400404 r = rsa_size;
Adam Langley95c29f32014-06-20 12:00:00 -0700405 break;
406 default:
David Benjamin3570d732015-06-29 00:28:17 -0400407 OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_PADDING_TYPE);
Adam Langley95c29f32014-06-20 12:00:00 -0700408 goto err;
409 }
410
411 if (r < 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400412 OPENSSL_PUT_ERROR(RSA, RSA_R_PADDING_CHECK_FAILED);
Adam Langley95c29f32014-06-20 12:00:00 -0700413 } else {
414 *out_len = r;
415 ret = 1;
416 }
417
418err:
David Benjamin74279b62014-07-24 13:09:19 -0400419 if (padding != RSA_NO_PADDING && buf != NULL) {
Adam Langley95c29f32014-06-20 12:00:00 -0700420 OPENSSL_cleanse(buf, rsa_size);
421 OPENSSL_free(buf);
422 }
Adam Langley95c29f32014-06-20 12:00:00 -0700423
424 return ret;
425}
426
Brian Smithf08c1c62016-03-25 13:24:46 -1000427static int mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
428
David Benjamind93831d2015-10-29 13:19:12 -0400429int rsa_default_verify_raw(RSA *rsa, size_t *out_len, uint8_t *out,
430 size_t max_out, const uint8_t *in, size_t in_len,
431 int padding) {
Adam Langley95c29f32014-06-20 12:00:00 -0700432 const unsigned rsa_size = RSA_size(rsa);
433 BIGNUM *f, *result;
434 int ret = 0;
Adam Langley6887edb2014-06-20 12:00:00 -0700435 int r = -1;
Adam Langley95c29f32014-06-20 12:00:00 -0700436 uint8_t *buf = NULL;
437 BN_CTX *ctx = NULL;
438
Adam Langley95c29f32014-06-20 12:00:00 -0700439 if (max_out < rsa_size) {
David Benjamin3570d732015-06-29 00:28:17 -0400440 OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
Adam Langley95c29f32014-06-20 12:00:00 -0700441 return 0;
442 }
443
Brian Smith625475f2016-01-12 10:47:25 -1000444 if (!check_modulus_and_exponent_sizes(rsa)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700445 return 0;
446 }
447
448 ctx = BN_CTX_new();
449 if (ctx == NULL) {
450 goto err;
451 }
452
453 BN_CTX_start(ctx);
454 f = BN_CTX_get(ctx);
455 result = BN_CTX_get(ctx);
David Benjamin74279b62014-07-24 13:09:19 -0400456 if (padding == RSA_NO_PADDING) {
457 buf = out;
458 } else {
459 /* Allocate a temporary buffer to hold the padded plaintext. */
460 buf = OPENSSL_malloc(rsa_size);
461 if (buf == NULL) {
462 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
463 goto err;
464 }
465 }
466 if (!f || !result) {
David Benjamin3570d732015-06-29 00:28:17 -0400467 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700468 goto err;
469 }
470
Adam Langley6bc658d2014-08-18 13:29:45 -0700471 if (in_len != rsa_size) {
David Benjamin3570d732015-06-29 00:28:17 -0400472 OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_LEN_NOT_EQUAL_TO_MOD_LEN);
Adam Langley95c29f32014-06-20 12:00:00 -0700473 goto err;
474 }
475
476 if (BN_bin2bn(in, in_len, f) == NULL) {
477 goto err;
478 }
479
480 if (BN_ucmp(f, rsa->n) >= 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400481 OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
Adam Langley95c29f32014-06-20 12:00:00 -0700482 goto err;
483 }
484
Brian Smith24493a42016-03-25 09:12:48 -1000485 if (BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx) == NULL ||
486 !BN_mod_exp_mont(result, f, rsa->e, rsa->n, ctx, rsa->mont_n)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700487 goto err;
488 }
489
Adam Langley6887edb2014-06-20 12:00:00 -0700490 if (!BN_bn2bin_padded(buf, rsa_size, result)) {
David Benjamin3570d732015-06-29 00:28:17 -0400491 OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
Adam Langley6887edb2014-06-20 12:00:00 -0700492 goto err;
493 }
Adam Langley95c29f32014-06-20 12:00:00 -0700494
495 switch (padding) {
496 case RSA_PKCS1_PADDING:
Adam Langley6887edb2014-06-20 12:00:00 -0700497 r = RSA_padding_check_PKCS1_type_1(out, rsa_size, buf, rsa_size);
Adam Langley95c29f32014-06-20 12:00:00 -0700498 break;
499 case RSA_NO_PADDING:
David Benjamin74279b62014-07-24 13:09:19 -0400500 r = rsa_size;
Adam Langley95c29f32014-06-20 12:00:00 -0700501 break;
502 default:
David Benjamin3570d732015-06-29 00:28:17 -0400503 OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_PADDING_TYPE);
Adam Langley95c29f32014-06-20 12:00:00 -0700504 goto err;
505 }
506
507 if (r < 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400508 OPENSSL_PUT_ERROR(RSA, RSA_R_PADDING_CHECK_FAILED);
Adam Langley95c29f32014-06-20 12:00:00 -0700509 } else {
510 *out_len = r;
511 ret = 1;
512 }
513
514err:
515 if (ctx != NULL) {
516 BN_CTX_end(ctx);
517 BN_CTX_free(ctx);
518 }
David Benjamin74279b62014-07-24 13:09:19 -0400519 if (padding != RSA_NO_PADDING && buf != NULL) {
Adam Langley95c29f32014-06-20 12:00:00 -0700520 OPENSSL_cleanse(buf, rsa_size);
521 OPENSSL_free(buf);
522 }
523 return ret;
524}
525
David Benjamind93831d2015-10-29 13:19:12 -0400526int rsa_default_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in,
527 size_t len) {
Adam Langley6bc658d2014-08-18 13:29:45 -0700528 BIGNUM *f, *result;
529 BN_CTX *ctx = NULL;
530 unsigned blinding_index = 0;
531 BN_BLINDING *blinding = NULL;
532 int ret = 0;
533
534 ctx = BN_CTX_new();
535 if (ctx == NULL) {
536 goto err;
537 }
538 BN_CTX_start(ctx);
539 f = BN_CTX_get(ctx);
540 result = BN_CTX_get(ctx);
541
542 if (f == NULL || result == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400543 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
Adam Langley6bc658d2014-08-18 13:29:45 -0700544 goto err;
545 }
546
547 if (BN_bin2bn(in, len, f) == NULL) {
548 goto err;
549 }
550
551 if (BN_ucmp(f, rsa->n) >= 0) {
552 /* Usually the padding functions would catch this. */
David Benjamin3570d732015-06-29 00:28:17 -0400553 OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
Adam Langley6bc658d2014-08-18 13:29:45 -0700554 goto err;
555 }
556
557 if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
Brian Smithcbf56a52016-03-21 11:25:39 -1000558 if (BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx) == NULL) {
559 OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
560 goto err;
561 }
562
Adam Langley6bc658d2014-08-18 13:29:45 -0700563 blinding = rsa_blinding_get(rsa, &blinding_index, ctx);
564 if (blinding == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400565 OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
Adam Langley6bc658d2014-08-18 13:29:45 -0700566 goto err;
567 }
Brian Smith3426d102016-03-17 16:10:04 -1000568 if (!BN_BLINDING_convert(f, blinding, rsa->mont_n, ctx)) {
Adam Langley6bc658d2014-08-18 13:29:45 -0700569 goto err;
570 }
571 }
572
Brian Smithf08c1c62016-03-25 13:24:46 -1000573 if (((rsa->p != NULL) && (rsa->q != NULL) && (rsa->dmp1 != NULL) &&
Adam Langley6bc658d2014-08-18 13:29:45 -0700574 (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
Brian Smithf08c1c62016-03-25 13:24:46 -1000575 if (!mod_exp(result, f, rsa, ctx)) {
Adam Langley6bc658d2014-08-18 13:29:45 -0700576 goto err;
577 }
578 } else {
579 BIGNUM local_d;
580 BIGNUM *d = NULL;
581
582 BN_init(&local_d);
583 d = &local_d;
584 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
585
Brian Smith24493a42016-03-25 09:12:48 -1000586 if (BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx) == NULL ||
587 !BN_mod_exp_mont_consttime(result, f, d, rsa->n, ctx, rsa->mont_n)) {
Adam Langley6bc658d2014-08-18 13:29:45 -0700588 goto err;
589 }
590 }
591
592 if (blinding) {
Brian Smith3426d102016-03-17 16:10:04 -1000593 if (!BN_BLINDING_invert(result, blinding, rsa->mont_n, ctx)) {
Adam Langley6bc658d2014-08-18 13:29:45 -0700594 goto err;
595 }
596 }
597
598 if (!BN_bn2bin_padded(out, len, result)) {
David Benjamin3570d732015-06-29 00:28:17 -0400599 OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
Adam Langley6bc658d2014-08-18 13:29:45 -0700600 goto err;
601 }
602
603 ret = 1;
604
605err:
606 if (ctx != NULL) {
607 BN_CTX_end(ctx);
608 BN_CTX_free(ctx);
609 }
610 if (blinding != NULL) {
611 rsa_blinding_release(rsa, blinding, blinding_index);
612 }
613
614 return ret;
615}
616
Adam Langley95c29f32014-06-20 12:00:00 -0700617static int mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) {
Brian Smithf08c1c62016-03-25 13:24:46 -1000618 assert(ctx != NULL);
619
Adam Langley95c29f32014-06-20 12:00:00 -0700620 BIGNUM *r1, *m1, *vrfy;
621 BIGNUM local_dmp1, local_dmq1, local_c, local_r1;
622 BIGNUM *dmp1, *dmq1, *c, *pr1;
623 int ret = 0;
Adam Langley839b8812015-05-26 11:36:46 -0700624 size_t i, num_additional_primes = 0;
625
626 if (rsa->additional_primes != NULL) {
627 num_additional_primes = sk_RSA_additional_prime_num(rsa->additional_primes);
628 }
Adam Langley95c29f32014-06-20 12:00:00 -0700629
630 BN_CTX_start(ctx);
631 r1 = BN_CTX_get(ctx);
632 m1 = BN_CTX_get(ctx);
633 vrfy = BN_CTX_get(ctx);
Brian Smith7cf60852016-03-19 22:39:37 -1000634 if (r1 == NULL ||
635 m1 == NULL ||
636 vrfy == NULL) {
637 goto err;
638 }
Adam Langley95c29f32014-06-20 12:00:00 -0700639
640 {
641 BIGNUM local_p, local_q;
642 BIGNUM *p = NULL, *q = NULL;
643
644 /* Make sure BN_mod_inverse in Montgomery intialization uses the
Brian Smith60a45aa2015-11-18 17:44:11 -1000645 * BN_FLG_CONSTTIME flag. */
Adam Langley95c29f32014-06-20 12:00:00 -0700646 BN_init(&local_p);
647 p = &local_p;
648 BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
649
650 BN_init(&local_q);
651 q = &local_q;
652 BN_with_flags(q, rsa->q, BN_FLG_CONSTTIME);
653
Brian Smith24493a42016-03-25 09:12:48 -1000654 if (BN_MONT_CTX_set_locked(&rsa->mont_p, &rsa->lock, p, ctx) == NULL ||
655 BN_MONT_CTX_set_locked(&rsa->mont_q, &rsa->lock, q, ctx) == NULL) {
656 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700657 }
658 }
659
Brian Smith24493a42016-03-25 09:12:48 -1000660 if (BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx) == NULL) {
661 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700662 }
663
664 /* compute I mod q */
665 c = &local_c;
666 BN_with_flags(c, I, BN_FLG_CONSTTIME);
667 if (!BN_mod(r1, c, rsa->q, ctx)) {
668 goto err;
669 }
670
671 /* compute r1^dmq1 mod q */
672 dmq1 = &local_dmq1;
673 BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
Brian Smith617804a2016-02-08 20:36:51 -1000674 if (!BN_mod_exp_mont_consttime(m1, r1, dmq1, rsa->q, ctx, rsa->mont_q)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700675 goto err;
676 }
677
678 /* compute I mod p */
679 c = &local_c;
680 BN_with_flags(c, I, BN_FLG_CONSTTIME);
681 if (!BN_mod(r1, c, rsa->p, ctx)) {
682 goto err;
683 }
684
685 /* compute r1^dmp1 mod p */
686 dmp1 = &local_dmp1;
687 BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
Brian Smith617804a2016-02-08 20:36:51 -1000688 if (!BN_mod_exp_mont_consttime(r0, r1, dmp1, rsa->p, ctx, rsa->mont_p)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700689 goto err;
690 }
691
692 if (!BN_sub(r0, r0, m1)) {
693 goto err;
694 }
695 /* This will help stop the size of r0 increasing, which does
696 * affect the multiply if it optimised for a power of 2 size */
697 if (BN_is_negative(r0)) {
698 if (!BN_add(r0, r0, rsa->p)) {
699 goto err;
700 }
701 }
702
703 if (!BN_mul(r1, r0, rsa->iqmp, ctx)) {
704 goto err;
705 }
706
707 /* Turn BN_FLG_CONSTTIME flag on before division operation */
708 pr1 = &local_r1;
709 BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
710
711 if (!BN_mod(r0, pr1, rsa->p, ctx)) {
712 goto err;
713 }
714
715 /* If p < q it is occasionally possible for the correction of
716 * adding 'p' if r0 is negative above to leave the result still
717 * negative. This can break the private key operations: the following
718 * second correction should *always* correct this rare occurrence.
719 * This will *never* happen with OpenSSL generated keys because
720 * they ensure p > q [steve] */
721 if (BN_is_negative(r0)) {
722 if (!BN_add(r0, r0, rsa->p)) {
723 goto err;
724 }
725 }
726 if (!BN_mul(r1, r0, rsa->q, ctx)) {
727 goto err;
728 }
729 if (!BN_add(r0, r1, m1)) {
730 goto err;
731 }
732
Adam Langley839b8812015-05-26 11:36:46 -0700733 for (i = 0; i < num_additional_primes; i++) {
734 /* multi-prime RSA. */
735 BIGNUM local_exp, local_prime;
736 BIGNUM *exp = &local_exp, *prime = &local_prime;
737 RSA_additional_prime *ap =
738 sk_RSA_additional_prime_value(rsa->additional_primes, i);
739
740 BN_with_flags(exp, ap->exp, BN_FLG_CONSTTIME);
741 BN_with_flags(prime, ap->prime, BN_FLG_CONSTTIME);
742
743 /* c will already point to a BIGNUM with the correct flags. */
744 if (!BN_mod(r1, c, prime, ctx)) {
745 goto err;
746 }
747
Brian Smith24493a42016-03-25 09:12:48 -1000748 if (BN_MONT_CTX_set_locked(&ap->mont, &rsa->lock, prime, ctx) == NULL ||
749 !BN_mod_exp_mont_consttime(m1, r1, exp, prime, ctx, ap->mont)) {
Adam Langley839b8812015-05-26 11:36:46 -0700750 goto err;
751 }
752
753 BN_set_flags(m1, BN_FLG_CONSTTIME);
754
755 if (!BN_sub(m1, m1, r0) ||
756 !BN_mul(m1, m1, ap->coeff, ctx) ||
757 !BN_mod(m1, m1, prime, ctx) ||
758 (BN_is_negative(m1) && !BN_add(m1, m1, prime)) ||
759 !BN_mul(m1, m1, ap->r, ctx) ||
760 !BN_add(r0, r0, m1)) {
761 goto err;
762 }
763 }
764
Adam Langley95c29f32014-06-20 12:00:00 -0700765 if (rsa->e && rsa->n) {
Brian Smith617804a2016-02-08 20:36:51 -1000766 if (!BN_mod_exp_mont(vrfy, r0, rsa->e, rsa->n, ctx, rsa->mont_n)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700767 goto err;
768 }
769 /* If 'I' was greater than (or equal to) rsa->n, the operation
770 * will be equivalent to using 'I mod n'. However, the result of
771 * the verify will *always* be less than 'n' so we don't check
772 * for absolute equality, just congruency. */
773 if (!BN_sub(vrfy, vrfy, I)) {
774 goto err;
775 }
776 if (!BN_mod(vrfy, vrfy, rsa->n, ctx)) {
777 goto err;
778 }
779 if (BN_is_negative(vrfy)) {
780 if (!BN_add(vrfy, vrfy, rsa->n)) {
781 goto err;
782 }
783 }
784 if (!BN_is_zero(vrfy)) {
785 /* 'I' and 'vrfy' aren't congruent mod n. Don't leak
786 * miscalculated CRT output, just do a raw (slower)
787 * mod_exp and return that instead. */
788
789 BIGNUM local_d;
790 BIGNUM *d = NULL;
791
792 d = &local_d;
793 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
Brian Smith617804a2016-02-08 20:36:51 -1000794 if (!BN_mod_exp_mont_consttime(r0, I, d, rsa->n, ctx, rsa->mont_n)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700795 goto err;
796 }
797 }
798 }
799 ret = 1;
800
801err:
802 BN_CTX_end(ctx);
803 return ret;
804}
805
David Benjamind93831d2015-10-29 13:19:12 -0400806int rsa_default_multi_prime_keygen(RSA *rsa, int bits, int num_primes,
807 BIGNUM *e_value, BN_GENCB *cb) {
Adam Langley95c29f32014-06-20 12:00:00 -0700808 BIGNUM *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL, *tmp;
809 BIGNUM local_r0, local_d, local_p;
810 BIGNUM *pr0, *d, *p;
Adam Langley839b8812015-05-26 11:36:46 -0700811 int prime_bits, ok = -1, n = 0, i, j;
Adam Langley95c29f32014-06-20 12:00:00 -0700812 BN_CTX *ctx = NULL;
Adam Langley839b8812015-05-26 11:36:46 -0700813 STACK_OF(RSA_additional_prime) *additional_primes = NULL;
814
815 if (num_primes < 2) {
816 ok = 0; /* we set our own err */
David Benjamin3570d732015-06-29 00:28:17 -0400817 OPENSSL_PUT_ERROR(RSA, RSA_R_MUST_HAVE_AT_LEAST_TWO_PRIMES);
Adam Langley839b8812015-05-26 11:36:46 -0700818 goto err;
819 }
Adam Langley95c29f32014-06-20 12:00:00 -0700820
821 ctx = BN_CTX_new();
822 if (ctx == NULL) {
823 goto err;
824 }
825 BN_CTX_start(ctx);
826 r0 = BN_CTX_get(ctx);
827 r1 = BN_CTX_get(ctx);
828 r2 = BN_CTX_get(ctx);
829 r3 = BN_CTX_get(ctx);
Brian Smithf4bbc2a2015-08-06 10:42:27 -0400830 if (r0 == NULL || r1 == NULL || r2 == NULL || r3 == NULL) {
Adam Langley95c29f32014-06-20 12:00:00 -0700831 goto err;
832 }
833
Adam Langley839b8812015-05-26 11:36:46 -0700834 if (num_primes > 2) {
835 additional_primes = sk_RSA_additional_prime_new_null();
836 if (additional_primes == NULL) {
837 goto err;
838 }
839 }
840
841 for (i = 2; i < num_primes; i++) {
842 RSA_additional_prime *ap = OPENSSL_malloc(sizeof(RSA_additional_prime));
843 if (ap == NULL) {
844 goto err;
845 }
846 memset(ap, 0, sizeof(RSA_additional_prime));
847 ap->prime = BN_new();
848 ap->exp = BN_new();
849 ap->coeff = BN_new();
850 ap->r = BN_new();
851 if (ap->prime == NULL ||
852 ap->exp == NULL ||
853 ap->coeff == NULL ||
854 ap->r == NULL ||
855 !sk_RSA_additional_prime_push(additional_primes, ap)) {
856 RSA_additional_prime_free(ap);
857 goto err;
858 }
859 }
Adam Langley95c29f32014-06-20 12:00:00 -0700860
861 /* We need the RSA components non-NULL */
David Benjamin6eb000d2015-02-11 01:17:41 -0500862 if (!rsa->n && ((rsa->n = BN_new()) == NULL)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700863 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500864 }
865 if (!rsa->d && ((rsa->d = BN_new()) == NULL)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700866 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500867 }
868 if (!rsa->e && ((rsa->e = BN_new()) == NULL)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700869 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500870 }
871 if (!rsa->p && ((rsa->p = BN_new()) == NULL)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700872 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500873 }
874 if (!rsa->q && ((rsa->q = BN_new()) == NULL)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700875 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500876 }
877 if (!rsa->dmp1 && ((rsa->dmp1 = BN_new()) == NULL)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700878 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500879 }
880 if (!rsa->dmq1 && ((rsa->dmq1 = BN_new()) == NULL)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700881 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500882 }
883 if (!rsa->iqmp && ((rsa->iqmp = BN_new()) == NULL)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700884 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500885 }
Adam Langley95c29f32014-06-20 12:00:00 -0700886
David Benjamin1c703cb2015-06-11 21:42:14 -0400887 if (!BN_copy(rsa->e, e_value)) {
888 goto err;
889 }
Adam Langley95c29f32014-06-20 12:00:00 -0700890
891 /* generate p and q */
Adam Langley839b8812015-05-26 11:36:46 -0700892 prime_bits = (bits + (num_primes - 1)) / num_primes;
Adam Langley95c29f32014-06-20 12:00:00 -0700893 for (;;) {
Adam Langley839b8812015-05-26 11:36:46 -0700894 if (!BN_generate_prime_ex(rsa->p, prime_bits, 0, NULL, NULL, cb) ||
David Benjamin6eb000d2015-02-11 01:17:41 -0500895 !BN_sub(r2, rsa->p, BN_value_one()) ||
896 !BN_gcd(r1, r2, rsa->e, ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700897 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500898 }
899 if (BN_is_one(r1)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700900 break;
David Benjamin6eb000d2015-02-11 01:17:41 -0500901 }
902 if (!BN_GENCB_call(cb, 2, n++)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700903 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500904 }
Adam Langley95c29f32014-06-20 12:00:00 -0700905 }
David Benjamin6eb000d2015-02-11 01:17:41 -0500906 if (!BN_GENCB_call(cb, 3, 0)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700907 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500908 }
Adam Langley839b8812015-05-26 11:36:46 -0700909 prime_bits = ((bits - prime_bits) + (num_primes - 2)) / (num_primes - 1);
Adam Langley95c29f32014-06-20 12:00:00 -0700910 for (;;) {
911 /* When generating ridiculously small keys, we can get stuck
912 * continually regenerating the same prime values. Check for
913 * this and bail if it happens 3 times. */
914 unsigned int degenerate = 0;
915 do {
Adam Langley839b8812015-05-26 11:36:46 -0700916 if (!BN_generate_prime_ex(rsa->q, prime_bits, 0, NULL, NULL, cb)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700917 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500918 }
Adam Langley95c29f32014-06-20 12:00:00 -0700919 } while ((BN_cmp(rsa->p, rsa->q) == 0) && (++degenerate < 3));
920 if (degenerate == 3) {
921 ok = 0; /* we set our own err */
David Benjamin3570d732015-06-29 00:28:17 -0400922 OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL);
Adam Langley95c29f32014-06-20 12:00:00 -0700923 goto err;
924 }
David Benjamin6eb000d2015-02-11 01:17:41 -0500925 if (!BN_sub(r2, rsa->q, BN_value_one()) ||
926 !BN_gcd(r1, r2, rsa->e, ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700927 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500928 }
929 if (BN_is_one(r1)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700930 break;
David Benjamin6eb000d2015-02-11 01:17:41 -0500931 }
932 if (!BN_GENCB_call(cb, 2, n++)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700933 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500934 }
Adam Langley95c29f32014-06-20 12:00:00 -0700935 }
Adam Langley839b8812015-05-26 11:36:46 -0700936
937 if (!BN_GENCB_call(cb, 3, 1) ||
938 !BN_mul(rsa->n, rsa->p, rsa->q, ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700939 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -0500940 }
Adam Langley839b8812015-05-26 11:36:46 -0700941
942 for (i = 2; i < num_primes; i++) {
943 RSA_additional_prime *ap =
944 sk_RSA_additional_prime_value(additional_primes, i - 2);
945 prime_bits = ((bits - BN_num_bits(rsa->n)) + (num_primes - (i + 1))) /
946 (num_primes - i);
947
948 for (;;) {
949 if (!BN_generate_prime_ex(ap->prime, prime_bits, 0, NULL, NULL, cb)) {
950 goto err;
951 }
952 if (BN_cmp(rsa->p, ap->prime) == 0 ||
953 BN_cmp(rsa->q, ap->prime) == 0) {
954 continue;
955 }
956
957 for (j = 0; j < i - 2; j++) {
958 if (BN_cmp(sk_RSA_additional_prime_value(additional_primes, j)->prime,
959 ap->prime) == 0) {
960 break;
961 }
962 }
963 if (j != i - 2) {
964 continue;
965 }
966
967 if (!BN_sub(r2, ap->prime, BN_value_one()) ||
968 !BN_gcd(r1, r2, rsa->e, ctx)) {
969 goto err;
970 }
971
972 if (!BN_is_one(r1)) {
973 continue;
974 }
975 if (i != num_primes - 1) {
976 break;
977 }
978
979 /* For the last prime we'll check that it makes n large enough. In the
980 * two prime case this isn't a problem because we generate primes with
981 * the top two bits set and so the product is always of the expected
982 * size. In the multi prime case, this doesn't follow. */
983 if (!BN_mul(r1, rsa->n, ap->prime, ctx)) {
984 goto err;
985 }
Adam Langley96c2a282015-06-02 14:16:44 -0700986 if (BN_num_bits(r1) == (unsigned) bits) {
Adam Langley839b8812015-05-26 11:36:46 -0700987 break;
988 }
989
990 if (!BN_GENCB_call(cb, 2, n++)) {
991 goto err;
992 }
993 }
994
995 /* ap->r is is the product of all the primes prior to the current one
996 * (including p and q). */
997 if (!BN_copy(ap->r, rsa->n)) {
998 goto err;
999 }
1000 if (i == num_primes - 1) {
1001 /* In the case of the last prime, we calculated n as |r1| in the loop
1002 * above. */
1003 if (!BN_copy(rsa->n, r1)) {
1004 goto err;
1005 }
1006 } else if (!BN_mul(rsa->n, rsa->n, ap->prime, ctx)) {
1007 goto err;
1008 }
1009
1010 if (!BN_GENCB_call(cb, 3, 1)) {
1011 goto err;
1012 }
1013 }
1014
Adam Langley95c29f32014-06-20 12:00:00 -07001015 if (BN_cmp(rsa->p, rsa->q) < 0) {
1016 tmp = rsa->p;
1017 rsa->p = rsa->q;
1018 rsa->q = tmp;
1019 }
1020
Adam Langley95c29f32014-06-20 12:00:00 -07001021 /* calculate d */
David Benjamin6eb000d2015-02-11 01:17:41 -05001022 if (!BN_sub(r1, rsa->p, BN_value_one())) {
Adam Langley95c29f32014-06-20 12:00:00 -07001023 goto err; /* p-1 */
David Benjamin6eb000d2015-02-11 01:17:41 -05001024 }
1025 if (!BN_sub(r2, rsa->q, BN_value_one())) {
Adam Langley95c29f32014-06-20 12:00:00 -07001026 goto err; /* q-1 */
David Benjamin6eb000d2015-02-11 01:17:41 -05001027 }
1028 if (!BN_mul(r0, r1, r2, ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -07001029 goto err; /* (p-1)(q-1) */
David Benjamin6eb000d2015-02-11 01:17:41 -05001030 }
Adam Langley839b8812015-05-26 11:36:46 -07001031 for (i = 2; i < num_primes; i++) {
1032 RSA_additional_prime *ap =
1033 sk_RSA_additional_prime_value(additional_primes, i - 2);
1034 if (!BN_sub(r3, ap->prime, BN_value_one()) ||
1035 !BN_mul(r0, r0, r3, ctx)) {
1036 goto err;
1037 }
1038 }
Adam Langley95c29f32014-06-20 12:00:00 -07001039 pr0 = &local_r0;
1040 BN_with_flags(pr0, r0, BN_FLG_CONSTTIME);
David Benjamin6eb000d2015-02-11 01:17:41 -05001041 if (!BN_mod_inverse(rsa->d, rsa->e, pr0, ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -07001042 goto err; /* d */
David Benjamin6eb000d2015-02-11 01:17:41 -05001043 }
Adam Langley95c29f32014-06-20 12:00:00 -07001044
1045 /* set up d for correct BN_FLG_CONSTTIME flag */
1046 d = &local_d;
1047 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
1048
1049 /* calculate d mod (p-1) */
David Benjamin6eb000d2015-02-11 01:17:41 -05001050 if (!BN_mod(rsa->dmp1, d, r1, ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -07001051 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -05001052 }
Adam Langley95c29f32014-06-20 12:00:00 -07001053
1054 /* calculate d mod (q-1) */
David Benjamin6eb000d2015-02-11 01:17:41 -05001055 if (!BN_mod(rsa->dmq1, d, r2, ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -07001056 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -05001057 }
Adam Langley95c29f32014-06-20 12:00:00 -07001058
1059 /* calculate inverse of q mod p */
1060 p = &local_p;
1061 BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
1062
David Benjamin6eb000d2015-02-11 01:17:41 -05001063 if (!BN_mod_inverse(rsa->iqmp, rsa->q, p, ctx)) {
Adam Langley95c29f32014-06-20 12:00:00 -07001064 goto err;
David Benjamin6eb000d2015-02-11 01:17:41 -05001065 }
Adam Langley95c29f32014-06-20 12:00:00 -07001066
Adam Langley839b8812015-05-26 11:36:46 -07001067 for (i = 2; i < num_primes; i++) {
1068 RSA_additional_prime *ap =
1069 sk_RSA_additional_prime_value(additional_primes, i - 2);
1070 if (!BN_sub(ap->exp, ap->prime, BN_value_one()) ||
1071 !BN_mod(ap->exp, rsa->d, ap->exp, ctx) ||
1072 !BN_mod_inverse(ap->coeff, ap->r, ap->prime, ctx)) {
1073 goto err;
1074 }
1075 }
1076
Adam Langley95c29f32014-06-20 12:00:00 -07001077 ok = 1;
Adam Langley839b8812015-05-26 11:36:46 -07001078 rsa->additional_primes = additional_primes;
1079 additional_primes = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001080
1081err:
1082 if (ok == -1) {
David Benjamin3570d732015-06-29 00:28:17 -04001083 OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
Adam Langley95c29f32014-06-20 12:00:00 -07001084 ok = 0;
1085 }
1086 if (ctx != NULL) {
1087 BN_CTX_end(ctx);
1088 BN_CTX_free(ctx);
1089 }
Adam Langley839b8812015-05-26 11:36:46 -07001090 sk_RSA_additional_prime_pop_free(additional_primes,
1091 RSA_additional_prime_free);
Adam Langley95c29f32014-06-20 12:00:00 -07001092 return ok;
1093}
1094
David Benjamind93831d2015-10-29 13:19:12 -04001095int rsa_default_keygen(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) {
1096 return rsa_default_multi_prime_keygen(rsa, bits, 2 /* num primes */, e_value,
1097 cb);
Adam Langley839b8812015-05-26 11:36:46 -07001098}
1099
Brian Smithf08c1c62016-03-25 13:24:46 -10001100/* All of the methods are NULL to make it easier for the compiler/linker to drop
1101 * unused functions. The wrapper functions will select the appropriate
1102 * |rsa_default_*| implementation. */
David Benjamind93831d2015-10-29 13:19:12 -04001103const RSA_METHOD RSA_default_method = {
Adam Langley95c29f32014-06-20 12:00:00 -07001104 {
1105 0 /* references */,
1106 1 /* is_static */,
1107 },
1108 NULL /* app_data */,
1109
1110 NULL /* init */,
David Benjamind93831d2015-10-29 13:19:12 -04001111 NULL /* finish (defaults to rsa_default_finish) */,
Adam Langley95c29f32014-06-20 12:00:00 -07001112
David Benjamind93831d2015-10-29 13:19:12 -04001113 NULL /* size (defaults to rsa_default_size) */,
David Benjamin925fee32014-07-11 14:14:08 -04001114
Adam Langley95c29f32014-06-20 12:00:00 -07001115 NULL /* sign */,
1116 NULL /* verify */,
1117
David Benjamind93831d2015-10-29 13:19:12 -04001118 NULL /* encrypt (defaults to rsa_default_encrypt) */,
1119 NULL /* sign_raw (defaults to rsa_default_sign_raw) */,
1120 NULL /* decrypt (defaults to rsa_default_decrypt) */,
1121 NULL /* verify_raw (defaults to rsa_default_verify_raw) */,
Adam Langley95c29f32014-06-20 12:00:00 -07001122
David Benjamind93831d2015-10-29 13:19:12 -04001123 NULL /* private_transform (defaults to rsa_default_private_transform) */,
Adam Langley6bc658d2014-08-18 13:29:45 -07001124
Brian Smithf08c1c62016-03-25 13:24:46 -10001125 NULL /* mod_exp (ignored) */,
1126 NULL /* bn_mod_exp (ignored) */,
Adam Langley95c29f32014-06-20 12:00:00 -07001127
1128 RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE,
1129
David Benjamind93831d2015-10-29 13:19:12 -04001130 NULL /* keygen (defaults to rsa_default_keygen) */,
1131 NULL /* multi_prime_keygen (defaults to rsa_default_multi_prime_keygen) */,
Adam Langley626c6862015-09-11 16:17:44 -07001132
1133 NULL /* supports_digest */,
Adam Langley95c29f32014-06-20 12:00:00 -07001134};