Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl40/crypto/dh/dh_lib.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2026 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 <openssl/bn.h>
18
#include <openssl/obj_mac.h>
19
#include <openssl/core_names.h>
20
#include "internal/cryptlib.h"
21
#include "internal/refcount.h"
22
#include "internal/common.h"
23
#include "crypto/evp.h"
24
#include "crypto/dh.h"
25
#include "dh_local.h"
26
27
static DH *dh_new_intern(OSSL_LIB_CTX *libctx);
28
29
#ifndef FIPS_MODULE
30
int DH_set_method(DH *dh, const DH_METHOD *meth)
31
0
{
32
    /*
33
     * NB: The caller is specifically setting a method, so it's not up to us
34
     * to deal with which ENGINE it comes from.
35
     */
36
0
    const DH_METHOD *mtmp;
37
0
    mtmp = dh->meth;
38
0
    if (mtmp->finish)
39
0
        mtmp->finish(dh);
40
0
    dh->meth = meth;
41
0
    if (meth->init)
42
0
        meth->init(dh);
43
0
    return 1;
44
0
}
45
46
const DH_METHOD *ossl_dh_get_method(const DH *dh)
47
9.07k
{
48
9.07k
    return dh->meth;
49
9.07k
}
50
#ifndef OPENSSL_NO_DEPRECATED_3_0
51
DH *DH_new(void)
52
329k
{
53
329k
    return dh_new_intern(NULL);
54
329k
}
55
#endif
56
57
DH *DH_new_method(ENGINE *engine)
58
0
{
59
0
    if (!ossl_assert(engine == NULL))
60
0
        return NULL;
61
0
    return dh_new_intern(NULL);
62
0
}
63
#endif /* !FIPS_MODULE */
64
65
DH *ossl_dh_new_ex(OSSL_LIB_CTX *libctx)
66
25.1k
{
67
25.1k
    return dh_new_intern(libctx);
68
25.1k
}
69
70
static DH *dh_new_intern(OSSL_LIB_CTX *libctx)
71
147k
{
72
147k
    DH *ret = OPENSSL_zalloc(sizeof(*ret));
73
74
147k
    if (ret == NULL)
75
0
        return NULL;
76
77
147k
    ret->lock = CRYPTO_THREAD_lock_new();
78
147k
    if (ret->lock == NULL) {
79
0
        ERR_raise(ERR_LIB_DH, ERR_R_CRYPTO_LIB);
80
0
        OPENSSL_free(ret);
81
0
        return NULL;
82
0
    }
83
84
147k
    if (!CRYPTO_NEW_REF(&ret->references, 1)) {
85
0
        CRYPTO_THREAD_lock_free(ret->lock);
86
0
        OPENSSL_free(ret);
87
0
        return NULL;
88
0
    }
89
90
147k
    ret->libctx = libctx;
91
147k
    ret->meth = DH_get_default_method();
92
147k
    ret->flags = ret->meth->flags;
93
94
147k
#ifndef FIPS_MODULE
95
147k
    if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data))
96
0
        goto err;
97
147k
#endif /* FIPS_MODULE */
98
99
147k
    ossl_ffc_params_init(&ret->params);
100
101
147k
    if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
102
0
        ERR_raise(ERR_LIB_DH, ERR_R_INIT_FAIL);
103
0
        goto err;
104
0
    }
105
106
147k
    return ret;
107
108
0
err:
109
0
    DH_free(ret);
110
0
    return NULL;
