1 /* 2 * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved. 3 * 4 * Licensed under the Apache License 2.0 (the "License"). You may not use 5 * this file except in compliance with the License. You can obtain a copy 6 * in the file LICENSE in the source distribution or at 7 * https://www.openssl.org/source/license.html 8 */ 9 10 /* 11 * NB: these functions have been "upgraded", the deprecated versions (which 12 * are compatibility wrappers using these functions) are in rsa_depr.c. - 13 * Geoff 14 */ 15 16 /* 17 * RSA low level APIs are deprecated for public use, but still ok for 18 * internal use. 19 */ 20 #include "internal/deprecated.h" 21 22 #include <stdio.h> 23 #include <time.h> 24 #include "internal/cryptlib.h" 25 #include <openssl/bn.h> 26 #include <openssl/self_test.h> 27 #include "prov/providercommon.h" 28 #include "rsa_local.h" 29 30 static int rsa_keygen_pairwise_test(RSA *rsa, OSSL_CALLBACK *cb, void *cbarg); 31 static int rsa_keygen(OSSL_LIB_CTX *libctx, RSA *rsa, int bits, int primes, 32 BIGNUM *e_value, BN_GENCB *cb, int pairwise_test); 33 34 /* 35 * NB: this wrapper would normally be placed in rsa_lib.c and the static 36 * implementation would probably be in rsa_eay.c. Nonetheless, is kept here 37 * so that we don't introduce a new linker dependency. Eg. any application 38 * that wasn't previously linking object code related to key-generation won't 39 * have to now just because key-generation is part of RSA_METHOD. 40 */ 41 int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) 42 { 43 if (rsa->meth->rsa_keygen != NULL) 44 return rsa->meth->rsa_keygen(rsa, bits, e_value, cb); 45 46 return RSA_generate_multi_prime_key(rsa, bits, RSA_DEFAULT_PRIME_NUM, 47 e_value, cb); 48 } 49 50 int RSA_generate_multi_prime_key(RSA *rsa, int bits, int primes, 51 BIGNUM *e_value, BN_GENCB *cb) 52 { 53 #ifndef FIPS_MODULE 54 /* multi-prime is only supported with the builtin key generation */ 55 if (rsa->meth->rsa_multi_prime_keygen != NULL) { 56 return rsa->meth->rsa_multi_prime_keygen(rsa, bits, primes, 57 e_value, cb); 58 } else if (rsa->meth->rsa_keygen != NULL) { 59 /* 60 * However, if rsa->meth implements only rsa_keygen, then we 61 * have to honour it in 2-prime case and assume that it wouldn't 62 * know what to do with multi-prime key generated by builtin 63 * subroutine... 64 */ 65 if (primes == 2) 66 return rsa->meth->rsa_keygen(rsa, bits, e_value, cb); 67 else 68 return 0; 69 } 70 #endif /* FIPS_MODULE */ 71 return rsa_keygen(rsa->libctx, rsa, bits, primes, e_value, cb, 0); 72 } 73 74 #ifndef FIPS_MODULE 75 static int rsa_multiprime_keygen(RSA *rsa, int bits, int primes, 76 BIGNUM *e_value, BN_GENCB *cb) 77 { 78 BIGNUM *r0 = NULL, *r1 = NULL, *r2 = NULL, *tmp, *prime; 79 int n = 0, bitsr[RSA_MAX_PRIME_NUM], bitse = 0; 80 int i = 0, quo = 0, rmd = 0, adj = 0, retries = 0; 81 RSA_PRIME_INFO *pinfo = NULL; 82 STACK_OF(RSA_PRIME_INFO) *prime_infos = NULL; 83 BN_CTX *ctx = NULL; 84 BN_ULONG bitst = 0; 85 unsigned long error = 0; 86 int ok = -1; 87 88 if (bits < RSA_MIN_MODULUS_BITS) { 89 ok = 0; /* we set our own err */ 90 ERR_raise(ERR_LIB_RSA, RSA_R_KEY_SIZE_TOO_SMALL); 91 goto err; 92 } 93 94 /* A bad value for e can cause infinite loops */ 95 if (e_value != NULL && !ossl_rsa_check_public_exponent(e_value)) { 96 ERR_raise(ERR_LIB_RSA, RSA_R_PUB_EXPONENT_OUT_OF_RANGE); 97 return 0; 98 } 99 100 if (primes < RSA_DEFAULT_PRIME_NUM || primes > ossl_rsa_multip_cap(bits)) { 101 ok = 0; /* we set our own err */ 102 ERR_raise(ERR_LIB_RSA, RSA_R_KEY_PRIME_NUM_INVALID); 103 goto err; 104 } 105 106 ctx = BN_CTX_new_ex(rsa->libctx); 107 if (ctx == NULL) 108 goto err; 109 BN_CTX_start(ctx); 110 r0 = BN_CTX_get(ctx); 111 r1 = BN_CTX_get(ctx); 112 r2 = BN_CTX_get(ctx); 113 if (r2 == NULL) 114 goto err; 115 116 /* divide bits into 'primes' pieces evenly */ 117 quo = bits / primes; 118 rmd = bits % primes; 119 120 for (i = 0; i < primes; i++) 121 bitsr[i] = (i < rmd) ? quo + 1 : quo; 122 123 rsa->dirty_cnt++; 124 125 /* We need the RSA components non-NULL */ 126 if (!rsa->n && ((rsa->n = BN_new()) == NULL)) 127 goto err; 128 if (!rsa->d && ((rsa->d = BN_secure_new()) == NULL)) 129 goto err; 130 BN_set_flags(rsa->d, BN_FLG_CONSTTIME); 131 if (!rsa->e && ((rsa->e = BN_new()) == NULL)) 132 goto err; 133 if (!rsa->p && ((rsa->p = BN_secure_new()) == NULL)) 134 goto err; 135 BN_set_flags(rsa->p, BN_FLG_CONSTTIME); 136 if (!rsa->q && ((rsa->q = BN_secure_new()) == NULL)) 137 goto err; 138 BN_set_flags(rsa->q, BN_FLG_CONSTTIME); 139 if (!rsa->dmp1 && ((rsa->dmp1 = BN_secure_new()) == NULL)) 140 goto err; 141 BN_set_flags(rsa->dmp1, BN_FLG_CONSTTIME); 142 if (!rsa->dmq1 && ((rsa->dmq1 = BN_secure_new()) == NULL)) 143 goto err; 144 BN_set_flags(rsa->dmq1, BN_FLG_CONSTTIME); 145 if (!rsa->iqmp && ((rsa->iqmp = BN_secure_new()) == NULL)) 146 goto err; 147 BN_set_flags(rsa->iqmp, BN_FLG_CONSTTIME); 148 149 /* initialize multi-prime components */ 150 if (primes > RSA_DEFAULT_PRIME_NUM) { 151 rsa->version = RSA_ASN1_VERSION_MULTI; 152 prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, primes - 2); 153 if (prime_infos == NULL) 154 goto err; 155 if (rsa->prime_infos != NULL) { 156 /* could this happen? */ 157 sk_RSA_PRIME_INFO_pop_free(rsa->prime_infos, 158 ossl_rsa_multip_info_free); 159 } 160 rsa->prime_infos = prime_infos; 161 162 /* prime_info from 2 to |primes| -1 */ 163 for (i = 2; i < primes; i++) { 164 pinfo = ossl_rsa_multip_info_new(); 165 if (pinfo == NULL) 166 goto err; 167 (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo); 168 } 169 } 170 171 if (BN_copy(rsa->e, e_value) == NULL) 172 goto err; 173 174 /* generate p, q and other primes (if any) */ 175 for (i = 0; i < primes; i++) { 176 adj = 0; 177 retries = 0; 178 179 if (i == 0) { 180 prime = rsa->p; 181 } else if (i == 1) { 182 prime = rsa->q; 183 } else { 184 pinfo = sk_RSA_PRIME_INFO_value(prime_infos, i - 2); 185 prime = pinfo->r; 186 } 187 BN_set_flags(prime, BN_FLG_CONSTTIME); 188 189 for (;;) { 190 redo: 191 if (!BN_generate_prime_ex2(prime, bitsr[i] + adj, 0, NULL, NULL, 192 cb, ctx)) 193 goto err; 194 /* 195 * prime should not be equal to p, q, r_3... 196 * (those primes prior to this one) 197 */ 198 { 199 int j; 200 201 for (j = 0; j < i; j++) { 202 BIGNUM *prev_prime; 203 204 if (j == 0) 205 prev_prime = rsa->p; 206 else if (j == 1) 207 prev_prime = rsa->q; 208 else 209 prev_prime = sk_RSA_PRIME_INFO_value(prime_infos, 210 j - 2)->r; 211 212 if (!BN_cmp(prime, prev_prime)) { 213 goto redo; 214 } 215 } 216 } 217 if (!BN_sub(r2, prime, BN_value_one())) 218 goto err; 219 ERR_set_mark(); 220 BN_set_flags(r2, BN_FLG_CONSTTIME); 221 if (BN_mod_inverse(r1, r2, rsa->e, ctx) != NULL) { 222 /* GCD == 1 since inverse exists */ 223 break; 224 } 225 error = ERR_peek_last_error(); 226 if (ERR_GET_LIB(error) == ERR_LIB_BN 227 && ERR_GET_REASON(error) == BN_R_NO_INVERSE) { 228 /* GCD != 1 */ 229 ERR_pop_to_mark(); 230 } else { 231 goto err; 232 } 233 if (!BN_GENCB_call(cb, 2, n++)) 234 goto err; 235 } 236 237 bitse += bitsr[i]; 238 239 /* calculate n immediately to see if it's sufficient */ 240 if (i == 1) { 241 /* we get at least 2 primes */ 242 if (!BN_mul(r1, rsa->p, rsa->q, ctx)) 243 goto err; 244 } else if (i != 0) { 245 /* modulus n = p * q * r_3 * r_4 ... */ 246 if (!BN_mul(r1, rsa->n, prime, ctx)) 247 goto err; 248 } else { 249 /* i == 0, do nothing */ 250 if (!BN_GENCB_call(cb, 3, i)) 251 goto err; 252 continue; 253 } 254 /* 255 * if |r1|, product of factors so far, is not as long as expected 256 * (by checking the first 4 bits are less than 0x9 or greater than 257 * 0xF). If so, re-generate the last prime. 258 * 259 * NOTE: This actually can't happen in two-prime case, because of 260 * the way factors are generated. 261 * 262 * Besides, another consideration is, for multi-prime case, even the 263 * length modulus is as long as expected, the modulus could start at 264 * 0x8, which could be utilized to distinguish a multi-prime private 265 * key by using the modulus in a certificate. This is also covered 266 * by checking the length should not be less than 0x9. 267 */ 268 if (!BN_rshift(r2, r1, bitse - 4)) 269 goto err; 270 bitst = BN_get_word(r2); 271 272 if (bitst < 0x9 || bitst > 0xF) { 273 /* 274 * For keys with more than 4 primes, we attempt longer factor to 275 * meet length requirement. 276 * 277 * Otherwise, we just re-generate the prime with the same length. 278 * 279 * This strategy has the following goals: 280 * 281 * 1. 1024-bit factors are efficient when using 3072 and 4096-bit key 282 * 2. stay the same logic with normal 2-prime key 283 */ 284 bitse -= bitsr[i]; 285 if (!BN_GENCB_call(cb, 2, n++)) 286 goto err; 287 if (primes > 4) { 288 if (bitst < 0x9) 289 adj++; 290 else 291 adj--; 292 } else if (retries == 4) { 293 /* 294 * re-generate all primes from scratch, mainly used 295 * in 4 prime case to avoid long loop. Max retry times 296 * is set to 4. 297 */ 298 i = -1; 299 bitse = 0; 300 continue; 301 } 302 retries++; 303 goto redo; 304 } 305 /* save product of primes for further use, for multi-prime only */ 306 if (i > 1 && BN_copy(pinfo->pp, rsa->n) == NULL) 307 goto err; 308 if (BN_copy(rsa->n, r1) == NULL) 309 goto err; 310 if (!BN_GENCB_call(cb, 3, i)) 311 goto err; 312 } 313 314 if (BN_cmp(rsa->p, rsa->q) < 0) { 315 tmp = rsa->p; 316 rsa->p = rsa->q; 317 rsa->q = tmp; 318 } 319 320 /* calculate d */ 321 322 /* p - 1 */ 323 if (!BN_sub(r1, rsa->p, BN_value_one())) 324 goto err; 325 /* q - 1 */ 326 if (!BN_sub(r2, rsa->q, BN_value_one())) 327 goto err; 328 /* (p - 1)(q - 1) */ 329 if (!BN_mul(r0, r1, r2, ctx)) 330 goto err; 331 /* multi-prime */ 332 for (i = 2; i < primes; i++) { 333 pinfo = sk_RSA_PRIME_INFO_value(prime_infos, i - 2); 334 /* save r_i - 1 to pinfo->d temporarily */ 335 if (!BN_sub(pinfo->d, pinfo->r, BN_value_one())) 336 goto err; 337 if (!BN_mul(r0, r0, pinfo->d, ctx)) 338 goto err; 339 } 340 341 { 342 BIGNUM *pr0 = BN_new(); 343 344 if (pr0 == NULL) 345 goto err; 346 347 BN_with_flags(pr0, r0, BN_FLG_CONSTTIME); 348 if (!BN_mod_inverse(rsa->d, rsa->e, pr0, ctx)) { 349 BN_free(pr0); 350 goto err; /* d */ 351 } 352 /* We MUST free pr0 before any further use of r0 */ 353 BN_free(pr0); 354 } 355 356 { 357 BIGNUM *d = BN_new(); 358 359 if (d == NULL) 360 goto err; 361 362 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME); 363 364 /* calculate d mod (p-1) and d mod (q - 1) */ 365 if (!BN_mod(rsa->dmp1, d, r1, ctx) 366 || !BN_mod(rsa->dmq1, d, r2, ctx)) { 367 BN_free(d); 368 goto err; 369 } 370 371 /* calculate CRT exponents */ 372 for (i = 2; i < primes; i++) { 373 pinfo = sk_RSA_PRIME_INFO_value(prime_infos, i - 2); 374 /* pinfo->d == r_i - 1 */ 375 if (!BN_mod(pinfo->d, d, pinfo->d, ctx)) { 376 BN_free(d); 377 goto err; 378 } 379 } 380 381 /* We MUST free d before any further use of rsa->d */ 382 BN_free(d); 383 } 384 385 { 386 BIGNUM *p = BN_new(); 387 388 if (p == NULL) 389 goto err; 390 BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME); 391 392 /* calculate inverse of q mod p */ 393 if (!BN_mod_inverse(rsa->iqmp, rsa->q, p, ctx)) { 394 BN_free(p); 395 goto err; 396 } 397 398 /* calculate CRT coefficient for other primes */ 399 for (i = 2; i < primes; i++) { 400 pinfo = sk_RSA_PRIME_INFO_value(prime_infos, i - 2); 401 BN_with_flags(p, pinfo->r, BN_FLG_CONSTTIME); 402 if (!BN_mod_inverse(pinfo->t, pinfo->pp, p, ctx)) { 403 BN_free(p); 404 goto err; 405 } 406 } 407 408 /* We MUST free p before any further use of rsa->p */ 409 BN_free(p); 410 } 411 412 ok = 1; 413 err: 414 if (ok == -1) { 415 ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB); 416 ok = 0; 417 } 418 BN_CTX_end(ctx); 419 BN_CTX_free(ctx); 420 return ok; 421 } 422 #endif /* FIPS_MODULE */ 423 424 static int rsa_keygen(OSSL_LIB_CTX *libctx, RSA *rsa, int bits, int primes, 425 BIGNUM *e_value, BN_GENCB *cb, int pairwise_test) 426 { 427 int ok = 0; 428 429 #ifdef FIPS_MODULE 430 ok = ossl_rsa_sp800_56b_generate_key(rsa, bits, e_value, cb); 431 pairwise_test = 1; /* FIPS MODE needs to always run the pairwise test */ 432 #else 433 /* 434 * Only multi-prime keys or insecure keys with a small key length or a 435 * public exponent <= 2^16 will use the older rsa_multiprime_keygen(). 436 */ 437 if (primes == 2 438 && bits >= 2048 439 && (e_value == NULL || BN_num_bits(e_value) > 16)) 440 ok = ossl_rsa_sp800_56b_generate_key(rsa, bits, e_value, cb); 441 else 442 ok = rsa_multiprime_keygen(rsa, bits, primes, e_value, cb); 443 #endif /* FIPS_MODULE */ 444 445 if (pairwise_test && ok > 0) { 446 OSSL_CALLBACK *stcb = NULL; 447 void *stcbarg = NULL; 448 449 OSSL_SELF_TEST_get_callback(libctx, &stcb, &stcbarg); 450 ok = rsa_keygen_pairwise_test(rsa, stcb, stcbarg); 451 if (!ok) { 452 ossl_set_error_state(OSSL_SELF_TEST_TYPE_PCT); 453 /* Clear intermediate results */ 454 BN_clear_free(rsa->d); 455 BN_clear_free(rsa->p); 456 BN_clear_free(rsa->q); 457 BN_clear_free(rsa->dmp1); 458 BN_clear_free(rsa->dmq1); 459 BN_clear_free(rsa->iqmp); 460 rsa->d = NULL; 461 rsa->p = NULL; 462 rsa->q = NULL; 463 rsa->dmp1 = NULL; 464 rsa->dmq1 = NULL; 465 rsa->iqmp = NULL; 466 } 467 } 468 return ok; 469 } 470 471 /* 472 * For RSA key generation it is not known whether the key pair will be used 473 * for key transport or signatures. FIPS 140-2 IG 9.9 states that in this case 474 * either a signature verification OR an encryption operation may be used to 475 * perform the pairwise consistency check. The simpler encrypt/decrypt operation 476 * has been chosen for this case. 477 */ 478 static int rsa_keygen_pairwise_test(RSA *rsa, OSSL_CALLBACK *cb, void *cbarg) 479 { 480 int ret = 0; 481 unsigned int ciphertxt_len; 482 unsigned char *ciphertxt = NULL; 483 const unsigned char plaintxt[16] = {0}; 484 unsigned char *decoded = NULL; 485 unsigned int decoded_len; 486 unsigned int plaintxt_len = (unsigned int)sizeof(plaintxt_len); 487 int padding = RSA_PKCS1_PADDING; 488 OSSL_SELF_TEST *st = NULL; 489 490 st = OSSL_SELF_TEST_new(cb, cbarg); 491 if (st == NULL) 492 goto err; 493 OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_PCT, 494 OSSL_SELF_TEST_DESC_PCT_RSA_PKCS1); 495 496 ciphertxt_len = RSA_size(rsa); 497 /* 498 * RSA_private_encrypt() and RSA_private_decrypt() requires the 'to' 499 * parameter to be a maximum of RSA_size() - allocate space for both. 500 */ 501 ciphertxt = OPENSSL_zalloc(ciphertxt_len * 2); 502 if (ciphertxt == NULL) 503 goto err; 504 decoded = ciphertxt + ciphertxt_len; 505 506 ciphertxt_len = RSA_public_encrypt(plaintxt_len, plaintxt, ciphertxt, rsa, 507 padding); 508 if (ciphertxt_len <= 0) 509 goto err; 510 if (ciphertxt_len == plaintxt_len 511 && memcmp(ciphertxt, plaintxt, plaintxt_len) == 0) 512 goto err; 513 514 OSSL_SELF_TEST_oncorrupt_byte(st, ciphertxt); 515 516 decoded_len = RSA_private_decrypt(ciphertxt_len, ciphertxt, decoded, rsa, 517 padding); 518 if (decoded_len != plaintxt_len 519 || memcmp(decoded, plaintxt, decoded_len) != 0) 520 goto err; 521 522 ret = 1; 523 err: 524 OSSL_SELF_TEST_onend(st, ret); 525 OSSL_SELF_TEST_free(st); 526 OPENSSL_free(ciphertxt); 527 528 return ret; 529 } 530