From 1a3cfa5fe7c1e4b1702d6f9ea573fa4fa8d84ce4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Tue, 12 May 2026 09:48:25 +0200 Subject: [PATCH] bignum: make small primes product static MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Avoid a heap allocation every time it is used. Signed-off-by: Manuel Pégourié-Gonnard --- library/bignum.c | 57 ++++++++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/library/bignum.c b/library/bignum.c index 5ba85daecc..89b2d689b6 100644 --- a/library/bignum.c +++ b/library/bignum.c @@ -2059,24 +2059,38 @@ int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi #if defined(MBEDTLS_GENPRIME) -/* Product of small primes up to 997 included */ static const mbedtls_mpi_sint small_primes_limit = 997; -static const unsigned char small_primes_product_bin[] = { - 0x05, 0xf0, 0x78, 0x61, 0x8e, 0x81, 0x21, 0xe1, 0xf0, 0xc5, 0x8a, 0xf3, - 0xf5, 0xc7, 0xea, 0x54, 0xbf, 0x9d, 0x72, 0x52, 0xd3, 0x0b, 0xa9, 0xf7, - 0xf2, 0xcb, 0x32, 0xc7, 0x69, 0x02, 0x6d, 0xe4, 0x48, 0xa9, 0x66, 0x72, - 0x7b, 0x6a, 0x2f, 0xf9, 0x8f, 0x3a, 0xf2, 0x3d, 0x78, 0x6c, 0xf7, 0x03, - 0xb4, 0xe6, 0x11, 0xac, 0xf6, 0xa8, 0xd9, 0xe8, 0x3b, 0x38, 0x7d, 0x1c, - 0x20, 0xdf, 0xd8, 0xd5, 0x92, 0xe4, 0x0d, 0x19, 0x14, 0x35, 0xe3, 0xb5, - 0x00, 0x68, 0x84, 0xce, 0x6d, 0x32, 0xbc, 0xa2, 0x0d, 0x62, 0x80, 0xe0, - 0x17, 0xe1, 0x37, 0x85, 0x7a, 0xfc, 0x66, 0x1d, 0xf0, 0x45, 0x05, 0xfc, - 0xa2, 0x31, 0xe5, 0x0c, 0x83, 0xa2, 0x46, 0x67, 0x43, 0x3a, 0x54, 0xf1, - 0x4a, 0xe0, 0x57, 0x07, 0x34, 0xf4, 0x3e, 0x41, 0x39, 0x9d, 0x61, 0x8a, - 0x96, 0x5d, 0xc9, 0x77, 0x8d, 0xe5, 0xe8, 0x18, 0x99, 0x1d, 0x8c, 0xc2, - 0xad, 0xfb, 0x89, 0xbb, 0xfb, 0x2c, 0xba, 0x37, 0xdb, 0xbf, 0xd5, 0xa8, - 0x30, 0xfb, 0x79, 0x35, 0xfc, 0x65, 0x61, 0xf5, 0xc0, 0xdf, 0xd6, 0x18, - 0x9d, 0x7b, 0x9f, 0xa4, 0x20, 0xa4, 0x10, 0x2c, 0xa7, 0x95, 0xdf, 0xd0, - 0xbb, 0x97, 0x6a, 0x13, 0x4b, +/* Product of small primes up to small_primes_limit included */ +static const mbedtls_mpi_uint small_primes_product_limbs[] = { + MBEDTLS_BYTES_TO_T_UINT_8(0x4b, 0x13, 0x6a, 0x97, 0xbb, 0xd0, 0xdf, 0x95), + MBEDTLS_BYTES_TO_T_UINT_8(0xa7, 0x2c, 0x10, 0xa4, 0x20, 0xa4, 0x9f, 0x7b), + MBEDTLS_BYTES_TO_T_UINT_8(0x9d, 0x18, 0xd6, 0xdf, 0xc0, 0xf5, 0x61, 0x65), + MBEDTLS_BYTES_TO_T_UINT_8(0xfc, 0x35, 0x79, 0xfb, 0x30, 0xa8, 0xd5, 0xbf), + MBEDTLS_BYTES_TO_T_UINT_8(0xdb, 0x37, 0xba, 0x2c, 0xfb, 0xbb, 0x89, 0xfb), + MBEDTLS_BYTES_TO_T_UINT_8(0xad, 0xc2, 0x8c, 0x1d, 0x99, 0x18, 0xe8, 0xe5), + MBEDTLS_BYTES_TO_T_UINT_8(0x8d, 0x77, 0xc9, 0x5d, 0x96, 0x8a, 0x61, 0x9d), + MBEDTLS_BYTES_TO_T_UINT_8(0x39, 0x41, 0x3e, 0xf4, 0x34, 0x07, 0x57, 0xe0), + MBEDTLS_BYTES_TO_T_UINT_8(0x4a, 0xf1, 0x54, 0x3a, 0x43, 0x67, 0x46, 0xa2), + MBEDTLS_BYTES_TO_T_UINT_8(0x83, 0x0c, 0xe5, 0x31, 0xa2, 0xfc, 0x05, 0x45), + MBEDTLS_BYTES_TO_T_UINT_8(0xf0, 0x1d, 0x66, 0xfc, 0x7a, 0x85, 0x37, 0xe1), + MBEDTLS_BYTES_TO_T_UINT_8(0x17, 0xe0, 0x80, 0x62, 0x0d, 0xa2, 0xbc, 0x32), + MBEDTLS_BYTES_TO_T_UINT_8(0x6d, 0xce, 0x84, 0x68, 0x00, 0xb5, 0xe3, 0x35), + MBEDTLS_BYTES_TO_T_UINT_8(0x14, 0x19, 0x0d, 0xe4, 0x92, 0xd5, 0xd8, 0xdf), + MBEDTLS_BYTES_TO_T_UINT_8(0x20, 0x1c, 0x7d, 0x38, 0x3b, 0xe8, 0xd9, 0xa8), + MBEDTLS_BYTES_TO_T_UINT_8(0xf6, 0xac, 0x11, 0xe6, 0xb4, 0x03, 0xf7, 0x6c), + MBEDTLS_BYTES_TO_T_UINT_8(0x78, 0x3d, 0xf2, 0x3a, 0x8f, 0xf9, 0x2f, 0x6a), + MBEDTLS_BYTES_TO_T_UINT_8(0x7b, 0x72, 0x66, 0xa9, 0x48, 0xe4, 0x6d, 0x02), + MBEDTLS_BYTES_TO_T_UINT_8(0x69, 0xc7, 0x32, 0xcb, 0xf2, 0xf7, 0xa9, 0x0b), + MBEDTLS_BYTES_TO_T_UINT_8(0xd3, 0x52, 0x72, 0x9d, 0xbf, 0x54, 0xea, 0xc7), + MBEDTLS_BYTES_TO_T_UINT_8(0xf5, 0xf3, 0x8a, 0xc5, 0xf0, 0xe1, 0x21, 0x81), + MBEDTLS_BYTES_TO_T_UINT_8(0x8e, 0x61, 0x78, 0xf0, 0x05, 0x00, 0x00, 0x00), +}; +/* Could make ECP_MPI_INIT_ARRAY() available outside ecp, but not doing it now + * as it would lead to conflicts with other in-flight PRs. */ +static const mbedtls_mpi small_primes_product = { + .p = (mbedtls_mpi_uint *) small_primes_product_limbs, + .s = 1, + .n = sizeof(small_primes_product_limbs) / sizeof(mbedtls_mpi_uint), }; /* Gaps between primes, starting at 3. https://oeis.org/A001223 */ static const unsigned char small_prime_gaps[] = { @@ -2116,9 +2130,8 @@ static const unsigned char small_prime_gaps[] = { static int mpi_check_small_factors(const mbedtls_mpi *X) { int ret = 0; - mbedtls_mpi prod, g; + mbedtls_mpi g; - mbedtls_mpi_init(&prod); mbedtls_mpi_init(&g); if ((X->p[0] & 1) == 0) { @@ -2137,14 +2150,11 @@ static int mpi_check_small_factors(const mbedtls_mpi *X) return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE; } - MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&prod, small_primes_product_bin, - sizeof(small_primes_product_bin))); - /* We can't directly use mbedtls_mpi_gcd_modinv_odd() because we don't know * if X is larger than prod or not (prod is 1379 bits). So, use this generic * wrapper - it does a bit more than what we need (handles even inputs as * well, while we know our inputs are both odd), but that's OK. */ - MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&g, &prod, X)); + MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&g, &small_primes_product, X)); if (mbedtls_mpi_cmp_int(&g, 1) == 0) { /* X is not divisible by a small prime */ @@ -2154,7 +2164,6 @@ static int mpi_check_small_factors(const mbedtls_mpi *X) } cleanup: - mbedtls_mpi_free(&prod); mbedtls_mpi_free(&g); return ret; }