rsa_internal.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. };
  77. const size_t num_primes = sizeof( primes ) / sizeof( *primes );
  78. if( P == NULL || Q == NULL || P->p != NULL || Q->p != NULL )
  79. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  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. {
  86. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  87. }
  88. /*
  89. * Initializations and temporary changes
  90. */
  91. mbedtls_mpi_init( &K );
  92. mbedtls_mpi_init( &T );
  93. /* T := DE - 1 */
  94. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, D, E ) );
  95. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &T, &T, 1 ) );
  96. if( ( order = (uint16_t) mbedtls_mpi_lsb( &T ) ) == 0 )
  97. {
  98. ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  99. goto cleanup;
  100. }
  101. /* After this operation, T holds the largest odd divisor of DE - 1. */
  102. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &T, order ) );
  103. /*
  104. * Actual work
  105. */
  106. /* Skip trying 2 if N == 1 mod 8 */
  107. attempt = 0;
  108. if( N->p[0] % 8 == 1 )
  109. attempt = 1;
  110. for( ; attempt < num_primes; ++attempt )
  111. {
  112. mbedtls_mpi_lset( &K, primes[attempt] );
  113. /* Check if gcd(K,N) = 1 */
  114. MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( P, &K, N ) );
  115. if( mbedtls_mpi_cmp_int( P, 1 ) != 0 )
  116. continue;
  117. /* Go through K^T + 1, K^(2T) + 1, K^(4T) + 1, ...
  118. * and check whether they have nontrivial GCD with N. */
  119. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &K, &K, &T, N,
  120. Q /* temporarily use Q for storing Montgomery
  121. * multiplication helper values */ ) );
  122. for( iter = 1; iter <= order; ++iter )
  123. {
  124. /* If we reach 1 prematurely, there's no point
  125. * in continuing to square K */
  126. if( mbedtls_mpi_cmp_int( &K, 1 ) == 0 )
  127. break;
  128. MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &K, &K, 1 ) );
  129. MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( P, &K, N ) );
  130. if( mbedtls_mpi_cmp_int( P, 1 ) == 1 &&
  131. mbedtls_mpi_cmp_mpi( P, N ) == -1 )
  132. {
  133. /*
  134. * Have found a nontrivial divisor P of N.
  135. * Set Q := N / P.
  136. */
  137. MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( Q, NULL, N, P ) );
  138. goto cleanup;
  139. }
  140. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
  141. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, &K, &K ) );
  142. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, N ) );
  143. }
  144. /*
  145. * If we get here, then either we prematurely aborted the loop because
  146. * we reached 1, or K holds primes[attempt]^(DE - 1) mod N, which must
  147. * be 1 if D,E,N were consistent.
  148. * Check if that's the case and abort if not, to avoid very long,
  149. * yet eventually failing, computations if N,D,E were not sane.
  150. */
  151. if( mbedtls_mpi_cmp_int( &K, 1 ) != 0 )
  152. {
  153. break;
  154. }
  155. }
  156. ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  157. cleanup:
  158. mbedtls_mpi_free( &K );
  159. mbedtls_mpi_free( &T );
  160. return( ret );
  161. }
  162. /*
  163. * Given P, Q and the public exponent E, deduce D.
  164. * This is essentially a modular inversion.
  165. */
  166. int mbedtls_rsa_deduce_private_exponent( mbedtls_mpi const *P,
  167. mbedtls_mpi const *Q,
  168. mbedtls_mpi const *E,
  169. mbedtls_mpi *D )
  170. {
  171. int ret = 0;
  172. mbedtls_mpi K, L;
  173. if( D == NULL || mbedtls_mpi_cmp_int( D, 0 ) != 0 )
  174. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  175. if( mbedtls_mpi_cmp_int( P, 1 ) <= 0 ||
  176. mbedtls_mpi_cmp_int( Q, 1 ) <= 0 ||
  177. mbedtls_mpi_cmp_int( E, 0 ) == 0 )
  178. {
  179. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  180. }
  181. mbedtls_mpi_init( &K );
  182. mbedtls_mpi_init( &L );
  183. /* Temporarily put K := P-1 and L := Q-1 */
  184. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
  185. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, Q, 1 ) );
  186. /* Temporarily put D := gcd(P-1, Q-1) */
  187. MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( D, &K, &L ) );
  188. /* K := LCM(P-1, Q-1) */
  189. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, &K, &L ) );
  190. MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &K, NULL, &K, D ) );
  191. /* Compute modular inverse of E in LCM(P-1, Q-1) */
  192. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( D, E, &K ) );
  193. cleanup:
  194. mbedtls_mpi_free( &K );
  195. mbedtls_mpi_free( &L );
  196. return( ret );
  197. }
  198. /*
  199. * Check that RSA CRT parameters are in accordance with core parameters.
  200. */
  201. int mbedtls_rsa_validate_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
  202. const mbedtls_mpi *D, const mbedtls_mpi *DP,
  203. const mbedtls_mpi *DQ, const mbedtls_mpi *QP )
  204. {
  205. int ret = 0;
  206. mbedtls_mpi K, L;
  207. mbedtls_mpi_init( &K );
  208. mbedtls_mpi_init( &L );
  209. /* Check that DP - D == 0 mod P - 1 */
  210. if( DP != NULL )
  211. {
  212. if( P == NULL )
  213. {
  214. ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
  215. goto cleanup;
  216. }
  217. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
  218. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &L, DP, D ) );
  219. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &L, &L, &K ) );
  220. if( mbedtls_mpi_cmp_int( &L, 0 ) != 0 )
  221. {
  222. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  223. goto cleanup;
  224. }
  225. }
  226. /* Check that DQ - D == 0 mod Q - 1 */
  227. if( DQ != NULL )
  228. {
  229. if( Q == NULL )
  230. {
  231. ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
  232. goto cleanup;
  233. }
  234. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, Q, 1 ) );
  235. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &L, DQ, D ) );
  236. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &L, &L, &K ) );
  237. if( mbedtls_mpi_cmp_int( &L, 0 ) != 0 )
  238. {
  239. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  240. goto cleanup;
  241. }
  242. }
  243. /* Check that QP * Q - 1 == 0 mod P */
  244. if( QP != NULL )
  245. {
  246. if( P == NULL || Q == NULL )
  247. {
  248. ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
  249. goto cleanup;
  250. }
  251. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, QP, Q ) );
  252. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
  253. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, P ) );
  254. if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
  255. {
  256. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  257. goto cleanup;
  258. }
  259. }
  260. cleanup:
  261. /* Wrap MPI error codes by RSA check failure error code */
  262. if( ret != 0 &&
  263. ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED &&
  264. ret != MBEDTLS_ERR_RSA_BAD_INPUT_DATA )
  265. {
  266. ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  267. }
  268. mbedtls_mpi_free( &K );
  269. mbedtls_mpi_free( &L );
  270. return( ret );
  271. }
  272. /*
  273. * Check that core RSA parameters are sane.
  274. */
  275. int mbedtls_rsa_validate_params( const mbedtls_mpi *N, const mbedtls_mpi *P,
  276. const mbedtls_mpi *Q, const mbedtls_mpi *D,
  277. const mbedtls_mpi *E,
  278. int (*f_rng)(void *, unsigned char *, size_t),
  279. void *p_rng )
  280. {
  281. int ret = 0;
  282. mbedtls_mpi K, L;
  283. mbedtls_mpi_init( &K );
  284. mbedtls_mpi_init( &L );
  285. /*
  286. * Step 1: If PRNG provided, check that P and Q are prime
  287. */
  288. #if defined(MBEDTLS_GENPRIME)
  289. /*
  290. * When generating keys, the strongest security we support aims for an error
  291. * rate of at most 2^-100 and we are aiming for the same certainty here as
  292. * well.
  293. */
  294. if( f_rng != NULL && P != NULL &&
  295. ( ret = mbedtls_mpi_is_prime_ext( P, 50, f_rng, p_rng ) ) != 0 )
  296. {
  297. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  298. goto cleanup;
  299. }
  300. if( f_rng != NULL && Q != NULL &&
  301. ( ret = mbedtls_mpi_is_prime_ext( Q, 50, f_rng, p_rng ) ) != 0 )
  302. {
  303. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  304. goto cleanup;
  305. }
  306. #else
  307. ((void) f_rng);
  308. ((void) p_rng);
  309. #endif /* MBEDTLS_GENPRIME */
  310. /*
  311. * Step 2: Check that 1 < N = P * Q
  312. */
  313. if( P != NULL && Q != NULL && N != NULL )
  314. {
  315. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, P, Q ) );
  316. if( mbedtls_mpi_cmp_int( N, 1 ) <= 0 ||
  317. mbedtls_mpi_cmp_mpi( &K, N ) != 0 )
  318. {
  319. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  320. goto cleanup;
  321. }
  322. }
  323. /*
  324. * Step 3: Check and 1 < D, E < N if present.
  325. */
  326. if( N != NULL && D != NULL && E != NULL )
  327. {
  328. if ( mbedtls_mpi_cmp_int( D, 1 ) <= 0 ||
  329. mbedtls_mpi_cmp_int( E, 1 ) <= 0 ||
  330. mbedtls_mpi_cmp_mpi( D, N ) >= 0 ||
  331. mbedtls_mpi_cmp_mpi( E, N ) >= 0 )
  332. {
  333. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  334. goto cleanup;
  335. }
  336. }
  337. /*
  338. * Step 4: Check that D, E are inverse modulo P-1 and Q-1
  339. */
  340. if( P != NULL && Q != NULL && D != NULL && E != NULL )
  341. {
  342. if( mbedtls_mpi_cmp_int( P, 1 ) <= 0 ||
  343. mbedtls_mpi_cmp_int( Q, 1 ) <= 0 )
  344. {
  345. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  346. goto cleanup;
  347. }
  348. /* Compute DE-1 mod P-1 */
  349. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, D, E ) );
  350. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
  351. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, P, 1 ) );
  352. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, &L ) );
  353. if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
  354. {
  355. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  356. goto cleanup;
  357. }
  358. /* Compute DE-1 mod Q-1 */
  359. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, D, E ) );
  360. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
  361. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, Q, 1 ) );
  362. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, &L ) );
  363. if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
  364. {
  365. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  366. goto cleanup;
  367. }
  368. }
  369. cleanup:
  370. mbedtls_mpi_free( &K );
  371. mbedtls_mpi_free( &L );
  372. /* Wrap MPI error codes by RSA check failure error code */
  373. if( ret != 0 && ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
  374. {
  375. ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  376. }
  377. return( ret );
  378. }
  379. int mbedtls_rsa_deduce_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
  380. const mbedtls_mpi *D, mbedtls_mpi *DP,
  381. mbedtls_mpi *DQ, mbedtls_mpi *QP )
  382. {
  383. int ret = 0;
  384. mbedtls_mpi K;
  385. mbedtls_mpi_init( &K );
  386. /* DP = D mod P-1 */
  387. if( DP != NULL )
  388. {
  389. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
  390. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DP, D, &K ) );
  391. }
  392. /* DQ = D mod Q-1 */
  393. if( DQ != NULL )
  394. {
  395. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, Q, 1 ) );
  396. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DQ, D, &K ) );
  397. }
  398. /* QP = Q^{-1} mod P */
  399. if( QP != NULL )
  400. {
  401. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( QP, Q, P ) );
  402. }
  403. cleanup:
  404. mbedtls_mpi_free( &K );
  405. return( ret );
  406. }
  407. #endif /* MBEDTLS_RSA_C */