Coverage Report

Created: 2025-08-28 07:07

/src/openssl35/crypto/dh/dh_check.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2025 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
9.06k
{
31
9.06k
    int errflags = 0;
32
33
9.06k
    if (!DH_check_params(dh, &errflags))
34
0
        return 0;
35
36
9.06k
    if ((errflags & DH_CHECK_P_NOT_PRIME) != 0)
37
9.06k
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_P_NOT_PRIME);
38
9.06k
    if ((errflags & DH_NOT_SUITABLE_GENERATOR) != 0)
39
9.06k
        ERR_raise(ERR_LIB_DH, DH_R_NOT_SUITABLE_GENERATOR);
40
9.06k
    if ((errflags & DH_MODULUS_TOO_SMALL) != 0)
41
9.06k
        ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_SMALL);
42
9.06k
    if ((errflags & DH_MODULUS_TOO_LARGE) != 0)
43
9.06k
        ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
44
45
9.06k
    return errflags == 0;
46
9.06k
}
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
17.9k
{
72
17.9k
    int ok = 0;
73
17.9k
    BIGNUM *tmp = NULL;
74
17.9k
    BN_CTX *ctx = NULL;
75
76
17.9k
    *ret = 0;
77
17.9k
    ctx = BN_CTX_new_ex(dh->libctx);
78
17.9k
    if (ctx == NULL)
79
0
        goto err;
80
17.9k
    BN_CTX_start(ctx);
81
17.9k
    tmp = BN_CTX_get(ctx);
82
17.9k
    if (tmp == NULL)
83
0
        goto err;
84
85
17.9k
    if (!BN_is_odd(dh->params.p))
86
6.97k
        *ret |= DH_CHECK_P_NOT_PRIME;
87
17.9k
    if (BN_is_negative(dh->params.g)
88
17.9k
        || BN_is_zero(dh->params.g)
89
17.9k
        || BN_is_one(dh->params.g))
90
3.41k
        *ret |= DH_NOT_SUITABLE_GENERATOR;
91
17.9k
    if (BN_copy(tmp, dh->params.p) == NULL || !BN_sub_word(tmp, 1))
92
0
        goto err;
93
17.9k
    if (BN_cmp(dh->params.g, tmp) >= 0)
94
1.23k
        *ret |= DH_NOT_SUITABLE_GENERATOR;
95
17.9k
    if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS)
96
6.18k
        *ret |= DH_MODULUS_TOO_SMALL;
97
17.9k
    if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS)
98
55
        *ret |= DH_MODULUS_TOO_LARGE;
99
100
17.9k
    ok = 1;
101
17.9k
 err:
102
17.9k
    BN_CTX_end(ctx);
103
17.9k
    BN_CTX_free(ctx);
104
17.9k
    return ok;
105
17.9k
}
106
#endif /* FIPS_MODULE */
107
108
/*-
109
 * Check that p is a safe prime and
110
 * g is a suitable generator.
111
 */
112
int DH_check_ex(const DH *dh)
113
8.89k
{
114
8.89k
    int errflags = 0;
115
116
8.89k
    if (!DH_check(dh, &errflags))
117
102
        return 0;
118
119
8.79k
    if ((errflags & DH_NOT_SUITABLE_GENERATOR) != 0)
120
8.79k
        ERR_raise(ERR_LIB_DH, DH_R_NOT_SUITABLE_GENERATOR);
121
8.79k
    if ((errflags & DH_CHECK_Q_NOT_PRIME) != 0)
122
8.79k
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_Q_NOT_PRIME);
123
8.79k
    if ((errflags & DH_CHECK_INVALID_Q_VALUE) != 0)
124
8.79k
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_INVALID_Q_VALUE);
125
8.79k
    if ((errflags & DH_CHECK_INVALID_J_VALUE) != 0)
126
8.79k
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_INVALID_J_VALUE);
127
8.79k
    if ((errflags & DH_UNABLE_TO_CHECK_GENERATOR) != 0)
