Coverage Report

Created: 2026-06-19 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/dh/dh_check.c
Line
Count
Source
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
0
{
31
0
    int errflags = 0;
32
33
0
    if (!DH_check_params(dh, &errflags))
34
0
        return 0;
35
36
0
    if ((errflags & DH_CHECK_P_NOT_PRIME) != 0)
37
0
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_P_NOT_PRIME);
38
0
    if ((errflags & DH_NOT_SUITABLE_GENERATOR) != 0)
39
0
        ERR_raise(ERR_LIB_DH, DH_R_NOT_SUITABLE_GENERATOR);
40
0
    if ((errflags & DH_MODULUS_TOO_SMALL) != 0)
41
0
        ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_SMALL);
42
0
    if ((errflags & DH_MODULUS_TOO_LARGE) != 0)
43
0
        ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
44
45
0
    return errflags == 0;
46
0
}
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
0
{
72
0
    int ok = 0;
73
0
    BIGNUM *tmp = NULL;
74
0
    BN_CTX *ctx = NULL;
75
76
0
    *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
0
    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
0
    ctx = BN_CTX_new_ex(dh->libctx);
86
0
    if (ctx == NULL)
87
0
        goto err;
88
0
    BN_CTX_start(ctx);
89
0
    tmp = BN_CTX_get(ctx);
90
0
    if (tmp == NULL)
91
0
        goto err;
92
93
0
    if (!BN_is_odd(dh->params.p))
94
0
        *ret |= DH_CHECK_P_NOT_PRIME;
95
0
    if (BN_is_negative(dh->params.g)
96
0
        || BN_is_zero(dh->params.g)
97
0
        || BN_is_one(dh->params.g))
98
0
        *ret |= DH_NOT_SUITABLE_GENERATOR;
99
0
    if (BN_copy(tmp, dh->params.p) == NULL || !BN_sub_word(tmp, 1))
100
0
        goto err;
101
0
    if (BN_cmp(dh->params.g, tmp) >= 0)
102
0
        *ret |= DH_NOT_SUITABLE_GENERATOR;
103
0
    if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS)
104
0
        *ret |= DH_MODULUS_TOO_SMALL;
105
0
    if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS)
106
0
        *ret |= DH_MODULUS_TOO_LARGE;
107
108
0
    ok = 1;
109
0
err:
110
0
    BN_CTX_end(ctx);
111
0
    BN_CTX_free(ctx);
112
0
    return ok;
113
0
}
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
0
{
122
0
    int errflags = 0;
123
124
0
    if (!DH_check(dh, &errflags))
125
0
        return 0;
126
127
0
    if ((errflags & DH_NOT_SUITABLE_GENERATOR) != 0)
128
0
        ERR_raise(ERR_LIB_DH, DH_R_NOT_SUITABLE_GENERATOR);
129
0
    if ((errflags & DH_CHECK_Q_NOT_PRIME) != 0)
130
0
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_Q_NOT_PRIME);
131
0
    if ((errflags & DH_CHECK_INVALID_Q_VALUE) != 0)
132
0
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_INVALID_Q_VALUE);
133
0
    if ((errflags & DH_CHECK_INVALID_J_VALUE) != 0)
134
0
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_INVALID_J_VALUE);
135
0
    if ((errflags & DH_UNABLE_TO_CHECK_GENERATOR) != 0)
136
0
        ERR_raise(ERR_LIB_DH, DH_R_UNABLE_TO_CHECK_GENERATOR);
137
0
    if ((errflags & DH_CHECK_P_NOT_PRIME) != 0)
138
0
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_P_NOT_PRIME);
139
0
    if ((errflags & DH_CHECK_P_NOT_SAFE_PRIME) != 0)
140
0
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_P_NOT_SAFE_PRIME);
141
0
    if ((errflags & DH_MODULUS_TOO_SMALL) != 0)
142
0
        ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_SMALL);
143
0
    if ((errflags & DH_MODULUS_TOO_LARGE) != 0)
144
0
        ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
145
146
0
    return errflags == 0;
