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"
26 /* functions for EC_GROUP objects */
28 EC_GROUP
*ossl_ec_group_new_ex(OSSL_LIB_CTX
*libctx
, const char *propq
,
29 const EC_METHOD
*meth
)
34 ERR_raise(ERR_LIB_EC
, EC_R_SLOT_FULL
);
37 if (meth
->group_init
== 0) {
38 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
42 ret
= OPENSSL_zalloc(sizeof(*ret
));
44 ERR_raise(ERR_LIB_EC
, ERR_R_MALLOC_FAILURE
);
50 ret
->propq
= OPENSSL_strdup(propq
);
51 if (ret
->propq
== NULL
) {
52 ERR_raise(ERR_LIB_EC
, ERR_R_MALLOC_FAILURE
);
57 if ((ret
->meth
->flags
& EC_FLAGS_CUSTOM_CURVE
) == 0) {
58 ret
->order
= BN_new();
59 if (ret
->order
== NULL
)
61 ret
->cofactor
= BN_new();
62 if (ret
->cofactor
== NULL
)
65 ret
->asn1_flag
= OPENSSL_EC_EXPLICIT_CURVE
;
66 ret
->asn1_form
= POINT_CONVERSION_UNCOMPRESSED
;
67 if (!meth
->group_init(ret
))
73 BN_free(ret
->cofactor
);
74 OPENSSL_free(ret
->propq
);
79 #ifndef OPENSSL_NO_DEPRECATED_3_0
81 EC_GROUP
*EC_GROUP_new(const EC_METHOD
*meth
)
83 return ossl_ec_group_new_ex(NULL
, NULL
, meth
);
88 void EC_pre_comp_free(EC_GROUP
*group
)
90 switch (group
->pre_comp_type
) {
94 #ifdef ECP_NISTZ256_ASM
95 EC_nistz256_pre_comp_free(group
->pre_comp
.nistz256
);
98 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
100 EC_nistp224_pre_comp_free(group
->pre_comp
.nistp224
);
103 EC_nistp256_pre_comp_free(group
->pre_comp
.nistp256
);
106 EC_nistp521_pre_comp_free(group
->pre_comp
.nistp521
);
115 EC_ec_pre_comp_free(group
->pre_comp
.ec
);
118 group
->pre_comp
.ec
= NULL
;
121 void EC_GROUP_free(EC_GROUP
*group
)
126 if (group
->meth
->group_finish
!= 0)
127 group
->meth
->group_finish(group
);
129 EC_pre_comp_free(group
);
130 BN_MONT_CTX_free(group
->mont_data
);
131 EC_POINT_free(group
->generator
);
132 BN_free(group
->order
);
133 BN_free(group
->cofactor
);
134 OPENSSL_free(group
->seed
);
135 OPENSSL_free(group
->propq
);
139 #ifndef OPENSSL_NO_DEPRECATED_3_0
140 void EC_GROUP_clear_free(EC_GROUP
*group
)
145 if (group
->meth
->group_clear_finish
!= 0)
146 group
->meth
->group_clear_finish(group
);
147 else if (group
->meth
->group_finish
!= 0)
148 group
->meth
->group_finish(group
);
150 EC_pre_comp_free(group
);
151 BN_MONT_CTX_free(group
->mont_data
);
152 EC_POINT_clear_free(group
->generator
);
153 BN_clear_free(group
->order
);
154 BN_clear_free(group
->cofactor
);
155 OPENSSL_clear_free(group
->seed
, group
->seed_len
);
156 OPENSSL_clear_free(group
, sizeof(*group
));
160 int EC_GROUP_copy(EC_GROUP
*dest
, const EC_GROUP
*src
)
162 if (dest
->meth
->group_copy
== 0) {
163 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
166 if (dest
->meth
!= src
->meth
) {
167 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
173 dest
->libctx
= src
->libctx
;
174 dest
->curve_name
= src
->curve_name
;
176 /* Copy precomputed */
177 dest
->pre_comp_type
= src
->pre_comp_type
;
178 switch (src
->pre_comp_type
) {
180 dest
->pre_comp
.ec
= NULL
;
183 #ifdef ECP_NISTZ256_ASM
184 dest
->pre_comp
.nistz256
= EC_nistz256_pre_comp_dup(src
->pre_comp
.nistz256
);
187 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
189 dest
->pre_comp
.nistp224
= EC_nistp224_pre_comp_dup(src
->pre_comp
.nistp224
);
192 dest
->pre_comp
.nistp256
= EC_nistp256_pre_comp_dup(src
->pre_comp
.nistp256
);
195 dest
->pre_comp
.nistp521
= EC_nistp521_pre_comp_dup(src
->pre_comp
.nistp521
);
204 dest
->pre_comp
.ec
= EC_ec_pre_comp_dup(src
->pre_comp
.ec
);
208 if (src
->mont_data
!= NULL
) {
209 if (dest
->mont_data
== NULL
) {
210 dest
->mont_data
= BN_MONT_CTX_new();
211 if (dest
->mont_data
== NULL
)
214 if (!BN_MONT_CTX_copy(dest
->mont_data
, src
->mont_data
))
217 /* src->generator == NULL */
218 BN_MONT_CTX_free(dest
->mont_data
);
219 dest
->mont_data
= NULL
;
222 if (src
->generator
!= NULL
) {
223 if (dest
->generator
== NULL
) {
224 dest
->generator
= EC_POINT_new(dest
);
225 if (dest
->generator
== NULL
)
228 if (!EC_POINT_copy(dest
->generator
, src
->generator
))
231 /* src->generator == NULL */
232 EC_POINT_clear_free(dest
->generator
);
233 dest
->generator
= NULL
;
236 if ((src
->meth
->flags
& EC_FLAGS_CUSTOM_CURVE
) == 0) {
237 if (!BN_copy(dest
->order
, src
->order
))
239 if (!BN_copy(dest
->cofactor
, src
->cofactor
))
243 dest
->asn1_flag
= src
->asn1_flag
;
244 dest
->asn1_form
= src
->asn1_form
;
245 dest
->decoded_from_explicit_params
= src
->decoded_from_explicit_params
;
248 OPENSSL_free(dest
->seed
);
249 if ((dest
->seed
= OPENSSL_malloc(src
->seed_len
)) == NULL
) {
250 ERR_raise(ERR_LIB_EC
, ERR_R_MALLOC_FAILURE
);
253 if (!memcpy(dest
->seed
, src
->seed
, src
->seed_len
))
255 dest
->seed_len
= src
->seed_len
;
257 OPENSSL_free(dest
->seed
);
262 return dest
->meth
->group_copy(dest
, src
);
265 EC_GROUP
*EC_GROUP_dup(const EC_GROUP
*a
)
273 if ((t
= ossl_ec_group_new_ex(a
->libctx
, a
->propq
, a
->meth
)) == NULL
)
275 if (!EC_GROUP_copy(t
, a
))
288 #ifndef OPENSSL_NO_DEPRECATED_3_0
289 const EC_METHOD
*EC_GROUP_method_of(const EC_GROUP
*group
)
294 int EC_METHOD_get_field_type(const EC_METHOD
*meth
)
296 return meth
->field_type
;
300 static int ec_precompute_mont_data(EC_GROUP
*);
303 * Try computing cofactor from the generator order (n) and field cardinality (q).
304 * This works for all curves of cryptographic interest.
306 * Hasse thm: q + 1 - 2*sqrt(q) <= n*h <= q + 1 + 2*sqrt(q)
307 * h_min = (q + 1 - 2*sqrt(q))/n
308 * h_max = (q + 1 + 2*sqrt(q))/n
309 * h_max - h_min = 4*sqrt(q)/n
310 * So if n > 4*sqrt(q) holds, there is only one possible value for h:
311 * h = \lfloor (h_min + h_max)/2 \rceil = \lfloor (q + 1)/n \rceil
313 * Otherwise, zero cofactor and return success.
315 static int ec_guess_cofactor(EC_GROUP
*group
) {
321 * If the cofactor is too large, we cannot guess it.
322 * The RHS of below is a strict overestimate of lg(4 * sqrt(q))
324 if (BN_num_bits(group
->order
) <= (BN_num_bits(group
->field
) + 1) / 2 + 3) {
326 BN_zero(group
->cofactor
);
331 if ((ctx
= BN_CTX_new_ex(group
->libctx
)) == NULL
)
335 if ((q
= BN_CTX_get(ctx
)) == NULL
)
338 /* set q = 2**m for binary fields; q = p otherwise */
339 if (group
->meth
->field_type
== NID_X9_62_characteristic_two_field
) {
341 if (!BN_set_bit(q
, BN_num_bits(group
->field
) - 1))
344 if (!BN_copy(q
, group
->field
))
348 /* compute h = \lfloor (q + 1)/n \rceil = \lfloor (q + 1 + n/2)/n \rfloor */
349 if (!BN_rshift1(group
->cofactor
, group
->order
) /* n/2 */
350 || !BN_add(group
->cofactor
, group
->cofactor
, q
) /* q + n/2 */
352 || !BN_add(group
->cofactor
, group
->cofactor
, BN_value_one())
353 /* (q + 1 + n/2)/n */
354 || !BN_div(group
->cofactor
, NULL
, group
->cofactor
, group
->order
, ctx
))
363 int EC_GROUP_set_generator(EC_GROUP
*group
, const EC_POINT
*generator
,
364 const BIGNUM
*order
, const BIGNUM
*cofactor
)
366 if (generator
== NULL
) {
367 ERR_raise(ERR_LIB_EC
, ERR_R_PASSED_NULL_PARAMETER
);
371 /* require group->field >= 1 */
372 if (group
->field
== NULL
|| BN_is_zero(group
->field
)
373 || BN_is_negative(group
->field
)) {
374 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_FIELD
);
379 * - require order >= 1
380 * - enforce upper bound due to Hasse thm: order can be no more than one bit
381 * longer than field cardinality
383 if (order
== NULL
|| BN_is_zero(order
) || BN_is_negative(order
)
384 || BN_num_bits(order
) > BN_num_bits(group
->field
) + 1) {
385 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_GROUP_ORDER
);
390 * Unfortunately the cofactor is an optional field in many standards.
391 * Internally, the lib uses 0 cofactor as a marker for "unknown cofactor".
392 * So accept cofactor == NULL or cofactor >= 0.
394 if (cofactor
!= NULL
&& BN_is_negative(cofactor
)) {
395 ERR_raise(ERR_LIB_EC
, EC_R_UNKNOWN_COFACTOR
);
399 if (group
->generator
== NULL
) {
400 group
->generator
= EC_POINT_new(group
);
401 if (group
->generator
== NULL
)
404 if (!EC_POINT_copy(group
->generator
, generator
))
407 if (!BN_copy(group
->order
, order
))
410 /* Either take the provided positive cofactor, or try to compute it */
411 if (cofactor
!= NULL
&& !BN_is_zero(cofactor
)) {
412 if (!BN_copy(group
->cofactor
, cofactor
))
414 } else if (!ec_guess_cofactor(group
)) {
415 BN_zero(group
->cofactor
);
420 * Some groups have an order with
421 * factors of two, which makes the Montgomery setup fail.
422 * |group->mont_data| will be NULL in this case.
424 if (BN_is_odd(group
->order
)) {
425 return ec_precompute_mont_data(group
);
428 BN_MONT_CTX_free(group
->mont_data
);
429 group
->mont_data
= NULL
;
433 const EC_POINT
*EC_GROUP_get0_generator(const EC_GROUP
*group
)
435 return group
->generator
;
438 BN_MONT_CTX
*EC_GROUP_get_mont_data(const EC_GROUP
*group
)
440 return group
->mont_data
;
443 int EC_GROUP_get_order(const EC_GROUP
*group
, BIGNUM
*order
, BN_CTX
*ctx
)
445 if (group
->order
== NULL
)
447 if (!BN_copy(order
, group
->order
))
450 return !BN_is_zero(order
);
453 const BIGNUM
*EC_GROUP_get0_order(const EC_GROUP
*group
)
458 int EC_GROUP_order_bits(const EC_GROUP
*group
)
460 return group
->meth
->group_order_bits(group
);
463 int EC_GROUP_get_cofactor(const EC_GROUP
*group
, BIGNUM
*cofactor
,
467 if (group
->cofactor
== NULL
)
469 if (!BN_copy(cofactor
, group
->cofactor
))
472 return !BN_is_zero(group
->cofactor
);
475 const BIGNUM
*EC_GROUP_get0_cofactor(const EC_GROUP
*group
)
477 return group
->cofactor
;
480 void EC_GROUP_set_curve_name(EC_GROUP
*group
, int nid
)
482 group
->curve_name
= nid
;
485 ? OPENSSL_EC_NAMED_CURVE
486 : OPENSSL_EC_EXPLICIT_CURVE
;
489 int EC_GROUP_get_curve_name(const EC_GROUP
*group
)
491 return group
->curve_name
;
494 const BIGNUM
*EC_GROUP_get0_field(const EC_GROUP
*group
)
499 int EC_GROUP_get_field_type(const EC_GROUP
*group
)
501 return group
->meth
->field_type
;
504 void EC_GROUP_set_asn1_flag(EC_GROUP
*group
, int flag
)
506 group
->asn1_flag
= flag
;
509 int EC_GROUP_get_asn1_flag(const EC_GROUP
*group
)
511 return group
->asn1_flag
;
514 void EC_GROUP_set_point_conversion_form(EC_GROUP
*group
,
515 point_conversion_form_t form
)
517 group
->asn1_form
= form
;
520 point_conversion_form_t
EC_GROUP_get_point_conversion_form(const EC_GROUP
523 return group
->asn1_form
;
526 size_t EC_GROUP_set_seed(EC_GROUP
*group
, const unsigned char *p
, size_t len
)
528 OPENSSL_free(group
->seed
);
535 if ((group
->seed
= OPENSSL_malloc(len
)) == NULL
) {
536 ERR_raise(ERR_LIB_EC
, ERR_R_MALLOC_FAILURE
);
539 memcpy(group
->seed
, p
, len
);
540 group
->seed_len
= len
;
545 unsigned char *EC_GROUP_get0_seed(const EC_GROUP
*group
)
550 size_t EC_GROUP_get_seed_len(const EC_GROUP
*group
)
552 return group
->seed_len
;
555 int EC_GROUP_set_curve(EC_GROUP
*group
, const BIGNUM
*p
, const BIGNUM
*a
,
556 const BIGNUM
*b
, BN_CTX
*ctx
)
558 if (group
->meth
->group_set_curve
== 0) {
559 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
562 return group
->meth
->group_set_curve(group
, p
, a
, b
, ctx
);
565 int EC_GROUP_get_curve(const EC_GROUP
*group
, BIGNUM
*p
, BIGNUM
*a
, BIGNUM
*b
,
568 if (group
->meth
->group_get_curve
== NULL
) {
569 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
572 return group
->meth
->group_get_curve(group
, p
, a
, b
, ctx
);
575 #ifndef OPENSSL_NO_DEPRECATED_3_0
576 int EC_GROUP_set_curve_GFp(EC_GROUP
*group
, const BIGNUM
*p
, const BIGNUM
*a
,
577 const BIGNUM
*b
, BN_CTX
*ctx
)
579 return EC_GROUP_set_curve(group
, p
, a
, b
, ctx
);
582 int EC_GROUP_get_curve_GFp(const EC_GROUP
*group
, BIGNUM
*p
, BIGNUM
*a
,
583 BIGNUM
*b
, BN_CTX
*ctx
)
585 return EC_GROUP_get_curve(group
, p
, a
, b
, ctx
);
588 # ifndef OPENSSL_NO_EC2M
589 int EC_GROUP_set_curve_GF2m(EC_GROUP
*group
, const BIGNUM
*p
, const BIGNUM
*a
,
590 const BIGNUM
*b
, BN_CTX
*ctx
)
592 return EC_GROUP_set_curve(group
, p
, a
, b
, ctx
);
595 int EC_GROUP_get_curve_GF2m(const EC_GROUP
*group
, BIGNUM
*p
, BIGNUM
*a
,
596 BIGNUM
*b
, BN_CTX
*ctx
)
598 return EC_GROUP_get_curve(group
, p
, a
, b
, ctx
);
603 int EC_GROUP_get_degree(const EC_GROUP
*group
)
605 if (group
->meth
->group_get_degree
== 0) {
606 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
609 return group
->meth
->group_get_degree(group
);
612 int EC_GROUP_check_discriminant(const EC_GROUP
*group
, BN_CTX
*ctx
)
614 if (group
->meth
->group_check_discriminant
== 0) {
615 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
618 return group
->meth
->group_check_discriminant(group
, ctx
);
621 int EC_GROUP_cmp(const EC_GROUP
*a
, const EC_GROUP
*b
, BN_CTX
*ctx
)
624 BIGNUM
*a1
, *a2
, *a3
, *b1
, *b2
, *b3
;
626 BN_CTX
*ctx_new
= NULL
;
629 /* compare the field types */
630 if (EC_GROUP_get_field_type(a
) != EC_GROUP_get_field_type(b
))
632 /* compare the curve name (if present in both) */
633 if (EC_GROUP_get_curve_name(a
) && EC_GROUP_get_curve_name(b
) &&
634 EC_GROUP_get_curve_name(a
) != EC_GROUP_get_curve_name(b
))
636 if (a
->meth
->flags
& EC_FLAGS_CUSTOM_CURVE
)
641 ctx_new
= ctx
= BN_CTX_new();
647 a1
= BN_CTX_get(ctx
);
648 a2
= BN_CTX_get(ctx
);
649 a3
= BN_CTX_get(ctx
);
650 b1
= BN_CTX_get(ctx
);
651 b2
= BN_CTX_get(ctx
);
652 b3
= BN_CTX_get(ctx
);
656 BN_CTX_free(ctx_new
);
662 * XXX This approach assumes that the external representation of curves
663 * over the same field type is the same.
665 if (!a
->meth
->group_get_curve(a
, a1
, a2
, a3
, ctx
) ||
666 !b
->meth
->group_get_curve(b
, b1
, b2
, b3
, ctx
))
669 /* return 1 if the curve parameters are different */
670 if (r
|| BN_cmp(a1
, b1
) != 0 || BN_cmp(a2
, b2
) != 0 || BN_cmp(a3
, b3
) != 0)
673 /* XXX EC_POINT_cmp() assumes that the methods are equal */
674 /* return 1 if the generators are different */
675 if (r
|| EC_POINT_cmp(a
, EC_GROUP_get0_generator(a
),
676 EC_GROUP_get0_generator(b
), ctx
) != 0)
680 const BIGNUM
*ao
, *bo
, *ac
, *bc
;
681 /* compare the orders */
682 ao
= EC_GROUP_get0_order(a
);
683 bo
= EC_GROUP_get0_order(b
);
684 if (ao
== NULL
|| bo
== NULL
) {
685 /* return an error if either order is NULL */
689 if (BN_cmp(ao
, bo
) != 0) {
690 /* return 1 if orders are different */
695 * It gets here if the curve parameters and generator matched.
696 * Now check the optional cofactors (if both are present).
698 ac
= EC_GROUP_get0_cofactor(a
);
699 bc
= EC_GROUP_get0_cofactor(b
);
700 /* Returns 1 (mismatch) if both cofactors are specified and different */
701 if (!BN_is_zero(ac
) && !BN_is_zero(bc
) && BN_cmp(ac
, bc
) != 0)
703 /* Returns 0 if the parameters matched */
708 BN_CTX_free(ctx_new
);
713 /* functions for EC_POINT objects */
715 EC_POINT
*EC_POINT_new(const EC_GROUP
*group
)
720 ERR_raise(ERR_LIB_EC
, ERR_R_PASSED_NULL_PARAMETER
);
723 if (group
->meth
->point_init
== NULL
) {
724 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
728 ret
= OPENSSL_zalloc(sizeof(*ret
));
730 ERR_raise(ERR_LIB_EC
, ERR_R_MALLOC_FAILURE
);
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 if (point
->meth
->point_finish
!= 0)
751 point
->meth
->point_finish(point
);
755 void EC_POINT_clear_free(EC_POINT
*point
)
760 if (point
->meth
->point_clear_finish
!= 0)
761 point
->meth
->point_clear_finish(point
);
762 else if (point
->meth
->point_finish
!= 0)
763 point
->meth
->point_finish(point
);
764 OPENSSL_clear_free(point
, sizeof(*point
));
767 int EC_POINT_copy(EC_POINT
*dest
, const EC_POINT
*src
)
769 if (dest
->meth
->point_copy
== 0) {
770 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
773 if (dest
->meth
!= src
->meth
774 || (dest
->curve_name
!= src
->curve_name
775 && dest
->curve_name
!= 0
776 && src
->curve_name
!= 0)) {
777 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
782 return dest
->meth
->point_copy(dest
, src
);
785 EC_POINT
*EC_POINT_dup(const EC_POINT
*a
, const EC_GROUP
*group
)
793 t
= EC_POINT_new(group
);
796 r
= EC_POINT_copy(t
, a
);
804 #ifndef OPENSSL_NO_DEPRECATED_3_0
805 const EC_METHOD
*EC_POINT_method_of(const EC_POINT
*point
)
811 int EC_POINT_set_to_infinity(const EC_GROUP
*group
, EC_POINT
*point
)
813 if (group
->meth
->point_set_to_infinity
== 0) {
814 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
817 if (group
->meth
!= point
->meth
) {
818 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
821 return group
->meth
->point_set_to_infinity(group
, point
);
824 #ifndef OPENSSL_NO_DEPRECATED_3_0
825 int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP
*group
,
826 EC_POINT
*point
, const BIGNUM
*x
,
827 const BIGNUM
*y
, const BIGNUM
*z
,
830 if (group
->meth
->field_type
!= NID_X9_62_prime_field
) {
831 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
834 if (!ec_point_is_compat(point
, group
)) {
835 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
838 return ossl_ec_GFp_simple_set_Jprojective_coordinates_GFp(group
, point
,
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 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
851 if (!ec_point_is_compat(point
, group
)) {
852 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
855 return ossl_ec_GFp_simple_get_Jprojective_coordinates_GFp(group
, point
,
860 int EC_POINT_set_affine_coordinates(const EC_GROUP
*group
, EC_POINT
*point
,
861 const BIGNUM
*x
, const BIGNUM
*y
,
864 if (group
->meth
->point_set_affine_coordinates
== NULL
) {
865 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
868 if (!ec_point_is_compat(point
, group
)) {
869 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
872 if (!group
->meth
->point_set_affine_coordinates(group
, point
, x
, y
, ctx
))
875 if (EC_POINT_is_on_curve(group
, point
, ctx
) <= 0) {
876 ERR_raise(ERR_LIB_EC
, EC_R_POINT_IS_NOT_ON_CURVE
);
882 #ifndef OPENSSL_NO_DEPRECATED_3_0
883 int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP
*group
,
884 EC_POINT
*point
, const BIGNUM
*x
,
885 const BIGNUM
*y
, BN_CTX
*ctx
)
887 return EC_POINT_set_affine_coordinates(group
, point
, x
, y
, ctx
);
890 # ifndef OPENSSL_NO_EC2M
891 int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP
*group
,
892 EC_POINT
*point
, const BIGNUM
*x
,
893 const BIGNUM
*y
, BN_CTX
*ctx
)
895 return EC_POINT_set_affine_coordinates(group
, point
, x
, y
, ctx
);
900 int EC_POINT_get_affine_coordinates(const EC_GROUP
*group
,
901 const EC_POINT
*point
, BIGNUM
*x
, BIGNUM
*y
,
904 if (group
->meth
->point_get_affine_coordinates
== NULL
) {
905 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
908 if (!ec_point_is_compat(point
, group
)) {
909 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
912 if (EC_POINT_is_at_infinity(group
, point
)) {
913 ERR_raise(ERR_LIB_EC
, EC_R_POINT_AT_INFINITY
);
916 return group
->meth
->point_get_affine_coordinates(group
, point
, x
, y
, ctx
);
919 #ifndef OPENSSL_NO_DEPRECATED_3_0
920 int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP
*group
,
921 const EC_POINT
*point
, BIGNUM
*x
,
922 BIGNUM
*y
, BN_CTX
*ctx
)
924 return EC_POINT_get_affine_coordinates(group
, point
, x
, y
, ctx
);
927 # ifndef OPENSSL_NO_EC2M
928 int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP
*group
,
929 const EC_POINT
*point
, BIGNUM
*x
,
930 BIGNUM
*y
, BN_CTX
*ctx
)
932 return EC_POINT_get_affine_coordinates(group
, point
, x
, y
, ctx
);
937 int EC_POINT_add(const EC_GROUP
*group
, EC_POINT
*r
, const EC_POINT
*a
,
938 const EC_POINT
*b
, BN_CTX
*ctx
)
940 if (group
->meth
->add
== 0) {
941 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
944 if (!ec_point_is_compat(r
, group
) || !ec_point_is_compat(a
, group
)
945 || !ec_point_is_compat(b
, group
)) {
946 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
949 return group
->meth
->add(group
, r
, a
, b
, ctx
);
952 int EC_POINT_dbl(const EC_GROUP
*group
, EC_POINT
*r
, const EC_POINT
*a
,
955 if (group
->meth
->dbl
== 0) {
956 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
959 if (!ec_point_is_compat(r
, group
) || !ec_point_is_compat(a
, group
)) {
960 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
963 return group
->meth
->dbl(group
, r
, a
, ctx
);
966 int EC_POINT_invert(const EC_GROUP
*group
, EC_POINT
*a
, BN_CTX
*ctx
)
968 if (group
->meth
->invert
== 0) {
969 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
972 if (!ec_point_is_compat(a
, group
)) {
973 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
976 return group
->meth
->invert(group
, a
, ctx
);
979 int EC_POINT_is_at_infinity(const EC_GROUP
*group
, const EC_POINT
*point
)
981 if (group
->meth
->is_at_infinity
== 0) {
982 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
985 if (!ec_point_is_compat(point
, group
)) {
986 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
989 return group
->meth
->is_at_infinity(group
, point
);
993 * Check whether an EC_POINT is on the curve or not. Note that the return
994 * value for this function should NOT be treated as a boolean. Return values:
995 * 1: The point is on the curve
996 * 0: The point is not on the curve
997 * -1: An error occurred
999 int EC_POINT_is_on_curve(const EC_GROUP
*group
, const EC_POINT
*point
,
1002 if (group
->meth
->is_on_curve
== 0) {
1003 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1006 if (!ec_point_is_compat(point
, group
)) {
1007 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
1010 return group
->meth
->is_on_curve(group
, point
, ctx
);
1013 int EC_POINT_cmp(const EC_GROUP
*group
, const EC_POINT
*a
, const EC_POINT
*b
,
1016 if (group
->meth
->point_cmp
== 0) {
1017 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1020 if (!ec_point_is_compat(a
, group
) || !ec_point_is_compat(b
, group
)) {
1021 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
1024 return group
->meth
->point_cmp(group
, a
, b
, ctx
);
1027 #ifndef OPENSSL_NO_DEPRECATED_3_0
1028 int EC_POINT_make_affine(const EC_GROUP
*group
, EC_POINT
*point
, BN_CTX
*ctx
)
1030 if (group
->meth
->make_affine
== 0) {
1031 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1034 if (!ec_point_is_compat(point
, group
)) {
1035 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
1038 return group
->meth
->make_affine(group
, point
, ctx
);
1041 int EC_POINTs_make_affine(const EC_GROUP
*group
, size_t num
,
1042 EC_POINT
*points
[], BN_CTX
*ctx
)
1046 if (group
->meth
->points_make_affine
== 0) {
1047 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1050 for (i
= 0; i
< num
; i
++) {
1051 if (!ec_point_is_compat(points
[i
], group
)) {
1052 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
1056 return group
->meth
->points_make_affine(group
, num
, points
, ctx
);
1061 * Functions for point multiplication. If group->meth->mul is 0, we use the
1062 * wNAF-based implementations in ec_mult.c; otherwise we dispatch through
1066 #ifndef OPENSSL_NO_DEPRECATED_3_0
1067 int EC_POINTs_mul(const EC_GROUP
*group
, EC_POINT
*r
, const BIGNUM
*scalar
,
1068 size_t num
, const EC_POINT
*points
[],
1069 const BIGNUM
*scalars
[], BN_CTX
*ctx
)
1074 BN_CTX
*new_ctx
= NULL
;
1077 if (!ec_point_is_compat(r
, group
)) {
1078 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
1082 if (scalar
== NULL
&& num
== 0)
1083 return EC_POINT_set_to_infinity(group
, r
);
1085 for (i
= 0; i
< num
; i
++) {
1086 if (!ec_point_is_compat(points
[i
], group
)) {
1087 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
1094 ctx
= new_ctx
= BN_CTX_secure_new();
1097 ERR_raise(ERR_LIB_EC
, ERR_R_INTERNAL_ERROR
);
1101 if (group
->meth
->mul
!= NULL
)
1102 ret
= group
->meth
->mul(group
, r
, scalar
, num
, points
, scalars
, ctx
);
1105 ret
= ossl_ec_wNAF_mul(group
, r
, scalar
, num
, points
, scalars
, ctx
);
1108 BN_CTX_free(new_ctx
);
1114 int EC_POINT_mul(const EC_GROUP
*group
, EC_POINT
*r
, const BIGNUM
*g_scalar
,
1115 const EC_POINT
*point
, const BIGNUM
*p_scalar
, BN_CTX
*ctx
)
1120 BN_CTX
*new_ctx
= NULL
;
1123 if (!ec_point_is_compat(r
, group
)
1124 || (point
!= NULL
&& !ec_point_is_compat(point
, group
))) {
1125 ERR_raise(ERR_LIB_EC
, EC_R_INCOMPATIBLE_OBJECTS
);
1129 if (g_scalar
== NULL
&& p_scalar
== NULL
)
1130 return EC_POINT_set_to_infinity(group
, r
);
1134 ctx
= new_ctx
= BN_CTX_secure_new();
1137 ERR_raise(ERR_LIB_EC
, ERR_R_INTERNAL_ERROR
);
1141 num
= (point
!= NULL
&& p_scalar
!= NULL
) ? 1 : 0;
1142 if (group
->meth
->mul
!= NULL
)
1143 ret
= group
->meth
->mul(group
, r
, g_scalar
, num
, &point
, &p_scalar
, ctx
);
1146 ret
= ossl_ec_wNAF_mul(group
, r
, g_scalar
, num
, &point
, &p_scalar
, ctx
);
1149 BN_CTX_free(new_ctx
);
1154 #ifndef OPENSSL_NO_DEPRECATED_3_0
1155 int EC_GROUP_precompute_mult(EC_GROUP
*group
, BN_CTX
*ctx
)
1157 if (group
->meth
->mul
== 0)
1159 return ossl_ec_wNAF_precompute_mult(group
, ctx
);
1161 if (group
->meth
->precompute_mult
!= 0)
1162 return group
->meth
->precompute_mult(group
, ctx
);
1164 return 1; /* nothing to do, so report success */
1167 int EC_GROUP_have_precompute_mult(const EC_GROUP
*group
)
1169 if (group
->meth
->mul
== 0)
1171 return ossl_ec_wNAF_have_precompute_mult(group
);
1173 if (group
->meth
->have_precompute_mult
!= 0)
1174 return group
->meth
->have_precompute_mult(group
);
1176 return 0; /* cannot tell whether precomputation has
1182 * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
1183 * returns one on success. On error it returns zero.
1185 static int ec_precompute_mont_data(EC_GROUP
*group
)
1187 BN_CTX
*ctx
= BN_CTX_new_ex(group
->libctx
);
1190 BN_MONT_CTX_free(group
->mont_data
);
1191 group
->mont_data
= NULL
;
1196 group
->mont_data
= BN_MONT_CTX_new();
1197 if (group
->mont_data
== NULL
)
1200 if (!BN_MONT_CTX_set(group
->mont_data
, group
->order
, ctx
)) {
1201 BN_MONT_CTX_free(group
->mont_data
);
1202 group
->mont_data
= NULL
;
1215 int EC_KEY_set_ex_data(EC_KEY
*key
, int idx
, void *arg
)
1217 return CRYPTO_set_ex_data(&key
->ex_data
, idx
, arg
);
1220 void *EC_KEY_get_ex_data(const EC_KEY
*key
, int idx
)
1222 return CRYPTO_get_ex_data(&key
->ex_data
, idx
);
1226 int ossl_ec_group_simple_order_bits(const EC_GROUP
*group
)
1228 if (group
->order
== NULL
)
1230 return BN_num_bits(group
->order
);
1233 static int ec_field_inverse_mod_ord(const EC_GROUP
*group
, BIGNUM
*r
,
1234 const BIGNUM
*x
, BN_CTX
*ctx
)
1239 BN_CTX
*new_ctx
= NULL
;
1242 if (group
->mont_data
== NULL
)
1247 ctx
= new_ctx
= BN_CTX_secure_new();
1253 if ((e
= BN_CTX_get(ctx
)) == NULL
)
1257 * We want inverse in constant time, therefore we utilize the fact
1258 * order must be prime and use Fermats Little Theorem instead.
1260 if (!BN_set_word(e
, 2))
1262 if (!BN_sub(e
, group
->order
, e
))
1265 * Exponent e is public.
1266 * No need for scatter-gather or BN_FLG_CONSTTIME.
1268 if (!BN_mod_exp_mont(r
, x
, e
, group
->order
, ctx
, group
->mont_data
))
1276 BN_CTX_free(new_ctx
);
1282 * Default behavior, if group->meth->field_inverse_mod_ord is NULL:
1283 * - When group->order is even, this function returns an error.
1284 * - When group->order is otherwise composite, the correctness
1285 * of the output is not guaranteed.
1286 * - When x is outside the range [1, group->order), the correctness
1287 * of the output is not guaranteed.
1288 * - Otherwise, this function returns the multiplicative inverse in the
1289 * range [1, group->order).
1291 * EC_METHODs must implement their own field_inverse_mod_ord for
1292 * other functionality.
1294 int ossl_ec_group_do_inverse_ord(const EC_GROUP
*group
, BIGNUM
*res
,
1295 const BIGNUM
*x
, BN_CTX
*ctx
)
1297 if (group
->meth
->field_inverse_mod_ord
!= NULL
)
1298 return group
->meth
->field_inverse_mod_ord(group
, res
, x
, ctx
);
1300 return ec_field_inverse_mod_ord(group
, res
, x
, ctx
);
1304 * Coordinate blinding for EC_POINT.
1306 * The underlying EC_METHOD can optionally implement this function:
1307 * underlying implementations should return 0 on errors, or 1 on
1310 * This wrapper returns 1 in case the underlying EC_METHOD does not
1311 * support coordinate blinding.
1313 int ossl_ec_point_blind_coordinates(const EC_GROUP
*group
, EC_POINT
*p
,
1316 if (group
->meth
->blind_coordinates
== NULL
)
1317 return 1; /* ignore if not implemented */
1319 return group
->meth
->blind_coordinates(group
, p
, ctx
);
1322 int EC_GROUP_get_basis_type(const EC_GROUP
*group
)
1326 if (EC_GROUP_get_field_type(group
) != NID_X9_62_characteristic_two_field
)
1327 /* everything else is currently not supported */
1330 /* Find the last non-zero element of group->poly[] */
1332 i
< (int)OSSL_NELEM(group
->poly
) && group
->poly
[i
] != 0;
1337 return NID_X9_62_ppBasis
;
1339 return NID_X9_62_tpBasis
;
1341 /* everything else is currently not supported */
1345 #ifndef OPENSSL_NO_EC2M
1346 int EC_GROUP_get_trinomial_basis(const EC_GROUP
*group
, unsigned int *k
)
1351 if (EC_GROUP_get_field_type(group
) != NID_X9_62_characteristic_two_field
1352 || !((group
->poly
[0] != 0) && (group
->poly
[1] != 0)
1353 && (group
->poly
[2] == 0))) {
1354 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1359 *k
= group
->poly
[1];
1364 int EC_GROUP_get_pentanomial_basis(const EC_GROUP
*group
, unsigned int *k1
,
1365 unsigned int *k2
, unsigned int *k3
)
1370 if (EC_GROUP_get_field_type(group
) != NID_X9_62_characteristic_two_field
1371 || !((group
->poly
[0] != 0) && (group
->poly
[1] != 0)
1372 && (group
->poly
[2] != 0) && (group
->poly
[3] != 0)
1373 && (group
->poly
[4] == 0))) {
1374 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1379 *k1
= group
->poly
[3];
1381 *k2
= group
->poly
[2];
1383 *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
);
1470 #endif /* FIPS_MODULE */
1472 static EC_GROUP
*group_new_from_name(const OSSL_PARAM
*p
,
1473 OSSL_LIB_CTX
*libctx
, const char *propq
)
1476 const char *curve_name
= NULL
;
1478 switch (p
->data_type
) {
1479 case OSSL_PARAM_UTF8_STRING
:
1480 /* The OSSL_PARAM functions have no support for this */
1481 curve_name
= p
->data
;
1482 ok
= (curve_name
!= NULL
);
1484 case OSSL_PARAM_UTF8_PTR
:
1485 ok
= OSSL_PARAM_get_utf8_ptr(p
, &curve_name
);
1490 nid
= ossl_ec_curve_name2nid(curve_name
);
1491 if (nid
== NID_undef
) {
1492 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_CURVE
);
1495 return EC_GROUP_new_by_curve_name_ex(libctx
, propq
, nid
);
1501 /* These parameters can be set directly into an EC_GROUP */
1502 int ossl_ec_group_set_params(EC_GROUP
*group
, const OSSL_PARAM params
[])
1504 int encoding_flag
= -1, format
= -1;
1505 const OSSL_PARAM
*p
;
1507 p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT
);
1509 if (!ossl_ec_pt_format_param2id(p
, &format
)) {
1510 ECerr(0, EC_R_INVALID_FORM
);
1513 EC_GROUP_set_point_conversion_form(group
, format
);
1516 p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_ENCODING
);
1518 if (!ossl_ec_encoding_param2id(p
, &encoding_flag
)) {
1519 ECerr(0, EC_R_INVALID_FORM
);
1522 EC_GROUP_set_asn1_flag(group
, encoding_flag
);
1525 p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_SEED
);
1527 /* The seed is allowed to be NULL */
1528 if (p
->data_type
!= OSSL_PARAM_OCTET_STRING
1529 || !EC_GROUP_set_seed(group
, p
->data
, p
->data_size
)) {
1530 ECerr(0, EC_R_INVALID_SEED
);
1537 EC_GROUP
*EC_GROUP_new_from_params(const OSSL_PARAM params
[],
1538 OSSL_LIB_CTX
*libctx
, const char *propq
)
1540 const OSSL_PARAM
*ptmp
;
1541 EC_GROUP
*group
= NULL
;
1544 const OSSL_PARAM
*pa
, *pb
;
1546 EC_GROUP
*named_group
= NULL
;
1547 BIGNUM
*p
= NULL
, *a
= NULL
, *b
= NULL
, *order
= NULL
, *cofactor
= NULL
;
1548 EC_POINT
*point
= NULL
;
1550 int is_prime_field
= 1;
1551 BN_CTX
*bnctx
= NULL
;
1552 const unsigned char *buf
= NULL
;
1553 int encoding_flag
= -1;
1556 /* This is the simple named group case */
1557 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_GROUP_NAME
);
1559 group
= group_new_from_name(ptmp
, libctx
, propq
);
1560 if (group
!= NULL
) {
1561 if (!ossl_ec_group_set_params(group
, params
)) {
1562 EC_GROUP_free(group
);
1569 ERR_raise(ERR_LIB_EC
, EC_R_EXPLICIT_PARAMS_NOT_SUPPORTED
);
1572 /* If it gets here then we are trying explicit parameters */
1573 bnctx
= BN_CTX_new_ex(libctx
);
1574 if (bnctx
== NULL
) {
1575 ERR_raise(ERR_LIB_EC
, ERR_R_MALLOC_FAILURE
);
1578 BN_CTX_start(bnctx
);
1580 p
= BN_CTX_get(bnctx
);
1581 a
= BN_CTX_get(bnctx
);
1582 b
= BN_CTX_get(bnctx
);
1583 order
= BN_CTX_get(bnctx
);
1584 if (order
== NULL
) {
1585 ERR_raise(ERR_LIB_EC
, ERR_R_MALLOC_FAILURE
);
1589 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_FIELD_TYPE
);
1590 if (ptmp
== NULL
|| ptmp
->data_type
!= OSSL_PARAM_UTF8_STRING
) {
1591 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_FIELD
);
1594 if (OPENSSL_strcasecmp(ptmp
->data
, SN_X9_62_prime_field
) == 0) {
1596 } else if (OPENSSL_strcasecmp(ptmp
->data
,
1597 SN_X9_62_characteristic_two_field
) == 0) {
1601 ERR_raise(ERR_LIB_EC
, EC_R_UNSUPPORTED_FIELD
);
1605 pa
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_A
);
1606 if (!OSSL_PARAM_get_BN(pa
, &a
)) {
1607 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_A
);
1610 pb
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_B
);
1611 if (!OSSL_PARAM_get_BN(pb
, &b
)) {
1612 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_B
);
1616 /* extract the prime number or irreducible polynomial */
1617 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_P
);
1618 if (!OSSL_PARAM_get_BN(ptmp
, &p
)) {
1619 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_P
);
1623 if (is_prime_field
) {
1624 if (BN_is_negative(p
) || BN_is_zero(p
)) {
1625 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_P
);
1628 field_bits
= BN_num_bits(p
);
1629 if (field_bits
> OPENSSL_ECC_MAX_FIELD_BITS
) {
1630 ERR_raise(ERR_LIB_EC
, EC_R_FIELD_TOO_LARGE
);
1634 /* create the EC_GROUP structure */
1635 group
= EC_GROUP_new_curve_GFp(p
, a
, b
, bnctx
);
1637 # ifdef OPENSSL_NO_EC2M
1638 ERR_raise(ERR_LIB_EC
, EC_R_GF2M_NOT_SUPPORTED
);
1641 /* create the EC_GROUP structure */
1642 group
= EC_GROUP_new_curve_GF2m(p
, a
, b
, NULL
);
1643 if (group
!= NULL
) {
1644 field_bits
= EC_GROUP_get_degree(group
);
1645 if (field_bits
> OPENSSL_ECC_MAX_FIELD_BITS
) {
1646 ERR_raise(ERR_LIB_EC
, EC_R_FIELD_TOO_LARGE
);
1650 # endif /* OPENSSL_NO_EC2M */
1653 if (group
== NULL
) {
1654 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
1659 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_SEED
);
1661 if (ptmp
->data_type
!= OSSL_PARAM_OCTET_STRING
) {
1662 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_SEED
);
1665 if (!EC_GROUP_set_seed(group
, ptmp
->data
, ptmp
->data_size
))
1669 /* generator base point */
1670 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_GENERATOR
);
1672 || ptmp
->data_type
!= OSSL_PARAM_OCTET_STRING
) {
1673 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_GENERATOR
);
1676 buf
= (const unsigned char *)(ptmp
->data
);
1677 if ((point
= EC_POINT_new(group
)) == NULL
)
1679 EC_GROUP_set_point_conversion_form(group
,
1680 (point_conversion_form_t
)buf
[0] & ~0x01);
1681 if (!EC_POINT_oct2point(group
, point
, buf
, ptmp
->data_size
, bnctx
)) {
1682 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_GENERATOR
);
1687 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_ORDER
);
1688 if (!OSSL_PARAM_get_BN(ptmp
, &order
)
1689 || (BN_is_negative(order
) || BN_is_zero(order
))
1690 || (BN_num_bits(order
) > (int)field_bits
+ 1)) { /* Hasse bound */
1691 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_GROUP_ORDER
);
1695 /* Optional cofactor */
1696 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_COFACTOR
);
1698 cofactor
= BN_CTX_get(bnctx
);
1699 if (cofactor
== NULL
|| !OSSL_PARAM_get_BN(ptmp
, &cofactor
)) {
1700 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_COFACTOR
);
1705 /* set the generator, order and cofactor (if present) */
1706 if (!EC_GROUP_set_generator(group
, point
, order
, cofactor
)) {
1707 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_GENERATOR
);
1711 named_group
= ec_group_explicit_to_named(group
, libctx
, propq
, bnctx
);
1712 if (named_group
== NULL
) {
1713 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_NAMED_GROUP_CONVERSION
);
1716 if (named_group
== group
) {
1718 * If we did not find a named group then the encoding should be explicit
1719 * if it was specified
1721 ptmp
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_ENCODING
);
1723 && !ossl_ec_encoding_param2id(ptmp
, &encoding_flag
)) {
1724 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_ENCODING
);
1727 if (encoding_flag
== OPENSSL_EC_NAMED_CURVE
) {
1728 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_ENCODING
);
1731 EC_GROUP_set_asn1_flag(group
, OPENSSL_EC_EXPLICIT_CURVE
);
1733 EC_GROUP_free(group
);
1734 group
= named_group
;
1739 EC_GROUP_free(group
);
1742 EC_POINT_free(point
);
1747 #endif /* FIPS_MODULE */