Skip to content

Truly independent web browser

LibCrypto: Name each hash kind for OpenSSL in one place

Shannon Booth3d3eb2f2174f
diff --git a/Libraries/LibCrypto/Authentication/HMAC.cpp b/Libraries/LibCrypto/Authentication/HMAC.cpp
index 3fbf470a1d8..5b0fb365c03 100644
--- a/Libraries/LibCrypto/Authentication/HMAC.cpp
+++ b/Libraries/LibCrypto/Authentication/HMAC.cpp
@@ -54,10 +54,10 @@ void HMAC::reset()
EVP_MAC_CTX_free(m_ctx);
m_ctx = EVP_MAC_CTX_new(m_mac);
- auto hash_name = MUST(hash_kind_to_openssl_digest_name(m_hash_kind));
+ auto const* hash_name = MUST(Hash::openssl_digest_name(m_hash_kind));
OSSL_PARAM params[] = {
- OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST, const_cast<char*>(hash_name.characters_without_null_termination()), hash_name.length()),
+ OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST, const_cast<char*>(hash_name), strlen(hash_name)),
OSSL_PARAM_END
};
diff --git a/Libraries/LibCrypto/Authentication/HMAC.h b/Libraries/LibCrypto/Authentication/HMAC.h
index d6065cb095e..304c0e35de7 100644
--- a/Libraries/LibCrypto/Authentication/HMAC.h
+++ b/Libraries/LibCrypto/Authentication/HMAC.h
@@ -39,16 +39,6 @@ public:
void reset();
- ByteString class_name() const
- {
- auto hash_name = MUST(hash_kind_to_openssl_digest_name(m_hash_kind));
-
- StringBuilder builder;
- builder.append("HMAC-"sv);
- builder.append(hash_name);
- return builder.to_byte_string();
- }
-
private:
Hash::HashKind m_hash_kind;
ReadonlyBytes m_key;
diff --git a/Libraries/LibCrypto/CMakeLists.txt b/Libraries/LibCrypto/CMakeLists.txt
index c05c1cdffeb..7daa1112fe5 100644
--- a/Libraries/LibCrypto/CMakeLists.txt
+++ b/Libraries/LibCrypto/CMakeLists.txt
@@ -15,6 +15,7 @@ set(SOURCES
Curves/EdwardsCurve.cpp
Curves/SECPxxxr1.cpp
Hash/Argon2.cpp
+ Hash/Hash.cpp
Hash/SHAKE.cpp
Hash/HKDF.cpp
Hash/PBKDF2.cpp
diff --git a/Libraries/LibCrypto/Hash/HKDF.cpp b/Libraries/LibCrypto/Hash/HKDF.cpp
index b6cafbdf7b6..02a7032db1e 100644
--- a/Libraries/LibCrypto/Hash/HKDF.cpp
+++ b/Libraries/LibCrypto/Hash/HKDF.cpp
@@ -21,12 +21,12 @@ HKDF::HKDF(HashKind hash_kind)
ErrorOr<ByteBuffer> HKDF::derive_key(Optional<ReadonlyBytes> maybe_salt, ReadonlyBytes key, ReadonlyBytes info, u32 key_length_bytes)
{
- auto hash_name = TRY(hash_kind_to_openssl_digest_name(m_hash_kind));
+ auto const* hash_name = TRY(openssl_digest_name(m_hash_kind));
auto ctx = TRY(OpenSSL_KDF_CTX::wrap(EVP_KDF_CTX_new(m_kdf)));
OSSL_PARAM params[] = {
- OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, const_cast<char*>(hash_name.characters_without_null_termination()), hash_name.length()),
+ OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, const_cast<char*>(hash_name), strlen(hash_name)),
OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, const_cast<u8*>(key.data()), key.size()),
OSSL_PARAM_octet_string(OSSL_KDF_PARAM_INFO, const_cast<u8*>(info.data()), info.size()),
OSSL_PARAM_END,
diff --git a/Libraries/LibCrypto/Hash/Hash.cpp b/Libraries/LibCrypto/Hash/Hash.cpp
new file mode 100644
index 00000000000..ec4761ac583
--- /dev/null
+++ b/Libraries/LibCrypto/Hash/Hash.cpp
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibCrypto/Hash/Hash.h>
+
+namespace Crypto::Hash {
+
+ErrorOr<char const*> openssl_digest_name(HashKind kind)
+{
+ switch (kind) {
+ case HashKind::SHA1:
+ return "SHA1";
+ case HashKind::SHA256:
+ return "SHA256";
+ case HashKind::SHA384:
+ return "SHA384";
+ case HashKind::SHA512:
+ return "SHA512";
+ case HashKind::SHA3_256:
+ return "SHA3-256";
+ case HashKind::SHA3_384:
+ return "SHA3-384";
+ case HashKind::SHA3_512:
+ return "SHA3-512";
+ case HashKind::Unknown:
+ case HashKind::None:
+ break;
+ }
+ return Error::from_string_literal("Unsupported hash kind");
+}
+
+}
diff --git a/Libraries/LibCrypto/Hash/Hash.h b/Libraries/LibCrypto/Hash/Hash.h
new file mode 100644
index 00000000000..014cf299d1a
--- /dev/null
+++ b/Libraries/LibCrypto/Hash/Hash.h
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Error.h>
+#include <AK/Types.h>
+
+namespace Crypto::Hash {
+
+// NB: libcrypto_rust passes these values across the FFI.
+enum class HashKind {
+ Unknown,
+ None,
+ SHA1,
+ SHA256,
+ SHA384,
+ SHA512,
+ SHA3_256,
+ SHA3_384,
+ SHA3_512
+};
+
+ErrorOr<char const*> openssl_digest_name(HashKind);
+
+}
diff --git a/Libraries/LibCrypto/Hash/HashManager.h b/Libraries/LibCrypto/Hash/HashManager.h
index 237292928b8..8cab7eb5972 100644
--- a/Libraries/LibCrypto/Hash/HashManager.h
+++ b/Libraries/LibCrypto/Hash/HashManager.h
@@ -9,6 +9,7 @@
#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>
@@ -16,18 +17,6 @@
namespace Crypto::Hash {
-enum class HashKind {
- Unknown,
- None,
- SHA1,
- SHA256,
- SHA384,
- SHA512,
- SHA3_256,
- SHA3_384,
- SHA3_512
-};
-
struct MultiHashDigestVariant {
constexpr static size_t Size = 0;
diff --git a/Libraries/LibCrypto/Hash/PBKDF2.cpp b/Libraries/LibCrypto/Hash/PBKDF2.cpp
index 803218ddabe..a4f0aea4987 100644
--- a/Libraries/LibCrypto/Hash/PBKDF2.cpp
+++ b/Libraries/LibCrypto/Hash/PBKDF2.cpp
@@ -21,12 +21,12 @@ PBKDF2::PBKDF2(HashKind hash_kind)
ErrorOr<ByteBuffer> PBKDF2::derive_key(ReadonlyBytes password, ReadonlyBytes salt, u32 iterations, u32 key_length_bytes)
{
- auto hash_name = TRY(hash_kind_to_openssl_digest_name(m_hash_kind));
+ auto const* hash_name = TRY(openssl_digest_name(m_hash_kind));
auto ctx = TRY(OpenSSL_KDF_CTX::wrap(EVP_KDF_CTX_new(m_kdf)));
OSSL_PARAM params[] = {
- OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, const_cast<char*>(hash_name.characters_without_null_termination()), hash_name.length()),
+ OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, const_cast<char*>(hash_name), strlen(hash_name)),
OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PASSWORD, const_cast<u8*>(password.data()), password.size()),
OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, const_cast<u8*>(salt.data()), salt.size()),
OSSL_PARAM_uint32(OSSL_KDF_PARAM_ITER, &iterations),
diff --git a/Libraries/LibCrypto/OpenSSL.cpp b/Libraries/LibCrypto/OpenSSL.cpp
index 9a33f92b006..dd3ff05e3fe 100644
--- a/Libraries/LibCrypto/OpenSSL.cpp
+++ b/Libraries/LibCrypto/OpenSSL.cpp
@@ -49,22 +49,6 @@ ErrorOr<UnsignedBigInteger> openssl_bignum_to_unsigned_big_integer(OpenSSL_BN co
return UnsignedBigInteger::import_data(buf);
}
-ErrorOr<StringView> hash_kind_to_openssl_digest_name(Hash::HashKind hash)
-{
- switch (hash) {
- case Hash::HashKind::SHA1:
- return "SHA1"sv;
- case Hash::HashKind::SHA256:
- return "SHA256"sv;
- case Hash::HashKind::SHA384:
- return "SHA384"sv;
- case Hash::HashKind::SHA512:
- return "SHA512"sv;
- default:
- return Error::from_string_literal("Unsupported hash kind");
- }
-}
-
ErrorOr<ByteBuffer> get_byte_buffer_param_from_key(OpenSSL_PKEY& key, char const* key_name)
{
size_t size;
diff --git a/Libraries/LibCrypto/OpenSSL.h b/Libraries/LibCrypto/OpenSSL.h
index b62d134ea64..1e52ea83fda 100644
--- a/Libraries/LibCrypto/OpenSSL.h
+++ b/Libraries/LibCrypto/OpenSSL.h
@@ -9,7 +9,7 @@
#include <AK/Error.h>
#include <AK/Format.h>
#include <LibCrypto/BigInt/UnsignedBigInteger.h>
-#include <LibCrypto/Hash/HashManager.h>
+#include <LibCrypto/Hash/Hash.h>
#include <LibCrypto/OpenSSLForward.h>
inline int openssl_print_errors(char const* str, size_t len, [[maybe_unused]] void* u)
@@ -135,8 +135,6 @@ public:
ErrorOr<OpenSSL_BN> unsigned_big_integer_to_openssl_bignum(UnsignedBigInteger const& integer);
ErrorOr<UnsignedBigInteger> openssl_bignum_to_unsigned_big_integer(OpenSSL_BN const& bn);
-ErrorOr<StringView> hash_kind_to_openssl_digest_name(Hash::HashKind hash);
-
ErrorOr<ByteBuffer> get_byte_buffer_param_from_key(OpenSSL_PKEY& key, char const* key_name);
}
diff --git a/Libraries/LibCrypto/PK/RSA.cpp b/Libraries/LibCrypto/PK/RSA.cpp
index 19aea8c8567..e17735b4801 100644
--- a/Libraries/LibCrypto/PK/RSA.cpp
+++ b/Libraries/LibCrypto/PK/RSA.cpp
@@ -400,20 +400,7 @@ ErrorOr<ByteBuffer> RSA::decrypt(ReadonlyBytes in)
ErrorOr<EVP_MD const*> hash_kind_to_hash_type(Hash::HashKind hash_kind)
{
- switch (hash_kind) {
- case Hash::HashKind::None:
- return nullptr;
- case Hash::HashKind::SHA1:
- return EVP_sha1();
- case Hash::HashKind::SHA256:
- return EVP_sha256();
- case Hash::HashKind::SHA384:
- return EVP_sha384();
- case Hash::HashKind::SHA512:
- return EVP_sha512();
- default:
- return Error::from_string_literal("Unsupported hash kind");
- }
+ return OPENSSL_TRY_PTR(EVP_get_digestbyname(TRY(Hash::openssl_digest_name(hash_kind))));
}
ErrorOr<bool> RSA_EMSA::verify(ReadonlyBytes message, ReadonlyBytes signature)