2 * Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 /* ====================================================================
11 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
12 * Binary polynomial ECC support in OpenSSL originally developed by
13 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
18 #include <openssl/err.h>
19 #include <openssl/opensslv.h>
23 /* functions for EC_GROUP objects */
25 EC_GROUP *EC_GROUP_new(const EC_METHOD *meth)
30 ECerr(EC_F_EC_GROUP_NEW, EC_R_SLOT_FULL);
33 if (meth->group_init == 0) {
34 ECerr(EC_F_EC_GROUP_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
38 ret = OPENSSL_zalloc(sizeof(*ret));
40 ECerr(EC_F_EC_GROUP_NEW, ERR_R_MALLOC_FAILURE);
45 if ((ret->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
46 ret->order = BN_new();
47 if (ret->order == NULL)
49 ret->cofactor = BN_new();
50 if (ret->cofactor == NULL)
53 ret->asn1_flag = OPENSSL_EC_NAMED_CURVE;
54 ret->asn1_form = POINT_CONVERSION_UNCOMPRESSED;
55 if (!meth->group_init(ret))
61 BN_free(ret->cofactor);
66 void EC_pre_comp_free(EC_GROUP *group)
68 switch (group->pre_comp_type) {
72 #ifdef ECP_NISTZ256_ASM
73 EC_nistz256_pre_comp_free(group->pre_comp.nistz256);
76 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
78 EC_nistp224_pre_comp_free(group->pre_comp.nistp224);
81 EC_nistp256_pre_comp_free(group->pre_comp.nistp256);
84 EC_nistp521_pre_comp_free(group->pre_comp.nistp521);
93 EC_ec_pre_comp_free(group->pre_comp.ec);
96 group->pre_comp.ec = NULL;
99 void EC_GROUP_free(EC_GROUP *group)
104 if (group->meth->group_finish != 0)
105 group->meth->group_finish(group);
107 EC_pre_comp_free(group);
108 BN_MONT_CTX_free(group->mont_data);
109 EC_POINT_free(group->generator);
110 BN_free(group->order);
111 BN_free(group->cofactor);
112 OPENSSL_free(group->seed);
116 void EC_GROUP_clear_free(EC_GROUP *group)
121 if (group->meth->group_clear_finish != 0)
122 group->meth->group_clear_finish(group);
123 else if (group->meth->group_finish != 0)
124 group->meth->group_finish(group);
126 EC_pre_comp_free(group);
127 BN_MONT_CTX_free(group->mont_data);
128 EC_POINT_clear_free(group->generator);
129 BN_clear_free(group->order);
130 BN_clear_free(group->cofactor);
131 OPENSSL_clear_free(group->seed, group->seed_len);
132 OPENSSL_clear_free(group, sizeof(*group));
135 int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)
137 if (dest->meth->group_copy == 0) {
138 ECerr(EC_F_EC_GROUP_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
141 if (dest->meth != src->meth) {
142 ECerr(EC_F_EC_GROUP_COPY, EC_R_INCOMPATIBLE_OBJECTS);
148 /* Copy precomputed */
149 dest->pre_comp_type = src->pre_comp_type;
150 switch (src->pre_comp_type) {
152 dest->pre_comp.ec = NULL;
155 #ifdef ECP_NISTZ256_ASM
156 dest->pre_comp.nistz256 = EC_nistz256_pre_comp_dup(src->pre_comp.nistz256);
159 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
161 dest->pre_comp.nistp224 = EC_nistp224_pre_comp_dup(src->pre_comp.nistp224);
164 dest->pre_comp.nistp256 = EC_nistp256_pre_comp_dup(src->pre_comp.nistp256);
167 dest->pre_comp.nistp521 = EC_nistp521_pre_comp_dup(src->pre_comp.nistp521);
176 dest->pre_comp.ec = EC_ec_pre_comp_dup(src->pre_comp.ec);
180 if (src->mont_data != NULL) {
181 if (dest->mont_data == NULL) {
182 dest->mont_data = BN_MONT_CTX_new();
183 if (dest->mont_data == NULL)
186 if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data))
189 /* src->generator == NULL */
190 BN_MONT_CTX_free(dest->mont_data);
191 dest->mont_data = NULL;
194 if (src->generator != NULL) {
195 if (dest->generator == NULL) {
196 dest->generator = EC_POINT_new(dest);
197 if (dest->generator == NULL)
200 if (!EC_POINT_copy(dest->generator, src->generator))
203 /* src->generator == NULL */
204 EC_POINT_clear_free(dest->generator);
205 dest->generator = NULL;
208 if ((src->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
209 if (!BN_copy(dest->order, src->order))
211 if (!BN_copy(dest->cofactor, src->cofactor))
215 dest->curve_name = src->curve_name;
216 dest->asn1_flag = src->asn1_flag;
217 dest->asn1_form = src->asn1_form;
220 OPENSSL_free(dest->seed);
221 dest->seed = OPENSSL_malloc(src->seed_len);
222 if (dest->seed == NULL)
224 if (!memcpy(dest->seed, src->seed, src->seed_len))
226 dest->seed_len = src->seed_len;
228 OPENSSL_free(dest->seed);
233 return dest->meth->group_copy(dest, src);
236 EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)
244 if ((t = EC_GROUP_new(a->meth)) == NULL)
246 if (!EC_GROUP_copy(t, a))
259 const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group)
264 int EC_METHOD_get_field_type(const EC_METHOD *meth)
266 return meth->field_type;
269 int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
270 const BIGNUM *order, const BIGNUM *cofactor)
272 if (generator == NULL) {
273 ECerr(EC_F_EC_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER);
277 if (group->generator == NULL) {
278 group->generator = EC_POINT_new(group);
279 if (group->generator == NULL)
282 if (!EC_POINT_copy(group->generator, generator))
286 if (!BN_copy(group->order, order))
289 BN_zero(group->order);
291 if (cofactor != NULL) {
292 if (!BN_copy(group->cofactor, cofactor))
295 BN_zero(group->cofactor);
298 * Some groups have an order with
299 * factors of two, which makes the Montgomery setup fail.
300 * |group->mont_data| will be NULL in this case.
302 if (BN_is_odd(group->order)) {
303 return ec_precompute_mont_data(group);
306 BN_MONT_CTX_free(group->mont_data);
307 group->mont_data = NULL;
311 const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
313 return group->generator;
316 BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group)
318 return group->mont_data;
321 int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
323 if (group->order == NULL)
325 if (!BN_copy(order, group->order))
328 return !BN_is_zero(order);
331 const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group)
336 int EC_GROUP_order_bits(const EC_GROUP *group)
338 OPENSSL_assert(group->meth->group_order_bits != NULL);
339 return group->meth->group_order_bits(group);
342 int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
346 if (group->cofactor == NULL)
348 if (!BN_copy(cofactor, group->cofactor))
351 return !BN_is_zero(group->cofactor);
354 const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group)
356 return group->cofactor;
359 void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)
361 group->curve_name = nid;
364 int EC_GROUP_get_curve_name(const EC_GROUP *group)
366 return group->curve_name;
369 void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
371 group->asn1_flag = flag;
374 int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
376 return group->asn1_flag;
379 void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
380 point_conversion_form_t form)
382 group->asn1_form = form;
385 point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP
388 return group->asn1_form;
391 size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
393 OPENSSL_free(group->seed);
400 if ((group->seed = OPENSSL_malloc(len)) == NULL)
402 memcpy(group->seed, p, len);
403 group->seed_len = len;
408 unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
413 size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
415 return group->seed_len;
418 int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
419 const BIGNUM *b, BN_CTX *ctx)
421 if (group->meth->group_set_curve == 0) {
422 ECerr(EC_F_EC_GROUP_SET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
425 return group->meth->group_set_curve(group, p, a, b, ctx);
428 int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
429 BIGNUM *b, BN_CTX *ctx)
431 if (group->meth->group_get_curve == 0) {
432 ECerr(EC_F_EC_GROUP_GET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
435 return group->meth->group_get_curve(group, p, a, b, ctx);
438 #ifndef OPENSSL_NO_EC2M
439 int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
440 const BIGNUM *b, BN_CTX *ctx)
442 if (group->meth->group_set_curve == 0) {
443 ECerr(EC_F_EC_GROUP_SET_CURVE_GF2M,
444 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
447 return group->meth->group_set_curve(group, p, a, b, ctx);
450 int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
451 BIGNUM *b, BN_CTX *ctx)
453 if (group->meth->group_get_curve == 0) {
454 ECerr(EC_F_EC_GROUP_GET_CURVE_GF2M,
455 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
458 return group->meth->group_get_curve(group, p, a, b, ctx);
462 int EC_GROUP_get_degree(const EC_GROUP *group)
464 if (group->meth->group_get_degree == 0) {
465 ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
468 return group->meth->group_get_degree(group);
471 int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
473 if (group->meth->group_check_discriminant == 0) {
474 ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT,
475 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
478 return group->meth->group_check_discriminant(group, ctx);
481 int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
484 BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
485 BN_CTX *ctx_new = NULL;
487 /* compare the field types */
488 if (EC_METHOD_get_field_type(EC_GROUP_method_of(a)) !=
489 EC_METHOD_get_field_type(EC_GROUP_method_of(b)))
491 /* compare the curve name (if present in both) */
492 if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
493 EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
495 if (a->meth->flags & EC_FLAGS_CUSTOM_CURVE)
499 ctx_new = ctx = BN_CTX_new();
504 a1 = BN_CTX_get(ctx);
505 a2 = BN_CTX_get(ctx);
506 a3 = BN_CTX_get(ctx);
507 b1 = BN_CTX_get(ctx);
508 b2 = BN_CTX_get(ctx);
509 b3 = BN_CTX_get(ctx);
512 BN_CTX_free(ctx_new);
517 * XXX This approach assumes that the external representation of curves
518 * over the same field type is the same.
520 if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
521 !b->meth->group_get_curve(b, b1, b2, b3, ctx))
524 if (r || BN_cmp(a1, b1) || BN_cmp(a2, b2) || BN_cmp(a3, b3))
527 /* XXX EC_POINT_cmp() assumes that the methods are equal */
528 if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
529 EC_GROUP_get0_generator(b), ctx))
533 const BIGNUM *ao, *bo, *ac, *bc;
534 /* compare the order and cofactor */
535 ao = EC_GROUP_get0_order(a);
536 bo = EC_GROUP_get0_order(b);
537 ac = EC_GROUP_get0_cofactor(a);
538 bc = EC_GROUP_get0_cofactor(b);
539 if (ao == NULL || bo == NULL) {
541 BN_CTX_free(ctx_new);
544 if (BN_cmp(ao, bo) || BN_cmp(ac, bc))
549 BN_CTX_free(ctx_new);
554 /* functions for EC_POINT objects */
556 EC_POINT *EC_POINT_new(const EC_GROUP *group)
561 ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER);
564 if (group->meth->point_init == 0) {
565 ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
569 ret = OPENSSL_zalloc(sizeof(*ret));
571 ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE);
575 ret->meth = group->meth;
577 if (!ret->meth->point_init(ret)) {
585 void EC_POINT_free(EC_POINT *point)
590 if (point->meth->point_finish != 0)
591 point->meth->point_finish(point);
595 void EC_POINT_clear_free(EC_POINT *point)
600 if (point->meth->point_clear_finish != 0)
601 point->meth->point_clear_finish(point);
602 else if (point->meth->point_finish != 0)
603 point->meth->point_finish(point);
604 OPENSSL_clear_free(point, sizeof(*point));
607 int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)
609 if (dest->meth->point_copy == 0) {
610 ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
613 if (dest->meth != src->meth) {
614 ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS);
619 return dest->meth->point_copy(dest, src);
622 EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
630 t = EC_POINT_new(group);
633 r = EC_POINT_copy(t, a);
641 const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
646 int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
648 if (group->meth->point_set_to_infinity == 0) {
649 ECerr(EC_F_EC_POINT_SET_TO_INFINITY,
650 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
653 if (group->meth != point->meth) {
654 ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
657 return group->meth->point_set_to_infinity(group, point);
660 int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
661 EC_POINT *point, const BIGNUM *x,
662 const BIGNUM *y, const BIGNUM *z,
665 if (group->meth->point_set_Jprojective_coordinates_GFp == 0) {
666 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
667 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
670 if (group->meth != point->meth) {
671 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
672 EC_R_INCOMPATIBLE_OBJECTS);
675 return group->meth->point_set_Jprojective_coordinates_GFp(group, point, x,
679 int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
680 const EC_POINT *point, BIGNUM *x,
681 BIGNUM *y, BIGNUM *z,
684 if (group->meth->point_get_Jprojective_coordinates_GFp == 0) {
685 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
686 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
689 if (group->meth != point->meth) {
690 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
691 EC_R_INCOMPATIBLE_OBJECTS);
694 return group->meth->point_get_Jprojective_coordinates_GFp(group, point, x,
698 int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
699 EC_POINT *point, const BIGNUM *x,
700 const BIGNUM *y, BN_CTX *ctx)
702 if (group->meth->point_set_affine_coordinates == 0) {
703 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP,
704 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
707 if (group->meth != point->meth) {
708 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP,
709 EC_R_INCOMPATIBLE_OBJECTS);
712 if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx))
715 if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {
716 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP,
717 EC_R_POINT_IS_NOT_ON_CURVE);
723 #ifndef OPENSSL_NO_EC2M
724 int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,
725 EC_POINT *point, const BIGNUM *x,
726 const BIGNUM *y, BN_CTX *ctx)
728 if (group->meth->point_set_affine_coordinates == 0) {
729 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M,
730 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
733 if (group->meth != point->meth) {
734 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M,
735 EC_R_INCOMPATIBLE_OBJECTS);
738 if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx))
741 if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {
742 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M,
743 EC_R_POINT_IS_NOT_ON_CURVE);
750 int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
751 const EC_POINT *point, BIGNUM *x,
752 BIGNUM *y, BN_CTX *ctx)
754 if (group->meth->point_get_affine_coordinates == 0) {
755 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP,
756 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
759 if (group->meth != point->meth) {
760 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP,
761 EC_R_INCOMPATIBLE_OBJECTS);
764 return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
767 #ifndef OPENSSL_NO_EC2M
768 int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,
769 const EC_POINT *point, BIGNUM *x,
770 BIGNUM *y, BN_CTX *ctx)
772 if (group->meth->point_get_affine_coordinates == 0) {
773 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M,
774 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
777 if (group->meth != point->meth) {
778 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M,
779 EC_R_INCOMPATIBLE_OBJECTS);
782 return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
786 int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
787 const EC_POINT *b, BN_CTX *ctx)
789 if (group->meth->add == 0) {
790 ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
793 if ((group->meth != r->meth) || (r->meth != a->meth)
794 || (a->meth != b->meth)) {
795 ECerr(EC_F_EC_POINT_ADD, EC_R_INCOMPATIBLE_OBJECTS);
798 return group->meth->add(group, r, a, b, ctx);
801 int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
804 if (group->meth->dbl == 0) {
805 ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
808 if ((group->meth != r->meth) || (r->meth != a->meth)) {
809 ECerr(EC_F_EC_POINT_DBL, EC_R_INCOMPATIBLE_OBJECTS);
812 return group->meth->dbl(group, r, a, ctx);
815 int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
817 if (group->meth->invert == 0) {
818 ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
821 if (group->meth != a->meth) {
822 ECerr(EC_F_EC_POINT_INVERT, EC_R_INCOMPATIBLE_OBJECTS);
825 return group->meth->invert(group, a, ctx);
828 int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
830 if (group->meth->is_at_infinity == 0) {
831 ECerr(EC_F_EC_POINT_IS_AT_INFINITY,
832 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
835 if (group->meth != point->meth) {
836 ECerr(EC_F_EC_POINT_IS_AT_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
839 return group->meth->is_at_infinity(group, point);
843 * Check whether an EC_POINT is on the curve or not. Note that the return
844 * value for this function should NOT be treated as a boolean. Return values:
845 * 1: The point is on the curve
846 * 0: The point is not on the curve
847 * -1: An error occurred
849 int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
852 if (group->meth->is_on_curve == 0) {
853 ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
856 if (group->meth != point->meth) {
857 ECerr(EC_F_EC_POINT_IS_ON_CURVE, EC_R_INCOMPATIBLE_OBJECTS);
860 return group->meth->is_on_curve(group, point, ctx);
863 int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
866 if (group->meth->point_cmp == 0) {
867 ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
870 if ((group->meth != a->meth) || (a->meth != b->meth)) {
871 ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS);
874 return group->meth->point_cmp(group, a, b, ctx);
877 int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
879 if (group->meth->make_affine == 0) {
880 ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
883 if (group->meth != point->meth) {
884 ECerr(EC_F_EC_POINT_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
887 return group->meth->make_affine(group, point, ctx);
890 int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
891 EC_POINT *points[], BN_CTX *ctx)
895 if (group->meth->points_make_affine == 0) {
896 ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
899 for (i = 0; i < num; i++) {
900 if (group->meth != points[i]->meth) {
901 ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
905 return group->meth->points_make_affine(group, num, points, ctx);
909 * Functions for point multiplication. If group->meth->mul is 0, we use the
910 * wNAF-based implementations in ec_mult.c; otherwise we dispatch through
914 int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
915 size_t num, const EC_POINT *points[],
916 const BIGNUM *scalars[], BN_CTX *ctx)
918 if (group->meth->mul == 0)
920 return ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
922 return group->meth->mul(group, r, scalar, num, points, scalars, ctx);
925 int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
926 const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
928 /* just a convenient interface to EC_POINTs_mul() */
930 const EC_POINT *points[1];
931 const BIGNUM *scalars[1];
934 scalars[0] = p_scalar;
936 return EC_POINTs_mul(group, r, g_scalar,
938 && p_scalar != NULL), points, scalars, ctx);
941 int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
943 if (group->meth->mul == 0)
945 return ec_wNAF_precompute_mult(group, ctx);
947 if (group->meth->precompute_mult != 0)
948 return group->meth->precompute_mult(group, ctx);
950 return 1; /* nothing to do, so report success */
953 int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
955 if (group->meth->mul == 0)
957 return ec_wNAF_have_precompute_mult(group);
959 if (group->meth->have_precompute_mult != 0)
960 return group->meth->have_precompute_mult(group);
962 return 0; /* cannot tell whether precomputation has
967 * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
968 * returns one on success. On error it returns zero.
970 int ec_precompute_mont_data(EC_GROUP *group)
972 BN_CTX *ctx = BN_CTX_new();
975 BN_MONT_CTX_free(group->mont_data);
976 group->mont_data = NULL;
981 group->mont_data = BN_MONT_CTX_new();
982 if (group->mont_data == NULL)
985 if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) {
986 BN_MONT_CTX_free(group->mont_data);
987 group->mont_data = NULL;
999 int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg)
1001 return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
1004 void *EC_KEY_get_ex_data(const EC_KEY *key, int idx)
1006 return CRYPTO_get_ex_data(&key->ex_data, idx);
1009 int ec_group_simple_order_bits(const EC_GROUP *group)
1011 if (group->order == NULL)
1013 return BN_num_bits(group->order);