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