mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +00:00
LibCrypto: Correctly pad blocks with FinalBlockSize < size < BlockSize
This fixes #2488
This commit is contained in:
parent
a3f51089d2
commit
63cc2f58ea
Notes:
sideshowbarker
2024-07-19 05:50:25 +09:00
Author: https://github.com/alimpfard Commit: https://github.com/SerenityOS/serenity/commit/63cc2f58ea5 Pull-request: https://github.com/SerenityOS/serenity/pull/2495 Issue: https://github.com/SerenityOS/serenity/issues/2488
2 changed files with 30 additions and 3 deletions
|
@ -115,17 +115,26 @@ SHA1::DigestType SHA1::peek()
|
|||
__builtin_memcpy(data, m_data_buffer, m_data_length);
|
||||
__builtin_memcpy(state, m_state, 20);
|
||||
|
||||
if (BlockSize == m_data_length) {
|
||||
transform(m_data_buffer);
|
||||
m_bit_length += BlockSize * 8;
|
||||
m_data_length = 0;
|
||||
i = 0;
|
||||
}
|
||||
|
||||
if (m_data_length < FinalBlockDataSize) {
|
||||
m_data_buffer[i++] = 0x80;
|
||||
while (i < FinalBlockDataSize)
|
||||
m_data_buffer[i++] = 0x00;
|
||||
|
||||
} else {
|
||||
// First, complete a block with some padding.
|
||||
m_data_buffer[i++] = 0x80;
|
||||
while (i < BlockSize)
|
||||
m_data_buffer[i++] = 0x00;
|
||||
|
||||
transform(m_data_buffer);
|
||||
|
||||
// Then start another block with BlockSize - 8 bytes of zeros
|
||||
__builtin_memset(m_data_buffer, 0, FinalBlockDataSize);
|
||||
}
|
||||
|
||||
|
|
|
@ -110,17 +110,26 @@ SHA256::DigestType SHA256::peek()
|
|||
DigestType digest;
|
||||
size_t i = m_data_length;
|
||||
|
||||
if (BlockSize == m_data_length) {
|
||||
transform(m_data_buffer);
|
||||
m_bit_length += BlockSize * 8;
|
||||
m_data_length = 0;
|
||||
i = 0;
|
||||
}
|
||||
|
||||
if (m_data_length < FinalBlockDataSize) {
|
||||
m_data_buffer[i++] = 0x80;
|
||||
while (i < FinalBlockDataSize)
|
||||
m_data_buffer[i++] = 0x00;
|
||||
|
||||
} else {
|
||||
// First, complete a block with some padding.
|
||||
m_data_buffer[i++] = 0x80;
|
||||
while (i < BlockSize)
|
||||
m_data_buffer[i++] = 0x00;
|
||||
|
||||
transform(m_data_buffer);
|
||||
|
||||
// Then start another block with BlockSize - 8 bytes of zeros
|
||||
__builtin_memset(m_data_buffer, 0, FinalBlockDataSize);
|
||||
}
|
||||
|
||||
|
@ -218,17 +227,26 @@ SHA512::DigestType SHA512::peek()
|
|||
DigestType digest;
|
||||
size_t i = m_data_length;
|
||||
|
||||
if (BlockSize == m_data_length) {
|
||||
transform(m_data_buffer);
|
||||
m_bit_length += BlockSize * 8;
|
||||
m_data_length = 0;
|
||||
i = 0;
|
||||
}
|
||||
|
||||
if (m_data_length < FinalBlockDataSize) {
|
||||
m_data_buffer[i++] = 0x80;
|
||||
while (i < FinalBlockDataSize)
|
||||
m_data_buffer[i++] = 0x00;
|
||||
|
||||
} else {
|
||||
// First, complete a block with some padding.
|
||||
m_data_buffer[i++] = 0x80;
|
||||
while (i < BlockSize)
|
||||
m_data_buffer[i++] = 0x00;
|
||||
|
||||
transform(m_data_buffer);
|
||||
|
||||
// Then start another block with BlockSize - 8 bytes of zeros
|
||||
__builtin_memset(m_data_buffer, 0, FinalBlockDataSize);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue