]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/dh/dh_lib.c
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
,
187 const BIGNUM
**p
, const BIGNUM
**q
, const BIGNUM
**g
)
197 int DH_set0_pqg(DH
*dh
, BIGNUM
*p
, BIGNUM
*q
, BIGNUM
*g
)
199 /* If the fields p and g in d are NULL, the corresponding input
200 * parameters MUST be non-NULL. q may remain NULL.
202 if (dh
->p
== NULL
&& p
== NULL
203 || dh
->g
== NULL
&& g
== NULL
)
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
, const BIGNUM
**pub_key
, const 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 field pub_key in dh is NULL, the corresponding input
248 * parameters MUST be non-NULL. The priv_key field may
251 if (dh
->pub_key
== NULL
&& pub_key
== NULL
)
254 if (pub_key
!= NULL
) {
255 BN_free(dh
->pub_key
);
256 dh
->pub_key
= pub_key
;
258 if (priv_key
!= NULL
) {
259 BN_free(dh
->priv_key
);
260 dh
->priv_key
= priv_key
;
266 void DH_clear_flags(DH
*dh
, int flags
)
271 int DH_test_flags(const DH
*dh
, int flags
)
273 return dh
->flags
& flags
;
276 void DH_set_flags(DH
*dh
, int flags
)
281 ENGINE
*DH_get0_engine(DH
*dh
)