Add error code for unsupported explicit parameters
[openssl.git] / crypto / ec / ec_lib.c
1 /*
2  * Copyright 2001-2022 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  * EC_GROUP 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 #include <openssl/params.h>
19 #include <openssl/core_names.h>
20 #include <openssl/err.h>
21 #include <openssl/opensslv.h>
22 #include "crypto/ec.h"
23 #include "internal/nelem.h"
24 #include "ec_local.h"
25 #include "e_os.h" /* strcasecmp */
26
27 /* functions for EC_GROUP objects */
28
29 EC_GROUP *ossl_ec_group_new_ex(OSSL_LIB_CTX *libctx, const char *propq,
30                                const EC_METHOD *meth)
31 {
32     EC_GROUP *ret;
33
34     if (meth == NULL) {
35         ERR_raise(ERR_LIB_EC, EC_R_SLOT_FULL);
36         return NULL;
37     }
38     if (meth->group_init == 0) {
39         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
40         return NULL;
41     }
42
43     ret = OPENSSL_zalloc(sizeof(*ret));
44     if (ret == NULL) {
45         ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
46         return NULL;
47     }
48
49     ret->libctx = libctx;
50     if (propq != NULL) {
51         ret->propq = OPENSSL_strdup(propq);
52         if (ret->propq == NULL) {
53             ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
54             goto err;
55         }
56     }
57     ret->meth = meth;
58     if ((ret->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
59         ret->order = BN_new();
60         if (ret->order == NULL)
61             goto err;
62         ret->cofactor = BN_new();
63         if (ret->cofactor == NULL)
64             goto err;
65     }
66     ret->asn1_flag = OPENSSL_EC_EXPLICIT_CURVE;
67     ret->asn1_form = POINT_CONVERSION_UNCOMPRESSED;
68     if (!meth->group_init(ret))
69         goto err;
70     return ret;
71
72  err:
73     BN_free(ret->order);
74     BN_free(ret->cofactor);
75     OPENSSL_free(ret->propq);
76     OPENSSL_free(ret);
77     return NULL;
78 }
79
80 #ifndef OPENSSL_NO_DEPRECATED_3_0
81 # ifndef FIPS_MODULE
82 EC_GROUP *EC_GROUP_new(const EC_METHOD *meth)
83 {
84     return ossl_ec_group_new_ex(NULL, NULL, meth);
85 }
86 # endif
87 #endif
88
89 void EC_pre_comp_free(EC_GROUP *group)
90 {
91     switch (group->pre_comp_type) {
92     case PCT_none:
93         break;
94     case PCT_nistz256:
95 #ifdef ECP_NISTZ256_ASM
96         EC_nistz256_pre_comp_free(group->pre_comp.nistz256);
97 #endif
98         break;
99 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
100     case PCT_nistp224:
101         EC_nistp224_pre_comp_free(group->pre_comp.nistp224);
102         break;
103     case PCT_nistp256:
104         EC_nistp256_pre_comp_free(group->pre_comp.nistp256);
105         break;
106     case PCT_nistp521:
107         EC_nistp521_pre_comp_free(group->pre_comp.nistp521);
108         break;
109 #else
110     case PCT_nistp224:
111     case PCT_nistp256:
112     case PCT_nistp521:
113         break;
114 #endif
115     case PCT_ec:
116         EC_ec_pre_comp_free(group->pre_comp.ec);
117         break;
118     }
119     group->pre_comp.ec = NULL;
120 }
121
122 void EC_GROUP_free(EC_GROUP *group)
123 {
124     if (!group)
125         return;
126
127     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_free(group->generator);
133     BN_free(group->order);
134     BN_free(group->cofactor);
135     OPENSSL_free(group->seed);
136     OPENSSL_free(group->propq);
137     OPENSSL_free(group);
138 }
139
140 #ifndef OPENSSL_NO_DEPRECATED_3_0
141 void EC_GROUP_clear_free(EC_GROUP *group)
142 {
143     if (!group)
144         return;
145
146     if (group->meth->group_clear_finish != 0)
147         group->meth->group_clear_finish(group);
148     else if (group->meth->group_finish != 0)
149         group->meth->group_finish(group);
150
151     EC_pre_comp_free(group);
152     BN_MONT_CTX_free(group->mont_data);
153     EC_POINT_clear_free(group->generator);
154     BN_clear_free(group->order);
155     BN_clear_free(group->cofactor);
156     OPENSSL_clear_free(group->seed, group->seed_len);
157     OPENSSL_clear_free(group, sizeof(*group));
158 }
159 #endif
160
161 int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)
162 {
163     if (dest->meth->group_copy == 0) {
164         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
165         return 0;
166     }
167     if (dest->meth != src->meth) {
168         ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
169         return 0;
170     }
171     if (dest == src)
172         return 1;
173
174     dest->libctx = src->libctx;
175     dest->curve_name = src->curve_name;
176
177     /* Copy precomputed */
178     dest->pre_comp_type = src->pre_comp_type;
179     switch (src->pre_comp_type) {
180     case PCT_none:
181         dest->pre_comp.ec = NULL;
182         break;
183     case PCT_nistz256:
184 #ifdef ECP_NISTZ256_ASM
185         dest->pre_comp.nistz256 = EC_nistz256_pre_comp_dup(src->pre_comp.nistz256);
186 #endif
187         break;
188 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
189     case PCT_nistp224:
190         dest->pre_comp.nistp224 = EC_nistp224_pre_comp_dup(src->pre_comp.nistp224);
191         break;
192     case PCT_nistp256:
193         dest->pre_comp.nistp256 = EC_nistp256_pre_comp_dup(src->pre_comp.nistp256);
194         break;
195     case PCT_nistp521:
196         dest->pre_comp.nistp521 = EC_nistp521_pre_comp_dup(src->pre_comp.nistp521);
197         break;
198 #else
199     case PCT_nistp224:
200     case PCT_nistp256:
201     case PCT_nistp521:
202         break;
203 #endif
204     case PCT_ec:
205         dest->pre_comp.ec = EC_ec_pre_comp_dup(src->pre_comp.ec);
206         break;
207     }
208
209     if (src->mont_data != NULL) {
210         if (dest->mont_data == NULL) {
211             dest->mont_data = BN_MONT_CTX_new();
212             if (dest->mont_data == NULL)
213                 return 0;
214         }
215         if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data))
216             return 0;
217     } else {
218         /* src->generator == NULL */
219         BN_MONT_CTX_free(dest->mont_data);
220         dest->mont_data = NULL;
221     }
222
223     if (src->generator != NULL) {
224         if (dest->generator == NULL) {
225             dest->generator = EC_POINT_new(dest);
226             if (dest->generator == NULL)
227                 return 0;
228         }
229         if (!EC_POINT_copy(dest->generator, src->generator))
230             return 0;
231     } else {
232         /* src->generator == NULL */
233         EC_POINT_clear_free(dest->generator);
234         dest->generator = NULL;
235     }
236
237     if ((src->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
238         if (!BN_copy(dest->order, src->order))
239             return 0;
240         if (!BN_copy(dest->cofactor, src->cofactor))
241             return 0;
242     }
243
244     dest->asn1_flag = src->asn1_flag;
245     dest->asn1_form = src->asn1_form;
246     dest->decoded_from_explicit_params = src->decoded_from_explicit_params;
247
248     if (src->seed) {
249         OPENSSL_free(dest->seed);
250         if ((dest->seed = OPENSSL_malloc(src->seed_len)) == NULL) {
251             ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
252             return 0;
253         }
254         if (!memcpy(dest->seed, src->seed, src->seed_len))
255             return 0;
256         dest->seed_len = src->seed_len;
257     } else {
258         OPENSSL_free(dest->seed);
259         dest->seed = NULL;
260         dest->seed_len = 0;
261     }
262
263     return dest->meth->group_copy(dest, src);
264 }
265
266 EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)
267 {
268     EC_GROUP *t = NULL;
269     int ok = 0;
270
271     if (a == NULL)
272         return NULL;
273
274     if ((t = ossl_ec_group_new_ex(a->libctx, a->propq, a->meth)) == NULL)
275         return NULL;
276     if (!EC_GROUP_copy(t, a))
277         goto err;
278
279     ok = 1;
280
281  err:
282     if (!ok) {
283         EC_GROUP_free(t);
284         return NULL;
285     }
286         return t;
287 }
288
289 #ifndef OPENSSL_NO_DEPRECATED_3_0
290 const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group)
291 {
292     return group->meth;
293 }
294
295 int EC_METHOD_get_field_type(const EC_METHOD *meth)
296 {
297     return meth->field_type;
298 }
299 #endif
300
301 static int ec_precompute_mont_data(EC_GROUP *);
302
303 /*-
304  * Try computing cofactor from the generator order (n) and field cardinality (q).
305  * This works for all curves of cryptographic interest.
306  *
307  * Hasse thm: q + 1 - 2*sqrt(q) <= n*h <= q + 1 + 2*sqrt(q)
308  * h_min = (q + 1 - 2*sqrt(q))/n
309  * h_max = (q + 1 + 2*sqrt(q))/n
310  * h_max - h_min = 4*sqrt(q)/n
311  * So if n > 4*sqrt(q) holds, there is only one possible value for h:
312  * h = \lfloor (h_min + h_max)/2 \rceil = \lfloor (q + 1)/n \rceil
313  *
314  * Otherwise, zero cofactor and return success.
315  */
316 static int ec_guess_cofactor(EC_GROUP *group) {
317     int ret = 0;
318     BN_CTX *ctx = NULL;
319     BIGNUM *q = NULL;
320
321     /*-
322      * If the cofactor is too large, we cannot guess it.
323      * The RHS of below is a strict overestimate of lg(4 * sqrt(q))
324      */
325     if (BN_num_bits(group->order) <= (BN_num_bits(group->field) + 1) / 2 + 3) {
326         /* default to 0 */
327         BN_zero(group->cofactor);
328         /* return success */
329         return 1;
330     }
331
332     if ((ctx = BN_CTX_new_ex(group->libctx)) == NULL)
333         return 0;
334
335     BN_CTX_start(ctx);
336     if ((q = BN_CTX_get(ctx)) == NULL)
337         goto err;
338
339     /* set q = 2**m for binary fields; q = p otherwise */
340     if (group->meth->field_type == NID_X9_62_characteristic_two_field) {
341         BN_zero(q);
342         if (!BN_set_bit(q, BN_num_bits(group->field) - 1))
343             goto err;
344     } else {
345         if (!BN_copy(q, group->field))
346             goto err;
347     }
348
349     /* compute h = \lfloor (q + 1)/n \rceil = \lfloor (q + 1 + n/2)/n \rfloor */
350     if (!BN_rshift1(group->cofactor, group->order) /* n/2 */
351         || !BN_add(group->cofactor, group->cofactor, q) /* q + n/2 */
352         /* q + 1 + n/2 */
353         || !BN_add(group->cofactor, group->cofactor, BN_value_one())
354         /* (q + 1 + n/2)/n */
355         || !BN_div(group->cofactor, NULL, group->cofactor, group->order, ctx))
356         goto err;
357     ret = 1;
358  err:
359     BN_CTX_end(ctx);
360     BN_CTX_free(ctx);
361     return ret;
362 }
363
364 int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
365                            const BIGNUM *order, const BIGNUM *cofactor)
366 {
367     if (generator == NULL) {
368         ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
369         return 0;
370     }
371
372     /* require group->field >= 1 */
373     if (group->field == NULL || BN_is_zero(group->field)
374         || BN_is_negative(group->field)) {
375         ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD);
376         return 0;
377     }
378
379     /*-
380      * - require order >= 1
381      * - enforce upper bound due to Hasse thm: order can be no more than one bit
382      *   longer than field cardinality
383      */
384     if (order == NULL || BN_is_zero(order) || BN_is_negative(order)
385         || BN_num_bits(order) > BN_num_bits(group->field) + 1) {
386         ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER);
387         return 0;
388     }
389
390     /*-
391      * Unfortunately the cofactor is an optional field in many standards.
392      * Internally, the lib uses 0 cofactor as a marker for "unknown cofactor".
393      * So accept cofactor == NULL or cofactor >= 0.
394      */
395     if (cofactor != NULL && BN_is_negative(cofactor)) {
396         ERR_raise(ERR_LIB_EC, EC_R_UNKNOWN_COFACTOR);
397         return 0;
398     }
399
400     if (group->generator == NULL) {
401         group->generator = EC_POINT_new(group);
402         if (group->generator == NULL)
403             return 0;
404     }
405     if (!EC_POINT_copy(group->generator, generator))
406         return 0;
407
408     if (!BN_copy(group->order, order))
409         return 0;
410
411     /* Either take the provided positive cofactor, or try to compute it */
412     if (cofactor != NULL && !BN_is_zero(cofactor)) {
413         if (!BN_copy(group->cofactor, cofactor))
414             return 0;
415     } else if (!ec_guess_cofactor(group)) {
416         BN_zero(group->cofactor);
417         return 0;
418     }
419
420     /*
421      * Some groups have an order with
422      * factors of two, which makes the Montgomery setup fail.
423      * |group->mont_data| will be NULL in this case.
424      */
425     if (BN_is_odd(group->order)) {
426         return ec_precompute_mont_data(group);
427     }
428
429     BN_MONT_CTX_free(group->mont_data);
430     group->mont_data = NULL;
431     return 1;
432 }
433
434 const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
435 {
436     return group->generator;
437 }
438
439 BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group)
440 {
441     return group->mont_data;
442 }
443
444 int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
445 {
446     if (group->order == NULL)
447         return 0;
448     if (!BN_copy(order, group->order))
449         return 0;
450
451     return !BN_is_zero(order);
452 }
453
454 const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group)
455 {
456     return group->order;
457 }
458
459 int EC_GROUP_order_bits(const EC_GROUP *group)
460 {
461     return group->meth->group_order_bits(group);
462 }
463
464 int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
465                           BN_CTX *ctx)
466 {
467
468     if (group->cofactor == NULL)
469         return 0;
470     if (!BN_copy(cofactor, group->cofactor))
471         return 0;
472
473     return !BN_is_zero(group->cofactor);
474 }
475
476 const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group)
477 {
478     return group->cofactor;
479 }
480
481 void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)
482 {
483     group->curve_name = nid;
484     group->asn1_flag =
485         (nid != NID_undef)
486         ? OPENSSL_EC_NAMED_CURVE
487         : OPENSSL_EC_EXPLICIT_CURVE;
488 }
489
490 int EC_GROUP_get_curve_name(const EC_GROUP *group)
491 {
492     return group->curve_name;
493 }
494
495 const BIGNUM *EC_GROUP_get0_field(const EC_GROUP *group)
496 {
497     return group->field;
498 }
499
500 int EC_GROUP_get_field_type(const EC_GROUP *group)
501 {
502     return group->meth->field_type;
503 }
504
505 void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
506 {
507     group->asn1_flag = flag;
508 }
509
510 int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
511 {
512     return group->asn1_flag;
513 }
514
515 void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
516                                         point_conversion_form_t form)
517 {
518     group->asn1_form = form;
519 }
520
521 point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP
522                                                            *group)
523 {
524     return group->asn1_form;
525 }
526
527 size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
528 {
529     OPENSSL_free(group->seed);
530     group->seed = NULL;
531     group->seed_len = 0;
532
533     if (!len || !p)
534         return 1;
535
536     if ((group->seed = OPENSSL_malloc(len)) == NULL) {
537         ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
538         return 0;
539     }
540     memcpy(group->seed, p, len);
541     group->seed_len = len;
542
543     return len;
544 }
545
546 unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
547 {
548     return group->seed;
549 }
550
551 size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
552 {
553     return group->seed_len;
554 }
555
556 int EC_GROUP_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
557                        const BIGNUM *b, BN_CTX *ctx)
558 {
559     if (group->meth->group_set_curve == 0) {
560         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
561         return 0;
562     }
563     return group->meth->group_set_curve(group, p, a, b, ctx);
564 }
565
566 int EC_GROUP_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b,
567                        BN_CTX *ctx)
568 {
569     if (group->meth->group_get_curve == NULL) {
570         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
571         return 0;
572     }
573     return group->meth->group_get_curve(group, p, a, b, ctx);
574 }
575
576 #ifndef OPENSSL_NO_DEPRECATED_3_0
577 int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
578                            const BIGNUM *b, BN_CTX *ctx)
579 {
580     return EC_GROUP_set_curve(group, p, a, b, ctx);
581 }
582
583 int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
584                            BIGNUM *b, BN_CTX *ctx)
585 {
586     return EC_GROUP_get_curve(group, p, a, b, ctx);
587 }
588
589 # ifndef OPENSSL_NO_EC2M
590 int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
591                             const BIGNUM *b, BN_CTX *ctx)
592 {
593     return EC_GROUP_set_curve(group, p, a, b, ctx);
594 }
595
596 int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
597                             BIGNUM *b, BN_CTX *ctx)
598 {
599     return EC_GROUP_get_curve(group, p, a, b, ctx);
600 }
601 # endif
602 #endif
603
604 int EC_GROUP_get_degree(const EC_GROUP *group)
605 {
606     if (group->meth->group_get_degree == 0) {
607         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
608         return 0;
609     }
610     return group->meth->group_get_degree(group);
611 }
612
613 int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
614 {
615     if (group->meth->group_check_discriminant == 0) {
616         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
617         return 0;
618     }
619     return group->meth->group_check_discriminant(group, ctx);
620 }
621
622 int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
623 {
624     int r = 0;
625     BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
626 #ifndef FIPS_MODULE
627     BN_CTX *ctx_new = NULL;
628 #endif
629
630     /* compare the field types */
631     if (EC_GROUP_get_field_type(a) != EC_GROUP_get_field_type(b))
632         return 1;
633     /* compare the curve name (if present in both) */
634     if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
635         EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
636         return 1;
637     if (a->meth->flags & EC_FLAGS_CUSTOM_CURVE)
638         return 0;
639
640 #ifndef FIPS_MODULE
641     if (ctx == NULL)
642         ctx_new = ctx = BN_CTX_new();
643 #endif
644     if (ctx == NULL)
645         return -1;
646
647     BN_CTX_start(ctx);
648     a1 = BN_CTX_get(ctx);
649     a2 = BN_CTX_get(ctx);
650     a3 = BN_CTX_get(ctx);
651     b1 = BN_CTX_get(ctx);
652     b2 = BN_CTX_get(ctx);
653     b3 = BN_CTX_get(ctx);
654     if (b3 == NULL) {
655         BN_CTX_end(ctx);
656 #ifndef FIPS_MODULE
657         BN_CTX_free(ctx_new);
658 #endif
659         return -1;
660     }
661
662     /*
663      * XXX This approach assumes that the external representation of curves
664      * over the same field type is the same.
665      */
666     if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
667         !b->meth->group_get_curve(b, b1, b2, b3, ctx))
668         r = 1;
669
670     /* return 1 if the curve parameters are different */
671     if (r || BN_cmp(a1, b1) != 0 || BN_cmp(a2, b2) != 0 || BN_cmp(a3, b3) != 0)
672         r = 1;
673
674     /* XXX EC_POINT_cmp() assumes that the methods are equal */
675     /* return 1 if the generators are different */
676     if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
677                           EC_GROUP_get0_generator(b), ctx) != 0)
678         r = 1;
679
680     if (!r) {
681         const BIGNUM *ao, *bo, *ac, *bc;
682         /* compare the orders */
683         ao = EC_GROUP_get0_order(a);
684         bo = EC_GROUP_get0_order(b);
685         if (ao == NULL || bo == NULL) {
686             /* return an error if either order is NULL */
687             r = -1;
688             goto end;
689         }
690         if (BN_cmp(ao, bo) != 0) {
691             /* return 1 if orders are different */
692             r = 1;
693             goto end;
694         }
695         /*
696          * It gets here if the curve parameters and generator matched.
697          * Now check the optional cofactors (if both are present).
698          */
699         ac = EC_GROUP_get0_cofactor(a);
700         bc = EC_GROUP_get0_cofactor(b);
701         /* Returns 1 (mismatch) if both cofactors are specified and different */
702         if (!BN_is_zero(ac) && !BN_is_zero(bc) && BN_cmp(ac, bc) != 0)
703             r = 1;
704         /* Returns 0 if the parameters matched */
705     }
706 end:
707     BN_CTX_end(ctx);
708 #ifndef FIPS_MODULE
709     BN_CTX_free(ctx_new);
710 #endif
711     return r;
712 }
713
714 /* functions for EC_POINT objects */
715
716 EC_POINT *EC_POINT_new(const EC_GROUP *group)
717 {
718     EC_POINT *ret;
719
720     if (group == NULL) {
721         ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
722         return NULL;
723     }
724     if (group->meth->point_init == NULL) {
725         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
726         return NULL;
727     }
728
729     ret = OPENSSL_zalloc(sizeof(*ret));
730     if (ret == NULL) {
731         ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
732         return NULL;
733     }
734
735     ret->meth = group->meth;
736     ret->curve_name = group->curve_name;
737
738     if (!ret->meth->point_init(ret)) {
739         OPENSSL_free(ret);
740         return NULL;
741     }
742
743     return ret;
744 }
745
746 void EC_POINT_free(EC_POINT *point)
747 {
748     if (point == NULL)
749         return;
750
751     if (point->meth->point_finish != 0)
752         point->meth->point_finish(point);
753     OPENSSL_free(point);
754 }
755
756 void EC_POINT_clear_free(EC_POINT *point)
757 {
758     if (point == NULL)
759         return;
760
761     if (point->meth->point_clear_finish != 0)
762         point->meth->point_clear_finish(point);
763     else if (point->meth->point_finish != 0)
764         point->meth->point_finish(point);
765     OPENSSL_clear_free(point, sizeof(*point));
766 }
767
768 int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)
769 {
770     if (dest->meth->point_copy == 0) {
771         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
772         return 0;
773     }
774     if (dest->meth != src->meth
775             || (dest->curve_name != src->curve_name
776                  && dest->curve_name != 0
777                  && src->curve_name != 0)) {
778         ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
779         return 0;
780     }
781     if (dest == src)
782         return 1;
783     return dest->meth->point_copy(dest, src);
784 }
785
786 EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
787 {
788     EC_POINT *t;
789     int r;
790
791     if (a == NULL)
792         return NULL;
793
794     t = EC_POINT_new(group);
795     if (t == NULL)
796         return NULL;
797     r = EC_POINT_copy(t, a);
798     if (!r) {
799         EC_POINT_free(t);
800         return NULL;
801     }
802     return t;
803 }
804
805 #ifndef OPENSSL_NO_DEPRECATED_3_0
806 const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
807 {
808     return point->meth;
809 }
810 #endif
811
812 int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
813 {
814     if (group->meth->point_set_to_infinity == 0) {
815         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
816         return 0;
817     }
818     if (group->meth != point->meth) {
819         ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
820         return 0;
821     }
822     return group->meth->point_set_to_infinity(group, point);
823 }
824
825 #ifndef OPENSSL_NO_DEPRECATED_3_0
826 int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
827                                              EC_POINT *point, const BIGNUM *x,
828                                              const BIGNUM *y, const BIGNUM *z,
829                                              BN_CTX *ctx)
830 {
831     if (group->meth->field_type != NID_X9_62_prime_field) {
832         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
833         return 0;
834     }
835     if (!ec_point_is_compat(point, group)) {
836         ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
837         return 0;
838     }
839     return ossl_ec_GFp_simple_set_Jprojective_coordinates_GFp(group, point,
840                                                               x, y, z, ctx);
841 }
842
843 int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
844                                              const EC_POINT *point, BIGNUM *x,
845                                              BIGNUM *y, BIGNUM *z,
846                                              BN_CTX *ctx)
847 {
848     if (group->meth->field_type != NID_X9_62_prime_field) {
849         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
850         return 0;
851     }
852     if (!ec_point_is_compat(point, group)) {
853         ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
854         return 0;
855     }
856     return ossl_ec_GFp_simple_get_Jprojective_coordinates_GFp(group, point,
857                                                               x, y, z, ctx);
858 }
859 #endif
860
861 int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point,
862                                     const BIGNUM *x, const BIGNUM *y,
863                                     BN_CTX *ctx)
864 {
865     if (group->meth->point_set_affine_coordinates == NULL) {
866         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
867         return 0;
868     }
869     if (!ec_point_is_compat(point, group)) {
870         ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
871         return 0;
872     }
873     if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx))
874         return 0;
875
876     if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {
877         ERR_raise(ERR_LIB_EC, EC_R_POINT_IS_NOT_ON_CURVE);
878         return 0;
879     }
880     return 1;
881 }
882
883 #ifndef OPENSSL_NO_DEPRECATED_3_0
884 int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
885                                         EC_POINT *point, const BIGNUM *x,
886                                         const BIGNUM *y, BN_CTX *ctx)
887 {
888     return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
889 }
890
891 # ifndef OPENSSL_NO_EC2M
892 int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,
893                                          EC_POINT *point, const BIGNUM *x,
894                                          const BIGNUM *y, BN_CTX *ctx)
895 {
896     return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
897 }
898 # endif
899 #endif
900
901 int EC_POINT_get_affine_coordinates(const EC_GROUP *group,
902                                     const EC_POINT *point, BIGNUM *x, BIGNUM *y,
903                                     BN_CTX *ctx)
904 {
905     if (group->meth->point_get_affine_coordinates == NULL) {
906         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
907         return 0;
908     }
909     if (!ec_point_is_compat(point, group)) {
910         ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
911         return 0;
912     }
913     if (EC_POINT_is_at_infinity(group, point)) {
914         ERR_raise(ERR_LIB_EC, EC_R_POINT_AT_INFINITY);
915         return 0;
916     }
917     return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
918 }
919
920 #ifndef OPENSSL_NO_DEPRECATED_3_0
921 int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
922                                         const EC_POINT *point, BIGNUM *x,
923                                         BIGNUM *y, BN_CTX *ctx)
924 {
925     return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
926 }
927
928 # ifndef OPENSSL_NO_EC2M
929 int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,
930                                          const EC_POINT *point, BIGNUM *x,
931                                          BIGNUM *y, BN_CTX *ctx)
932 {
933     return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
934 }
935 # endif
936 #endif
937
938 int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
939                  const EC_POINT *b, BN_CTX *ctx)
940 {
941     if (group->meth->add == 0) {
942         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
943         return 0;
944     }
945     if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)
946         || !ec_point_is_compat(b, group)) {
947         ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
948         return 0;
949     }
950     return group->meth->add(group, r, a, b, ctx);
951 }
952
953 int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
954                  BN_CTX *ctx)
955 {
956     if (group->meth->dbl == 0) {
957         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
958         return 0;
959     }
960     if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)) {
961         ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
962         return 0;
963     }
964     return group->meth->dbl(group, r, a, ctx);
965 }
966
967 int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
968 {
969     if (group->meth->invert == 0) {
970         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
971         return 0;
972     }
973     if (!ec_point_is_compat(a, group)) {
974         ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
975         return 0;
976     }
977     return group->meth->invert(group, a, ctx);
978 }
979
980 int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
981 {
982     if (group->meth->is_at_infinity == 0) {
983         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
984         return 0;
985     }
986     if (!ec_point_is_compat(point, group)) {
987         ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
988         return 0;
989     }
990     return group->meth->is_at_infinity(group, point);
991 }
992
993 /*
994  * Check whether an EC_POINT is on the curve or not. Note that the return
995  * value for this function should NOT be treated as a boolean. Return values:
996  *  1: The point is on the curve
997  *  0: The point is not on the curve
998  * -1: An error occurred
999  */
1000 int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
1001                          BN_CTX *ctx)
1002 {
1003     if (group->meth->is_on_curve == 0) {
1004         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1005         return 0;
1006     }
1007     if (!ec_point_is_compat(point, group)) {
1008         ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
1009         return 0;
1010     }
1011     return group->meth->is_on_curve(group, point, ctx);
1012 }
1013
1014 int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
1015                  BN_CTX *ctx)
1016 {
1017     if (group->meth->point_cmp == 0) {
1018         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1019         return -1;
1020     }
1021     if (!ec_point_is_compat(a, group) || !ec_point_is_compat(b, group)) {
1022         ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
1023         return -1;
1024     }
1025     return group->meth->point_cmp(group, a, b, ctx);
1026 }
1027
1028 #ifndef OPENSSL_NO_DEPRECATED_3_0
1029 int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
1030 {
1031     if (group->meth->make_affine == 0) {
1032         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1033         return 0;
1034     }
1035     if (!ec_point_is_compat(point, group)) {
1036         ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
1037         return 0;
1038     }
1039     return group->meth->make_affine(group, point, ctx);
1040 }
1041
1042 int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
1043                           EC_POINT *points[], BN_CTX *ctx)
1044 {
1045     size_t i;
1046
1047     if (group->meth->points_make_affine == 0) {
1048         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1049         return 0;
1050     }
1051     for (i = 0; i < num; i++) {
1052         if (!ec_point_is_compat(points[i], group)) {
1053             ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
1054             return 0;
1055         }
1056     }
1057     return group->meth->points_make_affine(group, num, points, ctx);
1058 }
1059 #endif
1060
1061 /*
1062  * Functions for point multiplication. If group->meth->mul is 0, we use the
1063  * wNAF-based implementations in ec_mult.c; otherwise we dispatch through
1064  * methods.
1065  */
1066
1067 #ifndef OPENSSL_NO_DEPRECATED_3_0
1068 int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
1069                   size_t num, const EC_POINT *points[],
1070                   const BIGNUM *scalars[], BN_CTX *ctx)
1071 {
1072     int ret = 0;
1073     size_t i = 0;
1074 #ifndef FIPS_MODULE
1075     BN_CTX *new_ctx = NULL;
1076 #endif
1077
1078     if (!ec_point_is_compat(r, group)) {
1079         ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
1080         return 0;
1081     }
1082
1083     if (scalar == NULL && num == 0)
1084         return EC_POINT_set_to_infinity(group, r);
1085
1086     for (i = 0; i < num; i++) {
1087         if (!ec_point_is_compat(points[i], group)) {
1088             ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
1089             return 0;
1090         }
1091     }
1092
1093 #ifndef FIPS_MODULE
1094     if (ctx == NULL)
1095         ctx = new_ctx = BN_CTX_secure_new();
1096 #endif
1097     if (ctx == NULL) {
1098         ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
1099         return 0;
1100     }
1101
1102     if (group->meth->mul != NULL)
1103         ret = group->meth->mul(group, r, scalar, num, points, scalars, ctx);
1104     else
1105         /* use default */
1106         ret = ossl_ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
1107
1108 #ifndef FIPS_MODULE
1109     BN_CTX_free(new_ctx);
1110 #endif
1111     return ret;
1112 }
1113 #endif
1114
1115 int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
1116                  const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
1117 {
1118     int ret = 0;
1119     size_t num;
1120 #ifndef FIPS_MODULE
1121     BN_CTX *new_ctx = NULL;
1122 #endif
1123
1124     if (!ec_point_is_compat(r, group)
1125         || (point != NULL && !ec_point_is_compat(point, group))) {
1126         ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
1127         return 0;
1128     }
1129
1130     if (g_scalar == NULL && p_scalar == NULL)
1131         return EC_POINT_set_to_infinity(group, r);
1132
1133 #ifndef FIPS_MODULE
1134     if (ctx == NULL)
1135         ctx = new_ctx = BN_CTX_secure_new();
1136 #endif
1137     if (ctx == NULL) {
1138         ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
1139         return 0;
1140     }
1141
1142     num = (point != NULL && p_scalar != NULL) ? 1 : 0;
1143     if (group->meth->mul != NULL)
1144         ret = group->meth->mul(group, r, g_scalar, num, &point, &p_scalar, ctx);
1145     else
1146         /* use default */
1147         ret = ossl_ec_wNAF_mul(group, r, g_scalar, num, &point, &p_scalar, ctx);
1148
1149 #ifndef FIPS_MODULE
1150     BN_CTX_free(new_ctx);
1151 #endif
1152     return ret;
1153 }
1154
1155 #ifndef OPENSSL_NO_DEPRECATED_3_0
1156 int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
1157 {
1158     if (group->meth->mul == 0)
1159         /* use default */
1160         return ossl_ec_wNAF_precompute_mult(group, ctx);
1161
1162     if (group->meth->precompute_mult != 0)
1163         return group->meth->precompute_mult(group, ctx);
1164     else
1165         return 1;               /* nothing to do, so report success */
1166 }
1167
1168 int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
1169 {
1170     if (group->meth->mul == 0)
1171         /* use default */
1172         return ossl_ec_wNAF_have_precompute_mult(group);
1173
1174     if (group->meth->have_precompute_mult != 0)
1175         return group->meth->have_precompute_mult(group);
1176     else
1177         return 0;               /* cannot tell whether precomputation has
1178                                  * been performed */
1179 }
1180 #endif
1181
1182 /*
1183  * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
1184  * returns one on success. On error it returns zero.
1185  */
1186 static int ec_precompute_mont_data(EC_GROUP *group)
1187 {
1188     BN_CTX *ctx = BN_CTX_new_ex(group->libctx);
1189     int ret = 0;
1190
1191     BN_MONT_CTX_free(group->mont_data);
1192     group->mont_data = NULL;
1193
1194     if (ctx == NULL)
1195         goto err;
1196
1197     group->mont_data = BN_MONT_CTX_new();
1198     if (group->mont_data == NULL)
1199         goto err;
1200
1201     if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) {
1202         BN_MONT_CTX_free(group->mont_data);
1203         group->mont_data = NULL;
1204         goto err;
1205     }
1206
1207     ret = 1;
1208
1209  err:
1210
1211     BN_CTX_free(ctx);
1212     return ret;
1213 }
1214
1215 #ifndef FIPS_MODULE
1216 int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg)
1217 {
1218     return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
1219 }
1220
1221 void *EC_KEY_get_ex_data(const EC_KEY *key, int idx)
1222 {
1223     return CRYPTO_get_ex_data(&key->ex_data, idx);
1224 }
1225 #endif
1226
1227 int ossl_ec_group_simple_order_bits(const EC_GROUP *group)
1228 {
1229     if (group->order == NULL)
1230         return 0;
1231     return BN_num_bits(group->order);
1232 }
1233
1234 static int ec_field_inverse_mod_ord(const EC_GROUP *group, BIGNUM *r,
1235                                     const BIGNUM *x, BN_CTX *ctx)
1236 {
1237     BIGNUM *e = NULL;
1238     int ret = 0;
1239 #ifndef FIPS_MODULE
1240     BN_CTX *new_ctx = NULL;
1241 #endif
1242
1243     if (group->mont_data == NULL)
1244         return 0;
1245
1246 #ifndef FIPS_MODULE
1247     if (ctx == NULL)
1248         ctx = new_ctx = BN_CTX_secure_new();
1249 #endif
1250     if (ctx == NULL)
1251         return 0;
1252
1253     BN_CTX_start(ctx);
1254     if ((e = BN_CTX_get(ctx)) == NULL)
1255         goto err;
1256
1257     /*-
1258      * We want inverse in constant time, therefore we utilize the fact
1259      * order must be prime and use Fermats Little Theorem instead.
1260      */
1261     if (!BN_set_word(e, 2))
1262         goto err;
1263     if (!BN_sub(e, group->order, e))
1264         goto err;
1265     /*-
1266      * Exponent e is public.
1267      * No need for scatter-gather or BN_FLG_CONSTTIME.
1268      */
1269     if (!BN_mod_exp_mont(r, x, e, group->order, ctx, group->mont_data))
1270         goto err;
1271
1272     ret = 1;
1273
1274  err:
1275     BN_CTX_end(ctx);
1276 #ifndef FIPS_MODULE
1277     BN_CTX_free(new_ctx);
1278 #endif
1279     return ret;
1280 }
1281
1282 /*-
1283  * Default behavior, if group->meth->field_inverse_mod_ord is NULL:
1284  * - When group->order is even, this function returns an error.
1285  * - When group->order is otherwise composite, the correctness
1286  *   of the output is not guaranteed.
1287  * - When x is outside the range [1, group->order), the correctness
1288  *   of the output is not guaranteed.
1289  * - Otherwise, this function returns the multiplicative inverse in the
1290  *   range [1, group->order).
1291  *
1292  * EC_METHODs must implement their own field_inverse_mod_ord for
1293  * other functionality.
1294  */
1295 int ossl_ec_group_do_inverse_ord(const EC_GROUP *group, BIGNUM *res,
1296                                  const BIGNUM *x, BN_CTX *ctx)
1297 {
1298     if (group->meth->field_inverse_mod_ord != NULL)
1299         return group->meth->field_inverse_mod_ord(group, res, x, ctx);
1300     else
1301         return ec_field_inverse_mod_ord(group, res, x, ctx);
1302 }
1303
1304 /*-
1305  * Coordinate blinding for EC_POINT.
1306  *
1307  * The underlying EC_METHOD can optionally implement this function:
1308  * underlying implementations should return 0 on errors, or 1 on
1309  * success.
1310  *
1311  * This wrapper returns 1 in case the underlying EC_METHOD does not
1312  * support coordinate blinding.
1313  */
1314 int ossl_ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p,
1315                                     BN_CTX *ctx)
1316 {
1317     if (group->meth->blind_coordinates == NULL)
1318         return 1; /* ignore if not implemented */
1319
1320     return group->meth->blind_coordinates(group, p, ctx);
1321 }
1322
1323 int EC_GROUP_get_basis_type(const EC_GROUP *group)
1324 {
1325     int i;
1326
1327     if (EC_GROUP_get_field_type(group) != NID_X9_62_characteristic_two_field)
1328         /* everything else is currently not supported */
1329         return 0;
1330
1331     /* Find the last non-zero element of group->poly[] */
1332     for (i = 0;
1333          i < (int)OSSL_NELEM(group->poly) && group->poly[i] != 0;
1334          i++)
1335         continue;
1336
1337     if (i == 4)
1338         return NID_X9_62_ppBasis;
1339     else if (i == 2)
1340         return NID_X9_62_tpBasis;
1341     else
1342         /* everything else is currently not supported */
1343         return 0;
1344 }
1345
1346 #ifndef OPENSSL_NO_EC2M
1347 int EC_GROUP_get_trinomial_basis(const EC_GROUP *group, unsigned int *k)
1348 {
1349     if (group == NULL)
1350         return 0;
1351
1352     if (EC_GROUP_get_field_type(group) != NID_X9_62_characteristic_two_field
1353         || !((group->poly[0] != 0) && (group->poly[1] != 0)
1354              && (group->poly[2] == 0))) {
1355         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1356         return 0;
1357     }
1358
1359     if (k)
1360         *k = group->poly[1];
1361
1362     return 1;
1363 }
1364
1365 int EC_GROUP_get_pentanomial_basis(const EC_GROUP *group, unsigned int *k1,
1366                                    unsigned int *k2, unsigned int *k3)
1367 {
1368     if (group == NULL)
1369         return 0;
1370
1371     if (EC_GROUP_get_field_type(group) != NID_X9_62_characteristic_two_field
1372         || !((group->poly[0] != 0) && (group->poly[1] != 0)
1373              && (group->poly[2] != 0) && (group->poly[3] != 0)
1374              && (group->poly[4] == 0))) {
1375         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1376         return 0;
1377     }
1378
1379     if (k1)
1380         *k1 = group->poly[3];
1381     if (k2)
1382         *k2 = group->poly[2];
1383     if (k3)
1384         *k3 = group->poly[1];
1385
1386     return 1;
1387 }
1388 #endif
1389
1390 #ifndef FIPS_MODULE
1391 /*
1392  * Check if the explicit parameters group matches any built-in curves.
1393  *
1394  * We create a copy of the group just built, so that we can remove optional
1395  * fields for the lookup: we do this to avoid the possibility that one of
1396  * the optional parameters is used to force the library into using a less
1397  * performant and less secure EC_METHOD instead of the specialized one.
1398  * In any case, `seed` is not really used in any computation, while a
1399  * cofactor different from the one in the built-in table is just
1400  * mathematically wrong anyway and should not be used.
1401  */
1402 static EC_GROUP *ec_group_explicit_to_named(const EC_GROUP *group,
1403                                             OSSL_LIB_CTX *libctx,
1404                                             const char *propq,
1405                                             BN_CTX *ctx)
1406 {
1407     EC_GROUP *ret_group = NULL, *dup = NULL;
1408     int curve_name_nid;
1409
1410     const EC_POINT *point = EC_GROUP_get0_generator(group);
1411     const BIGNUM *order = EC_GROUP_get0_order(group);
1412     int no_seed = (EC_GROUP_get0_seed(group) == NULL);
1413
1414     if ((dup = EC_GROUP_dup(group)) == NULL
1415             || EC_GROUP_set_seed(dup, NULL, 0) != 1
1416             || !EC_GROUP_set_generator(dup, point, order, NULL))
1417         goto err;
1418     if ((curve_name_nid = ossl_ec_curve_nid_from_params(dup, ctx)) != NID_undef) {
1419         /*
1420          * The input explicit parameters successfully matched one of the
1421          * built-in curves: often for built-in curves we have specialized
1422          * methods with better performance and hardening.
1423          *
1424          * In this case we replace the `EC_GROUP` created through explicit
1425          * parameters with one created from a named group.
1426          */
1427
1428 # ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
1429         /*
1430          * NID_wap_wsg_idm_ecid_wtls12 and NID_secp224r1 are both aliases for
1431          * the same curve, we prefer the SECP nid when matching explicit
1432          * parameters as that is associated with a specialized EC_METHOD.
1433          */
1434         if (curve_name_nid == NID_wap_wsg_idm_ecid_wtls12)
1435             curve_name_nid = NID_secp224r1;
1436 # endif /* !def(OPENSSL_NO_EC_NISTP_64_GCC_128) */
1437
1438         ret_group = EC_GROUP_new_by_curve_name_ex(libctx, propq, curve_name_nid);
1439         if (ret_group == NULL)
1440             goto err;
1441
1442         /*
1443          * Set the flag so that EC_GROUPs created from explicit parameters are
1444          * serialized using explicit parameters by default.
1445          */
1446         EC_GROUP_set_asn1_flag(ret_group, OPENSSL_EC_EXPLICIT_CURVE);
1447
1448         /*
1449          * If the input params do not contain the optional seed field we make
1450          * sure it is not added to the returned group.
1451          *
1452          * The seed field is not really used inside libcrypto anyway, and
1453          * adding it to parsed explicit parameter keys would alter their DER
1454          * encoding output (because of the extra field) which could impact
1455          * applications fingerprinting keys by their DER encoding.
1456          */
1457         if (no_seed) {
1458             if (EC_GROUP_set_seed(ret_group, NULL, 0) != 1)
1459                 goto err;
1460         }
1461     } else {
1462         ret_group = (EC_GROUP *)group;
1463     }
1464     EC_GROUP_free(dup);
1465     return ret_group;
1466 err:
1467     EC_GROUP_free(dup);
1468     EC_GROUP_free(ret_group);
1469     return NULL;
1470 }
1471 #endif /* FIPS_MODULE */
1472
1473 static EC_GROUP *group_new_from_name(const OSSL_PARAM *p,
1474                                      OSSL_LIB_CTX *libctx, const char *propq)
1475 {
1476     int ok = 0, nid;
1477     const char *curve_name = NULL;
1478
1479     switch (p->data_type) {
1480     case OSSL_PARAM_UTF8_STRING:
1481         /* The OSSL_PARAM functions have no support for this */
1482         curve_name = p->data;
1483         ok = (curve_name != NULL);
1484         break;
1485     case OSSL_PARAM_UTF8_PTR:
1486         ok = OSSL_PARAM_get_utf8_ptr(p, &curve_name);
1487         break;
1488     }
1489
1490     if (ok) {
1491         nid = ossl_ec_curve_name2nid(curve_name);
1492         if (nid == NID_undef) {
1493             ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE);
1494             return NULL;
1495         } else {
1496             return EC_GROUP_new_by_curve_name_ex(libctx, propq, nid);
1497         }
1498     }
1499     return NULL;
1500 }
1501
1502 /* These parameters can be set directly into an EC_GROUP */
1503 int ossl_ec_group_set_params(EC_GROUP *group, const OSSL_PARAM params[])
1504 {
1505     int encoding_flag = -1, format = -1;
1506     const OSSL_PARAM *p;
1507
1508     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT);
1509     if (p != NULL) {
1510         if (!ossl_ec_pt_format_param2id(p, &format)) {
1511             ECerr(0, EC_R_INVALID_FORM);
1512             return 0;
1513         }
1514         EC_GROUP_set_point_conversion_form(group, format);
1515     }
1516
1517     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ENCODING);
1518     if (p != NULL) {
1519         if (!ossl_ec_encoding_param2id(p, &encoding_flag)) {
1520             ECerr(0, EC_R_INVALID_FORM);
1521             return 0;
1522         }
1523         EC_GROUP_set_asn1_flag(group, encoding_flag);
1524     }
1525     /* Optional seed */
1526     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_SEED);
1527     if (p != NULL) {
1528         /* The seed is allowed to be NULL */
1529         if (p->data_type != OSSL_PARAM_OCTET_STRING
1530             || !EC_GROUP_set_seed(group, p->data, p->data_size)) {
1531             ECerr(0, EC_R_INVALID_SEED);
1532             return 0;
1533         }
1534     }
1535     return 1;
1536 }
1537
1538 EC_GROUP *EC_GROUP_new_from_params(const OSSL_PARAM params[],
1539                                    OSSL_LIB_CTX *libctx, const char *propq)
1540 {
1541     const OSSL_PARAM *ptmp;
1542     EC_GROUP *group = NULL;
1543
1544 #ifndef FIPS_MODULE
1545     const OSSL_PARAM *pa, *pb;
1546     int ok = 0;
1547     EC_GROUP *named_group = NULL;
1548     BIGNUM *p = NULL, *a = NULL, *b = NULL, *order = NULL, *cofactor = NULL;
1549     EC_POINT *point = NULL;
1550     int field_bits = 0;
1551     int is_prime_field = 1;
1552     BN_CTX *bnctx = NULL;
1553     const unsigned char *buf = NULL;
1554     int encoding_flag = -1;
1555 #endif
1556
1557     /* This is the simple named group case */
1558     ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME);
1559     if (ptmp != NULL) {
1560         group = group_new_from_name(ptmp, libctx, propq);
1561         if (group != NULL) {
1562             if (!ossl_ec_group_set_params(group, params)) {
1563                 EC_GROUP_free(group);
1564                 group = NULL;
1565             }
1566         }
1567         return group;
1568     }
1569 #ifdef FIPS_MODULE
1570     ERR_raise(ERR_LIB_EC, EC_R_EXPLICIT_PARAMS_NOT_SUPPORTED);
1571     return NULL;
1572 #else
1573     /* If it gets here then we are trying explicit parameters */
1574     bnctx = BN_CTX_new_ex(libctx);
1575     if (bnctx == NULL) {
1576         ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
1577         return 0;
1578     }
1579     BN_CTX_start(bnctx);
1580
1581     p = BN_CTX_get(bnctx);
1582     a = BN_CTX_get(bnctx);
1583     b = BN_CTX_get(bnctx);
1584     order = BN_CTX_get(bnctx);
1585     if (order == NULL) {
1586         ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
1587         goto err;
1588     }
1589
1590     ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_FIELD_TYPE);
1591     if (ptmp == NULL || ptmp->data_type != OSSL_PARAM_UTF8_STRING) {
1592         ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD);
1593         goto err;
1594     }
1595     if (strcasecmp(ptmp->data, SN_X9_62_prime_field) == 0) {
1596         is_prime_field = 1;
1597     } else if (strcasecmp(ptmp->data, SN_X9_62_characteristic_two_field) == 0) {
1598         is_prime_field = 0;
1599     } else {
1600         /* Invalid field */
1601         ERR_raise(ERR_LIB_EC, EC_R_UNSUPPORTED_FIELD);
1602         goto err;
1603     }
1604
1605     pa = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_A);
1606     if (!OSSL_PARAM_get_BN(pa, &a)) {
1607         ERR_raise(ERR_LIB_EC, EC_R_INVALID_A);
1608         goto err;
1609     }
1610     pb = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_B);
1611     if (!OSSL_PARAM_get_BN(pb, &b)) {
1612         ERR_raise(ERR_LIB_EC, EC_R_INVALID_B);
1613         goto err;
1614     }
1615
1616     /* extract the prime number or irreducible polynomial */
1617     ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_P);
1618     if (!OSSL_PARAM_get_BN(ptmp, &p)) {
1619         ERR_raise(ERR_LIB_EC, EC_R_INVALID_P);
1620         goto err;
1621     }
1622
1623     if (is_prime_field) {
1624         if (BN_is_negative(p) || BN_is_zero(p)) {
1625             ERR_raise(ERR_LIB_EC, EC_R_INVALID_P);
1626             goto err;
1627         }
1628         field_bits = BN_num_bits(p);
1629         if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
1630             ERR_raise(ERR_LIB_EC, EC_R_FIELD_TOO_LARGE);
1631             goto err;
1632         }
1633
1634         /* create the EC_GROUP structure */
1635         group = EC_GROUP_new_curve_GFp(p, a, b, bnctx);
1636     } else {
1637 # ifdef OPENSSL_NO_EC2M
1638         ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED);
1639         goto err;
1640 # else
1641         /* create the EC_GROUP structure */
1642         group = EC_GROUP_new_curve_GF2m(p, a, b, NULL);
1643         if (group != NULL) {
1644             field_bits = EC_GROUP_get_degree(group);
1645             if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
1646                 ERR_raise(ERR_LIB_EC, EC_R_FIELD_TOO_LARGE);
1647                 goto err;
1648             }
1649         }
1650 # endif /* OPENSSL_NO_EC2M */
1651     }
1652
1653     if (group == NULL) {
1654         ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
1655         goto err;
1656     }
1657
1658     /* Optional seed */
1659     ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_SEED);
1660     if (ptmp != NULL) {
1661         if (ptmp->data_type != OSSL_PARAM_OCTET_STRING) {
1662             ERR_raise(ERR_LIB_EC, EC_R_INVALID_SEED);
1663             goto err;
1664         }
1665         if (!EC_GROUP_set_seed(group, ptmp->data, ptmp->data_size))
1666             goto err;
1667     }
1668
1669     /* generator base point */
1670     ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_GENERATOR);
1671     if (ptmp == NULL
1672         || ptmp->data_type != OSSL_PARAM_OCTET_STRING) {
1673         ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);
1674         goto err;
1675     }
1676     buf = (const unsigned char *)(ptmp->data);
1677     if ((point = EC_POINT_new(group)) == NULL)
1678         goto err;
1679     EC_GROUP_set_point_conversion_form(group,
1680                                        (point_conversion_form_t)buf[0] & ~0x01);
1681     if (!EC_POINT_oct2point(group, point, buf, ptmp->data_size, bnctx)) {
1682         ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);
1683         goto err;
1684     }
1685
1686     /* order */
1687     ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ORDER);
1688     if (!OSSL_PARAM_get_BN(ptmp, &order)
1689         || (BN_is_negative(order) || BN_is_zero(order))
1690         || (BN_num_bits(order) > (int)field_bits + 1)) { /* Hasse bound */
1691         ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER);
1692         goto err;
1693     }
1694
1695     /* Optional cofactor */
1696     ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_COFACTOR);
1697     if (ptmp != NULL) {
1698         cofactor = BN_CTX_get(bnctx);
1699         if (cofactor == NULL || !OSSL_PARAM_get_BN(ptmp, &cofactor)) {
1700             ERR_raise(ERR_LIB_EC, EC_R_INVALID_COFACTOR);
1701             goto err;
1702         }
1703     }
1704
1705     /* set the generator, order and cofactor (if present) */
1706     if (!EC_GROUP_set_generator(group, point, order, cofactor)) {
1707         ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);
1708         goto err;
1709     }
1710
1711     named_group = ec_group_explicit_to_named(group, libctx, propq, bnctx);
1712     if (named_group == NULL) {
1713         ERR_raise(ERR_LIB_EC, EC_R_INVALID_NAMED_GROUP_CONVERSION);
1714         goto err;
1715     }
1716     if (named_group == group) {
1717         /*
1718          * If we did not find a named group then the encoding should be explicit
1719          * if it was specified
1720          */
1721         ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ENCODING);
1722         if (ptmp != NULL
1723             && !ossl_ec_encoding_param2id(ptmp, &encoding_flag)) {
1724             ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
1725             goto err;
1726         }
1727         if (encoding_flag == OPENSSL_EC_NAMED_CURVE) {
1728             ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
1729             goto err;
1730         }
1731         EC_GROUP_set_asn1_flag(group, OPENSSL_EC_EXPLICIT_CURVE);
1732     } else {
1733         EC_GROUP_free(group);
1734         group = named_group;
1735     }
1736     ok = 1;
1737  err:
1738     if (!ok) {
1739         EC_GROUP_free(group);
1740         group = NULL;
1741     }
1742     EC_POINT_free(point);
1743     BN_CTX_end(bnctx);
1744     BN_CTX_free(bnctx);
1745
1746     return group;
1747 #endif /* FIPS_MODULE */
1748 }