blob: 292fc132092a7b006a3e681462734b9e06aa8aaa [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 {
153 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
173 if( mbedtls_mpi_cmp_int( &K, 1 ) != 0 )
174 {
175 break;
176 }
Hanno Beckera565f542017-10-11 11:00:19 +0100177 }
178
179 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
180
181cleanup:
182
183 mbedtls_mpi_free( &K );
184 mbedtls_mpi_free( &T );
185 return( ret );
186}
187
188/*
189 * Given P, Q and the public exponent E, deduce D.
190 * This is essentially a modular inversion.
191 */
192int mbedtls_rsa_deduce_private_exponent( mbedtls_mpi const *P,
193 mbedtls_mpi const *Q,
194 mbedtls_mpi const *E,
195 mbedtls_mpi *D )
196{
197 int ret = 0;
198 mbedtls_mpi K, L;
199
200 if( D == NULL || mbedtls_mpi_cmp_int( D, 0 ) != 0 )
201 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
202
203 if( mbedtls_mpi_cmp_int( P, 1 ) <= 0 ||
204 mbedtls_mpi_cmp_int( Q, 1 ) <= 0 ||
205 mbedtls_mpi_cmp_int( E, 0 ) == 0 )
206 {
207 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
208 }
209
210 mbedtls_mpi_init( &K );
211 mbedtls_mpi_init( &L );
212
213 /* Temporarily put K := P-1 and L := Q-1 */
214 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
215 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, Q, 1 ) );
216
217 /* Temporarily put D := gcd(P-1, Q-1) */
218 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( D, &K, &L ) );
219
220 /* K := LCM(P-1, Q-1) */
221 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, &K, &L ) );
222 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &K, NULL, &K, D ) );
223
224 /* Compute modular inverse of E in LCM(P-1, Q-1) */
225 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( D, E, &K ) );
226
227cleanup:
228
229 mbedtls_mpi_free( &K );
230 mbedtls_mpi_free( &L );
231
232 return( ret );
233}
234
235/*
236 * Check that RSA CRT parameters are in accordance with core parameters.
237 */
238int mbedtls_rsa_validate_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
239 const mbedtls_mpi *D, const mbedtls_mpi *DP,
240 const mbedtls_mpi *DQ, const mbedtls_mpi *QP )
241{
242 int ret = 0;
243
244 mbedtls_mpi K, L;
245 mbedtls_mpi_init( &K );
246 mbedtls_mpi_init( &L );
247
248 /* Check that DP - D == 0 mod P - 1 */
249 if( DP != NULL )
250 {
251 if( P == NULL )
252 {
253 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
254 goto cleanup;
255 }
256
257 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
258 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &L, DP, D ) );
259 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &L, &L, &K ) );
260
261 if( mbedtls_mpi_cmp_int( &L, 0 ) != 0 )
262 {
263 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
264 goto cleanup;
265 }
266 }
267
268 /* Check that DQ - D == 0 mod Q - 1 */
269 if( DQ != NULL )
270 {
271 if( Q == NULL )
272 {
273 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
274 goto cleanup;
275 }
276
277 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, Q, 1 ) );
278 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &L, DQ, D ) );
279 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &L, &L, &K ) );
280
281 if( mbedtls_mpi_cmp_int( &L, 0 ) != 0 )
282 {
283 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
284 goto cleanup;
285 }
286 }
287
288 /* Check that QP * Q - 1 == 0 mod P */
289 if( QP != NULL )
290 {
291 if( P == NULL || Q == NULL )
292 {
293 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
294 goto cleanup;
295 }
296
297 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, QP, Q ) );
298 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
299 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, P ) );
300 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
301 {
302 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
303 goto cleanup;
304 }
305 }
306
307cleanup:
308
309 /* Wrap MPI error codes by RSA check failure error code */
310 if( ret != 0 &&
311 ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED &&
312 ret != MBEDTLS_ERR_RSA_BAD_INPUT_DATA )
313 {
314 ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
315 }
316
317 mbedtls_mpi_free( &K );
318 mbedtls_mpi_free( &L );
319
320 return( ret );
321}
322
323/*
324 * Check that core RSA parameters are sane.
325 */
326int mbedtls_rsa_validate_params( const mbedtls_mpi *N, const mbedtls_mpi *P,
327 const mbedtls_mpi *Q, const mbedtls_mpi *D,
328 const mbedtls_mpi *E,
329 int (*f_rng)(void *, unsigned char *, size_t),
330 void *p_rng )
331{
332 int ret = 0;
333 mbedtls_mpi K, L;
334
335 mbedtls_mpi_init( &K );
336 mbedtls_mpi_init( &L );
337
338 /*
339 * Step 1: If PRNG provided, check that P and Q are prime
340 */
341
342#if defined(MBEDTLS_GENPRIME)
343 if( f_rng != NULL && P != NULL &&
344 ( ret = mbedtls_mpi_is_prime( P, f_rng, p_rng ) ) != 0 )
345 {
346 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
347 goto cleanup;
348 }
349
350 if( f_rng != NULL && Q != NULL &&
351 ( ret = mbedtls_mpi_is_prime( Q, f_rng, p_rng ) ) != 0 )
352 {
353 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
354 goto cleanup;
355 }
356#else
357 ((void) f_rng);
358 ((void) p_rng);
359#endif /* MBEDTLS_GENPRIME */
360
361 /*
362 * Step 2: Check that 1 < N = PQ
363 */
364
365 if( P != NULL && Q != NULL && N != NULL )
366 {
367 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, P, Q ) );
368 if( mbedtls_mpi_cmp_int( N, 1 ) <= 0 ||
369 mbedtls_mpi_cmp_mpi( &K, N ) != 0 )
370 {
371 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
372 goto cleanup;
373 }
374 }
375
376 /*
377 * Step 3: Check and 1 < D, E < N if present.
378 */
379
380 if( N != NULL && D != NULL && E != NULL )
381 {
382 if ( mbedtls_mpi_cmp_int( D, 1 ) <= 0 ||
383 mbedtls_mpi_cmp_int( E, 1 ) <= 0 ||
384 mbedtls_mpi_cmp_mpi( D, N ) >= 0 ||
385 mbedtls_mpi_cmp_mpi( E, N ) >= 0 )
386 {
387 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
388 goto cleanup;
389 }
390 }
391
392 /*
393 * Step 4: Check that D, E are inverse modulo P-1 and Q-1
394 */
395
396 if( P != NULL && Q != NULL && D != NULL && E != NULL )
397 {
398 if( mbedtls_mpi_cmp_int( P, 1 ) <= 0 ||
399 mbedtls_mpi_cmp_int( Q, 1 ) <= 0 )
400 {
401 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
402 goto cleanup;
403 }
404
405 /* Compute DE-1 mod P-1 */
406 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, D, E ) );
407 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
408 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, P, 1 ) );
409 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, &L ) );
410 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
411 {
412 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
413 goto cleanup;
414 }
415
416 /* Compute DE-1 mod Q-1 */
417 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, D, E ) );
418 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
419 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, Q, 1 ) );
420 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, &L ) );
421 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
422 {
423 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
424 goto cleanup;
425 }
426 }
427
428cleanup:
429
430 mbedtls_mpi_free( &K );
431 mbedtls_mpi_free( &L );
432
433 /* Wrap MPI error codes by RSA check failure error code */
434 if( ret != 0 && ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
435 {
436 ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
437 }
438
439 return( ret );
440}
441
442int mbedtls_rsa_deduce_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
443 const mbedtls_mpi *D, mbedtls_mpi *DP,
444 mbedtls_mpi *DQ, mbedtls_mpi *QP )
445{
446 int ret = 0;
447 mbedtls_mpi K;
448 mbedtls_mpi_init( &K );
449
450 /* DP = D mod P-1 */
451 if( DP != NULL )
452 {
453 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
454 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DP, D, &K ) );
455 }
456
457 /* DQ = D mod Q-1 */
458 if( DQ != NULL )
459 {
460 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, Q, 1 ) );
461 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DQ, D, &K ) );
462 }
463
464 /* QP = Q^{-1} mod P */
465 if( QP != NULL )
466 {
467 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( QP, Q, P ) );
468 }
469
470cleanup:
471 mbedtls_mpi_free( &K );
472
473 return( ret );
474}
475
476#endif /* MBEDTLS_RSA_C */