Coverage Report

Created: 2025-11-25 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/rsa/rsa_sp800_56b_gen.c
Line
Count
Source
1
/*
2
 * Copyright 2018-2024 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright (c) 2018-2019, Oracle and/or its affiliates.  All rights reserved.
4
 *
5
 * Licensed under the Apache License 2.0 (the "License").  You may not use
6
 * this file except in compliance with the License.  You can obtain a copy
7
 * in the file LICENSE in the source distribution or at
8
 * https://www.openssl.org/source/license.html
9
 */
10
11
#include <openssl/err.h>
12
#include <openssl/bn.h>
13
#include <openssl/core.h>
14
#include <openssl/evp.h>
15
#include <openssl/rand.h>
16
#include "crypto/bn.h"
17
#include "crypto/security_bits.h"
18
#include "rsa_local.h"
19
20
0
#define RSA_FIPS186_5_MIN_KEYGEN_KEYSIZE 2048
21
#define RSA_FIPS186_5_MIN_KEYGEN_STRENGTH 112
22
23
/*
24
 * Generate probable primes 'p' & 'q'. See FIPS 186-5 Section A.1.6
25
 * "Generation of Probable Primes with Conditions Based on Auxiliary Probable
26
 * Primes".
27
 *
28
 * Params:
29
 *     rsa  Object used to store primes p & q.
30
 *     test Object used for CAVS testing only.that contains..
31
 *       p1, p2 The returned auxiliary primes for p.
32
 *              If NULL they are not returned.
33
 *       Xp An optional passed in value (that is random number used during
34
 *          generation of p).
35
 *       Xp1, Xp2 Optionally passed in randomly generated numbers from which
36
 *                auxiliary primes p1 & p2 are calculated. If NULL these values
37
 *                are generated internally.
38
 *       q1, q2 The returned auxiliary primes for q.
39
 *              If NULL they are not returned.
40
 *       Xq An optional passed in value (that is random number used during
41
 *          generation of q).
42
 *       Xq1, Xq2 Optionally passed in randomly generated numbers from which
43
 *                auxiliary primes q1 & q2 are calculated. If NULL these values
44
 *                are generated internally.
45
 *     nbits The key size in bits (The size of the modulus n).
46
 *     e The public exponent.
47
 *     ctx A BN_CTX object.
48
 *     cb An optional BIGNUM callback.
49
 *     a An optional number with a value of 0, 1, 3, 5 or 7 that may be used
50
 *       to add the requirement p is congruent to a mod 8. The value is ignored
51
 *       if it is zero.
52
 *     b An optional number with a value of 0, 1, 3, 5 or 7 that may be used
53
 *       to add the requirement q is congruent to b mod 8. The value is ignored
54
 *       if it is zero.
55
 *
56
 * Returns: 1 if successful, or  0 otherwise.
57
 * Notes:
58
 *     p1, p2, q1, q2 are returned if they are not NULL.
59
 *     Xp, Xp1, Xp2, Xq, Xq1, Xq2 are optionally passed in.
60
 *     (Required for CAVS testing).
61
 */
62
int ossl_rsa_fips186_5_gen_prob_primes(RSA *rsa, RSA_ACVP_TEST *test,
63
                                       int nbits, const BIGNUM *e, BN_CTX *ctx,
64
                                       BN_GENCB *cb, uint32_t a, uint32_t b)
