blob: d6ba97a14baf7f4046f9164a5dddb4ea3d8a26a7 [file] [log] [blame]
Hanno Beckera565f542017-10-11 11:00:19 +01001/*
2 * Helper functions for the RSA module
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Hanno Beckera565f542017-10-11 11:00:19 +01005 * 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 *
Hanno Beckera565f542017-10-11 11:00:19 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Hanno Beckera565f542017-10-11 11:00:19 +010022
23#if defined(MBEDTLS_RSA_C)
24
25#include "mbedtls/rsa.h"
26#include "mbedtls/bignum.h"
27#include "mbedtls/rsa_internal.h"
28
29/*
30 * Compute RSA prime factors from public and private exponents
31 *
32 * Summary of algorithm:
33 * Setting F := lcm(P-1,Q-1), the idea is as follows:
34 *
35 * (a) For any 1 <= X < N with gcd(X,N)=1, we have X^F = 1 modulo N, so X^(F/2)
36 * is a square root of 1 in Z/NZ. Since Z/NZ ~= Z/PZ x Z/QZ by CRT and the
37 * square roots of 1 in Z/PZ and Z/QZ are +1 and -1, this leaves the four
38 * possibilities X^(F/2) = (+-1, +-1). If it happens that X^(F/2) = (-1,+1)
39 * or (+1,-1), then gcd(X^(F/2) + 1, N) will be equal to one of the prime
40 * factors of N.
41 *
42 * (b) If we don't know F/2 but (F/2) * K for some odd (!) K, then the same
43 * construction still applies since (-)^K is the identity on the set of
44 * roots of 1 in Z/NZ.
45 *
46 * The public and private key primitives (-)^E and (-)^D are mutually inverse
47 * bijections on Z/NZ if and only if (-)^(DE) is the identity on Z/NZ, i.e.
48 * if and only if DE - 1 is a multiple of F, say DE - 1 = F * L.
49 * Splitting L = 2^t * K with K odd, we have
50 *
51 * DE - 1 = FL = (F/2) * (2^(t+1)) * K,
52 *
53 * so (F / 2) * K is among the numbers
54 *
55 * (DE - 1) >> 1, (DE - 1) >> 2, ..., (DE - 1) >> ord
56 *
57 * where ord is the order of 2 in (DE - 1).
58 * We can therefore iterate through these numbers apply the construction
59 * of (a) and (b) above to attempt to factor N.
60 *
61 */
62int mbedtls_rsa_deduce_primes( mbedtls_mpi const *N,
Hanno Beckerc36aab62017-10-17 09:15:06 +010063 mbedtls_mpi const *E, mbedtls_mpi const *D,
Hanno Beckera565f542017-10-11 11:00:19 +010064 mbedtls_mpi *P, mbedtls_mpi *Q )
65{
66 int ret = 0;
67
68 uint16_t attempt; /* Number of current attempt */
69 uint16_t iter; /* Number of squares computed in the current attempt */
70
71 uint16_t order; /* Order of 2 in DE - 1 */
72
73 mbedtls_mpi T; /* Holds largest odd divisor of DE - 1 */
74 mbedtls_mpi K; /* Temporary holding the current candidate */
75
Hanno Becker4055a3a2017-10-17 09:15:26 +010076 const unsigned char primes[] = { 2,
Hanno Beckera565f542017-10-11 11:00:19 +010077 3, 5, 7, 11, 13, 17, 19, 23,
78 29, 31, 37, 41, 43, 47, 53, 59,
79 61, 67, 71, 73, 79, 83, 89, 97,
80 101, 103, 107, 109, 113, 127, 131, 137,
81 139, 149, 151, 157, 163, 167, 173, 179,
82 181, 191, 193, 197, 199, 211, 223, 227,
Hanno Becker4055a3a2017-10-17 09:15:26 +010083 229, 233, 239, 241, 251
Hanno Beckera565f542017-10-11 11:00:19 +010084 };
85
86 const size_t num_primes = sizeof( primes ) / sizeof( *primes );
87
88 if( P == NULL || Q == NULL || P->p != NULL || Q->p != NULL )
89 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
90
91 if( mbedtls_mpi_cmp_int( N, 0 ) <= 0 ||
92 mbedtls_mpi_cmp_int( D, 1 ) <= 0 ||
93 mbedtls_mpi_cmp_mpi( D, N ) >= 0 ||
94 mbedtls_mpi_cmp_int( E, 1 ) <= 0 ||
95 mbedtls_mpi_cmp_mpi( E, N ) >= 0 )
96 {
97 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
98 }
99
100 /*
101 * Initializations and temporary changes
102 */
103
104 mbedtls_mpi_init( &K );
105 mbedtls_mpi_init( &T );
106
107 /* T := DE - 1 */
108 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, D, E ) );
109 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &T, &T, 1 ) );
110
Hanno Becker4952e7a2018-01-03 09:27:40 +0000111 if( ( order = (uint16_t) mbedtls_mpi_lsb( &T ) ) == 0 )
Hanno Beckera565f542017-10-11 11:00:19 +0100112 {
113 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
114 goto cleanup;
115 }
116
117 /* After this operation, T holds the largest odd divisor of DE - 1. */
118 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &T, order ) );
119
120 /*
121 * Actual work
122 */
123
124 /* Skip trying 2 if N == 1 mod 8 */
125 attempt = 0;
126 if( N->p[0] % 8 == 1 )
127 attempt = 1;
128
129 for( ; attempt < num_primes; ++attempt )
130 {
131 mbedtls_mpi_lset( &K, primes[attempt] );
132
133 /* Check if gcd(K,N) = 1 */
134 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( P, &K, N ) );
135 if( mbedtls_mpi_cmp_int( P, 1 ) != 0 )
136 continue;
137
138 /* Go through K^T + 1, K^(2T) + 1, K^(4T) + 1, ...
139 * and check whether they have nontrivial GCD with N. */
140 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &K, &K, &T, N,
141 Q /* temporarily use Q for storing Montgomery
142 * multiplication helper values */ ) );
143
Hanno Becker7643d4e2017-10-11 15:53:02 +0100144 for( iter = 1; iter <= order; ++iter )
Hanno Beckera565f542017-10-11 11:00:19 +0100145 {
Hanno Becker5d42b532017-10-11 15:58:00 +0100146 /* If we reach 1 prematurely, there's no point
147 * in continuing to square K */
148 if( mbedtls_mpi_cmp_int( &K, 1 ) == 0 )
149 break;
150
Hanno Beckera565f542017-10-11 11:00:19 +0100151 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &K, &K, 1 ) );
152 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( P, &K, N ) );
153
154 if( mbedtls_mpi_cmp_int( P, 1 ) == 1 &&
155 mbedtls_mpi_cmp_mpi( P, N ) == -1 )
156 {
157 /*
158 * Have found a nontrivial divisor P of N.
159 * Set Q := N / P.
160 */
161
162 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( Q, NULL, N, P ) );
163 goto cleanup;
164 }
165
166 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
167 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, &K, &K ) );
168 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, N ) );
169 }
Hanno Becker14a00c02017-10-11 12:58:23 +0100170
Hanno Becker5d42b532017-10-11 15:58:00 +0100171 /*
172 * If we get here, then either we prematurely aborted the loop because
173 * we reached 1, or K holds primes[attempt]^(DE - 1) mod N, which must
174 * be 1 if D,E,N were consistent.
175 * Check if that's the case and abort if not, to avoid very long,
176 * yet eventually failing, computations if N,D,E were not sane.
177 */
Hanno Becker14a00c02017-10-11 12:58:23 +0100178 if( mbedtls_mpi_cmp_int( &K, 1 ) != 0 )
179 {
180 break;
181 }
Hanno Beckera565f542017-10-11 11:00:19 +0100182 }
183
184 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
185
186cleanup:
187
188 mbedtls_mpi_free( &K );
189 mbedtls_mpi_free( &T );
190 return( ret );
191}
192
193/*
194 * Given P, Q and the public exponent E, deduce D.
195 * This is essentially a modular inversion.
196 */
197int 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
205 if( D == NULL || mbedtls_mpi_cmp_int( D, 0 ) != 0 )
206 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
207
208 if( mbedtls_mpi_cmp_int( P, 1 ) <= 0 ||
209 mbedtls_mpi_cmp_int( Q, 1 ) <= 0 ||
210 mbedtls_mpi_cmp_int( E, 0 ) == 0 )
211 {
212 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
213 }
214
215 mbedtls_mpi_init( &K );
216 mbedtls_mpi_init( &L );
217
218 /* Temporarily put K := P-1 and L := Q-1 */
219 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
220 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, Q, 1 ) );
221
222 /* Temporarily put D := gcd(P-1, Q-1) */
223 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( D, &K, &L ) );
224
225 /* K := LCM(P-1, Q-1) */
226 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, &K, &L ) );
227 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &K, NULL, &K, D ) );
228
229 /* Compute modular inverse of E in LCM(P-1, Q-1) */
230 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( D, E, &K ) );
231
232cleanup:
233
234 mbedtls_mpi_free( &K );
235 mbedtls_mpi_free( &L );
236
237 return( ret );
238}
239
240/*
241 * Check that RSA CRT parameters are in accordance with core parameters.
242 */
243int mbedtls_rsa_validate_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
244 const mbedtls_mpi *D, const mbedtls_mpi *DP,
245 const mbedtls_mpi *DQ, const mbedtls_mpi *QP )
246{
247 int ret = 0;
248
249 mbedtls_mpi K, L;
250 mbedtls_mpi_init( &K );
251 mbedtls_mpi_init( &L );
252
253 /* Check that DP - D == 0 mod P - 1 */
254 if( DP != NULL )
255 {
256 if( P == NULL )
257 {
258 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
259 goto cleanup;
260 }
261
262 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
263 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &L, DP, D ) );
264 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &L, &L, &K ) );
265
266 if( mbedtls_mpi_cmp_int( &L, 0 ) != 0 )
267 {
268 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
269 goto cleanup;
270 }
271 }
272
273 /* Check that DQ - D == 0 mod Q - 1 */
274 if( DQ != NULL )
275 {
276 if( Q == NULL )
277 {
278 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
279 goto cleanup;
280 }
281
282 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, Q, 1 ) );
283 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &L, DQ, D ) );
284 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &L, &L, &K ) );
285
286 if( mbedtls_mpi_cmp_int( &L, 0 ) != 0 )
287 {
288 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
289 goto cleanup;
290 }
291 }
292
293 /* Check that QP * Q - 1 == 0 mod P */
294 if( QP != NULL )
295 {
296 if( P == NULL || Q == NULL )
297 {
298 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
299 goto cleanup;
300 }
301
302 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, QP, Q ) );
303 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
304 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, P ) );
305 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
306 {
307 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
308 goto cleanup;
309 }
310 }
311
312cleanup:
313
314 /* Wrap MPI error codes by RSA check failure error code */
315 if( ret != 0 &&
316 ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED &&
317 ret != MBEDTLS_ERR_RSA_BAD_INPUT_DATA )
318 {
319 ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
320 }
321
322 mbedtls_mpi_free( &K );
323 mbedtls_mpi_free( &L );
324
325 return( ret );
326}
327
328/*
329 * Check that core RSA parameters are sane.
330 */
331int mbedtls_rsa_validate_params( const mbedtls_mpi *N, const mbedtls_mpi *P,
332 const mbedtls_mpi *Q, const mbedtls_mpi *D,
333 const mbedtls_mpi *E,
334 int (*f_rng)(void *, unsigned char *, size_t),
335 void *p_rng )
336{
337 int ret = 0;
338 mbedtls_mpi K, L;
339
340 mbedtls_mpi_init( &K );
341 mbedtls_mpi_init( &L );
342
343 /*
344 * Step 1: If PRNG provided, check that P and Q are prime
345 */
346
347#if defined(MBEDTLS_GENPRIME)
Janos Follatha0b67c22018-09-18 14:48:23 +0100348 /*
349 * When generating keys, the strongest security we support aims for an error
350 * rate of at most 2^-100 and we are aiming for the same certainty here as
351 * well.
352 */
Hanno Beckera565f542017-10-11 11:00:19 +0100353 if( f_rng != NULL && P != NULL &&
Janos Follatha0b67c22018-09-18 14:48:23 +0100354 ( ret = mbedtls_mpi_is_prime_ext( P, 50, f_rng, p_rng ) ) != 0 )
Hanno Beckera565f542017-10-11 11:00:19 +0100355 {
356 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
357 goto cleanup;
358 }
359
360 if( f_rng != NULL && Q != NULL &&
Janos Follatha0b67c22018-09-18 14:48:23 +0100361 ( ret = mbedtls_mpi_is_prime_ext( Q, 50, f_rng, p_rng ) ) != 0 )
Hanno Beckera565f542017-10-11 11:00:19 +0100362 {
363 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
364 goto cleanup;
365 }
366#else
367 ((void) f_rng);
368 ((void) p_rng);
369#endif /* MBEDTLS_GENPRIME */
370
371 /*
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100372 * Step 2: Check that 1 < N = P * Q
Hanno Beckera565f542017-10-11 11:00:19 +0100373 */
374
375 if( P != NULL && Q != NULL && N != NULL )
376 {
377 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, P, Q ) );
378 if( mbedtls_mpi_cmp_int( N, 1 ) <= 0 ||
379 mbedtls_mpi_cmp_mpi( &K, N ) != 0 )
380 {
381 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
382 goto cleanup;
383 }
384 }
385
386 /*
387 * Step 3: Check and 1 < D, E < N if present.
388 */
389
390 if( N != NULL && D != NULL && E != NULL )
391 {
392 if ( mbedtls_mpi_cmp_int( D, 1 ) <= 0 ||
393 mbedtls_mpi_cmp_int( E, 1 ) <= 0 ||
394 mbedtls_mpi_cmp_mpi( D, N ) >= 0 ||
395 mbedtls_mpi_cmp_mpi( E, N ) >= 0 )
396 {
397 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
398 goto cleanup;
399 }
400 }
401
402 /*
403 * Step 4: Check that D, E are inverse modulo P-1 and Q-1
404 */
405
406 if( P != NULL && Q != NULL && D != NULL && E != NULL )
407 {
408 if( mbedtls_mpi_cmp_int( P, 1 ) <= 0 ||
409 mbedtls_mpi_cmp_int( Q, 1 ) <= 0 )
410 {
411 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
412 goto cleanup;
413 }
414
415 /* Compute DE-1 mod P-1 */
416 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, D, E ) );
417 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
418 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, P, 1 ) );
419 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, &L ) );
420 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
421 {
422 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
423 goto cleanup;
424 }
425
426 /* Compute DE-1 mod Q-1 */
427 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, D, E ) );
428 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
429 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, Q, 1 ) );
430 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, &L ) );
431 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
432 {
433 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
434 goto cleanup;
435 }
436 }
437
438cleanup:
439
440 mbedtls_mpi_free( &K );
441 mbedtls_mpi_free( &L );
442
443 /* Wrap MPI error codes by RSA check failure error code */
444 if( ret != 0 && ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
445 {
446 ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
447 }
448
449 return( ret );
450}
451
452int mbedtls_rsa_deduce_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
453 const mbedtls_mpi *D, mbedtls_mpi *DP,
454 mbedtls_mpi *DQ, mbedtls_mpi *QP )
455{
456 int ret = 0;
457 mbedtls_mpi K;
458 mbedtls_mpi_init( &K );
459
460 /* DP = D mod P-1 */
461 if( DP != NULL )
462 {
463 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
464 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DP, D, &K ) );
465 }
466
467 /* DQ = D mod Q-1 */
468 if( DQ != NULL )
469 {
470 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, Q, 1 ) );
471 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DQ, D, &K ) );
472 }
473
474 /* QP = Q^{-1} mod P */
475 if( QP != NULL )
476 {
477 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( QP, Q, P ) );
478 }
479
480cleanup:
481 mbedtls_mpi_free( &K );
482
483 return( ret );
484}
485
486#endif /* MBEDTLS_RSA_C */