]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/dh/dh_lib.c
Copyright year updates
[thirdparty/openssl.git] / crypto / dh / dh_lib.c
CommitLineData
aa6bb135 1/*
da1c088f 2 * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
e38873f5 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
aa6bb135
RS
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
d02b48c6
RE
8 */
9
ada66e78
P
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
d02b48c6 16#include <stdio.h>
ec577822 17#include <openssl/bn.h>
3f773c91
TM
18#ifndef FIPS_MODULE
19# include <openssl/engine.h>
20#endif
5a778ce5 21#include <openssl/obj_mac.h>
7165593c 22#include <openssl/core_names.h>
5a778ce5
DG
23#include "internal/cryptlib.h"
24#include "internal/refcount.h"
7165593c 25#include "crypto/evp.h"
dc8de3e6 26#include "crypto/dh.h"
5a778ce5 27#include "dh_local.h"
d02b48c6 28
b4250010 29static DH *dh_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx);
8083fd3a 30
f844f9eb 31#ifndef FIPS_MODULE
cb78486d 32int DH_set_method(DH *dh, const DH_METHOD *meth)
0f113f3e
MC
33{
34 /*
35 * NB: The caller is specifically setting a method, so it's not up to us
36 * to deal with which ENGINE it comes from.
37 */
38 const DH_METHOD *mtmp;
39 mtmp = dh->meth;
40 if (mtmp->finish)
41 mtmp->finish(dh);
0b13e9f0 42#ifndef OPENSSL_NO_ENGINE
7c96dbcd
RS
43 ENGINE_finish(dh->engine);
44 dh->engine = NULL;
0b13e9f0 45#endif
0f113f3e
MC
46 dh->meth = meth;
47 if (meth->init)
48 meth->init(dh);
49 return 1;
50}
13066cee 51
19dbb742 52const DH_METHOD *ossl_dh_get_method(const DH *dh)
c518117b
RL
53{
54 return dh->meth;
55}
752419d8 56# ifndef OPENSSL_NO_DEPRECATED_3_0
6b691a5c 57DH *DH_new(void)
0f113f3e 58{
8083fd3a 59 return dh_new_intern(NULL, NULL);
0f113f3e 60}
752419d8 61# endif
13066cee 62
5270e702 63DH *DH_new_method(ENGINE *engine)
8083fd3a
SL
64{
65 return dh_new_intern(engine, NULL);
66}
f844f9eb 67#endif /* !FIPS_MODULE */
8083fd3a 68
19dbb742 69DH *ossl_dh_new_ex(OSSL_LIB_CTX *libctx)
8083fd3a
SL
70{
71 return dh_new_intern(NULL, libctx);
72}
73
b4250010 74static DH *dh_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx)
0f113f3e 75{
64b25758 76 DH *ret = OPENSSL_zalloc(sizeof(*ret));
13066cee 77
e077455e 78 if (ret == NULL)
d188a536 79 return NULL;
0c9de428 80
2bbf0baa
F
81 ret->lock = CRYPTO_THREAD_lock_new();
82 if (ret->lock == NULL) {
e077455e 83 ERR_raise(ERR_LIB_DH, ERR_R_CRYPTO_LIB);
2bbf0baa
F
84 OPENSSL_free(ret);
85 return NULL;
86 }
87
97beb77f
P
88 if (!CRYPTO_NEW_REF(&ret->references, 1)) {
89 CRYPTO_THREAD_lock_free(ret->lock);
90 OPENSSL_free(ret);
91 return NULL;
92 }
9015cbb6 93
8083fd3a 94 ret->libctx = libctx;
0f113f3e 95 ret->meth = DH_get_default_method();
f844f9eb 96#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
2bbf0baa 97 ret->flags = ret->meth->flags; /* early default init */
0f113f3e
MC
98 if (engine) {
99 if (!ENGINE_init(engine)) {
9311d0c4 100 ERR_raise(ERR_LIB_DH, ERR_R_ENGINE_LIB);
2bbf0baa 101 goto err;
0f113f3e
MC
102 }
103 ret->engine = engine;
104 } else
105 ret->engine = ENGINE_get_default_DH();
106 if (ret->engine) {
107 ret->meth = ENGINE_get_DH(ret->engine);
7c96dbcd 108 if (ret->meth == NULL) {
9311d0c4 109 ERR_raise(ERR_LIB_DH, ERR_R_ENGINE_LIB);
2bbf0baa 110 goto err;
0f113f3e
MC
111 }
112 }
0b13e9f0 113#endif
cb78486d 114
0f113f3e 115 ret->flags = ret->meth->flags;
d188a536 116
f844f9eb 117#ifndef FIPS_MODULE
2bbf0baa
F
118 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data))
119 goto err;
f844f9eb 120#endif /* FIPS_MODULE */
d188a536
AG
121
122 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
9311d0c4 123 ERR_raise(ERR_LIB_DH, ERR_R_INIT_FAIL);
544648a8 124 goto err;
0f113f3e 125 }
d188a536
AG
126
127 return ret;
544648a8
NT
128
129 err:
130 DH_free(ret);
131 return NULL;
0f113f3e 132}
d02b48c6 133
6b691a5c 134void DH_free(DH *r)
0f113f3e
MC
135{
136 int i;
d6407083 137
0f113f3e
MC
138 if (r == NULL)
139 return;
d188a536 140
9015cbb6 141 CRYPTO_DOWN_REF(&r->references, &i);
f3f1cf84 142 REF_PRINT_COUNT("DH", r);
0f113f3e
MC
143 if (i > 0)
144 return;
f3f1cf84 145 REF_ASSERT_ISNT(i < 0);
13066cee 146
0c5d725e 147 if (r->meth != NULL && r->meth->finish != NULL)
0f113f3e 148 r->meth->finish(r);
f844f9eb 149#if !defined(FIPS_MODULE)
62f49b90 150# if !defined(OPENSSL_NO_ENGINE)
7c96dbcd 151 ENGINE_finish(r->engine);
62f49b90 152# endif
0f113f3e 153 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
a3327784 154#endif
d50f1bdf 155
d188a536 156 CRYPTO_THREAD_lock_free(r->lock);
9015cbb6 157 CRYPTO_FREE_REF(&r->references);
d188a536 158
5357c106 159 ossl_ffc_params_cleanup(&r->params);
23a1d5e9
RS
160 BN_clear_free(r->pub_key);
161 BN_clear_free(r->priv_key);
0f113f3e
MC
162 OPENSSL_free(r);
163}
d02b48c6 164
dc2a33d6 165int DH_up_ref(DH *r)
0f113f3e 166{
d188a536
AG
167 int i;
168
9015cbb6 169 if (CRYPTO_UP_REF(&r->references, &i) <= 0)
d188a536 170 return 0;
f3f1cf84
RS
171
172 REF_PRINT_COUNT("DH", r);
173 REF_ASSERT_ISNT(i < 2);
0f113f3e
MC
174 return ((i > 1) ? 1 : 0);
175}
5cbc2e8b 176
6963979f
RL
177void ossl_dh_set0_libctx(DH *d, OSSL_LIB_CTX *libctx)
178{
179 d->libctx = libctx;
180}
181
f844f9eb 182#ifndef FIPS_MODULE
dd9d233e 183int DH_set_ex_data(DH *d, int idx, void *arg)
0f113f3e 184{
26a7d938 185 return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
0f113f3e 186}
13066cee 187
8cc86b81 188void *DH_get_ex_data(const DH *d, int idx)
0f113f3e 189{
26a7d938 190 return CRYPTO_get_ex_data(&d->ex_data, idx);
0f113f3e 191}
a3327784 192#endif
13066cee 193
26c79d56
KR
194int DH_bits(const DH *dh)
195{
5d8ffebb
SP
196 if (dh->params.p != NULL)
197 return BN_num_bits(dh->params.p);
198 return -1;
26c79d56
KR
199}
200
f971ccb2 201int DH_size(const DH *dh)
0f113f3e 202{
5d8ffebb
SP
203 if (dh->params.p != NULL)
204 return BN_num_bytes(dh->params.p);
205 return -1;
0f113f3e 206}
2514fa79
DSH
207
208int DH_security_bits(const DH *dh)
0f113f3e
MC
209{
210 int N;
99325852 211
dc8de3e6
SL
212 if (dh->params.q != NULL)
213 N = BN_num_bits(dh->params.q);
0f113f3e
MC
214 else if (dh->length)
215 N = dh->length;
216 else
217 N = -1;
5d8ffebb
SP
218 if (dh->params.p != NULL)
219 return BN_security_bits(BN_num_bits(dh->params.p), N);
220 return -1;
0f113f3e 221}
0aeddcfa 222
fd809cfd
RL
223void DH_get0_pqg(const DH *dh,
224 const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
0aeddcfa 225{
5357c106 226 ossl_ffc_params_get0_pqg(&dh->params, p, q, g);
0aeddcfa
MC
227}
228
229int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
230{
738ee181 231 /*
af9f2ee3 232 * If the fields p and g in dh are NULL, the corresponding input
1da12e34 233 * parameters MUST be non-NULL. q may remain NULL.
1da12e34 234 */
dc8de3e6
SL
235 if ((dh->params.p == NULL && p == NULL)
236 || (dh->params.g == NULL && g == NULL))
0aeddcfa 237 return 0;
1da12e34 238
5357c106 239 ossl_ffc_params_set0_pqg(&dh->params, p, q, g);
19dbb742 240 ossl_dh_cache_named_group(dh);
8b84b075 241 dh->dirty_cnt++;
0aeddcfa
MC
242 return 1;
243}
244
245long DH_get_length(const DH *dh)
246{
247 return dh->length;
248}
249
250int DH_set_length(DH *dh, long length)
251{
252 dh->length = length;
ee55a207 253 dh->dirty_cnt++;
0aeddcfa
MC
254 return 1;
255}
256
fd809cfd 257void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
0aeddcfa
MC
258{
259 if (pub_key != NULL)
260 *pub_key = dh->pub_key;
261 if (priv_key != NULL)
262 *priv_key = dh->priv_key;
263}
264
265int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
266{
1da12e34 267 if (pub_key != NULL) {
fa01370f 268 BN_clear_free(dh->pub_key);
1da12e34
RL
269 dh->pub_key = pub_key;
270 }
271 if (priv_key != NULL) {
fa01370f 272 BN_clear_free(dh->priv_key);
1da12e34
RL
273 dh->priv_key = priv_key;
274 }
0aeddcfa 275
8b84b075 276 dh->dirty_cnt++;
0aeddcfa
MC
277 return 1;
278}
279
6db7fadf
DMSP
280const BIGNUM *DH_get0_p(const DH *dh)
281{
dc8de3e6 282 return dh->params.p;
6db7fadf
DMSP
283}
284
285const BIGNUM *DH_get0_q(const DH *dh)
286{
dc8de3e6 287 return dh->params.q;
6db7fadf
DMSP
288}
289
290const BIGNUM *DH_get0_g(const DH *dh)
291{
dc8de3e6 292 return dh->params.g;
6db7fadf
DMSP
293}
294
295const BIGNUM *DH_get0_priv_key(const DH *dh)
296{
297 return dh->priv_key;
298}
299
300const BIGNUM *DH_get0_pub_key(const DH *dh)
301{
302 return dh->pub_key;
303}
304
0aeddcfa
MC
305void DH_clear_flags(DH *dh, int flags)
306{
307 dh->flags &= ~flags;
308}
309
310int DH_test_flags(const DH *dh, int flags)
311{
312 return dh->flags & flags;
313}
314
315void DH_set_flags(DH *dh, int flags)
316{
317 dh->flags |= flags;
318}
319
f844f9eb 320#ifndef FIPS_MODULE
0aeddcfa
MC
321ENGINE *DH_get0_engine(DH *dh)
322{
323 return dh->engine;
324}
f844f9eb 325#endif /*FIPS_MODULE */
dc8de3e6 326
19dbb742 327FFC_PARAMS *ossl_dh_get0_params(DH *dh)
dc8de3e6
SL
328{
329 return &dh->params;
330}
19dbb742 331int ossl_dh_get0_nid(const DH *dh)
ca2bf555
SL
332{
333 return dh->params.nid;
334}