]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/dh/dh_lib.c
Add missing RAND_DRBG locking
[thirdparty/openssl.git] / crypto / dh / dh_lib.c
CommitLineData
aa6bb135
RS
1/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
aa6bb135
RS
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
d02b48c6
RE
8 */
9
10#include <stdio.h>
b39fc560 11#include "internal/cryptlib.h"
cd420b0b 12#include "internal/refcount.h"
ec577822 13#include <openssl/bn.h>
0aeddcfa 14#include "dh_locl.h"
3c27208f 15#include <openssl/engine.h>
d02b48c6 16
cb78486d 17int DH_set_method(DH *dh, const DH_METHOD *meth)
0f113f3e
MC
18{
19 /*
20 * NB: The caller is specifically setting a method, so it's not up to us
21 * to deal with which ENGINE it comes from.
22 */
23 const DH_METHOD *mtmp;
24 mtmp = dh->meth;
25 if (mtmp->finish)
26 mtmp->finish(dh);
0b13e9f0 27#ifndef OPENSSL_NO_ENGINE
7c96dbcd
RS
28 ENGINE_finish(dh->engine);
29 dh->engine = NULL;
0b13e9f0 30#endif
0f113f3e
MC
31 dh->meth = meth;
32 if (meth->init)
33 meth->init(dh);
34 return 1;
35}
13066cee 36
6b691a5c 37DH *DH_new(void)
0f113f3e
MC
38{
39 return DH_new_method(NULL);
40}
13066cee 41
5270e702 42DH *DH_new_method(ENGINE *engine)
0f113f3e 43{
64b25758 44 DH *ret = OPENSSL_zalloc(sizeof(*ret));
13066cee 45
0f113f3e
MC
46 if (ret == NULL) {
47 DHerr(DH_F_DH_NEW_METHOD, ERR_R_MALLOC_FAILURE);
d188a536 48 return NULL;
0f113f3e 49 }
0c9de428 50
2bbf0baa
F
51 ret->references = 1;
52 ret->lock = CRYPTO_THREAD_lock_new();
53 if (ret->lock == NULL) {
b2b361f6 54 DHerr(DH_F_DH_NEW_METHOD, ERR_R_MALLOC_FAILURE);
2bbf0baa
F
55 OPENSSL_free(ret);
56 return NULL;
57 }
58
0f113f3e 59 ret->meth = DH_get_default_method();
0b13e9f0 60#ifndef OPENSSL_NO_ENGINE
2bbf0baa 61 ret->flags = ret->meth->flags; /* early default init */
0f113f3e
MC
62 if (engine) {
63 if (!ENGINE_init(engine)) {
64 DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB);
2bbf0baa 65 goto err;
0f113f3e
MC
66 }
67 ret->engine = engine;
68 } else
69 ret->engine = ENGINE_get_default_DH();
70 if (ret->engine) {
71 ret->meth = ENGINE_get_DH(ret->engine);
7c96dbcd 72 if (ret->meth == NULL) {
0f113f3e 73 DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB);
2bbf0baa 74 goto err;
0f113f3e
MC
75 }
76 }
0b13e9f0 77#endif
cb78486d 78
0f113f3e 79 ret->flags = ret->meth->flags;
d188a536 80
2bbf0baa
F
81 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data))
82 goto err;
d188a536
AG
83
84 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
2bbf0baa
F
85 DHerr(DH_F_DH_NEW_METHOD, ERR_R_INIT_FAIL);
86err:
d188a536 87 DH_free(ret);
0f113f3e
MC
88 ret = NULL;
89 }
d188a536
AG
90
91 return ret;
0f113f3e 92}
d02b48c6 93
6b691a5c 94void DH_free(DH *r)
0f113f3e
MC
95{
96 int i;
d6407083 97
0f113f3e
MC
98 if (r == NULL)
99 return;
d188a536 100
2f545ae4 101 CRYPTO_DOWN_REF(&r->references, &i, r->lock);
f3f1cf84 102 REF_PRINT_COUNT("DH", r);
0f113f3e
MC
103 if (i > 0)
104 return;
f3f1cf84 105 REF_ASSERT_ISNT(i < 0);
13066cee 106
0f113f3e
MC
107 if (r->meth->finish)
108 r->meth->finish(r);
0b13e9f0 109#ifndef OPENSSL_NO_ENGINE
7c96dbcd 110 ENGINE_finish(r->engine);
0b13e9f0 111#endif
ac8b4ee0 112
0f113f3e 113 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
d50f1bdf 114
d188a536
AG
115 CRYPTO_THREAD_lock_free(r->lock);
116
23a1d5e9
RS
117 BN_clear_free(r->p);
118 BN_clear_free(r->g);
119 BN_clear_free(r->q);
120 BN_clear_free(r->j);
b548a1f1 121 OPENSSL_free(r->seed);
23a1d5e9
RS
122 BN_clear_free(r->counter);
123 BN_clear_free(r->pub_key);
124 BN_clear_free(r->priv_key);
0f113f3e
MC
125 OPENSSL_free(r);
126}
d02b48c6 127
dc2a33d6 128int DH_up_ref(DH *r)
0f113f3e 129{
d188a536
AG
130 int i;
131
2f545ae4 132 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
d188a536 133 return 0;
f3f1cf84
RS
134
135 REF_PRINT_COUNT("DH", r);
136 REF_ASSERT_ISNT(i < 2);
0f113f3e
MC
137 return ((i > 1) ? 1 : 0);
138}
5cbc2e8b 139
dd9d233e 140int DH_set_ex_data(DH *d, int idx, void *arg)
0f113f3e
MC
141{
142 return (CRYPTO_set_ex_data(&d->ex_data, idx, arg));
143}
13066cee 144
dd9d233e 145void *DH_get_ex_data(DH *d, int idx)
0f113f3e
MC
146{
147 return (CRYPTO_get_ex_data(&d->ex_data, idx));
148}
13066cee 149
26c79d56
KR
150int DH_bits(const DH *dh)
151{
152 return BN_num_bits(dh->p);
153}
154
f971ccb2 155int DH_size(const DH *dh)
0f113f3e
MC
156{
157 return (BN_num_bytes(dh->p));
158}
2514fa79
DSH
159
160int DH_security_bits(const DH *dh)
0f113f3e
MC
161{
162 int N;
163 if (dh->q)
164 N = BN_num_bits(dh->q);
165 else if (dh->length)
166 N = dh->length;
167 else
168 N = -1;
169 return BN_security_bits(BN_num_bits(dh->p), N);
170}
0aeddcfa
MC
171
172
fd809cfd
RL
173void DH_get0_pqg(const DH *dh,
174 const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
0aeddcfa
MC
175{
176 if (p != NULL)
177 *p = dh->p;
178 if (q != NULL)
179 *q = dh->q;
180 if (g != NULL)
181 *g = dh->g;
182}
183
184int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
185{
1da12e34
RL
186 /* If the fields p and g in d are NULL, the corresponding input
187 * parameters MUST be non-NULL. q may remain NULL.
1da12e34 188 */
b84e1226
MC
189 if ((dh->p == NULL && p == NULL)
190 || (dh->g == NULL && g == NULL))
0aeddcfa 191 return 0;
1da12e34
RL
192
193 if (p != NULL) {
194 BN_free(dh->p);
195 dh->p = p;
196 }
197 if (q != NULL) {
198 BN_free(dh->q);
199 dh->q = q;
200 }
201 if (g != NULL) {
202 BN_free(dh->g);
203 dh->g = g;
204 }
0aeddcfa
MC
205
206 if (q != NULL) {
207 dh->length = BN_num_bits(q);
208 }
209
210 return 1;
211}
212
213long DH_get_length(const DH *dh)
214{
215 return dh->length;
216}
217
218int DH_set_length(DH *dh, long length)
219{
220 dh->length = length;
221 return 1;
222}
223
fd809cfd 224void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
0aeddcfa
MC
225{
226 if (pub_key != NULL)
227 *pub_key = dh->pub_key;
228 if (priv_key != NULL)
229 *priv_key = dh->priv_key;
230}
231
232int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
233{
1da12e34
RL
234 if (pub_key != NULL) {
235 BN_free(dh->pub_key);
236 dh->pub_key = pub_key;
237 }
238 if (priv_key != NULL) {
239 BN_free(dh->priv_key);
240 dh->priv_key = priv_key;
241 }
0aeddcfa
MC
242
243 return 1;
244}
245
246void DH_clear_flags(DH *dh, int flags)
247{
248 dh->flags &= ~flags;
249}
250
251int DH_test_flags(const DH *dh, int flags)
252{
253 return dh->flags & flags;
254}
255
256void DH_set_flags(DH *dh, int flags)
257{
258 dh->flags |= flags;
259}
260
261ENGINE *DH_get0_engine(DH *dh)
262{
263 return dh->engine;
264}