2 * Helper functions for the RSA module
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #if defined(MBEDTLS_RSA_C)
25 #include "mbedtls/rsa.h"
26 #include "mbedtls/bignum.h"
27 #include "mbedtls/rsa_internal.h"
30 * Compute RSA prime factors from public and private exponents
32 * Summary of algorithm:
33 * Setting F := lcm(P-1,Q-1), the idea is as follows:
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
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
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
51 * DE - 1 = FL = (F/2) * (2^(t+1)) * K,
53 * so (F / 2) * K is among the numbers
55 * (DE - 1) >> 1, (DE - 1) >> 2, ..., (DE - 1) >> ord
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.
62 int mbedtls_rsa_deduce_primes(mbedtls_mpi
const *N
,
63 mbedtls_mpi
const *E
, mbedtls_mpi
const *D
,
64 mbedtls_mpi
*P
, mbedtls_mpi
*Q
) {
67 uint16_t attempt
; /* Number of current attempt */
68 uint16_t iter
; /* Number of squares computed in the current attempt */
70 uint16_t order
; /* Order of 2 in DE - 1 */
72 mbedtls_mpi T
; /* Holds largest odd divisor of DE - 1 */
73 mbedtls_mpi K
; /* Temporary holding the current candidate */
75 const unsigned char primes
[] = { 2,
76 3, 5, 7, 11, 13, 17, 19, 23,
77 29, 31, 37, 41, 43, 47, 53, 59,
78 61, 67, 71, 73, 79, 83, 89, 97,
79 101, 103, 107, 109, 113, 127, 131, 137,
80 139, 149, 151, 157, 163, 167, 173, 179,
81 181, 191, 193, 197, 199, 211, 223, 227,
82 229, 233, 239, 241, 251
85 const size_t num_primes
= sizeof(primes
) / sizeof(*primes
);
87 if (P
== NULL
|| Q
== NULL
|| P
->p
!= NULL
|| Q
->p
!= NULL
)
88 return (MBEDTLS_ERR_MPI_BAD_INPUT_DATA
);
90 if (mbedtls_mpi_cmp_int(N
, 0) <= 0 ||
91 mbedtls_mpi_cmp_int(D
, 1) <= 0 ||
92 mbedtls_mpi_cmp_mpi(D
, N
) >= 0 ||
93 mbedtls_mpi_cmp_int(E
, 1) <= 0 ||
94 mbedtls_mpi_cmp_mpi(E
, N
) >= 0) {
95 return (MBEDTLS_ERR_MPI_BAD_INPUT_DATA
);
99 * Initializations and temporary changes
102 mbedtls_mpi_init(&K
);
103 mbedtls_mpi_init(&T
);
106 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T
, D
, E
));
107 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&T
, &T
, 1));
109 if ((order
= (uint16_t) mbedtls_mpi_lsb(&T
)) == 0) {
110 ret
= MBEDTLS_ERR_MPI_BAD_INPUT_DATA
;
114 /* After this operation, T holds the largest odd divisor of DE - 1. */
115 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&T
, order
));
121 /* Skip trying 2 if N == 1 mod 8 */
123 if (N
->p
[0] % 8 == 1)
126 for (; attempt
< num_primes
; ++attempt
) {
127 mbedtls_mpi_lset(&K
, primes
[attempt
]);
129 /* Check if gcd(K,N) = 1 */
130 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(P
, &K
, N
));
131 if (mbedtls_mpi_cmp_int(P
, 1) != 0)
134 /* Go through K^T + 1, K^(2T) + 1, K^(4T) + 1, ...
135 * and check whether they have nontrivial GCD with N. */
136 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&K
, &K
, &T
, N
,
137 Q
/* temporarily use Q for storing Montgomery
138 * multiplication helper values */));
140 for (iter
= 1; iter
<= order
; ++iter
) {
141 /* If we reach 1 prematurely, there's no point
142 * in continuing to square K */
143 if (mbedtls_mpi_cmp_int(&K
, 1) == 0)
146 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&K
, &K
, 1));
147 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(P
, &K
, N
));
149 if (mbedtls_mpi_cmp_int(P
, 1) == 1 &&
150 mbedtls_mpi_cmp_mpi(P
, N
) == -1) {
152 * Have found a nontrivial divisor P of N.
156 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(Q
, NULL
, N
, P
));
160 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K
, &K
, 1));
161 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K
, &K
, &K
));
162 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&K
, &K
, N
));
166 * If we get here, then either we prematurely aborted the loop because
167 * we reached 1, or K holds primes[attempt]^(DE - 1) mod N, which must
168 * be 1 if D,E,N were consistent.
169 * Check if that's the case and abort if not, to avoid very long,
170 * yet eventually failing, computations if N,D,E were not sane.
172 if (mbedtls_mpi_cmp_int(&K
, 1) != 0) {
177 ret
= MBEDTLS_ERR_MPI_BAD_INPUT_DATA
;
181 mbedtls_mpi_free(&K
);
182 mbedtls_mpi_free(&T
);
187 * Given P, Q and the public exponent E, deduce D.
188 * This is essentially a modular inversion.
190 int mbedtls_rsa_deduce_private_exponent(mbedtls_mpi
const *P
,
191 mbedtls_mpi
const *Q
,
192 mbedtls_mpi
const *E
,
197 if (D
== NULL
|| mbedtls_mpi_cmp_int(D
, 0) != 0)
198 return (MBEDTLS_ERR_MPI_BAD_INPUT_DATA
);
200 if (mbedtls_mpi_cmp_int(P
, 1) <= 0 ||
201 mbedtls_mpi_cmp_int(Q
, 1) <= 0 ||
202 mbedtls_mpi_cmp_int(E
, 0) == 0) {
203 return (MBEDTLS_ERR_MPI_BAD_INPUT_DATA
);
206 mbedtls_mpi_init(&K
);
207 mbedtls_mpi_init(&L
);
209 /* Temporarily put K := P-1 and L := Q-1 */
210 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K
, P
, 1));
211 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&L
, Q
, 1));
213 /* Temporarily put D := gcd(P-1, Q-1) */
214 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(D
, &K
, &L
));
216 /* K := LCM(P-1, Q-1) */
217 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K
, &K
, &L
));
218 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&K
, NULL
, &K
, D
));
220 /* Compute modular inverse of E in LCM(P-1, Q-1) */
221 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(D
, E
, &K
));
225 mbedtls_mpi_free(&K
);
226 mbedtls_mpi_free(&L
);
232 * Check that RSA CRT parameters are in accordance with core parameters.
234 int mbedtls_rsa_validate_crt(const mbedtls_mpi
*P
, const mbedtls_mpi
*Q
,
235 const mbedtls_mpi
*D
, const mbedtls_mpi
*DP
,
236 const mbedtls_mpi
*DQ
, const mbedtls_mpi
*QP
) {
240 mbedtls_mpi_init(&K
);
241 mbedtls_mpi_init(&L
);
243 /* Check that DP - D == 0 mod P - 1 */
246 ret
= MBEDTLS_ERR_RSA_BAD_INPUT_DATA
;
250 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K
, P
, 1));
251 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&L
, DP
, D
));
252 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&L
, &L
, &K
));
254 if (mbedtls_mpi_cmp_int(&L
, 0) != 0) {
255 ret
= MBEDTLS_ERR_RSA_KEY_CHECK_FAILED
;
260 /* Check that DQ - D == 0 mod Q - 1 */
263 ret
= MBEDTLS_ERR_RSA_BAD_INPUT_DATA
;
267 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K
, Q
, 1));
268 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&L
, DQ
, D
));
269 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&L
, &L
, &K
));
271 if (mbedtls_mpi_cmp_int(&L
, 0) != 0) {
272 ret
= MBEDTLS_ERR_RSA_KEY_CHECK_FAILED
;
277 /* Check that QP * Q - 1 == 0 mod P */
279 if (P
== NULL
|| Q
== NULL
) {
280 ret
= MBEDTLS_ERR_RSA_BAD_INPUT_DATA
;
284 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K
, QP
, Q
));
285 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K
, &K
, 1));
286 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&K
, &K
, P
));
287 if (mbedtls_mpi_cmp_int(&K
, 0) != 0) {
288 ret
= MBEDTLS_ERR_RSA_KEY_CHECK_FAILED
;
295 /* Wrap MPI error codes by RSA check failure error code */
297 ret
!= MBEDTLS_ERR_RSA_KEY_CHECK_FAILED
&&
298 ret
!= MBEDTLS_ERR_RSA_BAD_INPUT_DATA
) {
299 ret
+= MBEDTLS_ERR_RSA_KEY_CHECK_FAILED
;
302 mbedtls_mpi_free(&K
);
303 mbedtls_mpi_free(&L
);
309 * Check that core RSA parameters are sane.
311 int mbedtls_rsa_validate_params(const mbedtls_mpi
*N
, const mbedtls_mpi
*P
,
312 const mbedtls_mpi
*Q
, const mbedtls_mpi
*D
,
313 const mbedtls_mpi
*E
,
314 int (*f_rng
)(void *, unsigned char *, size_t),
319 mbedtls_mpi_init(&K
);
320 mbedtls_mpi_init(&L
);
323 * Step 1: If PRNG provided, check that P and Q are prime
326 #if defined(MBEDTLS_GENPRIME)
328 * When generating keys, the strongest security we support aims for an error
329 * rate of at most 2^-100 and we are aiming for the same certainty here as
332 if (f_rng
!= NULL
&& P
!= NULL
&&
333 (ret
= mbedtls_mpi_is_prime_ext(P
, 50, f_rng
, p_rng
)) != 0) {
334 ret
= MBEDTLS_ERR_RSA_KEY_CHECK_FAILED
;
338 if (f_rng
!= NULL
&& Q
!= NULL
&&
339 (ret
= mbedtls_mpi_is_prime_ext(Q
, 50, f_rng
, p_rng
)) != 0) {
340 ret
= MBEDTLS_ERR_RSA_KEY_CHECK_FAILED
;
346 #endif /* MBEDTLS_GENPRIME */
349 * Step 2: Check that 1 < N = P * Q
352 if (P
!= NULL
&& Q
!= NULL
&& N
!= NULL
) {
353 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K
, P
, Q
));
354 if (mbedtls_mpi_cmp_int(N
, 1) <= 0 ||
355 mbedtls_mpi_cmp_mpi(&K
, N
) != 0) {
356 ret
= MBEDTLS_ERR_RSA_KEY_CHECK_FAILED
;
362 * Step 3: Check and 1 < D, E < N if present.
365 if (N
!= NULL
&& D
!= NULL
&& E
!= NULL
) {
366 if (mbedtls_mpi_cmp_int(D
, 1) <= 0 ||
367 mbedtls_mpi_cmp_int(E
, 1) <= 0 ||
368 mbedtls_mpi_cmp_mpi(D
, N
) >= 0 ||
369 mbedtls_mpi_cmp_mpi(E
, N
) >= 0) {
370 ret
= MBEDTLS_ERR_RSA_KEY_CHECK_FAILED
;
376 * Step 4: Check that D, E are inverse modulo P-1 and Q-1
379 if (P
!= NULL
&& Q
!= NULL
&& D
!= NULL
&& E
!= NULL
) {
380 if (mbedtls_mpi_cmp_int(P
, 1) <= 0 ||
381 mbedtls_mpi_cmp_int(Q
, 1) <= 0) {
382 ret
= MBEDTLS_ERR_RSA_KEY_CHECK_FAILED
;
386 /* Compute DE-1 mod P-1 */
387 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K
, D
, E
));
388 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K
, &K
, 1));
389 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&L
, P
, 1));
390 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&K
, &K
, &L
));
391 if (mbedtls_mpi_cmp_int(&K
, 0) != 0) {
392 ret
= MBEDTLS_ERR_RSA_KEY_CHECK_FAILED
;
396 /* Compute DE-1 mod Q-1 */
397 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K
, D
, E
));
398 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K
, &K
, 1));
399 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&L
, Q
, 1));
400 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&K
, &K
, &L
));
401 if (mbedtls_mpi_cmp_int(&K
, 0) != 0) {
402 ret
= MBEDTLS_ERR_RSA_KEY_CHECK_FAILED
;
409 mbedtls_mpi_free(&K
);
410 mbedtls_mpi_free(&L
);
412 /* Wrap MPI error codes by RSA check failure error code */
413 if (ret
!= 0 && ret
!= MBEDTLS_ERR_RSA_KEY_CHECK_FAILED
) {
414 ret
+= MBEDTLS_ERR_RSA_KEY_CHECK_FAILED
;
420 int mbedtls_rsa_deduce_crt(const mbedtls_mpi
*P
, const mbedtls_mpi
*Q
,
421 const mbedtls_mpi
*D
, mbedtls_mpi
*DP
,
422 mbedtls_mpi
*DQ
, mbedtls_mpi
*QP
) {
425 mbedtls_mpi_init(&K
);
429 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K
, P
, 1));
430 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(DP
, D
, &K
));
435 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K
, Q
, 1));
436 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(DQ
, D
, &K
));
439 /* QP = Q^{-1} mod P */
441 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(QP
, Q
, P
));
445 mbedtls_mpi_free(&K
);
450 #endif /* MBEDTLS_RSA_C */