free cleanup almost the finale
[openssl.git] / crypto / ec / ec_lib.c
1 /* crypto/ec/ec_lib.c */
2 /*
3  * Originally written by Bodo Moeller for the OpenSSL project.
4  */
5 /* ====================================================================
6  * Copyright (c) 1998-2003 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  * Binary polynomial ECC support in OpenSSL originally developed by
61  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
62  */
63
64 #include <string.h>
65
66 #include <openssl/err.h>
67 #include <openssl/opensslv.h>
68
69 #include "ec_lcl.h"
70
71 const char EC_version[] = "EC" OPENSSL_VERSION_PTEXT;
72
73 /* functions for EC_GROUP objects */
74
75 EC_GROUP *EC_GROUP_new(const EC_METHOD *meth)
76 {
77     EC_GROUP *ret;
78
79     if (meth == NULL) {
80         ECerr(EC_F_EC_GROUP_NEW, EC_R_SLOT_FULL);
81         return NULL;
82     }
83     if (meth->group_init == 0) {
84         ECerr(EC_F_EC_GROUP_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
85         return NULL;
86     }
87
88     ret = OPENSSL_malloc(sizeof *ret);
89     if (ret == NULL) {
90         ECerr(EC_F_EC_GROUP_NEW, ERR_R_MALLOC_FAILURE);
91         return NULL;
92     }
93
94     ret->meth = meth;
95
96     ret->extra_data = NULL;
97     ret->mont_data = NULL;
98
99     ret->generator = NULL;
100     ret->order = BN_new();
101     ret->cofactor = NULL;
102     if (!ret->order)
103         goto err;
104     ret->cofactor = BN_new();
105     if (!ret->cofactor)
106         goto err;
107
108     ret->curve_name = 0;
109     ret->asn1_flag = OPENSSL_EC_NAMED_CURVE;
110     ret->asn1_form = POINT_CONVERSION_UNCOMPRESSED;
111
112     ret->seed = NULL;
113     ret->seed_len = 0;
114
115     if (!meth->group_init(ret))
116         goto err;
117
118     return ret;
119  err:
120     if (ret->order)
121         BN_free(ret->order);
122     if (ret->cofactor)
123         BN_free(ret->cofactor);
124     OPENSSL_free(ret);
125     return NULL;
126 }
127
128 void EC_GROUP_free(EC_GROUP *group)
129 {
130     if (!group)
131         return;
132
133     if (group->meth->group_finish != 0)
134         group->meth->group_finish(group);
135
136     EC_EX_DATA_free_all_data(&group->extra_data);
137
138     if (group->mont_data)
139         BN_MONT_CTX_free(group->mont_data);
140
141     EC_POINT_free(group->generator);
142     BN_free(group->order);
143     BN_free(group->cofactor);
144
145     if (group->seed)
146         OPENSSL_free(group->seed);
147
148     OPENSSL_free(group);
149 }
150
151 void EC_GROUP_clear_free(EC_GROUP *group)
152 {
153     if (!group)
154         return;
155
156     if (group->meth->group_clear_finish != 0)
157         group->meth->group_clear_finish(group);
158     else if (group->meth->group_finish != 0)
159         group->meth->group_finish(group);
160
161     EC_EX_DATA_clear_free_all_data(&group->extra_data);
162
163     if (group->mont_data)
164         BN_MONT_CTX_free(group->mont_data);
165
166     EC_POINT_clear_free(group->generator);
167     BN_clear_free(group->order);
168     BN_clear_free(group->cofactor);
169     OPENSSL_clear_free(group->seed, group->seed_len);
170     OPENSSL_clear_free(group, sizeof *group);
171 }
172
173 int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)
174 {
175     EC_EXTRA_DATA *d;
176
177     if (dest->meth->group_copy == 0) {
178         ECerr(EC_F_EC_GROUP_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
179         return 0;
180     }
181     if (dest->meth != src->meth) {
182         ECerr(EC_F_EC_GROUP_COPY, EC_R_INCOMPATIBLE_OBJECTS);
183         return 0;
184     }
185     if (dest == src)
186         return 1;
187
188     EC_EX_DATA_free_all_data(&dest->extra_data);
189
190     for (d = src->extra_data; d != NULL; d = d->next) {
191         void *t = d->dup_func(d->data);
192
193         if (t == NULL)
194             return 0;
195         if (!EC_EX_DATA_set_data
196             (&dest->extra_data, t, d->dup_func, d->free_func,
197              d->clear_free_func))
198             return 0;
199     }
200
201     if (src->mont_data != NULL) {
202         if (dest->mont_data == NULL) {
203             dest->mont_data = BN_MONT_CTX_new();
204             if (dest->mont_data == NULL)
205                 return 0;
206         }
207         if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data))
208             return 0;
209     } else {
210         /* src->generator == NULL */
211         if (dest->mont_data != NULL) {
212             BN_MONT_CTX_free(dest->mont_data);
213             dest->mont_data = NULL;
214         }
215     }
216
217     if (src->generator != NULL) {
218         if (dest->generator == NULL) {
219             dest->generator = EC_POINT_new(dest);
220             if (dest->generator == NULL)
221                 return 0;
222         }
223         if (!EC_POINT_copy(dest->generator, src->generator))
224             return 0;
225     } else {
226         /* src->generator == NULL */
227         EC_POINT_clear_free(dest->generator);
228         dest->generator = NULL;
229     }
230
231     if (!BN_copy(dest->order, src->order))
232         return 0;
233     if (!BN_copy(dest->cofactor, src->cofactor))
234         return 0;
235
236     dest->curve_name = src->curve_name;
237     dest->asn1_flag = src->asn1_flag;
238     dest->asn1_form = src->asn1_form;
239
240     if (src->seed) {
241         if (dest->seed)
242             OPENSSL_free(dest->seed);
243         dest->seed = OPENSSL_malloc(src->seed_len);
244         if (dest->seed == NULL)
245             return 0;
246         if (!memcpy(dest->seed, src->seed, src->seed_len))
247             return 0;
248         dest->seed_len = src->seed_len;
249     } else {
250         if (dest->seed)
251             OPENSSL_free(dest->seed);
252         dest->seed = NULL;
253         dest->seed_len = 0;
254     }
255
256     return dest->meth->group_copy(dest, src);
257 }
258
259 EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)
260 {
261     EC_GROUP *t = NULL;
262     int ok = 0;
263
264     if (a == NULL)
265         return NULL;
266
267     if ((t = EC_GROUP_new(a->meth)) == NULL)
268         return (NULL);
269     if (!EC_GROUP_copy(t, a))
270         goto err;
271
272     ok = 1;
273
274  err:
275     if (!ok) {
276         EC_GROUP_free(t);
277         return NULL;
278     }
279         return t;
280 }
281
282 const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group)
283 {
284     return group->meth;
285 }
286
287 int EC_METHOD_get_field_type(const EC_METHOD *meth)
288 {
289     return meth->field_type;
290 }
291
292 int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
293                            const BIGNUM *order, const BIGNUM *cofactor)
294 {
295     if (generator == NULL) {
296         ECerr(EC_F_EC_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER);
297         return 0;
298     }
299
300     if (group->generator == NULL) {
301         group->generator = EC_POINT_new(group);
302         if (group->generator == NULL)
303             return 0;
304     }
305     if (!EC_POINT_copy(group->generator, generator))
306         return 0;
307
308     if (order != NULL) {
309         if (!BN_copy(group->order, order))
310             return 0;
311     } else
312         BN_zero(group->order);
313
314     if (cofactor != NULL) {
315         if (!BN_copy(group->cofactor, cofactor))
316             return 0;
317     } else
318         BN_zero(group->cofactor);
319
320     /*
321      * We ignore the return value because some groups have an order with
322      * factors of two, which makes the Montgomery setup fail.
323      * |group->mont_data| will be NULL in this case.
324      */
325     ec_precompute_mont_data(group);
326
327     return 1;
328 }
329
330 const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
331 {
332     return group->generator;
333 }
334
335 BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group)
336 {
337     return group->mont_data;
338 }
339
340 int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
341 {
342     if (!BN_copy(order, group->order))
343         return 0;
344
345     return !BN_is_zero(order);
346 }
347
348 int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
349                           BN_CTX *ctx)
350 {
351     if (!BN_copy(cofactor, group->cofactor))
352         return 0;
353
354     return !BN_is_zero(group->cofactor);
355 }
356
357 void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)
358 {
359     group->curve_name = nid;
360 }
361
362 int EC_GROUP_get_curve_name(const EC_GROUP *group)
363 {
364     return group->curve_name;
365 }
366
367 void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
368 {
369     group->asn1_flag = flag;
370 }
371
372 int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
373 {
374     return group->asn1_flag;
375 }
376
377 void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
378                                         point_conversion_form_t form)
379 {
380     group->asn1_form = form;
381 }
382
383 point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP
384                                                            *group)
385 {
386     return group->asn1_form;
387 }
388
389 size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
390 {
391     if (group->seed) {
392         OPENSSL_free(group->seed);
393         group->seed = NULL;
394         group->seed_len = 0;
395     }
396
397     if (!len || !p)
398         return 1;
399
400     if ((group->seed = OPENSSL_malloc(len)) == NULL)
401         return 0;
402     memcpy(group->seed, p, len);
403     group->seed_len = len;
404
405     return len;
406 }
407
408 unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
409 {
410     return group->seed;
411 }
412
413 size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
414 {
415     return group->seed_len;
416 }
417
418 int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
419                            const BIGNUM *b, BN_CTX *ctx)
420 {
421     if (group->meth->group_set_curve == 0) {
422         ECerr(EC_F_EC_GROUP_SET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
423         return 0;
424     }
425     return group->meth->group_set_curve(group, p, a, b, ctx);
426 }
427
428 int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
429                            BIGNUM *b, BN_CTX *ctx)
430 {
431     if (group->meth->group_get_curve == 0) {
432         ECerr(EC_F_EC_GROUP_GET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
433         return 0;
434     }
435     return group->meth->group_get_curve(group, p, a, b, ctx);
436 }
437
438 #ifndef OPENSSL_NO_EC2M
439 int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
440                             const BIGNUM *b, BN_CTX *ctx)
441 {
442     if (group->meth->group_set_curve == 0) {
443         ECerr(EC_F_EC_GROUP_SET_CURVE_GF2M,
444               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
445         return 0;
446     }
447     return group->meth->group_set_curve(group, p, a, b, ctx);
448 }
449
450 int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
451                             BIGNUM *b, BN_CTX *ctx)
452 {
453     if (group->meth->group_get_curve == 0) {
454         ECerr(EC_F_EC_GROUP_GET_CURVE_GF2M,
455               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
456         return 0;
457     }
458     return group->meth->group_get_curve(group, p, a, b, ctx);
459 }
460 #endif
461
462 int EC_GROUP_get_degree(const EC_GROUP *group)
463 {
464     if (group->meth->group_get_degree == 0) {
465         ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
466         return 0;
467     }
468     return group->meth->group_get_degree(group);
469 }
470
471 int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
472 {
473     if (group->meth->group_check_discriminant == 0) {
474         ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT,
475               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
476         return 0;
477     }
478     return group->meth->group_check_discriminant(group, ctx);
479 }
480
481 int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
482 {
483     int r = 0;
484     BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
485     BN_CTX *ctx_new = NULL;
486
487     /* compare the field types */
488     if (EC_METHOD_get_field_type(EC_GROUP_method_of(a)) !=
489         EC_METHOD_get_field_type(EC_GROUP_method_of(b)))
490         return 1;
491     /* compare the curve name (if present in both) */
492     if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
493         EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
494         return 1;
495
496     if (!ctx)
497         ctx_new = ctx = BN_CTX_new();
498     if (!ctx)
499         return -1;
500
501     BN_CTX_start(ctx);
502     a1 = BN_CTX_get(ctx);
503     a2 = BN_CTX_get(ctx);
504     a3 = BN_CTX_get(ctx);
505     b1 = BN_CTX_get(ctx);
506     b2 = BN_CTX_get(ctx);
507     b3 = BN_CTX_get(ctx);
508     if (!b3) {
509         BN_CTX_end(ctx);
510         if (ctx_new)
511             BN_CTX_free(ctx);
512         return -1;
513     }
514
515     /*
516      * XXX This approach assumes that the external representation of curves
517      * over the same field type is the same.
518      */
519     if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
520         !b->meth->group_get_curve(b, b1, b2, b3, ctx))
521         r = 1;
522
523     if (r || BN_cmp(a1, b1) || BN_cmp(a2, b2) || BN_cmp(a3, b3))
524         r = 1;
525
526     /* XXX EC_POINT_cmp() assumes that the methods are equal */
527     if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
528                           EC_GROUP_get0_generator(b), ctx))
529         r = 1;
530
531     if (!r) {
532         /* compare the order and cofactor */
533         if (!EC_GROUP_get_order(a, a1, ctx) ||
534             !EC_GROUP_get_order(b, b1, ctx) ||
535             !EC_GROUP_get_cofactor(a, a2, ctx) ||
536             !EC_GROUP_get_cofactor(b, b2, ctx)) {
537             BN_CTX_end(ctx);
538             if (ctx_new)
539                 BN_CTX_free(ctx);
540             return -1;
541         }
542         if (BN_cmp(a1, b1) || BN_cmp(a2, b2))
543             r = 1;
544     }
545
546     BN_CTX_end(ctx);
547     if (ctx_new)
548         BN_CTX_free(ctx);
549
550     return r;
551 }
552
553 /* this has 'package' visibility */
554 int EC_EX_DATA_set_data(EC_EXTRA_DATA **ex_data, void *data,
555                         void *(*dup_func) (void *),
556                         void (*free_func) (void *),
557                         void (*clear_free_func) (void *))
558 {
559     EC_EXTRA_DATA *d;
560
561     if (ex_data == NULL)
562         return 0;
563
564     for (d = *ex_data; d != NULL; d = d->next) {
565         if (d->dup_func == dup_func && d->free_func == free_func
566             && d->clear_free_func == clear_free_func) {
567             ECerr(EC_F_EC_EX_DATA_SET_DATA, EC_R_SLOT_FULL);
568             return 0;
569         }
570     }
571
572     if (data == NULL)
573         /* no explicit entry needed */
574         return 1;
575
576     d = OPENSSL_malloc(sizeof *d);
577     if (d == NULL)
578         return 0;
579
580     d->data = data;
581     d->dup_func = dup_func;
582     d->free_func = free_func;
583     d->clear_free_func = clear_free_func;
584
585     d->next = *ex_data;
586     *ex_data = d;
587
588     return 1;
589 }
590
591 /* this has 'package' visibility */
592 void *EC_EX_DATA_get_data(const EC_EXTRA_DATA *ex_data,
593                           void *(*dup_func) (void *),
594                           void (*free_func) (void *),
595                           void (*clear_free_func) (void *))
596 {
597     const EC_EXTRA_DATA *d;
598
599     for (d = ex_data; d != NULL; d = d->next) {
600         if (d->dup_func == dup_func && d->free_func == free_func
601             && d->clear_free_func == clear_free_func)
602             return d->data;
603     }
604
605     return NULL;
606 }
607
608 /* this has 'package' visibility */
609 void EC_EX_DATA_free_data(EC_EXTRA_DATA **ex_data,
610                           void *(*dup_func) (void *),
611                           void (*free_func) (void *),
612                           void (*clear_free_func) (void *))
613 {
614     EC_EXTRA_DATA **p;
615
616     if (ex_data == NULL)
617         return;
618
619     for (p = ex_data; *p != NULL; p = &((*p)->next)) {
620         if ((*p)->dup_func == dup_func && (*p)->free_func == free_func
621             && (*p)->clear_free_func == clear_free_func) {
622             EC_EXTRA_DATA *next = (*p)->next;
623
624             (*p)->free_func((*p)->data);
625             OPENSSL_free(*p);
626
627             *p = next;
628             return;
629         }
630     }
631 }
632
633 /* this has 'package' visibility */
634 void EC_EX_DATA_clear_free_data(EC_EXTRA_DATA **ex_data,
635                                 void *(*dup_func) (void *),
636                                 void (*free_func) (void *),
637                                 void (*clear_free_func) (void *))
638 {
639     EC_EXTRA_DATA **p;
640
641     if (ex_data == NULL)
642         return;
643
644     for (p = ex_data; *p != NULL; p = &((*p)->next)) {
645         if ((*p)->dup_func == dup_func && (*p)->free_func == free_func
646             && (*p)->clear_free_func == clear_free_func) {
647             EC_EXTRA_DATA *next = (*p)->next;
648
649             (*p)->clear_free_func((*p)->data);
650             OPENSSL_free(*p);
651
652             *p = next;
653             return;
654         }
655     }
656 }
657
658 /* this has 'package' visibility */
659 void EC_EX_DATA_free_all_data(EC_EXTRA_DATA **ex_data)
660 {
661     EC_EXTRA_DATA *d;
662
663     if (ex_data == NULL)
664         return;
665
666     d = *ex_data;
667     while (d) {
668         EC_EXTRA_DATA *next = d->next;
669
670         d->free_func(d->data);
671         OPENSSL_free(d);
672
673         d = next;
674     }
675     *ex_data = NULL;
676 }
677
678 /* this has 'package' visibility */
679 void EC_EX_DATA_clear_free_all_data(EC_EXTRA_DATA **ex_data)
680 {
681     EC_EXTRA_DATA *d;
682
683     if (ex_data == NULL)
684         return;
685
686     d = *ex_data;
687     while (d) {
688         EC_EXTRA_DATA *next = d->next;
689
690         d->clear_free_func(d->data);
691         OPENSSL_free(d);
692
693         d = next;
694     }
695     *ex_data = NULL;
696 }
697
698 /* functions for EC_POINT objects */
699
700 EC_POINT *EC_POINT_new(const EC_GROUP *group)
701 {
702     EC_POINT *ret;
703
704     if (group == NULL) {
705         ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER);
706         return NULL;
707     }
708     if (group->meth->point_init == 0) {
709         ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
710         return NULL;
711     }
712
713     ret = OPENSSL_malloc(sizeof *ret);
714     if (ret == NULL) {
715         ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE);
716         return NULL;
717     }
718
719     ret->meth = group->meth;
720
721     if (!ret->meth->point_init(ret)) {
722         OPENSSL_free(ret);
723         return NULL;
724     }
725
726     return ret;
727 }
728
729 void EC_POINT_free(EC_POINT *point)
730 {
731     if (!point)
732         return;
733
734     if (point->meth->point_finish != 0)
735         point->meth->point_finish(point);
736     OPENSSL_free(point);
737 }
738
739 void EC_POINT_clear_free(EC_POINT *point)
740 {
741     if (!point)
742         return;
743
744     if (point->meth->point_clear_finish != 0)
745         point->meth->point_clear_finish(point);
746     else if (point->meth->point_finish != 0)
747         point->meth->point_finish(point);
748     OPENSSL_clear_free(point, sizeof *point);
749 }
750
751 int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)
752 {
753     if (dest->meth->point_copy == 0) {
754         ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
755         return 0;
756     }
757     if (dest->meth != src->meth) {
758         ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS);
759         return 0;
760     }
761     if (dest == src)
762         return 1;
763     return dest->meth->point_copy(dest, src);
764 }
765
766 EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
767 {
768     EC_POINT *t;
769     int r;
770
771     if (a == NULL)
772         return NULL;
773
774     t = EC_POINT_new(group);
775     if (t == NULL)
776         return (NULL);
777     r = EC_POINT_copy(t, a);
778     if (!r) {
779         EC_POINT_free(t);
780         return NULL;
781     }
782     return t;
783 }
784
785 const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
786 {
787     return point->meth;
788 }
789
790 int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
791 {
792     if (group->meth->point_set_to_infinity == 0) {
793         ECerr(EC_F_EC_POINT_SET_TO_INFINITY,
794               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
795         return 0;
796     }
797     if (group->meth != point->meth) {
798         ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
799         return 0;
800     }
801     return group->meth->point_set_to_infinity(group, point);
802 }
803
804 int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
805                                              EC_POINT *point, const BIGNUM *x,
806                                              const BIGNUM *y, const BIGNUM *z,
807                                              BN_CTX *ctx)
808 {
809     if (group->meth->point_set_Jprojective_coordinates_GFp == 0) {
810         ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
811               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
812         return 0;
813     }
814     if (group->meth != point->meth) {
815         ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
816               EC_R_INCOMPATIBLE_OBJECTS);
817         return 0;
818     }
819     return group->meth->point_set_Jprojective_coordinates_GFp(group, point, x,
820                                                               y, z, ctx);
821 }
822
823 int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
824                                              const EC_POINT *point, BIGNUM *x,
825                                              BIGNUM *y, BIGNUM *z,
826                                              BN_CTX *ctx)
827 {
828     if (group->meth->point_get_Jprojective_coordinates_GFp == 0) {
829         ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
830               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
831         return 0;
832     }
833     if (group->meth != point->meth) {
834         ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
835               EC_R_INCOMPATIBLE_OBJECTS);
836         return 0;
837     }
838     return group->meth->point_get_Jprojective_coordinates_GFp(group, point, x,
839                                                               y, z, ctx);
840 }
841
842 int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
843                                         EC_POINT *point, const BIGNUM *x,
844                                         const BIGNUM *y, BN_CTX *ctx)
845 {
846     if (group->meth->point_set_affine_coordinates == 0) {
847         ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP,
848               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
849         return 0;
850     }
851     if (group->meth != point->meth) {
852         ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP,
853               EC_R_INCOMPATIBLE_OBJECTS);
854         return 0;
855     }
856     return group->meth->point_set_affine_coordinates(group, point, x, y, ctx);
857 }
858
859 #ifndef OPENSSL_NO_EC2M
860 int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,
861                                          EC_POINT *point, const BIGNUM *x,
862                                          const BIGNUM *y, BN_CTX *ctx)
863 {
864     if (group->meth->point_set_affine_coordinates == 0) {
865         ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M,
866               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
867         return 0;
868     }
869     if (group->meth != point->meth) {
870         ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M,
871               EC_R_INCOMPATIBLE_OBJECTS);
872         return 0;
873     }
874     return group->meth->point_set_affine_coordinates(group, point, x, y, ctx);
875 }
876 #endif
877
878 int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
879                                         const EC_POINT *point, BIGNUM *x,
880                                         BIGNUM *y, BN_CTX *ctx)
881 {
882     if (group->meth->point_get_affine_coordinates == 0) {
883         ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP,
884               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
885         return 0;
886     }
887     if (group->meth != point->meth) {
888         ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP,
889               EC_R_INCOMPATIBLE_OBJECTS);
890         return 0;
891     }
892     return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
893 }
894
895 #ifndef OPENSSL_NO_EC2M
896 int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,
897                                          const EC_POINT *point, BIGNUM *x,
898                                          BIGNUM *y, BN_CTX *ctx)
899 {
900     if (group->meth->point_get_affine_coordinates == 0) {
901         ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M,
902               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
903         return 0;
904     }
905     if (group->meth != point->meth) {
906         ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M,
907               EC_R_INCOMPATIBLE_OBJECTS);
908         return 0;
909     }
910     return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
911 }
912 #endif
913
914 int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
915                  const EC_POINT *b, BN_CTX *ctx)
916 {
917     if (group->meth->add == 0) {
918         ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
919         return 0;
920     }
921     if ((group->meth != r->meth) || (r->meth != a->meth)
922         || (a->meth != b->meth)) {
923         ECerr(EC_F_EC_POINT_ADD, EC_R_INCOMPATIBLE_OBJECTS);
924         return 0;
925     }
926     return group->meth->add(group, r, a, b, ctx);
927 }
928
929 int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
930                  BN_CTX *ctx)
931 {
932     if (group->meth->dbl == 0) {
933         ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
934         return 0;
935     }
936     if ((group->meth != r->meth) || (r->meth != a->meth)) {
937         ECerr(EC_F_EC_POINT_DBL, EC_R_INCOMPATIBLE_OBJECTS);
938         return 0;
939     }
940     return group->meth->dbl(group, r, a, ctx);
941 }
942
943 int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
944 {
945     if (group->meth->invert == 0) {
946         ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
947         return 0;
948     }
949     if (group->meth != a->meth) {
950         ECerr(EC_F_EC_POINT_INVERT, EC_R_INCOMPATIBLE_OBJECTS);
951         return 0;
952     }
953     return group->meth->invert(group, a, ctx);
954 }
955
956 int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
957 {
958     if (group->meth->is_at_infinity == 0) {
959         ECerr(EC_F_EC_POINT_IS_AT_INFINITY,
960               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
961         return 0;
962     }
963     if (group->meth != point->meth) {
964         ECerr(EC_F_EC_POINT_IS_AT_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
965         return 0;
966     }
967     return group->meth->is_at_infinity(group, point);
968 }
969
970 int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
971                          BN_CTX *ctx)
972 {
973     if (group->meth->is_on_curve == 0) {
974         ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
975         return 0;
976     }
977     if (group->meth != point->meth) {
978         ECerr(EC_F_EC_POINT_IS_ON_CURVE, EC_R_INCOMPATIBLE_OBJECTS);
979         return 0;
980     }
981     return group->meth->is_on_curve(group, point, ctx);
982 }
983
984 int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
985                  BN_CTX *ctx)
986 {
987     if (group->meth->point_cmp == 0) {
988         ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
989         return -1;
990     }
991     if ((group->meth != a->meth) || (a->meth != b->meth)) {
992         ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS);
993         return -1;
994     }
995     return group->meth->point_cmp(group, a, b, ctx);
996 }
997
998 int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
999 {
1000     if (group->meth->make_affine == 0) {
1001         ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1002         return 0;
1003     }
1004     if (group->meth != point->meth) {
1005         ECerr(EC_F_EC_POINT_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
1006         return 0;
1007     }
1008     return group->meth->make_affine(group, point, ctx);
1009 }
1010
1011 int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
1012                           EC_POINT *points[], BN_CTX *ctx)
1013 {
1014     size_t i;
1015
1016     if (group->meth->points_make_affine == 0) {
1017         ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1018         return 0;
1019     }
1020     for (i = 0; i < num; i++) {
1021         if (group->meth != points[i]->meth) {
1022             ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
1023             return 0;
1024         }
1025     }
1026     return group->meth->points_make_affine(group, num, points, ctx);
1027 }
1028
1029 /*
1030  * Functions for point multiplication. If group->meth->mul is 0, we use the
1031  * wNAF-based implementations in ec_mult.c; otherwise we dispatch through
1032  * methods.
1033  */
1034
1035 int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
1036                   size_t num, const EC_POINT *points[],
1037                   const BIGNUM *scalars[], BN_CTX *ctx)
1038 {
1039     if (group->meth->mul == 0)
1040         /* use default */
1041         return ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
1042
1043     return group->meth->mul(group, r, scalar, num, points, scalars, ctx);
1044 }
1045
1046 int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
1047                  const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
1048 {
1049     /* just a convenient interface to EC_POINTs_mul() */
1050
1051     const EC_POINT *points[1];
1052     const BIGNUM *scalars[1];
1053
1054     points[0] = point;
1055     scalars[0] = p_scalar;
1056
1057     return EC_POINTs_mul(group, r, g_scalar,
1058                          (point != NULL
1059                           && p_scalar != NULL), points, scalars, ctx);
1060 }
1061
1062 int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
1063 {
1064     if (group->meth->mul == 0)
1065         /* use default */
1066         return ec_wNAF_precompute_mult(group, ctx);
1067
1068     if (group->meth->precompute_mult != 0)
1069         return group->meth->precompute_mult(group, ctx);
1070     else
1071         return 1;               /* nothing to do, so report success */
1072 }
1073
1074 int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
1075 {
1076     if (group->meth->mul == 0)
1077         /* use default */
1078         return ec_wNAF_have_precompute_mult(group);
1079
1080     if (group->meth->have_precompute_mult != 0)
1081         return group->meth->have_precompute_mult(group);
1082     else
1083         return 0;               /* cannot tell whether precomputation has
1084                                  * been performed */
1085 }
1086
1087 /*
1088  * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
1089  * returns one on success. On error it returns zero.
1090  */
1091 int ec_precompute_mont_data(EC_GROUP *group)
1092 {
1093     BN_CTX *ctx = BN_CTX_new();
1094     int ret = 0;
1095
1096     if (group->mont_data) {
1097         BN_MONT_CTX_free(group->mont_data);
1098         group->mont_data = NULL;
1099     }
1100
1101     if (ctx == NULL)
1102         goto err;
1103
1104     group->mont_data = BN_MONT_CTX_new();
1105     if (!group->mont_data)
1106         goto err;
1107
1108     if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) {
1109         BN_MONT_CTX_free(group->mont_data);
1110         group->mont_data = NULL;
1111         goto err;
1112     }
1113
1114     ret = 1;
1115
1116  err:
1117
1118     if (ctx)
1119         BN_CTX_free(ctx);
1120     return ret;
1121 }