mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-07 00:29:15 +00:00
LibCrypto: Add optimized RSA decryption with CRT method
The textbook RSA decryption method of `c^d % n` is quite slow. If the necessary parameters are present, the CRT variant will be used. Performing RSA decryption this way is ~3 times faster.
This commit is contained in:
parent
ec990d620f
commit
57cc248883
Notes:
github-actions[bot]
2024-12-15 22:32:40 +00:00
Author: https://github.com/devgianlu
Commit: 57cc248883
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2928
1 changed files with 16 additions and 4 deletions
|
@ -133,12 +133,24 @@ void RSA::encrypt(ReadonlyBytes in, Bytes& out)
|
||||||
|
|
||||||
void RSA::decrypt(ReadonlyBytes in, Bytes& out)
|
void RSA::decrypt(ReadonlyBytes in, Bytes& out)
|
||||||
{
|
{
|
||||||
// FIXME: Actually use the private key properly
|
|
||||||
|
|
||||||
auto in_integer = UnsignedBigInteger::import_data(in.data(), in.size());
|
auto in_integer = UnsignedBigInteger::import_data(in.data(), in.size());
|
||||||
auto exp = NumberTheory::ModularPower(in_integer, m_private_key.private_exponent(), m_private_key.modulus());
|
|
||||||
auto size = exp.export_data(out);
|
|
||||||
|
|
||||||
|
UnsignedBigInteger m;
|
||||||
|
if (m_private_key.prime1().is_zero() || m_private_key.prime2().is_zero()) {
|
||||||
|
m = NumberTheory::ModularPower(in_integer, m_private_key.private_exponent(), m_private_key.modulus());
|
||||||
|
} 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)
|
||||||
|
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()));
|
||||||
|
}
|
||||||
|
|
||||||
|
auto size = m.export_data(out);
|
||||||
auto align = m_private_key.length();
|
auto align = m_private_key.length();
|
||||||
auto aligned_size = (size + align - 1) / align * align;
|
auto aligned_size = (size + align - 1) / align * align;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue