]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/dh/dh_lib.c
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 "crypto/dh.h"
16 #include <openssl/engine.h>
17 #include "crypto/dh.h"
20 int DH_set_method(DH
*dh
, const DH_METHOD
*meth
)
23 * NB: The caller is specifically setting a method, so it's not up to us
24 * to deal with which ENGINE it comes from.
26 const DH_METHOD
*mtmp
;
30 #ifndef OPENSSL_NO_ENGINE
31 ENGINE_finish(dh
->engine
);
39 #endif /* !FIPS_MODE */
43 return DH_new_method(NULL
);
46 DH
*DH_new_method(ENGINE
*engine
)
48 DH
*ret
= OPENSSL_zalloc(sizeof(*ret
));
51 DHerr(DH_F_DH_NEW_METHOD
, ERR_R_MALLOC_FAILURE
);
56 ret
->lock
= CRYPTO_THREAD_lock_new();
57 if (ret
->lock
== NULL
) {
58 DHerr(DH_F_DH_NEW_METHOD
, ERR_R_MALLOC_FAILURE
);
63 ret
->meth
= DH_get_default_method();
64 #if !defined(FIPS_MODE) && !defined(OPENSSL_NO_ENGINE)
65 ret
->flags
= ret
->meth
->flags
; /* early default init */
67 if (!ENGINE_init(engine
)) {
68 DHerr(DH_F_DH_NEW_METHOD
, ERR_R_ENGINE_LIB
);
73 ret
->engine
= ENGINE_get_default_DH();
75 ret
->meth
= ENGINE_get_DH(ret
->engine
);
76 if (ret
->meth
== NULL
) {
77 DHerr(DH_F_DH_NEW_METHOD
, ERR_R_ENGINE_LIB
);
83 ret
->flags
= ret
->meth
->flags
;
86 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH
, ret
, &ret
->ex_data
))
88 #endif /* FIPS_MODE */
90 if ((ret
->meth
->init
!= NULL
) && !ret
->meth
->init(ret
)) {
91 DHerr(DH_F_DH_NEW_METHOD
, ERR_R_INIT_FAIL
);
109 CRYPTO_DOWN_REF(&r
->references
, &i
, r
->lock
);
110 REF_PRINT_COUNT("DH", r
);
113 REF_ASSERT_ISNT(i
< 0);
115 if (r
->meth
!= NULL
&& r
->meth
->finish
!= NULL
)
117 #if !defined(FIPS_MODE)
118 # if !defined(OPENSSL_NO_ENGINE)
119 ENGINE_finish(r
->engine
);
121 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH
, r
, &r
->ex_data
);
124 CRYPTO_THREAD_lock_free(r
->lock
);
126 ffc_params_cleanup(&r
->params
);
127 BN_clear_free(r
->pub_key
);
128 BN_clear_free(r
->priv_key
);
136 if (CRYPTO_UP_REF(&r
->references
, &i
, r
->lock
) <= 0)
139 REF_PRINT_COUNT("DH", r
);
140 REF_ASSERT_ISNT(i
< 2);
141 return ((i
> 1) ? 1 : 0);
145 int DH_set_ex_data(DH
*d
, int idx
, void *arg
)
147 return CRYPTO_set_ex_data(&d
->ex_data
, idx
, arg
);
150 void *DH_get_ex_data(DH
*d
, int idx
)
152 return CRYPTO_get_ex_data(&d
->ex_data
, idx
);
156 int DH_bits(const DH
*dh
)
158 return BN_num_bits(dh
->params
.p
);
161 int DH_size(const DH
*dh
)
163 return BN_num_bytes(dh
->params
.p
);
166 int DH_security_bits(const DH
*dh
)
169 if (dh
->params
.q
!= NULL
)
170 N
= BN_num_bits(dh
->params
.q
);
175 return BN_security_bits(BN_num_bits(dh
->params
.p
), N
);
178 void DH_get0_pqg(const DH
*dh
,
179 const BIGNUM
**p
, const BIGNUM
**q
, const BIGNUM
**g
)
181 ffc_params_get0_pqg(&dh
->params
, p
, q
, g
);
184 int DH_set0_pqg(DH
*dh
, BIGNUM
*p
, BIGNUM
*q
, BIGNUM
*g
)
186 /* If the fields p and g in d are NULL, the corresponding input
187 * parameters MUST be non-NULL. q may remain NULL.
189 if ((dh
->params
.p
== NULL
&& p
== NULL
)
190 || (dh
->params
.g
== NULL
&& g
== NULL
))
193 ffc_params_set0_pqg(&dh
->params
, p
, q
, g
);
196 dh
->length
= BN_num_bits(q
);
202 long DH_get_length(const DH
*dh
)
207 int DH_set_length(DH
*dh
, long length
)
213 void DH_get0_key(const DH
*dh
, const BIGNUM
**pub_key
, const BIGNUM
**priv_key
)
216 *pub_key
= dh
->pub_key
;
217 if (priv_key
!= NULL
)
218 *priv_key
= dh
->priv_key
;
221 int DH_set0_key(DH
*dh
, BIGNUM
*pub_key
, BIGNUM
*priv_key
)
223 if (pub_key
!= NULL
) {
224 BN_clear_free(dh
->pub_key
);
225 dh
->pub_key
= pub_key
;
227 if (priv_key
!= NULL
) {
228 BN_clear_free(dh
->priv_key
);
229 dh
->priv_key
= priv_key
;
236 const BIGNUM
*DH_get0_p(const DH
*dh
)
241 const BIGNUM
*DH_get0_q(const DH
*dh
)
246 const BIGNUM
*DH_get0_g(const DH
*dh
)
251 const BIGNUM
*DH_get0_priv_key(const DH
*dh
)
256 const BIGNUM
*DH_get0_pub_key(const DH
*dh
)
261 void DH_clear_flags(DH
*dh
, int flags
)
266 int DH_test_flags(const DH
*dh
, int flags
)
268 return dh
->flags
& flags
;
271 void DH_set_flags(DH
*dh
, int flags
)
277 ENGINE
*DH_get0_engine(DH
*dh
)
281 #endif /*FIPS_MODE */
283 FFC_PARAMS
*dh_get0_params(DH
*dh
)