rsa_internal.c 15 KB

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