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