]>
Commit | Line | Data |
---|---|---|
aa6bb135 | 1 | /* |
83cf7abf | 2 | * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. |
d02b48c6 | 3 | * |
e38873f5 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
aa6bb135 RS |
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> |
706457b7 | 14 | #include "dh_local.h" |
62f49b90 | 15 | #include "crypto/dh.h" |
3c27208f | 16 | #include <openssl/engine.h> |
dc8de3e6 | 17 | #include "crypto/dh.h" |
d02b48c6 | 18 | |
62f49b90 | 19 | #ifndef FIPS_MODE |
cb78486d | 20 | int DH_set_method(DH *dh, const DH_METHOD *meth) |
0f113f3e MC |
21 | { |
22 | /* | |
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. | |
25 | */ | |
26 | const DH_METHOD *mtmp; | |
27 | mtmp = dh->meth; | |
28 | if (mtmp->finish) | |
29 | mtmp->finish(dh); | |
0b13e9f0 | 30 | #ifndef OPENSSL_NO_ENGINE |
7c96dbcd RS |
31 | ENGINE_finish(dh->engine); |
32 | dh->engine = NULL; | |
0b13e9f0 | 33 | #endif |
0f113f3e MC |
34 | dh->meth = meth; |
35 | if (meth->init) | |
36 | meth->init(dh); | |
37 | return 1; | |
38 | } | |
62f49b90 | 39 | #endif /* !FIPS_MODE */ |
13066cee | 40 | |
6b691a5c | 41 | DH *DH_new(void) |
0f113f3e MC |
42 | { |
43 | return DH_new_method(NULL); | |
44 | } | |
13066cee | 45 | |
5270e702 | 46 | DH *DH_new_method(ENGINE *engine) |
0f113f3e | 47 | { |
64b25758 | 48 | DH *ret = OPENSSL_zalloc(sizeof(*ret)); |
13066cee | 49 | |
0f113f3e MC |
50 | if (ret == NULL) { |
51 | DHerr(DH_F_DH_NEW_METHOD, ERR_R_MALLOC_FAILURE); | |
d188a536 | 52 | return NULL; |
0f113f3e | 53 | } |
0c9de428 | 54 | |
2bbf0baa F |
55 | ret->references = 1; |
56 | ret->lock = CRYPTO_THREAD_lock_new(); | |
57 | if (ret->lock == NULL) { | |
b2b361f6 | 58 | DHerr(DH_F_DH_NEW_METHOD, ERR_R_MALLOC_FAILURE); |
2bbf0baa F |
59 | OPENSSL_free(ret); |
60 | return NULL; | |
61 | } | |
62 | ||
0f113f3e | 63 | ret->meth = DH_get_default_method(); |
62f49b90 | 64 | #if !defined(FIPS_MODE) && !defined(OPENSSL_NO_ENGINE) |
2bbf0baa | 65 | ret->flags = ret->meth->flags; /* early default init */ |
0f113f3e MC |
66 | if (engine) { |
67 | if (!ENGINE_init(engine)) { | |
68 | DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB); | |
2bbf0baa | 69 | goto err; |
0f113f3e MC |
70 | } |
71 | ret->engine = engine; | |
72 | } else | |
73 | ret->engine = ENGINE_get_default_DH(); | |
74 | if (ret->engine) { | |
75 | ret->meth = ENGINE_get_DH(ret->engine); | |
7c96dbcd | 76 | if (ret->meth == NULL) { |
0f113f3e | 77 | DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB); |
2bbf0baa | 78 | goto err; |
0f113f3e MC |
79 | } |
80 | } | |
0b13e9f0 | 81 | #endif |
cb78486d | 82 | |
0f113f3e | 83 | ret->flags = ret->meth->flags; |
d188a536 | 84 | |
a3327784 | 85 | #ifndef FIPS_MODE |
2bbf0baa F |
86 | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data)) |
87 | goto err; | |
62f49b90 | 88 | #endif /* FIPS_MODE */ |
d188a536 AG |
89 | |
90 | if ((ret->meth->init != NULL) && !ret->meth->init(ret)) { | |
2bbf0baa | 91 | DHerr(DH_F_DH_NEW_METHOD, ERR_R_INIT_FAIL); |
544648a8 | 92 | goto err; |
0f113f3e | 93 | } |
d188a536 AG |
94 | |
95 | return ret; | |
544648a8 NT |
96 | |
97 | err: | |
98 | DH_free(ret); | |
99 | return NULL; | |
0f113f3e | 100 | } |
d02b48c6 | 101 | |
6b691a5c | 102 | void DH_free(DH *r) |
0f113f3e MC |
103 | { |
104 | int i; | |
d6407083 | 105 | |
0f113f3e MC |
106 | if (r == NULL) |
107 | return; | |
d188a536 | 108 | |
2f545ae4 | 109 | CRYPTO_DOWN_REF(&r->references, &i, r->lock); |
f3f1cf84 | 110 | REF_PRINT_COUNT("DH", r); |
0f113f3e MC |
111 | if (i > 0) |
112 | return; | |
f3f1cf84 | 113 | REF_ASSERT_ISNT(i < 0); |
13066cee | 114 | |
0c5d725e | 115 | if (r->meth != NULL && r->meth->finish != NULL) |
0f113f3e | 116 | r->meth->finish(r); |
62f49b90 SL |
117 | #if !defined(FIPS_MODE) |
118 | # if !defined(OPENSSL_NO_ENGINE) | |
7c96dbcd | 119 | ENGINE_finish(r->engine); |
62f49b90 | 120 | # endif |
0f113f3e | 121 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data); |
a3327784 | 122 | #endif |
d50f1bdf | 123 | |
d188a536 AG |
124 | CRYPTO_THREAD_lock_free(r->lock); |
125 | ||
dc8de3e6 | 126 | ffc_params_cleanup(&r->params); |
23a1d5e9 RS |
127 | BN_clear_free(r->pub_key); |
128 | BN_clear_free(r->priv_key); | |
0f113f3e MC |
129 | OPENSSL_free(r); |
130 | } | |
d02b48c6 | 131 | |
dc2a33d6 | 132 | int DH_up_ref(DH *r) |
0f113f3e | 133 | { |
d188a536 AG |
134 | int i; |
135 | ||
2f545ae4 | 136 | if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0) |
d188a536 | 137 | return 0; |
f3f1cf84 RS |
138 | |
139 | REF_PRINT_COUNT("DH", r); | |
140 | REF_ASSERT_ISNT(i < 2); | |
0f113f3e MC |
141 | return ((i > 1) ? 1 : 0); |
142 | } | |
5cbc2e8b | 143 | |
a3327784 | 144 | #ifndef FIPS_MODE |
dd9d233e | 145 | int DH_set_ex_data(DH *d, int idx, void *arg) |
0f113f3e | 146 | { |
26a7d938 | 147 | return CRYPTO_set_ex_data(&d->ex_data, idx, arg); |
0f113f3e | 148 | } |
13066cee | 149 | |
dd9d233e | 150 | void *DH_get_ex_data(DH *d, int idx) |
0f113f3e | 151 | { |
26a7d938 | 152 | return CRYPTO_get_ex_data(&d->ex_data, idx); |
0f113f3e | 153 | } |
a3327784 | 154 | #endif |
13066cee | 155 | |
26c79d56 KR |
156 | int DH_bits(const DH *dh) |
157 | { | |
dc8de3e6 | 158 | return BN_num_bits(dh->params.p); |
26c79d56 KR |
159 | } |
160 | ||
f971ccb2 | 161 | int DH_size(const DH *dh) |
0f113f3e | 162 | { |
dc8de3e6 | 163 | return BN_num_bytes(dh->params.p); |
0f113f3e | 164 | } |
2514fa79 DSH |
165 | |
166 | int DH_security_bits(const DH *dh) | |
0f113f3e MC |
167 | { |
168 | int N; | |
dc8de3e6 SL |
169 | if (dh->params.q != NULL) |
170 | N = BN_num_bits(dh->params.q); | |
0f113f3e MC |
171 | else if (dh->length) |
172 | N = dh->length; | |
173 | else | |
174 | N = -1; | |
dc8de3e6 | 175 | return BN_security_bits(BN_num_bits(dh->params.p), N); |
0f113f3e | 176 | } |
0aeddcfa | 177 | |
fd809cfd RL |
178 | void DH_get0_pqg(const DH *dh, |
179 | const BIGNUM **p, const BIGNUM **q, const BIGNUM **g) | |
0aeddcfa | 180 | { |
dc8de3e6 | 181 | ffc_params_get0_pqg(&dh->params, p, q, g); |
0aeddcfa MC |
182 | } |
183 | ||
184 | int 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 | */ |
dc8de3e6 SL |
189 | if ((dh->params.p == NULL && p == NULL) |
190 | || (dh->params.g == NULL && g == NULL)) | |
0aeddcfa | 191 | return 0; |
1da12e34 | 192 | |
dc8de3e6 | 193 | ffc_params_set0_pqg(&dh->params, p, q, g); |
0aeddcfa | 194 | |
dc8de3e6 | 195 | if (q != NULL) |
0aeddcfa | 196 | dh->length = BN_num_bits(q); |
0aeddcfa | 197 | |
8b84b075 | 198 | dh->dirty_cnt++; |
0aeddcfa MC |
199 | return 1; |
200 | } | |
201 | ||
202 | long DH_get_length(const DH *dh) | |
203 | { | |
204 | return dh->length; | |
205 | } | |
206 | ||
207 | int DH_set_length(DH *dh, long length) | |
208 | { | |
209 | dh->length = length; | |
210 | return 1; | |
211 | } | |
212 | ||
fd809cfd | 213 | void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key) |
0aeddcfa MC |
214 | { |
215 | if (pub_key != NULL) | |
216 | *pub_key = dh->pub_key; | |
217 | if (priv_key != NULL) | |
218 | *priv_key = dh->priv_key; | |
219 | } | |
220 | ||
221 | int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key) | |
222 | { | |
1da12e34 | 223 | if (pub_key != NULL) { |
fa01370f | 224 | BN_clear_free(dh->pub_key); |
1da12e34 RL |
225 | dh->pub_key = pub_key; |
226 | } | |
227 | if (priv_key != NULL) { | |
fa01370f | 228 | BN_clear_free(dh->priv_key); |
1da12e34 RL |
229 | dh->priv_key = priv_key; |
230 | } | |
0aeddcfa | 231 | |
8b84b075 | 232 | dh->dirty_cnt++; |
0aeddcfa MC |
233 | return 1; |
234 | } | |
235 | ||
6db7fadf DMSP |
236 | const BIGNUM *DH_get0_p(const DH *dh) |
237 | { | |
dc8de3e6 | 238 | return dh->params.p; |
6db7fadf DMSP |
239 | } |
240 | ||
241 | const BIGNUM *DH_get0_q(const DH *dh) | |
242 | { | |
dc8de3e6 | 243 | return dh->params.q; |
6db7fadf DMSP |
244 | } |
245 | ||
246 | const BIGNUM *DH_get0_g(const DH *dh) | |
247 | { | |
dc8de3e6 | 248 | return dh->params.g; |
6db7fadf DMSP |
249 | } |
250 | ||
251 | const BIGNUM *DH_get0_priv_key(const DH *dh) | |
252 | { | |
253 | return dh->priv_key; | |
254 | } | |
255 | ||
256 | const BIGNUM *DH_get0_pub_key(const DH *dh) | |
257 | { | |
258 | return dh->pub_key; | |
259 | } | |
260 | ||
0aeddcfa MC |
261 | void DH_clear_flags(DH *dh, int flags) |
262 | { | |
263 | dh->flags &= ~flags; | |
264 | } | |
265 | ||
266 | int DH_test_flags(const DH *dh, int flags) | |
267 | { | |
268 | return dh->flags & flags; | |
269 | } | |
270 | ||
271 | void DH_set_flags(DH *dh, int flags) | |
272 | { | |
273 | dh->flags |= flags; | |
274 | } | |
275 | ||
62f49b90 | 276 | #ifndef FIPS_MODE |
0aeddcfa MC |
277 | ENGINE *DH_get0_engine(DH *dh) |
278 | { | |
279 | return dh->engine; | |
280 | } | |
62f49b90 | 281 | #endif /*FIPS_MODE */ |
dc8de3e6 SL |
282 | |
283 | FFC_PARAMS *dh_get0_params(DH *dh) | |
284 | { | |
285 | return &dh->params; | |
286 | } |