crypto/bn: 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:11:38 +02:00
parent d1cdadb43c
commit ddc1a636a9
7 changed files with 12 additions and 12 deletions

View File

@ -63,7 +63,7 @@ char *BN_bn2dec(const BIGNUM *a)
num = (i / 10 + i / 1000 + 1) + 1;
tbytes = num + 3; /* negative and terminator and one spare? */
bn_data_num = num / BN_DEC_NUM + 1;
bn_data = OPENSSL_malloc(bn_data_num * sizeof(BN_ULONG));
bn_data = OPENSSL_malloc_array(bn_data_num, sizeof(BN_ULONG));
buf = OPENSSL_malloc(tbytes);
if (buf == NULL || bn_data == NULL)
goto err;

View File

@ -266,7 +266,7 @@ static int BN_STACK_push(BN_STACK *st, unsigned int idx)
st->size ? (st->size * 3 / 2) : BN_CTX_START_FRAMES;
unsigned int *newitems;
if ((newitems = OPENSSL_malloc(sizeof(*newitems) * newsize)) == NULL)
if ((newitems = OPENSSL_malloc_array(newsize, sizeof(*newitems))) == NULL)
return 0;
if (st->depth)
memcpy(newitems, st->indexes, sizeof(*newitems) * st->depth);

View File

@ -474,7 +474,7 @@ int BN_GF2m_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
bn_check_top(b);
bn_check_top(p);
arr = OPENSSL_malloc(sizeof(*arr) * max);
arr = OPENSSL_malloc_array(max, sizeof(*arr));
if (arr == NULL)
return 0;
ret = BN_GF2m_poly2arr(p, arr, max);
@ -534,7 +534,7 @@ int BN_GF2m_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
bn_check_top(a);
bn_check_top(p);
arr = OPENSSL_malloc(sizeof(*arr) * max);
arr = OPENSSL_malloc_array(max, sizeof(*arr));
if (arr == NULL)
return 0;
ret = BN_GF2m_poly2arr(p, arr, max);
@ -917,7 +917,7 @@ int BN_GF2m_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
bn_check_top(b);
bn_check_top(p);
arr = OPENSSL_malloc(sizeof(*arr) * max);
arr = OPENSSL_malloc_array(max, sizeof(*arr));
if (arr == NULL)
return 0;
ret = BN_GF2m_poly2arr(p, arr, max);
@ -979,7 +979,7 @@ int BN_GF2m_mod_sqrt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
bn_check_top(a);
bn_check_top(p);
arr = OPENSSL_malloc(sizeof(*arr) * max);
arr = OPENSSL_malloc_array(max, sizeof(*arr));
if (arr == NULL)
return 0;
ret = BN_GF2m_poly2arr(p, arr, max);
@ -1113,7 +1113,7 @@ int BN_GF2m_mod_solve_quad(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
bn_check_top(a);
bn_check_top(p);
arr = OPENSSL_malloc(sizeof(*arr) * max);
arr = OPENSSL_malloc_array(max, sizeof(*arr));
if (arr == NULL)
goto err;
ret = BN_GF2m_poly2arr(p, arr, max);

View File

@ -276,9 +276,9 @@ static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
return NULL;
}
if (BN_get_flags(b, BN_FLG_SECURE))
a = OPENSSL_secure_zalloc(words * sizeof(*a));
a = OPENSSL_secure_calloc(words, sizeof(*a));
else
a = OPENSSL_zalloc(words * sizeof(*a));
a = OPENSSL_calloc(words, sizeof(*a));
if (ossl_unlikely(a == NULL))
return NULL;

View File

@ -63,7 +63,7 @@ int bn_mod_add_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
return 0;
if (mtop > OSSL_NELEM(storage)) {
tp = OPENSSL_malloc(mtop * sizeof(BN_ULONG));
tp = OPENSSL_malloc_array(mtop, sizeof(BN_ULONG));
if (tp == NULL)
return 0;
}

View File

@ -144,7 +144,7 @@ int BN_generate_prime_ex2(BIGNUM *ret, int bits, int safe,
return 0;
}
mods = OPENSSL_zalloc(sizeof(*mods) * NUMPRIMES);
mods = OPENSSL_calloc(NUMPRIMES, sizeof(*mods));
if (mods == NULL)
return 0;

View File

@ -31,7 +31,7 @@ static int s390x_mod_exp_hw(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
if (OPENSSL_s390xcex == -1 || OPENSSL_s390xcex_nodev)
return 0;
size = BN_num_bytes(m);
buffer = OPENSSL_zalloc(4 * size);
buffer = OPENSSL_calloc(size, 4);
if (buffer == NULL)
return 0;
me.inputdata = buffer;