]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/dh/dh_lib.c
Copyright consolidation 09/10
[thirdparty/openssl.git] / crypto / dh / dh_lib.c
CommitLineData
58964a49 1/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
d02b48c6
RE
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
0f113f3e 7 *
d02b48c6
RE
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
0f113f3e 14 *
d02b48c6
RE
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
0f113f3e 21 *
d02b48c6
RE
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
0f113f3e 36 * 4. If you include any Windows specific code (or a derivative thereof) from
d02b48c6
RE
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
0f113f3e 39 *
d02b48c6
RE
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
0f113f3e 51 *
d02b48c6
RE
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57
58#include <stdio.h>
b39fc560 59#include "internal/cryptlib.h"
ec577822 60#include <openssl/bn.h>
0aeddcfa 61#include "dh_locl.h"
3c27208f 62#include <openssl/engine.h>
d02b48c6 63
3a644582 64static const DH_METHOD *default_DH_method = NULL;
13066cee 65
cb78486d 66void DH_set_default_method(const DH_METHOD *meth)
0f113f3e
MC
67{
68 default_DH_method = meth;
69}
13066cee 70
cb78486d 71const DH_METHOD *DH_get_default_method(void)
0f113f3e
MC
72{
73 if (!default_DH_method)
74 default_DH_method = DH_OpenSSL();
75 return default_DH_method;
76}
13066cee 77
cb78486d 78int DH_set_method(DH *dh, const DH_METHOD *meth)
0f113f3e
MC
79{
80 /*
81 * NB: The caller is specifically setting a method, so it's not up to us
82 * to deal with which ENGINE it comes from.
83 */
84 const DH_METHOD *mtmp;
85 mtmp = dh->meth;
86 if (mtmp->finish)
87 mtmp->finish(dh);
0b13e9f0 88#ifndef OPENSSL_NO_ENGINE
7c96dbcd
RS
89 ENGINE_finish(dh->engine);
90 dh->engine = NULL;
0b13e9f0 91#endif
0f113f3e
MC
92 dh->meth = meth;
93 if (meth->init)
94 meth->init(dh);
95 return 1;
96}
13066cee 97
6b691a5c 98DH *DH_new(void)
0f113f3e
MC
99{
100 return DH_new_method(NULL);
101}
13066cee 102
5270e702 103DH *DH_new_method(ENGINE *engine)
0f113f3e 104{
64b25758 105 DH *ret = OPENSSL_zalloc(sizeof(*ret));
13066cee 106
0f113f3e
MC
107 if (ret == NULL) {
108 DHerr(DH_F_DH_NEW_METHOD, ERR_R_MALLOC_FAILURE);
d188a536 109 return NULL;
0f113f3e 110 }
0c9de428 111
2bbf0baa
F
112 ret->references = 1;
113 ret->lock = CRYPTO_THREAD_lock_new();
114 if (ret->lock == NULL) {
115 OPENSSL_free(ret);
116 return NULL;
117 }
118
0f113f3e 119 ret->meth = DH_get_default_method();
0b13e9f0 120#ifndef OPENSSL_NO_ENGINE
2bbf0baa 121 ret->flags = ret->meth->flags; /* early default init */
0f113f3e
MC
122 if (engine) {
123 if (!ENGINE_init(engine)) {
124 DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB);
2bbf0baa 125 goto err;
0f113f3e
MC
126 }
127 ret->engine = engine;
128 } else
129 ret->engine = ENGINE_get_default_DH();
130 if (ret->engine) {
131 ret->meth = ENGINE_get_DH(ret->engine);
7c96dbcd 132 if (ret->meth == NULL) {
0f113f3e 133 DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB);
2bbf0baa 134 goto err;
0f113f3e
MC
135 }
136 }
0b13e9f0 137#endif
cb78486d 138
0f113f3e 139 ret->flags = ret->meth->flags;
d188a536 140
2bbf0baa
F
141 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data))
142 goto err;
d188a536
AG
143
144 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
2bbf0baa
F
145 DHerr(DH_F_DH_NEW_METHOD, ERR_R_INIT_FAIL);
146err:
d188a536 147 DH_free(ret);
0f113f3e
MC
148 ret = NULL;
149 }
d188a536
AG
150
151 return ret;
0f113f3e 152}
d02b48c6 153
6b691a5c 154void DH_free(DH *r)
0f113f3e
MC
155{
156 int i;
d6407083 157
0f113f3e
MC
158 if (r == NULL)
159 return;
d188a536
AG
160
161 CRYPTO_atomic_add(&r->references, -1, &i, r->lock);
f3f1cf84 162 REF_PRINT_COUNT("DH", r);
0f113f3e
MC
163 if (i > 0)
164 return;
f3f1cf84 165 REF_ASSERT_ISNT(i < 0);
13066cee 166
0f113f3e
MC
167 if (r->meth->finish)
168 r->meth->finish(r);
0b13e9f0 169#ifndef OPENSSL_NO_ENGINE
7c96dbcd 170 ENGINE_finish(r->engine);
0b13e9f0 171#endif
ac8b4ee0 172
0f113f3e 173 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
d50f1bdf 174
d188a536
AG
175 CRYPTO_THREAD_lock_free(r->lock);
176
23a1d5e9
RS
177 BN_clear_free(r->p);
178 BN_clear_free(r->g);
179 BN_clear_free(r->q);
180 BN_clear_free(r->j);
b548a1f1 181 OPENSSL_free(r->seed);
23a1d5e9
RS
182 BN_clear_free(r->counter);
183 BN_clear_free(r->pub_key);
184 BN_clear_free(r->priv_key);
0f113f3e
MC
185 OPENSSL_free(r);
186}
d02b48c6 187
dc2a33d6 188int DH_up_ref(DH *r)
0f113f3e 189{
d188a536
AG
190 int i;
191
192 if (CRYPTO_atomic_add(&r->references, 1, &i, r->lock) <= 0)
193 return 0;
f3f1cf84
RS
194
195 REF_PRINT_COUNT("DH", r);
196 REF_ASSERT_ISNT(i < 2);
0f113f3e
MC
197 return ((i > 1) ? 1 : 0);
198}
5cbc2e8b 199
dd9d233e 200int DH_set_ex_data(DH *d, int idx, void *arg)
0f113f3e
MC
201{
202 return (CRYPTO_set_ex_data(&d->ex_data, idx, arg));
203}
13066cee 204
dd9d233e 205void *DH_get_ex_data(DH *d, int idx)
0f113f3e
MC
206{
207 return (CRYPTO_get_ex_data(&d->ex_data, idx));
208}
13066cee 209
26c79d56
KR
210int DH_bits(const DH *dh)
211{
212 return BN_num_bits(dh->p);
213}
214
f971ccb2 215int DH_size(const DH *dh)
0f113f3e
MC
216{
217 return (BN_num_bytes(dh->p));
218}
2514fa79
DSH
219
220int DH_security_bits(const DH *dh)
0f113f3e
MC
221{
222 int N;
223 if (dh->q)
224 N = BN_num_bits(dh->q);
225 else if (dh->length)
226 N = dh->length;
227 else
228 N = -1;
229 return BN_security_bits(BN_num_bits(dh->p), N);
230}
0aeddcfa
MC
231
232
233void DH_get0_pqg(const DH *dh, BIGNUM **p, BIGNUM **q, BIGNUM **g)
234{
235 if (p != NULL)
236 *p = dh->p;
237 if (q != NULL)
238 *q = dh->q;
239 if (g != NULL)
240 *g = dh->g;
241}
242
243int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
244{
1da12e34
RL
245 /* If the fields p and g in d are NULL, the corresponding input
246 * parameters MUST be non-NULL. q may remain NULL.
247 *
248 * It is an error to give the results from get0 on d
249 * as input parameters.
250 */
251 if (p == dh->p || (dh->q != NULL && q == dh->q) || g == dh->g)
0aeddcfa 252 return 0;
1da12e34
RL
253
254 if (p != NULL) {
255 BN_free(dh->p);
256 dh->p = p;
257 }
258 if (q != NULL) {
259 BN_free(dh->q);
260 dh->q = q;
261 }
262 if (g != NULL) {
263 BN_free(dh->g);
264 dh->g = g;
265 }
0aeddcfa
MC
266
267 if (q != NULL) {
268 dh->length = BN_num_bits(q);
269 }
270
271 return 1;
272}
273
274long DH_get_length(const DH *dh)
275{
276 return dh->length;
277}
278
279int DH_set_length(DH *dh, long length)
280{
281 dh->length = length;
282 return 1;
283}
284
285void DH_get0_key(const DH *dh, BIGNUM **pub_key, BIGNUM **priv_key)
286{
287 if (pub_key != NULL)
288 *pub_key = dh->pub_key;
289 if (priv_key != NULL)
290 *priv_key = dh->priv_key;
291}
292
293int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
294{
1da12e34
RL
295 /* If the pub_key in dh is NULL, the corresponding input
296 * parameters MUST be non-NULL. The priv_key field may
297 * be left NULL.
298 *
299 * It is an error to give the results from get0 on dh
300 * as input parameters.
301 */
302 if (dh->pub_key == pub_key
4a397f51 303 || (dh->priv_key != NULL && priv_key == dh->priv_key))
0aeddcfa
MC
304 return 0;
305
1da12e34
RL
306 if (pub_key != NULL) {
307 BN_free(dh->pub_key);
308 dh->pub_key = pub_key;
309 }
310 if (priv_key != NULL) {
311 BN_free(dh->priv_key);
312 dh->priv_key = priv_key;
313 }
0aeddcfa
MC
314
315 return 1;
316}
317
318void DH_clear_flags(DH *dh, int flags)
319{
320 dh->flags &= ~flags;
321}
322
323int DH_test_flags(const DH *dh, int flags)
324{
325 return dh->flags & flags;
326}
327
328void DH_set_flags(DH *dh, int flags)
329{
330 dh->flags |= flags;
331}
332
333ENGINE *DH_get0_engine(DH *dh)
334{
335 return dh->engine;
336}