Coverage Report

Created: 2025-08-25 06:30

/src/openssl/crypto/dh/dh_check.c
Line
Count
Source (jump to first uncovered line)
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 <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
0
    ctx = BN_CTX_new_ex(dh->libctx);
78
0
    if (ctx == NULL)
79
0
        goto err;
80
0
    BN_CTX_start(ctx);
81
0
    tmp = BN_CTX_get(ctx);
82
0
    if (tmp == NULL)
83
0
        goto err;
84
85
0
    if (!BN_is_odd(dh->params.p))
86
0
        *ret |= DH_CHECK_P_NOT_PRIME;
87
0
    if (BN_is_negative(dh->params.g)
88
0
        || BN_is_zero(dh->params.g)
89
0
        || BN_is_one(dh->params.g))
90
0
        *ret |= DH_NOT_SUITABLE_GENERATOR;
91
0
    if (BN_copy(tmp, dh->params.p) == NULL || !BN_sub_word(tmp, 1))
92
0
        goto err;
93
0
    if (BN_cmp(dh->params.g, tmp) >= 0)
94
0
        *ret |= DH_NOT_SUITABLE_GENERATOR;
95
0
    if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS)
96
0
        *ret |= DH_MODULUS_TOO_SMALL;
97
0
    if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS)
98
0
        *ret |= DH_MODULUS_TOO_LARGE;
99
100
0
    ok = 1;
101
0
 err:
102
0
    BN_CTX_end(ctx);
103
0
    BN_CTX_free(ctx);
104
0
    return ok;
105
0
}
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
0
{
114
0
    int errflags = 0;
115
116
0
    if (!DH_check(dh, &errflags))
117
0
        return 0;
118
119
0
    if ((errflags & DH_NOT_SUITABLE_GENERATOR) != 0)
120
0
        ERR_raise(ERR_LIB_DH, DH_R_NOT_SUITABLE_GENERATOR);
121
0
    if ((errflags & DH_CHECK_Q_NOT_PRIME) != 0)
122
0
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_Q_NOT_PRIME);
123
0
    if ((errflags & DH_CHECK_INVALID_Q_VALUE) != 0)
124
0
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_INVALID_Q_VALUE);
125
0
    if ((errflags & DH_CHECK_INVALID_J_VALUE) != 0)
126
0
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_INVALID_J_VALUE);
127
0
    if ((errflags & DH_UNABLE_TO_CHECK_GENERATOR) != 0)
128
0
        ERR_raise(ERR_LIB_DH, DH_R_UNABLE_TO_CHECK_GENERATOR);
129
0
    if ((errflags & DH_CHECK_P_NOT_PRIME) != 0)
130
0
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_P_NOT_PRIME);
131
0
    if ((errflags & DH_CHECK_P_NOT_SAFE_PRIME) != 0)
132
0
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_P_NOT_SAFE_PRIME);
133
0
    if ((errflags & DH_MODULUS_TOO_SMALL) != 0)
134
0
        ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_SMALL);
135
0
    if ((errflags & DH_MODULUS_TOO_LARGE) != 0)
136
0
        ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
137
138
0
    return errflags == 0;
