1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 18:28:12 +00:00

LibCrypto: Do not trim leading zeros in export_data by default

This fixes the issue with the exported data having a leading zero,
causing RSA::encrypt to trim the block down, and ruining the encryption.

Fixes #2691 :^)
This commit is contained in:
AnotherTest 2020-07-31 13:33:14 +04:30 committed by Andreas Kling
parent 180207062c
commit b00ffc860b
6 changed files with 27 additions and 19 deletions

View file

@ -126,9 +126,11 @@ void RSA::encrypt(const ByteBuffer& in, ByteBuffer& out)
}
auto exp = NumberTheory::ModularPower(in_integer, m_public_key.public_exponent(), m_public_key.modulus());
auto size = exp.export_data(out.span());
// FIXME: We should probably not do this...
if (size != out.size())
out = out.slice(out.size() - size, size);
auto outsize = out.size();
if (size != outsize) {
dbg() << "POSSIBLE RSA BUG!!! Size mismatch: " << outsize << " requested but " << size << " bytes generated";
out = out.slice(outsize - size, size);
}
}
void RSA::decrypt(const ByteBuffer& in, ByteBuffer& out)
@ -252,8 +254,8 @@ void RSA_PKCS1_EME::encrypt(const ByteBuffer& in, ByteBuffer& out)
// since arc4random can create zeros (shocking!)
// we have to go through and un-zero the zeros
for (size_t i = 0; i < ps_length; ++i)
if (!ps[i])
ps[i] = 0xfe;
while (!ps[i])
AK::fill_with_random(ps + i, 1);
u8 paddings[] { 0x00, 0x02 };