blob: 1a94fc646a8b83537906920f7f7c6497c33ed78f [file] [log] [blame]
Hanno Beckera565f542017-10-11 11:00:19 +01001/*
2 * Helper functions for the RSA module
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Hanno Beckera565f542017-10-11 11:00:19 +01005 * 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 *
Hanno Beckera565f542017-10-11 11:00:19 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Hanno Beckera565f542017-10-11 11:00:19 +010022
23#if defined(MBEDTLS_RSA_C)
24
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020025# include "mbedtls/rsa.h"
26# include "mbedtls/bignum.h"
27# include "rsa_alt_helpers.h"
Hanno Beckera565f542017-10-11 11:00:19 +010028
29/*
30 * Compute RSA prime factors from public and private exponents
31 *
32 * Summary of algorithm:
33 * Setting F := lcm(P-1,Q-1), the idea is as follows:
34 *
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
40 * factors of N.
41 *
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
44 * roots of 1 in Z/NZ.
45 *
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
50 *
51 * DE - 1 = FL = (F/2) * (2^(t+1)) * K,
52 *
53 * so (F / 2) * K is among the numbers
54 *
55 * (DE - 1) >> 1, (DE - 1) >> 2, ..., (DE - 1) >> ord
56 *
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.
60 *
61 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020062int mbedtls_rsa_deduce_primes(mbedtls_mpi const *N,
63 mbedtls_mpi const *E,
64 mbedtls_mpi const *D,
65 mbedtls_mpi *P,
66 mbedtls_mpi *Q)
Hanno Beckera565f542017-10-11 11:00:19 +010067{
68 int ret = 0;
69
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020070 uint16_t attempt; /* Number of current attempt */
71 uint16_t iter; /* Number of squares computed in the current attempt */
Hanno Beckera565f542017-10-11 11:00:19 +010072
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020073 uint16_t order; /* Order of 2 in DE - 1 */
Hanno Beckera565f542017-10-11 11:00:19 +010074
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020075 mbedtls_mpi T; /* Holds largest odd divisor of DE - 1 */
76 mbedtls_mpi K; /* Temporary holding the current candidate */
Hanno Beckera565f542017-10-11 11:00:19 +010077
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020078 const unsigned char primes[] = {
79 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,
80 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107,
81 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181,
82 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251
Hanno Beckera565f542017-10-11 11:00:19 +010083 };
84
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020085 const size_t num_primes = sizeof(primes) / sizeof(*primes);
Hanno Beckera565f542017-10-11 11:00:19 +010086
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020087 if (P == NULL || Q == NULL || P->p != NULL || Q->p != NULL)
88 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Hanno Beckera565f542017-10-11 11:00:19 +010089
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020090 if (mbedtls_mpi_cmp_int(N, 0) <= 0 || mbedtls_mpi_cmp_int(D, 1) <= 0 ||
91 mbedtls_mpi_cmp_mpi(D, N) >= 0 || mbedtls_mpi_cmp_int(E, 1) <= 0 ||
92 mbedtls_mpi_cmp_mpi(E, N) >= 0) {
93 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Hanno Beckera565f542017-10-11 11:00:19 +010094 }
95
96 /*
97 * Initializations and temporary changes
98 */
99
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200100 mbedtls_mpi_init(&K);
101 mbedtls_mpi_init(&T);
Hanno Beckera565f542017-10-11 11:00:19 +0100102
103 /* T := DE - 1 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200104 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, D, E));
105 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&T, &T, 1));
Hanno Beckera565f542017-10-11 11:00:19 +0100106
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200107 if ((order = (uint16_t)mbedtls_mpi_lsb(&T)) == 0) {
Hanno Beckera565f542017-10-11 11:00:19 +0100108 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
109 goto cleanup;
110 }
111
112 /* After this operation, T holds the largest odd divisor of DE - 1. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200113 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&T, order));
Hanno Beckera565f542017-10-11 11:00:19 +0100114
115 /*
116 * Actual work
117 */
118
119 /* Skip trying 2 if N == 1 mod 8 */
120 attempt = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200121 if (N->p[0] % 8 == 1)
Hanno Beckera565f542017-10-11 11:00:19 +0100122 attempt = 1;
123
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200124 for (; attempt < num_primes; ++attempt) {
125 mbedtls_mpi_lset(&K, primes[attempt]);
Hanno Beckera565f542017-10-11 11:00:19 +0100126
127 /* Check if gcd(K,N) = 1 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200128 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(P, &K, N));
129 if (mbedtls_mpi_cmp_int(P, 1) != 0)
Hanno Beckera565f542017-10-11 11:00:19 +0100130 continue;
131
132 /* Go through K^T + 1, K^(2T) + 1, K^(4T) + 1, ...
133 * and check whether they have nontrivial GCD with N. */
134 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &K, &K, &T, N,
135 Q /* temporarily use Q for storing Montgomery
136 * multiplication helper values */ ) );
137
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200138 for (iter = 1; iter <= order; ++iter) {
Hanno Becker5d42b532017-10-11 15:58:00 +0100139 /* If we reach 1 prematurely, there's no point
140 * in continuing to square K */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200141 if (mbedtls_mpi_cmp_int(&K, 1) == 0)
Hanno Becker5d42b532017-10-11 15:58:00 +0100142 break;
143
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200144 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&K, &K, 1));
145 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(P, &K, N));
Hanno Beckera565f542017-10-11 11:00:19 +0100146
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200147 if (mbedtls_mpi_cmp_int(P, 1) == 1 &&
148 mbedtls_mpi_cmp_mpi(P, N) == -1) {
Hanno Beckera565f542017-10-11 11:00:19 +0100149 /*
150 * Have found a nontrivial divisor P of N.
151 * Set Q := N / P.
152 */
153
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200154 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(Q, NULL, N, P));
Hanno Beckera565f542017-10-11 11:00:19 +0100155 goto cleanup;
156 }
157
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200158 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, &K, 1));
159 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, &K, &K));
160 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&K, &K, N));
Hanno Beckera565f542017-10-11 11:00:19 +0100161 }
Hanno Becker14a00c02017-10-11 12:58:23 +0100162
Hanno Becker5d42b532017-10-11 15:58:00 +0100163 /*
164 * If we get here, then either we prematurely aborted the loop because
165 * we reached 1, or K holds primes[attempt]^(DE - 1) mod N, which must
166 * be 1 if D,E,N were consistent.
167 * Check if that's the case and abort if not, to avoid very long,
168 * yet eventually failing, computations if N,D,E were not sane.
169 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200170 if (mbedtls_mpi_cmp_int(&K, 1) != 0) {
Hanno Becker14a00c02017-10-11 12:58:23 +0100171 break;
172 }
Hanno Beckera565f542017-10-11 11:00:19 +0100173 }
174
175 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
176
177cleanup:
178
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200179 mbedtls_mpi_free(&K);
180 mbedtls_mpi_free(&T);
181 return ret;
Hanno Beckera565f542017-10-11 11:00:19 +0100182}
183
184/*
185 * Given P, Q and the public exponent E, deduce D.
186 * This is essentially a modular inversion.
187 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200188int mbedtls_rsa_deduce_private_exponent(mbedtls_mpi const *P,
189 mbedtls_mpi const *Q,
190 mbedtls_mpi const *E,
191 mbedtls_mpi *D)
Hanno Beckera565f542017-10-11 11:00:19 +0100192{
193 int ret = 0;
194 mbedtls_mpi K, L;
195
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200196 if (D == NULL || mbedtls_mpi_cmp_int(D, 0) != 0)
197 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Hanno Beckera565f542017-10-11 11:00:19 +0100198
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200199 if (mbedtls_mpi_cmp_int(P, 1) <= 0 || mbedtls_mpi_cmp_int(Q, 1) <= 0 ||
200 mbedtls_mpi_cmp_int(E, 0) == 0) {
201 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Hanno Beckera565f542017-10-11 11:00:19 +0100202 }
203
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200204 mbedtls_mpi_init(&K);
205 mbedtls_mpi_init(&L);
Hanno Beckera565f542017-10-11 11:00:19 +0100206
207 /* Temporarily put K := P-1 and L := Q-1 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200208 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, P, 1));
209 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&L, Q, 1));
Hanno Beckera565f542017-10-11 11:00:19 +0100210
211 /* Temporarily put D := gcd(P-1, Q-1) */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200212 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(D, &K, &L));
Hanno Beckera565f542017-10-11 11:00:19 +0100213
214 /* K := LCM(P-1, Q-1) */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200215 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, &K, &L));
216 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&K, NULL, &K, D));
Hanno Beckera565f542017-10-11 11:00:19 +0100217
218 /* Compute modular inverse of E in LCM(P-1, Q-1) */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200219 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(D, E, &K));
Hanno Beckera565f542017-10-11 11:00:19 +0100220
221cleanup:
222
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200223 mbedtls_mpi_free(&K);
224 mbedtls_mpi_free(&L);
Hanno Beckera565f542017-10-11 11:00:19 +0100225
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200226 return ret;
Hanno Beckera565f542017-10-11 11:00:19 +0100227}
228
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200229int mbedtls_rsa_deduce_crt(const mbedtls_mpi *P,
230 const mbedtls_mpi *Q,
231 const mbedtls_mpi *D,
232 mbedtls_mpi *DP,
233 mbedtls_mpi *DQ,
234 mbedtls_mpi *QP)
Hanno Beckera565f542017-10-11 11:00:19 +0100235{
236 int ret = 0;
Chris Jones66a4cd42021-03-09 16:04:12 +0000237 mbedtls_mpi K;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200238 mbedtls_mpi_init(&K);
Hanno Beckera565f542017-10-11 11:00:19 +0100239
Chris Jones66a4cd42021-03-09 16:04:12 +0000240 /* DP = D mod P-1 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200241 if (DP != NULL) {
242 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, P, 1));
243 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(DP, D, &K));
Hanno Beckera565f542017-10-11 11:00:19 +0100244 }
245
Chris Jones66a4cd42021-03-09 16:04:12 +0000246 /* DQ = D mod Q-1 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200247 if (DQ != NULL) {
248 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, Q, 1));
249 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(DQ, D, &K));
Hanno Beckera565f542017-10-11 11:00:19 +0100250 }
251
Chris Jones66a4cd42021-03-09 16:04:12 +0000252 /* QP = Q^{-1} mod P */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200253 if (QP != NULL) {
254 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(QP, Q, P));
Hanno Beckera565f542017-10-11 11:00:19 +0100255 }
256
257cleanup:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200258 mbedtls_mpi_free(&K);
Hanno Beckera565f542017-10-11 11:00:19 +0100259
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200260 return ret;
Hanno Beckera565f542017-10-11 11:00:19 +0100261}
262
263/*
264 * Check that core RSA parameters are sane.
265 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200266int mbedtls_rsa_validate_params(const mbedtls_mpi *N,
267 const mbedtls_mpi *P,
268 const mbedtls_mpi *Q,
269 const mbedtls_mpi *D,
270 const mbedtls_mpi *E,
271 int (*f_rng)(void *, unsigned char *, size_t),
272 void *p_rng)
Hanno Beckera565f542017-10-11 11:00:19 +0100273{
274 int ret = 0;
275 mbedtls_mpi K, L;
276
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200277 mbedtls_mpi_init(&K);
278 mbedtls_mpi_init(&L);
Hanno Beckera565f542017-10-11 11:00:19 +0100279
280 /*
281 * Step 1: If PRNG provided, check that P and Q are prime
282 */
283
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200284# if defined(MBEDTLS_GENPRIME)
Janos Follatha0b67c22018-09-18 14:48:23 +0100285 /*
286 * When generating keys, the strongest security we support aims for an error
287 * rate of at most 2^-100 and we are aiming for the same certainty here as
288 * well.
289 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200290 if (f_rng != NULL && P != NULL &&
291 (ret = mbedtls_mpi_is_prime_ext(P, 50, f_rng, p_rng)) != 0) {
Hanno Beckera565f542017-10-11 11:00:19 +0100292 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
293 goto cleanup;
294 }
295
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200296 if (f_rng != NULL && Q != NULL &&
297 (ret = mbedtls_mpi_is_prime_ext(Q, 50, f_rng, p_rng)) != 0) {
Hanno Beckera565f542017-10-11 11:00:19 +0100298 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
299 goto cleanup;
300 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200301# else
302 ((void)f_rng);
303 ((void)p_rng);
304# endif /* MBEDTLS_GENPRIME */
Hanno Beckera565f542017-10-11 11:00:19 +0100305
306 /*
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100307 * Step 2: Check that 1 < N = P * Q
Hanno Beckera565f542017-10-11 11:00:19 +0100308 */
309
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200310 if (P != NULL && Q != NULL && N != NULL) {
311 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, P, Q));
312 if (mbedtls_mpi_cmp_int(N, 1) <= 0 || mbedtls_mpi_cmp_mpi(&K, N) != 0) {
Hanno Beckera565f542017-10-11 11:00:19 +0100313 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
314 goto cleanup;
315 }
316 }
317
318 /*
319 * Step 3: Check and 1 < D, E < N if present.
320 */
321
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200322 if (N != NULL && D != NULL && E != NULL) {
323 if (mbedtls_mpi_cmp_int(D, 1) <= 0 || mbedtls_mpi_cmp_int(E, 1) <= 0 ||
324 mbedtls_mpi_cmp_mpi(D, N) >= 0 || mbedtls_mpi_cmp_mpi(E, N) >= 0) {
Hanno Beckera565f542017-10-11 11:00:19 +0100325 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
326 goto cleanup;
327 }
328 }
329
330 /*
331 * Step 4: Check that D, E are inverse modulo P-1 and Q-1
332 */
333
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200334 if (P != NULL && Q != NULL && D != NULL && E != NULL) {
335 if (mbedtls_mpi_cmp_int(P, 1) <= 0 || mbedtls_mpi_cmp_int(Q, 1) <= 0) {
Hanno Beckera565f542017-10-11 11:00:19 +0100336 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
337 goto cleanup;
338 }
339
340 /* Compute DE-1 mod P-1 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200341 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, D, E));
342 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, &K, 1));
343 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&L, P, 1));
344 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&K, &K, &L));
345 if (mbedtls_mpi_cmp_int(&K, 0) != 0) {
Hanno Beckera565f542017-10-11 11:00:19 +0100346 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
347 goto cleanup;
348 }
349
350 /* Compute DE-1 mod Q-1 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200351 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, D, E));
352 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, &K, 1));
353 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&L, Q, 1));
354 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&K, &K, &L));
355 if (mbedtls_mpi_cmp_int(&K, 0) != 0) {
Hanno Beckera565f542017-10-11 11:00:19 +0100356 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
357 goto cleanup;
358 }
359 }
360
361cleanup:
362
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200363 mbedtls_mpi_free(&K);
364 mbedtls_mpi_free(&L);
Hanno Beckera565f542017-10-11 11:00:19 +0100365
366 /* Wrap MPI error codes by RSA check failure error code */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200367 if (ret != 0 && ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED) {
Hanno Beckera565f542017-10-11 11:00:19 +0100368 ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
369 }
370
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200371 return ret;
Hanno Beckera565f542017-10-11 11:00:19 +0100372}
373
Chris Jones66a4cd42021-03-09 16:04:12 +0000374/*
375 * Check that RSA CRT parameters are in accordance with core parameters.
376 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200377int mbedtls_rsa_validate_crt(const mbedtls_mpi *P,
378 const mbedtls_mpi *Q,
379 const mbedtls_mpi *D,
380 const mbedtls_mpi *DP,
381 const mbedtls_mpi *DQ,
382 const mbedtls_mpi *QP)
Hanno Beckera565f542017-10-11 11:00:19 +0100383{
384 int ret = 0;
Hanno Beckera565f542017-10-11 11:00:19 +0100385
Chris Jones66a4cd42021-03-09 16:04:12 +0000386 mbedtls_mpi K, L;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200387 mbedtls_mpi_init(&K);
388 mbedtls_mpi_init(&L);
Chris Jones66a4cd42021-03-09 16:04:12 +0000389
390 /* Check that DP - D == 0 mod P - 1 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200391 if (DP != NULL) {
392 if (P == NULL) {
Chris Jones66a4cd42021-03-09 16:04:12 +0000393 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
394 goto cleanup;
395 }
396
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200397 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, P, 1));
398 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&L, DP, D));
399 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&L, &L, &K));
Chris Jones66a4cd42021-03-09 16:04:12 +0000400
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200401 if (mbedtls_mpi_cmp_int(&L, 0) != 0) {
Chris Jones66a4cd42021-03-09 16:04:12 +0000402 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
403 goto cleanup;
404 }
Hanno Beckera565f542017-10-11 11:00:19 +0100405 }
406
Chris Jones66a4cd42021-03-09 16:04:12 +0000407 /* Check that DQ - D == 0 mod Q - 1 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200408 if (DQ != NULL) {
409 if (Q == NULL) {
Chris Jones66a4cd42021-03-09 16:04:12 +0000410 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
411 goto cleanup;
412 }
413
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200414 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, Q, 1));
415 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&L, DQ, D));
416 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&L, &L, &K));
Chris Jones66a4cd42021-03-09 16:04:12 +0000417
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200418 if (mbedtls_mpi_cmp_int(&L, 0) != 0) {
Chris Jones66a4cd42021-03-09 16:04:12 +0000419 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
420 goto cleanup;
421 }
Hanno Beckera565f542017-10-11 11:00:19 +0100422 }
423
Chris Jones66a4cd42021-03-09 16:04:12 +0000424 /* Check that QP * Q - 1 == 0 mod P */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200425 if (QP != NULL) {
426 if (P == NULL || Q == NULL) {
Chris Jones66a4cd42021-03-09 16:04:12 +0000427 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
428 goto cleanup;
429 }
430
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200431 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, QP, Q));
432 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, &K, 1));
433 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&K, &K, P));
434 if (mbedtls_mpi_cmp_int(&K, 0) != 0) {
Chris Jones66a4cd42021-03-09 16:04:12 +0000435 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
436 goto cleanup;
437 }
Hanno Beckera565f542017-10-11 11:00:19 +0100438 }
439
440cleanup:
Chris Jones66a4cd42021-03-09 16:04:12 +0000441
442 /* Wrap MPI error codes by RSA check failure error code */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200443 if (ret != 0 && ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED &&
444 ret != MBEDTLS_ERR_RSA_BAD_INPUT_DATA) {
Chris Jones66a4cd42021-03-09 16:04:12 +0000445 ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
446 }
447
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200448 mbedtls_mpi_free(&K);
449 mbedtls_mpi_free(&L);
Hanno Beckera565f542017-10-11 11:00:19 +0100450
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200451 return ret;
Hanno Beckera565f542017-10-11 11:00:19 +0100452}
453
454#endif /* MBEDTLS_RSA_C */