Coverage Report

Created: 2025-07-01 06:54

/work/mbedtls-2.28.8/library/rsa_internal.c
Line
Count
Source (jump to first uncovered line)
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 "common.h"
10
11
#if defined(MBEDTLS_RSA_C)
12
13
#include "mbedtls/rsa.h"
14
#include "mbedtls/bignum.h"
15
#include "mbedtls/rsa_internal.h"
16
17
/*
18
 * Compute RSA prime factors from public and private exponents
19
 *
20
 * Summary of algorithm:
21
 * Setting F := lcm(P-1,Q-1), the idea is as follows:
22
 *
23
 * (a) For any 1 <= X < N with gcd(X,N)=1, we have X^F = 1 modulo N, so X^(F/2)
24
 *     is a square root of 1 in Z/NZ. Since Z/NZ ~= Z/PZ x Z/QZ by CRT and the
25
 *     square roots of 1 in Z/PZ and Z/QZ are +1 and -1, this leaves the four
26
 *     possibilities X^(F/2) = (+-1, +-1). If it happens that X^(F/2) = (-1,+1)
27
 *     or (+1,-1), then gcd(X^(F/2) + 1, N) will be equal to one of the prime
28
 *     factors of N.
29
 *
30
 * (b) If we don't know F/2 but (F/2) * K for some odd (!) K, then the same
31
 *     construction still applies since (-)^K is the identity on the set of
32
 *     roots of 1 in Z/NZ.
33
 *
34
 * The public and private key primitives (-)^E and (-)^D are mutually inverse
35
 * bijections on Z/NZ if and only if (-)^(DE) is the identity on Z/NZ, i.e.
36
 * if and only if DE - 1 is a multiple of F, say DE - 1 = F * L.
37
 * Splitting L = 2^t * K with K odd, we have
38
 *
39
 *   DE - 1 = FL = (F/2) * (2^(t+1)) * K,
40
 *
41
 * so (F / 2) * K is among the numbers
42
 *
43
 *   (DE - 1) >> 1, (DE - 1) >> 2, ..., (DE - 1) >> ord
44
 *
45
 * where ord is the order of 2 in (DE - 1).
46
 * We can therefore iterate through these numbers apply the construction
47
 * of (a) and (b) above to attempt to factor N.
48
 *
49
 */
50
int mbedtls_rsa_deduce_primes(mbedtls_mpi const *N,
51
                              mbedtls_mpi const *E, mbedtls_mpi const *D,
52
                              mbedtls_mpi *P, mbedtls_mpi *Q)
53
0
{
54
0
    int ret = 0;
55
56
0
    uint16_t attempt;  /* Number of current attempt  */
57
0
    uint16_t iter;     /* Number of squares computed in the current attempt */
58
59
0
    uint16_t order;    /* Order of 2 in DE - 1 */
60
61
0
    mbedtls_mpi T;  /* Holds largest odd divisor of DE - 1     */
62
0
    mbedtls_mpi K;  /* Temporary holding the current candidate */
63
64
0
    const unsigned char primes[] = { 2,
65
0
                                     3,    5,    7,   11,   13,   17,   19,   23,
66
0
                                     29,   31,   37,   41,   43,   47,   53,   59,
67
0
                                     61,   67,   71,   73,   79,   83,   89,   97,
68
0
                                     101,  103,  107,  109,  113,  127,  131,  137,
69
0
                                     139,  149,  151,  157,  163,  167,  173,  179,
70
0
                                     181,  191,  193,  197,  199,  211,  223,  227,
71
0
                                     229,  233,  239,  241,  251 };
72
73
0
    const size_t num_primes = sizeof(primes) / sizeof(*primes);
74
75
0
    if (P == NULL || Q == NULL || P->p != NULL || Q->p != NULL) {
76
0
        return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
77
0
    }
78
79
0
    if (mbedtls_mpi_cmp_int(N, 0) <= 0 ||
80
0
        mbedtls_mpi_cmp_int(D, 1) <= 0 ||
81
0
        mbedtls_mpi_cmp_mpi(D, N) >= 0 ||
82
0
        mbedtls_mpi_cmp_int(E, 1) <= 0 ||
83
0
        mbedtls_mpi_cmp_mpi(E, N) >= 0) {
84
0
        return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
85
0
    }
86
87
    /*
88
     * Initializations and temporary changes
89
     */
90
91
0
    mbedtls_mpi_init(&K);
92
0
    mbedtls_mpi_init(&T);
93
94
    /* T := DE - 1 */
95
0
    MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, D,  E));
