Merge pull request #11000 from mpg/rsa-revert-keygen-hardening-3.6

[3.6] Fix RSA Key Generation performance regression
This commit is contained in:
Manuel Pégourié-Gonnard 2026-10-14 11:01:24 +00:00 committed by GitHub
commit e6e126523b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 60 deletions

View File

@ -0,0 +1,4 @@
Bugfix
* Revert hardening of RSA keygen introduced in the previous release, that
caused an unacceptable performance regression on platforms with higher
memory latency. #10830

View File

@ -2059,38 +2059,30 @@ int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi
#if defined(MBEDTLS_GENPRIME) #if defined(MBEDTLS_GENPRIME)
static const mbedtls_mpi_sint small_primes_limit = 997; /* Gaps between primes, starting at 3. https://oeis.org/A001223 */
/* Product of small primes up to small_primes_limit included */ static const unsigned char small_prime_gaps[] = {
static const mbedtls_mpi_uint small_primes_product_limbs[] = { 2, 2, 4, 2, 4, 2, 4, 6,
MBEDTLS_BYTES_TO_T_UINT_8(0x4b, 0x13, 0x6a, 0x97, 0xbb, 0xd0, 0xdf, 0x95), 2, 6, 4, 2, 4, 6, 6, 2,
MBEDTLS_BYTES_TO_T_UINT_8(0xa7, 0x2c, 0x10, 0xa4, 0x20, 0xa4, 0x9f, 0x7b), 6, 4, 2, 6, 4, 6, 8, 4,
MBEDTLS_BYTES_TO_T_UINT_8(0x9d, 0x18, 0xd6, 0xdf, 0xc0, 0xf5, 0x61, 0x65), 2, 4, 2, 4, 14, 4, 6, 2,
MBEDTLS_BYTES_TO_T_UINT_8(0xfc, 0x35, 0x79, 0xfb, 0x30, 0xa8, 0xd5, 0xbf), 10, 2, 6, 6, 4, 6, 6, 2,
MBEDTLS_BYTES_TO_T_UINT_8(0xdb, 0x37, 0xba, 0x2c, 0xfb, 0xbb, 0x89, 0xfb), 10, 2, 4, 2, 12, 12, 4, 2,
MBEDTLS_BYTES_TO_T_UINT_8(0xad, 0xc2, 0x8c, 0x1d, 0x99, 0x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2f, 0x6a), 10, 12, 2, 10, 2, 4, 2, 10,
MBEDTLS_BYTES_TO_T_UINT_8(0x7b, 0x72, 0x66, 0xa9, 0x48, 0xe4, 0x6d, 0x02), 14, 4, 2, 4, 14, 4, 2, 4,
MBEDTLS_BYTES_TO_T_UINT_8(0x69, 0xc7, 0x32, 0xcb, 0xf2, 0xf7, 0xa9, 0x0b), 20, 4, 8, 10, 8, 4, 6, 6,
MBEDTLS_BYTES_TO_T_UINT_8(0xd3, 0x52, 0x72, 0x9d, 0xbf, 0x54, 0xea, 0xc7), 14, 4, 6, 6, 8, 6, /*reaches 997*/
MBEDTLS_BYTES_TO_T_UINT_8(0xf5, 0xf3, 0x8a, 0xc5, 0xf0, 0xe1, 0x21, 0x81), 0 /* the last entry is effectively unused */
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),
}; };
/* /*
@ -2105,43 +2097,26 @@ static const mbedtls_mpi small_primes_product = {
static int mpi_check_small_factors(const mbedtls_mpi *X) static int mpi_check_small_factors(const mbedtls_mpi *X)
{ {
int ret = 0; int ret = 0;
mbedtls_mpi g; size_t i;
mbedtls_mpi_uint r;
mbedtls_mpi_init(&g); unsigned p = 3; /* The first odd prime */
if ((X->p[0] & 1) == 0) { if ((X->p[0] & 1) == 0) {
return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE; return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
} }
/* The GCD test below only works if X > small_primes_limit. for (i = 0; i < sizeof(small_prime_gaps); p += small_prime_gaps[i], i++) {
* Below this limit, use trial division: numbers that small are of no MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, p));
* interest for cryptography, so we don't care about performance or side if (r == 0) {
* channels. We're supporting them only for backwards compatibility, so if (mbedtls_mpi_cmp_int(X, p) == 0) {
* let's not waste code size on those. */ return 1;
if (mbedtls_mpi_cmp_int(X, small_primes_limit) <= 0) { } else {
mbedtls_mpi_uint x = X->p[0]; return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
mbedtls_mpi_uint d = 2; }
while (x % d != 0) {
++d;
} }
return x == d ? 1 : MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
}
/* 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 1380 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, &small_primes_product, X));
if (mbedtls_mpi_cmp_int(&g, 1) == 0) {
/* X is not divisible by a small prime */
ret = 0;
} else {
ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
} }
cleanup: cleanup:
mbedtls_mpi_free(&g);
return ret; return ret;
} }