bn_rsa_fips186_4.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. /*
  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. * Also see FIPS1402IG A.14
  18. * FIPS 186-4 relies on the use of the auxiliary primes p1, p2, q1 and q2 that
  19. * must be generated before the module generates the RSA primes p and q.
  20. * Table B.1 in FIPS 186-4 specifies, for RSA modulus lengths of 2048 and
  21. * 3072 bits only, the min/max total length of the auxiliary primes.
  22. * When implementing the RSA signature generation algorithm
  23. * with other approved RSA modulus sizes, the vendor shall use the limitations
  24. * from Table B.1 that apply to the longest RSA modulus shown in Table B.1 of
  25. * FIPS 186-4 whose length does not exceed that of the implementation's RSA
  26. * modulus. In particular, when generating the primes for the 4096-bit RSA
  27. * modulus the limitations stated for the 3072-bit modulus shall apply.
  28. */
  29. #include <stdio.h>
  30. #include <openssl/bn.h>
  31. #include "bn_lcl.h"
  32. #include "internal/bn_int.h"
  33. /*
  34. * FIPS 186-4 Table B.1. "Min length of auxiliary primes p1, p2, q1, q2".
  35. *
  36. * Params:
  37. * nbits The key size in bits.
  38. * Returns:
  39. * The minimum size of the auxiliary primes or 0 if nbits is invalid.
  40. */
  41. static int bn_rsa_fips186_4_aux_prime_min_size(int nbits)
  42. {
  43. if (nbits >= 3072)
  44. return 171;
  45. if (nbits == 2048)
  46. return 141;
  47. return 0;
  48. }
  49. /*
  50. * FIPS 186-4 Table B.1 "Maximum length of len(p1) + len(p2) and
  51. * len(q1) + len(q2) for p,q Probable Primes".
  52. *
  53. * Params:
  54. * nbits The key size in bits.
  55. * Returns:
  56. * The maximum length or 0 if nbits is invalid.
  57. */
  58. static int bn_rsa_fips186_4_aux_prime_max_sum_size_for_prob_primes(int nbits)
  59. {
  60. if (nbits >= 3072)
  61. return 1518;
  62. if (nbits == 2048)
  63. return 1007;
  64. return 0;
  65. }
  66. /*
  67. * FIPS 186-4 Table C.3 for error probability of 2^-100
  68. * Minimum number of Miller Rabin Rounds for p1, p2, q1 & q2.
  69. *
  70. * Params:
  71. * aux_prime_bits The auxiliary prime size in bits.
  72. * Returns:
  73. * The minimum number of Miller Rabin Rounds for an auxiliary prime, or
  74. * 0 if aux_prime_bits is invalid.
  75. */
  76. static int bn_rsa_fips186_4_aux_prime_MR_min_checks(int aux_prime_bits)
  77. {
  78. if (aux_prime_bits > 170)
  79. return 27;
  80. if (aux_prime_bits > 140)
  81. return 32;
  82. return 0; /* Error case */
  83. }
  84. /*
  85. * FIPS 186-4 Table C.3 for error probability of 2^-100
  86. * Minimum number of Miller Rabin Rounds for p, q.
  87. *
  88. * Params:
  89. * nbits The key size in bits.
  90. * Returns:
  91. * The minimum number of Miller Rabin Rounds required,
  92. * or 0 if nbits is invalid.
  93. */
  94. int bn_rsa_fips186_4_prime_MR_min_checks(int nbits)
  95. {
  96. if (nbits >= 3072) /* > 170 */
  97. return 3;
  98. if (nbits == 2048) /* > 140 */
  99. return 4;
  100. return 0; /* Error case */
  101. }
  102. /*
  103. * Find the first odd integer that is a probable prime.
  104. *
  105. * See section FIPS 186-4 B.3.6 (Steps 4.2/5.2).
  106. *
  107. * Params:
  108. * Xp1 The passed in starting point to find a probably prime.
  109. * p1 The returned probable prime (first odd integer >= Xp1)
  110. * ctx A BN_CTX object.
  111. * cb An optional BIGNUM callback.
  112. * Returns: 1 on success otherwise it returns 0.
  113. */
  114. static int bn_rsa_fips186_4_find_aux_prob_prime(const BIGNUM *Xp1,
  115. BIGNUM *p1, BN_CTX *ctx,
  116. BN_GENCB *cb)
  117. {
  118. int ret = 0;
  119. int i = 0;
  120. int checks = bn_rsa_fips186_4_aux_prime_MR_min_checks(BN_num_bits(Xp1));
  121. if (checks == 0 || BN_copy(p1, Xp1) == NULL)
  122. return 0;
  123. /* Find the first odd number >= Xp1 that is probably prime */
  124. for(;;) {
  125. i++;
  126. BN_GENCB_call(cb, 0, i);
  127. /* MR test with trial division */
  128. if (BN_is_prime_fasttest_ex(p1, checks, ctx, 1, cb))
  129. break;
  130. /* Get next odd number */
  131. if (!BN_add_word(p1, 2))
  132. goto err;
  133. }
  134. BN_GENCB_call(cb, 2, i);
  135. ret = 1;
  136. err:
  137. return ret;
  138. }
  139. /*
  140. * Generate a probable prime (p or q).
  141. *
  142. * See FIPS 186-4 B.3.6 (Steps 4 & 5)
  143. *
  144. * Params:
  145. * p The returned probable prime.
  146. * Xpout An optionally returned random number used during generation of p.
  147. * p1, p2 The returned auxiliary primes. If NULL they are not returned.
  148. * Xp An optional passed in value (that is random number used during
  149. * generation of p).
  150. * Xp1, Xp2 Optional passed in values that are normally generated
  151. * internally. Used to find p1, p2.
  152. * nlen The bit length of the modulus (the key size).
  153. * e The public exponent.
  154. * ctx A BN_CTX object.
  155. * cb An optional BIGNUM callback.
  156. * Returns: 1 on success otherwise it returns 0.
  157. */
  158. int bn_rsa_fips186_4_gen_prob_primes(BIGNUM *p, BIGNUM *Xpout,
  159. BIGNUM *p1, BIGNUM *p2,
  160. const BIGNUM *Xp, const BIGNUM *Xp1,
  161. const BIGNUM *Xp2, int nlen,
  162. const BIGNUM *e, BN_CTX *ctx, BN_GENCB *cb)
  163. {
  164. int ret = 0;
  165. BIGNUM *p1i = NULL, *p2i = NULL, *Xp1i = NULL, *Xp2i = NULL;
  166. int bitlen;
  167. if (p == NULL || Xpout == NULL)
  168. return 0;
  169. BN_CTX_start(ctx);
  170. p1i = (p1 != NULL) ? p1 : BN_CTX_get(ctx);
  171. p2i = (p2 != NULL) ? p2 : BN_CTX_get(ctx);
  172. Xp1i = (Xp1 != NULL) ? (BIGNUM *)Xp1 : BN_CTX_get(ctx);
  173. Xp2i = (Xp2 != NULL) ? (BIGNUM *)Xp2 : BN_CTX_get(ctx);
  174. if (p1i == NULL || p2i == NULL || Xp1i == NULL || Xp2i == NULL)
  175. goto err;
  176. bitlen = bn_rsa_fips186_4_aux_prime_min_size(nlen);
  177. if (bitlen == 0)
  178. goto err;
  179. /* (Steps 4.1/5.1): Randomly generate Xp1 if it is not passed in */
  180. if (Xp1 == NULL) {
  181. /* Set the top and bottom bits to make it odd and the correct size */
  182. if (!BN_priv_rand(Xp1i, bitlen, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))
  183. goto err;
  184. }
  185. /* (Steps 4.1/5.1): Randomly generate Xp2 if it is not passed in */
  186. if (Xp2 == NULL) {
  187. /* Set the top and bottom bits to make it odd and the correct size */
  188. if (!BN_priv_rand(Xp2i, bitlen, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))
  189. goto err;
  190. }
  191. /* (Steps 4.2/5.2) - find first auxiliary probable primes */
  192. if (!bn_rsa_fips186_4_find_aux_prob_prime(Xp1i, p1i, ctx, cb)
  193. || !bn_rsa_fips186_4_find_aux_prob_prime(Xp2i, p2i, ctx, cb))
  194. goto err;
  195. /* (Table B.1) auxiliary prime Max length check */
  196. if ((BN_num_bits(p1i) + BN_num_bits(p2i)) >=
  197. bn_rsa_fips186_4_aux_prime_max_sum_size_for_prob_primes(nlen))
  198. goto err;
  199. /* (Steps 4.3/5.3) - generate prime */
  200. if (!bn_rsa_fips186_4_derive_prime(p, Xpout, Xp, p1i, p2i, nlen, e, ctx, cb))
  201. goto err;
  202. ret = 1;
  203. err:
  204. /* Zeroize any internally generated values that are not returned */
  205. if (p1 == NULL)
  206. BN_clear(p1i);
  207. if (p2 == NULL)
  208. BN_clear(p2i);
  209. if (Xp1 == NULL)
  210. BN_clear(Xp1i);
  211. if (Xp2 == NULL)
  212. BN_clear(Xp2i);
  213. BN_CTX_end(ctx);
  214. return ret;
  215. }
  216. /*
  217. * Constructs a probable prime (a candidate for p or q) using 2 auxiliary
  218. * prime numbers and the Chinese Remainder Theorem.
  219. *
  220. * See FIPS 186-4 C.9 "Compute a Probable Prime Factor Based on Auxiliary
  221. * Primes". Used by FIPS 186-4 B.3.6 Section (4.3) for p and Section (5.3) for q.
  222. *
  223. * Params:
  224. * Y The returned prime factor (private_prime_factor) of the modulus n.
  225. * X The returned random number used during generation of the prime factor.
  226. * Xin An optional passed in value for X used for testing purposes.
  227. * r1 An auxiliary prime.
  228. * r2 An auxiliary prime.
  229. * nlen The desired length of n (the RSA modulus).
  230. * e The public exponent.
  231. * ctx A BN_CTX object.
  232. * cb An optional BIGNUM callback object.
  233. * Returns: 1 on success otherwise it returns 0.
  234. * Assumptions:
  235. * Y, X, r1, r2, e are not NULL.
  236. */
  237. int bn_rsa_fips186_4_derive_prime(BIGNUM *Y, BIGNUM *X, const BIGNUM *Xin,
  238. const BIGNUM *r1, const BIGNUM *r2, int nlen,
  239. const BIGNUM *e, BN_CTX *ctx, BN_GENCB *cb)
  240. {
  241. int ret = 0;
  242. int i, imax;
  243. int bits = nlen >> 1;
  244. int checks = bn_rsa_fips186_4_prime_MR_min_checks(nlen);
  245. BIGNUM *tmp, *R, *r1r2x2, *y1, *r1x2;
  246. if (checks == 0)
  247. return 0;
  248. BN_CTX_start(ctx);
  249. R = BN_CTX_get(ctx);
  250. tmp = BN_CTX_get(ctx);
  251. r1r2x2 = BN_CTX_get(ctx);
  252. y1 = BN_CTX_get(ctx);
  253. r1x2 = BN_CTX_get(ctx);
  254. if (r1x2 == NULL)
  255. goto err;
  256. if (Xin != NULL && BN_copy(X, Xin) == NULL)
  257. goto err;
  258. if (!(BN_lshift1(r1x2, r1)
  259. /* (Step 1) GCD(2r1, r2) = 1 */
  260. && BN_gcd(tmp, r1x2, r2, ctx)
  261. && BN_is_one(tmp)
  262. /* (Step 2) R = ((r2^-1 mod 2r1) * r2) - ((2r1^-1 mod r2)*2r1) */
  263. && BN_mod_inverse(R, r2, r1x2, ctx)
  264. && BN_mul(R, R, r2, ctx) /* R = (r2^-1 mod 2r1) * r2 */
  265. && BN_mod_inverse(tmp, r1x2, r2, ctx)
  266. && BN_mul(tmp, tmp, r1x2, ctx) /* tmp = (2r1^-1 mod r2)*2r1 */
  267. && BN_sub(R, R, tmp)
  268. /* Calculate 2r1r2 */
  269. && BN_mul(r1r2x2, r1x2, r2, ctx)))
  270. goto err;
  271. /* Make positive by adding the modulus */
  272. if (BN_is_negative(R) && !BN_add(R, R, r1r2x2))
  273. goto err;
  274. imax = 5 * bits; /* max = 5/2 * nbits */
  275. for (;;) {
  276. if (Xin == NULL) {
  277. /*
  278. * (Step 3) Choose Random X such that
  279. * sqrt(2) * 2^(nlen/2-1) < Random X < (2^(nlen/2)) - 1.
  280. *
  281. * For the lower bound:
  282. * sqrt(2) * 2^(nlen/2 - 1) == sqrt(2)/2 * 2^(nlen/2)
  283. * where sqrt(2)/2 = 0.70710678.. = 0.B504FC33F9DE...
  284. * so largest number will have B5... as the top byte
  285. * Setting the top 2 bits gives 0xC0.
  286. */
  287. if (!BN_priv_rand(X, bits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ANY))
  288. goto end;
  289. }
  290. /* (Step 4) Y = X + ((R - X) mod 2r1r2) */
  291. if (!BN_mod_sub(Y, R, X, r1r2x2, ctx) || !BN_add(Y, Y, X))
  292. goto err;
  293. /* (Step 5) */
  294. i = 0;
  295. for (;;) {
  296. /* (Step 6) */
  297. if (BN_num_bits(Y) > bits) {
  298. if (Xin == NULL)
  299. break; /* Randomly Generated X so Go back to Step 3 */
  300. else
  301. goto err; /* X is not random so it will always fail */
  302. }
  303. BN_GENCB_call(cb, 0, 2);
  304. /* (Step 7) If GCD(Y-1) == 1 & Y is probably prime then return Y */
  305. if (BN_copy(y1, Y) == NULL
  306. || !BN_sub_word(y1, 1)
  307. || !BN_gcd(tmp, y1, e, ctx))
  308. goto err;
  309. if (BN_is_one(tmp)
  310. && BN_is_prime_fasttest_ex(Y, checks, ctx, 1, cb))
  311. goto end;
  312. /* (Step 8-10) */
  313. if (++i >= imax || !BN_add(Y, Y, r1r2x2))
  314. goto err;
  315. }
  316. }
  317. end:
  318. ret = 1;
  319. BN_GENCB_call(cb, 3, 0);
  320. err:
  321. BN_clear(y1);
  322. BN_CTX_end(ctx);
  323. return ret;
  324. }