From adb410f7268eb765786681c53f78e743395d6402 Mon Sep 17 00:00:00 2001 From: Eugene Syromiatnikov Date: Thu, 17 Jul 2025 15:12:59 +0200 Subject: [PATCH] crypto/ec: use array memory (re)allocation routines Signed-off-by: Eugene Syromiatnikov --- crypto/ec/ec_curve.c | 2 +- crypto/ec/ec_mult.c | 12 ++++++------ crypto/ec/ecp_nistp224.c | 6 +++--- crypto/ec/ecp_nistp256.c | 8 ++++---- crypto/ec/ecp_nistp384.c | 6 +++--- crypto/ec/ecp_nistp521.c | 6 +++--- crypto/ec/ecp_nistz256.c | 9 ++++----- crypto/ec/ecp_sm2p256.c | 2 +- crypto/ec/ecp_smpl.c | 2 +- 9 files changed, 26 insertions(+), 27 deletions(-) diff --git a/crypto/ec/ec_curve.c b/crypto/ec/ec_curve.c index f46aac5d33..8d42047671 100644 --- a/crypto/ec/ec_curve.c +++ b/crypto/ec/ec_curve.c @@ -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; diff --git a/crypto/ec/ec_mult.c b/crypto/ec/ec_mult.c index c9ff0afe09..6ab18a1b1c 100644 --- a/crypto/ec/ec_mult.c +++ b/crypto/ec/ec_mult.c @@ -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; diff --git a/crypto/ec/ecp_nistp224.c b/crypto/ec/ecp_nistp224.c index 6485f46f71..d956e78aba 100644 --- a/crypto/ec/ecp_nistp224.c +++ b/crypto/ec/ecp_nistp224.c @@ -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; diff --git a/crypto/ec/ecp_nistp256.c b/crypto/ec/ecp_nistp256.c index 9c850f644f..ca0172f336 100644 --- a/crypto/ec/ecp_nistp256.c +++ b/crypto/ec/ecp_nistp256.c @@ -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; diff --git a/crypto/ec/ecp_nistp384.c b/crypto/ec/ecp_nistp384.c index 8b5c0f00eb..8777c7de78 100644 --- a/crypto/ec/ecp_nistp384.c +++ b/crypto/ec/ecp_nistp384.c @@ -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; diff --git a/crypto/ec/ecp_nistp521.c b/crypto/ec/ecp_nistp521.c index fe6836a147..6ff55b29e3 100644 --- a/crypto/ec/ecp_nistp521.c +++ b/crypto/ec/ecp_nistp521.c @@ -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; diff --git a/crypto/ec/ecp_nistz256.c b/crypto/ec/ecp_nistz256.c index dfb63c4ca4..124eb817eb 100644 --- a/crypto/ec/ecp_nistz256.c +++ b/crypto/ec/ecp_nistz256.c @@ -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; diff --git a/crypto/ec/ecp_sm2p256.c b/crypto/ec/ecp_sm2p256.c index 7668b61378..17e76bc040 100644 --- a/crypto/ec/ecp_sm2p256.c +++ b/crypto/ec/ecp_sm2p256.c @@ -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; } diff --git a/crypto/ec/ecp_smpl.c b/crypto/ec/ecp_smpl.c index 112a4f0a2f..33cacbfe42 100644 --- a/crypto/ec/ecp_smpl.c +++ b/crypto/ec/ecp_smpl.c @@ -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++) {