2 * Copyright 2001-2018 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
13 #include <openssl/err.h>
14 #include <openssl/opensslv.h>
18 /* functions for EC_GROUP objects */
20 EC_GROUP *EC_GROUP_new_ex(OPENSSL_CTX *libctx, const EC_METHOD *meth)
25 ECerr(EC_F_EC_GROUP_NEW_EX, EC_R_SLOT_FULL);
28 if (meth->group_init == 0) {
29 ECerr(EC_F_EC_GROUP_NEW_EX, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
33 ret = OPENSSL_zalloc(sizeof(*ret));
35 ECerr(EC_F_EC_GROUP_NEW_EX, ERR_R_MALLOC_FAILURE);
41 if ((ret->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
42 ret->order = BN_new();
43 if (ret->order == NULL)
45 ret->cofactor = BN_new();
46 if (ret->cofactor == NULL)
49 ret->asn1_flag = OPENSSL_EC_NAMED_CURVE;
50 ret->asn1_form = POINT_CONVERSION_UNCOMPRESSED;
51 if (!meth->group_init(ret))
57 BN_free(ret->cofactor);
63 EC_GROUP *EC_GROUP_new(const EC_METHOD *meth)
65 return EC_GROUP_new_ex(NULL, meth);
69 void EC_pre_comp_free(EC_GROUP *group)
71 switch (group->pre_comp_type) {
75 #ifdef ECP_NISTZ256_ASM
76 EC_nistz256_pre_comp_free(group->pre_comp.nistz256);
79 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
81 EC_nistp224_pre_comp_free(group->pre_comp.nistp224);
84 EC_nistp256_pre_comp_free(group->pre_comp.nistp256);
87 EC_nistp521_pre_comp_free(group->pre_comp.nistp521);
96 EC_ec_pre_comp_free(group->pre_comp.ec);
99 group->pre_comp.ec = NULL;
102 void EC_GROUP_free(EC_GROUP *group)
107 if (group->meth->group_finish != 0)
108 group->meth->group_finish(group);
110 EC_pre_comp_free(group);
111 BN_MONT_CTX_free(group->mont_data);
112 EC_POINT_free(group->generator);
113 BN_free(group->order);
114 BN_free(group->cofactor);
115 OPENSSL_free(group->seed);
119 void EC_GROUP_clear_free(EC_GROUP *group)
124 if (group->meth->group_clear_finish != 0)
125 group->meth->group_clear_finish(group);
126 else if (group->meth->group_finish != 0)
127 group->meth->group_finish(group);
129 EC_pre_comp_free(group);
130 BN_MONT_CTX_free(group->mont_data);
131 EC_POINT_clear_free(group->generator);
132 BN_clear_free(group->order);
133 BN_clear_free(group->cofactor);
134 OPENSSL_clear_free(group->seed, group->seed_len);
135 OPENSSL_clear_free(group, sizeof(*group));
138 int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)
140 if (dest->meth->group_copy == 0) {
141 ECerr(EC_F_EC_GROUP_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
144 if (dest->meth != src->meth) {
145 ECerr(EC_F_EC_GROUP_COPY, EC_R_INCOMPATIBLE_OBJECTS);
151 dest->libctx = src->libctx;
152 dest->curve_name = src->curve_name;
154 /* Copy precomputed */
155 dest->pre_comp_type = src->pre_comp_type;
156 switch (src->pre_comp_type) {
158 dest->pre_comp.ec = NULL;
161 #ifdef ECP_NISTZ256_ASM
162 dest->pre_comp.nistz256 = EC_nistz256_pre_comp_dup(src->pre_comp.nistz256);
165 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
167 dest->pre_comp.nistp224 = EC_nistp224_pre_comp_dup(src->pre_comp.nistp224);
170 dest->pre_comp.nistp256 = EC_nistp256_pre_comp_dup(src->pre_comp.nistp256);
173 dest->pre_comp.nistp521 = EC_nistp521_pre_comp_dup(src->pre_comp.nistp521);
182 dest->pre_comp.ec = EC_ec_pre_comp_dup(src->pre_comp.ec);
186 if (src->mont_data != NULL) {
187 if (dest->mont_data == NULL) {
188 dest->mont_data = BN_MONT_CTX_new();
189 if (dest->mont_data == NULL)
192 if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data))
195 /* src->generator == NULL */
196 BN_MONT_CTX_free(dest->mont_data);
197 dest->mont_data = NULL;
200 if (src->generator != NULL) {
201 if (dest->generator == NULL) {
202 dest->generator = EC_POINT_new(dest);
203 if (dest->generator == NULL)
206 if (!EC_POINT_copy(dest->generator, src->generator))
209 /* src->generator == NULL */
210 EC_POINT_clear_free(dest->generator);
211 dest->generator = NULL;
214 if ((src->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
215 if (!BN_copy(dest->order, src->order))
217 if (!BN_copy(dest->cofactor, src->cofactor))
221 dest->asn1_flag = src->asn1_flag;
222 dest->asn1_form = src->asn1_form;
225 OPENSSL_free(dest->seed);
226 if ((dest->seed = OPENSSL_malloc(src->seed_len)) == NULL) {
227 ECerr(EC_F_EC_GROUP_COPY, ERR_R_MALLOC_FAILURE);
230 if (!memcpy(dest->seed, src->seed, src->seed_len))
232 dest->seed_len = src->seed_len;
234 OPENSSL_free(dest->seed);
239 return dest->meth->group_copy(dest, src);
242 EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)
250 if ((t = EC_GROUP_new_ex(a->libctx, a->meth)) == NULL)
252 if (!EC_GROUP_copy(t, a))
265 const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group)
270 int EC_METHOD_get_field_type(const EC_METHOD *meth)
272 return meth->field_type;
275 static int ec_precompute_mont_data(EC_GROUP *);
278 * Try computing cofactor from the generator order (n) and field cardinality (q).
279 * This works for all curves of cryptographic interest.
281 * Hasse thm: q + 1 - 2*sqrt(q) <= n*h <= q + 1 + 2*sqrt(q)
282 * h_min = (q + 1 - 2*sqrt(q))/n
283 * h_max = (q + 1 + 2*sqrt(q))/n
284 * h_max - h_min = 4*sqrt(q)/n
285 * So if n > 4*sqrt(q) holds, there is only one possible value for h:
286 * h = \lfloor (h_min + h_max)/2 \rceil = \lfloor (q + 1)/n \rceil
288 * Otherwise, zero cofactor and return success.
290 static int ec_guess_cofactor(EC_GROUP *group) {
296 * If the cofactor is too large, we cannot guess it.
297 * The RHS of below is a strict overestimate of lg(4 * sqrt(q))
299 if (BN_num_bits(group->order) <= (BN_num_bits(group->field) + 1) / 2 + 3) {
301 BN_zero(group->cofactor);
306 if ((ctx = BN_CTX_new_ex(group->libctx)) == NULL)
310 if ((q = BN_CTX_get(ctx)) == NULL)
313 /* set q = 2**m for binary fields; q = p otherwise */
314 if (group->meth->field_type == NID_X9_62_characteristic_two_field) {
316 if (!BN_set_bit(q, BN_num_bits(group->field) - 1))
319 if (!BN_copy(q, group->field))
323 /* compute h = \lfloor (q + 1)/n \rceil = \lfloor (q + 1 + n/2)/n \rfloor */
324 if (!BN_rshift1(group->cofactor, group->order) /* n/2 */
325 || !BN_add(group->cofactor, group->cofactor, q) /* q + n/2 */
327 || !BN_add(group->cofactor, group->cofactor, BN_value_one())
328 /* (q + 1 + n/2)/n */
329 || !BN_div(group->cofactor, NULL, group->cofactor, group->order, ctx))
338 int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
339 const BIGNUM *order, const BIGNUM *cofactor)
341 if (generator == NULL) {
342 ECerr(EC_F_EC_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER);
346 /* require group->field >= 1 */
347 if (group->field == NULL || BN_is_zero(group->field)
348 || BN_is_negative(group->field)) {
349 ECerr(EC_F_EC_GROUP_SET_GENERATOR, EC_R_INVALID_FIELD);
354 * - require order >= 1
355 * - enforce upper bound due to Hasse thm: order can be no more than one bit
356 * longer than field cardinality
358 if (order == NULL || BN_is_zero(order) || BN_is_negative(order)
359 || BN_num_bits(order) > BN_num_bits(group->field) + 1) {
360 ECerr(EC_F_EC_GROUP_SET_GENERATOR, EC_R_INVALID_GROUP_ORDER);
365 * Unfortunately the cofactor is an optional field in many standards.
366 * Internally, the lib uses 0 cofactor as a marker for "unknown cofactor".
367 * So accept cofactor == NULL or cofactor >= 0.
369 if (cofactor != NULL && BN_is_negative(cofactor)) {
370 ECerr(EC_F_EC_GROUP_SET_GENERATOR, EC_R_UNKNOWN_COFACTOR);
374 if (group->generator == NULL) {
375 group->generator = EC_POINT_new(group);
376 if (group->generator == NULL)
379 if (!EC_POINT_copy(group->generator, generator))
382 if (!BN_copy(group->order, order))
385 /* Either take the provided positive cofactor, or try to compute it */
386 if (cofactor != NULL && !BN_is_zero(cofactor)) {
387 if (!BN_copy(group->cofactor, cofactor))
389 } else if (!ec_guess_cofactor(group)) {
390 BN_zero(group->cofactor);
395 * Some groups have an order with
396 * factors of two, which makes the Montgomery setup fail.
397 * |group->mont_data| will be NULL in this case.
399 if (BN_is_odd(group->order)) {
400 return ec_precompute_mont_data(group);
403 BN_MONT_CTX_free(group->mont_data);
404 group->mont_data = NULL;
408 const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
410 return group->generator;
413 BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group)
415 return group->mont_data;
418 int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
420 if (group->order == NULL)
422 if (!BN_copy(order, group->order))
425 return !BN_is_zero(order);
428 const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group)
433 int EC_GROUP_order_bits(const EC_GROUP *group)
435 return group->meth->group_order_bits(group);
438 int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
442 if (group->cofactor == NULL)
444 if (!BN_copy(cofactor, group->cofactor))
447 return !BN_is_zero(group->cofactor);
450 const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group)
452 return group->cofactor;
455 void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)
457 group->curve_name = nid;
460 int EC_GROUP_get_curve_name(const EC_GROUP *group)
462 return group->curve_name;
465 const BIGNUM *EC_GROUP_get0_field(const EC_GROUP *group)
470 void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
472 group->asn1_flag = flag;
475 int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
477 return group->asn1_flag;
480 void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
481 point_conversion_form_t form)
483 group->asn1_form = form;
486 point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP
489 return group->asn1_form;
492 size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
494 OPENSSL_free(group->seed);
501 if ((group->seed = OPENSSL_malloc(len)) == NULL) {
502 ECerr(EC_F_EC_GROUP_SET_SEED, ERR_R_MALLOC_FAILURE);
505 memcpy(group->seed, p, len);
506 group->seed_len = len;
511 unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
516 size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
518 return group->seed_len;
521 int EC_GROUP_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
522 const BIGNUM *b, BN_CTX *ctx)
524 if (group->meth->group_set_curve == 0) {
525 ECerr(EC_F_EC_GROUP_SET_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
528 return group->meth->group_set_curve(group, p, a, b, ctx);
531 int EC_GROUP_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b,
534 if (group->meth->group_get_curve == NULL) {
535 ECerr(EC_F_EC_GROUP_GET_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
538 return group->meth->group_get_curve(group, p, a, b, ctx);
542 int EC_GROUP_set_curve_GFp(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_GFp(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);
554 # ifndef OPENSSL_NO_EC2M
555 int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
556 const BIGNUM *b, BN_CTX *ctx)
558 return EC_GROUP_set_curve(group, p, a, b, ctx);
561 int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
562 BIGNUM *b, BN_CTX *ctx)
564 return EC_GROUP_get_curve(group, p, a, b, ctx);
569 int EC_GROUP_get_degree(const EC_GROUP *group)
571 if (group->meth->group_get_degree == 0) {
572 ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
575 return group->meth->group_get_degree(group);
578 int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
580 if (group->meth->group_check_discriminant == 0) {
581 ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT,
582 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
585 return group->meth->group_check_discriminant(group, ctx);
588 int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
591 BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
593 BN_CTX *ctx_new = NULL;
596 ctx_new = ctx = BN_CTX_new();
601 /* compare the field types */
602 if (EC_METHOD_get_field_type(EC_GROUP_method_of(a)) !=
603 EC_METHOD_get_field_type(EC_GROUP_method_of(b)))
605 /* compare the curve name (if present in both) */
606 if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
607 EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
609 if (a->meth->flags & EC_FLAGS_CUSTOM_CURVE)
613 a1 = BN_CTX_get(ctx);
614 a2 = BN_CTX_get(ctx);
615 a3 = BN_CTX_get(ctx);
616 b1 = BN_CTX_get(ctx);
617 b2 = BN_CTX_get(ctx);
618 b3 = BN_CTX_get(ctx);
622 BN_CTX_free(ctx_new);
628 * XXX This approach assumes that the external representation of curves
629 * over the same field type is the same.
631 if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
632 !b->meth->group_get_curve(b, b1, b2, b3, ctx))
635 /* return 1 if the curve parameters are different */
636 if (r || BN_cmp(a1, b1) != 0 || BN_cmp(a2, b2) != 0 || BN_cmp(a3, b3) != 0)
639 /* XXX EC_POINT_cmp() assumes that the methods are equal */
640 /* return 1 if the generators are different */
641 if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
642 EC_GROUP_get0_generator(b), ctx) != 0)
646 const BIGNUM *ao, *bo, *ac, *bc;
647 /* compare the orders */
648 ao = EC_GROUP_get0_order(a);
649 bo = EC_GROUP_get0_order(b);
650 if (ao == NULL || bo == NULL) {
651 /* return an error if either order is NULL */
655 if (BN_cmp(ao, bo) != 0) {
656 /* return 1 if orders are different */
661 * It gets here if the curve parameters and generator matched.
662 * Now check the optional cofactors (if both are present).
664 ac = EC_GROUP_get0_cofactor(a);
665 bc = EC_GROUP_get0_cofactor(b);
666 /* Returns 1 (mismatch) if both cofactors are specified and different */
667 if (!BN_is_zero(ac) && !BN_is_zero(bc) && BN_cmp(ac, bc) != 0)
669 /* Returns 0 if the parameters matched */
674 BN_CTX_free(ctx_new);
679 /* functions for EC_POINT objects */
681 EC_POINT *EC_POINT_new(const EC_GROUP *group)
686 ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER);
689 if (group->meth->point_init == NULL) {
690 ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
694 ret = OPENSSL_zalloc(sizeof(*ret));
696 ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE);
700 ret->meth = group->meth;
701 ret->curve_name = group->curve_name;
703 if (!ret->meth->point_init(ret)) {
711 void EC_POINT_free(EC_POINT *point)
716 if (point->meth->point_finish != 0)
717 point->meth->point_finish(point);
721 void EC_POINT_clear_free(EC_POINT *point)
726 if (point->meth->point_clear_finish != 0)
727 point->meth->point_clear_finish(point);
728 else if (point->meth->point_finish != 0)
729 point->meth->point_finish(point);
730 OPENSSL_clear_free(point, sizeof(*point));
733 int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)
735 if (dest->meth->point_copy == 0) {
736 ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
739 if (dest->meth != src->meth
740 || (dest->curve_name != src->curve_name
741 && dest->curve_name != 0
742 && src->curve_name != 0)) {
743 ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS);
748 return dest->meth->point_copy(dest, src);
751 EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
759 t = EC_POINT_new(group);
762 r = EC_POINT_copy(t, a);
770 const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
775 int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
777 if (group->meth->point_set_to_infinity == 0) {
778 ECerr(EC_F_EC_POINT_SET_TO_INFINITY,
779 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
782 if (group->meth != point->meth) {
783 ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
786 return group->meth->point_set_to_infinity(group, point);
789 int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
790 EC_POINT *point, const BIGNUM *x,
791 const BIGNUM *y, const BIGNUM *z,
794 if (group->meth->point_set_Jprojective_coordinates_GFp == 0) {
795 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
796 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
799 if (!ec_point_is_compat(point, group)) {
800 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
801 EC_R_INCOMPATIBLE_OBJECTS);
804 return group->meth->point_set_Jprojective_coordinates_GFp(group, point, x,
808 int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
809 const EC_POINT *point, BIGNUM *x,
810 BIGNUM *y, BIGNUM *z,
813 if (group->meth->point_get_Jprojective_coordinates_GFp == 0) {
814 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
815 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
818 if (!ec_point_is_compat(point, group)) {
819 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
820 EC_R_INCOMPATIBLE_OBJECTS);
823 return group->meth->point_get_Jprojective_coordinates_GFp(group, point, x,
827 int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point,
828 const BIGNUM *x, const BIGNUM *y,
831 if (group->meth->point_set_affine_coordinates == NULL) {
832 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES,
833 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
836 if (!ec_point_is_compat(point, group)) {
837 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, EC_R_INCOMPATIBLE_OBJECTS);
840 if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx))
843 if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {
844 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, EC_R_POINT_IS_NOT_ON_CURVE);
851 int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
852 EC_POINT *point, const BIGNUM *x,
853 const BIGNUM *y, BN_CTX *ctx)
855 return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
858 # ifndef OPENSSL_NO_EC2M
859 int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,
860 EC_POINT *point, const BIGNUM *x,
861 const BIGNUM *y, BN_CTX *ctx)
863 return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
868 int EC_POINT_get_affine_coordinates(const EC_GROUP *group,
869 const EC_POINT *point, BIGNUM *x, BIGNUM *y,
872 if (group->meth->point_get_affine_coordinates == NULL) {
873 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES,
874 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
877 if (!ec_point_is_compat(point, group)) {
878 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, EC_R_INCOMPATIBLE_OBJECTS);
881 if (EC_POINT_is_at_infinity(group, point)) {
882 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, EC_R_POINT_AT_INFINITY);
885 return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
889 int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
890 const EC_POINT *point, BIGNUM *x,
891 BIGNUM *y, BN_CTX *ctx)
893 return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
896 # ifndef OPENSSL_NO_EC2M
897 int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,
898 const EC_POINT *point, BIGNUM *x,
899 BIGNUM *y, BN_CTX *ctx)
901 return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
906 int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
907 const EC_POINT *b, BN_CTX *ctx)
909 if (group->meth->add == 0) {
910 ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
913 if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)
914 || !ec_point_is_compat(b, group)) {
915 ECerr(EC_F_EC_POINT_ADD, EC_R_INCOMPATIBLE_OBJECTS);
918 return group->meth->add(group, r, a, b, ctx);
921 int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
924 if (group->meth->dbl == 0) {
925 ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
928 if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)) {
929 ECerr(EC_F_EC_POINT_DBL, EC_R_INCOMPATIBLE_OBJECTS);
932 return group->meth->dbl(group, r, a, ctx);
935 int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
937 if (group->meth->invert == 0) {
938 ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
941 if (!ec_point_is_compat(a, group)) {
942 ECerr(EC_F_EC_POINT_INVERT, EC_R_INCOMPATIBLE_OBJECTS);
945 return group->meth->invert(group, a, ctx);
948 int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
950 if (group->meth->is_at_infinity == 0) {
951 ECerr(EC_F_EC_POINT_IS_AT_INFINITY,
952 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
955 if (!ec_point_is_compat(point, group)) {
956 ECerr(EC_F_EC_POINT_IS_AT_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
959 return group->meth->is_at_infinity(group, point);
963 * Check whether an EC_POINT is on the curve or not. Note that the return
964 * value for this function should NOT be treated as a boolean. Return values:
965 * 1: The point is on the curve
966 * 0: The point is not on the curve
967 * -1: An error occurred
969 int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
972 if (group->meth->is_on_curve == 0) {
973 ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
976 if (!ec_point_is_compat(point, group)) {
977 ECerr(EC_F_EC_POINT_IS_ON_CURVE, EC_R_INCOMPATIBLE_OBJECTS);
980 return group->meth->is_on_curve(group, point, ctx);
983 int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
986 if (group->meth->point_cmp == 0) {
987 ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
990 if (!ec_point_is_compat(a, group) || !ec_point_is_compat(b, group)) {
991 ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS);
994 return group->meth->point_cmp(group, a, b, ctx);
997 int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
999 if (group->meth->make_affine == 0) {
1000 ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1003 if (!ec_point_is_compat(point, group)) {
1004 ECerr(EC_F_EC_POINT_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
1007 return group->meth->make_affine(group, point, ctx);
1010 int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
1011 EC_POINT *points[], BN_CTX *ctx)
1015 if (group->meth->points_make_affine == 0) {
1016 ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1019 for (i = 0; i < num; i++) {
1020 if (!ec_point_is_compat(points[i], group)) {
1021 ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
1025 return group->meth->points_make_affine(group, num, points, ctx);
1029 * Functions for point multiplication. If group->meth->mul is 0, we use the
1030 * wNAF-based implementations in ec_mult.c; otherwise we dispatch through
1034 int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
1035 size_t num, const EC_POINT *points[],
1036 const BIGNUM *scalars[], BN_CTX *ctx)
1041 BN_CTX *new_ctx = NULL;
1044 ctx = new_ctx = BN_CTX_secure_new();
1047 ECerr(EC_F_EC_POINTS_MUL, ERR_R_INTERNAL_ERROR);
1051 if ((scalar == NULL) && (num == 0)) {
1052 return EC_POINT_set_to_infinity(group, r);
1055 if (!ec_point_is_compat(r, group)) {
1056 ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
1059 for (i = 0; i < num; i++) {
1060 if (!ec_point_is_compat(points[i], group)) {
1061 ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
1066 if (group->meth->mul != NULL)
1067 ret = group->meth->mul(group, r, scalar, num, points, scalars, ctx);
1070 ret = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
1073 BN_CTX_free(new_ctx);
1078 int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
1079 const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
1081 /* just a convenient interface to EC_POINTs_mul() */
1083 const EC_POINT *points[1];
1084 const BIGNUM *scalars[1];
1087 scalars[0] = p_scalar;
1089 return EC_POINTs_mul(group, r, g_scalar,
1091 && p_scalar != NULL), points, scalars, ctx);
1094 int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
1096 if (group->meth->mul == 0)
1098 return ec_wNAF_precompute_mult(group, ctx);
1100 if (group->meth->precompute_mult != 0)
1101 return group->meth->precompute_mult(group, ctx);
1103 return 1; /* nothing to do, so report success */
1106 int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
1108 if (group->meth->mul == 0)
1110 return ec_wNAF_have_precompute_mult(group);
1112 if (group->meth->have_precompute_mult != 0)
1113 return group->meth->have_precompute_mult(group);
1115 return 0; /* cannot tell whether precomputation has
1120 * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
1121 * returns one on success. On error it returns zero.
1123 static int ec_precompute_mont_data(EC_GROUP *group)
1125 BN_CTX *ctx = BN_CTX_new_ex(group->libctx);
1128 BN_MONT_CTX_free(group->mont_data);
1129 group->mont_data = NULL;
1134 group->mont_data = BN_MONT_CTX_new();
1135 if (group->mont_data == NULL)
1138 if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) {
1139 BN_MONT_CTX_free(group->mont_data);
1140 group->mont_data = NULL;
1153 int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg)
1155 return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
1158 void *EC_KEY_get_ex_data(const EC_KEY *key, int idx)
1160 return CRYPTO_get_ex_data(&key->ex_data, idx);
1164 int ec_group_simple_order_bits(const EC_GROUP *group)
1166 if (group->order == NULL)
1168 return BN_num_bits(group->order);
1171 static int ec_field_inverse_mod_ord(const EC_GROUP *group, BIGNUM *r,
1172 const BIGNUM *x, BN_CTX *ctx)
1177 BN_CTX *new_ctx = NULL;
1180 ctx = new_ctx = BN_CTX_secure_new();
1185 if (group->mont_data == NULL)
1189 if ((e = BN_CTX_get(ctx)) == NULL)
1193 * We want inverse in constant time, therefore we utilize the fact
1194 * order must be prime and use Fermats Little Theorem instead.
1196 if (!BN_set_word(e, 2))
1198 if (!BN_sub(e, group->order, e))
1201 * Exponent e is public.
1202 * No need for scatter-gather or BN_FLG_CONSTTIME.
1204 if (!BN_mod_exp_mont(r, x, e, group->order, ctx, group->mont_data))
1212 BN_CTX_free(new_ctx);
1218 * Default behavior, if group->meth->field_inverse_mod_ord is NULL:
1219 * - When group->order is even, this function returns an error.
1220 * - When group->order is otherwise composite, the correctness
1221 * of the output is not guaranteed.
1222 * - When x is outside the range [1, group->order), the correctness
1223 * of the output is not guaranteed.
1224 * - Otherwise, this function returns the multiplicative inverse in the
1225 * range [1, group->order).
1227 * EC_METHODs must implement their own field_inverse_mod_ord for
1228 * other functionality.
1230 int ec_group_do_inverse_ord(const EC_GROUP *group, BIGNUM *res,
1231 const BIGNUM *x, BN_CTX *ctx)
1233 if (group->meth->field_inverse_mod_ord != NULL)
1234 return group->meth->field_inverse_mod_ord(group, res, x, ctx);
1236 return ec_field_inverse_mod_ord(group, res, x, ctx);
1240 * Coordinate blinding for EC_POINT.
1242 * The underlying EC_METHOD can optionally implement this function:
1243 * underlying implementations should return 0 on errors, or 1 on
1246 * This wrapper returns 1 in case the underlying EC_METHOD does not
1247 * support coordinate blinding.
1249 int ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p, BN_CTX *ctx)
1251 if (group->meth->blind_coordinates == NULL)
1252 return 1; /* ignore if not implemented */
1254 return group->meth->blind_coordinates(group, p, ctx);