128
8.79k
        ERR_raise(ERR_LIB_DH, DH_R_UNABLE_TO_CHECK_GENERATOR);
129
8.79k
    if ((errflags & DH_CHECK_P_NOT_PRIME) != 0)
130
8.79k
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_P_NOT_PRIME);
131
8.79k
    if ((errflags & DH_CHECK_P_NOT_SAFE_PRIME) != 0)
132
8.79k
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_P_NOT_SAFE_PRIME);
133
8.79k
    if ((errflags & DH_MODULUS_TOO_SMALL) != 0)
134
8.79k
        ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_SMALL);
135
8.79k
    if ((errflags & DH_MODULUS_TOO_LARGE) != 0)
136
8.79k
        ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
137
138
8.79k
    return errflags == 0;
139
8.89k
}
140
141
/* Note: according to documentation - this only checks the params */
142
int DH_check(const DH *dh, int *ret)
143
8.89k
{
144
#ifdef FIPS_MODULE
145
    return DH_check_params(dh, ret);
146
#else
147
8.89k
    int ok = 0, r, q_good = 0;
148
8.89k
    BN_CTX *ctx = NULL;
149
8.89k
    BIGNUM *t1 = NULL, *t2 = NULL;
150
8.89k
    int nid = DH_get_nid((DH *)dh);
151
152
8.89k
    *ret = 0;
153
8.89k
    if (nid != NID_undef)
154
0
        return 1;
155
156
    /* Don't do any checks at all with an excessively large modulus */
157
8.89k
    if (BN_num_bits(dh->params.p) > OPENSSL_DH_CHECK_MAX_MODULUS_BITS) {
158
0
        ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
159
0
        *ret = DH_MODULUS_TOO_LARGE | DH_CHECK_P_NOT_PRIME;
160
0
        return 0;
161
0
    }
162
163
8.89k
    if (!DH_check_params(dh, ret))
164
0
        return 0;
165
166
8.89k
    ctx = BN_CTX_new_ex(dh->libctx);
167
8.89k
    if (ctx == NULL)
168
0
        goto err;
169
8.89k
    BN_CTX_start(ctx);
170
8.89k
    t1 = BN_CTX_get(ctx);
171
8.89k
    t2 = BN_CTX_get(ctx);
172
8.89k
    if (t2 == NULL)
173
0
        goto err;
174
175
8.89k
    if (dh->params.q != NULL) {
176
6.72k
        if (BN_ucmp(dh->params.p, dh->params.q) > 0)
177
6.33k
            q_good = 1;
178
391
        else
179
391
            *ret |= DH_CHECK_INVALID_Q_VALUE;
180
6.72k
    }
181
182
8.89k
    if (q_good) {
183
6.33k
        if (BN_cmp(dh->params.g, BN_value_one()) <= 0)
184
89
            *ret |= DH_NOT_SUITABLE_GENERATOR;
185
6.24k
        else if (BN_cmp(dh->params.g, dh->params.p) >= 0)
186
28
            *ret |= DH_NOT_SUITABLE_GENERATOR;
187
6.21k
        else {
188
            /* Check g^q == 1 mod p */
189
6.21k
            if (!BN_mod_exp(t1, dh->params.g, dh->params.q, dh->params.p, ctx))
190
0
                goto err;
191
6.21k
            if (!BN_is_one(t1))
192
6.15k
                *ret |= DH_NOT_SUITABLE_GENERATOR;
193
6.21k
        }
194
6.33k
        r = BN_check_prime(dh->params.q, ctx, NULL);
195
6.33k
        if (r < 0)
196
0
            goto err;
197
6.33k
        if (!r)
198
5.68k
            *ret |= DH_CHECK_Q_NOT_PRIME;
199
        /* Check p == 1 mod q  i.e. q divides p - 1 */
200
6.33k
        if (!BN_div(t1, t2, dh->params.p, dh->params.q, ctx))
201
102
            goto err;
202
6.23k
        if (!BN_is_one(t2))
203
6.15k
            *ret |= DH_CHECK_INVALID_Q_VALUE;
204
6.23k
        if (dh->params.j != NULL
205
6.23k
            && BN_cmp(dh->params.j, t1))
206
298
            *ret |= DH_CHECK_INVALID_J_VALUE;
207
6.23k
    }
208
209
8.79k
    r = BN_check_prime(dh->params.p, ctx, NULL);
210
8.79k
    if (r < 0)
211
0
        goto err;
212
8.79k
    if (!r)
213
7.81k
        *ret |= DH_CHECK_P_NOT_PRIME;
214
981
    else if (dh->params.q == NULL) {
215
735
        if (!BN_rshift1(t1, dh->params.p))
216
0
            goto err;
217
735
        r = BN_check_prime(t1, ctx, NULL);
218
735
        if (r < 0)
219
0
            goto err;
220
735
        if (!r)
221
636
            *ret |= DH_CHECK_P_NOT_SAFE_PRIME;
222
735
    }
223
8.79k
    ok = 1;
224
8.89k
 err:
225
8.89k
    BN_CTX_end(ctx);
226
8.89k
    BN_CTX_free(ctx);
227
8.89k
    return ok;
228
8.79k
#endif /* FIPS_MODULE */
229
8.79k
}
230
231
int DH_check_pub_key_ex(const DH *dh, const BIGNUM *pub_key)
232
12.0k
{
233
12.0k
    int errflags = 0;
234
235
12.0k
    if (!DH_check_pub_key(dh, pub_key, &errflags))
236
0
        return 0;
237
238
12.0k
    if ((errflags & DH_CHECK_PUBKEY_TOO_SMALL) != 0)
239
12.0k
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_TOO_SMALL);
240
12.0k
    if ((errflags & DH_CHECK_PUBKEY_TOO_LARGE) != 0)