147
0
}
148
149
/* Note: according to documentation - this only checks the params */
150
int DH_check(const DH *dh, int *ret)
151
0
{
152
#ifdef FIPS_MODULE
153
    return DH_check_params(dh, ret);
154
#else
155
0
    int ok = 0, r, q_good = 0;
156
0
    BN_CTX *ctx = NULL;
157
0
    BIGNUM *t1 = NULL, *t2 = NULL;
158
0
    int nid = DH_get_nid((DH *)dh);
159
160
0
    *ret = 0;
161
    /* A DH with no modulus or generator cannot be checked. */
162
0
    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
0
    if (nid != NID_undef)
167
0
        return 1;
168
169
    /* Don't do any checks at all with an excessively large modulus */
170
0
    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
0
    if (!DH_check_params(dh, ret))
177
0
        return 0;
178
179
0
    ctx = BN_CTX_new_ex(dh->libctx);
180
0
    if (ctx == NULL)
181
0
        goto err;
182
0
    BN_CTX_start(ctx);
183
0
    t1 = BN_CTX_get(ctx);
184
0
    t2 = BN_CTX_get(ctx);
185
0
    if (t2 == NULL)
186
0
        goto err;
187
188
0
    if (dh->params.q != NULL) {
189
0
        if (BN_ucmp(dh->params.p, dh->params.q) > 0)
190
0
            q_good = 1;
191
0
        else
192
0
            *ret |= DH_CHECK_INVALID_Q_VALUE;
193
0
    }
194
195
0
    if (q_good) {
196
0
        if (BN_cmp(dh->params.g, BN_value_one()) <= 0)
197
0
            *ret |= DH_NOT_SUITABLE_GENERATOR;
198
0
        else if (BN_cmp(dh->params.g, dh->params.p) >= 0)
199
0
            *ret |= DH_NOT_SUITABLE_GENERATOR;
200
0
        else {
201
            /* Check g^q == 1 mod p */
202
0
            if (!BN_mod_exp(t1, dh->params.g, dh->params.q, dh->params.p, ctx))
203
0
                goto err;
204
0
            if (!BN_is_one(t1))
205
0
                *ret |= DH_NOT_SUITABLE_GENERATOR;
206
0
        }
207
0
        r = BN_check_prime(dh->params.q, ctx, NULL);
208
0
        if (r < 0)
209
0
            goto err;
210
0
        if (!r)
211
0
            *ret |= DH_CHECK_Q_NOT_PRIME;
212
        /* Check p == 1 mod q  i.e. q divides p - 1 */
213
0
        if (!BN_div(t1, t2, dh->params.p, dh->params.q, ctx))
214
0
            goto err;
215
0
        if (!BN_is_one(t2))
216
0
            *ret |= DH_CHECK_INVALID_Q_VALUE;
217
0
        if (dh->params.j != NULL
218
0
            && BN_cmp(dh->params.j, t1))
219
0
            *ret |= DH_CHECK_INVALID_J_VALUE;
220
0
    }
221
222
0
    r = BN_check_prime(dh->params.p, ctx, NULL);
223
0
    if (r < 0)
224
0
        goto err;
225
0
    if (!r)
226
0
        *ret |= DH_CHECK_P_NOT_PRIME;
227
0
    else if (dh->params.q == NULL) {
228
0
        if (!BN_rshift1(t1, dh->params.p))
229
0
            goto err;
230
0
        r = BN_check_prime(t1, ctx, NULL);
231
0
        if (r < 0)
232
0
            goto err;
233
0
        if (!r)
234
0
            *ret |= DH_CHECK_P_NOT_SAFE_PRIME;
235
0
    }
236
0
    ok = 1;
237
0
err:
238
0
    BN_CTX_end(ctx);
239
0
    BN_CTX_free(ctx);
240
0
    return ok;
241
0
#endif /* FIPS_MODULE */
242
0
}
243
244
int DH_check_pub_key_ex(const DH *dh, const BIGNUM *pub_key)
245
0
{
246
0
    int errflags = 0;
247
248
0
    if (!DH_check_pub_key(dh, pub_key, &errflags))
249
0
        return 0;
250
251
0
    if ((errflags & DH_CHECK_PUBKEY_TOO_SMALL) != 0)
252
0
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_TOO_SMALL);
253
0
    if ((errflags & DH_CHECK_PUBKEY_TOO_LARGE) != 0)
254
0
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_TOO_LARGE);
255
0
    if ((errflags & DH_CHECK_PUBKEY_INVALID) != 0)
256
0
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_INVALID);
257
258
0
    return errflags == 0;
259
0
}
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
0
{
266
0
    *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
0
    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
0
    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
0
    if (dh->params.q != NULL && BN_ucmp(dh->params.p, dh->params.q) < 0) {
283
0
        *ret |= DH_CHECK_INVALID_Q_VALUE | DH_CHECK_PUBKEY_INVALID;
284
0
        return 1;
285
0
    }
286
287
0
    return ossl_ffc_validate_public_key(&dh->params, pub_key, ret);
288
0
}
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
0
{
297
0
    return ossl_ffc_validate_public_key_partial(&dh->params, pub_key, ret)
298
0
        && *ret == 0;
299
0
}
300
301
int ossl_dh_check_priv_key(const DH *dh, const BIGNUM *priv_key, int *ret)
302
0
{
303
0
    int ok = 0;
304
0
    BIGNUM *two_powN = NULL, *upper;
305
306
0
    *ret = 0;
307
0
    two_powN = BN_new();
308
0
    if (two_powN == NULL)
309
0
        return 0;
310
311
0
    if (dh->params.q != NULL) {
312
0
        upper = dh->params.q;
313
0
#ifndef FIPS_MODULE
314
0
    } 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
0
    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
0
    if (!ossl_ffc_validate_private_key(upper, priv_key, ret))
343
0
        goto end;
344
345
0
    ok = 1;
346
0
end:
347
0
    BN_free(two_powN);
348
0
    return ok;
349
0
}
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
}