LibCrypto: Mutualize Digests

This commit is contained in:
Michel Hermier 2022-01-03 22:28:19 +01:00 committed by Ali Mohammad Pur
parent 4e851145ba
commit 3f0e425f1e
Notes: sideshowbarker 2024-07-17 21:39:42 +09:00
5 changed files with 21 additions and 33 deletions

View file

@ -13,11 +13,24 @@
namespace Crypto {
namespace Hash {
template<size_t BlockS, typename DigestT>
template<size_t DigestS>
struct Digest {
static_assert(DigestS % 8 == 0);
constexpr static size_t Size = DigestS / 8;
u8 data[Size];
[[nodiscard]] ALWAYS_INLINE const u8* immutable_data() const { return data; }
[[nodiscard]] ALWAYS_INLINE size_t data_length() const { return Size; }
};
template<size_t BlockS, size_t DigestS, typename DigestT = Digest<DigestS>>
class HashFunction {
public:
static_assert(BlockS % 8 == 0);
static constexpr auto BlockSize = BlockS / 8;
static constexpr auto DigestSize = DigestT::Size;
static_assert(DigestS % 8 == 0);
static constexpr auto DigestSize = DigestS / 8;
using DigestType = DigestT;

View file

@ -77,7 +77,7 @@ struct MultiHashDigestVariant {
DigestVariant m_digest {};
};
class Manager final : public HashFunction<0, MultiHashDigestVariant> {
class Manager final : public HashFunction<0, 0, MultiHashDigestVariant> {
public:
using HashFunction::update;

View file

@ -13,14 +13,6 @@
namespace Crypto {
namespace Hash {
struct MD5Digest {
constexpr static size_t Size = 16;
u8 data[Size];
const u8* immutable_data() const { return data; }
size_t data_length() const { return Size; }
};
namespace MD5Constants {
constexpr u32 init_A = 0x67452301;
@ -53,7 +45,7 @@ constexpr u8 PADDING[] = {
}
class MD5 final : public HashFunction<512, MD5Digest> {
class MD5 final : public HashFunction<512, 128> {
public:
using HashFunction::update;

View file

@ -25,16 +25,7 @@ constexpr static u32 RoundConstants[4] {
}
template<size_t Bytes>
struct SHA1Digest {
u8 data[Bytes];
constexpr static size_t Size = Bytes;
const u8* immutable_data() const { return data; }
size_t data_length() const { return Bytes; }
};
class SHA1 final : public HashFunction<512, SHA1Digest<160 / 8>> {
class SHA1 final : public HashFunction<512, 160> {
public:
using HashFunction::update;

View file

@ -72,16 +72,8 @@ constexpr static u64 InitializationHashes[8] = {
};
}
template<size_t Bytes>
struct SHA2Digest {
u8 data[Bytes];
constexpr static size_t Size = Bytes;
const u8* immutable_data() const { return data; }
size_t data_length() const { return Bytes; }
};
// FIXME: I want template<size_t BlockSize> but the compiler gets confused
class SHA256 final : public HashFunction<512, SHA2Digest<256 / 8>> {
class SHA256 final : public HashFunction<512, 256> {
public:
using HashFunction::update;
@ -131,7 +123,7 @@ private:
constexpr static auto Rounds = 64;
};
class SHA384 final : public HashFunction<1024, SHA2Digest<384 / 8>> {
class SHA384 final : public HashFunction<1024, 384> {
public:
using HashFunction::update;
@ -181,7 +173,7 @@ private:
constexpr static auto Rounds = 80;
};
class SHA512 final : public HashFunction<1024, SHA2Digest<512 / 8>> {
class SHA512 final : public HashFunction<1024, 512> {
public:
using HashFunction::update;