rsa_sp800_56b_check.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 "internal/bn_int.h"
  13. #include "rsa_locl.h"
  14. /*
  15. * Part of the RSA keypair test.
  16. * Check the Chinese Remainder Theorem components are valid.
  17. *
  18. * See SP800-5bBr1
  19. * 6.4.1.2.3: rsakpv1-crt Step 7
  20. * 6.4.1.3.3: rsakpv2-crt Step 7
  21. */
  22. int rsa_check_crt_components(const RSA *rsa, BN_CTX *ctx)
  23. {
  24. int ret = 0;
  25. BIGNUM *r = NULL, *p1 = NULL, *q1 = NULL;
  26. /* check if only some of the crt components are set */
  27. if (rsa->dmp1 == NULL || rsa->dmq1 == NULL || rsa->iqmp == NULL) {
  28. if (rsa->dmp1 != NULL || rsa->dmq1 != NULL || rsa->iqmp != NULL)
  29. return 0;
  30. return 1; /* return ok if all components are NULL */
  31. }
  32. BN_CTX_start(ctx);
  33. r = BN_CTX_get(ctx);
  34. p1 = BN_CTX_get(ctx);
  35. q1 = BN_CTX_get(ctx);
  36. ret = (q1 != NULL)
  37. /* p1 = p -1 */
  38. && (BN_copy(p1, rsa->p) != NULL)
  39. && BN_sub_word(p1, 1)
  40. /* q1 = q - 1 */
  41. && (BN_copy(q1, rsa->q) != NULL)
  42. && BN_sub_word(q1, 1)
  43. /* (a) 1 < dP < (p – 1). */
  44. && (BN_cmp(rsa->dmp1, BN_value_one()) > 0)
  45. && (BN_cmp(rsa->dmp1, p1) < 0)
  46. /* (b) 1 < dQ < (q - 1). */
  47. && (BN_cmp(rsa->dmq1, BN_value_one()) > 0)
  48. && (BN_cmp(rsa->dmq1, q1) < 0)
  49. /* (c) 1 < qInv < p */
  50. && (BN_cmp(rsa->iqmp, BN_value_one()) > 0)
  51. && (BN_cmp(rsa->iqmp, rsa->p) < 0)
  52. /* (d) 1 = (dP . e) mod (p - 1)*/
  53. && BN_mod_mul(r, rsa->dmp1, rsa->e, p1, ctx)
  54. && BN_is_one(r)
  55. /* (e) 1 = (dQ . e) mod (q - 1) */
  56. && BN_mod_mul(r, rsa->dmq1, rsa->e, q1, ctx)
  57. && BN_is_one(r)
  58. /* (f) 1 = (qInv . q) mod p */
  59. && BN_mod_mul(r, rsa->iqmp, rsa->q, rsa->p, ctx)
  60. && BN_is_one(r);
  61. BN_clear(p1);
  62. BN_clear(q1);
  63. BN_CTX_end(ctx);
  64. return ret;
  65. }
  66. /*
  67. * Part of the RSA keypair test.
  68. * Check that (√2)(2^(nbits/2 - 1) <= p <= 2^(nbits/2) - 1
  69. *
  70. * See SP800-5bBr1 6.4.1.2.1 Part 5 (c) & (g) - used for both p and q.
  71. *
  72. * (√2)(2^(nbits/2 - 1) = (√2/2)(2^(nbits/2))
  73. * √2/2 = 0.707106781186547524400 = 0.B504F333F9DE6484597D8
  74. * 0.B504F334 gives an approximation to 11 decimal places.
  75. * The range is then from
  76. * 0xB504F334_0000.......................000 to
  77. * 0xFFFFFFFF_FFFF.......................FFF
  78. */
  79. int rsa_check_prime_factor_range(const BIGNUM *p, int nbits, BN_CTX *ctx)
  80. {
  81. int ret = 0;
  82. BIGNUM *tmp, *low;
  83. nbits >>= 1;
  84. /* Upper bound check */
  85. if (BN_num_bits(p) != nbits)
  86. return 0;
  87. BN_CTX_start(ctx);
  88. tmp = BN_CTX_get(ctx);
  89. low = BN_CTX_get(ctx);
  90. /* set low = (√2)(2^(nbits/2 - 1) */
  91. if (low == NULL || !BN_set_word(tmp, 0xB504F334))
  92. goto err;
  93. if (nbits >= 32) {
  94. if (!BN_lshift(low, tmp, nbits - 32))
  95. goto err;
  96. } else if (!BN_rshift(low, tmp, 32 - nbits)) {
  97. goto err;
  98. }
  99. if (BN_cmp(p, low) < 0)
  100. goto err;
  101. ret = 1;
  102. err:
  103. BN_CTX_end(ctx);
  104. return ret;
  105. }
  106. /*
  107. * Part of the RSA keypair test.
  108. * Check the prime factor (for either p or q)
  109. * i.e: p is prime AND GCD(p - 1, e) = 1
  110. *
  111. * See SP800-5bBr1 6.4.1.2.3 Step 5 (a to d) & (e to h).
  112. */
  113. int rsa_check_prime_factor(BIGNUM *p, BIGNUM *e, int nbits, BN_CTX *ctx)
  114. {
  115. int checks = bn_rsa_fips186_4_prime_MR_min_checks(nbits);
  116. int ret = 0;
  117. BIGNUM *p1 = NULL, *gcd = NULL;
  118. /* (Steps 5 a-b) prime test */
  119. if (BN_is_prime_fasttest_ex(p, checks, ctx, 1, NULL) != 1
  120. /* (Step 5c) (√2)(2^(nbits/2 - 1) <= p <= 2^(nbits/2 - 1) */
  121. || rsa_check_prime_factor_range(p, nbits, ctx) != 1)
  122. return 0;
  123. BN_CTX_start(ctx);
  124. p1 = BN_CTX_get(ctx);
  125. gcd = BN_CTX_get(ctx);
  126. ret = (gcd != NULL)
  127. /* (Step 5d) GCD(p-1, e) = 1 */
  128. && (BN_copy(p1, p) != NULL)
  129. && BN_sub_word(p1, 1)
  130. && BN_gcd(gcd, p1, e, ctx)
  131. && BN_is_one(gcd);
  132. BN_clear(p1);
  133. BN_CTX_end(ctx);
  134. return ret;
  135. }
  136. /*
  137. * See SP800-56Br1 6.4.1.2.3 Part 6(a-b) Check the private exponent d
  138. * satisfies:
  139. * (Step 6a) 2^(nBit/2) < d < LCM(p–1, q–1).
  140. * (Step 6b) 1 = (d*e) mod LCM(p–1, q–1)
  141. */
  142. int rsa_check_private_exponent(const RSA *rsa, int nbits, BN_CTX *ctx)
  143. {
  144. int ret;
  145. BIGNUM *r, *p1, *q1, *lcm, *p1q1, *gcd;
  146. /* (Step 6a) 2^(nbits/2) < d */
  147. if (BN_num_bits(rsa->d) <= (nbits >> 1))
  148. return 0;
  149. BN_CTX_start(ctx);
  150. r = BN_CTX_get(ctx);
  151. p1 = BN_CTX_get(ctx);
  152. q1 = BN_CTX_get(ctx);
  153. lcm = BN_CTX_get(ctx);
  154. p1q1 = BN_CTX_get(ctx);
  155. gcd = BN_CTX_get(ctx);
  156. ret = (gcd != NULL
  157. /* LCM(p - 1, q - 1) */
  158. && (rsa_get_lcm(ctx, rsa->p, rsa->q, lcm, gcd, p1, q1, p1q1) == 1)
  159. /* (Step 6a) d < LCM(p - 1, q - 1) */
  160. && (BN_cmp(rsa->d, lcm) < 0)
  161. /* (Step 6b) 1 = (e . d) mod LCM(p - 1, q - 1) */
  162. && BN_mod_mul(r, rsa->e, rsa->d, lcm, ctx)
  163. && BN_is_one(r));
  164. BN_clear(p1);
  165. BN_clear(q1);
  166. BN_clear(lcm);
  167. BN_clear(gcd);
  168. BN_CTX_end(ctx);
  169. return ret;
  170. }
  171. /* Check exponent is odd, and has a bitlen ranging from [17..256] */
  172. int rsa_check_public_exponent(const BIGNUM *e)
  173. {
  174. int bitlen = BN_num_bits(e);
  175. return (BN_is_odd(e) && bitlen > 16 && bitlen < 257);
  176. }
  177. /*
  178. * SP800-56Br1 6.4.1.2.1 (Step 5i): |p - q| > 2^(nbits/2 - 100)
  179. * i.e- numbits(p-q-1) > (nbits/2 -100)
  180. */
  181. int rsa_check_pminusq_diff(BIGNUM *diff, const BIGNUM *p, const BIGNUM *q,
  182. int nbits)
  183. {
  184. int bitlen = (nbits >> 1) - 100;
  185. if (!BN_sub(diff, p, q))
  186. return -1;
  187. BN_set_negative(diff, 0);
  188. if (BN_is_zero(diff))
  189. return 0;
  190. if (!BN_sub_word(diff, 1))
  191. return -1;
  192. return (BN_num_bits(diff) > bitlen);
  193. }
  194. /* return LCM(p-1, q-1) */
  195. int rsa_get_lcm(BN_CTX *ctx, const BIGNUM *p, const BIGNUM *q,
  196. BIGNUM *lcm, BIGNUM *gcd, BIGNUM *p1, BIGNUM *q1,
  197. BIGNUM *p1q1)
  198. {
  199. return BN_sub(p1, p, BN_value_one()) /* p-1 */
  200. && BN_sub(q1, q, BN_value_one()) /* q-1 */
  201. && BN_mul(p1q1, p1, q1, ctx) /* (p-1)(q-1) */
  202. && BN_gcd(gcd, p1, q1, ctx)
  203. && BN_div(lcm, NULL, p1q1, gcd, ctx); /* LCM((p-1, q-1)) */
  204. }
  205. /*
  206. * SP800-56Br1 6.4.2.2 Partial Public Key Validation for RSA refers to
  207. * SP800-89 5.3.3 (Explicit) Partial Public Key Validation for RSA
  208. * caveat is that the modulus must be as specified in SP800-56Br1
  209. */
  210. int rsa_sp800_56b_check_public(const RSA *rsa)
  211. {
  212. int ret = 0, nbits, iterations, status;
  213. BN_CTX *ctx = NULL;
  214. BIGNUM *gcd = NULL;
  215. if (rsa->n == NULL || rsa->e == NULL)
  216. return 0;
  217. /*
  218. * (Step a): modulus must be 2048 or 3072 (caveat from SP800-56Br1)
  219. * NOTE: changed to allow keys >= 2048
  220. */
  221. nbits = BN_num_bits(rsa->n);
  222. if (!rsa_sp800_56b_validate_strength(nbits, -1)) {
  223. RSAerr(RSA_F_RSA_SP800_56B_CHECK_PUBLIC, RSA_R_INVALID_KEY_LENGTH);
  224. return 0;
  225. }
  226. if (!BN_is_odd(rsa->n)) {
  227. RSAerr(RSA_F_RSA_SP800_56B_CHECK_PUBLIC, RSA_R_INVALID_MODULUS);
  228. return 0;
  229. }
  230. /* (Steps b-c): 2^16 < e < 2^256, n and e must be odd */
  231. if (!rsa_check_public_exponent(rsa->e)) {
  232. RSAerr(RSA_F_RSA_SP800_56B_CHECK_PUBLIC,
  233. RSA_R_PUB_EXPONENT_OUT_OF_RANGE);
  234. return 0;
  235. }
  236. ctx = BN_CTX_new();
  237. gcd = BN_new();
  238. if (ctx == NULL || gcd == NULL)
  239. goto err;
  240. iterations = bn_rsa_fips186_4_prime_MR_min_checks(nbits);
  241. /* (Steps d-f):
  242. * The modulus is composite, but not a power of a prime.
  243. * The modulus has no factors smaller than 752.
  244. */
  245. if (!BN_gcd(gcd, rsa->n, bn_get0_small_factors(), ctx) || !BN_is_one(gcd)) {
  246. RSAerr(RSA_F_RSA_SP800_56B_CHECK_PUBLIC, RSA_R_INVALID_MODULUS);
  247. goto err;
  248. }
  249. ret = bn_miller_rabin_is_prime(rsa->n, iterations, ctx, NULL, 1, &status);
  250. if (ret != 1 || status != BN_PRIMETEST_COMPOSITE_NOT_POWER_OF_PRIME) {
  251. RSAerr(RSA_F_RSA_SP800_56B_CHECK_PUBLIC, RSA_R_INVALID_MODULUS);
  252. ret = 0;
  253. goto err;
  254. }
  255. ret = 1;
  256. err:
  257. BN_free(gcd);
  258. BN_CTX_free(ctx);
  259. return ret;
  260. }
  261. /*
  262. * Perform validation of the RSA private key to check that 0 < D < N.
  263. */
  264. int rsa_sp800_56b_check_private(const RSA *rsa)
  265. {
  266. if (rsa->d == NULL || rsa->n == NULL)
  267. return 0;
  268. return BN_cmp(rsa->d, BN_value_one()) >= 0 && BN_cmp(rsa->d, rsa->n) < 0;
  269. }
  270. /*
  271. * RSA key pair validation.
  272. *
  273. * SP800-56Br1.
  274. * 6.4.1.2 "RSAKPV1 Family: RSA Key - Pair Validation with a Fixed Exponent"
  275. * 6.4.1.3 "RSAKPV2 Family: RSA Key - Pair Validation with a Random Exponent"
  276. *
  277. * It uses:
  278. * 6.4.1.2.3 "rsakpv1 - crt"
  279. * 6.4.1.3.3 "rsakpv2 - crt"
  280. */
  281. int rsa_sp800_56b_check_keypair(const RSA *rsa, const BIGNUM *efixed,
  282. int strength, int nbits)
  283. {
  284. int ret = 0;
  285. BN_CTX *ctx = NULL;
  286. BIGNUM *r = NULL;
  287. if (rsa->p == NULL
  288. || rsa->q == NULL
  289. || rsa->e == NULL
  290. || rsa->d == NULL
  291. || rsa->n == NULL) {
  292. RSAerr(RSA_F_RSA_SP800_56B_CHECK_KEYPAIR, RSA_R_INVALID_REQUEST);
  293. return 0;
  294. }
  295. /* (Step 1): Check Ranges */
  296. if (!rsa_sp800_56b_validate_strength(nbits, strength))
  297. return 0;
  298. /* If the exponent is known */
  299. if (efixed != NULL) {
  300. /* (2): Check fixed exponent matches public exponent. */
  301. if (BN_cmp(efixed, rsa->e) != 0) {
  302. RSAerr(RSA_F_RSA_SP800_56B_CHECK_KEYPAIR, RSA_R_INVALID_REQUEST);
  303. return 0;
  304. }
  305. }
  306. /* (Step 1.c): e is odd integer 65537 <= e < 2^256 */
  307. if (!rsa_check_public_exponent(rsa->e)) {
  308. /* exponent out of range */
  309. RSAerr(RSA_F_RSA_SP800_56B_CHECK_KEYPAIR,
  310. RSA_R_PUB_EXPONENT_OUT_OF_RANGE);
  311. return 0;
  312. }
  313. /* (Step 3.b): check the modulus */
  314. if (nbits != BN_num_bits(rsa->n)) {
  315. RSAerr(RSA_F_RSA_SP800_56B_CHECK_KEYPAIR, RSA_R_INVALID_KEYPAIR);
  316. return 0;
  317. }
  318. ctx = BN_CTX_new();
  319. if (ctx == NULL)
  320. return 0;
  321. BN_CTX_start(ctx);
  322. r = BN_CTX_get(ctx);
  323. if (r == NULL || !BN_mul(r, rsa->p, rsa->q, ctx))
  324. goto err;
  325. /* (Step 4.c): Check n = pq */
  326. if (BN_cmp(rsa->n, r) != 0) {
  327. RSAerr(RSA_F_RSA_SP800_56B_CHECK_KEYPAIR, RSA_R_INVALID_REQUEST);
  328. goto err;
  329. }
  330. /* (Step 5): check prime factors p & q */
  331. ret = rsa_check_prime_factor(rsa->p, rsa->e, nbits, ctx)
  332. && rsa_check_prime_factor(rsa->q, rsa->e, nbits, ctx)
  333. && (rsa_check_pminusq_diff(r, rsa->p, rsa->q, nbits) > 0)
  334. /* (Step 6): Check the private exponent d */
  335. && rsa_check_private_exponent(rsa, nbits, ctx)
  336. /* 6.4.1.2.3 (Step 7): Check the CRT components */
  337. && rsa_check_crt_components(rsa, ctx);
  338. if (ret != 1)
  339. RSAerr(RSA_F_RSA_SP800_56B_CHECK_KEYPAIR, RSA_R_INVALID_KEYPAIR);
  340. err:
  341. BN_clear(r);
  342. BN_CTX_end(ctx);
  343. BN_CTX_free(ctx);
  344. return ret;
  345. }