Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/crypto/dh/dh_check.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2024 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 "dh_local.h"
20
#include "crypto/dh.h"
21
22
/*-
23
 * Check that p and g are suitable enough
24
 *
25
 * p is odd
26
 * 1 < g < p - 1
27
 */
28
int DH_check_params_ex(const DH *dh)
29
7.85k
{
30
7.85k
    int errflags = 0;
31
32
7.85k
    if (!DH_check_params(dh, &errflags))
33
0
        return 0;
34
35
7.85k
    if ((errflags & DH_CHECK_P_NOT_PRIME) != 0)
36
7.85k
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_P_NOT_PRIME);
37
7.85k
    if ((errflags & DH_NOT_SUITABLE_GENERATOR) != 0)
38
7.85k
        ERR_raise(ERR_LIB_DH, DH_R_NOT_SUITABLE_GENERATOR);
39
7.85k
    if ((errflags & DH_MODULUS_TOO_SMALL) != 0)
40
7.85k
        ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_SMALL);
41
7.85k
    if ((errflags & DH_MODULUS_TOO_LARGE) != 0)
42
7.85k
        ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
43
44
7.85k
    return errflags == 0;
45
7.85k
}
46
47
#ifdef FIPS_MODULE
48
int DH_check_params(const DH *dh, int *ret)
49
{
50
    int nid;
51
52
    *ret = 0;
53
    /*
54
     * SP800-56A R3 Section 5.5.2 Assurances of Domain Parameter Validity
55
     * (1a) The domain parameters correspond to any approved safe prime group.
56
     */
57
    nid = DH_get_nid((DH *)dh);
58
    if (nid != NID_undef)
59
        return 1;
60
    /*
61
     * OR
62
     * (2b) FFC domain params conform to FIPS-186-4 explicit domain param
63
     * validity tests.
64
     */
65
    return ossl_ffc_params_FIPS186_4_validate(dh->libctx, &dh->params,
66
        FFC_PARAM_TYPE_DH, ret, NULL);
67
}
68
#else
69
int DH_check_params(const DH *dh, int *ret)
70
3.22k
{
71
3.22k
    int ok = 0;
72
3.22k
    BIGNUM *tmp = NULL;
73
3.22k
    BN_CTX *ctx = NULL;
74
75
3.22k
    *ret = 0;
76
3.22k
    ctx = BN_CTX_new_ex(dh->libctx);
77
3.22k
    if (ctx == NULL)
78
0
        goto err;
79
3.22k
    BN_CTX_start(ctx);
80
3.22k
    tmp = BN_CTX_get(ctx);
81
3.22k
    if (tmp == NULL)
82
0
        goto err;
83
84
3.22k
    if (!BN_is_odd(dh->params.p))
85
1.21k
        *ret |= DH_CHECK_P_NOT_PRIME;
86
3.22k
    if (BN_is_negative(dh->params.g)
87
3.22k
        || BN_is_zero(dh->params.g)
88
2.44k
        || BN_is_one(dh->params.g))
89
797
        *ret |= DH_NOT_SUITABLE_GENERATOR;
90
3.22k
    if (BN_copy(tmp, dh->params.p) == NULL || !BN_sub_word(tmp, 1))
91
0
        goto err;
92
3.22k
    if (BN_cmp(dh->params.g, tmp) >= 0)
93
225
        *ret |= DH_NOT_SUITABLE_GENERATOR;
94
3.22k
    if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS)
95
1.27k
        *ret |= DH_MODULUS_TOO_SMALL;
96
3.22k
    if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS)
97
0
        *ret |= DH_MODULUS_TOO_LARGE;
98
99
3.22k
    ok = 1;
100
3.22k
err:
101
3.22k
    BN_CTX_end(ctx);
102
3.22k
    BN_CTX_free(ctx);
103
3.22k
    return ok;
104
3.22k
}
105
#endif /* FIPS_MODULE */
106
107
/*-
108
 * Check that p is a safe prime and
109
 * g is a suitable generator.
110
 */
