import libcrypto (LibreSSL 2.5.2)
[unleashed.git] / lib / libcrypto / ec / ec_lib.c
blobbaddbf6dc853d57dc441e5e9a3cc7ea64cde85c5
1 /* $OpenBSD: ec_lib.c,v 1.23 2017/01/29 17:49:23 beck Exp $ */
2 /*
3 * Originally written by Bodo Moeller for the OpenSSL project.
4 */
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
10 * are met:
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
18 * distribution.
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
35 * acknowledgment:
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 #include <string.h>
66 #include <openssl/opensslconf.h>
68 #include <openssl/err.h>
69 #include <openssl/opensslv.h>
71 #include "ec_lcl.h"
73 /* functions for EC_GROUP objects */
75 EC_GROUP *
76 EC_GROUP_new(const EC_METHOD * meth)
78 EC_GROUP *ret;
80 if (meth == NULL) {
81 ECerror(EC_R_SLOT_FULL);
82 return NULL;
84 if (meth->group_init == 0) {
85 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
86 return NULL;
88 ret = malloc(sizeof *ret);
89 if (ret == NULL) {
90 ECerror(ERR_R_MALLOC_FAILURE);
91 return NULL;
93 ret->meth = meth;
95 ret->extra_data = NULL;
97 ret->generator = NULL;
98 BN_init(&ret->order);
99 BN_init(&ret->cofactor);
101 ret->curve_name = 0;
102 ret->asn1_flag = 0;
103 ret->asn1_form = POINT_CONVERSION_UNCOMPRESSED;
105 ret->seed = NULL;
106 ret->seed_len = 0;
108 if (!meth->group_init(ret)) {
109 free(ret);
110 return NULL;
112 return ret;
116 void
117 EC_GROUP_free(EC_GROUP * group)
119 if (!group)
120 return;
122 if (group->meth->group_finish != 0)
123 group->meth->group_finish(group);
125 EC_EX_DATA_free_all_data(&group->extra_data);
127 EC_POINT_free(group->generator);
128 BN_free(&group->order);
129 BN_free(&group->cofactor);
131 free(group->seed);
133 free(group);
137 void
138 EC_GROUP_clear_free(EC_GROUP * group)
140 if (!group)
141 return;
143 if (group->meth->group_clear_finish != 0)
144 group->meth->group_clear_finish(group);
145 else if (group->meth->group_finish != 0)
146 group->meth->group_finish(group);
148 EC_EX_DATA_clear_free_all_data(&group->extra_data);
150 EC_POINT_clear_free(group->generator);
151 BN_clear_free(&group->order);
152 BN_clear_free(&group->cofactor);
154 if (group->seed) {
155 explicit_bzero(group->seed, group->seed_len);
156 free(group->seed);
158 explicit_bzero(group, sizeof *group);
159 free(group);
163 int
164 EC_GROUP_copy(EC_GROUP * dest, const EC_GROUP * src)
166 EC_EXTRA_DATA *d;
168 if (dest->meth->group_copy == 0) {
169 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
170 return 0;
172 if (dest->meth != src->meth) {
173 ECerror(EC_R_INCOMPATIBLE_OBJECTS);
174 return 0;
176 if (dest == src)
177 return 1;
179 EC_EX_DATA_free_all_data(&dest->extra_data);
181 for (d = src->extra_data; d != NULL; d = d->next) {
182 void *t = d->dup_func(d->data);
184 if (t == NULL)
185 return 0;
186 if (!EC_EX_DATA_set_data(&dest->extra_data, t, d->dup_func,
187 d->free_func, d->clear_free_func))
188 return 0;
191 if (src->generator != NULL) {
192 if (dest->generator == NULL) {
193 dest->generator = EC_POINT_new(dest);
194 if (dest->generator == NULL)
195 return 0;
197 if (!EC_POINT_copy(dest->generator, src->generator))
198 return 0;
199 } else {
200 /* src->generator == NULL */
201 EC_POINT_clear_free(dest->generator);
202 dest->generator = NULL;
205 if (!BN_copy(&dest->order, &src->order))
206 return 0;
207 if (!BN_copy(&dest->cofactor, &src->cofactor))
208 return 0;
210 dest->curve_name = src->curve_name;
211 dest->asn1_flag = src->asn1_flag;
212 dest->asn1_form = src->asn1_form;
214 if (src->seed) {
215 free(dest->seed);
216 dest->seed = malloc(src->seed_len);
217 if (dest->seed == NULL)
218 return 0;
219 memcpy(dest->seed, src->seed, src->seed_len);
220 dest->seed_len = src->seed_len;
221 } else {
222 free(dest->seed);
223 dest->seed = NULL;
224 dest->seed_len = 0;
228 return dest->meth->group_copy(dest, src);
232 EC_GROUP *
233 EC_GROUP_dup(const EC_GROUP * a)
235 EC_GROUP *t = NULL;
237 if ((a != NULL) && ((t = EC_GROUP_new(a->meth)) != NULL) &&
238 (!EC_GROUP_copy(t, a))) {
239 EC_GROUP_free(t);
240 t = NULL;
242 return t;
246 const EC_METHOD *
247 EC_GROUP_method_of(const EC_GROUP *group)
249 return group->meth;
253 int
254 EC_METHOD_get_field_type(const EC_METHOD *meth)
256 return meth->field_type;
260 int
261 EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
262 const BIGNUM *order, const BIGNUM *cofactor)
264 if (generator == NULL) {
265 ECerror(ERR_R_PASSED_NULL_PARAMETER);
266 return 0;
268 if (group->generator == NULL) {
269 group->generator = EC_POINT_new(group);
270 if (group->generator == NULL)
271 return 0;
273 if (!EC_POINT_copy(group->generator, generator))
274 return 0;
276 if (order != NULL) {
277 if (!BN_copy(&group->order, order))
278 return 0;
279 } else
280 BN_zero(&group->order);
282 if (cofactor != NULL) {
283 if (!BN_copy(&group->cofactor, cofactor))
284 return 0;
285 } else
286 BN_zero(&group->cofactor);
288 return 1;
292 const EC_POINT *
293 EC_GROUP_get0_generator(const EC_GROUP *group)
295 return group->generator;
299 int
300 EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
302 if (!BN_copy(order, &group->order))
303 return 0;
305 return !BN_is_zero(order);
309 int
310 EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor, BN_CTX *ctx)
312 if (!BN_copy(cofactor, &group->cofactor))
313 return 0;
315 return !BN_is_zero(&group->cofactor);
319 void
320 EC_GROUP_set_curve_name(EC_GROUP * group, int nid)
322 group->curve_name = nid;
326 int
327 EC_GROUP_get_curve_name(const EC_GROUP * group)
329 return group->curve_name;
333 void
334 EC_GROUP_set_asn1_flag(EC_GROUP * group, int flag)
336 group->asn1_flag = flag;
340 int
341 EC_GROUP_get_asn1_flag(const EC_GROUP * group)
343 return group->asn1_flag;
347 void
348 EC_GROUP_set_point_conversion_form(EC_GROUP * group,
349 point_conversion_form_t form)
351 group->asn1_form = form;
355 point_conversion_form_t
356 EC_GROUP_get_point_conversion_form(const EC_GROUP * group)
358 return group->asn1_form;
362 size_t
363 EC_GROUP_set_seed(EC_GROUP * group, const unsigned char *p, size_t len)
365 if (group->seed) {
366 free(group->seed);
367 group->seed = NULL;
368 group->seed_len = 0;
370 if (!len || !p)
371 return 1;
373 if ((group->seed = malloc(len)) == NULL)
374 return 0;
375 memcpy(group->seed, p, len);
376 group->seed_len = len;
378 return len;
382 unsigned char *
383 EC_GROUP_get0_seed(const EC_GROUP * group)
385 return group->seed;
389 size_t
390 EC_GROUP_get_seed_len(const EC_GROUP * group)
392 return group->seed_len;
396 int
397 EC_GROUP_set_curve_GFp(EC_GROUP * group, const BIGNUM * p, const BIGNUM * a,
398 const BIGNUM * b, BN_CTX * ctx)
400 if (group->meth->group_set_curve == 0) {
401 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
402 return 0;
404 return group->meth->group_set_curve(group, p, a, b, ctx);
408 int
409 EC_GROUP_get_curve_GFp(const EC_GROUP * group, BIGNUM * p, BIGNUM * a,
410 BIGNUM * b, BN_CTX * ctx)
412 if (group->meth->group_get_curve == 0) {
413 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
414 return 0;
416 return group->meth->group_get_curve(group, p, a, b, ctx);
419 #ifndef OPENSSL_NO_EC2M
420 int
421 EC_GROUP_set_curve_GF2m(EC_GROUP * group, const BIGNUM * p, const BIGNUM * a,
422 const BIGNUM * b, BN_CTX * ctx)
424 if (group->meth->group_set_curve == 0) {
425 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
426 return 0;
428 return group->meth->group_set_curve(group, p, a, b, ctx);
432 int
433 EC_GROUP_get_curve_GF2m(const EC_GROUP * group, BIGNUM * p, BIGNUM * a,
434 BIGNUM * b, BN_CTX * ctx)
436 if (group->meth->group_get_curve == 0) {
437 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
438 return 0;
440 return group->meth->group_get_curve(group, p, a, b, ctx);
442 #endif
444 int
445 EC_GROUP_get_degree(const EC_GROUP * group)
447 if (group->meth->group_get_degree == 0) {
448 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
449 return 0;
451 return group->meth->group_get_degree(group);
455 int
456 EC_GROUP_check_discriminant(const EC_GROUP * group, BN_CTX * ctx)
458 if (group->meth->group_check_discriminant == 0) {
459 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
460 return 0;
462 return group->meth->group_check_discriminant(group, ctx);
466 int
467 EC_GROUP_cmp(const EC_GROUP * a, const EC_GROUP * b, BN_CTX * ctx)
469 int r = 0;
470 BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
471 BN_CTX *ctx_new = NULL;
473 /* compare the field types */
474 if (EC_METHOD_get_field_type(EC_GROUP_method_of(a)) !=
475 EC_METHOD_get_field_type(EC_GROUP_method_of(b)))
476 return 1;
477 /* compare the curve name (if present in both) */
478 if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
479 EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
480 return 1;
482 if (!ctx)
483 ctx_new = ctx = BN_CTX_new();
484 if (!ctx)
485 return -1;
487 BN_CTX_start(ctx);
488 if ((a1 = BN_CTX_get(ctx)) == NULL)
489 goto err;
490 if ((a2 = BN_CTX_get(ctx)) == NULL)
491 goto err;
492 if ((a3 = BN_CTX_get(ctx)) == NULL)
493 goto err;
494 if ((b1 = BN_CTX_get(ctx)) == NULL)
495 goto err;
496 if ((b2 = BN_CTX_get(ctx)) == NULL)
497 goto err;
498 if ((b3 = BN_CTX_get(ctx)) == NULL)
499 goto err;
502 * XXX This approach assumes that the external representation of
503 * curves over the same field type is the same.
505 if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
506 !b->meth->group_get_curve(b, b1, b2, b3, ctx))
507 r = 1;
509 if (r || BN_cmp(a1, b1) || BN_cmp(a2, b2) || BN_cmp(a3, b3))
510 r = 1;
512 /* XXX EC_POINT_cmp() assumes that the methods are equal */
513 if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
514 EC_GROUP_get0_generator(b), ctx))
515 r = 1;
517 if (!r) {
518 /* compare the order and cofactor */
519 if (!EC_GROUP_get_order(a, a1, ctx) ||
520 !EC_GROUP_get_order(b, b1, ctx) ||
521 !EC_GROUP_get_cofactor(a, a2, ctx) ||
522 !EC_GROUP_get_cofactor(b, b2, ctx))
523 goto err;
524 if (BN_cmp(a1, b1) || BN_cmp(a2, b2))
525 r = 1;
527 BN_CTX_end(ctx);
528 if (ctx_new)
529 BN_CTX_free(ctx);
531 return r;
533 err:
534 BN_CTX_end(ctx);
535 if (ctx_new)
536 BN_CTX_free(ctx);
537 return -1;
541 /* this has 'package' visibility */
542 int
543 EC_EX_DATA_set_data(EC_EXTRA_DATA ** ex_data, void *data,
544 void *(*dup_func) (void *),
545 void (*free_func) (void *),
546 void (*clear_free_func) (void *))
548 EC_EXTRA_DATA *d;
550 if (ex_data == NULL)
551 return 0;
553 for (d = *ex_data; d != NULL; d = d->next) {
554 if (d->dup_func == dup_func && d->free_func == free_func &&
555 d->clear_free_func == clear_free_func) {
556 ECerror(EC_R_SLOT_FULL);
557 return 0;
561 if (data == NULL)
562 /* no explicit entry needed */
563 return 1;
565 d = malloc(sizeof *d);
566 if (d == NULL)
567 return 0;
569 d->data = data;
570 d->dup_func = dup_func;
571 d->free_func = free_func;
572 d->clear_free_func = clear_free_func;
574 d->next = *ex_data;
575 *ex_data = d;
577 return 1;
580 /* this has 'package' visibility */
581 void *
582 EC_EX_DATA_get_data(const EC_EXTRA_DATA * ex_data,
583 void *(*dup_func) (void *),
584 void (*free_func) (void *),
585 void (*clear_free_func) (void *))
587 const EC_EXTRA_DATA *d;
589 for (d = ex_data; d != NULL; d = d->next) {
590 if (d->dup_func == dup_func && d->free_func == free_func && d->clear_free_func == clear_free_func)
591 return d->data;
594 return NULL;
597 /* this has 'package' visibility */
598 void
599 EC_EX_DATA_free_data(EC_EXTRA_DATA ** ex_data,
600 void *(*dup_func) (void *),
601 void (*free_func) (void *),
602 void (*clear_free_func) (void *))
604 EC_EXTRA_DATA **p;
606 if (ex_data == NULL)
607 return;
609 for (p = ex_data; *p != NULL; p = &((*p)->next)) {
610 if ((*p)->dup_func == dup_func &&
611 (*p)->free_func == free_func &&
612 (*p)->clear_free_func == clear_free_func) {
613 EC_EXTRA_DATA *next = (*p)->next;
615 (*p)->free_func((*p)->data);
616 free(*p);
618 *p = next;
619 return;
624 /* this has 'package' visibility */
625 void
626 EC_EX_DATA_clear_free_data(EC_EXTRA_DATA ** ex_data,
627 void *(*dup_func) (void *),
628 void (*free_func) (void *),
629 void (*clear_free_func) (void *))
631 EC_EXTRA_DATA **p;
633 if (ex_data == NULL)
634 return;
636 for (p = ex_data; *p != NULL; p = &((*p)->next)) {
637 if ((*p)->dup_func == dup_func &&
638 (*p)->free_func == free_func &&
639 (*p)->clear_free_func == clear_free_func) {
640 EC_EXTRA_DATA *next = (*p)->next;
642 (*p)->clear_free_func((*p)->data);
643 free(*p);
645 *p = next;
646 return;
651 /* this has 'package' visibility */
652 void
653 EC_EX_DATA_free_all_data(EC_EXTRA_DATA ** ex_data)
655 EC_EXTRA_DATA *d;
657 if (ex_data == NULL)
658 return;
660 d = *ex_data;
661 while (d) {
662 EC_EXTRA_DATA *next = d->next;
664 d->free_func(d->data);
665 free(d);
667 d = next;
669 *ex_data = NULL;
672 /* this has 'package' visibility */
673 void
674 EC_EX_DATA_clear_free_all_data(EC_EXTRA_DATA ** ex_data)
676 EC_EXTRA_DATA *d;
678 if (ex_data == NULL)
679 return;
681 d = *ex_data;
682 while (d) {
683 EC_EXTRA_DATA *next = d->next;
685 d->clear_free_func(d->data);
686 free(d);
688 d = next;
690 *ex_data = NULL;
694 /* functions for EC_POINT objects */
696 EC_POINT *
697 EC_POINT_new(const EC_GROUP * group)
699 EC_POINT *ret;
701 if (group == NULL) {
702 ECerror(ERR_R_PASSED_NULL_PARAMETER);
703 return NULL;
705 if (group->meth->point_init == 0) {
706 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
707 return NULL;
709 ret = malloc(sizeof *ret);
710 if (ret == NULL) {
711 ECerror(ERR_R_MALLOC_FAILURE);
712 return NULL;
714 ret->meth = group->meth;
716 if (!ret->meth->point_init(ret)) {
717 free(ret);
718 return NULL;
720 return ret;
724 void
725 EC_POINT_free(EC_POINT * point)
727 if (!point)
728 return;
730 if (point->meth->point_finish != 0)
731 point->meth->point_finish(point);
732 free(point);
736 void
737 EC_POINT_clear_free(EC_POINT * point)
739 if (!point)
740 return;
742 if (point->meth->point_clear_finish != 0)
743 point->meth->point_clear_finish(point);
744 else if (point->meth->point_finish != 0)
745 point->meth->point_finish(point);
746 explicit_bzero(point, sizeof *point);
747 free(point);
751 int
752 EC_POINT_copy(EC_POINT * dest, const EC_POINT * src)
754 if (dest->meth->point_copy == 0) {
755 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
756 return 0;
758 if (dest->meth != src->meth) {
759 ECerror(EC_R_INCOMPATIBLE_OBJECTS);
760 return 0;
762 if (dest == src)
763 return 1;
764 return dest->meth->point_copy(dest, src);
768 EC_POINT *
769 EC_POINT_dup(const EC_POINT * a, const EC_GROUP * group)
771 EC_POINT *t;
772 int r;
774 if (a == NULL)
775 return NULL;
777 t = EC_POINT_new(group);
778 if (t == NULL)
779 return (NULL);
780 r = EC_POINT_copy(t, a);
781 if (!r) {
782 EC_POINT_free(t);
783 return NULL;
784 } else
785 return t;
789 const EC_METHOD *
790 EC_POINT_method_of(const EC_POINT * point)
792 return point->meth;
796 int
797 EC_POINT_set_to_infinity(const EC_GROUP * group, EC_POINT * point)
799 if (group->meth->point_set_to_infinity == 0) {
800 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
801 return 0;
803 if (group->meth != point->meth) {
804 ECerror(EC_R_INCOMPATIBLE_OBJECTS);
805 return 0;
807 return group->meth->point_set_to_infinity(group, point);
811 int
812 EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
813 const BIGNUM *x, const BIGNUM *y, const BIGNUM *z, BN_CTX *ctx)
815 if (group->meth->point_set_Jprojective_coordinates_GFp == 0) {
816 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
817 return 0;
819 if (group->meth != point->meth) {
820 ECerror(EC_R_INCOMPATIBLE_OBJECTS);
821 return 0;
823 return group->meth->point_set_Jprojective_coordinates_GFp(group, point, x, y, z, ctx);
827 int
828 EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
829 const EC_POINT *point, BIGNUM *x, BIGNUM *y, BIGNUM *z, BN_CTX *ctx)
831 if (group->meth->point_get_Jprojective_coordinates_GFp == 0) {
832 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
833 return 0;
835 if (group->meth != point->meth) {
836 ECerror(EC_R_INCOMPATIBLE_OBJECTS);
837 return 0;
839 return group->meth->point_get_Jprojective_coordinates_GFp(group, point, x, y, z, ctx);
843 int
844 EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
845 const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx)
847 if (group->meth->point_set_affine_coordinates == 0) {
848 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
849 return 0;
851 if (group->meth != point->meth) {
852 ECerror(EC_R_INCOMPATIBLE_OBJECTS);
853 return 0;
855 return group->meth->point_set_affine_coordinates(group, point, x, y, ctx);
858 #ifndef OPENSSL_NO_EC2M
859 int
860 EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group, EC_POINT *point,
861 const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx)
863 if (group->meth->point_set_affine_coordinates == 0) {
864 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
865 return 0;
867 if (group->meth != point->meth) {
868 ECerror(EC_R_INCOMPATIBLE_OBJECTS);
869 return 0;
871 return group->meth->point_set_affine_coordinates(group, point, x, y, ctx);
873 #endif
875 int
876 EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group, const EC_POINT *point,
877 BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
879 if (group->meth->point_get_affine_coordinates == 0) {
880 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
881 return 0;
883 if (group->meth != point->meth) {
884 ECerror(EC_R_INCOMPATIBLE_OBJECTS);
885 return 0;
887 return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
890 #ifndef OPENSSL_NO_EC2M
891 int
892 EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group, const EC_POINT *point,
893 BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
895 if (group->meth->point_get_affine_coordinates == 0) {
896 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
897 return 0;
899 if (group->meth != point->meth) {
900 ECerror(EC_R_INCOMPATIBLE_OBJECTS);
901 return 0;
903 return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
905 #endif
907 int
908 EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
909 const EC_POINT *b, BN_CTX *ctx)
911 if (group->meth->add == 0) {
912 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
913 return 0;
915 if ((group->meth != r->meth) || (r->meth != a->meth) || (a->meth != b->meth)) {
916 ECerror(EC_R_INCOMPATIBLE_OBJECTS);
917 return 0;
919 return group->meth->add(group, r, a, b, ctx);
923 int
924 EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, BN_CTX *ctx)
926 if (group->meth->dbl == 0) {
927 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
928 return 0;
930 if ((group->meth != r->meth) || (r->meth != a->meth)) {
931 ECerror(EC_R_INCOMPATIBLE_OBJECTS);
932 return 0;
934 return group->meth->dbl(group, r, a, ctx);
938 int
939 EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
941 if (group->meth->invert == 0) {
942 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
943 return 0;
945 if (group->meth != a->meth) {
946 ECerror(EC_R_INCOMPATIBLE_OBJECTS);
947 return 0;
949 return group->meth->invert(group, a, ctx);
953 int
954 EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
956 if (group->meth->is_at_infinity == 0) {
957 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
958 return 0;
960 if (group->meth != point->meth) {
961 ECerror(EC_R_INCOMPATIBLE_OBJECTS);
962 return 0;
964 return group->meth->is_at_infinity(group, point);
968 int
969 EC_POINT_is_on_curve(const EC_GROUP * group, const EC_POINT * point, BN_CTX * ctx)
971 if (group->meth->is_on_curve == 0) {
972 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
973 return 0;
975 if (group->meth != point->meth) {
976 ECerror(EC_R_INCOMPATIBLE_OBJECTS);
977 return 0;
979 return group->meth->is_on_curve(group, point, ctx);
983 int
984 EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
985 BN_CTX * ctx)
987 if (group->meth->point_cmp == 0) {
988 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
989 return -1;
991 if ((group->meth != a->meth) || (a->meth != b->meth)) {
992 ECerror(EC_R_INCOMPATIBLE_OBJECTS);
993 return -1;
995 return group->meth->point_cmp(group, a, b, ctx);
999 int
1000 EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
1002 if (group->meth->make_affine == 0) {
1003 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1004 return 0;
1006 if (group->meth != point->meth) {
1007 ECerror(EC_R_INCOMPATIBLE_OBJECTS);
1008 return 0;
1010 return group->meth->make_affine(group, point, ctx);
1014 int
1015 EC_POINTs_make_affine(const EC_GROUP *group, size_t num, EC_POINT *points[],
1016 BN_CTX *ctx)
1018 size_t i;
1020 if (group->meth->points_make_affine == 0) {
1021 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1022 return 0;
1024 for (i = 0; i < num; i++) {
1025 if (group->meth != points[i]->meth) {
1026 ECerror(EC_R_INCOMPATIBLE_OBJECTS);
1027 return 0;
1030 return group->meth->points_make_affine(group, num, points, ctx);
1034 /* Functions for point multiplication.
1036 * If group->meth->mul is 0, we use the wNAF-based implementations in ec_mult.c;
1037 * otherwise we dispatch through methods.
1040 int
1041 EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
1042 size_t num, const EC_POINT *points[], const BIGNUM *scalars[], BN_CTX *ctx)
1044 if (group->meth->mul == 0)
1045 /* use default */
1046 return ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
1048 return group->meth->mul(group, r, scalar, num, points, scalars, ctx);
1051 int
1052 EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
1053 const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
1055 /* just a convenient interface to EC_POINTs_mul() */
1057 const EC_POINT *points[1];
1058 const BIGNUM *scalars[1];
1060 points[0] = point;
1061 scalars[0] = p_scalar;
1063 return EC_POINTs_mul(group, r, g_scalar,
1064 (point != NULL && p_scalar != NULL),
1065 points, scalars, ctx);
1068 int
1069 EC_GROUP_precompute_mult(EC_GROUP * group, BN_CTX * ctx)
1071 if (group->meth->mul == 0)
1072 /* use default */
1073 return ec_wNAF_precompute_mult(group, ctx);
1075 if (group->meth->precompute_mult != 0)
1076 return group->meth->precompute_mult(group, ctx);
1077 else
1078 return 1; /* nothing to do, so report success */
1081 int
1082 EC_GROUP_have_precompute_mult(const EC_GROUP * group)
1084 if (group->meth->mul == 0)
1085 /* use default */
1086 return ec_wNAF_have_precompute_mult(group);
1088 if (group->meth->have_precompute_mult != 0)
1089 return group->meth->have_precompute_mult(group);
1090 else
1091 return 0; /* cannot tell whether precomputation has
1092 * been performed */
1095 EC_KEY *
1096 ECParameters_dup(EC_KEY *key)
1098 unsigned char *p = NULL;
1099 EC_KEY *k = NULL;
1100 int len;
1102 if (key == NULL)
1103 return (NULL);
1105 if ((len = i2d_ECParameters(key, &p)) > 0)
1106 k = d2i_ECParameters(NULL, (const unsigned char **)&p, len);
1108 return (k);