edb4bb31fb74c817d97beae2b454e98e08c24e1a
[oweals/openssl.git] / crypto / dh / dh_lib.c
1 /* crypto/dh/dh_lib.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  * 
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  * 
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  * 
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from 
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  * 
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * 
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58
59 #include <stdio.h>
60 #include "cryptlib.h"
61 #include <openssl/bn.h>
62 #include <openssl/dh.h>
63 #ifndef OPENSSL_NO_ENGINE
64 #include <openssl/engine.h>
65 #endif
66
67 #ifdef OPENSSL_FIPS
68 #include <openssl/fips.h>
69 #endif
70
71 const char DH_version[]="Diffie-Hellman" OPENSSL_VERSION_PTEXT;
72
73 static const DH_METHOD *default_DH_method = NULL;
74
75 void DH_set_default_method(const DH_METHOD *meth)
76         {
77         default_DH_method = meth;
78         }
79
80 const DH_METHOD *DH_get_default_method(void)
81         {
82         if(!default_DH_method)
83                 {
84 #ifdef OPENSSL_FIPS
85                 if (FIPS_mode())
86                         default_DH_method = FIPS_dh_openssl();
87                 else
88 #endif
89                         default_DH_method = DH_OpenSSL();
90                 }
91         return default_DH_method;
92         }
93
94 int DH_set_method(DH *dh, const DH_METHOD *meth)
95         {
96         /* NB: The caller is specifically setting a method, so it's not up to us
97          * to deal with which ENGINE it comes from. */
98         const DH_METHOD *mtmp;
99         mtmp = dh->meth;
100         if (mtmp->finish) mtmp->finish(dh);
101 #ifndef OPENSSL_NO_ENGINE
102         if (dh->engine)
103                 {
104                 ENGINE_finish(dh->engine);
105                 dh->engine = NULL;
106                 }
107 #endif
108         dh->meth = meth;
109         if (meth->init) meth->init(dh);
110         return 1;
111         }
112
113 DH *DH_new(void)
114         {
115         return DH_new_method(NULL);
116         }
117
118 DH *DH_new_method(ENGINE *engine)
119         {
120         DH *ret;
121
122         ret=(DH *)OPENSSL_malloc(sizeof(DH));
123         if (ret == NULL)
124                 {
125                 DHerr(DH_F_DH_NEW_METHOD,ERR_R_MALLOC_FAILURE);
126                 return(NULL);
127                 }
128
129         ret->meth = DH_get_default_method();
130 #ifndef OPENSSL_NO_ENGINE
131         if (engine)
132                 {
133                 if (!ENGINE_init(engine))
134                         {
135                         DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB);
136                         OPENSSL_free(ret);
137                         return NULL;
138                         }
139                 ret->engine = engine;
140                 }
141         else
142                 ret->engine = ENGINE_get_default_DH();
143         if(ret->engine)
144                 {
145                 ret->meth = ENGINE_get_DH(ret->engine);
146                 if(!ret->meth)
147                         {
148                         DHerr(DH_F_DH_NEW_METHOD,ERR_R_ENGINE_LIB);
149                         ENGINE_finish(ret->engine);
150                         OPENSSL_free(ret);
151                         return NULL;
152                         }
153                 }
154 #endif
155
156         ret->pad=0;
157         ret->version=0;
158         ret->p=NULL;
159         ret->g=NULL;
160         ret->length=0;
161         ret->pub_key=NULL;
162         ret->priv_key=NULL;
163         ret->q=NULL;
164         ret->j=NULL;
165         ret->seed = NULL;
166         ret->seedlen = 0;
167         ret->counter = NULL;
168         ret->method_mont_p=NULL;
169         ret->references = 1;
170         ret->flags=ret->meth->flags & ~DH_FLAG_NON_FIPS_ALLOW;
171         CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data);
172         if ((ret->meth->init != NULL) && !ret->meth->init(ret))
173                 {
174 #ifndef OPENSSL_NO_ENGINE
175                 if (ret->engine)
176                         ENGINE_finish(ret->engine);
177 #endif
178                 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data);
179                 OPENSSL_free(ret);
180                 ret=NULL;
181                 }
182         return(ret);
183         }
184
185 void DH_free(DH *r)
186         {
187         int i;
188         if(r == NULL) return;
189         i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_DH);
190 #ifdef REF_PRINT
191         REF_PRINT("DH",r);
192 #endif
193         if (i > 0) return;
194 #ifdef REF_CHECK
195         if (i < 0)
196                 {
197                 fprintf(stderr,"DH_free, bad reference count\n");
198                 abort();
199         }
200 #endif
201
202         if (r->meth->finish)
203                 r->meth->finish(r);
204 #ifndef OPENSSL_NO_ENGINE
205         if (r->engine)
206                 ENGINE_finish(r->engine);
207 #endif
208
209         CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
210
211         if (r->p != NULL) BN_clear_free(r->p);
212         if (r->g != NULL) BN_clear_free(r->g);
213         if (r->q != NULL) BN_clear_free(r->q);
214         if (r->j != NULL) BN_clear_free(r->j);
215         if (r->seed) OPENSSL_free(r->seed);
216         if (r->counter != NULL) BN_clear_free(r->counter);
217         if (r->pub_key != NULL) BN_clear_free(r->pub_key);
218         if (r->priv_key != NULL) BN_clear_free(r->priv_key);
219         OPENSSL_free(r);
220         }
221
222 int DH_up_ref(DH *r)
223         {
224         int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_DH);
225 #ifdef REF_PRINT
226         REF_PRINT("DH",r);
227 #endif
228 #ifdef REF_CHECK
229         if (i < 2)
230                 {
231                 fprintf(stderr, "DH_up, bad reference count\n");
232                 abort();
233                 }
234 #endif
235         return ((i > 1) ? 1 : 0);
236         }
237
238 int DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
239              CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
240         {
241         return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DH, argl, argp,
242                                 new_func, dup_func, free_func);
243         }
244
245 int DH_set_ex_data(DH *d, int idx, void *arg)
246         {
247         return(CRYPTO_set_ex_data(&d->ex_data,idx,arg));
248         }
249
250 void *DH_get_ex_data(DH *d, int idx)
251         {
252         return(CRYPTO_get_ex_data(&d->ex_data,idx));
253         }
254
255 int DH_size(const DH *dh)
256         {
257         return(BN_num_bytes(dh->p));
258         }