]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/dh/dh_lib.c
e6fc3ef2c565210124d9216f71e14fbb4502265c
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 <openssl/bn.h>
12 #include <openssl/engine.h>
13 #include <openssl/obj_mac.h>
14 #include "internal/cryptlib.h"
15 #include "internal/refcount.h"
16 #include "crypto/dh.h"
19 static DH
*dh_new_intern(ENGINE
*engine
, OPENSSL_CTX
*libctx
);
22 int DH_set_method(DH
*dh
, const DH_METHOD
*meth
)
25 * NB: The caller is specifically setting a method, so it's not up to us
26 * to deal with which ENGINE it comes from.
28 const DH_METHOD
*mtmp
;
32 #ifndef OPENSSL_NO_ENGINE
33 ENGINE_finish(dh
->engine
);
44 return dh_new_intern(NULL
, NULL
);
47 DH
*DH_new_method(ENGINE
*engine
)
49 return dh_new_intern(engine
, NULL
);
51 #endif /* !FIPS_MODE */
53 DH
*dh_new_with_ctx(OPENSSL_CTX
*libctx
)
55 return dh_new_intern(NULL
, libctx
);
58 static DH
*dh_new_intern(ENGINE
*engine
, OPENSSL_CTX
*libctx
)
60 DH
*ret
= OPENSSL_zalloc(sizeof(*ret
));
63 DHerr(0, ERR_R_MALLOC_FAILURE
);
68 ret
->lock
= CRYPTO_THREAD_lock_new();
69 if (ret
->lock
== NULL
) {
70 DHerr(0, ERR_R_MALLOC_FAILURE
);
76 ret
->meth
= DH_get_default_method();
77 #if !defined(FIPS_MODE) && !defined(OPENSSL_NO_ENGINE)
78 ret
->flags
= ret
->meth
->flags
; /* early default init */
80 if (!ENGINE_init(engine
)) {
81 DHerr(0, ERR_R_ENGINE_LIB
);
86 ret
->engine
= ENGINE_get_default_DH();
88 ret
->meth
= ENGINE_get_DH(ret
->engine
);
89 if (ret
->meth
== NULL
) {
90 DHerr(0, ERR_R_ENGINE_LIB
);
96 ret
->flags
= ret
->meth
->flags
;
99 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH
, ret
, &ret
->ex_data
))
101 #endif /* FIPS_MODE */
103 if ((ret
->meth
->init
!= NULL
) && !ret
->meth
->init(ret
)) {
104 DHerr(0, ERR_R_INIT_FAIL
);
122 CRYPTO_DOWN_REF(&r
->references
, &i
, r
->lock
);
123 REF_PRINT_COUNT("DH", r
);
126 REF_ASSERT_ISNT(i
< 0);
128 if (r
->meth
!= NULL
&& r
->meth
->finish
!= NULL
)
130 #if !defined(FIPS_MODE)
131 # if !defined(OPENSSL_NO_ENGINE)
132 ENGINE_finish(r
->engine
);
134 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH
, r
, &r
->ex_data
);
137 CRYPTO_THREAD_lock_free(r
->lock
);
139 ffc_params_cleanup(&r
->params
);
140 BN_clear_free(r
->pub_key
);
141 BN_clear_free(r
->priv_key
);
149 if (CRYPTO_UP_REF(&r
->references
, &i
, r
->lock
) <= 0)
152 REF_PRINT_COUNT("DH", r
);
153 REF_ASSERT_ISNT(i
< 2);
154 return ((i
> 1) ? 1 : 0);
158 int DH_set_ex_data(DH
*d
, int idx
, void *arg
)
160 return CRYPTO_set_ex_data(&d
->ex_data
, idx
, arg
);
163 void *DH_get_ex_data(DH
*d
, int idx
)
165 return CRYPTO_get_ex_data(&d
->ex_data
, idx
);
169 int DH_bits(const DH
*dh
)
171 return BN_num_bits(dh
->params
.p
);
174 int DH_size(const DH
*dh
)
176 return BN_num_bytes(dh
->params
.p
);
179 int DH_security_bits(const DH
*dh
)
182 if (dh
->params
.q
!= NULL
)
183 N
= BN_num_bits(dh
->params
.q
);
188 return BN_security_bits(BN_num_bits(dh
->params
.p
), N
);
191 void DH_get0_pqg(const DH
*dh
,
192 const BIGNUM
**p
, const BIGNUM
**q
, const BIGNUM
**g
)
194 ffc_params_get0_pqg(&dh
->params
, p
, q
, 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
->params
.p
== NULL
&& p
== NULL
)
203 || (dh
->params
.g
== NULL
&& g
== NULL
))
206 ffc_params_set0_pqg(&dh
->params
, p
, q
, g
);
207 dh
->params
.nid
= NID_undef
;
208 DH_get_nid(dh
); /* Check if this is a named group and cache it */
211 dh
->length
= BN_num_bits(q
);
217 long DH_get_length(const DH
*dh
)
222 int DH_set_length(DH
*dh
, long length
)
228 void DH_get0_key(const DH
*dh
, const BIGNUM
**pub_key
, const BIGNUM
**priv_key
)
231 *pub_key
= dh
->pub_key
;
232 if (priv_key
!= NULL
)
233 *priv_key
= dh
->priv_key
;
236 int DH_set0_key(DH
*dh
, BIGNUM
*pub_key
, BIGNUM
*priv_key
)
238 if (pub_key
!= NULL
) {
239 BN_clear_free(dh
->pub_key
);
240 dh
->pub_key
= pub_key
;
242 if (priv_key
!= NULL
) {
243 BN_clear_free(dh
->priv_key
);
244 dh
->priv_key
= priv_key
;
251 const BIGNUM
*DH_get0_p(const DH
*dh
)
256 const BIGNUM
*DH_get0_q(const DH
*dh
)
261 const BIGNUM
*DH_get0_g(const DH
*dh
)
266 const BIGNUM
*DH_get0_priv_key(const DH
*dh
)
271 const BIGNUM
*DH_get0_pub_key(const DH
*dh
)
276 void DH_clear_flags(DH
*dh
, int flags
)
281 int DH_test_flags(const DH
*dh
, int flags
)
283 return dh
->flags
& flags
;
286 void DH_set_flags(DH
*dh
, int flags
)
292 ENGINE
*DH_get0_engine(DH
*dh
)
296 #endif /*FIPS_MODE */
298 FFC_PARAMS
*dh_get0_params(DH
*dh
)
302 int dh_get0_nid(const DH
*dh
)
304 return dh
->params
.nid
;