Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/crypto/dh/dh_check.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2026 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
 * DH low level APIs are deprecated for public use, but still ok for
12
 * internal use.
13
 */
14
#include "internal/deprecated.h"
15
16
#include <stdio.h>
17
#include "internal/cryptlib.h"
18
#include <openssl/bn.h>
19
#include <openssl/self_test.h>
20
#include "dh_local.h"
21
#include "crypto/dh.h"
22
23
/*-
24
 * Check that p and g are suitable enough
25
 *
26
 * p is odd
27
 * 1 < g < p - 1
28
 */
29
int DH_check_params_ex(const DH *dh)
30
10.2k
{
31
10.2k
    int errflags = 0;
32
33
10.2k
    if (!DH_check_params(dh, &errflags))
34
0
        return 0;
35
36
10.2k
    if ((errflags & DH_CHECK_P_NOT_PRIME) != 0)
37
10.2k
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_P_NOT_PRIME);
38
10.2k
    if ((errflags & DH_NOT_SUITABLE_GENERATOR) != 0)
39
10.2k
        ERR_raise(ERR_LIB_DH, DH_R_NOT_SUITABLE_GENERATOR);
40
10.2k
    if ((errflags & DH_MODULUS_TOO_SMALL) != 0)
41
10.2k
        ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_SMALL);
42
10.2k
    if ((errflags & DH_MODULUS_TOO_LARGE) != 0)
43
10.2k
        ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
44
45
10.2k
    return errflags == 0;
46
10.2k
}
47
48
#ifdef FIPS_MODULE
49
int DH_check_params(const DH *dh, int *ret)
50
{
51
    int nid;
52
53
    *ret = 0;
54
    /*
55
     * SP800-56A R3 Section 5.5.2 Assurances of Domain Parameter Validity
56
     * (1a) The domain parameters correspond to any approved safe prime group.
57
     */
58
    nid = DH_get_nid((DH *)dh);
59
    if (nid != NID_undef)
60
        return 1;
61
    /*
62
     * OR
63
     * (2b) FFC domain params conform to FIPS-186-4 explicit domain param
64
     * validity tests.
65
     */
66
    return ossl_ffc_params_FIPS186_4_validate(dh->libctx, &dh->params,
67
        FFC_PARAM_TYPE_DH, ret, NULL);
68
}
69
#else
70
int DH_check_params(const DH *dh, int *ret)
71
13.5k
{
72
13.5k
    int ok = 0;
73
13.5k
    BIGNUM *tmp = NULL;
74
13.5k
    BN_CTX *ctx = NULL;
75
76
13.5k
    *ret = 0;
77
    /*
78
     * A DH with no modulus or generator cannot be checked.  Report
79
     * the failure via |*ret| rather than dereferencing NULL below.
80
     */
81
13.5k
    if (dh->params.p == NULL || dh->params.g == NULL) {
82
0
        *ret = DH_NOT_SUITABLE_GENERATOR | DH_CHECK_P_NOT_PRIME;
83
0
        return 1;
84
0
    }
85
13.5k
    ctx = BN_CTX_new_ex(dh->libctx);
86
13.5k
    if (ctx == NULL)
87
0
        goto err;
88
13.5k
    BN_CTX_start(ctx);
89
13.5k
    tmp = BN_CTX_get(ctx);
90
13.5k
    if (tmp == NULL)
91
0
        goto err;
92
93
13.5k
    if (!BN_is_odd(dh->params.p))
94
3.91k
        *ret |= DH_CHECK_P_NOT_PRIME;
95
13.5k
    if (BN_is_negative(dh->params.g)
96
13.5k
        || BN_is_zero(dh->params.g)
97
11.0k
        || BN_is_one(dh->params.g))
98
2.49k
        *ret |= DH_NOT_SUITABLE_GENERATOR;
99
13.5k
    if (BN_copy(tmp, dh->params.p) == NULL || !BN_sub_word(tmp, 1))
100
0
        goto err;
101
13.5k
    if (BN_cmp(dh->params.g, tmp) >= 0)
102
1.01k
        *ret |= DH_NOT_SUITABLE_GENERATOR;
103
13.5k
    if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS)
104
4.52k
        *ret |= DH_MODULUS_TOO_SMALL;
105
13.5k
    if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS)
106
14
        *ret |= DH_MODULUS_TOO_LARGE;
107
108
13.5k
    ok = 1;
109
13.5k
err:
110
13.5k
    BN_CTX_end(ctx);
111
13.5k
    BN_CTX_free(ctx);
112
13.5k
    return ok;
