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