2 * Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
13 #include <openssl/err.h>
14 #include <openssl/opensslv.h>
18 /* functions for EC_GROUP objects */
20 EC_GROUP *EC_GROUP_new_ex(OPENSSL_CTX *libctx, const EC_METHOD *meth)
25 ECerr(EC_F_EC_GROUP_NEW_EX, EC_R_SLOT_FULL);
28 if (meth->group_init == 0) {
29 ECerr(EC_F_EC_GROUP_NEW_EX, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
33 ret = OPENSSL_zalloc(sizeof(*ret));
35 ECerr(EC_F_EC_GROUP_NEW_EX, ERR_R_MALLOC_FAILURE);
41 if ((ret->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
42 ret->order = BN_new();
43 if (ret->order == NULL)
45 ret->cofactor = BN_new();
46 if (ret->cofactor == NULL)
49 ret->asn1_flag = OPENSSL_EC_NAMED_CURVE;
50 ret->asn1_form = POINT_CONVERSION_UNCOMPRESSED;
51 if (!meth->group_init(ret))
57 BN_free(ret->cofactor);
63 EC_GROUP *EC_GROUP_new(const EC_METHOD *meth)
65 return EC_GROUP_new_ex(NULL, meth);
69 void EC_pre_comp_free(EC_GROUP *group)
71 switch (group->pre_comp_type) {
75 #ifdef ECP_NISTZ256_ASM
76 EC_nistz256_pre_comp_free(group->pre_comp.nistz256);
79 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
81 EC_nistp224_pre_comp_free(group->pre_comp.nistp224);
84 EC_nistp256_pre_comp_free(group->pre_comp.nistp256);
87 EC_nistp521_pre_comp_free(group->pre_comp.nistp521);
96 EC_ec_pre_comp_free(group->pre_comp.ec);
99 group->pre_comp.ec = NULL;
102 void EC_GROUP_free(EC_GROUP *group)
107 if (group->meth->group_finish != 0)
108 group->meth->group_finish(group);
110 EC_pre_comp_free(group);
111 BN_MONT_CTX_free(group->mont_data);
112 EC_POINT_free(group->generator);
113 BN_free(group->order);
114 BN_free(group->cofactor);
115 OPENSSL_free(group->seed);
119 void EC_GROUP_clear_free(EC_GROUP *group)
124 if (group->meth->group_clear_finish != 0)
125 group->meth->group_clear_finish(group);
126 else if (group->meth->group_finish != 0)
127 group->meth->group_finish(group);
129 EC_pre_comp_free(group);
130 BN_MONT_CTX_free(group->mont_data);
131 EC_POINT_clear_free(group->generator);
132 BN_clear_free(group->order);
133 BN_clear_free(group->cofactor);
134 OPENSSL_clear_free(group->seed, group->seed_len);
135 OPENSSL_clear_free(group, sizeof(*group));
138 int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)
140 if (dest->meth->group_copy == 0) {
141 ECerr(EC_F_EC_GROUP_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
144 if (dest->meth != src->meth) {
145 ECerr(EC_F_EC_GROUP_COPY, EC_R_INCOMPATIBLE_OBJECTS);
151 dest->libctx = src->libctx;
152 dest->curve_name = src->curve_name;
154 /* Copy precomputed */
155 dest->pre_comp_type = src->pre_comp_type;
156 switch (src->pre_comp_type) {
158 dest->pre_comp.ec = NULL;
161 #ifdef ECP_NISTZ256_ASM
162 dest->pre_comp.nistz256 = EC_nistz256_pre_comp_dup(src->pre_comp.nistz256);
165 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
167 dest->pre_comp.nistp224 = EC_nistp224_pre_comp_dup(src->pre_comp.nistp224);
170 dest->pre_comp.nistp256 = EC_nistp256_pre_comp_dup(src->pre_comp.nistp256);
173 dest->pre_comp.nistp521 = EC_nistp521_pre_comp_dup(src->pre_comp.nistp521);
182 dest->pre_comp.ec = EC_ec_pre_comp_dup(src->pre_comp.ec);
186 if (src->mont_data != NULL) {
187 if (dest->mont_data == NULL) {
188 dest->mont_data = BN_MONT_CTX_new();
189 if (dest->mont_data == NULL)
192 if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data))
195 /* src->generator == NULL */
196 BN_MONT_CTX_free(dest->mont_data);
197 dest->mont_data = NULL;
200 if (src->generator != NULL) {
201 if (dest->generator == NULL) {
202 dest->generator = EC_POINT_new(dest);
203 if (dest->generator == NULL)
206 if (!EC_POINT_copy(dest->generator, src->generator))
209 /* src->generator == NULL */
210 EC_POINT_clear_free(dest->generator);
211 dest->generator = NULL;
214 if ((src->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
215 if (!BN_copy(dest->order, src->order))
217 if (!BN_copy(dest->cofactor, src->cofactor))
221 dest->asn1_flag = src->asn1_flag;
222 dest->asn1_form = src->asn1_form;
225 OPENSSL_free(dest->seed);
226 if ((dest->seed = OPENSSL_malloc(src->seed_len)) == NULL) {
227 ECerr(EC_F_EC_GROUP_COPY, ERR_R_MALLOC_FAILURE);
230 if (!memcpy(dest->seed, src->seed, src->seed_len))
232 dest->seed_len = src->seed_len;
234 OPENSSL_free(dest->seed);
239 return dest->meth->group_copy(dest, src);
242 EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)
250 if ((t = EC_GROUP_new_ex(a->libctx, a->meth)) == NULL)
252 if (!EC_GROUP_copy(t, a))
265 const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group)
270 int EC_METHOD_get_field_type(const EC_METHOD *meth)
272 return meth->field_type;
275 static int ec_precompute_mont_data(EC_GROUP *);
277 int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
278 const BIGNUM *order, const BIGNUM *cofactor)
280 if (generator == NULL) {
281 ECerr(EC_F_EC_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER);
285 if (group->generator == NULL) {
286 group->generator = EC_POINT_new(group);
287 if (group->generator == NULL)
290 if (!EC_POINT_copy(group->generator, generator))
294 if (!BN_copy(group->order, order))
297 BN_zero(group->order);
300 /* The cofactor is an optional field, so it should be able to be NULL. */
301 if (cofactor != NULL) {
302 if (!BN_copy(group->cofactor, cofactor))
305 BN_zero(group->cofactor);
308 * Some groups have an order with
309 * factors of two, which makes the Montgomery setup fail.
310 * |group->mont_data| will be NULL in this case.
312 if (BN_is_odd(group->order)) {
313 return ec_precompute_mont_data(group);
316 BN_MONT_CTX_free(group->mont_data);
317 group->mont_data = NULL;
321 const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
323 return group->generator;
326 BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group)
328 return group->mont_data;
331 int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
333 if (group->order == NULL)
335 if (!BN_copy(order, group->order))
338 return !BN_is_zero(order);
341 const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group)
346 int EC_GROUP_order_bits(const EC_GROUP *group)
348 return group->meth->group_order_bits(group);
351 int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
355 if (group->cofactor == NULL)
357 if (!BN_copy(cofactor, group->cofactor))
360 return !BN_is_zero(group->cofactor);
363 const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group)
365 return 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 const BIGNUM *EC_GROUP_get0_field(const EC_GROUP *group)
383 void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
385 group->asn1_flag = flag;
388 int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
390 return group->asn1_flag;
393 void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
394 point_conversion_form_t form)
396 group->asn1_form = form;
399 point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP
402 return group->asn1_form;
405 size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
407 OPENSSL_free(group->seed);
414 if ((group->seed = OPENSSL_malloc(len)) == NULL) {
415 ECerr(EC_F_EC_GROUP_SET_SEED, ERR_R_MALLOC_FAILURE);
418 memcpy(group->seed, p, len);
419 group->seed_len = len;
424 unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
429 size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
431 return group->seed_len;
434 int EC_GROUP_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
435 const BIGNUM *b, BN_CTX *ctx)
437 if (group->meth->group_set_curve == 0) {
438 ECerr(EC_F_EC_GROUP_SET_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
441 return group->meth->group_set_curve(group, p, a, b, ctx);
444 int EC_GROUP_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b,
447 if (group->meth->group_get_curve == NULL) {
448 ECerr(EC_F_EC_GROUP_GET_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
451 return group->meth->group_get_curve(group, p, a, b, ctx);
455 int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
456 const BIGNUM *b, BN_CTX *ctx)
458 return EC_GROUP_set_curve(group, p, a, b, ctx);
461 int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
462 BIGNUM *b, BN_CTX *ctx)
464 return EC_GROUP_get_curve(group, p, a, b, ctx);
467 # ifndef OPENSSL_NO_EC2M
468 int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
469 const BIGNUM *b, BN_CTX *ctx)
471 return EC_GROUP_set_curve(group, p, a, b, ctx);
474 int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
475 BIGNUM *b, BN_CTX *ctx)
477 return EC_GROUP_get_curve(group, p, a, b, ctx);
482 int EC_GROUP_get_degree(const EC_GROUP *group)
484 if (group->meth->group_get_degree == 0) {
485 ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
488 return group->meth->group_get_degree(group);
491 int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
493 if (group->meth->group_check_discriminant == 0) {
494 ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT,
495 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
498 return group->meth->group_check_discriminant(group, ctx);
501 int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
504 BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
506 BN_CTX *ctx_new = NULL;
509 ctx_new = ctx = BN_CTX_new();
514 /* compare the field types */
515 if (EC_METHOD_get_field_type(EC_GROUP_method_of(a)) !=
516 EC_METHOD_get_field_type(EC_GROUP_method_of(b)))
518 /* compare the curve name (if present in both) */
519 if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
520 EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
522 if (a->meth->flags & EC_FLAGS_CUSTOM_CURVE)
526 a1 = BN_CTX_get(ctx);
527 a2 = BN_CTX_get(ctx);
528 a3 = BN_CTX_get(ctx);
529 b1 = BN_CTX_get(ctx);
530 b2 = BN_CTX_get(ctx);
531 b3 = BN_CTX_get(ctx);
535 BN_CTX_free(ctx_new);
541 * XXX This approach assumes that the external representation of curves
542 * over the same field type is the same.
544 if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
545 !b->meth->group_get_curve(b, b1, b2, b3, ctx))
548 /* return 1 if the curve parameters are different */
549 if (r || BN_cmp(a1, b1) != 0 || BN_cmp(a2, b2) != 0 || BN_cmp(a3, b3) != 0)
552 /* XXX EC_POINT_cmp() assumes that the methods are equal */
553 /* return 1 if the generators are different */
554 if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
555 EC_GROUP_get0_generator(b), ctx) != 0)
559 const BIGNUM *ao, *bo, *ac, *bc;
560 /* compare the orders */
561 ao = EC_GROUP_get0_order(a);
562 bo = EC_GROUP_get0_order(b);
563 if (ao == NULL || bo == NULL) {
564 /* return an error if either order is NULL */
568 if (BN_cmp(ao, bo) != 0) {
569 /* return 1 if orders are different */
574 * It gets here if the curve parameters and generator matched.
575 * Now check the optional cofactors (if both are present).
577 ac = EC_GROUP_get0_cofactor(a);
578 bc = EC_GROUP_get0_cofactor(b);
579 /* Returns 1 (mismatch) if both cofactors are specified and different */
580 if (!BN_is_zero(ac) && !BN_is_zero(bc) && BN_cmp(ac, bc) != 0)
582 /* Returns 0 if the parameters matched */
587 BN_CTX_free(ctx_new);
592 /* functions for EC_POINT objects */
594 EC_POINT *EC_POINT_new(const EC_GROUP *group)
599 ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER);
602 if (group->meth->point_init == NULL) {
603 ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
607 ret = OPENSSL_zalloc(sizeof(*ret));
609 ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE);
613 ret->meth = group->meth;
614 ret->curve_name = group->curve_name;
616 if (!ret->meth->point_init(ret)) {
624 void EC_POINT_free(EC_POINT *point)
629 if (point->meth->point_finish != 0)
630 point->meth->point_finish(point);
634 void EC_POINT_clear_free(EC_POINT *point)
639 if (point->meth->point_clear_finish != 0)
640 point->meth->point_clear_finish(point);
641 else if (point->meth->point_finish != 0)
642 point->meth->point_finish(point);
643 OPENSSL_clear_free(point, sizeof(*point));
646 int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)
648 if (dest->meth->point_copy == 0) {
649 ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
652 if (dest->meth != src->meth
653 || (dest->curve_name != src->curve_name
654 && dest->curve_name != 0
655 && src->curve_name != 0)) {
656 ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS);
661 return dest->meth->point_copy(dest, src);
664 EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
672 t = EC_POINT_new(group);
675 r = EC_POINT_copy(t, a);
683 const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
688 int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
690 if (group->meth->point_set_to_infinity == 0) {
691 ECerr(EC_F_EC_POINT_SET_TO_INFINITY,
692 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
695 if (group->meth != point->meth) {
696 ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
699 return group->meth->point_set_to_infinity(group, point);
702 int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
703 EC_POINT *point, const BIGNUM *x,
704 const BIGNUM *y, const BIGNUM *z,
707 if (group->meth->point_set_Jprojective_coordinates_GFp == 0) {
708 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
709 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
712 if (!ec_point_is_compat(point, group)) {
713 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
714 EC_R_INCOMPATIBLE_OBJECTS);
717 return group->meth->point_set_Jprojective_coordinates_GFp(group, point, x,
721 int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
722 const EC_POINT *point, BIGNUM *x,
723 BIGNUM *y, BIGNUM *z,
726 if (group->meth->point_get_Jprojective_coordinates_GFp == 0) {
727 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
728 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
731 if (!ec_point_is_compat(point, group)) {
732 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
733 EC_R_INCOMPATIBLE_OBJECTS);
736 return group->meth->point_get_Jprojective_coordinates_GFp(group, point, x,
740 int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point,
741 const BIGNUM *x, const BIGNUM *y,
744 if (group->meth->point_set_affine_coordinates == NULL) {
745 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES,
746 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
749 if (!ec_point_is_compat(point, group)) {
750 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, EC_R_INCOMPATIBLE_OBJECTS);
753 if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx))
756 if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {
757 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, EC_R_POINT_IS_NOT_ON_CURVE);
764 int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
765 EC_POINT *point, const BIGNUM *x,
766 const BIGNUM *y, BN_CTX *ctx)
768 return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
771 # ifndef OPENSSL_NO_EC2M
772 int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,
773 EC_POINT *point, const BIGNUM *x,
774 const BIGNUM *y, BN_CTX *ctx)
776 return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
781 int EC_POINT_get_affine_coordinates(const EC_GROUP *group,
782 const EC_POINT *point, BIGNUM *x, BIGNUM *y,
785 if (group->meth->point_get_affine_coordinates == NULL) {
786 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES,
787 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
790 if (!ec_point_is_compat(point, group)) {
791 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, EC_R_INCOMPATIBLE_OBJECTS);
794 if (EC_POINT_is_at_infinity(group, point)) {
795 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, EC_R_POINT_AT_INFINITY);
798 return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
802 int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
803 const EC_POINT *point, BIGNUM *x,
804 BIGNUM *y, BN_CTX *ctx)
806 return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
809 # ifndef OPENSSL_NO_EC2M
810 int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,
811 const EC_POINT *point, BIGNUM *x,
812 BIGNUM *y, BN_CTX *ctx)
814 return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
819 int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
820 const EC_POINT *b, BN_CTX *ctx)
822 if (group->meth->add == 0) {
823 ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
826 if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)
827 || !ec_point_is_compat(b, group)) {
828 ECerr(EC_F_EC_POINT_ADD, EC_R_INCOMPATIBLE_OBJECTS);
831 return group->meth->add(group, r, a, b, ctx);
834 int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
837 if (group->meth->dbl == 0) {
838 ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
841 if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)) {
842 ECerr(EC_F_EC_POINT_DBL, EC_R_INCOMPATIBLE_OBJECTS);
845 return group->meth->dbl(group, r, a, ctx);
848 int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
850 if (group->meth->invert == 0) {
851 ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
854 if (!ec_point_is_compat(a, group)) {
855 ECerr(EC_F_EC_POINT_INVERT, EC_R_INCOMPATIBLE_OBJECTS);
858 return group->meth->invert(group, a, ctx);
861 int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
863 if (group->meth->is_at_infinity == 0) {
864 ECerr(EC_F_EC_POINT_IS_AT_INFINITY,
865 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
868 if (!ec_point_is_compat(point, group)) {
869 ECerr(EC_F_EC_POINT_IS_AT_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
872 return group->meth->is_at_infinity(group, point);
876 * Check whether an EC_POINT is on the curve or not. Note that the return
877 * value for this function should NOT be treated as a boolean. Return values:
878 * 1: The point is on the curve
879 * 0: The point is not on the curve
880 * -1: An error occurred
882 int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
885 if (group->meth->is_on_curve == 0) {
886 ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
889 if (!ec_point_is_compat(point, group)) {
890 ECerr(EC_F_EC_POINT_IS_ON_CURVE, EC_R_INCOMPATIBLE_OBJECTS);
893 return group->meth->is_on_curve(group, point, ctx);
896 int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
899 if (group->meth->point_cmp == 0) {
900 ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
903 if (!ec_point_is_compat(a, group) || !ec_point_is_compat(b, group)) {
904 ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS);
907 return group->meth->point_cmp(group, a, b, ctx);
910 int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
912 if (group->meth->make_affine == 0) {
913 ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
916 if (!ec_point_is_compat(point, group)) {
917 ECerr(EC_F_EC_POINT_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
920 return group->meth->make_affine(group, point, ctx);
923 int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
924 EC_POINT *points[], BN_CTX *ctx)
928 if (group->meth->points_make_affine == 0) {
929 ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
932 for (i = 0; i < num; i++) {
933 if (!ec_point_is_compat(points[i], group)) {
934 ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
938 return group->meth->points_make_affine(group, num, points, ctx);
942 * Functions for point multiplication. If group->meth->mul is 0, we use the
943 * wNAF-based implementations in ec_mult.c; otherwise we dispatch through
947 int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
948 size_t num, const EC_POINT *points[],
949 const BIGNUM *scalars[], BN_CTX *ctx)
954 BN_CTX *new_ctx = NULL;
957 ctx = new_ctx = BN_CTX_secure_new();
960 ECerr(EC_F_EC_POINTS_MUL, ERR_R_INTERNAL_ERROR);
964 if ((scalar == NULL) && (num == 0)) {
965 return EC_POINT_set_to_infinity(group, r);
968 if (!ec_point_is_compat(r, group)) {
969 ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
972 for (i = 0; i < num; i++) {
973 if (!ec_point_is_compat(points[i], group)) {
974 ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
979 if (group->meth->mul != NULL)
980 ret = group->meth->mul(group, r, scalar, num, points, scalars, ctx);
983 ret = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
986 BN_CTX_free(new_ctx);
991 int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
992 const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
994 /* just a convenient interface to EC_POINTs_mul() */
996 const EC_POINT *points[1];
997 const BIGNUM *scalars[1];
1000 scalars[0] = p_scalar;
1002 return EC_POINTs_mul(group, r, g_scalar,
1004 && p_scalar != NULL), points, scalars, ctx);
1007 int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
1009 if (group->meth->mul == 0)
1011 return ec_wNAF_precompute_mult(group, ctx);
1013 if (group->meth->precompute_mult != 0)
1014 return group->meth->precompute_mult(group, ctx);
1016 return 1; /* nothing to do, so report success */
1019 int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
1021 if (group->meth->mul == 0)
1023 return ec_wNAF_have_precompute_mult(group);
1025 if (group->meth->have_precompute_mult != 0)
1026 return group->meth->have_precompute_mult(group);
1028 return 0; /* cannot tell whether precomputation has
1033 * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
1034 * returns one on success. On error it returns zero.
1036 static int ec_precompute_mont_data(EC_GROUP *group)
1038 BN_CTX *ctx = BN_CTX_new_ex(group->libctx);
1041 BN_MONT_CTX_free(group->mont_data);
1042 group->mont_data = NULL;
1047 group->mont_data = BN_MONT_CTX_new();
1048 if (group->mont_data == NULL)
1051 if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) {
1052 BN_MONT_CTX_free(group->mont_data);
1053 group->mont_data = NULL;
1066 int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg)
1068 return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
1071 void *EC_KEY_get_ex_data(const EC_KEY *key, int idx)
1073 return CRYPTO_get_ex_data(&key->ex_data, idx);
1077 int ec_group_simple_order_bits(const EC_GROUP *group)
1079 if (group->order == NULL)
1081 return BN_num_bits(group->order);
1084 static int ec_field_inverse_mod_ord(const EC_GROUP *group, BIGNUM *r,
1085 const BIGNUM *x, BN_CTX *ctx)
1090 BN_CTX *new_ctx = NULL;
1093 ctx = new_ctx = BN_CTX_secure_new();
1098 if (group->mont_data == NULL)
1102 if ((e = BN_CTX_get(ctx)) == NULL)
1106 * We want inverse in constant time, therefore we utilize the fact
1107 * order must be prime and use Fermats Little Theorem instead.
1109 if (!BN_set_word(e, 2))
1111 if (!BN_sub(e, group->order, e))
1114 * Exponent e is public.
1115 * No need for scatter-gather or BN_FLG_CONSTTIME.
1117 if (!BN_mod_exp_mont(r, x, e, group->order, ctx, group->mont_data))
1125 BN_CTX_free(new_ctx);
1131 * Default behavior, if group->meth->field_inverse_mod_ord is NULL:
1132 * - When group->order is even, this function returns an error.
1133 * - When group->order is otherwise composite, the correctness
1134 * of the output is not guaranteed.
1135 * - When x is outside the range [1, group->order), the correctness
1136 * of the output is not guaranteed.
1137 * - Otherwise, this function returns the multiplicative inverse in the
1138 * range [1, group->order).
1140 * EC_METHODs must implement their own field_inverse_mod_ord for
1141 * other functionality.
1143 int ec_group_do_inverse_ord(const EC_GROUP *group, BIGNUM *res,
1144 const BIGNUM *x, BN_CTX *ctx)
1146 if (group->meth->field_inverse_mod_ord != NULL)
1147 return group->meth->field_inverse_mod_ord(group, res, x, ctx);
1149 return ec_field_inverse_mod_ord(group, res, x, ctx);
1153 * Coordinate blinding for EC_POINT.
1155 * The underlying EC_METHOD can optionally implement this function:
1156 * underlying implementations should return 0 on errors, or 1 on
1159 * This wrapper returns 1 in case the underlying EC_METHOD does not
1160 * support coordinate blinding.
1162 int ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p, BN_CTX *ctx)
1164 if (group->meth->blind_coordinates == NULL)
1165 return 1; /* ignore if not implemented */
1167 return group->meth->blind_coordinates(group, p, ctx);