113
13.5k
}
114
#endif /* FIPS_MODULE */
115
116
/*-
117
 * Check that p is a safe prime and
118
 * g is a suitable generator.
119
 */
120
int DH_check_ex(const DH *dh)
121
6.53k
{
122
6.53k
    int errflags = 0;
123
124
6.53k
    if (!DH_check(dh, &errflags))
125
72
        return 0;
126
127
6.46k
    if ((errflags & DH_NOT_SUITABLE_GENERATOR) != 0)
128
6.46k
        ERR_raise(ERR_LIB_DH, DH_R_NOT_SUITABLE_GENERATOR);
129
6.46k
    if ((errflags & DH_CHECK_Q_NOT_PRIME) != 0)
130
6.46k
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_Q_NOT_PRIME);
131
6.46k
    if ((errflags & DH_CHECK_INVALID_Q_VALUE) != 0)
132
6.46k
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_INVALID_Q_VALUE);
133
6.46k
    if ((errflags & DH_CHECK_INVALID_J_VALUE) != 0)
134
6.46k
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_INVALID_J_VALUE);
135
6.46k
    if ((errflags & DH_UNABLE_TO_CHECK_GENERATOR) != 0)
136
6.46k
        ERR_raise(ERR_LIB_DH, DH_R_UNABLE_TO_CHECK_GENERATOR);
137
6.46k
    if ((errflags & DH_CHECK_P_NOT_PRIME) != 0)
138
6.46k
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_P_NOT_PRIME);
139
6.46k
    if ((errflags & DH_CHECK_P_NOT_SAFE_PRIME) != 0)
140
6.46k
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_P_NOT_SAFE_PRIME);
141
6.46k
    if ((errflags & DH_MODULUS_TOO_SMALL) != 0)
142
6.46k
        ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_SMALL);
143
6.46k
    if ((errflags & DH_MODULUS_TOO_LARGE) != 0)
144
6.46k
        ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
145
146
6.46k
    return errflags == 0;
147
6.53k
}
148
149
/* Note: according to documentation - this only checks the params */
150
int DH_check(const DH *dh, int *ret)
151
4.75k
{
152
#ifdef FIPS_MODULE
153
    return DH_check_params(dh, ret);
154
#else
155
4.75k
    int ok = 0, r, q_good = 0;
156
4.75k
    BN_CTX *ctx = NULL;
157
4.75k
    BIGNUM *t1 = NULL, *t2 = NULL;
158
4.75k
    int nid = DH_get_nid((DH *)dh);
159
160
4.75k
    *ret = 0;
161
    /* A DH with no modulus or generator cannot be checked. */
162
4.75k
    if (dh->params.p == NULL || dh->params.g == NULL) {
163
0
        *ret = DH_NOT_SUITABLE_GENERATOR | DH_CHECK_P_NOT_PRIME;
164
0
        return 1;
165
0
    }
166
4.75k
    if (nid != NID_undef)
167
0
        return 1;
168
169
    /* Don't do any checks at all with an excessively large modulus */
170
4.75k
    if (BN_num_bits(dh->params.p) > OPENSSL_DH_CHECK_MAX_MODULUS_BITS) {
171
0
        ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
172
0
        *ret = DH_MODULUS_TOO_LARGE | DH_CHECK_P_NOT_PRIME;
173
0
        return 0;
174
0
    }
175
176
4.75k
    if (!DH_check_params(dh, ret))
177
0
        return 0;
178
179
4.75k
    ctx = BN_CTX_new_ex(dh->libctx);
180
4.75k
    if (ctx == NULL)
181
0
        goto err;
182
4.75k
    BN_CTX_start(ctx);
183
4.75k
    t1 = BN_CTX_get(ctx);
184
4.75k
    t2 = BN_CTX_get(ctx);
185
4.75k
    if (t2 == NULL)
186
0
        goto err;
187
188
4.75k
    if (dh->params.q != NULL) {
189
2.71k
        if (BN_ucmp(dh->params.p, dh->params.q) > 0)
190
2.53k
            q_good = 1;
191
185
        else
192
185
            *ret |= DH_CHECK_INVALID_Q_VALUE;
193
2.71k
    }
194
195
4.75k
    if (q_good) {
196
2.53k
        if (BN_cmp(dh->params.g, BN_value_one()) <= 0)
197
39
            *ret |= DH_NOT_SUITABLE_GENERATOR;
198
2.49k
        else if (BN_cmp(dh->params.g, dh->params.p) >= 0)
199
15
            *ret |= DH_NOT_SUITABLE_GENERATOR;
200
2.47k
        else {
201
            /* Check g^q == 1 mod p */
202
2.47k
            if (!BN_mod_exp(t1, dh->params.g, dh->params.q, dh->params.p, ctx))
203
0
                goto err;
204
2.47k
            if (!BN_is_one(t1))
205
2.44k
                *ret |= DH_NOT_SUITABLE_GENERATOR;
206
2.47k
        }
207
2.53k
        r = BN_check_prime(dh->params.q, ctx, NULL);
208
2.53k
        if (r < 0)
209
0
            goto err;
210
2.53k
        if (!r)
211
2.21k
            *ret |= DH_CHECK_Q_NOT_PRIME;
212
        /* Check p == 1 mod q  i.e. q divides p - 1 */
213
2.53k
        if (!BN_div(t1, t2, dh->params.p, dh->params.q, ctx))
214
54
            goto err;
215
2.47k
        if (!BN_is_one(t2))
216
2.44k
            *ret |= DH_CHECK_INVALID_Q_VALUE;
217
2.47k
        if (dh->params.j != NULL
218
58
            && BN_cmp(dh->params.j, t1))
219
54
            *ret |= DH_CHECK_INVALID_J_VALUE;
220
2.47k
    }
221
222
4.69k
    r = BN_check_prime(dh->params.p, ctx, NULL);
223
4.69k
    if (r < 0)
224
0
        goto err;
225
4.69k
    if (!r)
226
3.90k
        *ret |= DH_CHECK_P_NOT_PRIME;
227
794
    else if (dh->params.q == NULL) {
228
696
        if (!BN_rshift1(t1, dh->params.p))
229
0
            goto err;
230
696
        r = BN_check_prime(t1, ctx, NULL);
231
696
        if (r < 0)
232
0
            goto err;
233
696
        if (!r)
234
589
            *ret |= DH_CHECK_P_NOT_SAFE_PRIME;
235
696
    }
236
4.69k
    ok = 1;
237
4.75k
err:
238
4.75k
    BN_CTX_end(ctx);
239
4.75k
    BN_CTX_free(ctx);
240
4.75k
    return ok;
241
4.69k
#endif /* FIPS_MODULE */
242
4.69k
}
243
244
int DH_check_pub_key_ex(const DH *dh, const BIGNUM *pub_key)
245
12.4k
{
246
12.4k
    int errflags = 0;
247
248
12.4k
    if (!DH_check_pub_key(dh, pub_key, &errflags))
249
0
        return 0;
250
251
12.4k
    if ((errflags & DH_CHECK_PUBKEY_TOO_SMALL) != 0)
252
12.4k
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_TOO_SMALL);
253
12.4k
    if ((errflags & DH_CHECK_PUBKEY_TOO_LARGE) != 0)
254
12.4k
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_TOO_LARGE);
255
12.4k
    if ((errflags & DH_CHECK_PUBKEY_INVALID) != 0)
256
12.4k
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_INVALID);
257
258
12.4k
    return errflags == 0;
259
12.4k
}
260
261
/*
262
 * See SP800-56Ar3 Section 5.6.2.3.1 : FFC Full public key validation.
263
 */
264
int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *ret)
265
10.4k
{
266
10.4k
    *ret = 0;
267
    /*
268
     * Without a modulus we cannot check anything; signal failure via
269
     * |*ret| rather than crashing in BN_num_bits below.
270
     */
271
10.4k
    if (dh->params.p == NULL) {
272
0
        *ret = DH_CHECK_PUBKEY_INVALID;
273
0
        return 1;
274
0
    }
275
    /* Don't do any checks at all with an excessively large modulus */
276
10.4k
    if (BN_num_bits(dh->params.p) > OPENSSL_DH_CHECK_MAX_MODULUS_BITS) {
277
0
        ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
278
0
        *ret = DH_MODULUS_TOO_LARGE | DH_CHECK_PUBKEY_INVALID;
279
0
        return 0;
280
0
    }
281
282
10.4k
    if (dh->params.q != NULL && BN_ucmp(dh->params.p, dh->params.q) < 0) {
283
26
        *ret |= DH_CHECK_INVALID_Q_VALUE | DH_CHECK_PUBKEY_INVALID;
284
26
        return 1;
285
26
    }
286
287
10.4k
    return ossl_ffc_validate_public_key(&dh->params, pub_key, ret);
288
10.4k
}
289
290
/*
291
 * See SP800-56Ar3 Section 5.6.2.3.1 : FFC Partial public key validation.
292
 * To only be used with ephemeral FFC public keys generated using the approved
293
 * safe-prime groups.
294
 */
