2 * Copyright 2001-2023 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 "internal/nelem.h"
27 /* functions for EC_GROUP objects */
29 EC_GROUP *ossl_ec_group_new_ex(OSSL_LIB_CTX *libctx, const char *propq,
30 const EC_METHOD *meth)
35 ERR_raise(ERR_LIB_EC, EC_R_SLOT_FULL);
38 if (meth->group_init == 0) {
39 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
43 ret = OPENSSL_zalloc(sizeof(*ret));
49 ret->propq = OPENSSL_strdup(propq);
50 if (ret->propq == NULL)
54 if ((ret->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
55 ret->order = BN_new();
56 if (ret->order == NULL)
58 ret->cofactor = BN_new();
59 if (ret->cofactor == NULL)
62 ret->asn1_flag = OPENSSL_EC_EXPLICIT_CURVE;
63 ret->asn1_form = POINT_CONVERSION_UNCOMPRESSED;
64 if (!meth->group_init(ret))
70 BN_free(ret->cofactor);
71 OPENSSL_free(ret->propq);
76 #ifndef OPENSSL_NO_DEPRECATED_3_0
78 EC_GROUP *EC_GROUP_new(const EC_METHOD *meth)
80 return ossl_ec_group_new_ex(NULL, NULL, meth);
85 void EC_pre_comp_free(EC_GROUP *group)
87 switch (group->pre_comp_type) {
91 #ifdef ECP_NISTZ256_ASM
92 EC_nistz256_pre_comp_free(group->pre_comp.nistz256);
95 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
97 EC_nistp224_pre_comp_free(group->pre_comp.nistp224);
100 EC_nistp256_pre_comp_free(group->pre_comp.nistp256);
103 ossl_ec_nistp384_pre_comp_free(group->pre_comp.nistp384);
106 EC_nistp521_pre_comp_free(group->pre_comp.nistp521);
116 EC_ec_pre_comp_free(group->pre_comp.ec);
119 group->pre_comp.ec = NULL;
122 void EC_GROUP_free(EC_GROUP *group)
127 if (group->meth->group_finish != 0)
128 group->meth->group_finish(group);
130 EC_pre_comp_free(group);
131 BN_MONT_CTX_free(group->mont_data);
132 EC_POINT_free(group->generator);
133 BN_free(group->order);
134 BN_free(group->cofactor);
135 OPENSSL_free(group->seed);
136 OPENSSL_free(group->propq);
140 #ifndef OPENSSL_NO_DEPRECATED_3_0
141 void EC_GROUP_clear_free(EC_GROUP *group)
146 if (group->meth->group_clear_finish != 0)
147 group->meth->group_clear_finish(group);
148 else if (group->meth->group_finish != 0)
149 group->meth->group_finish(group);
151 EC_pre_comp_free(group);
152 BN_MONT_CTX_free(group->mont_data);
153 EC_POINT_clear_free(group->generator);
154 BN_clear_free(group->order);
155 BN_clear_free(group->cofactor);
156 OPENSSL_clear_free(group->seed, group->seed_len);
157 OPENSSL_clear_free(group, sizeof(*group));
161 int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)
163 if (dest->meth->group_copy == 0) {
164 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
167 if (dest->meth != src->meth) {
168 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
174 dest->libctx = src->libctx;
175 dest->curve_name = src->curve_name;
177 /* Copy precomputed */
178 dest->pre_comp_type = src->pre_comp_type;
179 switch (src->pre_comp_type) {
181 dest->pre_comp.ec = NULL;
184 #ifdef ECP_NISTZ256_ASM
185 dest->pre_comp.nistz256 = EC_nistz256_pre_comp_dup(src->pre_comp.nistz256);
188 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
190 dest->pre_comp.nistp224 = EC_nistp224_pre_comp_dup(src->pre_comp.nistp224);
193 dest->pre_comp.nistp256 = EC_nistp256_pre_comp_dup(src->pre_comp.nistp256);
196 dest->pre_comp.nistp384 = ossl_ec_nistp384_pre_comp_dup(src->pre_comp.nistp384);
199 dest->pre_comp.nistp521 = EC_nistp521_pre_comp_dup(src->pre_comp.nistp521);
209 dest->pre_comp.ec = EC_ec_pre_comp_dup(src->pre_comp.ec);
213 if (src->mont_data != NULL) {
214 if (dest->mont_data == NULL) {
215 dest->mont_data = BN_MONT_CTX_new();
216 if (dest->mont_data == NULL)
219 if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data))
222 /* src->generator == NULL */
223 BN_MONT_CTX_free(dest->mont_data);
224 dest->mont_data = NULL;
227 if (src->generator != NULL) {
228 if (dest->generator == NULL) {
229 dest->generator = EC_POINT_new(dest);
230 if (dest->generator == NULL)
233 if (!EC_POINT_copy(dest->generator, src->generator))
236 /* src->generator == NULL */
237 EC_POINT_clear_free(dest->generator);
238 dest->generator = NULL;
241 if ((src->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
242 if (!BN_copy(dest->order, src->order))
244 if (!BN_copy(dest->cofactor, src->cofactor))
248 dest->asn1_flag = src->asn1_flag;
249 dest->asn1_form = src->asn1_form;
250 dest->decoded_from_explicit_params = src->decoded_from_explicit_params;
253 OPENSSL_free(dest->seed);
254 if ((dest->seed = OPENSSL_malloc(src->seed_len)) == NULL)
256 if (!memcpy(dest->seed, src->seed, src->seed_len))
258 dest->seed_len = src->seed_len;
260 OPENSSL_free(dest->seed);
265 return dest->meth->group_copy(dest, src);
268 EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)
276 if ((t = ossl_ec_group_new_ex(a->libctx, a->propq, a->meth)) == NULL)
278 if (!EC_GROUP_copy(t, a))
291 #ifndef OPENSSL_NO_DEPRECATED_3_0
292 const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group)
297 int EC_METHOD_get_field_type(const EC_METHOD *meth)
299 return meth->field_type;
303 static int ec_precompute_mont_data(EC_GROUP *);
306 * Try computing cofactor from the generator order (n) and field cardinality (q).
307 * This works for all curves of cryptographic interest.
309 * Hasse thm: q + 1 - 2*sqrt(q) <= n*h <= q + 1 + 2*sqrt(q)
310 * h_min = (q + 1 - 2*sqrt(q))/n
311 * h_max = (q + 1 + 2*sqrt(q))/n
312 * h_max - h_min = 4*sqrt(q)/n
313 * So if n > 4*sqrt(q) holds, there is only one possible value for h:
314 * h = \lfloor (h_min + h_max)/2 \rceil = \lfloor (q + 1)/n \rceil
316 * Otherwise, zero cofactor and return success.
318 static int ec_guess_cofactor(EC_GROUP *group) {
324 * If the cofactor is too large, we cannot guess it.
325 * The RHS of below is a strict overestimate of lg(4 * sqrt(q))
327 if (BN_num_bits(group->order) <= (BN_num_bits(group->field) + 1) / 2 + 3) {
329 BN_zero(group->cofactor);
334 if ((ctx = BN_CTX_new_ex(group->libctx)) == NULL)
338 if ((q = BN_CTX_get(ctx)) == NULL)
341 /* set q = 2**m for binary fields; q = p otherwise */
342 if (group->meth->field_type == NID_X9_62_characteristic_two_field) {
344 if (!BN_set_bit(q, BN_num_bits(group->field) - 1))
347 if (!BN_copy(q, group->field))
351 /* compute h = \lfloor (q + 1)/n \rceil = \lfloor (q + 1 + n/2)/n \rfloor */
352 if (!BN_rshift1(group->cofactor, group->order) /* n/2 */
353 || !BN_add(group->cofactor, group->cofactor, q) /* q + n/2 */
355 || !BN_add(group->cofactor, group->cofactor, BN_value_one())
356 /* (q + 1 + n/2)/n */
357 || !BN_div(group->cofactor, NULL, group->cofactor, group->order, ctx))
366 int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
367 const BIGNUM *order, const BIGNUM *cofactor)
369 if (generator == NULL) {
370 ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
374 /* require group->field >= 1 */
375 if (group->field == NULL || BN_is_zero(group->field)
376 || BN_is_negative(group->field)) {
377 ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD);
382 * - require order >= 1
383 * - enforce upper bound due to Hasse thm: order can be no more than one bit
384 * longer than field cardinality
386 if (order == NULL || BN_is_zero(order) || BN_is_negative(order)
387 || BN_num_bits(order) > BN_num_bits(group->field) + 1) {
388 ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER);
393 * Unfortunately the cofactor is an optional field in many standards.
394 * Internally, the lib uses 0 cofactor as a marker for "unknown cofactor".
395 * So accept cofactor == NULL or cofactor >= 0.
397 if (cofactor != NULL && BN_is_negative(cofactor)) {
398 ERR_raise(ERR_LIB_EC, EC_R_UNKNOWN_COFACTOR);
402 if (group->generator == NULL) {
403 group->generator = EC_POINT_new(group);
404 if (group->generator == NULL)
407 if (!EC_POINT_copy(group->generator, generator))
410 if (!BN_copy(group->order, order))
413 /* Either take the provided positive cofactor, or try to compute it */
414 if (cofactor != NULL && !BN_is_zero(cofactor)) {
415 if (!BN_copy(group->cofactor, cofactor))
417 } else if (!ec_guess_cofactor(group)) {
418 BN_zero(group->cofactor);
423 * Some groups have an order with
424 * factors of two, which makes the Montgomery setup fail.
425 * |group->mont_data| will be NULL in this case.
427 if (BN_is_odd(group->order)) {
428 return ec_precompute_mont_data(group);
431 BN_MONT_CTX_free(group->mont_data);
432 group->mont_data = NULL;
436 const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
438 return group->generator;
441 BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group)
443 return group->mont_data;
446 int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
448 if (group->order == NULL)
450 if (!BN_copy(order, group->order))
453 return !BN_is_zero(order);
456 const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group)
461 int EC_GROUP_order_bits(const EC_GROUP *group)
463 return group->meth->group_order_bits(group);
466 int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
470 if (group->cofactor == NULL)
472 if (!BN_copy(cofactor, group->cofactor))
475 return !BN_is_zero(group->cofactor);
478 const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group)
480 return group->cofactor;
483 void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)
485 group->curve_name = nid;
488 ? OPENSSL_EC_NAMED_CURVE
489 : OPENSSL_EC_EXPLICIT_CURVE;
492 int EC_GROUP_get_curve_name(const EC_GROUP *group)
494 return group->curve_name;
497 const BIGNUM *EC_GROUP_get0_field(const EC_GROUP *group)
502 int EC_GROUP_get_field_type(const EC_GROUP *group)
504 return group->meth->field_type;
507 void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
509 group->asn1_flag = flag;
512 int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
514 return group->asn1_flag;
517 void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
518 point_conversion_form_t form)
520 group->asn1_form = form;
523 point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP
526 return group->asn1_form;
529 size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
531 OPENSSL_free(group->seed);
538 if ((group->seed = OPENSSL_malloc(len)) == NULL)
540 memcpy(group->seed, p, len);
541 group->seed_len = len;
546 unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
551 size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
553 return group->seed_len;
556 int EC_GROUP_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
557 const BIGNUM *b, BN_CTX *ctx)
559 if (group->meth->group_set_curve == 0) {
560 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
563 return group->meth->group_set_curve(group, p, a, b, ctx);
566 int EC_GROUP_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b,
569 if (group->meth->group_get_curve == NULL) {
570 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
573 return group->meth->group_get_curve(group, p, a, b, ctx);
576 #ifndef OPENSSL_NO_DEPRECATED_3_0
577 int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
578 const BIGNUM *b, BN_CTX *ctx)
580 return EC_GROUP_set_curve(group, p, a, b, ctx);
583 int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
584 BIGNUM *b, BN_CTX *ctx)
586 return EC_GROUP_get_curve(group, p, a, b, ctx);
589 # ifndef OPENSSL_NO_EC2M
590 int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
591 const BIGNUM *b, BN_CTX *ctx)
593 return EC_GROUP_set_curve(group, p, a, b, ctx);
596 int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
597 BIGNUM *b, BN_CTX *ctx)
599 return EC_GROUP_get_curve(group, p, a, b, ctx);
604 int EC_GROUP_get_degree(const EC_GROUP *group)
606 if (group->meth->group_get_degree == 0) {
607 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
610 return group->meth->group_get_degree(group);
613 int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
615 if (group->meth->group_check_discriminant == 0) {
616 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
619 return group->meth->group_check_discriminant(group, ctx);
622 int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
625 BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
627 BN_CTX *ctx_new = NULL;
630 /* compare the field types */
631 if (EC_GROUP_get_field_type(a) != EC_GROUP_get_field_type(b))
633 /* compare the curve name (if present in both) */
634 if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
635 EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
637 if (a->meth->flags & EC_FLAGS_CUSTOM_CURVE)
642 ctx_new = ctx = BN_CTX_new();
648 a1 = BN_CTX_get(ctx);
649 a2 = BN_CTX_get(ctx);
650 a3 = BN_CTX_get(ctx);
651 b1 = BN_CTX_get(ctx);
652 b2 = BN_CTX_get(ctx);
653 b3 = BN_CTX_get(ctx);
657 BN_CTX_free(ctx_new);
663 * XXX This approach assumes that the external representation of curves
664 * over the same field type is the same.
666 if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
667 !b->meth->group_get_curve(b, b1, b2, b3, ctx))
670 /* return 1 if the curve parameters are different */
671 if (r || BN_cmp(a1, b1) != 0 || BN_cmp(a2, b2) != 0 || BN_cmp(a3, b3) != 0)
674 /* XXX EC_POINT_cmp() assumes that the methods are equal */
675 /* return 1 if the generators are different */
676 if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
677 EC_GROUP_get0_generator(b), ctx) != 0)
681 const BIGNUM *ao, *bo, *ac, *bc;
682 /* compare the orders */
683 ao = EC_GROUP_get0_order(a);
684 bo = EC_GROUP_get0_order(b);
685 if (ao == NULL || bo == NULL) {
686 /* return an error if either order is NULL */
690 if (BN_cmp(ao, bo) != 0) {
691 /* return 1 if orders are different */
696 * It gets here if the curve parameters and generator matched.
697 * Now check the optional cofactors (if both are present).
699 ac = EC_GROUP_get0_cofactor(a);
700 bc = EC_GROUP_get0_cofactor(b);
701 /* Returns 1 (mismatch) if both cofactors are specified and different */
702 if (!BN_is_zero(ac) && !BN_is_zero(bc) && BN_cmp(ac, bc) != 0)
704 /* Returns 0 if the parameters matched */
709 BN_CTX_free(ctx_new);
714 /* functions for EC_POINT objects */
716 EC_POINT *EC_POINT_new(const EC_GROUP *group)
721 ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
724 if (group->meth->point_init == NULL) {
725 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
729 ret = OPENSSL_zalloc(sizeof(*ret));
733 ret->meth = group->meth;
734 ret->curve_name = group->curve_name;
736 if (!ret->meth->point_init(ret)) {
744 void EC_POINT_free(EC_POINT *point)
750 EC_POINT_clear_free(point);
752 if (point->meth->point_finish != 0)
753 point->meth->point_finish(point);
758 void EC_POINT_clear_free(EC_POINT *point)
763 if (point->meth->point_clear_finish != 0)
764 point->meth->point_clear_finish(point);
765 else if (point->meth->point_finish != 0)
766 point->meth->point_finish(point);
767 OPENSSL_clear_free(point, sizeof(*point));
770 int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)
772 if (dest->meth->point_copy == 0) {
773 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
776 if (dest->meth != src->meth
777 || (dest->curve_name != src->curve_name
778 && dest->curve_name != 0
779 && src->curve_name != 0)) {
780 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
785 return dest->meth->point_copy(dest, src);
788 EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
796 t = EC_POINT_new(group);
799 r = EC_POINT_copy(t, a);
807 #ifndef OPENSSL_NO_DEPRECATED_3_0
808 const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
814 int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
816 if (group->meth->point_set_to_infinity == 0) {
817 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
820 if (group->meth != point->meth) {
821 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
824 return group->meth->point_set_to_infinity(group, point);
827 #ifndef OPENSSL_NO_DEPRECATED_3_0
828 int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
829 EC_POINT *point, const BIGNUM *x,
830 const BIGNUM *y, const BIGNUM *z,
833 if (group->meth->field_type != NID_X9_62_prime_field) {
834 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
837 if (!ec_point_is_compat(point, group)) {
838 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
841 return ossl_ec_GFp_simple_set_Jprojective_coordinates_GFp(group, point,
845 int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
846 const EC_POINT *point, BIGNUM *x,
847 BIGNUM *y, BIGNUM *z,
850 if (group->meth->field_type != NID_X9_62_prime_field) {
851 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
854 if (!ec_point_is_compat(point, group)) {
855 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
858 return ossl_ec_GFp_simple_get_Jprojective_coordinates_GFp(group, point,
863 int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point,
864 const BIGNUM *x, const BIGNUM *y,
867 if (group->meth->point_set_affine_coordinates == NULL) {
868 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
871 if (!ec_point_is_compat(point, group)) {
872 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
875 if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx))
878 if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {
879 ERR_raise(ERR_LIB_EC, EC_R_POINT_IS_NOT_ON_CURVE);
885 #ifndef OPENSSL_NO_DEPRECATED_3_0
886 int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
887 EC_POINT *point, const BIGNUM *x,
888 const BIGNUM *y, BN_CTX *ctx)
890 return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
893 # ifndef OPENSSL_NO_EC2M
894 int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,
895 EC_POINT *point, const BIGNUM *x,
896 const BIGNUM *y, BN_CTX *ctx)
898 return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
903 int EC_POINT_get_affine_coordinates(const EC_GROUP *group,
904 const EC_POINT *point, BIGNUM *x, BIGNUM *y,
907 if (group->meth->point_get_affine_coordinates == NULL) {
908 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
911 if (!ec_point_is_compat(point, group)) {
912 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
915 if (EC_POINT_is_at_infinity(group, point)) {
916 ERR_raise(ERR_LIB_EC, 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 ERR_raise(ERR_LIB_EC, 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 ERR_raise(ERR_LIB_EC, 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 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
962 if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)) {
963 ERR_raise(ERR_LIB_EC, 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 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
975 if (!ec_point_is_compat(a, group)) {
976 ERR_raise(ERR_LIB_EC, 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 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
988 if (!ec_point_is_compat(point, group)) {
989 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
992 return group->meth->is_at_infinity(group, point);
996 * Check whether an EC_POINT is on the curve or not. Note that the return
997 * value for this function should NOT be treated as a boolean. Return values:
998 * 1: The point is on the curve
999 * 0: The point is not on the curve
1000 * -1: An error occurred
1002 int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
1005 if (group->meth->is_on_curve == 0) {
1006 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1009 if (!ec_point_is_compat(point, group)) {
1010 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
1013 return group->meth->is_on_curve(group, point, ctx);
1016 int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
1019 if (group->meth->point_cmp == 0) {
1020 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1023 if (!ec_point_is_compat(a, group) || !ec_point_is_compat(b, group)) {
1024 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
1027 return group->meth->point_cmp(group, a, b, ctx);
1030 #ifndef OPENSSL_NO_DEPRECATED_3_0
1031 int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
1033 if (group->meth->make_affine == 0) {
1034 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1037 if (!ec_point_is_compat(point, group)) {
1038 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
1041 return group->meth->make_affine(group, point, ctx);
1044 int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
1045 EC_POINT *points[], BN_CTX *ctx)
1049 if (group->meth->points_make_affine == 0) {
1050 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1053 for (i = 0; i < num; i++) {
1054 if (!ec_point_is_compat(points[i], group)) {
1055 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
1059 return group->meth->points_make_affine(group, num, points, ctx);
1064 * Functions for point multiplication. If group->meth->mul is 0, we use the
1065 * wNAF-based implementations in ec_mult.c; otherwise we dispatch through
1069 #ifndef OPENSSL_NO_DEPRECATED_3_0
1070 int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
1071 size_t num, const EC_POINT *points[],
1072 const BIGNUM *scalars[], BN_CTX *ctx)
1077 BN_CTX *new_ctx = NULL;
1080 if (!ec_point_is_compat(r, group)) {
1081 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
1085 if (scalar == NULL && num == 0)
1086 return EC_POINT_set_to_infinity(group, r);
1088 for (i = 0; i < num; i++) {
1089 if (!ec_point_is_compat(points[i], group)) {
1090 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
1097 ctx = new_ctx = BN_CTX_secure_new();
1100 ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
1104 if (group->meth->mul != NULL)
1105 ret = group->meth->mul(group, r, scalar, num, points, scalars, ctx);
1108 ret = ossl_ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
1111 BN_CTX_free(new_ctx);
1117 int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
1118 const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
1123 BN_CTX *new_ctx = NULL;
1126 if (!ec_point_is_compat(r, group)
1127 || (point != NULL && !ec_point_is_compat(point, group))) {
1128 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
1132 if (g_scalar == NULL && p_scalar == NULL)
1133 return EC_POINT_set_to_infinity(group, r);
1137 ctx = new_ctx = BN_CTX_secure_new();
1140 ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
1144 num = (point != NULL && p_scalar != NULL) ? 1 : 0;
1145 if (group->meth->mul != NULL)
1146 ret = group->meth->mul(group, r, g_scalar, num, &point, &p_scalar, ctx);
1149 ret = ossl_ec_wNAF_mul(group, r, g_scalar, num, &point, &p_scalar, ctx);
1152 BN_CTX_free(new_ctx);
1157 #ifndef OPENSSL_NO_DEPRECATED_3_0
1158 int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
1160 if (group->meth->mul == 0)
1162 return ossl_ec_wNAF_precompute_mult(group, ctx);
1164 if (group->meth->precompute_mult != 0)
1165 return group->meth->precompute_mult(group, ctx);
1167 return 1; /* nothing to do, so report success */
1170 int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
1172 if (group->meth->mul == 0)
1174 return ossl_ec_wNAF_have_precompute_mult(group);
1176 if (group->meth->have_precompute_mult != 0)
1177 return group->meth->have_precompute_mult(group);
1179 return 0; /* cannot tell whether precomputation has
1185 * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
1186 * returns one on success. On error it returns zero.
1188 static int ec_precompute_mont_data(EC_GROUP *group)
1190 BN_CTX *ctx = BN_CTX_new_ex(group->libctx);
1193 BN_MONT_CTX_free(group->mont_data);
1194 group->mont_data = NULL;
1199 group->mont_data = BN_MONT_CTX_new();
1200 if (group->mont_data == NULL)
1203 if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) {
1204 BN_MONT_CTX_free(group->mont_data);
1205 group->mont_data = NULL;
1218 int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg)
1220 return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
1223 void *EC_KEY_get_ex_data(const EC_KEY *key, int idx)
1225 return CRYPTO_get_ex_data(&key->ex_data, idx);
1229 int ossl_ec_group_simple_order_bits(const EC_GROUP *group)
1231 if (group->order == NULL)
1233 return BN_num_bits(group->order);
1236 static int ec_field_inverse_mod_ord(const EC_GROUP *group, BIGNUM *r,
1237 const BIGNUM *x, BN_CTX *ctx)
1242 BN_CTX *new_ctx = NULL;
1245 if (group->mont_data == NULL)
1250 ctx = new_ctx = BN_CTX_secure_new();
1256 if ((e = BN_CTX_get(ctx)) == NULL)
1260 * We want inverse in constant time, therefore we utilize the fact
1261 * order must be prime and use Fermats Little Theorem instead.
1263 if (!BN_set_word(e, 2))
1265 if (!BN_sub(e, group->order, e))
1268 * Exponent e is public.
1269 * No need for scatter-gather or BN_FLG_CONSTTIME.
1271 if (!BN_mod_exp_mont(r, x, e, group->order, ctx, group->mont_data))
1279 BN_CTX_free(new_ctx);
1285 * Default behavior, if group->meth->field_inverse_mod_ord is NULL:
1286 * - When group->order is even, this function returns an error.
1287 * - When group->order is otherwise composite, the correctness
1288 * of the output is not guaranteed.
1289 * - When x is outside the range [1, group->order), the correctness
1290 * of the output is not guaranteed.
1291 * - Otherwise, this function returns the multiplicative inverse in the
1292 * range [1, group->order).
1294 * EC_METHODs must implement their own field_inverse_mod_ord for
1295 * other functionality.
1297 int ossl_ec_group_do_inverse_ord(const EC_GROUP *group, BIGNUM *res,
1298 const BIGNUM *x, BN_CTX *ctx)
1300 if (group->meth->field_inverse_mod_ord != NULL)
1301 return group->meth->field_inverse_mod_ord(group, res, x, ctx);
1303 return ec_field_inverse_mod_ord(group, res, x, ctx);
1307 * Coordinate blinding for EC_POINT.
1309 * The underlying EC_METHOD can optionally implement this function:
1310 * underlying implementations should return 0 on errors, or 1 on
1313 * This wrapper returns 1 in case the underlying EC_METHOD does not
1314 * support coordinate blinding.
1316 int ossl_ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p,
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 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1362 *k = group->poly[1];
1367 int EC_GROUP_get_pentanomial_basis(const EC_GROUP *group, unsigned int *k1,
1368 unsigned int *k2, unsigned int *k3)
1373 if (EC_GROUP_get_field_type(group) != NID_X9_62_characteristic_two_field
1374 || !((group->poly[0] != 0) && (group->poly[1] != 0)
1375 && (group->poly[2] != 0) && (group->poly[3] != 0)
1376 && (group->poly[4] == 0))) {
1377 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1382 *k1 = group->poly[3];
1384 *k2 = group->poly[2];
1386 *k3 = group->poly[1];
1394 * Check if the explicit parameters group matches any built-in curves.
1396 * We create a copy of the group just built, so that we can remove optional
1397 * fields for the lookup: we do this to avoid the possibility that one of
1398 * the optional parameters is used to force the library into using a less
1399 * performant and less secure EC_METHOD instead of the specialized one.
1400 * In any case, `seed` is not really used in any computation, while a
1401 * cofactor different from the one in the built-in table is just
1402 * mathematically wrong anyway and should not be used.
1404 static EC_GROUP *ec_group_explicit_to_named(const EC_GROUP *group,
1405 OSSL_LIB_CTX *libctx,
1409 EC_GROUP *ret_group = NULL, *dup = NULL;
1412 const EC_POINT *point = EC_GROUP_get0_generator(group);
1413 const BIGNUM *order = EC_GROUP_get0_order(group);
1414 int no_seed = (EC_GROUP_get0_seed(group) == NULL);
1416 if ((dup = EC_GROUP_dup(group)) == NULL
1417 || EC_GROUP_set_seed(dup, NULL, 0) != 1
1418 || !EC_GROUP_set_generator(dup, point, order, NULL))
1420 if ((curve_name_nid = ossl_ec_curve_nid_from_params(dup, ctx)) != NID_undef) {
1422 * The input explicit parameters successfully matched one of the
1423 * built-in curves: often for built-in curves we have specialized
1424 * methods with better performance and hardening.
1426 * In this case we replace the `EC_GROUP` created through explicit
1427 * parameters with one created from a named group.
1430 # ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
1432 * NID_wap_wsg_idm_ecid_wtls12 and NID_secp224r1 are both aliases for
1433 * the same curve, we prefer the SECP nid when matching explicit
1434 * parameters as that is associated with a specialized EC_METHOD.
1436 if (curve_name_nid == NID_wap_wsg_idm_ecid_wtls12)
1437 curve_name_nid = NID_secp224r1;
1438 # endif /* !def(OPENSSL_NO_EC_NISTP_64_GCC_128) */
1440 ret_group = EC_GROUP_new_by_curve_name_ex(libctx, propq, curve_name_nid);
1441 if (ret_group == NULL)
1445 * Set the flag so that EC_GROUPs created from explicit parameters are
1446 * serialized using explicit parameters by default.
1448 EC_GROUP_set_asn1_flag(ret_group, OPENSSL_EC_EXPLICIT_CURVE);
1451 * If the input params do not contain the optional seed field we make
1452 * sure it is not added to the returned group.
1454 * The seed field is not really used inside libcrypto anyway, and
1455 * adding it to parsed explicit parameter keys would alter their DER
1456 * encoding output (because of the extra field) which could impact
1457 * applications fingerprinting keys by their DER encoding.
1460 if (EC_GROUP_set_seed(ret_group, NULL, 0) != 1)
1464 ret_group = (EC_GROUP *)group;
1470 EC_GROUP_free(ret_group);
1473 #endif /* FIPS_MODULE */
1475 static EC_GROUP *group_new_from_name(const OSSL_PARAM *p,
1476 OSSL_LIB_CTX *libctx, const char *propq)
1479 const char *curve_name = NULL;
1481 switch (p->data_type) {
1482 case OSSL_PARAM_UTF8_STRING:
1483 /* The OSSL_PARAM functions have no support for this */
1484 curve_name = p->data;
1485 ok = (curve_name != NULL);
1487 case OSSL_PARAM_UTF8_PTR:
1488 ok = OSSL_PARAM_get_utf8_ptr(p, &curve_name);
1493 nid = ossl_ec_curve_name2nid(curve_name);
1494 if (nid == NID_undef) {
1495 ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE);
1498 return EC_GROUP_new_by_curve_name_ex(libctx, propq, nid);
1504 /* These parameters can be set directly into an EC_GROUP */
1505 int ossl_ec_group_set_params(EC_GROUP *group, const OSSL_PARAM params[])
1507 int encoding_flag = -1, format = -1;
1508 const OSSL_PARAM *p;
1510 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT);
1512 if (!ossl_ec_pt_format_param2id(p, &format)) {
1513 ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM);
1516 EC_GROUP_set_point_conversion_form(group, format);
1519 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ENCODING);
1521 if (!ossl_ec_encoding_param2id(p, &encoding_flag)) {
1522 ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM);
1525 EC_GROUP_set_asn1_flag(group, encoding_flag);
1528 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_SEED);
1530 /* The seed is allowed to be NULL */
1531 if (p->data_type != OSSL_PARAM_OCTET_STRING
1532 || !EC_GROUP_set_seed(group, p->data, p->data_size)) {
1533 ERR_raise(ERR_LIB_EC, EC_R_INVALID_SEED);
1540 EC_GROUP *EC_GROUP_new_from_params(const OSSL_PARAM params[],
1541 OSSL_LIB_CTX *libctx, const char *propq)
1543 const OSSL_PARAM *ptmp;
1544 EC_GROUP *group = NULL;
1547 const OSSL_PARAM *pa, *pb;
1549 EC_GROUP *named_group = NULL;
1550 BIGNUM *p = NULL, *a = NULL, *b = NULL, *order = NULL, *cofactor = NULL;
1551 EC_POINT *point = NULL;
1553 int is_prime_field = 1;
1554 BN_CTX *bnctx = NULL;
1555 const unsigned char *buf = NULL;
1556 int encoding_flag = -1;
1559 /* This is the simple named group case */
1560 ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME);
1564 if ((group = group_new_from_name(ptmp, libctx, propq)) == NULL)
1566 if (!ossl_ec_group_set_params(group, params)) {
1567 EC_GROUP_free(group);
1571 ptmp = OSSL_PARAM_locate_const(params,
1572 OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS);
1573 if (ptmp != NULL && !OSSL_PARAM_get_int(ptmp, &decoded)) {
1574 ERR_raise(ERR_LIB_EC, EC_R_WRONG_CURVE_PARAMETERS);
1575 EC_GROUP_free(group);
1578 group->decoded_from_explicit_params = decoded > 0;
1582 ERR_raise(ERR_LIB_EC, EC_R_EXPLICIT_PARAMS_NOT_SUPPORTED);
1585 /* If it gets here then we are trying explicit parameters */
1586 bnctx = BN_CTX_new_ex(libctx);
1587 if (bnctx == NULL) {
1588 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
1591 BN_CTX_start(bnctx);
1593 p = BN_CTX_get(bnctx);
1594 a = BN_CTX_get(bnctx);
1595 b = BN_CTX_get(bnctx);
1596 order = BN_CTX_get(bnctx);
1597 if (order == NULL) {
1598 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
1602 ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_FIELD_TYPE);
1603 if (ptmp == NULL || ptmp->data_type != OSSL_PARAM_UTF8_STRING) {
1604 ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD);
1607 if (OPENSSL_strcasecmp(ptmp->data, SN_X9_62_prime_field) == 0) {
1609 } else if (OPENSSL_strcasecmp(ptmp->data,
1610 SN_X9_62_characteristic_two_field) == 0) {
1614 ERR_raise(ERR_LIB_EC, EC_R_UNSUPPORTED_FIELD);
1618 pa = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_A);
1619 if (!OSSL_PARAM_get_BN(pa, &a)) {
1620 ERR_raise(ERR_LIB_EC, EC_R_INVALID_A);
1623 pb = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_B);
1624 if (!OSSL_PARAM_get_BN(pb, &b)) {
1625 ERR_raise(ERR_LIB_EC, EC_R_INVALID_B);
1629 /* extract the prime number or irreducible polynomial */
1630 ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_P);
1631 if (!OSSL_PARAM_get_BN(ptmp, &p)) {
1632 ERR_raise(ERR_LIB_EC, EC_R_INVALID_P);
1636 if (is_prime_field) {
1637 if (BN_is_negative(p) || BN_is_zero(p)) {
1638 ERR_raise(ERR_LIB_EC, EC_R_INVALID_P);
1641 field_bits = BN_num_bits(p);
1642 if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
1643 ERR_raise(ERR_LIB_EC, EC_R_FIELD_TOO_LARGE);
1647 /* create the EC_GROUP structure */
1648 group = EC_GROUP_new_curve_GFp(p, a, b, bnctx);
1650 # ifdef OPENSSL_NO_EC2M
1651 ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED);
1654 /* create the EC_GROUP structure */
1655 group = EC_GROUP_new_curve_GF2m(p, a, b, NULL);
1656 if (group != NULL) {
1657 field_bits = EC_GROUP_get_degree(group);
1658 if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
1659 ERR_raise(ERR_LIB_EC, EC_R_FIELD_TOO_LARGE);
1663 # endif /* OPENSSL_NO_EC2M */
1666 if (group == NULL) {
1667 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
1672 ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_SEED);
1674 if (ptmp->data_type != OSSL_PARAM_OCTET_STRING) {
1675 ERR_raise(ERR_LIB_EC, EC_R_INVALID_SEED);
1678 if (!EC_GROUP_set_seed(group, ptmp->data, ptmp->data_size))
1682 /* generator base point */
1683 ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_GENERATOR);
1685 || ptmp->data_type != OSSL_PARAM_OCTET_STRING) {
1686 ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);
1689 buf = (const unsigned char *)(ptmp->data);
1690 if ((point = EC_POINT_new(group)) == NULL)
1692 EC_GROUP_set_point_conversion_form(group,
1693 (point_conversion_form_t)buf[0] & ~0x01);
1694 if (!EC_POINT_oct2point(group, point, buf, ptmp->data_size, bnctx)) {
1695 ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);
1700 ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ORDER);
1701 if (!OSSL_PARAM_get_BN(ptmp, &order)
1702 || (BN_is_negative(order) || BN_is_zero(order))
1703 || (BN_num_bits(order) > (int)field_bits + 1)) { /* Hasse bound */
1704 ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER);
1708 /* Optional cofactor */
1709 ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_COFACTOR);
1711 cofactor = BN_CTX_get(bnctx);
1712 if (cofactor == NULL || !OSSL_PARAM_get_BN(ptmp, &cofactor)) {
1713 ERR_raise(ERR_LIB_EC, EC_R_INVALID_COFACTOR);
1718 /* set the generator, order and cofactor (if present) */
1719 if (!EC_GROUP_set_generator(group, point, order, cofactor)) {
1720 ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);
1724 named_group = ec_group_explicit_to_named(group, libctx, propq, bnctx);
1725 if (named_group == NULL) {
1726 ERR_raise(ERR_LIB_EC, EC_R_INVALID_NAMED_GROUP_CONVERSION);
1729 if (named_group == group) {
1731 * If we did not find a named group then the encoding should be explicit
1732 * if it was specified
1734 ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ENCODING);
1736 && !ossl_ec_encoding_param2id(ptmp, &encoding_flag)) {
1737 ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
1740 if (encoding_flag == OPENSSL_EC_NAMED_CURVE) {
1741 ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
1744 EC_GROUP_set_asn1_flag(group, OPENSSL_EC_EXPLICIT_CURVE);
1746 EC_GROUP_free(group);
1747 group = named_group;
1749 /* We've imported the group from explicit parameters, set it so. */
1750 group->decoded_from_explicit_params = 1;
1754 EC_GROUP_free(group);
1757 EC_POINT_free(point);
1762 #endif /* FIPS_MODULE */
1765 OSSL_PARAM *EC_GROUP_to_params(const EC_GROUP *group, OSSL_LIB_CTX *libctx,
1766 const char *propq, BN_CTX *bnctx)
1768 OSSL_PARAM_BLD *tmpl = NULL;
1769 BN_CTX *new_bnctx = NULL;
1770 unsigned char *gen_buf = NULL;
1771 OSSL_PARAM *params = NULL;
1776 tmpl = OSSL_PARAM_BLD_new();
1781 bnctx = new_bnctx = BN_CTX_new_ex(libctx);
1784 BN_CTX_start(bnctx);
1786 if (!ossl_ec_group_todata(
1787 group, tmpl, NULL, libctx, propq, bnctx, &gen_buf))
1790 params = OSSL_PARAM_BLD_to_param(tmpl);
1793 OSSL_PARAM_BLD_free(tmpl);
1794 OPENSSL_free(gen_buf);
1796 BN_CTX_free(new_bnctx);