2 * Copyright 1995-2021 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>
18 #include <openssl/engine.h>
19 #include <openssl/obj_mac.h>
20 #include <openssl/core_names.h>
21 #include "internal/cryptlib.h"
22 #include "internal/refcount.h"
23 #include "crypto/evp.h"
24 #include "crypto/dh.h"
27 static DH *dh_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx);
30 int DH_set_method(DH *dh, const DH_METHOD *meth)
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.
36 const DH_METHOD *mtmp;
40 #ifndef OPENSSL_NO_ENGINE
41 ENGINE_finish(dh->engine);
50 const DH_METHOD *ossl_dh_get_method(const DH *dh)
54 # ifndef OPENSSL_NO_DEPRECATED_3_0
57 return dh_new_intern(NULL, NULL);
61 DH *DH_new_method(ENGINE *engine)
63 return dh_new_intern(engine, NULL);
65 #endif /* !FIPS_MODULE */
67 DH *ossl_dh_new_ex(OSSL_LIB_CTX *libctx)
69 return dh_new_intern(NULL, libctx);
72 static DH *dh_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx)
74 DH *ret = OPENSSL_zalloc(sizeof(*ret));
77 ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE);
82 ret->lock = CRYPTO_THREAD_lock_new();
83 if (ret->lock == NULL) {
84 ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE);
90 ret->meth = DH_get_default_method();
91 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
92 ret->flags = ret->meth->flags; /* early default init */
94 if (!ENGINE_init(engine)) {
95 ERR_raise(ERR_LIB_DH, ERR_R_ENGINE_LIB);
100 ret->engine = ENGINE_get_default_DH();
102 ret->meth = ENGINE_get_DH(ret->engine);
103 if (ret->meth == NULL) {
104 ERR_raise(ERR_LIB_DH, ERR_R_ENGINE_LIB);
110 ret->flags = ret->meth->flags;
113 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data))
115 #endif /* FIPS_MODULE */
117 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
118 ERR_raise(ERR_LIB_DH, ERR_R_INIT_FAIL);
136 CRYPTO_DOWN_REF(&r->references, &i, r->lock);
137 REF_PRINT_COUNT("DH", r);
140 REF_ASSERT_ISNT(i < 0);
142 if (r->meth != NULL && r->meth->finish != NULL)
144 #if !defined(FIPS_MODULE)
145 # if !defined(OPENSSL_NO_ENGINE)
146 ENGINE_finish(r->engine);
148 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
151 CRYPTO_THREAD_lock_free(r->lock);
153 ossl_ffc_params_cleanup(&r->params);
154 BN_clear_free(r->pub_key);
155 BN_clear_free(r->priv_key);
163 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
166 REF_PRINT_COUNT("DH", r);
167 REF_ASSERT_ISNT(i < 2);
168 return ((i > 1) ? 1 : 0);
171 void ossl_dh_set0_libctx(DH *d, OSSL_LIB_CTX *libctx)
177 int DH_set_ex_data(DH *d, int idx, void *arg)
179 return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
182 void *DH_get_ex_data(const DH *d, int idx)
184 return CRYPTO_get_ex_data(&d->ex_data, idx);
188 int DH_bits(const DH *dh)
190 if (dh->params.p != NULL)
191 return BN_num_bits(dh->params.p);
195 int DH_size(const DH *dh)
197 if (dh->params.p != NULL)
198 return BN_num_bytes(dh->params.p);
202 int DH_security_bits(const DH *dh)
205 if (dh->params.q != NULL)
206 N = BN_num_bits(dh->params.q);
211 if (dh->params.p != NULL)
212 return BN_security_bits(BN_num_bits(dh->params.p), N);
216 void DH_get0_pqg(const DH *dh,
217 const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
219 ossl_ffc_params_get0_pqg(&dh->params, p, q, g);
222 int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
225 * If the fields p and g in dh are NULL, the corresponding input
226 * parameters MUST be non-NULL. q may remain NULL.
228 if ((dh->params.p == NULL && p == NULL)
229 || (dh->params.g == NULL && g == NULL))
232 ossl_ffc_params_set0_pqg(&dh->params, p, q, g);
233 ossl_dh_cache_named_group(dh);
238 long DH_get_length(const DH *dh)
243 int DH_set_length(DH *dh, long length)
250 void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
253 *pub_key = dh->pub_key;
254 if (priv_key != NULL)
255 *priv_key = dh->priv_key;
258 int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
260 if (pub_key != NULL) {
261 BN_clear_free(dh->pub_key);
262 dh->pub_key = pub_key;
264 if (priv_key != NULL) {
265 BN_clear_free(dh->priv_key);
266 dh->priv_key = priv_key;
273 const BIGNUM *DH_get0_p(const DH *dh)
278 const BIGNUM *DH_get0_q(const DH *dh)
283 const BIGNUM *DH_get0_g(const DH *dh)
288 const BIGNUM *DH_get0_priv_key(const DH *dh)
293 const BIGNUM *DH_get0_pub_key(const DH *dh)
298 void DH_clear_flags(DH *dh, int flags)
303 int DH_test_flags(const DH *dh, int flags)
305 return dh->flags & flags;
308 void DH_set_flags(DH *dh, int flags)
314 ENGINE *DH_get0_engine(DH *dh)
318 #endif /*FIPS_MODULE */
320 FFC_PARAMS *ossl_dh_get0_params(DH *dh)
324 int ossl_dh_get0_nid(const DH *dh)
326 return dh->params.nid;