Skip to content

Truly independent web browser

LibCrypto: Replace the hash classes with one OpenSSL digest call

Shannon Booth555598c3ab7e
diff --git a/Libraries/LibCrypto/Authentication/HMAC.h b/Libraries/LibCrypto/Authentication/HMAC.h
index 304c0e35de7..13aee656cb8 100644
--- a/Libraries/LibCrypto/Authentication/HMAC.h
+++ b/Libraries/LibCrypto/Authentication/HMAC.h
@@ -9,7 +9,7 @@
#include <AK/ByteBuffer.h>
#include <AK/ByteString.h>
-#include <LibCrypto/Hash/HashManager.h>
+#include <LibCrypto/Hash/Hash.h>
#include <LibCrypto/OpenSSL.h>
#include <LibCrypto/OpenSSLForward.h>
diff --git a/Libraries/LibCrypto/CMakeLists.txt b/Libraries/LibCrypto/CMakeLists.txt
index 7daa1112fe5..7222c3eea2a 100644
--- a/Libraries/LibCrypto/CMakeLists.txt
+++ b/Libraries/LibCrypto/CMakeLists.txt
@@ -19,9 +19,6 @@ set(SOURCES
Hash/SHAKE.cpp
Hash/HKDF.cpp
Hash/PBKDF2.cpp
- Hash/SHA1.cpp
- Hash/SHA2.cpp
- Hash/SHA3.cpp
PK/EC.cpp
PK/MLDSA.cpp
PK/MLKEM.cpp
diff --git a/Libraries/LibCrypto/Hash/HKDF.h b/Libraries/LibCrypto/Hash/HKDF.h
index 5b9dbe3aa8d..c171179113c 100644
--- a/Libraries/LibCrypto/Hash/HKDF.h
+++ b/Libraries/LibCrypto/Hash/HKDF.h
@@ -8,7 +8,7 @@
#pragma once
-#include <LibCrypto/Hash/HashManager.h>
+#include <LibCrypto/Hash/Hash.h>
namespace Crypto::Hash {
diff --git a/Libraries/LibCrypto/Hash/Hash.cpp b/Libraries/LibCrypto/Hash/Hash.cpp
index ec4761ac583..5b2cbb37445 100644
--- a/Libraries/LibCrypto/Hash/Hash.cpp
+++ b/Libraries/LibCrypto/Hash/Hash.cpp
@@ -6,8 +6,12 @@
#include <LibCrypto/Hash/Hash.h>
+#include <openssl/evp.h>
+
namespace Crypto::Hash {
+static_assert(sizeof(Digest::data) == EVP_MAX_MD_SIZE);
+
ErrorOr<char const*> openssl_digest_name(HashKind kind)
{
switch (kind) {
@@ -32,4 +36,63 @@ ErrorOr<char const*> openssl_digest_name(HashKind kind)
return Error::from_string_literal("Unsupported hash kind");
}
+size_t digest_size(HashKind kind)
+{
+ switch (kind) {
+ case HashKind::SHA1:
+ return 20;
+ case HashKind::SHA256:
+ case HashKind::SHA3_256:
+ return 32;
+ case HashKind::SHA384:
+ case HashKind::SHA3_384:
+ return 48;
+ case HashKind::SHA512:
+ case HashKind::SHA3_512:
+ return 64;
+ case HashKind::Unknown:
+ case HashKind::None:
+ break;
+ }
+ VERIFY_NOT_REACHED();
+}
+
+Digest digest(HashKind kind, ReadonlyBytes data)
+{
+ Digest result;
+ if (EVP_Q_digest(nullptr, MUST(openssl_digest_name(kind)), nullptr, data.data(), data.size(), result.data.data(), &result.size) != 1)
+ VERIFY_NOT_REACHED();
+ return result;
+}
+
+Hasher::Hasher(HashKind kind)
+ : m_context(EVP_MD_CTX_new())
+{
+ auto* type = EVP_MD_fetch(nullptr, MUST(openssl_digest_name(kind)), nullptr);
+ if (!m_context || !type || EVP_DigestInit_ex(m_context, type, nullptr) != 1)
+ VERIFY_NOT_REACHED();
+ EVP_MD_free(type);
+}
+
+Hasher::~Hasher()
+{
+ EVP_MD_CTX_free(m_context);
+}
+
+void Hasher::update(ReadonlyBytes data)
+{
+ if (EVP_DigestUpdate(m_context, data.data(), data.size()) != 1)
+ VERIFY_NOT_REACHED();
+}
+
+Digest Hasher::finish()
+{
+ Digest result;
+ unsigned int size = 0;
+ if (EVP_DigestFinal_ex(m_context, result.data.data(), &size) != 1)
+ VERIFY_NOT_REACHED();
+ result.size = size;
+ return result;
+}
+
}
diff --git a/Libraries/LibCrypto/Hash/Hash.h b/Libraries/LibCrypto/Hash/Hash.h
index 014cf299d1a..8ca4142d163 100644
--- a/Libraries/LibCrypto/Hash/Hash.h
+++ b/Libraries/LibCrypto/Hash/Hash.h
@@ -6,8 +6,12 @@
#pragma once
+#include <AK/Array.h>
#include <AK/Error.h>
+#include <AK/Noncopyable.h>
+#include <AK/StringView.h>
#include <AK/Types.h>
+#include <LibCrypto/OpenSSLForward.h>
namespace Crypto::Hash {
@@ -25,5 +29,32 @@ enum class HashKind {
};
ErrorOr<char const*> openssl_digest_name(HashKind);
+size_t digest_size(HashKind);
+
+struct Digest {
+ ReadonlyBytes bytes() const LIFETIME_BOUND { return data.span().trim(size); }
+
+ Array<u8, 64> data {};
+ size_t size { 0 };
+};
+
+Digest digest(HashKind, ReadonlyBytes);
+
+class Hasher {
+ AK_MAKE_NONCOPYABLE(Hasher);
+ AK_MAKE_NONMOVABLE(Hasher);
+
+public:
+ explicit Hasher(HashKind);
+ ~Hasher();
+
+ void update(ReadonlyBytes);
+ void update(StringView string) { update(string.bytes()); }
+
+ Digest finish();
+
+private:
+ EVP_MD_CTX* m_context { nullptr };
+};
}
diff --git a/Libraries/LibCrypto/Hash/HashFunction.h b/Libraries/LibCrypto/Hash/HashFunction.h
deleted file mode 100644
index 142d5487da1..00000000000
--- a/Libraries/LibCrypto/Hash/HashFunction.h
+++ /dev/null
@@ -1,78 +0,0 @@
-/*
- * Copyright (c) 2020, Ali Mohammad Pur <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <AK/ByteBuffer.h>
-#include <AK/Format.h>
-#include <AK/StringView.h>
-#include <AK/Types.h>
-
-namespace Crypto::Hash {
-
-template<size_t DigestS>
-struct Digest {
- static_assert(DigestS % 8 == 0);
- constexpr static size_t Size = DigestS / 8;
- u8 data[Size];
-
- [[nodiscard]] ALWAYS_INLINE u8 const* immutable_data() const { return data; }
- [[nodiscard]] ALWAYS_INLINE size_t data_length() const { return Size; }
-
- [[nodiscard]] ALWAYS_INLINE ReadonlyBytes bytes() const LIFETIME_BOUND { return { immutable_data(), data_length() }; }
-
- [[nodiscard]] bool operator==(Digest const& other) const = default;
- [[nodiscard]] bool operator!=(Digest const& other) const = default;
-};
-
-template<size_t BlockS, size_t DigestS, typename DigestT = Digest<DigestS>>
-class HashFunction {
-public:
- AK_ALLOC_WITH_KMALLOC;
-
- static_assert(BlockS % 8 == 0);
- static constexpr auto BlockSize = BlockS / 8;
-
- static_assert(DigestS % 8 == 0);
- static constexpr auto DigestSize = DigestS / 8;
-
- using DigestType = DigestT;
-
- constexpr static size_t block_size() { return BlockSize; }
- constexpr static size_t digest_size() { return DigestSize; }
-
- virtual void update(u8 const*, size_t) = 0;
-
- void update(Bytes buffer) { update(buffer.data(), buffer.size()); }
- void update(ReadonlyBytes buffer) { update(buffer.data(), buffer.size()); }
- void update(ByteBuffer const& buffer) { update(buffer.data(), buffer.size()); }
- void update(StringView string) { update((u8 const*)string.characters_without_null_termination(), string.length()); }
-
- virtual DigestType peek() = 0;
- virtual DigestType digest() = 0;
-
- virtual void reset() = 0;
-
- virtual ByteString class_name() const = 0;
-
-protected:
- virtual ~HashFunction() = default;
-};
-
-}
-
-template<size_t DigestS>
-struct AK::Formatter<Crypto::Hash::Digest<DigestS>> : StandardFormatter {
- ErrorOr<void> format(FormatBuilder& builder, Crypto::Hash::Digest<DigestS> const& digest)
- {
- for (size_t i = 0; i < digest.Size; ++i) {
- if (i > 0 && i % 4 == 0)
- TRY(builder.put_padding('-', 1));
- TRY(builder.put_u64(digest.data[i], 16, false, false, true, false, FormatBuilder::Align::Right, 2));
- }
- return {};
- }
-};
diff --git a/Libraries/LibCrypto/Hash/HashManager.h b/Libraries/LibCrypto/Hash/HashManager.h
deleted file mode 100644
index 8cab7eb5972..00000000000
--- a/Libraries/LibCrypto/Hash/HashManager.h
+++ /dev/null
@@ -1,235 +0,0 @@
-/*
- * Copyright (c) 2020, Ali Mohammad Pur <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <AK/Optional.h>
-#include <AK/OwnPtr.h>
-#include <AK/Variant.h>
-#include <LibCrypto/Hash/Hash.h>
-#include <LibCrypto/Hash/HashFunction.h>
-#include <LibCrypto/Hash/SHA1.h>
-#include <LibCrypto/Hash/SHA2.h>
-#include <LibCrypto/Hash/SHA3.h>
-
-namespace Crypto::Hash {
-
-struct MultiHashDigestVariant {
- constexpr static size_t Size = 0;
-
- MultiHashDigestVariant(Empty digest)
- : m_digest(move(digest))
- {
- }
-
- MultiHashDigestVariant(SHA1::DigestType digest)
- : m_digest(move(digest))
- {
- }
-
- MultiHashDigestVariant(SHA256::DigestType digest)
- : m_digest(move(digest))
- {
- }
-
- MultiHashDigestVariant(SHA384::DigestType digest)
- : m_digest(move(digest))
- {
- }
-
- MultiHashDigestVariant(SHA512::DigestType digest)
- : m_digest(move(digest))
- {
- }
-
- [[nodiscard]] u8 const* immutable_data() const
- {
- return m_digest.visit(
- [&](Empty const&) -> u8 const* { VERIFY_NOT_REACHED(); },
- [&](auto const& value) { return value.immutable_data(); });
- }
-
- [[nodiscard]] size_t data_length() const
- {
- return m_digest.visit(
- [&](Empty const&) -> size_t { VERIFY_NOT_REACHED(); },
- [&](auto const& value) { return value.data_length(); });
- }
-
- [[nodiscard]] ReadonlyBytes bytes() const LIFETIME_BOUND
- {
- return m_digest.visit(
- [&](Empty const&) -> ReadonlyBytes { VERIFY_NOT_REACHED(); },
- [&](auto const& value) { return value.bytes(); });
- }
-
- using DigestVariant = Variant<Empty, SHA1::DigestType, SHA256::DigestType, SHA384::DigestType, SHA512::DigestType>;
- DigestVariant m_digest {};
-};
-
-class Manager final : public HashFunction<0, 0, MultiHashDigestVariant> {
-public:
- using HashFunction::update;
-
- static NonnullOwnPtr<Manager> create(HashKind kind)
- {
- return make<Manager>(kind);
- }
-
- Manager()
- {
- m_pre_init_buffer = ByteBuffer();
- }
-
- Manager(Manager const& other) // NOT a copy constructor!
- {
- m_pre_init_buffer = ByteBuffer(); // will not be used
- initialize(other.m_kind);
- }
-
- Manager(HashKind kind)
- {
- m_pre_init_buffer = ByteBuffer();
- initialize(kind);
- }
-
- ~Manager()
- {
- m_algorithm = Empty {};
- }
-
- inline size_t digest_size() const
- {
- return m_algorithm.visit(
- [&](Empty const&) -> size_t { return 0; },
- [&](auto const& hash) { return hash->digest_size(); });
- }
-
- inline size_t block_size() const
- {
- return m_algorithm.visit(
- [&](Empty const&) -> size_t { return 0; },
- [&](auto const& hash) { return hash->block_size(); });
- }
-
- inline void initialize(HashKind kind)
- {
- if (!m_algorithm.has<Empty>()) {
- VERIFY_NOT_REACHED();
- }
-
- m_kind = kind;
- switch (kind) {
- case HashKind::SHA1:
- m_algorithm = SHA1::create();
- break;
- case HashKind::SHA256:
- m_algorithm = SHA256::create();
- break;
- case HashKind::SHA384:
- m_algorithm = SHA384::create();
- break;
- 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 {};
- break;
- }
- }
-
- virtual void update(u8 const* data, size_t length) override
- {
- auto size = m_pre_init_buffer.size();
- if (size) {
- m_algorithm.visit(
- [&](Empty&) { },
- [&](auto& hash) { hash->update(m_pre_init_buffer); });
- }
- m_algorithm.visit(
- [&](Empty&) { m_pre_init_buffer.append(data, length); },
- [&](auto& hash) { hash->update(data, length); });
- if (size && m_kind != HashKind::None)
- m_pre_init_buffer.clear();
- }
-
- virtual DigestType peek() override
- {
- return m_algorithm.visit(
- [&](Empty&) -> DigestType { VERIFY_NOT_REACHED(); },
- [&](auto& hash) -> DigestType { return hash->peek(); });
- }
-
- virtual DigestType digest() override
- {
- return m_algorithm.visit(
- [&](Empty&) -> DigestType { VERIFY_NOT_REACHED(); },
- [&](auto& hash) -> DigestType { return hash->digest(); });
- }
-
- virtual void reset() override
- {
- m_pre_init_buffer.clear();
- m_algorithm.visit(
- [&](Empty&) { },
- [&](auto& hash) { hash->reset(); });
- }
-
- virtual ByteString class_name() const override
- {
- return m_algorithm.visit(
- [&](Empty const&) -> ByteString { return "UninitializedHashManager"; },
- [&](auto const& hash) { return hash->class_name(); });
- }
-
- inline HashKind kind() const
- {
- return m_kind;
- }
-
- inline bool is(HashKind kind) const
- {
- return m_kind == kind;
- }
-
- inline Manager copy() const
- {
- auto algorithm = m_algorithm.visit(
- [&](Empty const&) -> AlgorithmVariant { VERIFY_NOT_REACHED(); },
- [&](auto const& hash) -> AlgorithmVariant { return hash->copy(); });
-
- Manager result;
- result.m_algorithm = move(algorithm);
- result.m_kind = m_kind;
- result.m_pre_init_buffer = m_pre_init_buffer;
- return result;
- }
-
-private:
- using AlgorithmVariant = Variant<Empty,
- 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;
-};
-
-}
diff --git a/Libraries/LibCrypto/Hash/OpenSSLHashFunction.h b/Libraries/LibCrypto/Hash/OpenSSLHashFunction.h
deleted file mode 100644
index ca50b2dae13..00000000000
--- a/Libraries/LibCrypto/Hash/OpenSSLHashFunction.h
+++ /dev/null
@@ -1,100 +0,0 @@
-/*
- * Copyright (c) 2024, Altomani Gianluca <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <AK/ByteString.h>
-#include <AK/NonnullOwnPtr.h>
-#include <AK/Types.h>
-#include <LibCrypto/Hash/HashFunction.h>
-#include <LibCrypto/OpenSSLForward.h>
-
-namespace Crypto::Hash {
-
-template<typename T, size_t BlockS, size_t DigestS, typename DigestT = Digest<DigestS>>
-class OpenSSLHashFunction : public HashFunction<BlockS, DigestS, DigestT> {
- AK_MAKE_NONCOPYABLE(OpenSSLHashFunction);
-
-public:
- using HashFunction<BlockS, DigestS, DigestT>::update;
-
- static NonnullOwnPtr<T> create()
- {
- auto* context = EVP_MD_CTX_new();
- return make<T>(context);
- }
-
- OpenSSLHashFunction(EVP_MD const* type, EVP_MD_CTX* context)
- : m_type(type)
- , m_context(context)
- {
- OpenSSLHashFunction::reset();
- }
-
- virtual ~OpenSSLHashFunction() override
- {
- EVP_MD_CTX_free(m_context);
- }
-
- virtual ByteString class_name() const override = 0;
-
- void update(u8 const* input, size_t length) override
- {
- if (EVP_DigestUpdate(m_context, input, length) != 1) {
- VERIFY_NOT_REACHED();
- }
- }
-
- DigestT digest() override
- {
- DigestT digest;
- if (EVP_DigestFinal_ex(m_context, digest.data, nullptr) != 1) {
- VERIFY_NOT_REACHED();
- }
-
- reset();
- return digest;
- }
-
- DigestT peek() override
- {
- auto c = copy();
- return c->digest();
- }
-
- void reset() override
- {
- if (EVP_DigestInit_ex(m_context, m_type, nullptr) != 1) {
- VERIFY_NOT_REACHED();
- }
- }
-
- virtual NonnullOwnPtr<T> copy() const
- {
- auto context = create();
- if (EVP_MD_CTX_copy_ex(context->m_context, m_context) != 1) {
- VERIFY_NOT_REACHED();
- }
-
- return context;
- }
-
- static DigestT hash(ReadonlyBytes buffer)
- {
- auto hasher = create();
- hasher->update(buffer.data(), buffer.size());
- return hasher->digest();
- }
-
- static DigestT hash(ByteBuffer const& buffer) { return hash(buffer.bytes()); }
- static DigestT hash(StringView buffer) { return hash(buffer.bytes()); }
-
-private:
- EVP_MD const* m_type { nullptr };
- EVP_MD_CTX* m_context { nullptr };
-};
-
-}
diff --git a/Libraries/LibCrypto/Hash/PBKDF2.h b/Libraries/LibCrypto/Hash/PBKDF2.h
index cab6a8afeca..a0046f1491e 100644
--- a/Libraries/LibCrypto/Hash/PBKDF2.h
+++ b/Libraries/LibCrypto/Hash/PBKDF2.h
@@ -7,7 +7,7 @@
#pragma once
-#include <LibCrypto/Hash/HashManager.h>
+#include <LibCrypto/Hash/Hash.h>
namespace Crypto::Hash {
diff --git a/Libraries/LibCrypto/Hash/SHA1.cpp b/Libraries/LibCrypto/Hash/SHA1.cpp
deleted file mode 100644
index 40e94fbf728..00000000000
--- a/Libraries/LibCrypto/Hash/SHA1.cpp
+++ /dev/null
@@ -1,18 +0,0 @@
-/*
- * Copyright (c) 2025, Altomani Gianluca <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibCrypto/Hash/SHA1.h>
-
-#include <openssl/evp.h>
-
-namespace Crypto::Hash {
-
-SHA1::SHA1(EVP_MD_CTX* context)
- : OpenSSLHashFunction(EVP_sha1(), context)
-{
-}
-
-}
diff --git a/Libraries/LibCrypto/Hash/SHA1.h b/Libraries/LibCrypto/Hash/SHA1.h
deleted file mode 100644
index 3c50487a8f8..00000000000
--- a/Libraries/LibCrypto/Hash/SHA1.h
+++ /dev/null
@@ -1,26 +0,0 @@
-/*
- * Copyright (c) 2020, Ali Mohammad Pur <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <AK/ByteString.h>
-#include <LibCrypto/Hash/OpenSSLHashFunction.h>
-
-namespace Crypto::Hash {
-
-class SHA1 final : public OpenSSLHashFunction<SHA1, 512, 160> {
- AK_MAKE_NONCOPYABLE(SHA1);
-
-public:
- explicit SHA1(EVP_MD_CTX* context);
-
- virtual ByteString class_name() const override
- {
- return "SHA1";
- }
-};
-
-}
diff --git a/Libraries/LibCrypto/Hash/SHA2.cpp b/Libraries/LibCrypto/Hash/SHA2.cpp
deleted file mode 100644
index a19026022f7..00000000000
--- a/Libraries/LibCrypto/Hash/SHA2.cpp
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * Copyright (c) 2025, Altomani Gianluca <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibCrypto/Hash/SHA2.h>
-
-#include <openssl/evp.h>
-
-namespace Crypto::Hash {
-
-SHA256::SHA256(EVP_MD_CTX* context)
- : OpenSSLHashFunction(EVP_sha256(), context)
-{
-}
-
-SHA384::SHA384(EVP_MD_CTX* context)
- : OpenSSLHashFunction(EVP_sha384(), context)
-{
-}
-
-SHA512::SHA512(EVP_MD_CTX* context)
- : OpenSSLHashFunction(EVP_sha512(), context)
-{
-}
-
-}
diff --git a/Libraries/LibCrypto/Hash/SHA2.h b/Libraries/LibCrypto/Hash/SHA2.h
deleted file mode 100644
index 9fe597619bb..00000000000
--- a/Libraries/LibCrypto/Hash/SHA2.h
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * Copyright (c) 2020, Ali Mohammad Pur <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <AK/ByteString.h>
-#include <LibCrypto/Hash/OpenSSLHashFunction.h>
-
-namespace Crypto::Hash {
-
-class SHA256 final : public OpenSSLHashFunction<SHA256, 512, 256> {
- AK_MAKE_NONCOPYABLE(SHA256);
-
-public:
- explicit SHA256(EVP_MD_CTX* context);
-
- virtual ByteString class_name() const override
- {
- return "SHA256";
- }
-};
-
-class SHA384 final : public OpenSSLHashFunction<SHA384, 1024, 384> {
- AK_MAKE_NONCOPYABLE(SHA384);
-
-public:
- explicit SHA384(EVP_MD_CTX* context);
-
- virtual ByteString class_name() const override
- {
- return "SHA384";
- }
-};
-
-class SHA512 final : public OpenSSLHashFunction<SHA512, 1024, 512> {
- AK_MAKE_NONCOPYABLE(SHA512);
-
-public:
- explicit SHA512(EVP_MD_CTX* context);
-
- virtual ByteString class_name() const override
- {
- return "SHA512";
- }
-};
-
-}
diff --git a/Libraries/LibCrypto/Hash/SHA3.cpp b/Libraries/LibCrypto/Hash/SHA3.cpp
deleted file mode 100644
index 2485f0e2291..00000000000
--- a/Libraries/LibCrypto/Hash/SHA3.cpp
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * Copyright (c) 2025, Miguel Sacristán Izcue <[email protected]>
- *
- * 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)
-{
-}
-
-}
diff --git a/Libraries/LibCrypto/Hash/SHA3.h b/Libraries/LibCrypto/Hash/SHA3.h
deleted file mode 100644
index 62dd2ea42c1..00000000000
--- a/Libraries/LibCrypto/Hash/SHA3.h
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * Copyright (c) 2025, Miguel Sacristán Izcue <[email protected]>
- *
- * 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";
- }
-};
-
-}
diff --git a/Libraries/LibCrypto/OpenSSLForward.h b/Libraries/LibCrypto/OpenSSLForward.h
index 96fc6b710c1..5b699467343 100644
--- a/Libraries/LibCrypto/OpenSSLForward.h
+++ b/Libraries/LibCrypto/OpenSSLForward.h
@@ -27,10 +27,6 @@ void X509_free(X509*);
EVP_MD_CTX* EVP_MD_CTX_new();
void EVP_MD_CTX_free(EVP_MD_CTX*);
-int EVP_DigestUpdate(EVP_MD_CTX*, void const*, size_t);
-int EVP_DigestInit_ex(EVP_MD_CTX*, const EVP_MD*, ENGINE*);
-int EVP_DigestFinal_ex(EVP_MD_CTX*, unsigned char*, unsigned int*);
-int EVP_MD_CTX_copy_ex(EVP_MD_CTX*, EVP_MD_CTX const*);
void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX*);
void EVP_PKEY_CTX_free(EVP_PKEY_CTX*);
diff --git a/Libraries/LibCrypto/PK/RSA.h b/Libraries/LibCrypto/PK/RSA.h
index bf91ae7ecb0..8502f1ace82 100644
--- a/Libraries/LibCrypto/PK/RSA.h
+++ b/Libraries/LibCrypto/PK/RSA.h
@@ -10,7 +10,7 @@
#include <LibCrypto/ASN1/DER.h>
#include <LibCrypto/BigInt/UnsignedBigInteger.h>
-#include <LibCrypto/Hash/HashManager.h>
+#include <LibCrypto/Hash/Hash.h>
#include <LibCrypto/OpenSSL.h>
#include <LibCrypto/PK/PK.h>
diff --git a/Libraries/LibCrypto/RustBindings.cpp b/Libraries/LibCrypto/RustBindings.cpp
index ce5b0d4c9d7..c031a9c42cd 100644
--- a/Libraries/LibCrypto/RustBindings.cpp
+++ b/Libraries/LibCrypto/RustBindings.cpp
@@ -22,10 +22,8 @@
#include <LibCrypto/Curves/SECPxxxr1.h>
#include <LibCrypto/Hash/Argon2.h>
#include <LibCrypto/Hash/HKDF.h>
-#include <LibCrypto/Hash/HashManager.h>
+#include <LibCrypto/Hash/Hash.h>
#include <LibCrypto/Hash/PBKDF2.h>
-#include <LibCrypto/Hash/SHA1.h>
-#include <LibCrypto/Hash/SHA2.h>
#include <LibCrypto/Hash/SHAKE.h>
#include <LibCrypto/KeyHandle.h>
#include <LibCrypto/PK/MLDSA.h>
@@ -409,12 +407,9 @@ extern "C" bool crypto_sha_digest(::Crypto::Hash::HashKind, ReadonlyBytes, void*
bool crypto_sha_digest(::Crypto::Hash::HashKind hash_kind, ReadonlyBytes data, void* state, ReceiveEncodedBytes receive)
{
- ::Crypto::Hash::Manager hash { hash_kind };
- hash.update(data);
-
- auto digest = hash.digest();
- auto result_buffer = ByteBuffer::copy(digest.immutable_data(), hash.digest_size());
- return receive_byte_result(result_buffer, state, receive);
+ auto digest = ::Crypto::Hash::digest(hash_kind, data);
+ receive(state, digest.bytes());
+ return true;
}
struct OptionalBytes {
@@ -791,11 +786,6 @@ extern "C" {
void crypto_hmac_sign(::Crypto::Hash::HashKind, ReadonlyBytes, ReadonlyBytes, void*, ReceiveEncodedBytes);
}
-static_assert(::Crypto::Hash::SHA1::block_size() == 64);
-static_assert(::Crypto::Hash::SHA256::block_size() == 64);
-static_assert(::Crypto::Hash::SHA384::block_size() == 128);
-static_assert(::Crypto::Hash::SHA512::block_size() == 128);
-
void crypto_hmac_sign(::Crypto::Hash::HashKind hash_kind, ReadonlyBytes key, ReadonlyBytes message, void* state, ReceiveEncodedBytes receive)
{
::Crypto::Authentication::HMAC hmac(hash_kind, key);
@@ -977,7 +967,7 @@ FlatPtr crypto_argon2_derive_key(::Crypto::Hash::Argon2Type, Argon2Inputs const*
size_t crypto_hash_digest_size(::Crypto::Hash::HashKind hash_kind)
{
- return ::Crypto::Hash::Manager { hash_kind }.digest_size();
+ return ::Crypto::Hash::digest_size(hash_kind);
}
bool crypto_hkdf_derive_key(::Crypto::Hash::HashKind kind, ReadonlyBytes salt, ReadonlyBytes key_derivation_key, ReadonlyBytes info, u32 length, void* state, ReceiveEncodedBytes receive)
diff --git a/Libraries/LibDNS/Validator.cpp b/Libraries/LibDNS/Validator.cpp
index d95d5b4882f..9876a2bc197 100644
--- a/Libraries/LibDNS/Validator.cpp
+++ b/Libraries/LibDNS/Validator.cpp
@@ -11,8 +11,7 @@
#include <AK/QuickSort.h>
#include <LibCrypto/Curves/EdwardsCurve.h>
#include <LibCrypto/Curves/SECPxxxr1.h>
-#include <LibCrypto/Hash/SHA1.h>
-#include <LibCrypto/Hash/SHA2.h>
+#include <LibCrypto/Hash/Hash.h>
#include <LibCrypto/PK/RSA.h>
#include <LibDNS/Validator.h>
@@ -152,11 +151,11 @@ static ErrorOr<bool> verify_signature(Records::DNSKEY const& key, ReadonlyBytes
scalar_size,
};
if (is_p256) {
- auto digest = Crypto::Hash::SHA256::hash(signed_data);
+ auto digest = Crypto::Hash::digest(Crypto::Hash::HashKind::SHA256, signed_data);
Crypto::Curves::SECP256r1 curve;
return curve.verify(digest.bytes(), point, ecdsa_signature);
}
- auto digest = Crypto::Hash::SHA384::hash(signed_data);
+ auto digest = Crypto::Hash::digest(Crypto::Hash::HashKind::SHA384, signed_data);
Crypto::Curves::SECP384r1 curve;
return curve.verify(digest.bytes(), point, ecdsa_signature);
}
@@ -179,11 +178,11 @@ static ErrorOr<ByteBuffer> ds_digest(DomainName const& owner, Records::DNSKEY co
TRY(key.to_raw(input));
switch (digest_type) {
case DigestType::SHA1:
- return ByteBuffer::copy(Crypto::Hash::SHA1::hash(input).bytes());
+ return ByteBuffer::copy(Crypto::Hash::digest(Crypto::Hash::HashKind::SHA1, input).bytes());
case DigestType::SHA256:
- return ByteBuffer::copy(Crypto::Hash::SHA256::hash(input).bytes());
+ return ByteBuffer::copy(Crypto::Hash::digest(Crypto::Hash::HashKind::SHA256, input).bytes());
case DigestType::SHA384:
- return ByteBuffer::copy(Crypto::Hash::SHA384::hash(input).bytes());
+ return ByteBuffer::copy(Crypto::Hash::digest(Crypto::Hash::HashKind::SHA384, input).bytes());
default:
return Error::from_string_literal("Unsupported DS digest type");
}
@@ -389,12 +388,12 @@ static ErrorOr<ByteBuffer> nsec3_hash(DomainName const& name, Records::NSEC3 con
ByteBuffer input;
TRY(name.canonicalized().to_raw(input));
TRY(input.try_append(params.salt));
- auto digest = Crypto::Hash::SHA1::hash(input);
+ auto digest = Crypto::Hash::digest(Crypto::Hash::HashKind::SHA1, input);
for (size_t i = 0; i < params.iterations; ++i) {
ByteBuffer next;
TRY(next.try_append(digest.bytes()));
TRY(next.try_append(params.salt));
- digest = Crypto::Hash::SHA1::hash(next);
+ digest = Crypto::Hash::digest(Crypto::Hash::HashKind::SHA1, next);
}
return ByteBuffer::copy(digest.bytes());
}
diff --git a/Libraries/LibHTTP/Cache/Utilities.cpp b/Libraries/LibHTTP/Cache/Utilities.cpp
index c3b144ab70c..80c82daca13 100644
--- a/Libraries/LibHTTP/Cache/Utilities.cpp
+++ b/Libraries/LibHTTP/Cache/Utilities.cpp
@@ -13,7 +13,7 @@
#include <AK/StringBuilder.h>
#include <AK/StringConversions.h>
#include <AK/Utf16View.h>
-#include <LibCrypto/Hash/SHA1.h>
+#include <LibCrypto/Hash/Hash.h>
#include <LibHTTP/Cache/DiskCache.h>
#include <LibHTTP/Cache/Utilities.h>
#include <LibHTTP/HTTP.h>
@@ -81,9 +81,9 @@ String serialize_url_for_cache_storage(URL::URL const& url)
return url.serialize(URL::ExcludeFragment::Yes);
}
-static u64 serialize_hash(Crypto::Hash::SHA1& hasher)
+static u64 serialize_hash(Crypto::Hash::Hasher& hasher)
{
- auto digest = hasher.digest();
+ auto digest = hasher.finish();
auto bytes = digest.bytes();
u64 result = 0;
@@ -101,35 +101,35 @@ static u64 serialize_hash(Crypto::Hash::SHA1& hasher)
u64 create_cache_key(Utf16View const& partition, StringView url, StringView method)
{
- auto hasher = Crypto::Hash::SHA1::create();
+ Crypto::Hash::Hasher hasher { Crypto::Hash::HashKind::SHA1 };
// NB: Hash the code units rather than the string's storage, so the same text always gives the same key.
Array<u8, 256> buffer;
size_t buffer_size = 0;
for (size_t i = 0; i < partition.length_in_code_units(); ++i) {
if (buffer_size == buffer.size()) {
- hasher->update(buffer.span());
+ hasher.update(buffer.span());
buffer_size = 0;
}
auto code_unit = partition.code_unit_at(i);
buffer[buffer_size++] = static_cast<u8>(code_unit & 0xff);
buffer[buffer_size++] = static_cast<u8>(code_unit >> 8);
}
- hasher->update(buffer.span().trim(buffer_size));
- hasher->update("\n"sv);
- hasher->update(url);
- hasher->update(method);
+ hasher.update(buffer.span().trim(buffer_size));
+ hasher.update("\n"sv);
+ hasher.update(url);
+ hasher.update(method);
- return serialize_hash(*hasher);
+ return serialize_hash(hasher);
}
u64 create_cache_key(StringView url, StringView method)
{
- auto hasher = Crypto::Hash::SHA1::create();
- hasher->update(url);
- hasher->update(method);
+ Crypto::Hash::Hasher hasher { Crypto::Hash::HashKind::SHA1 };
+ hasher.update(url);
+ hasher.update(method);
- return serialize_hash(*hasher);
+ return serialize_hash(hasher);
}
// https://httpwg.org/specs/rfc9111.html#caching.negotiated.responses
@@ -185,13 +185,13 @@ Optional<u64> create_vary_key(HeaderList const& request_headers, HeaderList cons
if (field_names->is_empty())
return 0;
- auto hasher = Crypto::Hash::SHA1::create();
+ Crypto::Hash::Hasher hasher { Crypto::Hash::HashKind::SHA1 };
// Length prefixes prevent distinct field sets from hashing the same byte sequence.
auto update_with_length = [&](StringView bytes) {
u64 length = bytes.length();
- hasher->update(ReadonlyBytes { &length, sizeof(length) });
- hasher->update(bytes);
+ hasher.update(ReadonlyBytes { &length, sizeof(length) });
+ hasher.update(bytes);
};
for (auto const& field_name : *field_names) {
@@ -203,7 +203,7 @@ Optional<u64> create_vary_key(HeaderList const& request_headers, HeaderList cons
// NB: Offset present-field counts to distinguish absent and empty fields.
auto values = normalize_request_vary_header_values(field_name, request_headers);
u64 value_count = values.has_value() ? values->size() + 1 : 0;
- hasher->update(ReadonlyBytes { &value_count, sizeof(value_count) });
+ hasher.update(ReadonlyBytes { &value_count, sizeof(value_count) });
if (values.has_value()) {
for (auto const& value : *values)
@@ -211,7 +211,7 @@ Optional<u64> create_vary_key(HeaderList const& request_headers, HeaderList cons
}
}
- return serialize_hash(*hasher);
+ return serialize_hash(hasher);
}
LexicalPath path_for_cache_entry(LexicalPath const& cache_directory, u64 cache_key, u64 vary_key)
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/HTMLIntegration.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Directives/HTMLIntegration.cpp
index 97a15796b92..a7b7fe60158 100644
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/HTMLIntegration.cpp
+++ b/Libraries/LibWeb/ContentSecurityPolicy/Directives/HTMLIntegration.cpp
@@ -6,7 +6,7 @@
*/
#include <AK/Base64.h>
-#include <LibCrypto/Hash/SHA2.h>
+#include <LibCrypto/Hash/Hash.h>
#include <LibURL/Origin.h>
#include <LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.h>
#include <LibWeb/ContentSecurityPolicy/Directives/HTMLIntegration.h>
@@ -278,17 +278,17 @@ static MatchResult does_element_match_source_list_for_type_and_source(GC::Ptr<DO
// 1. Let actual be the result of base64 encoding the result of applying algorithm to source.
auto apply_algorithm_to_source = [&] {
if (*algorithm == "SHA-256"sv) {
- auto result = ::Crypto::Hash::SHA256::hash(converted_source_bytes);
+ auto result = ::Crypto::Hash::digest(::Crypto::Hash::HashKind::SHA256, converted_source_bytes);
return MUST(encode_base64(result.bytes()));
}
if (*algorithm == "SHA-384"sv) {
- auto result = ::Crypto::Hash::SHA384::hash(converted_source_bytes);
+ auto result = ::Crypto::Hash::digest(::Crypto::Hash::HashKind::SHA384, converted_source_bytes);
return MUST(encode_base64(result.bytes()));
}
if (*algorithm == "SHA-512"sv) {
- auto result = ::Crypto::Hash::SHA512::hash(converted_source_bytes);
+ auto result = ::Crypto::Hash::digest(::Crypto::Hash::HashKind::SHA512, converted_source_bytes);
return MUST(encode_base64(result.bytes()));
}
diff --git a/Libraries/LibWeb/SRI/SRI.cpp b/Libraries/LibWeb/SRI/SRI.cpp
index b76f03ec36e..8efedb6f7b0 100644
--- a/Libraries/LibWeb/SRI/SRI.cpp
+++ b/Libraries/LibWeb/SRI/SRI.cpp
@@ -11,7 +11,7 @@
#include <AK/GenericLexer.h>
#include <AK/StringBuilder.h>
#include <AK/Vector.h>
-#include <LibCrypto/Hash/SHA2.h>
+#include <LibCrypto/Hash/Hash.h>
#include <LibWeb/SRI/SRI.h>
#include <LibWebCommon/Infra/CharacterTypes.h>
@@ -54,33 +54,21 @@ static StringView get_prioritized_hash_function(StringView a, StringView b)
// https://w3c.github.io/webappsec-subresource-integrity/#apply-algorithm-to-response
ErrorOr<String> apply_algorithm_to_bytes(StringView algorithm, ByteBuffer const& bytes)
{
- // NOTE: The steps are duplicated here because each hash algorithm returns a different result type.
-
- if (algorithm == "sha256"sv) {
- // 1. Let result be the result of applying algorithm to bytes.
- auto result = Crypto::Hash::SHA256::hash(bytes);
-
- // 2. Return the result of base64 encoding result.
- return encode_base64(result.bytes());
- }
-
- if (algorithm == "sha384"sv) {
- // 1. Let result be the result of applying algorithm to bytes.
- auto result = Crypto::Hash::SHA384::hash(bytes);
-
- // 2. Return the result of base64 encoding result.
- return encode_base64(result.bytes());
- }
-
- if (algorithm == "sha512"sv) {
- // 1. Let result be the result of applying algorithm to bytes.
- auto result = Crypto::Hash::SHA512::hash(bytes);
-
- // 2. Return the result of base64 encoding result.
- return encode_base64(result.bytes());
- }
-
- VERIFY_NOT_REACHED();
+ auto hash_kind = [&] {
+ if (algorithm == "sha256"sv)
+ return Crypto::Hash::HashKind::SHA256;
+ if (algorithm == "sha384"sv)
+ return Crypto::Hash::HashKind::SHA384;
+ if (algorithm == "sha512"sv)
+ return Crypto::Hash::HashKind::SHA512;
+ VERIFY_NOT_REACHED();
+ }();
+
+ // 1. Let result be the result of applying algorithm to bytes.
+ auto result = Crypto::Hash::digest(hash_kind, bytes);
+
+ // 2. Return the result of base64 encoding result.
+ return encode_base64(result.bytes());
}
// https://w3c.github.io/webappsec-subresource-integrity/#parse-metadata
diff --git a/Libraries/LibWebSocket/WebSocket.cpp b/Libraries/LibWebSocket/WebSocket.cpp
index 65a52bb0311..65057c11a8e 100644
--- a/Libraries/LibWebSocket/WebSocket.cpp
+++ b/Libraries/LibWebSocket/WebSocket.cpp
@@ -10,7 +10,7 @@
#include <AK/Random.h>
#include <AK/Utf8View.h>
#include <LibCore/Timer.h>
-#include <LibCrypto/Hash/HashManager.h>
+#include <LibCrypto/Hash/Hash.h>
#include <LibWebSocket/Impl/AcceptedSocket.h>
#include <LibWebSocket/WebSocket.h>
@@ -378,11 +378,9 @@ void WebSocket::read_client_handshake()
}
if (m_state != InternalState::WaitingForClientHandshake)
return;
- Crypto::Hash::Manager hash;
- hash.initialize(Crypto::Hash::HashKind::SHA1);
- hash.update(ByteString::formatted("{}258EAFA5-E914-47DA-95CA-C5AB0DC85B11", *key));
- auto digest = hash.digest();
- auto accept = MUST(encode_base64({ digest.immutable_data(), digest.data_length() }));
+ auto accept_input = ByteString::formatted("{}258EAFA5-E914-47DA-95CA-C5AB0DC85B11", *key);
+ auto digest = Crypto::Hash::digest(Crypto::Hash::HashKind::SHA1, accept_input.bytes());
+ auto accept = MUST(encode_base64(digest.bytes()));
auto response = ByteString::formatted("HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {}\r\n\r\n", accept);
if (!m_impl->send(response.bytes())) {
fail_connection(to_underlying(CloseStatusCode::AbnormalClosure), Error::ConnectionUpgradeFailed, {});
diff --git a/Libraries/LibWebView/CrashReportReview.cpp b/Libraries/LibWebView/CrashReportReview.cpp
index f9bd120713f..94b05ab2d41 100644
--- a/Libraries/LibWebView/CrashReportReview.cpp
+++ b/Libraries/LibWebView/CrashReportReview.cpp
@@ -6,7 +6,7 @@
#include <AK/Hex.h>
#include <AK/WeakPtr.h>
-#include <LibCrypto/Hash/SHA2.h>
+#include <LibCrypto/Hash/Hash.h>
#include <LibURL/URL.h>
#include <LibWebView/CrashReportDiagnostics.h>
#include <LibWebView/CrashReportReview.h>
@@ -20,7 +20,7 @@ static String to_string(StringView text)
static ByteString sha256_hex(ReadonlyBytes bytes)
{
- return encode_hex(Crypto::Hash::SHA256::hash(bytes).bytes());
+ return encode_hex(Crypto::Hash::digest(Crypto::Hash::HashKind::SHA256, bytes).bytes());
}
CrashReportReview::CrashReportReview(CrashReportStore& store)
diff --git a/Libraries/LibWebView/CrashReportSubmission.cpp b/Libraries/LibWebView/CrashReportSubmission.cpp
index 2d4c0b6b6cc..43c267b7277 100644
--- a/Libraries/LibWebView/CrashReportSubmission.cpp
+++ b/Libraries/LibWebView/CrashReportSubmission.cpp
@@ -6,7 +6,7 @@
#include <AK/Hex.h>
#include <LibCore/ImmutableBytes.h>
-#include <LibCrypto/Hash/SHA2.h>
+#include <LibCrypto/Hash/Hash.h>
#include <LibWebView/CrashReportDiagnostics.h>
#include <LibWebView/CrashReportStore.h>
#include <LibWebView/CrashReportSubmission.h>
@@ -18,7 +18,7 @@ static constexpr auto diagnostics_name = "crash-diagnostics.txt"sv;
static ByteString sha256_hex(ReadonlyBytes bytes)
{
- return encode_hex(Crypto::Hash::SHA256::hash(bytes).bytes());
+ return encode_hex(Crypto::Hash::digest(Crypto::Hash::HashKind::SHA256, bytes).bytes());
}
static ReportSubmission::Contents describe_report(CrashReportSubmission::Options const& options, StringView text)
diff --git a/Libraries/LibWebView/FaviconStore.cpp b/Libraries/LibWebView/FaviconStore.cpp
index 273f25e6a5a..02aa9e0acc0 100644
--- a/Libraries/LibWebView/FaviconStore.cpp
+++ b/Libraries/LibWebView/FaviconStore.cpp
@@ -7,7 +7,7 @@
#include <AK/Array.h>
#include <AK/Hex.h>
#include <AK/Vector.h>
-#include <LibCrypto/Hash/SHA2.h>
+#include <LibCrypto/Hash/Hash.h>
#include <LibDatabase/Database.h>
#include <LibDatabase/ResultRow.h>
#include <LibWebView/FaviconStore.h>
@@ -76,7 +76,7 @@ Optional<String> FaviconStore::add_favicon(ByteBuffer favicon_png)
if (favicon_png.is_empty() || favicon_png.size() > MAXIMUM_FAVICON_BYTE_COUNT)
return {};
- auto hash_bytes = encode_hex(Crypto::Hash::SHA256::hash(favicon_png).bytes());
+ auto hash_bytes = encode_hex(Crypto::Hash::digest(Crypto::Hash::HashKind::SHA256, favicon_png).bytes());
auto hash = MUST(String::from_byte_string(hash_bytes));
if (!m_storage->add_favicon(hash, move(favicon_png)))
return {};
diff --git a/Libraries/LibWebView/Profile.cpp b/Libraries/LibWebView/Profile.cpp
index e476d3acdc1..918ec037d25 100644
--- a/Libraries/LibWebView/Profile.cpp
+++ b/Libraries/LibWebView/Profile.cpp
@@ -9,7 +9,7 @@
#include <AK/LexicalPath.h>
#include <AK/Random.h>
#include <LibCore/Directory.h>
-#include <LibCrypto/Hash/SHA2.h>
+#include <LibCrypto/Hash/Hash.h>
#include <LibFileSystem/FileSystem.h>
#include <LibWebView/Profile.h>
@@ -87,7 +87,7 @@ bool Profile::is_valid_name(StringView name)
ByteString Profile::routing_identifier(StringView canonical_profile_root)
{
- auto digest = Crypto::Hash::SHA256::hash(canonical_profile_root);
+ auto digest = Crypto::Hash::digest(Crypto::Hash::HashKind::SHA256, canonical_profile_root.bytes());
return encode_hex(digest.bytes().slice(0, 16));
}
diff --git a/Libraries/LibWebView/ReportSubmission.cpp b/Libraries/LibWebView/ReportSubmission.cpp
index 0b6e05c7ded..fddb2f7e570 100644
--- a/Libraries/LibWebView/ReportSubmission.cpp
+++ b/Libraries/LibWebView/ReportSubmission.cpp
@@ -16,7 +16,7 @@
#include <LibCore/EventLoop.h>
#include <LibCore/Platform/ThreadQoS.h>
#include <LibCore/Timer.h>
-#include <LibCrypto/Hash/SHA2.h>
+#include <LibCrypto/Hash/Hash.h>
#include <LibHTTP/HeaderList.h>
#include <LibRequests/Request.h>
#include <LibRequests/RequestClient.h>
@@ -52,7 +52,7 @@ static Optional<JsonObject> parse_json_object(StringView text)
static ByteString sha256_hex(ReadonlyBytes bytes)
{
- return encode_hex(Crypto::Hash::SHA256::hash(bytes).bytes());
+ return encode_hex(Crypto::Hash::digest(Crypto::Hash::HashKind::SHA256, bytes).bytes());
}
void ReportSubmission::Contents::add(StringView key, StringView type, JsonValue value)
@@ -149,7 +149,7 @@ static ByteString build_multipart_body(StringView manifest, ReadonlySpan<ReportS
static Optional<u64> solve_challenge(ByteString const& token, u64 expected_work, Atomic<bool> const& cancelled)
{
- auto seed = Crypto::Hash::SHA256::hash(token.bytes());
+ auto seed = Crypto::Hash::digest(Crypto::Hash::HashKind::SHA256, token.bytes());
Array<u8, 40> candidate {};
seed.bytes().copy_to(candidate.span().slice(0, 32));
auto threshold = NumericLimits<u64>::max() / expected_work;
@@ -159,7 +159,7 @@ static Optional<u64> solve_challenge(ByteString const& token, u64 expected_work,
return {};
for (size_t i = 0; i < 8; ++i)
candidate[32 + i] = static_cast<u8>(nonce >> (i * 8));
- auto digest = Crypto::Hash::SHA256::hash(candidate.span());
+ auto digest = Crypto::Hash::digest(Crypto::Hash::HashKind::SHA256, candidate.span());
u64 prefix = 0;
for (size_t i = 0; i < 8; ++i)
prefix = prefix << 8 | digest.bytes()[i];
diff --git a/Libraries/LibWebView/WebUI/CrashReportsUI.cpp b/Libraries/LibWebView/WebUI/CrashReportsUI.cpp
index 579a3f8d235..2494b699b0c 100644
--- a/Libraries/LibWebView/WebUI/CrashReportsUI.cpp
+++ b/Libraries/LibWebView/WebUI/CrashReportsUI.cpp
@@ -8,7 +8,7 @@
#include <AK/Hex.h>
#include <AK/JsonArray.h>
#include <AK/JsonObject.h>
-#include <LibCrypto/Hash/SHA2.h>
+#include <LibCrypto/Hash/Hash.h>
#include <LibURL/Parser.h>
#include <LibWebView/CrashReportDiagnostics.h>
#include <LibWebView/CrashReportStore.h>
@@ -121,7 +121,7 @@ void CrashReportsUI::load_reports()
auto index = group_indices.ensure(signature, [&] {
groups.append({
- .id = encode_hex(Crypto::Hash::SHA256::hash(signature.bytes()).bytes()),
+ .id = encode_hex(Crypto::Hash::digest(Crypto::Hash::HashKind::SHA256, signature.bytes()).bytes()),
.process = process,
.last_crashed_at = crashed_at,
.names = {},
diff --git a/Tests/LibCrypto/TestHash.cpp b/Tests/LibCrypto/TestHash.cpp
index ff2212ff1f4..5b9a99ff86f 100644
--- a/Tests/LibCrypto/TestHash.cpp
+++ b/Tests/LibCrypto/TestHash.cpp
@@ -5,8 +5,7 @@
*/
#include <LibCrypto/Authentication/HMAC.h>
-#include <LibCrypto/Hash/SHA1.h>
-#include <LibCrypto/Hash/SHA2.h>
+#include <LibCrypto/Hash/Hash.h>
#include <LibTest/TestCase.h>
#include <cstring>
@@ -15,22 +14,22 @@ TEST_CASE(test_SHA1_hash_successive_updates)
u8 result[] {
0xd6, 0x6e, 0xce, 0xd1, 0xf4, 0x08, 0xc6, 0xd8, 0x35, 0xab, 0xf0, 0xc9, 0x05, 0x26, 0xa4, 0xb2, 0xb8, 0xa3, 0x7c, 0xd3
};
- auto hasher = Crypto::Hash::SHA1::create();
- hasher->update("aaaaaaaaaaaaaaa"sv);
- hasher->update("aaaaaaaaaaaaaaa"sv);
- hasher->update("aaaaaaaaaaaaaaa"sv);
- hasher->update("aaaaaaaaaaaaaaa"sv);
- hasher->update("aaaaaaaaaaaaaaa"sv);
- hasher->update("aaaaaaaaaaaaaaa"sv);
- hasher->update("aaaaaaaaaaaaaaa"sv);
- hasher->update("aaaaaaaaaaaaaaa"sv);
- hasher->update("aaaaaaaaaaaaaaa"sv);
- hasher->update("aaaaaaaaaaaaaaa"sv);
- hasher->update("aaaaaaaaaaaaaaa"sv);
- hasher->update("aaaaaaaaaaaaaaa"sv);
- hasher->update("aaaaaaaaa"sv);
- auto digest = hasher->digest();
- EXPECT(memcmp(result, digest.data, Crypto::Hash::SHA1::digest_size()) == 0);
+ Crypto::Hash::Hasher hasher { Crypto::Hash::HashKind::SHA1 };
+ hasher.update("aaaaaaaaaaaaaaa"sv);
+ hasher.update("aaaaaaaaaaaaaaa"sv);
+ hasher.update("aaaaaaaaaaaaaaa"sv);
+ hasher.update("aaaaaaaaaaaaaaa"sv);
+ hasher.update("aaaaaaaaaaaaaaa"sv);
+ hasher.update("aaaaaaaaaaaaaaa"sv);
+ hasher.update("aaaaaaaaaaaaaaa"sv);
+ hasher.update("aaaaaaaaaaaaaaa"sv);
+ hasher.update("aaaaaaaaaaaaaaa"sv);
+ hasher.update("aaaaaaaaaaaaaaa"sv);
+ hasher.update("aaaaaaaaaaaaaaa"sv);
+ hasher.update("aaaaaaaaaaaaaaa"sv);
+ hasher.update("aaaaaaaaa"sv);
+ auto digest = hasher.finish();
+ EXPECT_EQ(digest.bytes(), ReadonlyBytes { result });
}
TEST_CASE(test_SHA256_hash_string)
@@ -38,8 +37,8 @@ TEST_CASE(test_SHA256_hash_string)
u8 result[] {
0x9a, 0xcd, 0x50, 0xf9, 0xa2, 0xaf, 0x37, 0xe4, 0x71, 0xf7, 0x61, 0xc3, 0xfe, 0x7b, 0x8d, 0xea, 0x56, 0x17, 0xe5, 0x1d, 0xac, 0x80, 0x2f, 0xe6, 0xc1, 0x77, 0xb7, 0x4a, 0xbf, 0x0a, 0xbb, 0x5a
};
- auto digest = Crypto::Hash::SHA256::hash("Well hello friends"sv);
- EXPECT(memcmp(result, digest.data, Crypto::Hash::SHA256::digest_size()) == 0);
+ auto digest = Crypto::Hash::digest(Crypto::Hash::HashKind::SHA256, "Well hello friends"sv.bytes());
+ EXPECT_EQ(digest.bytes(), ReadonlyBytes { result });
}
TEST_CASE(test_SHA256_hash_split_into_blocks)
@@ -47,8 +46,8 @@ TEST_CASE(test_SHA256_hash_split_into_blocks)
u8 result[] {
0x1b, 0x90, 0x74, 0xf9, 0x9b, 0xf3, 0x56, 0x20, 0xd3, 0xf6, 0xbf, 0x7d, 0x72, 0x53, 0xb3, 0x3c, 0xd1, 0x1b, 0x7d, 0xd8, 0xa9, 0xa0, 0x4e, 0x55, 0xf5, 0x72, 0x43, 0x94, 0xaf, 0x27, 0x6c, 0x5c
};
- auto digest = Crypto::Hash::SHA256::hash("0123456789012345678901234567890123456789012345678901234567890123abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcd"sv);
- EXPECT(memcmp(result, digest.data, Crypto::Hash::SHA256::digest_size()) == 0);
+ auto digest = Crypto::Hash::digest(Crypto::Hash::HashKind::SHA256, "0123456789012345678901234567890123456789012345678901234567890123abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcd"sv.bytes());
+ EXPECT_EQ(digest.bytes(), ReadonlyBytes { result });
}
TEST_CASE(test_SHA3_256_hash_split_into_blocks)
@@ -56,6 +55,6 @@ 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);
+ auto digest = Crypto::Hash::digest(Crypto::Hash::HashKind::SHA3_256, "0123456789012345678901234567890123456789012345678901234567890123abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcd"sv.bytes());
+ EXPECT_EQ(digest.bytes(), ReadonlyBytes { result });
}
diff --git a/Tests/LibCrypto/TestRSA.cpp b/Tests/LibCrypto/TestRSA.cpp
index 334d4c9d2c0..c1134db9558 100644
--- a/Tests/LibCrypto/TestRSA.cpp
+++ b/Tests/LibCrypto/TestRSA.cpp
@@ -7,7 +7,7 @@
#include <AK/Base64.h>
#include <LibCrypto/Certificate/Certificate.h>
-#include <LibCrypto/Hash/SHA2.h>
+#include <LibCrypto/Hash/Hash.h>
#include <LibCrypto/PK/PK.h>
#include <LibCrypto/PK/RSA.h>
#include <LibTest/TestCase.h>
@@ -21,7 +21,7 @@ static ByteBuffer operator""_b(char const* string, size_t length)
TEST_CASE(test_RSA_rejects_invalid_public_key)
{
Array<u8, 1> message { 42 };
- auto digest = Crypto::Hash::SHA256::hash(message);
+ auto digest = Crypto::Hash::digest(Crypto::Hash::HashKind::SHA256, message);
Array<u8, 128> modulus;
modulus.fill(0xff);
@@ -36,7 +36,7 @@ TEST_CASE(test_RSA_rejects_invalid_public_key)
auto separator = encoded_message.size() - digest_info_prefix.size() - digest.bytes().size() - 1;
encoded_message[separator] = 0;
__builtin_memcpy(encoded_message.data() + separator + 1, digest_info_prefix.data(), digest_info_prefix.size());
- __builtin_memcpy(encoded_message.data() + separator + 1 + digest_info_prefix.size(), digest.data, digest.bytes().size());
+ __builtin_memcpy(encoded_message.data() + separator + 1 + digest_info_prefix.size(), digest.bytes().data(), digest.bytes().size());
Crypto::PK::RSAPublicKey invalid_key { Crypto::UnsignedBigInteger::import_data(modulus), 1 };
Crypto::PK::RSA_PKCS1_EMSA verifier { Crypto::Hash::HashKind::SHA256, invalid_key };
@@ -169,7 +169,7 @@ TEST_CASE(test_RSA_rejects_exponent_one)
// With e = 1, the EMSA-PKCS1-v1_5 encoding of the message is its own signature.
ByteBuffer message { "message"_b };
- auto digest = Crypto::Hash::SHA256::hash(message);
+ auto digest = Crypto::Hash::digest(Crypto::Hash::HashKind::SHA256, message);
static constexpr Array<u8, 19> sha256_digest_info { 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20 };
auto encoded = TRY_OR_FAIL(ByteBuffer::create_zeroed(public_key.length()));
diff --git a/Tests/LibDNS/TestDNSSEC.cpp b/Tests/LibDNS/TestDNSSEC.cpp
index dfdb579ab95..b8aa962cabc 100644
--- a/Tests/LibDNS/TestDNSSEC.cpp
+++ b/Tests/LibDNS/TestDNSSEC.cpp
@@ -13,8 +13,7 @@
#include <LibCore/Socket.h>
#include <LibCore/UDPServer.h>
#include <LibCrypto/Curves/EdwardsCurve.h>
-#include <LibCrypto/Hash/SHA1.h>
-#include <LibCrypto/Hash/SHA2.h>
+#include <LibCrypto/Hash/Hash.h>
#include <LibDNS/Resolver.h>
#include <LibTest/TestCase.h>
@@ -62,7 +61,7 @@ static ByteBuffer nsec3_hash(DomainName const& name)
{
ByteBuffer input;
MUST(name.canonicalized().to_raw(input));
- return MUST(ByteBuffer::copy(Crypto::Hash::SHA1::hash(input).bytes()));
+ return MUST(ByteBuffer::copy(Crypto::Hash::digest(Crypto::Hash::HashKind::SHA1, input).bytes()));
}
static int compare_buffers(ByteBuffer const& a, ByteBuffer const& b)
@@ -123,7 +122,7 @@ struct Zone {
ByteBuffer input;
MUST(apex.canonicalized().to_raw(input));
MUST(dnskey.to_raw(input));
- return Records::DS { dnskey.calculated_key_tag, DNSSEC::Algorithm::ED25519, DNSSEC::DigestType::SHA256, MUST(ByteBuffer::copy(Crypto::Hash::SHA256::hash(input).bytes())) };
+ return Records::DS { dnskey.calculated_key_tag, DNSSEC::Algorithm::ED25519, DNSSEC::DigestType::SHA256, MUST(ByteBuffer::copy(Crypto::Hash::digest(Crypto::Hash::HashKind::SHA256, input).bytes())) };
}
void add(DomainName const& owner, ResourceType type, Record record, u32 ttl)
diff --git a/Tests/LibWebView/ReportServer.h b/Tests/LibWebView/ReportServer.h
index 72e202ef1cd..1a11eaa67ad 100644
--- a/Tests/LibWebView/ReportServer.h
+++ b/Tests/LibWebView/ReportServer.h
@@ -18,7 +18,7 @@
#include <AK/Vector.h>
#include <LibCore/Socket.h>
#include <LibCore/TCPServer.h>
-#include <LibCrypto/Hash/SHA2.h>
+#include <LibCrypto/Hash/Hash.h>
#include <LibWebView/Application.h>
namespace ReportServerTest {
@@ -144,7 +144,7 @@ private:
inline ByteString sha256_hex(StringView text)
{
- return encode_hex(Crypto::Hash::SHA256::hash(text.bytes()).bytes());
+ return encode_hex(Crypto::Hash::digest(Crypto::Hash::HashKind::SHA256, text.bytes()).bytes());
}
// The content of the multipart part with the given name.