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