2 * Copyright 2001-2021 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 * EC_GROUP 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
*ossl_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_EXPLICIT_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 ossl_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
= ossl_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 ? OPENSSL_EC_NAMED_CURVE
487 : OPENSSL_EC_EXPLICIT_CURVE
;
490 int EC_GROUP_get_curve_name(const EC_GROUP
*group
)
492 return group
->curve_name
;
495 const BIGNUM
*EC_GROUP_get0_field(const EC_GROUP
*group
)
500 int EC_GROUP_get_field_type(const EC_GROUP
*group
)
502 return group
->meth
->field_type
;
505 void EC_GROUP_set_asn1_flag(EC_GROUP
*group
, int flag
)
507 group
->asn1_flag
= flag
;
510 int EC_GROUP_get_asn1_flag(const EC_GROUP
*group
)
512 return group
->asn1_flag
;
515 void EC_GROUP_set_point_conversion_form(EC_GROUP
*group
,
516 point_conversion_form_t form
)
518 group
->asn1_form
= form
;
521 point_conversion_form_t
EC_GROUP_get_point_conversion_form(const EC_GROUP
524 return group
->asn1_form
;
527 size_t EC_GROUP_set_seed(EC_GROUP
*group
, const unsigned char *p
, size_t len
)
529 OPENSSL_free(group
->seed
);
536 if ((group
->seed
= OPENSSL_malloc(len
)) == NULL
) {
537 ERR_raise(ERR_LIB_EC
, ERR_R_MALLOC_FAILURE
);
540 memcpy(group
->seed
, p
, len
);
541 group
->seed_len
= len
;
546 unsigned char *EC_GROUP_get0_seed(const EC_GROUP
*group
)
551 size_t EC_GROUP_get_seed_len(const EC_GROUP
*group
)
553 return group
->seed_len
;
556 int EC_GROUP_set_curve(EC_GROUP
*group
, const BIGNUM
*p
, const BIGNUM
*a
,
557 const BIGNUM
*b
, BN_CTX
*ctx
)
559 if (group
->meth
->group_set_curve
== 0) {
560 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
563 return group
->meth
->group_set_curve(group
, p
, a
, b
, ctx
);
566 int EC_GROUP_get_curve(const EC_GROUP
*group
, BIGNUM
*p
, BIGNUM
*a
, BIGNUM
*b
,
569 if (group
->meth
->group_get_curve
== NULL
) {
570 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
573 return group
->meth
->group_get_curve(group
, p
, a
, b
, ctx
);
576 #ifndef OPENSSL_NO_DEPRECATED_3_0
577 int EC_GROUP_set_curve_GFp(EC_GROUP
*group
, const BIGNUM
*p
, const BIGNUM
*a
,
578 const BIGNUM
*b
, BN_CTX
*ctx
)
580 return EC_GROUP_set_curve(group
, p
, a
, b
, ctx
);
583 int EC_GROUP_get_curve_GFp(const EC_GROUP
*group
, BIGNUM
*p
, BIGNUM
*a
,
584 BIGNUM
*b
, BN_CTX
*ctx
)
586 return EC_GROUP_get_curve(group
, p
, a
, b
, ctx
);
589 # ifndef OPENSSL_NO_EC2M
590 int EC_GROUP_set_curve_GF2m(EC_GROUP
*group
, const BIGNUM
*p
, const BIGNUM
*a
,
591 const BIGNUM
*b
, BN_CTX
*ctx
)
593 return EC_GROUP_set_curve(group
, p
, a
, b
, ctx
);
596 int EC_GROUP_get_curve_GF2m(const EC_GROUP
*group
, BIGNUM
*p
, BIGNUM
*a
,
597 BIGNUM
*b
, BN_CTX
*ctx
)
599 return EC_GROUP_get_curve(group
, p
, a
, b
, ctx
);
604 int EC_GROUP_get_degree(const EC_GROUP
*group
)
606 if (group
->meth
->group_get_degree
== 0) {
607 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
610 return group
->meth
->group_get_degree(group
);
613 int EC_GROUP_check_discriminant(const EC_GROUP
*group
, BN_CTX
*ctx
)
615 if (group
->meth
->group_check_discriminant
== 0) {
616 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
619 return group
->meth
->group_check_discriminant(group
, ctx
);
622 int EC_GROUP_cmp(const EC_GROUP
*a
, const EC_GROUP
*b
, BN_CTX
*ctx
)
625 BIGNUM
*a1
, *a2
, *a3
, *b1
, *b2
, *b3
;
627 BN_CTX
*ctx_new
= NULL
;
630 /* compare the field types */
631 if (EC_GROUP_get_field_type(a
) != EC_GROUP_get_field_type(b
))
633 /* compare the curve name (if present in both) */
634 if (EC_GROUP_get_curve_name(a
) && EC_GROUP_get_curve_name(b
) &&
635 EC_GROUP_get_curve_name(a
) != EC_GROUP_get_curve_name(b
))
637 if (a
->meth
->flags
& EC_FLAGS_CUSTOM_CURVE
)
642 ctx_new
= ctx
= BN_CTX_new();
648 a1
= BN_CTX_get(ctx
);
649 a2
= BN_CTX_get(ctx
);
650 a3
= BN_CTX_get(ctx
);
651 b1
= BN_CTX_get(ctx
);
652 b2
= BN_CTX_get(ctx
);
653 b3
= BN_CTX_get(ctx
);
657 BN_CTX_free(ctx_new
);
663 * XXX This approach assumes that the external representation of curves
664 * over the same field type is the same.
666 if (!a
->meth
->group_get_curve(a
, a1
, a2
, a3
, ctx
) ||
667 !b
->meth
->group_get_curve(b
, b1
, b2
, b3
, ctx
))
670 /* return 1 if the curve parameters are different */
671 if (r
|| BN_cmp(a1
, b1
) != 0 || BN_cmp(a2
, b2
) != 0 || BN_cmp(a3
, b3
) != 0)
674 /* XXX EC_POINT_cmp() assumes that the methods are equal */
675 /* return 1 if the generators are different */
676 if (r
|| EC_POINT_cmp(a
, EC_GROUP_get0_generator(a
),
677 EC_GROUP_get0_generator(b
), ctx
) != 0)
681 const BIGNUM
*ao
, *bo
, *ac
, *bc
;
682 /* compare the orders */
683 ao
= EC_GROUP_get0_order(a
);
684 bo
= EC_GROUP_get0_order(b
);
685 if (ao
== NULL
|| bo
== NULL
) {
686 /* return an error if either order is NULL */
690 if (BN_cmp(ao
, bo
) != 0) {
691 /* return 1 if orders are different */
696 * It gets here if the curve parameters and generator matched.
697 * Now check the optional cofactors (if both are present).
699 ac
= EC_GROUP_get0_cofactor(a
);
700 bc
= EC_GROUP_get0_cofactor(b
);
701 /* Returns 1 (mismatch) if both cofactors are specified and different */
702 if (!BN_is_zero(ac
) && !BN_is_zero(bc
) && BN_cmp(ac
, bc
) != 0)
704 /* Returns 0 if the parameters matched */
709 BN_CTX_free(ctx_new
);
714 /* functions for EC_POINT objects */
716 EC_POINT
*EC_POINT_new(const EC_GROUP
*group
)
721 ERR_raise(ERR_LIB_EC
, ERR_R_PASSED_NULL_PARAMETER
);
724 if (group
->meth
->point_init
== NULL
) {
725 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
729 ret
= OPENSSL_zalloc(sizeof(*ret
));
731 ERR_raise(ERR_LIB_EC
, ERR_R_MALLOC_FAILURE
);
735 ret
->meth
= group
->meth
;
736 ret
->curve_name
= group
->curve_name
;
738 if (!ret
->meth
->point_init(ret
)) {
746 void EC_POINT_free(EC_POINT
*point
)
751 if (point
->meth
->point_finish
!= 0)
752 point
->meth
->point_finish(point
);
756 void EC_POINT_clear_free(EC_POINT
*point
)
761 if (point
->meth
->point_clear_finish
!= 0)
762 point
->meth
->point_clear_finish(point
);
763 else if (point
->meth
->point_finish
!= 0)
764 point
->meth
->point_finish(point
);
765 OPENSSL_clear_free(point
, sizeof(*point
));
768 int EC_POINT_copy(EC_POINT
*dest
, const EC_POINT
*src
)
770 if (dest
->meth
->point_copy
== 0) {
771 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
774 if (dest
->meth
!= src
->meth
775 || (dest
->curve_name
!= src
->curve_name
776 && dest
->curve_name
!= 0
777 && src
->curve_name
!= 0)) {
778 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
783 return dest
->meth
->point_copy(dest
, src
);
786 EC_POINT
*EC_POINT_dup(const EC_POINT
*a
, const EC_GROUP
*group
)
794 t
= EC_POINT_new(group
);
797 r
= EC_POINT_copy(t
, a
);
805 #ifndef OPENSSL_NO_DEPRECATED_3_0
806 const EC_METHOD
*EC_POINT_method_of(const EC_POINT
*point
)
812 int EC_POINT_set_to_infinity(const EC_GROUP
*group
, EC_POINT
*point
)
814 if (group
->meth
->point_set_to_infinity
== 0) {
815 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
818 if (group
->meth
!= point
->meth
) {
819 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
822 return group
->meth
->point_set_to_infinity(group
, point
);
825 #ifndef OPENSSL_NO_DEPRECATED_3_0
826 int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP
*group
,
827 EC_POINT
*point
, const BIGNUM
*x
,
828 const BIGNUM
*y
, const BIGNUM
*z
,
831 if (group
->meth
->field_type
!= NID_X9_62_prime_field
) {
832 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
835 if (!ec_point_is_compat(point
, group
)) {
836 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
839 return ossl_ec_GFp_simple_set_Jprojective_coordinates_GFp(group
, point
,
843 int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP
*group
,
844 const EC_POINT
*point
, BIGNUM
*x
,
845 BIGNUM
*y
, BIGNUM
*z
,
848 if (group
->meth
->field_type
!= NID_X9_62_prime_field
) {
849 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
852 if (!ec_point_is_compat(point
, group
)) {
853 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
856 return ossl_ec_GFp_simple_get_Jprojective_coordinates_GFp(group
, point
,
861 int EC_POINT_set_affine_coordinates(const EC_GROUP
*group
, EC_POINT
*point
,
862 const BIGNUM
*x
, const BIGNUM
*y
,
865 if (group
->meth
->point_set_affine_coordinates
== NULL
) {
866 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
869 if (!ec_point_is_compat(point
, group
)) {
870 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
873 if (!group
->meth
->point_set_affine_coordinates(group
, point
, x
, y
, ctx
))
876 if (EC_POINT_is_on_curve(group
, point
, ctx
) <= 0) {
877 ERR_raise(ERR_LIB_EC
, EC_R_POINT_IS_NOT_ON_CURVE
);
883 #ifndef OPENSSL_NO_DEPRECATED_3_0
884 int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP
*group
,
885 EC_POINT
*point
, const BIGNUM
*x
,
886 const BIGNUM
*y
, BN_CTX
*ctx
)
888 return EC_POINT_set_affine_coordinates(group
, point
, x
, y
, ctx
);
891 # ifndef OPENSSL_NO_EC2M
892 int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP
*group
,
893 EC_POINT
*point
, const BIGNUM
*x
,
894 const BIGNUM
*y
, BN_CTX
*ctx
)
896 return EC_POINT_set_affine_coordinates(group
, point
, x
, y
, ctx
);
901 int EC_POINT_get_affine_coordinates(const EC_GROUP
*group
,
902 const EC_POINT
*point
, BIGNUM
*x
, BIGNUM
*y
,
905 if (group
->meth
->point_get_affine_coordinates
== NULL
) {
906 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
909 if (!ec_point_is_compat(point
, group
)) {
910 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
913 if (EC_POINT_is_at_infinity(group
, point
)) {
914 ERR_raise(ERR_LIB_EC
, EC_R_POINT_AT_INFINITY
);
917 return group
->meth
->point_get_affine_coordinates(group
, point
, x
, y
, ctx
);
920 #ifndef OPENSSL_NO_DEPRECATED_3_0
921 int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP
*group
,
922 const EC_POINT
*point
, BIGNUM
*x
,
923 BIGNUM
*y
, BN_CTX
*ctx
)
925 return EC_POINT_get_affine_coordinates(group
, point
, x
, y
, ctx
);
928 # ifndef OPENSSL_NO_EC2M
929 int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP
*group
,
930 const EC_POINT
*point
, BIGNUM
*x
,
931 BIGNUM
*y
, BN_CTX
*ctx
)
933 return EC_POINT_get_affine_coordinates(group
, point
, x
, y
, ctx
);
938 int EC_POINT_add(const EC_GROUP
*group
, EC_POINT
*r
, const EC_POINT
*a
,
939 const EC_POINT
*b
, BN_CTX
*ctx
)
941 if (group
->meth
->add
== 0) {
942 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
945 if (!ec_point_is_compat(r
, group
) || !ec_point_is_compat(a
, group
)
946 || !ec_point_is_compat(b
, group
)) {
947 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
950 return group
->meth
->add(group
, r
, a
, b
, ctx
);
953 int EC_POINT_dbl(const EC_GROUP
*group
, EC_POINT
*r
, const EC_POINT
*a
,
956 if (group
->meth
->dbl
== 0) {
957 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
960 if (!ec_point_is_compat(r
, group
) || !ec_point_is_compat(a
, group
)) {
961 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
964 return group
->meth
->dbl(group
, r
, a
, ctx
);
967 int EC_POINT_invert(const EC_GROUP
*group
, EC_POINT
*a
, BN_CTX
*ctx
)
969 if (group
->meth
->invert
== 0) {
970 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
973 if (!ec_point_is_compat(a
, group
)) {
974 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
977 return group
->meth
->invert(group
, a
, ctx
);
980 int EC_POINT_is_at_infinity(const EC_GROUP
*group
, const EC_POINT
*point
)
982 if (group
->meth
->is_at_infinity
== 0) {
983 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
986 if (!ec_point_is_compat(point
, group
)) {
987 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
990 return group
->meth
->is_at_infinity(group
, point
);
994 * Check whether an EC_POINT is on the curve or not. Note that the return
995 * value for this function should NOT be treated as a boolean. Return values:
996 * 1: The point is on the curve
997 * 0: The point is not on the curve
998 * -1: An error occurred
1000 int EC_POINT_is_on_curve(const EC_GROUP
*group
, const EC_POINT
*point
,
1003 if (group
->meth
->is_on_curve
== 0) {
1004 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1007 if (!ec_point_is_compat(point
, group
)) {
1008 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
1011 return group
->meth
->is_on_curve(group
, point
, ctx
);
1014 int EC_POINT_cmp(const EC_GROUP
*group
, const EC_POINT
*a
, const EC_POINT
*b
,
1017 if (group
->meth
->point_cmp
== 0) {
1018 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1021 if (!ec_point_is_compat(a
, group
) || !ec_point_is_compat(b
, group
)) {
1022 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
1025 return group
->meth
->point_cmp(group
, a
, b
, ctx
);
1028 #ifndef OPENSSL_NO_DEPRECATED_3_0
1029 int EC_POINT_make_affine(const EC_GROUP
*group
, EC_POINT
*point
, BN_CTX
*ctx
)
1031 if (group
->meth
->make_affine
== 0) {
1032 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1035 if (!ec_point_is_compat(point
, group
)) {
1036 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
1039 return group
->meth
->make_affine(group
, point
, ctx
);
1042 int EC_POINTs_make_affine(const EC_GROUP
*group
, size_t num
,
1043 EC_POINT
*points
[], BN_CTX
*ctx
)
1047 if (group
->meth
->points_make_affine
== 0) {
1048 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1051 for (i
= 0; i
< num
; i
++) {
1052 if (!ec_point_is_compat(points
[i
], group
)) {
1053 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
1057 return group
->meth
->points_make_affine(group
, num
, points
, ctx
);
1062 * Functions for point multiplication. If group->meth->mul is 0, we use the
1063 * wNAF-based implementations in ec_mult.c; otherwise we dispatch through
1067 #ifndef OPENSSL_NO_DEPRECATED_3_0
1068 int EC_POINTs_mul(const EC_GROUP
*group
, EC_POINT
*r
, const BIGNUM
*scalar
,
1069 size_t num
, const EC_POINT
*points
[],
1070 const BIGNUM
*scalars
[], BN_CTX
*ctx
)
1075 BN_CTX
*new_ctx
= NULL
;
1078 if (!ec_point_is_compat(r
, group
)) {
1079 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
1083 if (scalar
== NULL
&& num
== 0)
1084 return EC_POINT_set_to_infinity(group
, r
);
1086 for (i
= 0; i
< num
; i
++) {
1087 if (!ec_point_is_compat(points
[i
], group
)) {
1088 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
1095 ctx
= new_ctx
= BN_CTX_secure_new();
1098 ERR_raise(ERR_LIB_EC
, ERR_R_INTERNAL_ERROR
);
1102 if (group
->meth
->mul
!= NULL
)
1103 ret
= group
->meth
->mul(group
, r
, scalar
, num
, points
, scalars
, ctx
);
1106 ret
= ossl_ec_wNAF_mul(group
, r
, scalar
, num
, points
, scalars
, ctx
);
1109 BN_CTX_free(new_ctx
);
1115 int EC_POINT_mul(const EC_GROUP
*group
, EC_POINT
*r
, const BIGNUM
*g_scalar
,
1116 const EC_POINT
*point
, const BIGNUM
*p_scalar
, BN_CTX
*ctx
)
1121 BN_CTX
*new_ctx
= NULL
;
1124 if (!ec_point_is_compat(r
, group
)
1125 || (point
!= NULL
&& !ec_point_is_compat(point
, group
))) {
1126 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
1130 if (g_scalar
== NULL
&& p_scalar
== NULL
)
1131 return EC_POINT_set_to_infinity(group
, r
);
1135 ctx
= new_ctx
= BN_CTX_secure_new();
1138 ERR_raise(ERR_LIB_EC
, ERR_R_INTERNAL_ERROR
);
1142 num
= (point
!= NULL
&& p_scalar
!= NULL
) ? 1 : 0;
1143 if (group
->meth
->mul
!= NULL
)
1144 ret
= group
->meth
->mul(group
, r
, g_scalar
, num
, &point
, &p_scalar
, ctx
);
1147 ret
= ossl_ec_wNAF_mul(group
, r
, g_scalar
, num
, &point
, &p_scalar
, ctx
);
1150 BN_CTX_free(new_ctx
);
1155 #ifndef OPENSSL_NO_DEPRECATED_3_0
1156 int EC_GROUP_precompute_mult(EC_GROUP
*group
, BN_CTX
*ctx
)
1158 if (group
->meth
->mul
== 0)
1160 return ossl_ec_wNAF_precompute_mult(group
, ctx
);
1162 if (group
->meth
->precompute_mult
!= 0)
1163 return group
->meth
->precompute_mult(group
, ctx
);
1165 return 1; /* nothing to do, so report success */
1168 int EC_GROUP_have_precompute_mult(const EC_GROUP
*group
)
1170 if (group
->meth
->mul
== 0)
1172 return ossl_ec_wNAF_have_precompute_mult(group
);
1174 if (group
->meth
->have_precompute_mult
!= 0)
1175 return group
->meth
->have_precompute_mult(group
);
1177 return 0; /* cannot tell whether precomputation has
1183 * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
1184 * returns one on success. On error it returns zero.
1186 static int ec_precompute_mont_data(EC_GROUP
*group
)
1188 BN_CTX
*ctx
= BN_CTX_new_ex(group
->libctx
);
1191 BN_MONT_CTX_free(group
->mont_data
);
1192 group
->mont_data
= NULL
;
1197 group
->mont_data
= BN_MONT_CTX_new();
1198 if (group
->mont_data
== NULL
)
1201 if (!BN_MONT_CTX_set(group
->mont_data
, group
->order
, ctx
)) {
1202 BN_MONT_CTX_free(group
->mont_data
);
1203 group
->mont_data
= NULL
;
1216 int EC_KEY_set_ex_data(EC_KEY
*key
, int idx
, void *arg
)
1218 return CRYPTO_set_ex_data(&key
->ex_data
, idx
, arg
);
1221 void *EC_KEY_get_ex_data(const EC_KEY
*key
, int idx
)
1223 return CRYPTO_get_ex_data(&key
->ex_data
, idx
);
1227 int ossl_ec_group_simple_order_bits(const EC_GROUP
*group
)
1229 if (group
->order
== NULL
)
1231 return BN_num_bits(group
->order
);
1234 static int ec_field_inverse_mod_ord(const EC_GROUP
*group
, BIGNUM
*r
,
1235 const BIGNUM
*x
, BN_CTX
*ctx
)
1240 BN_CTX
*new_ctx
= NULL
;
1243 if (group
->mont_data
== NULL
)
1248 ctx
= new_ctx
= BN_CTX_secure_new();
1254 if ((e
= BN_CTX_get(ctx
)) == NULL
)
1258 * We want inverse in constant time, therefore we utilize the fact
1259 * order must be prime and use Fermats Little Theorem instead.
1261 if (!BN_set_word(e
, 2))
1263 if (!BN_sub(e
, group
->order
, e
))
1266 * Exponent e is public.
1267 * No need for scatter-gather or BN_FLG_CONSTTIME.
1269 if (!BN_mod_exp_mont(r
, x
, e
, group
->order
, ctx
, group
->mont_data
))
1277 BN_CTX_free(new_ctx
);
1283 * Default behavior, if group->meth->field_inverse_mod_ord is NULL:
1284 * - When group->order is even, this function returns an error.
1285 * - When group->order is otherwise composite, the correctness
1286 * of the output is not guaranteed.
1287 * - When x is outside the range [1, group->order), the correctness
1288 * of the output is not guaranteed.
1289 * - Otherwise, this function returns the multiplicative inverse in the
1290 * range [1, group->order).
1292 * EC_METHODs must implement their own field_inverse_mod_ord for
1293 * other functionality.
1295 int ossl_ec_group_do_inverse_ord(const EC_GROUP
*group
, BIGNUM
*res
,
1296 const BIGNUM
*x
, BN_CTX
*ctx
)
1298 if (group
->meth
->field_inverse_mod_ord
!= NULL
)
1299 return group
->meth
->field_inverse_mod_ord(group
, res
, x
, ctx
);
1301 return ec_field_inverse_mod_ord(group
, res
, x
, ctx
);
1305 * Coordinate blinding for EC_POINT.
1307 * The underlying EC_METHOD can optionally implement this function:
1308 * underlying implementations should return 0 on errors, or 1 on
1311 * This wrapper returns 1 in case the underlying EC_METHOD does not
1312 * support coordinate blinding.
1314 int ossl_ec_point_blind_coordinates(const EC_GROUP
*group
, EC_POINT
*p
,
1317 if (group
->meth
->blind_coordinates
== NULL
)
1318 return 1; /* ignore if not implemented */
1320 return group
->meth
->blind_coordinates(group
, p
, ctx
);
1323 int EC_GROUP_get_basis_type(const EC_GROUP
*group
)
1327 if (EC_GROUP_get_field_type(group
) != NID_X9_62_characteristic_two_field
)
1328 /* everything else is currently not supported */
1331 /* Find the last non-zero element of group->poly[] */
1333 i
< (int)OSSL_NELEM(group
->poly
) && group
->poly
[i
] != 0;
1338 return NID_X9_62_ppBasis
;
1340 return NID_X9_62_tpBasis
;
1342 /* everything else is currently not supported */
1346 #ifndef OPENSSL_NO_EC2M
1347 int EC_GROUP_get_trinomial_basis(const EC_GROUP
*group
, unsigned int *k
)
1352 if (EC_GROUP_get_field_type(group
) != NID_X9_62_characteristic_two_field
1353 || !((group
->poly
[0] != 0) && (group
->poly
[1] != 0)
1354 && (group
->poly
[2] == 0))) {
1355 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1360 *k
= group
->poly
[1];
1365 int EC_GROUP_get_pentanomial_basis(const EC_GROUP
*group
, unsigned int *k1
,
1366 unsigned int *k2
, unsigned int *k3
)
1371 if (EC_GROUP_get_field_type(group
) != NID_X9_62_characteristic_two_field
1372 || !((group
->poly
[0] != 0) && (group
->poly
[1] != 0)
1373 && (group
->poly
[2] != 0) && (group
->poly
[3] != 0)
1374 && (group
->poly
[4] == 0))) {
1375 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1380 *k1
= group
->poly
[3];
1382 *k2
= group
->poly
[2];
1384 *k3
= group
->poly
[1];
1391 * Check if the explicit parameters group matches any built-in curves.
1393 * We create a copy of the group just built, so that we can remove optional
1394 * fields for the lookup: we do this to avoid the possibility that one of
1395 * the optional parameters is used to force the library into using a less
1396 * performant and less secure EC_METHOD instead of the specialized one.
1397 * In any case, `seed` is not really used in any computation, while a
1398 * cofactor different from the one in the built-in table is just
1399 * mathematically wrong anyway and should not be used.
1401 static EC_GROUP
*ec_group_explicit_to_named(const EC_GROUP
*group
,
1402 OSSL_LIB_CTX
*libctx
,
1406 EC_GROUP
*ret_group
= NULL
, *dup
= NULL
;
1409 const EC_POINT
*point
= EC_GROUP_get0_generator(group
);
1410 const BIGNUM
*order
= EC_GROUP_get0_order(group
);
1411 int no_seed
= (EC_GROUP_get0_seed(group
) == NULL
);
1413 if ((dup
= EC_GROUP_dup(group
)) == NULL
1414 || EC_GROUP_set_seed(dup
, NULL
, 0) != 1
1415 || !EC_GROUP_set_generator(dup
, point
, order
, NULL
))
1417 if ((curve_name_nid
= ossl_ec_curve_nid_from_params(dup
, ctx
)) != NID_undef
) {
1419 * The input explicit parameters successfully matched one of the
1420 * built-in curves: often for built-in curves we have specialized
1421 * methods with better performance and hardening.
1423 * In this case we replace the `EC_GROUP` created through explicit
1424 * parameters with one created from a named group.
1427 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
1429 * NID_wap_wsg_idm_ecid_wtls12 and NID_secp224r1 are both aliases for
1430 * the same curve, we prefer the SECP nid when matching explicit
1431 * parameters as that is associated with a specialized EC_METHOD.
1433 if (curve_name_nid
== NID_wap_wsg_idm_ecid_wtls12
)
1434 curve_name_nid
= NID_secp224r1
;
1435 #endif /* !def(OPENSSL_NO_EC_NISTP_64_GCC_128) */
1437 ret_group
= EC_GROUP_new_by_curve_name_ex(libctx
, propq
, curve_name_nid
);
1438 if (ret_group
== NULL
)
1442 * Set the flag so that EC_GROUPs created from explicit parameters are
1443 * serialized using explicit parameters by default.
1445 EC_GROUP_set_asn1_flag(ret_group
, OPENSSL_EC_EXPLICIT_CURVE
);
1448 * If the input params do not contain the optional seed field we make
1449 * sure it is not added to the returned group.
1451 * The seed field is not really used inside libcrypto anyway, and
1452 * adding it to parsed explicit parameter keys would alter their DER
1453 * encoding output (because of the extra field) which could impact
1454 * applications fingerprinting keys by their DER encoding.
1457 if (EC_GROUP_set_seed(ret_group
, NULL
, 0) != 1)
1461 ret_group
= (EC_GROUP
*)group
;
1467 EC_GROUP_free(ret_group
);
1471 static EC_GROUP
*group_new_from_name(const OSSL_PARAM
*p
,
1472 OSSL_LIB_CTX
*libctx
, const char *propq
)
1475 const char *curve_name
= NULL
;
1477 switch (p
->data_type
) {
1478 case OSSL_PARAM_UTF8_STRING
:
1479 /* The OSSL_PARAM functions have no support for this */
1480 curve_name
= p
->data
;
1481 ok
= (curve_name
!= NULL
);
1483 case OSSL_PARAM_UTF8_PTR
:
1484 ok
= OSSL_PARAM_get_utf8_ptr(p
, &curve_name
);
1489 nid
= ossl_ec_curve_name2nid(curve_name
);
1490 if (nid
== NID_undef
) {
1491 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_CURVE
);
1494 return EC_GROUP_new_by_curve_name_ex(libctx
, propq
, nid
);
1500 /* These parameters can be set directly into an EC_GROUP */
1501 int ossl_ec_group_set_params(EC_GROUP
*group
, const OSSL_PARAM params
[])
1503 int encoding_flag
= -1, format
= -1;
1504 const OSSL_PARAM
*p
;
1506 p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT
);
1508 if (!ossl_ec_pt_format_param2id(p
, &format
)) {
1509 ECerr(0, EC_R_INVALID_FORM
);
1512 EC_GROUP_set_point_conversion_form(group
, format
);
1515 p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_ENCODING
);
1517 if (!ossl_ec_encoding_param2id(p
, &encoding_flag
)) {
1518 ECerr(0, EC_R_INVALID_FORM
);
1521 EC_GROUP_set_asn1_flag(group
, encoding_flag
);
1524 p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_SEED
);
1526 /* The seed is allowed to be NULL */
1527 if (p
->data_type
!= OSSL_PARAM_OCTET_STRING
1528 || !EC_GROUP_set_seed(group
, p
->data
, p
->data_size
)) {
1529 ECerr(0, EC_R_INVALID_SEED
);
1536 EC_GROUP
*EC_GROUP_new_from_params(const OSSL_PARAM params
[],
1537 OSSL_LIB_CTX
*libctx
, const char *propq
)
1539 const OSSL_PARAM
*ptmp
, *pa
, *pb
;
1541 EC_GROUP
*group
= NULL
, *named_group
= NULL
;
1542 BIGNUM
*p
= NULL
, *a
= NULL
, *b
= NULL
, *order
= NULL
, *cofactor
= NULL
;
1543 EC_POINT
*point
= NULL
;
1545 int is_prime_field
= 1;
1546 BN_CTX
*bnctx
= NULL
;
1547 const unsigned char *buf
= NULL
;
1548 int encoding_flag
= -1;
1550 /* This is the simple named group case */
1551 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_GROUP_NAME
);
1553 group
= group_new_from_name(ptmp
, libctx
, propq
);
1554 if (group
!= NULL
) {
1555 if (!ossl_ec_group_set_params(group
, params
)) {
1556 EC_GROUP_free(group
);
1562 /* If it gets here then we are trying explicit parameters */
1563 bnctx
= BN_CTX_new_ex(libctx
);
1564 if (bnctx
== NULL
) {
1565 ERR_raise(ERR_LIB_EC
, ERR_R_MALLOC_FAILURE
);
1568 BN_CTX_start(bnctx
);
1570 p
= BN_CTX_get(bnctx
);
1571 a
= BN_CTX_get(bnctx
);
1572 b
= BN_CTX_get(bnctx
);
1573 order
= BN_CTX_get(bnctx
);
1574 if (order
== NULL
) {
1575 ERR_raise(ERR_LIB_EC
, ERR_R_MALLOC_FAILURE
);
1579 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_FIELD_TYPE
);
1580 if (ptmp
== NULL
|| ptmp
->data_type
!= OSSL_PARAM_UTF8_STRING
) {
1581 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_FIELD
);
1584 if (strcasecmp(ptmp
->data
, SN_X9_62_prime_field
) == 0) {
1586 } else if (strcasecmp(ptmp
->data
, SN_X9_62_characteristic_two_field
) == 0) {
1590 ERR_raise(ERR_LIB_EC
, EC_R_UNSUPPORTED_FIELD
);
1594 pa
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_A
);
1595 if (!OSSL_PARAM_get_BN(pa
, &a
)) {
1596 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_A
);
1599 pb
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_B
);
1600 if (!OSSL_PARAM_get_BN(pb
, &b
)) {
1601 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_B
);
1605 /* extract the prime number or irreducible polynomial */
1606 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_P
);
1607 if (!OSSL_PARAM_get_BN(ptmp
, &p
)) {
1608 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_P
);
1612 if (is_prime_field
) {
1613 if (BN_is_negative(p
) || BN_is_zero(p
)) {
1614 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_P
);
1617 field_bits
= BN_num_bits(p
);
1618 if (field_bits
> OPENSSL_ECC_MAX_FIELD_BITS
) {
1619 ERR_raise(ERR_LIB_EC
, EC_R_FIELD_TOO_LARGE
);
1623 /* create the EC_GROUP structure */
1624 group
= EC_GROUP_new_curve_GFp(p
, a
, b
, bnctx
);
1626 #ifdef OPENSSL_NO_EC2M
1627 ERR_raise(ERR_LIB_EC
, EC_R_GF2M_NOT_SUPPORTED
);
1630 /* create the EC_GROUP structure */
1631 group
= EC_GROUP_new_curve_GF2m(p
, a
, b
, NULL
);
1632 if (group
!= NULL
) {
1633 field_bits
= EC_GROUP_get_degree(group
);
1634 if (field_bits
> OPENSSL_ECC_MAX_FIELD_BITS
) {
1635 ERR_raise(ERR_LIB_EC
, EC_R_FIELD_TOO_LARGE
);
1639 #endif /* OPENSSL_NO_EC2M */
1642 if (group
== NULL
) {
1643 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
1648 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_SEED
);
1650 if (ptmp
->data_type
!= OSSL_PARAM_OCTET_STRING
) {
1651 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_SEED
);
1654 if (!EC_GROUP_set_seed(group
, ptmp
->data
, ptmp
->data_size
))
1658 /* generator base point */
1659 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_GENERATOR
);
1661 || ptmp
->data_type
!= OSSL_PARAM_OCTET_STRING
) {
1662 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_GENERATOR
);
1665 buf
= (const unsigned char *)(ptmp
->data
);
1666 if ((point
= EC_POINT_new(group
)) == NULL
)
1668 EC_GROUP_set_point_conversion_form(group
,
1669 (point_conversion_form_t
)buf
[0] & ~0x01);
1670 if (!EC_POINT_oct2point(group
, point
, buf
, ptmp
->data_size
, bnctx
)) {
1671 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_GENERATOR
);
1676 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_ORDER
);
1677 if (!OSSL_PARAM_get_BN(ptmp
, &order
)
1678 || (BN_is_negative(order
) || BN_is_zero(order
))
1679 || (BN_num_bits(order
) > (int)field_bits
+ 1)) { /* Hasse bound */
1680 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_GROUP_ORDER
);
1684 /* Optional cofactor */
1685 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_COFACTOR
);
1687 cofactor
= BN_CTX_get(bnctx
);
1688 if (cofactor
== NULL
|| !OSSL_PARAM_get_BN(ptmp
, &cofactor
)) {
1689 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_COFACTOR
);
1694 /* set the generator, order and cofactor (if present) */
1695 if (!EC_GROUP_set_generator(group
, point
, order
, cofactor
)) {
1696 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_GENERATOR
);
1700 named_group
= ec_group_explicit_to_named(group
, libctx
, propq
, bnctx
);
1701 if (named_group
== NULL
) {
1702 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_NAMED_GROUP_CONVERSION
);
1705 if (named_group
== group
) {
1707 * If we did not find a named group then the encoding should be explicit
1708 * if it was specified
1710 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_ENCODING
);
1712 && !ossl_ec_encoding_param2id(ptmp
, &encoding_flag
)) {
1713 ECerr(0, EC_R_INVALID_ENCODING
);
1716 if (encoding_flag
== OPENSSL_EC_NAMED_CURVE
) {
1717 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_ENCODING
);
1720 EC_GROUP_set_asn1_flag(group
, OPENSSL_EC_EXPLICIT_CURVE
);
1722 EC_GROUP_free(group
);
1723 group
= named_group
;
1728 EC_GROUP_free(group
);
1731 EC_POINT_free(point
);