65
0
{
66
0
    int ret = 0, ok;
67
    /* Temp allocated BIGNUMS */
68
0
    BIGNUM *Xpo = NULL, *Xqo = NULL, *tmp = NULL;
69
    /* Intermediate BIGNUMS that can be returned for testing */
70
0
    BIGNUM *p1 = NULL, *p2 = NULL;
71
0
    BIGNUM *q1 = NULL, *q2 = NULL;
72
    /* Intermediate BIGNUMS that can be input for testing */
73
0
    BIGNUM *Xp = NULL, *Xp1 = NULL, *Xp2 = NULL;
74
0
    BIGNUM *Xq = NULL, *Xq1 = NULL, *Xq2 = NULL;
75
76
#if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
77
    if (test != NULL) {
78
        Xp1 = test->Xp1;
79
        Xp2 = test->Xp2;
80
        Xq1 = test->Xq1;
81
        Xq2 = test->Xq2;
82
        Xp = test->Xp;
83
        Xq = test->Xq;
84
        p1 = test->p1;
85
        p2 = test->p2;
86
        q1 = test->q1;
87
        q2 = test->q2;
88
    }
89
#endif
90
91
    /*
92
     * (Step 1) Check key length
93
     * NOTE: SP800-131A Rev1 Disallows key lengths of < 2048 bits for RSA
94
     * Signature Generation and Key Agree/Transport.
95
     */
96
0
    if (nbits < RSA_FIPS186_5_MIN_KEYGEN_KEYSIZE) {
97
0
        ERR_raise(ERR_LIB_RSA, RSA_R_KEY_SIZE_TOO_SMALL);
98
0
        return 0;
99
0
    }
100
101
    /* (Step 2) Check exponent */
102
0
    if (!ossl_rsa_check_public_exponent(e)) {
103
0
        ERR_raise(ERR_LIB_RSA, RSA_R_PUB_EXPONENT_OUT_OF_RANGE);
104
0
        return 0;
105
0
    }
106
107
    /*
108
     * (Step 3) Determine strength and check rand generator strength is ok -
109
     * this step is redundant because the generator always returns a higher
110
     * strength than is required.
111
     */
112
113
0
    BN_CTX_start(ctx);
114
0
    tmp = BN_CTX_get(ctx);
115
0
    Xpo = BN_CTX_get(ctx);
116
0
    Xqo = BN_CTX_get(ctx);
117
0
    if (tmp == NULL || Xpo == NULL || Xqo == NULL)
118
0
        goto err;
119
0
    BN_set_flags(Xpo, BN_FLG_CONSTTIME);
120
0
    BN_set_flags(Xqo, BN_FLG_CONSTTIME);
121
122
0
    if (rsa->p == NULL)
123
0
        rsa->p = BN_secure_new();
124
0
    if (rsa->q == NULL)
125
0
        rsa->q = BN_secure_new();
126
0
    if (rsa->p == NULL || rsa->q == NULL)
127
0
        goto err;
128
0
    BN_set_flags(rsa->p, BN_FLG_CONSTTIME);
129
0
    BN_set_flags(rsa->q, BN_FLG_CONSTTIME);
130
131
    /* (Step 4) Generate p, Xp */
132
0
    if (!ossl_bn_rsa_fips186_5_gen_prob_primes(rsa->p, Xpo, p1, p2, Xp, Xp1, Xp2,
133
0
                                               nbits, e, ctx, cb, a))
134
0
        goto err;
135
0
    for (;;) {
136
        /* (Step 5) Generate q, Xq*/
137
0
        if (!ossl_bn_rsa_fips186_5_gen_prob_primes(rsa->q, Xqo, q1, q2, Xq, Xq1,
138
0
                                                   Xq2, nbits, e, ctx, cb, b))
139
0
            goto err;
140
141
        /* (Step 6) |Xp - Xq| > 2^(nbitlen/2 - 100) */
142
0
        ok = ossl_rsa_check_pminusq_diff(tmp, Xpo, Xqo, nbits);
143
0
        if (ok < 0)
144
0
            goto err;
145
0
        if (ok == 0)
146
0
            continue;
147
148
        /* (Step 6) |p - q| > 2^(nbitlen/2 - 100) */
149
0
        ok = ossl_rsa_check_pminusq_diff(tmp, rsa->p, rsa->q, nbits);
150
0
        if (ok < 0)
151
0
            goto err;
152
0
        if (ok == 0)
153
0
            continue;
154
0
        break; /* successfully finished */
155
0
    }
156
0
    rsa->dirty_cnt++;
157
0
    ret = 1;
158
0
err:
159
    /* (Step 7) Zeroize any internally generated values that are not returned */
160
0
    BN_clear(Xpo);
161
0
    BN_clear(Xqo);
162
0
    BN_clear(tmp);
163
0
    if (ret != 1) {
164
0
        BN_clear_free(rsa->p);
165
0
        rsa->p = NULL;
166
0
        BN_clear_free(rsa->q);
167
0
        rsa->q = NULL;
168
0
    }
169
170
0
    BN_CTX_end(ctx);
171
0
    return ret;
172
0
}
173
174
/*
175
 * Validates the RSA key size based on the target strength.
176
 * See SP800-56Br1 6.3.1.1 (Steps 1a-1b)
177
 *
178
 * Params:
179
 *     nbits The key size in bits.
180
 *     strength The target strength in bits. -1 means the target
181
 *              strength is unknown.
182
 * Returns: 1 if the key size matches the target strength, or 0 otherwise.
183
 */
