d537fe7de6b975bac1e076315e6b733d5cc80002
[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 <string.h>
65 #include "ec_lcl.h"
66 #include <openssl/err.h>
67 #include <string.h>
68
69 EC_KEY *EC_KEY_new(void)
70         {
71         EC_KEY *ret;
72
73         ret=(EC_KEY *)OPENSSL_malloc(sizeof(EC_KEY));
74         if (ret == NULL)
75                 {
76                 ECerr(EC_F_EC_NEW, ERR_R_MALLOC_FAILURE);
77                 return(NULL);
78                 }
79
80         ret->version = 1;       
81         ret->group   = NULL;
82         ret->pub_key = NULL;
83         ret->priv_key= NULL;
84         ret->enc_flag= 0; 
85         ret->conv_form = POINT_CONVERSION_UNCOMPRESSED;
86         ret->references= 1;
87         ret->meth_data = NULL;
88         return(ret);
89         }
90
91
92 void EC_KEY_free(EC_KEY *r)
93         {
94         int i;
95
96         if (r == NULL) return;
97
98         i=CRYPTO_add(&r->references,-1,CRYPTO_LOCK_EC);
99 #ifdef REF_PRINT
100         REF_PRINT("EC_KEY",r);
101 #endif
102         if (i > 0) return;
103 #ifdef REF_CHECK
104         if (i < 0)
105                 {
106                 fprintf(stderr,"EC_KEY_free, bad reference count\n");
107                 abort();
108                 }
109 #endif
110
111         if (r->group    != NULL) 
112                 EC_GROUP_free(r->group);
113         if (r->pub_key  != NULL)
114                 EC_POINT_free(r->pub_key);
115         if (r->priv_key != NULL)
116                 BN_clear_free(r->priv_key);
117
118         if (r->meth_data && r->meth_data->finish)
119                 r->meth_data->finish(r);
120
121         memset((void *)r, 0x0, sizeof(EC_KEY));
122
123         OPENSSL_free(r);
124         }
125
126 EC_KEY *EC_KEY_copy(EC_KEY *dest, const EC_KEY *src)
127         {
128         if (dest == NULL || src == NULL)
129                 {
130                 ECerr(EC_F_EC_KEY_COPY, ERR_R_PASSED_NULL_PARAMETER);
131                 return NULL;
132                 }
133         /* copy the parameters */
134         if (src->group)
135                 {
136                 const EC_METHOD *meth = EC_GROUP_method_of(src->group);
137                 /* clear the old group */
138                 if (dest->group)
139                         EC_GROUP_free(dest->group);
140                 dest->group = EC_GROUP_new(meth);
141                 if (dest->group == NULL)
142                         return NULL;
143                 if (!EC_GROUP_copy(dest->group, src->group))
144                         return NULL;
145                 }
146         /*  copy the public key */
147         if (src->pub_key && src->group)
148                 {
149                 if (dest->pub_key)
150                         EC_POINT_free(dest->pub_key);
151                 dest->pub_key = EC_POINT_new(src->group);
152                 if (dest->pub_key == NULL)
153                         return NULL;
154                 if (!EC_POINT_copy(dest->pub_key, src->pub_key))
155                         return NULL;
156                 }
157         /* copy the private key */
158         if (src->priv_key)
159                 {
160                 if (dest->priv_key == NULL)
161                         {
162                         dest->priv_key = BN_new();
163                         if (dest->priv_key == NULL)
164                                 return NULL;
165                         }
166                 if (!BN_copy(dest->priv_key, src->priv_key))
167                         return NULL;
168                 }
169         /* copy the rest */
170         dest->enc_flag  = src->enc_flag;
171         dest->conv_form = src->conv_form;
172         dest->version   = src->version;
173
174         return dest;
175         }
176
177 EC_KEY *EC_KEY_dup(const EC_KEY *eckey)
178         {
179         EC_KEY *ret = NULL;
180         int     ok = 1;
181
182         ret = EC_KEY_new();
183         if (ret == NULL)
184                 return NULL;
185         /* copy the parameters */
186         if (eckey->group)
187                 {
188                 ret->group = EC_GROUP_dup(eckey->group);
189                 if (ret->group == NULL)
190                         ok = 0;
191                 }
192         /*  copy the public key */
193         if (eckey->pub_key && eckey->group)
194                 {
195                 ret->pub_key = EC_POINT_dup(eckey->pub_key, eckey->group);
196                 if (ret->pub_key == NULL)
197                         ok = 0;
198                 }
199         /* copy the private key */
200         if (eckey->priv_key)
201                 {
202                 ret->priv_key = BN_dup(ret->priv_key);
203                 if (ret->priv_key == NULL)
204                         ok = 0;
205                 }
206         /* copy the rest */
207         ret->enc_flag  = eckey->enc_flag;
208         ret->conv_form = eckey->conv_form;
209         ret->version   = eckey->version;
210
211         if (!ok)
212                 {
213                 EC_KEY_free(ret);
214                 ret = NULL;
215                 }
216
217         return ret;
218         }
219
220 int EC_KEY_up_ref(EC_KEY *r)
221         {
222         int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_EC);
223 #ifdef REF_PRINT
224         REF_PRINT("EC_KEY",r);
225 #endif
226 #ifdef REF_CHECK
227         if (i < 2)
228                 {
229                 fprintf(stderr, "EC_KEY_up, bad reference count\n");
230                 abort();
231                 }
232 #endif
233         return ((i > 1) ? 1 : 0);
234         }
235
236 int EC_KEY_generate_key(EC_KEY *eckey)
237         {       
238         int     ok = 0;
239         BN_CTX  *ctx = NULL;
240         BIGNUM  *priv_key = NULL, *order = NULL;
241         EC_POINT *pub_key = NULL;
242
243         if (!eckey || !eckey->group)
244                 {
245                 ECerr(EC_F_EC_KEY_GENERATE_KEY, ERR_R_PASSED_NULL_PARAMETER);
246                 return 0;
247                 }
248
249         if ((order = BN_new()) == NULL) goto err;
250         if ((ctx = BN_CTX_new()) == NULL) goto err;
251
252         if (eckey->priv_key == NULL)
253                 {
254                 priv_key = BN_new();
255                 if (priv_key == NULL)
256                         goto err;
257                 }
258         else
259                 priv_key = eckey->priv_key;
260
261         if (!EC_GROUP_get_order(eckey->group, order, ctx))
262                 goto err;
263
264         do
265                 if (!BN_rand_range(priv_key, order))
266                         goto err;
267         while (BN_is_zero(priv_key));
268
269         if (eckey->pub_key == NULL)
270                 {
271                 pub_key = EC_POINT_new(eckey->group);
272                 if (pub_key == NULL)
273                         goto err;
274                 }
275         else
276                 pub_key = eckey->pub_key;
277
278         if (!EC_POINT_mul(eckey->group, pub_key, priv_key, NULL, NULL, ctx))
279                 goto err;
280
281         eckey->priv_key = priv_key;
282         eckey->pub_key  = pub_key;
283
284         ok=1;
285
286 err:    
287         if (order)
288                 BN_free(order);
289         if (pub_key  != NULL && eckey->pub_key  == NULL)
290                 EC_POINT_free(pub_key);
291         if (priv_key != NULL && eckey->priv_key == NULL)
292                 BN_free(priv_key);
293         if (ctx != NULL)
294                 BN_CTX_free(ctx);
295         return(ok);
296         }
297
298 int EC_KEY_check_key(const EC_KEY *eckey)
299         {
300         int     ok   = 0;
301         BN_CTX  *ctx = NULL;
302         BIGNUM  *order  = NULL;
303         EC_POINT *point = NULL;
304
305         if (!eckey || !eckey->group || !eckey->pub_key)
306                 {
307                 ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
308                 return 0;
309                 }
310         
311         if ((ctx = BN_CTX_new()) == NULL)
312                 goto err;
313         if ((order = BN_new()) == NULL)
314                 goto err;
315         if ((point = EC_POINT_new(eckey->group)) == NULL)
316                 goto err;
317
318         /* testing whether the pub_key is on the elliptic curve */
319         if (!EC_POINT_is_on_curve(eckey->group, eckey->pub_key, ctx))
320                 {
321                 ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_POINT_IS_NOT_ON_CURVE);
322                 goto err;
323                 }
324         /* testing whether pub_key * order is the point at infinity */
325         if (!EC_GROUP_get_order(eckey->group, order, ctx))
326                 {
327                 ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_INVALID_GROUP_ORDER);
328                 goto err;
329                 }
330         if (!EC_POINT_copy(point, eckey->pub_key))
331                 {
332                 ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_EC_LIB);
333                 goto err;
334                 }
335         if (!EC_POINT_mul(eckey->group, point, order, NULL, NULL, ctx))
336                 {
337                 ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_EC_LIB);
338                 goto err;
339                 }
340         if (!EC_POINT_is_at_infinity(eckey->group, point))
341                 {
342                 ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_WRONG_ORDER);
343                 goto err;
344                 }
345         /* in case the priv_key is present : 
346          * check if generator * priv_key == pub_key 
347          */
348         if (eckey->priv_key)
349                 {
350                 if (BN_cmp(eckey->priv_key, order) >= 0)
351                         {
352                         ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_WRONG_ORDER);
353                         goto err;
354                         }
355                 if (!EC_POINT_mul(eckey->group, point, eckey->priv_key,
356                         NULL, NULL, ctx))
357                         {
358                         ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_EC_LIB);
359                         goto err;
360                         }
361                 if (EC_POINT_cmp(eckey->group, point, eckey->pub_key, 
362                         ctx) != 0)
363                         {
364                         ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_INVALID_PRIVATE_KEY);
365                         goto err;
366                         }
367                 }
368         ok = 1;
369 err:
370         if (ctx   != NULL)
371                 BN_CTX_free(ctx);
372         if (order != NULL)
373                 BN_free(order);
374         if (point != NULL)
375                 EC_POINT_free(point);
376         return(ok);
377         }