1 /* $OpenBSD: ec_lib.c,v 1.24 2017/05/02 03:59:44 deraadt Exp $ */
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/opensslconf.h>
68 #include <openssl/err.h>
69 #include <openssl/opensslv.h>
73 /* functions for EC_GROUP objects */
76 EC_GROUP_new(const EC_METHOD
* meth
)
81 ECerror(EC_R_SLOT_FULL
);
84 if (meth
->group_init
== 0) {
85 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
88 ret
= malloc(sizeof *ret
);
90 ECerror(ERR_R_MALLOC_FAILURE
);
95 ret
->extra_data
= NULL
;
97 ret
->generator
= NULL
;
99 BN_init(&ret
->cofactor
);
103 ret
->asn1_form
= POINT_CONVERSION_UNCOMPRESSED
;
108 if (!meth
->group_init(ret
)) {
117 EC_GROUP_free(EC_GROUP
* group
)
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
);
138 EC_GROUP_clear_free(EC_GROUP
* group
)
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 freezero(group
->seed
, group
->seed_len
);
155 freezero(group
, sizeof *group
);
160 EC_GROUP_copy(EC_GROUP
* dest
, const EC_GROUP
* src
)
164 if (dest
->meth
->group_copy
== 0) {
165 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
168 if (dest
->meth
!= src
->meth
) {
169 ECerror(EC_R_INCOMPATIBLE_OBJECTS
);
175 EC_EX_DATA_free_all_data(&dest
->extra_data
);
177 for (d
= src
->extra_data
; d
!= NULL
; d
= d
->next
) {
178 void *t
= d
->dup_func(d
->data
);
182 if (!EC_EX_DATA_set_data(&dest
->extra_data
, t
, d
->dup_func
,
183 d
->free_func
, d
->clear_free_func
))
187 if (src
->generator
!= NULL
) {
188 if (dest
->generator
== NULL
) {
189 dest
->generator
= EC_POINT_new(dest
);
190 if (dest
->generator
== NULL
)
193 if (!EC_POINT_copy(dest
->generator
, src
->generator
))
196 /* src->generator == NULL */
197 EC_POINT_clear_free(dest
->generator
);
198 dest
->generator
= NULL
;
201 if (!BN_copy(&dest
->order
, &src
->order
))
203 if (!BN_copy(&dest
->cofactor
, &src
->cofactor
))
206 dest
->curve_name
= src
->curve_name
;
207 dest
->asn1_flag
= src
->asn1_flag
;
208 dest
->asn1_form
= src
->asn1_form
;
212 dest
->seed
= malloc(src
->seed_len
);
213 if (dest
->seed
== NULL
)
215 memcpy(dest
->seed
, src
->seed
, src
->seed_len
);
216 dest
->seed_len
= src
->seed_len
;
224 return dest
->meth
->group_copy(dest
, src
);
229 EC_GROUP_dup(const EC_GROUP
* a
)
233 if ((a
!= NULL
) && ((t
= EC_GROUP_new(a
->meth
)) != NULL
) &&
234 (!EC_GROUP_copy(t
, a
))) {
243 EC_GROUP_method_of(const EC_GROUP
*group
)
250 EC_METHOD_get_field_type(const EC_METHOD
*meth
)
252 return meth
->field_type
;
257 EC_GROUP_set_generator(EC_GROUP
*group
, const EC_POINT
*generator
,
258 const BIGNUM
*order
, const BIGNUM
*cofactor
)
260 if (generator
== NULL
) {
261 ECerror(ERR_R_PASSED_NULL_PARAMETER
);
264 if (group
->generator
== NULL
) {
265 group
->generator
= EC_POINT_new(group
);
266 if (group
->generator
== NULL
)
269 if (!EC_POINT_copy(group
->generator
, generator
))
273 if (!BN_copy(&group
->order
, order
))
276 BN_zero(&group
->order
);
278 if (cofactor
!= NULL
) {
279 if (!BN_copy(&group
->cofactor
, cofactor
))
282 BN_zero(&group
->cofactor
);
289 EC_GROUP_get0_generator(const EC_GROUP
*group
)
291 return group
->generator
;
296 EC_GROUP_get_order(const EC_GROUP
*group
, BIGNUM
*order
, BN_CTX
*ctx
)
298 if (!BN_copy(order
, &group
->order
))
301 return !BN_is_zero(order
);
306 EC_GROUP_get_cofactor(const EC_GROUP
*group
, BIGNUM
*cofactor
, BN_CTX
*ctx
)
308 if (!BN_copy(cofactor
, &group
->cofactor
))
311 return !BN_is_zero(&group
->cofactor
);
316 EC_GROUP_set_curve_name(EC_GROUP
* group
, int nid
)
318 group
->curve_name
= nid
;
323 EC_GROUP_get_curve_name(const EC_GROUP
* group
)
325 return group
->curve_name
;
330 EC_GROUP_set_asn1_flag(EC_GROUP
* group
, int flag
)
332 group
->asn1_flag
= flag
;
337 EC_GROUP_get_asn1_flag(const EC_GROUP
* group
)
339 return group
->asn1_flag
;
344 EC_GROUP_set_point_conversion_form(EC_GROUP
* group
,
345 point_conversion_form_t form
)
347 group
->asn1_form
= form
;
351 point_conversion_form_t
352 EC_GROUP_get_point_conversion_form(const EC_GROUP
* group
)
354 return group
->asn1_form
;
359 EC_GROUP_set_seed(EC_GROUP
* group
, const unsigned char *p
, size_t len
)
369 if ((group
->seed
= malloc(len
)) == NULL
)
371 memcpy(group
->seed
, p
, len
);
372 group
->seed_len
= len
;
379 EC_GROUP_get0_seed(const EC_GROUP
* group
)
386 EC_GROUP_get_seed_len(const EC_GROUP
* group
)
388 return group
->seed_len
;
393 EC_GROUP_set_curve_GFp(EC_GROUP
* group
, const BIGNUM
* p
, const BIGNUM
* a
,
394 const BIGNUM
* b
, BN_CTX
* ctx
)
396 if (group
->meth
->group_set_curve
== 0) {
397 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
400 return group
->meth
->group_set_curve(group
, p
, a
, b
, ctx
);
405 EC_GROUP_get_curve_GFp(const EC_GROUP
* group
, BIGNUM
* p
, BIGNUM
* a
,
406 BIGNUM
* b
, BN_CTX
* ctx
)
408 if (group
->meth
->group_get_curve
== 0) {
409 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
412 return group
->meth
->group_get_curve(group
, p
, a
, b
, ctx
);
415 #ifndef OPENSSL_NO_EC2M
417 EC_GROUP_set_curve_GF2m(EC_GROUP
* group
, const BIGNUM
* p
, const BIGNUM
* a
,
418 const BIGNUM
* b
, BN_CTX
* ctx
)
420 if (group
->meth
->group_set_curve
== 0) {
421 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
424 return group
->meth
->group_set_curve(group
, p
, a
, b
, ctx
);
429 EC_GROUP_get_curve_GF2m(const EC_GROUP
* group
, BIGNUM
* p
, BIGNUM
* a
,
430 BIGNUM
* b
, BN_CTX
* ctx
)
432 if (group
->meth
->group_get_curve
== 0) {
433 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
436 return group
->meth
->group_get_curve(group
, p
, a
, b
, ctx
);
441 EC_GROUP_get_degree(const EC_GROUP
* group
)
443 if (group
->meth
->group_get_degree
== 0) {
444 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
447 return group
->meth
->group_get_degree(group
);
452 EC_GROUP_check_discriminant(const EC_GROUP
* group
, BN_CTX
* ctx
)
454 if (group
->meth
->group_check_discriminant
== 0) {
455 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
458 return group
->meth
->group_check_discriminant(group
, ctx
);
463 EC_GROUP_cmp(const EC_GROUP
* a
, const EC_GROUP
* b
, BN_CTX
* ctx
)
466 BIGNUM
*a1
, *a2
, *a3
, *b1
, *b2
, *b3
;
467 BN_CTX
*ctx_new
= NULL
;
469 /* compare the field types */
470 if (EC_METHOD_get_field_type(EC_GROUP_method_of(a
)) !=
471 EC_METHOD_get_field_type(EC_GROUP_method_of(b
)))
473 /* compare the curve name (if present in both) */
474 if (EC_GROUP_get_curve_name(a
) && EC_GROUP_get_curve_name(b
) &&
475 EC_GROUP_get_curve_name(a
) != EC_GROUP_get_curve_name(b
))
479 ctx_new
= ctx
= BN_CTX_new();
484 if ((a1
= BN_CTX_get(ctx
)) == NULL
)
486 if ((a2
= BN_CTX_get(ctx
)) == NULL
)
488 if ((a3
= BN_CTX_get(ctx
)) == NULL
)
490 if ((b1
= BN_CTX_get(ctx
)) == NULL
)
492 if ((b2
= BN_CTX_get(ctx
)) == NULL
)
494 if ((b3
= BN_CTX_get(ctx
)) == NULL
)
498 * XXX This approach assumes that the external representation of
499 * curves over the same field type is the same.
501 if (!a
->meth
->group_get_curve(a
, a1
, a2
, a3
, ctx
) ||
502 !b
->meth
->group_get_curve(b
, b1
, b2
, b3
, ctx
))
505 if (r
|| BN_cmp(a1
, b1
) || BN_cmp(a2
, b2
) || BN_cmp(a3
, b3
))
508 /* XXX EC_POINT_cmp() assumes that the methods are equal */
509 if (r
|| EC_POINT_cmp(a
, EC_GROUP_get0_generator(a
),
510 EC_GROUP_get0_generator(b
), ctx
))
514 /* compare the order and cofactor */
515 if (!EC_GROUP_get_order(a
, a1
, ctx
) ||
516 !EC_GROUP_get_order(b
, b1
, ctx
) ||
517 !EC_GROUP_get_cofactor(a
, a2
, ctx
) ||
518 !EC_GROUP_get_cofactor(b
, b2
, ctx
))
520 if (BN_cmp(a1
, b1
) || BN_cmp(a2
, b2
))
537 /* this has 'package' visibility */
539 EC_EX_DATA_set_data(EC_EXTRA_DATA
** ex_data
, void *data
,
540 void *(*dup_func
) (void *),
541 void (*free_func
) (void *),
542 void (*clear_free_func
) (void *))
549 for (d
= *ex_data
; d
!= NULL
; d
= d
->next
) {
550 if (d
->dup_func
== dup_func
&& d
->free_func
== free_func
&&
551 d
->clear_free_func
== clear_free_func
) {
552 ECerror(EC_R_SLOT_FULL
);
558 /* no explicit entry needed */
561 d
= malloc(sizeof *d
);
566 d
->dup_func
= dup_func
;
567 d
->free_func
= free_func
;
568 d
->clear_free_func
= clear_free_func
;
576 /* this has 'package' visibility */
578 EC_EX_DATA_get_data(const EC_EXTRA_DATA
* ex_data
,
579 void *(*dup_func
) (void *),
580 void (*free_func
) (void *),
581 void (*clear_free_func
) (void *))
583 const EC_EXTRA_DATA
*d
;
585 for (d
= ex_data
; d
!= NULL
; d
= d
->next
) {
586 if (d
->dup_func
== dup_func
&& d
->free_func
== free_func
&& d
->clear_free_func
== clear_free_func
)
593 /* this has 'package' visibility */
595 EC_EX_DATA_free_data(EC_EXTRA_DATA
** ex_data
,
596 void *(*dup_func
) (void *),
597 void (*free_func
) (void *),
598 void (*clear_free_func
) (void *))
605 for (p
= ex_data
; *p
!= NULL
; p
= &((*p
)->next
)) {
606 if ((*p
)->dup_func
== dup_func
&&
607 (*p
)->free_func
== free_func
&&
608 (*p
)->clear_free_func
== clear_free_func
) {
609 EC_EXTRA_DATA
*next
= (*p
)->next
;
611 (*p
)->free_func((*p
)->data
);
620 /* this has 'package' visibility */
622 EC_EX_DATA_clear_free_data(EC_EXTRA_DATA
** ex_data
,
623 void *(*dup_func
) (void *),
624 void (*free_func
) (void *),
625 void (*clear_free_func
) (void *))
632 for (p
= ex_data
; *p
!= NULL
; p
= &((*p
)->next
)) {
633 if ((*p
)->dup_func
== dup_func
&&
634 (*p
)->free_func
== free_func
&&
635 (*p
)->clear_free_func
== clear_free_func
) {
636 EC_EXTRA_DATA
*next
= (*p
)->next
;
638 (*p
)->clear_free_func((*p
)->data
);
647 /* this has 'package' visibility */
649 EC_EX_DATA_free_all_data(EC_EXTRA_DATA
** ex_data
)
658 EC_EXTRA_DATA
*next
= d
->next
;
660 d
->free_func(d
->data
);
668 /* this has 'package' visibility */
670 EC_EX_DATA_clear_free_all_data(EC_EXTRA_DATA
** ex_data
)
679 EC_EXTRA_DATA
*next
= d
->next
;
681 d
->clear_free_func(d
->data
);
690 /* functions for EC_POINT objects */
693 EC_POINT_new(const EC_GROUP
* group
)
698 ECerror(ERR_R_PASSED_NULL_PARAMETER
);
701 if (group
->meth
->point_init
== 0) {
702 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
705 ret
= malloc(sizeof *ret
);
707 ECerror(ERR_R_MALLOC_FAILURE
);
710 ret
->meth
= group
->meth
;
712 if (!ret
->meth
->point_init(ret
)) {
721 EC_POINT_free(EC_POINT
* point
)
726 if (point
->meth
->point_finish
!= 0)
727 point
->meth
->point_finish(point
);
733 EC_POINT_clear_free(EC_POINT
* point
)
738 if (point
->meth
->point_clear_finish
!= 0)
739 point
->meth
->point_clear_finish(point
);
740 else if (point
->meth
->point_finish
!= 0)
741 point
->meth
->point_finish(point
);
742 freezero(point
, sizeof *point
);
747 EC_POINT_copy(EC_POINT
* dest
, const EC_POINT
* src
)
749 if (dest
->meth
->point_copy
== 0) {
750 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
753 if (dest
->meth
!= src
->meth
) {
754 ECerror(EC_R_INCOMPATIBLE_OBJECTS
);
759 return dest
->meth
->point_copy(dest
, src
);
764 EC_POINT_dup(const EC_POINT
* a
, const EC_GROUP
* group
)
772 t
= EC_POINT_new(group
);
775 r
= EC_POINT_copy(t
, a
);
785 EC_POINT_method_of(const EC_POINT
* point
)
792 EC_POINT_set_to_infinity(const EC_GROUP
* group
, EC_POINT
* point
)
794 if (group
->meth
->point_set_to_infinity
== 0) {
795 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
798 if (group
->meth
!= point
->meth
) {
799 ECerror(EC_R_INCOMPATIBLE_OBJECTS
);
802 return group
->meth
->point_set_to_infinity(group
, point
);
807 EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP
*group
, EC_POINT
*point
,
808 const BIGNUM
*x
, const BIGNUM
*y
, const BIGNUM
*z
, BN_CTX
*ctx
)
810 if (group
->meth
->point_set_Jprojective_coordinates_GFp
== 0) {
811 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
814 if (group
->meth
!= point
->meth
) {
815 ECerror(EC_R_INCOMPATIBLE_OBJECTS
);
818 return group
->meth
->point_set_Jprojective_coordinates_GFp(group
, point
, x
, y
, z
, ctx
);
823 EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP
*group
,
824 const EC_POINT
*point
, BIGNUM
*x
, BIGNUM
*y
, BIGNUM
*z
, BN_CTX
*ctx
)
826 if (group
->meth
->point_get_Jprojective_coordinates_GFp
== 0) {
827 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
830 if (group
->meth
!= point
->meth
) {
831 ECerror(EC_R_INCOMPATIBLE_OBJECTS
);
834 return group
->meth
->point_get_Jprojective_coordinates_GFp(group
, point
, x
, y
, z
, ctx
);
839 EC_POINT_set_affine_coordinates_GFp(const EC_GROUP
*group
, EC_POINT
*point
,
840 const BIGNUM
*x
, const BIGNUM
*y
, BN_CTX
*ctx
)
842 if (group
->meth
->point_set_affine_coordinates
== 0) {
843 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
846 if (group
->meth
!= point
->meth
) {
847 ECerror(EC_R_INCOMPATIBLE_OBJECTS
);
850 return group
->meth
->point_set_affine_coordinates(group
, point
, x
, y
, ctx
);
853 #ifndef OPENSSL_NO_EC2M
855 EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP
*group
, EC_POINT
*point
,
856 const BIGNUM
*x
, const BIGNUM
*y
, BN_CTX
*ctx
)
858 if (group
->meth
->point_set_affine_coordinates
== 0) {
859 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
862 if (group
->meth
!= point
->meth
) {
863 ECerror(EC_R_INCOMPATIBLE_OBJECTS
);
866 return group
->meth
->point_set_affine_coordinates(group
, point
, x
, y
, ctx
);
871 EC_POINT_get_affine_coordinates_GFp(const EC_GROUP
*group
, const EC_POINT
*point
,
872 BIGNUM
*x
, BIGNUM
*y
, BN_CTX
*ctx
)
874 if (group
->meth
->point_get_affine_coordinates
== 0) {
875 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
878 if (group
->meth
!= point
->meth
) {
879 ECerror(EC_R_INCOMPATIBLE_OBJECTS
);
882 return group
->meth
->point_get_affine_coordinates(group
, point
, x
, y
, ctx
);
885 #ifndef OPENSSL_NO_EC2M
887 EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP
*group
, const EC_POINT
*point
,
888 BIGNUM
*x
, BIGNUM
*y
, BN_CTX
*ctx
)
890 if (group
->meth
->point_get_affine_coordinates
== 0) {
891 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
894 if (group
->meth
!= point
->meth
) {
895 ECerror(EC_R_INCOMPATIBLE_OBJECTS
);
898 return group
->meth
->point_get_affine_coordinates(group
, point
, x
, y
, ctx
);
903 EC_POINT_add(const EC_GROUP
*group
, EC_POINT
*r
, const EC_POINT
*a
,
904 const EC_POINT
*b
, BN_CTX
*ctx
)
906 if (group
->meth
->add
== 0) {
907 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
910 if ((group
->meth
!= r
->meth
) || (r
->meth
!= a
->meth
) || (a
->meth
!= b
->meth
)) {
911 ECerror(EC_R_INCOMPATIBLE_OBJECTS
);
914 return group
->meth
->add(group
, r
, a
, b
, ctx
);
919 EC_POINT_dbl(const EC_GROUP
*group
, EC_POINT
*r
, const EC_POINT
*a
, BN_CTX
*ctx
)
921 if (group
->meth
->dbl
== 0) {
922 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
925 if ((group
->meth
!= r
->meth
) || (r
->meth
!= a
->meth
)) {
926 ECerror(EC_R_INCOMPATIBLE_OBJECTS
);
929 return group
->meth
->dbl(group
, r
, a
, ctx
);
934 EC_POINT_invert(const EC_GROUP
*group
, EC_POINT
*a
, BN_CTX
*ctx
)
936 if (group
->meth
->invert
== 0) {
937 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
940 if (group
->meth
!= a
->meth
) {
941 ECerror(EC_R_INCOMPATIBLE_OBJECTS
);
944 return group
->meth
->invert(group
, a
, ctx
);
949 EC_POINT_is_at_infinity(const EC_GROUP
*group
, const EC_POINT
*point
)
951 if (group
->meth
->is_at_infinity
== 0) {
952 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
955 if (group
->meth
!= point
->meth
) {
956 ECerror(EC_R_INCOMPATIBLE_OBJECTS
);
959 return group
->meth
->is_at_infinity(group
, point
);
964 EC_POINT_is_on_curve(const EC_GROUP
* group
, const EC_POINT
* point
, BN_CTX
* ctx
)
966 if (group
->meth
->is_on_curve
== 0) {
967 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
970 if (group
->meth
!= point
->meth
) {
971 ECerror(EC_R_INCOMPATIBLE_OBJECTS
);
974 return group
->meth
->is_on_curve(group
, point
, ctx
);
979 EC_POINT_cmp(const EC_GROUP
*group
, const EC_POINT
*a
, const EC_POINT
*b
,
982 if (group
->meth
->point_cmp
== 0) {
983 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
986 if ((group
->meth
!= a
->meth
) || (a
->meth
!= b
->meth
)) {
987 ECerror(EC_R_INCOMPATIBLE_OBJECTS
);
990 return group
->meth
->point_cmp(group
, a
, b
, ctx
);
995 EC_POINT_make_affine(const EC_GROUP
*group
, EC_POINT
*point
, BN_CTX
*ctx
)
997 if (group
->meth
->make_affine
== 0) {
998 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1001 if (group
->meth
!= point
->meth
) {
1002 ECerror(EC_R_INCOMPATIBLE_OBJECTS
);
1005 return group
->meth
->make_affine(group
, point
, ctx
);
1010 EC_POINTs_make_affine(const EC_GROUP
*group
, size_t num
, EC_POINT
*points
[],
1015 if (group
->meth
->points_make_affine
== 0) {
1016 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1019 for (i
= 0; i
< num
; i
++) {
1020 if (group
->meth
!= points
[i
]->meth
) {
1021 ECerror(EC_R_INCOMPATIBLE_OBJECTS
);
1025 return group
->meth
->points_make_affine(group
, num
, points
, ctx
);
1029 /* Functions for point multiplication.
1031 * If group->meth->mul is 0, we use the wNAF-based implementations in ec_mult.c;
1032 * otherwise we dispatch through methods.
1036 EC_POINTs_mul(const EC_GROUP
*group
, EC_POINT
*r
, const BIGNUM
*scalar
,
1037 size_t num
, const EC_POINT
*points
[], const BIGNUM
*scalars
[], BN_CTX
*ctx
)
1039 if (group
->meth
->mul
== 0)
1041 return ec_wNAF_mul(group
, r
, scalar
, num
, points
, scalars
, ctx
);
1043 return group
->meth
->mul(group
, r
, scalar
, num
, points
, scalars
, ctx
);
1047 EC_POINT_mul(const EC_GROUP
*group
, EC_POINT
*r
, const BIGNUM
*g_scalar
,
1048 const EC_POINT
*point
, const BIGNUM
*p_scalar
, BN_CTX
*ctx
)
1050 /* just a convenient interface to EC_POINTs_mul() */
1052 const EC_POINT
*points
[1];
1053 const BIGNUM
*scalars
[1];
1056 scalars
[0] = p_scalar
;
1058 return EC_POINTs_mul(group
, r
, g_scalar
,
1059 (point
!= NULL
&& p_scalar
!= NULL
),
1060 points
, scalars
, ctx
);
1064 EC_GROUP_precompute_mult(EC_GROUP
* group
, BN_CTX
* ctx
)
1066 if (group
->meth
->mul
== 0)
1068 return ec_wNAF_precompute_mult(group
, ctx
);
1070 if (group
->meth
->precompute_mult
!= 0)
1071 return group
->meth
->precompute_mult(group
, ctx
);
1073 return 1; /* nothing to do, so report success */
1077 EC_GROUP_have_precompute_mult(const EC_GROUP
* group
)
1079 if (group
->meth
->mul
== 0)
1081 return ec_wNAF_have_precompute_mult(group
);
1083 if (group
->meth
->have_precompute_mult
!= 0)
1084 return group
->meth
->have_precompute_mult(group
);
1086 return 0; /* cannot tell whether precomputation has
1091 ECParameters_dup(EC_KEY
*key
)
1093 unsigned char *p
= NULL
;
1100 if ((len
= i2d_ECParameters(key
, &p
)) > 0)
1101 k
= d2i_ECParameters(NULL
, (const unsigned char **)&p
, len
);