rsa_sp800_56b_gen.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. * Copyright 2018-2024 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2018-2019, Oracle and/or its affiliates. All rights reserved.
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #include <openssl/err.h>
  11. #include <openssl/bn.h>
  12. #include <openssl/core.h>
  13. #include <openssl/evp.h>
  14. #include <openssl/rand.h>
  15. #include "crypto/bn.h"
  16. #include "crypto/security_bits.h"
  17. #include "rsa_local.h"
  18. #define RSA_FIPS1864_MIN_KEYGEN_KEYSIZE 2048
  19. #define RSA_FIPS1864_MIN_KEYGEN_STRENGTH 112
  20. /*
  21. * Generate probable primes 'p' & 'q'. See FIPS 186-4 Section B.3.6
  22. * "Generation of Probable Primes with Conditions Based on Auxiliary Probable
  23. * Primes".
  24. *
  25. * Params:
  26. * rsa Object used to store primes p & q.
  27. * test Object used for CAVS testing only.that contains..
  28. * p1, p2 The returned auxiliary primes for p.
  29. * If NULL they are not returned.
  30. * Xp An optional passed in value (that is random number used during
  31. * generation of p).
  32. * Xp1, Xp2 Optionally passed in randomly generated numbers from which
  33. * auxiliary primes p1 & p2 are calculated. If NULL these values
  34. * are generated internally.
  35. * q1, q2 The returned auxiliary primes for q.
  36. * If NULL they are not returned.
  37. * Xq An optional passed in value (that is random number used during
  38. * generation of q).
  39. * Xq1, Xq2 Optionally passed in randomly generated numbers from which
  40. * auxiliary primes q1 & q2 are calculated. If NULL these values
  41. * are generated internally.
  42. * nbits The key size in bits (The size of the modulus n).
  43. * e The public exponent.
  44. * ctx A BN_CTX object.
  45. * cb An optional BIGNUM callback.
  46. * Returns: 1 if successful, or 0 otherwise.
  47. * Notes:
  48. * p1, p2, q1, q2 are returned if they are not NULL.
  49. * Xp, Xp1, Xp2, Xq, Xq1, Xq2 are optionally passed in.
  50. * (Required for CAVS testing).
  51. */
  52. int ossl_rsa_fips186_4_gen_prob_primes(RSA *rsa, RSA_ACVP_TEST *test,
  53. int nbits, const BIGNUM *e, BN_CTX *ctx,
  54. BN_GENCB *cb)
  55. {
  56. int ret = 0, ok;
  57. /* Temp allocated BIGNUMS */
  58. BIGNUM *Xpo = NULL, *Xqo = NULL, *tmp = NULL;
  59. /* Intermediate BIGNUMS that can be returned for testing */
  60. BIGNUM *p1 = NULL, *p2 = NULL;
  61. BIGNUM *q1 = NULL, *q2 = NULL;
  62. /* Intermediate BIGNUMS that can be input for testing */
  63. BIGNUM *Xp = NULL, *Xp1 = NULL, *Xp2 = NULL;
  64. BIGNUM *Xq = NULL, *Xq1 = NULL, *Xq2 = NULL;
  65. #if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
  66. if (test != NULL) {
  67. Xp1 = test->Xp1;
  68. Xp2 = test->Xp2;
  69. Xq1 = test->Xq1;
  70. Xq2 = test->Xq2;
  71. Xp = test->Xp;
  72. Xq = test->Xq;
  73. p1 = test->p1;
  74. p2 = test->p2;
  75. q1 = test->q1;
  76. q2 = test->q2;
  77. }
  78. #endif
  79. /* (Step 1) Check key length
  80. * NOTE: SP800-131A Rev1 Disallows key lengths of < 2048 bits for RSA
  81. * Signature Generation and Key Agree/Transport.
  82. */
  83. if (nbits < RSA_FIPS1864_MIN_KEYGEN_KEYSIZE) {
  84. ERR_raise(ERR_LIB_RSA, RSA_R_KEY_SIZE_TOO_SMALL);
  85. return 0;
  86. }
  87. if (!ossl_rsa_check_public_exponent(e)) {
  88. ERR_raise(ERR_LIB_RSA, RSA_R_PUB_EXPONENT_OUT_OF_RANGE);
  89. return 0;
  90. }
  91. /* (Step 3) Determine strength and check rand generator strength is ok -
  92. * this step is redundant because the generator always returns a higher
  93. * strength than is required.
  94. */
  95. BN_CTX_start(ctx);
  96. tmp = BN_CTX_get(ctx);
  97. Xpo = BN_CTX_get(ctx);
  98. Xqo = BN_CTX_get(ctx);
  99. if (tmp == NULL || Xpo == NULL || Xqo == NULL)
  100. goto err;
  101. BN_set_flags(Xpo, BN_FLG_CONSTTIME);
  102. BN_set_flags(Xqo, BN_FLG_CONSTTIME);
  103. if (rsa->p == NULL)
  104. rsa->p = BN_secure_new();
  105. if (rsa->q == NULL)
  106. rsa->q = BN_secure_new();
  107. if (rsa->p == NULL || rsa->q == NULL)
  108. goto err;
  109. BN_set_flags(rsa->p, BN_FLG_CONSTTIME);
  110. BN_set_flags(rsa->q, BN_FLG_CONSTTIME);
  111. /* (Step 4) Generate p, Xp */
  112. if (!ossl_bn_rsa_fips186_4_gen_prob_primes(rsa->p, Xpo, p1, p2, Xp, Xp1, Xp2,
  113. nbits, e, ctx, cb))
  114. goto err;
  115. for (;;) {
  116. /* (Step 5) Generate q, Xq*/
  117. if (!ossl_bn_rsa_fips186_4_gen_prob_primes(rsa->q, Xqo, q1, q2, Xq, Xq1,
  118. Xq2, nbits, e, ctx, cb))
  119. goto err;
  120. /* (Step 6) |Xp - Xq| > 2^(nbitlen/2 - 100) */
  121. ok = ossl_rsa_check_pminusq_diff(tmp, Xpo, Xqo, nbits);
  122. if (ok < 0)
  123. goto err;
  124. if (ok == 0)
  125. continue;
  126. /* (Step 6) |p - q| > 2^(nbitlen/2 - 100) */
  127. ok = ossl_rsa_check_pminusq_diff(tmp, rsa->p, rsa->q, nbits);
  128. if (ok < 0)
  129. goto err;
  130. if (ok == 0)
  131. continue;
  132. break; /* successfully finished */
  133. }
  134. rsa->dirty_cnt++;
  135. ret = 1;
  136. err:
  137. /* Zeroize any internally generated values that are not returned */
  138. if (Xpo != NULL)
  139. BN_clear(Xpo);
  140. if (Xqo != NULL)
  141. BN_clear(Xqo);
  142. BN_clear(tmp);
  143. BN_CTX_end(ctx);
  144. return ret;
  145. }
  146. /*
  147. * Validates the RSA key size based on the target strength.
  148. * See SP800-56Br1 6.3.1.1 (Steps 1a-1b)
  149. *
  150. * Params:
  151. * nbits The key size in bits.
  152. * strength The target strength in bits. -1 means the target
  153. * strength is unknown.
  154. * Returns: 1 if the key size matches the target strength, or 0 otherwise.
  155. */
  156. int ossl_rsa_sp800_56b_validate_strength(int nbits, int strength)
  157. {
  158. int s = (int)ossl_ifc_ffc_compute_security_bits(nbits);
  159. #ifdef FIPS_MODULE
  160. if (s < RSA_FIPS1864_MIN_KEYGEN_STRENGTH) {
  161. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_MODULUS);
  162. return 0;
  163. }
  164. #endif
  165. if (strength != -1 && s != strength) {
  166. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_STRENGTH);
  167. return 0;
  168. }
  169. return 1;
  170. }
  171. /*
  172. * Validate that the random bit generator is of sufficient strength to generate
  173. * a key of the specified length.
  174. */
  175. static int rsa_validate_rng_strength(EVP_RAND_CTX *rng, int nbits)
  176. {
  177. if (rng == NULL)
  178. return 0;
  179. #ifdef FIPS_MODULE
  180. /*
  181. * This should become mainstream once similar tests are added to the other
  182. * key generations and once there is a way to disable these checks.
  183. */
  184. if (EVP_RAND_get_strength(rng) < ossl_ifc_ffc_compute_security_bits(nbits)) {
  185. ERR_raise(ERR_LIB_RSA,
  186. RSA_R_RANDOMNESS_SOURCE_STRENGTH_INSUFFICIENT);
  187. return 0;
  188. }
  189. #endif
  190. return 1;
  191. }
  192. /*
  193. *
  194. * Using p & q, calculate other required parameters such as n, d.
  195. * as well as the CRT parameters dP, dQ, qInv.
  196. *
  197. * See SP800-56Br1
  198. * 6.3.1.1 rsakpg1 - basic (Steps 3-4)
  199. * 6.3.1.3 rsakpg1 - crt (Step 5)
  200. *
  201. * Params:
  202. * rsa An rsa object.
  203. * nbits The key size.
  204. * e The public exponent.
  205. * ctx A BN_CTX object.
  206. * Notes:
  207. * There is a small chance that the generated d will be too small.
  208. * Returns: -1 = error,
  209. * 0 = d is too small,
  210. * 1 = success.
  211. *
  212. * SP800-56b key generation always passes a non NULL value for e.
  213. * For other purposes, if e is NULL then it is assumed that e, n and d are
  214. * already set in the RSA key and do not need to be recalculated.
  215. */
  216. int ossl_rsa_sp800_56b_derive_params_from_pq(RSA *rsa, int nbits,
  217. const BIGNUM *e, BN_CTX *ctx)
  218. {
  219. int ret = -1;
  220. BIGNUM *p1, *q1, *lcm, *p1q1, *gcd;
  221. BN_CTX_start(ctx);
  222. p1 = BN_CTX_get(ctx);
  223. q1 = BN_CTX_get(ctx);
  224. lcm = BN_CTX_get(ctx);
  225. p1q1 = BN_CTX_get(ctx);
  226. gcd = BN_CTX_get(ctx);
  227. if (gcd == NULL)
  228. goto err;
  229. BN_set_flags(p1, BN_FLG_CONSTTIME);
  230. BN_set_flags(q1, BN_FLG_CONSTTIME);
  231. BN_set_flags(lcm, BN_FLG_CONSTTIME);
  232. BN_set_flags(p1q1, BN_FLG_CONSTTIME);
  233. BN_set_flags(gcd, BN_FLG_CONSTTIME);
  234. /* LCM((p-1, q-1)) */
  235. if (ossl_rsa_get_lcm(ctx, rsa->p, rsa->q, lcm, gcd, p1, q1, p1q1) != 1)
  236. goto err;
  237. /*
  238. * if e is provided as a parameter, don't recompute e, d or n
  239. */
  240. if (e != NULL) {
  241. /* copy e */
  242. BN_free(rsa->e);
  243. rsa->e = BN_dup(e);
  244. if (rsa->e == NULL)
  245. goto err;
  246. BN_clear_free(rsa->d);
  247. /* (Step 3) d = (e^-1) mod (LCM(p-1, q-1)) */
  248. rsa->d = BN_secure_new();
  249. if (rsa->d == NULL)
  250. goto err;
  251. BN_set_flags(rsa->d, BN_FLG_CONSTTIME);
  252. if (BN_mod_inverse(rsa->d, e, lcm, ctx) == NULL)
  253. goto err;
  254. /* (Step 3) return an error if d is too small */
  255. if (BN_num_bits(rsa->d) <= (nbits >> 1)) {
  256. ret = 0;
  257. goto err;
  258. }
  259. /* (Step 4) n = pq */
  260. if (rsa->n == NULL)
  261. rsa->n = BN_new();
  262. if (rsa->n == NULL || !BN_mul(rsa->n, rsa->p, rsa->q, ctx))
  263. goto err;
  264. }
  265. /* (Step 5a) dP = d mod (p-1) */
  266. if (rsa->dmp1 == NULL)
  267. rsa->dmp1 = BN_secure_new();
  268. if (rsa->dmp1 == NULL)
  269. goto err;
  270. BN_set_flags(rsa->dmp1, BN_FLG_CONSTTIME);
  271. if (!BN_mod(rsa->dmp1, rsa->d, p1, ctx))
  272. goto err;
  273. /* (Step 5b) dQ = d mod (q-1) */
  274. if (rsa->dmq1 == NULL)
  275. rsa->dmq1 = BN_secure_new();
  276. if (rsa->dmq1 == NULL)
  277. goto err;
  278. BN_set_flags(rsa->dmq1, BN_FLG_CONSTTIME);
  279. if (!BN_mod(rsa->dmq1, rsa->d, q1, ctx))
  280. goto err;
  281. /* (Step 5c) qInv = (inverse of q) mod p */
  282. BN_free(rsa->iqmp);
  283. rsa->iqmp = BN_secure_new();
  284. if (rsa->iqmp == NULL)
  285. goto err;
  286. BN_set_flags(rsa->iqmp, BN_FLG_CONSTTIME);
  287. if (BN_mod_inverse(rsa->iqmp, rsa->q, rsa->p, ctx) == NULL)
  288. goto err;
  289. rsa->dirty_cnt++;
  290. ret = 1;
  291. err:
  292. if (ret != 1) {
  293. BN_free(rsa->e);
  294. rsa->e = NULL;
  295. BN_free(rsa->d);
  296. rsa->d = NULL;
  297. BN_free(rsa->n);
  298. rsa->n = NULL;
  299. BN_free(rsa->iqmp);
  300. rsa->iqmp = NULL;
  301. BN_free(rsa->dmq1);
  302. rsa->dmq1 = NULL;
  303. BN_free(rsa->dmp1);
  304. rsa->dmp1 = NULL;
  305. }
  306. BN_clear(p1);
  307. BN_clear(q1);
  308. BN_clear(lcm);
  309. BN_clear(p1q1);
  310. BN_clear(gcd);
  311. BN_CTX_end(ctx);
  312. return ret;
  313. }
  314. /*
  315. * Generate a SP800-56B RSA key.
  316. *
  317. * See SP800-56Br1 6.3.1 "RSA Key-Pair Generation with a Fixed Public Exponent"
  318. * 6.3.1.1 rsakpg1 - basic
  319. * 6.3.1.3 rsakpg1 - crt
  320. *
  321. * See also FIPS 186-4 Section B.3.6
  322. * "Generation of Probable Primes with Conditions Based on Auxiliary
  323. * Probable Primes."
  324. *
  325. * Params:
  326. * rsa The rsa object.
  327. * nbits The intended key size in bits.
  328. * efixed The public exponent. If NULL a default of 65537 is used.
  329. * cb An optional BIGNUM callback.
  330. * Returns: 1 if successfully generated otherwise it returns 0.
  331. */
  332. int ossl_rsa_sp800_56b_generate_key(RSA *rsa, int nbits, const BIGNUM *efixed,
  333. BN_GENCB *cb)
  334. {
  335. int ret = 0;
  336. int ok;
  337. BN_CTX *ctx = NULL;
  338. BIGNUM *e = NULL;
  339. RSA_ACVP_TEST *info = NULL;
  340. BIGNUM *tmp;
  341. #if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
  342. info = rsa->acvp_test;
  343. #endif
  344. /* (Steps 1a-1b) : Currently ignores the strength check */
  345. if (!ossl_rsa_sp800_56b_validate_strength(nbits, -1))
  346. return 0;
  347. /* Check that the RNG is capable of generating a key this large */
  348. if (!rsa_validate_rng_strength(RAND_get0_private(rsa->libctx), nbits))
  349. return 0;
  350. ctx = BN_CTX_new_ex(rsa->libctx);
  351. if (ctx == NULL)
  352. return 0;
  353. /* Set default if e is not passed in */
  354. if (efixed == NULL) {
  355. e = BN_new();
  356. if (e == NULL || !BN_set_word(e, 65537))
  357. goto err;
  358. } else {
  359. e = (BIGNUM *)efixed;
  360. }
  361. /* (Step 1c) fixed exponent is checked later .*/
  362. for (;;) {
  363. /* (Step 2) Generate prime factors */
  364. if (!ossl_rsa_fips186_4_gen_prob_primes(rsa, info, nbits, e, ctx, cb))
  365. goto err;
  366. /* p>q check and skipping in case of acvp test */
  367. if (info == NULL && BN_cmp(rsa->p, rsa->q) < 0) {
  368. tmp = rsa->p;
  369. rsa->p = rsa->q;
  370. rsa->q = tmp;
  371. }
  372. /* (Steps 3-5) Compute params d, n, dP, dQ, qInv */
  373. ok = ossl_rsa_sp800_56b_derive_params_from_pq(rsa, nbits, e, ctx);
  374. if (ok < 0)
  375. goto err;
  376. if (ok > 0)
  377. break;
  378. /* Gets here if computed d is too small - so try again */
  379. }
  380. /* (Step 6) Do pairwise test - optional validity test has been omitted */
  381. ret = ossl_rsa_sp800_56b_pairwise_test(rsa, ctx);
  382. err:
  383. if (efixed == NULL)
  384. BN_free(e);
  385. BN_CTX_free(ctx);
  386. return ret;
  387. }
  388. /*
  389. * See SP800-56Br1 6.3.1.3 (Step 6) Perform a pair-wise consistency test by
  390. * verifying that: k = (k^e)^d mod n for some integer k where 1 < k < n-1.
  391. *
  392. * Returns 1 if the RSA key passes the pairwise test or 0 if it fails.
  393. */
  394. int ossl_rsa_sp800_56b_pairwise_test(RSA *rsa, BN_CTX *ctx)
  395. {
  396. int ret = 0;
  397. BIGNUM *k, *tmp;
  398. BN_CTX_start(ctx);
  399. tmp = BN_CTX_get(ctx);
  400. k = BN_CTX_get(ctx);
  401. if (k == NULL)
  402. goto err;
  403. BN_set_flags(k, BN_FLG_CONSTTIME);
  404. ret = (BN_set_word(k, 2)
  405. && BN_mod_exp(tmp, k, rsa->e, rsa->n, ctx)
  406. && BN_mod_exp(tmp, tmp, rsa->d, rsa->n, ctx)
  407. && BN_cmp(k, tmp) == 0);
  408. if (ret == 0)
  409. ERR_raise(ERR_LIB_RSA, RSA_R_PAIRWISE_TEST_FAILURE);
  410. err:
  411. BN_CTX_end(ctx);
  412. return ret;
  413. }