crypto/ec: use array memory (re)allocation routines

Signed-off-by: Eugene Syromiatnikov <esyr@openssl.org>
This commit is contained in:
Eugene Syromiatnikov 2025-07-17 15:12:59 +02:00
parent ddc1a636a9
commit adb410f726
9 changed files with 26 additions and 27 deletions

View File

@ -3406,7 +3406,7 @@ int ossl_ec_curve_nid_from_params(const EC_GROUP *group, BN_CTX *ctx)
param_len = len;
/* Allocate space to store the padded data for (p, a, b, x, y, order) */
param_bytes = OPENSSL_malloc(param_len * NUM_BN_FIELDS);
param_bytes = OPENSSL_malloc_array(NUM_BN_FIELDS, param_len);
if (param_bytes == NULL)
goto end;

View File

@ -504,11 +504,11 @@ int ossl_ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
totalnum = num + numblocks;
wsize = OPENSSL_malloc(totalnum * sizeof(wsize[0]));
wNAF_len = OPENSSL_malloc(totalnum * sizeof(wNAF_len[0]));
wsize = OPENSSL_malloc_array(totalnum, sizeof(wsize[0]));
wNAF_len = OPENSSL_malloc_array(totalnum, sizeof(wNAF_len[0]));
/* include space for pivot */
wNAF = OPENSSL_malloc((totalnum + 1) * sizeof(wNAF[0]));
val_sub = OPENSSL_malloc(totalnum * sizeof(val_sub[0]));
wNAF = OPENSSL_malloc_array(totalnum + 1, sizeof(wNAF[0]));
val_sub = OPENSSL_malloc_array(totalnum, sizeof(val_sub[0]));
/* Ensure wNAF is initialised in case we end up going to err */
if (wNAF != NULL)
@ -651,7 +651,7 @@ int ossl_ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
* 'val_sub[i]' is a pointer to the subarray for the i-th point, or to a
* subarray of 'pre_comp->points' if we already have precomputation.
*/
val = OPENSSL_malloc((num_val + 1) * sizeof(val[0]));
val = OPENSSL_malloc_array(num_val + 1, sizeof(val[0]));
if (val == NULL)
goto err;
val[num_val] = NULL; /* pivot element */
@ -883,7 +883,7 @@ int ossl_ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
num = pre_points_per_block * numblocks; /* number of points to compute
* and store */
points = OPENSSL_malloc(sizeof(*points) * (num + 1));
points = OPENSSL_malloc_array(num + 1, sizeof(*points));
if (points == NULL)
goto err;

View File

@ -1475,11 +1475,11 @@ int ossl_ec_GFp_nistp224_points_mul(const EC_GROUP *group, EC_POINT *r,
*/
mixed = 1;
}
secrets = OPENSSL_zalloc(sizeof(*secrets) * num_points);
pre_comp = OPENSSL_zalloc(sizeof(*pre_comp) * num_points);
secrets = OPENSSL_calloc(num_points, sizeof(*secrets));
pre_comp = OPENSSL_calloc(num_points, sizeof(*pre_comp));
if (mixed)
tmp_felems =
OPENSSL_malloc(sizeof(felem) * (num_points * 17 + 1));
OPENSSL_malloc_array(num_points * 17 + 1, sizeof(felem));
if ((secrets == NULL) || (pre_comp == NULL)
|| (mixed && (tmp_felems == NULL)))
goto err;

View File

@ -2088,11 +2088,11 @@ int ossl_ec_GFp_nistp256_points_mul(const EC_GROUP *group, EC_POINT *r,
*/
mixed = 1;
}
secrets = OPENSSL_zalloc(sizeof(*secrets) * num_points);
pre_comp = OPENSSL_zalloc(sizeof(*pre_comp) * num_points);
secrets = OPENSSL_calloc(num_points, sizeof(*secrets));
pre_comp = OPENSSL_calloc(num_points, sizeof(*pre_comp));
if (mixed)
tmp_smallfelems =
OPENSSL_malloc(sizeof(*tmp_smallfelems) * (num_points * 17 + 1));
tmp_smallfelems = OPENSSL_malloc_array(num_points * 17 + 1,
sizeof(*tmp_smallfelems));
if ((secrets == NULL) || (pre_comp == NULL)
|| (mixed && (tmp_smallfelems == NULL)))
goto err;