96
0
    MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&T, &T, 1));
97
98
0
    if ((order = (uint16_t) mbedtls_mpi_lsb(&T)) == 0) {
99
0
        ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
100
0
        goto cleanup;
101
0
    }
102
103
    /* After this operation, T holds the largest odd divisor of DE - 1. */
104
0
    MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&T, order));
105
106
    /*
107
     * Actual work
108
     */
109
110
    /* Skip trying 2 if N == 1 mod 8 */
111
0
    attempt = 0;
112
0
    if (N->p[0] % 8 == 1) {
113
0
        attempt = 1;
114
0
    }
115
116
0
    for (; attempt < num_primes; ++attempt) {
117
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&K, primes[attempt]));
118
119
        /* Check if gcd(K,N) = 1 */
120
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(P, &K, N));
121
0
        if (mbedtls_mpi_cmp_int(P, 1) != 0) {
122
0
            continue;
123
0
        }
124
125
        /* Go through K^T + 1, K^(2T) + 1, K^(4T) + 1, ...
126
         * and check whether they have nontrivial GCD with N. */
127
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&K, &K, &T, N,
128
0
                                            Q /* temporarily use Q for storing Montgomery
129
0
                                               * multiplication helper values */));
130
131
0
        for (iter = 1; iter <= order; ++iter) {
132
            /* If we reach 1 prematurely, there's no point
133
             * in continuing to square K */
134
0
            if (mbedtls_mpi_cmp_int(&K, 1) == 0) {
135
0
                break;
136
0
            }
137
138
0
            MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&K, &K, 1));
139
0
            MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(P, &K, N));
140
141
0
            if (mbedtls_mpi_cmp_int(P, 1) ==  1 &&
142
0
                mbedtls_mpi_cmp_mpi(P, N) == -1) {
143
                /*
144
                 * Have found a nontrivial divisor P of N.
145
                 * Set Q := N / P.
146
                 */
147
148
0
                MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(Q, NULL, N, P));
149
0
                goto cleanup;
150
0
            }
151
152
0
            MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, &K, 1));
153
0
            MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, &K, &K));
154
0
            MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&K, &K, N));
155
0
        }
156
157
        /*
158
         * If we get here, then either we prematurely aborted the loop because
159
         * we reached 1, or K holds primes[attempt]^(DE - 1) mod N, which must
160
         * be 1 if D,E,N were consistent.
161
         * Check if that's the case and abort if not, to avoid very long,
162
         * yet eventually failing, computations if N,D,E were not sane.
163
         */
164
0
        if (mbedtls_mpi_cmp_int(&K, 1) != 0) {
165
0
            break;
166
0
        }
167
0
    }
168
169
0
    ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
170
171
0
cleanup:
172
173
0
    mbedtls_mpi_free(&K);
174
0
    mbedtls_mpi_free(&T);
175
0
    return ret;
176
0
}
177
178
/*
179
 * Given P, Q and the public exponent E, deduce D.
180
 * This is essentially a modular inversion.
181
 */
182
int mbedtls_rsa_deduce_private_exponent(mbedtls_mpi const *P,
183
                                        mbedtls_mpi const *Q,
184
                                        mbedtls_mpi const *E,
185
                                        mbedtls_mpi *D)
186
0
{
187
0
    int ret = 0;
188
0
    mbedtls_mpi K, L;
189
190
0
    if (D == NULL || mbedtls_mpi_cmp_int(D, 0) != 0) {
191
0
        return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
192
0
    }
193
194
0
    if (mbedtls_mpi_cmp_int(P, 1) <= 0 ||
195
0
        mbedtls_mpi_cmp_int(Q, 1) <= 0 ||
196
0
        mbedtls_mpi_cmp_int(E, 0) == 0) {
197
0
        return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
198
0
    }
199
200
0
    mbedtls_mpi_init(&K);
201
0
    mbedtls_mpi_init(&L);
202
203
    /* Temporarily put K := P-1 and L := Q-1 */
204
0
    MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, P, 1));
205
0
    MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&L, Q, 1));
206
207
    /* Temporarily put D := gcd(P-1, Q-1) */
208
0
    MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(D, &K, &L));
209
210
    /* K := LCM(P-1, Q-1) */
211
0
    MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, &K, &L));
212
0
    MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&K, NULL, &K, D));
213
214
    /* Compute modular inverse of E in LCM(P-1, Q-1) */
215
0
    MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(D, E, &K));
216
217
0
cleanup:
218
219
0
    mbedtls_mpi_free(&K);
220
0
    mbedtls_mpi_free(&L);
221
222
0
    return ret;
223
0
}
224
225
/*
226
 * Check that RSA CRT parameters are in accordance with core parameters.
227
 */
228
int mbedtls_rsa_validate_crt(const mbedtls_mpi *P,  const mbedtls_mpi *Q,
229
                             const mbedtls_mpi *D,  const mbedtls_mpi *DP,
230
                             const mbedtls_mpi *DQ, const mbedtls_mpi *QP)
231
0
{
232
0
    int ret = 0;
233
234
0
    mbedtls_mpi K, L;
235
0
    mbedtls_mpi_init(&K);
236
0
    mbedtls_mpi_init(&L);
237
238
    /* Check that DP - D == 0 mod P - 1 */
239
0
    if (DP != NULL) {
240
0
        if (P == NULL) {
241
0
            ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
242
0
            goto cleanup;
243
0
        }
244
245
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, P, 1));
246
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&L, DP, D));
247
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&L, &L, &K));
248
249
0
        if (mbedtls_mpi_cmp_int(&L, 0) != 0) {
250
0
            ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
251
0
            goto cleanup;
252
0
        }
253
0
    }
254
255
    /* Check that DQ - D == 0 mod Q - 1 */
256
0
    if (DQ != NULL) {
257
0
        if (Q == NULL) {
258
0
            ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
259
0
            goto cleanup;
260
0
        }
261
262
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, Q, 1));
263
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&L, DQ, D));
264
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&L, &L, &K));
265
266
0
        if (mbedtls_mpi_cmp_int(&L, 0) != 0) {
267
0
            ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
268
0
            goto cleanup;
269
0
        }
270
0
    }
271
272
    /* Check that QP * Q - 1 == 0 mod P */
273
0
    if (QP != NULL) {
274
0
        if (P == NULL || Q == NULL) {
275
0
            ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
276
0
            goto cleanup;
277
0
        }
278
279
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, QP, Q));
280
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, &K, 1));
281
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&K, &K, P));
282
0
        if (mbedtls_mpi_cmp_int(&K, 0) != 0) {
283
0
            ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
284
0
            goto cleanup;
285
0
        }
286
0
    }
287
288
0
cleanup:
289
290
    /* Wrap MPI error codes by RSA check failure error code */
291
0
    if (ret != 0 &&
292
0
        ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED &&
293
0
        ret != MBEDTLS_ERR_RSA_BAD_INPUT_DATA) {
294
0
        ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
295
0
    }
296
297
0
    mbedtls_mpi_free(&K);
298
0
    mbedtls_mpi_free(&L);
299
300
0
    return ret;
301
0
}
302
303
/*
304
 * Check that core RSA parameters are sane.
305
 */
306
int mbedtls_rsa_validate_params(const mbedtls_mpi *N, const mbedtls_mpi *P,
307
                                const mbedtls_mpi *Q, const mbedtls_mpi *D,
308
                                const mbedtls_mpi *E,
309
                                int (*f_rng)(void *, unsigned char *, size_t),
310
                                void *p_rng)
311
0
{
312
0
    int ret = 0;
313
0
    mbedtls_mpi K, L;
314
315
0
    mbedtls_mpi_init(&K);
316
0
    mbedtls_mpi_init(&L);
317
318
    /*
319
     * Step 1: If PRNG provided, check that P and Q are prime
320
     */
321
322
0
#if defined(MBEDTLS_GENPRIME)
323
    /*
324
     * When generating keys, the strongest security we support aims for an error
325
     * rate of at most 2^-100 and we are aiming for the same certainty here as
326
     * well.
327
     */
328
0
    if (f_rng != NULL && P != NULL &&
329
0
        (ret = mbedtls_mpi_is_prime_ext(P, 50, f_rng, p_rng)) != 0) {
330
0
        ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
331
0
        goto cleanup;
332
0
    }
333
334
0
    if (f_rng != NULL && Q != NULL &&
335
0
        (ret = mbedtls_mpi_is_prime_ext(Q, 50, f_rng, p_rng)) != 0) {
336
0
        ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
337
0
        goto cleanup;
338
0
    }
339
#else
340
    ((void) f_rng);
341
    ((void) p_rng);
342
#endif /* MBEDTLS_GENPRIME */
343
344
    /*
345
     * Step 2: Check that 1 < N = P * Q
346
     */
347
348
0
    if (P != NULL && Q != NULL && N != NULL) {
349
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, P, Q));
350
0
        if (mbedtls_mpi_cmp_int(N, 1)  <= 0 ||
351
0
            mbedtls_mpi_cmp_mpi(&K, N) != 0) {
352
0
            ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
353
0
            goto cleanup;
354
0
        }
355
0
    }
356
357
    /*
358
     * Step 3: Check and 1 < D, E < N if present.
359
     */
360
361
0
    if (N != NULL && D != NULL && E != NULL) {
362
0
        if (mbedtls_mpi_cmp_int(D, 1) <= 0 ||
363
0
            mbedtls_mpi_cmp_int(E, 1) <= 0 ||
364
0
            mbedtls_mpi_cmp_mpi(D, N) >= 0 ||
365
0
            mbedtls_mpi_cmp_mpi(E, N) >= 0) {
366
0
            ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
367
0
            goto cleanup;
368
0
        }
369
0
    }
370
371
    /*
372
     * Step 4: Check that D, E are inverse modulo P-1 and Q-1
373
     */
374
375
0
    if (P != NULL && Q != NULL && D != NULL && E != NULL) {
376
0
        if (mbedtls_mpi_cmp_int(P, 1) <= 0 ||
377
0
            mbedtls_mpi_cmp_int(Q, 1) <= 0) {
378
0
            ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
379
0
            goto cleanup;
380
0
        }
381
382
        /* Compute DE-1 mod P-1 */
383
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, D, E));
384
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, &K, 1));
385
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&L, P, 1));
386
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&K, &K, &L));
387
0
        if (mbedtls_mpi_cmp_int(&K, 0) != 0) {
388
0
            ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
389
0
            goto cleanup;
390
0
        }
391
392
        /* Compute DE-1 mod Q-1 */
393
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, D, E));
394
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, &K, 1));
395
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&L, Q, 1));
396
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&K, &K, &L));
397
0
        if (mbedtls_mpi_cmp_int(&K, 0) != 0) {
398
0
            ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
399
0
            goto cleanup;
400
0
        }
401
0
    }
402
403
0
cleanup:
404
405
0
    mbedtls_mpi_free(&K);
406
0
    mbedtls_mpi_free(&L);
407
408
    /* Wrap MPI error codes by RSA check failure error code */
409
0
    if (ret != 0 && ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED) {
410
0
        ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
411
0
    }
412
413
0
    return ret;
414
0
}
415
416
int mbedtls_rsa_deduce_crt(const mbedtls_mpi *P, const mbedtls_mpi *Q,
417
                           const mbedtls_mpi *D, mbedtls_mpi *DP,
418
                           mbedtls_mpi *DQ, mbedtls_mpi *QP)
419
0
{
420
0
    int ret = 0;
421
0
    mbedtls_mpi K;
422
0
    mbedtls_mpi_init(&K);
423
424
    /* DP = D mod P-1 */
425
0
    if (DP != NULL) {
426
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, P, 1));
427
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(DP, D, &K));
428
0
    }
429
430
    /* DQ = D mod Q-1 */
431
0
    if (DQ != NULL) {
432
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, Q, 1));
433
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(DQ, D, &K));
434
0
    }
435
436
    /* QP = Q^{-1} mod P */
437
0
    if (QP != NULL) {
438
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(QP, Q, P));
439
0
    }
440
441
0
cleanup:
442
0
    mbedtls_mpi_free(&K);
443
444
0
    return ret;
445
0
}
446
447
#endif /* MBEDTLS_RSA_C */