111
int DH_check_ex(const DH *dh)
112
5.66k
{
113
5.66k
    int errflags = 0;
114
115
5.66k
    if (!DH_check(dh, &errflags))
116
57
        return 0;
117
118
5.61k
    if ((errflags & DH_NOT_SUITABLE_GENERATOR) != 0)
119
5.61k
        ERR_raise(ERR_LIB_DH, DH_R_NOT_SUITABLE_GENERATOR);
120
5.61k
    if ((errflags & DH_CHECK_Q_NOT_PRIME) != 0)
121
5.61k
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_Q_NOT_PRIME);
122
5.61k
    if ((errflags & DH_CHECK_INVALID_Q_VALUE) != 0)
123
5.61k
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_INVALID_Q_VALUE);
124
5.61k
    if ((errflags & DH_CHECK_INVALID_J_VALUE) != 0)
125
5.61k
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_INVALID_J_VALUE);
126
5.61k
    if ((errflags & DH_UNABLE_TO_CHECK_GENERATOR) != 0)
127
5.61k
        ERR_raise(ERR_LIB_DH, DH_R_UNABLE_TO_CHECK_GENERATOR);
128
5.61k
    if ((errflags & DH_CHECK_P_NOT_PRIME) != 0)
129
5.61k
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_P_NOT_PRIME);
130
5.61k
    if ((errflags & DH_CHECK_P_NOT_SAFE_PRIME) != 0)
131
5.61k
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_P_NOT_SAFE_PRIME);
132
5.61k
    if ((errflags & DH_MODULUS_TOO_SMALL) != 0)
133
5.61k
        ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_SMALL);
134
5.61k
    if ((errflags & DH_MODULUS_TOO_LARGE) != 0)
135
5.61k
        ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
136
137
5.61k
    return errflags == 0;
