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 * 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 "internal/cryptlib.h"
21 #include "internal/refcount.h"
22 #include "crypto/dh.h"
25 static DH *dh_new_intern(ENGINE *engine, OPENSSL_CTX *libctx);
28 int DH_set_method(DH *dh, const DH_METHOD *meth)
31 * NB: The caller is specifically setting a method, so it's not up to us
32 * to deal with which ENGINE it comes from.
34 const DH_METHOD *mtmp;
38 #ifndef OPENSSL_NO_ENGINE
39 ENGINE_finish(dh->engine);
48 const DH_METHOD *dh_get_method(const DH *dh)
55 return dh_new_intern(NULL, NULL);
58 DH *DH_new_method(ENGINE *engine)
60 return dh_new_intern(engine, NULL);
62 #endif /* !FIPS_MODE */
64 DH *dh_new_with_ctx(OPENSSL_CTX *libctx)
66 return dh_new_intern(NULL, libctx);
69 static DH *dh_new_intern(ENGINE *engine, OPENSSL_CTX *libctx)
71 DH *ret = OPENSSL_zalloc(sizeof(*ret));
74 DHerr(0, ERR_R_MALLOC_FAILURE);
79 ret->lock = CRYPTO_THREAD_lock_new();
80 if (ret->lock == NULL) {
81 DHerr(0, ERR_R_MALLOC_FAILURE);
87 ret->meth = DH_get_default_method();
88 #if !defined(FIPS_MODE) && !defined(OPENSSL_NO_ENGINE)
89 ret->flags = ret->meth->flags; /* early default init */
91 if (!ENGINE_init(engine)) {
92 DHerr(0, ERR_R_ENGINE_LIB);
97 ret->engine = ENGINE_get_default_DH();
99 ret->meth = ENGINE_get_DH(ret->engine);
100 if (ret->meth == NULL) {
101 DHerr(0, ERR_R_ENGINE_LIB);
107 ret->flags = ret->meth->flags;
110 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data))
112 #endif /* FIPS_MODE */
114 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
115 DHerr(0, ERR_R_INIT_FAIL);
133 CRYPTO_DOWN_REF(&r->references, &i, r->lock);
134 REF_PRINT_COUNT("DH", r);
137 REF_ASSERT_ISNT(i < 0);
139 if (r->meth != NULL && r->meth->finish != NULL)
141 #if !defined(FIPS_MODE)
142 # if !defined(OPENSSL_NO_ENGINE)
143 ENGINE_finish(r->engine);
145 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
148 CRYPTO_THREAD_lock_free(r->lock);
150 ffc_params_cleanup(&r->params);
151 BN_clear_free(r->pub_key);
152 BN_clear_free(r->priv_key);
160 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
163 REF_PRINT_COUNT("DH", r);
164 REF_ASSERT_ISNT(i < 2);
165 return ((i > 1) ? 1 : 0);
169 int DH_set_ex_data(DH *d, int idx, void *arg)
171 return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
174 void *DH_get_ex_data(DH *d, int idx)
176 return CRYPTO_get_ex_data(&d->ex_data, idx);
180 int DH_bits(const DH *dh)
182 return BN_num_bits(dh->params.p);
185 int DH_size(const DH *dh)
187 return BN_num_bytes(dh->params.p);
190 int DH_security_bits(const DH *dh)
193 if (dh->params.q != NULL)
194 N = BN_num_bits(dh->params.q);
199 return BN_security_bits(BN_num_bits(dh->params.p), N);
202 void DH_get0_pqg(const DH *dh,
203 const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
205 ffc_params_get0_pqg(&dh->params, p, q, g);
208 int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
210 /* If the fields p and g in d are NULL, the corresponding input
211 * parameters MUST be non-NULL. q may remain NULL.
213 if ((dh->params.p == NULL && p == NULL)
214 || (dh->params.g == NULL && g == NULL))
217 ffc_params_set0_pqg(&dh->params, p, q, g);
218 dh->params.nid = NID_undef;
220 * Check if this is a named group. If it finds a named group then the
221 * 'q' and 'length' value are either already set or are set by the
224 if (DH_get_nid(dh) == NID_undef) {
225 /* If its not a named group then set the 'length' if q is not NULL */
227 dh->length = BN_num_bits(q);
233 long DH_get_length(const DH *dh)
238 int DH_set_length(DH *dh, long length)
244 void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
247 *pub_key = dh->pub_key;
248 if (priv_key != NULL)
249 *priv_key = dh->priv_key;
252 int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
254 if (pub_key != NULL) {
255 BN_clear_free(dh->pub_key);
256 dh->pub_key = pub_key;
258 if (priv_key != NULL) {
259 BN_clear_free(dh->priv_key);
260 dh->priv_key = priv_key;
267 const BIGNUM *DH_get0_p(const DH *dh)
272 const BIGNUM *DH_get0_q(const DH *dh)
277 const BIGNUM *DH_get0_g(const DH *dh)
282 const BIGNUM *DH_get0_priv_key(const DH *dh)
287 const BIGNUM *DH_get0_pub_key(const DH *dh)
292 void DH_clear_flags(DH *dh, int flags)
297 int DH_test_flags(const DH *dh, int flags)
299 return dh->flags & flags;
302 void DH_set_flags(DH *dh, int flags)
308 ENGINE *DH_get0_engine(DH *dh)
312 #endif /*FIPS_MODE */
314 FFC_PARAMS *dh_get0_params(DH *dh)
318 int dh_get0_nid(const DH *dh)
320 return dh->params.nid;