]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/dh/dh_lib.c
fips module header inclusion fine-tunning
[thirdparty/openssl.git] / crypto / dh / dh_lib.c
CommitLineData
aa6bb135 1/*
8020d79b 2 * Copyright 1995-2021 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
0f113f3e 78 if (ret == NULL) {
9311d0c4 79 ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE);
d188a536 80 return NULL;
0f113f3e 81 }
0c9de428 82
2bbf0baa
F
83 ret->references = 1;
84 ret->lock = CRYPTO_THREAD_lock_new();
85 if (ret->lock == NULL) {
9311d0c4 86 ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE);
2bbf0baa
F
87 OPENSSL_free(ret);
88 return NULL;
89 }
90
8083fd3a 91 ret->libctx = libctx;
0f113f3e 92 ret->meth = DH_get_default_method();
f844f9eb 93#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
2bbf0baa 94 ret->flags = ret->meth->flags; /* early default init */
0f113f3e
MC
95 if (engine) {
96 if (!ENGINE_init(engine)) {
9311d0c4 97 ERR_raise(ERR_LIB_DH, ERR_R_ENGINE_LIB);
2bbf0baa 98 goto err;
0f113f3e
MC
99 }
100 ret->engine = engine;
101 } else
102 ret->engine = ENGINE_get_default_DH();
103 if (ret->engine) {
104 ret->meth = ENGINE_get_DH(ret->engine);
7c96dbcd 105 if (ret->meth == NULL) {
9311d0c4 106 ERR_raise(ERR_LIB_DH, ERR_R_ENGINE_LIB);
2bbf0baa 107 goto err;
0f113f3e
MC
108 }
109 }
0b13e9f0 110#endif
cb78486d 111
0f113f3e 112 ret->flags = ret->meth->flags;
d188a536 113
f844f9eb 114#ifndef FIPS_MODULE
2bbf0baa
F
115 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data))
116 goto err;
f844f9eb 117#endif /* FIPS_MODULE */
d188a536
AG
118
119 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
9311d0c4 120 ERR_raise(ERR_LIB_DH, ERR_R_INIT_FAIL);
544648a8 121 goto err;
0f113f3e 122 }
d188a536
AG
123
124 return ret;
544648a8
NT
125
126 err:
127 DH_free(ret);
128 return NULL;
0f113f3e 129}
d02b48c6 130
6b691a5c 131void DH_free(DH *r)
0f113f3e
MC
132{
133 int i;
d6407083 134
0f113f3e
MC
135 if (r == NULL)
136 return;
d188a536 137
2f545ae4 138 CRYPTO_DOWN_REF(&r->references, &i, r->lock);
f3f1cf84 139 REF_PRINT_COUNT("DH", r);
0f113f3e
MC
140 if (i > 0)
141 return;
f3f1cf84 142 REF_ASSERT_ISNT(i < 0);
13066cee 143
0c5d725e 144 if (r->meth != NULL && r->meth->finish != NULL)
0f113f3e 145 r->meth->finish(r);
f844f9eb 146#if !defined(FIPS_MODULE)
62f49b90 147# if !defined(OPENSSL_NO_ENGINE)
7c96dbcd 148 ENGINE_finish(r->engine);
62f49b90 149# endif
0f113f3e 150 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
a3327784 151#endif
d50f1bdf 152
d188a536
AG
153 CRYPTO_THREAD_lock_free(r->lock);
154
5357c106 155 ossl_ffc_params_cleanup(&r->params);
23a1d5e9
RS
156 BN_clear_free(r->pub_key);
157 BN_clear_free(r->priv_key);
0f113f3e
MC
158 OPENSSL_free(r);
159}
d02b48c6 160
dc2a33d6 161int DH_up_ref(DH *r)
0f113f3e 162{
d188a536
AG
163 int i;
164
2f545ae4 165 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
d188a536 166 return 0;
f3f1cf84
RS
167
168 REF_PRINT_COUNT("DH", r);
169 REF_ASSERT_ISNT(i < 2);
0f113f3e
MC
170 return ((i > 1) ? 1 : 0);
171}
5cbc2e8b 172
6963979f
RL
173void ossl_dh_set0_libctx(DH *d, OSSL_LIB_CTX *libctx)
174{
175 d->libctx = libctx;
176}
177
f844f9eb 178#ifndef FIPS_MODULE
dd9d233e 179int DH_set_ex_data(DH *d, int idx, void *arg)
0f113f3e 180{
26a7d938 181 return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
0f113f3e 182}
13066cee 183
8cc86b81 184void *DH_get_ex_data(const DH *d, int idx)
0f113f3e 185{
26a7d938 186 return CRYPTO_get_ex_data(&d->ex_data, idx);
0f113f3e 187}
a3327784 188#endif
13066cee 189
26c79d56
KR
190int DH_bits(const DH *dh)
191{
5d8ffebb
SP
192 if (dh->params.p != NULL)
193 return BN_num_bits(dh->params.p);
194 return -1;
26c79d56
KR
195}
196
f971ccb2 197int DH_size(const DH *dh)
0f113f3e 198{
5d8ffebb
SP
199 if (dh->params.p != NULL)
200 return BN_num_bytes(dh->params.p);
201 return -1;
0f113f3e 202}
2514fa79
DSH
203
204int DH_security_bits(const DH *dh)
0f113f3e
MC
205{
206 int N;
99325852 207
dc8de3e6
SL
208 if (dh->params.q != NULL)
209 N = BN_num_bits(dh->params.q);
0f113f3e
MC
210 else if (dh->length)
211 N = dh->length;
212 else
213 N = -1;
5d8ffebb
SP
214 if (dh->params.p != NULL)
215 return BN_security_bits(BN_num_bits(dh->params.p), N);
216 return -1;
0f113f3e 217}
0aeddcfa 218
fd809cfd
RL
219void DH_get0_pqg(const DH *dh,
220 const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
0aeddcfa 221{
5357c106 222 ossl_ffc_params_get0_pqg(&dh->params, p, q, g);
0aeddcfa
MC
223}
224
225int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
226{
738ee181 227 /*
af9f2ee3 228 * If the fields p and g in dh are NULL, the corresponding input
1da12e34 229 * parameters MUST be non-NULL. q may remain NULL.
1da12e34 230 */
dc8de3e6
SL
231 if ((dh->params.p == NULL && p == NULL)
232 || (dh->params.g == NULL && g == NULL))
0aeddcfa 233 return 0;
1da12e34 234
5357c106 235 ossl_ffc_params_set0_pqg(&dh->params, p, q, g);
19dbb742 236 ossl_dh_cache_named_group(dh);
8b84b075 237 dh->dirty_cnt++;
0aeddcfa
MC
238 return 1;
239}
240
241long DH_get_length(const DH *dh)
242{
243 return dh->length;
244}
245
246int DH_set_length(DH *dh, long length)
247{
248 dh->length = length;
ee55a207 249 dh->dirty_cnt++;
0aeddcfa
MC
250 return 1;
251}
252
fd809cfd 253void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
0aeddcfa
MC
254{
255 if (pub_key != NULL)
256 *pub_key = dh->pub_key;
257 if (priv_key != NULL)
258 *priv_key = dh->priv_key;
259}
260
261int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
262{
1da12e34 263 if (pub_key != NULL) {
fa01370f 264 BN_clear_free(dh->pub_key);
1da12e34
RL
265 dh->pub_key = pub_key;
266 }
267 if (priv_key != NULL) {
fa01370f 268 BN_clear_free(dh->priv_key);
1da12e34
RL
269 dh->priv_key = priv_key;
270 }
0aeddcfa 271
8b84b075 272 dh->dirty_cnt++;
0aeddcfa
MC
273 return 1;
274}
275
6db7fadf
DMSP
276const BIGNUM *DH_get0_p(const DH *dh)
277{
dc8de3e6 278 return dh->params.p;
6db7fadf
DMSP
279}
280
281const BIGNUM *DH_get0_q(const DH *dh)
282{
dc8de3e6 283 return dh->params.q;
6db7fadf
DMSP
284}
285
286const BIGNUM *DH_get0_g(const DH *dh)
287{
dc8de3e6 288 return dh->params.g;
6db7fadf
DMSP
289}
290
291const BIGNUM *DH_get0_priv_key(const DH *dh)
292{
293 return dh->priv_key;
294}
295
296const BIGNUM *DH_get0_pub_key(const DH *dh)
297{
298 return dh->pub_key;
299}
300
0aeddcfa
MC
301void DH_clear_flags(DH *dh, int flags)
302{
303 dh->flags &= ~flags;
304}
305
306int DH_test_flags(const DH *dh, int flags)
307{
308 return dh->flags & flags;
309}
310
311void DH_set_flags(DH *dh, int flags)
312{
313 dh->flags |= flags;
314}
315
f844f9eb 316#ifndef FIPS_MODULE
0aeddcfa
MC
317ENGINE *DH_get0_engine(DH *dh)
318{
319 return dh->engine;
320}
f844f9eb 321#endif /*FIPS_MODULE */
dc8de3e6 322
19dbb742 323FFC_PARAMS *ossl_dh_get0_params(DH *dh)
dc8de3e6
SL
324{
325 return &dh->params;
326}
19dbb742 327int ossl_dh_get0_nid(const DH *dh)
ca2bf555
SL
328{
329 return dh->params.nid;
330}