Coverage Report

Created: 2023-09-25 06:45

/src/openssl30/crypto/dh/dh_check.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2023 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
2.81k
{
30
2.81k
    int errflags = 0;
31
32
2.81k
    if (!DH_check_params(dh, &errflags))
33
0
        return 0;
34
35
2.81k
    if ((errflags & DH_CHECK_P_NOT_PRIME) != 0)
36
2.81k
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_P_NOT_PRIME);
37
2.81k
    if ((errflags & DH_NOT_SUITABLE_GENERATOR) != 0)
38
2.81k
        ERR_raise(ERR_LIB_DH, DH_R_NOT_SUITABLE_GENERATOR);
39
2.81k
    if ((errflags & DH_MODULUS_TOO_SMALL) != 0)
40
2.81k
        ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_SMALL);
41
2.81k
    if ((errflags & DH_MODULUS_TOO_LARGE) != 0)
42
2.81k
        ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
43
44
2.81k
    return errflags == 0;
45
2.81k
}
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
5.08k
{
71
5.08k
    int ok = 0;
72
5.08k
    BIGNUM *tmp = NULL;
73
5.08k
    BN_CTX *ctx = NULL;
74
75
5.08k
    *ret = 0;
76
5.08k
    ctx = BN_CTX_new_ex(dh->libctx);
77
5.08k
    if (ctx == NULL)
78
0
        goto err;
79
5.08k
    BN_CTX_start(ctx);
80
5.08k
    tmp = BN_CTX_get(ctx);
81
5.08k
    if (tmp == NULL)
82
0
        goto err;
83
84
5.08k
    if (!BN_is_odd(dh->params.p))
85
1.89k
        *ret |= DH_CHECK_P_NOT_PRIME;
86
5.08k
    if (BN_is_negative(dh->params.g)
87
5.08k
        || BN_is_zero(dh->params.g)
88
5.08k
        || BN_is_one(dh->params.g))
89
641
        *ret |= DH_NOT_SUITABLE_GENERATOR;
90
5.08k
    if (BN_copy(tmp, dh->params.p) == NULL || !BN_sub_word(tmp, 1))
91
0
        goto err;
92
5.08k
    if (BN_cmp(dh->params.g, tmp) >= 0)
93
340
        *ret |= DH_NOT_SUITABLE_GENERATOR;
94
5.08k
    if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS)
95
1.44k
        *ret |= DH_MODULUS_TOO_SMALL;
96
5.08k
    if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS)
97
42
        *ret |= DH_MODULUS_TOO_LARGE;
98
99
5.08k
    ok = 1;
100
5.08k
 err:
101
5.08k
    BN_CTX_end(ctx);
102
5.08k
    BN_CTX_free(ctx);
103
5.08k
    return ok;
104
5.08k
}
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
2.26k
{
113
2.26k
    int errflags = 0;
114
115
2.26k
    if (!DH_check(dh, &errflags))
116
27
        return 0;
117
118
2.24k
    if ((errflags & DH_NOT_SUITABLE_GENERATOR) != 0)
119
2.24k
        ERR_raise(ERR_LIB_DH, DH_R_NOT_SUITABLE_GENERATOR);
120
2.24k
    if ((errflags & DH_CHECK_Q_NOT_PRIME) != 0)
121
2.24k
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_Q_NOT_PRIME);
122
2.24k
    if ((errflags & DH_CHECK_INVALID_Q_VALUE) != 0)
123
2.24k
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_INVALID_Q_VALUE);
124
2.24k
    if ((errflags & DH_CHECK_INVALID_J_VALUE) != 0)
125
2.24k
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_INVALID_J_VALUE);
126
2.24k
    if ((errflags & DH_UNABLE_TO_CHECK_GENERATOR) != 0)
127
2.24k
        ERR_raise(ERR_LIB_DH, DH_R_UNABLE_TO_CHECK_GENERATOR);
128
2.24k
    if ((errflags & DH_CHECK_P_NOT_PRIME) != 0)
129
2.24k
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_P_NOT_PRIME);
130
2.24k
    if ((errflags & DH_CHECK_P_NOT_SAFE_PRIME) != 0)
131
2.24k
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_P_NOT_SAFE_PRIME);
132
2.24k
    if ((errflags & DH_MODULUS_TOO_SMALL) != 0)
133
2.24k
        ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_SMALL);
134
2.24k
    if ((errflags & DH_MODULUS_TOO_LARGE) != 0)
135
2.24k
        ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
136
137
2.24k
    return errflags == 0;
