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) {
117 #ifdef ECP_NISTZ256_REFERENCE_IMPLEMENTATION
119 EC_nistz256_pre_comp_free(group->pre_comp.nistz256);
122 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
124 EC_nistp224_pre_comp_free(group->pre_comp.nistp224);
127 EC_nistp256_pre_comp_free(group->pre_comp.nistp256);
130 EC_nistp521_pre_comp_free(group->pre_comp.nistp521);
134 EC_ec_pre_comp_free(group->pre_comp.ec);
137 group->pre_comp.ec = NULL;
140 void EC_GROUP_free(EC_GROUP *group)
145 if (group->meth->group_finish != 0)
146 group->meth->group_finish(group);
148 ec_group_free_precomp(group);
149 BN_MONT_CTX_free(group->mont_data);
150 EC_POINT_free(group->generator);
151 BN_free(group->order);
152 BN_free(group->cofactor);
153 OPENSSL_free(group->seed);
157 void EC_GROUP_clear_free(EC_GROUP *group)
162 if (group->meth->group_clear_finish != 0)
163 group->meth->group_clear_finish(group);
164 else if (group->meth->group_finish != 0)
165 group->meth->group_finish(group);
167 ec_group_free_precomp(group);
168 BN_MONT_CTX_free(group->mont_data);
169 EC_POINT_clear_free(group->generator);
170 BN_clear_free(group->order);
171 BN_clear_free(group->cofactor);
172 OPENSSL_clear_free(group->seed, group->seed_len);
173 OPENSSL_clear_free(group, sizeof(*group));
176 int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)
178 if (dest->meth->group_copy == 0) {
179 ECerr(EC_F_EC_GROUP_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
182 if (dest->meth != src->meth) {
183 ECerr(EC_F_EC_GROUP_COPY, EC_R_INCOMPATIBLE_OBJECTS);
189 /* Copy precomputed */
190 dest->pre_comp_type = src->pre_comp_type;
191 switch (src->pre_comp_type) {
193 dest->pre_comp.ec = NULL;
195 #ifdef ECP_NISTZ256_REFERENCE_IMPLEMENTATION
197 dest->pre_comp.nistz256 = EC_nistz256_pre_comp_dup(src->pre_comp.nistz256);
200 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
202 dest->pre_comp.nistp224 = EC_nistp224_pre_comp_dup(src->pre_comp.nistp224);
205 dest->pre_comp.nistp256 = EC_nistp256_pre_comp_dup(src->pre_comp.nistp256);
208 dest->pre_comp.nistp521 = EC_nistp521_pre_comp_dup(src->pre_comp.nistp521);
212 dest->pre_comp.ec = EC_ec_pre_comp_dup(src->pre_comp.ec);
216 if (src->mont_data != NULL) {
217 if (dest->mont_data == NULL) {
218 dest->mont_data = BN_MONT_CTX_new();
219 if (dest->mont_data == NULL)
222 if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data))
225 /* src->generator == NULL */
226 BN_MONT_CTX_free(dest->mont_data);
227 dest->mont_data = NULL;
230 if (src->generator != NULL) {
231 if (dest->generator == NULL) {
232 dest->generator = EC_POINT_new(dest);
233 if (dest->generator == NULL)
236 if (!EC_POINT_copy(dest->generator, src->generator))
239 /* src->generator == NULL */
240 EC_POINT_clear_free(dest->generator);
241 dest->generator = NULL;
244 if (!BN_copy(dest->order, src->order))
246 if (!BN_copy(dest->cofactor, src->cofactor))
249 dest->curve_name = src->curve_name;
250 dest->asn1_flag = src->asn1_flag;
251 dest->asn1_form = src->asn1_form;
254 OPENSSL_free(dest->seed);
255 dest->seed = OPENSSL_malloc(src->seed_len);
256 if (dest->seed == NULL)
258 if (!memcpy(dest->seed, src->seed, src->seed_len))
260 dest->seed_len = src->seed_len;
262 OPENSSL_free(dest->seed);
267 return dest->meth->group_copy(dest, src);
270 EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)
278 if ((t = EC_GROUP_new(a->meth)) == NULL)
280 if (!EC_GROUP_copy(t, a))
293 const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group)
298 int EC_METHOD_get_field_type(const EC_METHOD *meth)
300 return meth->field_type;
303 int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
304 const BIGNUM *order, const BIGNUM *cofactor)
306 if (generator == NULL) {
307 ECerr(EC_F_EC_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER);
311 if (group->generator == NULL) {
312 group->generator = EC_POINT_new(group);
313 if (group->generator == NULL)
316 if (!EC_POINT_copy(group->generator, generator))
320 if (!BN_copy(group->order, order))
323 BN_zero(group->order);
325 if (cofactor != NULL) {
326 if (!BN_copy(group->cofactor, cofactor))
329 BN_zero(group->cofactor);
332 * We ignore the return value because some groups have an order with
333 * factors of two, which makes the Montgomery setup fail.
334 * |group->mont_data| will be NULL in this case.
336 ec_precompute_mont_data(group);
341 const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
343 return group->generator;
346 BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group)
348 return group->mont_data;
351 int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
353 if (!BN_copy(order, group->order))
356 return !BN_is_zero(order);
359 int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
362 if (!BN_copy(cofactor, group->cofactor))
365 return !BN_is_zero(group->cofactor);
368 void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)
370 group->curve_name = nid;
373 int EC_GROUP_get_curve_name(const EC_GROUP *group)
375 return group->curve_name;
378 void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
380 group->asn1_flag = flag;
383 int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
385 return group->asn1_flag;
388 void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
389 point_conversion_form_t form)
391 group->asn1_form = form;
394 point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP
397 return group->asn1_form;
400 size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
402 OPENSSL_free(group->seed);
409 if ((group->seed = OPENSSL_malloc(len)) == NULL)
411 memcpy(group->seed, p, len);
412 group->seed_len = len;
417 unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
422 size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
424 return group->seed_len;
427 int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
428 const BIGNUM *b, BN_CTX *ctx)
430 if (group->meth->group_set_curve == 0) {
431 ECerr(EC_F_EC_GROUP_SET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
434 return group->meth->group_set_curve(group, p, a, b, ctx);
437 int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
438 BIGNUM *b, BN_CTX *ctx)
440 if (group->meth->group_get_curve == 0) {
441 ECerr(EC_F_EC_GROUP_GET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
444 return group->meth->group_get_curve(group, p, a, b, ctx);
447 #ifndef OPENSSL_NO_EC2M
448 int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
449 const BIGNUM *b, BN_CTX *ctx)
451 if (group->meth->group_set_curve == 0) {
452 ECerr(EC_F_EC_GROUP_SET_CURVE_GF2M,
453 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
456 return group->meth->group_set_curve(group, p, a, b, ctx);
459 int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
460 BIGNUM *b, BN_CTX *ctx)
462 if (group->meth->group_get_curve == 0) {
463 ECerr(EC_F_EC_GROUP_GET_CURVE_GF2M,
464 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
467 return group->meth->group_get_curve(group, p, a, b, ctx);
471 int EC_GROUP_get_degree(const EC_GROUP *group)
473 if (group->meth->group_get_degree == 0) {
474 ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
477 return group->meth->group_get_degree(group);
480 int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
482 if (group->meth->group_check_discriminant == 0) {
483 ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT,
484 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
487 return group->meth->group_check_discriminant(group, ctx);
490 int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
493 BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
494 BN_CTX *ctx_new = NULL;
496 /* compare the field types */
497 if (EC_METHOD_get_field_type(EC_GROUP_method_of(a)) !=
498 EC_METHOD_get_field_type(EC_GROUP_method_of(b)))
500 /* compare the curve name (if present in both) */
501 if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
502 EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
506 ctx_new = ctx = BN_CTX_new();
511 a1 = BN_CTX_get(ctx);
512 a2 = BN_CTX_get(ctx);
513 a3 = BN_CTX_get(ctx);
514 b1 = BN_CTX_get(ctx);
515 b2 = BN_CTX_get(ctx);
516 b3 = BN_CTX_get(ctx);
519 BN_CTX_free(ctx_new);
524 * XXX This approach assumes that the external representation of curves
525 * over the same field type is the same.
527 if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
528 !b->meth->group_get_curve(b, b1, b2, b3, ctx))
531 if (r || BN_cmp(a1, b1) || BN_cmp(a2, b2) || BN_cmp(a3, b3))
534 /* XXX EC_POINT_cmp() assumes that the methods are equal */
535 if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
536 EC_GROUP_get0_generator(b), ctx))
540 /* compare the order and cofactor */
541 if (!EC_GROUP_get_order(a, a1, ctx) ||
542 !EC_GROUP_get_order(b, b1, ctx) ||
543 !EC_GROUP_get_cofactor(a, a2, ctx) ||
544 !EC_GROUP_get_cofactor(b, b2, ctx)) {
546 BN_CTX_free(ctx_new);
549 if (BN_cmp(a1, b1) || BN_cmp(a2, b2))
554 BN_CTX_free(ctx_new);
559 /* functions for EC_POINT objects */
561 EC_POINT *EC_POINT_new(const EC_GROUP *group)
566 ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER);
569 if (group->meth->point_init == 0) {
570 ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
574 ret = OPENSSL_malloc(sizeof(*ret));
576 ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE);
580 ret->meth = group->meth;
582 if (!ret->meth->point_init(ret)) {
590 void EC_POINT_free(EC_POINT *point)
595 if (point->meth->point_finish != 0)
596 point->meth->point_finish(point);
600 void EC_POINT_clear_free(EC_POINT *point)
605 if (point->meth->point_clear_finish != 0)
606 point->meth->point_clear_finish(point);
607 else if (point->meth->point_finish != 0)
608 point->meth->point_finish(point);
609 OPENSSL_clear_free(point, sizeof(*point));
612 int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)
614 if (dest->meth->point_copy == 0) {
615 ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
618 if (dest->meth != src->meth) {
619 ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS);
624 return dest->meth->point_copy(dest, src);
627 EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
635 t = EC_POINT_new(group);
638 r = EC_POINT_copy(t, a);
646 const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
651 int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
653 if (group->meth->point_set_to_infinity == 0) {
654 ECerr(EC_F_EC_POINT_SET_TO_INFINITY,
655 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
658 if (group->meth != point->meth) {
659 ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
662 return group->meth->point_set_to_infinity(group, point);
665 int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
666 EC_POINT *point, const BIGNUM *x,
667 const BIGNUM *y, const BIGNUM *z,
670 if (group->meth->point_set_Jprojective_coordinates_GFp == 0) {
671 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
672 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
675 if (group->meth != point->meth) {
676 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
677 EC_R_INCOMPATIBLE_OBJECTS);
680 return group->meth->point_set_Jprojective_coordinates_GFp(group, point, x,
684 int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
685 const EC_POINT *point, BIGNUM *x,
686 BIGNUM *y, BIGNUM *z,
689 if (group->meth->point_get_Jprojective_coordinates_GFp == 0) {
690 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
691 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
694 if (group->meth != point->meth) {
695 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
696 EC_R_INCOMPATIBLE_OBJECTS);
699 return group->meth->point_get_Jprojective_coordinates_GFp(group, point, x,
703 int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
704 EC_POINT *point, const BIGNUM *x,
705 const BIGNUM *y, BN_CTX *ctx)
707 if (group->meth->point_set_affine_coordinates == 0) {
708 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP,
709 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
712 if (group->meth != point->meth) {
713 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP,
714 EC_R_INCOMPATIBLE_OBJECTS);
717 return group->meth->point_set_affine_coordinates(group, point, x, y, ctx);
720 #ifndef OPENSSL_NO_EC2M
721 int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,
722 EC_POINT *point, const BIGNUM *x,
723 const BIGNUM *y, BN_CTX *ctx)
725 if (group->meth->point_set_affine_coordinates == 0) {
726 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M,
727 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
730 if (group->meth != point->meth) {
731 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M,
732 EC_R_INCOMPATIBLE_OBJECTS);
735 return group->meth->point_set_affine_coordinates(group, point, x, y, ctx);
739 int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
740 const EC_POINT *point, BIGNUM *x,
741 BIGNUM *y, BN_CTX *ctx)
743 if (group->meth->point_get_affine_coordinates == 0) {
744 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP,
745 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
748 if (group->meth != point->meth) {
749 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP,
750 EC_R_INCOMPATIBLE_OBJECTS);
753 return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
756 #ifndef OPENSSL_NO_EC2M
757 int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,
758 const EC_POINT *point, BIGNUM *x,
759 BIGNUM *y, BN_CTX *ctx)
761 if (group->meth->point_get_affine_coordinates == 0) {
762 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M,
763 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
766 if (group->meth != point->meth) {
767 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M,
768 EC_R_INCOMPATIBLE_OBJECTS);
771 return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
775 int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
776 const EC_POINT *b, BN_CTX *ctx)
778 if (group->meth->add == 0) {
779 ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
782 if ((group->meth != r->meth) || (r->meth != a->meth)
783 || (a->meth != b->meth)) {
784 ECerr(EC_F_EC_POINT_ADD, EC_R_INCOMPATIBLE_OBJECTS);
787 return group->meth->add(group, r, a, b, ctx);
790 int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
793 if (group->meth->dbl == 0) {
794 ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
797 if ((group->meth != r->meth) || (r->meth != a->meth)) {
798 ECerr(EC_F_EC_POINT_DBL, EC_R_INCOMPATIBLE_OBJECTS);
801 return group->meth->dbl(group, r, a, ctx);
804 int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
806 if (group->meth->invert == 0) {
807 ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
810 if (group->meth != a->meth) {
811 ECerr(EC_F_EC_POINT_INVERT, EC_R_INCOMPATIBLE_OBJECTS);
814 return group->meth->invert(group, a, ctx);
817 int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
819 if (group->meth->is_at_infinity == 0) {
820 ECerr(EC_F_EC_POINT_IS_AT_INFINITY,
821 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
824 if (group->meth != point->meth) {
825 ECerr(EC_F_EC_POINT_IS_AT_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
828 return group->meth->is_at_infinity(group, point);
832 * Check whether an EC_POINT is on the curve or not. Note that the return
833 * value for this function should NOT be treated as a boolean. Return values:
834 * 1: The point is on the curve
835 * 0: The point is not on the curve
836 * -1: An error occurred
838 int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
841 if (group->meth->is_on_curve == 0) {
842 ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
845 if (group->meth != point->meth) {
846 ECerr(EC_F_EC_POINT_IS_ON_CURVE, EC_R_INCOMPATIBLE_OBJECTS);
849 return group->meth->is_on_curve(group, point, ctx);
852 int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
855 if (group->meth->point_cmp == 0) {
856 ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
859 if ((group->meth != a->meth) || (a->meth != b->meth)) {
860 ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS);
863 return group->meth->point_cmp(group, a, b, ctx);
866 int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
868 if (group->meth->make_affine == 0) {
869 ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
872 if (group->meth != point->meth) {
873 ECerr(EC_F_EC_POINT_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
876 return group->meth->make_affine(group, point, ctx);
879 int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
880 EC_POINT *points[], BN_CTX *ctx)
884 if (group->meth->points_make_affine == 0) {
885 ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
888 for (i = 0; i < num; i++) {
889 if (group->meth != points[i]->meth) {
890 ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
894 return group->meth->points_make_affine(group, num, points, ctx);
898 * Functions for point multiplication. If group->meth->mul is 0, we use the
899 * wNAF-based implementations in ec_mult.c; otherwise we dispatch through
903 int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
904 size_t num, const EC_POINT *points[],
905 const BIGNUM *scalars[], BN_CTX *ctx)
907 if (group->meth->mul == 0)
909 return ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
911 return group->meth->mul(group, r, scalar, num, points, scalars, ctx);
914 int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
915 const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
917 /* just a convenient interface to EC_POINTs_mul() */
919 const EC_POINT *points[1];
920 const BIGNUM *scalars[1];
923 scalars[0] = p_scalar;
925 return EC_POINTs_mul(group, r, g_scalar,
927 && p_scalar != NULL), points, scalars, ctx);
930 int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
932 if (group->meth->mul == 0)
934 return ec_wNAF_precompute_mult(group, ctx);
936 if (group->meth->precompute_mult != 0)
937 return group->meth->precompute_mult(group, ctx);
939 return 1; /* nothing to do, so report success */
942 int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
944 if (group->meth->mul == 0)
946 return ec_wNAF_have_precompute_mult(group);
948 if (group->meth->have_precompute_mult != 0)
949 return group->meth->have_precompute_mult(group);
951 return 0; /* cannot tell whether precomputation has
956 * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
957 * returns one on success. On error it returns zero.
959 int ec_precompute_mont_data(EC_GROUP *group)
961 BN_CTX *ctx = BN_CTX_new();
964 BN_MONT_CTX_free(group->mont_data);
965 group->mont_data = NULL;
970 group->mont_data = BN_MONT_CTX_new();
971 if (group->mont_data == NULL)
974 if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) {
975 BN_MONT_CTX_free(group->mont_data);
976 group->mont_data = NULL;
988 int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg)
990 return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
993 void *EC_KEY_get_ex_data(const EC_KEY *key, int idx)
995 return CRYPTO_get_ex_data(&key->ex_data, idx);