blob: b4098f4949ef1b8d48a98e218f84057ee3c02fd0 [file] [log] [blame]
Hanno Beckera565f542017-10-11 11:00:19 +01001/*
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
Gilles Peskinedb09ef62020-06-03 01:43:33 +020023#include "common.h"
Hanno Beckera565f542017-10-11 11:00:19 +010024
25#if defined(MBEDTLS_RSA_C)
26
27#include "mbedtls/rsa.h"
28#include "mbedtls/bignum.h"
29#include "mbedtls/rsa_internal.h"
30
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 */
64int mbedtls_rsa_deduce_primes( mbedtls_mpi const *N,
Hanno Beckerc36aab62017-10-17 09:15:06 +010065 mbedtls_mpi const *E, mbedtls_mpi const *D,
Hanno Beckera565f542017-10-11 11:00:19 +010066 mbedtls_mpi *P, mbedtls_mpi *Q )
67{
68 int ret = 0;
69
70 uint16_t attempt; /* Number of current attempt */
71 uint16_t iter; /* Number of squares computed in the current attempt */
72
73 uint16_t order; /* Order of 2 in DE - 1 */
74
75 mbedtls_mpi T; /* Holds largest odd divisor of DE - 1 */
76 mbedtls_mpi K; /* Temporary holding the current candidate */
77
Hanno Becker4055a3a2017-10-17 09:15:26 +010078 const unsigned char primes[] = { 2,
Hanno Beckera565f542017-10-11 11:00:19 +010079 3, 5, 7, 11, 13, 17, 19, 23,
80 29, 31, 37, 41, 43, 47, 53, 59,
81 61, 67, 71, 73, 79, 83, 89, 97,
82 101, 103, 107, 109, 113, 127, 131, 137,
83 139, 149, 151, 157, 163, 167, 173, 179,
84 181, 191, 193, 197, 199, 211, 223, 227,
Hanno Becker4055a3a2017-10-17 09:15:26 +010085 229, 233, 239, 241, 251
Hanno Beckera565f542017-10-11 11:00:19 +010086 };
87
88 const size_t num_primes = sizeof( primes ) / sizeof( *primes );
89
90 if( P == NULL || Q == NULL || P->p != NULL || Q->p != NULL )
91 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
92
93 if( mbedtls_mpi_cmp_int( N, 0 ) <= 0 ||
94 mbedtls_mpi_cmp_int( D, 1 ) <= 0 ||
95 mbedtls_mpi_cmp_mpi( D, N ) >= 0 ||
96 mbedtls_mpi_cmp_int( E, 1 ) <= 0 ||
97 mbedtls_mpi_cmp_mpi( E, N ) >= 0 )
98 {
99 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
100 }
101
102 /*
103 * Initializations and temporary changes
104 */
105
106 mbedtls_mpi_init( &K );
107 mbedtls_mpi_init( &T );
108
109 /* T := DE - 1 */
110 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, D, E ) );
111 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &T, &T, 1 ) );
112
Hanno Becker4952e7a2018-01-03 09:27:40 +0000113 if( ( order = (uint16_t) mbedtls_mpi_lsb( &T ) ) == 0 )
Hanno Beckera565f542017-10-11 11:00:19 +0100114 {
115 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
116 goto cleanup;
117 }
118
119 /* After this operation, T holds the largest odd divisor of DE - 1. */
120 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &T, order ) );
121
122 /*
123 * Actual work
124 */
125
126 /* Skip trying 2 if N == 1 mod 8 */
127 attempt = 0;
128 if( N->p[0] % 8 == 1 )
129 attempt = 1;
130
131 for( ; attempt < num_primes; ++attempt )
132 {
133 mbedtls_mpi_lset( &K, primes[attempt] );
134
135 /* Check if gcd(K,N) = 1 */
136 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( P, &K, N ) );
137 if( mbedtls_mpi_cmp_int( P, 1 ) != 0 )
138 continue;
139
140 /* Go through K^T + 1, K^(2T) + 1, K^(4T) + 1, ...
141 * and check whether they have nontrivial GCD with N. */
142 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &K, &K, &T, N,
143 Q /* temporarily use Q for storing Montgomery
144 * multiplication helper values */ ) );
145
Hanno Becker7643d4e2017-10-11 15:53:02 +0100146 for( iter = 1; iter <= order; ++iter )
Hanno Beckera565f542017-10-11 11:00:19 +0100147 {
Hanno Becker5d42b532017-10-11 15:58:00 +0100148 /* If we reach 1 prematurely, there's no point
149 * in continuing to square K */
150 if( mbedtls_mpi_cmp_int( &K, 1 ) == 0 )
151 break;
152
Hanno Beckera565f542017-10-11 11:00:19 +0100153 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &K, &K, 1 ) );
154 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( P, &K, N ) );
155
156 if( mbedtls_mpi_cmp_int( P, 1 ) == 1 &&
157 mbedtls_mpi_cmp_mpi( P, N ) == -1 )
158 {
159 /*
160 * Have found a nontrivial divisor P of N.
161 * Set Q := N / P.
162 */
163
164 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( Q, NULL, N, P ) );
165 goto cleanup;
166 }
167
168 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
169 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, &K, &K ) );
170 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, N ) );
171 }
Hanno Becker14a00c02017-10-11 12:58:23 +0100172
Hanno Becker5d42b532017-10-11 15:58:00 +0100173 /*
174 * If we get here, then either we prematurely aborted the loop because
175 * we reached 1, or K holds primes[attempt]^(DE - 1) mod N, which must
176 * be 1 if D,E,N were consistent.
177 * Check if that's the case and abort if not, to avoid very long,
178 * yet eventually failing, computations if N,D,E were not sane.
179 */
Hanno Becker14a00c02017-10-11 12:58:23 +0100180 if( mbedtls_mpi_cmp_int( &K, 1 ) != 0 )
181 {
182 break;
183 }
Hanno Beckera565f542017-10-11 11:00:19 +0100184 }
185
186 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
187
188cleanup:
189
190 mbedtls_mpi_free( &K );
191 mbedtls_mpi_free( &T );
192 return( ret );
193}
194
195/*
196 * Given P, Q and the public exponent E, deduce D.
197 * This is essentially a modular inversion.
198 */
199int mbedtls_rsa_deduce_private_exponent( mbedtls_mpi const *P,
200 mbedtls_mpi const *Q,
201 mbedtls_mpi const *E,
202 mbedtls_mpi *D )
203{
204 int ret = 0;
205 mbedtls_mpi K, L;
206
207 if( D == NULL || mbedtls_mpi_cmp_int( D, 0 ) != 0 )
208 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
209
210 if( mbedtls_mpi_cmp_int( P, 1 ) <= 0 ||
211 mbedtls_mpi_cmp_int( Q, 1 ) <= 0 ||
212 mbedtls_mpi_cmp_int( E, 0 ) == 0 )
213 {
214 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
215 }
216
217 mbedtls_mpi_init( &K );
218 mbedtls_mpi_init( &L );
219
220 /* Temporarily put K := P-1 and L := Q-1 */
221 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
222 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, Q, 1 ) );
223
224 /* Temporarily put D := gcd(P-1, Q-1) */
225 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( D, &K, &L ) );
226
227 /* K := LCM(P-1, Q-1) */
228 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, &K, &L ) );
229 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &K, NULL, &K, D ) );
230
231 /* Compute modular inverse of E in LCM(P-1, Q-1) */
232 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( D, E, &K ) );
233
234cleanup:
235
236 mbedtls_mpi_free( &K );
237 mbedtls_mpi_free( &L );
238
239 return( ret );
240}
241
242/*
243 * Check that RSA CRT parameters are in accordance with core parameters.
244 */
245int mbedtls_rsa_validate_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
246 const mbedtls_mpi *D, const mbedtls_mpi *DP,
247 const mbedtls_mpi *DQ, const mbedtls_mpi *QP )
248{
249 int ret = 0;
250
251 mbedtls_mpi K, L;
252 mbedtls_mpi_init( &K );
253 mbedtls_mpi_init( &L );
254
255 /* Check that DP - D == 0 mod P - 1 */
256 if( DP != NULL )
257 {
258 if( P == NULL )
259 {
260 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
261 goto cleanup;
262 }
263
264 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
265 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &L, DP, D ) );
266 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &L, &L, &K ) );
267
268 if( mbedtls_mpi_cmp_int( &L, 0 ) != 0 )
269 {
270 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
271 goto cleanup;
272 }
273 }
274
275 /* Check that DQ - D == 0 mod Q - 1 */
276 if( DQ != NULL )
277 {
278 if( Q == NULL )
279 {
280 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
281 goto cleanup;
282 }
283
284 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, Q, 1 ) );
285 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &L, DQ, D ) );
286 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &L, &L, &K ) );
287
288 if( mbedtls_mpi_cmp_int( &L, 0 ) != 0 )
289 {
290 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
291 goto cleanup;
292 }
293 }
294
295 /* Check that QP * Q - 1 == 0 mod P */
296 if( QP != NULL )
297 {
298 if( P == NULL || Q == NULL )
299 {
300 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
301 goto cleanup;
302 }
303
304 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, QP, Q ) );
305 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
306 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, P ) );
307 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
308 {
309 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
310 goto cleanup;
311 }
312 }
313
314cleanup:
315
316 /* Wrap MPI error codes by RSA check failure error code */
317 if( ret != 0 &&
318 ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED &&
319 ret != MBEDTLS_ERR_RSA_BAD_INPUT_DATA )
320 {
321 ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
322 }
323
324 mbedtls_mpi_free( &K );
325 mbedtls_mpi_free( &L );
326
327 return( ret );
328}
329
330/*
331 * Check that core RSA parameters are sane.
332 */
333int mbedtls_rsa_validate_params( const mbedtls_mpi *N, const mbedtls_mpi *P,
334 const mbedtls_mpi *Q, const mbedtls_mpi *D,
335 const mbedtls_mpi *E,
336 int (*f_rng)(void *, unsigned char *, size_t),
337 void *p_rng )
338{
339 int ret = 0;
340 mbedtls_mpi K, L;
341
342 mbedtls_mpi_init( &K );
343 mbedtls_mpi_init( &L );
344
345 /*
346 * Step 1: If PRNG provided, check that P and Q are prime
347 */
348
349#if defined(MBEDTLS_GENPRIME)
Janos Follatha0b67c22018-09-18 14:48:23 +0100350 /*
351 * When generating keys, the strongest security we support aims for an error
352 * rate of at most 2^-100 and we are aiming for the same certainty here as
353 * well.
354 */
Hanno Beckera565f542017-10-11 11:00:19 +0100355 if( f_rng != NULL && P != NULL &&
Janos Follatha0b67c22018-09-18 14:48:23 +0100356 ( ret = mbedtls_mpi_is_prime_ext( P, 50, f_rng, p_rng ) ) != 0 )
Hanno Beckera565f542017-10-11 11:00:19 +0100357 {
358 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
359 goto cleanup;
360 }
361
362 if( f_rng != NULL && Q != NULL &&
Janos Follatha0b67c22018-09-18 14:48:23 +0100363 ( ret = mbedtls_mpi_is_prime_ext( Q, 50, f_rng, p_rng ) ) != 0 )
Hanno Beckera565f542017-10-11 11:00:19 +0100364 {
365 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
366 goto cleanup;
367 }
368#else
369 ((void) f_rng);
370 ((void) p_rng);
371#endif /* MBEDTLS_GENPRIME */
372
373 /*
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100374 * Step 2: Check that 1 < N = P * Q
Hanno Beckera565f542017-10-11 11:00:19 +0100375 */
376
377 if( P != NULL && Q != NULL && N != NULL )
378 {
379 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, P, Q ) );
380 if( mbedtls_mpi_cmp_int( N, 1 ) <= 0 ||
381 mbedtls_mpi_cmp_mpi( &K, N ) != 0 )
382 {
383 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
384 goto cleanup;
385 }
386 }
387
388 /*
389 * Step 3: Check and 1 < D, E < N if present.
390 */
391
392 if( N != NULL && D != NULL && E != NULL )
393 {
394 if ( mbedtls_mpi_cmp_int( D, 1 ) <= 0 ||
395 mbedtls_mpi_cmp_int( E, 1 ) <= 0 ||
396 mbedtls_mpi_cmp_mpi( D, N ) >= 0 ||
397 mbedtls_mpi_cmp_mpi( E, N ) >= 0 )
398 {
399 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
400 goto cleanup;
401 }
402 }
403
404 /*
405 * Step 4: Check that D, E are inverse modulo P-1 and Q-1
406 */
407
408 if( P != NULL && Q != NULL && D != NULL && E != NULL )
409 {
410 if( mbedtls_mpi_cmp_int( P, 1 ) <= 0 ||
411 mbedtls_mpi_cmp_int( Q, 1 ) <= 0 )
412 {
413 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
414 goto cleanup;
415 }
416
417 /* Compute DE-1 mod P-1 */
418 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, D, E ) );
419 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
420 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, P, 1 ) );
421 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, &L ) );
422 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
423 {
424 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
425 goto cleanup;
426 }
427
428 /* Compute DE-1 mod Q-1 */
429 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, D, E ) );
430 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
431 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, Q, 1 ) );
432 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, &L ) );
433 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
434 {
435 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
436 goto cleanup;
437 }
438 }
439
440cleanup:
441
442 mbedtls_mpi_free( &K );
443 mbedtls_mpi_free( &L );
444
445 /* Wrap MPI error codes by RSA check failure error code */
446 if( ret != 0 && ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
447 {
448 ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
449 }
450
451 return( ret );
452}
453
454int mbedtls_rsa_deduce_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
455 const mbedtls_mpi *D, mbedtls_mpi *DP,
456 mbedtls_mpi *DQ, mbedtls_mpi *QP )
457{
458 int ret = 0;
459 mbedtls_mpi K;
460 mbedtls_mpi_init( &K );
461
462 /* DP = D mod P-1 */
463 if( DP != NULL )
464 {
465 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
466 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DP, D, &K ) );
467 }
468
469 /* DQ = D mod Q-1 */
470 if( DQ != NULL )
471 {
472 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, Q, 1 ) );
473 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DQ, D, &K ) );
474 }
475
476 /* QP = Q^{-1} mod P */
477 if( QP != NULL )
478 {
479 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( QP, Q, P ) );
480 }
481
482cleanup:
483 mbedtls_mpi_free( &K );
484
485 return( ret );
486}
487
488#endif /* MBEDTLS_RSA_C */