]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/dh/dh_lib.c
2 * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
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
11 * DH low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
17 #include <openssl/bn.h>
19 # include <openssl/engine.h>
21 #include <openssl/obj_mac.h>
22 #include <openssl/core_names.h>
23 #include "internal/cryptlib.h"
24 #include "internal/refcount.h"
25 #include "crypto/evp.h"
26 #include "crypto/dh.h"
29 static DH
*dh_new_intern(ENGINE
*engine
, OSSL_LIB_CTX
*libctx
);
32 int DH_set_method(DH
*dh
, const DH_METHOD
*meth
)
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.
38 const DH_METHOD
*mtmp
;
42 #ifndef OPENSSL_NO_ENGINE
43 ENGINE_finish(dh
->engine
);
52 const DH_METHOD
*ossl_dh_get_method(const DH
*dh
)
56 # ifndef OPENSSL_NO_DEPRECATED_3_0
59 return dh_new_intern(NULL
, NULL
);
63 DH
*DH_new_method(ENGINE
*engine
)
65 return dh_new_intern(engine
, NULL
);
67 #endif /* !FIPS_MODULE */
69 DH
*ossl_dh_new_ex(OSSL_LIB_CTX
*libctx
)
71 return dh_new_intern(NULL
, libctx
);
74 static DH
*dh_new_intern(ENGINE
*engine
, OSSL_LIB_CTX
*libctx
)
76 DH
*ret
= OPENSSL_zalloc(sizeof(*ret
));
81 ret
->lock
= CRYPTO_THREAD_lock_new();
82 if (ret
->lock
== NULL
) {
83 ERR_raise(ERR_LIB_DH
, ERR_R_CRYPTO_LIB
);
88 if (!CRYPTO_NEW_REF(&ret
->references
, 1)) {
89 CRYPTO_THREAD_lock_free(ret
->lock
);
95 ret
->meth
= DH_get_default_method();
96 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
97 ret
->flags
= ret
->meth
->flags
; /* early default init */
99 if (!ENGINE_init(engine
)) {
100 ERR_raise(ERR_LIB_DH
, ERR_R_ENGINE_LIB
);
103 ret
->engine
= engine
;
105 ret
->engine
= ENGINE_get_default_DH();
107 ret
->meth
= ENGINE_get_DH(ret
->engine
);
108 if (ret
->meth
== NULL
) {
109 ERR_raise(ERR_LIB_DH
, ERR_R_ENGINE_LIB
);
115 ret
->flags
= ret
->meth
->flags
;
118 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH
, ret
, &ret
->ex_data
))
120 #endif /* FIPS_MODULE */
122 if ((ret
->meth
->init
!= NULL
) && !ret
->meth
->init(ret
)) {
123 ERR_raise(ERR_LIB_DH
, ERR_R_INIT_FAIL
);
141 CRYPTO_DOWN_REF(&r
->references
, &i
);
142 REF_PRINT_COUNT("DH", r
);
145 REF_ASSERT_ISNT(i
< 0);
147 if (r
->meth
!= NULL
&& r
->meth
->finish
!= NULL
)
149 #if !defined(FIPS_MODULE)
150 # if !defined(OPENSSL_NO_ENGINE)
151 ENGINE_finish(r
->engine
);
153 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH
, r
, &r
->ex_data
);
156 CRYPTO_THREAD_lock_free(r
->lock
);
157 CRYPTO_FREE_REF(&r
->references
);
159 ossl_ffc_params_cleanup(&r
->params
);
160 BN_clear_free(r
->pub_key
);
161 BN_clear_free(r
->priv_key
);
169 if (CRYPTO_UP_REF(&r
->references
, &i
) <= 0)
172 REF_PRINT_COUNT("DH", r
);
173 REF_ASSERT_ISNT(i
< 2);
174 return ((i
> 1) ? 1 : 0);
177 void ossl_dh_set0_libctx(DH
*d
, OSSL_LIB_CTX
*libctx
)
183 int DH_set_ex_data(DH
*d
, int idx
, void *arg
)
185 return CRYPTO_set_ex_data(&d
->ex_data
, idx
, arg
);
188 void *DH_get_ex_data(const DH
*d
, int idx
)
190 return CRYPTO_get_ex_data(&d
->ex_data
, idx
);
194 int DH_bits(const DH
*dh
)
196 if (dh
->params
.p
!= NULL
)
197 return BN_num_bits(dh
->params
.p
);
201 int DH_size(const DH
*dh
)
203 if (dh
->params
.p
!= NULL
)
204 return BN_num_bytes(dh
->params
.p
);
208 int DH_security_bits(const DH
*dh
)
212 if (dh
->params
.q
!= NULL
)
213 N
= BN_num_bits(dh
->params
.q
);
218 if (dh
->params
.p
!= NULL
)
219 return BN_security_bits(BN_num_bits(dh
->params
.p
), N
);
223 void DH_get0_pqg(const DH
*dh
,
224 const BIGNUM
**p
, const BIGNUM
**q
, const BIGNUM
**g
)
226 ossl_ffc_params_get0_pqg(&dh
->params
, p
, q
, g
);
229 int DH_set0_pqg(DH
*dh
, BIGNUM
*p
, BIGNUM
*q
, BIGNUM
*g
)
232 * If the fields p and g in dh are NULL, the corresponding input
233 * parameters MUST be non-NULL. q may remain NULL.
235 if ((dh
->params
.p
== NULL
&& p
== NULL
)
236 || (dh
->params
.g
== NULL
&& g
== NULL
))
239 ossl_ffc_params_set0_pqg(&dh
->params
, p
, q
, g
);
240 ossl_dh_cache_named_group(dh
);
245 long DH_get_length(const DH
*dh
)
250 int DH_set_length(DH
*dh
, long length
)
257 void DH_get0_key(const DH
*dh
, const BIGNUM
**pub_key
, const BIGNUM
**priv_key
)
260 *pub_key
= dh
->pub_key
;
261 if (priv_key
!= NULL
)
262 *priv_key
= dh
->priv_key
;
265 int DH_set0_key(DH
*dh
, BIGNUM
*pub_key
, BIGNUM
*priv_key
)
267 if (pub_key
!= NULL
) {
268 BN_clear_free(dh
->pub_key
);
269 dh
->pub_key
= pub_key
;
271 if (priv_key
!= NULL
) {
272 BN_clear_free(dh
->priv_key
);
273 dh
->priv_key
= priv_key
;
280 const BIGNUM
*DH_get0_p(const DH
*dh
)
285 const BIGNUM
*DH_get0_q(const DH
*dh
)
290 const BIGNUM
*DH_get0_g(const DH
*dh
)
295 const BIGNUM
*DH_get0_priv_key(const DH
*dh
)
300 const BIGNUM
*DH_get0_pub_key(const DH
*dh
)
305 void DH_clear_flags(DH
*dh
, int flags
)
310 int DH_test_flags(const DH
*dh
, int flags
)
312 return dh
->flags
& flags
;
315 void DH_set_flags(DH
*dh
, int flags
)
321 ENGINE
*DH_get0_engine(DH
*dh
)
325 #endif /*FIPS_MODULE */
327 FFC_PARAMS
*ossl_dh_get0_params(DH
*dh
)
331 int ossl_dh_get0_nid(const DH
*dh
)
333 return dh
->params
.nid
;