Coverage Report

Created: 2025-06-13 06:58

/src/openssl30/crypto/rsa/rsa_gen.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
/*
11
 * NB: these functions have been "upgraded", the deprecated versions (which
12
 * are compatibility wrappers using these functions) are in rsa_depr.c. -
13
 * Geoff
14
 */
15
16
/*
17
 * RSA low level APIs are deprecated for public use, but still ok for
18
 * internal use.
19
 */
20
#include "internal/deprecated.h"
21
22
#include <stdio.h>
23
#include <time.h>
24
#include "internal/cryptlib.h"
25
#include <openssl/bn.h>
26
#include <openssl/self_test.h>
27
#include "prov/providercommon.h"
28
#include "rsa_local.h"
29
30
static int rsa_keygen_pairwise_test(RSA *rsa, OSSL_CALLBACK *cb, void *cbarg);
31
static int rsa_keygen(OSSL_LIB_CTX *libctx, RSA *rsa, int bits, int primes,
32
                      BIGNUM *e_value, BN_GENCB *cb, int pairwise_test);
33
34
/*
35
 * NB: this wrapper would normally be placed in rsa_lib.c and the static
36
 * implementation would probably be in rsa_eay.c. Nonetheless, is kept here
37
 * so that we don't introduce a new linker dependency. Eg. any application
38
 * that wasn't previously linking object code related to key-generation won't
39
 * have to now just because key-generation is part of RSA_METHOD.
40
 */
41
int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb)
42
0
{
43
0
    if (rsa->meth->rsa_keygen != NULL)
44
0
        return rsa->meth->rsa_keygen(rsa, bits, e_value, cb);
45
46
0
    return RSA_generate_multi_prime_key(rsa, bits, RSA_DEFAULT_PRIME_NUM,
47
0
                                        e_value, cb);
48
0
}
49
50
int RSA_generate_multi_prime_key(RSA *rsa, int bits, int primes,
51
                                 BIGNUM *e_value, BN_GENCB *cb)
52
0
{
53
0
#ifndef FIPS_MODULE
54
    /* multi-prime is only supported with the builtin key generation */
55
0
    if (rsa->meth->rsa_multi_prime_keygen != NULL) {
56
0
        return rsa->meth->rsa_multi_prime_keygen(rsa, bits, primes,
57
0
                                                 e_value, cb);
58
0
    } else if (rsa->meth->rsa_keygen != NULL) {
59
        /*
60
         * However, if rsa->meth implements only rsa_keygen, then we
61
         * have to honour it in 2-prime case and assume that it wouldn't
62
         * know what to do with multi-prime key generated by builtin
63
         * subroutine...
64
         */
65
0
        if (primes == 2)
66
0
            return rsa->meth->rsa_keygen(rsa, bits, e_value, cb);
67
0
        else
68
0
            return 0;
69
0
    }
70
0
#endif /* FIPS_MODULE */
71
0
    return rsa_keygen(rsa->libctx, rsa, bits, primes, e_value, cb, 0);
72
0
}
73
74
#ifndef FIPS_MODULE
75
static int rsa_multiprime_keygen(RSA *rsa, int bits, int primes,
76
                                 BIGNUM *e_value, BN_GENCB *cb)
