rsa_sp800_56b_check.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * Copyright 2018-2024 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 "crypto/bn.h"
  13. #include "rsa_local.h"
  14. #include "../bn/bn_local.h" // WINSCP
  15. /*
  16. * Part of the RSA keypair test.
  17. * Check the Chinese Remainder Theorem components are valid.
  18. *
  19. * See SP800-5bBr1
  20. * 6.4.1.2.3: rsakpv1-crt Step 7
  21. * 6.4.1.3.3: rsakpv2-crt Step 7
  22. */
  23. int ossl_rsa_check_crt_components(const RSA *rsa, BN_CTX *ctx)
  24. {
  25. int ret = 0;
  26. BIGNUM *r = NULL, *p1 = NULL, *q1 = NULL;
  27. /* check if only some of the crt components are set */
  28. if (rsa->dmp1 == NULL || rsa->dmq1 == NULL || rsa->iqmp == NULL) {
  29. if (rsa->dmp1 != NULL || rsa->dmq1 != NULL || rsa->iqmp != NULL)
  30. return 0;
  31. return 1; /* return ok if all components are NULL */
  32. }
  33. BN_CTX_start(ctx);
  34. r = BN_CTX_get(ctx);
  35. p1 = BN_CTX_get(ctx);
  36. q1 = BN_CTX_get(ctx);
  37. if (q1 != NULL) {
  38. BN_set_flags(r, BN_FLG_CONSTTIME);
  39. BN_set_flags(p1, BN_FLG_CONSTTIME);
  40. BN_set_flags(q1, BN_FLG_CONSTTIME);
  41. ret = 1;
  42. } else {
  43. ret = 0;
  44. }
  45. ret = ret
  46. /* p1 = p -1 */
  47. && (BN_copy(p1, rsa->p) != NULL)
  48. && BN_sub_word(p1, 1)
  49. /* q1 = q - 1 */
  50. && (BN_copy(q1, rsa->q) != NULL)
  51. && BN_sub_word(q1, 1)
  52. /* (a) 1 < dP < (p – 1). */
  53. && (BN_cmp(rsa->dmp1, BN_value_one()) > 0)
  54. && (BN_cmp(rsa->dmp1, p1) < 0)
  55. /* (b) 1 < dQ < (q - 1). */
  56. && (BN_cmp(rsa->dmq1, BN_value_one()) > 0)
  57. && (BN_cmp(rsa->dmq1, q1) < 0)
  58. /* (c) 1 < qInv < p */
  59. && (BN_cmp(rsa->iqmp, BN_value_one()) > 0)
  60. && (BN_cmp(rsa->iqmp, rsa->p) < 0)
  61. /* (d) 1 = (dP . e) mod (p - 1)*/
  62. && BN_mod_mul(r, rsa->dmp1, rsa->e, p1, ctx)
  63. && BN_is_one(r)
  64. /* (e) 1 = (dQ . e) mod (q - 1) */
  65. && BN_mod_mul(r, rsa->dmq1, rsa->e, q1, ctx)
  66. && BN_is_one(r)
  67. /* (f) 1 = (qInv . q) mod p */
  68. && BN_mod_mul(r, rsa->iqmp, rsa->q, rsa->p, ctx)
  69. && BN_is_one(r);
  70. BN_clear(r);
  71. BN_clear(p1);
  72. BN_clear(q1);
  73. BN_CTX_end(ctx);
  74. return ret;
  75. }
  76. /*
  77. * Part of the RSA keypair test.
  78. * Check that (√2)(2^(nbits/2 - 1) <= p <= 2^(nbits/2) - 1
  79. *
  80. * See SP800-5bBr1 6.4.1.2.1 Part 5 (c) & (g) - used for both p and q.
  81. *
  82. * (√2)(2^(nbits/2 - 1) = (√2/2)(2^(nbits/2))
  83. */
  84. int ossl_rsa_check_prime_factor_range(const BIGNUM *p, int nbits, BN_CTX *ctx)
  85. {
  86. int ret = 0;
  87. BIGNUM *low;
  88. int shift;
  89. nbits >>= 1;
  90. shift = nbits - BN_num_bits(&ossl_bn_inv_sqrt_2);
  91. /* Upper bound check */
  92. if (BN_num_bits(p) != nbits)
  93. return 0;
  94. BN_CTX_start(ctx);
  95. low = BN_CTX_get(ctx);
  96. if (low == NULL)
  97. goto err;
  98. /* set low = (√2)(2^(nbits/2 - 1) */
  99. if (!BN_copy(low, &ossl_bn_inv_sqrt_2))
  100. goto err;
  101. if (shift >= 0) {
  102. /*
  103. * We don't have all the bits. ossl_bn_inv_sqrt_2 contains a rounded up
  104. * value, so there is a very low probability that we'll reject a valid
  105. * value.
  106. */
  107. if (!BN_lshift(low, low, shift))
  108. goto err;
  109. } else if (!BN_rshift(low, low, -shift)) {
  110. goto err;
  111. }
  112. if (BN_cmp(p, low) <= 0)
  113. goto err;
  114. ret = 1;
  115. err:
  116. BN_CTX_end(ctx);
  117. return ret;
  118. }
  119. /*
  120. * Part of the RSA keypair test.
  121. * Check the prime factor (for either p or q)
  122. * i.e: p is prime AND GCD(p - 1, e) = 1
  123. *
  124. * See SP800-56Br1 6.4.1.2.3 Step 5 (a to d) & (e to h).
  125. */
  126. int ossl_rsa_check_prime_factor(BIGNUM *p, BIGNUM *e, int nbits, BN_CTX *ctx)
  127. {
  128. int ret = 0;
  129. BIGNUM *p1 = NULL, *gcd = NULL;
  130. /* (Steps 5 a-b) prime test */
  131. if (BN_check_prime(p, ctx, NULL) != 1
  132. /* (Step 5c) (√2)(2^(nbits/2 - 1) <= p <= 2^(nbits/2 - 1) */
  133. || ossl_rsa_check_prime_factor_range(p, nbits, ctx) != 1)
  134. return 0;
  135. BN_CTX_start(ctx);
  136. p1 = BN_CTX_get(ctx);
  137. gcd = BN_CTX_get(ctx);
  138. if (gcd != NULL) {
  139. BN_set_flags(p1, BN_FLG_CONSTTIME);
  140. BN_set_flags(gcd, BN_FLG_CONSTTIME);
  141. ret = 1;
  142. } else {
  143. ret = 0;
  144. }
  145. ret = ret
  146. /* (Step 5d) GCD(p-1, e) = 1 */
  147. && (BN_copy(p1, p) != NULL)
  148. && BN_sub_word(p1, 1)
  149. && BN_gcd(gcd, p1, e, ctx)
  150. && BN_is_one(gcd);
  151. BN_clear(p1);
  152. BN_CTX_end(ctx);
  153. return ret;
  154. }
  155. /*
  156. * See SP800-56Br1 6.4.1.2.3 Part 6(a-b) Check the private exponent d
  157. * satisfies:
  158. * (Step 6a) 2^(nBit/2) < d < LCM(p–1, q–1).
  159. * (Step 6b) 1 = (d*e) mod LCM(p–1, q–1)
  160. */
  161. int ossl_rsa_check_private_exponent(const RSA *rsa, int nbits, BN_CTX *ctx)
  162. {
  163. int ret;
  164. BIGNUM *r, *p1, *q1, *lcm, *p1q1, *gcd;
  165. /* (Step 6a) 2^(nbits/2) < d */
  166. if (BN_num_bits(rsa->d) <= (nbits >> 1))
  167. return 0;
  168. BN_CTX_start(ctx);
  169. r = BN_CTX_get(ctx);
  170. p1 = BN_CTX_get(ctx);
  171. q1 = BN_CTX_get(ctx);
  172. lcm = BN_CTX_get(ctx);
  173. p1q1 = BN_CTX_get(ctx);
  174. gcd = BN_CTX_get(ctx);
  175. if (gcd != NULL) {
  176. BN_set_flags(r, BN_FLG_CONSTTIME);
  177. BN_set_flags(p1, BN_FLG_CONSTTIME);
  178. BN_set_flags(q1, BN_FLG_CONSTTIME);
  179. BN_set_flags(lcm, BN_FLG_CONSTTIME);
  180. BN_set_flags(p1q1, BN_FLG_CONSTTIME);
  181. BN_set_flags(gcd, BN_FLG_CONSTTIME);
  182. ret = 1;
  183. } else {
  184. ret = 0;
  185. }
  186. ret = (ret
  187. /* LCM(p - 1, q - 1) */
  188. && (ossl_rsa_get_lcm(ctx, rsa->p, rsa->q, lcm, gcd, p1, q1,
  189. p1q1) == 1)
  190. /* (Step 6a) d < LCM(p - 1, q - 1) */
  191. && (BN_cmp(rsa->d, lcm) < 0)
  192. /* (Step 6b) 1 = (e . d) mod LCM(p - 1, q - 1) */
  193. && BN_mod_mul(r, rsa->e, rsa->d, lcm, ctx)
  194. && BN_is_one(r));
  195. BN_clear(r);
  196. BN_clear(p1);
  197. BN_clear(q1);
  198. BN_clear(lcm);
  199. BN_clear(gcd);
  200. BN_CTX_end(ctx);
  201. return ret;
  202. }
  203. /*
  204. * Check exponent is odd.
  205. * For FIPS also check the bit length is in the range [17..256]
  206. */
  207. int ossl_rsa_check_public_exponent(const BIGNUM *e)
  208. {
  209. #ifdef FIPS_MODULE
  210. int bitlen;
  211. bitlen = BN_num_bits(e);
  212. return (BN_is_odd(e) && bitlen > 16 && bitlen < 257);
  213. #else
  214. /* Allow small exponents larger than 1 for legacy purposes */
  215. return BN_is_odd(e) && BN_cmp(e, BN_value_one()) > 0;
  216. #endif /* FIPS_MODULE */
  217. }
  218. /*
  219. * SP800-56Br1 6.4.1.2.1 (Step 5i): |p - q| > 2^(nbits/2 - 100)
  220. * i.e- numbits(p-q-1) > (nbits/2 -100)
  221. */
  222. int ossl_rsa_check_pminusq_diff(BIGNUM *diff, const BIGNUM *p, const BIGNUM *q,
  223. int nbits)
  224. {
  225. int bitlen = (nbits >> 1) - 100;
  226. if (!BN_sub(diff, p, q))
  227. return -1;
  228. BN_set_negative(diff, 0);
  229. if (BN_is_zero(diff))
  230. return 0;
  231. if (!BN_sub_word(diff, 1))
  232. return -1;
  233. return (BN_num_bits(diff) > bitlen);
  234. }
  235. /*
  236. * return LCM(p-1, q-1)
  237. *
  238. * Caller should ensure that lcm, gcd, p1, q1, p1q1 are flagged with
  239. * BN_FLG_CONSTTIME.
  240. */
  241. int ossl_rsa_get_lcm(BN_CTX *ctx, const BIGNUM *p, const BIGNUM *q,
  242. BIGNUM *lcm, BIGNUM *gcd, BIGNUM *p1, BIGNUM *q1,
  243. BIGNUM *p1q1)
  244. {
  245. return BN_sub(p1, p, BN_value_one()) /* p-1 */
  246. && BN_sub(q1, q, BN_value_one()) /* q-1 */
  247. && BN_mul(p1q1, p1, q1, ctx) /* (p-1)(q-1) */
  248. && BN_gcd(gcd, p1, q1, ctx)
  249. && BN_div(lcm, NULL, p1q1, gcd, ctx); /* LCM((p-1, q-1)) */
  250. }
  251. /*
  252. * SP800-56Br1 6.4.2.2 Partial Public Key Validation for RSA refers to
  253. * SP800-89 5.3.3 (Explicit) Partial Public Key Validation for RSA
  254. * caveat is that the modulus must be as specified in SP800-56Br1
  255. */
  256. int ossl_rsa_sp800_56b_check_public(const RSA *rsa)
  257. {
  258. int ret = 0, status;
  259. int nbits;
  260. BN_CTX *ctx = NULL;
  261. BIGNUM *gcd = NULL;
  262. if (rsa->n == NULL || rsa->e == NULL)
  263. return 0;
  264. nbits = BN_num_bits(rsa->n);
  265. if (nbits > OPENSSL_RSA_MAX_MODULUS_BITS) {
  266. ERR_raise(ERR_LIB_RSA, RSA_R_MODULUS_TOO_LARGE);
  267. return 0;
  268. }
  269. #ifdef FIPS_MODULE
  270. /*
  271. * (Step a): modulus must be 2048 or 3072 (caveat from SP800-56Br1)
  272. * NOTE: changed to allow keys >= 2048
  273. */
  274. if (!ossl_rsa_sp800_56b_validate_strength(nbits, -1)) {
  275. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_KEY_LENGTH);
  276. return 0;
  277. }
  278. #endif
  279. if (!BN_is_odd(rsa->n)) {
  280. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_MODULUS);
  281. return 0;
  282. }
  283. /* (Steps b-c): 2^16 < e < 2^256, n and e must be odd */
  284. if (!ossl_rsa_check_public_exponent(rsa->e)) {
  285. ERR_raise(ERR_LIB_RSA, RSA_R_PUB_EXPONENT_OUT_OF_RANGE);
  286. return 0;
  287. }
  288. ctx = BN_CTX_new_ex(rsa->libctx);
  289. gcd = BN_new();
  290. if (ctx == NULL || gcd == NULL)
  291. goto err;
  292. /* (Steps d-f):
  293. * The modulus is composite, but not a power of a prime.
  294. * The modulus has no factors smaller than 752.
  295. */
  296. if (!BN_gcd(gcd, rsa->n, ossl_bn_get0_small_factors(), ctx)
  297. || !BN_is_one(gcd)) {
  298. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_MODULUS);
  299. goto err;
  300. }
  301. /* Highest number of MR rounds from FIPS 186-5 Section B.3 Table B.1 */
  302. ret = ossl_bn_miller_rabin_is_prime(rsa->n, 5, ctx, NULL, 1, &status);
  303. #ifdef FIPS_MODULE
  304. if (ret != 1 || status != BN_PRIMETEST_COMPOSITE_NOT_POWER_OF_PRIME) {
  305. #else
  306. if (ret != 1 || (status != BN_PRIMETEST_COMPOSITE_NOT_POWER_OF_PRIME
  307. && (nbits >= RSA_MIN_MODULUS_BITS
  308. || status != BN_PRIMETEST_COMPOSITE_WITH_FACTOR))) {
  309. #endif
  310. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_MODULUS);
  311. ret = 0;
  312. goto err;
  313. }
  314. ret = 1;
  315. err:
  316. BN_free(gcd);
  317. BN_CTX_free(ctx);
  318. return ret;
  319. }
  320. /*
  321. * Perform validation of the RSA private key to check that 0 < D < N.
  322. */
  323. int ossl_rsa_sp800_56b_check_private(const RSA *rsa)
  324. {
  325. if (rsa->d == NULL || rsa->n == NULL)
  326. return 0;
  327. return BN_cmp(rsa->d, BN_value_one()) >= 0 && BN_cmp(rsa->d, rsa->n) < 0;
  328. }
  329. /*
  330. * RSA key pair validation.
  331. *
  332. * SP800-56Br1.
  333. * 6.4.1.2 "RSAKPV1 Family: RSA Key - Pair Validation with a Fixed Exponent"
  334. * 6.4.1.3 "RSAKPV2 Family: RSA Key - Pair Validation with a Random Exponent"
  335. *
  336. * It uses:
  337. * 6.4.1.2.3 "rsakpv1 - crt"
  338. * 6.4.1.3.3 "rsakpv2 - crt"
  339. */
  340. int ossl_rsa_sp800_56b_check_keypair(const RSA *rsa, const BIGNUM *efixed,
  341. int strength, int nbits)
  342. {
  343. int ret = 0;
  344. BN_CTX *ctx = NULL;
  345. BIGNUM *r = NULL;
  346. if (rsa->p == NULL
  347. || rsa->q == NULL
  348. || rsa->e == NULL
  349. || rsa->d == NULL
  350. || rsa->n == NULL) {
  351. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_REQUEST);
  352. return 0;
  353. }
  354. /* (Step 1): Check Ranges */
  355. if (!ossl_rsa_sp800_56b_validate_strength(nbits, strength))
  356. return 0;
  357. /* If the exponent is known */
  358. if (efixed != NULL) {
  359. /* (2): Check fixed exponent matches public exponent. */
  360. if (BN_cmp(efixed, rsa->e) != 0) {
  361. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_REQUEST);
  362. return 0;
  363. }
  364. }
  365. /* (Step 1.c): e is odd integer 65537 <= e < 2^256 */
  366. if (!ossl_rsa_check_public_exponent(rsa->e)) {
  367. /* exponent out of range */
  368. ERR_raise(ERR_LIB_RSA, RSA_R_PUB_EXPONENT_OUT_OF_RANGE);
  369. return 0;
  370. }
  371. /* (Step 3.b): check the modulus */
  372. if (nbits != BN_num_bits(rsa->n)) {
  373. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_KEYPAIR);
  374. return 0;
  375. }
  376. /* (Step 3.c): check that the modulus length is a positive even integer */
  377. if (nbits <= 0 || (nbits & 0x1)) {
  378. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_KEYPAIR);
  379. return 0;
  380. }
  381. ctx = BN_CTX_new_ex(rsa->libctx);
  382. if (ctx == NULL)
  383. return 0;
  384. BN_CTX_start(ctx);
  385. r = BN_CTX_get(ctx);
  386. if (r == NULL || !BN_mul(r, rsa->p, rsa->q, ctx))
  387. goto err;
  388. /* (Step 4.c): Check n = pq */
  389. if (BN_cmp(rsa->n, r) != 0) {
  390. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_REQUEST);
  391. goto err;
  392. }
  393. /* (Step 5): check prime factors p & q */
  394. ret = ossl_rsa_check_prime_factor(rsa->p, rsa->e, nbits, ctx)
  395. && ossl_rsa_check_prime_factor(rsa->q, rsa->e, nbits, ctx)
  396. && (ossl_rsa_check_pminusq_diff(r, rsa->p, rsa->q, nbits) > 0)
  397. /* (Step 6): Check the private exponent d */
  398. && ossl_rsa_check_private_exponent(rsa, nbits, ctx)
  399. /* 6.4.1.2.3 (Step 7): Check the CRT components */
  400. && ossl_rsa_check_crt_components(rsa, ctx);
  401. if (ret != 1)
  402. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_KEYPAIR);
  403. err:
  404. BN_clear(r);
  405. BN_CTX_end(ctx);
  406. BN_CTX_free(ctx);
  407. return ret;
  408. }