139
0
}
140
141
/* Note: according to documentation - this only checks the params */
142
int DH_check(const DH *dh, int *ret)
143
0
{
144
#ifdef FIPS_MODULE
145
    return DH_check_params(dh, ret);
146
#else
147
0
    int ok = 0, r, q_good = 0;
148
0
    BN_CTX *ctx = NULL;
149
0
    BIGNUM *t1 = NULL, *t2 = NULL;
150
0
    int nid = DH_get_nid((DH *)dh);
151
152
0
    *ret = 0;
153
0
    if (nid != NID_undef)
154
0
        return 1;
155
156
    /* Don't do any checks at all with an excessively large modulus */
157
0
    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
0
    if (!DH_check_params(dh, ret))
164
0
        return 0;
165
166
0
    ctx = BN_CTX_new_ex(dh->libctx);
167
0
    if (ctx == NULL)
168
0
        goto err;
169
0
    BN_CTX_start(ctx);
170
0
    t1 = BN_CTX_get(ctx);
171
0
    t2 = BN_CTX_get(ctx);
172
0
    if (t2 == NULL)
173
0
        goto err;
174
175
0
    if (dh->params.q != NULL) {
176
0
        if (BN_ucmp(dh->params.p, dh->params.q) > 0)
177
0
            q_good = 1;
178
0
        else
179
0
            *ret |= DH_CHECK_INVALID_Q_VALUE;
180
0
    }
181
182
0
    if (q_good) {
183
0
        if (BN_cmp(dh->params.g, BN_value_one()) <= 0)
184
0
            *ret |= DH_NOT_SUITABLE_GENERATOR;
185
0
        else if (BN_cmp(dh->params.g, dh->params.p) >= 0)
186
0
            *ret |= DH_NOT_SUITABLE_GENERATOR;
187
0
        else {
188
            /* Check g^q == 1 mod p */
189
0
            if (!BN_mod_exp(t1, dh->params.g, dh->params.q, dh->params.p, ctx))
190
0
                goto err;
191
0
            if (!BN_is_one(t1))
192
0
                *ret |= DH_NOT_SUITABLE_GENERATOR;
193
0
        }
194
0
        r = BN_check_prime(dh->params.q, ctx, NULL);
195
0
        if (r < 0)
196
0
            goto err;
197
0
        if (!r)
198
0
            *ret |= DH_CHECK_Q_NOT_PRIME;
199
        /* Check p == 1 mod q  i.e. q divides p - 1 */
200
0
        if (!BN_div(t1, t2, dh->params.p, dh->params.q, ctx))
201
0
            goto err;
202
0
        if (!BN_is_one(t2))
203
0
            *ret |= DH_CHECK_INVALID_Q_VALUE;
204
0
        if (dh->params.j != NULL
205
0
            && BN_cmp(dh->params.j, t1))
206
0
            *ret |= DH_CHECK_INVALID_J_VALUE;
207
0
    }
208
209
0
    r = BN_check_prime(dh->params.p, ctx, NULL);
210
0
    if (r < 0)
211
0
        goto err;
212
0
    if (!r)
213
0
        *ret |= DH_CHECK_P_NOT_PRIME;
214
0
    else if (dh->params.q == NULL) {
215
0
        if (!BN_rshift1(t1, dh->params.p))
216
0
            goto err;
217
0
        r = BN_check_prime(t1, ctx, NULL);
218
0
        if (r < 0)
219
0
            goto err;
220
0
        if (!r)
221
0
            *ret |= DH_CHECK_P_NOT_SAFE_PRIME;
222
0
    }
223
0
    ok = 1;
224
0
 err:
225
0
    BN_CTX_end(ctx);
226
0
    BN_CTX_free(ctx);
227
0
    return ok;
228
0
#endif /* FIPS_MODULE */
229
0
}
230
231
int DH_check_pub_key_ex(const DH *dh, const BIGNUM *pub_key)
232
0
{
233
0
    int errflags = 0;
234
235
0
    if (!DH_check_pub_key(dh, pub_key, &errflags))
236
0
        return 0;
237
238
0
    if ((errflags & DH_CHECK_PUBKEY_TOO_SMALL) != 0)
239
0
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_TOO_SMALL);
240
0
    if ((errflags & DH_CHECK_PUBKEY_TOO_LARGE) != 0)
241
0
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_TOO_LARGE);
242
0
    if ((errflags & DH_CHECK_PUBKEY_INVALID) != 0)
243
0
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_INVALID);
244
245
0
    return errflags == 0;
246
0
}
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
0
{
253
    /* Don't do any checks at all with an excessively large modulus */
254
0
    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
0
    if (dh->params.q != NULL && BN_ucmp(dh->params.p, dh->params.q) < 0) {
261
0
        *ret |= DH_CHECK_INVALID_Q_VALUE | DH_CHECK_PUBKEY_INVALID;
262
0
        return 1;
263
0
    }
264
265
0
    return ossl_ffc_validate_public_key(&dh->params, pub_key, ret);
266
0
}
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
0
{
275
0
    return ossl_ffc_validate_public_key_partial(&dh->params, pub_key, ret)
276
0
           && *ret == 0;
277
0
}
278
279
int ossl_dh_check_priv_key(const DH *dh, const BIGNUM *priv_key, int *ret)
280
0
{
281
0
    int ok = 0;
282
0
    BIGNUM *two_powN = NULL, *upper;
283
284
0
    *ret = 0;
285
0
    two_powN = BN_new();
286
0
    if (two_powN == NULL)
287
0
        return 0;
288
289
0
    if (dh->params.q != NULL) {
290
0
        upper = dh->params.q;
291
0
#ifndef FIPS_MODULE
292
0
    } 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
0
    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
0
    if (!ossl_ffc_validate_private_key(upper, priv_key, ret))
321
0
        goto end;
322
323
0
    ok = 1;
324
0
end:
325
0
    BN_free(two_powN);
326
0
    return ok;
327
0
}
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
}