Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* 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 Benjamin | cfa9de8 | 2016-03-14 14:19:41 -0400 | [diff] [blame] | 59 | #include <assert.h> |
David Benjamin | fb8b763 | 2017-04-10 18:35:22 -0400 | [diff] [blame] | 60 | #include <limits.h> |
Adam Langley | 2b2d66d | 2015-01-30 17:08:37 -0800 | [diff] [blame] | 61 | #include <string.h> |
| 62 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 63 | #include <openssl/bn.h> |
| 64 | #include <openssl/err.h> |
| 65 | #include <openssl/mem.h> |
Brian Smith | 054e682 | 2015-03-27 21:12:01 -1000 | [diff] [blame] | 66 | #include <openssl/thread.h> |
David Benjamin | fb8b763 | 2017-04-10 18:35:22 -0400 | [diff] [blame] | 67 | #include <openssl/type_check.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 68 | |
| 69 | #include "internal.h" |
Adam Langley | 5c38c05 | 2017-04-28 14:47:06 -0700 | [diff] [blame] | 70 | #include "../fipsmodule/bn/internal.h" |
Adam Langley | 683d7bd | 2015-04-13 11:04:14 -0700 | [diff] [blame] | 71 | #include "../internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 72 | |
| 73 | |
Brian Smith | 625475f | 2016-01-12 10:47:25 -1000 | [diff] [blame] | 74 | static int check_modulus_and_exponent_sizes(const RSA *rsa) { |
| 75 | unsigned rsa_bits = BN_num_bits(rsa->n); |
David Benjamin | cfa9de8 | 2016-03-14 14:19:41 -0400 | [diff] [blame] | 76 | |
Brian Smith | 625475f | 2016-01-12 10:47:25 -1000 | [diff] [blame] | 77 | if (rsa_bits > 16 * 1024) { |
| 78 | OPENSSL_PUT_ERROR(RSA, RSA_R_MODULUS_TOO_LARGE); |
| 79 | return 0; |
| 80 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 81 | |
David Benjamin | cfa9de8 | 2016-03-14 14:19:41 -0400 | [diff] [blame] | 82 | /* 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 Smith | 625475f | 2016-01-12 10:47:25 -1000 | [diff] [blame] | 94 | OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_E_VALUE); |
| 95 | return 0; |
| 96 | } |
| 97 | |
David Benjamin | cfa9de8 | 2016-03-14 14:19:41 -0400 | [diff] [blame] | 98 | /* 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 Smith | 625475f | 2016-01-12 10:47:25 -1000 | [diff] [blame] | 104 | return 0; |
| 105 | } |
David Benjamin | cfa9de8 | 2016-03-14 14:19:41 -0400 | [diff] [blame] | 106 | assert(BN_ucmp(rsa->n, rsa->e) > 0); |
Brian Smith | 625475f | 2016-01-12 10:47:25 -1000 | [diff] [blame] | 107 | |
| 108 | return 1; |
| 109 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 110 | |
David Benjamin | d93831d | 2015-10-29 13:19:12 -0400 | [diff] [blame] | 111 | size_t rsa_default_size(const RSA *rsa) { |
David Benjamin | 925fee3 | 2014-07-11 14:14:08 -0400 | [diff] [blame] | 112 | return BN_num_bytes(rsa->n); |
| 113 | } |
| 114 | |
David Benjamin | 073391f | 2017-05-03 15:03:35 -0400 | [diff] [blame^] | 115 | int 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 Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 122 | const unsigned rsa_size = RSA_size(rsa); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 123 | BIGNUM *f, *result; |
| 124 | uint8_t *buf = NULL; |
| 125 | BN_CTX *ctx = NULL; |
Adam Langley | 6887edb | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 126 | int i, ret = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 127 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 128 | if (max_out < rsa_size) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 129 | OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 130 | return 0; |
| 131 | } |
| 132 | |
Brian Smith | 625475f | 2016-01-12 10:47:25 -1000 | [diff] [blame] | 133 | if (!check_modulus_and_exponent_sizes(rsa)) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 134 | 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 Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 147 | OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 148 | 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 Langley | 6887edb | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 156 | /* 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 Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 159 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 160 | case RSA_NO_PADDING: |
| 161 | i = RSA_padding_add_none(buf, rsa_size, in, in_len); |
| 162 | break; |
| 163 | default: |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 164 | OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_PADDING_TYPE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 165 | 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 Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 178 | OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 179 | goto err; |
| 180 | } |
| 181 | |
Brian Smith | d035730 | 2016-03-25 10:11:04 -1000 | [diff] [blame] | 182 | if (!BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx) || |
Brian Smith | 24493a4 | 2016-03-25 09:12:48 -1000 | [diff] [blame] | 183 | !BN_mod_exp_mont(result, f, rsa->e, rsa->n, ctx, rsa->mont_n)) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 184 | goto err; |
| 185 | } |
| 186 | |
| 187 | /* put in leading 0 bytes if the number is less than the length of the |
| 188 | * modulus */ |
Adam Langley | 6887edb | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 189 | if (!BN_bn2bin_padded(out, rsa_size, result)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 190 | OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR); |
Adam Langley | 6887edb | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 191 | goto err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | *out_len = rsa_size; |
| 195 | ret = 1; |
| 196 | |
| 197 | err: |
| 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. */ |
| 222 | static BN_BLINDING *rsa_blinding_get(RSA *rsa, unsigned *index_used, |
| 223 | BN_CTX *ctx) { |
Brian Smith | 3426d10 | 2016-03-17 16:10:04 -1000 | [diff] [blame] | 224 | assert(ctx != NULL); |
Brian Smith | cbf56a5 | 2016-03-21 11:25:39 -1000 | [diff] [blame] | 225 | assert(rsa->mont_n != NULL); |
| 226 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 227 | BN_BLINDING *ret = NULL; |
| 228 | BN_BLINDING **new_blindings; |
| 229 | uint8_t *new_blindings_inuse; |
| 230 | char overflow = 0; |
| 231 | |
Adam Langley | 683d7bd | 2015-04-13 11:04:14 -0700 | [diff] [blame] | 232 | CRYPTO_MUTEX_lock_write(&rsa->lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 233 | |
Adam Langley | 3367273 | 2015-03-31 18:55:53 -0700 | [diff] [blame] | 234 | 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 Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
| 244 | if (ret != NULL) { |
David Benjamin | 29270de | 2016-05-24 15:28:36 +0000 | [diff] [blame] | 245 | CRYPTO_MUTEX_unlock_write(&rsa->lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 246 | 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 Benjamin | 29270de | 2016-05-24 15:28:36 +0000 | [diff] [blame] | 254 | CRYPTO_MUTEX_unlock_write(&rsa->lock); |
Brian Smith | 86361a3 | 2016-03-26 19:42:31 -1000 | [diff] [blame] | 255 | ret = BN_BLINDING_new(); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 256 | 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 Langley | 683d7bd | 2015-04-13 11:04:14 -0700 | [diff] [blame] | 267 | CRYPTO_MUTEX_lock_write(&rsa->lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 268 | |
| 269 | new_blindings = |
| 270 | OPENSSL_malloc(sizeof(BN_BLINDING *) * (rsa->num_blindings + 1)); |
| 271 | if (new_blindings == NULL) { |
| 272 | goto err1; |
| 273 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 274 | OPENSSL_memcpy(new_blindings, rsa->blindings, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 275 | 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 Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 282 | OPENSSL_memcpy(new_blindings_inuse, rsa->blindings_inuse, rsa->num_blindings); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 283 | new_blindings_inuse[rsa->num_blindings] = 1; |
| 284 | *index_used = rsa->num_blindings; |
| 285 | |
David Benjamin | d8b65c8 | 2015-04-22 16:09:09 -0400 | [diff] [blame] | 286 | OPENSSL_free(rsa->blindings); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 287 | rsa->blindings = new_blindings; |
David Benjamin | d8b65c8 | 2015-04-22 16:09:09 -0400 | [diff] [blame] | 288 | OPENSSL_free(rsa->blindings_inuse); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 289 | rsa->blindings_inuse = new_blindings_inuse; |
| 290 | rsa->num_blindings++; |
| 291 | |
David Benjamin | 29270de | 2016-05-24 15:28:36 +0000 | [diff] [blame] | 292 | CRYPTO_MUTEX_unlock_write(&rsa->lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 293 | return ret; |
| 294 | |
| 295 | err2: |
| 296 | OPENSSL_free(new_blindings); |
| 297 | |
| 298 | err1: |
David Benjamin | 29270de | 2016-05-24 15:28:36 +0000 | [diff] [blame] | 299 | CRYPTO_MUTEX_unlock_write(&rsa->lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 300 | 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. */ |
| 306 | static 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 Langley | 683d7bd | 2015-04-13 11:04:14 -0700 | [diff] [blame] | 314 | CRYPTO_MUTEX_lock_write(&rsa->lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 315 | rsa->blindings_inuse[blinding_index] = 0; |
David Benjamin | 29270de | 2016-05-24 15:28:36 +0000 | [diff] [blame] | 316 | CRYPTO_MUTEX_unlock_write(&rsa->lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | /* signing */ |
David Benjamin | d93831d | 2015-10-29 13:19:12 -0400 | [diff] [blame] | 320 | int 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 Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 323 | const unsigned rsa_size = RSA_size(rsa); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 324 | uint8_t *buf = NULL; |
Adam Langley | 6887edb | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 325 | int i, ret = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 326 | |
| 327 | if (max_out < rsa_size) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 328 | OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 329 | return 0; |
| 330 | } |
| 331 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 332 | buf = OPENSSL_malloc(rsa_size); |
Adam Langley | 6bc658d | 2014-08-18 13:29:45 -0700 | [diff] [blame] | 333 | if (buf == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 334 | OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 335 | 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 Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 346 | OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_PADDING_TYPE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 347 | goto err; |
| 348 | } |
Adam Langley | 6bc658d | 2014-08-18 13:29:45 -0700 | [diff] [blame] | 349 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 350 | if (i <= 0) { |
| 351 | goto err; |
| 352 | } |
| 353 | |
Adam Langley | 6bc658d | 2014-08-18 13:29:45 -0700 | [diff] [blame] | 354 | if (!RSA_private_transform(rsa, out, buf, rsa_size)) { |
Adam Langley | 5f5bf6f | 2015-02-24 13:49:41 -0800 | [diff] [blame] | 355 | goto err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | *out_len = rsa_size; |
| 359 | ret = 1; |
| 360 | |
| 361 | err: |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 362 | if (buf != NULL) { |
| 363 | OPENSSL_cleanse(buf, rsa_size); |
| 364 | OPENSSL_free(buf); |
| 365 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 366 | |
| 367 | return ret; |
| 368 | } |
| 369 | |
David Benjamin | d93831d | 2015-10-29 13:19:12 -0400 | [diff] [blame] | 370 | int 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 Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 372 | const unsigned rsa_size = RSA_size(rsa); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 373 | uint8_t *buf = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 374 | int ret = 0; |
| 375 | |
| 376 | if (max_out < rsa_size) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 377 | OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 378 | return 0; |
| 379 | } |
| 380 | |
David Benjamin | 74279b6 | 2014-07-24 13:09:19 -0400 | [diff] [blame] | 381 | 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 Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Adam Langley | 6bc658d | 2014-08-18 13:29:45 -0700 | [diff] [blame] | 392 | if (in_len != rsa_size) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 393 | OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_LEN_NOT_EQUAL_TO_MOD_LEN); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 394 | goto err; |
| 395 | } |
| 396 | |
Adam Langley | 6bc658d | 2014-08-18 13:29:45 -0700 | [diff] [blame] | 397 | if (!RSA_private_transform(rsa, buf, in, rsa_size)) { |
Adam Langley | 6887edb | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 398 | goto err; |
| 399 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 400 | |
| 401 | switch (padding) { |
| 402 | case RSA_PKCS1_PADDING: |
David Benjamin | b0ad3d7 | 2017-03-16 13:15:31 -0400 | [diff] [blame] | 403 | ret = |
| 404 | RSA_padding_check_PKCS1_type_2(out, out_len, rsa_size, buf, rsa_size); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 405 | break; |
| 406 | case RSA_PKCS1_OAEP_PADDING: |
Adam Langley | 6887edb | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 407 | /* Use the default parameters: SHA-1 for both hashes and no label. */ |
David Benjamin | b0ad3d7 | 2017-03-16 13:15:31 -0400 | [diff] [blame] | 408 | ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, out_len, rsa_size, buf, |
| 409 | rsa_size, NULL, 0, NULL, NULL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 410 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 411 | case RSA_NO_PADDING: |
David Benjamin | b0ad3d7 | 2017-03-16 13:15:31 -0400 | [diff] [blame] | 412 | *out_len = rsa_size; |
| 413 | ret = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 414 | break; |
| 415 | default: |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 416 | OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_PADDING_TYPE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 417 | goto err; |
| 418 | } |
| 419 | |
David Benjamin | b0ad3d7 | 2017-03-16 13:15:31 -0400 | [diff] [blame] | 420 | if (!ret) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 421 | OPENSSL_PUT_ERROR(RSA, RSA_R_PADDING_CHECK_FAILED); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | err: |
David Benjamin | 74279b6 | 2014-07-24 13:09:19 -0400 | [diff] [blame] | 425 | if (padding != RSA_NO_PADDING && buf != NULL) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 426 | OPENSSL_cleanse(buf, rsa_size); |
| 427 | OPENSSL_free(buf); |
| 428 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 429 | |
| 430 | return ret; |
| 431 | } |
| 432 | |
Brian Smith | f08c1c6 | 2016-03-25 13:24:46 -1000 | [diff] [blame] | 433 | static int mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx); |
| 434 | |
Brian Smith | c0b196d | 2016-03-04 08:54:07 -1000 | [diff] [blame] | 435 | int 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 Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 442 | const unsigned rsa_size = RSA_size(rsa); |
| 443 | BIGNUM *f, *result; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 444 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 445 | if (max_out < rsa_size) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 446 | OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 447 | return 0; |
| 448 | } |
| 449 | |
Brian Smith | 9902262 | 2016-03-04 09:20:07 -1000 | [diff] [blame] | 450 | 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 Smith | 625475f | 2016-01-12 10:47:25 -1000 | [diff] [blame] | 455 | if (!check_modulus_and_exponent_sizes(rsa)) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 456 | return 0; |
| 457 | } |
| 458 | |
Brian Smith | 2a92031 | 2016-03-04 13:42:47 -1000 | [diff] [blame] | 459 | BN_CTX *ctx = BN_CTX_new(); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 460 | if (ctx == NULL) { |
Brian Smith | 2a92031 | 2016-03-04 13:42:47 -1000 | [diff] [blame] | 461 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 462 | } |
| 463 | |
Brian Smith | 2a92031 | 2016-03-04 13:42:47 -1000 | [diff] [blame] | 464 | int ret = 0; |
| 465 | uint8_t *buf = NULL; |
| 466 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 467 | BN_CTX_start(ctx); |
| 468 | f = BN_CTX_get(ctx); |
| 469 | result = BN_CTX_get(ctx); |
Brian Smith | 2a92031 | 2016-03-04 13:42:47 -1000 | [diff] [blame] | 470 | if (f == NULL || result == NULL) { |
| 471 | OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE); |
| 472 | goto err; |
| 473 | } |
| 474 | |
David Benjamin | 74279b6 | 2014-07-24 13:09:19 -0400 | [diff] [blame] | 475 | 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 Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 485 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 486 | if (BN_bin2bn(in, in_len, f) == NULL) { |
| 487 | goto err; |
| 488 | } |
| 489 | |
| 490 | if (BN_ucmp(f, rsa->n) >= 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 491 | OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 492 | goto err; |
| 493 | } |
| 494 | |
Brian Smith | d035730 | 2016-03-25 10:11:04 -1000 | [diff] [blame] | 495 | if (!BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx) || |
Brian Smith | 24493a4 | 2016-03-25 09:12:48 -1000 | [diff] [blame] | 496 | !BN_mod_exp_mont(result, f, rsa->e, rsa->n, ctx, rsa->mont_n)) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 497 | goto err; |
| 498 | } |
| 499 | |
Adam Langley | 6887edb | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 500 | if (!BN_bn2bin_padded(buf, rsa_size, result)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 501 | OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR); |
Adam Langley | 6887edb | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 502 | goto err; |
| 503 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 504 | |
| 505 | switch (padding) { |
| 506 | case RSA_PKCS1_PADDING: |
David Benjamin | 43ea204 | 2017-03-16 11:54:11 -0400 | [diff] [blame] | 507 | ret = |
| 508 | RSA_padding_check_PKCS1_type_1(out, out_len, rsa_size, buf, rsa_size); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 509 | break; |
| 510 | case RSA_NO_PADDING: |
David Benjamin | 43ea204 | 2017-03-16 11:54:11 -0400 | [diff] [blame] | 511 | ret = 1; |
| 512 | *out_len = rsa_size; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 513 | break; |
| 514 | default: |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 515 | OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_PADDING_TYPE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 516 | goto err; |
| 517 | } |
| 518 | |
David Benjamin | 43ea204 | 2017-03-16 11:54:11 -0400 | [diff] [blame] | 519 | if (!ret) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 520 | OPENSSL_PUT_ERROR(RSA, RSA_R_PADDING_CHECK_FAILED); |
David Benjamin | 43ea204 | 2017-03-16 11:54:11 -0400 | [diff] [blame] | 521 | goto err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | err: |
Brian Smith | 2a92031 | 2016-03-04 13:42:47 -1000 | [diff] [blame] | 525 | BN_CTX_end(ctx); |
| 526 | BN_CTX_free(ctx); |
| 527 | if (buf != out) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 528 | OPENSSL_free(buf); |
| 529 | } |
| 530 | return ret; |
| 531 | } |
| 532 | |
David Benjamin | d93831d | 2015-10-29 13:19:12 -0400 | [diff] [blame] | 533 | int rsa_default_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in, |
| 534 | size_t len) { |
Adam Langley | 6bc658d | 2014-08-18 13:29:45 -0700 | [diff] [blame] | 535 | 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 Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 550 | OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE); |
Adam Langley | 6bc658d | 2014-08-18 13:29:45 -0700 | [diff] [blame] | 551 | 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 Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 560 | OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS); |
Adam Langley | 6bc658d | 2014-08-18 13:29:45 -0700 | [diff] [blame] | 561 | goto err; |
| 562 | } |
| 563 | |
Brian Smith | 86080c3 | 2016-03-25 12:23:16 -1000 | [diff] [blame] | 564 | 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 Smith | 598e55a | 2016-03-26 20:17:37 -1000 | [diff] [blame] | 569 | /* 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 Smith | 86361a3 | 2016-03-26 19:42:31 -1000 | [diff] [blame] | 576 | /* 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 Langley | 6bc658d | 2014-08-18 13:29:45 -0700 | [diff] [blame] | 583 | blinding = rsa_blinding_get(rsa, &blinding_index, ctx); |
| 584 | if (blinding == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 585 | OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR); |
Adam Langley | 6bc658d | 2014-08-18 13:29:45 -0700 | [diff] [blame] | 586 | goto err; |
| 587 | } |
Brian Smith | 86361a3 | 2016-03-26 19:42:31 -1000 | [diff] [blame] | 588 | if (!BN_BLINDING_convert(f, blinding, rsa->e, rsa->mont_n, ctx)) { |
Adam Langley | 6bc658d | 2014-08-18 13:29:45 -0700 | [diff] [blame] | 589 | goto err; |
| 590 | } |
| 591 | } |
| 592 | |
Brian Smith | 51b0d5b | 2016-03-25 13:15:39 -1000 | [diff] [blame] | 593 | if (rsa->p != NULL && rsa->q != NULL && rsa->e != NULL && rsa->dmp1 != NULL && |
| 594 | rsa->dmq1 != NULL && rsa->iqmp != NULL) { |
Brian Smith | f08c1c6 | 2016-03-25 13:24:46 -1000 | [diff] [blame] | 595 | if (!mod_exp(result, f, rsa, ctx)) { |
Adam Langley | 6bc658d | 2014-08-18 13:29:45 -0700 | [diff] [blame] | 596 | goto err; |
| 597 | } |
Brian Smith | 9f05de4 | 2016-08-02 18:21:18 -1000 | [diff] [blame] | 598 | } else if (!BN_mod_exp_mont_consttime(result, f, rsa->d, rsa->n, ctx, |
| 599 | rsa->mont_n)) { |
| 600 | goto err; |
Brian Smith | 86080c3 | 2016-03-25 12:23:16 -1000 | [diff] [blame] | 601 | } |
| 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 Smith | 598e55a | 2016-03-26 20:17:37 -1000 | [diff] [blame] | 611 | * This check is cheap assuming |e| is small; it almost always is. */ |
| 612 | if (!disable_security) { |
Brian Smith | 86080c3 | 2016-03-25 12:23:16 -1000 | [diff] [blame] | 613 | 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 Langley | 6bc658d | 2014-08-18 13:29:45 -0700 | [diff] [blame] | 618 | goto err; |
| 619 | } |
Adam Langley | 6bc658d | 2014-08-18 13:29:45 -0700 | [diff] [blame] | 620 | |
Brian Smith | 3426d10 | 2016-03-17 16:10:04 -1000 | [diff] [blame] | 621 | if (!BN_BLINDING_invert(result, blinding, rsa->mont_n, ctx)) { |
Adam Langley | 6bc658d | 2014-08-18 13:29:45 -0700 | [diff] [blame] | 622 | goto err; |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | if (!BN_bn2bin_padded(out, len, result)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 627 | OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR); |
Adam Langley | 6bc658d | 2014-08-18 13:29:45 -0700 | [diff] [blame] | 628 | goto err; |
| 629 | } |
| 630 | |
| 631 | ret = 1; |
| 632 | |
| 633 | err: |
| 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 Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 645 | static int mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) { |
Brian Smith | f08c1c6 | 2016-03-25 13:24:46 -1000 | [diff] [blame] | 646 | assert(ctx != NULL); |
| 647 | |
Brian Smith | 51b0d5b | 2016-03-25 13:15:39 -1000 | [diff] [blame] | 648 | 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 Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 657 | BIGNUM *r1, *m1, *vrfy; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 658 | 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 Smith | 7cf6085 | 2016-03-19 22:39:37 -1000 | [diff] [blame] | 664 | if (r1 == NULL || |
| 665 | m1 == NULL || |
| 666 | vrfy == NULL) { |
| 667 | goto err; |
| 668 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 669 | |
Brian Smith | 9f05de4 | 2016-08-02 18:21:18 -1000 | [diff] [blame] | 670 | 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 Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 673 | } |
| 674 | |
Brian Smith | d035730 | 2016-03-25 10:11:04 -1000 | [diff] [blame] | 675 | if (!BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx)) { |
Brian Smith | 24493a4 | 2016-03-25 09:12:48 -1000 | [diff] [blame] | 676 | goto err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | /* compute I mod q */ |
Brian Smith | 9f05de4 | 2016-08-02 18:21:18 -1000 | [diff] [blame] | 680 | if (!BN_mod(r1, I, rsa->q, ctx)) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 681 | goto err; |
| 682 | } |
| 683 | |
| 684 | /* compute r1^dmq1 mod q */ |
Brian Smith | 9f05de4 | 2016-08-02 18:21:18 -1000 | [diff] [blame] | 685 | if (!BN_mod_exp_mont_consttime(m1, r1, rsa->dmq1, rsa->q, ctx, rsa->mont_q)) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 686 | goto err; |
| 687 | } |
| 688 | |
| 689 | /* compute I mod p */ |
Brian Smith | 9f05de4 | 2016-08-02 18:21:18 -1000 | [diff] [blame] | 690 | if (!BN_mod(r1, I, rsa->p, ctx)) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 691 | goto err; |
| 692 | } |
| 693 | |
| 694 | /* compute r1^dmp1 mod p */ |
Brian Smith | 9f05de4 | 2016-08-02 18:21:18 -1000 | [diff] [blame] | 695 | if (!BN_mod_exp_mont_consttime(r0, r1, rsa->dmp1, rsa->p, ctx, rsa->mont_p)) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 696 | 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 Smith | 9f05de4 | 2016-08-02 18:21:18 -1000 | [diff] [blame] | 714 | if (!BN_mod(r0, r1, rsa->p, ctx)) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 715 | 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 Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 736 | ret = 1; |
| 737 | |
| 738 | err: |
| 739 | BN_CTX_end(ctx); |
| 740 | return ret; |
| 741 | } |
| 742 | |
David Benjamin | 43780cb | 2017-04-10 18:05:19 -0400 | [diff] [blame] | 743 | static int ensure_bignum(BIGNUM **out) { |
| 744 | if (*out == NULL) { |
| 745 | *out = BN_new(); |
| 746 | } |
| 747 | return *out != NULL; |
| 748 | } |
| 749 | |
David Benjamin | fb8b763 | 2017-04-10 18:35:22 -0400 | [diff] [blame] | 750 | /* 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 | */ |
| 764 | const 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 | }; |
| 778 | const size_t kBoringSSLRSASqrtTwoLen = OPENSSL_ARRAY_SIZE(kBoringSSLRSASqrtTwo); |
| 779 | |
| 780 | int rsa_less_than_words(const BN_ULONG *a, const BN_ULONG *b, size_t len) { |
Adam Langley | 518ba07 | 2017-04-20 13:51:11 -0700 | [diff] [blame] | 781 | OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t), |
| 782 | crypto_word_t_too_small); |
David Benjamin | fb8b763 | 2017-04-10 18:35:22 -0400 | [diff] [blame] | 783 | int ret = 0; |
| 784 | /* Process the words in little-endian order. */ |
| 785 | for (size_t i = 0; i < len; i++) { |
Adam Langley | 518ba07 | 2017-04-20 13:51:11 -0700 | [diff] [blame] | 786 | 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 Benjamin | fb8b763 | 2017-04-10 18:35:22 -0400 | [diff] [blame] | 788 | ret = constant_time_select_int(eq, ret, constant_time_select_int(lt, 1, 0)); |
| 789 | } |
| 790 | return ret; |
| 791 | } |
| 792 | |
| 793 | int 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|. */ |
| 805 | static 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 | |
| 905 | err: |
| 906 | BN_CTX_end(ctx); |
| 907 | return ret; |
| 908 | } |
| 909 | |
David Benjamin | 073391f | 2017-05-03 15:03:35 -0400 | [diff] [blame^] | 910 | int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) { |
David Benjamin | fb8b763 | 2017-04-10 18:35:22 -0400 | [diff] [blame] | 911 | /* 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 Benjamin | b7ded43 | 2017-04-11 13:24:31 -0400 | [diff] [blame] | 915 | /* 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 Benjamin | fb8b763 | 2017-04-10 18:35:22 -0400 | [diff] [blame] | 925 | int ret = 0; |
| 926 | BN_CTX *ctx = BN_CTX_new(); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 927 | if (ctx == NULL) { |
David Benjamin | fb8b763 | 2017-04-10 18:35:22 -0400 | [diff] [blame] | 928 | goto bn_err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 929 | } |
| 930 | BN_CTX_start(ctx); |
David Benjamin | fb8b763 | 2017-04-10 18:35:22 -0400 | [diff] [blame] | 931 | 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 Smith | f4bbc2a | 2015-08-06 10:42:27 -0400 | [diff] [blame] | 935 | if (r0 == NULL || r1 == NULL || r2 == NULL || r3 == NULL) { |
David Benjamin | fb8b763 | 2017-04-10 18:35:22 -0400 | [diff] [blame] | 936 | goto bn_err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 937 | } |
| 938 | |
David Benjamin | fb8b763 | 2017-04-10 18:35:22 -0400 | [diff] [blame] | 939 | /* We need the RSA components non-NULL. */ |
David Benjamin | 43780cb | 2017-04-10 18:05:19 -0400 | [diff] [blame] | 940 | 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 Benjamin | fb8b763 | 2017-04-10 18:35:22 -0400 | [diff] [blame] | 948 | goto bn_err; |
David Benjamin | 6eb000d | 2015-02-11 01:17:41 -0500 | [diff] [blame] | 949 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 950 | |
David Benjamin | 1c703cb | 2015-06-11 21:42:14 -0400 | [diff] [blame] | 951 | if (!BN_copy(rsa->e, e_value)) { |
David Benjamin | fb8b763 | 2017-04-10 18:35:22 -0400 | [diff] [blame] | 952 | goto bn_err; |
David Benjamin | 1c703cb | 2015-06-11 21:42:14 -0400 | [diff] [blame] | 953 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 954 | |
David Benjamin | fb8b763 | 2017-04-10 18:35:22 -0400 | [diff] [blame] | 955 | 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 Benjamin | 6eb000d | 2015-02-11 01:17:41 -0500 | [diff] [blame] | 964 | } |
David Benjamin | fb8b763 | 2017-04-10 18:35:22 -0400 | [diff] [blame] | 965 | |
| 966 | if (BN_cmp(rsa->p, rsa->q) < 0) { |
| 967 | BIGNUM *tmp = rsa->p; |
| 968 | rsa->p = rsa->q; |
| 969 | rsa->q = tmp; |
David Benjamin | 6eb000d | 2015-02-11 01:17:41 -0500 | [diff] [blame] | 970 | } |
David Benjamin | fb8b763 | 2017-04-10 18:35:22 -0400 | [diff] [blame] | 971 | |
| 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 Benjamin | 6eb000d | 2015-02-11 01:17:41 -0500 | [diff] [blame] | 978 | } |
David Benjamin | fb8b763 | 2017-04-10 18:35:22 -0400 | [diff] [blame] | 979 | |
| 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 Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 991 | } |
Adam Langley | 839b881 | 2015-05-26 11:36:46 -0700 | [diff] [blame] | 992 | |
David Benjamin | fb8b763 | 2017-04-10 18:35:22 -0400 | [diff] [blame] | 993 | /* 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 Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 997 | goto err; |
David Benjamin | 6eb000d | 2015-02-11 01:17:41 -0500 | [diff] [blame] | 998 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 999 | |
David Benjamin | 0b8dc30 | 2016-12-17 14:27:16 -0500 | [diff] [blame] | 1000 | /* 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 Benjamin | 0b8dc30 | 2016-12-17 14:27:16 -0500 | [diff] [blame] | 1004 | if (!BN_MONT_CTX_set_locked(&rsa->mont_p, &rsa->lock, rsa->p, ctx) || |
David Benjamin | 0a211df | 2016-12-17 15:25:55 -0500 | [diff] [blame] | 1005 | !bn_mod_inverse_secret_prime(rsa->iqmp, rsa->q, rsa->p, ctx, |
| 1006 | rsa->mont_p)) { |
David Benjamin | fb8b763 | 2017-04-10 18:35:22 -0400 | [diff] [blame] | 1007 | goto bn_err; |
David Benjamin | 6eb000d | 2015-02-11 01:17:41 -0500 | [diff] [blame] | 1008 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1009 | |
Brian Smith | febf771 | 2016-03-21 13:47:32 -1000 | [diff] [blame] | 1010 | /* 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 Benjamin | fb8b763 | 2017-04-10 18:35:22 -0400 | [diff] [blame] | 1013 | if (!RSA_check_key(rsa)) { |
Brian Smith | febf771 | 2016-03-21 13:47:32 -1000 | [diff] [blame] | 1014 | OPENSSL_PUT_ERROR(RSA, RSA_R_INTERNAL_ERROR); |
David Benjamin | fb8b763 | 2017-04-10 18:35:22 -0400 | [diff] [blame] | 1015 | goto err; |
Brian Smith | febf771 | 2016-03-21 13:47:32 -1000 | [diff] [blame] | 1016 | } |
| 1017 | |
David Benjamin | fb8b763 | 2017-04-10 18:35:22 -0400 | [diff] [blame] | 1018 | ret = 1; |
| 1019 | |
| 1020 | bn_err: |
| 1021 | if (!ret) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1022 | OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1023 | } |
David Benjamin | fb8b763 | 2017-04-10 18:35:22 -0400 | [diff] [blame] | 1024 | err: |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1025 | if (ctx != NULL) { |
| 1026 | BN_CTX_end(ctx); |
| 1027 | BN_CTX_free(ctx); |
| 1028 | } |
David Benjamin | fb8b763 | 2017-04-10 18:35:22 -0400 | [diff] [blame] | 1029 | return ret; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
Brian Smith | f08c1c6 | 2016-03-25 13:24:46 -1000 | [diff] [blame] | 1032 | /* 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 Benjamin | d93831d | 2015-10-29 13:19:12 -0400 | [diff] [blame] | 1035 | const RSA_METHOD RSA_default_method = { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1036 | { |
| 1037 | 0 /* references */, |
| 1038 | 1 /* is_static */, |
| 1039 | }, |
| 1040 | NULL /* app_data */, |
| 1041 | |
| 1042 | NULL /* init */, |
David Benjamin | d93831d | 2015-10-29 13:19:12 -0400 | [diff] [blame] | 1043 | NULL /* finish (defaults to rsa_default_finish) */, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1044 | |
David Benjamin | d93831d | 2015-10-29 13:19:12 -0400 | [diff] [blame] | 1045 | NULL /* size (defaults to rsa_default_size) */, |
David Benjamin | 925fee3 | 2014-07-11 14:14:08 -0400 | [diff] [blame] | 1046 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1047 | NULL /* sign */, |
| 1048 | NULL /* verify */, |
| 1049 | |
David Benjamin | 073391f | 2017-05-03 15:03:35 -0400 | [diff] [blame^] | 1050 | NULL /* encrypt (ignored) */, |
David Benjamin | d93831d | 2015-10-29 13:19:12 -0400 | [diff] [blame] | 1051 | NULL /* sign_raw (defaults to rsa_default_sign_raw) */, |
| 1052 | NULL /* decrypt (defaults to rsa_default_decrypt) */, |
David Benjamin | 073391f | 2017-05-03 15:03:35 -0400 | [diff] [blame^] | 1053 | NULL /* verify_raw (ignored) */, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1054 | |
David Benjamin | d93831d | 2015-10-29 13:19:12 -0400 | [diff] [blame] | 1055 | NULL /* private_transform (defaults to rsa_default_private_transform) */, |
Adam Langley | 6bc658d | 2014-08-18 13:29:45 -0700 | [diff] [blame] | 1056 | |
Brian Smith | f08c1c6 | 2016-03-25 13:24:46 -1000 | [diff] [blame] | 1057 | NULL /* mod_exp (ignored) */, |
| 1058 | NULL /* bn_mod_exp (ignored) */, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1059 | |
| 1060 | RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE, |
| 1061 | |
David Benjamin | 073391f | 2017-05-03 15:03:35 -0400 | [diff] [blame^] | 1062 | NULL /* keygen (ignored) */, |
David Benjamin | 4a2cc28 | 2017-04-10 18:20:55 -0400 | [diff] [blame] | 1063 | NULL /* multi_prime_keygen (ignored) */, |
Adam Langley | 626c686 | 2015-09-11 16:17:44 -0700 | [diff] [blame] | 1064 | |
David Benjamin | 073391f | 2017-05-03 15:03:35 -0400 | [diff] [blame^] | 1065 | NULL /* supports_digest (ignored) */, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1066 | }; |