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