View File

@ -1805,11 +1805,11 @@ int ossl_ec_GFp_nistp384_points_mul(const EC_GROUP *group, EC_POINT *r,
*/
mixed = 1;
}
secrets = OPENSSL_zalloc(sizeof(*secrets) * num_points);
pre_comp = OPENSSL_zalloc(sizeof(*pre_comp) * num_points);
secrets = OPENSSL_calloc(num_points, sizeof(*secrets));
pre_comp = OPENSSL_calloc(num_points, sizeof(*pre_comp));
if (mixed)
tmp_felems =
OPENSSL_malloc(sizeof(*tmp_felems) * (num_points * 17 + 1));
OPENSSL_malloc_array(num_points * 17 + 1, sizeof(*tmp_felems));
if ((secrets == NULL) || (pre_comp == NULL)
|| (mixed && (tmp_felems == NULL)))
goto err;

View File

@ -1978,11 +1978,11 @@ int ossl_ec_GFp_nistp521_points_mul(const EC_GROUP *group, EC_POINT *r,
*/
mixed = 1;
}
secrets = OPENSSL_zalloc(sizeof(*secrets) * num_points);
pre_comp = OPENSSL_zalloc(sizeof(*pre_comp) * num_points);
secrets = OPENSSL_calloc(num_points, sizeof(*secrets));
pre_comp = OPENSSL_calloc(num_points, sizeof(*pre_comp));
if (mixed)
tmp_felems =
OPENSSL_malloc(sizeof(*tmp_felems) * (num_points * 17 + 1));
OPENSSL_malloc_array(num_points * 17 + 1, sizeof(*tmp_felems));
if ((secrets == NULL) || (pre_comp == NULL)
|| (mixed && (tmp_felems == NULL)))
goto err;

View File

@ -625,9 +625,8 @@ __owur static int ecp_nistz256_windowed_mul(const EC_GROUP *group,
if ((num * 16 + 6) > OPENSSL_MALLOC_MAX_NELEMS(P256_POINT)
|| (table_storage =
OPENSSL_malloc((num * 16 + 5) * sizeof(P256_POINT) + 64)) == NULL
|| (p_str =
OPENSSL_malloc(num * 33 * sizeof(unsigned char))) == NULL
|| (scalars = OPENSSL_malloc(num * sizeof(BIGNUM *))) == 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);
@ -1109,11 +1108,11 @@ __owur static int ecp_nistz256_points_mul(const EC_GROUP *group,
* Without a precomputed table for the generator, it has to be
* handled like a normal point.
*/
new_scalars = OPENSSL_malloc((num + 1) * sizeof(BIGNUM *));
new_scalars = OPENSSL_malloc_array(num + 1, sizeof(BIGNUM *));
if (new_scalars == NULL)
goto err;
new_points = OPENSSL_malloc((num + 1) * sizeof(EC_POINT *));
new_points = OPENSSL_malloc_array(num + 1, sizeof(EC_POINT *));
if (new_points == NULL)
goto err;

View File

@ -518,7 +518,7 @@ static int ecp_sm2p256_windowed_mul(const EC_GROUP *group,
} t, p;
if (num > OPENSSL_MALLOC_MAX_NELEMS(P256_POINT)
|| (scalars = OPENSSL_malloc(num * sizeof(BIGNUM *))) == NULL) {
|| (scalars = OPENSSL_malloc_array(num, sizeof(BIGNUM *))) == NULL) {
ECerr(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
goto err;
}

View File

@ -1227,7 +1227,7 @@ int ossl_ec_GFp_simple_points_make_affine(const EC_GROUP *group, size_t num,
if (tmp_Z == NULL)
goto err;
prod_Z = OPENSSL_malloc(num * sizeof(prod_Z[0]));
prod_Z = OPENSSL_malloc_array(num, sizeof(prod_Z[0]));
if (prod_Z == NULL)
goto err;
for (i = 0; i < num; i++) {