Truly independent web browser
LibCrypto: Compute HMACs with a single EVP_Q_mac call
Shannon Booth418ad13f7620
diff --git a/Libraries/LibCrypto/Authentication/HMAC.cpp b/Libraries/LibCrypto/Authentication/HMAC.cppindex 5b0fb365c03..cb464bd0b92 100644--- a/Libraries/LibCrypto/Authentication/HMAC.cpp+++ b/Libraries/LibCrypto/Authentication/HMAC.cpp@@ -5,65 +5,23 @@*/#include <LibCrypto/Authentication/HMAC.h>+#include <LibCrypto/OpenSSL.h>-#include <openssl/core_names.h>#include <openssl/evp.h>namespace Crypto::Authentication {-HMAC::HMAC(Hash::HashKind hash_kind, ReadonlyBytes key)- : m_hash_kind(hash_kind)- , m_key(key)- , m_mac(EVP_MAC_fetch(nullptr, "HMAC", nullptr))+ErrorOr<Hash::Digest> hmac(Hash::HashKind hash_kind, ReadonlyBytes key, ReadonlyBytes message){- reset();-}--HMAC::~HMAC()-{- EVP_MAC_free(m_mac);- EVP_MAC_CTX_free(m_ctx);-}--size_t HMAC::digest_size() const-{- return EVP_MAC_CTX_get_mac_size(m_ctx);-}--void HMAC::update(u8 const* message, size_t length)-{- if (EVP_MAC_update(m_ctx, message, length) != 1) {- VERIFY_NOT_REACHED();- }-}--ByteBuffer HMAC::digest()-{- auto buf = MUST(ByteBuffer::create_uninitialized(digest_size()));-- auto size = digest_size();- if (EVP_MAC_final(m_ctx, buf.data(), &size, size) != 1) {- VERIFY_NOT_REACHED();- }-- return MUST(buf.slice(0, size));-}--void HMAC::reset()-{- EVP_MAC_CTX_free(m_ctx);- m_ctx = EVP_MAC_CTX_new(m_mac);-- auto const* hash_name = MUST(Hash::openssl_digest_name(m_hash_kind));+ auto const* hash_name = TRY(Hash::openssl_digest_name(hash_kind));- OSSL_PARAM params[] = {- OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST, const_cast<char*>(hash_name), strlen(hash_name)),- OSSL_PARAM_END- };+ // NB: OpenSSL rejects a null key pointer, which an empty key may have.+ static constexpr u8 empty_key[1] {};+ auto const* key_data = key.is_empty() ? empty_key : key.data();- if (EVP_MAC_init(m_ctx, m_key.data(), m_key.size(), params) != 1) {- VERIFY_NOT_REACHED();- }+ Hash::Digest result;+ OPENSSL_TRY_PTR(EVP_Q_mac(nullptr, "HMAC", nullptr, hash_name, nullptr, key_data, key.size(), message.data(), message.size(), result.data.data(), result.data.size(), &result.size));+ return result;}}diff --git a/Libraries/LibCrypto/Authentication/HMAC.h b/Libraries/LibCrypto/Authentication/HMAC.hindex 13aee656cb8..9ec16e4b918 100644--- a/Libraries/LibCrypto/Authentication/HMAC.h+++ b/Libraries/LibCrypto/Authentication/HMAC.h@@ -7,43 +7,11 @@#pragma once-#include <AK/ByteBuffer.h>-#include <AK/ByteString.h>+#include <AK/Error.h>#include <LibCrypto/Hash/Hash.h>-#include <LibCrypto/OpenSSL.h>-#include <LibCrypto/OpenSSLForward.h>namespace Crypto::Authentication {-class HMAC {-public:- explicit HMAC(Hash::HashKind hash, ReadonlyBytes key);- ~HMAC();-- size_t digest_size() const;-- void update(u8 const* message, size_t length);- void update(ReadonlyBytes span) { return update(span.data(), span.size()); }- void update(StringView string) { return update((u8 const*)string.characters_without_null_termination(), string.length()); }-- ByteBuffer process(u8 const* message, size_t length)- {- reset();- update(message, length);- return digest();- }- ByteBuffer process(ReadonlyBytes span) { return process(span.data(), span.size()); }- ByteBuffer process(StringView string) { return process((u8 const*)string.characters_without_null_termination(), string.length()); }-- ByteBuffer digest();-- void reset();--private:- Hash::HashKind m_hash_kind;- ReadonlyBytes m_key;- EVP_MAC* m_mac { nullptr };- EVP_MAC_CTX* m_ctx { nullptr };-};+ErrorOr<Hash::Digest> hmac(Hash::HashKind, ReadonlyBytes key, ReadonlyBytes message);}diff --git a/Libraries/LibCrypto/Rust/src/ffi.rs b/Libraries/LibCrypto/Rust/src/ffi.rsindex 0979778a481..5a826dedc1a 100644--- a/Libraries/LibCrypto/Rust/src/ffi.rs+++ b/Libraries/LibCrypto/Rust/src/ffi.rs@@ -70,7 +70,7 @@ unsafe extern "C" {message: Span<'_, u8>,state: *mut c_void,receive: ReceiveBytes,- ) -> ();+ ) -> bool;pub(super) fn crypto_kmac_default_key_length(kind: KmacKind) -> u32;pub(super) fn crypto_kmac_validate_parameters(output_length: u32, customization: OptionalBytes<'_>) -> bool;pub(super) fn crypto_kmac_sign(diff --git a/Libraries/LibCrypto/Rust/src/lib.rs b/Libraries/LibCrypto/Rust/src/lib.rsindex 6267278d5a3..8c6c3951b61 100644--- a/Libraries/LibCrypto/Rust/src/lib.rs+++ b/Libraries/LibCrypto/Rust/src/lib.rs@@ -196,12 +196,15 @@ pub fn cshake_digest(Ok(bytes)}-pub fn hmac_sign(kind: ShaHashKind, key: &[u8], message: &[u8]) -> Vec<u8> {+pub fn hmac_sign(kind: ShaHashKind, key: &[u8], message: &[u8]) -> Result<Vec<u8>, BackendError> {// SAFETY: Inputs and output are borrowed for the synchronous native call.- let ((), bytes) = receive_native_bytes(|state, receive| unsafe {- ffi::crypto_hmac_sign(kind, Span::new(key), Span::new(message), state, receive);+ let (succeeded, bytes) = receive_native_bytes(|state, receive| unsafe {+ ffi::crypto_hmac_sign(kind, Span::new(key), Span::new(message), state, receive)});- bytes+ if !succeeded {+ return Err(BackendError);+ }+ Ok(bytes)}pub fn timing_safe_compare(left: &[u8], right: &[u8]) -> bool {diff --git a/Libraries/LibCrypto/RustBindings.cpp b/Libraries/LibCrypto/RustBindings.cppindex c031a9c42cd..9bda4e3d25e 100644--- a/Libraries/LibCrypto/RustBindings.cpp+++ b/Libraries/LibCrypto/RustBindings.cpp@@ -783,14 +783,16 @@ bool crypto_okp_export_der(OkpCurve curve, bool is_public, ReadonlyBytes key, vo}extern "C" {-void crypto_hmac_sign(::Crypto::Hash::HashKind, ReadonlyBytes, ReadonlyBytes, void*, ReceiveEncodedBytes);+bool crypto_hmac_sign(::Crypto::Hash::HashKind, ReadonlyBytes, ReadonlyBytes, void*, ReceiveEncodedBytes);}-void crypto_hmac_sign(::Crypto::Hash::HashKind hash_kind, ReadonlyBytes key, ReadonlyBytes message, void* state, ReceiveEncodedBytes receive)+bool crypto_hmac_sign(::Crypto::Hash::HashKind hash_kind, ReadonlyBytes key, ReadonlyBytes message, void* state, ReceiveEncodedBytes receive){- ::Crypto::Authentication::HMAC hmac(hash_kind, key);- auto mac = hmac.process(message);- receive(state, mac);+ auto mac = ::Crypto::Authentication::hmac(hash_kind, key, message);+ if (mac.is_error())+ return false;+ receive(state, mac.value().bytes());+ return true;}extern "C" {diff --git a/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs b/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rsindex e7e0ba583a5..4b574c3a4cc 100644--- a/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs@@ -760,8 +760,10 @@ fn hmac_calculate_message_digest(hash_name: &Utf16String, key: &[u8], message: &));};- let mac = Bytes::new(libcrypto_rust::hmac_sign(hash_kind, key, message));- Ok(mac)+ let Ok(mac) = libcrypto_rust::hmac_sign(hash_kind, key, message) else {+ return Err(dom_error("OperationError", b"Failed to compute the HMAC"));+ };+ Ok(Bytes::new(mac))}fn hmac_hash_block_size(hash: &HashAlgorithmIdentifier) -> WebIdlResult<u32> {diff --git a/Libraries/LibWebView/RequestServerManager.cpp b/Libraries/LibWebView/RequestServerManager.cppindex 90b2f9fd685..4d2edcb5502 100644--- a/Libraries/LibWebView/RequestServerManager.cpp+++ b/Libraries/LibWebView/RequestServerManager.cpp@@ -279,9 +279,8 @@ ByteString RequestServerManager::sites_directory() const// itself would tell it which other sites the user has visited.ByteString RequestServerManager::cache_path_for_site(Utf16String const& site) const{- Crypto::Authentication::HMAC hmac(Crypto::Hash::HashKind::SHA256, m_site_cache_key);auto site_bytes = site.to_byte_string();- auto digest = hmac.process(site_bytes.view());+ auto digest = MUST(Crypto::Authentication::hmac(Crypto::Hash::HashKind::SHA256, m_site_cache_key, site_bytes.bytes()));return LexicalPath::join(sites_directory(), encode_hex(digest.bytes())).string();}diff --git a/Tests/LibCrypto/TestHMAC.cpp b/Tests/LibCrypto/TestHMAC.cppindex a2e4281dd38..049d09b342d 100644--- a/Tests/LibCrypto/TestHMAC.cpp+++ b/Tests/LibCrypto/TestHMAC.cpp@@ -16,43 +16,38 @@ static ByteBuffer operator""_b(char const* string, size_t length)TEST_CASE(test_hmac_sha256_process){auto key = "Well Hello Friends"_b;- Crypto::Authentication::HMAC hmac(Crypto::Hash::HashKind::SHA256, key);u8 result[] {0x1a, 0xf2, 0x20, 0x62, 0xde, 0x3b, 0x84, 0x65, 0xc1, 0x25, 0x23, 0x99, 0x76, 0x15, 0x1b, 0xec, 0x15, 0x21, 0x82, 0x1f, 0x23, 0xca, 0x11, 0x66, 0xdd, 0x8c, 0x6e, 0xf1, 0x81, 0x3b, 0x7f, 0x1b};- auto mac = hmac.process("Some bogus data"sv);- EXPECT(memcmp(result, mac.data(), hmac.digest_size()) == 0);-}--TEST_CASE(test_hmac_sha256_reuse)-{- auto key = "Well Hello Friends"_b;- Crypto::Authentication::HMAC hmac(Crypto::Hash::HashKind::SHA256, key);-- auto mac_0 = hmac.process("Some bogus data"sv);- auto mac_1 = hmac.process("Some bogus data"sv);-- EXPECT(memcmp(mac_0.data(), mac_1.data(), hmac.digest_size()) == 0);+ auto mac = TRY_OR_FAIL(Crypto::Authentication::hmac(Crypto::Hash::HashKind::SHA256, key, "Some bogus data"sv.bytes()));+ EXPECT_EQ(mac.bytes(), ReadonlyBytes { result });}TEST_CASE(test_hmac_sha256_data_is_same_size_as_block){auto key = "Well Hello Friends"_b;- Crypto::Authentication::HMAC hmac(Crypto::Hash::HashKind::SHA256, key);u8 result[] = {0x1d, 0x90, 0xce, 0x68, 0x45, 0x0b, 0xba, 0xd6, 0xbe, 0x1c, 0xb2, 0x3a, 0xea, 0x7f, 0xac, 0x4b, 0x68, 0x08, 0xa4, 0x77, 0x81, 0x2a, 0xad, 0x5d, 0x05, 0xe2, 0x15, 0xe8, 0xf4, 0xcb, 0x06, 0xaf};- auto mac = hmac.process("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"sv);- EXPECT(memcmp(result, mac.data(), hmac.digest_size()) == 0);+ auto mac = TRY_OR_FAIL(Crypto::Authentication::hmac(Crypto::Hash::HashKind::SHA256, key, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"sv.bytes()));+ EXPECT_EQ(mac.bytes(), ReadonlyBytes { result });}TEST_CASE(test_hmac_sha256_data_is_bigger_size_as_block){auto key = "Well Hello Friends"_b;- Crypto::Authentication::HMAC hmac(Crypto::Hash::HashKind::SHA256, key);u8 result[] = {0x9b, 0xa3, 0x9e, 0xf3, 0xb4, 0x30, 0x5f, 0x6f, 0x67, 0xd0, 0xa8, 0xb0, 0xf0, 0xcb, 0x12, 0xf5, 0x85, 0xe2, 0x19, 0xba, 0x0c, 0x8b, 0xe5, 0x43, 0xf0, 0x93, 0x39, 0xa8, 0xa3, 0x07, 0xf1, 0x95};- auto mac = hmac.process("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"sv);- EXPECT(memcmp(result, mac.data(), hmac.digest_size()) == 0);+ auto mac = TRY_OR_FAIL(Crypto::Authentication::hmac(Crypto::Hash::HashKind::SHA256, key, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"sv.bytes()));+ EXPECT_EQ(mac.bytes(), ReadonlyBytes { result });+}++TEST_CASE(test_hmac_sha256_empty_key)+{+ u8 result[] {+ 0xb6, 0x13, 0x67, 0x9a, 0x08, 0x14, 0xd9, 0xec, 0x77, 0x2f, 0x95, 0xd7, 0x78, 0xc3, 0x5f, 0xc5, 0xff, 0x16, 0x97, 0xc4, 0x93, 0x71, 0x56, 0x53, 0xc6, 0xc7, 0x12, 0x14, 0x42, 0x92, 0xc5, 0xad+ };+ auto mac = TRY_OR_FAIL(Crypto::Authentication::hmac(Crypto::Hash::HashKind::SHA256, {}, {}));+ EXPECT_EQ(mac.bytes(), ReadonlyBytes { result });}diff --git a/Tests/LibCrypto/TestHash.cpp b/Tests/LibCrypto/TestHash.cppindex 5b9a99ff86f..411c74e0c7a 100644--- a/Tests/LibCrypto/TestHash.cpp+++ b/Tests/LibCrypto/TestHash.cpp@@ -4,7 +4,6 @@* SPDX-License-Identifier: BSD-2-Clause*/-#include <LibCrypto/Authentication/HMAC.h>#include <LibCrypto/Hash/Hash.h>#include <LibTest/TestCase.h>#include <cstring>