650ca43da0b3963271d10af837c6180c44c7f41e
[openssl.git] / crypto / dh / dh_lib.c
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
7  *
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).
14  *
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.
21  *
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 :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
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.
51  *
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>
59 #include "internal/cryptlib.h"
60 #include <openssl/bn.h>
61 #include "dh_locl.h"
62 #include <openssl/engine.h>
63
64 static const DH_METHOD *default_DH_method = NULL;
65
66 void DH_set_default_method(const DH_METHOD *meth)
67 {
68     default_DH_method = meth;
69 }
70
71 const DH_METHOD *DH_get_default_method(void)
72 {
73     if (!default_DH_method)
74         default_DH_method = DH_OpenSSL();
75     return default_DH_method;
76 }
77
78 int DH_set_method(DH *dh, const DH_METHOD *meth)
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);
88 #ifndef OPENSSL_NO_ENGINE
89     ENGINE_finish(dh->engine);
90     dh->engine = NULL;
91 #endif
92     dh->meth = meth;
93     if (meth->init)
94         meth->init(dh);
95     return 1;
96 }
97
98 DH *DH_new(void)
99 {
100     return DH_new_method(NULL);
101 }
102
103 DH *DH_new_method(ENGINE *engine)
104 {
105     DH *ret = OPENSSL_zalloc(sizeof(*ret));
106
107     if (ret == NULL) {
108         DHerr(DH_F_DH_NEW_METHOD, ERR_R_MALLOC_FAILURE);
109         return NULL;
110     }
111
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
119     ret->meth = DH_get_default_method();
120 #ifndef OPENSSL_NO_ENGINE
121     ret->flags = ret->meth->flags;  /* early default init */
122     if (engine) {
123         if (!ENGINE_init(engine)) {
124             DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB);
125             goto err;
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);
132         if (ret->meth == NULL) {
133             DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB);
134             goto err;
135         }
136     }
137 #endif
138
139     ret->flags = ret->meth->flags;
140
141     if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data))
142         goto err;
143
144     if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
145         DHerr(DH_F_DH_NEW_METHOD, ERR_R_INIT_FAIL);
146 err:
147         DH_free(ret);
148         ret = NULL;
149     }
150
151     return ret;
152 }
153
154 void DH_free(DH *r)
155 {
156     int i;
157
158     if (r == NULL)
159         return;
160
161     CRYPTO_atomic_add(&r->references, -1, &i, r->lock);
162     REF_PRINT_COUNT("DH", r);
163     if (i > 0)
164         return;
165     REF_ASSERT_ISNT(i < 0);
166
167     if (r->meth->finish)
168         r->meth->finish(r);
169 #ifndef OPENSSL_NO_ENGINE
170     ENGINE_finish(r->engine);
171 #endif
172
173     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
174
175     CRYPTO_THREAD_lock_free(r->lock);
176
177     BN_clear_free(r->p);
178     BN_clear_free(r->g);
179     BN_clear_free(r->q);
180     BN_clear_free(r->j);
181     OPENSSL_free(r->seed);
182     BN_clear_free(r->counter);
183     BN_clear_free(r->pub_key);
184     BN_clear_free(r->priv_key);
185     OPENSSL_free(r);
186 }
187
188 int DH_up_ref(DH *r)
189 {
190     int i;
191
192     if (CRYPTO_atomic_add(&r->references, 1, &i, r->lock) <= 0)
193         return 0;
194
195     REF_PRINT_COUNT("DH", r);
196     REF_ASSERT_ISNT(i < 2);
197     return ((i > 1) ? 1 : 0);
198 }
199
200 int DH_set_ex_data(DH *d, int idx, void *arg)
201 {
202     return (CRYPTO_set_ex_data(&d->ex_data, idx, arg));
203 }
204
205 void *DH_get_ex_data(DH *d, int idx)
206 {
207     return (CRYPTO_get_ex_data(&d->ex_data, idx));
208 }
209
210 int DH_bits(const DH *dh)
211 {
212     return BN_num_bits(dh->p);
213 }
214
215 int DH_size(const DH *dh)
216 {
217     return (BN_num_bytes(dh->p));
218 }
219
220 int DH_security_bits(const DH *dh)
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 }
231
232
233 void 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
243 int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
244 {
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)
252         return 0;
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     }
266
267     if (q != NULL) {
268         dh->length = BN_num_bits(q);
269     }
270
271     return 1;
272 }
273
274 long DH_get_length(const DH *dh)
275 {
276     return dh->length;
277 }
278
279 int DH_set_length(DH *dh, long length)
280 {
281     dh->length = length;
282     return 1;
283 }
284
285 void 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
293 int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
294 {
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
303         || (dh->priv_key != NULL && priv_key == dh->priv_key))
304         return 0;
305
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     }
314
315     return 1;
316 }
317
318 void DH_clear_flags(DH *dh, int flags)
319 {
320     dh->flags &= ~flags;
321 }
322
323 int DH_test_flags(const DH *dh, int flags)
324 {
325     return dh->flags & flags;
326 }
327
328 void DH_set_flags(DH *dh, int flags)
329 {
330     dh->flags |= flags;
331 }
332
333 ENGINE *DH_get0_engine(DH *dh)
334 {
335     return dh->engine;
336 }