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_with_libctx(OPENSSL_CTX
*libctx
, const char *propq
,
30 const EC_METHOD
*meth
)
35 ECerr(0, EC_R_SLOT_FULL
);
38 if (meth
->group_init
== 0) {
39 ECerr(0, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
43 ret
= OPENSSL_zalloc(sizeof(*ret
));
45 ECerr(0, ERR_R_MALLOC_FAILURE
);
51 ret
->propq
= OPENSSL_strdup(propq
);
52 if (ret
->propq
== NULL
) {
53 ECerr(0, 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_with_libctx(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 ECerr(EC_F_EC_GROUP_COPY
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
167 if (dest
->meth
!= src
->meth
) {
168 ECerr(EC_F_EC_GROUP_COPY
, 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 ECerr(EC_F_EC_GROUP_COPY
, 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_with_libctx(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 ECerr(EC_F_EC_GROUP_SET_GENERATOR
, 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 ECerr(EC_F_EC_GROUP_SET_GENERATOR
, 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 ECerr(EC_F_EC_GROUP_SET_GENERATOR
, 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 ECerr(EC_F_EC_GROUP_SET_GENERATOR
, 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 ECerr(EC_F_EC_GROUP_SET_SEED
, 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 ECerr(EC_F_EC_GROUP_SET_CURVE
, 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 ECerr(EC_F_EC_GROUP_GET_CURVE
, 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 ECerr(EC_F_EC_GROUP_GET_DEGREE
, 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 ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT
,
613 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
616 return group
->meth
->group_check_discriminant(group
, ctx
);
619 int EC_GROUP_cmp(const EC_GROUP
*a
, const EC_GROUP
*b
, BN_CTX
*ctx
)
622 BIGNUM
*a1
, *a2
, *a3
, *b1
, *b2
, *b3
;
624 BN_CTX
*ctx_new
= NULL
;
627 /* compare the field types */
628 if (EC_GROUP_get_field_type(a
) != EC_GROUP_get_field_type(b
))
630 /* compare the curve name (if present in both) */
631 if (EC_GROUP_get_curve_name(a
) && EC_GROUP_get_curve_name(b
) &&
632 EC_GROUP_get_curve_name(a
) != EC_GROUP_get_curve_name(b
))
634 if (a
->meth
->flags
& EC_FLAGS_CUSTOM_CURVE
)
639 ctx_new
= ctx
= BN_CTX_new();
645 a1
= BN_CTX_get(ctx
);
646 a2
= BN_CTX_get(ctx
);
647 a3
= BN_CTX_get(ctx
);
648 b1
= BN_CTX_get(ctx
);
649 b2
= BN_CTX_get(ctx
);
650 b3
= BN_CTX_get(ctx
);
654 BN_CTX_free(ctx_new
);
660 * XXX This approach assumes that the external representation of curves
661 * over the same field type is the same.
663 if (!a
->meth
->group_get_curve(a
, a1
, a2
, a3
, ctx
) ||
664 !b
->meth
->group_get_curve(b
, b1
, b2
, b3
, ctx
))
667 /* return 1 if the curve parameters are different */
668 if (r
|| BN_cmp(a1
, b1
) != 0 || BN_cmp(a2
, b2
) != 0 || BN_cmp(a3
, b3
) != 0)
671 /* XXX EC_POINT_cmp() assumes that the methods are equal */
672 /* return 1 if the generators are different */
673 if (r
|| EC_POINT_cmp(a
, EC_GROUP_get0_generator(a
),
674 EC_GROUP_get0_generator(b
), ctx
) != 0)
678 const BIGNUM
*ao
, *bo
, *ac
, *bc
;
679 /* compare the orders */
680 ao
= EC_GROUP_get0_order(a
);
681 bo
= EC_GROUP_get0_order(b
);
682 if (ao
== NULL
|| bo
== NULL
) {
683 /* return an error if either order is NULL */
687 if (BN_cmp(ao
, bo
) != 0) {
688 /* return 1 if orders are different */
693 * It gets here if the curve parameters and generator matched.
694 * Now check the optional cofactors (if both are present).
696 ac
= EC_GROUP_get0_cofactor(a
);
697 bc
= EC_GROUP_get0_cofactor(b
);
698 /* Returns 1 (mismatch) if both cofactors are specified and different */
699 if (!BN_is_zero(ac
) && !BN_is_zero(bc
) && BN_cmp(ac
, bc
) != 0)
701 /* Returns 0 if the parameters matched */
706 BN_CTX_free(ctx_new
);
711 /* functions for EC_POINT objects */
713 EC_POINT
*EC_POINT_new(const EC_GROUP
*group
)
718 ECerr(EC_F_EC_POINT_NEW
, ERR_R_PASSED_NULL_PARAMETER
);
721 if (group
->meth
->point_init
== NULL
) {
722 ECerr(EC_F_EC_POINT_NEW
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
726 ret
= OPENSSL_zalloc(sizeof(*ret
));
728 ECerr(EC_F_EC_POINT_NEW
, ERR_R_MALLOC_FAILURE
);
732 ret
->meth
= group
->meth
;
733 ret
->curve_name
= group
->curve_name
;
735 if (!ret
->meth
->point_init(ret
)) {
743 void EC_POINT_free(EC_POINT
*point
)
748 if (point
->meth
->point_finish
!= 0)
749 point
->meth
->point_finish(point
);
753 void EC_POINT_clear_free(EC_POINT
*point
)
758 if (point
->meth
->point_clear_finish
!= 0)
759 point
->meth
->point_clear_finish(point
);
760 else if (point
->meth
->point_finish
!= 0)
761 point
->meth
->point_finish(point
);
762 OPENSSL_clear_free(point
, sizeof(*point
));
765 int EC_POINT_copy(EC_POINT
*dest
, const EC_POINT
*src
)
767 if (dest
->meth
->point_copy
== 0) {
768 ECerr(EC_F_EC_POINT_COPY
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
771 if (dest
->meth
!= src
->meth
772 || (dest
->curve_name
!= src
->curve_name
773 && dest
->curve_name
!= 0
774 && src
->curve_name
!= 0)) {
775 ECerr(EC_F_EC_POINT_COPY
, EC_R_INCOMPATIBLE_OBJECTS
);
780 return dest
->meth
->point_copy(dest
, src
);
783 EC_POINT
*EC_POINT_dup(const EC_POINT
*a
, const EC_GROUP
*group
)
791 t
= EC_POINT_new(group
);
794 r
= EC_POINT_copy(t
, a
);
802 #ifndef OPENSSL_NO_DEPRECATED_3_0
803 const EC_METHOD
*EC_POINT_method_of(const EC_POINT
*point
)
809 int EC_POINT_set_to_infinity(const EC_GROUP
*group
, EC_POINT
*point
)
811 if (group
->meth
->point_set_to_infinity
== 0) {
812 ECerr(EC_F_EC_POINT_SET_TO_INFINITY
,
813 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
816 if (group
->meth
!= point
->meth
) {
817 ECerr(EC_F_EC_POINT_SET_TO_INFINITY
, EC_R_INCOMPATIBLE_OBJECTS
);
820 return group
->meth
->point_set_to_infinity(group
, point
);
823 #ifndef OPENSSL_NO_DEPRECATED_3_0
824 int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP
*group
,
825 EC_POINT
*point
, const BIGNUM
*x
,
826 const BIGNUM
*y
, const BIGNUM
*z
,
829 if (group
->meth
->field_type
!= NID_X9_62_prime_field
) {
830 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP
,
831 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
834 if (!ec_point_is_compat(point
, group
)) {
835 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP
,
836 EC_R_INCOMPATIBLE_OBJECTS
);
839 return ec_GFp_simple_set_Jprojective_coordinates_GFp(group
, point
, x
, y
, z
, ctx
);
842 int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP
*group
,
843 const EC_POINT
*point
, BIGNUM
*x
,
844 BIGNUM
*y
, BIGNUM
*z
,
847 if (group
->meth
->field_type
!= NID_X9_62_prime_field
) {
848 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP
,
849 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
852 if (!ec_point_is_compat(point
, group
)) {
853 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP
,
854 EC_R_INCOMPATIBLE_OBJECTS
);
857 return ec_GFp_simple_get_Jprojective_coordinates_GFp(group
, point
, x
, y
, z
, ctx
);
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 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES
,
867 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
870 if (!ec_point_is_compat(point
, group
)) {
871 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES
, EC_R_INCOMPATIBLE_OBJECTS
);
874 if (!group
->meth
->point_set_affine_coordinates(group
, point
, x
, y
, ctx
))
877 if (EC_POINT_is_on_curve(group
, point
, ctx
) <= 0) {
878 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES
, EC_R_POINT_IS_NOT_ON_CURVE
);
884 #ifndef OPENSSL_NO_DEPRECATED_3_0
885 int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP
*group
,
886 EC_POINT
*point
, const BIGNUM
*x
,
887 const BIGNUM
*y
, BN_CTX
*ctx
)
889 return EC_POINT_set_affine_coordinates(group
, point
, x
, y
, ctx
);
892 # ifndef OPENSSL_NO_EC2M
893 int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP
*group
,
894 EC_POINT
*point
, const BIGNUM
*x
,
895 const BIGNUM
*y
, BN_CTX
*ctx
)
897 return EC_POINT_set_affine_coordinates(group
, point
, x
, y
, ctx
);
902 int EC_POINT_get_affine_coordinates(const EC_GROUP
*group
,
903 const EC_POINT
*point
, BIGNUM
*x
, BIGNUM
*y
,
906 if (group
->meth
->point_get_affine_coordinates
== NULL
) {
907 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES
,
908 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
911 if (!ec_point_is_compat(point
, group
)) {
912 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES
, EC_R_INCOMPATIBLE_OBJECTS
);
915 if (EC_POINT_is_at_infinity(group
, point
)) {
916 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES
, EC_R_POINT_AT_INFINITY
);
919 return group
->meth
->point_get_affine_coordinates(group
, point
, x
, y
, ctx
);
922 #ifndef OPENSSL_NO_DEPRECATED_3_0
923 int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP
*group
,
924 const EC_POINT
*point
, BIGNUM
*x
,
925 BIGNUM
*y
, BN_CTX
*ctx
)
927 return EC_POINT_get_affine_coordinates(group
, point
, x
, y
, ctx
);
930 # ifndef OPENSSL_NO_EC2M
931 int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP
*group
,
932 const EC_POINT
*point
, BIGNUM
*x
,
933 BIGNUM
*y
, BN_CTX
*ctx
)
935 return EC_POINT_get_affine_coordinates(group
, point
, x
, y
, ctx
);
940 int EC_POINT_add(const EC_GROUP
*group
, EC_POINT
*r
, const EC_POINT
*a
,
941 const EC_POINT
*b
, BN_CTX
*ctx
)
943 if (group
->meth
->add
== 0) {
944 ECerr(EC_F_EC_POINT_ADD
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
947 if (!ec_point_is_compat(r
, group
) || !ec_point_is_compat(a
, group
)
948 || !ec_point_is_compat(b
, group
)) {
949 ECerr(EC_F_EC_POINT_ADD
, EC_R_INCOMPATIBLE_OBJECTS
);
952 return group
->meth
->add(group
, r
, a
, b
, ctx
);
955 int EC_POINT_dbl(const EC_GROUP
*group
, EC_POINT
*r
, const EC_POINT
*a
,
958 if (group
->meth
->dbl
== 0) {
959 ECerr(EC_F_EC_POINT_DBL
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
962 if (!ec_point_is_compat(r
, group
) || !ec_point_is_compat(a
, group
)) {
963 ECerr(EC_F_EC_POINT_DBL
, EC_R_INCOMPATIBLE_OBJECTS
);
966 return group
->meth
->dbl(group
, r
, a
, ctx
);
969 int EC_POINT_invert(const EC_GROUP
*group
, EC_POINT
*a
, BN_CTX
*ctx
)
971 if (group
->meth
->invert
== 0) {
972 ECerr(EC_F_EC_POINT_INVERT
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
975 if (!ec_point_is_compat(a
, group
)) {
976 ECerr(EC_F_EC_POINT_INVERT
, EC_R_INCOMPATIBLE_OBJECTS
);
979 return group
->meth
->invert(group
, a
, ctx
);
982 int EC_POINT_is_at_infinity(const EC_GROUP
*group
, const EC_POINT
*point
)
984 if (group
->meth
->is_at_infinity
== 0) {
985 ECerr(EC_F_EC_POINT_IS_AT_INFINITY
,
986 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
989 if (!ec_point_is_compat(point
, group
)) {
990 ECerr(EC_F_EC_POINT_IS_AT_INFINITY
, 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 ECerr(EC_F_EC_POINT_IS_ON_CURVE
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1010 if (!ec_point_is_compat(point
, group
)) {
1011 ECerr(EC_F_EC_POINT_IS_ON_CURVE
, 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 ECerr(EC_F_EC_POINT_CMP
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1024 if (!ec_point_is_compat(a
, group
) || !ec_point_is_compat(b
, group
)) {
1025 ECerr(EC_F_EC_POINT_CMP
, EC_R_INCOMPATIBLE_OBJECTS
);
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 ECerr(EC_F_EC_POINT_MAKE_AFFINE
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1038 if (!ec_point_is_compat(point
, group
)) {
1039 ECerr(EC_F_EC_POINT_MAKE_AFFINE
, 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 ECerr(EC_F_EC_POINTS_MAKE_AFFINE
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1054 for (i
= 0; i
< num
; i
++) {
1055 if (!ec_point_is_compat(points
[i
], group
)) {
1056 ECerr(EC_F_EC_POINTS_MAKE_AFFINE
, EC_R_INCOMPATIBLE_OBJECTS
);
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 ECerr(EC_F_EC_POINTS_MUL
, 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 ECerr(EC_F_EC_POINTS_MUL
, EC_R_INCOMPATIBLE_OBJECTS
);
1098 ctx
= new_ctx
= BN_CTX_secure_new();
1101 ECerr(EC_F_EC_POINTS_MUL
, ERR_R_INTERNAL_ERROR
);
1105 if (group
->meth
->mul
!= NULL
)
1106 ret
= group
->meth
->mul(group
, r
, scalar
, num
, points
, scalars
, ctx
);
1109 ret
= 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 ECerr(EC_F_EC_POINT_MUL
, 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 ECerr(EC_F_EC_POINT_MUL
, 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
= 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 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 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 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 * Exponent e is public.
1270 * No need for scatter-gather or BN_FLG_CONSTTIME.
1272 if (!BN_mod_exp_mont(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 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 ec_point_blind_coordinates(const EC_GROUP
*group
, EC_POINT
*p
, BN_CTX
*ctx
)
1319 if (group
->meth
->blind_coordinates
== NULL
)
1320 return 1; /* ignore if not implemented */
1322 return group
->meth
->blind_coordinates(group
, p
, ctx
);
1325 int EC_GROUP_get_basis_type(const EC_GROUP
*group
)
1329 if (EC_GROUP_get_field_type(group
) != NID_X9_62_characteristic_two_field
)
1330 /* everything else is currently not supported */
1333 /* Find the last non-zero element of group->poly[] */
1335 i
< (int)OSSL_NELEM(group
->poly
) && group
->poly
[i
] != 0;
1340 return NID_X9_62_ppBasis
;
1342 return NID_X9_62_tpBasis
;
1344 /* everything else is currently not supported */
1348 #ifndef OPENSSL_NO_EC2M
1349 int EC_GROUP_get_trinomial_basis(const EC_GROUP
*group
, unsigned int *k
)
1354 if (EC_GROUP_get_field_type(group
) != NID_X9_62_characteristic_two_field
1355 || !((group
->poly
[0] != 0) && (group
->poly
[1] != 0)
1356 && (group
->poly
[2] == 0))) {
1357 ECerr(EC_F_EC_GROUP_GET_TRINOMIAL_BASIS
,
1358 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
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 ECerr(EC_F_EC_GROUP_GET_PENTANOMIAL_BASIS
,
1379 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1384 *k1
= group
->poly
[3];
1386 *k2
= group
->poly
[2];
1388 *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 OPENSSL_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
= 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_with_libctx(libctx
, propq
,
1443 if (ret_group
== NULL
)
1447 * Set the flag so that EC_GROUPs created from explicit parameters are
1448 * serialized using explicit parameters by default.
1450 EC_GROUP_set_asn1_flag(ret_group
, OPENSSL_EC_EXPLICIT_CURVE
);
1453 * If the input params do not contain the optional seed field we make
1454 * sure it is not added to the returned group.
1456 * The seed field is not really used inside libcrypto anyway, and
1457 * adding it to parsed explicit parameter keys would alter their DER
1458 * encoding output (because of the extra field) which could impact
1459 * applications fingerprinting keys by their DER encoding.
1462 if (EC_GROUP_set_seed(ret_group
, NULL
, 0) != 1)
1466 ret_group
= (EC_GROUP
*)group
;
1472 EC_GROUP_free(ret_group
);
1476 static int ec_encoding_param2id(const OSSL_PARAM
*p
, int *id
)
1478 const char *name
= NULL
;
1481 switch (p
->data_type
) {
1482 case OSSL_PARAM_UTF8_STRING
:
1483 /* The OSSL_PARAM functions have no support for this */
1485 status
= (name
!= NULL
);
1487 case OSSL_PARAM_UTF8_PTR
:
1488 status
= OSSL_PARAM_get_utf8_ptr(p
, &name
);
1492 int i
= ec_encoding_name2id(name
);
1502 static EC_GROUP
*group_new_from_name(const OSSL_PARAM
*p
,
1503 OPENSSL_CTX
*libctx
, const char *propq
)
1506 const char *curve_name
= NULL
;
1508 switch (p
->data_type
) {
1509 case OSSL_PARAM_UTF8_STRING
:
1510 /* The OSSL_PARAM functions have no support for this */
1511 curve_name
= p
->data
;
1512 ok
= (curve_name
!= NULL
);
1514 case OSSL_PARAM_UTF8_PTR
:
1515 ok
= OSSL_PARAM_get_utf8_ptr(p
, &curve_name
);
1520 nid
= ec_curve_name2nid(curve_name
);
1521 if (nid
== NID_undef
) {
1522 ECerr(0, EC_R_INVALID_CURVE
);
1525 return EC_GROUP_new_by_curve_name_with_libctx(libctx
, propq
, nid
);
1531 EC_GROUP
*EC_GROUP_new_from_params(const OSSL_PARAM params
[],
1532 OPENSSL_CTX
*libctx
, const char *propq
)
1534 const OSSL_PARAM
*ptmp
, *pa
, *pb
;
1536 EC_GROUP
*group
= NULL
, *named_group
= NULL
;
1537 BIGNUM
*p
= NULL
, *a
= NULL
, *b
= NULL
, *order
= NULL
, *cofactor
= NULL
;
1538 EC_POINT
*point
= NULL
;
1540 int is_prime_field
= 1;
1541 BN_CTX
*bnctx
= NULL
;
1542 const unsigned char *buf
= NULL
;
1543 int encoding_flag
= -1;
1545 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_ENCODING
);
1546 if (ptmp
!= NULL
&& !ec_encoding_param2id(ptmp
, &encoding_flag
)) {
1547 ECerr(0, EC_R_INVALID_ENCODING
);
1551 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_GROUP_NAME
);
1553 group
= group_new_from_name(ptmp
, libctx
, propq
);
1555 EC_GROUP_set_asn1_flag(group
, encoding_flag
);
1558 bnctx
= BN_CTX_new_ex(libctx
);
1559 if (bnctx
== NULL
) {
1560 ECerr(0, ERR_R_MALLOC_FAILURE
);
1563 BN_CTX_start(bnctx
);
1565 p
= BN_CTX_get(bnctx
);
1566 a
= BN_CTX_get(bnctx
);
1567 b
= BN_CTX_get(bnctx
);
1568 order
= BN_CTX_get(bnctx
);
1569 if (order
== NULL
) {
1570 ECerr(0, ERR_R_MALLOC_FAILURE
);
1574 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_FIELD_TYPE
);
1575 if (ptmp
== NULL
|| ptmp
->data_type
!= OSSL_PARAM_UTF8_STRING
) {
1576 ECerr(0, EC_R_INVALID_FIELD
);
1579 if (strcasecmp(ptmp
->data
, SN_X9_62_prime_field
) == 0) {
1581 } else if (strcasecmp(ptmp
->data
, SN_X9_62_characteristic_two_field
) == 0) {
1585 ECerr(0, EC_R_UNSUPPORTED_FIELD
);
1589 pa
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_A
);
1590 if (!OSSL_PARAM_get_BN(pa
, &a
)) {
1591 ECerr(0, EC_R_INVALID_A
);
1594 pb
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_B
);
1595 if (!OSSL_PARAM_get_BN(pb
, &b
)) {
1596 ECerr(0, EC_R_INVALID_B
);
1600 /* extract the prime number or irreducible polynomial */
1601 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_P
);
1602 if (!OSSL_PARAM_get_BN(ptmp
, &p
)) {
1603 ECerr(0, EC_R_INVALID_P
);
1607 if (is_prime_field
) {
1608 if (BN_is_negative(p
) || BN_is_zero(p
)) {
1609 ECerr(0, EC_R_INVALID_P
);
1612 field_bits
= BN_num_bits(p
);
1613 if (field_bits
> OPENSSL_ECC_MAX_FIELD_BITS
) {
1614 ECerr(0, EC_R_FIELD_TOO_LARGE
);
1618 /* create the EC_GROUP structure */
1619 group
= EC_GROUP_new_curve_GFp(p
, a
, b
, bnctx
);
1621 #ifdef OPENSSL_NO_EC2M
1622 ECerr(0, EC_R_GF2M_NOT_SUPPORTED
);
1625 /* create the EC_GROUP structure */
1626 group
= EC_GROUP_new_curve_GF2m(p
, a
, b
, NULL
);
1627 if (group
!= NULL
) {
1628 field_bits
= EC_GROUP_get_degree(group
);
1629 if (field_bits
> OPENSSL_ECC_MAX_FIELD_BITS
) {
1630 ECerr(0, EC_R_FIELD_TOO_LARGE
);
1634 #endif /* OPENSSL_NO_EC2M */
1637 if (group
== NULL
) {
1638 ECerr(0, ERR_R_EC_LIB
);
1643 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_SEED
);
1645 if (ptmp
->data_type
!= OSSL_PARAM_OCTET_STRING
) {
1646 ECerr(0, EC_R_INVALID_SEED
);
1649 if (!EC_GROUP_set_seed(group
, ptmp
->data
, ptmp
->data_size
))
1653 /* generator base point */
1654 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_GENERATOR
);
1656 || ptmp
->data_type
!= OSSL_PARAM_OCTET_STRING
) {
1657 ECerr(0, EC_R_INVALID_GENERATOR
);
1660 buf
= (const unsigned char *)(ptmp
->data
);
1661 if ((point
= EC_POINT_new(group
)) == NULL
)
1663 EC_GROUP_set_point_conversion_form(group
,
1664 (point_conversion_form_t
)buf
[0] & ~0x01);
1665 if (!EC_POINT_oct2point(group
, point
, buf
, ptmp
->data_size
, bnctx
)) {
1666 ECerr(0, EC_R_INVALID_GENERATOR
);
1671 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_ORDER
);
1672 if (!OSSL_PARAM_get_BN(ptmp
, &order
)
1673 || (BN_is_negative(order
) || BN_is_zero(order
))
1674 || (BN_num_bits(order
) > (int)field_bits
+ 1)) { /* Hasse bound */
1675 ECerr(0, EC_R_INVALID_GROUP_ORDER
);
1679 /* Optional cofactor */
1680 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_COFACTOR
);
1682 cofactor
= BN_CTX_get(bnctx
);
1683 if (cofactor
== NULL
|| !OSSL_PARAM_get_BN(ptmp
, &cofactor
)) {
1684 ECerr(0, EC_R_INVALID_COFACTOR
);
1689 /* set the generator, order and cofactor (if present) */
1690 if (!EC_GROUP_set_generator(group
, point
, order
, cofactor
)) {
1691 ECerr(0, EC_R_INVALID_GENERATOR
);
1695 named_group
= ec_group_explicit_to_named(group
, libctx
, propq
, bnctx
);
1696 if (named_group
== NULL
) {
1697 ECerr(0, EC_R_INVALID_NAMED_GROUP_CONVERSION
);
1700 if (named_group
== group
) {
1702 * If we did not find a named group then the encoding should be explicit
1703 * if it was specified
1705 if (encoding_flag
== OPENSSL_EC_NAMED_CURVE
) {
1706 ECerr(0, EC_R_INVALID_ENCODING
);
1709 EC_GROUP_set_asn1_flag(group
, OPENSSL_EC_EXPLICIT_CURVE
);
1711 EC_GROUP_free(group
);
1712 group
= named_group
;
1717 EC_GROUP_free(group
);
1720 EC_POINT_free(point
);