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.
64 #define OPENSSL_FIPSAPI
68 #include <openssl/err.h>
69 #include <openssl/opensslv.h>
74 static const char EC_version[] = "EC" OPENSSL_VERSION_PTEXT;
77 /* functions for EC_GROUP objects */
79 EC_GROUP *EC_GROUP_new(const EC_METHOD *meth)
85 ECerr(EC_F_EC_GROUP_NEW, EC_R_SLOT_FULL);
88 if (meth->group_init == 0)
90 ECerr(EC_F_EC_GROUP_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
94 ret = OPENSSL_malloc(sizeof *ret);
97 ECerr(EC_F_EC_GROUP_NEW, ERR_R_MALLOC_FAILURE);
103 ret->extra_data = NULL;
104 ret->mont_data = NULL;
106 ret->generator = NULL;
107 BN_init(&ret->order);
108 BN_init(&ret->cofactor);
112 ret->asn1_form = POINT_CONVERSION_UNCOMPRESSED;
117 if (!meth->group_init(ret))
127 void EC_GROUP_free(EC_GROUP *group)
131 if (group->meth->group_finish != 0)
132 group->meth->group_finish(group);
134 EC_EX_DATA_free_all_data(&group->extra_data);
136 if (group->mont_data)
137 BN_MONT_CTX_free(group->mont_data);
139 if (group->generator != NULL)
140 EC_POINT_free(group->generator);
141 BN_free(&group->order);
142 BN_free(&group->cofactor);
145 OPENSSL_free(group->seed);
151 void EC_GROUP_clear_free(EC_GROUP *group)
155 if (group->meth->group_clear_finish != 0)
156 group->meth->group_clear_finish(group);
157 else if (group->meth->group_finish != 0)
158 group->meth->group_finish(group);
160 EC_EX_DATA_clear_free_all_data(&group->extra_data);
162 if (group->mont_data)
163 BN_MONT_CTX_free(group->mont_data);
165 if (group->generator != NULL)
166 EC_POINT_clear_free(group->generator);
167 BN_clear_free(&group->order);
168 BN_clear_free(&group->cofactor);
172 OPENSSL_cleanse(group->seed, group->seed_len);
173 OPENSSL_free(group->seed);
176 OPENSSL_cleanse(group, sizeof *group);
181 int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)
185 if (dest->meth->group_copy == 0)
187 ECerr(EC_F_EC_GROUP_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
190 if (dest->meth != src->meth)
192 ECerr(EC_F_EC_GROUP_COPY, EC_R_INCOMPATIBLE_OBJECTS);
198 EC_EX_DATA_free_all_data(&dest->extra_data);
200 for (d = src->extra_data; d != NULL; d = d->next)
202 void *t = d->dup_func(d->data);
206 if (!EC_EX_DATA_set_data(&dest->extra_data, t, d->dup_func, d->free_func, d->clear_free_func))
210 if (src->mont_data != NULL)
212 if (dest->mont_data == NULL)
214 dest->mont_data = BN_MONT_CTX_new();
215 if (dest->mont_data == NULL) return 0;
217 if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data)) return 0;
221 /* src->generator == NULL */
222 if (dest->mont_data != NULL)
224 BN_MONT_CTX_free(dest->mont_data);
225 dest->mont_data = NULL;
229 if (src->generator != NULL)
231 if (dest->generator == NULL)
233 dest->generator = EC_POINT_new(dest);
234 if (dest->generator == NULL) return 0;
236 if (!EC_POINT_copy(dest->generator, src->generator)) return 0;
240 /* src->generator == NULL */
241 if (dest->generator != NULL)
243 EC_POINT_clear_free(dest->generator);
244 dest->generator = NULL;
248 if (!BN_copy(&dest->order, &src->order)) return 0;
249 if (!BN_copy(&dest->cofactor, &src->cofactor)) return 0;
251 dest->curve_name = src->curve_name;
252 dest->asn1_flag = src->asn1_flag;
253 dest->asn1_form = src->asn1_form;
258 OPENSSL_free(dest->seed);
259 dest->seed = OPENSSL_malloc(src->seed_len);
260 if (dest->seed == NULL)
262 if (!memcpy(dest->seed, src->seed, src->seed_len))
264 dest->seed_len = src->seed_len;
269 OPENSSL_free(dest->seed);
275 return dest->meth->group_copy(dest, src);
279 EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)
284 if (a == NULL) return NULL;
286 if ((t = EC_GROUP_new(a->meth)) == NULL) return(NULL);
287 if (!EC_GROUP_copy(t, a)) goto err;
294 if (t) EC_GROUP_free(t);
301 const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group)
307 int EC_METHOD_get_field_type(const EC_METHOD *meth)
309 return meth->field_type;
313 int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator, const BIGNUM *order, const BIGNUM *cofactor)
315 if (generator == NULL)
317 ECerr(EC_F_EC_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER);
321 if (group->generator == NULL)
323 group->generator = EC_POINT_new(group);
324 if (group->generator == NULL) return 0;
326 if (!EC_POINT_copy(group->generator, generator)) return 0;
329 { if (!BN_copy(&group->order, order)) return 0; }
331 BN_zero(&group->order);
333 if (cofactor != NULL)
334 { if (!BN_copy(&group->cofactor, cofactor)) return 0; }
336 BN_zero(&group->cofactor);
338 /* We ignore the return value because some groups have an order with
339 * factors of two, which makes the Montgomery setup fail.
340 * |group->mont_data| will be NULL in this case. */
341 ec_precompute_mont_data(group);
347 const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
349 return group->generator;
352 BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group)
354 return group->mont_data;
357 int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
359 if (!BN_copy(order, &group->order))
362 return !BN_is_zero(order);
366 int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor, BN_CTX *ctx)
368 if (!BN_copy(cofactor, &group->cofactor))
371 return !BN_is_zero(&group->cofactor);
375 void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)
377 group->curve_name = nid;
381 int EC_GROUP_get_curve_name(const EC_GROUP *group)
383 return group->curve_name;
387 void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
389 group->asn1_flag = flag;
393 int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
395 return group->asn1_flag;
399 void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
400 point_conversion_form_t form)
402 group->asn1_form = form;
406 point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP *group)
408 return group->asn1_form;
412 size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
416 OPENSSL_free(group->seed);
424 if ((group->seed = OPENSSL_malloc(len)) == NULL)
426 memcpy(group->seed, p, len);
427 group->seed_len = len;
433 unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
439 size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
441 return group->seed_len;
445 int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
447 if (group->meth->group_set_curve == 0)
449 ECerr(EC_F_EC_GROUP_SET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
452 return group->meth->group_set_curve(group, p, a, b, ctx);
456 int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)
458 if (group->meth->group_get_curve == 0)
460 ECerr(EC_F_EC_GROUP_GET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
463 return group->meth->group_get_curve(group, p, a, b, ctx);
466 #ifndef OPENSSL_NO_EC2M
467 int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
469 if (group->meth->group_set_curve == 0)
471 ECerr(EC_F_EC_GROUP_SET_CURVE_GF2M, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
474 return group->meth->group_set_curve(group, p, a, b, ctx);
478 int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)
480 if (group->meth->group_get_curve == 0)
482 ECerr(EC_F_EC_GROUP_GET_CURVE_GF2M, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
485 return group->meth->group_get_curve(group, p, a, b, ctx);
489 int EC_GROUP_get_degree(const EC_GROUP *group)
491 if (group->meth->group_get_degree == 0)
493 ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
496 return group->meth->group_get_degree(group);
500 int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
502 if (group->meth->group_check_discriminant == 0)
504 ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
507 return group->meth->group_check_discriminant(group, ctx);
511 int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
514 BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
515 BN_CTX *ctx_new = NULL;
517 /* compare the field types*/
518 if (EC_METHOD_get_field_type(EC_GROUP_method_of(a)) !=
519 EC_METHOD_get_field_type(EC_GROUP_method_of(b)))
521 /* compare the curve name (if present in both) */
522 if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
523 EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
527 ctx_new = ctx = BN_CTX_new();
532 a1 = BN_CTX_get(ctx);
533 a2 = BN_CTX_get(ctx);
534 a3 = BN_CTX_get(ctx);
535 b1 = BN_CTX_get(ctx);
536 b2 = BN_CTX_get(ctx);
537 b3 = BN_CTX_get(ctx);
546 /* XXX This approach assumes that the external representation
547 * of curves over the same field type is the same.
549 if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
550 !b->meth->group_get_curve(b, b1, b2, b3, ctx))
553 if (r || BN_cmp(a1, b1) || BN_cmp(a2, b2) || BN_cmp(a3, b3))
556 /* XXX EC_POINT_cmp() assumes that the methods are equal */
557 if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
558 EC_GROUP_get0_generator(b), ctx))
563 /* compare the order and cofactor */
564 if (!EC_GROUP_get_order(a, a1, ctx) ||
565 !EC_GROUP_get_order(b, b1, ctx) ||
566 !EC_GROUP_get_cofactor(a, a2, ctx) ||
567 !EC_GROUP_get_cofactor(b, b2, ctx))
574 if (BN_cmp(a1, b1) || BN_cmp(a2, b2))
586 /* this has 'package' visibility */
587 int EC_EX_DATA_set_data(EC_EXTRA_DATA **ex_data, void *data,
588 void *(*dup_func)(void *), void (*free_func)(void *), void (*clear_free_func)(void *))
595 for (d = *ex_data; d != NULL; d = d->next)
597 if (d->dup_func == dup_func && d->free_func == free_func && d->clear_free_func == clear_free_func)
599 ECerr(EC_F_EC_EX_DATA_SET_DATA, EC_R_SLOT_FULL);
605 /* no explicit entry needed */
608 d = OPENSSL_malloc(sizeof *d);
613 d->dup_func = dup_func;
614 d->free_func = free_func;
615 d->clear_free_func = clear_free_func;
623 /* this has 'package' visibility */
624 void *EC_EX_DATA_get_data(const EC_EXTRA_DATA *ex_data,
625 void *(*dup_func)(void *), void (*free_func)(void *), void (*clear_free_func)(void *))
627 const EC_EXTRA_DATA *d;
629 for (d = ex_data; d != NULL; d = d->next)
631 if (d->dup_func == dup_func && d->free_func == free_func && d->clear_free_func == clear_free_func)
638 /* this has 'package' visibility */
639 void EC_EX_DATA_free_data(EC_EXTRA_DATA **ex_data,
640 void *(*dup_func)(void *), void (*free_func)(void *), void (*clear_free_func)(void *))
647 for (p = ex_data; *p != NULL; p = &((*p)->next))
649 if ((*p)->dup_func == dup_func && (*p)->free_func == free_func && (*p)->clear_free_func == clear_free_func)
651 EC_EXTRA_DATA *next = (*p)->next;
653 (*p)->free_func((*p)->data);
662 /* this has 'package' visibility */
663 void EC_EX_DATA_clear_free_data(EC_EXTRA_DATA **ex_data,
664 void *(*dup_func)(void *), void (*free_func)(void *), void (*clear_free_func)(void *))
671 for (p = ex_data; *p != NULL; p = &((*p)->next))
673 if ((*p)->dup_func == dup_func && (*p)->free_func == free_func && (*p)->clear_free_func == clear_free_func)
675 EC_EXTRA_DATA *next = (*p)->next;
677 (*p)->clear_free_func((*p)->data);
686 /* this has 'package' visibility */
687 void EC_EX_DATA_free_all_data(EC_EXTRA_DATA **ex_data)
697 EC_EXTRA_DATA *next = d->next;
699 d->free_func(d->data);
707 /* this has 'package' visibility */
708 void EC_EX_DATA_clear_free_all_data(EC_EXTRA_DATA **ex_data)
718 EC_EXTRA_DATA *next = d->next;
720 d->clear_free_func(d->data);
729 /* functions for EC_POINT objects */
731 EC_POINT *EC_POINT_new(const EC_GROUP *group)
737 ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER);
740 if (group->meth->point_init == 0)
742 ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
746 ret = OPENSSL_malloc(sizeof *ret);
749 ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE);
753 ret->meth = group->meth;
755 if (!ret->meth->point_init(ret))
765 void EC_POINT_free(EC_POINT *point)
769 if (point->meth->point_finish != 0)
770 point->meth->point_finish(point);
775 void EC_POINT_clear_free(EC_POINT *point)
779 if (point->meth->point_clear_finish != 0)
780 point->meth->point_clear_finish(point);
781 else if (point->meth->point_finish != 0)
782 point->meth->point_finish(point);
783 OPENSSL_cleanse(point, sizeof *point);
788 int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)
790 if (dest->meth->point_copy == 0)
792 ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
795 if (dest->meth != src->meth)
797 ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS);
802 return dest->meth->point_copy(dest, src);
806 EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
811 if (a == NULL) return NULL;
813 t = EC_POINT_new(group);
814 if (t == NULL) return(NULL);
815 r = EC_POINT_copy(t, a);
825 const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
831 int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
833 if (group->meth->point_set_to_infinity == 0)
835 ECerr(EC_F_EC_POINT_SET_TO_INFINITY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
838 if (group->meth != point->meth)
840 ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
843 return group->meth->point_set_to_infinity(group, point);
847 int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
848 const BIGNUM *x, const BIGNUM *y, const BIGNUM *z, BN_CTX *ctx)
850 if (group->meth->point_set_Jprojective_coordinates_GFp == 0)
852 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
855 if (group->meth != point->meth)
857 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP, EC_R_INCOMPATIBLE_OBJECTS);
860 return group->meth->point_set_Jprojective_coordinates_GFp(group, point, x, y, z, ctx);
864 int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group, const EC_POINT *point,
865 BIGNUM *x, BIGNUM *y, BIGNUM *z, BN_CTX *ctx)
867 if (group->meth->point_get_Jprojective_coordinates_GFp == 0)
869 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
872 if (group->meth != point->meth)
874 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP, EC_R_INCOMPATIBLE_OBJECTS);
877 return group->meth->point_get_Jprojective_coordinates_GFp(group, point, x, y, z, ctx);
881 int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
882 const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx)
884 if (group->meth->point_set_affine_coordinates == 0)
886 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
889 if (group->meth != point->meth)
891 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP, EC_R_INCOMPATIBLE_OBJECTS);
894 return group->meth->point_set_affine_coordinates(group, point, x, y, ctx);
897 #ifndef OPENSSL_NO_EC2M
898 int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group, EC_POINT *point,
899 const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx)
901 if (group->meth->point_set_affine_coordinates == 0)
903 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
906 if (group->meth != point->meth)
908 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M, EC_R_INCOMPATIBLE_OBJECTS);
911 return group->meth->point_set_affine_coordinates(group, point, x, y, ctx);
915 int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group, const EC_POINT *point,
916 BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
918 if (group->meth->point_get_affine_coordinates == 0)
920 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
923 if (group->meth != point->meth)
925 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP, EC_R_INCOMPATIBLE_OBJECTS);
928 return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
931 #ifndef OPENSSL_NO_EC2M
932 int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group, const EC_POINT *point,
933 BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
935 if (group->meth->point_get_affine_coordinates == 0)
937 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
940 if (group->meth != point->meth)
942 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M, EC_R_INCOMPATIBLE_OBJECTS);
945 return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
949 int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, const EC_POINT *b, BN_CTX *ctx)
951 if (group->meth->add == 0)
953 ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
956 if ((group->meth != r->meth) || (r->meth != a->meth) || (a->meth != b->meth))
958 ECerr(EC_F_EC_POINT_ADD, EC_R_INCOMPATIBLE_OBJECTS);
961 return group->meth->add(group, r, a, b, ctx);
965 int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, BN_CTX *ctx)
967 if (group->meth->dbl == 0)
969 ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
972 if ((group->meth != r->meth) || (r->meth != a->meth))
974 ECerr(EC_F_EC_POINT_DBL, EC_R_INCOMPATIBLE_OBJECTS);
977 return group->meth->dbl(group, r, a, ctx);
981 int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
983 if (group->meth->invert == 0)
985 ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
988 if (group->meth != a->meth)
990 ECerr(EC_F_EC_POINT_INVERT, EC_R_INCOMPATIBLE_OBJECTS);
993 return group->meth->invert(group, a, ctx);
997 int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
999 if (group->meth->is_at_infinity == 0)
1001 ECerr(EC_F_EC_POINT_IS_AT_INFINITY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1004 if (group->meth != point->meth)
1006 ECerr(EC_F_EC_POINT_IS_AT_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
1009 return group->meth->is_at_infinity(group, point);
1013 int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point, BN_CTX *ctx)
1015 if (group->meth->is_on_curve == 0)
1017 ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1020 if (group->meth != point->meth)
1022 ECerr(EC_F_EC_POINT_IS_ON_CURVE, EC_R_INCOMPATIBLE_OBJECTS);
1025 return group->meth->is_on_curve(group, point, ctx);
1029 int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b, BN_CTX *ctx)
1031 if (group->meth->point_cmp == 0)
1033 ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1036 if ((group->meth != a->meth) || (a->meth != b->meth))
1038 ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS);
1041 return group->meth->point_cmp(group, a, b, ctx);
1045 int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
1047 if (group->meth->make_affine == 0)
1049 ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1052 if (group->meth != point->meth)
1054 ECerr(EC_F_EC_POINT_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
1057 return group->meth->make_affine(group, point, ctx);
1061 int EC_POINTs_make_affine(const EC_GROUP *group, size_t num, EC_POINT *points[], BN_CTX *ctx)
1065 if (group->meth->points_make_affine == 0)
1067 ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1070 for (i = 0; i < num; i++)
1072 if (group->meth != points[i]->meth)
1074 ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
1078 return group->meth->points_make_affine(group, num, points, ctx);
1082 /* Functions for point multiplication.
1084 * If group->meth->mul is 0, we use the wNAF-based implementations in ec_mult.c;
1085 * otherwise we dispatch through methods.
1088 int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
1089 size_t num, const EC_POINT *points[], const BIGNUM *scalars[], BN_CTX *ctx)
1091 if (group->meth->mul == 0)
1093 return ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
1095 return group->meth->mul(group, r, scalar, num, points, scalars, ctx);
1098 int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
1099 const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
1101 /* just a convenient interface to EC_POINTs_mul() */
1103 const EC_POINT *points[1];
1104 const BIGNUM *scalars[1];
1107 scalars[0] = p_scalar;
1109 return EC_POINTs_mul(group, r, g_scalar, (point != NULL && p_scalar != NULL), points, scalars, ctx);
1112 int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
1114 if (group->meth->mul == 0)
1116 return ec_wNAF_precompute_mult(group, ctx);
1118 if (group->meth->precompute_mult != 0)
1119 return group->meth->precompute_mult(group, ctx);
1121 return 1; /* nothing to do, so report success */
1124 int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
1126 if (group->meth->mul == 0)
1128 return ec_wNAF_have_precompute_mult(group);
1130 if (group->meth->have_precompute_mult != 0)
1131 return group->meth->have_precompute_mult(group);
1133 return 0; /* cannot tell whether precomputation has been performed */
1136 /* ec_precompute_mont_data sets |group->mont_data| from |group->order| and
1137 * returns one on success. On error it returns zero. */
1138 int ec_precompute_mont_data(EC_GROUP *group)
1140 BN_CTX *ctx = BN_CTX_new();
1143 if (group->mont_data)
1145 BN_MONT_CTX_free(group->mont_data);
1146 group->mont_data = NULL;
1152 group->mont_data = BN_MONT_CTX_new();
1153 if (!group->mont_data)
1156 if (!BN_MONT_CTX_set(group->mont_data, &group->order, ctx))
1158 BN_MONT_CTX_free(group->mont_data);
1159 group->mont_data = NULL;