Sfoglia il codice sorgente

LibCrypto: Mutualize `Digest`s

Michel Hermier 3 anni fa
parent
commit
3f0e425f1e

+ 15 - 2
Userland/Libraries/LibCrypto/Hash/HashFunction.h

@@ -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;
 

+ 1 - 1
Userland/Libraries/LibCrypto/Hash/HashManager.h

@@ -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;
 

+ 1 - 9
Userland/Libraries/LibCrypto/Hash/MD5.h

@@ -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;
 

+ 1 - 10
Userland/Libraries/LibCrypto/Hash/SHA1.h

@@ -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;
 

+ 3 - 11
Userland/Libraries/LibCrypto/Hash/SHA2.h

@@ -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;