241
12.0k
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_TOO_LARGE);
242
12.0k
    if ((errflags & DH_CHECK_PUBKEY_INVALID) != 0)
243
12.0k
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_INVALID);
244
245
12.0k
    return errflags == 0;
246
12.0k
}
247
248
/*
249
 * See SP800-56Ar3 Section 5.6.2.3.1 : FFC Full public key validation.
250
 */
251
int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *ret)
252
12.0k
{
253
    /* Don't do any checks at all with an excessively large modulus */
254
12.0k
    if (BN_num_bits(dh->params.p) > OPENSSL_DH_CHECK_MAX_MODULUS_BITS) {
255
0
        ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
256
0
        *ret = DH_MODULUS_TOO_LARGE | DH_CHECK_PUBKEY_INVALID;
257
0
        return 0;
258
0
    }
259
260
12.0k
    if (dh->params.q != NULL && BN_ucmp(dh->params.p, dh->params.q) < 0) {
261
24
        *ret |= DH_CHECK_INVALID_Q_VALUE | DH_CHECK_PUBKEY_INVALID;
262
24
        return 1;
263
24
    }
264
265
12.0k
    return ossl_ffc_validate_public_key(&dh->params, pub_key, ret);
266
12.0k
}
267
268
/*
269
 * See SP800-56Ar3 Section 5.6.2.3.1 : FFC Partial public key validation.
270
 * To only be used with ephemeral FFC public keys generated using the approved
271
 * safe-prime groups.
272
 */
