From ff1ace8f50a4d6bd350ebd7bcc6c29fa0a3af23c Mon Sep 17 00:00:00 2001 From: tb <> Date: Mon, 6 Jan 2025 14:22:55 +0000 Subject: Inline the copy handlers in EC_GROUP_copy() This is another bit of indirection that makes this code so hard to follow. ok jsing --- src/lib/libcrypto/ec/ec_lib.c | 25 +++++++++++++++------ src/lib/libcrypto/ec/ecp_methods.c | 45 +------------------------------------- 2 files changed, 19 insertions(+), 51 deletions(-) diff --git a/src/lib/libcrypto/ec/ec_lib.c b/src/lib/libcrypto/ec/ec_lib.c index b1aad34017..8bae5940c2 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.97 2025/01/06 12:35:14 jsing Exp $ */ +/* $OpenBSD: ec_lib.c,v 1.98 2025/01/06 14:22:55 tb Exp $ */ /* * Originally written by Bodo Moeller for the OpenSSL project. */ @@ -150,10 +150,6 @@ LCRYPTO_ALIAS(EC_GROUP_clear_free); int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src) { - if (dest->meth->group_copy == NULL) { - ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); - return 0; - } if (dest->meth != src->meth) { ECerror(EC_R_INCOMPATIBLE_OBJECTS); return 0; @@ -161,8 +157,23 @@ EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src) if (dest == src) return 1; - if (!dest->meth->group_copy(dest, src)) + if (!bn_copy(dest->p, src->p)) return 0; + if (!bn_copy(dest->a, src->a)) + return 0; + if (!bn_copy(dest->b, src->b)) + return 0; + + dest->a_is_minus3 = src->a_is_minus3; + + BN_MONT_CTX_free(dest->mont_ctx); + dest->mont_ctx = NULL; + if (src->mont_ctx != NULL) { + if ((dest->mont_ctx = BN_MONT_CTX_new()) == NULL) + return 0; + if (!BN_MONT_CTX_copy(dest->mont_ctx, src->mont_ctx)) + return 0; + } EC_POINT_free(dest->generator); dest->generator = NULL; @@ -185,7 +196,7 @@ EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src) if (!EC_GROUP_set_seed(dest, src->seed, src->seed_len)) return 0; - return dest->meth->group_copy(dest, src); + return 1; } LCRYPTO_ALIAS(EC_GROUP_copy); diff --git a/src/lib/libcrypto/ec/ecp_methods.c b/src/lib/libcrypto/ec/ecp_methods.c index 44322f27f2..042db054a8 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.22 2025/01/06 12:36:41 jsing Exp $ */ +/* $OpenBSD: ecp_methods.c,v 1.23 2025/01/06 14:22:55 tb Exp $ */ /* Includes code written by Lenka Fibikova * for the OpenSSL project. * Includes code written by Bodo Moeller for the OpenSSL project. @@ -84,21 +84,6 @@ * representation (i.e. 'encoding' means multiplying by some factor R). */ -static int -ec_group_copy(EC_GROUP *dest, const EC_GROUP *src) -{ - if (!bn_copy(dest->p, src->p)) - return 0; - if (!bn_copy(dest->a, src->a)) - return 0; - if (!bn_copy(dest->b, src->b)) - return 0; - - dest->a_is_minus3 = src->a_is_minus3; - - return 1; -} - static int ec_decode_scalar(const EC_GROUP *group, BIGNUM *bn, const BIGNUM *x, BN_CTX *ctx) { @@ -1458,32 +1443,6 @@ ec_mont_group_clear(EC_GROUP *group) group->mont_ctx = NULL; } -static int -ec_mont_group_copy(EC_GROUP *dest, const EC_GROUP *src) -{ - ec_mont_group_clear(dest); - - if (!ec_group_copy(dest, src)) - return 0; - - if (src->mont_ctx != NULL) { - dest->mont_ctx = BN_MONT_CTX_new(); - if (dest->mont_ctx == NULL) - return 0; - if (!BN_MONT_CTX_copy(dest->mont_ctx, src->mont_ctx)) - goto err; - } - - return 1; - - err: - if (dest->mont_ctx != NULL) { - BN_MONT_CTX_free(dest->mont_ctx); - dest->mont_ctx = NULL; - } - return 0; -} - static int ec_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) @@ -1559,7 +1518,6 @@ ec_mont_field_decode(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, static const EC_METHOD ec_GFp_simple_method = { .field_type = NID_X9_62_prime_field, - .group_copy = ec_group_copy, .group_set_curve = ec_group_set_curve, .group_get_curve = ec_group_get_curve, .group_get_degree = ec_group_get_degree, @@ -1591,7 +1549,6 @@ LCRYPTO_ALIAS(EC_GFp_simple_method); static const EC_METHOD ec_GFp_mont_method = { .field_type = NID_X9_62_prime_field, - .group_copy = ec_mont_group_copy, .group_set_curve = ec_mont_group_set_curve, .group_get_curve = ec_group_get_curve, .group_get_degree = ec_group_get_degree, -- cgit v1.2.3-55-g6feb