Run util/openssl-format-source -v -c .
[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-2005 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
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         ECerr(EC_F_EC_KEY_NEW, ERR_R_MALLOC_FAILURE);
75         return (NULL);
76     }
77
78     ret->version = 1;
79     ret->flags = 0;
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->method_data = NULL;
87     return (ret);
88 }
89
90 EC_KEY *EC_KEY_new_by_curve_name(int nid)
91 {
92     EC_KEY *ret = EC_KEY_new();
93     if (ret == NULL)
94         return NULL;
95     ret->group = EC_GROUP_new_by_curve_name(nid);
96     if (ret->group == NULL) {
97         EC_KEY_free(ret);
98         return NULL;
99     }
100     return ret;
101 }
102
103 void EC_KEY_free(EC_KEY *r)
104 {
105     int i;
106
107     if (r == NULL)
108         return;
109
110     i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_EC);
111 #ifdef REF_PRINT
112     REF_PRINT("EC_KEY", r);
113 #endif
114     if (i > 0)
115         return;
116 #ifdef REF_CHECK
117     if (i < 0) {
118         fprintf(stderr, "EC_KEY_free, bad reference count\n");
119         abort();
120     }
121 #endif
122
123     if (r->group != NULL)
124         EC_GROUP_free(r->group);
125     if (r->pub_key != NULL)
126         EC_POINT_free(r->pub_key);
127     if (r->priv_key != NULL)
128         BN_clear_free(r->priv_key);
129
130     EC_EX_DATA_free_all_data(&r->method_data);
131
132     OPENSSL_cleanse((void *)r, sizeof(EC_KEY));
133
134     OPENSSL_free(r);
135 }
136
137 EC_KEY *EC_KEY_copy(EC_KEY *dest, const EC_KEY *src)
138 {
139     EC_EXTRA_DATA *d;
140
141     if (dest == NULL || src == NULL) {
142         ECerr(EC_F_EC_KEY_COPY, ERR_R_PASSED_NULL_PARAMETER);
143         return NULL;
144     }
145     /* copy the parameters */
146     if (src->group) {
147         const EC_METHOD *meth = EC_GROUP_method_of(src->group);
148         /* clear the old group */
149         if (dest->group)
150             EC_GROUP_free(dest->group);
151         dest->group = EC_GROUP_new(meth);
152         if (dest->group == NULL)
153             return NULL;
154         if (!EC_GROUP_copy(dest->group, src->group))
155             return NULL;
156     }
157     /*  copy the public key */
158     if (src->pub_key && src->group) {
159         if (dest->pub_key)
160             EC_POINT_free(dest->pub_key);
161         dest->pub_key = EC_POINT_new(src->group);
162         if (dest->pub_key == NULL)
163             return NULL;
164         if (!EC_POINT_copy(dest->pub_key, src->pub_key))
165             return NULL;
166     }
167     /* copy the private key */
168     if (src->priv_key) {
169         if (dest->priv_key == NULL) {
170             dest->priv_key = BN_new();
171             if (dest->priv_key == NULL)
172                 return NULL;
173         }
174         if (!BN_copy(dest->priv_key, src->priv_key))
175             return NULL;
176     }
177     /* copy method/extra data */
178     EC_EX_DATA_free_all_data(&dest->method_data);
179
180     for (d = src->method_data; d != NULL; d = d->next) {
181         void *t = d->dup_func(d->data);
182
183         if (t == NULL)
184             return 0;
185         if (!EC_EX_DATA_set_data
186             (&dest->method_data, t, d->dup_func, d->free_func,
187              d->clear_free_func))
188             return 0;
189     }
190
191     /* copy the rest */
192     dest->enc_flag = src->enc_flag;
193     dest->conv_form = src->conv_form;
194     dest->version = src->version;
195     dest->flags = src->flags;
196
197     return dest;
198 }
199
200 EC_KEY *EC_KEY_dup(const EC_KEY *ec_key)
201 {
202     EC_KEY *ret = EC_KEY_new();
203     if (ret == NULL)
204         return NULL;
205     if (EC_KEY_copy(ret, ec_key) == NULL) {
206         EC_KEY_free(ret);
207         return NULL;
208     }
209     return ret;
210 }
211
212 int EC_KEY_up_ref(EC_KEY *r)
213 {
214     int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_EC);
215 #ifdef REF_PRINT
216     REF_PRINT("EC_KEY", r);
217 #endif
218 #ifdef REF_CHECK
219     if (i < 2) {
220         fprintf(stderr, "EC_KEY_up, bad reference count\n");
221         abort();
222     }
223 #endif
224     return ((i > 1) ? 1 : 0);
225 }
226
227 int EC_KEY_generate_key(EC_KEY *eckey)
228 {
229     int ok = 0;
230     BN_CTX *ctx = NULL;
231     BIGNUM *priv_key = NULL, *order = NULL;
232     EC_POINT *pub_key = NULL;
233
234     if (!eckey || !eckey->group) {
235         ECerr(EC_F_EC_KEY_GENERATE_KEY, ERR_R_PASSED_NULL_PARAMETER);
236         return 0;
237     }
238
239     if ((order = BN_new()) == NULL)
240         goto err;
241     if ((ctx = BN_CTX_new()) == NULL)
242         goto err;
243
244     if (eckey->priv_key == NULL) {
245         priv_key = BN_new();
246         if (priv_key == NULL)
247             goto err;
248     } else
249         priv_key = eckey->priv_key;
250
251     if (!EC_GROUP_get_order(eckey->group, order, ctx))
252         goto err;
253
254     do
255         if (!BN_rand_range(priv_key, order))
256             goto err;
257     while (BN_is_zero(priv_key)) ;
258
259     if (eckey->pub_key == NULL) {
260         pub_key = EC_POINT_new(eckey->group);
261         if (pub_key == NULL)
262             goto err;
263     } else
264         pub_key = eckey->pub_key;
265
266     if (!EC_POINT_mul(eckey->group, pub_key, priv_key, NULL, NULL, ctx))
267         goto err;
268
269     eckey->priv_key = priv_key;
270     eckey->pub_key = pub_key;
271
272     ok = 1;
273
274  err:
275     if (order)
276         BN_free(order);
277     if (pub_key != NULL && eckey->pub_key == NULL)
278         EC_POINT_free(pub_key);
279     if (priv_key != NULL && eckey->priv_key == NULL)
280         BN_free(priv_key);
281     if (ctx != NULL)
282         BN_CTX_free(ctx);
283     return (ok);
284 }
285
286 int EC_KEY_check_key(const EC_KEY *eckey)
287 {
288     int ok = 0;
289     BN_CTX *ctx = NULL;
290     const BIGNUM *order = NULL;
291     EC_POINT *point = NULL;
292
293     if (!eckey || !eckey->group || !eckey->pub_key) {
294         ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
295         return 0;
296     }
297
298     if (EC_POINT_is_at_infinity(eckey->group, eckey->pub_key)) {
299         ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_POINT_AT_INFINITY);
300         goto err;
301     }
302
303     if ((ctx = BN_CTX_new()) == NULL)
304         goto err;
305     if ((point = EC_POINT_new(eckey->group)) == NULL)
306         goto err;
307
308     /* testing whether the pub_key is on the elliptic curve */
309     if (!EC_POINT_is_on_curve(eckey->group, eckey->pub_key, ctx)) {
310         ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_POINT_IS_NOT_ON_CURVE);
311         goto err;
312     }
313     /* testing whether pub_key * order is the point at infinity */
314     order = eckey->group->order;
315     if (BN_is_zero(order)) {
316         ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_INVALID_GROUP_ORDER);
317         goto err;
318     }
319     if (!EC_POINT_mul(eckey->group, point, NULL, eckey->pub_key, order, ctx)) {
320         ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_EC_LIB);
321         goto err;
322     }
323     if (!EC_POINT_is_at_infinity(eckey->group, point)) {
324         ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_WRONG_ORDER);
325         goto err;
326     }
327     /*
328      * in case the priv_key is present : check if generator * priv_key ==
329      * pub_key
330      */
331     if (eckey->priv_key) {
332         if (BN_cmp(eckey->priv_key, order) >= 0) {
333             ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_WRONG_ORDER);
334             goto err;
335         }
336         if (!EC_POINT_mul(eckey->group, point, eckey->priv_key,
337                           NULL, NULL, ctx)) {
338             ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_EC_LIB);
339             goto err;
340         }
341         if (EC_POINT_cmp(eckey->group, point, eckey->pub_key, ctx) != 0) {
342             ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_INVALID_PRIVATE_KEY);
343             goto err;
344         }
345     }
346     ok = 1;
347  err:
348     if (ctx != NULL)
349         BN_CTX_free(ctx);
350     if (point != NULL)
351         EC_POINT_free(point);
352     return (ok);
353 }
354
355 int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x,
356                                              BIGNUM *y)
357 {
358     BN_CTX *ctx = NULL;
359     BIGNUM *tx, *ty;
360     EC_POINT *point = NULL;
361     int ok = 0, tmp_nid, is_char_two = 0;
362
363     if (!key || !key->group || !x || !y) {
364         ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
365               ERR_R_PASSED_NULL_PARAMETER);
366         return 0;
367     }
368     ctx = BN_CTX_new();
369     if (!ctx)
370         goto err;
371
372     point = EC_POINT_new(key->group);
373
374     if (!point)
375         goto err;
376
377     tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(key->group));
378
379     if (tmp_nid == NID_X9_62_characteristic_two_field)
380         is_char_two = 1;
381
382     tx = BN_CTX_get(ctx);
383     ty = BN_CTX_get(ctx);
384 #ifndef OPENSSL_NO_EC2M
385     if (is_char_two) {
386         if (!EC_POINT_set_affine_coordinates_GF2m(key->group, point,
387                                                   x, y, ctx))
388             goto err;
389         if (!EC_POINT_get_affine_coordinates_GF2m(key->group, point,
390                                                   tx, ty, ctx))
391             goto err;
392     } else
393 #endif
394     {
395         if (!EC_POINT_set_affine_coordinates_GFp(key->group, point,
396                                                  x, y, ctx))
397             goto err;
398         if (!EC_POINT_get_affine_coordinates_GFp(key->group, point,
399                                                  tx, ty, ctx))
400             goto err;
401     }
402     /*
403      * Check if retrieved coordinates match originals and are less than field
404      * order: if not values are out of range.
405      */
406     if (BN_cmp(x, tx) || BN_cmp(y, ty)
407         || (BN_cmp(x, key->group->field) >= 0)
408         || (BN_cmp(y, key->group->field) >= 0)) {
409         ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
410               EC_R_COORDINATES_OUT_OF_RANGE);
411         goto err;
412     }
413
414     if (!EC_KEY_set_public_key(key, point))
415         goto err;
416
417     if (EC_KEY_check_key(key) == 0)
418         goto err;
419
420     ok = 1;
421
422  err:
423     if (ctx)
424         BN_CTX_free(ctx);
425     if (point)
426         EC_POINT_free(point);
427     return ok;
428
429 }
430
431 const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key)
432 {
433     return key->group;
434 }
435
436 int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group)
437 {
438     if (key->group != NULL)
439         EC_GROUP_free(key->group);
440     key->group = EC_GROUP_dup(group);
441     return (key->group == NULL) ? 0 : 1;
442 }
443
444 const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key)
445 {
446     return key->priv_key;
447 }
448
449 int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv_key)
450 {
451     if (key->priv_key)
452         BN_clear_free(key->priv_key);
453     key->priv_key = BN_dup(priv_key);
454     return (key->priv_key == NULL) ? 0 : 1;
455 }
456
457 const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key)
458 {
459     return key->pub_key;
460 }
461
462 int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub_key)
463 {
464     if (key->pub_key != NULL)
465         EC_POINT_free(key->pub_key);
466     key->pub_key = EC_POINT_dup(pub_key, key->group);
467     return (key->pub_key == NULL) ? 0 : 1;
468 }
469
470 unsigned int EC_KEY_get_enc_flags(const EC_KEY *key)
471 {
472     return key->enc_flag;
473 }
474
475 void EC_KEY_set_enc_flags(EC_KEY *key, unsigned int flags)
476 {
477     key->enc_flag = flags;
478 }
479
480 point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key)
481 {
482     return key->conv_form;
483 }
484
485 void EC_KEY_set_conv_form(EC_KEY *key, point_conversion_form_t cform)
486 {
487     key->conv_form = cform;
488     if (key->group != NULL)
489         EC_GROUP_set_point_conversion_form(key->group, cform);
490 }
491
492 void *EC_KEY_get_key_method_data(EC_KEY *key,
493                                  void *(*dup_func) (void *),
494                                  void (*free_func) (void *),
495                                  void (*clear_free_func) (void *))
496 {
497     void *ret;
498
499     CRYPTO_r_lock(CRYPTO_LOCK_EC);
500     ret =
501         EC_EX_DATA_get_data(key->method_data, dup_func, free_func,
502                             clear_free_func);
503     CRYPTO_r_unlock(CRYPTO_LOCK_EC);
504
505     return ret;
506 }
507
508 void *EC_KEY_insert_key_method_data(EC_KEY *key, void *data,
509                                     void *(*dup_func) (void *),
510                                     void (*free_func) (void *),
511                                     void (*clear_free_func) (void *))
512 {
513     EC_EXTRA_DATA *ex_data;
514
515     CRYPTO_w_lock(CRYPTO_LOCK_EC);
516     ex_data =
517         EC_EX_DATA_get_data(key->method_data, dup_func, free_func,
518                             clear_free_func);
519     if (ex_data == NULL)
520         EC_EX_DATA_set_data(&key->method_data, data, dup_func, free_func,
521                             clear_free_func);
522     CRYPTO_w_unlock(CRYPTO_LOCK_EC);
523
524     return ex_data;
525 }
526
527 void EC_KEY_set_asn1_flag(EC_KEY *key, int flag)
528 {
529     if (key->group != NULL)
530         EC_GROUP_set_asn1_flag(key->group, flag);
531 }
532
533 int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx)
534 {
535     if (key->group == NULL)
536         return 0;
537     return EC_GROUP_precompute_mult(key->group, ctx);
538 }
539
540 int EC_KEY_get_flags(const EC_KEY *key)
541 {
542     return key->flags;
543 }
544
545 void EC_KEY_set_flags(EC_KEY *key, int flags)
546 {
547     key->flags |= flags;
548 }
549
550 void EC_KEY_clear_flags(EC_KEY *key, int flags)
551 {
552     key->flags &= ~flags;
553 }