summaryrefslogtreecommitdiff
path: root/Libraries/LibCrypto/Hash
diff options
context:
space:
mode:
authorAnotherTest <ali.mpfard@gmail.com>2020-04-23 03:03:05 +0430
committerAndreas Kling <kling@serenityos.org>2020-05-02 12:24:10 +0200
commit05e2c7d9cf529c4b635f762fc1b5683c076cb00e (patch)
treeaa1ade0b6aca6a662be6ac731f2d4c0e590513e9 /Libraries/LibCrypto/Hash
parenta1e15705529e9edaa4c67ed8683e7f987c4e473c (diff)
downloadserenity-05e2c7d9cf529c4b635f762fc1b5683c076cb00e.zip
LibCrypto+LibTLS: Reformat everything
I have no idea how I'll squash _this_ one...
Diffstat (limited to 'Libraries/LibCrypto/Hash')
-rw-r--r--Libraries/LibCrypto/Hash/HashFunction.h32
-rw-r--r--Libraries/LibCrypto/Hash/MD5.cpp281
-rw-r--r--Libraries/LibCrypto/Hash/MD5.h165
-rw-r--r--Libraries/LibCrypto/Hash/SHA2.cpp400
-rw-r--r--Libraries/LibCrypto/Hash/SHA2.h310
5 files changed, 595 insertions, 593 deletions
diff --git a/Libraries/LibCrypto/Hash/HashFunction.h b/Libraries/LibCrypto/Hash/HashFunction.h
index 0d626fcc48..19c2f0db05 100644
--- a/Libraries/LibCrypto/Hash/HashFunction.h
+++ b/Libraries/LibCrypto/Hash/HashFunction.h
@@ -33,27 +33,27 @@
namespace Crypto {
namespace Hash {
- template <size_t BlockS, typename DigestT>
- class HashFunction {
- public:
- static constexpr auto BlockSize = BlockS / 8;
- static constexpr auto DigestSize = sizeof(DigestT);
+template<size_t BlockS, typename DigestT>
+class HashFunction {
+public:
+ static constexpr auto BlockSize = BlockS / 8;
+ static constexpr auto DigestSize = sizeof(DigestT);
- using DigestType = DigestT;
+ using DigestType = DigestT;
- static size_t block_size() { return BlockSize; };
- static size_t digest_size() { return DigestSize; };
+ static size_t block_size() { return BlockSize; };
+ static size_t digest_size() { return DigestSize; };
- virtual void update(const u8*, size_t) = 0;
- virtual void update(const ByteBuffer& buffer) = 0;
- virtual void update(const StringView& string) = 0;
+ virtual void update(const u8*, size_t) = 0;
+ virtual void update(const ByteBuffer& buffer) = 0;
+ virtual void update(const StringView& string) = 0;
- virtual DigestType peek() = 0;
- virtual DigestType digest() = 0;
+ virtual DigestType peek() = 0;
+ virtual DigestType digest() = 0;
- virtual void reset() = 0;
+ virtual void reset() = 0;
- virtual String class_name() const = 0;
- };
+ virtual String class_name() const = 0;
+};
}
}
diff --git a/Libraries/LibCrypto/Hash/MD5.cpp b/Libraries/LibCrypto/Hash/MD5.cpp
index 2a44efb1fb..0cde62d29d 100644
--- a/Libraries/LibCrypto/Hash/MD5.cpp
+++ b/Libraries/LibCrypto/Hash/MD5.cpp
@@ -67,158 +67,159 @@ static constexpr inline void round_4(u32& a, u32 b, u32 c, u32 d, u32 x, u32 s,
namespace Crypto {
namespace Hash {
- void MD5::update(const u8* input, size_t length)
- {
- auto index = (u32)(m_count[0] >> 3) & 0x3f;
- size_t offset { 0 };
- m_count[0] += (u32)length << 3;
- if (m_count[0] < ((u32)length << 3)) {
- ++m_count[1];
- }
- m_count[1] += (u32)length >> 29;
-
- auto part_length = 64 - index;
- if (length >= part_length) {
- m_buffer.overwrite(index, input, part_length);
- transform(m_buffer.data());
-
- for (offset = part_length; offset + 63 < length; offset += 64)
- transform(&input[offset]);
-
- index = 0;
- }
- ASSERT(length < part_length || length - offset <= 64);
- m_buffer.overwrite(index, &input[offset], length - offset);
+void MD5::update(const u8* input, size_t length)
+{
+ auto index = (u32)(m_count[0] >> 3) & 0x3f;
+ size_t offset { 0 };
+ m_count[0] += (u32)length << 3;
+ if (m_count[0] < ((u32)length << 3)) {
+ ++m_count[1];
}
- MD5::DigestType MD5::digest()
- {
- auto digest = peek();
- reset();
- return digest;
+ m_count[1] += (u32)length >> 29;
+
+ auto part_length = 64 - index;
+ if (length >= part_length) {
+ m_buffer.overwrite(index, input, part_length);
+ transform(m_buffer.data());
+
+ for (offset = part_length; offset + 63 < length; offset += 64)
+ transform(&input[offset]);
+
+ index = 0;
}
- MD5::DigestType MD5::peek()
- {
- DigestType digest;
- u8 bits[8];
+ ASSERT(length < part_length || length - offset <= 64);
+ m_buffer.overwrite(index, &input[offset], length - offset);
+}
+MD5::DigestType MD5::digest()
+{
+ auto digest = peek();
+ reset();
+ return digest;
+}
- encode(m_count, bits, 8);
+MD5::DigestType MD5::peek()
+{
+ DigestType digest;
+ u8 bits[8];
- // pad the data to 56%64
- u32 index = (u32)((m_count[0] >> 3) & 0x3f);
- u32 pad_length = index < 56 ? 56 - index : 120 - index;
- update(MD5Constants::PADDING, pad_length);
+ encode(m_count, bits, 8);
- // append length
- update(bits, 8);
+ // pad the data to 56%64
+ u32 index = (u32)((m_count[0] >> 3) & 0x3f);
+ u32 pad_length = index < 56 ? 56 - index : 120 - index;
+ update(MD5Constants::PADDING, pad_length);
- // store state (4 registers ABCD)
- encode(&m_A, digest.data, 4 * sizeof(m_A));
+ // append length
+ update(bits, 8);
- return digest;
- }
+ // store state (4 registers ABCD)
+ encode(&m_A, digest.data, 4 * sizeof(m_A));
- void MD5::encode(const u32* from, u8* to, size_t length)
- {
- for (size_t i = 0, j = 0; j < length; ++i, j += 4) {
- to[j] = (u8)(from[i] & 0xff);
- to[j + 1] = (u8)((from[i] >> 8) & 0xff);
- to[j + 2] = (u8)((from[i] >> 16) & 0xff);
- to[j + 3] = (u8)((from[i] >> 24) & 0xff);
- }
- }
+ return digest;
+}
- void MD5::decode(const u8* from, u32* to, size_t length)
- {
- for (size_t i = 0, j = 0; j < length; ++i, j += 4)
- to[i] = (((u32)from[j]) | (((u32)from[j + 1]) << 8) | (((u32)from[j + 2]) << 16) | (((u32)from[j + 3]) << 24));
+void MD5::encode(const u32* from, u8* to, size_t length)
+{
+ for (size_t i = 0, j = 0; j < length; ++i, j += 4) {
+ to[j] = (u8)(from[i] & 0xff);
+ to[j + 1] = (u8)((from[i] >> 8) & 0xff);
+ to[j + 2] = (u8)((from[i] >> 16) & 0xff);
+ to[j + 3] = (u8)((from[i] >> 24) & 0xff);
}
+}
- void MD5::transform(const u8* block)
- {
- auto a = m_A;
- auto b = m_B;
- auto c = m_C;
- auto d = m_D;
- u32 x[16];
-
- decode(block, x, 64);
-
- round_1(a, b, c, d, x[0], MD5Constants::S11, 0xd76aa478); // 1
- round_1(d, a, b, c, x[1], MD5Constants::S12, 0xe8c7b756); // 2
- round_1(c, d, a, b, x[2], MD5Constants::S13, 0x242070db); // 3
- round_1(b, c, d, a, x[3], MD5Constants::S14, 0xc1bdceee); // 4
- round_1(a, b, c, d, x[4], MD5Constants::S11, 0xf57c0faf); // 5
- round_1(d, a, b, c, x[5], MD5Constants::S12, 0x4787c62a); // 6
- round_1(c, d, a, b, x[6], MD5Constants::S13, 0xa8304613); // 7
- round_1(b, c, d, a, x[7], MD5Constants::S14, 0xfd469501); // 8
- round_1(a, b, c, d, x[8], MD5Constants::S11, 0x698098d8); // 9
- round_1(d, a, b, c, x[9], MD5Constants::S12, 0x8b44f7af); // 10
- round_1(c, d, a, b, x[10], MD5Constants::S13, 0xffff5bb1); // 11
- round_1(b, c, d, a, x[11], MD5Constants::S14, 0x895cd7be); // 12
- round_1(a, b, c, d, x[12], MD5Constants::S11, 0x6b901122); // 13
- round_1(d, a, b, c, x[13], MD5Constants::S12, 0xfd987193); // 14
- round_1(c, d, a, b, x[14], MD5Constants::S13, 0xa679438e); // 15
- round_1(b, c, d, a, x[15], MD5Constants::S14, 0x49b40821); // 16
-
- round_2(a, b, c, d, x[1], MD5Constants::S21, 0xf61e2562); // 17
- round_2(d, a, b, c, x[6], MD5Constants::S22, 0xc040b340); // 18
- round_2(c, d, a, b, x[11], MD5Constants::S23, 0x265e5a51); // 19
- round_2(b, c, d, a, x[0], MD5Constants::S24, 0xe9b6c7aa); // 20
- round_2(a, b, c, d, x[5], MD5Constants::S21, 0xd62f105d); // 21
- round_2(d, a, b, c, x[10], MD5Constants::S22, 0x2441453); // 22
- round_2(c, d, a, b, x[15], MD5Constants::S23, 0xd8a1e681); // 23
- round_2(b, c, d, a, x[4], MD5Constants::S24, 0xe7d3fbc8); // 24
- round_2(a, b, c, d, x[9], MD5Constants::S21, 0x21e1cde6); // 25
- round_2(d, a, b, c, x[14], MD5Constants::S22, 0xc33707d6); // 26
- round_2(c, d, a, b, x[3], MD5Constants::S23, 0xf4d50d87); // 27
- round_2(b, c, d, a, x[8], MD5Constants::S24, 0x455a14ed); // 28
- round_2(a, b, c, d, x[13], MD5Constants::S21, 0xa9e3e905); // 29
- round_2(d, a, b, c, x[2], MD5Constants::S22, 0xfcefa3f8); // 30
- round_2(c, d, a, b, x[7], MD5Constants::S23, 0x676f02d9); // 31
- round_2(b, c, d, a, x[12], MD5Constants::S24, 0x8d2a4c8a); // 32
-
- round_3(a, b, c, d, x[5], MD5Constants::S31, 0xfffa3942); // 33
- round_3(d, a, b, c, x[8], MD5Constants::S32, 0x8771f681); // 34
- round_3(c, d, a, b, x[11], MD5Constants::S33, 0x6d9d6122); // 35
- round_3(b, c, d, a, x[14], MD5Constants::S34, 0xfde5380c); // 36
- round_3(a, b, c, d, x[1], MD5Constants::S31, 0xa4beea44); // 37
- round_3(d, a, b, c, x[4], MD5Constants::S32, 0x4bdecfa9); // 38
- round_3(c, d, a, b, x[7], MD5Constants::S33, 0xf6bb4b60); // 39
- round_3(b, c, d, a, x[10], MD5Constants::S34, 0xbebfbc70); // 40
- round_3(a, b, c, d, x[13], MD5Constants::S31, 0x289b7ec6); // 41
- round_3(d, a, b, c, x[0], MD5Constants::S32, 0xeaa127fa); // 42
- round_3(c, d, a, b, x[3], MD5Constants::S33, 0xd4ef3085); // 43
- round_3(b, c, d, a, x[6], MD5Constants::S34, 0x4881d05); // 44
- round_3(a, b, c, d, x[9], MD5Constants::S31, 0xd9d4d039); // 45
- round_3(d, a, b, c, x[12], MD5Constants::S32, 0xe6db99e5); // 46
- round_3(c, d, a, b, x[15], MD5Constants::S33, 0x1fa27cf8); // 47
- round_3(b, c, d, a, x[2], MD5Constants::S34, 0xc4ac5665); // 48
-
- round_4(a, b, c, d, x[0], MD5Constants::S41, 0xf4292244); // 49
- round_4(d, a, b, c, x[7], MD5Constants::S42, 0x432aff97); // 50
- round_4(c, d, a, b, x[14], MD5Constants::S43, 0xab9423a7); // 51
- round_4(b, c, d, a, x[5], MD5Constants::S44, 0xfc93a039); // 52
- round_4(a, b, c, d, x[12], MD5Constants::S41, 0x655b59c3); // 53
- round_4(d, a, b, c, x[3], MD5Constants::S42, 0x8f0ccc92); // 54
- round_4(c, d, a, b, x[10], MD5Constants::S43, 0xffeff47d); // 55
- round_4(b, c, d, a, x[1], MD5Constants::S44, 0x85845dd1); // 56
- round_4(a, b, c, d, x[8], MD5Constants::S41, 0x6fa87e4f); // 57
- round_4(d, a, b, c, x[15], MD5Constants::S42, 0xfe2ce6e0); // 58
- round_4(c, d, a, b, x[6], MD5Constants::S43, 0xa3014314); // 59
- round_4(b, c, d, a, x[13], MD5Constants::S44, 0x4e0811a1); // 60
- round_4(a, b, c, d, x[4], MD5Constants::S41, 0xf7537e82); // 61
- round_4(d, a, b, c, x[11], MD5Constants::S42, 0xbd3af235); // 62
- round_4(c, d, a, b, x[2], MD5Constants::S43, 0x2ad7d2bb); // 63
- round_4(b, c, d, a, x[9], MD5Constants::S44, 0xeb86d391); // 64
-
- m_A += a;
- m_B += b;
- m_C += c;
- m_D += d;
-
- __builtin_memset(x, 0, sizeof(x));
- }
+void MD5::decode(const u8* from, u32* to, size_t length)
+{
+ for (size_t i = 0, j = 0; j < length; ++i, j += 4)
+ to[i] = (((u32)from[j]) | (((u32)from[j + 1]) << 8) | (((u32)from[j + 2]) << 16) | (((u32)from[j + 3]) << 24));
+}
+
+void MD5::transform(const u8* block)
+{
+ auto a = m_A;
+ auto b = m_B;
+ auto c = m_C;
+ auto d = m_D;
+ u32 x[16];
+
+ decode(block, x, 64);
+
+ round_1(a, b, c, d, x[0], MD5Constants::S11, 0xd76aa478); // 1
+ round_1(d, a, b, c, x[1], MD5Constants::S12, 0xe8c7b756); // 2
+ round_1(c, d, a, b, x[2], MD5Constants::S13, 0x242070db); // 3
+ round_1(b, c, d, a, x[3], MD5Constants::S14, 0xc1bdceee); // 4
+ round_1(a, b, c, d, x[4], MD5Constants::S11, 0xf57c0faf); // 5
+ round_1(d, a, b, c, x[5], MD5Constants::S12, 0x4787c62a); // 6
+ round_1(c, d, a, b, x[6], MD5Constants::S13, 0xa8304613); // 7
+ round_1(b, c, d, a, x[7], MD5Constants::S14, 0xfd469501); // 8
+ round_1(a, b, c, d, x[8], MD5Constants::S11, 0x698098d8); // 9
+ round_1(d, a, b, c, x[9], MD5Constants::S12, 0x8b44f7af); // 10
+ round_1(c, d, a, b, x[10], MD5Constants::S13, 0xffff5bb1); // 11
+ round_1(b, c, d, a, x[11], MD5Constants::S14, 0x895cd7be); // 12
+ round_1(a, b, c, d, x[12], MD5Constants::S11, 0x6b901122); // 13
+ round_1(d, a, b, c, x[13], MD5Constants::S12, 0xfd987193); // 14
+ round_1(c, d, a, b, x[14], MD5Constants::S13, 0xa679438e); // 15
+ round_1(b, c, d, a, x[15], MD5Constants::S14, 0x49b40821); // 16
+
+ round_2(a, b, c, d, x[1], MD5Constants::S21, 0xf61e2562); // 17
+ round_2(d, a, b, c, x[6], MD5Constants::S22, 0xc040b340); // 18
+ round_2(c, d, a, b, x[11], MD5Constants::S23, 0x265e5a51); // 19
+ round_2(b, c, d, a, x[0], MD5Constants::S24, 0xe9b6c7aa); // 20
+ round_2(a, b, c, d, x[5], MD5Constants::S21, 0xd62f105d); // 21
+ round_2(d, a, b, c, x[10], MD5Constants::S22, 0x2441453); // 22
+ round_2(c, d, a, b, x[15], MD5Constants::S23, 0xd8a1e681); // 23
+ round_2(b, c, d, a, x[4], MD5Constants::S24, 0xe7d3fbc8); // 24
+ round_2(a, b, c, d, x[9], MD5Constants::S21, 0x21e1cde6); // 25
+ round_2(d, a, b, c, x[14], MD5Constants::S22, 0xc33707d6); // 26
+ round_2(c, d, a, b, x[3], MD5Constants::S23, 0xf4d50d87); // 27
+ round_2(b, c, d, a, x[8], MD5Constants::S24, 0x455a14ed); // 28
+ round_2(a, b, c, d, x[13], MD5Constants::S21, 0xa9e3e905); // 29
+ round_2(d, a, b, c, x[2], MD5Constants::S22, 0xfcefa3f8); // 30
+ round_2(c, d, a, b, x[7], MD5Constants::S23, 0x676f02d9); // 31
+ round_2(b, c, d, a, x[12], MD5Constants::S24, 0x8d2a4c8a); // 32
+
+ round_3(a, b, c, d, x[5], MD5Constants::S31, 0xfffa3942); // 33
+ round_3(d, a, b, c, x[8], MD5Constants::S32, 0x8771f681); // 34
+ round_3(c, d, a, b, x[11], MD5Constants::S33, 0x6d9d6122); // 35
+ round_3(b, c, d, a, x[14], MD5Constants::S34, 0xfde5380c); // 36
+ round_3(a, b, c, d, x[1], MD5Constants::S31, 0xa4beea44); // 37
+ round_3(d, a, b, c, x[4], MD5Constants::S32, 0x4bdecfa9); // 38
+ round_3(c, d, a, b, x[7], MD5Constants::S33, 0xf6bb4b60); // 39
+ round_3(b, c, d, a, x[10], MD5Constants::S34, 0xbebfbc70); // 40
+ round_3(a, b, c, d, x[13], MD5Constants::S31, 0x289b7ec6); // 41
+ round_3(d, a, b, c, x[0], MD5Constants::S32, 0xeaa127fa); // 42
+ round_3(c, d, a, b, x[3], MD5Constants::S33, 0xd4ef3085); // 43
+ round_3(b, c, d, a, x[6], MD5Constants::S34, 0x4881d05); // 44
+ round_3(a, b, c, d, x[9], MD5Constants::S31, 0xd9d4d039); // 45
+ round_3(d, a, b, c, x[12], MD5Constants::S32, 0xe6db99e5); // 46
+ round_3(c, d, a, b, x[15], MD5Constants::S33, 0x1fa27cf8); // 47
+ round_3(b, c, d, a, x[2], MD5Constants::S34, 0xc4ac5665); // 48
+
+ round_4(a, b, c, d, x[0], MD5Constants::S41, 0xf4292244); // 49
+ round_4(d, a, b, c, x[7], MD5Constants::S42, 0x432aff97); // 50
+ round_4(c, d, a, b, x[14], MD5Constants::S43, 0xab9423a7); // 51
+ round_4(b, c, d, a, x[5], MD5Constants::S44, 0xfc93a039); // 52
+ round_4(a, b, c, d, x[12], MD5Constants::S41, 0x655b59c3); // 53
+ round_4(d, a, b, c, x[3], MD5Constants::S42, 0x8f0ccc92); // 54
+ round_4(c, d, a, b, x[10], MD5Constants::S43, 0xffeff47d); // 55
+ round_4(b, c, d, a, x[1], MD5Constants::S44, 0x85845dd1); // 56
+ round_4(a, b, c, d, x[8], MD5Constants::S41, 0x6fa87e4f); // 57
+ round_4(d, a, b, c, x[15], MD5Constants::S42, 0xfe2ce6e0); // 58
+ round_4(c, d, a, b, x[6], MD5Constants::S43, 0xa3014314); // 59
+ round_4(b, c, d, a, x[13], MD5Constants::S44, 0x4e0811a1); // 60
+ round_4(a, b, c, d, x[4], MD5Constants::S41, 0xf7537e82); // 61
+ round_4(d, a, b, c, x[11], MD5Constants::S42, 0xbd3af235); // 62
+ round_4(c, d, a, b, x[2], MD5Constants::S43, 0x2ad7d2bb); // 63
+ round_4(b, c, d, a, x[9], MD5Constants::S44, 0xeb86d391); // 64
+
+ m_A += a;
+ m_B += b;
+ m_C += c;
+ m_D += d;
+
+ __builtin_memset(x, 0, sizeof(x));
+}
}
}
diff --git a/Libraries/LibCrypto/Hash/MD5.h b/Libraries/LibCrypto/Hash/MD5.h
index 12da7c84dc..883029527c 100644
--- a/Libraries/LibCrypto/Hash/MD5.h
+++ b/Libraries/LibCrypto/Hash/MD5.h
@@ -33,90 +33,91 @@
namespace Crypto {
namespace Hash {
- struct MD5Digest {
- u8 data[16];
- };
-
- namespace MD5Constants {
-
- constexpr u32 init_A = 0x67452301;
- constexpr u32 init_B = 0xefcdab89;
- constexpr u32 init_C = 0x98badcfe;
- constexpr u32 init_D = 0x10325476;
- constexpr u32 S11 = 7;
- constexpr u32 S12 = 12;
- constexpr u32 S13 = 17;
- constexpr u32 S14 = 22;
- constexpr u32 S21 = 5;
- constexpr u32 S22 = 9;
- constexpr u32 S23 = 14;
- constexpr u32 S24 = 20;
- constexpr u32 S31 = 4;
- constexpr u32 S32 = 11;
- constexpr u32 S33 = 16;
- constexpr u32 S34 = 23;
- constexpr u32 S41 = 6;
- constexpr u32 S42 = 10;
- constexpr u32 S43 = 15;
- constexpr u32 S44 = 21;
- constexpr u8 PADDING[] = {
- 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0
- };
+struct MD5Digest {
+ u8 data[16];
+};
+
+namespace MD5Constants {
+
+constexpr u32 init_A = 0x67452301;
+constexpr u32 init_B = 0xefcdab89;
+constexpr u32 init_C = 0x98badcfe;
+constexpr u32 init_D = 0x10325476;
+constexpr u32 S11 = 7;
+constexpr u32 S12 = 12;
+constexpr u32 S13 = 17;
+constexpr u32 S14 = 22;
+constexpr u32 S21 = 5;
+constexpr u32 S22 = 9;
+constexpr u32 S23 = 14;
+constexpr u32 S24 = 20;
+constexpr u32 S31 = 4;
+constexpr u32 S32 = 11;
+constexpr u32 S33 = 16;
+constexpr u32 S34 = 23;
+constexpr u32 S41 = 6;
+constexpr u32 S42 = 10;
+constexpr u32 S43 = 15;
+constexpr u32 S44 = 21;
+constexpr u8 PADDING[] = {
+ 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0
+};
+
+}
+
+class MD5 final : public HashFunction<512, MD5Digest> {
+public:
+ MD5()
+ {
+ m_buffer = ByteBuffer::wrap(m_data_buffer, sizeof(m_data_buffer));
+ }
+
+ virtual void update(const u8*, size_t) override;
+ virtual void update(const ByteBuffer& buffer) override { update(buffer.data(), buffer.size()); };
+ virtual void update(const StringView& string) override { update((const u8*)string.characters_without_null_termination(), string.length()); };
+ virtual DigestType digest() override;
+ virtual DigestType peek() override;
+
+ virtual String class_name() const override { return "MD5"; }
+
+ inline static DigestType hash(const u8* data, size_t length)
+ {
+ MD5 md5;
+ md5.update(data, length);
+ return md5.digest();
}
- class MD5 final : public HashFunction<512, MD5Digest> {
- public:
- MD5()
- {
- m_buffer = ByteBuffer::wrap(m_data_buffer, sizeof(m_data_buffer));
- }
-
- virtual void update(const u8*, size_t) override;
- virtual void update(const ByteBuffer& buffer) override { update(buffer.data(), buffer.size()); };
- virtual void update(const StringView& string) override { update((const u8*)string.characters_without_null_termination(), string.length()); };
- virtual DigestType digest() override;
- virtual DigestType peek() override;
-
- virtual String class_name() const override { return "MD5"; }
-
- inline static DigestType hash(const u8* data, size_t length)
- {
- MD5 md5;
- md5.update(data, length);
- return md5.digest();
- }
-
- inline static DigestType hash(const ByteBuffer& buffer) { return hash(buffer.data(), buffer.size()); }
- inline static DigestType hash(const StringView& buffer) { return hash((const u8*)buffer.characters_without_null_termination(), buffer.length()); }
- inline virtual void reset() override
- {
- m_A = MD5Constants::init_A;
- m_B = MD5Constants::init_B;
- m_C = MD5Constants::init_C;
- m_D = MD5Constants::init_D;
-
- m_count[0] = 0;
- m_count[1] = 0;
-
- __builtin_memset(m_data_buffer, 0, sizeof(m_data_buffer));
- }
-
- private:
- inline void transform(const u8*);
-
- static void encode(const u32* from, u8* to, size_t length);
- static void decode(const u8* from, u32* to, size_t length);
-
- u32 m_A { MD5Constants::init_A }, m_B { MD5Constants::init_B }, m_C { MD5Constants::init_C }, m_D { MD5Constants::init_D };
- u32 m_count[2] { 0, 0 };
- ByteBuffer m_buffer;
-
- u8 m_data_buffer[64];
- };
+ inline static DigestType hash(const ByteBuffer& buffer) { return hash(buffer.data(), buffer.size()); }
+ inline static DigestType hash(const StringView& buffer) { return hash((const u8*)buffer.characters_without_null_termination(), buffer.length()); }
+ inline virtual void reset() override
+ {
+ m_A = MD5Constants::init_A;
+ m_B = MD5Constants::init_B;
+ m_C = MD5Constants::init_C;
+ m_D = MD5Constants::init_D;
+
+ m_count[0] = 0;
+ m_count[1] = 0;
+
+ __builtin_memset(m_data_buffer, 0, sizeof(m_data_buffer));
+ }
+
+private:
+ inline void transform(const u8*);
+
+ static void encode(const u32* from, u8* to, size_t length);
+ static void decode(const u8* from, u32* to, size_t length);
+
+ u32 m_A { MD5Constants::init_A }, m_B { MD5Constants::init_B }, m_C { MD5Constants::init_C }, m_D { MD5Constants::init_D };
+ u32 m_count[2] { 0, 0 };
+ ByteBuffer m_buffer;
+
+ u8 m_data_buffer[64];
+};
}
diff --git a/Libraries/LibCrypto/Hash/SHA2.cpp b/Libraries/LibCrypto/Hash/SHA2.cpp
index 6073276b41..a0fb9de78a 100644
--- a/Libraries/LibCrypto/Hash/SHA2.cpp
+++ b/Libraries/LibCrypto/Hash/SHA2.cpp
@@ -29,236 +29,236 @@
namespace Crypto {
namespace Hash {
- constexpr inline static auto ROTRIGHT(u32 a, size_t b) { return (a >> b) | (a << (32 - b)); }
- constexpr inline static auto CH(u32 x, u32 y, u32 z) { return (x & y) ^ (z & ~x); }
- constexpr inline static auto MAJ(u32 x, u32 y, u32 z) { return (x & y) ^ (x & z) ^ (y & z); }
- constexpr inline static auto EP0(u32 x) { return ROTRIGHT(x, 2) ^ ROTRIGHT(x, 13) ^ ROTRIGHT(x, 22); }
- constexpr inline static auto EP1(u32 x) { return ROTRIGHT(x, 6) ^ ROTRIGHT(x, 11) ^ ROTRIGHT(x, 25); }
- constexpr inline static auto SIGN0(u32 x) { return ROTRIGHT(x, 7) ^ ROTRIGHT(x, 18) ^ (x >> 3); }
- constexpr inline static auto SIGN1(u32 x) { return ROTRIGHT(x, 17) ^ ROTRIGHT(x, 19) ^ (x >> 10); }
-
- constexpr inline static auto ROTRIGHT(u64 a, size_t b) { return (a >> b) | (a << (64 - b)); }
- constexpr inline static auto CH(u64 x, u64 y, u64 z) { return (x & y) ^ (z & ~x); }
- constexpr inline static auto MAJ(u64 x, u64 y, u64 z) { return (x & y) ^ (x & z) ^ (y & z); }
- constexpr inline static auto EP0(u64 x) { return ROTRIGHT(x, 28) ^ ROTRIGHT(x, 34) ^ ROTRIGHT(x, 39); }
- constexpr inline static auto EP1(u64 x) { return ROTRIGHT(x, 14) ^ ROTRIGHT(x, 18) ^ ROTRIGHT(x, 41); }
- constexpr inline static auto SIGN0(u64 x) { return ROTRIGHT(x, 1) ^ ROTRIGHT(x, 8) ^ (x >> 7); }
- constexpr inline static auto SIGN1(u64 x) { return ROTRIGHT(x, 19) ^ ROTRIGHT(x, 61) ^ (x >> 6); }
-
- inline void SHA256::transform(const u8* data)
- {
- u32 m[64];
-
- size_t i = 0;
- for (size_t j = 0; i < 16; ++i, j += 4) {
- m[i] = (data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | data[j + 3];
- }
-
- for (; i < BlockSize; ++i) {
- m[i] = SIGN1(m[i - 2]) + m[i - 7] + SIGN0(m[i - 15]) + m[i - 16];
- }
-
- auto a = m_state[0], b = m_state[1],
- c = m_state[2], d = m_state[3],
- e = m_state[4], f = m_state[5],
- g = m_state[6], h = m_state[7];
-
- for (size_t i = 0; i < Rounds; ++i) {
- auto temp0 = h + EP1(e) + CH(e, f, g) + SHA256Constants::RoundConstants[i] + m[i];
- auto temp1 = EP0(a) + MAJ(a, b, c);
- h = g;
- g = f;
- f = e;
- e = d + temp0;
- d = c;
- c = b;
- b = a;
- a = temp0 + temp1;
- }
-
- m_state[0] += a;
- m_state[1] += b;
- m_state[2] += c;
- m_state[3] += d;
- m_state[4] += e;
- m_state[5] += f;
- m_state[6] += g;
- m_state[7] += h;
+constexpr inline static auto ROTRIGHT(u32 a, size_t b) { return (a >> b) | (a << (32 - b)); }
+constexpr inline static auto CH(u32 x, u32 y, u32 z) { return (x & y) ^ (z & ~x); }
+constexpr inline static auto MAJ(u32 x, u32 y, u32 z) { return (x & y) ^ (x & z) ^ (y & z); }
+constexpr inline static auto EP0(u32 x) { return ROTRIGHT(x, 2) ^ ROTRIGHT(x, 13) ^ ROTRIGHT(x, 22); }
+constexpr inline static auto EP1(u32 x) { return ROTRIGHT(x, 6) ^ ROTRIGHT(x, 11) ^ ROTRIGHT(x, 25); }
+constexpr inline static auto SIGN0(u32 x) { return ROTRIGHT(x, 7) ^ ROTRIGHT(x, 18) ^ (x >> 3); }
+constexpr inline static auto SIGN1(u32 x) { return ROTRIGHT(x, 17) ^ ROTRIGHT(x, 19) ^ (x >> 10); }
+
+constexpr inline static auto ROTRIGHT(u64 a, size_t b) { return (a >> b) | (a << (64 - b)); }
+constexpr inline static auto CH(u64 x, u64 y, u64 z) { return (x & y) ^ (z & ~x); }
+constexpr inline static auto MAJ(u64 x, u64 y, u64 z) { return (x & y) ^ (x & z) ^ (y & z); }
+constexpr inline static auto EP0(u64 x) { return ROTRIGHT(x, 28) ^ ROTRIGHT(x, 34) ^ ROTRIGHT(x, 39); }
+constexpr inline static auto EP1(u64 x) { return ROTRIGHT(x, 14) ^ ROTRIGHT(x, 18) ^ ROTRIGHT(x, 41); }
+constexpr inline static auto SIGN0(u64 x) { return ROTRIGHT(x, 1) ^ ROTRIGHT(x, 8) ^ (x >> 7); }
+constexpr inline static auto SIGN1(u64 x) { return ROTRIGHT(x, 19) ^ ROTRIGHT(x, 61) ^ (x >> 6); }
+
+inline void SHA256::transform(const u8* data)
+{
+ u32 m[64];
+
+ size_t i = 0;
+ for (size_t j = 0; i < 16; ++i, j += 4) {
+ m[i] = (data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | data[j + 3];
}
- void SHA256::update(const u8* message, size_t length)
- {
- for (size_t i = 0; i < length; ++i) {
- if (m_data_length == BlockSize) {
- transform(m_data_buffer);
- m_bit_length += 512;
- m_data_length = 0;
- }
- m_data_buffer[m_data_length++] = message[i];
- }
+ for (; i < BlockSize; ++i) {
+ m[i] = SIGN1(m[i - 2]) + m[i - 7] + SIGN0(m[i - 15]) + m[i - 16];
}
- SHA256::DigestType SHA256::digest()
- {
- auto digest = peek();
- reset();
- return digest;
+ auto a = m_state[0], b = m_state[1],
+ c = m_state[2], d = m_state[3],
+ e = m_state[4], f = m_state[5],
+ g = m_state[6], h = m_state[7];
+
+ for (size_t i = 0; i < Rounds; ++i) {
+ auto temp0 = h + EP1(e) + CH(e, f, g) + SHA256Constants::RoundConstants[i] + m[i];
+ auto temp1 = EP0(a) + MAJ(a, b, c);
+ h = g;
+ g = f;
+ f = e;
+ e = d + temp0;
+ d = c;
+ c = b;
+ b = a;
+ a = temp0 + temp1;
}
- SHA256::DigestType SHA256::peek()
- {
- DigestType digest;
- size_t i = m_data_length;
-
- if (m_data_length < FinalBlockDataSize) {
- m_data_buffer[i++] = 0x80;
- while (i < FinalBlockDataSize)
- m_data_buffer[i++] = 0x00;
-
- } else {
- m_data_buffer[i++] = 0x80;
- while (i < BlockSize)
- m_data_buffer[i++] = 0x00;
+ m_state[0] += a;
+ m_state[1] += b;
+ m_state[2] += c;
+ m_state[3] += d;
+ m_state[4] += e;
+ m_state[5] += f;
+ m_state[6] += g;
+ m_state[7] += h;
+}
+void SHA256::update(const u8* message, size_t length)
+{
+ for (size_t i = 0; i < length; ++i) {
+ if (m_data_length == BlockSize) {
transform(m_data_buffer);
- __builtin_memset(m_data_buffer, 0, FinalBlockDataSize);
+ m_bit_length += 512;
+ m_data_length = 0;
}
-
- // append total message length
- m_bit_length += m_data_length * 8;
- m_data_buffer[BlockSize - 1] = m_bit_length;
- m_data_buffer[BlockSize - 2] = m_bit_length >> 8;
- m_data_buffer[BlockSize - 3] = m_bit_length >> 16;
- m_data_buffer[BlockSize - 4] = m_bit_length >> 24;
- m_data_buffer[BlockSize - 5] = m_bit_length >> 32;
- 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;
-
- transform(m_data_buffer);
-
- // SHA uses big-endian and we assume little-endian
- // FIXME: looks like a thing for AK::NetworkOrdered,
- // but he doesn't support shifting operations
- for (size_t i = 0; i < 4; ++i) {
- digest.data[i + 0] = (m_state[0] >> (24 - i * 8)) & 0x000000ff;
- digest.data[i + 4] = (m_state[1] >> (24 - i * 8)) & 0x000000ff;
- digest.data[i + 8] = (m_state[2] >> (24 - i * 8)) & 0x000000ff;
- digest.data[i + 12] = (m_state[3] >> (24 - i * 8)) & 0x000000ff;
- digest.data[i + 16] = (m_state[4] >> (24 - i * 8)) & 0x000000ff;
- digest.data[i + 20] = (m_state[5] >> (24 - i * 8)) & 0x000000ff;
- digest.data[i + 24] = (m_state[6] >> (24 - i * 8)) & 0x000000ff;
- digest.data[i + 28] = (m_state[7] >> (24 - i * 8)) & 0x000000ff;
- }
- return digest;
+ m_data_buffer[m_data_length++] = message[i];
}
+}
- inline void SHA512::transform(const u8* data)
- {
- u64 m[80];
+SHA256::DigestType SHA256::digest()
+{
+ auto digest = peek();
+ reset();
+ return digest;
+}
- size_t i = 0;
- for (size_t j = 0; i < 16; ++i, j += 8) {
- m[i] = ((u64)data[j] << 56) | ((u64)data[j + 1] << 48) | ((u64)data[j + 2] << 40) | ((u64)data[j + 3] << 32) | ((u64)data[j + 4] << 24) | ((u64)data[j + 5] << 16) | ((u64)data[j + 6] << 8) | (u64)data[j + 7];
- }
+SHA256::DigestType SHA256::peek()
+{
+ DigestType digest;
+ size_t i = m_data_length;
- for (; i < Rounds; ++i) {
- m[i] = SIGN1(m[i - 2]) + m[i - 7] + SIGN0(m[i - 15]) + m[i - 16];
- }
+ if (m_data_length < FinalBlockDataSize) {
+ m_data_buffer[i++] = 0x80;
+ while (i < FinalBlockDataSize)
+ m_data_buffer[i++] = 0x00;
- auto a = m_state[0], b = m_state[1],
- c = m_state[2], d = m_state[3],
- e = m_state[4], f = m_state[5],
- g = m_state[6], h = m_state[7];
-
- for (size_t i = 0; i < Rounds; ++i) {
- auto temp0 = h + EP1(e) + CH(e, f, g) + SHA512Constants::RoundConstants[i] + m[i];
- auto temp1 = EP0(a) + MAJ(a, b, c);
- h = g;
- g = f;
- f = e;
- e = d + temp0;
- d = c;
- c = b;
- b = a;
- a = temp0 + temp1;
- }
+ } else {
+ m_data_buffer[i++] = 0x80;
+ while (i < BlockSize)
+ m_data_buffer[i++] = 0x00;
- m_state[0] += a;
- m_state[1] += b;
- m_state[2] += c;
- m_state[3] += d;
- m_state[4] += e;
- m_state[5] += f;
- m_state[6] += g;
- m_state[7] += h;
+ transform(m_data_buffer);
+ __builtin_memset(m_data_buffer, 0, FinalBlockDataSize);
}
- void SHA512::update(const u8* message, size_t length)
- {
- for (size_t i = 0; i < length; ++i) {
- if (m_data_length == BlockSize) {
- transform(m_data_buffer);
- m_bit_length += 1024;
- m_data_length = 0;
- }
- m_data_buffer[m_data_length++] = message[i];
- }
+ // append total message length
+ m_bit_length += m_data_length * 8;
+ m_data_buffer[BlockSize - 1] = m_bit_length;
+ m_data_buffer[BlockSize - 2] = m_bit_length >> 8;
+ m_data_buffer[BlockSize - 3] = m_bit_length >> 16;
+ m_data_buffer[BlockSize - 4] = m_bit_length >> 24;
+ m_data_buffer[BlockSize - 5] = m_bit_length >> 32;
+ 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;
+
+ transform(m_data_buffer);
+
+ // SHA uses big-endian and we assume little-endian
+ // FIXME: looks like a thing for AK::NetworkOrdered,
+ // but he doesn't support shifting operations
+ for (size_t i = 0; i < 4; ++i) {
+ digest.data[i + 0] = (m_state[0] >> (24 - i * 8)) & 0x000000ff;
+ digest.data[i + 4] = (m_state[1] >> (24 - i * 8)) & 0x000000ff;
+ digest.data[i + 8] = (m_state[2] >> (24 - i * 8)) & 0x000000ff;
+ digest.data[i + 12] = (m_state[3] >> (24 - i * 8)) & 0x000000ff;
+ digest.data[i + 16] = (m_state[4] >> (24 - i * 8)) & 0x000000ff;
+ digest.data[i + 20] = (m_state[5] >> (24 - i * 8)) & 0x000000ff;
+ digest.data[i + 24] = (m_state[6] >> (24 - i * 8)) & 0x000000ff;
+ digest.data[i + 28] = (m_state[7] >> (24 - i * 8)) & 0x000000ff;
}
+ return digest;
+}
- SHA512::DigestType SHA512::digest()
- {
- auto digest = peek();
- reset();
- return digest;
+inline void SHA512::transform(const u8* data)
+{
+ u64 m[80];
+
+ size_t i = 0;
+ for (size_t j = 0; i < 16; ++i, j += 8) {
+ m[i] = ((u64)data[j] << 56) | ((u64)data[j + 1] << 48) | ((u64)data[j + 2] << 40) | ((u64)data[j + 3] << 32) | ((u64)data[j + 4] << 24) | ((u64)data[j + 5] << 16) | ((u64)data[j + 6] << 8) | (u64)data[j + 7];
}
- SHA512::DigestType SHA512::peek()
- {
- DigestType digest;
- size_t i = m_data_length;
+ for (; i < Rounds; ++i) {
+ m[i] = SIGN1(m[i - 2]) + m[i - 7] + SIGN0(m[i - 15]) + m[i - 16];
+ }
- if (m_data_length < FinalBlockDataSize) {
- m_data_buffer[i++] = 0x80;
- while (i < FinalBlockDataSize)
- m_data_buffer[i++] = 0x00;
+ auto a = m_state[0], b = m_state[1],
+ c = m_state[2], d = m_state[3],
+ e = m_state[4], f = m_state[5],
+ g = m_state[6], h = m_state[7];
+
+ for (size_t i = 0; i < Rounds; ++i) {
+ auto temp0 = h + EP1(e) + CH(e, f, g) + SHA512Constants::RoundConstants[i] + m[i];
+ auto temp1 = EP0(a) + MAJ(a, b, c);
+ h = g;
+ g = f;
+ f = e;
+ e = d + temp0;
+ d = c;
+ c = b;
+ b = a;
+ a = temp0 + temp1;
+ }
- } else {
- m_data_buffer[i++] = 0x80;
- while (i < BlockSize)
- m_data_buffer[i++] = 0x00;
+ m_state[0] += a;
+ m_state[1] += b;
+ m_state[2] += c;
+ m_state[3] += d;
+ m_state[4] += e;
+ m_state[5] += f;
+ m_state[6] += g;
+ m_state[7] += h;
+}
+void SHA512::update(const u8* message, size_t length)
+{
+ for (size_t i = 0; i < length; ++i) {
+ if (m_data_length == BlockSize) {
transform(m_data_buffer);
- __builtin_memset(m_data_buffer, 0, FinalBlockDataSize);
+ m_bit_length += 1024;
+ m_data_length = 0;
}
+ m_data_buffer[m_data_length++] = message[i];
+ }
+}
+
+SHA512::DigestType SHA512::digest()
+{
+ auto digest = peek();
+ reset();
+ return digest;
+}
+
+SHA512::DigestType SHA512::peek()
+{
+ DigestType digest;
+ size_t i = m_data_length;
- // append total message length
- m_bit_length += m_data_length * 8;
- m_data_buffer[BlockSize - 1] = m_bit_length;
- m_data_buffer[BlockSize - 2] = m_bit_length >> 8;
- m_data_buffer[BlockSize - 3] = m_bit_length >> 16;
- m_data_buffer[BlockSize - 4] = m_bit_length >> 24;
- m_data_buffer[BlockSize - 5] = m_bit_length >> 32;
- 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;
+ if (m_data_length < FinalBlockDataSize) {
+ m_data_buffer[i++] = 0x80;
+ while (i < FinalBlockDataSize)
+ m_data_buffer[i++] = 0x00;
+
+ } else {
+ m_data_buffer[i++] = 0x80;
+ while (i < BlockSize)
+ m_data_buffer[i++] = 0x00;
transform(m_data_buffer);
+ __builtin_memset(m_data_buffer, 0, FinalBlockDataSize);
+ }
- // SHA uses big-endian and we assume little-endian
- // FIXME: looks like a thing for AK::NetworkOrdered,
- // but he doesn't support shifting operations
- for (size_t i = 0; i < 8; ++i) {
- digest.data[i + 0] = (m_state[0] >> (56 - i * 8)) & 0x000000ff;
- digest.data[i + 8] = (m_state[1] >> (56 - i * 8)) & 0x000000ff;
- digest.data[i + 16] = (m_state[2] >> (56 - i * 8)) & 0x000000ff;
- digest.data[i + 24] = (m_state[3] >> (56 - i * 8)) & 0x000000ff;
- digest.data[i + 32] = (m_state[4] >> (56 - i * 8)) & 0x000000ff;
- digest.data[i + 40] = (m_state[5] >> (56 - i * 8)) & 0x000000ff;
- digest.data[i + 48] = (m_state[6] >> (56 - i * 8)) & 0x000000ff;
- digest.data[i + 56] = (m_state[7] >> (56 - i * 8)) & 0x000000ff;
- }
- return digest;
+ // append total message length
+ m_bit_length += m_data_length * 8;
+ m_data_buffer[BlockSize - 1] = m_bit_length;
+ m_data_buffer[BlockSize - 2] = m_bit_length >> 8;
+ m_data_buffer[BlockSize - 3] = m_bit_length >> 16;
+ m_data_buffer[BlockSize - 4] = m_bit_length >> 24;
+ m_data_buffer[BlockSize - 5] = m_bit_length >> 32;
+ 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;
+
+ transform(m_data_buffer);
+
+ // SHA uses big-endian and we assume little-endian
+ // FIXME: looks like a thing for AK::NetworkOrdered,
+ // but he doesn't support shifting operations
+ for (size_t i = 0; i < 8; ++i) {
+ digest.data[i + 0] = (m_state[0] >> (56 - i * 8)) & 0x000000ff;
+ digest.data[i + 8] = (m_state[1] >> (56 - i * 8)) & 0x000000ff;
+ digest.data[i + 16] = (m_state[2] >> (56 - i * 8)) & 0x000000ff;
+ digest.data[i + 24] = (m_state[3] >> (56 - i * 8)) & 0x000000ff;
+ digest.data[i + 32] = (m_state[4] >> (56 - i * 8)) & 0x000000ff;
+ digest.data[i + 40] = (m_state[5] >> (56 - i * 8)) & 0x000000ff;
+ digest.data[i + 48] = (m_state[6] >> (56 - i * 8)) & 0x000000ff;
+ digest.data[i + 56] = (m_state[7] >> (56 - i * 8)) & 0x000000ff;
}
+ return digest;
+}
}
}
diff --git a/Libraries/LibCrypto/Hash/SHA2.h b/Libraries/LibCrypto/Hash/SHA2.h
index bedc3a7941..2437cc17cc 100644
--- a/Libraries/LibCrypto/Hash/SHA2.h
+++ b/Libraries/LibCrypto/Hash/SHA2.h
@@ -33,169 +33,169 @@
namespace Crypto {
namespace Hash {
- namespace SHA256Constants {
- constexpr static u32 RoundConstants[64] {
- 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
- 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
- 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
- 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
- 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
- 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
- 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
- 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
- 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
- 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
- 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
- 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
- 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
- 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
- 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
- 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
- };
-
- constexpr static u32 InitializationHashes[8] = {
- 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
- 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
- };
+namespace SHA256Constants {
+constexpr static u32 RoundConstants[64] {
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
+ 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
+ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
+ 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
+ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
+ 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
+ 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
+ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
+ 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
+ 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
+ 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
+ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
+ 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
+};
+
+constexpr static u32 InitializationHashes[8] = {
+ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
+ 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
+};
+}
+
+namespace SHA512Constants {
+constexpr static u64 RoundConstants[80] {
+ 0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc, 0x3956c25bf348b538,
+ 0x59f111f1b605d019, 0x923f82a4af194f9b, 0xab1c5ed5da6d8118, 0xd807aa98a3030242, 0x12835b0145706fbe,
+ 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2, 0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235,
+ 0xc19bf174cf692694, 0xe49b69c19ef14ad2, 0xefbe4786384f25e3, 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65,
+ 0x2de92c6f592b0275, 0x4a7484aa6ea6e483, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5, 0x983e5152ee66dfab,
+ 0xa831c66d2db43210, 0xb00327c898fb213f, 0xbf597fc7beef0ee4, 0xc6e00bf33da88fc2, 0xd5a79147930aa725,
+ 0x06ca6351e003826f, 0x142929670a0e6e70, 0x27b70a8546d22ffc, 0x2e1b21385c26c926, 0x4d2c6dfc5ac42aed,
+ 0x53380d139d95b3df, 0x650a73548baf63de, 0x766a0abb3c77b2a8, 0x81c2c92e47edaee6, 0x92722c851482353b,
+ 0xa2bfe8a14cf10364, 0xa81a664bbc423001, 0xc24b8b70d0f89791, 0xc76c51a30654be30, 0xd192e819d6ef5218,
+ 0xd69906245565a910, 0xf40e35855771202a, 0x106aa07032bbd1b8, 0x19a4c116b8d2d0c8, 0x1e376c085141ab53,
+ 0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8, 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb, 0x5b9cca4f7763e373,
+ 0x682e6ff3d6b2b8a3, 0x748f82ee5defb2fc, 0x78a5636f43172f60, 0x84c87814a1f0ab72, 0x8cc702081a6439ec,
+ 0x90befffa23631e28, 0xa4506cebde82bde9, 0xbef9a3f7b2c67915, 0xc67178f2e372532b, 0xca273eceea26619c,
+ 0xd186b8c721c0c207, 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178, 0x06f067aa72176fba, 0x0a637dc5a2c898a6,
+ 0x113f9804bef90dae, 0x1b710b35131c471b, 0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc,
+ 0x431d67c49c100d4c, 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817
+};
+
+constexpr static u64 InitializationHashes[8] = {
+ 0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1,
+ 0x510e527fade682d1, 0x9b05688c2b3e6c1f, 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179
+};
+}
+
+template<size_t Bytes>
+struct SHA2Digest {
+ u8 data[Bytes];
+};
+
+// FIXME: I want template<size_t BlockSize> but the compiler gets confused
+class SHA256 final : public HashFunction<512, SHA2Digest<256 / 8>> {
+public:
+ SHA256()
+ {
+ reset();
}
- namespace SHA512Constants {
- constexpr static u64 RoundConstants[80] {
- 0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc, 0x3956c25bf348b538,
- 0x59f111f1b605d019, 0x923f82a4af194f9b, 0xab1c5ed5da6d8118, 0xd807aa98a3030242, 0x12835b0145706fbe,
- 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2, 0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235,
- 0xc19bf174cf692694, 0xe49b69c19ef14ad2, 0xefbe4786384f25e3, 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65,
- 0x2de92c6f592b0275, 0x4a7484aa6ea6e483, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5, 0x983e5152ee66dfab,
- 0xa831c66d2db43210, 0xb00327c898fb213f, 0xbf597fc7beef0ee4, 0xc6e00bf33da88fc2, 0xd5a79147930aa725,
- 0x06ca6351e003826f, 0x142929670a0e6e70, 0x27b70a8546d22ffc, 0x2e1b21385c26c926, 0x4d2c6dfc5ac42aed,
- 0x53380d139d95b3df, 0x650a73548baf63de, 0x766a0abb3c77b2a8, 0x81c2c92e47edaee6, 0x92722c851482353b,
- 0xa2bfe8a14cf10364, 0xa81a664bbc423001, 0xc24b8b70d0f89791, 0xc76c51a30654be30, 0xd192e819d6ef5218,
- 0xd69906245565a910, 0xf40e35855771202a, 0x106aa07032bbd1b8, 0x19a4c116b8d2d0c8, 0x1e376c085141ab53,
- 0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8, 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb, 0x5b9cca4f7763e373,
- 0x682e6ff3d6b2b8a3, 0x748f82ee5defb2fc, 0x78a5636f43172f60, 0x84c87814a1f0ab72, 0x8cc702081a6439ec,
- 0x90befffa23631e28, 0xa4506cebde82bde9, 0xbef9a3f7b2c67915, 0xc67178f2e372532b, 0xca273eceea26619c,
- 0xd186b8c721c0c207, 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178, 0x06f067aa72176fba, 0x0a637dc5a2c898a6,
- 0x113f9804bef90dae, 0x1b710b35131c471b, 0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc,
- 0x431d67c49c100d4c, 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817
- };
-
- constexpr static u64 InitializationHashes[8] = {
- 0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1,
- 0x510e527fade682d1, 0x9b05688c2b3e6c1f, 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179
- };
+ virtual void update(const u8*, size_t) override;
+
+ virtual void update(const ByteBuffer& buffer) override { update(buffer.data(), buffer.size()); };
+ virtual void update(const StringView& string) override { update((const u8*)string.characters_without_null_termination(), string.length()); };
+
+ virtual DigestType digest() override;
+ virtual DigestType peek() override;
+
+ inline static DigestType hash(const u8* data, size_t length)
+ {
+ SHA256 sha;
+ sha.update(data, length);
+ return sha.digest();
}
- template <size_t Bytes>
- struct SHA2Digest {
- u8 data[Bytes];
- };
+ inline static DigestType hash(const ByteBuffer& buffer) { return hash(buffer.data(), buffer.size()); }
+ inline static DigestType hash(const StringView& buffer) { return hash((const u8*)buffer.characters_without_null_termination(), buffer.length()); }
- // FIXME: I want template<size_t BlockSize> but the compiler gets confused
- class SHA256 final : public HashFunction<512, SHA2Digest<256 / 8>> {
- public:
- SHA256()
- {
- reset();
- }
-
- virtual void update(const u8*, size_t) override;
-
- virtual void update(const ByteBuffer& buffer) override { update(buffer.data(), buffer.size()); };
- virtual void update(const StringView& string) override { update((const u8*)string.characters_without_null_termination(), string.length()); };
-
- virtual DigestType digest() override;
- virtual DigestType peek() override;
-
- inline static DigestType hash(const u8* data, size_t length)
- {
- SHA256 sha;
- sha.update(data, length);
- return sha.digest();
- }
-
- inline static DigestType hash(const ByteBuffer& buffer) { return hash(buffer.data(), buffer.size()); }
- inline static DigestType hash(const StringView& buffer) { return hash((const u8*)buffer.characters_without_null_termination(), buffer.length()); }
-
- virtual String class_name() const override
- {
- StringBuilder builder;
- builder.append("SHA");
- builder.appendf("%zu", this->DigestSize * 8);
- return builder.build();
- };
- inline virtual void reset() override
- {
- m_data_length = 0;
- m_bit_length = 0;
- for (size_t i = 0; i < 8; ++i)
- m_state[i] = SHA256Constants::InitializationHashes[i];
- }
-
- private:
- inline void transform(const u8*);
-
- u8 m_data_buffer[BlockSize];
- size_t m_data_length { 0 };
-
- u64 m_bit_length { 0 };
- u32 m_state[8];
-
- constexpr static auto FinalBlockDataSize = BlockSize - 8;
- constexpr static auto Rounds = 64;
+ virtual String class_name() const override
+ {
+ StringBuilder builder;
+ builder.append("SHA");
+ builder.appendf("%zu", this->DigestSize * 8);
+ return builder.build();
};
+ inline virtual void reset() override
+ {
+ m_data_length = 0;
+ m_bit_length = 0;
+ for (size_t i = 0; i < 8; ++i)
+ m_state[i] = SHA256Constants::InitializationHashes[i];
+ }
+
+private:
+ inline void transform(const u8*);
+
+ u8 m_data_buffer[BlockSize];
+ size_t m_data_length { 0 };
+
+ u64 m_bit_length { 0 };
+ u32 m_state[8];
+
+ constexpr static auto FinalBlockDataSize = BlockSize - 8;
+ constexpr static auto Rounds = 64;
+};
- class SHA512 final : public HashFunction<1024, SHA2Digest<512 / 8>> {
- public:
- SHA512()
- {
- reset();
- }
-
- virtual void update(const u8*, size_t) override;
-
- virtual void update(const ByteBuffer& buffer) override { update(buffer.data(), buffer.size()); };
- virtual void update(const StringView& string) override { update((const u8*)string.characters_without_null_termination(), string.length()); };
-
- virtual DigestType digest() override;
- virtual DigestType peek() override;
-
- inline static DigestType hash(const u8* data, size_t length)
- {
- SHA512 sha;
- sha.update(data, length);
- return sha.digest();
- }
-
- inline static DigestType hash(const ByteBuffer& buffer) { return hash(buffer.data(), buffer.size()); }
- inline static DigestType hash(const StringView& buffer) { return hash((const u8*)buffer.characters_without_null_termination(), buffer.length()); }
-
- virtual String class_name() const override
- {
- StringBuilder builder;
- builder.append("SHA");
- builder.appendf("%zu", this->DigestSize * 8);
- return builder.build();
- };
- inline virtual void reset() override
- {
- m_data_length = 0;
- m_bit_length = 0;
- for (size_t i = 0; i < 8; ++i)
- m_state[i] = SHA512Constants::InitializationHashes[i];
- }
-
- private:
- inline void transform(const u8*);
-
- u8 m_data_buffer[BlockSize];
- size_t m_data_length { 0 };
-
- u64 m_bit_length { 0 };
- u64 m_state[8];
-
- constexpr static auto FinalBlockDataSize = BlockSize - 8;
- constexpr static auto Rounds = 80;
+class SHA512 final : public HashFunction<1024, SHA2Digest<512 / 8>> {
+public:
+ SHA512()
+ {
+ reset();
+ }
+
+ virtual void update(const u8*, size_t) override;
+
+ virtual void update(const ByteBuffer& buffer) override { update(buffer.data(), buffer.size()); };
+ virtual void update(const StringView& string) override { update((const u8*)string.characters_without_null_termination(), string.length()); };
+
+ virtual DigestType digest() override;
+ virtual DigestType peek() override;
+
+ inline static DigestType hash(const u8* data, size_t length)
+ {
+ SHA512 sha;
+ sha.update(data, length);
+ return sha.digest();
+ }
+
+ inline static DigestType hash(const ByteBuffer& buffer) { return hash(buffer.data(), buffer.size()); }
+ inline static DigestType hash(const StringView& buffer) { return hash((const u8*)buffer.characters_without_null_termination(), buffer.length()); }
+
+ virtual String class_name() const override
+ {
+ StringBuilder builder;
+ builder.append("SHA");
+ builder.appendf("%zu", this->DigestSize * 8);
+ return builder.build();
};
+ inline virtual void reset() override
+ {
+ m_data_length = 0;
+ m_bit_length = 0;
+ for (size_t i = 0; i < 8; ++i)
+ m_state[i] = SHA512Constants::InitializationHashes[i];
+ }
+
+private:
+ inline void transform(const u8*);
+
+ u8 m_data_buffer[BlockSize];
+ size_t m_data_length { 0 };
+
+ u64 m_bit_length { 0 };
+ u64 m_state[8];
+
+ constexpr static auto FinalBlockDataSize = BlockSize - 8;
+ constexpr static auto Rounds = 80;
+};
}
}