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)
This commit is contained in:
Eugene Syromiatnikov 2025-08-18 14:32:32 +02:00 committed by Neil Horman
parent 6c02774fd3
commit 1acbaf74ec
1 changed files with 7 additions and 9 deletions

View File

@ -37,7 +37,6 @@
# define TOBN(hi,lo) ((BN_ULONG)hi<<32|lo) # define TOBN(hi,lo) ((BN_ULONG)hi<<32|lo)
#endif #endif
#define ALIGNPTR(p,N) ((unsigned char *)p+N-(size_t)p%N)
#define P256_LIMBS (256/BN_BITS2) #define P256_LIMBS (256/BN_BITS2)
typedef unsigned short u16; typedef unsigned short u16;
@ -623,13 +622,13 @@ __owur static int ecp_nistz256_windowed_mul(const EC_GROUP *group,
void *table_storage = NULL; void *table_storage = NULL;
if ((num * 16 + 6) > OPENSSL_MALLOC_MAX_NELEMS(P256_POINT) if ((num * 16 + 6) > OPENSSL_MALLOC_MAX_NELEMS(P256_POINT)
|| (table_storage = || (table =
OPENSSL_malloc((num * 16 + 5) * sizeof(P256_POINT) + 64)) == NULL OPENSSL_aligned_alloc_array(num * 16 + 5, sizeof(P256_POINT), 64,
&table_storage)) == NULL
|| (p_str = OPENSSL_malloc_array(num, 33)) == NULL || (p_str = OPENSSL_malloc_array(num, 33)) == NULL
|| (scalars = OPENSSL_malloc_array(num, sizeof(BIGNUM *))) == NULL) || (scalars = OPENSSL_malloc_array(num, sizeof(BIGNUM *))) == NULL)
goto err; goto err;
table = (void *)ALIGNPTR(table_storage, 64);
temp = (P256_POINT *)(table + num); temp = (P256_POINT *)(table + num);
for (i = 0; i < num; i++) { 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; size_t w;
PRECOMP256_ROW *preComputedTable = NULL; 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 */ /* if there is an old NISTZ256_PRE_COMP object, throw it away */
EC_pre_comp_free(group); EC_pre_comp_free(group);
@ -855,12 +854,11 @@ __owur static int ecp_nistz256_mult_precompute(EC_GROUP *group, BN_CTX *ctx)
w = 7; w = 7;
if ((precomp_storage = if ((preComputedTable =
OPENSSL_malloc(37 * 64 * sizeof(P256_POINT_AFFINE) + 64)) == NULL) OPENSSL_aligned_alloc_array(37 * 64, sizeof(P256_POINT_AFFINE), 64,
&precomp_storage)) == NULL)
goto err; goto err;
preComputedTable = (void *)ALIGNPTR(precomp_storage, 64);
P = EC_POINT_new(group); P = EC_POINT_new(group);
T = EC_POINT_new(group); T = EC_POINT_new(group);
if (P == NULL || T == NULL) if (P == NULL || T == NULL)