rsa_sp800_56b_gen.c 12 KB

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