LibCrypto: SHA3 hashing algorithm

This commit is contained in:
Tete17
2025-11-26 19:36:08 +01:00
committed by Tim Flynn
parent 7b0dfa61b1
commit 0786486aa2
Notes: github-actions[bot] 2025-11-27 03:01:59 +00:00
5 changed files with 194 additions and 1 deletions

View File

@@ -19,6 +19,7 @@ set(SOURCES
Hash/PBKDF2.cpp
Hash/SHA1.cpp
Hash/SHA2.cpp
Hash/SHA3.cpp
PK/RSA.cpp
PK/EC.cpp
SecureRandom.cpp

View File

@@ -14,6 +14,7 @@
#include <LibCrypto/Hash/MD5.h>
#include <LibCrypto/Hash/SHA1.h>
#include <LibCrypto/Hash/SHA2.h>
#include <LibCrypto/Hash/SHA3.h>
namespace Crypto::Hash {
@@ -26,6 +27,9 @@ enum class HashKind {
SHA256,
SHA384,
SHA512,
SHA3_256,
SHA3_384,
SHA3_512
};
struct MultiHashDigestVariant {
@@ -157,6 +161,15 @@ public:
case HashKind::SHA512:
m_algorithm = SHA512::create();
break;
case HashKind::SHA3_256:
m_algorithm = SHA3_256::create();
break;
case HashKind::SHA3_384:
m_algorithm = SHA3_384::create();
break;
case HashKind::SHA3_512:
m_algorithm = SHA3_512::create();
break;
default:
case HashKind::None:
m_algorithm = Empty {};
@@ -232,7 +245,16 @@ public:
}
private:
using AlgorithmVariant = Variant<Empty, NonnullOwnPtr<BLAKE2b>, NonnullOwnPtr<MD5>, NonnullOwnPtr<SHA1>, NonnullOwnPtr<SHA256>, NonnullOwnPtr<SHA384>, NonnullOwnPtr<SHA512>>;
using AlgorithmVariant = Variant<Empty,
NonnullOwnPtr<BLAKE2b>,
NonnullOwnPtr<MD5>,
NonnullOwnPtr<SHA1>,
NonnullOwnPtr<SHA256>,
NonnullOwnPtr<SHA384>,
NonnullOwnPtr<SHA512>,
NonnullOwnPtr<SHA3_256>,
NonnullOwnPtr<SHA3_384>,
NonnullOwnPtr<SHA3_512>>;
AlgorithmVariant m_algorithm {};
HashKind m_kind { HashKind::None };
ByteBuffer m_pre_init_buffer;

View File

@@ -0,0 +1,28 @@
/*
* Copyright (c) 2025, Miguel Sacristán Izcue <miguel_tete17@hotmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibCrypto/Hash/SHA3.h>
#include <openssl/evp.h>
namespace Crypto::Hash {
SHA3_256::SHA3_256(EVP_MD_CTX* context)
: OpenSSLHashFunction(EVP_sha3_256(), context)
{
}
SHA3_384::SHA3_384(EVP_MD_CTX* context)
: OpenSSLHashFunction(EVP_sha3_384(), context)
{
}
SHA3_512::SHA3_512(EVP_MD_CTX* context)
: OpenSSLHashFunction(EVP_sha3_512(), context)
{
}
}

View File

@@ -0,0 +1,50 @@
/*
* Copyright (c) 2025, Miguel Sacristán Izcue <miguel_tete17@hotmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Noncopyable.h>
#include <LibCrypto/Hash/OpenSSLHashFunction.h>
namespace Crypto::Hash {
class SHA3_256 final : public OpenSSLHashFunction<SHA3_256, 1088, 256> {
AK_MAKE_NONCOPYABLE(SHA3_256);
public:
explicit SHA3_256(EVP_MD_CTX* context);
virtual ByteString class_name() const override
{
return "SHA3-256";
}
};
class SHA3_384 final : public OpenSSLHashFunction<SHA3_384, 832, 384> {
AK_MAKE_NONCOPYABLE(SHA3_384);
public:
explicit SHA3_384(EVP_MD_CTX* context);
virtual ByteString class_name() const override
{
return "SHA3-384";
}
};
class SHA3_512 final : public OpenSSLHashFunction<SHA3_512, 576, 512> {
AK_MAKE_NONCOPYABLE(SHA3_512);
public:
explicit SHA3_512(EVP_MD_CTX* context);
virtual ByteString class_name() const override
{
return "SHA3-512";
}
};
}

View File

@@ -298,3 +298,95 @@ TEST_CASE(test_SHA512_hash_empty_string)
auto digest = Crypto::Hash::SHA512::hash(""sv);
EXPECT(memcmp(result, digest.data, Crypto::Hash::SHA512::digest_size()) == 0);
}
TEST_CASE(test_SHA3_256_name)
{
auto sha = Crypto::Hash::SHA3_256::create();
EXPECT_EQ(sha->class_name(), "SHA3-256"sv);
}
TEST_CASE(test_SHA3_256_hash_string)
{
u8 result[] {
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
};
auto digest = Crypto::Hash::SHA3_256::hash("Well hello friends"sv);
EXPECT(memcmp(result, digest.data, Crypto::Hash::SHA3_256::digest_size()) == 0);
}
TEST_CASE(test_SHA3_256_hash_empty_string)
{
u8 result[] {
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
};
auto digest = Crypto::Hash::SHA3_256::hash(""sv);
EXPECT(memcmp(result, digest.data, Crypto::Hash::SHA3_256::digest_size()) == 0);
}
TEST_CASE(test_SHA3_256_hash_split_into_blocks)
{
u8 result[] {
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
};
auto digest = Crypto::Hash::SHA3_256::hash("0123456789012345678901234567890123456789012345678901234567890123abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcd"sv);
EXPECT(memcmp(result, digest.data, Crypto::Hash::SHA3_256::digest_size()) == 0);
}
TEST_CASE(test_SHA3_384_name)
{
auto sha = Crypto::Hash::SHA3_384::create();
EXPECT_EQ(sha->class_name(), "SHA3-384"sv);
}
TEST_CASE(test_SHA3_384_hash_string)
{
u8 result[] {
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
};
auto digest = Crypto::Hash::SHA3_384::hash("Well hello friends"sv);
EXPECT(memcmp(result, digest.data, Crypto::Hash::SHA3_384::digest_size()) == 0);
}
TEST_CASE(test_SHA3_384_hash_bug)
{
u8 result[] {
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
};
ReadonlyBytes result_bytes { result, 48 };
auto digest = Crypto::Hash::SHA3_384::hash("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"sv);
EXPECT_EQ(result_bytes, digest.bytes());
}
TEST_CASE(test_SHA3_512_name)
{
auto sha = Crypto::Hash::SHA3_512::create();
EXPECT_EQ(sha->class_name(), "SHA3-512"sv);
}
TEST_CASE(test_SHA3_512_hash_string)
{
u8 result[] {
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
};
auto digest = Crypto::Hash::SHA3_512::hash("Well hello friends"sv);
EXPECT(memcmp(result, digest.data, Crypto::Hash::SHA3_512::digest_size()) == 0);
}
TEST_CASE(test_SHA3_512_hash_bug)
{
u8 result[] {
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
};
ReadonlyBytes result_bytes { result, 64 };
auto digest = Crypto::Hash::SHA3_512::hash("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"sv);
EXPECT_EQ(result_bytes, digest.bytes());
}
TEST_CASE(test_SHA3_512_hash_empty_string)
{
u8 result[] {
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
};
auto digest = Crypto::Hash::SHA3_512::hash(""sv);
EXPECT(memcmp(result, digest.data, Crypto::Hash::SHA3_512::digest_size()) == 0);
}