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 OpenSSL license (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
13 #include <openssl/err.h>
14 #include <openssl/opensslv.h>
18 /* functions for EC_GROUP objects */
20 EC_GROUP *EC_GROUP_new(const EC_METHOD *meth)
25 ECerr(EC_F_EC_GROUP_NEW, EC_R_SLOT_FULL);
28 if (meth->group_init == 0) {
29 ECerr(EC_F_EC_GROUP_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
33 ret = OPENSSL_zalloc(sizeof(*ret));
35 ECerr(EC_F_EC_GROUP_NEW, ERR_R_MALLOC_FAILURE);
40 if ((ret->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
41 ret->order = BN_new();
42 if (ret->order == NULL)
44 ret->cofactor = BN_new();
45 if (ret->cofactor == NULL)
48 ret->asn1_flag = OPENSSL_EC_NAMED_CURVE;
49 ret->asn1_form = POINT_CONVERSION_UNCOMPRESSED;
50 if (!meth->group_init(ret))
56 BN_free(ret->cofactor);
61 void EC_pre_comp_free(EC_GROUP *group)
63 switch (group->pre_comp_type) {
67 #ifdef ECP_NISTZ256_ASM
68 EC_nistz256_pre_comp_free(group->pre_comp.nistz256);
71 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
73 EC_nistp224_pre_comp_free(group->pre_comp.nistp224);
76 EC_nistp256_pre_comp_free(group->pre_comp.nistp256);
79 EC_nistp521_pre_comp_free(group->pre_comp.nistp521);
88 EC_ec_pre_comp_free(group->pre_comp.ec);
91 group->pre_comp.ec = NULL;
94 void EC_GROUP_free(EC_GROUP *group)
99 if (group->meth->group_finish != 0)
100 group->meth->group_finish(group);
102 EC_pre_comp_free(group);
103 BN_MONT_CTX_free(group->mont_data);
104 EC_POINT_free(group->generator);
105 BN_free(group->order);
106 BN_free(group->cofactor);
107 OPENSSL_free(group->seed);
111 void EC_GROUP_clear_free(EC_GROUP *group)
116 if (group->meth->group_clear_finish != 0)
117 group->meth->group_clear_finish(group);
118 else if (group->meth->group_finish != 0)
119 group->meth->group_finish(group);
121 EC_pre_comp_free(group);
122 BN_MONT_CTX_free(group->mont_data);
123 EC_POINT_clear_free(group->generator);
124 BN_clear_free(group->order);
125 BN_clear_free(group->cofactor);
126 OPENSSL_clear_free(group->seed, group->seed_len);
127 OPENSSL_clear_free(group, sizeof(*group));
130 int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)
132 if (dest->meth->group_copy == 0) {
133 ECerr(EC_F_EC_GROUP_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
136 if (dest->meth != src->meth) {
137 ECerr(EC_F_EC_GROUP_COPY, EC_R_INCOMPATIBLE_OBJECTS);
143 dest->curve_name = src->curve_name;
145 /* Copy precomputed */
146 dest->pre_comp_type = src->pre_comp_type;
147 switch (src->pre_comp_type) {
149 dest->pre_comp.ec = NULL;
152 #ifdef ECP_NISTZ256_ASM
153 dest->pre_comp.nistz256 = EC_nistz256_pre_comp_dup(src->pre_comp.nistz256);
156 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
158 dest->pre_comp.nistp224 = EC_nistp224_pre_comp_dup(src->pre_comp.nistp224);
161 dest->pre_comp.nistp256 = EC_nistp256_pre_comp_dup(src->pre_comp.nistp256);
164 dest->pre_comp.nistp521 = EC_nistp521_pre_comp_dup(src->pre_comp.nistp521);
173 dest->pre_comp.ec = EC_ec_pre_comp_dup(src->pre_comp.ec);
177 if (src->mont_data != NULL) {
178 if (dest->mont_data == NULL) {
179 dest->mont_data = BN_MONT_CTX_new();
180 if (dest->mont_data == NULL)
183 if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data))
186 /* src->generator == NULL */
187 BN_MONT_CTX_free(dest->mont_data);
188 dest->mont_data = NULL;
191 if (src->generator != NULL) {
192 if (dest->generator == NULL) {
193 dest->generator = EC_POINT_new(dest);
194 if (dest->generator == NULL)
197 if (!EC_POINT_copy(dest->generator, src->generator))
200 /* src->generator == NULL */
201 EC_POINT_clear_free(dest->generator);
202 dest->generator = NULL;
205 if ((src->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
206 if (!BN_copy(dest->order, src->order))
208 if (!BN_copy(dest->cofactor, src->cofactor))
212 dest->asn1_flag = src->asn1_flag;
213 dest->asn1_form = src->asn1_form;
214 dest->decoded_from_explicit_params = src->decoded_from_explicit_params;
217 OPENSSL_free(dest->seed);
218 if ((dest->seed = OPENSSL_malloc(src->seed_len)) == NULL) {
219 ECerr(EC_F_EC_GROUP_COPY, ERR_R_MALLOC_FAILURE);
222 if (!memcpy(dest->seed, src->seed, src->seed_len))
224 dest->seed_len = src->seed_len;
226 OPENSSL_free(dest->seed);
231 return dest->meth->group_copy(dest, src);
234 EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)
242 if ((t = EC_GROUP_new(a->meth)) == NULL)
244 if (!EC_GROUP_copy(t, a))
257 const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group)
262 int EC_METHOD_get_field_type(const EC_METHOD *meth)
264 return meth->field_type;
267 static int ec_precompute_mont_data(EC_GROUP *);
270 * Try computing cofactor from the generator order (n) and field cardinality (q).
271 * This works for all curves of cryptographic interest.
273 * Hasse thm: q + 1 - 2*sqrt(q) <= n*h <= q + 1 + 2*sqrt(q)
274 * h_min = (q + 1 - 2*sqrt(q))/n
275 * h_max = (q + 1 + 2*sqrt(q))/n
276 * h_max - h_min = 4*sqrt(q)/n
277 * So if n > 4*sqrt(q) holds, there is only one possible value for h:
278 * h = \lfloor (h_min + h_max)/2 \rceil = \lfloor (q + 1)/n \rceil
280 * Otherwise, zero cofactor and return success.
282 static int ec_guess_cofactor(EC_GROUP *group) {
288 * If the cofactor is too large, we cannot guess it.
289 * The RHS of below is a strict overestimate of lg(4 * sqrt(q))
291 if (BN_num_bits(group->order) <= (BN_num_bits(group->field) + 1) / 2 + 3) {
293 BN_zero(group->cofactor);
298 if ((ctx = BN_CTX_new()) == NULL)
302 if ((q = BN_CTX_get(ctx)) == NULL)
305 /* set q = 2**m for binary fields; q = p otherwise */
306 if (group->meth->field_type == NID_X9_62_characteristic_two_field) {
308 if (!BN_set_bit(q, BN_num_bits(group->field) - 1))
311 if (!BN_copy(q, group->field))
315 /* compute h = \lfloor (q + 1)/n \rceil = \lfloor (q + 1 + n/2)/n \rfloor */
316 if (!BN_rshift1(group->cofactor, group->order) /* n/2 */
317 || !BN_add(group->cofactor, group->cofactor, q) /* q + n/2 */
319 || !BN_add(group->cofactor, group->cofactor, BN_value_one())
320 /* (q + 1 + n/2)/n */
321 || !BN_div(group->cofactor, NULL, group->cofactor, group->order, ctx))
330 int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
331 const BIGNUM *order, const BIGNUM *cofactor)
333 if (generator == NULL) {
334 ECerr(EC_F_EC_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER);
338 /* require group->field >= 1 */
339 if (group->field == NULL || BN_is_zero(group->field)
340 || BN_is_negative(group->field)) {
341 ECerr(EC_F_EC_GROUP_SET_GENERATOR, EC_R_INVALID_FIELD);
346 * - require order >= 1
347 * - enforce upper bound due to Hasse thm: order can be no more than one bit
348 * longer than field cardinality
350 if (order == NULL || BN_is_zero(order) || BN_is_negative(order)
351 || BN_num_bits(order) > BN_num_bits(group->field) + 1) {
352 ECerr(EC_F_EC_GROUP_SET_GENERATOR, EC_R_INVALID_GROUP_ORDER);
357 * Unfortunately the cofactor is an optional field in many standards.
358 * Internally, the lib uses 0 cofactor as a marker for "unknown cofactor".
359 * So accept cofactor == NULL or cofactor >= 0.
361 if (cofactor != NULL && BN_is_negative(cofactor)) {
362 ECerr(EC_F_EC_GROUP_SET_GENERATOR, EC_R_UNKNOWN_COFACTOR);
366 if (group->generator == NULL) {
367 group->generator = EC_POINT_new(group);
368 if (group->generator == NULL)
371 if (!EC_POINT_copy(group->generator, generator))
374 if (!BN_copy(group->order, order))
377 /* Either take the provided positive cofactor, or try to compute it */
378 if (cofactor != NULL && !BN_is_zero(cofactor)) {
379 if (!BN_copy(group->cofactor, cofactor))
381 } else if (!ec_guess_cofactor(group)) {
382 BN_zero(group->cofactor);
387 * Some groups have an order with
388 * factors of two, which makes the Montgomery setup fail.
389 * |group->mont_data| will be NULL in this case.
391 if (BN_is_odd(group->order)) {
392 return ec_precompute_mont_data(group);
395 BN_MONT_CTX_free(group->mont_data);
396 group->mont_data = NULL;
400 const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
402 return group->generator;
405 BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group)
407 return group->mont_data;
410 int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
412 if (group->order == NULL)
414 if (!BN_copy(order, group->order))
417 return !BN_is_zero(order);
420 const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group)
425 int EC_GROUP_order_bits(const EC_GROUP *group)
427 return group->meth->group_order_bits(group);
430 int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
434 if (group->cofactor == NULL)
436 if (!BN_copy(cofactor, group->cofactor))
439 return !BN_is_zero(group->cofactor);
442 const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group)
444 return group->cofactor;
447 void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)
449 group->curve_name = nid;
452 int EC_GROUP_get_curve_name(const EC_GROUP *group)
454 return group->curve_name;
457 void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
459 group->asn1_flag = flag;
462 int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
464 return group->asn1_flag;
467 void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
468 point_conversion_form_t form)
470 group->asn1_form = form;
473 point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP
476 return group->asn1_form;
479 size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
481 OPENSSL_free(group->seed);
488 if ((group->seed = OPENSSL_malloc(len)) == NULL) {
489 ECerr(EC_F_EC_GROUP_SET_SEED, ERR_R_MALLOC_FAILURE);
492 memcpy(group->seed, p, len);
493 group->seed_len = len;
498 unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
503 size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
505 return group->seed_len;
508 int EC_GROUP_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
509 const BIGNUM *b, BN_CTX *ctx)
511 if (group->meth->group_set_curve == 0) {
512 ECerr(EC_F_EC_GROUP_SET_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
515 return group->meth->group_set_curve(group, p, a, b, ctx);
518 int EC_GROUP_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b,
521 if (group->meth->group_get_curve == NULL) {
522 ECerr(EC_F_EC_GROUP_GET_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
525 return group->meth->group_get_curve(group, p, a, b, ctx);
528 #if OPENSSL_API_COMPAT < 0x10200000L
529 int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
530 const BIGNUM *b, BN_CTX *ctx)
532 return EC_GROUP_set_curve(group, p, a, b, ctx);
535 int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
536 BIGNUM *b, BN_CTX *ctx)
538 return EC_GROUP_get_curve(group, p, a, b, ctx);
541 # ifndef OPENSSL_NO_EC2M
542 int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
543 const BIGNUM *b, BN_CTX *ctx)
545 return EC_GROUP_set_curve(group, p, a, b, ctx);
548 int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
549 BIGNUM *b, BN_CTX *ctx)
551 return EC_GROUP_get_curve(group, p, a, b, ctx);
556 int EC_GROUP_get_degree(const EC_GROUP *group)
558 if (group->meth->group_get_degree == 0) {
559 ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
562 return group->meth->group_get_degree(group);
565 int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
567 if (group->meth->group_check_discriminant == 0) {
568 ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT,
569 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
572 return group->meth->group_check_discriminant(group, ctx);
575 int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
578 BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
579 BN_CTX *ctx_new = NULL;
581 /* compare the field types */
582 if (EC_METHOD_get_field_type(EC_GROUP_method_of(a)) !=
583 EC_METHOD_get_field_type(EC_GROUP_method_of(b)))
585 /* compare the curve name (if present in both) */
586 if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
587 EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
589 if (a->meth->flags & EC_FLAGS_CUSTOM_CURVE)
593 ctx_new = ctx = BN_CTX_new();
598 a1 = BN_CTX_get(ctx);
599 a2 = BN_CTX_get(ctx);
600 a3 = BN_CTX_get(ctx);
601 b1 = BN_CTX_get(ctx);
602 b2 = BN_CTX_get(ctx);
603 b3 = BN_CTX_get(ctx);
606 BN_CTX_free(ctx_new);
611 * XXX This approach assumes that the external representation of curves
612 * over the same field type is the same.
614 if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
615 !b->meth->group_get_curve(b, b1, b2, b3, ctx))
618 if (r || BN_cmp(a1, b1) || BN_cmp(a2, b2) || BN_cmp(a3, b3))
621 /* XXX EC_POINT_cmp() assumes that the methods are equal */
622 if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
623 EC_GROUP_get0_generator(b), ctx))
627 const BIGNUM *ao, *bo, *ac, *bc;
628 /* compare the order and cofactor */
629 ao = EC_GROUP_get0_order(a);
630 bo = EC_GROUP_get0_order(b);
631 ac = EC_GROUP_get0_cofactor(a);
632 bc = EC_GROUP_get0_cofactor(b);
633 if (ao == NULL || bo == NULL) {
635 BN_CTX_free(ctx_new);
638 if (BN_cmp(ao, bo) || BN_cmp(ac, bc))
643 BN_CTX_free(ctx_new);
648 /* functions for EC_POINT objects */
650 EC_POINT *EC_POINT_new(const EC_GROUP *group)
655 ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER);
658 if (group->meth->point_init == NULL) {
659 ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
663 ret = OPENSSL_zalloc(sizeof(*ret));
665 ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE);
669 ret->meth = group->meth;
670 ret->curve_name = group->curve_name;
672 if (!ret->meth->point_init(ret)) {
680 void EC_POINT_free(EC_POINT *point)
685 if (point->meth->point_finish != 0)
686 point->meth->point_finish(point);
690 void EC_POINT_clear_free(EC_POINT *point)
695 if (point->meth->point_clear_finish != 0)
696 point->meth->point_clear_finish(point);
697 else if (point->meth->point_finish != 0)
698 point->meth->point_finish(point);
699 OPENSSL_clear_free(point, sizeof(*point));
702 int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)
704 if (dest->meth->point_copy == 0) {
705 ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
708 if (dest->meth != src->meth
709 || (dest->curve_name != src->curve_name
710 && dest->curve_name != 0
711 && src->curve_name != 0)) {
712 ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS);
717 return dest->meth->point_copy(dest, src);
720 EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
728 t = EC_POINT_new(group);
731 r = EC_POINT_copy(t, a);
739 const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
744 int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
746 if (group->meth->point_set_to_infinity == 0) {
747 ECerr(EC_F_EC_POINT_SET_TO_INFINITY,
748 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
751 if (group->meth != point->meth) {
752 ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
755 return group->meth->point_set_to_infinity(group, point);
758 int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
759 EC_POINT *point, const BIGNUM *x,
760 const BIGNUM *y, const BIGNUM *z,
763 if (group->meth->point_set_Jprojective_coordinates_GFp == 0) {
764 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
765 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
768 if (!ec_point_is_compat(point, group)) {
769 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
770 EC_R_INCOMPATIBLE_OBJECTS);
773 return group->meth->point_set_Jprojective_coordinates_GFp(group, point, x,
777 int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
778 const EC_POINT *point, BIGNUM *x,
779 BIGNUM *y, BIGNUM *z,
782 if (group->meth->point_get_Jprojective_coordinates_GFp == 0) {
783 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
784 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
787 if (!ec_point_is_compat(point, group)) {
788 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
789 EC_R_INCOMPATIBLE_OBJECTS);
792 return group->meth->point_get_Jprojective_coordinates_GFp(group, point, x,
796 int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point,
797 const BIGNUM *x, const BIGNUM *y,
800 if (group->meth->point_set_affine_coordinates == NULL) {
801 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES,
802 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
805 if (!ec_point_is_compat(point, group)) {
806 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, EC_R_INCOMPATIBLE_OBJECTS);
809 if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx))
812 if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {
813 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, EC_R_POINT_IS_NOT_ON_CURVE);
819 #if OPENSSL_API_COMPAT < 0x10200000L
820 int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
821 EC_POINT *point, const BIGNUM *x,
822 const BIGNUM *y, BN_CTX *ctx)
824 return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
827 # ifndef OPENSSL_NO_EC2M
828 int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,
829 EC_POINT *point, const BIGNUM *x,
830 const BIGNUM *y, BN_CTX *ctx)
832 return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
837 int EC_POINT_get_affine_coordinates(const EC_GROUP *group,
838 const EC_POINT *point, BIGNUM *x, BIGNUM *y,
841 if (group->meth->point_get_affine_coordinates == NULL) {
842 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES,
843 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
846 if (!ec_point_is_compat(point, group)) {
847 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, EC_R_INCOMPATIBLE_OBJECTS);
850 if (EC_POINT_is_at_infinity(group, point)) {
851 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, EC_R_POINT_AT_INFINITY);
854 return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
857 #if OPENSSL_API_COMPAT < 0x10200000L
858 int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
859 const EC_POINT *point, BIGNUM *x,
860 BIGNUM *y, BN_CTX *ctx)
862 return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
865 # ifndef OPENSSL_NO_EC2M
866 int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,
867 const EC_POINT *point, BIGNUM *x,
868 BIGNUM *y, BN_CTX *ctx)
870 return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
875 int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
876 const EC_POINT *b, BN_CTX *ctx)
878 if (group->meth->add == 0) {
879 ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
882 if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)
883 || !ec_point_is_compat(b, group)) {
884 ECerr(EC_F_EC_POINT_ADD, EC_R_INCOMPATIBLE_OBJECTS);
887 return group->meth->add(group, r, a, b, ctx);
890 int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
893 if (group->meth->dbl == 0) {
894 ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
897 if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)) {
898 ECerr(EC_F_EC_POINT_DBL, EC_R_INCOMPATIBLE_OBJECTS);
901 return group->meth->dbl(group, r, a, ctx);
904 int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
906 if (group->meth->invert == 0) {
907 ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
910 if (!ec_point_is_compat(a, group)) {
911 ECerr(EC_F_EC_POINT_INVERT, EC_R_INCOMPATIBLE_OBJECTS);
914 return group->meth->invert(group, a, ctx);
917 int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
919 if (group->meth->is_at_infinity == 0) {
920 ECerr(EC_F_EC_POINT_IS_AT_INFINITY,
921 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
924 if (!ec_point_is_compat(point, group)) {
925 ECerr(EC_F_EC_POINT_IS_AT_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
928 return group->meth->is_at_infinity(group, point);
932 * Check whether an EC_POINT is on the curve or not. Note that the return
933 * value for this function should NOT be treated as a boolean. Return values:
934 * 1: The point is on the curve
935 * 0: The point is not on the curve
936 * -1: An error occurred
938 int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
941 if (group->meth->is_on_curve == 0) {
942 ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
945 if (!ec_point_is_compat(point, group)) {
946 ECerr(EC_F_EC_POINT_IS_ON_CURVE, EC_R_INCOMPATIBLE_OBJECTS);
949 return group->meth->is_on_curve(group, point, ctx);
952 int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
955 if (group->meth->point_cmp == 0) {
956 ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
959 if (!ec_point_is_compat(a, group) || !ec_point_is_compat(b, group)) {
960 ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS);
963 return group->meth->point_cmp(group, a, b, ctx);
966 int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
968 if (group->meth->make_affine == 0) {
969 ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
972 if (!ec_point_is_compat(point, group)) {
973 ECerr(EC_F_EC_POINT_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
976 return group->meth->make_affine(group, point, ctx);
979 int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
980 EC_POINT *points[], BN_CTX *ctx)
984 if (group->meth->points_make_affine == 0) {
985 ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
988 for (i = 0; i < num; i++) {
989 if (!ec_point_is_compat(points[i], group)) {
990 ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
994 return group->meth->points_make_affine(group, num, points, ctx);
998 * Functions for point multiplication. If group->meth->mul is 0, we use the
999 * wNAF-based implementations in ec_mult.c; otherwise we dispatch through
1003 int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
1004 size_t num, const EC_POINT *points[],
1005 const BIGNUM *scalars[], BN_CTX *ctx)
1009 BN_CTX *new_ctx = NULL;
1011 if (!ec_point_is_compat(r, group)) {
1012 ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
1016 if (scalar == NULL && num == 0)
1017 return EC_POINT_set_to_infinity(group, r);
1019 for (i = 0; i < num; i++) {
1020 if (!ec_point_is_compat(points[i], group)) {
1021 ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
1026 if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL) {
1027 ECerr(EC_F_EC_POINTS_MUL, ERR_R_INTERNAL_ERROR);
1031 if (group->meth->mul != NULL)
1032 ret = group->meth->mul(group, r, scalar, num, points, scalars, ctx);
1035 ret = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
1037 BN_CTX_free(new_ctx);
1041 int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
1042 const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
1044 /* just a convenient interface to EC_POINTs_mul() */
1046 const EC_POINT *points[1];
1047 const BIGNUM *scalars[1];
1050 scalars[0] = p_scalar;
1052 return EC_POINTs_mul(group, r, g_scalar,
1054 && p_scalar != NULL), points, scalars, ctx);
1057 int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
1059 if (group->meth->mul == 0)
1061 return ec_wNAF_precompute_mult(group, ctx);
1063 if (group->meth->precompute_mult != 0)
1064 return group->meth->precompute_mult(group, ctx);
1066 return 1; /* nothing to do, so report success */
1069 int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
1071 if (group->meth->mul == 0)
1073 return ec_wNAF_have_precompute_mult(group);
1075 if (group->meth->have_precompute_mult != 0)
1076 return group->meth->have_precompute_mult(group);
1078 return 0; /* cannot tell whether precomputation has
1083 * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
1084 * returns one on success. On error it returns zero.
1086 static int ec_precompute_mont_data(EC_GROUP *group)
1088 BN_CTX *ctx = BN_CTX_new();
1091 BN_MONT_CTX_free(group->mont_data);
1092 group->mont_data = NULL;
1097 group->mont_data = BN_MONT_CTX_new();
1098 if (group->mont_data == NULL)
1101 if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) {
1102 BN_MONT_CTX_free(group->mont_data);
1103 group->mont_data = NULL;
1115 int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg)
1117 return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
1120 void *EC_KEY_get_ex_data(const EC_KEY *key, int idx)
1122 return CRYPTO_get_ex_data(&key->ex_data, idx);
1125 int ec_group_simple_order_bits(const EC_GROUP *group)
1127 if (group->order == NULL)
1129 return BN_num_bits(group->order);
1132 static int ec_field_inverse_mod_ord(const EC_GROUP *group, BIGNUM *r,
1133 const BIGNUM *x, BN_CTX *ctx)
1136 BN_CTX *new_ctx = NULL;
1139 if (group->mont_data == NULL)
1142 if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL)
1146 if ((e = BN_CTX_get(ctx)) == NULL)
1150 * We want inverse in constant time, therefore we utilize the fact
1151 * order must be prime and use Fermats Little Theorem instead.
1153 if (!BN_set_word(e, 2))
1155 if (!BN_sub(e, group->order, e))
1158 * Exponent e is public.
1159 * No need for scatter-gather or BN_FLG_CONSTTIME.
1161 if (!BN_mod_exp_mont(r, x, e, group->order, ctx, group->mont_data))
1168 BN_CTX_free(new_ctx);
1173 * Default behavior, if group->meth->field_inverse_mod_ord is NULL:
1174 * - When group->order is even, this function returns an error.
1175 * - When group->order is otherwise composite, the correctness
1176 * of the output is not guaranteed.
1177 * - When x is outside the range [1, group->order), the correctness
1178 * of the output is not guaranteed.
1179 * - Otherwise, this function returns the multiplicative inverse in the
1180 * range [1, group->order).
1182 * EC_METHODs must implement their own field_inverse_mod_ord for
1183 * other functionality.
1185 int ec_group_do_inverse_ord(const EC_GROUP *group, BIGNUM *res,
1186 const BIGNUM *x, BN_CTX *ctx)
1188 if (group->meth->field_inverse_mod_ord != NULL)
1189 return group->meth->field_inverse_mod_ord(group, res, x, ctx);
1191 return ec_field_inverse_mod_ord(group, res, x, ctx);
1195 * Coordinate blinding for EC_POINT.
1197 * The underlying EC_METHOD can optionally implement this function:
1198 * underlying implementations should return 0 on errors, or 1 on
1201 * This wrapper returns 1 in case the underlying EC_METHOD does not
1202 * support coordinate blinding.
1204 int ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p, BN_CTX *ctx)
1206 if (group->meth->blind_coordinates == NULL)
1207 return 1; /* ignore if not implemented */
1209 return group->meth->blind_coordinates(group, p, ctx);