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_ex(OPENSSL_CTX *libctx, const EC_METHOD *meth)
31 ECerr(EC_F_EC_GROUP_NEW_EX, EC_R_SLOT_FULL);
34 if (meth->group_init == 0) {
35 ECerr(EC_F_EC_GROUP_NEW_EX, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
39 ret = OPENSSL_zalloc(sizeof(*ret));
41 ECerr(EC_F_EC_GROUP_NEW_EX, ERR_R_MALLOC_FAILURE);
47 if ((ret->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
48 ret->order = BN_new();
49 if (ret->order == NULL)
51 ret->cofactor = BN_new();
52 if (ret->cofactor == NULL)
55 ret->asn1_flag = OPENSSL_EC_NAMED_CURVE;
56 ret->asn1_form = POINT_CONVERSION_UNCOMPRESSED;
57 if (!meth->group_init(ret))
63 BN_free(ret->cofactor);
68 #ifndef OPENSSL_NO_DEPRECATED_3_0
70 EC_GROUP *EC_GROUP_new(const EC_METHOD *meth)
72 return ec_group_new_ex(NULL, meth);
77 void EC_pre_comp_free(EC_GROUP *group)
79 switch (group->pre_comp_type) {
83 #ifdef ECP_NISTZ256_ASM
84 EC_nistz256_pre_comp_free(group->pre_comp.nistz256);
87 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
89 EC_nistp224_pre_comp_free(group->pre_comp.nistp224);
92 EC_nistp256_pre_comp_free(group->pre_comp.nistp256);
95 EC_nistp521_pre_comp_free(group->pre_comp.nistp521);
104 EC_ec_pre_comp_free(group->pre_comp.ec);
107 group->pre_comp.ec = NULL;
110 void EC_GROUP_free(EC_GROUP *group)
115 if (group->meth->group_finish != 0)
116 group->meth->group_finish(group);
118 EC_pre_comp_free(group);
119 BN_MONT_CTX_free(group->mont_data);
120 EC_POINT_free(group->generator);
121 BN_free(group->order);
122 BN_free(group->cofactor);
123 OPENSSL_free(group->seed);
127 #ifndef OPENSSL_NO_DEPRECATED_3_0
128 void EC_GROUP_clear_free(EC_GROUP *group)
133 if (group->meth->group_clear_finish != 0)
134 group->meth->group_clear_finish(group);
135 else if (group->meth->group_finish != 0)
136 group->meth->group_finish(group);
138 EC_pre_comp_free(group);
139 BN_MONT_CTX_free(group->mont_data);
140 EC_POINT_clear_free(group->generator);
141 BN_clear_free(group->order);
142 BN_clear_free(group->cofactor);
143 OPENSSL_clear_free(group->seed, group->seed_len);
144 OPENSSL_clear_free(group, sizeof(*group));
148 int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)
150 if (dest->meth->group_copy == 0) {
151 ECerr(EC_F_EC_GROUP_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
154 if (dest->meth != src->meth) {
155 ECerr(EC_F_EC_GROUP_COPY, EC_R_INCOMPATIBLE_OBJECTS);
161 dest->libctx = src->libctx;
162 dest->curve_name = src->curve_name;
164 /* Copy precomputed */
165 dest->pre_comp_type = src->pre_comp_type;
166 switch (src->pre_comp_type) {
168 dest->pre_comp.ec = NULL;
171 #ifdef ECP_NISTZ256_ASM
172 dest->pre_comp.nistz256 = EC_nistz256_pre_comp_dup(src->pre_comp.nistz256);
175 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
177 dest->pre_comp.nistp224 = EC_nistp224_pre_comp_dup(src->pre_comp.nistp224);
180 dest->pre_comp.nistp256 = EC_nistp256_pre_comp_dup(src->pre_comp.nistp256);
183 dest->pre_comp.nistp521 = EC_nistp521_pre_comp_dup(src->pre_comp.nistp521);
192 dest->pre_comp.ec = EC_ec_pre_comp_dup(src->pre_comp.ec);
196 if (src->mont_data != NULL) {
197 if (dest->mont_data == NULL) {
198 dest->mont_data = BN_MONT_CTX_new();
199 if (dest->mont_data == NULL)
202 if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data))
205 /* src->generator == NULL */
206 BN_MONT_CTX_free(dest->mont_data);
207 dest->mont_data = NULL;
210 if (src->generator != NULL) {
211 if (dest->generator == NULL) {
212 dest->generator = EC_POINT_new(dest);
213 if (dest->generator == NULL)
216 if (!EC_POINT_copy(dest->generator, src->generator))
219 /* src->generator == NULL */
220 EC_POINT_clear_free(dest->generator);
221 dest->generator = NULL;
224 if ((src->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
225 if (!BN_copy(dest->order, src->order))
227 if (!BN_copy(dest->cofactor, src->cofactor))
231 dest->asn1_flag = src->asn1_flag;
232 dest->asn1_form = src->asn1_form;
235 OPENSSL_free(dest->seed);
236 if ((dest->seed = OPENSSL_malloc(src->seed_len)) == NULL) {
237 ECerr(EC_F_EC_GROUP_COPY, ERR_R_MALLOC_FAILURE);
240 if (!memcpy(dest->seed, src->seed, src->seed_len))
242 dest->seed_len = src->seed_len;
244 OPENSSL_free(dest->seed);
249 return dest->meth->group_copy(dest, src);
252 EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)
260 if ((t = ec_group_new_ex(a->libctx, a->meth)) == NULL)
262 if (!EC_GROUP_copy(t, a))
275 #ifndef OPENSSL_NO_DEPRECATED_3_0
276 const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group)
281 int EC_METHOD_get_field_type(const EC_METHOD *meth)
283 return meth->field_type;
287 static int ec_precompute_mont_data(EC_GROUP *);
290 * Try computing cofactor from the generator order (n) and field cardinality (q).
291 * This works for all curves of cryptographic interest.
293 * Hasse thm: q + 1 - 2*sqrt(q) <= n*h <= q + 1 + 2*sqrt(q)
294 * h_min = (q + 1 - 2*sqrt(q))/n
295 * h_max = (q + 1 + 2*sqrt(q))/n
296 * h_max - h_min = 4*sqrt(q)/n
297 * So if n > 4*sqrt(q) holds, there is only one possible value for h:
298 * h = \lfloor (h_min + h_max)/2 \rceil = \lfloor (q + 1)/n \rceil
300 * Otherwise, zero cofactor and return success.
302 static int ec_guess_cofactor(EC_GROUP *group) {
308 * If the cofactor is too large, we cannot guess it.
309 * The RHS of below is a strict overestimate of lg(4 * sqrt(q))
311 if (BN_num_bits(group->order) <= (BN_num_bits(group->field) + 1) / 2 + 3) {
313 BN_zero(group->cofactor);
318 if ((ctx = BN_CTX_new_ex(group->libctx)) == NULL)
322 if ((q = BN_CTX_get(ctx)) == NULL)
325 /* set q = 2**m for binary fields; q = p otherwise */
326 if (group->meth->field_type == NID_X9_62_characteristic_two_field) {
328 if (!BN_set_bit(q, BN_num_bits(group->field) - 1))
331 if (!BN_copy(q, group->field))
335 /* compute h = \lfloor (q + 1)/n \rceil = \lfloor (q + 1 + n/2)/n \rfloor */
336 if (!BN_rshift1(group->cofactor, group->order) /* n/2 */
337 || !BN_add(group->cofactor, group->cofactor, q) /* q + n/2 */
339 || !BN_add(group->cofactor, group->cofactor, BN_value_one())
340 /* (q + 1 + n/2)/n */
341 || !BN_div(group->cofactor, NULL, group->cofactor, group->order, ctx))
350 int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
351 const BIGNUM *order, const BIGNUM *cofactor)
353 if (generator == NULL) {
354 ECerr(EC_F_EC_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER);
358 /* require group->field >= 1 */
359 if (group->field == NULL || BN_is_zero(group->field)
360 || BN_is_negative(group->field)) {
361 ECerr(EC_F_EC_GROUP_SET_GENERATOR, EC_R_INVALID_FIELD);
366 * - require order >= 1
367 * - enforce upper bound due to Hasse thm: order can be no more than one bit
368 * longer than field cardinality
370 if (order == NULL || BN_is_zero(order) || BN_is_negative(order)
371 || BN_num_bits(order) > BN_num_bits(group->field) + 1) {
372 ECerr(EC_F_EC_GROUP_SET_GENERATOR, EC_R_INVALID_GROUP_ORDER);
377 * Unfortunately the cofactor is an optional field in many standards.
378 * Internally, the lib uses 0 cofactor as a marker for "unknown cofactor".
379 * So accept cofactor == NULL or cofactor >= 0.
381 if (cofactor != NULL && BN_is_negative(cofactor)) {
382 ECerr(EC_F_EC_GROUP_SET_GENERATOR, EC_R_UNKNOWN_COFACTOR);
386 if (group->generator == NULL) {
387 group->generator = EC_POINT_new(group);
388 if (group->generator == NULL)
391 if (!EC_POINT_copy(group->generator, generator))
394 if (!BN_copy(group->order, order))
397 /* Either take the provided positive cofactor, or try to compute it */
398 if (cofactor != NULL && !BN_is_zero(cofactor)) {
399 if (!BN_copy(group->cofactor, cofactor))
401 } else if (!ec_guess_cofactor(group)) {
402 BN_zero(group->cofactor);
407 * Some groups have an order with
408 * factors of two, which makes the Montgomery setup fail.
409 * |group->mont_data| will be NULL in this case.
411 if (BN_is_odd(group->order)) {
412 return ec_precompute_mont_data(group);
415 BN_MONT_CTX_free(group->mont_data);
416 group->mont_data = NULL;
420 const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
422 return group->generator;
425 BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group)
427 return group->mont_data;
430 int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
432 if (group->order == NULL)
434 if (!BN_copy(order, group->order))
437 return !BN_is_zero(order);
440 const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group)
445 int EC_GROUP_order_bits(const EC_GROUP *group)
447 return group->meth->group_order_bits(group);
450 int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
454 if (group->cofactor == NULL)
456 if (!BN_copy(cofactor, group->cofactor))
459 return !BN_is_zero(group->cofactor);
462 const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group)
464 return group->cofactor;
467 void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)
469 group->curve_name = nid;
472 int EC_GROUP_get_curve_name(const EC_GROUP *group)
474 return group->curve_name;
477 const BIGNUM *EC_GROUP_get0_field(const EC_GROUP *group)
482 int EC_GROUP_get_field_type(const EC_GROUP *group)
484 return group->meth->field_type;
487 void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
489 group->asn1_flag = flag;
492 int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
494 return group->asn1_flag;
497 void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
498 point_conversion_form_t form)
500 group->asn1_form = form;
503 point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP
506 return group->asn1_form;
509 size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
511 OPENSSL_free(group->seed);
518 if ((group->seed = OPENSSL_malloc(len)) == NULL) {
519 ECerr(EC_F_EC_GROUP_SET_SEED, ERR_R_MALLOC_FAILURE);
522 memcpy(group->seed, p, len);
523 group->seed_len = len;
528 unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
533 size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
535 return group->seed_len;
538 int EC_GROUP_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
539 const BIGNUM *b, BN_CTX *ctx)
541 if (group->meth->group_set_curve == 0) {
542 ECerr(EC_F_EC_GROUP_SET_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
545 return group->meth->group_set_curve(group, p, a, b, ctx);
548 int EC_GROUP_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b,
551 if (group->meth->group_get_curve == NULL) {
552 ECerr(EC_F_EC_GROUP_GET_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
555 return group->meth->group_get_curve(group, p, a, b, ctx);
558 #ifndef OPENSSL_NO_DEPRECATED_3_0
559 int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
560 const BIGNUM *b, BN_CTX *ctx)
562 return EC_GROUP_set_curve(group, p, a, b, ctx);
565 int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
566 BIGNUM *b, BN_CTX *ctx)
568 return EC_GROUP_get_curve(group, p, a, b, ctx);
571 # ifndef OPENSSL_NO_EC2M
572 int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
573 const BIGNUM *b, BN_CTX *ctx)
575 return EC_GROUP_set_curve(group, p, a, b, ctx);
578 int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
579 BIGNUM *b, BN_CTX *ctx)
581 return EC_GROUP_get_curve(group, p, a, b, ctx);
586 int EC_GROUP_get_degree(const EC_GROUP *group)
588 if (group->meth->group_get_degree == 0) {
589 ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
592 return group->meth->group_get_degree(group);
595 int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
597 if (group->meth->group_check_discriminant == 0) {
598 ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT,
599 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
602 return group->meth->group_check_discriminant(group, ctx);
605 int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
608 BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
610 BN_CTX *ctx_new = NULL;
613 /* compare the field types */
614 if (EC_GROUP_get_field_type(a) != EC_GROUP_get_field_type(b))
616 /* compare the curve name (if present in both) */
617 if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
618 EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
620 if (a->meth->flags & EC_FLAGS_CUSTOM_CURVE)
625 ctx_new = ctx = BN_CTX_new();
631 a1 = BN_CTX_get(ctx);
632 a2 = BN_CTX_get(ctx);
633 a3 = BN_CTX_get(ctx);
634 b1 = BN_CTX_get(ctx);
635 b2 = BN_CTX_get(ctx);
636 b3 = BN_CTX_get(ctx);
640 BN_CTX_free(ctx_new);
646 * XXX This approach assumes that the external representation of curves
647 * over the same field type is the same.
649 if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
650 !b->meth->group_get_curve(b, b1, b2, b3, ctx))
653 /* return 1 if the curve parameters are different */
654 if (r || BN_cmp(a1, b1) != 0 || BN_cmp(a2, b2) != 0 || BN_cmp(a3, b3) != 0)
657 /* XXX EC_POINT_cmp() assumes that the methods are equal */
658 /* return 1 if the generators are different */
659 if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
660 EC_GROUP_get0_generator(b), ctx) != 0)
664 const BIGNUM *ao, *bo, *ac, *bc;
665 /* compare the orders */
666 ao = EC_GROUP_get0_order(a);
667 bo = EC_GROUP_get0_order(b);
668 if (ao == NULL || bo == NULL) {
669 /* return an error if either order is NULL */
673 if (BN_cmp(ao, bo) != 0) {
674 /* return 1 if orders are different */
679 * It gets here if the curve parameters and generator matched.
680 * Now check the optional cofactors (if both are present).
682 ac = EC_GROUP_get0_cofactor(a);
683 bc = EC_GROUP_get0_cofactor(b);
684 /* Returns 1 (mismatch) if both cofactors are specified and different */
685 if (!BN_is_zero(ac) && !BN_is_zero(bc) && BN_cmp(ac, bc) != 0)
687 /* Returns 0 if the parameters matched */
692 BN_CTX_free(ctx_new);
697 /* functions for EC_POINT objects */
699 EC_POINT *EC_POINT_new(const EC_GROUP *group)
704 ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER);
707 if (group->meth->point_init == NULL) {
708 ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
712 ret = OPENSSL_zalloc(sizeof(*ret));
714 ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE);
718 ret->meth = group->meth;
719 ret->curve_name = group->curve_name;
721 if (!ret->meth->point_init(ret)) {
729 void EC_POINT_free(EC_POINT *point)
734 if (point->meth->point_finish != 0)
735 point->meth->point_finish(point);
739 void EC_POINT_clear_free(EC_POINT *point)
744 if (point->meth->point_clear_finish != 0)
745 point->meth->point_clear_finish(point);
746 else if (point->meth->point_finish != 0)
747 point->meth->point_finish(point);
748 OPENSSL_clear_free(point, sizeof(*point));
751 int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)
753 if (dest->meth->point_copy == 0) {
754 ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
757 if (dest->meth != src->meth
758 || (dest->curve_name != src->curve_name
759 && dest->curve_name != 0
760 && src->curve_name != 0)) {
761 ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS);
766 return dest->meth->point_copy(dest, src);
769 EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
777 t = EC_POINT_new(group);
780 r = EC_POINT_copy(t, a);
788 #ifndef OPENSSL_NO_DEPRECATED_3_0
789 const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
795 int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
797 if (group->meth->point_set_to_infinity == 0) {
798 ECerr(EC_F_EC_POINT_SET_TO_INFINITY,
799 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
802 if (group->meth != point->meth) {
803 ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
806 return group->meth->point_set_to_infinity(group, point);
809 #ifndef OPENSSL_NO_DEPRECATED_3_0
810 int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
811 EC_POINT *point, const BIGNUM *x,
812 const BIGNUM *y, const BIGNUM *z,
815 if (group->meth->field_type != NID_X9_62_prime_field) {
816 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
817 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
820 if (!ec_point_is_compat(point, group)) {
821 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
822 EC_R_INCOMPATIBLE_OBJECTS);
825 return ec_GFp_simple_set_Jprojective_coordinates_GFp(group, point, x, y, z, ctx);
828 int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
829 const EC_POINT *point, BIGNUM *x,
830 BIGNUM *y, BIGNUM *z,
833 if (group->meth->field_type != NID_X9_62_prime_field) {
834 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
835 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
838 if (!ec_point_is_compat(point, group)) {
839 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
840 EC_R_INCOMPATIBLE_OBJECTS);
843 return ec_GFp_simple_get_Jprojective_coordinates_GFp(group, point, x, y, z, ctx);
847 int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point,
848 const BIGNUM *x, const BIGNUM *y,
851 if (group->meth->point_set_affine_coordinates == NULL) {
852 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES,
853 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
856 if (!ec_point_is_compat(point, group)) {
857 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, EC_R_INCOMPATIBLE_OBJECTS);
860 if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx))
863 if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {
864 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, EC_R_POINT_IS_NOT_ON_CURVE);
870 #ifndef OPENSSL_NO_DEPRECATED_3_0
871 int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
872 EC_POINT *point, const BIGNUM *x,
873 const BIGNUM *y, BN_CTX *ctx)
875 return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
878 # ifndef OPENSSL_NO_EC2M
879 int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,
880 EC_POINT *point, const BIGNUM *x,
881 const BIGNUM *y, BN_CTX *ctx)
883 return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
888 int EC_POINT_get_affine_coordinates(const EC_GROUP *group,
889 const EC_POINT *point, BIGNUM *x, BIGNUM *y,
892 if (group->meth->point_get_affine_coordinates == NULL) {
893 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES,
894 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
897 if (!ec_point_is_compat(point, group)) {
898 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, EC_R_INCOMPATIBLE_OBJECTS);
901 if (EC_POINT_is_at_infinity(group, point)) {
902 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, EC_R_POINT_AT_INFINITY);
905 return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
908 #ifndef OPENSSL_NO_DEPRECATED_3_0
909 int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
910 const EC_POINT *point, BIGNUM *x,
911 BIGNUM *y, BN_CTX *ctx)
913 return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
916 # ifndef OPENSSL_NO_EC2M
917 int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,
918 const EC_POINT *point, BIGNUM *x,
919 BIGNUM *y, BN_CTX *ctx)
921 return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
926 int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
927 const EC_POINT *b, BN_CTX *ctx)
929 if (group->meth->add == 0) {
930 ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
933 if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)
934 || !ec_point_is_compat(b, group)) {
935 ECerr(EC_F_EC_POINT_ADD, EC_R_INCOMPATIBLE_OBJECTS);
938 return group->meth->add(group, r, a, b, ctx);
941 int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
944 if (group->meth->dbl == 0) {
945 ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
948 if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)) {
949 ECerr(EC_F_EC_POINT_DBL, EC_R_INCOMPATIBLE_OBJECTS);
952 return group->meth->dbl(group, r, a, ctx);
955 int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
957 if (group->meth->invert == 0) {
958 ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
961 if (!ec_point_is_compat(a, group)) {
962 ECerr(EC_F_EC_POINT_INVERT, EC_R_INCOMPATIBLE_OBJECTS);
965 return group->meth->invert(group, a, ctx);
968 int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
970 if (group->meth->is_at_infinity == 0) {
971 ECerr(EC_F_EC_POINT_IS_AT_INFINITY,
972 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
975 if (!ec_point_is_compat(point, group)) {
976 ECerr(EC_F_EC_POINT_IS_AT_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
979 return group->meth->is_at_infinity(group, point);
983 * Check whether an EC_POINT is on the curve or not. Note that the return
984 * value for this function should NOT be treated as a boolean. Return values:
985 * 1: The point is on the curve
986 * 0: The point is not on the curve
987 * -1: An error occurred
989 int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
992 if (group->meth->is_on_curve == 0) {
993 ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
996 if (!ec_point_is_compat(point, group)) {
997 ECerr(EC_F_EC_POINT_IS_ON_CURVE, EC_R_INCOMPATIBLE_OBJECTS);
1000 return group->meth->is_on_curve(group, point, ctx);
1003 int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
1006 if (group->meth->point_cmp == 0) {
1007 ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1010 if (!ec_point_is_compat(a, group) || !ec_point_is_compat(b, group)) {
1011 ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS);
1014 return group->meth->point_cmp(group, a, b, ctx);
1017 #ifndef OPENSSL_NO_DEPRECATED_3_0
1018 int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
1020 if (group->meth->make_affine == 0) {
1021 ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1024 if (!ec_point_is_compat(point, group)) {
1025 ECerr(EC_F_EC_POINT_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
1028 return group->meth->make_affine(group, point, ctx);
1031 int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
1032 EC_POINT *points[], BN_CTX *ctx)
1036 if (group->meth->points_make_affine == 0) {
1037 ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1040 for (i = 0; i < num; i++) {
1041 if (!ec_point_is_compat(points[i], group)) {
1042 ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
1046 return group->meth->points_make_affine(group, num, points, ctx);
1051 * Functions for point multiplication. If group->meth->mul is 0, we use the
1052 * wNAF-based implementations in ec_mult.c; otherwise we dispatch through
1056 #ifndef OPENSSL_NO_DEPRECATED_3_0
1057 int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
1058 size_t num, const EC_POINT *points[],
1059 const BIGNUM *scalars[], BN_CTX *ctx)
1064 BN_CTX *new_ctx = NULL;
1067 if (!ec_point_is_compat(r, group)) {
1068 ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
1072 if (scalar == NULL && num == 0)
1073 return EC_POINT_set_to_infinity(group, r);
1075 for (i = 0; i < num; i++) {
1076 if (!ec_point_is_compat(points[i], group)) {
1077 ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
1084 ctx = new_ctx = BN_CTX_secure_new();
1087 ECerr(EC_F_EC_POINTS_MUL, ERR_R_INTERNAL_ERROR);
1091 if (group->meth->mul != NULL)
1092 ret = group->meth->mul(group, r, scalar, num, points, scalars, ctx);
1095 ret = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
1098 BN_CTX_free(new_ctx);
1104 int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
1105 const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
1110 BN_CTX *new_ctx = NULL;
1113 if (!ec_point_is_compat(r, group)
1114 || (point != NULL && !ec_point_is_compat(point, group))) {
1115 ECerr(EC_F_EC_POINT_MUL, EC_R_INCOMPATIBLE_OBJECTS);
1119 if (g_scalar == NULL && p_scalar == NULL)
1120 return EC_POINT_set_to_infinity(group, r);
1124 ctx = new_ctx = BN_CTX_secure_new();
1127 ECerr(EC_F_EC_POINT_MUL, ERR_R_INTERNAL_ERROR);
1131 num = (point != NULL && p_scalar != NULL) ? 1 : 0;
1132 if (group->meth->mul != NULL)
1133 ret = group->meth->mul(group, r, g_scalar, num, &point, &p_scalar, ctx);
1136 ret = ec_wNAF_mul(group, r, g_scalar, num, &point, &p_scalar, ctx);
1139 BN_CTX_free(new_ctx);
1144 #ifndef OPENSSL_NO_DEPRECATED_3_0
1145 int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
1147 if (group->meth->mul == 0)
1149 return ec_wNAF_precompute_mult(group, ctx);
1151 if (group->meth->precompute_mult != 0)
1152 return group->meth->precompute_mult(group, ctx);
1154 return 1; /* nothing to do, so report success */
1157 int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
1159 if (group->meth->mul == 0)
1161 return ec_wNAF_have_precompute_mult(group);
1163 if (group->meth->have_precompute_mult != 0)
1164 return group->meth->have_precompute_mult(group);
1166 return 0; /* cannot tell whether precomputation has
1172 * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
1173 * returns one on success. On error it returns zero.
1175 static int ec_precompute_mont_data(EC_GROUP *group)
1177 BN_CTX *ctx = BN_CTX_new_ex(group->libctx);
1180 BN_MONT_CTX_free(group->mont_data);
1181 group->mont_data = NULL;
1186 group->mont_data = BN_MONT_CTX_new();
1187 if (group->mont_data == NULL)
1190 if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) {
1191 BN_MONT_CTX_free(group->mont_data);
1192 group->mont_data = NULL;
1205 int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg)
1207 return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
1210 void *EC_KEY_get_ex_data(const EC_KEY *key, int idx)
1212 return CRYPTO_get_ex_data(&key->ex_data, idx);
1216 int ec_group_simple_order_bits(const EC_GROUP *group)
1218 if (group->order == NULL)
1220 return BN_num_bits(group->order);
1223 static int ec_field_inverse_mod_ord(const EC_GROUP *group, BIGNUM *r,
1224 const BIGNUM *x, BN_CTX *ctx)
1229 BN_CTX *new_ctx = NULL;
1232 if (group->mont_data == NULL)
1237 ctx = new_ctx = BN_CTX_secure_new();
1243 if ((e = BN_CTX_get(ctx)) == NULL)
1247 * We want inverse in constant time, therefore we utilize the fact
1248 * order must be prime and use Fermats Little Theorem instead.
1250 if (!BN_set_word(e, 2))
1252 if (!BN_sub(e, group->order, e))
1255 * Exponent e is public.
1256 * No need for scatter-gather or BN_FLG_CONSTTIME.
1258 if (!BN_mod_exp_mont(r, x, e, group->order, ctx, group->mont_data))
1266 BN_CTX_free(new_ctx);
1272 * Default behavior, if group->meth->field_inverse_mod_ord is NULL:
1273 * - When group->order is even, this function returns an error.
1274 * - When group->order is otherwise composite, the correctness
1275 * of the output is not guaranteed.
1276 * - When x is outside the range [1, group->order), the correctness
1277 * of the output is not guaranteed.
1278 * - Otherwise, this function returns the multiplicative inverse in the
1279 * range [1, group->order).
1281 * EC_METHODs must implement their own field_inverse_mod_ord for
1282 * other functionality.
1284 int ec_group_do_inverse_ord(const EC_GROUP *group, BIGNUM *res,
1285 const BIGNUM *x, BN_CTX *ctx)
1287 if (group->meth->field_inverse_mod_ord != NULL)
1288 return group->meth->field_inverse_mod_ord(group, res, x, ctx);
1290 return ec_field_inverse_mod_ord(group, res, x, ctx);
1294 * Coordinate blinding for EC_POINT.
1296 * The underlying EC_METHOD can optionally implement this function:
1297 * underlying implementations should return 0 on errors, or 1 on
1300 * This wrapper returns 1 in case the underlying EC_METHOD does not
1301 * support coordinate blinding.
1303 int ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p, BN_CTX *ctx)
1305 if (group->meth->blind_coordinates == NULL)
1306 return 1; /* ignore if not implemented */
1308 return group->meth->blind_coordinates(group, p, ctx);