Commit 1acbaf74ec for openssl.org
commit 1acbaf74ec4bec6340ffb173f2c7f5cb48dd057f
Author: Eugene Syromiatnikov <esyr@openssl.org>
Date: Mon Aug 18 14:32:32 2025 +0200
crypto/ec/ecp_nistz256.c: use OPENSSL_aligned_alloc_array
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 <esyr@openssl.org>
Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Saša NedvÄ›dický <sashan@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/28295)
diff --git a/crypto/ec/ecp_nistz256.c b/crypto/ec/ecp_nistz256.c
index fb56909187..f90c012277 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)