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