rsa_gen.c 22 KB

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