rsa_gen.c 22 KB

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