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"
19 #include <openssl/err.h>
20 #include <openssl/opensslv.h>
24 /* functions for EC_GROUP objects */
26 EC_GROUP *ec_group_new_with_libctx(OPENSSL_CTX *libctx, const char *propq,
27 const EC_METHOD *meth)
32 ECerr(0, EC_R_SLOT_FULL);
35 if (meth->group_init == 0) {
36 ECerr(0, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
40 ret = OPENSSL_zalloc(sizeof(*ret));
42 ECerr(0, ERR_R_MALLOC_FAILURE);
48 ret->propq = OPENSSL_strdup(propq);
49 if (ret->propq == NULL) {
50 ECerr(0, ERR_R_MALLOC_FAILURE);
55 if ((ret->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
56 ret->order = BN_new();
57 if (ret->order == NULL)
59 ret->cofactor = BN_new();
60 if (ret->cofactor == NULL)
63 ret->asn1_flag = OPENSSL_EC_NAMED_CURVE;
64 ret->asn1_form = POINT_CONVERSION_UNCOMPRESSED;
65 if (!meth->group_init(ret))
71 BN_free(ret->cofactor);
72 OPENSSL_free(ret->propq);
77 #ifndef OPENSSL_NO_DEPRECATED_3_0
79 EC_GROUP *EC_GROUP_new(const EC_METHOD *meth)
81 return ec_group_new_with_libctx(NULL, NULL, meth);
86 void EC_pre_comp_free(EC_GROUP *group)
88 switch (group->pre_comp_type) {
92 #ifdef ECP_NISTZ256_ASM
93 EC_nistz256_pre_comp_free(group->pre_comp.nistz256);
96 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
98 EC_nistp224_pre_comp_free(group->pre_comp.nistp224);
101 EC_nistp256_pre_comp_free(group->pre_comp.nistp256);
104 EC_nistp521_pre_comp_free(group->pre_comp.nistp521);
113 EC_ec_pre_comp_free(group->pre_comp.ec);
116 group->pre_comp.ec = NULL;
119 void EC_GROUP_free(EC_GROUP *group)
124 if (group->meth->group_finish != 0)
125 group->meth->group_finish(group);
127 EC_pre_comp_free(group);
128 BN_MONT_CTX_free(group->mont_data);
129 EC_POINT_free(group->generator);
130 BN_free(group->order);
131 BN_free(group->cofactor);
132 OPENSSL_free(group->seed);
133 OPENSSL_free(group->propq);
137 #ifndef OPENSSL_NO_DEPRECATED_3_0
138 void EC_GROUP_clear_free(EC_GROUP *group)
143 if (group->meth->group_clear_finish != 0)
144 group->meth->group_clear_finish(group);
145 else if (group->meth->group_finish != 0)
146 group->meth->group_finish(group);
148 EC_pre_comp_free(group);
149 BN_MONT_CTX_free(group->mont_data);
150 EC_POINT_clear_free(group->generator);
151 BN_clear_free(group->order);
152 BN_clear_free(group->cofactor);
153 OPENSSL_clear_free(group->seed, group->seed_len);
154 OPENSSL_clear_free(group, sizeof(*group));
158 int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)
160 if (dest->meth->group_copy == 0) {
161 ECerr(EC_F_EC_GROUP_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
164 if (dest->meth != src->meth) {
165 ECerr(EC_F_EC_GROUP_COPY, EC_R_INCOMPATIBLE_OBJECTS);
171 dest->libctx = src->libctx;
172 dest->curve_name = src->curve_name;
174 /* Copy precomputed */
175 dest->pre_comp_type = src->pre_comp_type;
176 switch (src->pre_comp_type) {
178 dest->pre_comp.ec = NULL;
181 #ifdef ECP_NISTZ256_ASM
182 dest->pre_comp.nistz256 = EC_nistz256_pre_comp_dup(src->pre_comp.nistz256);
185 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
187 dest->pre_comp.nistp224 = EC_nistp224_pre_comp_dup(src->pre_comp.nistp224);
190 dest->pre_comp.nistp256 = EC_nistp256_pre_comp_dup(src->pre_comp.nistp256);
193 dest->pre_comp.nistp521 = EC_nistp521_pre_comp_dup(src->pre_comp.nistp521);
202 dest->pre_comp.ec = EC_ec_pre_comp_dup(src->pre_comp.ec);
206 if (src->mont_data != NULL) {
207 if (dest->mont_data == NULL) {
208 dest->mont_data = BN_MONT_CTX_new();
209 if (dest->mont_data == NULL)
212 if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data))
215 /* src->generator == NULL */
216 BN_MONT_CTX_free(dest->mont_data);
217 dest->mont_data = NULL;
220 if (src->generator != NULL) {
221 if (dest->generator == NULL) {
222 dest->generator = EC_POINT_new(dest);
223 if (dest->generator == NULL)
226 if (!EC_POINT_copy(dest->generator, src->generator))
229 /* src->generator == NULL */
230 EC_POINT_clear_free(dest->generator);
231 dest->generator = NULL;
234 if ((src->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
235 if (!BN_copy(dest->order, src->order))
237 if (!BN_copy(dest->cofactor, src->cofactor))
241 dest->asn1_flag = src->asn1_flag;
242 dest->asn1_form = src->asn1_form;
245 OPENSSL_free(dest->seed);
246 if ((dest->seed = OPENSSL_malloc(src->seed_len)) == NULL) {
247 ECerr(EC_F_EC_GROUP_COPY, ERR_R_MALLOC_FAILURE);
250 if (!memcpy(dest->seed, src->seed, src->seed_len))
252 dest->seed_len = src->seed_len;
254 OPENSSL_free(dest->seed);
259 return dest->meth->group_copy(dest, src);
262 EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)
270 if ((t = ec_group_new_with_libctx(a->libctx, a->propq, a->meth)) == NULL)
272 if (!EC_GROUP_copy(t, a))
285 #ifndef OPENSSL_NO_DEPRECATED_3_0
286 const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group)
291 int EC_METHOD_get_field_type(const EC_METHOD *meth)
293 return meth->field_type;
297 static int ec_precompute_mont_data(EC_GROUP *);
300 * Try computing cofactor from the generator order (n) and field cardinality (q).
301 * This works for all curves of cryptographic interest.
303 * Hasse thm: q + 1 - 2*sqrt(q) <= n*h <= q + 1 + 2*sqrt(q)
304 * h_min = (q + 1 - 2*sqrt(q))/n
305 * h_max = (q + 1 + 2*sqrt(q))/n
306 * h_max - h_min = 4*sqrt(q)/n
307 * So if n > 4*sqrt(q) holds, there is only one possible value for h:
308 * h = \lfloor (h_min + h_max)/2 \rceil = \lfloor (q + 1)/n \rceil
310 * Otherwise, zero cofactor and return success.
312 static int ec_guess_cofactor(EC_GROUP *group) {
318 * If the cofactor is too large, we cannot guess it.
319 * The RHS of below is a strict overestimate of lg(4 * sqrt(q))
321 if (BN_num_bits(group->order) <= (BN_num_bits(group->field) + 1) / 2 + 3) {
323 BN_zero(group->cofactor);
328 if ((ctx = BN_CTX_new_ex(group->libctx)) == NULL)
332 if ((q = BN_CTX_get(ctx)) == NULL)
335 /* set q = 2**m for binary fields; q = p otherwise */
336 if (group->meth->field_type == NID_X9_62_characteristic_two_field) {
338 if (!BN_set_bit(q, BN_num_bits(group->field) - 1))
341 if (!BN_copy(q, group->field))
345 /* compute h = \lfloor (q + 1)/n \rceil = \lfloor (q + 1 + n/2)/n \rfloor */
346 if (!BN_rshift1(group->cofactor, group->order) /* n/2 */
347 || !BN_add(group->cofactor, group->cofactor, q) /* q + n/2 */
349 || !BN_add(group->cofactor, group->cofactor, BN_value_one())
350 /* (q + 1 + n/2)/n */
351 || !BN_div(group->cofactor, NULL, group->cofactor, group->order, ctx))
360 int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
361 const BIGNUM *order, const BIGNUM *cofactor)
363 if (generator == NULL) {
364 ECerr(EC_F_EC_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER);
368 /* require group->field >= 1 */
369 if (group->field == NULL || BN_is_zero(group->field)
370 || BN_is_negative(group->field)) {
371 ECerr(EC_F_EC_GROUP_SET_GENERATOR, EC_R_INVALID_FIELD);
376 * - require order >= 1
377 * - enforce upper bound due to Hasse thm: order can be no more than one bit
378 * longer than field cardinality
380 if (order == NULL || BN_is_zero(order) || BN_is_negative(order)
381 || BN_num_bits(order) > BN_num_bits(group->field) + 1) {
382 ECerr(EC_F_EC_GROUP_SET_GENERATOR, EC_R_INVALID_GROUP_ORDER);
387 * Unfortunately the cofactor is an optional field in many standards.
388 * Internally, the lib uses 0 cofactor as a marker for "unknown cofactor".
389 * So accept cofactor == NULL or cofactor >= 0.
391 if (cofactor != NULL && BN_is_negative(cofactor)) {
392 ECerr(EC_F_EC_GROUP_SET_GENERATOR, EC_R_UNKNOWN_COFACTOR);
396 if (group->generator == NULL) {
397 group->generator = EC_POINT_new(group);
398 if (group->generator == NULL)
401 if (!EC_POINT_copy(group->generator, generator))
404 if (!BN_copy(group->order, order))
407 /* Either take the provided positive cofactor, or try to compute it */
408 if (cofactor != NULL && !BN_is_zero(cofactor)) {
409 if (!BN_copy(group->cofactor, cofactor))
411 } else if (!ec_guess_cofactor(group)) {
412 BN_zero(group->cofactor);
417 * Some groups have an order with
418 * factors of two, which makes the Montgomery setup fail.
419 * |group->mont_data| will be NULL in this case.
421 if (BN_is_odd(group->order)) {
422 return ec_precompute_mont_data(group);
425 BN_MONT_CTX_free(group->mont_data);
426 group->mont_data = NULL;
430 const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
432 return group->generator;
435 BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group)
437 return group->mont_data;
440 int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
442 if (group->order == NULL)
444 if (!BN_copy(order, group->order))
447 return !BN_is_zero(order);
450 const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group)
455 int EC_GROUP_order_bits(const EC_GROUP *group)
457 return group->meth->group_order_bits(group);
460 int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
464 if (group->cofactor == NULL)
466 if (!BN_copy(cofactor, group->cofactor))
469 return !BN_is_zero(group->cofactor);
472 const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group)
474 return group->cofactor;
477 void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)
479 group->curve_name = nid;
482 int EC_GROUP_get_curve_name(const EC_GROUP *group)
484 return group->curve_name;
487 const BIGNUM *EC_GROUP_get0_field(const EC_GROUP *group)
492 int EC_GROUP_get_field_type(const EC_GROUP *group)
494 return group->meth->field_type;
497 void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
499 group->asn1_flag = flag;
502 int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
504 return group->asn1_flag;
507 void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
508 point_conversion_form_t form)
510 group->asn1_form = form;
513 point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP
516 return group->asn1_form;
519 size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
521 OPENSSL_free(group->seed);
528 if ((group->seed = OPENSSL_malloc(len)) == NULL) {
529 ECerr(EC_F_EC_GROUP_SET_SEED, ERR_R_MALLOC_FAILURE);
532 memcpy(group->seed, p, len);
533 group->seed_len = len;
538 unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
543 size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
545 return group->seed_len;
548 int EC_GROUP_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
549 const BIGNUM *b, BN_CTX *ctx)
551 if (group->meth->group_set_curve == 0) {
552 ECerr(EC_F_EC_GROUP_SET_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
555 return group->meth->group_set_curve(group, p, a, b, ctx);
558 int EC_GROUP_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b,
561 if (group->meth->group_get_curve == NULL) {
562 ECerr(EC_F_EC_GROUP_GET_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
565 return group->meth->group_get_curve(group, p, a, b, ctx);
568 #ifndef OPENSSL_NO_DEPRECATED_3_0
569 int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
570 const BIGNUM *b, BN_CTX *ctx)
572 return EC_GROUP_set_curve(group, p, a, b, ctx);
575 int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
576 BIGNUM *b, BN_CTX *ctx)
578 return EC_GROUP_get_curve(group, p, a, b, ctx);
581 # ifndef OPENSSL_NO_EC2M
582 int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
583 const BIGNUM *b, BN_CTX *ctx)
585 return EC_GROUP_set_curve(group, p, a, b, ctx);
588 int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
589 BIGNUM *b, BN_CTX *ctx)
591 return EC_GROUP_get_curve(group, p, a, b, ctx);
596 int EC_GROUP_get_degree(const EC_GROUP *group)
598 if (group->meth->group_get_degree == 0) {
599 ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
602 return group->meth->group_get_degree(group);
605 int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
607 if (group->meth->group_check_discriminant == 0) {
608 ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT,
609 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
612 return group->meth->group_check_discriminant(group, ctx);
615 int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
618 BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
620 BN_CTX *ctx_new = NULL;
623 /* compare the field types */
624 if (EC_GROUP_get_field_type(a) != EC_GROUP_get_field_type(b))
626 /* compare the curve name (if present in both) */
627 if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
628 EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
630 if (a->meth->flags & EC_FLAGS_CUSTOM_CURVE)
635 ctx_new = ctx = BN_CTX_new();
641 a1 = BN_CTX_get(ctx);
642 a2 = BN_CTX_get(ctx);
643 a3 = BN_CTX_get(ctx);
644 b1 = BN_CTX_get(ctx);
645 b2 = BN_CTX_get(ctx);
646 b3 = BN_CTX_get(ctx);
650 BN_CTX_free(ctx_new);
656 * XXX This approach assumes that the external representation of curves
657 * over the same field type is the same.
659 if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
660 !b->meth->group_get_curve(b, b1, b2, b3, ctx))
663 /* return 1 if the curve parameters are different */
664 if (r || BN_cmp(a1, b1) != 0 || BN_cmp(a2, b2) != 0 || BN_cmp(a3, b3) != 0)
667 /* XXX EC_POINT_cmp() assumes that the methods are equal */
668 /* return 1 if the generators are different */
669 if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
670 EC_GROUP_get0_generator(b), ctx) != 0)
674 const BIGNUM *ao, *bo, *ac, *bc;
675 /* compare the orders */
676 ao = EC_GROUP_get0_order(a);
677 bo = EC_GROUP_get0_order(b);
678 if (ao == NULL || bo == NULL) {
679 /* return an error if either order is NULL */
683 if (BN_cmp(ao, bo) != 0) {
684 /* return 1 if orders are different */
689 * It gets here if the curve parameters and generator matched.
690 * Now check the optional cofactors (if both are present).
692 ac = EC_GROUP_get0_cofactor(a);
693 bc = EC_GROUP_get0_cofactor(b);
694 /* Returns 1 (mismatch) if both cofactors are specified and different */
695 if (!BN_is_zero(ac) && !BN_is_zero(bc) && BN_cmp(ac, bc) != 0)
697 /* Returns 0 if the parameters matched */
702 BN_CTX_free(ctx_new);
707 /* functions for EC_POINT objects */
709 EC_POINT *EC_POINT_new(const EC_GROUP *group)
714 ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER);
717 if (group->meth->point_init == NULL) {
718 ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
722 ret = OPENSSL_zalloc(sizeof(*ret));
724 ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE);
728 ret->meth = group->meth;
729 ret->curve_name = group->curve_name;
731 if (!ret->meth->point_init(ret)) {
739 void EC_POINT_free(EC_POINT *point)
744 if (point->meth->point_finish != 0)
745 point->meth->point_finish(point);
749 void EC_POINT_clear_free(EC_POINT *point)
754 if (point->meth->point_clear_finish != 0)
755 point->meth->point_clear_finish(point);
756 else if (point->meth->point_finish != 0)
757 point->meth->point_finish(point);
758 OPENSSL_clear_free(point, sizeof(*point));
761 int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)
763 if (dest->meth->point_copy == 0) {
764 ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
767 if (dest->meth != src->meth
768 || (dest->curve_name != src->curve_name
769 && dest->curve_name != 0
770 && src->curve_name != 0)) {
771 ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS);
776 return dest->meth->point_copy(dest, src);
779 EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
787 t = EC_POINT_new(group);
790 r = EC_POINT_copy(t, a);
798 #ifndef OPENSSL_NO_DEPRECATED_3_0
799 const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
805 int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
807 if (group->meth->point_set_to_infinity == 0) {
808 ECerr(EC_F_EC_POINT_SET_TO_INFINITY,
809 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
812 if (group->meth != point->meth) {
813 ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
816 return group->meth->point_set_to_infinity(group, point);
819 #ifndef OPENSSL_NO_DEPRECATED_3_0
820 int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
821 EC_POINT *point, const BIGNUM *x,
822 const BIGNUM *y, const BIGNUM *z,
825 if (group->meth->field_type != NID_X9_62_prime_field) {
826 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
827 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
830 if (!ec_point_is_compat(point, group)) {
831 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
832 EC_R_INCOMPATIBLE_OBJECTS);
835 return ec_GFp_simple_set_Jprojective_coordinates_GFp(group, point, x, y, z, ctx);
838 int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
839 const EC_POINT *point, BIGNUM *x,
840 BIGNUM *y, BIGNUM *z,
843 if (group->meth->field_type != NID_X9_62_prime_field) {
844 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
845 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
848 if (!ec_point_is_compat(point, group)) {
849 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
850 EC_R_INCOMPATIBLE_OBJECTS);
853 return ec_GFp_simple_get_Jprojective_coordinates_GFp(group, point, x, y, z, ctx);
857 int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point,
858 const BIGNUM *x, const BIGNUM *y,
861 if (group->meth->point_set_affine_coordinates == NULL) {
862 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES,
863 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
866 if (!ec_point_is_compat(point, group)) {
867 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, EC_R_INCOMPATIBLE_OBJECTS);
870 if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx))
873 if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {
874 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, EC_R_POINT_IS_NOT_ON_CURVE);
880 #ifndef OPENSSL_NO_DEPRECATED_3_0
881 int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
882 EC_POINT *point, const BIGNUM *x,
883 const BIGNUM *y, BN_CTX *ctx)
885 return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
888 # ifndef OPENSSL_NO_EC2M
889 int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,
890 EC_POINT *point, const BIGNUM *x,
891 const BIGNUM *y, BN_CTX *ctx)
893 return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
898 int EC_POINT_get_affine_coordinates(const EC_GROUP *group,
899 const EC_POINT *point, BIGNUM *x, BIGNUM *y,
902 if (group->meth->point_get_affine_coordinates == NULL) {
903 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES,
904 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
907 if (!ec_point_is_compat(point, group)) {
908 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, EC_R_INCOMPATIBLE_OBJECTS);
911 if (EC_POINT_is_at_infinity(group, point)) {
912 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, EC_R_POINT_AT_INFINITY);
915 return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
918 #ifndef OPENSSL_NO_DEPRECATED_3_0
919 int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
920 const EC_POINT *point, BIGNUM *x,
921 BIGNUM *y, BN_CTX *ctx)
923 return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
926 # ifndef OPENSSL_NO_EC2M
927 int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,
928 const EC_POINT *point, BIGNUM *x,
929 BIGNUM *y, BN_CTX *ctx)
931 return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
936 int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
937 const EC_POINT *b, BN_CTX *ctx)
939 if (group->meth->add == 0) {
940 ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
943 if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)
944 || !ec_point_is_compat(b, group)) {
945 ECerr(EC_F_EC_POINT_ADD, EC_R_INCOMPATIBLE_OBJECTS);
948 return group->meth->add(group, r, a, b, ctx);
951 int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
954 if (group->meth->dbl == 0) {
955 ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
958 if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)) {
959 ECerr(EC_F_EC_POINT_DBL, EC_R_INCOMPATIBLE_OBJECTS);
962 return group->meth->dbl(group, r, a, ctx);
965 int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
967 if (group->meth->invert == 0) {
968 ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
971 if (!ec_point_is_compat(a, group)) {
972 ECerr(EC_F_EC_POINT_INVERT, EC_R_INCOMPATIBLE_OBJECTS);
975 return group->meth->invert(group, a, ctx);
978 int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
980 if (group->meth->is_at_infinity == 0) {
981 ECerr(EC_F_EC_POINT_IS_AT_INFINITY,
982 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
985 if (!ec_point_is_compat(point, group)) {
986 ECerr(EC_F_EC_POINT_IS_AT_INFINITY, 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 ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1006 if (!ec_point_is_compat(point, group)) {
1007 ECerr(EC_F_EC_POINT_IS_ON_CURVE, 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 ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1020 if (!ec_point_is_compat(a, group) || !ec_point_is_compat(b, group)) {
1021 ECerr(EC_F_EC_POINT_CMP, 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 ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1034 if (!ec_point_is_compat(point, group)) {
1035 ECerr(EC_F_EC_POINT_MAKE_AFFINE, 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 ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1050 for (i = 0; i < num; i++) {
1051 if (!ec_point_is_compat(points[i], group)) {
1052 ECerr(EC_F_EC_POINTS_MAKE_AFFINE, 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 ECerr(EC_F_EC_POINTS_MUL, 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 ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
1094 ctx = new_ctx = BN_CTX_secure_new();
1097 ECerr(EC_F_EC_POINTS_MUL, ERR_R_INTERNAL_ERROR);
1101 if (group->meth->mul != NULL)
1102 ret = group->meth->mul(group, r, scalar, num, points, scalars, ctx);
1105 ret = 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 ECerr(EC_F_EC_POINT_MUL, 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 ECerr(EC_F_EC_POINT_MUL, 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 = 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 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 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 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 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 ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p, BN_CTX *ctx)
1315 if (group->meth->blind_coordinates == NULL)
1316 return 1; /* ignore if not implemented */
1318 return group->meth->blind_coordinates(group, p, ctx);