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 int DH_set_method(DH *dh, const DH_METHOD *meth)
19 * NB: The caller is specifically setting a method, so it's not up to us
20 * to deal with which ENGINE it comes from.
22 const DH_METHOD *mtmp;
26 #ifndef OPENSSL_NO_ENGINE
27 ENGINE_finish(dh->engine);
38 return DH_new_method(NULL);
41 DH *DH_new_method(ENGINE *engine)
43 DH *ret = OPENSSL_zalloc(sizeof(*ret));
46 DHerr(DH_F_DH_NEW_METHOD, ERR_R_MALLOC_FAILURE);
51 ret->lock = CRYPTO_THREAD_lock_new();
52 if (ret->lock == NULL) {
53 DHerr(DH_F_DH_NEW_METHOD, ERR_R_MALLOC_FAILURE);
58 ret->meth = DH_get_default_method();
59 #ifndef OPENSSL_NO_ENGINE
60 ret->flags = ret->meth->flags; /* early default init */
62 if (!ENGINE_init(engine)) {
63 DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB);
68 ret->engine = ENGINE_get_default_DH();
70 ret->meth = ENGINE_get_DH(ret->engine);
71 if (ret->meth == NULL) {
72 DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB);
78 ret->flags = ret->meth->flags;
80 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data))
83 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
84 DHerr(DH_F_DH_NEW_METHOD, ERR_R_INIT_FAIL);
100 CRYPTO_DOWN_REF(&r->references, &i, r->lock);
101 REF_PRINT_COUNT("DH", r);
104 REF_ASSERT_ISNT(i < 0);
108 #ifndef OPENSSL_NO_ENGINE
109 ENGINE_finish(r->engine);
112 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
114 CRYPTO_THREAD_lock_free(r->lock);
120 OPENSSL_free(r->seed);
121 BN_clear_free(r->counter);
122 BN_clear_free(r->pub_key);
123 BN_clear_free(r->priv_key);
131 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
134 REF_PRINT_COUNT("DH", r);
135 REF_ASSERT_ISNT(i < 2);
136 return ((i > 1) ? 1 : 0);
139 int DH_set_ex_data(DH *d, int idx, void *arg)
141 return (CRYPTO_set_ex_data(&d->ex_data, idx, arg));
144 void *DH_get_ex_data(DH *d, int idx)
146 return (CRYPTO_get_ex_data(&d->ex_data, idx));
149 int DH_bits(const DH *dh)
151 return BN_num_bits(dh->p);
154 int DH_size(const DH *dh)
156 return (BN_num_bytes(dh->p));
159 int DH_security_bits(const DH *dh)
163 N = BN_num_bits(dh->q);
168 return BN_security_bits(BN_num_bits(dh->p), N);
172 void DH_get0_pqg(const DH *dh,
173 const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
183 int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
185 /* If the fields p and g in d are NULL, the corresponding input
186 * parameters MUST be non-NULL. q may remain NULL.
188 if ((dh->p == NULL && p == NULL)
189 || (dh->g == NULL && g == NULL))
206 dh->length = BN_num_bits(q);
212 long DH_get_length(const DH *dh)
217 int DH_set_length(DH *dh, long length)
223 void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
226 *pub_key = dh->pub_key;
227 if (priv_key != NULL)
228 *priv_key = dh->priv_key;
231 int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
233 /* If the field pub_key in dh is NULL, the corresponding input
234 * parameters MUST be non-NULL. The priv_key field may
237 if (dh->pub_key == NULL && pub_key == NULL)
240 if (pub_key != NULL) {
241 BN_free(dh->pub_key);
242 dh->pub_key = pub_key;
244 if (priv_key != NULL) {
245 BN_free(dh->priv_key);
246 dh->priv_key = priv_key;
252 void DH_clear_flags(DH *dh, int flags)
257 int DH_test_flags(const DH *dh, int flags)
259 return dh->flags & flags;
262 void DH_set_flags(DH *dh, int flags)
267 ENGINE *DH_get0_engine(DH *dh)