2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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 #include "internal/cryptlib.h"
12 #include <openssl/bn.h>
14 #include <openssl/engine.h>
16 static const DH_METHOD *default_DH_method = NULL;
18 void DH_set_default_method(const DH_METHOD *meth)
20 default_DH_method = meth;
23 const DH_METHOD *DH_get_default_method(void)
25 if (!default_DH_method)
26 default_DH_method = DH_OpenSSL();
27 return default_DH_method;
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);
52 return DH_new_method(NULL);
55 DH *DH_new_method(ENGINE *engine)
57 DH *ret = OPENSSL_zalloc(sizeof(*ret));
60 DHerr(DH_F_DH_NEW_METHOD, ERR_R_MALLOC_FAILURE);
65 ret->lock = CRYPTO_THREAD_lock_new();
66 if (ret->lock == NULL) {
67 DHerr(DH_F_DH_NEW_METHOD, ERR_R_MALLOC_FAILURE);
72 ret->meth = DH_get_default_method();
73 #ifndef OPENSSL_NO_ENGINE
74 ret->flags = ret->meth->flags; /* early default init */
76 if (!ENGINE_init(engine)) {
77 DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB);
82 ret->engine = ENGINE_get_default_DH();
84 ret->meth = ENGINE_get_DH(ret->engine);
85 if (ret->meth == NULL) {
86 DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB);
92 ret->flags = ret->meth->flags;
94 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data))
97 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
98 DHerr(DH_F_DH_NEW_METHOD, ERR_R_INIT_FAIL);
114 CRYPTO_atomic_add(&r->references, -1, &i, r->lock);
115 REF_PRINT_COUNT("DH", r);
118 REF_ASSERT_ISNT(i < 0);
122 #ifndef OPENSSL_NO_ENGINE
123 ENGINE_finish(r->engine);
126 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
128 CRYPTO_THREAD_lock_free(r->lock);
134 OPENSSL_free(r->seed);
135 BN_clear_free(r->counter);
136 BN_clear_free(r->pub_key);
137 BN_clear_free(r->priv_key);
145 if (CRYPTO_atomic_add(&r->references, 1, &i, r->lock) <= 0)
148 REF_PRINT_COUNT("DH", r);
149 REF_ASSERT_ISNT(i < 2);
150 return ((i > 1) ? 1 : 0);
153 int DH_set_ex_data(DH *d, int idx, void *arg)
155 return (CRYPTO_set_ex_data(&d->ex_data, idx, arg));
158 void *DH_get_ex_data(DH *d, int idx)
160 return (CRYPTO_get_ex_data(&d->ex_data, idx));
163 int DH_bits(const DH *dh)
165 return BN_num_bits(dh->p);
168 int DH_size(const DH *dh)
170 return (BN_num_bytes(dh->p));
173 int DH_security_bits(const DH *dh)
177 N = BN_num_bits(dh->q);
182 return BN_security_bits(BN_num_bits(dh->p), N);
186 void DH_get0_pqg(const DH *dh, BIGNUM **p, BIGNUM **q, BIGNUM **g)
196 int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
198 /* If the fields p and g in d are NULL, the corresponding input
199 * parameters MUST be non-NULL. q may remain NULL.
201 * It is an error to give the results from get0 on d
202 * as input parameters.
204 if (p == dh->p || (dh->q != NULL && q == dh->q) || g == dh->g)
221 dh->length = BN_num_bits(q);
227 long DH_get_length(const DH *dh)
232 int DH_set_length(DH *dh, long length)
238 void DH_get0_key(const DH *dh, BIGNUM **pub_key, BIGNUM **priv_key)
241 *pub_key = dh->pub_key;
242 if (priv_key != NULL)
243 *priv_key = dh->priv_key;
246 int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
248 /* If the pub_key in dh is NULL, the corresponding input
249 * parameters MUST be non-NULL. The priv_key field may
252 * It is an error to give the results from get0 on dh
253 * as input parameters.
255 if (dh->pub_key == pub_key
256 || (dh->priv_key != NULL && priv_key == dh->priv_key))
259 if (pub_key != NULL) {
260 BN_free(dh->pub_key);
261 dh->pub_key = pub_key;
263 if (priv_key != NULL) {
264 BN_free(dh->priv_key);
265 dh->priv_key = priv_key;
271 void DH_clear_flags(DH *dh, int flags)
276 int DH_test_flags(const DH *dh, int flags)
278 return dh->flags & flags;
281 void DH_set_flags(DH *dh, int flags)
286 ENGINE *DH_get0_engine(DH *dh)