138
5.66k
}
139
140
/* Note: according to documentation - this only checks the params */
141
int DH_check(const DH *dh, int *ret)
142
1.79k
{
143
#ifdef FIPS_MODULE
144
    return DH_check_params(dh, ret);
145
#else
146
1.79k
    int ok = 0, r, q_good = 0;
147
1.79k
    BN_CTX *ctx = NULL;
148
1.79k
    BIGNUM *t1 = NULL, *t2 = NULL;
149
1.79k
    int nid = DH_get_nid((DH *)dh);
150
151
1.79k
    *ret = 0;
152
1.79k
    if (nid != NID_undef)
153
0
        return 1;
154
155
    /* Don't do any checks at all with an excessively large modulus */
156
1.79k
    if (BN_num_bits(dh->params.p) > OPENSSL_DH_CHECK_MAX_MODULUS_BITS) {
157
0
        ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
158
0
        *ret = DH_MODULUS_TOO_LARGE | DH_CHECK_P_NOT_PRIME;
159
0
        return 0;
160
0
    }
161
162
1.79k
    if (!DH_check_params(dh, ret))
163
0
        return 0;
164
165
1.79k
    ctx = BN_CTX_new_ex(dh->libctx);
166
1.79k
    if (ctx == NULL)
167
0
        goto err;
168
1.79k
    BN_CTX_start(ctx);
169
1.79k
    t1 = BN_CTX_get(ctx);
170
1.79k
    t2 = BN_CTX_get(ctx);
171
1.79k
    if (t2 == NULL)
172
0
        goto err;
173
174
1.79k
    if (dh->params.q != NULL) {
175
1.22k
        if (BN_ucmp(dh->params.p, dh->params.q) > 0)
176
1.13k
            q_good = 1;
177
84
        else
178
84
            *ret |= DH_CHECK_INVALID_Q_VALUE;
179
1.22k
    }
180
181
1.79k
    if (q_good) {
182
1.13k
        if (BN_cmp(dh->params.g, BN_value_one()) <= 0)
183
20
            *ret |= DH_NOT_SUITABLE_GENERATOR;
184
1.11k
        else if (BN_cmp(dh->params.g, dh->params.p) >= 0)
185
3
            *ret |= DH_NOT_SUITABLE_GENERATOR;
186
1.11k
        else {
187
            /* Check g^q == 1 mod p */
188
1.11k
            if (!BN_mod_exp(t1, dh->params.g, dh->params.q, dh->params.p, ctx))
189
0
                goto err;
190
1.11k
            if (!BN_is_one(t1))
191
1.10k
                *ret |= DH_NOT_SUITABLE_GENERATOR;
192
1.11k
        }
193
1.13k
        r = BN_check_prime(dh->params.q, ctx, NULL);
194
1.13k
        if (r < 0)
195
0
            goto err;
196
1.13k
        if (!r)
197
1.04k
            *ret |= DH_CHECK_Q_NOT_PRIME;
198
        /* Check p == 1 mod q  i.e. q divides p - 1 */
199
1.13k
        if (!BN_div(t1, t2, dh->params.p, dh->params.q, ctx))
200
17
            goto err;
201
1.12k
        if (!BN_is_one(t2))
202
1.10k
            *ret |= DH_CHECK_INVALID_Q_VALUE;
203
1.12k
        if (dh->params.j != NULL
204
17
            && BN_cmp(dh->params.j, t1))
205
15
            *ret |= DH_CHECK_INVALID_J_VALUE;
206
1.12k
    }
207
208
1.77k
    r = BN_check_prime(dh->params.p, ctx, NULL);
209
1.77k
    if (r < 0)
210
0
        goto err;
211
1.77k
    if (!r)
212
1.53k
        *ret |= DH_CHECK_P_NOT_PRIME;
213
242
    else if (dh->params.q == NULL) {
214
183
        if (!BN_rshift1(t1, dh->params.p))
215
0
            goto err;
216
183
        r = BN_check_prime(t1, ctx, NULL);
217
183
        if (r < 0)
218
0
            goto err;
219
183
        if (!r)
220
158
            *ret |= DH_CHECK_P_NOT_SAFE_PRIME;
221
183
    }
222
1.77k
    ok = 1;
223
1.79k
err:
224
1.79k
    BN_CTX_end(ctx);
225
1.79k
    BN_CTX_free(ctx);
226
1.79k
    return ok;
227
1.77k
#endif /* FIPS_MODULE */
228
1.77k
}
229
230
int DH_check_pub_key_ex(const DH *dh, const BIGNUM *pub_key)
231
9.87k
{
232
9.87k
    int errflags = 0;
233
234
9.87k
    if (!DH_check_pub_key(dh, pub_key, &errflags))
235
0
        return 0;
236
237
9.87k
    if ((errflags & DH_CHECK_PUBKEY_TOO_SMALL) != 0)
238
9.87k
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_TOO_SMALL);
239
9.87k
    if ((errflags & DH_CHECK_PUBKEY_TOO_LARGE) != 0)
240
9.87k
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_TOO_LARGE);
241
9.87k
    if ((errflags & DH_CHECK_PUBKEY_INVALID) != 0)
242
9.87k
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_INVALID);
243
244
9.87k
    return errflags == 0;
245
9.87k
}
246
247
/*
248
 * See SP800-56Ar3 Section 5.6.2.3.1 : FFC Full public key validation.
249
 */
250
int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *ret)
251
1.92k
{
252
    /* Don't do any checks at all with an excessively large modulus */
253
1.92k
    if (BN_num_bits(dh->params.p) > OPENSSL_DH_CHECK_MAX_MODULUS_BITS) {
254
0
        ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
255
0
        *ret = DH_MODULUS_TOO_LARGE | DH_CHECK_PUBKEY_INVALID;
256
0
        return 0;
257
0
    }
258
259
1.92k
    if (dh->params.q != NULL && BN_ucmp(dh->params.p, dh->params.q) < 0) {
260
4
        *ret |= DH_CHECK_INVALID_Q_VALUE | DH_CHECK_PUBKEY_INVALID;
261
4
        return 1;
262
4
    }
263
264
1.92k
    return ossl_ffc_validate_public_key(&dh->params, pub_key, ret);
265
1.92k
}
266
267
/*
268
 * See SP800-56Ar3 Section 5.6.2.3.1 : FFC Partial public key validation.
269
 * To only be used with ephemeral FFC public keys generated using the approved
270
 * safe-prime groups.
271
 */
