[PATCH 1/5] mpi/ec: remove VLA usage
Jussi Kivilinna
jussi.kivilinna at iki.fi
Sat Oct 1 09:48:19 CEST 2022
* mpi/ec-nist.c (_gcry_mpi_ec_nist192_mod, _gcry_mpi_ec_nist224_mod)
(_gcry_mpi_ec_nist256_mod, _gcry_mpi_ec_nist384_mod)
(_gcry_mpi_ec_nist521_mod): Avoid VLA for arrays on stack.
* mpi/ec.c (ec_secp256k1_mod): Avoid VLA for arrays on stack.
--
Signed-off-by: Jussi Kivilinna <jussi.kivilinna at iki.fi>
---
mpi/ec-nist.c | 40 ++++++++++++++++++++--------------------
mpi/ec.c | 6 +++---
2 files changed, 23 insertions(+), 23 deletions(-)
diff --git a/mpi/ec-nist.c b/mpi/ec-nist.c
index 69b05a6d..14e3c3ab 100644
--- a/mpi/ec-nist.c
+++ b/mpi/ec-nist.c
@@ -94,9 +94,9 @@ _gcry_mpi_ec_nist192_mod (gcry_mpi_t w, mpi_ec_t ctx)
};
const mpi_limb64_t zero = LIMB_TO64(0);
mpi_ptr_t wp;
- mpi_size_t wsize = 192 / BITS_PER_MPI_LIMB64;
- mpi_limb64_t s[wsize + 1];
- mpi_limb64_t o[wsize + 1];
+ mpi_limb64_t s[192 / BITS_PER_MPI_LIMB64 + 1];
+ mpi_limb64_t o[DIM(s)];
+ const mpi_size_t wsize = DIM(s) - 1;
mpi_limb_t mask1;
mpi_limb_t mask2;
mpi_limb_t s_is_negative;
@@ -186,10 +186,10 @@ _gcry_mpi_ec_nist224_mod (gcry_mpi_t w, mpi_ec_t ctx)
};
const mpi_limb64_t zero = LIMB_TO64(0);
mpi_ptr_t wp;
- mpi_size_t wsize = (224 + BITS_PER_MPI_LIMB64 - 1) / BITS_PER_MPI_LIMB64;
+ mpi_limb64_t s[(224 + BITS_PER_MPI_LIMB64 - 1) / BITS_PER_MPI_LIMB64];
+ mpi_limb64_t d[DIM(s)];
+ const mpi_size_t wsize = DIM(s);
mpi_size_t psize = ctx->p->nlimbs;
- mpi_limb64_t s[wsize];
- mpi_limb64_t d[wsize];
mpi_limb_t mask1;
mpi_limb_t mask2;
mpi_limb_t s_is_negative;
@@ -345,12 +345,12 @@ _gcry_mpi_ec_nist256_mod (gcry_mpi_t w, mpi_ec_t ctx)
};
const mpi_limb64_t zero = LIMB_TO64(0);
mpi_ptr_t wp;
- mpi_size_t wsize = (256 + BITS_PER_MPI_LIMB64 - 1) / BITS_PER_MPI_LIMB64;
+ mpi_limb64_t s[(256 + BITS_PER_MPI_LIMB64 - 1) / BITS_PER_MPI_LIMB64 + 1];
+ mpi_limb64_t t[DIM(s)];
+ mpi_limb64_t d[DIM(s)];
+ mpi_limb64_t e[DIM(s)];
+ const mpi_size_t wsize = DIM(s) - 1;
mpi_size_t psize = ctx->p->nlimbs;
- mpi_limb64_t s[wsize + 1];
- mpi_limb64_t t[wsize + 1];
- mpi_limb64_t d[wsize + 1];
- mpi_limb64_t e[wsize + 1];
mpi_limb_t mask1;
mpi_limb_t mask2;
mpi_limb_t mask3;
@@ -595,15 +595,15 @@ _gcry_mpi_ec_nist384_mod (gcry_mpi_t w, mpi_ec_t ctx)
};
const mpi_limb64_t zero = LIMB_TO64(0);
mpi_ptr_t wp;
- mpi_size_t wsize = (384 + BITS_PER_MPI_LIMB64 - 1) / BITS_PER_MPI_LIMB64;
- mpi_size_t psize = ctx->p->nlimbs;
+ mpi_limb64_t s[(384 + BITS_PER_MPI_LIMB64 - 1) / BITS_PER_MPI_LIMB64 + 1];
+ mpi_limb64_t t[DIM(s)];
+ mpi_limb64_t d[DIM(s)];
+ mpi_limb64_t x[DIM(s)];
#if (BITS_PER_MPI_LIMB64 == BITS_PER_MPI_LIMB) && defined(WORDS_BIGENDIAN)
- mpi_limb_t wp_shr32[wsize * LIMBS_PER_LIMB64];
+ mpi_limb_t wp_shr32[(DIM(s) - 1) * LIMBS_PER_LIMB64];
#endif
- mpi_limb64_t s[wsize + 1];
- mpi_limb64_t t[wsize + 1];
- mpi_limb64_t d[wsize + 1];
- mpi_limb64_t x[wsize + 1];
+ const mpi_size_t wsize = DIM(s) - 1;
+ mpi_size_t psize = ctx->p->nlimbs;
mpi_limb_t mask1;
mpi_limb_t mask2;
mpi_limb_t s_is_negative;
@@ -791,8 +791,8 @@ _gcry_mpi_ec_nist384_mod (gcry_mpi_t w, mpi_ec_t ctx)
void
_gcry_mpi_ec_nist521_mod (gcry_mpi_t w, mpi_ec_t ctx)
{
- mpi_size_t wsize = (521 + BITS_PER_MPI_LIMB - 1) / BITS_PER_MPI_LIMB;
- mpi_limb_t s[wsize];
+ mpi_limb_t s[(521 + BITS_PER_MPI_LIMB - 1) / BITS_PER_MPI_LIMB];
+ const mpi_size_t wsize = DIM(s);
mpi_limb_t cy;
mpi_ptr_t wp;
diff --git a/mpi/ec.c b/mpi/ec.c
index c24921ee..0ad6769c 100644
--- a/mpi/ec.c
+++ b/mpi/ec.c
@@ -581,9 +581,9 @@ ec_pow2_448 (gcry_mpi_t w, const gcry_mpi_t b, mpi_ec_t ctx)
static void
ec_secp256k1_mod (gcry_mpi_t w, mpi_ec_t ctx)
{
- mpi_size_t wsize = (256 + BITS_PER_MPI_LIMB - 1) / BITS_PER_MPI_LIMB;
- mpi_limb_t n[wsize + 1];
- mpi_limb_t s[wsize + 1];
+ mpi_limb_t s[(256 + BITS_PER_MPI_LIMB - 1) / BITS_PER_MPI_LIMB + 1];
+ mpi_limb_t n[sizeof(s)];
+ const mpi_size_t wsize = DIM(s) - 1;
mpi_limb_t cy, borrow;
mpi_ptr_t wp;
--
2.34.1
More information about the Gcrypt-devel
mailing list