295
int ossl_dh_check_pub_key_partial(const DH *dh, const BIGNUM *pub_key, int *ret)
296
3.15k
{
297
3.15k
    return ossl_ffc_validate_public_key_partial(&dh->params, pub_key, ret)
298
3.15k
        && *ret == 0;
299
3.15k
}
300
301
int ossl_dh_check_priv_key(const DH *dh, const BIGNUM *priv_key, int *ret)
302
46
{
303
46
    int ok = 0;
304
46
    BIGNUM *two_powN = NULL, *upper;
305
306
46
    *ret = 0;
307
46
    two_powN = BN_new();
308
46
    if (two_powN == NULL)
309
0
        return 0;
310
311
46
    if (dh->params.q != NULL) {
312
46
        upper = dh->params.q;
313
46
#ifndef FIPS_MODULE
314
46
    } else if (dh->params.p != NULL) {
315
        /*
316
         * We do not have q so we just check the key is within some
317
         * reasonable range, or the number of bits is equal to dh->length.
318
         */
319
0
        int length = dh->length;
320
321
0
        if (length == 0) {
322
0
            length = BN_num_bits(dh->params.p) - 1;
323
0
            if (BN_num_bits(priv_key) <= length
324
0
                && BN_num_bits(priv_key) > 1)
325
0
                ok = 1;
326
0
        } else if (BN_num_bits(priv_key) == length) {
327
0
            ok = 1;
328
0
        }
329
0
        goto end;
330
0
#endif
331
0
    } else {
332
0
        goto end;
333
0
    }
334
335
    /* Is it from an approved Safe prime group ?*/
336
46
    if (DH_get_nid((DH *)dh) != NID_undef && dh->length != 0) {
337
0
        if (!BN_lshift(two_powN, BN_value_one(), dh->length))
338
0
            goto end;
339
0
        if (BN_cmp(two_powN, dh->params.q) < 0)
340
0
            upper = two_powN;
341
0
    }
342
46
    if (!ossl_ffc_validate_private_key(upper, priv_key, ret))
343
18
        goto end;
344
345
28
    ok = 1;
346
46
end:
347
46
    BN_free(two_powN);
348
46
    return ok;
349
28
}
350
351
/*
352
 * FFC pairwise check from SP800-56A R3.
353
 *    Section 5.6.2.1.4 Owner Assurance of Pair-wise Consistency
354
 */
355
int ossl_dh_check_pairwise(const DH *dh, int return_on_null_numbers)
356
0
{
357
0
    int ret = 0;
358
0
    BN_CTX *ctx = NULL;
359
0
    BIGNUM *pub_key = NULL;
360
0
    OSSL_SELF_TEST *st = NULL;
361
0
    OSSL_CALLBACK *stcb = NULL;
362
0
    void *stcbarg = NULL;
363
364
0
    if (dh->params.p == NULL
365
0
        || dh->params.g == NULL
366
0
        || dh->priv_key == NULL
367
0
        || dh->pub_key == NULL)
368
0
        return return_on_null_numbers;
369
370
0
    OSSL_SELF_TEST_get_callback(dh->libctx, &stcb, &stcbarg);
371
0
    st = OSSL_SELF_TEST_new(stcb, stcbarg);
372
0
    if (st == NULL)
373
0
        goto err;
374
0
    OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_PCT,
375
0
        OSSL_SELF_TEST_DESC_PCT_DH);
376
377
0
    ctx = BN_CTX_new_ex(dh->libctx);
378
0
    if (ctx == NULL)
379
0
        goto err;
380
0
    pub_key = BN_new();
381
0
    if (pub_key == NULL)
382
0
        goto err;
383
384
    /* recalculate the public key = (g ^ priv) mod p */
385
0
    if (!ossl_dh_generate_public_key(ctx, dh, dh->priv_key, pub_key))
386
0
        goto err;
387
388
#ifdef FIPS_MODULE
389
    {
390
        int len;
391
        unsigned char bytes[1024] = { 0 }; /* Max key size of 8192 bits */
392
393
        if (BN_num_bytes(pub_key) > (int)sizeof(bytes))
394
            goto err;
395
        len = BN_bn2bin(pub_key, bytes);
396
        OSSL_SELF_TEST_oncorrupt_byte(st, bytes);
397
        if (BN_bin2bn(bytes, len, pub_key) == NULL)
398
            goto err;
399
    }
400
#endif
401
    /* check it matches the existing public_key */
402
0
    ret = BN_cmp(pub_key, dh->pub_key) == 0;
403
0
err:
404
0
    BN_free(pub_key);
405
0
    BN_CTX_free(ctx);
406
407
0
    OSSL_SELF_TEST_onend(st, ret);
408
0
    OSSL_SELF_TEST_free(st);
409
0
    return ret;
410
0
}