138
2.26k
}
139
140
/* Note: according to documentation - this only checks the params */
141
int DH_check(const DH *dh, int *ret)
142
2.26k
{
143
#ifdef FIPS_MODULE
144
    return DH_check_params(dh, ret);
145
#else
146
2.26k
    int ok = 0, r, q_good = 0;
147
2.26k
    BN_CTX *ctx = NULL;
148
2.26k
    BIGNUM *t1 = NULL, *t2 = NULL;
149
2.26k
    int nid = DH_get_nid((DH *)dh);
150
151
2.26k
    *ret = 0;
152
2.26k
    if (nid != NID_undef)
153
0
        return 1;
154
155
    /* Don't do any checks at all with an excessively large modulus */
156
2.26k
    if (BN_num_bits(dh->params.p) > OPENSSL_DH_CHECK_MAX_MODULUS_BITS) {
157
7
        ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
158
7
        *ret = DH_MODULUS_TOO_LARGE | DH_CHECK_P_NOT_PRIME;
159
7
        return 0;
160
7
    }
161
162
2.26k
    if (!DH_check_params(dh, ret))
163
0
        return 0;
164
165
2.26k
    ctx = BN_CTX_new_ex(dh->libctx);
166
2.26k
    if (ctx == NULL)
167
0
        goto err;
168
2.26k
    BN_CTX_start(ctx);
169
2.26k
    t1 = BN_CTX_get(ctx);
170
2.26k
    t2 = BN_CTX_get(ctx);
171
2.26k
    if (t2 == NULL)
172
0
        goto err;
173
174
2.26k
    if (dh->params.q != NULL) {
175
1.82k
        if (BN_ucmp(dh->params.p, dh->params.q) > 0)
176
1.75k
            q_good = 1;
177
78
        else
178
78
            *ret |= DH_CHECK_INVALID_Q_VALUE;
179
1.82k
    }
180
181
2.26k
    if (q_good) {
182
1.75k
        if (BN_cmp(dh->params.g, BN_value_one()) <= 0)
183
17
            *ret |= DH_NOT_SUITABLE_GENERATOR;
184
1.73k
        else if (BN_cmp(dh->params.g, dh->params.p) >= 0)
185
5
            *ret |= DH_NOT_SUITABLE_GENERATOR;
186
1.72k
        else {
187
            /* Check g^q == 1 mod p */
188
1.72k
            if (!BN_mod_exp(t1, dh->params.g, dh->params.q, dh->params.p, ctx))
189
0
                goto err;
190
1.72k
            if (!BN_is_one(t1))
191
1.71k
                *ret |= DH_NOT_SUITABLE_GENERATOR;
192
1.72k
        }
193
1.75k
        r = BN_check_prime(dh->params.q, ctx, NULL);
194
1.75k
        if (r < 0)
195
0
            goto err;
196
1.75k
        if (!r)
197
1.60k
            *ret |= DH_CHECK_Q_NOT_PRIME;
198
        /* Check p == 1 mod q  i.e. q divides p - 1 */
199
1.75k
        if (!BN_div(t1, t2, dh->params.p, dh->params.q, ctx))
200
20
            goto err;
201
1.73k
        if (!BN_is_one(t2))
202
1.70k
            *ret |= DH_CHECK_INVALID_Q_VALUE;
203
1.73k
        if (dh->params.j != NULL
204
1.73k
            && BN_cmp(dh->params.j, t1))
205
66
            *ret |= DH_CHECK_INVALID_J_VALUE;
206
1.73k
    }
207
208
2.24k
    r = BN_check_prime(dh->params.p, ctx, NULL);
209
2.24k
    if (r < 0)
210
0
        goto err;
211
2.24k
    if (!r)
212
2.03k
        *ret |= DH_CHECK_P_NOT_PRIME;
213
208
    else if (dh->params.q == NULL) {
214
155
        if (!BN_rshift1(t1, dh->params.p))
215
0
            goto err;
216
155
        r = BN_check_prime(t1, ctx, NULL);
217
155
        if (r < 0)
218
0
            goto err;
219
155
        if (!r)
220
130
            *ret |= DH_CHECK_P_NOT_SAFE_PRIME;
221
155
    }
222
2.24k
    ok = 1;
223
2.26k
 err:
224
2.26k
    BN_CTX_end(ctx);
225
2.26k
    BN_CTX_free(ctx);
226
2.26k
    return ok;
227
2.24k
#endif /* FIPS_MODULE */
228
2.24k
}
229
230
int DH_check_pub_key_ex(const DH *dh, const BIGNUM *pub_key)
231
0
{
232
0
    int errflags = 0;
233
234
0
    if (!DH_check_pub_key(dh, pub_key, &errflags))
235
0
        return 0;
236
237
0
    if ((errflags & DH_CHECK_PUBKEY_TOO_SMALL) != 0)
238
0
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_TOO_SMALL);
239
0
    if ((errflags & DH_CHECK_PUBKEY_TOO_LARGE) != 0)
240
0
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_TOO_LARGE);
241
0
    if ((errflags & DH_CHECK_PUBKEY_INVALID) != 0)
242
0
        ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_INVALID);
243
244
0
    return errflags == 0;
245
0
}
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
3.99k
{
252
3.99k
    return ossl_ffc_validate_public_key(&dh->params, pub_key, ret);
253
3.99k
}
254
255
/*
256
 * See SP800-56Ar3 Section 5.6.2.3.1 : FFC Partial public key validation.
257
 * To only be used with ephemeral FFC public keys generated using the approved
258
 * safe-prime groups.
259
 */
260
int ossl_dh_check_pub_key_partial(const DH *dh, const BIGNUM *pub_key, int *ret)
261
606
{
262
606
    return ossl_ffc_validate_public_key_partial(&dh->params, pub_key, ret);
263
606
}
264
265
int ossl_dh_check_priv_key(const DH *dh, const BIGNUM *priv_key, int *ret)
266
5
{
267
5
    int ok = 0;
268
5
    BIGNUM *two_powN = NULL, *upper;
269
270
5
    *ret = 0;
271
5
    two_powN = BN_new();
272
5
    if (two_powN == NULL)
273
0
        return 0;
274
275
5
    if (dh->params.q != NULL) {
276
5
        upper = dh->params.q;
277
5
#ifndef FIPS_MODULE
278
5
    } else if (dh->params.p != NULL) {
279
        /*
280
         * We do not have q so we just check the key is within some
281
         * reasonable range, or the number of bits is equal to dh->length.
282
         */
283
0
        int length = dh->length;
284
285
0
        if (length == 0) {
286
0
            length = BN_num_bits(dh->params.p) - 1;
287
0
            if (BN_num_bits(priv_key) <= length
288
0
                && BN_num_bits(priv_key) > 1)
289
0
                ok = 1;
290
0
        } else if (BN_num_bits(priv_key) == length) {
291
0
            ok = 1;
292
0
        }
293
0
        goto end;
294
0
#endif
295
0
    } else {
296
0
        goto end;
297
0
    }
298
299
    /* Is it from an approved Safe prime group ?*/
300
5
    if (DH_get_nid((DH *)dh) != NID_undef && dh->length != 0) {
301
0
        if (!BN_lshift(two_powN, BN_value_one(), dh->length))
302
0
            goto end;
303
0
        if (BN_cmp(two_powN, dh->params.q) < 0)
304
0
            upper = two_powN;
305
0
    }
306
5
    if (!ossl_ffc_validate_private_key(upper, priv_key, ret))
307
2
        goto end;
308
309
3
    ok = 1;
310
5
end:
311
5
    BN_free(two_powN);
312
5
    return ok;
313
3
}
314
315
/*
316
 * FFC pairwise check from SP800-56A R3.
317
 *    Section 5.6.2.1.4 Owner Assurance of Pair-wise Consistency
318
 */
319
int ossl_dh_check_pairwise(const DH *dh)
320
0
{
321
0
    int ret = 0;
322
0
    BN_CTX *ctx = NULL;
323
0
    BIGNUM *pub_key = NULL;
324
325
0
    if (dh->params.p == NULL
326
0
        || dh->params.g == NULL
327
0
        || dh->priv_key == NULL
328
0
        || dh->pub_key == NULL)
329
0
        return 0;
330
331
0
    ctx = BN_CTX_new_ex(dh->libctx);
332
0
    if (ctx == NULL)
333
0
        goto err;
334
0
    pub_key = BN_new();
335
0
    if (pub_key == NULL)
336
0
        goto err;
337
338
    /* recalculate the public key = (g ^ priv) mod p */
339
0
    if (!ossl_dh_generate_public_key(ctx, dh, dh->priv_key, pub_key))
340
0
        goto err;
341
    /* check it matches the existing pubic_key */
342
0
    ret = BN_cmp(pub_key, dh->pub_key) == 0;
343
0
err:
344
0
    BN_free(pub_key);
345
0
    BN_CTX_free(ctx);
346
0
    return ret;
347
0
}