20 #ifndef CS_CRYPTO_DRIVERS_BOTAN_HASH_H
21 #define CS_CRYPTO_DRIVERS_BOTAN_HASH_H
23 #ifdef CSCRYPTO_HAVE_BOTAN
25 #include <drivers/base/hash.h>
26 #include <util/conversions/byte.h>
27 #include <util/tools/span.h>
32 #include <type_traits>
34 #include <botan/md4.h>
35 #include <botan/md5.h>
36 #include <botan/sha160.h>
37 #include <botan/sha2_32.h>
38 #include <botan/sha2_64.h>
39 #include <botan/sha3.h>
41 namespace cs_crypto::drivers::botan {
43 template <
typename BotanContext, std::size_t SIZE>
44 struct hasher_interface {
46 constexpr static const std::size_t digest_size = SIZE;
48 hasher_interface(
const hasher_interface & other)
54 this->m_context = other.m_context->copy_state();
57 hasher_interface &operator=(
const hasher_interface & other) &
63 hasher_interface tmp = other;
64 std::swap(*
this, tmp);
69 hasher_interface(hasher_interface &&) =
default;
70 hasher_interface &operator=(hasher_interface &&) & =
default;
72 static std::optional<hasher_interface> make_context()
74 auto retval = hasher_interface();
76 if (retval.m_context ==
nullptr) {
83 void update(cs_crypto::util::span<std::byte> bytes) &
85 m_context->update(cs_crypto::util::from_byte_ptr(bytes.data()), bytes.size());
90 std::array<std::byte, digest_size> md = {};
91 m_context->final(cs_crypto::util::from_byte_ptr(md.data()));
97 std::unique_ptr<Botan::HashFunction> m_context;
100 : m_context(std::make_unique<BotanContext>())
105 struct hash : cs_crypto::drivers::basic_hash {
106 using md4 = hasher_interface<Botan::MD4, 16>;
108 using md5 = hasher_interface<Botan::MD5, 16>;
110 using sha1 = hasher_interface<Botan::SHA_160, 20>;
112 using sha2_224 = hasher_interface<Botan::SHA_224, 28>;
113 using sha2_256 = hasher_interface<Botan::SHA_256, 32>;
114 using sha2_384 = hasher_interface<Botan::SHA_384, 48>;
115 using sha2_512 = hasher_interface<Botan::SHA_512, 64>;
117 using sha3_224 = hasher_interface<Botan::SHA_3_224, 28>;
118 using sha3_256 = hasher_interface<Botan::SHA_3_256, 32>;
119 using sha3_384 = hasher_interface<Botan::SHA_3_384, 48>;
120 using sha3_512 = hasher_interface<Botan::SHA_3_512, 64>;