77
0
{
78
0
    BIGNUM *r0 = NULL, *r1 = NULL, *r2 = NULL, *tmp, *prime;
79
0
    int n = 0, bitsr[RSA_MAX_PRIME_NUM], bitse = 0;
80
0
    int i = 0, quo = 0, rmd = 0, adj = 0, retries = 0;
81
0
    RSA_PRIME_INFO *pinfo = NULL;
82
0
    STACK_OF(RSA_PRIME_INFO) *prime_infos = NULL;
83
0
    BN_CTX *ctx = NULL;
84
0
    BN_ULONG bitst = 0;
85
0
    unsigned long error = 0;
86
0
    int ok = -1;
87
88
0
    if (bits < RSA_MIN_MODULUS_BITS) {
89
0
        ok = 0;             /* we set our own err */
90
0
        ERR_raise(ERR_LIB_RSA, RSA_R_KEY_SIZE_TOO_SMALL);
91
0
        goto err;
92
0
    }
93
94
    /* A bad value for e can cause infinite loops */
95
0
    if (e_value != NULL && !ossl_rsa_check_public_exponent(e_value)) {
96
0
        ERR_raise(ERR_LIB_RSA, RSA_R_PUB_EXPONENT_OUT_OF_RANGE);
97
0
        return 0;
98
0
    }
99
100
0
    if (primes < RSA_DEFAULT_PRIME_NUM || primes > ossl_rsa_multip_cap(bits)) {
101
0
        ok = 0;             /* we set our own err */
102
0
        ERR_raise(ERR_LIB_RSA, RSA_R_KEY_PRIME_NUM_INVALID);
103
0
        goto err;
104
0
    }
105
106
0
    ctx = BN_CTX_new_ex(rsa->libctx);
107
0
    if (ctx == NULL)
108
0
        goto err;
109
0
    BN_CTX_start(ctx);
110
0
    r0 = BN_CTX_get(ctx);
111
0
    r1 = BN_CTX_get(ctx);
112
0
    r2 = BN_CTX_get(ctx);
113
0
    if (r2 == NULL)
114
0
        goto err;
115
116
    /* divide bits into 'primes' pieces evenly */
117
0
    quo = bits / primes;
118
0
    rmd = bits % primes;
119
120
0
    for (i = 0; i < primes; i++)
121
0
        bitsr[i] = (i < rmd) ? quo + 1 : quo;
122
123
0
    rsa->dirty_cnt++;
124
125
    /* We need the RSA components non-NULL */
126
0
    if (!rsa->n && ((rsa->n = BN_new()) == NULL))
127
0
        goto err;
128
0
    if (!rsa->d && ((rsa->d = BN_secure_new()) == NULL))
129
0
        goto err;
130
0
    BN_set_flags(rsa->d, BN_FLG_CONSTTIME);
131
0
    if (!rsa->e && ((rsa->e = BN_new()) == NULL))
132
0
        goto err;
133
0
    if (!rsa->p && ((rsa->p = BN_secure_new()) == NULL))
134
0
        goto err;
135
0
    BN_set_flags(rsa->p, BN_FLG_CONSTTIME);
136
0
    if (!rsa->q && ((rsa->q = BN_secure_new()) == NULL))
137
0
        goto err;
138
0
    BN_set_flags(rsa->q, BN_FLG_CONSTTIME);
139
0
    if (!rsa->dmp1 && ((rsa->dmp1 = BN_secure_new()) == NULL))
140
0
        goto err;
141
0
    BN_set_flags(rsa->dmp1, BN_FLG_CONSTTIME);
142
0
    if (!rsa->dmq1 && ((rsa->dmq1 = BN_secure_new()) == NULL))
143
0
        goto err;
144
0
    BN_set_flags(rsa->dmq1, BN_FLG_CONSTTIME);
145
0
    if (!rsa->iqmp && ((rsa->iqmp = BN_secure_new()) == NULL))
146
0
        goto err;
147
0
    BN_set_flags(rsa->iqmp, BN_FLG_CONSTTIME);
148
149
    /* initialize multi-prime components */
150
0
    if (primes > RSA_DEFAULT_PRIME_NUM) {
151
0
        rsa->version = RSA_ASN1_VERSION_MULTI;
152
0
        prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, primes - 2);
153
0
        if (prime_infos == NULL)
154
0
            goto err;
155
0
        if (rsa->prime_infos != NULL) {
156
            /* could this happen? */
157
0
            sk_RSA_PRIME_INFO_pop_free(rsa->prime_infos,
158
0
                                       ossl_rsa_multip_info_free);
159
0
        }
160
0
        rsa->prime_infos = prime_infos;
161
162
        /* prime_info from 2 to |primes| -1 */
163
0
        for (i = 2; i < primes; i++) {
164
0
            pinfo = ossl_rsa_multip_info_new();
165
0
            if (pinfo == NULL)
166
0
                goto err;
167
0
            (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
168
0
        }
169
0
    }
170
171
0
    if (BN_copy(rsa->e, e_value) == NULL)
172
0
        goto err;
173
174
    /* generate p, q and other primes (if any) */
175
0
    for (i = 0; i < primes; i++) {
176
0
        adj = 0;
177
0
        retries = 0;
178
179
0
        if (i == 0) {
180
0
            prime = rsa->p;
181
0
        } else if (i == 1) {
182
0
            prime = rsa->q;
183
0
        } else {
184
0
            pinfo = sk_RSA_PRIME_INFO_value(prime_infos, i - 2);
185
0
            prime = pinfo->r;
186
0
        }
187
0
        BN_set_flags(prime, BN_FLG_CONSTTIME);
188
189
0
        for (;;) {
190
0
 redo:
191
0
            if (!BN_generate_prime_ex2(prime, bitsr[i] + adj, 0, NULL, NULL,
192
0
                                       cb, ctx))
193
0
                goto err;
194
            /*
195
             * prime should not be equal to p, q, r_3...
196
             * (those primes prior to this one)
197
             */
198
0
            {
199
0
                int j;
200
201
0
                for (j = 0; j < i; j++) {
202
0
                    BIGNUM *prev_prime;
203
204
0
                    if (j == 0)
205
0
                        prev_prime = rsa->p;
206
0
                    else if (j == 1)
207
0
                        prev_prime = rsa->q;
208
0
                    else
209
0
                        prev_prime = sk_RSA_PRIME_INFO_value(prime_infos,
210
0
                                                             j - 2)->r;
211
212
0
                    if (!BN_cmp(prime, prev_prime)) {
213
0
                        goto redo;
214
0
                    }
215
0
                }
216
0
            }
217
0
            if (!BN_sub(r2, prime, BN_value_one()))
218
0
                goto err;
219
0
            ERR_set_mark();
220
0
            BN_set_flags(r2, BN_FLG_CONSTTIME);
221
0
            if (BN_mod_inverse(r1, r2, rsa->e, ctx) != NULL) {
222
               /* GCD == 1 since inverse exists */
223
0
                break;
224
0
            }
225
0
            error = ERR_peek_last_error();
226
0
            if (ERR_GET_LIB(error) == ERR_LIB_BN
227
0
                && ERR_GET_REASON(error) == BN_R_NO_INVERSE) {
228
                /* GCD != 1 */
229
0
                ERR_pop_to_mark();
230
0
            } else {
231
0
                goto err;
232
0
            }
233
0
            if (!BN_GENCB_call(cb, 2, n++))
234
0
                goto err;
235
0
        }
236
237
0
        bitse += bitsr[i];
238
239
        /* calculate n immediately to see if it's sufficient */
240
0
        if (i == 1) {
241
            /* we get at least 2 primes */
242
0
            if (!BN_mul(r1, rsa->p, rsa->q, ctx))
243
0
                goto err;
244
0
        } else if (i != 0) {
245
            /* modulus n = p * q * r_3 * r_4 ... */
246
0
            if (!BN_mul(r1, rsa->n, prime, ctx))
247
0
                goto err;
248
0
        } else {
249
            /* i == 0, do nothing */
250
0
            if (!BN_GENCB_call(cb, 3, i))
251
0
                goto err;
252
0
            continue;
253
0
        }
254
        /*
255
         * if |r1|, product of factors so far, is not as long as expected
256
         * (by checking the first 4 bits are less than 0x9 or greater than
257
         * 0xF). If so, re-generate the last prime.
258
         *
259
         * NOTE: This actually can't happen in two-prime case, because of
260
         * the way factors are generated.
261
         *
262
         * Besides, another consideration is, for multi-prime case, even the
263
         * length modulus is as long as expected, the modulus could start at
264
         * 0x8, which could be utilized to distinguish a multi-prime private
265
         * key by using the modulus in a certificate. This is also covered
266
         * by checking the length should not be less than 0x9.
267
         */
268
0
        if (!BN_rshift(r2, r1, bitse - 4))
269
0
            goto err;
270
0
        bitst = BN_get_word(r2);
271
272
0
        if (bitst < 0x9 || bitst > 0xF) {
273
            /*
274
             * For keys with more than 4 primes, we attempt longer factor to
275
             * meet length requirement.
276
             *
277
             * Otherwise, we just re-generate the prime with the same length.
278
             *
279
             * This strategy has the following goals:
280
             *
281
             * 1. 1024-bit factors are efficient when using 3072 and 4096-bit key
282
             * 2. stay the same logic with normal 2-prime key
283
             */
284
0
            bitse -= bitsr[i];
285
0
            if (!BN_GENCB_call(cb, 2, n++))
286
0
                goto err;
287
0
            if (primes > 4) {
288
0
                if (bitst < 0x9)
289
0
                    adj++;
290
0
                else
291
0
                    adj--;
292
0
            } else if (retries == 4) {
293
                /*
294
                 * re-generate all primes from scratch, mainly used
295
                 * in 4 prime case to avoid long loop. Max retry times
296
                 * is set to 4.
297
                 */
298
0
                i = -1;
299
0
                bitse = 0;
300
0
                continue;
301
0
            }
302
0
            retries++;
303
0
            goto redo;
304
0
        }
305
        /* save product of primes for further use, for multi-prime only */
306
0
        if (i > 1 && BN_copy(pinfo->pp, rsa->n) == NULL)
307
0
            goto err;
308
0
        if (BN_copy(rsa->n, r1) == NULL)
309
0
            goto err;
310
0
        if (!BN_GENCB_call(cb, 3, i))
311
0
            goto err;
312
0
    }
313
314
0
    if (BN_cmp(rsa->p, rsa->q) < 0) {
315
0
        tmp = rsa->p;
316
0
        rsa->p = rsa->q;
317
0
        rsa->q = tmp;
318
0
    }
319
320
    /* calculate d */
321
322
    /* p - 1 */
323
0
    if (!BN_sub(r1, rsa->p, BN_value_one()))
324
0
        goto err;
325
    /* q - 1 */
326
0
    if (!BN_sub(r2, rsa->q, BN_value_one()))
327
0
        goto err;
328
    /* (p - 1)(q - 1) */
329
0
    if (!BN_mul(r0, r1, r2, ctx))
330
0
        goto err;
331
    /* multi-prime */
332
0
    for (i = 2; i < primes; i++) {
333
0
        pinfo = sk_RSA_PRIME_INFO_value(prime_infos, i - 2);
334
        /* save r_i - 1 to pinfo->d temporarily */
335
0
        if (!BN_sub(pinfo->d, pinfo->r, BN_value_one()))
336
0
            goto err;
337
0
        if (!BN_mul(r0, r0, pinfo->d, ctx))
338
0
            goto err;
339
0
    }
340
341
0
    {
342
0
        BIGNUM *pr0 = BN_new();
343
344
0
        if (pr0 == NULL)
345
0
            goto err;
346
347
0
        BN_with_flags(pr0, r0, BN_FLG_CONSTTIME);
348
0
        if (!BN_mod_inverse(rsa->d, rsa->e, pr0, ctx)) {
349
0
            BN_free(pr0);
350
0
            goto err;               /* d */
351
0
        }
352
        /* We MUST free pr0 before any further use of r0 */
353
0
        BN_free(pr0);
354
0
    }
355
356
0
    {
357
0
        BIGNUM *d = BN_new();
358
359
0
        if (d == NULL)
360
0
            goto err;
361
362
0
        BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
363
364
        /* calculate d mod (p-1) and d mod (q - 1) */
365
0
        if (!BN_mod(rsa->dmp1, d, r1, ctx)
366
0
            || !BN_mod(rsa->dmq1, d, r2, ctx)) {
367
0
            BN_free(d);
368
0
            goto err;
369
0
        }
370
371
        /* calculate CRT exponents */
372
0
        for (i = 2; i < primes; i++) {
373
0
            pinfo = sk_RSA_PRIME_INFO_value(prime_infos, i - 2);
374
            /* pinfo->d == r_i - 1 */
375
0
            if (!BN_mod(pinfo->d, d, pinfo->d, ctx)) {
376
0
                BN_free(d);
377
0
                goto err;
378
0
            }
379
0
        }
380
381
        /* We MUST free d before any further use of rsa->d */
382
0
        BN_free(d);
383
0
    }
384
385
0
    {
386
0
        BIGNUM *p = BN_new();
387
388
0
        if (p == NULL)
389
0
            goto err;
390
0
        BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
391
392
        /* calculate inverse of q mod p */
393
0
        if (!BN_mod_inverse(rsa->iqmp, rsa->q, p, ctx)) {
394
0
            BN_free(p);
395
0
            goto err;
396
0
        }
397
398
        /* calculate CRT coefficient for other primes */
399
0
        for (i = 2; i < primes; i++) {
400
0
            pinfo = sk_RSA_PRIME_INFO_value(prime_infos, i - 2);
401
0
            BN_with_flags(p, pinfo->r, BN_FLG_CONSTTIME);
402
0
            if (!BN_mod_inverse(pinfo->t, pinfo->pp, p, ctx)) {
403
0
                BN_free(p);
404
0
                goto err;
405
0
            }
406
0
        }
407
408
        /* We MUST free p before any further use of rsa->p */
409
0
        BN_free(p);
410
0
    }
411
412
0
    ok = 1;
413
0
 err:
414
0
    if (ok == -1) {
415
0
        ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
416
0
        ok = 0;
417
0
    }
418
0
    BN_CTX_end(ctx);
419
0
    BN_CTX_free(ctx);
420
0
    return ok;
421
0
}
422
#endif /* FIPS_MODULE */
423
424
static int rsa_keygen(OSSL_LIB_CTX *libctx, RSA *rsa, int bits, int primes,
425
                      BIGNUM *e_value, BN_GENCB *cb, int pairwise_test)
