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