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) {
71 ret->meth = DH_get_default_method();
72 #ifndef OPENSSL_NO_ENGINE
73 ret->flags = ret->meth->flags; /* early default init */
75 if (!ENGINE_init(engine)) {
76 DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB);
81 ret->engine = ENGINE_get_default_DH();
83 ret->meth = ENGINE_get_DH(ret->engine);
84 if (ret->meth == NULL) {
85 DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB);
91 ret->flags = ret->meth->flags;
93 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data))
96 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
97 DHerr(DH_F_DH_NEW_METHOD, ERR_R_INIT_FAIL);
113 CRYPTO_atomic_add(&r->references, -1, &i, r->lock);
114 REF_PRINT_COUNT("DH", r);
117 REF_ASSERT_ISNT(i < 0);
121 #ifndef OPENSSL_NO_ENGINE
122 ENGINE_finish(r->engine);
125 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
127 CRYPTO_THREAD_lock_free(r->lock);
133 OPENSSL_free(r->seed);
134 BN_clear_free(r->counter);
135 BN_clear_free(r->pub_key);
136 BN_clear_free(r->priv_key);
144 if (CRYPTO_atomic_add(&r->references, 1, &i, r->lock) <= 0)
147 REF_PRINT_COUNT("DH", r);
148 REF_ASSERT_ISNT(i < 2);
149 return ((i > 1) ? 1 : 0);
152 int DH_set_ex_data(DH *d, int idx, void *arg)
154 return (CRYPTO_set_ex_data(&d->ex_data, idx, arg));
157 void *DH_get_ex_data(DH *d, int idx)
159 return (CRYPTO_get_ex_data(&d->ex_data, idx));
162 int DH_bits(const DH *dh)
164 return BN_num_bits(dh->p);
167 int DH_size(const DH *dh)
169 return (BN_num_bytes(dh->p));
172 int DH_security_bits(const DH *dh)
176 N = BN_num_bits(dh->q);
181 return BN_security_bits(BN_num_bits(dh->p), N);
185 void DH_get0_pqg(const DH *dh, BIGNUM **p, BIGNUM **q, BIGNUM **g)
195 int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
197 /* If the fields p and g in d are NULL, the corresponding input
198 * parameters MUST be non-NULL. q may remain NULL.
200 * It is an error to give the results from get0 on d
201 * as input parameters.
203 if (p == dh->p || (dh->q != NULL && q == dh->q) || g == dh->g)
220 dh->length = BN_num_bits(q);
226 long DH_get_length(const DH *dh)
231 int DH_set_length(DH *dh, long length)
237 void DH_get0_key(const DH *dh, BIGNUM **pub_key, BIGNUM **priv_key)
240 *pub_key = dh->pub_key;
241 if (priv_key != NULL)
242 *priv_key = dh->priv_key;
245 int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
247 /* If the pub_key in dh is NULL, the corresponding input
248 * parameters MUST be non-NULL. The priv_key field may
251 * It is an error to give the results from get0 on dh
252 * as input parameters.
254 if (dh->pub_key == pub_key
255 || (dh->priv_key != NULL && priv_key == dh->priv_key))
258 if (pub_key != NULL) {
259 BN_free(dh->pub_key);
260 dh->pub_key = pub_key;
262 if (priv_key != NULL) {
263 BN_free(dh->priv_key);
264 dh->priv_key = priv_key;
270 void DH_clear_flags(DH *dh, int flags)
275 int DH_test_flags(const DH *dh, int flags)
277 return dh->flags & flags;
280 void DH_set_flags(DH *dh, int flags)
285 ENGINE *DH_get0_engine(DH *dh)