426
0
{
427
0
    int ok = 0;
428
429
#ifdef FIPS_MODULE
430
    ok = ossl_rsa_sp800_56b_generate_key(rsa, bits, e_value, cb);
431
    pairwise_test = 1; /* FIPS MODE needs to always run the pairwise test */
432
#else
433
    /*
434
     * Only multi-prime keys or insecure keys with a small key length or a
435
     * public exponent <= 2^16 will use the older rsa_multiprime_keygen().
436
     */
437
0
    if (primes == 2
438
0
            && bits >= 2048
439
0
            && (e_value == NULL || BN_num_bits(e_value) > 16))
440
0
        ok = ossl_rsa_sp800_56b_generate_key(rsa, bits, e_value, cb);
441
0
    else
442
0
        ok = rsa_multiprime_keygen(rsa, bits, primes, e_value, cb);
443
0
#endif /* FIPS_MODULE */
444
445
0
    if (pairwise_test && ok > 0) {
446
0
        OSSL_CALLBACK *stcb = NULL;
447
0
        void *stcbarg = NULL;
448
449
0
        OSSL_SELF_TEST_get_callback(libctx, &stcb, &stcbarg);
450
0
        ok = rsa_keygen_pairwise_test(rsa, stcb, stcbarg);
451
0
        if (!ok) {
452
0
            ossl_set_error_state(OSSL_SELF_TEST_TYPE_PCT);
453
            /* Clear intermediate results */
454
0
            BN_clear_free(rsa->d);
455
0
            BN_clear_free(rsa->p);
456
0
            BN_clear_free(rsa->q);
457
0
            BN_clear_free(rsa->dmp1);
458
0
            BN_clear_free(rsa->dmq1);
459
0
            BN_clear_free(rsa->iqmp);
460
0
            rsa->d = NULL;
461
0
            rsa->p = NULL;
462
0
            rsa->q = NULL;
463
0
            rsa->dmp1 = NULL;
464
0
            rsa->dmq1 = NULL;
465
0
            rsa->iqmp = NULL;
466
0
        }
467
0
    }
468
0
    return ok;
469
0
}
470
471
/*
472
 * For RSA key generation it is not known whether the key pair will be used
473
 * for key transport or signatures. FIPS 140-2 IG 9.9 states that in this case
474
 * either a signature verification OR an encryption operation may be used to
475
 * perform the pairwise consistency check. The simpler encrypt/decrypt operation
476
 * has been chosen for this case.
477
 */
