2
0

bn_rsa_fips186_4.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. /*
  11. * According to NIST SP800-131A "Transitioning the use of cryptographic
  12. * algorithms and key lengths" Generation of 1024 bit RSA keys are no longer
  13. * allowed for signatures (Table 2) or key transport (Table 5). In the code
  14. * below any attempt to generate 1024 bit RSA keys will result in an error (Note
  15. * that digital signature verification can still use deprecated 1024 bit keys).
  16. *
  17. * FIPS 186-4 relies on the use of the auxiliary primes p1, p2, q1 and q2 that
  18. * must be generated before the module generates the RSA primes p and q.
  19. * Table B.1 in FIPS 186-4 specifies RSA modulus lengths of 2048 and
  20. * 3072 bits only, the min/max total length of the auxiliary primes.
  21. * FIPS 186-5 Table A.1 includes an additional entry for 4096 which has been
  22. * included here.
  23. */
  24. #include <stdio.h>
  25. #include <openssl/bn.h>
  26. #include "bn_local.h"
  27. #include "crypto/bn.h"
  28. #include "internal/nelem.h"
  29. #if BN_BITS2 == 64
  30. # define BN_DEF(lo, hi) (BN_ULONG)hi<<32|lo
  31. #else
  32. # define BN_DEF(lo, hi) lo, hi
  33. #endif
  34. /* 1 / sqrt(2) * 2^256, rounded up */
  35. static const BN_ULONG inv_sqrt_2_val[] = {
  36. BN_DEF(0x83339916UL, 0xED17AC85UL), BN_DEF(0x893BA84CUL, 0x1D6F60BAUL),
  37. BN_DEF(0x754ABE9FUL, 0x597D89B3UL), BN_DEF(0xF9DE6484UL, 0xB504F333UL)
  38. };
  39. const BIGNUM ossl_bn_inv_sqrt_2 = {
  40. (BN_ULONG *)inv_sqrt_2_val,
  41. OSSL_NELEM(inv_sqrt_2_val),
  42. OSSL_NELEM(inv_sqrt_2_val),
  43. 0,
  44. BN_FLG_STATIC_DATA
  45. };
  46. /*
  47. * FIPS 186-5 Table A.1. "Min length of auxiliary primes p1, p2, q1, q2".
  48. * (FIPS 186-5 has an entry for >= 4096 bits).
  49. *
  50. * Params:
  51. * nbits The key size in bits.
  52. * Returns:
  53. * The minimum size of the auxiliary primes or 0 if nbits is invalid.
  54. */
  55. static int bn_rsa_fips186_5_aux_prime_min_size(int nbits)
  56. {
  57. if (nbits >= 4096)
  58. return 201;
  59. if (nbits >= 3072)
  60. return 171;
  61. if (nbits >= 2048)
  62. return 141;
  63. return 0;
  64. }
  65. /*
  66. * FIPS 186-5 Table A.1 "Max of len(p1) + len(p2) and
  67. * len(q1) + len(q2) for p,q Probable Primes".
  68. * (FIPS 186-5 has an entry for >= 4096 bits).
  69. * Params:
  70. * nbits The key size in bits.
  71. * Returns:
  72. * The maximum length or 0 if nbits is invalid.
  73. */
  74. static int bn_rsa_fips186_5_aux_prime_max_sum_size_for_prob_primes(int nbits)
  75. {
  76. if (nbits >= 4096)
  77. return 2030;
  78. if (nbits >= 3072)
  79. return 1518;
  80. if (nbits >= 2048)
  81. return 1007;
  82. return 0;
  83. }
  84. /*
  85. * Find the first odd integer that is a probable prime.
  86. *
  87. * See section FIPS 186-4 B.3.6 (Steps 4.2/5.2).
  88. *
  89. * Params:
  90. * Xp1 The passed in starting point to find a probably prime.
  91. * p1 The returned probable prime (first odd integer >= Xp1)
  92. * ctx A BN_CTX object.
  93. * cb An optional BIGNUM callback.
  94. * Returns: 1 on success otherwise it returns 0.
  95. */
  96. static int bn_rsa_fips186_4_find_aux_prob_prime(const BIGNUM *Xp1,
  97. BIGNUM *p1, BN_CTX *ctx,
  98. BN_GENCB *cb)
  99. {
  100. int ret = 0;
  101. int i = 0;
  102. int tmp = 0;
  103. if (BN_copy(p1, Xp1) == NULL)
  104. return 0;
  105. BN_set_flags(p1, BN_FLG_CONSTTIME);
  106. /* Find the first odd number >= Xp1 that is probably prime */
  107. for (;;) {
  108. i++;
  109. BN_GENCB_call(cb, 0, i);
  110. /* MR test with trial division */
  111. tmp = BN_check_prime(p1, ctx, cb);
  112. if (tmp > 0)
  113. break;
  114. if (tmp < 0)
  115. goto err;
  116. /* Get next odd number */
  117. if (!BN_add_word(p1, 2))
  118. goto err;
  119. }
  120. BN_GENCB_call(cb, 2, i);
  121. ret = 1;
  122. err:
  123. return ret;
  124. }
  125. /*
  126. * Generate a probable prime (p or q).
  127. *
  128. * See FIPS 186-4 B.3.6 (Steps 4 & 5)
  129. *
  130. * Params:
  131. * p The returned probable prime.
  132. * Xpout An optionally returned random number used during generation of p.
  133. * p1, p2 The returned auxiliary primes. If NULL they are not returned.
  134. * Xp An optional passed in value (that is random number used during
  135. * generation of p).
  136. * Xp1, Xp2 Optional passed in values that are normally generated
  137. * internally. Used to find p1, p2.
  138. * nlen The bit length of the modulus (the key size).
  139. * e The public exponent.
  140. * ctx A BN_CTX object.
  141. * cb An optional BIGNUM callback.
  142. * Returns: 1 on success otherwise it returns 0.
  143. */
  144. int ossl_bn_rsa_fips186_4_gen_prob_primes(BIGNUM *p, BIGNUM *Xpout,
  145. BIGNUM *p1, BIGNUM *p2,
  146. const BIGNUM *Xp, const BIGNUM *Xp1,
  147. const BIGNUM *Xp2, int nlen,
  148. const BIGNUM *e, BN_CTX *ctx,
  149. BN_GENCB *cb)
  150. {
  151. int ret = 0;
  152. BIGNUM *p1i = NULL, *p2i = NULL, *Xp1i = NULL, *Xp2i = NULL;
  153. int bitlen;
  154. if (p == NULL || Xpout == NULL)
  155. return 0;
  156. BN_CTX_start(ctx);
  157. p1i = (p1 != NULL) ? p1 : BN_CTX_get(ctx);
  158. p2i = (p2 != NULL) ? p2 : BN_CTX_get(ctx);
  159. Xp1i = (Xp1 != NULL) ? (BIGNUM *)Xp1 : BN_CTX_get(ctx);
  160. Xp2i = (Xp2 != NULL) ? (BIGNUM *)Xp2 : BN_CTX_get(ctx);
  161. if (p1i == NULL || p2i == NULL || Xp1i == NULL || Xp2i == NULL)
  162. goto err;
  163. bitlen = bn_rsa_fips186_5_aux_prime_min_size(nlen);
  164. if (bitlen == 0)
  165. goto err;
  166. /* (Steps 4.1/5.1): Randomly generate Xp1 if it is not passed in */
  167. if (Xp1 == NULL) {
  168. /* Set the top and bottom bits to make it odd and the correct size */
  169. if (!BN_priv_rand_ex(Xp1i, bitlen, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD,
  170. 0, ctx))
  171. goto err;
  172. }
  173. /* (Steps 4.1/5.1): Randomly generate Xp2 if it is not passed in */
  174. if (Xp2 == NULL) {
  175. /* Set the top and bottom bits to make it odd and the correct size */
  176. if (!BN_priv_rand_ex(Xp2i, bitlen, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD,
  177. 0, ctx))
  178. goto err;
  179. }
  180. /* (Steps 4.2/5.2) - find first auxiliary probable primes */
  181. if (!bn_rsa_fips186_4_find_aux_prob_prime(Xp1i, p1i, ctx, cb)
  182. || !bn_rsa_fips186_4_find_aux_prob_prime(Xp2i, p2i, ctx, cb))
  183. goto err;
  184. /* (Table B.1) auxiliary prime Max length check */
  185. if ((BN_num_bits(p1i) + BN_num_bits(p2i)) >=
  186. bn_rsa_fips186_5_aux_prime_max_sum_size_for_prob_primes(nlen))
  187. goto err;
  188. /* (Steps 4.3/5.3) - generate prime */
  189. if (!ossl_bn_rsa_fips186_4_derive_prime(p, Xpout, Xp, p1i, p2i, nlen, e,
  190. ctx, cb))
  191. goto err;
  192. ret = 1;
  193. err:
  194. /* Zeroize any internally generated values that are not returned */
  195. if (p1 == NULL)
  196. BN_clear(p1i);
  197. if (p2 == NULL)
  198. BN_clear(p2i);
  199. if (Xp1 == NULL)
  200. BN_clear(Xp1i);
  201. if (Xp2 == NULL)
  202. BN_clear(Xp2i);
  203. BN_CTX_end(ctx);
  204. return ret;
  205. }
  206. /*
  207. * Constructs a probable prime (a candidate for p or q) using 2 auxiliary
  208. * prime numbers and the Chinese Remainder Theorem.
  209. *
  210. * See FIPS 186-4 C.9 "Compute a Probable Prime Factor Based on Auxiliary
  211. * Primes". Used by FIPS 186-4 B.3.6 Section (4.3) for p and Section (5.3) for q.
  212. *
  213. * Params:
  214. * Y The returned prime factor (private_prime_factor) of the modulus n.
  215. * X The returned random number used during generation of the prime factor.
  216. * Xin An optional passed in value for X used for testing purposes.
  217. * r1 An auxiliary prime.
  218. * r2 An auxiliary prime.
  219. * nlen The desired length of n (the RSA modulus).
  220. * e The public exponent.
  221. * ctx A BN_CTX object.
  222. * cb An optional BIGNUM callback object.
  223. * Returns: 1 on success otherwise it returns 0.
  224. * Assumptions:
  225. * Y, X, r1, r2, e are not NULL.
  226. */
  227. int ossl_bn_rsa_fips186_4_derive_prime(BIGNUM *Y, BIGNUM *X, const BIGNUM *Xin,
  228. const BIGNUM *r1, const BIGNUM *r2,
  229. int nlen, const BIGNUM *e, BN_CTX *ctx,
  230. BN_GENCB *cb)
  231. {
  232. int ret = 0;
  233. int i, imax;
  234. int bits = nlen >> 1;
  235. BIGNUM *tmp, *R, *r1r2x2, *y1, *r1x2;
  236. BIGNUM *base, *range;
  237. BN_CTX_start(ctx);
  238. base = BN_CTX_get(ctx);
  239. range = BN_CTX_get(ctx);
  240. R = BN_CTX_get(ctx);
  241. tmp = BN_CTX_get(ctx);
  242. r1r2x2 = BN_CTX_get(ctx);
  243. y1 = BN_CTX_get(ctx);
  244. r1x2 = BN_CTX_get(ctx);
  245. if (r1x2 == NULL)
  246. goto err;
  247. if (Xin != NULL && BN_copy(X, Xin) == NULL)
  248. goto err;
  249. /*
  250. * We need to generate a random number X in the range
  251. * 1/sqrt(2) * 2^(nlen/2) <= X < 2^(nlen/2).
  252. * We can rewrite that as:
  253. * base = 1/sqrt(2) * 2^(nlen/2)
  254. * range = ((2^(nlen/2))) - (1/sqrt(2) * 2^(nlen/2))
  255. * X = base + random(range)
  256. * We only have the first 256 bit of 1/sqrt(2)
  257. */
  258. if (Xin == NULL) {
  259. if (bits < BN_num_bits(&ossl_bn_inv_sqrt_2))
  260. goto err;
  261. if (!BN_lshift(base, &ossl_bn_inv_sqrt_2,
  262. bits - BN_num_bits(&ossl_bn_inv_sqrt_2))
  263. || !BN_lshift(range, BN_value_one(), bits)
  264. || !BN_sub(range, range, base))
  265. goto err;
  266. }
  267. if (!(BN_lshift1(r1x2, r1)
  268. /* (Step 1) GCD(2r1, r2) = 1 */
  269. && BN_gcd(tmp, r1x2, r2, ctx)
  270. && BN_is_one(tmp)
  271. /* (Step 2) R = ((r2^-1 mod 2r1) * r2) - ((2r1^-1 mod r2)*2r1) */
  272. && BN_mod_inverse(R, r2, r1x2, ctx)
  273. && BN_mul(R, R, r2, ctx) /* R = (r2^-1 mod 2r1) * r2 */
  274. && BN_mod_inverse(tmp, r1x2, r2, ctx)
  275. && BN_mul(tmp, tmp, r1x2, ctx) /* tmp = (2r1^-1 mod r2)*2r1 */
  276. && BN_sub(R, R, tmp)
  277. /* Calculate 2r1r2 */
  278. && BN_mul(r1r2x2, r1x2, r2, ctx)))
  279. goto err;
  280. /* Make positive by adding the modulus */
  281. if (BN_is_negative(R) && !BN_add(R, R, r1r2x2))
  282. goto err;
  283. /*
  284. * In FIPS 186-4 imax was set to 5 * nlen/2.
  285. * Analysis by Allen Roginsky (See https://csrc.nist.gov/CSRC/media/Publications/fips/186/4/final/documents/comments-received-fips186-4-december-2015.pdf
  286. * page 68) indicates this has a 1 in 2 million chance of failure.
  287. * The number has been updated to 20 * nlen/2 as used in
  288. * FIPS186-5 Appendix B.9 Step 9.
  289. */
  290. imax = 20 * bits; /* max = 20/2 * nbits */
  291. for (;;) {
  292. if (Xin == NULL) {
  293. /*
  294. * (Step 3) Choose Random X such that
  295. * sqrt(2) * 2^(nlen/2-1) <= Random X <= (2^(nlen/2)) - 1.
  296. */
  297. if (!BN_priv_rand_range_ex(X, range, 0, ctx) || !BN_add(X, X, base))
  298. goto end;
  299. }
  300. /* (Step 4) Y = X + ((R - X) mod 2r1r2) */
  301. if (!BN_mod_sub(Y, R, X, r1r2x2, ctx) || !BN_add(Y, Y, X))
  302. goto err;
  303. /* (Step 5) */
  304. i = 0;
  305. for (;;) {
  306. /* (Step 6) */
  307. if (BN_num_bits(Y) > bits) {
  308. if (Xin == NULL)
  309. break; /* Randomly Generated X so Go back to Step 3 */
  310. else
  311. goto err; /* X is not random so it will always fail */
  312. }
  313. BN_GENCB_call(cb, 0, 2);
  314. /* (Step 7) If GCD(Y-1) == 1 & Y is probably prime then return Y */
  315. if (BN_copy(y1, Y) == NULL
  316. || !BN_sub_word(y1, 1)
  317. || !BN_gcd(tmp, y1, e, ctx))
  318. goto err;
  319. if (BN_is_one(tmp)) {
  320. int rv = BN_check_prime(Y, ctx, cb);
  321. if (rv > 0)
  322. goto end;
  323. if (rv < 0)
  324. goto err;
  325. }
  326. /* (Step 8-10) */
  327. if (++i >= imax) {
  328. ERR_raise(ERR_LIB_BN, BN_R_NO_PRIME_CANDIDATE);
  329. goto err;
  330. }
  331. if (!BN_add(Y, Y, r1r2x2))
  332. goto err;
  333. }
  334. }
  335. end:
  336. ret = 1;
  337. BN_GENCB_call(cb, 3, 0);
  338. err:
  339. BN_clear(y1);
  340. BN_CTX_end(ctx);
  341. return ret;
  342. }