diff options
author | AnotherTest <ali.mpfard@gmail.com> | 2020-06-04 16:46:26 +0430 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-06-04 15:58:04 +0200 |
commit | 63cc2f58ea5d5481afa570aebd047dbe9cd78e28 (patch) | |
tree | 3d5f2400263887bfefa8b1157487c603abf95518 /Libraries/LibCrypto/Hash | |
parent | a3f51089d205c8a3ef7b54f7515a5768b8872577 (diff) | |
download | serenity-63cc2f58ea5d5481afa570aebd047dbe9cd78e28.zip |
LibCrypto: Correctly pad blocks with FinalBlockSize < size < BlockSize
This fixes #2488
Diffstat (limited to 'Libraries/LibCrypto/Hash')
-rw-r--r-- | Libraries/LibCrypto/Hash/SHA1.cpp | 11 | ||||
-rw-r--r-- | Libraries/LibCrypto/Hash/SHA2.cpp | 22 |
2 files changed, 30 insertions, 3 deletions
diff --git a/Libraries/LibCrypto/Hash/SHA1.cpp b/Libraries/LibCrypto/Hash/SHA1.cpp index c6afad79b0..42b23209a0 100644 --- a/Libraries/LibCrypto/Hash/SHA1.cpp +++ b/Libraries/LibCrypto/Hash/SHA1.cpp @@ -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); } diff --git a/Libraries/LibCrypto/Hash/SHA2.cpp b/Libraries/LibCrypto/Hash/SHA2.cpp index a0fb9de78a..60ad8e4a09 100644 --- a/Libraries/LibCrypto/Hash/SHA2.cpp +++ b/Libraries/LibCrypto/Hash/SHA2.cpp @@ -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); } |