2 * Copyright 1995-2018 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 #include "internal/cryptlib.h"
12 #include "internal/refcount.h"
13 #include <openssl/bn.h>
15 #include <openssl/engine.h>
17 int DH_set_method(DH *dh, const DH_METHOD *meth)
20 * NB: The caller is specifically setting a method, so it's not up to us
21 * to deal with which ENGINE it comes from.
23 const DH_METHOD *mtmp;
27 #ifndef OPENSSL_NO_ENGINE
28 ENGINE_finish(dh->engine);
39 return DH_new_method(NULL);
42 DH *DH_new_method(ENGINE *engine)
44 DH *ret = OPENSSL_zalloc(sizeof(*ret));
47 DHerr(DH_F_DH_NEW_METHOD, ERR_R_MALLOC_FAILURE);
52 ret->lock = CRYPTO_THREAD_lock_new();
53 if (ret->lock == NULL) {
54 DHerr(DH_F_DH_NEW_METHOD, ERR_R_MALLOC_FAILURE);
59 ret->meth = DH_get_default_method();
60 #ifndef OPENSSL_NO_ENGINE
61 ret->flags = ret->meth->flags; /* early default init */
63 if (!ENGINE_init(engine)) {
64 DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB);
69 ret->engine = ENGINE_get_default_DH();
71 ret->meth = ENGINE_get_DH(ret->engine);
72 if (ret->meth == NULL) {
73 DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB);
79 ret->flags = ret->meth->flags;
81 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data))
84 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
85 DHerr(DH_F_DH_NEW_METHOD, ERR_R_INIT_FAIL);
103 CRYPTO_DOWN_REF(&r->references, &i, r->lock);
104 REF_PRINT_COUNT("DH", r);
107 REF_ASSERT_ISNT(i < 0);
109 if (r->meth != NULL && r->meth->finish != NULL)
111 #ifndef OPENSSL_NO_ENGINE
112 ENGINE_finish(r->engine);
115 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
117 CRYPTO_THREAD_lock_free(r->lock);
123 OPENSSL_free(r->seed);
124 BN_clear_free(r->counter);
125 BN_clear_free(r->pub_key);
126 BN_clear_free(r->priv_key);
134 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
137 REF_PRINT_COUNT("DH", r);
138 REF_ASSERT_ISNT(i < 2);
139 return ((i > 1) ? 1 : 0);
142 int DH_set_ex_data(DH *d, int idx, void *arg)
144 return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
147 void *DH_get_ex_data(DH *d, int idx)
149 return CRYPTO_get_ex_data(&d->ex_data, idx);
152 int DH_bits(const DH *dh)
154 return BN_num_bits(dh->p);
157 int DH_size(const DH *dh)
159 return BN_num_bytes(dh->p);
162 int DH_security_bits(const DH *dh)
166 N = BN_num_bits(dh->q);
171 return BN_security_bits(BN_num_bits(dh->p), N);
175 void DH_get0_pqg(const DH *dh,
176 const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
186 int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
188 /* If the fields p and g in d are NULL, the corresponding input
189 * parameters MUST be non-NULL. q may remain NULL.
191 if ((dh->p == NULL && p == NULL)
192 || (dh->g == NULL && g == NULL))
209 dh->length = BN_num_bits(q);
216 long DH_get_length(const DH *dh)
221 int DH_set_length(DH *dh, long length)
227 void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
230 *pub_key = dh->pub_key;
231 if (priv_key != NULL)
232 *priv_key = dh->priv_key;
235 int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
237 if (pub_key != NULL) {
238 BN_clear_free(dh->pub_key);
239 dh->pub_key = pub_key;
241 if (priv_key != NULL) {
242 BN_clear_free(dh->priv_key);
243 dh->priv_key = priv_key;
250 const BIGNUM *DH_get0_p(const DH *dh)
255 const BIGNUM *DH_get0_q(const DH *dh)
260 const BIGNUM *DH_get0_g(const DH *dh)
265 const BIGNUM *DH_get0_priv_key(const DH *dh)
270 const BIGNUM *DH_get0_pub_key(const DH *dh)
275 void DH_clear_flags(DH *dh, int flags)
280 int DH_test_flags(const DH *dh, int flags)
282 return dh->flags & flags;
285 void DH_set_flags(DH *dh, int flags)
290 ENGINE *DH_get0_engine(DH *dh)