72ada1f723ce455ebaaebfa05563fa7b6c465635
[openssl.git] / crypto / ec / ec_lib.c
1 /*
2  * Copyright 2001-2020 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4  *
5  * Licensed under the Apache License 2.0 (the "License").  You may not use
6  * this file except in compliance with the License.  You can obtain a copy
7  * in the file LICENSE in the source distribution or at
8  * https://www.openssl.org/source/license.html
9  */
10
11 /*
12  * ECDSA low level APIs are deprecated for public use, but still ok for
13  * internal use.
14  */
15 #include "internal/deprecated.h"
16
17 #include <string.h>
18 #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 *ec_group_new_ex(OPENSSL_CTX *libctx, const char *propq,
30                           const EC_METHOD *meth)
31 {
32     EC_GROUP *ret;
33
34     if (meth == NULL) {
35         ECerr(0, EC_R_SLOT_FULL);
36         return NULL;
37     }
38     if (meth->group_init == 0) {
39         ECerr(0, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
40         return NULL;
41     }
42
43     ret = OPENSSL_zalloc(sizeof(*ret));
44     if (ret == NULL) {
45         ECerr(0, 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             ECerr(0, 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_NAMED_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 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         ECerr(EC_F_EC_GROUP_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
165         return 0;
166     }
167     if (dest->meth != src->meth) {
168         ECerr(EC_F_EC_GROUP_COPY, 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             ECerr(EC_F_EC_GROUP_COPY, 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 = 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         ECerr(EC_F_EC_GROUP_SET_GENERATOR, 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         ECerr(EC_F_EC_GROUP_SET_GENERATOR, 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         ECerr(EC_F_EC_GROUP_SET_GENERATOR, 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         ECerr(EC_F_EC_GROUP_SET_GENERATOR, 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 }
485
486 int EC_GROUP_get_curve_name(const EC_GROUP *group)
487 {
488     return group->curve_name;
489 }
490
491 const BIGNUM *EC_GROUP_get0_field(const EC_GROUP *group)
492 {
493     return group->field;
494 }
495
496 int EC_GROUP_get_field_type(const EC_GROUP *group)
497 {
498     return group->meth->field_type;
499 }
500
501 void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
502 {
503     group->asn1_flag = flag;
504 }
505
506 int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
507 {
508     return group->asn1_flag;
509 }
510
511 void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
512                                         point_conversion_form_t form)
513 {
514     group->asn1_form = form;
515 }
516
517 point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP
518                                                            *group)
519 {
520     return group->asn1_form;
521 }
522
523 size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
524 {
525     OPENSSL_free(group->seed);
526     group->seed = NULL;
527     group->seed_len = 0;
528
529     if (!len || !p)
530         return 1;
531
532     if ((group->seed = OPENSSL_malloc(len)) == NULL) {
533         ECerr(EC_F_EC_GROUP_SET_SEED, ERR_R_MALLOC_FAILURE);
534         return 0;
535     }
536     memcpy(group->seed, p, len);
537     group->seed_len = len;
538
539     return len;
540 }
541
542 unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
543 {
544     return group->seed;
545 }
546
547 size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
548 {
549     return group->seed_len;
550 }
551
552 int EC_GROUP_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
553                        const BIGNUM *b, BN_CTX *ctx)
554 {
555     if (group->meth->group_set_curve == 0) {
556         ECerr(EC_F_EC_GROUP_SET_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
557         return 0;
558     }
559     return group->meth->group_set_curve(group, p, a, b, ctx);
560 }
561
562 int EC_GROUP_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b,
563                        BN_CTX *ctx)
564 {
565     if (group->meth->group_get_curve == NULL) {
566         ECerr(EC_F_EC_GROUP_GET_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
567         return 0;
568     }
569     return group->meth->group_get_curve(group, p, a, b, ctx);
570 }
571
572 #ifndef OPENSSL_NO_DEPRECATED_3_0
573 int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
574                            const BIGNUM *b, BN_CTX *ctx)
575 {
576     return EC_GROUP_set_curve(group, p, a, b, ctx);
577 }
578
579 int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
580                            BIGNUM *b, BN_CTX *ctx)
581 {
582     return EC_GROUP_get_curve(group, p, a, b, ctx);
583 }
584
585 # ifndef OPENSSL_NO_EC2M
586 int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
587                             const BIGNUM *b, BN_CTX *ctx)
588 {
589     return EC_GROUP_set_curve(group, p, a, b, ctx);
590 }
591
592 int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
593                             BIGNUM *b, BN_CTX *ctx)
594 {
595     return EC_GROUP_get_curve(group, p, a, b, ctx);
596 }
597 # endif
598 #endif
599
600 int EC_GROUP_get_degree(const EC_GROUP *group)
601 {
602     if (group->meth->group_get_degree == 0) {
603         ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
604         return 0;
605     }
606     return group->meth->group_get_degree(group);
607 }
608
609 int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
610 {
611     if (group->meth->group_check_discriminant == 0) {
612         ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT,
613               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
614         return 0;
615     }
616     return group->meth->group_check_discriminant(group, ctx);
617 }
618
619 int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
620 {
621     int r = 0;
622     BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
623 #ifndef FIPS_MODULE
624     BN_CTX *ctx_new = NULL;
625 #endif
626
627     /* compare the field types */
628     if (EC_GROUP_get_field_type(a) != EC_GROUP_get_field_type(b))
629         return 1;
630     /* compare the curve name (if present in both) */
631     if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
632         EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
633         return 1;
634     if (a->meth->flags & EC_FLAGS_CUSTOM_CURVE)
635         return 0;
636
637 #ifndef FIPS_MODULE
638     if (ctx == NULL)
639         ctx_new = ctx = BN_CTX_new();
640 #endif
641     if (ctx == NULL)
642         return -1;
643
644     BN_CTX_start(ctx);
645     a1 = BN_CTX_get(ctx);
646     a2 = BN_CTX_get(ctx);
647     a3 = BN_CTX_get(ctx);
648     b1 = BN_CTX_get(ctx);
649     b2 = BN_CTX_get(ctx);
650     b3 = BN_CTX_get(ctx);
651     if (b3 == NULL) {
652         BN_CTX_end(ctx);
653 #ifndef FIPS_MODULE
654         BN_CTX_free(ctx_new);
655 #endif
656         return -1;
657     }
658
659     /*
660      * XXX This approach assumes that the external representation of curves
661      * over the same field type is the same.
662      */
663     if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
664         !b->meth->group_get_curve(b, b1, b2, b3, ctx))
665         r = 1;
666
667     /* return 1 if the curve parameters are different */
668     if (r || BN_cmp(a1, b1) != 0 || BN_cmp(a2, b2) != 0 || BN_cmp(a3, b3) != 0)
669         r = 1;
670
671     /* XXX EC_POINT_cmp() assumes that the methods are equal */
672     /* return 1 if the generators are different */
673     if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
674                           EC_GROUP_get0_generator(b), ctx) != 0)
675         r = 1;
676
677     if (!r) {
678         const BIGNUM *ao, *bo, *ac, *bc;
679         /* compare the orders */
680         ao = EC_GROUP_get0_order(a);
681         bo = EC_GROUP_get0_order(b);
682         if (ao == NULL || bo == NULL) {
683             /* return an error if either order is NULL */
684             r = -1;
685             goto end;
686         }
687         if (BN_cmp(ao, bo) != 0) {
688             /* return 1 if orders are different */
689             r = 1;
690             goto end;
691         }
692         /*
693          * It gets here if the curve parameters and generator matched.
694          * Now check the optional cofactors (if both are present).
695          */
696         ac = EC_GROUP_get0_cofactor(a);
697         bc = EC_GROUP_get0_cofactor(b);
698         /* Returns 1 (mismatch) if both cofactors are specified and different */
699         if (!BN_is_zero(ac) && !BN_is_zero(bc) && BN_cmp(ac, bc) != 0)
700             r = 1;
701         /* Returns 0 if the parameters matched */
702     }
703 end:
704     BN_CTX_end(ctx);
705 #ifndef FIPS_MODULE
706     BN_CTX_free(ctx_new);
707 #endif
708     return r;
709 }
710
711 /* functions for EC_POINT objects */
712
713 EC_POINT *EC_POINT_new(const EC_GROUP *group)
714 {
715     EC_POINT *ret;
716
717     if (group == NULL) {
718         ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER);
719         return NULL;
720     }
721     if (group->meth->point_init == NULL) {
722         ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
723         return NULL;
724     }
725
726     ret = OPENSSL_zalloc(sizeof(*ret));
727     if (ret == NULL) {
728         ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE);
729         return NULL;
730     }
731
732     ret->meth = group->meth;
733     ret->curve_name = group->curve_name;
734
735     if (!ret->meth->point_init(ret)) {
736         OPENSSL_free(ret);
737         return NULL;
738     }
739
740     return ret;
741 }
742
743 void EC_POINT_free(EC_POINT *point)
744 {
745     if (point == NULL)
746         return;
747
748     if (point->meth->point_finish != 0)
749         point->meth->point_finish(point);
750     OPENSSL_free(point);
751 }
752
753 void EC_POINT_clear_free(EC_POINT *point)
754 {
755     if (point == NULL)
756         return;
757
758     if (point->meth->point_clear_finish != 0)
759         point->meth->point_clear_finish(point);
760     else if (point->meth->point_finish != 0)
761         point->meth->point_finish(point);
762     OPENSSL_clear_free(point, sizeof(*point));
763 }
764
765 int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)
766 {
767     if (dest->meth->point_copy == 0) {
768         ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
769         return 0;
770     }
771     if (dest->meth != src->meth
772             || (dest->curve_name != src->curve_name
773                  && dest->curve_name != 0
774                  && src->curve_name != 0)) {
775         ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS);
776         return 0;
777     }
778     if (dest == src)
779         return 1;
780     return dest->meth->point_copy(dest, src);
781 }
782
783 EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
784 {
785     EC_POINT *t;
786     int r;
787
788     if (a == NULL)
789         return NULL;
790
791     t = EC_POINT_new(group);
792     if (t == NULL)
793         return NULL;
794     r = EC_POINT_copy(t, a);
795     if (!r) {
796         EC_POINT_free(t);
797         return NULL;
798     }
799     return t;
800 }
801
802 #ifndef OPENSSL_NO_DEPRECATED_3_0
803 const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
804 {
805     return point->meth;
806 }
807 #endif
808
809 int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
810 {
811     if (group->meth->point_set_to_infinity == 0) {
812         ECerr(EC_F_EC_POINT_SET_TO_INFINITY,
813               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
814         return 0;
815     }
816     if (group->meth != point->meth) {
817         ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
818         return 0;
819     }
820     return group->meth->point_set_to_infinity(group, point);
821 }
822
823 #ifndef OPENSSL_NO_DEPRECATED_3_0
824 int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
825                                              EC_POINT *point, const BIGNUM *x,
826                                              const BIGNUM *y, const BIGNUM *z,
827                                              BN_CTX *ctx)
828 {
829     if (group->meth->field_type != NID_X9_62_prime_field) {
830         ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
831               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
832         return 0;
833     }
834     if (!ec_point_is_compat(point, group)) {
835         ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
836               EC_R_INCOMPATIBLE_OBJECTS);
837         return 0;
838     }
839     return ec_GFp_simple_set_Jprojective_coordinates_GFp(group, point, x, y, z, ctx);
840 }
841
842 int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
843                                              const EC_POINT *point, BIGNUM *x,
844                                              BIGNUM *y, BIGNUM *z,
845                                              BN_CTX *ctx)
846 {
847     if (group->meth->field_type != NID_X9_62_prime_field) {
848         ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
849               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
850         return 0;
851     }
852     if (!ec_point_is_compat(point, group)) {
853         ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
854               EC_R_INCOMPATIBLE_OBJECTS);
855         return 0;
856     }
857     return ec_GFp_simple_get_Jprojective_coordinates_GFp(group, point, 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         ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES,
867               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
868         return 0;
869     }
870     if (!ec_point_is_compat(point, group)) {
871         ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, EC_R_INCOMPATIBLE_OBJECTS);
872         return 0;
873     }
874     if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx))
875         return 0;
876
877     if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {
878         ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, EC_R_POINT_IS_NOT_ON_CURVE);
879         return 0;
880     }
881     return 1;
882 }
883
884 #ifndef OPENSSL_NO_DEPRECATED_3_0
885 int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
886                                         EC_POINT *point, const BIGNUM *x,
887                                         const BIGNUM *y, BN_CTX *ctx)
888 {
889     return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
890 }
891
892 # ifndef OPENSSL_NO_EC2M
893 int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,
894                                          EC_POINT *point, const BIGNUM *x,
895                                          const BIGNUM *y, BN_CTX *ctx)
896 {
897     return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
898 }
899 # endif
900 #endif
901
902 int EC_POINT_get_affine_coordinates(const EC_GROUP *group,
903                                     const EC_POINT *point, BIGNUM *x, BIGNUM *y,
904                                     BN_CTX *ctx)
905 {
906     if (group->meth->point_get_affine_coordinates == NULL) {
907         ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES,
908               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
909         return 0;
910     }
911     if (!ec_point_is_compat(point, group)) {
912         ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, EC_R_INCOMPATIBLE_OBJECTS);
913         return 0;
914     }
915     if (EC_POINT_is_at_infinity(group, point)) {
916         ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, 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         ECerr(EC_F_EC_POINT_ADD, 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         ECerr(EC_F_EC_POINT_ADD, 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         ECerr(EC_F_EC_POINT_DBL, 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         ECerr(EC_F_EC_POINT_DBL, 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         ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
973         return 0;
974     }
975     if (!ec_point_is_compat(a, group)) {
976         ECerr(EC_F_EC_POINT_INVERT, 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         ECerr(EC_F_EC_POINT_IS_AT_INFINITY,
986               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
987         return 0;
988     }
989     if (!ec_point_is_compat(point, group)) {
990         ECerr(EC_F_EC_POINT_IS_AT_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
991         return 0;
992     }
993     return group->meth->is_at_infinity(group, point);
994 }
995
996 /*
997  * Check whether an EC_POINT is on the curve or not. Note that the return
998  * value for this function should NOT be treated as a boolean. Return values:
999  *  1: The point is on the curve
1000  *  0: The point is not on the curve
1001  * -1: An error occurred
1002  */
1003 int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
1004                          BN_CTX *ctx)
1005 {
1006     if (group->meth->is_on_curve == 0) {
1007         ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1008         return 0;
1009     }
1010     if (!ec_point_is_compat(point, group)) {
1011         ECerr(EC_F_EC_POINT_IS_ON_CURVE, EC_R_INCOMPATIBLE_OBJECTS);
1012         return 0;
1013     }
1014     return group->meth->is_on_curve(group, point, ctx);
1015 }
1016
1017 int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
1018                  BN_CTX *ctx)
1019 {
1020     if (group->meth->point_cmp == 0) {
1021         ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1022         return -1;
1023     }
1024     if (!ec_point_is_compat(a, group) || !ec_point_is_compat(b, group)) {
1025         ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS);
1026         return -1;
1027     }
1028     return group->meth->point_cmp(group, a, b, ctx);
1029 }
1030
1031 #ifndef OPENSSL_NO_DEPRECATED_3_0
1032 int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
1033 {
1034     if (group->meth->make_affine == 0) {
1035         ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1036         return 0;
1037     }
1038     if (!ec_point_is_compat(point, group)) {
1039         ECerr(EC_F_EC_POINT_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
1040         return 0;
1041     }
1042     return group->meth->make_affine(group, point, ctx);
1043 }
1044
1045 int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
1046                           EC_POINT *points[], BN_CTX *ctx)
1047 {
1048     size_t i;
1049
1050     if (group->meth->points_make_affine == 0) {
1051         ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1052         return 0;
1053     }
1054     for (i = 0; i < num; i++) {
1055         if (!ec_point_is_compat(points[i], group)) {
1056             ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
1057             return 0;
1058         }
1059     }
1060     return group->meth->points_make_affine(group, num, points, ctx);
1061 }
1062 #endif
1063
1064 /*
1065  * Functions for point multiplication. If group->meth->mul is 0, we use the
1066  * wNAF-based implementations in ec_mult.c; otherwise we dispatch through
1067  * methods.
1068  */
1069
1070 #ifndef OPENSSL_NO_DEPRECATED_3_0
1071 int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
1072                   size_t num, const EC_POINT *points[],
1073                   const BIGNUM *scalars[], BN_CTX *ctx)
1074 {
1075     int ret = 0;
1076     size_t i = 0;
1077 #ifndef FIPS_MODULE
1078     BN_CTX *new_ctx = NULL;
1079 #endif
1080
1081     if (!ec_point_is_compat(r, group)) {
1082         ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
1083         return 0;
1084     }
1085
1086     if (scalar == NULL && num == 0)
1087         return EC_POINT_set_to_infinity(group, r);
1088
1089     for (i = 0; i < num; i++) {
1090         if (!ec_point_is_compat(points[i], group)) {
1091             ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
1092             return 0;
1093         }
1094     }
1095
1096 #ifndef FIPS_MODULE
1097     if (ctx == NULL)
1098         ctx = new_ctx = BN_CTX_secure_new();
1099 #endif
1100     if (ctx == NULL) {
1101         ECerr(EC_F_EC_POINTS_MUL, ERR_R_INTERNAL_ERROR);
1102         return 0;
1103     }
1104
1105     if (group->meth->mul != NULL)
1106         ret = group->meth->mul(group, r, scalar, num, points, scalars, ctx);
1107     else
1108         /* use default */
1109         ret = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
1110
1111 #ifndef FIPS_MODULE
1112     BN_CTX_free(new_ctx);
1113 #endif
1114     return ret;
1115 }
1116 #endif
1117
1118 int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
1119                  const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
1120 {
1121     int ret = 0;
1122     size_t num;
1123 #ifndef FIPS_MODULE
1124     BN_CTX *new_ctx = NULL;
1125 #endif
1126
1127     if (!ec_point_is_compat(r, group)
1128         || (point != NULL && !ec_point_is_compat(point, group))) {
1129         ECerr(EC_F_EC_POINT_MUL, EC_R_INCOMPATIBLE_OBJECTS);
1130         return 0;
1131     }
1132
1133     if (g_scalar == NULL && p_scalar == NULL)
1134         return EC_POINT_set_to_infinity(group, r);
1135
1136 #ifndef FIPS_MODULE
1137     if (ctx == NULL)
1138         ctx = new_ctx = BN_CTX_secure_new();
1139 #endif
1140     if (ctx == NULL) {
1141         ECerr(EC_F_EC_POINT_MUL, ERR_R_INTERNAL_ERROR);
1142         return 0;
1143     }
1144
1145     num = (point != NULL && p_scalar != NULL) ? 1 : 0;
1146     if (group->meth->mul != NULL)
1147         ret = group->meth->mul(group, r, g_scalar, num, &point, &p_scalar, ctx);
1148     else
1149         /* use default */
1150         ret = ec_wNAF_mul(group, r, g_scalar, num, &point, &p_scalar, ctx);
1151
1152 #ifndef FIPS_MODULE
1153     BN_CTX_free(new_ctx);
1154 #endif
1155     return ret;
1156 }
1157
1158 #ifndef OPENSSL_NO_DEPRECATED_3_0
1159 int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
1160 {
1161     if (group->meth->mul == 0)
1162         /* use default */
1163         return ec_wNAF_precompute_mult(group, ctx);
1164
1165     if (group->meth->precompute_mult != 0)
1166         return group->meth->precompute_mult(group, ctx);
1167     else
1168         return 1;               /* nothing to do, so report success */
1169 }
1170
1171 int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
1172 {
1173     if (group->meth->mul == 0)
1174         /* use default */
1175         return ec_wNAF_have_precompute_mult(group);
1176
1177     if (group->meth->have_precompute_mult != 0)
1178         return group->meth->have_precompute_mult(group);
1179     else
1180         return 0;               /* cannot tell whether precomputation has
1181                                  * been performed */
1182 }
1183 #endif
1184
1185 /*
1186  * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
1187  * returns one on success. On error it returns zero.
1188  */
1189 static int ec_precompute_mont_data(EC_GROUP *group)
1190 {
1191     BN_CTX *ctx = BN_CTX_new_ex(group->libctx);
1192     int ret = 0;
1193
1194     BN_MONT_CTX_free(group->mont_data);
1195     group->mont_data = NULL;
1196
1197     if (ctx == NULL)
1198         goto err;
1199
1200     group->mont_data = BN_MONT_CTX_new();
1201     if (group->mont_data == NULL)
1202         goto err;
1203
1204     if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) {
1205         BN_MONT_CTX_free(group->mont_data);
1206         group->mont_data = NULL;
1207         goto err;
1208     }
1209
1210     ret = 1;
1211
1212  err:
1213
1214     BN_CTX_free(ctx);
1215     return ret;
1216 }
1217
1218 #ifndef FIPS_MODULE
1219 int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg)
1220 {
1221     return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
1222 }
1223
1224 void *EC_KEY_get_ex_data(const EC_KEY *key, int idx)
1225 {
1226     return CRYPTO_get_ex_data(&key->ex_data, idx);
1227 }
1228 #endif
1229
1230 int ec_group_simple_order_bits(const EC_GROUP *group)
1231 {
1232     if (group->order == NULL)
1233         return 0;
1234     return BN_num_bits(group->order);
1235 }
1236
1237 static int ec_field_inverse_mod_ord(const EC_GROUP *group, BIGNUM *r,
1238                                     const BIGNUM *x, BN_CTX *ctx)
1239 {
1240     BIGNUM *e = NULL;
1241     int ret = 0;
1242 #ifndef FIPS_MODULE
1243     BN_CTX *new_ctx = NULL;
1244 #endif
1245
1246     if (group->mont_data == NULL)
1247         return 0;
1248
1249 #ifndef FIPS_MODULE
1250     if (ctx == NULL)
1251         ctx = new_ctx = BN_CTX_secure_new();
1252 #endif
1253     if (ctx == NULL)
1254         return 0;
1255
1256     BN_CTX_start(ctx);
1257     if ((e = BN_CTX_get(ctx)) == NULL)
1258         goto err;
1259
1260     /*-
1261      * We want inverse in constant time, therefore we utilize the fact
1262      * order must be prime and use Fermats Little Theorem instead.
1263      */
1264     if (!BN_set_word(e, 2))
1265         goto err;
1266     if (!BN_sub(e, group->order, e))
1267         goto err;
1268     /*-
1269      * Exponent e is public.
1270      * No need for scatter-gather or BN_FLG_CONSTTIME.
1271      */
1272     if (!BN_mod_exp_mont(r, x, e, group->order, ctx, group->mont_data))
1273         goto err;
1274
1275     ret = 1;
1276
1277  err:
1278     BN_CTX_end(ctx);
1279 #ifndef FIPS_MODULE
1280     BN_CTX_free(new_ctx);
1281 #endif
1282     return ret;
1283 }
1284
1285 /*-
1286  * Default behavior, if group->meth->field_inverse_mod_ord is NULL:
1287  * - When group->order is even, this function returns an error.
1288  * - When group->order is otherwise composite, the correctness
1289  *   of the output is not guaranteed.
1290  * - When x is outside the range [1, group->order), the correctness
1291  *   of the output is not guaranteed.
1292  * - Otherwise, this function returns the multiplicative inverse in the
1293  *   range [1, group->order).
1294  *
1295  * EC_METHODs must implement their own field_inverse_mod_ord for
1296  * other functionality.
1297  */
1298 int ec_group_do_inverse_ord(const EC_GROUP *group, BIGNUM *res,
1299                             const BIGNUM *x, BN_CTX *ctx)
1300 {
1301     if (group->meth->field_inverse_mod_ord != NULL)
1302         return group->meth->field_inverse_mod_ord(group, res, x, ctx);
1303     else
1304         return ec_field_inverse_mod_ord(group, res, x, ctx);
1305 }
1306
1307 /*-
1308  * Coordinate blinding for EC_POINT.
1309  *
1310  * The underlying EC_METHOD can optionally implement this function:
1311  * underlying implementations should return 0 on errors, or 1 on
1312  * success.
1313  *
1314  * This wrapper returns 1 in case the underlying EC_METHOD does not
1315  * support coordinate blinding.
1316  */
1317 int ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p, 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         ECerr(EC_F_EC_GROUP_GET_TRINOMIAL_BASIS,
1358               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1359         return 0;
1360     }
1361
1362     if (k)
1363         *k = group->poly[1];
1364
1365     return 1;
1366 }
1367
1368 int EC_GROUP_get_pentanomial_basis(const EC_GROUP *group, unsigned int *k1,
1369                                    unsigned int *k2, unsigned int *k3)
1370 {
1371     if (group == NULL)
1372         return 0;
1373
1374     if (EC_GROUP_get_field_type(group) != NID_X9_62_characteristic_two_field
1375         || !((group->poly[0] != 0) && (group->poly[1] != 0)
1376              && (group->poly[2] != 0) && (group->poly[3] != 0)
1377              && (group->poly[4] == 0))) {
1378         ECerr(EC_F_EC_GROUP_GET_PENTANOMIAL_BASIS,
1379               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1380         return 0;
1381     }
1382
1383     if (k1)
1384         *k1 = group->poly[3];
1385     if (k2)
1386         *k2 = group->poly[2];
1387     if (k3)
1388         *k3 = group->poly[1];
1389
1390     return 1;
1391 }
1392 #endif
1393
1394 /*
1395  * Check if the explicit parameters group matches any built-in curves.
1396  *
1397  * We create a copy of the group just built, so that we can remove optional
1398  * fields for the lookup: we do this to avoid the possibility that one of
1399  * the optional parameters is used to force the library into using a less
1400  * performant and less secure EC_METHOD instead of the specialized one.
1401  * In any case, `seed` is not really used in any computation, while a
1402  * cofactor different from the one in the built-in table is just
1403  * mathematically wrong anyway and should not be used.
1404  */
1405 static EC_GROUP *ec_group_explicit_to_named(const EC_GROUP *group,
1406                                             OPENSSL_CTX *libctx,
1407                                             const char *propq,
1408                                             BN_CTX *ctx)
1409 {
1410     EC_GROUP *ret_group = NULL, *dup = NULL;
1411     int curve_name_nid;
1412
1413     const EC_POINT *point = EC_GROUP_get0_generator(group);
1414     const BIGNUM *order = EC_GROUP_get0_order(group);
1415     int no_seed = (EC_GROUP_get0_seed(group) == NULL);
1416
1417     if ((dup = EC_GROUP_dup(group)) == NULL
1418             || EC_GROUP_set_seed(dup, NULL, 0) != 1
1419             || !EC_GROUP_set_generator(dup, point, order, NULL))
1420         goto err;
1421     if ((curve_name_nid = ec_curve_nid_from_params(dup, ctx)) != NID_undef) {
1422         /*
1423          * The input explicit parameters successfully matched one of the
1424          * built-in curves: often for built-in curves we have specialized
1425          * methods with better performance and hardening.
1426          *
1427          * In this case we replace the `EC_GROUP` created through explicit
1428          * parameters with one created from a named group.
1429          */
1430
1431 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
1432         /*
1433          * NID_wap_wsg_idm_ecid_wtls12 and NID_secp224r1 are both aliases for
1434          * the same curve, we prefer the SECP nid when matching explicit
1435          * parameters as that is associated with a specialized EC_METHOD.
1436          */
1437         if (curve_name_nid == NID_wap_wsg_idm_ecid_wtls12)
1438             curve_name_nid = NID_secp224r1;
1439 #endif /* !def(OPENSSL_NO_EC_NISTP_64_GCC_128) */
1440
1441         ret_group = EC_GROUP_new_by_curve_name_ex(libctx, propq, curve_name_nid);
1442         if (ret_group == NULL)
1443             goto err;
1444
1445         /*
1446          * Set the flag so that EC_GROUPs created from explicit parameters are
1447          * serialized using explicit parameters by default.
1448          */
1449         EC_GROUP_set_asn1_flag(ret_group, OPENSSL_EC_EXPLICIT_CURVE);
1450
1451         /*
1452          * If the input params do not contain the optional seed field we make
1453          * sure it is not added to the returned group.
1454          *
1455          * The seed field is not really used inside libcrypto anyway, and
1456          * adding it to parsed explicit parameter keys would alter their DER
1457          * encoding output (because of the extra field) which could impact
1458          * applications fingerprinting keys by their DER encoding.
1459          */
1460         if (no_seed) {
1461             if (EC_GROUP_set_seed(ret_group, NULL, 0) != 1)
1462                 goto err;
1463         }
1464     } else {
1465         ret_group = (EC_GROUP *)group;
1466     }
1467     EC_GROUP_free(dup);
1468     return ret_group;
1469 err:
1470     EC_GROUP_free(dup);
1471     EC_GROUP_free(ret_group);
1472     return NULL;
1473 }
1474
1475 static int ec_encoding_param2id(const OSSL_PARAM *p, int *id)
1476 {
1477     const char *name = NULL;
1478     int status = 0;
1479
1480     switch (p->data_type) {
1481     case OSSL_PARAM_UTF8_STRING:
1482         /* The OSSL_PARAM functions have no support for this */
1483         name = p->data;
1484         status = (name != NULL);
1485         break;
1486     case OSSL_PARAM_UTF8_PTR:
1487         status = OSSL_PARAM_get_utf8_ptr(p, &name);
1488         break;
1489     }
1490     if (status) {
1491         int i = ec_encoding_name2id(name);
1492
1493         if (i >= 0) {
1494             *id = i;
1495             return 1;
1496         }
1497     }
1498     return 0;
1499 }
1500
1501 static EC_GROUP *group_new_from_name(const OSSL_PARAM *p,
1502                                      OPENSSL_CTX *libctx, const char *propq)
1503 {
1504     int ok = 0, nid;
1505     const char *curve_name = NULL;
1506
1507     switch (p->data_type) {
1508     case OSSL_PARAM_UTF8_STRING:
1509         /* The OSSL_PARAM functions have no support for this */
1510         curve_name = p->data;
1511         ok = (curve_name != NULL);
1512         break;
1513     case OSSL_PARAM_UTF8_PTR:
1514         ok = OSSL_PARAM_get_utf8_ptr(p, &curve_name);
1515         break;
1516     }
1517
1518     if (ok) {
1519         nid = ec_curve_name2nid(curve_name);
1520         if (nid == NID_undef) {
1521             ECerr(0, EC_R_INVALID_CURVE);
1522             return NULL;
1523         } else {
1524             return EC_GROUP_new_by_curve_name_ex(libctx, propq, nid);
1525         }
1526     }
1527     return NULL;
1528 }
1529
1530 EC_GROUP *EC_GROUP_new_from_params(const OSSL_PARAM params[],
1531                                    OPENSSL_CTX *libctx, const char *propq)
1532 {
1533     const OSSL_PARAM *ptmp, *pa, *pb;
1534     int ok = 0;
1535     EC_GROUP *group = NULL, *named_group = NULL;
1536     BIGNUM *p = NULL, *a = NULL, *b = NULL, *order = NULL, *cofactor = NULL;
1537     EC_POINT *point = NULL;
1538     int field_bits = 0;
1539     int is_prime_field = 1;
1540     BN_CTX *bnctx = NULL;
1541     const unsigned char *buf = NULL;
1542     int encoding_flag = -1;
1543
1544     ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ENCODING);
1545     if (ptmp != NULL && !ec_encoding_param2id(ptmp, &encoding_flag)) {
1546         ECerr(0, EC_R_INVALID_ENCODING);
1547         return 0;
1548     }
1549
1550     ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME);
1551     if (ptmp != NULL) {
1552         group = group_new_from_name(ptmp, libctx, propq);
1553         if (group != NULL)
1554             EC_GROUP_set_asn1_flag(group, encoding_flag);
1555         return group;
1556     }
1557     bnctx = BN_CTX_new_ex(libctx);
1558     if (bnctx == NULL) {
1559         ECerr(0, ERR_R_MALLOC_FAILURE);
1560         return 0;
1561     }
1562     BN_CTX_start(bnctx);
1563
1564     p = BN_CTX_get(bnctx);
1565     a = BN_CTX_get(bnctx);
1566     b = BN_CTX_get(bnctx);
1567     order = BN_CTX_get(bnctx);
1568     if (order == NULL) {
1569         ECerr(0, ERR_R_MALLOC_FAILURE);
1570         goto err;
1571     }
1572
1573     ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_FIELD_TYPE);
1574     if (ptmp == NULL || ptmp->data_type != OSSL_PARAM_UTF8_STRING) {
1575         ECerr(0, EC_R_INVALID_FIELD);
1576         goto err;
1577     }
1578     if (strcasecmp(ptmp->data, SN_X9_62_prime_field) == 0) {
1579         is_prime_field = 1;
1580     } else if (strcasecmp(ptmp->data, SN_X9_62_characteristic_two_field) == 0) {
1581         is_prime_field = 0;
1582     } else {
1583         /* Invalid field */
1584         ECerr(0, EC_R_UNSUPPORTED_FIELD);
1585         goto err;
1586     }
1587
1588     pa = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_A);
1589     if (!OSSL_PARAM_get_BN(pa, &a)) {
1590         ECerr(0, EC_R_INVALID_A);
1591         goto err;
1592     }
1593     pb = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_B);
1594     if (!OSSL_PARAM_get_BN(pb, &b)) {
1595         ECerr(0, EC_R_INVALID_B);
1596         goto err;
1597     }
1598
1599     /* extract the prime number or irreducible polynomial */
1600     ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_P);
1601     if (!OSSL_PARAM_get_BN(ptmp, &p)) {
1602         ECerr(0, EC_R_INVALID_P);
1603         goto err;
1604     }
1605
1606     if (is_prime_field) {
1607         if (BN_is_negative(p) || BN_is_zero(p)) {
1608             ECerr(0, EC_R_INVALID_P);
1609             goto err;
1610         }
1611         field_bits = BN_num_bits(p);
1612         if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
1613             ECerr(0, EC_R_FIELD_TOO_LARGE);
1614             goto err;
1615         }
1616
1617         /* create the EC_GROUP structure */
1618         group = EC_GROUP_new_curve_GFp(p, a, b, bnctx);
1619     } else {
1620 #ifdef OPENSSL_NO_EC2M
1621         ECerr(0, EC_R_GF2M_NOT_SUPPORTED);
1622         goto err;
1623 #else
1624         /* create the EC_GROUP structure */
1625         group = EC_GROUP_new_curve_GF2m(p, a, b, NULL);
1626         if (group != NULL) {
1627             field_bits = EC_GROUP_get_degree(group);
1628             if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
1629                 ECerr(0, EC_R_FIELD_TOO_LARGE);
1630                 goto err;
1631             }
1632         }
1633 #endif /* OPENSSL_NO_EC2M */
1634     }
1635
1636     if (group == NULL) {
1637         ECerr(0, ERR_R_EC_LIB);
1638         goto err;
1639     }
1640
1641     /* Optional seed */
1642     ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_SEED);
1643     if (ptmp != NULL) {
1644         if (ptmp->data_type != OSSL_PARAM_OCTET_STRING) {
1645             ECerr(0, EC_R_INVALID_SEED);
1646             goto err;
1647         }
1648         if (!EC_GROUP_set_seed(group, ptmp->data, ptmp->data_size))
1649             goto err;
1650     }
1651
1652     /* generator base point */
1653     ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_GENERATOR);
1654     if (ptmp == NULL
1655         || ptmp->data_type != OSSL_PARAM_OCTET_STRING) {
1656         ECerr(0, EC_R_INVALID_GENERATOR);
1657         goto err;
1658     }
1659     buf = (const unsigned char *)(ptmp->data);
1660     if ((point = EC_POINT_new(group)) == NULL)
1661         goto err;
1662     EC_GROUP_set_point_conversion_form(group,
1663                                        (point_conversion_form_t)buf[0] & ~0x01);
1664     if (!EC_POINT_oct2point(group, point, buf, ptmp->data_size, bnctx)) {
1665         ECerr(0, EC_R_INVALID_GENERATOR);
1666         goto err;
1667     }
1668
1669     /* order */
1670     ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ORDER);
1671     if (!OSSL_PARAM_get_BN(ptmp, &order)
1672         || (BN_is_negative(order) || BN_is_zero(order))
1673         || (BN_num_bits(order) > (int)field_bits + 1)) { /* Hasse bound */
1674         ECerr(0, EC_R_INVALID_GROUP_ORDER);
1675         goto err;
1676     }
1677
1678     /* Optional cofactor */
1679     ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_COFACTOR);
1680     if (ptmp != NULL) {
1681         cofactor = BN_CTX_get(bnctx);
1682         if (cofactor == NULL || !OSSL_PARAM_get_BN(ptmp, &cofactor)) {
1683             ECerr(0, EC_R_INVALID_COFACTOR);
1684             goto err;
1685         }
1686     }
1687
1688     /* set the generator, order and cofactor (if present) */
1689     if (!EC_GROUP_set_generator(group, point, order, cofactor)) {
1690         ECerr(0, EC_R_INVALID_GENERATOR);
1691         goto err;
1692     }
1693
1694     named_group = ec_group_explicit_to_named(group, libctx, propq, bnctx);
1695     if (named_group == NULL) {
1696         ECerr(0, EC_R_INVALID_NAMED_GROUP_CONVERSION);
1697         goto err;
1698     }
1699     if (named_group == group) {
1700         /*
1701          * If we did not find a named group then the encoding should be explicit
1702          * if it was specified
1703          */
1704         if (encoding_flag == OPENSSL_EC_NAMED_CURVE) {
1705             ECerr(0, EC_R_INVALID_ENCODING);
1706             goto err;
1707         }
1708         EC_GROUP_set_asn1_flag(group, OPENSSL_EC_EXPLICIT_CURVE);
1709     } else {
1710         EC_GROUP_free(group);
1711         group = named_group;
1712     }
1713     ok = 1;
1714  err:
1715     if (!ok) {
1716         EC_GROUP_free(group);
1717         group = NULL;
1718     }
1719     EC_POINT_free(point);
1720     BN_CTX_end(bnctx);
1721     BN_CTX_free(bnctx);
1722
1723     return group;
1724 }