rsa_sp800_56b_gen.c 11 KB

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