184
int ossl_rsa_sp800_56b_validate_strength(int nbits, int strength)
185
0
{
186
0
    int s = (int)ossl_ifc_ffc_compute_security_bits(nbits);
187
188
#ifdef FIPS_MODULE
189
    if (s < RSA_FIPS186_5_MIN_KEYGEN_STRENGTH) {
190
        ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_MODULUS);
191
        return 0;
192
    }
193
#endif
194
0
    if (strength != -1 && s != strength) {
195
0
        ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_STRENGTH);
196
0
        return 0;
197
0
    }
198
0
    return 1;
199
0
}
200
201
/*
202
 * Validate that the random bit generator is of sufficient strength to generate
203
 * a key of the specified length.
204
 */
205
static int rsa_validate_rng_strength(EVP_RAND_CTX *rng, int nbits)
206
0
{
207
0
    if (rng == NULL)
208
0
        return 0;
209
#ifdef FIPS_MODULE
210
    /*
211
     * This should become mainstream once similar tests are added to the other
212
     * key generations and once there is a way to disable these checks.
213
     */
214
    if (EVP_RAND_get_strength(rng) < ossl_ifc_ffc_compute_security_bits(nbits)) {
215
        ERR_raise(ERR_LIB_RSA,
216
                  RSA_R_RANDOMNESS_SOURCE_STRENGTH_INSUFFICIENT);
217
        return 0;
218
    }
219
#endif
220
0
    return 1;
221
0
}
222
223
/*
224
 *
225
 * Using p & q, calculate other required parameters such as n, d.
226
 * as well as the CRT parameters dP, dQ, qInv.
227
 *
228
 * See SP800-56Br1
229
 *   6.3.1.1 rsakpg1 - basic (Steps 3-4)
230
 *   6.3.1.3 rsakpg1 - crt   (Step 5)
231
 *
232
 * Params:
233
 *     rsa An rsa object.
234
 *     nbits The key size.
235
 *     e The public exponent.
236
 *     ctx A BN_CTX object.
237
 * Notes:
238
 *   There is a small chance that the generated d will be too small.
239
 * Returns: -1 = error,
240
 *           0 = d is too small,
241
 *           1 = success.
242
 *
243
 * SP800-56b key generation always passes a non NULL value for e.
244
 * For other purposes, if e is NULL then it is assumed that e, n and d are
245
 * already set in the RSA key and do not need to be recalculated.
246
 */
247
int ossl_rsa_sp800_56b_derive_params_from_pq(RSA *rsa, int nbits,
248
                                             const BIGNUM *e, BN_CTX *ctx)
