Allow specifying the tag after AAD in CCM mode (2)
[openssl.git] / crypto / ec / ec_lib.c
1 /*
2  * Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4  *
5  * Licensed under the Apache License 2.0 (the "License").  You may not use
6  * this file except in compliance with the License.  You can obtain a copy
7  * in the file LICENSE in the source distribution or at
8  * https://www.openssl.org/source/license.html
9  */
10
11 #include <string.h>
12
13 #include <openssl/err.h>
14 #include <openssl/opensslv.h>
15
16 #include "ec_local.h"
17
18 /* functions for EC_GROUP objects */
19
20 EC_GROUP *EC_GROUP_new_ex(OPENSSL_CTX *libctx, const EC_METHOD *meth)
21 {
22     EC_GROUP *ret;
23
24     if (meth == NULL) {
25         ECerr(EC_F_EC_GROUP_NEW_EX, EC_R_SLOT_FULL);
26         return NULL;
27     }
28     if (meth->group_init == 0) {
29         ECerr(EC_F_EC_GROUP_NEW_EX, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
30         return NULL;
31     }
32
33     ret = OPENSSL_zalloc(sizeof(*ret));
34     if (ret == NULL) {
35         ECerr(EC_F_EC_GROUP_NEW_EX, ERR_R_MALLOC_FAILURE);
36         return NULL;
37     }
38
39     ret->libctx = libctx;
40     ret->meth = meth;
41     if ((ret->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
42         ret->order = BN_new();
43         if (ret->order == NULL)
44             goto err;
45         ret->cofactor = BN_new();
46         if (ret->cofactor == NULL)
47             goto err;
48     }
49     ret->asn1_flag = OPENSSL_EC_NAMED_CURVE;
50     ret->asn1_form = POINT_CONVERSION_UNCOMPRESSED;
51     if (!meth->group_init(ret))
52         goto err;
53     return ret;
54
55  err:
56     BN_free(ret->order);
57     BN_free(ret->cofactor);
58     OPENSSL_free(ret);
59     return NULL;
60 }
61
62 #ifndef FIPS_MODE
63 EC_GROUP *EC_GROUP_new(const EC_METHOD *meth)
64 {
65     return EC_GROUP_new_ex(NULL, meth);
66 }
67 #endif
68
69 void EC_pre_comp_free(EC_GROUP *group)
70 {
71     switch (group->pre_comp_type) {
72     case PCT_none:
73         break;
74     case PCT_nistz256:
75 #ifdef ECP_NISTZ256_ASM
76         EC_nistz256_pre_comp_free(group->pre_comp.nistz256);
77 #endif
78         break;
79 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
80     case PCT_nistp224:
81         EC_nistp224_pre_comp_free(group->pre_comp.nistp224);
82         break;
83     case PCT_nistp256:
84         EC_nistp256_pre_comp_free(group->pre_comp.nistp256);
85         break;
86     case PCT_nistp521:
87         EC_nistp521_pre_comp_free(group->pre_comp.nistp521);
88         break;
89 #else
90     case PCT_nistp224:
91     case PCT_nistp256:
92     case PCT_nistp521:
93         break;
94 #endif
95     case PCT_ec:
96         EC_ec_pre_comp_free(group->pre_comp.ec);
97         break;
98     }
99     group->pre_comp.ec = NULL;
100 }
101
102 void EC_GROUP_free(EC_GROUP *group)
103 {
104     if (!group)
105         return;
106
107     if (group->meth->group_finish != 0)
108         group->meth->group_finish(group);
109
110     EC_pre_comp_free(group);
111     BN_MONT_CTX_free(group->mont_data);
112     EC_POINT_free(group->generator);
113     BN_free(group->order);
114     BN_free(group->cofactor);
115     OPENSSL_free(group->seed);
116     OPENSSL_free(group);
117 }
118
119 #ifndef OPENSSL_NO_DEPRECATED_3_0
120 void EC_GROUP_clear_free(EC_GROUP *group)
121 {
122     if (!group)
123         return;
124
125     if (group->meth->group_clear_finish != 0)
126         group->meth->group_clear_finish(group);
127     else if (group->meth->group_finish != 0)
128         group->meth->group_finish(group);
129
130     EC_pre_comp_free(group);
131     BN_MONT_CTX_free(group->mont_data);
132     EC_POINT_clear_free(group->generator);
133     BN_clear_free(group->order);
134     BN_clear_free(group->cofactor);
135     OPENSSL_clear_free(group->seed, group->seed_len);
136     OPENSSL_clear_free(group, sizeof(*group));
137 }
138 #endif
139
140 int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)
141 {
142     if (dest->meth->group_copy == 0) {
143         ECerr(EC_F_EC_GROUP_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
144         return 0;
145     }
146     if (dest->meth != src->meth) {
147         ECerr(EC_F_EC_GROUP_COPY, EC_R_INCOMPATIBLE_OBJECTS);
148         return 0;
149     }
150     if (dest == src)
151         return 1;
152
153     dest->libctx = src->libctx;
154     dest->curve_name = src->curve_name;
155
156     /* Copy precomputed */
157     dest->pre_comp_type = src->pre_comp_type;
158     switch (src->pre_comp_type) {
159     case PCT_none:
160         dest->pre_comp.ec = NULL;
161         break;
162     case PCT_nistz256:
163 #ifdef ECP_NISTZ256_ASM
164         dest->pre_comp.nistz256 = EC_nistz256_pre_comp_dup(src->pre_comp.nistz256);
165 #endif
166         break;
167 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
168     case PCT_nistp224:
169         dest->pre_comp.nistp224 = EC_nistp224_pre_comp_dup(src->pre_comp.nistp224);
170         break;
171     case PCT_nistp256:
172         dest->pre_comp.nistp256 = EC_nistp256_pre_comp_dup(src->pre_comp.nistp256);
173         break;
174     case PCT_nistp521:
175         dest->pre_comp.nistp521 = EC_nistp521_pre_comp_dup(src->pre_comp.nistp521);
176         break;
177 #else
178     case PCT_nistp224:
179     case PCT_nistp256:
180     case PCT_nistp521:
181         break;
182 #endif
183     case PCT_ec:
184         dest->pre_comp.ec = EC_ec_pre_comp_dup(src->pre_comp.ec);
185         break;
186     }
187
188     if (src->mont_data != NULL) {
189         if (dest->mont_data == NULL) {
190             dest->mont_data = BN_MONT_CTX_new();
191             if (dest->mont_data == NULL)
192                 return 0;
193         }
194         if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data))
195             return 0;
196     } else {
197         /* src->generator == NULL */
198         BN_MONT_CTX_free(dest->mont_data);
199         dest->mont_data = NULL;
200     }
201
202     if (src->generator != NULL) {
203         if (dest->generator == NULL) {
204             dest->generator = EC_POINT_new(dest);
205             if (dest->generator == NULL)
206                 return 0;
207         }
208         if (!EC_POINT_copy(dest->generator, src->generator))
209             return 0;
210     } else {
211         /* src->generator == NULL */
212         EC_POINT_clear_free(dest->generator);
213         dest->generator = NULL;
214     }
215
216     if ((src->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
217         if (!BN_copy(dest->order, src->order))
218             return 0;
219         if (!BN_copy(dest->cofactor, src->cofactor))
220             return 0;
221     }
222
223     dest->asn1_flag = src->asn1_flag;
224     dest->asn1_form = src->asn1_form;
225
226     if (src->seed) {
227         OPENSSL_free(dest->seed);
228         if ((dest->seed = OPENSSL_malloc(src->seed_len)) == NULL) {
229             ECerr(EC_F_EC_GROUP_COPY, ERR_R_MALLOC_FAILURE);
230             return 0;
231         }
232         if (!memcpy(dest->seed, src->seed, src->seed_len))
233             return 0;
234         dest->seed_len = src->seed_len;
235     } else {
236         OPENSSL_free(dest->seed);
237         dest->seed = NULL;
238         dest->seed_len = 0;
239     }
240
241     return dest->meth->group_copy(dest, src);
242 }
243
244 EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)
245 {
246     EC_GROUP *t = NULL;
247     int ok = 0;
248
249     if (a == NULL)
250         return NULL;
251
252     if ((t = EC_GROUP_new_ex(a->libctx, a->meth)) == NULL)
253         return NULL;
254     if (!EC_GROUP_copy(t, a))
255         goto err;
256
257     ok = 1;
258
259  err:
260     if (!ok) {
261         EC_GROUP_free(t);
262         return NULL;
263     }
264         return t;
265 }
266
267 const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group)
268 {
269     return group->meth;
270 }
271
272 int EC_METHOD_get_field_type(const EC_METHOD *meth)
273 {
274     return meth->field_type;
275 }
276
277 static int ec_precompute_mont_data(EC_GROUP *);
278
279 /*-
280  * Try computing cofactor from the generator order (n) and field cardinality (q).
281  * This works for all curves of cryptographic interest.
282  *
283  * Hasse thm: q + 1 - 2*sqrt(q) <= n*h <= q + 1 + 2*sqrt(q)
284  * h_min = (q + 1 - 2*sqrt(q))/n
285  * h_max = (q + 1 + 2*sqrt(q))/n
286  * h_max - h_min = 4*sqrt(q)/n
287  * So if n > 4*sqrt(q) holds, there is only one possible value for h:
288  * h = \lfloor (h_min + h_max)/2 \rceil = \lfloor (q + 1)/n \rceil
289  *
290  * Otherwise, zero cofactor and return success.
291  */
292 static int ec_guess_cofactor(EC_GROUP *group) {
293     int ret = 0;
294     BN_CTX *ctx = NULL;
295     BIGNUM *q = NULL;
296
297     /*-
298      * If the cofactor is too large, we cannot guess it.
299      * The RHS of below is a strict overestimate of lg(4 * sqrt(q))
300      */
301     if (BN_num_bits(group->order) <= (BN_num_bits(group->field) + 1) / 2 + 3) {
302         /* default to 0 */
303         BN_zero(group->cofactor);
304         /* return success */
305         return 1;
306     }
307
308     if ((ctx = BN_CTX_new_ex(group->libctx)) == NULL)
309         return 0;
310
311     BN_CTX_start(ctx);
312     if ((q = BN_CTX_get(ctx)) == NULL)
313         goto err;
314
315     /* set q = 2**m for binary fields; q = p otherwise */
316     if (group->meth->field_type == NID_X9_62_characteristic_two_field) {
317         BN_zero(q);
318         if (!BN_set_bit(q, BN_num_bits(group->field) - 1))
319             goto err;
320     } else {
321         if (!BN_copy(q, group->field))
322             goto err;
323     }
324
325     /* compute h = \lfloor (q + 1)/n \rceil = \lfloor (q + 1 + n/2)/n \rfloor */
326     if (!BN_rshift1(group->cofactor, group->order) /* n/2 */
327         || !BN_add(group->cofactor, group->cofactor, q) /* q + n/2 */
328         /* q + 1 + n/2 */
329         || !BN_add(group->cofactor, group->cofactor, BN_value_one())
330         /* (q + 1 + n/2)/n */
331         || !BN_div(group->cofactor, NULL, group->cofactor, group->order, ctx))
332         goto err;
333     ret = 1;
334  err:
335     BN_CTX_end(ctx);
336     BN_CTX_free(ctx);
337     return ret;
338 }
339
340 int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
341                            const BIGNUM *order, const BIGNUM *cofactor)
342 {
343     if (generator == NULL) {
344         ECerr(EC_F_EC_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER);
345         return 0;
346     }
347
348     /* require group->field >= 1 */
349     if (group->field == NULL || BN_is_zero(group->field)
350         || BN_is_negative(group->field)) {
351         ECerr(EC_F_EC_GROUP_SET_GENERATOR, EC_R_INVALID_FIELD);
352         return 0;
353     }
354
355     /*-
356      * - require order >= 1
357      * - enforce upper bound due to Hasse thm: order can be no more than one bit
358      *   longer than field cardinality
359      */
360     if (order == NULL || BN_is_zero(order) || BN_is_negative(order)
361         || BN_num_bits(order) > BN_num_bits(group->field) + 1) {
362         ECerr(EC_F_EC_GROUP_SET_GENERATOR, EC_R_INVALID_GROUP_ORDER);
363         return 0;
364     }
365
366     /*-
367      * Unfortunately the cofactor is an optional field in many standards.
368      * Internally, the lib uses 0 cofactor as a marker for "unknown cofactor".
369      * So accept cofactor == NULL or cofactor >= 0.
370      */
371     if (cofactor != NULL && BN_is_negative(cofactor)) {
372         ECerr(EC_F_EC_GROUP_SET_GENERATOR, EC_R_UNKNOWN_COFACTOR);
373         return 0;
374     }
375
376     if (group->generator == NULL) {
377         group->generator = EC_POINT_new(group);
378         if (group->generator == NULL)
379             return 0;
380     }
381     if (!EC_POINT_copy(group->generator, generator))
382         return 0;
383
384     if (!BN_copy(group->order, order))
385         return 0;
386
387     /* Either take the provided positive cofactor, or try to compute it */
388     if (cofactor != NULL && !BN_is_zero(cofactor)) {
389         if (!BN_copy(group->cofactor, cofactor))
390             return 0;
391     } else if (!ec_guess_cofactor(group)) {
392         BN_zero(group->cofactor);
393         return 0;
394     }
395
396     /*
397      * Some groups have an order with
398      * factors of two, which makes the Montgomery setup fail.
399      * |group->mont_data| will be NULL in this case.
400      */
401     if (BN_is_odd(group->order)) {
402         return ec_precompute_mont_data(group);
403     }
404
405     BN_MONT_CTX_free(group->mont_data);
406     group->mont_data = NULL;
407     return 1;
408 }
409
410 const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
411 {
412     return group->generator;
413 }
414
415 BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group)
416 {
417     return group->mont_data;
418 }
419
420 int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
421 {
422     if (group->order == NULL)
423         return 0;
424     if (!BN_copy(order, group->order))
425         return 0;
426
427     return !BN_is_zero(order);
428 }
429
430 const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group)
431 {
432     return group->order;
433 }
434
435 int EC_GROUP_order_bits(const EC_GROUP *group)
436 {
437     return group->meth->group_order_bits(group);
438 }
439
440 int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
441                           BN_CTX *ctx)
442 {
443
444     if (group->cofactor == NULL)
445         return 0;
446     if (!BN_copy(cofactor, group->cofactor))
447         return 0;
448
449     return !BN_is_zero(group->cofactor);
450 }
451
452 const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group)
453 {
454     return group->cofactor;
455 }
456
457 void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)
458 {
459     group->curve_name = nid;
460 }
461
462 int EC_GROUP_get_curve_name(const EC_GROUP *group)
463 {
464     return group->curve_name;
465 }
466
467 const BIGNUM *EC_GROUP_get0_field(const EC_GROUP *group)
468 {
469     return group->field;
470 }
471
472 void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
473 {
474     group->asn1_flag = flag;
475 }
476
477 int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
478 {
479     return group->asn1_flag;
480 }
481
482 void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
483                                         point_conversion_form_t form)
484 {
485     group->asn1_form = form;
486 }
487
488 point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP
489                                                            *group)
490 {
491     return group->asn1_form;
492 }
493
494 size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
495 {
496     OPENSSL_free(group->seed);
497     group->seed = NULL;
498     group->seed_len = 0;
499
500     if (!len || !p)
501         return 1;
502
503     if ((group->seed = OPENSSL_malloc(len)) == NULL) {
504         ECerr(EC_F_EC_GROUP_SET_SEED, ERR_R_MALLOC_FAILURE);
505         return 0;
506     }
507     memcpy(group->seed, p, len);
508     group->seed_len = len;
509
510     return len;
511 }
512
513 unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
514 {
515     return group->seed;
516 }
517
518 size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
519 {
520     return group->seed_len;
521 }
522
523 int EC_GROUP_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
524                        const BIGNUM *b, BN_CTX *ctx)
525 {
526     if (group->meth->group_set_curve == 0) {
527         ECerr(EC_F_EC_GROUP_SET_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
528         return 0;
529     }
530     return group->meth->group_set_curve(group, p, a, b, ctx);
531 }
532
533 int EC_GROUP_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b,
534                        BN_CTX *ctx)
535 {
536     if (group->meth->group_get_curve == NULL) {
537         ECerr(EC_F_EC_GROUP_GET_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
538         return 0;
539     }
540     return group->meth->group_get_curve(group, p, a, b, ctx);
541 }
542
543 #ifndef OPENSSL_NO_DEPRECATED_3_0
544 int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
545                            const BIGNUM *b, BN_CTX *ctx)
546 {
547     return EC_GROUP_set_curve(group, p, a, b, ctx);
548 }
549
550 int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
551                            BIGNUM *b, BN_CTX *ctx)
552 {
553     return EC_GROUP_get_curve(group, p, a, b, ctx);
554 }
555
556 # ifndef OPENSSL_NO_EC2M
557 int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
558                             const BIGNUM *b, BN_CTX *ctx)
559 {
560     return EC_GROUP_set_curve(group, p, a, b, ctx);
561 }
562
563 int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
564                             BIGNUM *b, BN_CTX *ctx)
565 {
566     return EC_GROUP_get_curve(group, p, a, b, ctx);
567 }
568 # endif
569 #endif
570
571 int EC_GROUP_get_degree(const EC_GROUP *group)
572 {
573     if (group->meth->group_get_degree == 0) {
574         ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
575         return 0;
576     }
577     return group->meth->group_get_degree(group);
578 }
579
580 int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
581 {
582     if (group->meth->group_check_discriminant == 0) {
583         ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT,
584               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
585         return 0;
586     }
587     return group->meth->group_check_discriminant(group, ctx);
588 }
589
590 int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
591 {
592     int r = 0;
593     BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
594 #ifndef FIPS_MODE
595     BN_CTX *ctx_new = NULL;
596
597     if (ctx == NULL)
598         ctx_new = ctx = BN_CTX_new();
599 #endif
600     if (ctx == NULL)
601         return -1;
602
603     /* compare the field types */
604     if (EC_METHOD_get_field_type(EC_GROUP_method_of(a)) !=
605         EC_METHOD_get_field_type(EC_GROUP_method_of(b)))
606         return 1;
607     /* compare the curve name (if present in both) */
608     if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
609         EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
610         return 1;
611     if (a->meth->flags & EC_FLAGS_CUSTOM_CURVE)
612         return 0;
613
614     BN_CTX_start(ctx);
615     a1 = BN_CTX_get(ctx);
616     a2 = BN_CTX_get(ctx);
617     a3 = BN_CTX_get(ctx);
618     b1 = BN_CTX_get(ctx);
619     b2 = BN_CTX_get(ctx);
620     b3 = BN_CTX_get(ctx);
621     if (b3 == NULL) {
622         BN_CTX_end(ctx);
623 #ifndef FIPS_MODE
624         BN_CTX_free(ctx_new);
625 #endif
626         return -1;
627     }
628
629     /*
630      * XXX This approach assumes that the external representation of curves
631      * over the same field type is the same.
632      */
633     if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
634         !b->meth->group_get_curve(b, b1, b2, b3, ctx))
635         r = 1;
636
637     /* return 1 if the curve parameters are different */
638     if (r || BN_cmp(a1, b1) != 0 || BN_cmp(a2, b2) != 0 || BN_cmp(a3, b3) != 0)
639         r = 1;
640
641     /* XXX EC_POINT_cmp() assumes that the methods are equal */
642     /* return 1 if the generators are different */
643     if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
644                           EC_GROUP_get0_generator(b), ctx) != 0)
645         r = 1;
646
647     if (!r) {
648         const BIGNUM *ao, *bo, *ac, *bc;
649         /* compare the orders */
650         ao = EC_GROUP_get0_order(a);
651         bo = EC_GROUP_get0_order(b);
652         if (ao == NULL || bo == NULL) {
653             /* return an error if either order is NULL */
654             r = -1;
655             goto end;
656         }
657         if (BN_cmp(ao, bo) != 0) {
658             /* return 1 if orders are different */
659             r = 1;
660             goto end;
661         }
662         /*
663          * It gets here if the curve parameters and generator matched.
664          * Now check the optional cofactors (if both are present).
665          */
666         ac = EC_GROUP_get0_cofactor(a);
667         bc = EC_GROUP_get0_cofactor(b);
668         /* Returns 1 (mismatch) if both cofactors are specified and different */
669         if (!BN_is_zero(ac) && !BN_is_zero(bc) && BN_cmp(ac, bc) != 0)
670             r = 1;
671         /* Returns 0 if the parameters matched */
672     }
673 end:
674     BN_CTX_end(ctx);
675 #ifndef FIPS_MODE
676     BN_CTX_free(ctx_new);
677 #endif
678     return r;
679 }
680
681 /* functions for EC_POINT objects */
682
683 EC_POINT *EC_POINT_new(const EC_GROUP *group)
684 {
685     EC_POINT *ret;
686
687     if (group == NULL) {
688         ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER);
689         return NULL;
690     }
691     if (group->meth->point_init == NULL) {
692         ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
693         return NULL;
694     }
695
696     ret = OPENSSL_zalloc(sizeof(*ret));
697     if (ret == NULL) {
698         ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE);
699         return NULL;
700     }
701
702     ret->meth = group->meth;
703     ret->curve_name = group->curve_name;
704
705     if (!ret->meth->point_init(ret)) {
706         OPENSSL_free(ret);
707         return NULL;
708     }
709
710     return ret;
711 }
712
713 void EC_POINT_free(EC_POINT *point)
714 {
715     if (point == NULL)
716         return;
717
718     if (point->meth->point_finish != 0)
719         point->meth->point_finish(point);
720     OPENSSL_free(point);
721 }
722
723 void EC_POINT_clear_free(EC_POINT *point)
724 {
725     if (point == NULL)
726         return;
727
728     if (point->meth->point_clear_finish != 0)
729         point->meth->point_clear_finish(point);
730     else if (point->meth->point_finish != 0)
731         point->meth->point_finish(point);
732     OPENSSL_clear_free(point, sizeof(*point));
733 }
734
735 int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)
736 {
737     if (dest->meth->point_copy == 0) {
738         ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
739         return 0;
740     }
741     if (dest->meth != src->meth
742             || (dest->curve_name != src->curve_name
743                  && dest->curve_name != 0
744                  && src->curve_name != 0)) {
745         ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS);
746         return 0;
747     }
748     if (dest == src)
749         return 1;
750     return dest->meth->point_copy(dest, src);
751 }
752
753 EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
754 {
755     EC_POINT *t;
756     int r;
757
758     if (a == NULL)
759         return NULL;
760
761     t = EC_POINT_new(group);
762     if (t == NULL)
763         return NULL;
764     r = EC_POINT_copy(t, a);
765     if (!r) {
766         EC_POINT_free(t);
767         return NULL;
768     }
769     return t;
770 }
771
772 const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
773 {
774     return point->meth;
775 }
776
777 int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
778 {
779     if (group->meth->point_set_to_infinity == 0) {
780         ECerr(EC_F_EC_POINT_SET_TO_INFINITY,
781               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
782         return 0;
783     }
784     if (group->meth != point->meth) {
785         ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
786         return 0;
787     }
788     return group->meth->point_set_to_infinity(group, point);
789 }
790
791 int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
792                                              EC_POINT *point, const BIGNUM *x,
793                                              const BIGNUM *y, const BIGNUM *z,
794                                              BN_CTX *ctx)
795 {
796     if (group->meth->point_set_Jprojective_coordinates_GFp == 0) {
797         ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
798               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
799         return 0;
800     }
801     if (!ec_point_is_compat(point, group)) {
802         ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
803               EC_R_INCOMPATIBLE_OBJECTS);
804         return 0;
805     }
806     return group->meth->point_set_Jprojective_coordinates_GFp(group, point, x,
807                                                               y, z, ctx);
808 }
809
810 int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
811                                              const EC_POINT *point, BIGNUM *x,
812                                              BIGNUM *y, BIGNUM *z,
813                                              BN_CTX *ctx)
814 {
815     if (group->meth->point_get_Jprojective_coordinates_GFp == 0) {
816         ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
817               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
818         return 0;
819     }
820     if (!ec_point_is_compat(point, group)) {
821         ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
822               EC_R_INCOMPATIBLE_OBJECTS);
823         return 0;
824     }
825     return group->meth->point_get_Jprojective_coordinates_GFp(group, point, x,
826                                                               y, z, ctx);
827 }
828
829 int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point,
830                                     const BIGNUM *x, const BIGNUM *y,
831                                     BN_CTX *ctx)
832 {
833     if (group->meth->point_set_affine_coordinates == NULL) {
834         ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES,
835               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
836         return 0;
837     }
838     if (!ec_point_is_compat(point, group)) {
839         ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, EC_R_INCOMPATIBLE_OBJECTS);
840         return 0;
841     }
842     if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx))
843         return 0;
844
845     if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {
846         ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, EC_R_POINT_IS_NOT_ON_CURVE);
847         return 0;
848     }
849     return 1;
850 }
851
852 #ifndef OPENSSL_NO_DEPRECATED_3_0
853 int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
854                                         EC_POINT *point, const BIGNUM *x,
855                                         const BIGNUM *y, BN_CTX *ctx)
856 {
857     return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
858 }
859
860 # ifndef OPENSSL_NO_EC2M
861 int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,
862                                          EC_POINT *point, const BIGNUM *x,
863                                          const BIGNUM *y, BN_CTX *ctx)
864 {
865     return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
866 }
867 # endif
868 #endif
869
870 int EC_POINT_get_affine_coordinates(const EC_GROUP *group,
871                                     const EC_POINT *point, BIGNUM *x, BIGNUM *y,
872                                     BN_CTX *ctx)
873 {
874     if (group->meth->point_get_affine_coordinates == NULL) {
875         ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES,
876               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
877         return 0;
878     }
879     if (!ec_point_is_compat(point, group)) {
880         ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, EC_R_INCOMPATIBLE_OBJECTS);
881         return 0;
882     }
883     if (EC_POINT_is_at_infinity(group, point)) {
884         ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, EC_R_POINT_AT_INFINITY);
885         return 0;
886     }
887     return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
888 }
889
890 #ifndef OPENSSL_NO_DEPRECATED_3_0
891 int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
892                                         const EC_POINT *point, BIGNUM *x,
893                                         BIGNUM *y, BN_CTX *ctx)
894 {
895     return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
896 }
897
898 # ifndef OPENSSL_NO_EC2M
899 int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,
900                                          const EC_POINT *point, BIGNUM *x,
901                                          BIGNUM *y, BN_CTX *ctx)
902 {
903     return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
904 }
905 # endif
906 #endif
907
908 int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
909                  const EC_POINT *b, BN_CTX *ctx)
910 {
911     if (group->meth->add == 0) {
912         ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
913         return 0;
914     }
915     if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)
916         || !ec_point_is_compat(b, group)) {
917         ECerr(EC_F_EC_POINT_ADD, EC_R_INCOMPATIBLE_OBJECTS);
918         return 0;
919     }
920     return group->meth->add(group, r, a, b, ctx);
921 }
922
923 int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
924                  BN_CTX *ctx)
925 {
926     if (group->meth->dbl == 0) {
927         ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
928         return 0;
929     }
930     if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)) {
931         ECerr(EC_F_EC_POINT_DBL, EC_R_INCOMPATIBLE_OBJECTS);
932         return 0;
933     }
934     return group->meth->dbl(group, r, a, ctx);
935 }
936
937 int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
938 {
939     if (group->meth->invert == 0) {
940         ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
941         return 0;
942     }
943     if (!ec_point_is_compat(a, group)) {
944         ECerr(EC_F_EC_POINT_INVERT, EC_R_INCOMPATIBLE_OBJECTS);
945         return 0;
946     }
947     return group->meth->invert(group, a, ctx);
948 }
949
950 int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
951 {
952     if (group->meth->is_at_infinity == 0) {
953         ECerr(EC_F_EC_POINT_IS_AT_INFINITY,
954               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
955         return 0;
956     }
957     if (!ec_point_is_compat(point, group)) {
958         ECerr(EC_F_EC_POINT_IS_AT_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
959         return 0;
960     }
961     return group->meth->is_at_infinity(group, point);
962 }
963
964 /*
965  * Check whether an EC_POINT is on the curve or not. Note that the return
966  * value for this function should NOT be treated as a boolean. Return values:
967  *  1: The point is on the curve
968  *  0: The point is not on the curve
969  * -1: An error occurred
970  */
971 int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
972                          BN_CTX *ctx)
973 {
974     if (group->meth->is_on_curve == 0) {
975         ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
976         return 0;
977     }
978     if (!ec_point_is_compat(point, group)) {
979         ECerr(EC_F_EC_POINT_IS_ON_CURVE, EC_R_INCOMPATIBLE_OBJECTS);
980         return 0;
981     }
982     return group->meth->is_on_curve(group, point, ctx);
983 }
984
985 int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
986                  BN_CTX *ctx)
987 {
988     if (group->meth->point_cmp == 0) {
989         ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
990         return -1;
991     }
992     if (!ec_point_is_compat(a, group) || !ec_point_is_compat(b, group)) {
993         ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS);
994         return -1;
995     }
996     return group->meth->point_cmp(group, a, b, ctx);
997 }
998
999 int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
1000 {
1001     if (group->meth->make_affine == 0) {
1002         ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1003         return 0;
1004     }
1005     if (!ec_point_is_compat(point, group)) {
1006         ECerr(EC_F_EC_POINT_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
1007         return 0;
1008     }
1009     return group->meth->make_affine(group, point, ctx);
1010 }
1011
1012 int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
1013                           EC_POINT *points[], BN_CTX *ctx)
1014 {
1015     size_t i;
1016
1017     if (group->meth->points_make_affine == 0) {
1018         ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1019         return 0;
1020     }
1021     for (i = 0; i < num; i++) {
1022         if (!ec_point_is_compat(points[i], group)) {
1023             ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
1024             return 0;
1025         }
1026     }
1027     return group->meth->points_make_affine(group, num, points, ctx);
1028 }
1029
1030 /*
1031  * Functions for point multiplication. If group->meth->mul is 0, we use the
1032  * wNAF-based implementations in ec_mult.c; otherwise we dispatch through
1033  * methods.
1034  */
1035
1036 int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
1037                   size_t num, const EC_POINT *points[],
1038                   const BIGNUM *scalars[], BN_CTX *ctx)
1039 {
1040     int ret = 0;
1041     size_t i = 0;
1042 #ifndef FIPS_MODE
1043     BN_CTX *new_ctx = NULL;
1044
1045     if (ctx == NULL)
1046         ctx = new_ctx = BN_CTX_secure_new();
1047 #endif
1048     if (ctx == NULL) {
1049         ECerr(EC_F_EC_POINTS_MUL, ERR_R_INTERNAL_ERROR);
1050         return 0;
1051     }
1052
1053     if ((scalar == NULL) && (num == 0)) {
1054         return EC_POINT_set_to_infinity(group, r);
1055     }
1056
1057     if (!ec_point_is_compat(r, group)) {
1058         ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
1059         return 0;
1060     }
1061     for (i = 0; i < num; i++) {
1062         if (!ec_point_is_compat(points[i], group)) {
1063             ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
1064             return 0;
1065         }
1066     }
1067
1068     if (group->meth->mul != NULL)
1069         ret = group->meth->mul(group, r, scalar, num, points, scalars, ctx);
1070     else
1071         /* use default */
1072         ret = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
1073
1074 #ifndef FIPS_MODE
1075     BN_CTX_free(new_ctx);
1076 #endif
1077     return ret;
1078 }
1079
1080 int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
1081                  const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
1082 {
1083     /* just a convenient interface to EC_POINTs_mul() */
1084
1085     const EC_POINT *points[1];
1086     const BIGNUM *scalars[1];
1087
1088     points[0] = point;
1089     scalars[0] = p_scalar;
1090
1091     return EC_POINTs_mul(group, r, g_scalar,
1092                          (point != NULL
1093                           && p_scalar != NULL), points, scalars, ctx);
1094 }
1095
1096 int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
1097 {
1098     if (group->meth->mul == 0)
1099         /* use default */
1100         return ec_wNAF_precompute_mult(group, ctx);
1101
1102     if (group->meth->precompute_mult != 0)
1103         return group->meth->precompute_mult(group, ctx);
1104     else
1105         return 1;               /* nothing to do, so report success */
1106 }
1107
1108 int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
1109 {
1110     if (group->meth->mul == 0)
1111         /* use default */
1112         return ec_wNAF_have_precompute_mult(group);
1113
1114     if (group->meth->have_precompute_mult != 0)
1115         return group->meth->have_precompute_mult(group);
1116     else
1117         return 0;               /* cannot tell whether precomputation has
1118                                  * been performed */
1119 }
1120
1121 /*
1122  * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
1123  * returns one on success. On error it returns zero.
1124  */
1125 static int ec_precompute_mont_data(EC_GROUP *group)
1126 {
1127     BN_CTX *ctx = BN_CTX_new_ex(group->libctx);
1128     int ret = 0;
1129
1130     BN_MONT_CTX_free(group->mont_data);
1131     group->mont_data = NULL;
1132
1133     if (ctx == NULL)
1134         goto err;
1135
1136     group->mont_data = BN_MONT_CTX_new();
1137     if (group->mont_data == NULL)
1138         goto err;
1139
1140     if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) {
1141         BN_MONT_CTX_free(group->mont_data);
1142         group->mont_data = NULL;
1143         goto err;
1144     }
1145
1146     ret = 1;
1147
1148  err:
1149
1150     BN_CTX_free(ctx);
1151     return ret;
1152 }
1153
1154 #ifndef FIPS_MODE
1155 int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg)
1156 {
1157     return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
1158 }
1159
1160 void *EC_KEY_get_ex_data(const EC_KEY *key, int idx)
1161 {
1162     return CRYPTO_get_ex_data(&key->ex_data, idx);
1163 }
1164 #endif
1165
1166 int ec_group_simple_order_bits(const EC_GROUP *group)
1167 {
1168     if (group->order == NULL)
1169         return 0;
1170     return BN_num_bits(group->order);
1171 }
1172
1173 static int ec_field_inverse_mod_ord(const EC_GROUP *group, BIGNUM *r,
1174                                     const BIGNUM *x, BN_CTX *ctx)
1175 {
1176     BIGNUM *e = NULL;
1177     int ret = 0;
1178 #ifndef FIPS_MODE
1179     BN_CTX *new_ctx = NULL;
1180
1181     if (ctx == NULL)
1182         ctx = new_ctx = BN_CTX_secure_new();
1183 #endif
1184     if (ctx == NULL)
1185         return 0;
1186
1187     if (group->mont_data == NULL)
1188         goto err;
1189
1190     BN_CTX_start(ctx);
1191     if ((e = BN_CTX_get(ctx)) == NULL)
1192         goto err;
1193
1194     /*-
1195      * We want inverse in constant time, therefore we utilize the fact
1196      * order must be prime and use Fermats Little Theorem instead.
1197      */
1198     if (!BN_set_word(e, 2))
1199         goto err;
1200     if (!BN_sub(e, group->order, e))
1201         goto err;
1202     /*-
1203      * Exponent e is public.
1204      * No need for scatter-gather or BN_FLG_CONSTTIME.
1205      */
1206     if (!BN_mod_exp_mont(r, x, e, group->order, ctx, group->mont_data))
1207         goto err;
1208
1209     ret = 1;
1210
1211  err:
1212     BN_CTX_end(ctx);
1213 #ifndef FIPS_MODE
1214     BN_CTX_free(new_ctx);
1215 #endif
1216     return ret;
1217 }
1218
1219 /*-
1220  * Default behavior, if group->meth->field_inverse_mod_ord is NULL:
1221  * - When group->order is even, this function returns an error.
1222  * - When group->order is otherwise composite, the correctness
1223  *   of the output is not guaranteed.
1224  * - When x is outside the range [1, group->order), the correctness
1225  *   of the output is not guaranteed.
1226  * - Otherwise, this function returns the multiplicative inverse in the
1227  *   range [1, group->order).
1228  *
1229  * EC_METHODs must implement their own field_inverse_mod_ord for
1230  * other functionality.
1231  */
1232 int ec_group_do_inverse_ord(const EC_GROUP *group, BIGNUM *res,
1233                             const BIGNUM *x, BN_CTX *ctx)
1234 {
1235     if (group->meth->field_inverse_mod_ord != NULL)
1236         return group->meth->field_inverse_mod_ord(group, res, x, ctx);
1237     else
1238         return ec_field_inverse_mod_ord(group, res, x, ctx);
1239 }
1240
1241 /*-
1242  * Coordinate blinding for EC_POINT.
1243  *
1244  * The underlying EC_METHOD can optionally implement this function:
1245  * underlying implementations should return 0 on errors, or 1 on
1246  * success.
1247  *
1248  * This wrapper returns 1 in case the underlying EC_METHOD does not
1249  * support coordinate blinding.
1250  */
1251 int ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p, BN_CTX *ctx)
1252 {
1253     if (group->meth->blind_coordinates == NULL)
1254         return 1; /* ignore if not implemented */
1255
1256     return group->meth->blind_coordinates(group, p, ctx);
1257 }