From 8620a2af47352d880f320d8b7a563b66734dc58f Mon Sep 17 00:00:00 2001 From: devgianlu Date: Wed, 18 Dec 2024 18:51:48 +0100 Subject: [PATCH] LibCrypto: Ensure RSA decryption with CRT works for all inputs Ensure becomes `m1` greater than `m2` even when smaller by more than one `p`. Since the next operations on `m1` are modulus `p` we can add it as many times as it's needed. --- Libraries/LibCrypto/PK/RSA.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Libraries/LibCrypto/PK/RSA.cpp b/Libraries/LibCrypto/PK/RSA.cpp index c4c5d1569bb..4a9f82c5703 100644 --- a/Libraries/LibCrypto/PK/RSA.cpp +++ b/Libraries/LibCrypto/PK/RSA.cpp @@ -141,11 +141,9 @@ void RSA::decrypt(ReadonlyBytes in, Bytes& out) } else { auto m1 = NumberTheory::ModularPower(in_integer, m_private_key.exponent1(), m_private_key.prime1()); auto m2 = NumberTheory::ModularPower(in_integer, m_private_key.exponent2(), m_private_key.prime2()); - if (m1 < m2) + while (m1 < m2) m1 = m1.plus(m_private_key.prime1()); - VERIFY(m1 >= m2); - auto h = NumberTheory::Mod(m1.minus(m2).multiplied_by(m_private_key.coefficient()), m_private_key.prime1()); m = m2.plus(h.multiplied_by(m_private_key.prime2())); }