111
147k
}
112
113
void DH_free(DH *r)
114
926k
{
115
926k
    int i;
116
117
926k
    if (r == NULL)
118
513k
        return;
119
120
413k
    CRYPTO_DOWN_REF(&r->references, &i);
121
413k
    REF_PRINT_COUNT("DH", i, r);
122
413k
    if (i > 0)
123
58.4k
        return;
124
354k
    REF_ASSERT_ISNT(i < 0);
125
126
354k
    if (r->meth != NULL && r->meth->finish != NULL)
127
354k
        r->meth->finish(r);
128
354k
#if !defined(FIPS_MODULE)
129
354k
    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
130
354k
#endif
131
132
354k
    CRYPTO_THREAD_lock_free(r->lock);
133
354k
    CRYPTO_FREE_REF(&r->references);
134
135
354k
    ossl_ffc_params_cleanup(&r->params);
136
354k
    BN_clear_free(r->pub_key);
137
354k
    BN_clear_free(r->priv_key);
138
354k
    OPENSSL_free(r);
139
354k
}
140
141
int DH_up_ref(DH *r)
142
48.3k
{
143
48.3k
    int i;
144
145
48.3k
    if (CRYPTO_UP_REF(&r->references, &i) <= 0)
146
0
        return 0;
147
148
48.3k
    REF_PRINT_COUNT("DH", i, r);
149
48.3k
    REF_ASSERT_ISNT(i < 2);
150
48.3k
    return ((i > 1) ? 1 : 0);
151
48.3k
}
152
153
OSSL_LIB_CTX *ossl_dh_get0_libctx(const DH *dh)
154
0
{
155
0
    return dh->libctx;
156
0
}
157
158
void ossl_dh_set0_libctx(DH *d, OSSL_LIB_CTX *libctx)
159
53.9k
{
160
53.9k
    d->libctx = libctx;
161
53.9k
}
162
163
#ifndef FIPS_MODULE
164
int DH_set_ex_data(DH *d, int idx, void *arg)
165
0
{
166
0
    return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
167
0
}
168
169
void *DH_get_ex_data(const DH *d, int idx)
170
0
{
171
0
    return CRYPTO_get_ex_data(&d->ex_data, idx);
172
0
}
173
#endif
174
175
int DH_bits(const DH *dh)
176
79.0k
{
177
79.0k
    if (dh->params.p != NULL)
178
79.0k
        return BN_num_bits(dh->params.p);
179
0
    return -1;
180
79.0k
}
181
182
int DH_size(const DH *dh)
183
89.8k
{
184
89.8k
    if (dh->params.p != NULL)
185
89.8k
        return BN_num_bytes(dh->params.p);
186
0
    return -1;
187
89.8k
}
188
189
int DH_security_bits(const DH *dh)
190
79.0k
{
191
79.0k
    int N;
192
193
79.0k
    if (dh->params.q != NULL)
194
58.3k
        N = BN_num_bits(dh->params.q);
195
20.7k
    else if (dh->length)
196
1.87k
        N = dh->length;
197
18.8k
    else
198
18.8k
        N = -1;
199
79.0k
    if (dh->params.p != NULL)
200
79.0k
        return BN_security_bits(BN_num_bits(dh->params.p), N);
201
0
    return -1;
202
79.0k
}
203
204
void DH_get0_pqg(const DH *dh,
205
    const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
206
10.8k
{
207
10.8k
    ossl_ffc_params_get0_pqg(&dh->params, p, q, g);
208
10.8k
}
209
210
int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
211
55.6k
{
212
    /*
213
     * If the fields p and g in dh are NULL, the corresponding input
214
     * parameters MUST be non-NULL.  q may remain NULL.
215
     */
216
55.6k
    if ((dh->params.p == NULL && p == NULL)
217
55.6k
        || (dh->params.g == NULL && g == NULL))
218
0
        return 0;
219
220
55.6k
    ossl_ffc_params_set0_pqg(&dh->params, p, q, g);
221
55.6k
    ossl_dh_cache_named_group(dh);
222
55.6k
    dh->dirty_cnt++;
223
55.6k
    return 1;
224
55.6k
}
225
226
long DH_get_length(const DH *dh)
227
109k
{
228
109k
    return dh->length;
229
109k
}
230
231
int DH_set_length(DH *dh, long length)
232
0
{
233
0
    dh->length = length;
234
0
    dh->dirty_cnt++;
235
0
    return 1;
236
0
}
237
238
void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
239
140k
{
240
140k
    if (pub_key != NULL)
241
134k
        *pub_key = dh->pub_key;
242
140k
    if (priv_key != NULL)
243
100k
        *priv_key = dh->priv_key;
244
140k
}
245
246
int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
247
12.2k
{
248
12.2k
    if (pub_key != NULL) {
249
12.0k
        BN_clear_free(dh->pub_key);
250
12.0k
        dh->pub_key = pub_key;
251
12.0k
    }
252
12.2k
    if (priv_key != NULL) {
253
196
        BN_clear_free(dh->priv_key);
254
196
        dh->priv_key = priv_key;
255
196
    }
256
257
12.2k
    dh->dirty_cnt++;
258
12.2k
    return 1;
259
12.2k
}
260
261
const BIGNUM *DH_get0_p(const DH *dh)
262
8.95k
{
263
8.95k
    return dh->params.p;
264
8.95k
}
265
266
const BIGNUM *DH_get0_q(const DH *dh)
267
0
{
268
0
    return dh->params.q;
269
0
}
270
271
const BIGNUM *DH_get0_g(const DH *dh)
272
1.03k
{
273
1.03k
    return dh->params.g;
274
1.03k
}
275
276
const BIGNUM *DH_get0_priv_key(const DH *dh)
277
19.2k
{
278
19.2k
    return dh->priv_key;
279
19.2k
}
280
281
const BIGNUM *DH_get0_pub_key(const DH *dh)
282
27.7k
{
283
27.7k
    return dh->pub_key;
284
27.7k
}
285
286
void DH_clear_flags(DH *dh, int flags)
287
78.4k
{
288
78.4k
    dh->flags &= ~flags;
289
78.4k
}
290
291
int DH_test_flags(const DH *dh, int flags)
292
82
{
293
82
    return dh->flags & flags;
294
82
}
295
296
void DH_set_flags(DH *dh, int flags)
297
78.4k
{
298
78.4k
    dh->flags |= flags;
299
78.4k
}
300
301
FFC_PARAMS *ossl_dh_get0_params(DH *dh)
302
147k
{
303
147k
    return &dh->params;
304
147k
}
305
int ossl_dh_get0_nid(const DH *dh)
306
0
{
307
0
    return dh->params.nid;
308
0
}