2 * Copyright 2001-2025 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 <openssl/param_build.h>
23 #include "crypto/ec.h"
24 #include "crypto/bn.h"
25 #include "internal/nelem.h"
28 /* functions for EC_GROUP objects */
30 EC_GROUP
*ossl_ec_group_new_ex(OSSL_LIB_CTX
*libctx
, const char *propq
,
31 const EC_METHOD
*meth
)
36 ERR_raise(ERR_LIB_EC
, EC_R_SLOT_FULL
);
39 if (meth
->group_init
== 0) {
40 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
44 ret
= OPENSSL_zalloc(sizeof(*ret
));
50 ret
->propq
= OPENSSL_strdup(propq
);
51 if (ret
->propq
== NULL
)
55 if ((ret
->meth
->flags
& EC_FLAGS_CUSTOM_CURVE
) == 0) {
56 ret
->order
= BN_new();
57 if (ret
->order
== NULL
)
59 ret
->cofactor
= BN_new();
60 if (ret
->cofactor
== NULL
)
63 ret
->asn1_flag
= OPENSSL_EC_EXPLICIT_CURVE
;
64 ret
->asn1_form
= POINT_CONVERSION_UNCOMPRESSED
;
65 if (!meth
->group_init(ret
))
71 BN_free(ret
->cofactor
);
72 OPENSSL_free(ret
->propq
);
77 #ifndef OPENSSL_NO_DEPRECATED_3_0
79 EC_GROUP
*EC_GROUP_new(const EC_METHOD
*meth
)
81 return ossl_ec_group_new_ex(NULL
, NULL
, meth
);
86 void EC_pre_comp_free(EC_GROUP
*group
)
88 switch (group
->pre_comp_type
) {
92 #ifdef ECP_NISTZ256_ASM
93 EC_nistz256_pre_comp_free(group
->pre_comp
.nistz256
);
96 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
98 EC_nistp224_pre_comp_free(group
->pre_comp
.nistp224
);
101 EC_nistp256_pre_comp_free(group
->pre_comp
.nistp256
);
104 ossl_ec_nistp384_pre_comp_free(group
->pre_comp
.nistp384
);
107 EC_nistp521_pre_comp_free(group
->pre_comp
.nistp521
);
117 EC_ec_pre_comp_free(group
->pre_comp
.ec
);
120 group
->pre_comp
.ec
= NULL
;
123 void EC_GROUP_free(EC_GROUP
*group
)
128 if (group
->meth
->group_finish
!= 0)
129 group
->meth
->group_finish(group
);
131 EC_pre_comp_free(group
);
132 BN_MONT_CTX_free(group
->mont_data
);
133 EC_POINT_free(group
->generator
);
134 BN_free(group
->order
);
135 BN_free(group
->cofactor
);
136 OPENSSL_free(group
->seed
);
137 OPENSSL_free(group
->propq
);
141 #ifndef OPENSSL_NO_DEPRECATED_3_0
142 void EC_GROUP_clear_free(EC_GROUP
*group
)
147 if (group
->meth
->group_clear_finish
!= 0)
148 group
->meth
->group_clear_finish(group
);
149 else if (group
->meth
->group_finish
!= 0)
150 group
->meth
->group_finish(group
);
152 EC_pre_comp_free(group
);
153 BN_MONT_CTX_free(group
->mont_data
);
154 EC_POINT_clear_free(group
->generator
);
155 BN_clear_free(group
->order
);
156 BN_clear_free(group
->cofactor
);
157 OPENSSL_clear_free(group
->seed
, group
->seed_len
);
158 OPENSSL_clear_free(group
, sizeof(*group
));
162 int EC_GROUP_copy(EC_GROUP
*dest
, const EC_GROUP
*src
)
164 if (dest
->meth
->group_copy
== 0) {
165 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
168 if (dest
->meth
!= src
->meth
) {
169 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
175 dest
->libctx
= src
->libctx
;
176 dest
->curve_name
= src
->curve_name
;
178 /* Copy precomputed */
179 dest
->pre_comp_type
= src
->pre_comp_type
;
180 switch (src
->pre_comp_type
) {
182 dest
->pre_comp
.ec
= NULL
;
185 #ifdef ECP_NISTZ256_ASM
186 dest
->pre_comp
.nistz256
= EC_nistz256_pre_comp_dup(src
->pre_comp
.nistz256
);
189 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
191 dest
->pre_comp
.nistp224
= EC_nistp224_pre_comp_dup(src
->pre_comp
.nistp224
);
194 dest
->pre_comp
.nistp256
= EC_nistp256_pre_comp_dup(src
->pre_comp
.nistp256
);
197 dest
->pre_comp
.nistp384
= ossl_ec_nistp384_pre_comp_dup(src
->pre_comp
.nistp384
);
200 dest
->pre_comp
.nistp521
= EC_nistp521_pre_comp_dup(src
->pre_comp
.nistp521
);
210 dest
->pre_comp
.ec
= EC_ec_pre_comp_dup(src
->pre_comp
.ec
);
214 if (src
->mont_data
!= NULL
) {
215 if (dest
->mont_data
== NULL
) {
216 dest
->mont_data
= BN_MONT_CTX_new();
217 if (dest
->mont_data
== NULL
)
220 if (!BN_MONT_CTX_copy(dest
->mont_data
, src
->mont_data
))
223 /* src->generator == NULL */
224 BN_MONT_CTX_free(dest
->mont_data
);
225 dest
->mont_data
= NULL
;
228 if (src
->generator
!= NULL
) {
229 if (dest
->generator
== NULL
) {
230 dest
->generator
= EC_POINT_new(dest
);
231 if (dest
->generator
== NULL
)
234 if (!EC_POINT_copy(dest
->generator
, src
->generator
))
237 /* src->generator == NULL */
238 EC_POINT_clear_free(dest
->generator
);
239 dest
->generator
= NULL
;
242 if ((src
->meth
->flags
& EC_FLAGS_CUSTOM_CURVE
) == 0) {
243 if (!BN_copy(dest
->order
, src
->order
))
245 if (!BN_copy(dest
->cofactor
, src
->cofactor
))
249 dest
->asn1_flag
= src
->asn1_flag
;
250 dest
->asn1_form
= src
->asn1_form
;
251 dest
->decoded_from_explicit_params
= src
->decoded_from_explicit_params
;
254 OPENSSL_free(dest
->seed
);
255 if ((dest
->seed
= OPENSSL_malloc(src
->seed_len
)) == NULL
)
257 if (!memcpy(dest
->seed
, src
->seed
, src
->seed_len
))
259 dest
->seed_len
= src
->seed_len
;
261 OPENSSL_free(dest
->seed
);
266 return dest
->meth
->group_copy(dest
, src
);
269 EC_GROUP
*EC_GROUP_dup(const EC_GROUP
*a
)
277 if ((t
= ossl_ec_group_new_ex(a
->libctx
, a
->propq
, a
->meth
)) == NULL
)
279 if (!EC_GROUP_copy(t
, a
))
292 #ifndef OPENSSL_NO_DEPRECATED_3_0
293 const EC_METHOD
*EC_GROUP_method_of(const EC_GROUP
*group
)
298 int EC_METHOD_get_field_type(const EC_METHOD
*meth
)
300 return meth
->field_type
;
304 static int ec_precompute_mont_data(EC_GROUP
*);
307 * Try computing cofactor from the generator order (n) and field cardinality (q).
308 * This works for all curves of cryptographic interest.
310 * Hasse thm: q + 1 - 2*sqrt(q) <= n*h <= q + 1 + 2*sqrt(q)
311 * h_min = (q + 1 - 2*sqrt(q))/n
312 * h_max = (q + 1 + 2*sqrt(q))/n
313 * h_max - h_min = 4*sqrt(q)/n
314 * So if n > 4*sqrt(q) holds, there is only one possible value for h:
315 * h = \lfloor (h_min + h_max)/2 \rceil = \lfloor (q + 1)/n \rceil
317 * Otherwise, zero cofactor and return success.
319 static int ec_guess_cofactor(EC_GROUP
*group
) {
325 * If the cofactor is too large, we cannot guess it.
326 * The RHS of below is a strict overestimate of lg(4 * sqrt(q))
328 if (BN_num_bits(group
->order
) <= (BN_num_bits(group
->field
) + 1) / 2 + 3) {
330 BN_zero(group
->cofactor
);
335 if ((ctx
= BN_CTX_new_ex(group
->libctx
)) == NULL
)
339 if ((q
= BN_CTX_get(ctx
)) == NULL
)
342 /* set q = 2**m for binary fields; q = p otherwise */
343 if (group
->meth
->field_type
== NID_X9_62_characteristic_two_field
) {
345 if (!BN_set_bit(q
, BN_num_bits(group
->field
) - 1))
348 if (!BN_copy(q
, group
->field
))
352 /* compute h = \lfloor (q + 1)/n \rceil = \lfloor (q + 1 + n/2)/n \rfloor */
353 if (!BN_rshift1(group
->cofactor
, group
->order
) /* n/2 */
354 || !BN_add(group
->cofactor
, group
->cofactor
, q
) /* q + n/2 */
356 || !BN_add(group
->cofactor
, group
->cofactor
, BN_value_one())
357 /* (q + 1 + n/2)/n */
358 || !BN_div(group
->cofactor
, NULL
, group
->cofactor
, group
->order
, ctx
))
367 int EC_GROUP_set_generator(EC_GROUP
*group
, const EC_POINT
*generator
,
368 const BIGNUM
*order
, const BIGNUM
*cofactor
)
370 if (generator
== NULL
) {
371 ERR_raise(ERR_LIB_EC
, ERR_R_PASSED_NULL_PARAMETER
);
375 /* require group->field >= 1 */
376 if (group
->field
== NULL
|| BN_is_zero(group
->field
)
377 || BN_is_negative(group
->field
)) {
378 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_FIELD
);
383 * - require order >= 1
384 * - enforce upper bound due to Hasse thm: order can be no more than one bit
385 * longer than field cardinality
387 if (order
== NULL
|| BN_is_zero(order
) || BN_is_negative(order
)
388 || BN_num_bits(order
) > BN_num_bits(group
->field
) + 1) {
389 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_GROUP_ORDER
);
394 * Unfortunately the cofactor is an optional field in many standards.
395 * Internally, the lib uses 0 cofactor as a marker for "unknown cofactor".
396 * So accept cofactor == NULL or cofactor >= 0.
398 if (cofactor
!= NULL
&& BN_is_negative(cofactor
)) {
399 ERR_raise(ERR_LIB_EC
, EC_R_UNKNOWN_COFACTOR
);
403 if (group
->generator
== NULL
) {
404 group
->generator
= EC_POINT_new(group
);
405 if (group
->generator
== NULL
)
408 if (!EC_POINT_copy(group
->generator
, generator
))
411 if (!BN_copy(group
->order
, order
))
414 /* Either take the provided positive cofactor, or try to compute it */
415 if (cofactor
!= NULL
&& !BN_is_zero(cofactor
)) {
416 if (!BN_copy(group
->cofactor
, cofactor
))
418 } else if (!ec_guess_cofactor(group
)) {
419 BN_zero(group
->cofactor
);
424 * Some groups have an order with
425 * factors of two, which makes the Montgomery setup fail.
426 * |group->mont_data| will be NULL in this case.
428 if (BN_is_odd(group
->order
)) {
429 return ec_precompute_mont_data(group
);
432 BN_MONT_CTX_free(group
->mont_data
);
433 group
->mont_data
= NULL
;
437 const EC_POINT
*EC_GROUP_get0_generator(const EC_GROUP
*group
)
439 return group
->generator
;
442 BN_MONT_CTX
*EC_GROUP_get_mont_data(const EC_GROUP
*group
)
444 return group
->mont_data
;
447 int EC_GROUP_get_order(const EC_GROUP
*group
, BIGNUM
*order
, BN_CTX
*ctx
)
449 if (group
->order
== NULL
)
451 if (!BN_copy(order
, group
->order
))
454 return !BN_is_zero(order
);
457 const BIGNUM
*EC_GROUP_get0_order(const EC_GROUP
*group
)
462 int EC_GROUP_order_bits(const EC_GROUP
*group
)
464 return group
->meth
->group_order_bits(group
);
467 int EC_GROUP_get_cofactor(const EC_GROUP
*group
, BIGNUM
*cofactor
,
471 if (group
->cofactor
== NULL
)
473 if (!BN_copy(cofactor
, group
->cofactor
))
476 return !BN_is_zero(group
->cofactor
);
479 const BIGNUM
*EC_GROUP_get0_cofactor(const EC_GROUP
*group
)
481 return group
->cofactor
;
484 void EC_GROUP_set_curve_name(EC_GROUP
*group
, int nid
)
486 group
->curve_name
= nid
;
489 ? OPENSSL_EC_NAMED_CURVE
490 : OPENSSL_EC_EXPLICIT_CURVE
;
493 int EC_GROUP_get_curve_name(const EC_GROUP
*group
)
495 return group
->curve_name
;
498 const BIGNUM
*EC_GROUP_get0_field(const EC_GROUP
*group
)
503 int EC_GROUP_get_field_type(const EC_GROUP
*group
)
505 return group
->meth
->field_type
;
508 void EC_GROUP_set_asn1_flag(EC_GROUP
*group
, int flag
)
510 group
->asn1_flag
= flag
;
513 int EC_GROUP_get_asn1_flag(const EC_GROUP
*group
)
515 return group
->asn1_flag
;
518 void EC_GROUP_set_point_conversion_form(EC_GROUP
*group
,
519 point_conversion_form_t form
)
521 group
->asn1_form
= form
;
524 point_conversion_form_t
EC_GROUP_get_point_conversion_form(const EC_GROUP
527 return group
->asn1_form
;
530 size_t EC_GROUP_set_seed(EC_GROUP
*group
, const unsigned char *p
, size_t len
)
532 OPENSSL_free(group
->seed
);
539 if ((group
->seed
= OPENSSL_malloc(len
)) == NULL
)
541 memcpy(group
->seed
, p
, len
);
542 group
->seed_len
= len
;
547 unsigned char *EC_GROUP_get0_seed(const EC_GROUP
*group
)
552 size_t EC_GROUP_get_seed_len(const EC_GROUP
*group
)
554 return group
->seed_len
;
557 int EC_GROUP_set_curve(EC_GROUP
*group
, const BIGNUM
*p
, const BIGNUM
*a
,
558 const BIGNUM
*b
, BN_CTX
*ctx
)
560 if (group
->meth
->group_set_curve
== 0) {
561 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
564 return group
->meth
->group_set_curve(group
, p
, a
, b
, ctx
);
567 int EC_GROUP_get_curve(const EC_GROUP
*group
, BIGNUM
*p
, BIGNUM
*a
, BIGNUM
*b
,
570 if (group
->meth
->group_get_curve
== NULL
) {
571 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
574 return group
->meth
->group_get_curve(group
, p
, a
, b
, ctx
);
577 #ifndef OPENSSL_NO_DEPRECATED_3_0
578 int EC_GROUP_set_curve_GFp(EC_GROUP
*group
, const BIGNUM
*p
, const BIGNUM
*a
,
579 const BIGNUM
*b
, BN_CTX
*ctx
)
581 return EC_GROUP_set_curve(group
, p
, a
, b
, ctx
);
584 int EC_GROUP_get_curve_GFp(const EC_GROUP
*group
, BIGNUM
*p
, BIGNUM
*a
,
585 BIGNUM
*b
, BN_CTX
*ctx
)
587 return EC_GROUP_get_curve(group
, p
, a
, b
, ctx
);
590 # ifndef OPENSSL_NO_EC2M
591 int EC_GROUP_set_curve_GF2m(EC_GROUP
*group
, const BIGNUM
*p
, const BIGNUM
*a
,
592 const BIGNUM
*b
, BN_CTX
*ctx
)
594 return EC_GROUP_set_curve(group
, p
, a
, b
, ctx
);
597 int EC_GROUP_get_curve_GF2m(const EC_GROUP
*group
, BIGNUM
*p
, BIGNUM
*a
,
598 BIGNUM
*b
, BN_CTX
*ctx
)
600 return EC_GROUP_get_curve(group
, p
, a
, b
, ctx
);
605 int EC_GROUP_get_degree(const EC_GROUP
*group
)
607 if (group
->meth
->group_get_degree
== 0) {
608 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
611 return group
->meth
->group_get_degree(group
);
614 int EC_GROUP_check_discriminant(const EC_GROUP
*group
, BN_CTX
*ctx
)
616 if (group
->meth
->group_check_discriminant
== 0) {
617 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
620 return group
->meth
->group_check_discriminant(group
, ctx
);
623 int EC_GROUP_cmp(const EC_GROUP
*a
, const EC_GROUP
*b
, BN_CTX
*ctx
)
626 BIGNUM
*a1
, *a2
, *a3
, *b1
, *b2
, *b3
;
628 BN_CTX
*ctx_new
= NULL
;
631 /* compare the field types */
632 if (EC_GROUP_get_field_type(a
) != EC_GROUP_get_field_type(b
))
634 /* compare the curve name (if present in both) */
635 if (EC_GROUP_get_curve_name(a
) && EC_GROUP_get_curve_name(b
) &&
636 EC_GROUP_get_curve_name(a
) != EC_GROUP_get_curve_name(b
))
638 if (a
->meth
->flags
& EC_FLAGS_CUSTOM_CURVE
)
643 ctx_new
= ctx
= BN_CTX_new();
649 a1
= BN_CTX_get(ctx
);
650 a2
= BN_CTX_get(ctx
);
651 a3
= BN_CTX_get(ctx
);
652 b1
= BN_CTX_get(ctx
);
653 b2
= BN_CTX_get(ctx
);
654 b3
= BN_CTX_get(ctx
);
658 BN_CTX_free(ctx_new
);
664 * XXX This approach assumes that the external representation of curves
665 * over the same field type is the same.
667 if (!a
->meth
->group_get_curve(a
, a1
, a2
, a3
, ctx
) ||
668 !b
->meth
->group_get_curve(b
, b1
, b2
, b3
, ctx
))
671 /* return 1 if the curve parameters are different */
672 if (r
|| BN_cmp(a1
, b1
) != 0 || BN_cmp(a2
, b2
) != 0 || BN_cmp(a3
, b3
) != 0)
675 /* XXX EC_POINT_cmp() assumes that the methods are equal */
676 /* return 1 if the generators are different */
677 if (r
|| EC_POINT_cmp(a
, EC_GROUP_get0_generator(a
),
678 EC_GROUP_get0_generator(b
), ctx
) != 0)
682 const BIGNUM
*ao
, *bo
, *ac
, *bc
;
683 /* compare the orders */
684 ao
= EC_GROUP_get0_order(a
);
685 bo
= EC_GROUP_get0_order(b
);
686 if (ao
== NULL
|| bo
== NULL
) {
687 /* return an error if either order is NULL */
691 if (BN_cmp(ao
, bo
) != 0) {
692 /* return 1 if orders are different */
697 * It gets here if the curve parameters and generator matched.
698 * Now check the optional cofactors (if both are present).
700 ac
= EC_GROUP_get0_cofactor(a
);
701 bc
= EC_GROUP_get0_cofactor(b
);
702 /* Returns 1 (mismatch) if both cofactors are specified and different */
703 if (!BN_is_zero(ac
) && !BN_is_zero(bc
) && BN_cmp(ac
, bc
) != 0)
705 /* Returns 0 if the parameters matched */
710 BN_CTX_free(ctx_new
);
715 /* functions for EC_POINT objects */
717 EC_POINT
*EC_POINT_new(const EC_GROUP
*group
)
722 ERR_raise(ERR_LIB_EC
, ERR_R_PASSED_NULL_PARAMETER
);
725 if (group
->meth
->point_init
== NULL
) {
726 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
730 ret
= OPENSSL_zalloc(sizeof(*ret
));
734 ret
->meth
= group
->meth
;
735 ret
->curve_name
= group
->curve_name
;
737 if (!ret
->meth
->point_init(ret
)) {
745 void EC_POINT_free(EC_POINT
*point
)
750 #ifdef OPENSSL_PEDANTIC_ZEROIZATION
751 EC_POINT_clear_free(point
);
753 if (point
->meth
->point_finish
!= 0)
754 point
->meth
->point_finish(point
);
759 void EC_POINT_clear_free(EC_POINT
*point
)
764 if (point
->meth
->point_clear_finish
!= 0)
765 point
->meth
->point_clear_finish(point
);
766 else if (point
->meth
->point_finish
!= 0)
767 point
->meth
->point_finish(point
);
768 OPENSSL_clear_free(point
, sizeof(*point
));
771 int EC_POINT_copy(EC_POINT
*dest
, const EC_POINT
*src
)
773 if (dest
->meth
->point_copy
== 0) {
774 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
777 if (dest
->meth
!= src
->meth
778 || (dest
->curve_name
!= src
->curve_name
779 && dest
->curve_name
!= 0
780 && src
->curve_name
!= 0)) {
781 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
786 return dest
->meth
->point_copy(dest
, src
);
789 EC_POINT
*EC_POINT_dup(const EC_POINT
*a
, const EC_GROUP
*group
)
797 t
= EC_POINT_new(group
);
800 r
= EC_POINT_copy(t
, a
);
808 #ifndef OPENSSL_NO_DEPRECATED_3_0
809 const EC_METHOD
*EC_POINT_method_of(const EC_POINT
*point
)
815 int EC_POINT_set_to_infinity(const EC_GROUP
*group
, EC_POINT
*point
)
817 if (group
->meth
->point_set_to_infinity
== 0) {
818 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
821 if (group
->meth
!= point
->meth
) {
822 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
825 return group
->meth
->point_set_to_infinity(group
, point
);
828 #ifndef OPENSSL_NO_DEPRECATED_3_0
829 int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP
*group
,
830 EC_POINT
*point
, const BIGNUM
*x
,
831 const BIGNUM
*y
, const BIGNUM
*z
,
834 if (group
->meth
->field_type
!= NID_X9_62_prime_field
) {
835 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
838 if (!ec_point_is_compat(point
, group
)) {
839 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
842 return ossl_ec_GFp_simple_set_Jprojective_coordinates_GFp(group
, point
,
846 int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP
*group
,
847 const EC_POINT
*point
, BIGNUM
*x
,
848 BIGNUM
*y
, BIGNUM
*z
,
851 if (group
->meth
->field_type
!= NID_X9_62_prime_field
) {
852 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
855 if (!ec_point_is_compat(point
, group
)) {
856 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
859 return ossl_ec_GFp_simple_get_Jprojective_coordinates_GFp(group
, point
,
864 int EC_POINT_set_affine_coordinates(const EC_GROUP
*group
, EC_POINT
*point
,
865 const BIGNUM
*x
, const BIGNUM
*y
,
868 if (group
->meth
->point_set_affine_coordinates
== NULL
) {
869 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
872 if (!ec_point_is_compat(point
, group
)) {
873 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
876 if (!group
->meth
->point_set_affine_coordinates(group
, point
, x
, y
, ctx
))
879 if (EC_POINT_is_on_curve(group
, point
, ctx
) <= 0) {
880 ERR_raise(ERR_LIB_EC
, EC_R_POINT_IS_NOT_ON_CURVE
);
886 #ifndef OPENSSL_NO_DEPRECATED_3_0
887 int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP
*group
,
888 EC_POINT
*point
, const BIGNUM
*x
,
889 const BIGNUM
*y
, BN_CTX
*ctx
)
891 return EC_POINT_set_affine_coordinates(group
, point
, x
, y
, ctx
);
894 # ifndef OPENSSL_NO_EC2M
895 int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP
*group
,
896 EC_POINT
*point
, const BIGNUM
*x
,
897 const BIGNUM
*y
, BN_CTX
*ctx
)
899 return EC_POINT_set_affine_coordinates(group
, point
, x
, y
, ctx
);
904 int EC_POINT_get_affine_coordinates(const EC_GROUP
*group
,
905 const EC_POINT
*point
, BIGNUM
*x
, BIGNUM
*y
,
908 if (group
->meth
->point_get_affine_coordinates
== NULL
) {
909 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
912 if (!ec_point_is_compat(point
, group
)) {
913 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
916 if (EC_POINT_is_at_infinity(group
, point
)) {
917 ERR_raise(ERR_LIB_EC
, EC_R_POINT_AT_INFINITY
);
920 return group
->meth
->point_get_affine_coordinates(group
, point
, x
, y
, ctx
);
923 #ifndef OPENSSL_NO_DEPRECATED_3_0
924 int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP
*group
,
925 const EC_POINT
*point
, BIGNUM
*x
,
926 BIGNUM
*y
, BN_CTX
*ctx
)
928 return EC_POINT_get_affine_coordinates(group
, point
, x
, y
, ctx
);
931 # ifndef OPENSSL_NO_EC2M
932 int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP
*group
,
933 const EC_POINT
*point
, BIGNUM
*x
,
934 BIGNUM
*y
, BN_CTX
*ctx
)
936 return EC_POINT_get_affine_coordinates(group
, point
, x
, y
, ctx
);
941 int EC_POINT_add(const EC_GROUP
*group
, EC_POINT
*r
, const EC_POINT
*a
,
942 const EC_POINT
*b
, BN_CTX
*ctx
)
944 if (group
->meth
->add
== 0) {
945 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
948 if (!ec_point_is_compat(r
, group
) || !ec_point_is_compat(a
, group
)
949 || !ec_point_is_compat(b
, group
)) {
950 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
953 return group
->meth
->add(group
, r
, a
, b
, ctx
);
956 int EC_POINT_dbl(const EC_GROUP
*group
, EC_POINT
*r
, const EC_POINT
*a
,
959 if (group
->meth
->dbl
== 0) {
960 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
963 if (!ec_point_is_compat(r
, group
) || !ec_point_is_compat(a
, group
)) {
964 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
967 return group
->meth
->dbl(group
, r
, a
, ctx
);
970 int EC_POINT_invert(const EC_GROUP
*group
, EC_POINT
*a
, BN_CTX
*ctx
)
972 if (group
->meth
->invert
== 0) {
973 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
976 if (!ec_point_is_compat(a
, group
)) {
977 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
980 return group
->meth
->invert(group
, a
, ctx
);
983 int EC_POINT_is_at_infinity(const EC_GROUP
*group
, const EC_POINT
*point
)
985 if (group
->meth
->is_at_infinity
== 0) {
986 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
989 if (!ec_point_is_compat(point
, group
)) {
990 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
993 return group
->meth
->is_at_infinity(group
, point
);
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
1003 int EC_POINT_is_on_curve(const EC_GROUP
*group
, const EC_POINT
*point
,
1006 if (group
->meth
->is_on_curve
== 0) {
1007 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1010 if (!ec_point_is_compat(point
, group
)) {
1011 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
1014 return group
->meth
->is_on_curve(group
, point
, ctx
);
1017 int EC_POINT_cmp(const EC_GROUP
*group
, const EC_POINT
*a
, const EC_POINT
*b
,
1020 if (group
->meth
->point_cmp
== 0) {
1021 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1024 if (!ec_point_is_compat(a
, group
) || !ec_point_is_compat(b
, group
)) {
1025 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
1028 return group
->meth
->point_cmp(group
, a
, b
, ctx
);
1031 #ifndef OPENSSL_NO_DEPRECATED_3_0
1032 int EC_POINT_make_affine(const EC_GROUP
*group
, EC_POINT
*point
, BN_CTX
*ctx
)
1034 if (group
->meth
->make_affine
== 0) {
1035 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1038 if (!ec_point_is_compat(point
, group
)) {
1039 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
1042 return group
->meth
->make_affine(group
, point
, ctx
);
1045 int EC_POINTs_make_affine(const EC_GROUP
*group
, size_t num
,
1046 EC_POINT
*points
[], BN_CTX
*ctx
)
1050 if (group
->meth
->points_make_affine
== 0) {
1051 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1054 for (i
= 0; i
< num
; i
++) {
1055 if (!ec_point_is_compat(points
[i
], group
)) {
1056 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
1060 return group
->meth
->points_make_affine(group
, num
, points
, ctx
);
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
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
)
1078 BN_CTX
*new_ctx
= NULL
;
1081 if (!ec_point_is_compat(r
, group
)) {
1082 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
1086 if (scalar
== NULL
&& num
== 0)
1087 return EC_POINT_set_to_infinity(group
, r
);
1089 for (i
= 0; i
< num
; i
++) {
1090 if (!ec_point_is_compat(points
[i
], group
)) {
1091 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
1098 ctx
= new_ctx
= BN_CTX_secure_new();
1101 ERR_raise(ERR_LIB_EC
, ERR_R_INTERNAL_ERROR
);
1105 if (group
->meth
->mul
!= NULL
)
1106 ret
= group
->meth
->mul(group
, r
, scalar
, num
, points
, scalars
, ctx
);
1109 ret
= ossl_ec_wNAF_mul(group
, r
, scalar
, num
, points
, scalars
, ctx
);
1112 BN_CTX_free(new_ctx
);
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
)
1124 BN_CTX
*new_ctx
= NULL
;
1127 if (!ec_point_is_compat(r
, group
)
1128 || (point
!= NULL
&& !ec_point_is_compat(point
, group
))) {
1129 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
1133 if (g_scalar
== NULL
&& p_scalar
== NULL
)
1134 return EC_POINT_set_to_infinity(group
, r
);
1138 ctx
= new_ctx
= BN_CTX_secure_new();
1141 ERR_raise(ERR_LIB_EC
, ERR_R_INTERNAL_ERROR
);
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
);
1150 ret
= ossl_ec_wNAF_mul(group
, r
, g_scalar
, num
, &point
, &p_scalar
, ctx
);
1153 BN_CTX_free(new_ctx
);
1158 #ifndef OPENSSL_NO_DEPRECATED_3_0
1159 int EC_GROUP_precompute_mult(EC_GROUP
*group
, BN_CTX
*ctx
)
1161 if (group
->meth
->mul
== 0)
1163 return ossl_ec_wNAF_precompute_mult(group
, ctx
);
1165 if (group
->meth
->precompute_mult
!= 0)
1166 return group
->meth
->precompute_mult(group
, ctx
);
1168 return 1; /* nothing to do, so report success */
1171 int EC_GROUP_have_precompute_mult(const EC_GROUP
*group
)
1173 if (group
->meth
->mul
== 0)
1175 return ossl_ec_wNAF_have_precompute_mult(group
);
1177 if (group
->meth
->have_precompute_mult
!= 0)
1178 return group
->meth
->have_precompute_mult(group
);
1180 return 0; /* cannot tell whether precomputation has
1186 * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
1187 * returns one on success. On error it returns zero.
1189 static int ec_precompute_mont_data(EC_GROUP
*group
)
1191 BN_CTX
*ctx
= BN_CTX_new_ex(group
->libctx
);
1194 BN_MONT_CTX_free(group
->mont_data
);
1195 group
->mont_data
= NULL
;
1200 group
->mont_data
= BN_MONT_CTX_new();
1201 if (group
->mont_data
== NULL
)
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
;
1219 int EC_KEY_set_ex_data(EC_KEY
*key
, int idx
, void *arg
)
1221 return CRYPTO_set_ex_data(&key
->ex_data
, idx
, arg
);
1224 void *EC_KEY_get_ex_data(const EC_KEY
*key
, int idx
)
1226 return CRYPTO_get_ex_data(&key
->ex_data
, idx
);
1230 int ossl_ec_group_simple_order_bits(const EC_GROUP
*group
)
1232 if (group
->order
== NULL
)
1234 return BN_num_bits(group
->order
);
1237 static int ec_field_inverse_mod_ord(const EC_GROUP
*group
, BIGNUM
*r
,
1238 const BIGNUM
*x
, BN_CTX
*ctx
)
1243 BN_CTX
*new_ctx
= NULL
;
1246 if (group
->mont_data
== NULL
)
1251 ctx
= new_ctx
= BN_CTX_secure_new();
1257 if ((e
= BN_CTX_get(ctx
)) == NULL
)
1261 * We want inverse in constant time, therefore we utilize the fact
1262 * order must be prime and use Fermats Little Theorem instead.
1264 if (!BN_set_word(e
, 2))
1266 if (!BN_sub(e
, group
->order
, e
))
1269 * Although the exponent is public we want the result to be
1272 if (!bn_mod_exp_mont_fixed_top(r
, x
, e
, group
->order
, ctx
, group
->mont_data
))
1280 BN_CTX_free(new_ctx
);
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).
1295 * EC_METHODs must implement their own field_inverse_mod_ord for
1296 * other functionality.
1298 int ossl_ec_group_do_inverse_ord(const EC_GROUP
*group
, BIGNUM
*res
,
1299 const BIGNUM
*x
, BN_CTX
*ctx
)
1301 if (group
->meth
->field_inverse_mod_ord
!= NULL
)
1302 return group
->meth
->field_inverse_mod_ord(group
, res
, x
, ctx
);
1304 return ec_field_inverse_mod_ord(group
, res
, x
, ctx
);
1308 * Coordinate blinding for EC_POINT.
1310 * The underlying EC_METHOD can optionally implement this function:
1311 * underlying implementations should return 0 on errors, or 1 on
1314 * This wrapper returns 1 in case the underlying EC_METHOD does not
1315 * support coordinate blinding.
1317 int ossl_ec_point_blind_coordinates(const EC_GROUP
*group
, EC_POINT
*p
,
1320 if (group
->meth
->blind_coordinates
== NULL
)
1321 return 1; /* ignore if not implemented */
1323 return group
->meth
->blind_coordinates(group
, p
, ctx
);
1326 int EC_GROUP_get_basis_type(const EC_GROUP
*group
)
1330 if (EC_GROUP_get_field_type(group
) != NID_X9_62_characteristic_two_field
)
1331 /* everything else is currently not supported */
1334 /* Find the last non-zero element of group->poly[] */
1336 i
< (int)OSSL_NELEM(group
->poly
) && group
->poly
[i
] != 0;
1341 return NID_X9_62_ppBasis
;
1343 return NID_X9_62_tpBasis
;
1345 /* everything else is currently not supported */
1349 #ifndef OPENSSL_NO_EC2M
1350 int EC_GROUP_get_trinomial_basis(const EC_GROUP
*group
, unsigned int *k
)
1355 if (EC_GROUP_get_field_type(group
) != NID_X9_62_characteristic_two_field
1356 || !((group
->poly
[0] != 0) && (group
->poly
[1] != 0)
1357 && (group
->poly
[2] == 0))) {
1358 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1363 *k
= group
->poly
[1];
1368 int EC_GROUP_get_pentanomial_basis(const EC_GROUP
*group
, unsigned int *k1
,
1369 unsigned int *k2
, unsigned int *k3
)
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 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1383 *k1
= group
->poly
[3];
1385 *k2
= group
->poly
[2];
1387 *k3
= group
->poly
[1];
1395 * Check if the explicit parameters group matches any built-in curves.
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.
1405 static EC_GROUP
*ec_group_explicit_to_named(const EC_GROUP
*group
,
1406 OSSL_LIB_CTX
*libctx
,
1410 EC_GROUP
*ret_group
= NULL
, *dup
= NULL
;
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
);
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
))
1421 if ((curve_name_nid
= ossl_ec_curve_nid_from_params(dup
, ctx
)) != NID_undef
) {
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.
1427 * In this case we replace the `EC_GROUP` created through explicit
1428 * parameters with one created from a named group.
1431 # ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
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.
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) */
1441 ret_group
= EC_GROUP_new_by_curve_name_ex(libctx
, propq
, curve_name_nid
);
1442 if (ret_group
== NULL
)
1446 * Set the flag so that EC_GROUPs created from explicit parameters are
1447 * serialized using explicit parameters by default.
1449 EC_GROUP_set_asn1_flag(ret_group
, OPENSSL_EC_EXPLICIT_CURVE
);
1452 * If the input params do not contain the optional seed field we make
1453 * sure it is not added to the returned group.
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.
1461 if (EC_GROUP_set_seed(ret_group
, NULL
, 0) != 1)
1465 ret_group
= (EC_GROUP
*)group
;
1471 EC_GROUP_free(ret_group
);
1474 #endif /* FIPS_MODULE */
1476 static EC_GROUP
*group_new_from_name(const OSSL_PARAM
*p
,
1477 OSSL_LIB_CTX
*libctx
, const char *propq
)
1480 const char *curve_name
= NULL
;
1482 switch (p
->data_type
) {
1483 case OSSL_PARAM_UTF8_STRING
:
1484 /* The OSSL_PARAM functions have no support for this */
1485 curve_name
= p
->data
;
1486 ok
= (curve_name
!= NULL
);
1488 case OSSL_PARAM_UTF8_PTR
:
1489 ok
= OSSL_PARAM_get_utf8_ptr(p
, &curve_name
);
1494 nid
= ossl_ec_curve_name2nid(curve_name
);
1495 if (nid
== NID_undef
) {
1496 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_CURVE
);
1499 return EC_GROUP_new_by_curve_name_ex(libctx
, propq
, nid
);
1505 /* These parameters can be set directly into an EC_GROUP */
1506 int ossl_ec_group_set_params(EC_GROUP
*group
, const OSSL_PARAM params
[])
1508 int encoding_flag
= -1, format
= -1;
1509 const OSSL_PARAM
*p
;
1511 p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT
);
1513 if (!ossl_ec_pt_format_param2id(p
, &format
)) {
1514 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_FORM
);
1517 EC_GROUP_set_point_conversion_form(group
, format
);
1520 p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_ENCODING
);
1522 if (!ossl_ec_encoding_param2id(p
, &encoding_flag
)) {
1523 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_FORM
);
1526 EC_GROUP_set_asn1_flag(group
, encoding_flag
);
1529 p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_SEED
);
1531 /* The seed is allowed to be NULL */
1532 if (p
->data_type
!= OSSL_PARAM_OCTET_STRING
1533 || !EC_GROUP_set_seed(group
, p
->data
, p
->data_size
)) {
1534 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_SEED
);
1541 EC_GROUP
*EC_GROUP_new_from_params(const OSSL_PARAM params
[],
1542 OSSL_LIB_CTX
*libctx
, const char *propq
)
1544 const OSSL_PARAM
*ptmp
;
1545 EC_GROUP
*group
= NULL
;
1548 const OSSL_PARAM
*pa
, *pb
;
1550 EC_GROUP
*named_group
= NULL
;
1551 BIGNUM
*p
= NULL
, *a
= NULL
, *b
= NULL
, *order
= NULL
, *cofactor
= NULL
;
1552 EC_POINT
*point
= NULL
;
1554 int is_prime_field
= 1;
1555 BN_CTX
*bnctx
= NULL
;
1556 const unsigned char *buf
= NULL
;
1557 int encoding_flag
= -1;
1560 /* This is the simple named group case */
1561 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_GROUP_NAME
);
1565 if ((group
= group_new_from_name(ptmp
, libctx
, propq
)) == NULL
)
1567 if (!ossl_ec_group_set_params(group
, params
)) {
1568 EC_GROUP_free(group
);
1572 ptmp
= OSSL_PARAM_locate_const(params
,
1573 OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS
);
1574 if (ptmp
!= NULL
&& !OSSL_PARAM_get_int(ptmp
, &decoded
)) {
1575 ERR_raise(ERR_LIB_EC
, EC_R_WRONG_CURVE_PARAMETERS
);
1576 EC_GROUP_free(group
);
1579 group
->decoded_from_explicit_params
= decoded
> 0;
1583 ERR_raise(ERR_LIB_EC
, EC_R_EXPLICIT_PARAMS_NOT_SUPPORTED
);
1586 /* If it gets here then we are trying explicit parameters */
1587 bnctx
= BN_CTX_new_ex(libctx
);
1588 if (bnctx
== NULL
) {
1589 ERR_raise(ERR_LIB_EC
, ERR_R_BN_LIB
);
1592 BN_CTX_start(bnctx
);
1594 p
= BN_CTX_get(bnctx
);
1595 a
= BN_CTX_get(bnctx
);
1596 b
= BN_CTX_get(bnctx
);
1597 order
= BN_CTX_get(bnctx
);
1598 if (order
== NULL
) {
1599 ERR_raise(ERR_LIB_EC
, ERR_R_BN_LIB
);
1603 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_FIELD_TYPE
);
1604 if (ptmp
== NULL
|| ptmp
->data_type
!= OSSL_PARAM_UTF8_STRING
) {
1605 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_FIELD
);
1608 if (OPENSSL_strcasecmp(ptmp
->data
, SN_X9_62_prime_field
) == 0) {
1610 } else if (OPENSSL_strcasecmp(ptmp
->data
,
1611 SN_X9_62_characteristic_two_field
) == 0) {
1615 ERR_raise(ERR_LIB_EC
, EC_R_UNSUPPORTED_FIELD
);
1619 pa
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_A
);
1620 if (!OSSL_PARAM_get_BN(pa
, &a
)) {
1621 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_A
);
1624 pb
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_B
);
1625 if (!OSSL_PARAM_get_BN(pb
, &b
)) {
1626 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_B
);
1630 /* extract the prime number or irreducible polynomial */
1631 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_P
);
1632 if (!OSSL_PARAM_get_BN(ptmp
, &p
)) {
1633 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_P
);
1637 if (is_prime_field
) {
1638 if (BN_is_negative(p
) || BN_is_zero(p
)) {
1639 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_P
);
1642 field_bits
= BN_num_bits(p
);
1643 if (field_bits
> OPENSSL_ECC_MAX_FIELD_BITS
) {
1644 ERR_raise(ERR_LIB_EC
, EC_R_FIELD_TOO_LARGE
);
1648 /* create the EC_GROUP structure */
1649 group
= EC_GROUP_new_curve_GFp(p
, a
, b
, bnctx
);
1651 # ifdef OPENSSL_NO_EC2M
1652 ERR_raise(ERR_LIB_EC
, EC_R_GF2M_NOT_SUPPORTED
);
1655 /* create the EC_GROUP structure */
1656 group
= EC_GROUP_new_curve_GF2m(p
, a
, b
, NULL
);
1657 if (group
!= NULL
) {
1658 field_bits
= EC_GROUP_get_degree(group
);
1659 if (field_bits
> OPENSSL_ECC_MAX_FIELD_BITS
) {
1660 ERR_raise(ERR_LIB_EC
, EC_R_FIELD_TOO_LARGE
);
1664 # endif /* OPENSSL_NO_EC2M */
1667 if (group
== NULL
) {
1668 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
1673 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_SEED
);
1675 if (ptmp
->data_type
!= OSSL_PARAM_OCTET_STRING
) {
1676 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_SEED
);
1679 if (!EC_GROUP_set_seed(group
, ptmp
->data
, ptmp
->data_size
))
1683 /* generator base point */
1684 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_GENERATOR
);
1686 || ptmp
->data_type
!= OSSL_PARAM_OCTET_STRING
) {
1687 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_GENERATOR
);
1690 buf
= (const unsigned char *)(ptmp
->data
);
1691 if ((point
= EC_POINT_new(group
)) == NULL
)
1693 EC_GROUP_set_point_conversion_form(group
,
1694 (point_conversion_form_t
)buf
[0] & ~0x01);
1695 if (!EC_POINT_oct2point(group
, point
, buf
, ptmp
->data_size
, bnctx
)) {
1696 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_GENERATOR
);
1701 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_ORDER
);
1702 if (!OSSL_PARAM_get_BN(ptmp
, &order
)
1703 || (BN_is_negative(order
) || BN_is_zero(order
))
1704 || (BN_num_bits(order
) > (int)field_bits
+ 1)) { /* Hasse bound */
1705 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_GROUP_ORDER
);
1709 /* Optional cofactor */
1710 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_COFACTOR
);
1712 cofactor
= BN_CTX_get(bnctx
);
1713 if (cofactor
== NULL
|| !OSSL_PARAM_get_BN(ptmp
, &cofactor
)) {
1714 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_COFACTOR
);
1719 /* set the generator, order and cofactor (if present) */
1720 if (!EC_GROUP_set_generator(group
, point
, order
, cofactor
)) {
1721 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_GENERATOR
);
1725 named_group
= ec_group_explicit_to_named(group
, libctx
, propq
, bnctx
);
1726 if (named_group
== NULL
) {
1727 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_NAMED_GROUP_CONVERSION
);
1730 if (named_group
== group
) {
1732 * If we did not find a named group then the encoding should be explicit
1733 * if it was specified
1735 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_ENCODING
);
1737 && !ossl_ec_encoding_param2id(ptmp
, &encoding_flag
)) {
1738 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_ENCODING
);
1741 if (encoding_flag
== OPENSSL_EC_NAMED_CURVE
) {
1742 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_ENCODING
);
1745 EC_GROUP_set_asn1_flag(group
, OPENSSL_EC_EXPLICIT_CURVE
);
1747 EC_GROUP_free(group
);
1748 group
= named_group
;
1750 /* We've imported the group from explicit parameters, set it so. */
1751 group
->decoded_from_explicit_params
= 1;
1755 EC_GROUP_free(group
);
1758 EC_POINT_free(point
);
1763 #endif /* FIPS_MODULE */
1766 OSSL_PARAM
*EC_GROUP_to_params(const EC_GROUP
*group
, OSSL_LIB_CTX
*libctx
,
1767 const char *propq
, BN_CTX
*bnctx
)
1769 OSSL_PARAM_BLD
*tmpl
= NULL
;
1770 BN_CTX
*new_bnctx
= NULL
;
1771 unsigned char *gen_buf
= NULL
;
1772 OSSL_PARAM
*params
= NULL
;
1777 tmpl
= OSSL_PARAM_BLD_new();
1782 bnctx
= new_bnctx
= BN_CTX_new_ex(libctx
);
1785 BN_CTX_start(bnctx
);
1787 if (!ossl_ec_group_todata(
1788 group
, tmpl
, NULL
, libctx
, propq
, bnctx
, &gen_buf
))
1791 params
= OSSL_PARAM_BLD_to_param(tmpl
);
1794 OSSL_PARAM_BLD_free(tmpl
);
1795 OPENSSL_free(gen_buf
);
1797 BN_CTX_free(new_bnctx
);