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;
216 OPENSSL_free(dest->seed);
217 if ((dest->seed = OPENSSL_malloc(src->seed_len)) == NULL) {
218 ECerr(EC_F_EC_GROUP_COPY, ERR_R_MALLOC_FAILURE);
221 if (!memcpy(dest->seed, src->seed, src->seed_len))
223 dest->seed_len = src->seed_len;
225 OPENSSL_free(dest->seed);
230 return dest->meth->group_copy(dest, src);
233 EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)
241 if ((t = EC_GROUP_new(a->meth)) == NULL)
243 if (!EC_GROUP_copy(t, a))
256 const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group)
261 int EC_METHOD_get_field_type(const EC_METHOD *meth)
263 return meth->field_type;
266 static int ec_precompute_mont_data(EC_GROUP *);
269 * Try computing cofactor from the generator order (n) and field cardinality (q).
270 * This works for all curves of cryptographic interest.
272 * Hasse thm: q + 1 - 2*sqrt(q) <= n*h <= q + 1 + 2*sqrt(q)
273 * h_min = (q + 1 - 2*sqrt(q))/n
274 * h_max = (q + 1 + 2*sqrt(q))/n
275 * h_max - h_min = 4*sqrt(q)/n
276 * So if n > 4*sqrt(q) holds, there is only one possible value for h:
277 * h = \lfloor (h_min + h_max)/2 \rceil = \lfloor (q + 1)/n \rceil
279 * Otherwise, zero cofactor and return success.
281 static int ec_guess_cofactor(EC_GROUP *group) {
287 * If the cofactor is too large, we cannot guess it.
288 * The RHS of below is a strict overestimate of lg(4 * sqrt(q))
290 if (BN_num_bits(group->order) <= (BN_num_bits(group->field) + 1) / 2 + 3) {
292 BN_zero(group->cofactor);
297 if ((ctx = BN_CTX_new()) == NULL)
301 if ((q = BN_CTX_get(ctx)) == NULL)
304 /* set q = 2**m for binary fields; q = p otherwise */
305 if (group->meth->field_type == NID_X9_62_characteristic_two_field) {
307 if (!BN_set_bit(q, BN_num_bits(group->field) - 1))
310 if (!BN_copy(q, group->field))
314 /* compute h = \lfloor (q + 1)/n \rceil = \lfloor (q + 1 + n/2)/n \rfloor */
315 if (!BN_rshift1(group->cofactor, group->order) /* n/2 */
316 || !BN_add(group->cofactor, group->cofactor, q) /* q + n/2 */
318 || !BN_add(group->cofactor, group->cofactor, BN_value_one())
319 /* (q + 1 + n/2)/n */
320 || !BN_div(group->cofactor, NULL, group->cofactor, group->order, ctx))
329 int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
330 const BIGNUM *order, const BIGNUM *cofactor)
332 if (generator == NULL) {
333 ECerr(EC_F_EC_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER);
337 /* require group->field >= 1 */
338 if (group->field == NULL || BN_is_zero(group->field)
339 || BN_is_negative(group->field)) {
340 ECerr(EC_F_EC_GROUP_SET_GENERATOR, EC_R_INVALID_FIELD);
345 * - require order >= 1
346 * - enforce upper bound due to Hasse thm: order can be no more than one bit
347 * longer than field cardinality
349 if (order == NULL || BN_is_zero(order) || BN_is_negative(order)
350 || BN_num_bits(order) > BN_num_bits(group->field) + 1) {
351 ECerr(EC_F_EC_GROUP_SET_GENERATOR, EC_R_INVALID_GROUP_ORDER);
356 * Unfortunately the cofactor is an optional field in many standards.
357 * Internally, the lib uses 0 cofactor as a marker for "unknown cofactor".
358 * So accept cofactor == NULL or cofactor >= 0.
360 if (cofactor != NULL && BN_is_negative(cofactor)) {
361 ECerr(EC_F_EC_GROUP_SET_GENERATOR, EC_R_UNKNOWN_COFACTOR);
365 if (group->generator == NULL) {
366 group->generator = EC_POINT_new(group);
367 if (group->generator == NULL)
370 if (!EC_POINT_copy(group->generator, generator))
373 if (!BN_copy(group->order, order))
376 /* Either take the provided positive cofactor, or try to compute it */
377 if (cofactor != NULL && !BN_is_zero(cofactor)) {
378 if (!BN_copy(group->cofactor, cofactor))
380 } else if (!ec_guess_cofactor(group)) {
381 BN_zero(group->cofactor);
386 * Some groups have an order with
387 * factors of two, which makes the Montgomery setup fail.
388 * |group->mont_data| will be NULL in this case.
390 if (BN_is_odd(group->order)) {
391 return ec_precompute_mont_data(group);
394 BN_MONT_CTX_free(group->mont_data);
395 group->mont_data = NULL;
399 const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
401 return group->generator;
404 BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group)
406 return group->mont_data;
409 int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
411 if (group->order == NULL)
413 if (!BN_copy(order, group->order))
416 return !BN_is_zero(order);
419 const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group)
424 int EC_GROUP_order_bits(const EC_GROUP *group)
426 return group->meth->group_order_bits(group);
429 int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
433 if (group->cofactor == NULL)
435 if (!BN_copy(cofactor, group->cofactor))
438 return !BN_is_zero(group->cofactor);
441 const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group)
443 return group->cofactor;
446 void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)
448 group->curve_name = nid;
451 int EC_GROUP_get_curve_name(const EC_GROUP *group)
453 return group->curve_name;
456 void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
458 group->asn1_flag = flag;
461 int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
463 return group->asn1_flag;
466 void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
467 point_conversion_form_t form)
469 group->asn1_form = form;
472 point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP
475 return group->asn1_form;
478 size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
480 OPENSSL_free(group->seed);
487 if ((group->seed = OPENSSL_malloc(len)) == NULL) {
488 ECerr(EC_F_EC_GROUP_SET_SEED, ERR_R_MALLOC_FAILURE);
491 memcpy(group->seed, p, len);
492 group->seed_len = len;
497 unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
502 size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
504 return group->seed_len;
507 int EC_GROUP_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
508 const BIGNUM *b, BN_CTX *ctx)
510 if (group->meth->group_set_curve == 0) {
511 ECerr(EC_F_EC_GROUP_SET_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
514 return group->meth->group_set_curve(group, p, a, b, ctx);
517 int EC_GROUP_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b,
520 if (group->meth->group_get_curve == NULL) {
521 ECerr(EC_F_EC_GROUP_GET_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
524 return group->meth->group_get_curve(group, p, a, b, ctx);
527 #if OPENSSL_API_COMPAT < 0x10200000L
528 int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
529 const BIGNUM *b, BN_CTX *ctx)
531 return EC_GROUP_set_curve(group, p, a, b, ctx);
534 int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
535 BIGNUM *b, BN_CTX *ctx)
537 return EC_GROUP_get_curve(group, p, a, b, ctx);
540 # ifndef OPENSSL_NO_EC2M
541 int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
542 const BIGNUM *b, BN_CTX *ctx)
544 return EC_GROUP_set_curve(group, p, a, b, ctx);
547 int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
548 BIGNUM *b, BN_CTX *ctx)
550 return EC_GROUP_get_curve(group, p, a, b, ctx);
555 int EC_GROUP_get_degree(const EC_GROUP *group)
557 if (group->meth->group_get_degree == 0) {
558 ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
561 return group->meth->group_get_degree(group);
564 int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
566 if (group->meth->group_check_discriminant == 0) {
567 ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT,
568 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
571 return group->meth->group_check_discriminant(group, ctx);
574 int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
577 BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
578 BN_CTX *ctx_new = NULL;
580 /* compare the field types */
581 if (EC_METHOD_get_field_type(EC_GROUP_method_of(a)) !=
582 EC_METHOD_get_field_type(EC_GROUP_method_of(b)))
584 /* compare the curve name (if present in both) */
585 if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
586 EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
588 if (a->meth->flags & EC_FLAGS_CUSTOM_CURVE)
592 ctx_new = ctx = BN_CTX_new();
597 a1 = BN_CTX_get(ctx);
598 a2 = BN_CTX_get(ctx);
599 a3 = BN_CTX_get(ctx);
600 b1 = BN_CTX_get(ctx);
601 b2 = BN_CTX_get(ctx);
602 b3 = BN_CTX_get(ctx);
605 BN_CTX_free(ctx_new);
610 * XXX This approach assumes that the external representation of curves
611 * over the same field type is the same.
613 if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
614 !b->meth->group_get_curve(b, b1, b2, b3, ctx))
617 if (r || BN_cmp(a1, b1) || BN_cmp(a2, b2) || BN_cmp(a3, b3))
620 /* XXX EC_POINT_cmp() assumes that the methods are equal */
621 if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
622 EC_GROUP_get0_generator(b), ctx))
626 const BIGNUM *ao, *bo, *ac, *bc;
627 /* compare the order and cofactor */
628 ao = EC_GROUP_get0_order(a);
629 bo = EC_GROUP_get0_order(b);
630 ac = EC_GROUP_get0_cofactor(a);
631 bc = EC_GROUP_get0_cofactor(b);
632 if (ao == NULL || bo == NULL) {
634 BN_CTX_free(ctx_new);
637 if (BN_cmp(ao, bo) || BN_cmp(ac, bc))
642 BN_CTX_free(ctx_new);
647 /* functions for EC_POINT objects */
649 EC_POINT *EC_POINT_new(const EC_GROUP *group)
654 ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER);
657 if (group->meth->point_init == NULL) {
658 ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
662 ret = OPENSSL_zalloc(sizeof(*ret));
664 ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE);
668 ret->meth = group->meth;
669 ret->curve_name = group->curve_name;
671 if (!ret->meth->point_init(ret)) {
679 void EC_POINT_free(EC_POINT *point)
684 if (point->meth->point_finish != 0)
685 point->meth->point_finish(point);
689 void EC_POINT_clear_free(EC_POINT *point)
694 if (point->meth->point_clear_finish != 0)
695 point->meth->point_clear_finish(point);
696 else if (point->meth->point_finish != 0)
697 point->meth->point_finish(point);
698 OPENSSL_clear_free(point, sizeof(*point));
701 int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)
703 if (dest->meth->point_copy == 0) {
704 ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
707 if (dest->meth != src->meth
708 || (dest->curve_name != src->curve_name
709 && dest->curve_name != 0
710 && src->curve_name != 0)) {
711 ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS);
716 return dest->meth->point_copy(dest, src);
719 EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
727 t = EC_POINT_new(group);
730 r = EC_POINT_copy(t, a);
738 const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
743 int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
745 if (group->meth->point_set_to_infinity == 0) {
746 ECerr(EC_F_EC_POINT_SET_TO_INFINITY,
747 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
750 if (group->meth != point->meth) {
751 ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
754 return group->meth->point_set_to_infinity(group, point);
757 int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
758 EC_POINT *point, const BIGNUM *x,
759 const BIGNUM *y, const BIGNUM *z,
762 if (group->meth->point_set_Jprojective_coordinates_GFp == 0) {
763 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
764 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
767 if (!ec_point_is_compat(point, group)) {
768 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
769 EC_R_INCOMPATIBLE_OBJECTS);
772 return group->meth->point_set_Jprojective_coordinates_GFp(group, point, x,
776 int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
777 const EC_POINT *point, BIGNUM *x,
778 BIGNUM *y, BIGNUM *z,
781 if (group->meth->point_get_Jprojective_coordinates_GFp == 0) {
782 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
783 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
786 if (!ec_point_is_compat(point, group)) {
787 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
788 EC_R_INCOMPATIBLE_OBJECTS);
791 return group->meth->point_get_Jprojective_coordinates_GFp(group, point, x,
795 int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point,
796 const BIGNUM *x, const BIGNUM *y,
799 if (group->meth->point_set_affine_coordinates == NULL) {
800 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES,
801 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
804 if (!ec_point_is_compat(point, group)) {
805 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, EC_R_INCOMPATIBLE_OBJECTS);
808 if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx))
811 if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {
812 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, EC_R_POINT_IS_NOT_ON_CURVE);
818 #if OPENSSL_API_COMPAT < 0x10200000L
819 int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
820 EC_POINT *point, const BIGNUM *x,
821 const BIGNUM *y, BN_CTX *ctx)
823 return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
826 # ifndef OPENSSL_NO_EC2M
827 int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,
828 EC_POINT *point, const BIGNUM *x,
829 const BIGNUM *y, BN_CTX *ctx)
831 return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
836 int EC_POINT_get_affine_coordinates(const EC_GROUP *group,
837 const EC_POINT *point, BIGNUM *x, BIGNUM *y,
840 if (group->meth->point_get_affine_coordinates == NULL) {
841 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES,
842 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
845 if (!ec_point_is_compat(point, group)) {
846 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, EC_R_INCOMPATIBLE_OBJECTS);
849 if (EC_POINT_is_at_infinity(group, point)) {
850 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, EC_R_POINT_AT_INFINITY);
853 return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
856 #if OPENSSL_API_COMPAT < 0x10200000L
857 int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
858 const EC_POINT *point, BIGNUM *x,
859 BIGNUM *y, BN_CTX *ctx)
861 return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
864 # ifndef OPENSSL_NO_EC2M
865 int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,
866 const EC_POINT *point, BIGNUM *x,
867 BIGNUM *y, BN_CTX *ctx)
869 return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
874 int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
875 const EC_POINT *b, BN_CTX *ctx)
877 if (group->meth->add == 0) {
878 ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
881 if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)
882 || !ec_point_is_compat(b, group)) {
883 ECerr(EC_F_EC_POINT_ADD, EC_R_INCOMPATIBLE_OBJECTS);
886 return group->meth->add(group, r, a, b, ctx);
889 int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
892 if (group->meth->dbl == 0) {
893 ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
896 if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)) {
897 ECerr(EC_F_EC_POINT_DBL, EC_R_INCOMPATIBLE_OBJECTS);
900 return group->meth->dbl(group, r, a, ctx);
903 int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
905 if (group->meth->invert == 0) {
906 ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
909 if (!ec_point_is_compat(a, group)) {
910 ECerr(EC_F_EC_POINT_INVERT, EC_R_INCOMPATIBLE_OBJECTS);
913 return group->meth->invert(group, a, ctx);
916 int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
918 if (group->meth->is_at_infinity == 0) {
919 ECerr(EC_F_EC_POINT_IS_AT_INFINITY,
920 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
923 if (!ec_point_is_compat(point, group)) {
924 ECerr(EC_F_EC_POINT_IS_AT_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
927 return group->meth->is_at_infinity(group, point);
931 * Check whether an EC_POINT is on the curve or not. Note that the return
932 * value for this function should NOT be treated as a boolean. Return values:
933 * 1: The point is on the curve
934 * 0: The point is not on the curve
935 * -1: An error occurred
937 int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
940 if (group->meth->is_on_curve == 0) {
941 ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
944 if (!ec_point_is_compat(point, group)) {
945 ECerr(EC_F_EC_POINT_IS_ON_CURVE, EC_R_INCOMPATIBLE_OBJECTS);
948 return group->meth->is_on_curve(group, point, ctx);
951 int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
954 if (group->meth->point_cmp == 0) {
955 ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
958 if (!ec_point_is_compat(a, group) || !ec_point_is_compat(b, group)) {
959 ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS);
962 return group->meth->point_cmp(group, a, b, ctx);
965 int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
967 if (group->meth->make_affine == 0) {
968 ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
971 if (!ec_point_is_compat(point, group)) {
972 ECerr(EC_F_EC_POINT_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
975 return group->meth->make_affine(group, point, ctx);
978 int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
979 EC_POINT *points[], BN_CTX *ctx)
983 if (group->meth->points_make_affine == 0) {
984 ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
987 for (i = 0; i < num; i++) {
988 if (!ec_point_is_compat(points[i], group)) {
989 ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
993 return group->meth->points_make_affine(group, num, points, ctx);
997 * Functions for point multiplication. If group->meth->mul is 0, we use the
998 * wNAF-based implementations in ec_mult.c; otherwise we dispatch through
1002 int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
1003 size_t num, const EC_POINT *points[],
1004 const BIGNUM *scalars[], BN_CTX *ctx)
1008 BN_CTX *new_ctx = NULL;
1010 if (!ec_point_is_compat(r, group)) {
1011 ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
1015 if (scalar == NULL && num == 0)
1016 return EC_POINT_set_to_infinity(group, r);
1018 for (i = 0; i < num; i++) {
1019 if (!ec_point_is_compat(points[i], group)) {
1020 ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
1025 if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL) {
1026 ECerr(EC_F_EC_POINTS_MUL, ERR_R_INTERNAL_ERROR);
1030 if (group->meth->mul != NULL)
1031 ret = group->meth->mul(group, r, scalar, num, points, scalars, ctx);
1034 ret = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
1036 BN_CTX_free(new_ctx);
1040 int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
1041 const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
1043 /* just a convenient interface to EC_POINTs_mul() */
1045 const EC_POINT *points[1];
1046 const BIGNUM *scalars[1];
1049 scalars[0] = p_scalar;
1051 return EC_POINTs_mul(group, r, g_scalar,
1053 && p_scalar != NULL), points, scalars, ctx);
1056 int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
1058 if (group->meth->mul == 0)
1060 return ec_wNAF_precompute_mult(group, ctx);
1062 if (group->meth->precompute_mult != 0)
1063 return group->meth->precompute_mult(group, ctx);
1065 return 1; /* nothing to do, so report success */
1068 int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
1070 if (group->meth->mul == 0)
1072 return ec_wNAF_have_precompute_mult(group);
1074 if (group->meth->have_precompute_mult != 0)
1075 return group->meth->have_precompute_mult(group);
1077 return 0; /* cannot tell whether precomputation has
1082 * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
1083 * returns one on success. On error it returns zero.
1085 static int ec_precompute_mont_data(EC_GROUP *group)
1087 BN_CTX *ctx = BN_CTX_new();
1090 BN_MONT_CTX_free(group->mont_data);
1091 group->mont_data = NULL;
1096 group->mont_data = BN_MONT_CTX_new();
1097 if (group->mont_data == NULL)
1100 if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) {
1101 BN_MONT_CTX_free(group->mont_data);
1102 group->mont_data = NULL;
1114 int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg)
1116 return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
1119 void *EC_KEY_get_ex_data(const EC_KEY *key, int idx)
1121 return CRYPTO_get_ex_data(&key->ex_data, idx);
1124 int ec_group_simple_order_bits(const EC_GROUP *group)
1126 if (group->order == NULL)
1128 return BN_num_bits(group->order);
1131 static int ec_field_inverse_mod_ord(const EC_GROUP *group, BIGNUM *r,
1132 const BIGNUM *x, BN_CTX *ctx)
1135 BN_CTX *new_ctx = NULL;
1138 if (group->mont_data == NULL)
1141 if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL)
1145 if ((e = BN_CTX_get(ctx)) == NULL)
1149 * We want inverse in constant time, therefore we utilize the fact
1150 * order must be prime and use Fermats Little Theorem instead.
1152 if (!BN_set_word(e, 2))
1154 if (!BN_sub(e, group->order, e))
1157 * Exponent e is public.
1158 * No need for scatter-gather or BN_FLG_CONSTTIME.
1160 if (!BN_mod_exp_mont(r, x, e, group->order, ctx, group->mont_data))
1167 BN_CTX_free(new_ctx);
1172 * Default behavior, if group->meth->field_inverse_mod_ord is NULL:
1173 * - When group->order is even, this function returns an error.
1174 * - When group->order is otherwise composite, the correctness
1175 * of the output is not guaranteed.
1176 * - When x is outside the range [1, group->order), the correctness
1177 * of the output is not guaranteed.
1178 * - Otherwise, this function returns the multiplicative inverse in the
1179 * range [1, group->order).
1181 * EC_METHODs must implement their own field_inverse_mod_ord for
1182 * other functionality.
1184 int ec_group_do_inverse_ord(const EC_GROUP *group, BIGNUM *res,
1185 const BIGNUM *x, BN_CTX *ctx)
1187 if (group->meth->field_inverse_mod_ord != NULL)
1188 return group->meth->field_inverse_mod_ord(group, res, x, ctx);
1190 return ec_field_inverse_mod_ord(group, res, x, ctx);
1194 * Coordinate blinding for EC_POINT.
1196 * The underlying EC_METHOD can optionally implement this function:
1197 * underlying implementations should return 0 on errors, or 1 on
1200 * This wrapper returns 1 in case the underlying EC_METHOD does not
1201 * support coordinate blinding.
1203 int ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p, BN_CTX *ctx)
1205 if (group->meth->blind_coordinates == NULL)
1206 return 1; /* ignore if not implemented */
1208 return group->meth->blind_coordinates(group, p, ctx);