1 /* crypto/ec/ec_lib.c */
3 * Originally written by Bodo Moeller for the OpenSSL project.
5 /* ====================================================================
6 * Copyright (c) 1998-2003 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * openssl-core@openssl.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
58 /* ====================================================================
59 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60 * Binary polynomial ECC support in OpenSSL originally developed by
61 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
66 #include <openssl/err.h>
67 #include <openssl/opensslv.h>
71 /* functions for EC_GROUP objects */
73 EC_GROUP *EC_GROUP_new(const EC_METHOD *meth)
78 ECerr(EC_F_EC_GROUP_NEW, EC_R_SLOT_FULL);
81 if (meth->group_init == 0) {
82 ECerr(EC_F_EC_GROUP_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
86 ret = OPENSSL_zalloc(sizeof(*ret));
88 ECerr(EC_F_EC_GROUP_NEW, ERR_R_MALLOC_FAILURE);
93 ret->order = BN_new();
94 if (ret->order == NULL)
96 ret->cofactor = BN_new();
97 if (ret->cofactor == NULL)
99 ret->asn1_flag = OPENSSL_EC_NAMED_CURVE;
100 ret->asn1_form = POINT_CONVERSION_UNCOMPRESSED;
101 if (!meth->group_init(ret))
107 BN_free(ret->cofactor);
112 static void ec_group_free_precomp(EC_GROUP *group)
114 switch (group->pre_comp_type) {
118 EC_nistz256_pre_comp_free(group->pre_comp.nistz256);
120 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
122 EC_nistp224_pre_comp_free(group->pre_comp.nistp224);
125 EC_nistp256_pre_comp_free(group->pre_comp.nistp256);
128 EC_nistp521_pre_comp_free(group->pre_comp.nistp521);
132 EC_ec_pre_comp_free(group->pre_comp.ec);
135 group->pre_comp.ec = NULL;
138 void EC_GROUP_free(EC_GROUP *group)
143 if (group->meth->group_finish != 0)
144 group->meth->group_finish(group);
146 ec_group_free_precomp(group);
147 BN_MONT_CTX_free(group->mont_data);
148 EC_POINT_free(group->generator);
149 BN_free(group->order);
150 BN_free(group->cofactor);
151 OPENSSL_free(group->seed);
155 void EC_GROUP_clear_free(EC_GROUP *group)
160 if (group->meth->group_clear_finish != 0)
161 group->meth->group_clear_finish(group);
162 else if (group->meth->group_finish != 0)
163 group->meth->group_finish(group);
165 ec_group_free_precomp(group);
166 BN_MONT_CTX_free(group->mont_data);
167 EC_POINT_clear_free(group->generator);
168 BN_clear_free(group->order);
169 BN_clear_free(group->cofactor);
170 OPENSSL_clear_free(group->seed, group->seed_len);
171 OPENSSL_clear_free(group, sizeof(*group));
174 int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)
176 if (dest->meth->group_copy == 0) {
177 ECerr(EC_F_EC_GROUP_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
180 if (dest->meth != src->meth) {
181 ECerr(EC_F_EC_GROUP_COPY, EC_R_INCOMPATIBLE_OBJECTS);
187 /* Copy precomputed */
188 dest->pre_comp_type = src->pre_comp_type;
189 switch (src->pre_comp_type) {
191 dest->pre_comp.ec = NULL;
194 dest->pre_comp.nistz256 = EC_nistz256_pre_comp_dup(src->pre_comp.nistz256);
196 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
198 dest->pre_comp.nistp224 = EC_nistp224_pre_comp_dup(src->pre_comp.nistp224);
201 dest->pre_comp.nistp256 = EC_nistp256_pre_comp_dup(src->pre_comp.nistp256);
204 dest->pre_comp.nistp521 = EC_nistp521_pre_comp_dup(src->pre_comp.nistp521);
208 dest->pre_comp.ec = EC_ec_pre_comp_dup(src->pre_comp.ec);
212 if (src->mont_data != NULL) {
213 if (dest->mont_data == NULL) {
214 dest->mont_data = BN_MONT_CTX_new();
215 if (dest->mont_data == NULL)
218 if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data))
221 /* src->generator == NULL */
222 BN_MONT_CTX_free(dest->mont_data);
223 dest->mont_data = NULL;
226 if (src->generator != NULL) {
227 if (dest->generator == NULL) {
228 dest->generator = EC_POINT_new(dest);
229 if (dest->generator == NULL)
232 if (!EC_POINT_copy(dest->generator, src->generator))
235 /* src->generator == NULL */
236 EC_POINT_clear_free(dest->generator);
237 dest->generator = NULL;
240 if (!BN_copy(dest->order, src->order))
242 if (!BN_copy(dest->cofactor, src->cofactor))
245 dest->curve_name = src->curve_name;
246 dest->asn1_flag = src->asn1_flag;
247 dest->asn1_form = src->asn1_form;
250 OPENSSL_free(dest->seed);
251 dest->seed = OPENSSL_malloc(src->seed_len);
252 if (dest->seed == NULL)
254 if (!memcpy(dest->seed, src->seed, src->seed_len))
256 dest->seed_len = src->seed_len;
258 OPENSSL_free(dest->seed);
263 return dest->meth->group_copy(dest, src);
266 EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)
274 if ((t = EC_GROUP_new(a->meth)) == NULL)
276 if (!EC_GROUP_copy(t, a))
289 const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group)
294 int EC_METHOD_get_field_type(const EC_METHOD *meth)
296 return meth->field_type;
299 int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
300 const BIGNUM *order, const BIGNUM *cofactor)
302 if (generator == NULL) {
303 ECerr(EC_F_EC_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER);
307 if (group->generator == NULL) {
308 group->generator = EC_POINT_new(group);
309 if (group->generator == NULL)
312 if (!EC_POINT_copy(group->generator, generator))
316 if (!BN_copy(group->order, order))
319 BN_zero(group->order);
321 if (cofactor != NULL) {
322 if (!BN_copy(group->cofactor, cofactor))
325 BN_zero(group->cofactor);
328 * We ignore the return value because some groups have an order with
329 * factors of two, which makes the Montgomery setup fail.
330 * |group->mont_data| will be NULL in this case.
332 ec_precompute_mont_data(group);
337 const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
339 return group->generator;
342 BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group)
344 return group->mont_data;
347 int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
349 if (!BN_copy(order, group->order))
352 return !BN_is_zero(order);
355 int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
358 if (!BN_copy(cofactor, group->cofactor))
361 return !BN_is_zero(group->cofactor);
364 void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)
366 group->curve_name = nid;
369 int EC_GROUP_get_curve_name(const EC_GROUP *group)
371 return group->curve_name;
374 void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
376 group->asn1_flag = flag;
379 int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
381 return group->asn1_flag;
384 void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
385 point_conversion_form_t form)
387 group->asn1_form = form;
390 point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP
393 return group->asn1_form;
396 size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
398 OPENSSL_free(group->seed);
405 if ((group->seed = OPENSSL_malloc(len)) == NULL)
407 memcpy(group->seed, p, len);
408 group->seed_len = len;
413 unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
418 size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
420 return group->seed_len;
423 int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
424 const BIGNUM *b, BN_CTX *ctx)
426 if (group->meth->group_set_curve == 0) {
427 ECerr(EC_F_EC_GROUP_SET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
430 return group->meth->group_set_curve(group, p, a, b, ctx);
433 int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
434 BIGNUM *b, BN_CTX *ctx)
436 if (group->meth->group_get_curve == 0) {
437 ECerr(EC_F_EC_GROUP_GET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
440 return group->meth->group_get_curve(group, p, a, b, ctx);
443 #ifndef OPENSSL_NO_EC2M
444 int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
445 const BIGNUM *b, BN_CTX *ctx)
447 if (group->meth->group_set_curve == 0) {
448 ECerr(EC_F_EC_GROUP_SET_CURVE_GF2M,
449 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
452 return group->meth->group_set_curve(group, p, a, b, ctx);
455 int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
456 BIGNUM *b, BN_CTX *ctx)
458 if (group->meth->group_get_curve == 0) {
459 ECerr(EC_F_EC_GROUP_GET_CURVE_GF2M,
460 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
463 return group->meth->group_get_curve(group, p, a, b, ctx);
467 int EC_GROUP_get_degree(const EC_GROUP *group)
469 if (group->meth->group_get_degree == 0) {
470 ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
473 return group->meth->group_get_degree(group);
476 int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
478 if (group->meth->group_check_discriminant == 0) {
479 ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT,
480 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
483 return group->meth->group_check_discriminant(group, ctx);
486 int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
489 BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
490 BN_CTX *ctx_new = NULL;
492 /* compare the field types */
493 if (EC_METHOD_get_field_type(EC_GROUP_method_of(a)) !=
494 EC_METHOD_get_field_type(EC_GROUP_method_of(b)))
496 /* compare the curve name (if present in both) */
497 if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
498 EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
502 ctx_new = ctx = BN_CTX_new();
507 a1 = BN_CTX_get(ctx);
508 a2 = BN_CTX_get(ctx);
509 a3 = BN_CTX_get(ctx);
510 b1 = BN_CTX_get(ctx);
511 b2 = BN_CTX_get(ctx);
512 b3 = BN_CTX_get(ctx);
515 BN_CTX_free(ctx_new);
520 * XXX This approach assumes that the external representation of curves
521 * over the same field type is the same.
523 if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
524 !b->meth->group_get_curve(b, b1, b2, b3, ctx))
527 if (r || BN_cmp(a1, b1) || BN_cmp(a2, b2) || BN_cmp(a3, b3))
530 /* XXX EC_POINT_cmp() assumes that the methods are equal */
531 if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
532 EC_GROUP_get0_generator(b), ctx))
536 /* compare the order and cofactor */
537 if (!EC_GROUP_get_order(a, a1, ctx) ||
538 !EC_GROUP_get_order(b, b1, ctx) ||
539 !EC_GROUP_get_cofactor(a, a2, ctx) ||
540 !EC_GROUP_get_cofactor(b, b2, ctx)) {
542 BN_CTX_free(ctx_new);
545 if (BN_cmp(a1, b1) || BN_cmp(a2, b2))
550 BN_CTX_free(ctx_new);
555 /* functions for EC_POINT objects */
557 EC_POINT *EC_POINT_new(const EC_GROUP *group)
562 ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER);
565 if (group->meth->point_init == 0) {
566 ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
570 ret = OPENSSL_malloc(sizeof(*ret));
572 ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE);
576 ret->meth = group->meth;
578 if (!ret->meth->point_init(ret)) {
586 void EC_POINT_free(EC_POINT *point)
591 if (point->meth->point_finish != 0)
592 point->meth->point_finish(point);
596 void EC_POINT_clear_free(EC_POINT *point)
601 if (point->meth->point_clear_finish != 0)
602 point->meth->point_clear_finish(point);
603 else if (point->meth->point_finish != 0)
604 point->meth->point_finish(point);
605 OPENSSL_clear_free(point, sizeof(*point));
608 int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)
610 if (dest->meth->point_copy == 0) {
611 ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
614 if (dest->meth != src->meth) {
615 ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS);
620 return dest->meth->point_copy(dest, src);
623 EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
631 t = EC_POINT_new(group);
634 r = EC_POINT_copy(t, a);
642 const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
647 int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
649 if (group->meth->point_set_to_infinity == 0) {
650 ECerr(EC_F_EC_POINT_SET_TO_INFINITY,
651 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
654 if (group->meth != point->meth) {
655 ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
658 return group->meth->point_set_to_infinity(group, point);
661 int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
662 EC_POINT *point, const BIGNUM *x,
663 const BIGNUM *y, const BIGNUM *z,
666 if (group->meth->point_set_Jprojective_coordinates_GFp == 0) {
667 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
668 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
671 if (group->meth != point->meth) {
672 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
673 EC_R_INCOMPATIBLE_OBJECTS);
676 return group->meth->point_set_Jprojective_coordinates_GFp(group, point, x,
680 int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
681 const EC_POINT *point, BIGNUM *x,
682 BIGNUM *y, BIGNUM *z,
685 if (group->meth->point_get_Jprojective_coordinates_GFp == 0) {
686 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
687 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
690 if (group->meth != point->meth) {
691 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
692 EC_R_INCOMPATIBLE_OBJECTS);
695 return group->meth->point_get_Jprojective_coordinates_GFp(group, point, x,
699 int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
700 EC_POINT *point, const BIGNUM *x,
701 const BIGNUM *y, BN_CTX *ctx)
703 if (group->meth->point_set_affine_coordinates == 0) {
704 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP,
705 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
708 if (group->meth != point->meth) {
709 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP,
710 EC_R_INCOMPATIBLE_OBJECTS);
713 return group->meth->point_set_affine_coordinates(group, point, x, y, ctx);
716 #ifndef OPENSSL_NO_EC2M
717 int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,
718 EC_POINT *point, const BIGNUM *x,
719 const BIGNUM *y, BN_CTX *ctx)
721 if (group->meth->point_set_affine_coordinates == 0) {
722 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M,
723 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
726 if (group->meth != point->meth) {
727 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M,
728 EC_R_INCOMPATIBLE_OBJECTS);
731 return group->meth->point_set_affine_coordinates(group, point, x, y, ctx);
735 int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
736 const EC_POINT *point, BIGNUM *x,
737 BIGNUM *y, BN_CTX *ctx)
739 if (group->meth->point_get_affine_coordinates == 0) {
740 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP,
741 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
744 if (group->meth != point->meth) {
745 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP,
746 EC_R_INCOMPATIBLE_OBJECTS);
749 return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
752 #ifndef OPENSSL_NO_EC2M
753 int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,
754 const EC_POINT *point, BIGNUM *x,
755 BIGNUM *y, BN_CTX *ctx)
757 if (group->meth->point_get_affine_coordinates == 0) {
758 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M,
759 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
762 if (group->meth != point->meth) {
763 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M,
764 EC_R_INCOMPATIBLE_OBJECTS);
767 return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
771 int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
772 const EC_POINT *b, BN_CTX *ctx)
774 if (group->meth->add == 0) {
775 ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
778 if ((group->meth != r->meth) || (r->meth != a->meth)
779 || (a->meth != b->meth)) {
780 ECerr(EC_F_EC_POINT_ADD, EC_R_INCOMPATIBLE_OBJECTS);
783 return group->meth->add(group, r, a, b, ctx);
786 int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
789 if (group->meth->dbl == 0) {
790 ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
793 if ((group->meth != r->meth) || (r->meth != a->meth)) {
794 ECerr(EC_F_EC_POINT_DBL, EC_R_INCOMPATIBLE_OBJECTS);
797 return group->meth->dbl(group, r, a, ctx);
800 int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
802 if (group->meth->invert == 0) {
803 ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
806 if (group->meth != a->meth) {
807 ECerr(EC_F_EC_POINT_INVERT, EC_R_INCOMPATIBLE_OBJECTS);
810 return group->meth->invert(group, a, ctx);
813 int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
815 if (group->meth->is_at_infinity == 0) {
816 ECerr(EC_F_EC_POINT_IS_AT_INFINITY,
817 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
820 if (group->meth != point->meth) {
821 ECerr(EC_F_EC_POINT_IS_AT_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
824 return group->meth->is_at_infinity(group, point);
828 * Check whether an EC_POINT is on the curve or not. Note that the return
829 * value for this function should NOT be treated as a boolean. Return values:
830 * 1: The point is on the curve
831 * 0: The point is not on the curve
832 * -1: An error occurred
834 int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
837 if (group->meth->is_on_curve == 0) {
838 ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
841 if (group->meth != point->meth) {
842 ECerr(EC_F_EC_POINT_IS_ON_CURVE, EC_R_INCOMPATIBLE_OBJECTS);
845 return group->meth->is_on_curve(group, point, ctx);
848 int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
851 if (group->meth->point_cmp == 0) {
852 ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
855 if ((group->meth != a->meth) || (a->meth != b->meth)) {
856 ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS);
859 return group->meth->point_cmp(group, a, b, ctx);
862 int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
864 if (group->meth->make_affine == 0) {
865 ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
868 if (group->meth != point->meth) {
869 ECerr(EC_F_EC_POINT_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
872 return group->meth->make_affine(group, point, ctx);
875 int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
876 EC_POINT *points[], BN_CTX *ctx)
880 if (group->meth->points_make_affine == 0) {
881 ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
884 for (i = 0; i < num; i++) {
885 if (group->meth != points[i]->meth) {
886 ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
890 return group->meth->points_make_affine(group, num, points, ctx);
894 * Functions for point multiplication. If group->meth->mul is 0, we use the
895 * wNAF-based implementations in ec_mult.c; otherwise we dispatch through
899 int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
900 size_t num, const EC_POINT *points[],
901 const BIGNUM *scalars[], BN_CTX *ctx)
903 if (group->meth->mul == 0)
905 return ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
907 return group->meth->mul(group, r, scalar, num, points, scalars, ctx);
910 int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
911 const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
913 /* just a convenient interface to EC_POINTs_mul() */
915 const EC_POINT *points[1];
916 const BIGNUM *scalars[1];
919 scalars[0] = p_scalar;
921 return EC_POINTs_mul(group, r, g_scalar,
923 && p_scalar != NULL), points, scalars, ctx);
926 int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
928 if (group->meth->mul == 0)
930 return ec_wNAF_precompute_mult(group, ctx);
932 if (group->meth->precompute_mult != 0)
933 return group->meth->precompute_mult(group, ctx);
935 return 1; /* nothing to do, so report success */
938 int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
940 if (group->meth->mul == 0)
942 return ec_wNAF_have_precompute_mult(group);
944 if (group->meth->have_precompute_mult != 0)
945 return group->meth->have_precompute_mult(group);
947 return 0; /* cannot tell whether precomputation has
952 * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
953 * returns one on success. On error it returns zero.
955 int ec_precompute_mont_data(EC_GROUP *group)
957 BN_CTX *ctx = BN_CTX_new();
960 BN_MONT_CTX_free(group->mont_data);
961 group->mont_data = NULL;
966 group->mont_data = BN_MONT_CTX_new();
967 if (group->mont_data == NULL)
970 if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) {
971 BN_MONT_CTX_free(group->mont_data);
972 group->mont_data = NULL;
984 int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg)
986 return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
989 void *EC_KEY_get_ex_data(const EC_KEY *key, int idx)
991 return CRYPTO_get_ex_data(&key->ex_data, idx);