2 * Copyright 1995-2020 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, OPENSSL_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 *dh_get_method(const DH *dh)
57 return dh_new_intern(NULL, NULL);
60 DH *DH_new_method(ENGINE *engine)
62 return dh_new_intern(engine, NULL);
64 #endif /* !FIPS_MODULE */
66 DH *dh_new_ex(OPENSSL_CTX *libctx)
68 return dh_new_intern(NULL, libctx);
71 static DH *dh_new_intern(ENGINE *engine, OPENSSL_CTX *libctx)
73 DH *ret = OPENSSL_zalloc(sizeof(*ret));
76 DHerr(0, ERR_R_MALLOC_FAILURE);
81 ret->lock = CRYPTO_THREAD_lock_new();
82 if (ret->lock == NULL) {
83 DHerr(0, ERR_R_MALLOC_FAILURE);
89 ret->meth = DH_get_default_method();
90 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
91 ret->flags = ret->meth->flags; /* early default init */
93 if (!ENGINE_init(engine)) {
94 DHerr(0, ERR_R_ENGINE_LIB);
99 ret->engine = ENGINE_get_default_DH();
101 ret->meth = ENGINE_get_DH(ret->engine);
102 if (ret->meth == NULL) {
103 DHerr(0, ERR_R_ENGINE_LIB);
109 ret->flags = ret->meth->flags;
112 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data))
114 #endif /* FIPS_MODULE */
116 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
117 DHerr(0, ERR_R_INIT_FAIL);
135 CRYPTO_DOWN_REF(&r->references, &i, r->lock);
136 REF_PRINT_COUNT("DH", r);
139 REF_ASSERT_ISNT(i < 0);
141 if (r->meth != NULL && r->meth->finish != NULL)
143 #if !defined(FIPS_MODULE)
144 # if !defined(OPENSSL_NO_ENGINE)
145 ENGINE_finish(r->engine);
147 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
150 CRYPTO_THREAD_lock_free(r->lock);
152 ossl_ffc_params_cleanup(&r->params);
153 BN_clear_free(r->pub_key);
154 BN_clear_free(r->priv_key);
162 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
165 REF_PRINT_COUNT("DH", r);
166 REF_ASSERT_ISNT(i < 2);
167 return ((i > 1) ? 1 : 0);
171 int DH_set_ex_data(DH *d, int idx, void *arg)
173 return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
176 void *DH_get_ex_data(const DH *d, int idx)
178 return CRYPTO_get_ex_data(&d->ex_data, idx);
182 int DH_bits(const DH *dh)
184 return BN_num_bits(dh->params.p);
187 int DH_size(const DH *dh)
189 return BN_num_bytes(dh->params.p);
192 int DH_security_bits(const DH *dh)
195 if (dh->params.q != NULL)
196 N = BN_num_bits(dh->params.q);
201 return BN_security_bits(BN_num_bits(dh->params.p), N);
204 void DH_get0_pqg(const DH *dh,
205 const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
207 ossl_ffc_params_get0_pqg(&dh->params, p, q, g);
210 int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
213 * If the fields p and g in d are NULL, the corresponding input
214 * parameters MUST be non-NULL. q may remain NULL.
216 if ((dh->params.p == NULL && p == NULL)
217 || (dh->params.g == NULL && g == NULL))
220 ossl_ffc_params_set0_pqg(&dh->params, p, q, g);
221 dh_cache_named_group(dh);
223 dh->length = BN_num_bits(q);
225 * Check if this is a named group. If it finds a named group then the
226 * 'q' and 'length' value are either already set or are set by the
229 if (DH_get_nid(dh) == NID_undef) {
230 /* If its not a named group then set the 'length' if q is not NULL */
232 dh->length = BN_num_bits(q);
238 long DH_get_length(const DH *dh)
243 int DH_set_length(DH *dh, long length)
249 void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
252 *pub_key = dh->pub_key;
253 if (priv_key != NULL)
254 *priv_key = dh->priv_key;
257 int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
259 if (pub_key != NULL) {
260 BN_clear_free(dh->pub_key);
261 dh->pub_key = pub_key;
263 if (priv_key != NULL) {
264 BN_clear_free(dh->priv_key);
265 dh->priv_key = priv_key;
266 dh->length = BN_num_bits(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 *dh_get0_params(DH *dh)
324 int dh_get0_nid(const DH *dh)
326 return dh->params.nid;
329 int dh_ffc_params_fromdata(DH *dh, const OSSL_PARAM params[])
336 ffc = dh_get0_params(dh);
340 ret = ossl_ffc_params_fromdata(ffc, params);
342 dh_cache_named_group(dh);