2 * Copyright 2001-2020 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
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
12 * ECDSA low level APIs are deprecated for public use, but still ok for
15 #include "internal/deprecated.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"
25 #include "e_os.h" /* strcasecmp */
27 /* functions for EC_GROUP objects */
29 EC_GROUP
*ec_group_new_ex(OSSL_LIB_CTX
*libctx
, const char *propq
,
30 const EC_METHOD
*meth
)
35 ERR_raise(ERR_LIB_EC
, EC_R_SLOT_FULL
);
38 if (meth
->group_init
== 0) {
39 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
43 ret
= OPENSSL_zalloc(sizeof(*ret
));
45 ERR_raise(ERR_LIB_EC
, ERR_R_MALLOC_FAILURE
);
51 ret
->propq
= OPENSSL_strdup(propq
);
52 if (ret
->propq
== NULL
) {
53 ERR_raise(ERR_LIB_EC
, ERR_R_MALLOC_FAILURE
);
58 if ((ret
->meth
->flags
& EC_FLAGS_CUSTOM_CURVE
) == 0) {
59 ret
->order
= BN_new();
60 if (ret
->order
== NULL
)
62 ret
->cofactor
= BN_new();
63 if (ret
->cofactor
== NULL
)
66 ret
->asn1_flag
= OPENSSL_EC_NAMED_CURVE
;
67 ret
->asn1_form
= POINT_CONVERSION_UNCOMPRESSED
;
68 if (!meth
->group_init(ret
))
74 BN_free(ret
->cofactor
);
75 OPENSSL_free(ret
->propq
);
80 #ifndef OPENSSL_NO_DEPRECATED_3_0
82 EC_GROUP
*EC_GROUP_new(const EC_METHOD
*meth
)
84 return ec_group_new_ex(NULL
, NULL
, meth
);
89 void EC_pre_comp_free(EC_GROUP
*group
)
91 switch (group
->pre_comp_type
) {
95 #ifdef ECP_NISTZ256_ASM
96 EC_nistz256_pre_comp_free(group
->pre_comp
.nistz256
);
99 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
101 EC_nistp224_pre_comp_free(group
->pre_comp
.nistp224
);
104 EC_nistp256_pre_comp_free(group
->pre_comp
.nistp256
);
107 EC_nistp521_pre_comp_free(group
->pre_comp
.nistp521
);
116 EC_ec_pre_comp_free(group
->pre_comp
.ec
);
119 group
->pre_comp
.ec
= NULL
;
122 void EC_GROUP_free(EC_GROUP
*group
)
127 if (group
->meth
->group_finish
!= 0)
128 group
->meth
->group_finish(group
);
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
);
140 #ifndef OPENSSL_NO_DEPRECATED_3_0
141 void EC_GROUP_clear_free(EC_GROUP
*group
)
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
);
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
));
161 int EC_GROUP_copy(EC_GROUP
*dest
, const EC_GROUP
*src
)
163 if (dest
->meth
->group_copy
== 0) {
164 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
167 if (dest
->meth
!= src
->meth
) {
168 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
174 dest
->libctx
= src
->libctx
;
175 dest
->curve_name
= src
->curve_name
;
177 /* Copy precomputed */
178 dest
->pre_comp_type
= src
->pre_comp_type
;
179 switch (src
->pre_comp_type
) {
181 dest
->pre_comp
.ec
= NULL
;
184 #ifdef ECP_NISTZ256_ASM
185 dest
->pre_comp
.nistz256
= EC_nistz256_pre_comp_dup(src
->pre_comp
.nistz256
);
188 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
190 dest
->pre_comp
.nistp224
= EC_nistp224_pre_comp_dup(src
->pre_comp
.nistp224
);
193 dest
->pre_comp
.nistp256
= EC_nistp256_pre_comp_dup(src
->pre_comp
.nistp256
);
196 dest
->pre_comp
.nistp521
= EC_nistp521_pre_comp_dup(src
->pre_comp
.nistp521
);
205 dest
->pre_comp
.ec
= EC_ec_pre_comp_dup(src
->pre_comp
.ec
);
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
)
215 if (!BN_MONT_CTX_copy(dest
->mont_data
, src
->mont_data
))
218 /* src->generator == NULL */
219 BN_MONT_CTX_free(dest
->mont_data
);
220 dest
->mont_data
= NULL
;
223 if (src
->generator
!= NULL
) {
224 if (dest
->generator
== NULL
) {
225 dest
->generator
= EC_POINT_new(dest
);
226 if (dest
->generator
== NULL
)
229 if (!EC_POINT_copy(dest
->generator
, src
->generator
))
232 /* src->generator == NULL */
233 EC_POINT_clear_free(dest
->generator
);
234 dest
->generator
= NULL
;
237 if ((src
->meth
->flags
& EC_FLAGS_CUSTOM_CURVE
) == 0) {
238 if (!BN_copy(dest
->order
, src
->order
))
240 if (!BN_copy(dest
->cofactor
, src
->cofactor
))
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
;
249 OPENSSL_free(dest
->seed
);
250 if ((dest
->seed
= OPENSSL_malloc(src
->seed_len
)) == NULL
) {
251 ERR_raise(ERR_LIB_EC
, ERR_R_MALLOC_FAILURE
);
254 if (!memcpy(dest
->seed
, src
->seed
, src
->seed_len
))
256 dest
->seed_len
= src
->seed_len
;
258 OPENSSL_free(dest
->seed
);
263 return dest
->meth
->group_copy(dest
, src
);
266 EC_GROUP
*EC_GROUP_dup(const EC_GROUP
*a
)
274 if ((t
= ec_group_new_ex(a
->libctx
, a
->propq
, a
->meth
)) == NULL
)
276 if (!EC_GROUP_copy(t
, a
))
289 #ifndef OPENSSL_NO_DEPRECATED_3_0
290 const EC_METHOD
*EC_GROUP_method_of(const EC_GROUP
*group
)
295 int EC_METHOD_get_field_type(const EC_METHOD
*meth
)
297 return meth
->field_type
;
301 static int ec_precompute_mont_data(EC_GROUP
*);
304 * Try computing cofactor from the generator order (n) and field cardinality (q).
305 * This works for all curves of cryptographic interest.
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
314 * Otherwise, zero cofactor and return success.
316 static int ec_guess_cofactor(EC_GROUP
*group
) {
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))
325 if (BN_num_bits(group
->order
) <= (BN_num_bits(group
->field
) + 1) / 2 + 3) {
327 BN_zero(group
->cofactor
);
332 if ((ctx
= BN_CTX_new_ex(group
->libctx
)) == NULL
)
336 if ((q
= BN_CTX_get(ctx
)) == NULL
)
339 /* set q = 2**m for binary fields; q = p otherwise */
340 if (group
->meth
->field_type
== NID_X9_62_characteristic_two_field
) {
342 if (!BN_set_bit(q
, BN_num_bits(group
->field
) - 1))
345 if (!BN_copy(q
, group
->field
))
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 */
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
))
364 int EC_GROUP_set_generator(EC_GROUP
*group
, const EC_POINT
*generator
,
365 const BIGNUM
*order
, const BIGNUM
*cofactor
)
367 if (generator
== NULL
) {
368 ERR_raise(ERR_LIB_EC
, ERR_R_PASSED_NULL_PARAMETER
);
372 /* require group->field >= 1 */
373 if (group
->field
== NULL
|| BN_is_zero(group
->field
)
374 || BN_is_negative(group
->field
)) {
375 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_FIELD
);
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
384 if (order
== NULL
|| BN_is_zero(order
) || BN_is_negative(order
)
385 || BN_num_bits(order
) > BN_num_bits(group
->field
) + 1) {
386 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_GROUP_ORDER
);
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.
395 if (cofactor
!= NULL
&& BN_is_negative(cofactor
)) {
396 ERR_raise(ERR_LIB_EC
, EC_R_UNKNOWN_COFACTOR
);
400 if (group
->generator
== NULL
) {
401 group
->generator
= EC_POINT_new(group
);
402 if (group
->generator
== NULL
)
405 if (!EC_POINT_copy(group
->generator
, generator
))
408 if (!BN_copy(group
->order
, order
))
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
))
415 } else if (!ec_guess_cofactor(group
)) {
416 BN_zero(group
->cofactor
);
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.
425 if (BN_is_odd(group
->order
)) {
426 return ec_precompute_mont_data(group
);
429 BN_MONT_CTX_free(group
->mont_data
);
430 group
->mont_data
= NULL
;
434 const EC_POINT
*EC_GROUP_get0_generator(const EC_GROUP
*group
)
436 return group
->generator
;
439 BN_MONT_CTX
*EC_GROUP_get_mont_data(const EC_GROUP
*group
)
441 return group
->mont_data
;
444 int EC_GROUP_get_order(const EC_GROUP
*group
, BIGNUM
*order
, BN_CTX
*ctx
)
446 if (group
->order
== NULL
)
448 if (!BN_copy(order
, group
->order
))
451 return !BN_is_zero(order
);
454 const BIGNUM
*EC_GROUP_get0_order(const EC_GROUP
*group
)
459 int EC_GROUP_order_bits(const EC_GROUP
*group
)
461 return group
->meth
->group_order_bits(group
);
464 int EC_GROUP_get_cofactor(const EC_GROUP
*group
, BIGNUM
*cofactor
,
468 if (group
->cofactor
== NULL
)
470 if (!BN_copy(cofactor
, group
->cofactor
))
473 return !BN_is_zero(group
->cofactor
);
476 const BIGNUM
*EC_GROUP_get0_cofactor(const EC_GROUP
*group
)
478 return group
->cofactor
;
481 void EC_GROUP_set_curve_name(EC_GROUP
*group
, int nid
)
483 group
->curve_name
= nid
;
486 int EC_GROUP_get_curve_name(const EC_GROUP
*group
)
488 return group
->curve_name
;
491 const BIGNUM
*EC_GROUP_get0_field(const EC_GROUP
*group
)
496 int EC_GROUP_get_field_type(const EC_GROUP
*group
)
498 return group
->meth
->field_type
;
501 void EC_GROUP_set_asn1_flag(EC_GROUP
*group
, int flag
)
503 group
->asn1_flag
= flag
;
506 int EC_GROUP_get_asn1_flag(const EC_GROUP
*group
)
508 return group
->asn1_flag
;
511 void EC_GROUP_set_point_conversion_form(EC_GROUP
*group
,
512 point_conversion_form_t form
)
514 group
->asn1_form
= form
;
517 point_conversion_form_t
EC_GROUP_get_point_conversion_form(const EC_GROUP
520 return group
->asn1_form
;
523 size_t EC_GROUP_set_seed(EC_GROUP
*group
, const unsigned char *p
, size_t len
)
525 OPENSSL_free(group
->seed
);
532 if ((group
->seed
= OPENSSL_malloc(len
)) == NULL
) {
533 ERR_raise(ERR_LIB_EC
, ERR_R_MALLOC_FAILURE
);
536 memcpy(group
->seed
, p
, len
);
537 group
->seed_len
= len
;
542 unsigned char *EC_GROUP_get0_seed(const EC_GROUP
*group
)
547 size_t EC_GROUP_get_seed_len(const EC_GROUP
*group
)
549 return group
->seed_len
;
552 int EC_GROUP_set_curve(EC_GROUP
*group
, const BIGNUM
*p
, const BIGNUM
*a
,
553 const BIGNUM
*b
, BN_CTX
*ctx
)
555 if (group
->meth
->group_set_curve
== 0) {
556 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
559 return group
->meth
->group_set_curve(group
, p
, a
, b
, ctx
);
562 int EC_GROUP_get_curve(const EC_GROUP
*group
, BIGNUM
*p
, BIGNUM
*a
, BIGNUM
*b
,
565 if (group
->meth
->group_get_curve
== NULL
) {
566 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
569 return group
->meth
->group_get_curve(group
, p
, a
, b
, ctx
);
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
)
576 return EC_GROUP_set_curve(group
, p
, a
, b
, ctx
);
579 int EC_GROUP_get_curve_GFp(const EC_GROUP
*group
, BIGNUM
*p
, BIGNUM
*a
,
580 BIGNUM
*b
, BN_CTX
*ctx
)
582 return EC_GROUP_get_curve(group
, p
, a
, b
, ctx
);
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
)
589 return EC_GROUP_set_curve(group
, p
, a
, b
, ctx
);
592 int EC_GROUP_get_curve_GF2m(const EC_GROUP
*group
, BIGNUM
*p
, BIGNUM
*a
,
593 BIGNUM
*b
, BN_CTX
*ctx
)
595 return EC_GROUP_get_curve(group
, p
, a
, b
, ctx
);
600 int EC_GROUP_get_degree(const EC_GROUP
*group
)
602 if (group
->meth
->group_get_degree
== 0) {
603 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
606 return group
->meth
->group_get_degree(group
);
609 int EC_GROUP_check_discriminant(const EC_GROUP
*group
, BN_CTX
*ctx
)
611 if (group
->meth
->group_check_discriminant
== 0) {
612 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
615 return group
->meth
->group_check_discriminant(group
, ctx
);
618 int EC_GROUP_cmp(const EC_GROUP
*a
, const EC_GROUP
*b
, BN_CTX
*ctx
)
621 BIGNUM
*a1
, *a2
, *a3
, *b1
, *b2
, *b3
;
623 BN_CTX
*ctx_new
= NULL
;
626 /* compare the field types */
627 if (EC_GROUP_get_field_type(a
) != EC_GROUP_get_field_type(b
))
629 /* compare the curve name (if present in both) */
630 if (EC_GROUP_get_curve_name(a
) && EC_GROUP_get_curve_name(b
) &&
631 EC_GROUP_get_curve_name(a
) != EC_GROUP_get_curve_name(b
))
633 if (a
->meth
->flags
& EC_FLAGS_CUSTOM_CURVE
)
638 ctx_new
= ctx
= BN_CTX_new();
644 a1
= BN_CTX_get(ctx
);
645 a2
= BN_CTX_get(ctx
);
646 a3
= BN_CTX_get(ctx
);
647 b1
= BN_CTX_get(ctx
);
648 b2
= BN_CTX_get(ctx
);
649 b3
= BN_CTX_get(ctx
);
653 BN_CTX_free(ctx_new
);
659 * XXX This approach assumes that the external representation of curves
660 * over the same field type is the same.
662 if (!a
->meth
->group_get_curve(a
, a1
, a2
, a3
, ctx
) ||
663 !b
->meth
->group_get_curve(b
, b1
, b2
, b3
, ctx
))
666 /* return 1 if the curve parameters are different */
667 if (r
|| BN_cmp(a1
, b1
) != 0 || BN_cmp(a2
, b2
) != 0 || BN_cmp(a3
, b3
) != 0)
670 /* XXX EC_POINT_cmp() assumes that the methods are equal */
671 /* return 1 if the generators are different */
672 if (r
|| EC_POINT_cmp(a
, EC_GROUP_get0_generator(a
),
673 EC_GROUP_get0_generator(b
), ctx
) != 0)
677 const BIGNUM
*ao
, *bo
, *ac
, *bc
;
678 /* compare the orders */
679 ao
= EC_GROUP_get0_order(a
);
680 bo
= EC_GROUP_get0_order(b
);
681 if (ao
== NULL
|| bo
== NULL
) {
682 /* return an error if either order is NULL */
686 if (BN_cmp(ao
, bo
) != 0) {
687 /* return 1 if orders are different */
692 * It gets here if the curve parameters and generator matched.
693 * Now check the optional cofactors (if both are present).
695 ac
= EC_GROUP_get0_cofactor(a
);
696 bc
= EC_GROUP_get0_cofactor(b
);
697 /* Returns 1 (mismatch) if both cofactors are specified and different */
698 if (!BN_is_zero(ac
) && !BN_is_zero(bc
) && BN_cmp(ac
, bc
) != 0)
700 /* Returns 0 if the parameters matched */
705 BN_CTX_free(ctx_new
);
710 /* functions for EC_POINT objects */
712 EC_POINT
*EC_POINT_new(const EC_GROUP
*group
)
717 ERR_raise(ERR_LIB_EC
, ERR_R_PASSED_NULL_PARAMETER
);
720 if (group
->meth
->point_init
== NULL
) {
721 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
725 ret
= OPENSSL_zalloc(sizeof(*ret
));
727 ERR_raise(ERR_LIB_EC
, ERR_R_MALLOC_FAILURE
);
731 ret
->meth
= group
->meth
;
732 ret
->curve_name
= group
->curve_name
;
734 if (!ret
->meth
->point_init(ret
)) {
742 void EC_POINT_free(EC_POINT
*point
)
747 if (point
->meth
->point_finish
!= 0)
748 point
->meth
->point_finish(point
);
752 void EC_POINT_clear_free(EC_POINT
*point
)
757 if (point
->meth
->point_clear_finish
!= 0)
758 point
->meth
->point_clear_finish(point
);
759 else if (point
->meth
->point_finish
!= 0)
760 point
->meth
->point_finish(point
);
761 OPENSSL_clear_free(point
, sizeof(*point
));
764 int EC_POINT_copy(EC_POINT
*dest
, const EC_POINT
*src
)
766 if (dest
->meth
->point_copy
== 0) {
767 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
770 if (dest
->meth
!= src
->meth
771 || (dest
->curve_name
!= src
->curve_name
772 && dest
->curve_name
!= 0
773 && src
->curve_name
!= 0)) {
774 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
779 return dest
->meth
->point_copy(dest
, src
);
782 EC_POINT
*EC_POINT_dup(const EC_POINT
*a
, const EC_GROUP
*group
)
790 t
= EC_POINT_new(group
);
793 r
= EC_POINT_copy(t
, a
);
801 #ifndef OPENSSL_NO_DEPRECATED_3_0
802 const EC_METHOD
*EC_POINT_method_of(const EC_POINT
*point
)
808 int EC_POINT_set_to_infinity(const EC_GROUP
*group
, EC_POINT
*point
)
810 if (group
->meth
->point_set_to_infinity
== 0) {
811 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
814 if (group
->meth
!= point
->meth
) {
815 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
818 return group
->meth
->point_set_to_infinity(group
, point
);
821 #ifndef OPENSSL_NO_DEPRECATED_3_0
822 int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP
*group
,
823 EC_POINT
*point
, const BIGNUM
*x
,
824 const BIGNUM
*y
, const BIGNUM
*z
,
827 if (group
->meth
->field_type
!= NID_X9_62_prime_field
) {
828 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
831 if (!ec_point_is_compat(point
, group
)) {
832 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
835 return ec_GFp_simple_set_Jprojective_coordinates_GFp(group
, point
, x
, y
, z
, ctx
);
838 int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP
*group
,
839 const EC_POINT
*point
, BIGNUM
*x
,
840 BIGNUM
*y
, BIGNUM
*z
,
843 if (group
->meth
->field_type
!= NID_X9_62_prime_field
) {
844 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
847 if (!ec_point_is_compat(point
, group
)) {
848 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
851 return ec_GFp_simple_get_Jprojective_coordinates_GFp(group
, point
, x
, y
, z
, ctx
);
855 int EC_POINT_set_affine_coordinates(const EC_GROUP
*group
, EC_POINT
*point
,
856 const BIGNUM
*x
, const BIGNUM
*y
,
859 if (group
->meth
->point_set_affine_coordinates
== NULL
) {
860 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
863 if (!ec_point_is_compat(point
, group
)) {
864 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
867 if (!group
->meth
->point_set_affine_coordinates(group
, point
, x
, y
, ctx
))
870 if (EC_POINT_is_on_curve(group
, point
, ctx
) <= 0) {
871 ERR_raise(ERR_LIB_EC
, EC_R_POINT_IS_NOT_ON_CURVE
);
877 #ifndef OPENSSL_NO_DEPRECATED_3_0
878 int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP
*group
,
879 EC_POINT
*point
, const BIGNUM
*x
,
880 const BIGNUM
*y
, BN_CTX
*ctx
)
882 return EC_POINT_set_affine_coordinates(group
, point
, x
, y
, ctx
);
885 # ifndef OPENSSL_NO_EC2M
886 int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP
*group
,
887 EC_POINT
*point
, const BIGNUM
*x
,
888 const BIGNUM
*y
, BN_CTX
*ctx
)
890 return EC_POINT_set_affine_coordinates(group
, point
, x
, y
, ctx
);
895 int EC_POINT_get_affine_coordinates(const EC_GROUP
*group
,
896 const EC_POINT
*point
, BIGNUM
*x
, BIGNUM
*y
,
899 if (group
->meth
->point_get_affine_coordinates
== NULL
) {
900 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
903 if (!ec_point_is_compat(point
, group
)) {
904 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
907 if (EC_POINT_is_at_infinity(group
, point
)) {
908 ERR_raise(ERR_LIB_EC
, EC_R_POINT_AT_INFINITY
);
911 return group
->meth
->point_get_affine_coordinates(group
, point
, x
, y
, ctx
);
914 #ifndef OPENSSL_NO_DEPRECATED_3_0
915 int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP
*group
,
916 const EC_POINT
*point
, BIGNUM
*x
,
917 BIGNUM
*y
, BN_CTX
*ctx
)
919 return EC_POINT_get_affine_coordinates(group
, point
, x
, y
, ctx
);
922 # ifndef OPENSSL_NO_EC2M
923 int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP
*group
,
924 const EC_POINT
*point
, BIGNUM
*x
,
925 BIGNUM
*y
, BN_CTX
*ctx
)
927 return EC_POINT_get_affine_coordinates(group
, point
, x
, y
, ctx
);
932 int EC_POINT_add(const EC_GROUP
*group
, EC_POINT
*r
, const EC_POINT
*a
,
933 const EC_POINT
*b
, BN_CTX
*ctx
)
935 if (group
->meth
->add
== 0) {
936 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
939 if (!ec_point_is_compat(r
, group
) || !ec_point_is_compat(a
, group
)
940 || !ec_point_is_compat(b
, group
)) {
941 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
944 return group
->meth
->add(group
, r
, a
, b
, ctx
);
947 int EC_POINT_dbl(const EC_GROUP
*group
, EC_POINT
*r
, const EC_POINT
*a
,
950 if (group
->meth
->dbl
== 0) {
951 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
954 if (!ec_point_is_compat(r
, group
) || !ec_point_is_compat(a
, group
)) {
955 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
958 return group
->meth
->dbl(group
, r
, a
, ctx
);
961 int EC_POINT_invert(const EC_GROUP
*group
, EC_POINT
*a
, BN_CTX
*ctx
)
963 if (group
->meth
->invert
== 0) {
964 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
967 if (!ec_point_is_compat(a
, group
)) {
968 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
971 return group
->meth
->invert(group
, a
, ctx
);
974 int EC_POINT_is_at_infinity(const EC_GROUP
*group
, const EC_POINT
*point
)
976 if (group
->meth
->is_at_infinity
== 0) {
977 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
980 if (!ec_point_is_compat(point
, group
)) {
981 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
984 return group
->meth
->is_at_infinity(group
, point
);
988 * Check whether an EC_POINT is on the curve or not. Note that the return
989 * value for this function should NOT be treated as a boolean. Return values:
990 * 1: The point is on the curve
991 * 0: The point is not on the curve
992 * -1: An error occurred
994 int EC_POINT_is_on_curve(const EC_GROUP
*group
, const EC_POINT
*point
,
997 if (group
->meth
->is_on_curve
== 0) {
998 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1001 if (!ec_point_is_compat(point
, group
)) {
1002 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
1005 return group
->meth
->is_on_curve(group
, point
, ctx
);
1008 int EC_POINT_cmp(const EC_GROUP
*group
, const EC_POINT
*a
, const EC_POINT
*b
,
1011 if (group
->meth
->point_cmp
== 0) {
1012 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1015 if (!ec_point_is_compat(a
, group
) || !ec_point_is_compat(b
, group
)) {
1016 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
1019 return group
->meth
->point_cmp(group
, a
, b
, ctx
);
1022 #ifndef OPENSSL_NO_DEPRECATED_3_0
1023 int EC_POINT_make_affine(const EC_GROUP
*group
, EC_POINT
*point
, BN_CTX
*ctx
)
1025 if (group
->meth
->make_affine
== 0) {
1026 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1029 if (!ec_point_is_compat(point
, group
)) {
1030 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
1033 return group
->meth
->make_affine(group
, point
, ctx
);
1036 int EC_POINTs_make_affine(const EC_GROUP
*group
, size_t num
,
1037 EC_POINT
*points
[], BN_CTX
*ctx
)
1041 if (group
->meth
->points_make_affine
== 0) {
1042 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1045 for (i
= 0; i
< num
; i
++) {
1046 if (!ec_point_is_compat(points
[i
], group
)) {
1047 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
1051 return group
->meth
->points_make_affine(group
, num
, points
, ctx
);
1056 * Functions for point multiplication. If group->meth->mul is 0, we use the
1057 * wNAF-based implementations in ec_mult.c; otherwise we dispatch through
1061 #ifndef OPENSSL_NO_DEPRECATED_3_0
1062 int EC_POINTs_mul(const EC_GROUP
*group
, EC_POINT
*r
, const BIGNUM
*scalar
,
1063 size_t num
, const EC_POINT
*points
[],
1064 const BIGNUM
*scalars
[], BN_CTX
*ctx
)
1069 BN_CTX
*new_ctx
= NULL
;
1072 if (!ec_point_is_compat(r
, group
)) {
1073 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
1077 if (scalar
== NULL
&& num
== 0)
1078 return EC_POINT_set_to_infinity(group
, r
);
1080 for (i
= 0; i
< num
; i
++) {
1081 if (!ec_point_is_compat(points
[i
], group
)) {
1082 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
1089 ctx
= new_ctx
= BN_CTX_secure_new();
1092 ERR_raise(ERR_LIB_EC
, ERR_R_INTERNAL_ERROR
);
1096 if (group
->meth
->mul
!= NULL
)
1097 ret
= group
->meth
->mul(group
, r
, scalar
, num
, points
, scalars
, ctx
);
1100 ret
= ec_wNAF_mul(group
, r
, scalar
, num
, points
, scalars
, ctx
);
1103 BN_CTX_free(new_ctx
);
1109 int EC_POINT_mul(const EC_GROUP
*group
, EC_POINT
*r
, const BIGNUM
*g_scalar
,
1110 const EC_POINT
*point
, const BIGNUM
*p_scalar
, BN_CTX
*ctx
)
1115 BN_CTX
*new_ctx
= NULL
;
1118 if (!ec_point_is_compat(r
, group
)
1119 || (point
!= NULL
&& !ec_point_is_compat(point
, group
))) {
1120 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
1124 if (g_scalar
== NULL
&& p_scalar
== NULL
)
1125 return EC_POINT_set_to_infinity(group
, r
);
1129 ctx
= new_ctx
= BN_CTX_secure_new();
1132 ERR_raise(ERR_LIB_EC
, ERR_R_INTERNAL_ERROR
);
1136 num
= (point
!= NULL
&& p_scalar
!= NULL
) ? 1 : 0;
1137 if (group
->meth
->mul
!= NULL
)
1138 ret
= group
->meth
->mul(group
, r
, g_scalar
, num
, &point
, &p_scalar
, ctx
);
1141 ret
= ec_wNAF_mul(group
, r
, g_scalar
, num
, &point
, &p_scalar
, ctx
);
1144 BN_CTX_free(new_ctx
);
1149 #ifndef OPENSSL_NO_DEPRECATED_3_0
1150 int EC_GROUP_precompute_mult(EC_GROUP
*group
, BN_CTX
*ctx
)
1152 if (group
->meth
->mul
== 0)
1154 return ec_wNAF_precompute_mult(group
, ctx
);
1156 if (group
->meth
->precompute_mult
!= 0)
1157 return group
->meth
->precompute_mult(group
, ctx
);
1159 return 1; /* nothing to do, so report success */
1162 int EC_GROUP_have_precompute_mult(const EC_GROUP
*group
)
1164 if (group
->meth
->mul
== 0)
1166 return ec_wNAF_have_precompute_mult(group
);
1168 if (group
->meth
->have_precompute_mult
!= 0)
1169 return group
->meth
->have_precompute_mult(group
);
1171 return 0; /* cannot tell whether precomputation has
1177 * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
1178 * returns one on success. On error it returns zero.
1180 static int ec_precompute_mont_data(EC_GROUP
*group
)
1182 BN_CTX
*ctx
= BN_CTX_new_ex(group
->libctx
);
1185 BN_MONT_CTX_free(group
->mont_data
);
1186 group
->mont_data
= NULL
;
1191 group
->mont_data
= BN_MONT_CTX_new();
1192 if (group
->mont_data
== NULL
)
1195 if (!BN_MONT_CTX_set(group
->mont_data
, group
->order
, ctx
)) {
1196 BN_MONT_CTX_free(group
->mont_data
);
1197 group
->mont_data
= NULL
;
1210 int EC_KEY_set_ex_data(EC_KEY
*key
, int idx
, void *arg
)
1212 return CRYPTO_set_ex_data(&key
->ex_data
, idx
, arg
);
1215 void *EC_KEY_get_ex_data(const EC_KEY
*key
, int idx
)
1217 return CRYPTO_get_ex_data(&key
->ex_data
, idx
);
1221 int ec_group_simple_order_bits(const EC_GROUP
*group
)
1223 if (group
->order
== NULL
)
1225 return BN_num_bits(group
->order
);
1228 static int ec_field_inverse_mod_ord(const EC_GROUP
*group
, BIGNUM
*r
,
1229 const BIGNUM
*x
, BN_CTX
*ctx
)
1234 BN_CTX
*new_ctx
= NULL
;
1237 if (group
->mont_data
== NULL
)
1242 ctx
= new_ctx
= BN_CTX_secure_new();
1248 if ((e
= BN_CTX_get(ctx
)) == NULL
)
1252 * We want inverse in constant time, therefore we utilize the fact
1253 * order must be prime and use Fermats Little Theorem instead.
1255 if (!BN_set_word(e
, 2))
1257 if (!BN_sub(e
, group
->order
, e
))
1260 * Exponent e is public.
1261 * No need for scatter-gather or BN_FLG_CONSTTIME.
1263 if (!BN_mod_exp_mont(r
, x
, e
, group
->order
, ctx
, group
->mont_data
))
1271 BN_CTX_free(new_ctx
);
1277 * Default behavior, if group->meth->field_inverse_mod_ord is NULL:
1278 * - When group->order is even, this function returns an error.
1279 * - When group->order is otherwise composite, the correctness
1280 * of the output is not guaranteed.
1281 * - When x is outside the range [1, group->order), the correctness
1282 * of the output is not guaranteed.
1283 * - Otherwise, this function returns the multiplicative inverse in the
1284 * range [1, group->order).
1286 * EC_METHODs must implement their own field_inverse_mod_ord for
1287 * other functionality.
1289 int ec_group_do_inverse_ord(const EC_GROUP
*group
, BIGNUM
*res
,
1290 const BIGNUM
*x
, BN_CTX
*ctx
)
1292 if (group
->meth
->field_inverse_mod_ord
!= NULL
)
1293 return group
->meth
->field_inverse_mod_ord(group
, res
, x
, ctx
);
1295 return ec_field_inverse_mod_ord(group
, res
, x
, ctx
);
1299 * Coordinate blinding for EC_POINT.
1301 * The underlying EC_METHOD can optionally implement this function:
1302 * underlying implementations should return 0 on errors, or 1 on
1305 * This wrapper returns 1 in case the underlying EC_METHOD does not
1306 * support coordinate blinding.
1308 int ec_point_blind_coordinates(const EC_GROUP
*group
, EC_POINT
*p
, BN_CTX
*ctx
)
1310 if (group
->meth
->blind_coordinates
== NULL
)
1311 return 1; /* ignore if not implemented */
1313 return group
->meth
->blind_coordinates(group
, p
, ctx
);
1316 int EC_GROUP_get_basis_type(const EC_GROUP
*group
)
1320 if (EC_GROUP_get_field_type(group
) != NID_X9_62_characteristic_two_field
)
1321 /* everything else is currently not supported */
1324 /* Find the last non-zero element of group->poly[] */
1326 i
< (int)OSSL_NELEM(group
->poly
) && group
->poly
[i
] != 0;
1331 return NID_X9_62_ppBasis
;
1333 return NID_X9_62_tpBasis
;
1335 /* everything else is currently not supported */
1339 #ifndef OPENSSL_NO_EC2M
1340 int EC_GROUP_get_trinomial_basis(const EC_GROUP
*group
, unsigned int *k
)
1345 if (EC_GROUP_get_field_type(group
) != NID_X9_62_characteristic_two_field
1346 || !((group
->poly
[0] != 0) && (group
->poly
[1] != 0)
1347 && (group
->poly
[2] == 0))) {
1348 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1353 *k
= group
->poly
[1];
1358 int EC_GROUP_get_pentanomial_basis(const EC_GROUP
*group
, unsigned int *k1
,
1359 unsigned int *k2
, unsigned int *k3
)
1364 if (EC_GROUP_get_field_type(group
) != NID_X9_62_characteristic_two_field
1365 || !((group
->poly
[0] != 0) && (group
->poly
[1] != 0)
1366 && (group
->poly
[2] != 0) && (group
->poly
[3] != 0)
1367 && (group
->poly
[4] == 0))) {
1368 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1373 *k1
= group
->poly
[3];
1375 *k2
= group
->poly
[2];
1377 *k3
= group
->poly
[1];
1384 * Check if the explicit parameters group matches any built-in curves.
1386 * We create a copy of the group just built, so that we can remove optional
1387 * fields for the lookup: we do this to avoid the possibility that one of
1388 * the optional parameters is used to force the library into using a less
1389 * performant and less secure EC_METHOD instead of the specialized one.
1390 * In any case, `seed` is not really used in any computation, while a
1391 * cofactor different from the one in the built-in table is just
1392 * mathematically wrong anyway and should not be used.
1394 static EC_GROUP
*ec_group_explicit_to_named(const EC_GROUP
*group
,
1395 OSSL_LIB_CTX
*libctx
,
1399 EC_GROUP
*ret_group
= NULL
, *dup
= NULL
;
1402 const EC_POINT
*point
= EC_GROUP_get0_generator(group
);
1403 const BIGNUM
*order
= EC_GROUP_get0_order(group
);
1404 int no_seed
= (EC_GROUP_get0_seed(group
) == NULL
);
1406 if ((dup
= EC_GROUP_dup(group
)) == NULL
1407 || EC_GROUP_set_seed(dup
, NULL
, 0) != 1
1408 || !EC_GROUP_set_generator(dup
, point
, order
, NULL
))
1410 if ((curve_name_nid
= ec_curve_nid_from_params(dup
, ctx
)) != NID_undef
) {
1412 * The input explicit parameters successfully matched one of the
1413 * built-in curves: often for built-in curves we have specialized
1414 * methods with better performance and hardening.
1416 * In this case we replace the `EC_GROUP` created through explicit
1417 * parameters with one created from a named group.
1420 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
1422 * NID_wap_wsg_idm_ecid_wtls12 and NID_secp224r1 are both aliases for
1423 * the same curve, we prefer the SECP nid when matching explicit
1424 * parameters as that is associated with a specialized EC_METHOD.
1426 if (curve_name_nid
== NID_wap_wsg_idm_ecid_wtls12
)
1427 curve_name_nid
= NID_secp224r1
;
1428 #endif /* !def(OPENSSL_NO_EC_NISTP_64_GCC_128) */
1430 ret_group
= EC_GROUP_new_by_curve_name_ex(libctx
, propq
, curve_name_nid
);
1431 if (ret_group
== NULL
)
1435 * Set the flag so that EC_GROUPs created from explicit parameters are
1436 * serialized using explicit parameters by default.
1438 EC_GROUP_set_asn1_flag(ret_group
, OPENSSL_EC_EXPLICIT_CURVE
);
1441 * If the input params do not contain the optional seed field we make
1442 * sure it is not added to the returned group.
1444 * The seed field is not really used inside libcrypto anyway, and
1445 * adding it to parsed explicit parameter keys would alter their DER
1446 * encoding output (because of the extra field) which could impact
1447 * applications fingerprinting keys by their DER encoding.
1450 if (EC_GROUP_set_seed(ret_group
, NULL
, 0) != 1)
1454 ret_group
= (EC_GROUP
*)group
;
1460 EC_GROUP_free(ret_group
);
1464 static int ec_encoding_param2id(const OSSL_PARAM
*p
, int *id
)
1466 const char *name
= NULL
;
1469 switch (p
->data_type
) {
1470 case OSSL_PARAM_UTF8_STRING
:
1471 /* The OSSL_PARAM functions have no support for this */
1473 status
= (name
!= NULL
);
1475 case OSSL_PARAM_UTF8_PTR
:
1476 status
= OSSL_PARAM_get_utf8_ptr(p
, &name
);
1480 int i
= ec_encoding_name2id(name
);
1490 static EC_GROUP
*group_new_from_name(const OSSL_PARAM
*p
,
1491 OSSL_LIB_CTX
*libctx
, const char *propq
)
1494 const char *curve_name
= NULL
;
1496 switch (p
->data_type
) {
1497 case OSSL_PARAM_UTF8_STRING
:
1498 /* The OSSL_PARAM functions have no support for this */
1499 curve_name
= p
->data
;
1500 ok
= (curve_name
!= NULL
);
1502 case OSSL_PARAM_UTF8_PTR
:
1503 ok
= OSSL_PARAM_get_utf8_ptr(p
, &curve_name
);
1508 nid
= ec_curve_name2nid(curve_name
);
1509 if (nid
== NID_undef
) {
1510 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_CURVE
);
1513 return EC_GROUP_new_by_curve_name_ex(libctx
, propq
, nid
);
1519 EC_GROUP
*EC_GROUP_new_from_params(const OSSL_PARAM params
[],
1520 OSSL_LIB_CTX
*libctx
, const char *propq
)
1522 const OSSL_PARAM
*ptmp
, *pa
, *pb
;
1524 EC_GROUP
*group
= NULL
, *named_group
= NULL
;
1525 BIGNUM
*p
= NULL
, *a
= NULL
, *b
= NULL
, *order
= NULL
, *cofactor
= NULL
;
1526 EC_POINT
*point
= NULL
;
1528 int is_prime_field
= 1;
1529 BN_CTX
*bnctx
= NULL
;
1530 const unsigned char *buf
= NULL
;
1531 int encoding_flag
= -1;
1533 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_ENCODING
);
1534 if (ptmp
!= NULL
&& !ec_encoding_param2id(ptmp
, &encoding_flag
)) {
1535 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_ENCODING
);
1539 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_GROUP_NAME
);
1541 group
= group_new_from_name(ptmp
, libctx
, propq
);
1543 EC_GROUP_set_asn1_flag(group
, encoding_flag
);
1546 bnctx
= BN_CTX_new_ex(libctx
);
1547 if (bnctx
== NULL
) {
1548 ERR_raise(ERR_LIB_EC
, ERR_R_MALLOC_FAILURE
);
1551 BN_CTX_start(bnctx
);
1553 p
= BN_CTX_get(bnctx
);
1554 a
= BN_CTX_get(bnctx
);
1555 b
= BN_CTX_get(bnctx
);
1556 order
= BN_CTX_get(bnctx
);
1557 if (order
== NULL
) {
1558 ERR_raise(ERR_LIB_EC
, ERR_R_MALLOC_FAILURE
);
1562 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_FIELD_TYPE
);
1563 if (ptmp
== NULL
|| ptmp
->data_type
!= OSSL_PARAM_UTF8_STRING
) {
1564 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_FIELD
);
1567 if (strcasecmp(ptmp
->data
, SN_X9_62_prime_field
) == 0) {
1569 } else if (strcasecmp(ptmp
->data
, SN_X9_62_characteristic_two_field
) == 0) {
1573 ERR_raise(ERR_LIB_EC
, EC_R_UNSUPPORTED_FIELD
);
1577 pa
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_A
);
1578 if (!OSSL_PARAM_get_BN(pa
, &a
)) {
1579 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_A
);
1582 pb
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_B
);
1583 if (!OSSL_PARAM_get_BN(pb
, &b
)) {
1584 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_B
);
1588 /* extract the prime number or irreducible polynomial */
1589 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_P
);
1590 if (!OSSL_PARAM_get_BN(ptmp
, &p
)) {
1591 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_P
);
1595 if (is_prime_field
) {
1596 if (BN_is_negative(p
) || BN_is_zero(p
)) {
1597 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_P
);
1600 field_bits
= BN_num_bits(p
);
1601 if (field_bits
> OPENSSL_ECC_MAX_FIELD_BITS
) {
1602 ERR_raise(ERR_LIB_EC
, EC_R_FIELD_TOO_LARGE
);
1606 /* create the EC_GROUP structure */
1607 group
= EC_GROUP_new_curve_GFp(p
, a
, b
, bnctx
);
1609 #ifdef OPENSSL_NO_EC2M
1610 ERR_raise(ERR_LIB_EC
, EC_R_GF2M_NOT_SUPPORTED
);
1613 /* create the EC_GROUP structure */
1614 group
= EC_GROUP_new_curve_GF2m(p
, a
, b
, NULL
);
1615 if (group
!= NULL
) {
1616 field_bits
= EC_GROUP_get_degree(group
);
1617 if (field_bits
> OPENSSL_ECC_MAX_FIELD_BITS
) {
1618 ERR_raise(ERR_LIB_EC
, EC_R_FIELD_TOO_LARGE
);
1622 #endif /* OPENSSL_NO_EC2M */
1625 if (group
== NULL
) {
1626 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
1631 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_SEED
);
1633 if (ptmp
->data_type
!= OSSL_PARAM_OCTET_STRING
) {
1634 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_SEED
);
1637 if (!EC_GROUP_set_seed(group
, ptmp
->data
, ptmp
->data_size
))
1641 /* generator base point */
1642 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_GENERATOR
);
1644 || ptmp
->data_type
!= OSSL_PARAM_OCTET_STRING
) {
1645 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_GENERATOR
);
1648 buf
= (const unsigned char *)(ptmp
->data
);
1649 if ((point
= EC_POINT_new(group
)) == NULL
)
1651 EC_GROUP_set_point_conversion_form(group
,
1652 (point_conversion_form_t
)buf
[0] & ~0x01);
1653 if (!EC_POINT_oct2point(group
, point
, buf
, ptmp
->data_size
, bnctx
)) {
1654 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_GENERATOR
);
1659 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_ORDER
);
1660 if (!OSSL_PARAM_get_BN(ptmp
, &order
)
1661 || (BN_is_negative(order
) || BN_is_zero(order
))
1662 || (BN_num_bits(order
) > (int)field_bits
+ 1)) { /* Hasse bound */
1663 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_GROUP_ORDER
);
1667 /* Optional cofactor */
1668 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_COFACTOR
);
1670 cofactor
= BN_CTX_get(bnctx
);
1671 if (cofactor
== NULL
|| !OSSL_PARAM_get_BN(ptmp
, &cofactor
)) {
1672 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_COFACTOR
);
1677 /* set the generator, order and cofactor (if present) */
1678 if (!EC_GROUP_set_generator(group
, point
, order
, cofactor
)) {
1679 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_GENERATOR
);
1683 named_group
= ec_group_explicit_to_named(group
, libctx
, propq
, bnctx
);
1684 if (named_group
== NULL
) {
1685 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_NAMED_GROUP_CONVERSION
);
1688 if (named_group
== group
) {
1690 * If we did not find a named group then the encoding should be explicit
1691 * if it was specified
1693 if (encoding_flag
== OPENSSL_EC_NAMED_CURVE
) {
1694 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_ENCODING
);
1697 EC_GROUP_set_asn1_flag(group
, OPENSSL_EC_EXPLICIT_CURVE
);
1699 EC_GROUP_free(group
);
1700 group
= named_group
;
1705 EC_GROUP_free(group
);
1708 EC_POINT_free(point
);