From 56c2d56ad840436ee3e6f9e935795a718b9edb37 Mon Sep 17 00:00:00 2001 From: tb <> Date: Sat, 30 Nov 2024 21:09:59 +0000 Subject: Inline trivial EC point methods Like most of the "group" methods these are shared between Montgomery curves and simple curves. There's no point in five methods hanging off the EC_METHODS struct whne they can just as well be inlined in the public API. It makes all files involved shorter... ok jsing --- src/lib/libcrypto/ec/ec_lib.c | 46 +++++++++++++---------------- src/lib/libcrypto/ec/ec_local.h | 12 +------- src/lib/libcrypto/ec/ecp_methods.c | 60 +------------------------------------- 3 files changed, 23 insertions(+), 95 deletions(-) (limited to 'src') diff --git a/src/lib/libcrypto/ec/ec_lib.c b/src/lib/libcrypto/ec/ec_lib.c index 542f7a0ba2..a1c80c328b 100644 --- a/src/lib/libcrypto/ec/ec_lib.c +++ b/src/lib/libcrypto/ec/ec_lib.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ec_lib.c,v 1.88 2024/11/22 12:14:41 tb Exp $ */ +/* $OpenBSD: ec_lib.c,v 1.89 2024/11/30 21:09:59 tb Exp $ */ /* * Originally written by Bodo Moeller for the OpenSSL project. */ @@ -785,10 +785,6 @@ EC_POINT_new(const EC_GROUP *group) ECerror(ERR_R_PASSED_NULL_PARAMETER); goto err; } - if (group->meth->point_init == NULL) { - ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); - goto err; - } if ((point = calloc(1, sizeof(*point))) == NULL) { ECerror(ERR_R_MALLOC_FAILURE); @@ -797,9 +793,6 @@ EC_POINT_new(const EC_GROUP *group) point->meth = group->meth; - if (!point->meth->point_init(point)) - goto err; - return point; err: @@ -815,8 +808,9 @@ EC_POINT_free(EC_POINT *point) if (point == NULL) return; - if (point->meth->point_finish != NULL) - point->meth->point_finish(point); + BN_free(&point->X); + BN_free(&point->Y); + BN_free(&point->Z); freezero(point, sizeof *point); } @@ -832,17 +826,22 @@ LCRYPTO_ALIAS(EC_POINT_clear_free); int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src) { - if (dest->meth->point_copy == NULL) { - ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); - return 0; - } if (dest->meth != src->meth) { ECerror(EC_R_INCOMPATIBLE_OBJECTS); return 0; } if (dest == src) return 1; - return dest->meth->point_copy(dest, src); + + if (!bn_copy(&dest->X, &src->X)) + return 0; + if (!bn_copy(&dest->Y, &src->Y)) + return 0; + if (!bn_copy(&dest->Z, &src->Z)) + return 0; + dest->Z_is_one = src->Z_is_one; + + return 1; } LCRYPTO_ALIAS(EC_POINT_copy); @@ -879,15 +878,15 @@ LCRYPTO_ALIAS(EC_POINT_method_of); int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point) { - if (group->meth->point_set_to_infinity == NULL) { - ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); - return 0; - } if (group->meth != point->meth) { ECerror(EC_R_INCOMPATIBLE_OBJECTS); return 0; } - return group->meth->point_set_to_infinity(group, point); + + BN_zero(&point->Z); + point->Z_is_one = 0; + + return 1; } LCRYPTO_ALIAS(EC_POINT_set_to_infinity); @@ -1196,15 +1195,12 @@ LCRYPTO_ALIAS(EC_POINT_invert); int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point) { - if (group->meth->is_at_infinity == NULL) { - ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); - return 0; - } if (group->meth != point->meth) { ECerror(EC_R_INCOMPATIBLE_OBJECTS); return 0; } - return group->meth->is_at_infinity(group, point); + + return BN_is_zero(&point->Z); } LCRYPTO_ALIAS(EC_POINT_is_at_infinity); diff --git a/src/lib/libcrypto/ec/ec_local.h b/src/lib/libcrypto/ec/ec_local.h index 5d1909db03..11cc36cf67 100644 --- a/src/lib/libcrypto/ec/ec_local.h +++ b/src/lib/libcrypto/ec/ec_local.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ec_local.h,v 1.39 2024/11/22 12:14:41 tb Exp $ */ +/* $OpenBSD: ec_local.h,v 1.40 2024/11/30 21:09:59 tb Exp $ */ /* * Originally written by Bodo Moeller for the OpenSSL project. */ @@ -101,11 +101,6 @@ struct ec_method_st { int (*group_order_bits)(const EC_GROUP *); int (*group_check_discriminant)(const EC_GROUP *, BN_CTX *); - int (*point_init)(EC_POINT *); - void (*point_finish)(EC_POINT *); - int (*point_copy)(EC_POINT *, const EC_POINT *); - - int (*point_set_to_infinity)(const EC_GROUP *, EC_POINT *); int (*point_set_Jprojective_coordinates)(const EC_GROUP *, EC_POINT *, const BIGNUM *x, const BIGNUM *y, const BIGNUM *z, BN_CTX *); int (*point_get_Jprojective_coordinates)(const EC_GROUP *, @@ -122,7 +117,6 @@ struct ec_method_st { int (*dbl)(const EC_GROUP *, EC_POINT *r, const EC_POINT *a, BN_CTX *); int (*invert)(const EC_GROUP *, EC_POINT *, BN_CTX *); - int (*is_at_infinity)(const EC_GROUP *, const EC_POINT *); int (*is_on_curve)(const EC_GROUP *, const EC_POINT *, BN_CTX *); int (*point_cmp)(const EC_GROUP *, const EC_POINT *a, const EC_POINT *b, BN_CTX *); @@ -139,10 +133,6 @@ struct ec_method_st { const BIGNUM *g_scalar, const BIGNUM *p_scalar, const EC_POINT *point, BN_CTX *); - /* - * Internal methods. - */ - /* * These can be used by 'add' and 'dbl' so that the same implementations * of point operations can be used with different optimized versions of diff --git a/src/lib/libcrypto/ec/ecp_methods.c b/src/lib/libcrypto/ec/ecp_methods.c index b394206aea..b14cd0b158 100644 --- a/src/lib/libcrypto/ec/ecp_methods.c +++ b/src/lib/libcrypto/ec/ecp_methods.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ecp_methods.c,v 1.11 2024/11/30 16:34:34 tb Exp $ */ +/* $OpenBSD: ecp_methods.c,v 1.12 2024/11/30 21:09:59 tb Exp $ */ /* Includes code written by Lenka Fibikova * for the OpenSSL project. * Includes code written by Bodo Moeller for the OpenSSL project. @@ -279,48 +279,6 @@ ec_group_check_discriminant(const EC_GROUP *group, BN_CTX *ctx) return ret; } -static int -ec_point_init(EC_POINT * point) -{ - BN_init(&point->X); - BN_init(&point->Y); - BN_init(&point->Z); - point->Z_is_one = 0; - - return 1; -} - -static void -ec_point_finish(EC_POINT *point) -{ - BN_free(&point->X); - BN_free(&point->Y); - BN_free(&point->Z); - point->Z_is_one = 0; -} - -static int -ec_point_copy(EC_POINT *dest, const EC_POINT *src) -{ - if (!bn_copy(&dest->X, &src->X)) - return 0; - if (!bn_copy(&dest->Y, &src->Y)) - return 0; - if (!bn_copy(&dest->Z, &src->Z)) - return 0; - dest->Z_is_one = src->Z_is_one; - - return 1; -} - -static int -ec_point_set_to_infinity(const EC_GROUP *group, EC_POINT *point) -{ - point->Z_is_one = 0; - BN_zero(&point->Z); - return 1; -} - static int ec_set_Jprojective_coordinates(const EC_GROUP *group, EC_POINT *point, const BIGNUM *x, const BIGNUM *y, const BIGNUM *z, BN_CTX *ctx) @@ -890,12 +848,6 @@ ec_invert(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx) return BN_usub(&point->Y, &group->field, &point->Y); } -static int -ec_is_at_infinity(const EC_GROUP *group, const EC_POINT *point) -{ - return BN_is_zero(&point->Z); -} - static int ec_is_on_curve(const EC_GROUP *group, const EC_POINT *point, BN_CTX *ctx) { @@ -1738,10 +1690,6 @@ static const EC_METHOD ec_GFp_simple_method = { .group_get_degree = ec_group_get_degree, .group_order_bits = ec_group_simple_order_bits, .group_check_discriminant = ec_group_check_discriminant, - .point_init = ec_point_init, - .point_finish = ec_point_finish, - .point_copy = ec_point_copy, - .point_set_to_infinity = ec_point_set_to_infinity, .point_set_Jprojective_coordinates = ec_set_Jprojective_coordinates, .point_get_Jprojective_coordinates = ec_get_Jprojective_coordinates, .point_set_affine_coordinates = ec_point_set_affine_coordinates, @@ -1750,7 +1698,6 @@ static const EC_METHOD ec_GFp_simple_method = { .add = ec_add, .dbl = ec_dbl, .invert = ec_invert, - .is_at_infinity = ec_is_at_infinity, .is_on_curve = ec_is_on_curve, .point_cmp = ec_cmp, .make_affine = ec_make_affine, @@ -1780,10 +1727,6 @@ static const EC_METHOD ec_GFp_mont_method = { .group_get_degree = ec_group_get_degree, .group_order_bits = ec_group_simple_order_bits, .group_check_discriminant = ec_group_check_discriminant, - .point_init = ec_point_init, - .point_finish = ec_point_finish, - .point_copy = ec_point_copy, - .point_set_to_infinity = ec_point_set_to_infinity, .point_set_Jprojective_coordinates = ec_set_Jprojective_coordinates, .point_get_Jprojective_coordinates = ec_get_Jprojective_coordinates, .point_set_affine_coordinates = ec_point_set_affine_coordinates, @@ -1792,7 +1735,6 @@ static const EC_METHOD ec_GFp_mont_method = { .add = ec_add, .dbl = ec_dbl, .invert = ec_invert, - .is_at_infinity = ec_is_at_infinity, .is_on_curve = ec_is_on_curve, .point_cmp = ec_cmp, .make_affine = ec_make_affine, -- cgit v1.2.3-55-g6feb