From 1acbaf74ec4bec6340ffb173f2c7f5cb48dd057f Mon Sep 17 00:00:00 2001 From: Eugene Syromiatnikov Date: Mon, 18 Aug 2025 14:32:32 +0200 Subject: [PATCH] crypto/ec/ecp_nistz256.c: use OPENSSL_aligned_alloc_array MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Allocate table in ecp_nistz256_windowed_mul() and preComputedTable in ecp_nistz256_mult_precompute() using OPENSSL_aligned_alloc_array() call instead of OPENSSL_malloc with a 64-byte slack and manual pointer alignment adjustement. Signed-off-by: Eugene Syromiatnikov Reviewed-by: Neil Horman Reviewed-by: Saša Nedvědický (Merged from https://github.com/openssl/openssl/pull/28295) --- crypto/ec/ecp_nistz256.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/crypto/ec/ecp_nistz256.c b/crypto/ec/ecp_nistz256.c index fb569091875..f90c0122778 100644 --- a/crypto/ec/ecp_nistz256.c +++ b/crypto/ec/ecp_nistz256.c @@ -37,7 +37,6 @@ # define TOBN(hi,lo) ((BN_ULONG)hi<<32|lo) #endif -#define ALIGNPTR(p,N) ((unsigned char *)p+N-(size_t)p%N) #define P256_LIMBS (256/BN_BITS2) typedef unsigned short u16; @@ -623,13 +622,13 @@ __owur static int ecp_nistz256_windowed_mul(const EC_GROUP *group, void *table_storage = NULL; if ((num * 16 + 6) > OPENSSL_MALLOC_MAX_NELEMS(P256_POINT) - || (table_storage = - OPENSSL_malloc((num * 16 + 5) * sizeof(P256_POINT) + 64)) == NULL + || (table = + OPENSSL_aligned_alloc_array(num * 16 + 5, sizeof(P256_POINT), 64, + &table_storage)) == NULL || (p_str = OPENSSL_malloc_array(num, 33)) == NULL || (scalars = OPENSSL_malloc_array(num, sizeof(BIGNUM *))) == NULL) goto err; - table = (void *)ALIGNPTR(table_storage, 64); temp = (P256_POINT *)(table + num); for (i = 0; i < num; i++) { @@ -815,7 +814,7 @@ __owur static int ecp_nistz256_mult_precompute(EC_GROUP *group, BN_CTX *ctx) size_t w; PRECOMP256_ROW *preComputedTable = NULL; - unsigned char *precomp_storage = NULL; + void *precomp_storage = NULL; /* if there is an old NISTZ256_PRE_COMP object, throw it away */ EC_pre_comp_free(group); @@ -855,12 +854,11 @@ __owur static int ecp_nistz256_mult_precompute(EC_GROUP *group, BN_CTX *ctx) w = 7; - if ((precomp_storage = - OPENSSL_malloc(37 * 64 * sizeof(P256_POINT_AFFINE) + 64)) == NULL) + if ((preComputedTable = + OPENSSL_aligned_alloc_array(37 * 64, sizeof(P256_POINT_AFFINE), 64, + &precomp_storage)) == NULL) goto err; - preComputedTable = (void *)ALIGNPTR(precomp_storage, 64); - P = EC_POINT_new(group); T = EC_POINT_new(group); if (P == NULL || T == NULL) -- 2.47.3