273
int ossl_dh_check_pub_key_partial(const DH *dh, const BIGNUM *pub_key, int *ret)
274
2.04k
{
275
2.04k
    return ossl_ffc_validate_public_key_partial(&dh->params, pub_key, ret)
276
2.04k
           && *ret == 0;
277
2.04k
}
278
279
int ossl_dh_check_priv_key(const DH *dh, const BIGNUM *priv_key, int *ret)
280
42
{
281
42
    int ok = 0;
282
42
    BIGNUM *two_powN = NULL, *upper;
283
284
42
    *ret = 0;
285
42
    two_powN = BN_new();
286
42
    if (two_powN == NULL)
287
0
        return 0;
288
289
42
    if (dh->params.q != NULL) {
290
42
        upper = dh->params.q;
291
42
#ifndef FIPS_MODULE
292
42
    } else if (dh->params.p != NULL) {
293
        /*
294
         * We do not have q so we just check the key is within some
295
         * reasonable range, or the number of bits is equal to dh->length.
296
         */
297
0
        int length = dh->length;
298
299
0
        if (length == 0) {
300
0
            length = BN_num_bits(dh->params.p) - 1;
301
0
            if (BN_num_bits(priv_key) <= length
302
0
                && BN_num_bits(priv_key) > 1)
303
0
                ok = 1;
304
0
        } else if (BN_num_bits(priv_key) == length) {
305
0
            ok = 1;
306
0
        }
307
0
        goto end;
308
0
#endif
309
0
    } else {
310
0
        goto end;
311
0
    }
312
313
    /* Is it from an approved Safe prime group ?*/
314
42
    if (DH_get_nid((DH *)dh) != NID_undef && dh->length != 0) {
315
0
        if (!BN_lshift(two_powN, BN_value_one(), dh->length))
316
0
            goto end;
317
0
        if (BN_cmp(two_powN, dh->params.q) < 0)
318
0
            upper = two_powN;
319
0
    }
320
42
    if (!ossl_ffc_validate_private_key(upper, priv_key, ret))
321
21
        goto end;
322
323
21
    ok = 1;
324
42
end:
325
42
    BN_free(two_powN);
326
42
    return ok;
327
21
}
328
329
/*
330
 * FFC pairwise check from SP800-56A R3.
331
 *    Section 5.6.2.1.4 Owner Assurance of Pair-wise Consistency
332
 */
333
int ossl_dh_check_pairwise(const DH *dh, int return_on_null_numbers)
334
0
{
335
0
    int ret = 0;
336
0
    BN_CTX *ctx = NULL;
337
0
    BIGNUM *pub_key = NULL;
338
0
    OSSL_SELF_TEST *st = NULL;
339
0
    OSSL_CALLBACK *stcb = NULL;
340
0
    void *stcbarg = NULL;
341
342
0
    if (dh->params.p == NULL
343
0
        || dh->params.g == NULL
344
0
        || dh->priv_key == NULL
345
0
        || dh->pub_key == NULL)
346
0
        return return_on_null_numbers;
347
348
0
    OSSL_SELF_TEST_get_callback(dh->libctx, &stcb, &stcbarg);
349
0
    st = OSSL_SELF_TEST_new(stcb, stcbarg);
350
0
    if (st == NULL)
351
0
        goto err;
352
0
    OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_PCT,
353
0
                           OSSL_SELF_TEST_DESC_PCT_DH);
354
355
0
    ctx = BN_CTX_new_ex(dh->libctx);
356
0
    if (ctx == NULL)
357
0
        goto err;
358
0
    pub_key = BN_new();
359
0
    if (pub_key == NULL)
360
0
        goto err;
361
362
    /* recalculate the public key = (g ^ priv) mod p */
363
0
    if (!ossl_dh_generate_public_key(ctx, dh, dh->priv_key, pub_key))
364
0
        goto err;
365
366
#ifdef FIPS_MODULE
367
    {
368
        int len;
369
        unsigned char bytes[1024] = {0};    /* Max key size of 8192 bits */
370
371
        if (BN_num_bytes(pub_key) > (int)sizeof(bytes))
372
            goto err;
373
        len = BN_bn2bin(pub_key, bytes);
374
        OSSL_SELF_TEST_oncorrupt_byte(st, bytes);
375
        if (BN_bin2bn(bytes, len, pub_key) == NULL)
376
            goto err;
377
    }
378
#endif
379
    /* check it matches the existing public_key */
380
0
    ret = BN_cmp(pub_key, dh->pub_key) == 0;
381
0
 err:
382
0
    BN_free(pub_key);
383
0
    BN_CTX_free(ctx);
384
385
0
    OSSL_SELF_TEST_onend(st, ret);
386
0
    OSSL_SELF_TEST_free(st);
387
0
    return ret;
388
0
}