272
int ossl_dh_check_pub_key_partial(const DH *dh, const BIGNUM *pub_key, int *ret)
273
1.40k
{
274
1.40k
    return ossl_ffc_validate_public_key_partial(&dh->params, pub_key, ret)
275
1.40k
        && *ret == 0;
276
1.40k
}
277
278
int ossl_dh_check_priv_key(const DH *dh, const BIGNUM *priv_key, int *ret)
279
40
{
280
40
    int ok = 0;
281
40
    BIGNUM *two_powN = NULL, *upper;
282
283
40
    *ret = 0;
284
40
    two_powN = BN_new();
285
40
    if (two_powN == NULL)
286
0
        return 0;
287
288
40
    if (dh->params.q != NULL) {
289
40
        upper = dh->params.q;
290
40
#ifndef FIPS_MODULE
291
40
    } else if (dh->params.p != NULL) {
292
        /*
293
         * We do not have q so we just check the key is within some
294
         * reasonable range, or the number of bits is equal to dh->length.
295
         */
296
0
        int length = dh->length;
297
298
0
        if (length == 0) {
299
0
            length = BN_num_bits(dh->params.p) - 1;
300
0
            if (BN_num_bits(priv_key) <= length
301
0
                && BN_num_bits(priv_key) > 1)
302
0
                ok = 1;
303
0
        } else if (BN_num_bits(priv_key) == length) {
304
0
            ok = 1;
305
0
        }
306
0
        goto end;
307
0
#endif
308
0
    } else {
309
0
        goto end;
310
0
    }
311
312
    /* Is it from an approved Safe prime group ?*/
313
40
    if (DH_get_nid((DH *)dh) != NID_undef && dh->length != 0) {
314
0
        if (!BN_lshift(two_powN, BN_value_one(), dh->length))
315
0
            goto end;
316
0
        if (BN_cmp(two_powN, dh->params.q) < 0)
317
0
            upper = two_powN;
318
0
    }
319
40
    if (!ossl_ffc_validate_private_key(upper, priv_key, ret))
320
14
        goto end;
321
322
26
    ok = 1;
323
40
end:
324
40
    BN_free(two_powN);
325
40
    return ok;
326
26
}
327
328
/*
329
 * FFC pairwise check from SP800-56A R3.
330
 *    Section 5.6.2.1.4 Owner Assurance of Pair-wise Consistency
331
 */
332
int ossl_dh_check_pairwise(const DH *dh)
333
0
{
334
0
    int ret = 0;
335
0
    BN_CTX *ctx = NULL;
336
0
    BIGNUM *pub_key = NULL;
337
338
0
    if (dh->params.p == NULL
339
0
        || dh->params.g == NULL
340
0
        || dh->priv_key == NULL
341
0
        || dh->pub_key == NULL)
342
0
        return 0;
343
344
0
    ctx = BN_CTX_new_ex(dh->libctx);
345
0
    if (ctx == NULL)
346
0
        goto err;
347
0
    pub_key = BN_new();
348
0
    if (pub_key == NULL)
349
0
        goto err;
350
351
    /* recalculate the public key = (g ^ priv) mod p */
352
0
    if (!ossl_dh_generate_public_key(ctx, dh, dh->priv_key, pub_key))
353
0
        goto err;
354
    /* check it matches the existing public_key */
355
0
    ret = BN_cmp(pub_key, dh->pub_key) == 0;
356
0
err:
357
0
    BN_free(pub_key);
358
0
    BN_CTX_free(ctx);
359
0
    return ret;
360
0
}