478
static int rsa_keygen_pairwise_test(RSA *rsa, OSSL_CALLBACK *cb, void *cbarg)
479
0
{
480
0
    int ret = 0;
481
0
    unsigned int ciphertxt_len;
482
0
    unsigned char *ciphertxt = NULL;
483
0
    const unsigned char plaintxt[16] = {0};
484
0
    unsigned char *decoded = NULL;
485
0
    unsigned int decoded_len;
486
0
    unsigned int plaintxt_len = (unsigned int)sizeof(plaintxt_len);
487
0
    int padding = RSA_PKCS1_PADDING;
488
0
    OSSL_SELF_TEST *st = NULL;
489
490
0
    st = OSSL_SELF_TEST_new(cb, cbarg);
491
0
    if (st == NULL)
492
0
        goto err;
493
0
    OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_PCT,
494
0
                           OSSL_SELF_TEST_DESC_PCT_RSA_PKCS1);
495
496
0
    ciphertxt_len = RSA_size(rsa);
497
    /*
498
     * RSA_private_encrypt() and RSA_private_decrypt() requires the 'to'
499
     * parameter to be a maximum of RSA_size() - allocate space for both.
500
     */
501
0
    ciphertxt = OPENSSL_zalloc(ciphertxt_len * 2);
502
0
    if (ciphertxt == NULL)
503
0
        goto err;
504
0
    decoded = ciphertxt + ciphertxt_len;
505
506
0
    ciphertxt_len = RSA_public_encrypt(plaintxt_len, plaintxt, ciphertxt, rsa,
507
0
                                       padding);
508
0
    if (ciphertxt_len <= 0)
509
0
        goto err;
510
0
    if (ciphertxt_len == plaintxt_len
511
0
        && memcmp(ciphertxt, plaintxt, plaintxt_len) == 0)
512
0
        goto err;
513
514
0
    OSSL_SELF_TEST_oncorrupt_byte(st, ciphertxt);
515
516
0
    decoded_len = RSA_private_decrypt(ciphertxt_len, ciphertxt, decoded, rsa,
517
0
                                      padding);
518
0
    if (decoded_len != plaintxt_len
519
0
        || memcmp(decoded, plaintxt,  decoded_len) != 0)
520
0
        goto err;
521
522
0
    ret = 1;
523
0
err:
524
0
    OSSL_SELF_TEST_onend(st, ret);
525
0
    OSSL_SELF_TEST_free(st);
526
0
    OPENSSL_free(ciphertxt);
527
528
0
    return ret;
529
0
}