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