Skip to content

Commit 0786486

Browse files
tete17trflynn89
authored andcommitted
LibCrypto: SHA3 hashing algorithm
1 parent 7b0dfa6 commit 0786486

File tree

5 files changed

+194
-1
lines changed

5 files changed

+194
-1
lines changed

Libraries/LibCrypto/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ set(SOURCES
1919
Hash/PBKDF2.cpp
2020
Hash/SHA1.cpp
2121
Hash/SHA2.cpp
22+
Hash/SHA3.cpp
2223
PK/RSA.cpp
2324
PK/EC.cpp
2425
SecureRandom.cpp

Libraries/LibCrypto/Hash/HashManager.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <LibCrypto/Hash/MD5.h>
1515
#include <LibCrypto/Hash/SHA1.h>
1616
#include <LibCrypto/Hash/SHA2.h>
17+
#include <LibCrypto/Hash/SHA3.h>
1718

1819
namespace Crypto::Hash {
1920

@@ -26,6 +27,9 @@ enum class HashKind {
2627
SHA256,
2728
SHA384,
2829
SHA512,
30+
SHA3_256,
31+
SHA3_384,
32+
SHA3_512
2933
};
3034

3135
struct MultiHashDigestVariant {
@@ -157,6 +161,15 @@ class Manager final : public HashFunction<0, 0, MultiHashDigestVariant> {
157161
case HashKind::SHA512:
158162
m_algorithm = SHA512::create();
159163
break;
164+
case HashKind::SHA3_256:
165+
m_algorithm = SHA3_256::create();
166+
break;
167+
case HashKind::SHA3_384:
168+
m_algorithm = SHA3_384::create();
169+
break;
170+
case HashKind::SHA3_512:
171+
m_algorithm = SHA3_512::create();
172+
break;
160173
default:
161174
case HashKind::None:
162175
m_algorithm = Empty {};
@@ -232,7 +245,16 @@ class Manager final : public HashFunction<0, 0, MultiHashDigestVariant> {
232245
}
233246

234247
private:
235-
using AlgorithmVariant = Variant<Empty, NonnullOwnPtr<BLAKE2b>, NonnullOwnPtr<MD5>, NonnullOwnPtr<SHA1>, NonnullOwnPtr<SHA256>, NonnullOwnPtr<SHA384>, NonnullOwnPtr<SHA512>>;
248+
using AlgorithmVariant = Variant<Empty,
249+
NonnullOwnPtr<BLAKE2b>,
250+
NonnullOwnPtr<MD5>,
251+
NonnullOwnPtr<SHA1>,
252+
NonnullOwnPtr<SHA256>,
253+
NonnullOwnPtr<SHA384>,
254+
NonnullOwnPtr<SHA512>,
255+
NonnullOwnPtr<SHA3_256>,
256+
NonnullOwnPtr<SHA3_384>,
257+
NonnullOwnPtr<SHA3_512>>;
236258
AlgorithmVariant m_algorithm {};
237259
HashKind m_kind { HashKind::None };
238260
ByteBuffer m_pre_init_buffer;

Libraries/LibCrypto/Hash/SHA3.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2025, Miguel Sacristán Izcue <miguel_tete17@hotmail.com>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#include <LibCrypto/Hash/SHA3.h>
8+
9+
#include <openssl/evp.h>
10+
11+
namespace Crypto::Hash {
12+
13+
SHA3_256::SHA3_256(EVP_MD_CTX* context)
14+
: OpenSSLHashFunction(EVP_sha3_256(), context)
15+
{
16+
}
17+
18+
SHA3_384::SHA3_384(EVP_MD_CTX* context)
19+
: OpenSSLHashFunction(EVP_sha3_384(), context)
20+
{
21+
}
22+
23+
SHA3_512::SHA3_512(EVP_MD_CTX* context)
24+
: OpenSSLHashFunction(EVP_sha3_512(), context)
25+
{
26+
}
27+
28+
}

Libraries/LibCrypto/Hash/SHA3.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2025, Miguel Sacristán Izcue <miguel_tete17@hotmail.com>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#pragma once
8+
9+
#include <AK/Noncopyable.h>
10+
#include <LibCrypto/Hash/OpenSSLHashFunction.h>
11+
12+
namespace Crypto::Hash {
13+
14+
class SHA3_256 final : public OpenSSLHashFunction<SHA3_256, 1088, 256> {
15+
AK_MAKE_NONCOPYABLE(SHA3_256);
16+
17+
public:
18+
explicit SHA3_256(EVP_MD_CTX* context);
19+
20+
virtual ByteString class_name() const override
21+
{
22+
return "SHA3-256";
23+
}
24+
};
25+
26+
class SHA3_384 final : public OpenSSLHashFunction<SHA3_384, 832, 384> {
27+
AK_MAKE_NONCOPYABLE(SHA3_384);
28+
29+
public:
30+
explicit SHA3_384(EVP_MD_CTX* context);
31+
32+
virtual ByteString class_name() const override
33+
{
34+
return "SHA3-384";
35+
}
36+
};
37+
38+
class SHA3_512 final : public OpenSSLHashFunction<SHA3_512, 576, 512> {
39+
AK_MAKE_NONCOPYABLE(SHA3_512);
40+
41+
public:
42+
explicit SHA3_512(EVP_MD_CTX* context);
43+
44+
virtual ByteString class_name() const override
45+
{
46+
return "SHA3-512";
47+
}
48+
};
49+
50+
}

Tests/LibCrypto/TestHash.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,95 @@ TEST_CASE(test_SHA512_hash_empty_string)
298298
auto digest = Crypto::Hash::SHA512::hash(""sv);
299299
EXPECT(memcmp(result, digest.data, Crypto::Hash::SHA512::digest_size()) == 0);
300300
}
301+
302+
TEST_CASE(test_SHA3_256_name)
303+
{
304+
auto sha = Crypto::Hash::SHA3_256::create();
305+
EXPECT_EQ(sha->class_name(), "SHA3-256"sv);
306+
}
307+
308+
TEST_CASE(test_SHA3_256_hash_string)
309+
{
310+
u8 result[] {
311+
0x25, 0x5c, 0x1e, 0x6c, 0xf3, 0x10, 0x48, 0x9e, 0xca, 0x35, 0xbf, 0xab, 0x82, 0x1d, 0x38, 0x2d, 0xc0, 0xc8, 0x95, 0x16, 0x49, 0x85, 0x54, 0xc6, 0xf1, 0xa5, 0xf5, 0x26, 0x1a, 0xc2, 0x06, 0x9e
312+
};
313+
auto digest = Crypto::Hash::SHA3_256::hash("Well hello friends"sv);
314+
EXPECT(memcmp(result, digest.data, Crypto::Hash::SHA3_256::digest_size()) == 0);
315+
}
316+
317+
TEST_CASE(test_SHA3_256_hash_empty_string)
318+
{
319+
u8 result[] {
320+
0xa7, 0xff, 0xc6, 0xf8, 0xbf, 0x1e, 0xd7, 0x66, 0x51, 0xc1, 0x47, 0x56, 0xa0, 0x61, 0xd6, 0x62, 0xf5, 0x80, 0xff, 0x4d, 0xe4, 0x3b, 0x49, 0xfa, 0x82, 0xd8, 0x0a, 0x4b, 0x80, 0xf8, 0x43, 0x4a
321+
};
322+
auto digest = Crypto::Hash::SHA3_256::hash(""sv);
323+
EXPECT(memcmp(result, digest.data, Crypto::Hash::SHA3_256::digest_size()) == 0);
324+
}
325+
326+
TEST_CASE(test_SHA3_256_hash_split_into_blocks)
327+
{
328+
u8 result[] {
329+
0x0c, 0x4e, 0x02, 0xfb, 0x73, 0x31, 0xe9, 0xe7, 0x25, 0x8c, 0xcf, 0xc6, 0x25, 0x03, 0xe9, 0x2c, 0xa5, 0x7f, 0xaa, 0x91, 0x4a, 0x21, 0xde, 0x00, 0x03, 0x72, 0x9a, 0xa0, 0xaa, 0x0d, 0x9e, 0x76
330+
};
331+
auto digest = Crypto::Hash::SHA3_256::hash("0123456789012345678901234567890123456789012345678901234567890123abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcd"sv);
332+
EXPECT(memcmp(result, digest.data, Crypto::Hash::SHA3_256::digest_size()) == 0);
333+
}
334+
335+
TEST_CASE(test_SHA3_384_name)
336+
{
337+
auto sha = Crypto::Hash::SHA3_384::create();
338+
EXPECT_EQ(sha->class_name(), "SHA3-384"sv);
339+
}
340+
341+
TEST_CASE(test_SHA3_384_hash_string)
342+
{
343+
u8 result[] {
344+
0x4e, 0x86, 0xc0, 0x05, 0x5f, 0x8d, 0x11, 0x51, 0xc8, 0xcd, 0x71, 0xd6, 0xe8, 0x61, 0xc0, 0x86, 0x14, 0x6d, 0xdc, 0xa8, 0x79, 0xe0, 0x2f, 0x07, 0x69, 0x8e, 0xde, 0xfd, 0xcf, 0x36, 0x4f, 0x9b, 0xfa, 0x86, 0xb0, 0x78, 0xee, 0xbe, 0xca, 0xa4, 0xff, 0x20, 0xc9, 0x51, 0x32, 0x31, 0xd2, 0xe5
345+
};
346+
auto digest = Crypto::Hash::SHA3_384::hash("Well hello friends"sv);
347+
EXPECT(memcmp(result, digest.data, Crypto::Hash::SHA3_384::digest_size()) == 0);
348+
}
349+
350+
TEST_CASE(test_SHA3_384_hash_bug)
351+
{
352+
u8 result[] {
353+
0x79, 0x40, 0x7d, 0x3b, 0x59, 0x16, 0xb5, 0x9c, 0x3e, 0x30, 0xb0, 0x98, 0x22, 0x97, 0x47, 0x91, 0xc3, 0x13, 0xfb, 0x9e, 0xcc, 0x84, 0x9e, 0x40, 0x6f, 0x23, 0x59, 0x2d, 0x04, 0xf6, 0x25, 0xdc, 0x8c, 0x70, 0x9b, 0x98, 0xb4, 0x3b, 0x38, 0x52, 0xb3, 0x37, 0x21, 0x61, 0x79, 0xaa, 0x7f, 0xc7
354+
};
355+
ReadonlyBytes result_bytes { result, 48 };
356+
auto digest = Crypto::Hash::SHA3_384::hash("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"sv);
357+
EXPECT_EQ(result_bytes, digest.bytes());
358+
}
359+
360+
TEST_CASE(test_SHA3_512_name)
361+
{
362+
auto sha = Crypto::Hash::SHA3_512::create();
363+
EXPECT_EQ(sha->class_name(), "SHA3-512"sv);
364+
}
365+
366+
TEST_CASE(test_SHA3_512_hash_string)
367+
{
368+
u8 result[] {
369+
0x4e, 0xda, 0x90, 0x1e, 0x2c, 0xf7, 0x4a, 0xe1, 0x5a, 0x30, 0xbd, 0x3a, 0x13, 0x5a, 0xeb, 0x07, 0x3e, 0x3c, 0x5d, 0x6a, 0x86, 0x34, 0xab, 0x94, 0xa8, 0x97, 0x5b, 0x7c, 0x38, 0x91, 0x89, 0xe5, 0xd1, 0x5a, 0xd9, 0x49, 0x7c, 0xbf, 0x85, 0xd6, 0x8c, 0x5d, 0xd2, 0x75, 0x09, 0x41, 0xb4, 0xa5, 0xe5, 0xf5, 0xf5, 0xc8, 0xc6, 0x44, 0x9b, 0xe1, 0x2f, 0x0e, 0x45, 0xa8, 0x28, 0xf4, 0x49, 0xea
370+
};
371+
auto digest = Crypto::Hash::SHA3_512::hash("Well hello friends"sv);
372+
EXPECT(memcmp(result, digest.data, Crypto::Hash::SHA3_512::digest_size()) == 0);
373+
}
374+
375+
TEST_CASE(test_SHA3_512_hash_bug)
376+
{
377+
u8 result[] {
378+
0xaf, 0xeb, 0xb2, 0xef, 0x54, 0x2e, 0x65, 0x79, 0xc5, 0x0c, 0xad, 0x06, 0xd2, 0xe5, 0x78, 0xf9, 0xf8, 0xdd, 0x68, 0x81, 0xd7, 0xdc, 0x82, 0x4d, 0x26, 0x36, 0x0f, 0xee, 0xbf, 0x18, 0xa4, 0xfa, 0x73, 0xe3, 0x26, 0x11, 0x22, 0x94, 0x8e, 0xfc, 0xfd, 0x49, 0x2e, 0x74, 0xe8, 0x2e, 0x21, 0x89, 0xed, 0x0f, 0xb4, 0x40, 0xd1, 0x87, 0xf3, 0x82, 0x27, 0x0c, 0xb4, 0x55, 0xf2, 0x1d, 0xd1, 0x85
379+
};
380+
ReadonlyBytes result_bytes { result, 64 };
381+
auto digest = Crypto::Hash::SHA3_512::hash("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"sv);
382+
EXPECT_EQ(result_bytes, digest.bytes());
383+
}
384+
385+
TEST_CASE(test_SHA3_512_hash_empty_string)
386+
{
387+
u8 result[] {
388+
0xa6, 0x9f, 0x73, 0xcc, 0xa2, 0x3a, 0x9a, 0xc5, 0xc8, 0xb5, 0x67, 0xdc, 0x18, 0x5a, 0x75, 0x6e, 0x97, 0xc9, 0x82, 0x16, 0x4f, 0xe2, 0x58, 0x59, 0xe0, 0xd1, 0xdc, 0xc1, 0x47, 0x5c, 0x80, 0xa6, 0x15, 0xb2, 0x12, 0x3a, 0xf1, 0xf5, 0xf9, 0x4c, 0x11, 0xe3, 0xe9, 0x40, 0x2c, 0x3a, 0xc5, 0x58, 0xf5, 0x00, 0x19, 0x9d, 0x95, 0xb6, 0xd3, 0xe3, 0x01, 0x75, 0x85, 0x86, 0x28, 0x1d, 0xcd, 0x26
389+
};
390+
auto digest = Crypto::Hash::SHA3_512::hash(""sv);
391+
EXPECT(memcmp(result, digest.data, Crypto::Hash::SHA3_512::digest_size()) == 0);
392+
}

0 commit comments

Comments
 (0)