rsa_internal.c 14 KB

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