rsa_alt_helpers.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * Helper functions for the RSA module
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. *
  7. */
  8. #include "common.h"
  9. #if defined(MBEDTLS_RSA_C)
  10. #include "mbedtls/rsa.h"
  11. #include "mbedtls/bignum.h"
  12. #include "rsa_alt_helpers.h"
  13. /*
  14. * Compute RSA prime factors from public and private exponents
  15. *
  16. * Summary of algorithm:
  17. * Setting F := lcm(P-1,Q-1), the idea is as follows:
  18. *
  19. * (a) For any 1 <= X < N with gcd(X,N)=1, we have X^F = 1 modulo N, so X^(F/2)
  20. * is a square root of 1 in Z/NZ. Since Z/NZ ~= Z/PZ x Z/QZ by CRT and the
  21. * square roots of 1 in Z/PZ and Z/QZ are +1 and -1, this leaves the four
  22. * possibilities X^(F/2) = (+-1, +-1). If it happens that X^(F/2) = (-1,+1)
  23. * or (+1,-1), then gcd(X^(F/2) + 1, N) will be equal to one of the prime
  24. * factors of N.
  25. *
  26. * (b) If we don't know F/2 but (F/2) * K for some odd (!) K, then the same
  27. * construction still applies since (-)^K is the identity on the set of
  28. * roots of 1 in Z/NZ.
  29. *
  30. * The public and private key primitives (-)^E and (-)^D are mutually inverse
  31. * bijections on Z/NZ if and only if (-)^(DE) is the identity on Z/NZ, i.e.
  32. * if and only if DE - 1 is a multiple of F, say DE - 1 = F * L.
  33. * Splitting L = 2^t * K with K odd, we have
  34. *
  35. * DE - 1 = FL = (F/2) * (2^(t+1)) * K,
  36. *
  37. * so (F / 2) * K is among the numbers
  38. *
  39. * (DE - 1) >> 1, (DE - 1) >> 2, ..., (DE - 1) >> ord
  40. *
  41. * where ord is the order of 2 in (DE - 1).
  42. * We can therefore iterate through these numbers apply the construction
  43. * of (a) and (b) above to attempt to factor N.
  44. *
  45. */
  46. int mbedtls_rsa_deduce_primes(mbedtls_mpi const *N,
  47. mbedtls_mpi const *E, mbedtls_mpi const *D,
  48. mbedtls_mpi *P, mbedtls_mpi *Q)
  49. {
  50. int ret = 0;
  51. uint16_t attempt; /* Number of current attempt */
  52. uint16_t iter; /* Number of squares computed in the current attempt */
  53. uint16_t order; /* Order of 2 in DE - 1 */
  54. mbedtls_mpi T; /* Holds largest odd divisor of DE - 1 */
  55. mbedtls_mpi K; /* Temporary holding the current candidate */
  56. const unsigned char primes[] = { 2,
  57. 3, 5, 7, 11, 13, 17, 19, 23,
  58. 29, 31, 37, 41, 43, 47, 53, 59,
  59. 61, 67, 71, 73, 79, 83, 89, 97,
  60. 101, 103, 107, 109, 113, 127, 131, 137,
  61. 139, 149, 151, 157, 163, 167, 173, 179,
  62. 181, 191, 193, 197, 199, 211, 223, 227,
  63. 229, 233, 239, 241, 251 };
  64. const size_t num_primes = sizeof(primes) / sizeof(*primes);
  65. if (P == NULL || Q == NULL || P->p != NULL || Q->p != NULL) {
  66. return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  67. }
  68. if (mbedtls_mpi_cmp_int(N, 0) <= 0 ||
  69. mbedtls_mpi_cmp_int(D, 1) <= 0 ||
  70. mbedtls_mpi_cmp_mpi(D, N) >= 0 ||
  71. mbedtls_mpi_cmp_int(E, 1) <= 0 ||
  72. mbedtls_mpi_cmp_mpi(E, N) >= 0) {
  73. return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  74. }
  75. /*
  76. * Initializations and temporary changes
  77. */
  78. mbedtls_mpi_init(&K);
  79. mbedtls_mpi_init(&T);
  80. /* T := DE - 1 */
  81. MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, D, E));
  82. MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&T, &T, 1));
  83. if ((order = (uint16_t) mbedtls_mpi_lsb(&T)) == 0) {
  84. ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  85. goto cleanup;
  86. }
  87. /* After this operation, T holds the largest odd divisor of DE - 1. */
  88. MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&T, order));
  89. /*
  90. * Actual work
  91. */
  92. /* Skip trying 2 if N == 1 mod 8 */
  93. attempt = 0;
  94. if (N->p[0] % 8 == 1) {
  95. attempt = 1;
  96. }
  97. for (; attempt < num_primes; ++attempt) {
  98. MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&K, primes[attempt]));
  99. /* Check if gcd(K,N) = 1 */
  100. MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(P, &K, N));
  101. if (mbedtls_mpi_cmp_int(P, 1) != 0) {
  102. continue;
  103. }
  104. /* Go through K^T + 1, K^(2T) + 1, K^(4T) + 1, ...
  105. * and check whether they have nontrivial GCD with N. */
  106. MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&K, &K, &T, N,
  107. Q /* temporarily use Q for storing Montgomery
  108. * multiplication helper values */));
  109. for (iter = 1; iter <= order; ++iter) {
  110. /* If we reach 1 prematurely, there's no point
  111. * in continuing to square K */
  112. if (mbedtls_mpi_cmp_int(&K, 1) == 0) {
  113. break;
  114. }
  115. MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&K, &K, 1));
  116. MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(P, &K, N));
  117. if (mbedtls_mpi_cmp_int(P, 1) == 1 &&
  118. mbedtls_mpi_cmp_mpi(P, N) == -1) {
  119. /*
  120. * Have found a nontrivial divisor P of N.
  121. * Set Q := N / P.
  122. */
  123. MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(Q, NULL, N, P));
  124. goto cleanup;
  125. }
  126. MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, &K, 1));
  127. MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, &K, &K));
  128. MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&K, &K, N));
  129. }
  130. /*
  131. * If we get here, then either we prematurely aborted the loop because
  132. * we reached 1, or K holds primes[attempt]^(DE - 1) mod N, which must
  133. * be 1 if D,E,N were consistent.
  134. * Check if that's the case and abort if not, to avoid very long,
  135. * yet eventually failing, computations if N,D,E were not sane.
  136. */
  137. if (mbedtls_mpi_cmp_int(&K, 1) != 0) {
  138. break;
  139. }
  140. }
  141. ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  142. cleanup:
  143. mbedtls_mpi_free(&K);
  144. mbedtls_mpi_free(&T);
  145. return ret;
  146. }
  147. /*
  148. * Given P, Q and the public exponent E, deduce D.
  149. * This is essentially a modular inversion.
  150. */
  151. int mbedtls_rsa_deduce_private_exponent(mbedtls_mpi const *P,
  152. mbedtls_mpi const *Q,
  153. mbedtls_mpi const *E,
  154. mbedtls_mpi *D)
  155. {
  156. int ret = 0;
  157. mbedtls_mpi K, L;
  158. if (D == NULL || mbedtls_mpi_cmp_int(D, 0) != 0) {
  159. return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  160. }
  161. if (mbedtls_mpi_cmp_int(P, 1) <= 0 ||
  162. mbedtls_mpi_cmp_int(Q, 1) <= 0 ||
  163. mbedtls_mpi_cmp_int(E, 0) == 0) {
  164. return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  165. }
  166. mbedtls_mpi_init(&K);
  167. mbedtls_mpi_init(&L);
  168. /* Temporarily put K := P-1 and L := Q-1 */
  169. MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, P, 1));
  170. MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&L, Q, 1));
  171. /* Temporarily put D := gcd(P-1, Q-1) */
  172. MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(D, &K, &L));
  173. /* K := LCM(P-1, Q-1) */
  174. MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, &K, &L));
  175. MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&K, NULL, &K, D));
  176. /* Compute modular inverse of E in LCM(P-1, Q-1) */
  177. MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(D, E, &K));
  178. cleanup:
  179. mbedtls_mpi_free(&K);
  180. mbedtls_mpi_free(&L);
  181. return ret;
  182. }
  183. int mbedtls_rsa_deduce_crt(const mbedtls_mpi *P, const mbedtls_mpi *Q,
  184. const mbedtls_mpi *D, mbedtls_mpi *DP,
  185. mbedtls_mpi *DQ, mbedtls_mpi *QP)
  186. {
  187. int ret = 0;
  188. mbedtls_mpi K;
  189. mbedtls_mpi_init(&K);
  190. /* DP = D mod P-1 */
  191. if (DP != NULL) {
  192. MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, P, 1));
  193. MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(DP, D, &K));
  194. }
  195. /* DQ = D mod Q-1 */
  196. if (DQ != NULL) {
  197. MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, Q, 1));
  198. MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(DQ, D, &K));
  199. }
  200. /* QP = Q^{-1} mod P */
  201. if (QP != NULL) {
  202. MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(QP, Q, P));
  203. }
  204. cleanup:
  205. mbedtls_mpi_free(&K);
  206. return ret;
  207. }
  208. /*
  209. * Check that core RSA parameters are sane.
  210. */
  211. int mbedtls_rsa_validate_params(const mbedtls_mpi *N, const mbedtls_mpi *P,
  212. const mbedtls_mpi *Q, const mbedtls_mpi *D,
  213. const mbedtls_mpi *E,
  214. int (*f_rng)(void *, unsigned char *, size_t),
  215. void *p_rng)
  216. {
  217. int ret = 0;
  218. mbedtls_mpi K, L;
  219. mbedtls_mpi_init(&K);
  220. mbedtls_mpi_init(&L);
  221. /*
  222. * Step 1: If PRNG provided, check that P and Q are prime
  223. */
  224. #if defined(MBEDTLS_GENPRIME)
  225. /*
  226. * When generating keys, the strongest security we support aims for an error
  227. * rate of at most 2^-100 and we are aiming for the same certainty here as
  228. * well.
  229. */
  230. if (f_rng != NULL && P != NULL &&
  231. (ret = mbedtls_mpi_is_prime_ext(P, 50, f_rng, p_rng)) != 0) {
  232. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  233. goto cleanup;
  234. }
  235. if (f_rng != NULL && Q != NULL &&
  236. (ret = mbedtls_mpi_is_prime_ext(Q, 50, f_rng, p_rng)) != 0) {
  237. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  238. goto cleanup;
  239. }
  240. #else
  241. ((void) f_rng);
  242. ((void) p_rng);
  243. #endif /* MBEDTLS_GENPRIME */
  244. /*
  245. * Step 2: Check that 1 < N = P * Q
  246. */
  247. if (P != NULL && Q != NULL && N != NULL) {
  248. MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, P, Q));
  249. if (mbedtls_mpi_cmp_int(N, 1) <= 0 ||
  250. mbedtls_mpi_cmp_mpi(&K, N) != 0) {
  251. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  252. goto cleanup;
  253. }
  254. }
  255. /*
  256. * Step 3: Check and 1 < D, E < N if present.
  257. */
  258. if (N != NULL && D != NULL && E != NULL) {
  259. if (mbedtls_mpi_cmp_int(D, 1) <= 0 ||
  260. mbedtls_mpi_cmp_int(E, 1) <= 0 ||
  261. mbedtls_mpi_cmp_mpi(D, N) >= 0 ||
  262. mbedtls_mpi_cmp_mpi(E, N) >= 0) {
  263. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  264. goto cleanup;
  265. }
  266. }
  267. /*
  268. * Step 4: Check that D, E are inverse modulo P-1 and Q-1
  269. */
  270. if (P != NULL && Q != NULL && D != NULL && E != NULL) {
  271. if (mbedtls_mpi_cmp_int(P, 1) <= 0 ||
  272. mbedtls_mpi_cmp_int(Q, 1) <= 0) {
  273. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  274. goto cleanup;
  275. }
  276. /* Compute DE-1 mod P-1 */
  277. MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, D, E));
  278. MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, &K, 1));
  279. MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&L, P, 1));
  280. MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&K, &K, &L));
  281. if (mbedtls_mpi_cmp_int(&K, 0) != 0) {
  282. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  283. goto cleanup;
  284. }
  285. /* Compute DE-1 mod Q-1 */
  286. MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, D, E));
  287. MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, &K, 1));
  288. MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&L, Q, 1));
  289. MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&K, &K, &L));
  290. if (mbedtls_mpi_cmp_int(&K, 0) != 0) {
  291. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  292. goto cleanup;
  293. }
  294. }
  295. cleanup:
  296. mbedtls_mpi_free(&K);
  297. mbedtls_mpi_free(&L);
  298. /* Wrap MPI error codes by RSA check failure error code */
  299. if (ret != 0 && ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED) {
  300. ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  301. }
  302. return ret;
  303. }
  304. /*
  305. * Check that RSA CRT parameters are in accordance with core parameters.
  306. */
  307. int mbedtls_rsa_validate_crt(const mbedtls_mpi *P, const mbedtls_mpi *Q,
  308. const mbedtls_mpi *D, const mbedtls_mpi *DP,
  309. const mbedtls_mpi *DQ, const mbedtls_mpi *QP)
  310. {
  311. int ret = 0;
  312. mbedtls_mpi K, L;
  313. mbedtls_mpi_init(&K);
  314. mbedtls_mpi_init(&L);
  315. /* Check that DP - D == 0 mod P - 1 */
  316. if (DP != NULL) {
  317. if (P == NULL) {
  318. ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
  319. goto cleanup;
  320. }
  321. MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, P, 1));
  322. MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&L, DP, D));
  323. MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&L, &L, &K));
  324. if (mbedtls_mpi_cmp_int(&L, 0) != 0) {
  325. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  326. goto cleanup;
  327. }
  328. }
  329. /* Check that DQ - D == 0 mod Q - 1 */
  330. if (DQ != NULL) {
  331. if (Q == NULL) {
  332. ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
  333. goto cleanup;
  334. }
  335. MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, Q, 1));
  336. MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&L, DQ, D));
  337. MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&L, &L, &K));
  338. if (mbedtls_mpi_cmp_int(&L, 0) != 0) {
  339. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  340. goto cleanup;
  341. }
  342. }
  343. /* Check that QP * Q - 1 == 0 mod P */
  344. if (QP != NULL) {
  345. if (P == NULL || Q == NULL) {
  346. ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
  347. goto cleanup;
  348. }
  349. MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, QP, Q));
  350. MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, &K, 1));
  351. MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&K, &K, P));
  352. if (mbedtls_mpi_cmp_int(&K, 0) != 0) {
  353. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  354. goto cleanup;
  355. }
  356. }
  357. cleanup:
  358. /* Wrap MPI error codes by RSA check failure error code */
  359. if (ret != 0 &&
  360. ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED &&
  361. ret != MBEDTLS_ERR_RSA_BAD_INPUT_DATA) {
  362. ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  363. }
  364. mbedtls_mpi_free(&K);
  365. mbedtls_mpi_free(&L);
  366. return ret;
  367. }
  368. #endif /* MBEDTLS_RSA_C */