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