rsa_internal.c 14 KB

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