2 * Copyright 2001-2016 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 /* Copy precomputed */
144 dest->pre_comp_type = src->pre_comp_type;
145 switch (src->pre_comp_type) {
147 dest->pre_comp.ec = NULL;
150 #ifdef ECP_NISTZ256_ASM
151 dest->pre_comp.nistz256 = EC_nistz256_pre_comp_dup(src->pre_comp.nistz256);
154 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
156 dest->pre_comp.nistp224 = EC_nistp224_pre_comp_dup(src->pre_comp.nistp224);
159 dest->pre_comp.nistp256 = EC_nistp256_pre_comp_dup(src->pre_comp.nistp256);
162 dest->pre_comp.nistp521 = EC_nistp521_pre_comp_dup(src->pre_comp.nistp521);
171 dest->pre_comp.ec = EC_ec_pre_comp_dup(src->pre_comp.ec);
175 if (src->mont_data != NULL) {
176 if (dest->mont_data == NULL) {
177 dest->mont_data = BN_MONT_CTX_new();
178 if (dest->mont_data == NULL)
181 if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data))
184 /* src->generator == NULL */
185 BN_MONT_CTX_free(dest->mont_data);
186 dest->mont_data = NULL;
189 if (src->generator != NULL) {
190 if (dest->generator == NULL) {
191 dest->generator = EC_POINT_new(dest);
192 if (dest->generator == NULL)
195 if (!EC_POINT_copy(dest->generator, src->generator))
198 /* src->generator == NULL */
199 EC_POINT_clear_free(dest->generator);
200 dest->generator = NULL;
203 if ((src->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
204 if (!BN_copy(dest->order, src->order))
206 if (!BN_copy(dest->cofactor, src->cofactor))
210 dest->curve_name = src->curve_name;
211 dest->asn1_flag = src->asn1_flag;
212 dest->asn1_form = src->asn1_form;
215 OPENSSL_free(dest->seed);
216 dest->seed = OPENSSL_malloc(src->seed_len);
217 if (dest->seed == NULL)
219 if (!memcpy(dest->seed, src->seed, src->seed_len))
221 dest->seed_len = src->seed_len;
223 OPENSSL_free(dest->seed);
228 return dest->meth->group_copy(dest, src);
231 EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)
239 if ((t = EC_GROUP_new(a->meth)) == NULL)
241 if (!EC_GROUP_copy(t, a))
254 const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group)
259 int EC_METHOD_get_field_type(const EC_METHOD *meth)
261 return meth->field_type;
264 int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
265 const BIGNUM *order, const BIGNUM *cofactor)
267 if (generator == NULL) {
268 ECerr(EC_F_EC_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER);
272 if (group->generator == NULL) {
273 group->generator = EC_POINT_new(group);
274 if (group->generator == NULL)
277 if (!EC_POINT_copy(group->generator, generator))
281 if (!BN_copy(group->order, order))
284 BN_zero(group->order);
286 if (cofactor != NULL) {
287 if (!BN_copy(group->cofactor, cofactor))
290 BN_zero(group->cofactor);
293 * Some groups have an order with
294 * factors of two, which makes the Montgomery setup fail.
295 * |group->mont_data| will be NULL in this case.
297 if (BN_is_odd(group->order)) {
298 return ec_precompute_mont_data(group);
301 BN_MONT_CTX_free(group->mont_data);
302 group->mont_data = NULL;
306 const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
308 return group->generator;
311 BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group)
313 return group->mont_data;
316 int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
318 if (group->order == NULL)
320 if (!BN_copy(order, group->order))
323 return !BN_is_zero(order);
326 const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group)
331 int EC_GROUP_order_bits(const EC_GROUP *group)
333 OPENSSL_assert(group->meth->group_order_bits != NULL);
334 return group->meth->group_order_bits(group);
337 int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
341 if (group->cofactor == NULL)
343 if (!BN_copy(cofactor, group->cofactor))
346 return !BN_is_zero(group->cofactor);
349 const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group)
351 return group->cofactor;
354 void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)
356 group->curve_name = nid;
359 int EC_GROUP_get_curve_name(const EC_GROUP *group)
361 return group->curve_name;
364 void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
366 group->asn1_flag = flag;
369 int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
371 return group->asn1_flag;
374 void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
375 point_conversion_form_t form)
377 group->asn1_form = form;
380 point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP
383 return group->asn1_form;
386 size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
388 OPENSSL_free(group->seed);
395 if ((group->seed = OPENSSL_malloc(len)) == NULL)
397 memcpy(group->seed, p, len);
398 group->seed_len = len;
403 unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
408 size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
410 return group->seed_len;
413 int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
414 const BIGNUM *b, BN_CTX *ctx)
416 if (group->meth->group_set_curve == 0) {
417 ECerr(EC_F_EC_GROUP_SET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
420 return group->meth->group_set_curve(group, p, a, b, ctx);
423 int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
424 BIGNUM *b, BN_CTX *ctx)
426 if (group->meth->group_get_curve == 0) {
427 ECerr(EC_F_EC_GROUP_GET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
430 return group->meth->group_get_curve(group, p, a, b, ctx);
433 #ifndef OPENSSL_NO_EC2M
434 int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
435 const BIGNUM *b, BN_CTX *ctx)
437 if (group->meth->group_set_curve == 0) {
438 ECerr(EC_F_EC_GROUP_SET_CURVE_GF2M,
439 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
442 return group->meth->group_set_curve(group, p, a, b, ctx);
445 int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
446 BIGNUM *b, BN_CTX *ctx)
448 if (group->meth->group_get_curve == 0) {
449 ECerr(EC_F_EC_GROUP_GET_CURVE_GF2M,
450 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
453 return group->meth->group_get_curve(group, p, a, b, ctx);
457 int EC_GROUP_get_degree(const EC_GROUP *group)
459 if (group->meth->group_get_degree == 0) {
460 ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
463 return group->meth->group_get_degree(group);
466 int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
468 if (group->meth->group_check_discriminant == 0) {
469 ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT,
470 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
473 return group->meth->group_check_discriminant(group, ctx);
476 int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
479 BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
480 BN_CTX *ctx_new = NULL;
482 /* compare the field types */
483 if (EC_METHOD_get_field_type(EC_GROUP_method_of(a)) !=
484 EC_METHOD_get_field_type(EC_GROUP_method_of(b)))
486 /* compare the curve name (if present in both) */
487 if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
488 EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
490 if (a->meth->flags & EC_FLAGS_CUSTOM_CURVE)
494 ctx_new = ctx = BN_CTX_new();
499 a1 = BN_CTX_get(ctx);
500 a2 = BN_CTX_get(ctx);
501 a3 = BN_CTX_get(ctx);
502 b1 = BN_CTX_get(ctx);
503 b2 = BN_CTX_get(ctx);
504 b3 = BN_CTX_get(ctx);
507 BN_CTX_free(ctx_new);
512 * XXX This approach assumes that the external representation of curves
513 * over the same field type is the same.
515 if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
516 !b->meth->group_get_curve(b, b1, b2, b3, ctx))
519 if (r || BN_cmp(a1, b1) || BN_cmp(a2, b2) || BN_cmp(a3, b3))
522 /* XXX EC_POINT_cmp() assumes that the methods are equal */
523 if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
524 EC_GROUP_get0_generator(b), ctx))
528 const BIGNUM *ao, *bo, *ac, *bc;
529 /* compare the order and cofactor */
530 ao = EC_GROUP_get0_order(a);
531 bo = EC_GROUP_get0_order(b);
532 ac = EC_GROUP_get0_cofactor(a);
533 bc = EC_GROUP_get0_cofactor(b);
534 if (ao == NULL || bo == NULL) {
536 BN_CTX_free(ctx_new);
539 if (BN_cmp(ao, bo) || BN_cmp(ac, bc))
544 BN_CTX_free(ctx_new);
549 /* functions for EC_POINT objects */
551 EC_POINT *EC_POINT_new(const EC_GROUP *group)
556 ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER);
559 if (group->meth->point_init == 0) {
560 ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
564 ret = OPENSSL_zalloc(sizeof(*ret));
566 ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE);
570 ret->meth = group->meth;
572 if (!ret->meth->point_init(ret)) {
580 void EC_POINT_free(EC_POINT *point)
585 if (point->meth->point_finish != 0)
586 point->meth->point_finish(point);
590 void EC_POINT_clear_free(EC_POINT *point)
595 if (point->meth->point_clear_finish != 0)
596 point->meth->point_clear_finish(point);
597 else if (point->meth->point_finish != 0)
598 point->meth->point_finish(point);
599 OPENSSL_clear_free(point, sizeof(*point));
602 int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)
604 if (dest->meth->point_copy == 0) {
605 ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
608 if (dest->meth != src->meth) {
609 ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS);
614 return dest->meth->point_copy(dest, src);
617 EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
625 t = EC_POINT_new(group);
628 r = EC_POINT_copy(t, a);
636 const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
641 int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
643 if (group->meth->point_set_to_infinity == 0) {
644 ECerr(EC_F_EC_POINT_SET_TO_INFINITY,
645 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
648 if (group->meth != point->meth) {
649 ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
652 return group->meth->point_set_to_infinity(group, point);
655 int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
656 EC_POINT *point, const BIGNUM *x,
657 const BIGNUM *y, const BIGNUM *z,
660 if (group->meth->point_set_Jprojective_coordinates_GFp == 0) {
661 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
662 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
665 if (group->meth != point->meth) {
666 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
667 EC_R_INCOMPATIBLE_OBJECTS);
670 return group->meth->point_set_Jprojective_coordinates_GFp(group, point, x,
674 int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
675 const EC_POINT *point, BIGNUM *x,
676 BIGNUM *y, BIGNUM *z,
679 if (group->meth->point_get_Jprojective_coordinates_GFp == 0) {
680 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
681 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
684 if (group->meth != point->meth) {
685 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
686 EC_R_INCOMPATIBLE_OBJECTS);
689 return group->meth->point_get_Jprojective_coordinates_GFp(group, point, x,
693 int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
694 EC_POINT *point, const BIGNUM *x,
695 const BIGNUM *y, BN_CTX *ctx)
697 if (group->meth->point_set_affine_coordinates == 0) {
698 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP,
699 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
702 if (group->meth != point->meth) {
703 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP,
704 EC_R_INCOMPATIBLE_OBJECTS);
707 if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx))
710 if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {
711 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP,
712 EC_R_POINT_IS_NOT_ON_CURVE);
718 #ifndef OPENSSL_NO_EC2M
719 int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,
720 EC_POINT *point, const BIGNUM *x,
721 const BIGNUM *y, BN_CTX *ctx)
723 if (group->meth->point_set_affine_coordinates == 0) {
724 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M,
725 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
728 if (group->meth != point->meth) {
729 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M,
730 EC_R_INCOMPATIBLE_OBJECTS);
733 if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx))
736 if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {
737 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M,
738 EC_R_POINT_IS_NOT_ON_CURVE);
745 int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
746 const EC_POINT *point, BIGNUM *x,
747 BIGNUM *y, BN_CTX *ctx)
749 if (group->meth->point_get_affine_coordinates == 0) {
750 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP,
751 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
754 if (group->meth != point->meth) {
755 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP,
756 EC_R_INCOMPATIBLE_OBJECTS);
759 return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
762 #ifndef OPENSSL_NO_EC2M
763 int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,
764 const EC_POINT *point, BIGNUM *x,
765 BIGNUM *y, BN_CTX *ctx)
767 if (group->meth->point_get_affine_coordinates == 0) {
768 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M,
769 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
772 if (group->meth != point->meth) {
773 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M,
774 EC_R_INCOMPATIBLE_OBJECTS);
777 return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
781 int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
782 const EC_POINT *b, BN_CTX *ctx)
784 if (group->meth->add == 0) {
785 ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
788 if ((group->meth != r->meth) || (r->meth != a->meth)
789 || (a->meth != b->meth)) {
790 ECerr(EC_F_EC_POINT_ADD, EC_R_INCOMPATIBLE_OBJECTS);
793 return group->meth->add(group, r, a, b, ctx);
796 int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
799 if (group->meth->dbl == 0) {
800 ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
803 if ((group->meth != r->meth) || (r->meth != a->meth)) {
804 ECerr(EC_F_EC_POINT_DBL, EC_R_INCOMPATIBLE_OBJECTS);
807 return group->meth->dbl(group, r, a, ctx);
810 int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
812 if (group->meth->invert == 0) {
813 ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
816 if (group->meth != a->meth) {
817 ECerr(EC_F_EC_POINT_INVERT, EC_R_INCOMPATIBLE_OBJECTS);
820 return group->meth->invert(group, a, ctx);
823 int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
825 if (group->meth->is_at_infinity == 0) {
826 ECerr(EC_F_EC_POINT_IS_AT_INFINITY,
827 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
830 if (group->meth != point->meth) {
831 ECerr(EC_F_EC_POINT_IS_AT_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
834 return group->meth->is_at_infinity(group, point);
838 * Check whether an EC_POINT is on the curve or not. Note that the return
839 * value for this function should NOT be treated as a boolean. Return values:
840 * 1: The point is on the curve
841 * 0: The point is not on the curve
842 * -1: An error occurred
844 int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
847 if (group->meth->is_on_curve == 0) {
848 ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
851 if (group->meth != point->meth) {
852 ECerr(EC_F_EC_POINT_IS_ON_CURVE, EC_R_INCOMPATIBLE_OBJECTS);
855 return group->meth->is_on_curve(group, point, ctx);
858 int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
861 if (group->meth->point_cmp == 0) {
862 ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
865 if ((group->meth != a->meth) || (a->meth != b->meth)) {
866 ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS);
869 return group->meth->point_cmp(group, a, b, ctx);
872 int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
874 if (group->meth->make_affine == 0) {
875 ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
878 if (group->meth != point->meth) {
879 ECerr(EC_F_EC_POINT_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
882 return group->meth->make_affine(group, point, ctx);
885 int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
886 EC_POINT *points[], BN_CTX *ctx)
890 if (group->meth->points_make_affine == 0) {
891 ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
894 for (i = 0; i < num; i++) {
895 if (group->meth != points[i]->meth) {
896 ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
900 return group->meth->points_make_affine(group, num, points, ctx);
904 * Functions for point multiplication. If group->meth->mul is 0, we use the
905 * wNAF-based implementations in ec_mult.c; otherwise we dispatch through
909 int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
910 size_t num, const EC_POINT *points[],
911 const BIGNUM *scalars[], BN_CTX *ctx)
913 if (group->meth->mul == 0)
915 return ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
917 return group->meth->mul(group, r, scalar, num, points, scalars, ctx);
920 int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
921 const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
923 /* just a convenient interface to EC_POINTs_mul() */
925 const EC_POINT *points[1];
926 const BIGNUM *scalars[1];
929 scalars[0] = p_scalar;
931 return EC_POINTs_mul(group, r, g_scalar,
933 && p_scalar != NULL), points, scalars, ctx);
936 int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
938 if (group->meth->mul == 0)
940 return ec_wNAF_precompute_mult(group, ctx);
942 if (group->meth->precompute_mult != 0)
943 return group->meth->precompute_mult(group, ctx);
945 return 1; /* nothing to do, so report success */
948 int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
950 if (group->meth->mul == 0)
952 return ec_wNAF_have_precompute_mult(group);
954 if (group->meth->have_precompute_mult != 0)
955 return group->meth->have_precompute_mult(group);
957 return 0; /* cannot tell whether precomputation has
962 * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
963 * returns one on success. On error it returns zero.
965 int ec_precompute_mont_data(EC_GROUP *group)
967 BN_CTX *ctx = BN_CTX_new();
970 BN_MONT_CTX_free(group->mont_data);
971 group->mont_data = NULL;
976 group->mont_data = BN_MONT_CTX_new();
977 if (group->mont_data == NULL)
980 if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) {
981 BN_MONT_CTX_free(group->mont_data);
982 group->mont_data = NULL;
994 int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg)
996 return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
999 void *EC_KEY_get_ex_data(const EC_KEY *key, int idx)
1001 return CRYPTO_get_ex_data(&key->ex_data, idx);
1004 int ec_group_simple_order_bits(const EC_GROUP *group)
1006 if (group->order == NULL)
1008 return BN_num_bits(group->order);