Add ECDH support.
[openssl.git] / crypto / ec / ec_key.c
1 /* crypto/ec/ec_key.c */
2 /*
3  * Written by Nils Larsch for the OpenSSL project.
4  */
5 /* ====================================================================
6  * Copyright (c) 1998-2002 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer. 
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    openssl-core@openssl.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58 /* ====================================================================
59  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60  * Portions originally developed by SUN MICROSYSTEMS, INC., and 
61  * contributed to the OpenSSL project.
62  */
63
64 #include "ec_lcl.h"
65 #include <openssl/err.h>
66 #include <string.h>
67
68 EC_KEY *EC_KEY_new(void)
69         {
70         EC_KEY *ret;
71
72         ret=(EC_KEY *)OPENSSL_malloc(sizeof(EC_KEY));
73         if (ret == NULL)
74                 {
75                 ECerr(EC_F_EC_NEW, ERR_R_MALLOC_FAILURE);
76                 return(NULL);
77                 }
78
79         ret->version = 1;       
80         ret->group   = NULL;
81         ret->pub_key = NULL;
82         ret->priv_key= NULL;
83         ret->enc_flag= 0; 
84         ret->conv_form = POINT_CONVERSION_UNCOMPRESSED;
85         ret->references= 1;
86         ret->meth_data = NULL;
87         return(ret);
88         }
89
90
91 void EC_KEY_free(EC_KEY *r)
92         {
93         int i;
94
95         if (r == NULL) return;
96
97         i=CRYPTO_add(&r->references,-1,CRYPTO_LOCK_EC);
98 #ifdef REF_PRINT
99         REF_PRINT("EC_KEY",r);
100 #endif
101         if (i > 0) return;
102 #ifdef REF_CHECK
103         if (i < 0)
104                 {
105                 fprintf(stderr,"EC_KEY_free, bad reference count\n");
106                 abort();
107                 }
108 #endif
109
110         if (r->group    != NULL) 
111                 EC_GROUP_free(r->group);
112         if (r->pub_key  != NULL)
113                 EC_POINT_free(r->pub_key);
114         if (r->priv_key != NULL)
115                 BN_clear_free(r->priv_key);
116
117         if (r->meth_data && r->meth_data->finish)
118                 r->meth_data->finish(r);
119
120         memset((void *)r, 0x0, sizeof(EC_KEY));
121
122         OPENSSL_free(r);
123         }
124
125 EC_KEY *EC_KEY_copy(EC_KEY *dest, const EC_KEY *src)
126         {
127         if (dest == NULL || src == NULL)
128                 {
129                 ECerr(EC_F_EC_KEY_COPY, ERR_R_PASSED_NULL_PARAMETER);
130                 return NULL;
131                 }
132         /* copy the parameters */
133         if (src->group)
134                 {
135                 const EC_METHOD *meth = EC_GROUP_method_of(src->group);
136                 /* clear the old group */
137                 if (dest->group)
138                         EC_GROUP_free(dest->group);
139                 dest->group = EC_GROUP_new(meth);
140                 if (dest->group == NULL)
141                         return NULL;
142                 if (!EC_GROUP_copy(dest->group, src->group))
143                         return NULL;
144                 }
145         /*  copy the public key */
146         if (src->pub_key && src->group)
147                 {
148                 if (dest->pub_key)
149                         EC_POINT_free(dest->pub_key);
150                 dest->pub_key = EC_POINT_new(src->group);
151                 if (dest->pub_key == NULL)
152                         return NULL;
153                 if (!EC_POINT_copy(dest->pub_key, src->pub_key))
154                         return NULL;
155                 }
156         /* copy the private key */
157         if (src->priv_key)
158                 {
159                 if (dest->priv_key == NULL)
160                         {
161                         dest->priv_key = BN_new();
162                         if (dest->priv_key == NULL)
163                                 return NULL;
164                         }
165                 if (!BN_copy(dest->priv_key, src->priv_key))
166                         return NULL;
167                 }
168         /* copy the rest */
169         dest->enc_flag  = src->enc_flag;
170         dest->conv_form = src->conv_form;
171         dest->version   = src->version;
172
173         return dest;
174         }
175
176 EC_KEY *EC_KEY_dup(const EC_KEY *eckey)
177         {
178         EC_KEY *ret = NULL;
179         int     ok = 1;
180
181         ret = EC_KEY_new();
182         if (ret == NULL)
183                 return NULL;
184         /* copy the parameters */
185         if (eckey->group)
186                 {
187                 ret->group = EC_GROUP_dup(eckey->group);
188                 if (ret->group == NULL)
189                         ok = 0;
190                 }
191         /*  copy the public key */
192         if (eckey->pub_key && eckey->group)
193                 {
194                 ret->pub_key = EC_POINT_dup(eckey->pub_key, eckey->group);
195                 if (ret->pub_key == NULL)
196                         ok = 0;
197                 }
198         /* copy the private key */
199         if (eckey->priv_key)
200                 {
201                 ret->priv_key = BN_dup(ret->priv_key);
202                 if (ret->priv_key == NULL)
203                         ok = 0;
204                 }
205         /* copy the rest */
206         ret->enc_flag  = eckey->enc_flag;
207         ret->conv_form = eckey->conv_form;
208         ret->version   = eckey->version;
209
210         if (!ok)
211                 {
212                 EC_KEY_free(ret);
213                 ret = NULL;
214                 }
215
216         return ret;
217         }
218
219 int EC_KEY_up_ref(EC_KEY *r)
220         {
221         int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_EC);
222 #ifdef REF_PRINT
223         REF_PRINT("EC_KEY",r);
224 #endif
225 #ifdef REF_CHECK
226         if (i < 2)
227                 {
228                 fprintf(stderr, "EC_KEY_up, bad reference count\n");
229                 abort();
230                 }
231 #endif
232         return ((i > 1) ? 1 : 0);
233         }
234
235 int EC_KEY_generate_key(EC_KEY *eckey)
236         {       
237         int     ok = 0;
238         BN_CTX  *ctx = NULL;
239         BIGNUM  *priv_key = NULL, *order = NULL;
240         EC_POINT *pub_key = NULL;
241
242         if (!eckey || !eckey->group)
243                 {
244                 ECerr(EC_F_EC_KEY_GENERATE_KEY, ERR_R_PASSED_NULL_PARAMETER);
245                 return 0;
246                 }
247
248         if ((order = BN_new()) == NULL) goto err;
249         if ((ctx = BN_CTX_new()) == NULL) goto err;
250
251         if (eckey->priv_key == NULL)
252                 {
253                 priv_key = BN_new();
254                 if (priv_key == NULL)
255                         goto err;
256                 }
257         else
258                 priv_key = eckey->priv_key;
259
260         if (!EC_GROUP_get_order(eckey->group, order, ctx))
261                 goto err;
262
263         do
264                 if (!BN_rand_range(priv_key, order))
265                         goto err;
266         while (BN_is_zero(priv_key));
267
268         if (eckey->pub_key == NULL)
269                 {
270                 pub_key = EC_POINT_new(eckey->group);
271                 if (pub_key == NULL)
272                         goto err;
273                 }
274         else
275                 pub_key = eckey->pub_key;
276
277         if (!EC_POINT_mul(eckey->group, pub_key, priv_key, NULL, NULL, ctx))
278                 goto err;
279
280         eckey->priv_key = priv_key;
281         eckey->pub_key  = pub_key;
282
283         ok=1;
284
285 err:    
286         if (order)
287                 BN_free(order);
288         if (pub_key  != NULL && eckey->pub_key  == NULL)
289                 EC_POINT_free(pub_key);
290         if (priv_key != NULL && eckey->priv_key == NULL)
291                 BN_free(priv_key);
292         if (ctx != NULL)
293                 BN_CTX_free(ctx);
294         return(ok);
295         }
296
297 int EC_KEY_check_key(const EC_KEY *eckey)
298         {
299         int     ok   = 0;
300         BN_CTX  *ctx = NULL;
301         BIGNUM  *order  = NULL;
302         EC_POINT *point = NULL;
303
304         if (!eckey || !eckey->group || !eckey->pub_key)
305                 {
306                 ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
307                 return 0;
308                 }
309         
310         if ((ctx = BN_CTX_new()) == NULL)
311                 goto err;
312         if ((order = BN_new()) == NULL)
313                 goto err;
314         if ((point = EC_POINT_new(eckey->group)) == NULL)
315                 goto err;
316
317         /* testing whether the pub_key is on the elliptic curve */
318         if (!EC_POINT_is_on_curve(eckey->group, eckey->pub_key, ctx))
319                 {
320                 ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_POINT_IS_NOT_ON_CURVE);
321                 goto err;
322                 }
323         /* testing whether pub_key * order is the point at infinity */
324         if (!EC_GROUP_get_order(eckey->group, order, ctx))
325                 {
326                 ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_INVALID_GROUP_ORDER);
327                 goto err;
328                 }
329         if (!EC_POINT_copy(point, eckey->pub_key))
330                 {
331                 ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_EC_LIB);
332                 goto err;
333                 }
334         if (!EC_POINT_mul(eckey->group, point, order, NULL, NULL, ctx))
335                 {
336                 ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_EC_LIB);
337                 goto err;
338                 }
339         if (!EC_POINT_is_at_infinity(eckey->group, point))
340                 {
341                 ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_WRONG_ORDER);
342                 goto err;
343                 }
344         /* in case the priv_key is present : 
345          * check if generator * priv_key == pub_key 
346          */
347         if (eckey->priv_key)
348                 {
349                 if (BN_cmp(eckey->priv_key, order) >= 0)
350                         {
351                         ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_WRONG_ORDER);
352                         goto err;
353                         }
354                 if (!EC_POINT_mul(eckey->group, point, eckey->priv_key,
355                         NULL, NULL, ctx))
356                         {
357                         ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_EC_LIB);
358                         goto err;
359                         }
360                 if (EC_POINT_cmp(eckey->group, point, eckey->pub_key, 
361                         ctx) != 0)
362                         {
363                         ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_INVALID_PRIVATE_KEY);
364                         goto err;
365                         }
366                 }
367         ok = 1;
368 err:
369         if (ctx   != NULL)
370                 BN_CTX_free(ctx);
371         if (order != NULL)
372                 BN_free(order);
373         if (point != NULL)
374                 EC_POINT_free(point);
375         return(ok);
376         }