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