mirror of
https://github.com/LadybirdBrowser/ladybird
synced 2026-04-26 01:35:08 +02: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:
committed by
Andreas Kling
parent
180207062c
commit
b00ffc860b
Notes:
sideshowbarker
2024-07-19 04:26:38 +09:00
Author: https://github.com/alimpfard Commit: https://github.com/SerenityOS/serenity/commit/b00ffc860b9 Pull-request: https://github.com/SerenityOS/serenity/pull/2929 Issue: https://github.com/SerenityOS/serenity/issues/2691
@@ -55,21 +55,23 @@ UnsignedBigInteger UnsignedBigInteger::create_invalid()
|
||||
return invalid;
|
||||
}
|
||||
|
||||
size_t UnsignedBigInteger::export_data(Bytes data) const
|
||||
size_t UnsignedBigInteger::export_data(Bytes data, bool remove_leading_zeros) const
|
||||
{
|
||||
size_t word_count = trimmed_length();
|
||||
size_t out = 0;
|
||||
if (word_count > 0) {
|
||||
ssize_t leading_zeros = -1;
|
||||
u32 word = m_words[word_count - 1];
|
||||
for (size_t i = 0; i < sizeof(u32); i++) {
|
||||
u8 byte = (u8)(word >> ((sizeof(u32) - i - 1) * 8));
|
||||
data[out++] = byte;
|
||||
if (leading_zeros < 0 && byte != 0)
|
||||
leading_zeros = (int)i;
|
||||
if (remove_leading_zeros) {
|
||||
u32 word = m_words[word_count - 1];
|
||||
for (size_t i = 0; i < sizeof(u32); i++) {
|
||||
u8 byte = (u8)(word >> ((sizeof(u32) - i - 1) * 8));
|
||||
data[out++] = byte;
|
||||
if (leading_zeros < 0 && byte != 0)
|
||||
leading_zeros = (int)i;
|
||||
}
|
||||
}
|
||||
for (size_t i = word_count - 1; i > 0; i--) {
|
||||
word = m_words[i - 1];
|
||||
for (size_t i = word_count - (remove_leading_zeros ? 1 : 0); i > 0; i--) {
|
||||
auto word = m_words[i - 1];
|
||||
data[out++] = (u8)(word >> 24);
|
||||
data[out++] = (u8)(word >> 16);
|
||||
data[out++] = (u8)(word >> 8);
|
||||
|
||||
Reference in New Issue
Block a user