249
0
{
250
0
    int ret = -1;
251
0
    BIGNUM *p1, *q1, *lcm, *p1q1, *gcd;
252
0
    BN_CTX_start(ctx);
253
0
    p1 = BN_CTX_get(ctx);
254
0
    q1 = BN_CTX_get(ctx);
255
0
    lcm = BN_CTX_get(ctx);
256
0
    p1q1 = BN_CTX_get(ctx);
257
0
    gcd = BN_CTX_get(ctx);
258
0
    if (gcd == NULL)
259
0
        goto err;
260
261
0
    BN_set_flags(p1, BN_FLG_CONSTTIME);
262
0
    BN_set_flags(q1, BN_FLG_CONSTTIME);
263
0
    BN_set_flags(lcm, BN_FLG_CONSTTIME);
264
0
    BN_set_flags(p1q1, BN_FLG_CONSTTIME);
265
0
    BN_set_flags(gcd, BN_FLG_CONSTTIME);
266
267
    /* LCM((p-1, q-1)) */
268
0
    if (ossl_rsa_get_lcm(ctx, rsa->p, rsa->q, lcm, gcd, p1, q1, p1q1) != 1)
269
0
        goto err;
270
271
    /*
272
     * if e is provided as a parameter, don't recompute e, d or n
273
     */
274
0
    if (e != NULL) {
275
        /* copy e */
276
0
        BN_free(rsa->e);
277
0
        rsa->e = BN_dup(e);
278
0
        if (rsa->e == NULL)
279
0
            goto err;
280
281
0
        BN_clear_free(rsa->d);
282
        /* (Step 3) d = (e^-1) mod (LCM(p-1, q-1)) */
283
0
        rsa->d = BN_secure_new();
284
0
        if (rsa->d == NULL)
285
0
            goto err;
286
0
        BN_set_flags(rsa->d, BN_FLG_CONSTTIME);
287
0
        if (BN_mod_inverse(rsa->d, e, lcm, ctx) == NULL)
288
0
            goto err;
289
290
        /* (Step 3) return an error if d is too small */
291
0
        if (BN_num_bits(rsa->d) <= (nbits >> 1)) {
292
0
            ret = 0;
293
0
            goto err;
294
0
        }
295
296
        /* (Step 4) n = pq */
297
0
        if (rsa->n == NULL)
298
0
            rsa->n = BN_new();
299
0
        if (rsa->n == NULL || !BN_mul(rsa->n, rsa->p, rsa->q, ctx))
300
0
            goto err;
301
0
    }
302
303
    /* (Step 5a) dP = d mod (p-1) */
304
0
    if (rsa->dmp1 == NULL)
305
0
        rsa->dmp1 = BN_secure_new();
306
0
    if (rsa->dmp1 == NULL)
307
0
        goto err;
308
0
    BN_set_flags(rsa->dmp1, BN_FLG_CONSTTIME);
309
0
    if (!BN_mod(rsa->dmp1, rsa->d, p1, ctx))
310
0
        goto err;
311
312
    /* (Step 5b) dQ = d mod (q-1) */
313
0
    if (rsa->dmq1 == NULL)
314
0
        rsa->dmq1 = BN_secure_new();
315
0
    if (rsa->dmq1 == NULL)
316
0
        goto err;
317
0
    BN_set_flags(rsa->dmq1, BN_FLG_CONSTTIME);
318
0
    if (!BN_mod(rsa->dmq1, rsa->d, q1, ctx))
319
0
        goto err;
320
321
    /* (Step 5c) qInv = (inverse of q) mod p */
322
0
    BN_free(rsa->iqmp);
323
0
    rsa->iqmp = BN_secure_new();
324
0
    if (rsa->iqmp == NULL)
325
0
        goto err;
326
0
    BN_set_flags(rsa->iqmp, BN_FLG_CONSTTIME);
327
0
    if (BN_mod_inverse(rsa->iqmp, rsa->q, rsa->p, ctx) == NULL)
328
0
        goto err;
329
330
0
    rsa->dirty_cnt++;
331
0
    ret = 1;
332
0
err:
333
0
    if (ret != 1) {
334
0
        BN_free(rsa->e);
335
0
        rsa->e = NULL;
336
0
        BN_free(rsa->d);
337
0
        rsa->d = NULL;
338
0
        BN_free(rsa->n);
339
0
        rsa->n = NULL;
340
0
        BN_free(rsa->iqmp);
341
0
        rsa->iqmp = NULL;
342
0
        BN_free(rsa->dmq1);
343
0
        rsa->dmq1 = NULL;
344
0
        BN_free(rsa->dmp1);
345
0
        rsa->dmp1 = NULL;
346
0
    }
347
0
    BN_clear(p1);
348
0
    BN_clear(q1);
349
0
    BN_clear(lcm);
350
0
    BN_clear(p1q1);
351
0
    BN_clear(gcd);
352
353
0
    BN_CTX_end(ctx);
354
0
    return ret;
355
0
}
356
357
/*
358
 * Generate a SP800-56B RSA key.
359
 *
360
 * See SP800-56Br1 6.3.1 "RSA Key-Pair Generation with a Fixed Public Exponent"
361
 *    6.3.1.1 rsakpg1 - basic
362
 *    6.3.1.3 rsakpg1 - crt
363
 *
364
 * See also FIPS 186-5 Section A.1.6
365
 * "Generation of Probable Primes with Conditions Based on Auxiliary
366
 * Probable Primes."
367
 *
368
 * Params:
369
 *     rsa The rsa object.
370
 *     nbits The intended key size in bits.
371
 *     efixed The public exponent. If NULL a default of 65537 is used.
372
 *     cb An optional BIGNUM callback.
373
 *     a An optional number with a value of 0, 1, 3, 5 or 7 that may be used
374
 *       to add the requirement p is congruent to a mod 8. The value is ignored
375
 *       if it is zero.
376
 *     b An optional number with a value of 0, 1, 3, 5 or 7 that may be used
377
 *       to add the requirement q is congruent to b mod 8. The value is ignored
378
 *       if it is zero.
379
 * Returns: 1 if successfully generated otherwise it returns 0.
380
 */
381
int ossl_rsa_sp800_56b_generate_key(RSA *rsa, int nbits, const BIGNUM *efixed,
382
                                    BN_GENCB *cb, uint32_t a, uint32_t b)
383
0
{
384
0
    int ret = 0;
385
0
    int ok;
386
0
    BN_CTX *ctx = NULL;
387
0
    BIGNUM *e = NULL;
388
0
    RSA_ACVP_TEST *info = NULL;
389
0
    BIGNUM *tmp;
390
391
#if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
392
    info = rsa->acvp_test;
393
#endif
394
395
    /* (Steps 1a-1b) : Currently ignores the strength check */
396
0
    if (!ossl_rsa_sp800_56b_validate_strength(nbits, -1))
397
0
        return 0;
398
399
    /* Check that the RNG is capable of generating a key this large */
400
0
   if (!rsa_validate_rng_strength(RAND_get0_private(rsa->libctx), nbits))
401
0
        return 0;
402
403
0
    ctx = BN_CTX_new_ex(rsa->libctx);
404
0
    if (ctx == NULL)
405
0
        return 0;
406
407
    /* Set default if e is not passed in */
408
0
    if (efixed == NULL) {
409
0
        e = BN_new();
410
0
        if (e == NULL || !BN_set_word(e, 65537))
411
0
            goto err;
412
0
    } else {
413
0
        e = (BIGNUM *)efixed;
414
0
    }
415
    /* (Step 1c) fixed exponent is checked later .*/
416
417
0
    for (;;) {
418
        /* (Step 2) Generate prime factors */
419
0
        if (!ossl_rsa_fips186_5_gen_prob_primes(rsa, info, nbits, e, ctx, cb, a, b))
420
0
            goto err;
421
422
        /* p>q check and skipping in case of acvp test */
423
0
        if (info == NULL && BN_cmp(rsa->p, rsa->q) < 0) {
424
0
            tmp = rsa->p;
425
0
            rsa->p = rsa->q;
426
0
            rsa->q = tmp;
427
0
        }
428
429
        /* (Steps 3-5) Compute params d, n, dP, dQ, qInv */
430
0
        ok = ossl_rsa_sp800_56b_derive_params_from_pq(rsa, nbits, e, ctx);
431
0
        if (ok < 0)
432
0
            goto err;
433
0
        if (ok > 0)
434
0
            break;
435
        /* Gets here if computed d is too small - so try again */
436
0
    }
437
438
    /* (Step 6) Do pairwise test - optional validity test has been omitted */
439
0
    ret = ossl_rsa_sp800_56b_pairwise_test(rsa, ctx);
440
0
err:
441
0
    if (efixed == NULL)
442
0
        BN_free(e);
443
0
    BN_CTX_free(ctx);
444
0
    return ret;
445
0
}
446
447
/*
448
 * See SP800-56Br1 6.3.1.3 (Step 6) Perform a pair-wise consistency test by
449
 * verifying that: k = (k^e)^d mod n for some integer k where 1 < k < n-1.
450
 *
451
 * Returns 1 if the RSA key passes the pairwise test or 0 if it fails.
452
 */
453
int ossl_rsa_sp800_56b_pairwise_test(RSA *rsa, BN_CTX *ctx)
454
0
{
455
0
    int ret = 0;
456
0
    BIGNUM *k, *tmp;
457
458
0
    BN_CTX_start(ctx);
459
0
    tmp = BN_CTX_get(ctx);
460
0
    k = BN_CTX_get(ctx);
461
0
    if (k == NULL)
462
0
        goto err;
463
0
    BN_set_flags(k, BN_FLG_CONSTTIME);
464
465
0
    ret = (BN_set_word(k, 2)
466
0
           && BN_mod_exp(tmp, k, rsa->e, rsa->n, ctx)
467
0
           && BN_mod_exp(tmp, tmp, rsa->d, rsa->n, ctx)
468
0
           && BN_cmp(k, tmp) == 0);
469
0
    if (ret == 0)
470
0
        ERR_raise(ERR_LIB_RSA, RSA_R_PAIRWISE_TEST_FAILURE);
471
0
err:
472
0
    BN_CTX_end(ctx);
473
0
    return ret;
474
0
}