rsa_internal.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. if( f_rng != NULL && P != NULL &&
  296. ( ret = mbedtls_mpi_is_prime( P, f_rng, p_rng ) ) != 0 )
  297. {
  298. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  299. goto cleanup;
  300. }
  301. if( f_rng != NULL && Q != NULL &&
  302. ( ret = mbedtls_mpi_is_prime( Q, f_rng, p_rng ) ) != 0 )
  303. {
  304. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  305. goto cleanup;
  306. }
  307. #else
  308. ((void) f_rng);
  309. ((void) p_rng);
  310. #endif /* MBEDTLS_GENPRIME */
  311. /*
  312. * Step 2: Check that 1 < N = P * Q
  313. */
  314. if( P != NULL && Q != NULL && N != NULL )
  315. {
  316. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, P, Q ) );
  317. if( mbedtls_mpi_cmp_int( N, 1 ) <= 0 ||
  318. mbedtls_mpi_cmp_mpi( &K, N ) != 0 )
  319. {
  320. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  321. goto cleanup;
  322. }
  323. }
  324. /*
  325. * Step 3: Check and 1 < D, E < N if present.
  326. */
  327. if( N != NULL && D != NULL && E != NULL )
  328. {
  329. if ( mbedtls_mpi_cmp_int( D, 1 ) <= 0 ||
  330. mbedtls_mpi_cmp_int( E, 1 ) <= 0 ||
  331. mbedtls_mpi_cmp_mpi( D, N ) >= 0 ||
  332. mbedtls_mpi_cmp_mpi( E, N ) >= 0 )
  333. {
  334. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  335. goto cleanup;
  336. }
  337. }
  338. /*
  339. * Step 4: Check that D, E are inverse modulo P-1 and Q-1
  340. */
  341. if( P != NULL && Q != NULL && D != NULL && E != NULL )
  342. {
  343. if( mbedtls_mpi_cmp_int( P, 1 ) <= 0 ||
  344. mbedtls_mpi_cmp_int( Q, 1 ) <= 0 )
  345. {
  346. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  347. goto cleanup;
  348. }
  349. /* Compute DE-1 mod P-1 */
  350. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, D, E ) );
  351. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
  352. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, P, 1 ) );
  353. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, &L ) );
  354. if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
  355. {
  356. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  357. goto cleanup;
  358. }
  359. /* Compute DE-1 mod Q-1 */
  360. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, D, E ) );
  361. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
  362. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, Q, 1 ) );
  363. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, &L ) );
  364. if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
  365. {
  366. ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  367. goto cleanup;
  368. }
  369. }
  370. cleanup:
  371. mbedtls_mpi_free( &K );
  372. mbedtls_mpi_free( &L );
  373. /* Wrap MPI error codes by RSA check failure error code */
  374. if( ret != 0 && ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
  375. {
  376. ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  377. }
  378. return( ret );
  379. }
  380. int mbedtls_rsa_deduce_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
  381. const mbedtls_mpi *D, mbedtls_mpi *DP,
  382. mbedtls_mpi *DQ, mbedtls_mpi *QP )
  383. {
  384. int ret = 0;
  385. mbedtls_mpi K;
  386. mbedtls_mpi_init( &K );
  387. /* DP = D mod P-1 */
  388. if( DP != NULL )
  389. {
  390. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
  391. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DP, D, &K ) );
  392. }
  393. /* DQ = D mod Q-1 */
  394. if( DQ != NULL )
  395. {
  396. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, Q, 1 ) );
  397. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DQ, D, &K ) );
  398. }
  399. /* QP = Q^{-1} mod P */
  400. if( QP != NULL )
  401. {
  402. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( QP, Q, P ) );
  403. }
  404. cleanup:
  405. mbedtls_mpi_free( &K );
  406. return( ret );
  407. }
  408. #endif /* MBEDTLS_RSA_C */