Skip to content
0.2.0-dev Download ZIP
294 lines ·8.1 KB Raw
1/*
2 * Helper functions for the RSA module
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 */
8
9#include "tf_psa_crypto_common.h"
10
11#if defined(MBEDTLS_RSA_C)
12
13#include "mbedtls/private/rsa.h"
14#include "mbedtls/private/bignum.h"
15#include "bignum_internal.h"
16#include "rsa_alt_helpers.h"
17
18/*
19 * Given P, Q and the public exponent E, deduce D.
20 * This is essentially a modular inversion.
21 */
22int mbedtls_rsa_deduce_private_exponent(mbedtls_mpi const *P,
23 mbedtls_mpi const *Q,
24 mbedtls_mpi const *E,
25 mbedtls_mpi *D)
26{
27 int ret = 0;
28 mbedtls_mpi K, L;
29
30 if (D == NULL) {
31 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
32 }
33
34 if (mbedtls_mpi_cmp_int(P, 1) <= 0 ||
35 mbedtls_mpi_cmp_int(Q, 1) <= 0 ||
36 mbedtls_mpi_cmp_int(E, 0) == 0) {
37 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
38 }
39
40 if (mbedtls_mpi_get_bit(E, 0) != 1) {
41 return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
42 }
43
44 mbedtls_mpi_init(&K);
45 mbedtls_mpi_init(&L);
46
47 /* Temporarily put K := P-1 and L := Q-1 */
48 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, P, 1));
49 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&L, Q, 1));
50
51 /* Temporarily put D := gcd(P-1, Q-1) */
52 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(D, &K, &L));
53
54 /* K := LCM(P-1, Q-1) */
55 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, &K, &L));
56 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&K, NULL, &K, D));
57
58 /* Compute modular inverse of E mod LCM(P-1, Q-1)
59 * This is FIPS 186-4 §B.3.1 criterion 3(b).
60 * This will return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if E is not coprime to
61 * (P-1)(Q-1), also validating FIPS 186-4 §B.3.1 criterion 2(a). */
62 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod_even_in_range(D, E, &K));
63
64cleanup:
65
66 mbedtls_mpi_free(&K);
67 mbedtls_mpi_free(&L);
68
69 return ret;
70}
71
72int mbedtls_rsa_deduce_crt(const mbedtls_mpi *P, const mbedtls_mpi *Q,
73 const mbedtls_mpi *D, mbedtls_mpi *DP,
74 mbedtls_mpi *DQ, mbedtls_mpi *QP)
75{
76 int ret = 0;
77 mbedtls_mpi K;
78 mbedtls_mpi_init(&K);
79
80 /* DP = D mod P-1 */
81 if (DP != NULL) {
82 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, P, 1));
83 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(DP, D, &K));
84 }
85
86 /* DQ = D mod Q-1 */
87 if (DQ != NULL) {
88 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, Q, 1));
89 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(DQ, D, &K));
90 }
91
92 /* QP = Q^{-1} mod P */
93 if (QP != NULL) {
94 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod_odd(QP, Q, P));
95 }
96
97cleanup:
98 mbedtls_mpi_free(&K);
99
100 return ret;
101}
102
103/*
104 * Check that core RSA parameters are sane.
105 */
106int mbedtls_rsa_validate_params(const mbedtls_mpi *N, const mbedtls_mpi *P,
107 const mbedtls_mpi *Q, const mbedtls_mpi *D,
108 const mbedtls_mpi *E,
109 int (*f_rng)(void *, unsigned char *, size_t),
110 void *p_rng)
111{
112 int ret = 0;
113 mbedtls_mpi K, L;
114
115 mbedtls_mpi_init(&K);
116 mbedtls_mpi_init(&L);
117
118 /*
119 * Step 1: If PRNG provided, check that P and Q are prime
120 */
121
122#if defined(MBEDTLS_GENPRIME)
123 /*
124 * When generating keys, the strongest security we support aims for an error
125 * rate of at most 2^-100 and we are aiming for the same certainty here as
126 * well.
127 */
128 if (f_rng != NULL && P != NULL &&
129 (ret = mbedtls_mpi_is_prime_ext(P, 50, f_rng, p_rng)) != 0) {
130 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
131 goto cleanup;
132 }
133
134 if (f_rng != NULL && Q != NULL &&
135 (ret = mbedtls_mpi_is_prime_ext(Q, 50, f_rng, p_rng)) != 0) {
136 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
137 goto cleanup;
138 }
139#else
140 ((void) f_rng);
141 ((void) p_rng);
142#endif /* MBEDTLS_GENPRIME */
143
144 /*
145 * Step 2: Check that 1 < N = P * Q
146 */
147
148 if (P != NULL && Q != NULL && N != NULL) {
149 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, P, Q));
150 if (mbedtls_mpi_cmp_int(N, 1) <= 0 ||
151 mbedtls_mpi_cmp_mpi(&K, N) != 0) {
152 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
153 goto cleanup;
154 }
155 }
156
157 /*
158 * Step 3: Check and 1 < D, E < N if present.
159 */
160
161 if (N != NULL && D != NULL && E != NULL) {
162 if (mbedtls_mpi_cmp_int(D, 1) <= 0 ||
163 mbedtls_mpi_cmp_int(E, 1) <= 0 ||
164 mbedtls_mpi_cmp_mpi(D, N) >= 0 ||
165 mbedtls_mpi_cmp_mpi(E, N) >= 0) {
166 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
167 goto cleanup;
168 }
169 }
170
171 /*
172 * Step 4: Check that D, E are inverse modulo P-1 and Q-1
173 */
174
175 if (P != NULL && Q != NULL && D != NULL && E != NULL) {
176 if (mbedtls_mpi_cmp_int(P, 1) <= 0 ||
177 mbedtls_mpi_cmp_int(Q, 1) <= 0) {
178 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
179 goto cleanup;
180 }
181
182 /* Compute DE-1 mod P-1 */
183 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, D, E));
184 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, &K, 1));
185 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&L, P, 1));
186 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&K, &K, &L));
187 if (mbedtls_mpi_cmp_int(&K, 0) != 0) {
188 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
189 goto cleanup;
190 }
191
192 /* Compute DE-1 mod Q-1 */
193 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, D, E));
194 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, &K, 1));
195 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&L, Q, 1));
196 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&K, &K, &L));
197 if (mbedtls_mpi_cmp_int(&K, 0) != 0) {
198 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
199 goto cleanup;
200 }
201 }
202
203cleanup:
204
205 mbedtls_mpi_free(&K);
206 mbedtls_mpi_free(&L);
207
208 /* Wrap MPI error codes by RSA check failure error code */
209 if (ret != 0 && ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED) {
210 ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
211 }
212
213 return ret;
214}
215
216/*
217 * Check that RSA CRT parameters are in accordance with core parameters.
218 */
219int mbedtls_rsa_validate_crt(const mbedtls_mpi *P, const mbedtls_mpi *Q,
220 const mbedtls_mpi *D, const mbedtls_mpi *DP,
221 const mbedtls_mpi *DQ, const mbedtls_mpi *QP)
222{
223 int ret = 0;
224
225 mbedtls_mpi K, L;
226 mbedtls_mpi_init(&K);
227 mbedtls_mpi_init(&L);
228
229 /* Check that DP - D == 0 mod P - 1 */
230 if (DP != NULL) {
231 if (P == NULL) {
232 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
233 goto cleanup;
234 }
235
236 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, P, 1));
237 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&L, DP, D));
238 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&L, &L, &K));
239
240 if (mbedtls_mpi_cmp_int(&L, 0) != 0) {
241 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
242 goto cleanup;
243 }
244 }
245
246 /* Check that DQ - D == 0 mod Q - 1 */
247 if (DQ != NULL) {
248 if (Q == NULL) {
249 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
250 goto cleanup;
251 }
252
253 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, Q, 1));
254 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&L, DQ, D));
255 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&L, &L, &K));
256
257 if (mbedtls_mpi_cmp_int(&L, 0) != 0) {
258 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
259 goto cleanup;
260 }
261 }
262
263 /* Check that QP * Q - 1 == 0 mod P */
264 if (QP != NULL) {
265 if (P == NULL || Q == NULL) {
266 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
267 goto cleanup;
268 }
269
270 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, QP, Q));
271 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, &K, 1));
272 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&K, &K, P));
273 if (mbedtls_mpi_cmp_int(&K, 0) != 0) {
274 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
275 goto cleanup;
276 }
277 }
278
279cleanup:
280
281 /* Wrap MPI error codes by RSA check failure error code */
282 if (ret != 0 &&
283 ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED &&
284 ret != MBEDTLS_ERR_RSA_BAD_INPUT_DATA) {
285 ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
286 }
287
288 mbedtls_mpi_free(&K);
289 mbedtls_mpi_free(&L);
290
291 return ret;
292}
293
294#endif /* MBEDTLS_RSA_C */