]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/dh/dh_lib.c
d7fe850f58cddde5f6c3aa50ec0b6de035244af0
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
);
50 return dh_new_intern(NULL
, NULL
);
53 DH
*DH_new_method(ENGINE
*engine
)
55 return dh_new_intern(engine
, NULL
);
57 #endif /* !FIPS_MODE */
59 DH
*dh_new_with_ctx(OPENSSL_CTX
*libctx
)
61 return dh_new_intern(NULL
, libctx
);
64 static DH
*dh_new_intern(ENGINE
*engine
, OPENSSL_CTX
*libctx
)
66 DH
*ret
= OPENSSL_zalloc(sizeof(*ret
));
69 DHerr(0, ERR_R_MALLOC_FAILURE
);
74 ret
->lock
= CRYPTO_THREAD_lock_new();
75 if (ret
->lock
== NULL
) {
76 DHerr(0, ERR_R_MALLOC_FAILURE
);
82 ret
->meth
= DH_get_default_method();
83 #if !defined(FIPS_MODE) && !defined(OPENSSL_NO_ENGINE)
84 ret
->flags
= ret
->meth
->flags
; /* early default init */
86 if (!ENGINE_init(engine
)) {
87 DHerr(0, ERR_R_ENGINE_LIB
);
92 ret
->engine
= ENGINE_get_default_DH();
94 ret
->meth
= ENGINE_get_DH(ret
->engine
);
95 if (ret
->meth
== NULL
) {
96 DHerr(0, ERR_R_ENGINE_LIB
);
102 ret
->flags
= ret
->meth
->flags
;
105 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH
, ret
, &ret
->ex_data
))
107 #endif /* FIPS_MODE */
109 if ((ret
->meth
->init
!= NULL
) && !ret
->meth
->init(ret
)) {
110 DHerr(0, ERR_R_INIT_FAIL
);
128 CRYPTO_DOWN_REF(&r
->references
, &i
, r
->lock
);
129 REF_PRINT_COUNT("DH", r
);
132 REF_ASSERT_ISNT(i
< 0);
134 if (r
->meth
!= NULL
&& r
->meth
->finish
!= NULL
)
136 #if !defined(FIPS_MODE)
137 # if !defined(OPENSSL_NO_ENGINE)
138 ENGINE_finish(r
->engine
);
140 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH
, r
, &r
->ex_data
);
143 CRYPTO_THREAD_lock_free(r
->lock
);
145 ffc_params_cleanup(&r
->params
);
146 BN_clear_free(r
->pub_key
);
147 BN_clear_free(r
->priv_key
);
155 if (CRYPTO_UP_REF(&r
->references
, &i
, r
->lock
) <= 0)
158 REF_PRINT_COUNT("DH", r
);
159 REF_ASSERT_ISNT(i
< 2);
160 return ((i
> 1) ? 1 : 0);
164 int DH_set_ex_data(DH
*d
, int idx
, void *arg
)
166 return CRYPTO_set_ex_data(&d
->ex_data
, idx
, arg
);
169 void *DH_get_ex_data(DH
*d
, int idx
)
171 return CRYPTO_get_ex_data(&d
->ex_data
, idx
);
175 int DH_bits(const DH
*dh
)
177 return BN_num_bits(dh
->params
.p
);
180 int DH_size(const DH
*dh
)
182 return BN_num_bytes(dh
->params
.p
);
185 int DH_security_bits(const DH
*dh
)
188 if (dh
->params
.q
!= NULL
)
189 N
= BN_num_bits(dh
->params
.q
);
194 return BN_security_bits(BN_num_bits(dh
->params
.p
), N
);
197 void DH_get0_pqg(const DH
*dh
,
198 const BIGNUM
**p
, const BIGNUM
**q
, const BIGNUM
**g
)
200 ffc_params_get0_pqg(&dh
->params
, p
, q
, g
);
203 int DH_set0_pqg(DH
*dh
, BIGNUM
*p
, BIGNUM
*q
, BIGNUM
*g
)
205 /* If the fields p and g in d are NULL, the corresponding input
206 * parameters MUST be non-NULL. q may remain NULL.
208 if ((dh
->params
.p
== NULL
&& p
== NULL
)
209 || (dh
->params
.g
== NULL
&& g
== NULL
))
212 ffc_params_set0_pqg(&dh
->params
, p
, q
, g
);
213 dh
->params
.nid
= NID_undef
;
214 DH_get_nid(dh
); /* Check if this is a named group and cache it */
217 dh
->length
= BN_num_bits(q
);
223 long DH_get_length(const DH
*dh
)
228 int DH_set_length(DH
*dh
, long length
)
234 void DH_get0_key(const DH
*dh
, const BIGNUM
**pub_key
, const BIGNUM
**priv_key
)
237 *pub_key
= dh
->pub_key
;
238 if (priv_key
!= NULL
)
239 *priv_key
= dh
->priv_key
;
242 int DH_set0_key(DH
*dh
, BIGNUM
*pub_key
, BIGNUM
*priv_key
)
244 if (pub_key
!= NULL
) {
245 BN_clear_free(dh
->pub_key
);
246 dh
->pub_key
= pub_key
;
248 if (priv_key
!= NULL
) {
249 BN_clear_free(dh
->priv_key
);
250 dh
->priv_key
= priv_key
;
257 const BIGNUM
*DH_get0_p(const DH
*dh
)
262 const BIGNUM
*DH_get0_q(const DH
*dh
)
267 const BIGNUM
*DH_get0_g(const DH
*dh
)
272 const BIGNUM
*DH_get0_priv_key(const DH
*dh
)
277 const BIGNUM
*DH_get0_pub_key(const DH
*dh
)
282 void DH_clear_flags(DH
*dh
, int flags
)
287 int DH_test_flags(const DH
*dh
, int flags
)
289 return dh
->flags
& flags
;
292 void DH_set_flags(DH
*dh
, int flags
)
298 ENGINE
*DH_get0_engine(DH
*dh
)
302 #endif /*FIPS_MODE */
304 FFC_PARAMS
*dh_get0_params(DH
*dh
)
308 int dh_get0_nid(const DH
*dh
)
310 return dh
->params
.nid
;