2020-04-25 05:06:33 +04:30
|
|
|
/*
|
2021-04-23 00:43:01 +04:30
|
|
|
* Copyright (c) 2020, Ali Mohammad Pur <mpfard@serenityos.org>
|
2020-04-25 05:06:33 +04:30
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-25 05:06:33 +04:30
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Optional.h>
|
2020-04-25 05:07:24 +04:30
|
|
|
#include <AK/OwnPtr.h>
|
2021-05-18 20:15:20 +02:00
|
|
|
#include <AK/Variant.h>
|
2023-09-16 17:01:54 +04:00
|
|
|
#include <LibCrypto/Hash/BLAKE2b.h>
|
2020-04-25 05:06:33 +04:30
|
|
|
#include <LibCrypto/Hash/HashFunction.h>
|
|
|
|
#include <LibCrypto/Hash/MD5.h>
|
|
|
|
#include <LibCrypto/Hash/SHA1.h>
|
|
|
|
#include <LibCrypto/Hash/SHA2.h>
|
|
|
|
|
2023-07-11 13:49:08 -04:00
|
|
|
namespace Crypto::Hash {
|
2020-04-25 05:06:33 +04:30
|
|
|
|
|
|
|
enum class HashKind {
|
2023-04-12 21:35:06 +02:00
|
|
|
Unknown,
|
2020-04-25 05:06:33 +04:30
|
|
|
None,
|
2023-09-16 17:01:54 +04:00
|
|
|
BLAKE2b,
|
|
|
|
MD5,
|
2020-04-25 05:06:33 +04:30
|
|
|
SHA1,
|
|
|
|
SHA256,
|
2021-05-17 22:24:32 +02:00
|
|
|
SHA384,
|
2020-04-25 05:06:33 +04:30
|
|
|
SHA512,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct MultiHashDigestVariant {
|
|
|
|
constexpr static size_t Size = 0;
|
|
|
|
|
2021-05-18 20:41:00 +02:00
|
|
|
MultiHashDigestVariant(Empty digest)
|
|
|
|
: m_digest(move(digest))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-05-18 20:15:20 +02:00
|
|
|
MultiHashDigestVariant(MD5::DigestType digest)
|
|
|
|
: m_digest(move(digest))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-04-25 05:06:33 +04:30
|
|
|
MultiHashDigestVariant(SHA1::DigestType digest)
|
2021-05-18 20:15:20 +02:00
|
|
|
: m_digest(move(digest))
|
2020-04-25 05:06:33 +04:30
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
MultiHashDigestVariant(SHA256::DigestType digest)
|
2021-05-18 20:15:20 +02:00
|
|
|
: m_digest(move(digest))
|
2020-04-25 05:06:33 +04:30
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-05-17 22:24:32 +02:00
|
|
|
MultiHashDigestVariant(SHA384::DigestType digest)
|
2021-05-18 20:15:20 +02:00
|
|
|
: m_digest(move(digest))
|
2021-05-17 22:24:32 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-04-25 05:06:33 +04:30
|
|
|
MultiHashDigestVariant(SHA512::DigestType digest)
|
2021-05-18 20:15:20 +02:00
|
|
|
: m_digest(move(digest))
|
2020-04-25 05:06:33 +04:30
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
[[nodiscard]] u8 const* immutable_data() const
|
2020-04-25 05:06:33 +04:30
|
|
|
{
|
2021-05-20 00:32:42 +04:30
|
|
|
return m_digest.visit(
|
2022-04-01 20:58:27 +03:00
|
|
|
[&](Empty const&) -> u8 const* { VERIFY_NOT_REACHED(); },
|
|
|
|
[&](auto const& value) { return value.immutable_data(); });
|
2020-04-25 05:06:33 +04:30
|
|
|
}
|
|
|
|
|
2022-01-04 02:41:27 +01:00
|
|
|
[[nodiscard]] size_t data_length() const
|
2020-04-25 05:06:33 +04:30
|
|
|
{
|
2021-05-20 00:32:42 +04:30
|
|
|
return m_digest.visit(
|
2022-04-01 20:58:27 +03:00
|
|
|
[&](Empty const&) -> size_t { VERIFY_NOT_REACHED(); },
|
|
|
|
[&](auto const& value) { return value.data_length(); });
|
2020-04-25 05:06:33 +04:30
|
|
|
}
|
|
|
|
|
2022-01-04 02:34:00 +01:00
|
|
|
[[nodiscard]] ReadonlyBytes bytes() const
|
|
|
|
{
|
|
|
|
return m_digest.visit(
|
2022-04-01 20:58:27 +03:00
|
|
|
[&](Empty const&) -> ReadonlyBytes { VERIFY_NOT_REACHED(); },
|
|
|
|
[&](auto const& value) { return value.bytes(); });
|
2022-01-04 02:34:00 +01:00
|
|
|
}
|
|
|
|
|
2021-05-18 20:15:20 +02:00
|
|
|
using DigestVariant = Variant<Empty, MD5::DigestType, SHA1::DigestType, SHA256::DigestType, SHA384::DigestType, SHA512::DigestType>;
|
2021-09-19 23:00:45 +02:00
|
|
|
DigestVariant m_digest {};
|
2020-04-25 05:06:33 +04:30
|
|
|
};
|
|
|
|
|
2022-01-03 22:28:19 +01:00
|
|
|
class Manager final : public HashFunction<0, 0, MultiHashDigestVariant> {
|
2020-04-25 05:06:33 +04:30
|
|
|
public:
|
2020-12-19 15:56:15 +01:00
|
|
|
using HashFunction::update;
|
|
|
|
|
2020-04-25 05:06:33 +04:30
|
|
|
Manager()
|
|
|
|
{
|
2021-09-06 03:29:52 +04:30
|
|
|
m_pre_init_buffer = ByteBuffer();
|
2020-04-25 05:06:33 +04:30
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
Manager(Manager const& other) // NOT a copy constructor!
|
2020-04-25 05:06:33 +04:30
|
|
|
{
|
2021-09-06 03:29:52 +04:30
|
|
|
m_pre_init_buffer = ByteBuffer(); // will not be used
|
2020-04-25 05:07:24 +04:30
|
|
|
initialize(other.m_kind);
|
2020-04-25 05:06:33 +04:30
|
|
|
}
|
|
|
|
|
|
|
|
Manager(HashKind kind)
|
|
|
|
{
|
2021-09-06 03:29:52 +04:30
|
|
|
m_pre_init_buffer = ByteBuffer();
|
2020-04-25 05:06:33 +04:30
|
|
|
initialize(kind);
|
|
|
|
}
|
|
|
|
|
|
|
|
~Manager()
|
|
|
|
{
|
2021-05-18 20:41:00 +02:00
|
|
|
m_algorithm = Empty {};
|
2020-04-25 05:06:33 +04:30
|
|
|
}
|
|
|
|
|
|
|
|
inline size_t digest_size() const
|
|
|
|
{
|
2021-05-20 00:32:42 +04:30
|
|
|
return m_algorithm.visit(
|
2022-04-01 20:58:27 +03:00
|
|
|
[&](Empty const&) -> size_t { return 0; },
|
|
|
|
[&](auto const& hash) { return hash.digest_size(); });
|
2020-04-25 05:06:33 +04:30
|
|
|
}
|
2021-05-18 20:41:00 +02:00
|
|
|
|
2020-04-25 05:06:33 +04:30
|
|
|
inline size_t block_size() const
|
|
|
|
{
|
2021-05-20 00:32:42 +04:30
|
|
|
return m_algorithm.visit(
|
2022-04-01 20:58:27 +03:00
|
|
|
[&](Empty const&) -> size_t { return 0; },
|
|
|
|
[&](auto const& hash) { return hash.block_size(); });
|
2020-04-25 05:06:33 +04:30
|
|
|
}
|
2021-05-18 20:41:00 +02:00
|
|
|
|
2020-04-25 05:06:33 +04:30
|
|
|
inline void initialize(HashKind kind)
|
|
|
|
{
|
2021-05-18 20:41:00 +02:00
|
|
|
if (!m_algorithm.has<Empty>()) {
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-04-25 05:06:33 +04:30
|
|
|
}
|
|
|
|
|
|
|
|
m_kind = kind;
|
|
|
|
switch (kind) {
|
2023-09-16 17:01:54 +04:00
|
|
|
case HashKind::BLAKE2b:
|
|
|
|
m_algorithm = BLAKE2b();
|
|
|
|
break;
|
2020-04-25 05:06:33 +04:30
|
|
|
case HashKind::MD5:
|
2021-05-18 20:41:00 +02:00
|
|
|
m_algorithm = MD5();
|
2020-04-25 05:06:33 +04:30
|
|
|
break;
|
|
|
|
case HashKind::SHA1:
|
2021-05-18 20:41:00 +02:00
|
|
|
m_algorithm = SHA1();
|
2020-04-25 05:06:33 +04:30
|
|
|
break;
|
|
|
|
case HashKind::SHA256:
|
2021-05-18 20:41:00 +02:00
|
|
|
m_algorithm = SHA256();
|
2020-04-25 05:06:33 +04:30
|
|
|
break;
|
2021-05-17 22:24:32 +02:00
|
|
|
case HashKind::SHA384:
|
2021-05-18 20:41:00 +02:00
|
|
|
m_algorithm = SHA384();
|
2021-05-17 22:24:32 +02:00
|
|
|
break;
|
2020-04-25 05:06:33 +04:30
|
|
|
case HashKind::SHA512:
|
2021-05-18 20:41:00 +02:00
|
|
|
m_algorithm = SHA512();
|
2020-04-25 05:06:33 +04:30
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case HashKind::None:
|
2021-05-18 20:41:00 +02:00
|
|
|
m_algorithm = Empty {};
|
2020-04-25 05:06:33 +04:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
virtual void update(u8 const* data, size_t length) override
|
2020-04-25 05:06:33 +04:30
|
|
|
{
|
2020-04-25 05:07:24 +04:30
|
|
|
auto size = m_pre_init_buffer.size();
|
2021-05-18 20:41:00 +02:00
|
|
|
if (size) {
|
|
|
|
m_algorithm.visit(
|
|
|
|
[&](Empty&) {},
|
|
|
|
[&](auto& hash) { hash.update(m_pre_init_buffer); });
|
2020-04-25 05:06:33 +04:30
|
|
|
}
|
2021-05-18 20:41:00 +02:00
|
|
|
m_algorithm.visit(
|
|
|
|
[&](Empty&) { m_pre_init_buffer.append(data, length); },
|
|
|
|
[&](auto& hash) { hash.update(data, length); });
|
|
|
|
if (size && m_kind != HashKind::None)
|
2020-04-25 05:07:24 +04:30
|
|
|
m_pre_init_buffer.clear();
|
2020-04-25 05:06:33 +04:30
|
|
|
}
|
|
|
|
|
|
|
|
virtual DigestType peek() override
|
|
|
|
{
|
2021-05-20 00:32:42 +04:30
|
|
|
return m_algorithm.visit(
|
|
|
|
[&](Empty&) -> DigestType { VERIFY_NOT_REACHED(); },
|
|
|
|
[&](auto& hash) -> DigestType { return hash.peek(); });
|
2020-04-25 05:06:33 +04:30
|
|
|
}
|
|
|
|
|
|
|
|
virtual DigestType digest() override
|
|
|
|
{
|
|
|
|
auto digest = peek();
|
|
|
|
reset();
|
|
|
|
return digest;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void reset() override
|
|
|
|
{
|
2020-04-25 05:07:24 +04:30
|
|
|
m_pre_init_buffer.clear();
|
2021-05-18 20:41:00 +02:00
|
|
|
m_algorithm.visit(
|
|
|
|
[&](Empty&) {},
|
|
|
|
[&](auto& hash) { hash.reset(); });
|
2020-04-25 05:06:33 +04:30
|
|
|
}
|
|
|
|
|
2022-02-15 21:36:46 +02:00
|
|
|
#ifndef KERNEL
|
2022-12-04 18:02:33 +00:00
|
|
|
virtual DeprecatedString class_name() const override
|
2020-04-25 05:06:33 +04:30
|
|
|
{
|
2021-05-20 00:32:42 +04:30
|
|
|
return m_algorithm.visit(
|
2022-12-04 18:02:33 +00:00
|
|
|
[&](Empty const&) -> DeprecatedString { return "UninitializedHashManager"; },
|
2022-04-01 20:58:27 +03:00
|
|
|
[&](auto const& hash) { return hash.class_name(); });
|
2020-04-25 05:06:33 +04:30
|
|
|
}
|
2022-02-15 21:36:46 +02:00
|
|
|
#endif
|
2020-04-25 05:06:33 +04:30
|
|
|
|
2022-02-18 10:58:56 +01:00
|
|
|
inline HashKind kind() const
|
|
|
|
{
|
|
|
|
return m_kind;
|
|
|
|
}
|
|
|
|
|
2020-04-25 05:06:33 +04:30
|
|
|
inline bool is(HashKind kind) const
|
|
|
|
{
|
|
|
|
return m_kind == kind;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2023-09-16 17:01:54 +04:00
|
|
|
using AlgorithmVariant = Variant<Empty, BLAKE2b, MD5, SHA1, SHA256, SHA384, SHA512>;
|
2021-09-19 23:00:45 +02:00
|
|
|
AlgorithmVariant m_algorithm {};
|
2020-04-25 05:06:33 +04:30
|
|
|
HashKind m_kind { HashKind::None };
|
|
|
|
ByteBuffer m_pre_init_buffer;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|