rsa_alt_helpers.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 "rsa_alt_helpers.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. int mbedtls_rsa_deduce_crt(const mbedtls_mpi *P, const mbedtls_mpi *Q,
  196. const mbedtls_mpi *D, mbedtls_mpi *DP,
  197. mbedtls_mpi *DQ, mbedtls_mpi *QP)
  198. {
  199. int ret = 0;
  200. mbedtls_mpi K;
  201. mbedtls_mpi_init(&K);
  202. /* DP = D mod P-1 */
  203. if (DP != NULL) {
  204. MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, P, 1));
  205. MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(DP, D, &K));
  206. }
  207. /* DQ = D mod Q-1 */
  208. if (DQ != NULL) {
  209. MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, Q, 1));
  210. MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(DQ, D, &K));
  211. }
  212. /* QP = Q^{-1} mod P */
  213. if (QP != NULL) {
  214. MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(QP, Q, P));
  215. }
  216. cleanup:
  217. mbedtls_mpi_free(&K);
  218. return ret;
  219. }
  220. /*
  221. * Check that core RSA parameters are sane.
  222. */
  223. int mbedtls_rsa_validate_params(const mbedtls_mpi *N, const mbedtls_mpi *P,
  224. const mbedtls_mpi *Q, const mbedtls_mpi *D,
  225. const mbedtls_mpi *E,
  226. int (*f_rng)(void *, unsigned char *, size_t),
  227. void *p_rng)
  228. {
  229. int ret = 0;
  230. mbedtls_mpi K, L;
  231. mbedtls_mpi_init(&K);
  232. mbedtls_mpi_init(&L);
  233. /*
  234. * Step 1: If PRNG provided, check that P and Q are prime
  235. */
  236. #if defined(MBEDTLS_GENPRIME)
  237. /*
  238. * When generating keys, the strongest security we support aims for an error
  239. * rate of at most 2^-100 and we are aiming for the same certainty here as
  240. * well.
  241. */
  242. if (f_rng != NULL && P != NULL &&
  243. (ret = mbedtls_mpi_is_prime_ext(P, 50, f_rng, p_rng)) != 0) {
  244. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  245. goto cleanup;
  246. }
  247. if (f_rng != NULL && Q != NULL &&
  248. (ret = mbedtls_mpi_is_prime_ext(Q, 50, f_rng, p_rng)) != 0) {
  249. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  250. goto cleanup;
  251. }
  252. #else
  253. ((void) f_rng);
  254. ((void) p_rng);
  255. #endif /* MBEDTLS_GENPRIME */
  256. /*
  257. * Step 2: Check that 1 < N = P * Q
  258. */
  259. if (P != NULL && Q != NULL && N != NULL) {
  260. MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, P, Q));
  261. if (mbedtls_mpi_cmp_int(N, 1) <= 0 ||
  262. mbedtls_mpi_cmp_mpi(&K, N) != 0) {
  263. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  264. goto cleanup;
  265. }
  266. }
  267. /*
  268. * Step 3: Check and 1 < D, E < N if present.
  269. */
  270. if (N != NULL && D != NULL && E != NULL) {
  271. if (mbedtls_mpi_cmp_int(D, 1) <= 0 ||
  272. mbedtls_mpi_cmp_int(E, 1) <= 0 ||
  273. mbedtls_mpi_cmp_mpi(D, N) >= 0 ||
  274. mbedtls_mpi_cmp_mpi(E, N) >= 0) {
  275. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  276. goto cleanup;
  277. }
  278. }
  279. /*
  280. * Step 4: Check that D, E are inverse modulo P-1 and Q-1
  281. */
  282. if (P != NULL && Q != NULL && D != NULL && E != NULL) {
  283. if (mbedtls_mpi_cmp_int(P, 1) <= 0 ||
  284. mbedtls_mpi_cmp_int(Q, 1) <= 0) {
  285. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  286. goto cleanup;
  287. }
  288. /* Compute DE-1 mod P-1 */
  289. MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, D, E));
  290. MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, &K, 1));
  291. MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&L, P, 1));
  292. MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&K, &K, &L));
  293. if (mbedtls_mpi_cmp_int(&K, 0) != 0) {
  294. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  295. goto cleanup;
  296. }
  297. /* Compute DE-1 mod Q-1 */
  298. MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, D, E));
  299. MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, &K, 1));
  300. MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&L, Q, 1));
  301. MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&K, &K, &L));
  302. if (mbedtls_mpi_cmp_int(&K, 0) != 0) {
  303. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  304. goto cleanup;
  305. }
  306. }
  307. cleanup:
  308. mbedtls_mpi_free(&K);
  309. mbedtls_mpi_free(&L);
  310. /* Wrap MPI error codes by RSA check failure error code */
  311. if (ret != 0 && ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED) {
  312. ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  313. }
  314. return ret;
  315. }
  316. /*
  317. * Check that RSA CRT parameters are in accordance with core parameters.
  318. */
  319. int mbedtls_rsa_validate_crt(const mbedtls_mpi *P, const mbedtls_mpi *Q,
  320. const mbedtls_mpi *D, const mbedtls_mpi *DP,
  321. const mbedtls_mpi *DQ, const mbedtls_mpi *QP)
  322. {
  323. int ret = 0;
  324. mbedtls_mpi K, L;
  325. mbedtls_mpi_init(&K);
  326. mbedtls_mpi_init(&L);
  327. /* Check that DP - D == 0 mod P - 1 */
  328. if (DP != NULL) {
  329. if (P == NULL) {
  330. ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
  331. goto cleanup;
  332. }
  333. MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, P, 1));
  334. MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&L, DP, D));
  335. MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&L, &L, &K));
  336. if (mbedtls_mpi_cmp_int(&L, 0) != 0) {
  337. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  338. goto cleanup;
  339. }
  340. }
  341. /* Check that DQ - D == 0 mod Q - 1 */
  342. if (DQ != NULL) {
  343. if (Q == NULL) {
  344. ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
  345. goto cleanup;
  346. }
  347. MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, Q, 1));
  348. MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&L, DQ, D));
  349. MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&L, &L, &K));
  350. if (mbedtls_mpi_cmp_int(&L, 0) != 0) {
  351. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  352. goto cleanup;
  353. }
  354. }
  355. /* Check that QP * Q - 1 == 0 mod P */
  356. if (QP != NULL) {
  357. if (P == NULL || Q == NULL) {
  358. ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
  359. goto cleanup;
  360. }
  361. MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, QP, Q));
  362. MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, &K, 1));
  363. MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&K, &K, P));
  364. if (mbedtls_mpi_cmp_int(&K, 0) != 0) {
  365. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  366. goto cleanup;
  367. }
  368. }
  369. cleanup:
  370. /* Wrap MPI error codes by RSA check failure error code */
  371. if (ret != 0 &&
  372. ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED &&
  373. ret != MBEDTLS_ERR_RSA_BAD_INPUT_DATA) {
  374. ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  375. }
  376. mbedtls_mpi_free(&K);
  377. mbedtls_mpi_free(&L);
  378. return ret;
  379. }
  380. #endif /* MBEDTLS_RSA_C */