summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Tests/LibCrypto/TestHash.cpp20
-rw-r--r--Userland/Libraries/LibCrypto/Hash/SHA2.cpp18
-rw-r--r--Userland/Libraries/LibCrypto/Hash/SHA2.h4
3 files changed, 40 insertions, 2 deletions
diff --git a/Tests/LibCrypto/TestHash.cpp b/Tests/LibCrypto/TestHash.cpp
index a5f8a4fe83..29fd33f88c 100644
--- a/Tests/LibCrypto/TestHash.cpp
+++ b/Tests/LibCrypto/TestHash.cpp
@@ -181,6 +181,16 @@ TEST_CASE(test_SHA384_hash_string)
EXPECT(memcmp(result, digest.data, Crypto::Hash::SHA384::digest_size()) == 0);
}
+TEST_CASE(test_SHA384_hash_bug)
+{
+ u8 result[] {
+ 0x09, 0x33, 0x0c, 0x33, 0xf7, 0x11, 0x47, 0xe8, 0x3d, 0x19, 0x2f, 0xc7, 0x82, 0xcd, 0x1b, 0x47, 0x53, 0x11, 0x1b, 0x17, 0x3b, 0x3b, 0x05, 0xd2, 0x2f, 0xa0, 0x80, 0x86, 0xe3, 0xb0, 0xf7, 0x12, 0xfc, 0xc7, 0xc7, 0x1a, 0x55, 0x7e, 0x2d, 0xb9, 0x66, 0xc3, 0xe9, 0xfa, 0x91, 0x74, 0x60, 0x39
+ };
+ ReadonlyBytes result_bytes { result, 48 };
+ auto digest = Crypto::Hash::SHA384::hash("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu");
+ EXPECT_EQ(result_bytes, digest.bytes());
+}
+
TEST_CASE(test_SHA512_name)
{
Crypto::Hash::SHA512 sha;
@@ -196,6 +206,16 @@ TEST_CASE(test_SHA512_hash_string)
EXPECT(memcmp(result, digest.data, Crypto::Hash::SHA512::digest_size()) == 0);
}
+TEST_CASE(test_SHA512_hash_bug)
+{
+ u8 result[] {
+ 0x8e, 0x95, 0x9b, 0x75, 0xda, 0xe3, 0x13, 0xda, 0x8c, 0xf4, 0xf7, 0x28, 0x14, 0xfc, 0x14, 0x3f, 0x8f, 0x77, 0x79, 0xc6, 0xeb, 0x9f, 0x7f, 0xa1, 0x72, 0x99, 0xae, 0xad, 0xb6, 0x88, 0x90, 0x18, 0x50, 0x1d, 0x28, 0x9e, 0x49, 0x00, 0xf7, 0xe4, 0x33, 0x1b, 0x99, 0xde, 0xc4, 0xb5, 0x43, 0x3a, 0xc7, 0xd3, 0x29, 0xee, 0xb6, 0xdd, 0x26, 0x54, 0x5e, 0x96, 0xe5, 0x5b, 0x87, 0x4b, 0xe9, 0x09
+ };
+ ReadonlyBytes result_bytes { result, 64 };
+ auto digest = Crypto::Hash::SHA512::hash("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu");
+ EXPECT_EQ(result_bytes, digest.bytes());
+}
+
TEST_CASE(test_SHA512_hash_empty_string)
{
u8 result[] {
diff --git a/Userland/Libraries/LibCrypto/Hash/SHA2.cpp b/Userland/Libraries/LibCrypto/Hash/SHA2.cpp
index 786bbceff9..85a4a0dd87 100644
--- a/Userland/Libraries/LibCrypto/Hash/SHA2.cpp
+++ b/Userland/Libraries/LibCrypto/Hash/SHA2.cpp
@@ -241,6 +241,15 @@ SHA384::DigestType SHA384::peek()
m_data_buffer[BlockSize - 6] = m_bit_length >> 40;
m_data_buffer[BlockSize - 7] = m_bit_length >> 48;
m_data_buffer[BlockSize - 8] = m_bit_length >> 56;
+ // FIXME: Theoretically we should keep track of the number of bits as a u128, now we can only hash up to 2 EiB.
+ m_data_buffer[BlockSize - 9] = 0;
+ m_data_buffer[BlockSize - 10] = 0;
+ m_data_buffer[BlockSize - 11] = 0;
+ m_data_buffer[BlockSize - 12] = 0;
+ m_data_buffer[BlockSize - 13] = 0;
+ m_data_buffer[BlockSize - 14] = 0;
+ m_data_buffer[BlockSize - 15] = 0;
+ m_data_buffer[BlockSize - 16] = 0;
transform(m_data_buffer);
@@ -356,6 +365,15 @@ SHA512::DigestType SHA512::peek()
m_data_buffer[BlockSize - 6] = m_bit_length >> 40;
m_data_buffer[BlockSize - 7] = m_bit_length >> 48;
m_data_buffer[BlockSize - 8] = m_bit_length >> 56;
+ // FIXME: Theoretically we should keep track of the number of bits as a u128, now we can only hash up to 2 EiB.
+ m_data_buffer[BlockSize - 9] = 0;
+ m_data_buffer[BlockSize - 10] = 0;
+ m_data_buffer[BlockSize - 11] = 0;
+ m_data_buffer[BlockSize - 12] = 0;
+ m_data_buffer[BlockSize - 13] = 0;
+ m_data_buffer[BlockSize - 14] = 0;
+ m_data_buffer[BlockSize - 15] = 0;
+ m_data_buffer[BlockSize - 16] = 0;
transform(m_data_buffer);
diff --git a/Userland/Libraries/LibCrypto/Hash/SHA2.h b/Userland/Libraries/LibCrypto/Hash/SHA2.h
index 584e229845..b9cb3315dd 100644
--- a/Userland/Libraries/LibCrypto/Hash/SHA2.h
+++ b/Userland/Libraries/LibCrypto/Hash/SHA2.h
@@ -176,7 +176,7 @@ private:
u64 m_bit_length { 0 };
u64 m_state[8];
- constexpr static auto FinalBlockDataSize = BlockSize - 8;
+ constexpr static auto FinalBlockDataSize = BlockSize - 16;
constexpr static auto Rounds = 80;
};
@@ -228,7 +228,7 @@ private:
u64 m_bit_length { 0 };
u64 m_state[8];
- constexpr static auto FinalBlockDataSize = BlockSize - 8;
+ constexpr static auto FinalBlockDataSize = BlockSize - 16;
constexpr static auto Rounds = 80;
};