2020-04-08 01:54:50 +04:30
|
|
|
/*
|
2021-04-23 00:43:01 +04:30
|
|
|
* Copyright (c) 2020, Ali Mohammad Pur <mpfard@serenityos.org>
|
2025-02-23 12:10:27 +01:00
|
|
|
* Copyright (c) 2025, Altomani Gianluca <altomanigianluca@gmail.com>
|
2020-04-08 01:54:50 +04:30
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-08 01:54:50 +04:30
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <AK/ByteBuffer.h>
|
2024-06-17 23:12:53 +01:00
|
|
|
#include <AK/ByteString.h>
|
2025-02-23 12:10:27 +01:00
|
|
|
#include <LibCrypto/Hash/HashManager.h>
|
|
|
|
|
#include <LibCrypto/OpenSSL.h>
|
|
|
|
|
#include <LibCrypto/OpenSSLForward.h>
|
2020-04-08 01:54:50 +04:30
|
|
|
|
2024-07-06 23:12:39 +02:00
|
|
|
namespace Crypto::Authentication {
|
|
|
|
|
|
2020-04-23 03:03:05 +04:30
|
|
|
class HMAC {
|
|
|
|
|
public:
|
2025-02-23 12:10:27 +01:00
|
|
|
explicit HMAC(Hash::HashKind hash, ReadonlyBytes key);
|
|
|
|
|
~HMAC();
|
2020-04-25 05:06:33 +04:30
|
|
|
|
2025-02-23 12:10:27 +01:00
|
|
|
size_t digest_size() const;
|
2020-04-08 01:54:50 +04:30
|
|
|
|
2025-02-23 12:10:27 +01:00
|
|
|
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()); }
|
2020-04-08 01:54:50 +04:30
|
|
|
|
2025-02-23 12:10:27 +01:00
|
|
|
ByteBuffer process(u8 const* message, size_t length)
|
2020-04-23 03:03:05 +04:30
|
|
|
{
|
|
|
|
|
reset();
|
|
|
|
|
update(message, length);
|
|
|
|
|
return digest();
|
|
|
|
|
}
|
2025-02-23 12:10:27 +01:00
|
|
|
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()); }
|
2020-04-08 01:54:50 +04:30
|
|
|
|
2025-02-23 12:10:27 +01:00
|
|
|
ByteBuffer digest();
|
2020-04-23 03:03:05 +04:30
|
|
|
|
2025-02-23 12:10:27 +01:00
|
|
|
void reset();
|
2020-04-23 03:03:05 +04:30
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
ByteString class_name() const
|
2020-04-23 03:03:05 +04:30
|
|
|
{
|
2025-02-23 12:10:27 +01:00
|
|
|
auto hash_name = MUST(hash_kind_to_openssl_digest_name(m_hash_kind));
|
|
|
|
|
|
2020-04-23 03:03:05 +04:30
|
|
|
StringBuilder builder;
|
2022-07-11 17:32:29 +00:00
|
|
|
builder.append("HMAC-"sv);
|
2025-02-23 12:10:27 +01:00
|
|
|
builder.append(hash_name);
|
2023-12-16 17:49:34 +03:30
|
|
|
return builder.to_byte_string();
|
2020-04-23 03:03:05 +04:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2025-02-23 12:10:27 +01:00
|
|
|
Hash::HashKind m_hash_kind;
|
|
|
|
|
ReadonlyBytes m_key;
|
|
|
|
|
EVP_MAC* m_mac { nullptr };
|
|
|
|
|
EVP_MAC_CTX* m_ctx { nullptr };
|
2020-04-23 03:03:05 +04:30
|
|
|
};
|
2020-04-08 01:54:50 +04:30
|
|
|
|
|
|
|
|
}
|