diff --git a/Chaos/Cipher/Block/Decryptor.hpp b/Chaos/Cipher/Block/Decryptor.hpp index e27c130..bd38cf2 100644 --- a/Chaos/Cipher/Block/Decryptor.hpp +++ b/Chaos/Cipher/Block/Decryptor.hpp @@ -1,44 +1,32 @@ #ifndef CHAOS_CIPHER_BLOCK_DECRYPTOR_HPP #define CHAOS_CIPHER_BLOCK_DECRYPTOR_HPP +#include +#include +#include + namespace Chaos::Cipher::Block { template -class Decryptor +concept Decryptor = requires(const T constDecryptor, + typename T::Block block, + uint8_t * outBegin, uint8_t * outEnd, + uint8_t * inBegin, uint8_t * inEnd) { -public: - template - void DecryptBlock(OutputIt outBegin, OutputIt outEnd, - InputIt inBegin, InputIt inEnd) const - { - Impl().DecryptBlock(outBegin, outEnd, inBegin, inEnd); - } + typename T::Block; + typename T::Key; + requires std::constructible_from; - template - auto DecryptBlock(Block block) const - { - return Impl().DecryptBlock(block); - } + requires std::unsigned_integral>; + typename std::integral_constant; - auto GetBlockSize() const - { - return Impl().GetBlockSize(); - } + requires std::unsigned_integral>; + typename std::integral_constant; -protected: - Decryptor() = default; - -private: - const T & Impl() const - { - return static_cast(*this); - } - - T & Impl() - { - return static_cast(*this); - } + constDecryptor.DecryptBlock(outBegin, outEnd, inBegin, inEnd); + { constDecryptor.DecryptBlock(block) } -> std::same_as; + { constDecryptor.GetBlockSize() } -> std::unsigned_integral; }; } // namespace Chaos::Cipher::Block diff --git a/Chaos/Cipher/Block/Des/DesCrypt.hpp b/Chaos/Cipher/Block/Des/DesCrypt.hpp index f303c50..de6119f 100644 --- a/Chaos/Cipher/Block/Des/DesCrypt.hpp +++ b/Chaos/Cipher/Block/Des/DesCrypt.hpp @@ -233,9 +233,10 @@ public: Inner_::RawKey Key_; }; - class Encryptor : public Chaos::Cipher::Block::Encryptor + class Encryptor { public: + using Block = DesCrypt::Block; using Key = DesCrypt::Key; static constexpr size_t BlockSize = DesCrypt::BlockSize; static constexpr size_t KeySize = DesCrypt::KeySize; @@ -278,9 +279,12 @@ public: Inner_::KeySchedule Schedule_; }; - class Decryptor : public Chaos::Cipher::Block::Decryptor + static_assert(Chaos::Cipher::Block::Encryptor); + + class Decryptor { public: + using Block = DesCrypt::Block; using Key = DesCrypt::Key; static constexpr size_t BlockSize = DesCrypt::BlockSize; static constexpr size_t KeySize = DesCrypt::KeySize; @@ -323,6 +327,8 @@ public: Inner_::KeySchedule Schedule_; }; + static_assert(Chaos::Cipher::Block::Decryptor); + private: using BlockHalf = uint32_t; using RawBlockArray = Service::SeArray; diff --git a/Chaos/Cipher/Block/Encryptor.hpp b/Chaos/Cipher/Block/Encryptor.hpp index ba3f9f5..ddd3cf3 100644 --- a/Chaos/Cipher/Block/Encryptor.hpp +++ b/Chaos/Cipher/Block/Encryptor.hpp @@ -1,44 +1,32 @@ #ifndef CHAOS_CIPHER_BLOCK_ENCRYPTOR_HPP #define CHAOS_CIPHER_BLOCK_ENCRYPTOR_HPP +#include +#include +#include + namespace Chaos::Cipher::Block { template -class Encryptor +concept Encryptor = requires(const T constEncryptor, + typename T::Block block, + uint8_t * outBegin, uint8_t * outEnd, + uint8_t * inBegin, uint8_t * inEnd) { -public: - template - void EncryptBlock(OutputIt outBegin, OutputIt outEnd, - InputIt inBegin, InputIt inEnd) const - { - Impl().EncryptBlock(outBegin, outEnd, inBegin, inEnd); - } + typename T::Block; + typename T::Key; + requires std::constructible_from; - template - auto EncryptBlock(Block block) const - { - return Impl().EncryptBlock(block); - } + requires std::unsigned_integral>; + typename std::integral_constant; - auto GetBlockSize() const - { - return Impl().GetBlockSize(); - } + requires std::unsigned_integral>; + typename std::integral_constant; -protected: - Encryptor() = default; - -private: - const T & Impl() const - { - return static_cast(*this); - } - - T & Impl() - { - return static_cast(*this); - } + constEncryptor.EncryptBlock(outBegin, outEnd, inBegin, inEnd); + { constEncryptor.EncryptBlock(block) } -> std::same_as; + { constEncryptor.GetBlockSize() } -> std::unsigned_integral; }; } // namespace Chaos::Cipher::Block diff --git a/Chaos/Hash/Hash.hpp b/Chaos/Hash/Hash.hpp index fe252e1..ea73799 100644 --- a/Chaos/Hash/Hash.hpp +++ b/Chaos/Hash/Hash.hpp @@ -1,38 +1,17 @@ #ifndef CHAOS_HASH_HASH_HPP #define CHAOS_HASH_HASH_HPP +#include #include namespace Chaos::Hash { template -class Hash +concept Hash = requires(const T constHash) { -public: - auto GetRawDigest() const - { - return Impl().GetRawDigest(); - } - - std::string ToHexString() const - { - return Impl().ToHexString(); - } - -protected: - Hash() = default; - -private: - const T & Impl() const - { - return static_cast(*this); - } - - T & Impl() - { - return static_cast(*this); - } + constHash.GetRawDigest(); + { constHash.ToHexString() } -> std::same_as; }; } // namespace Chaos::Hash diff --git a/Chaos/Hash/Hasher.hpp b/Chaos/Hash/Hasher.hpp index 796c02f..7055828 100644 --- a/Chaos/Hash/Hasher.hpp +++ b/Chaos/Hash/Hasher.hpp @@ -1,42 +1,26 @@ #ifndef CHAOS_HASH_HASHER_HPP #define CHAOS_HASH_HASHER_HPP +#include +#include +#include + +#include "Hash.hpp" + namespace Chaos::Hash { template -class Hasher +concept Hasher = requires(T hasher, uint8_t * begin, uint8_t * end) { -public: - void Reset() - { - Impl().Reset(); - } + typename T::HashType; - template - void Update(InputIt begin, InputIt end) - { - Impl().Update(begin, end); - } + requires std::unsigned_integral>; + typename std::integral_constant; - auto Finish() - { - return Impl().Finish(); - } - -protected: - Hasher() = default; - -private: - const T & Impl() const - { - return static_cast(*this); - } - - T & Impl() - { - return static_cast(*this); - } + hasher.Reset(); + hasher.Update(begin, end); + { hasher.Finish() } -> Hash; }; } // namespace Chaos::Hash diff --git a/Chaos/Hash/Md4.hpp b/Chaos/Hash/Md4.hpp index 103bd3e..e78dec3 100644 --- a/Chaos/Hash/Md4.hpp +++ b/Chaos/Hash/Md4.hpp @@ -136,7 +136,7 @@ private: namespace Chaos::Hash::Md4 { -struct Md4Hash : public Hash +struct Md4Hash { std::array GetRawDigest() const { @@ -161,7 +161,9 @@ struct Md4Hash : public Hash std::array RawDigest_; }; -class Md4Hasher : public Hasher +static_assert(Hash); + +class Md4Hasher { public: using HashType = Md4Hash; @@ -302,6 +304,8 @@ private: } }; +static_assert(Hasher); + } // namespace Chaos::Hash::Md4 #endif // CHAOS_HASH_MD4_HPP diff --git a/Chaos/Hash/Md5.hpp b/Chaos/Hash/Md5.hpp index 7e6c97a..cb1be91 100644 --- a/Chaos/Hash/Md5.hpp +++ b/Chaos/Hash/Md5.hpp @@ -160,7 +160,7 @@ private: namespace Chaos::Hash::Md5 { -struct Md5Hash : public Hash +struct Md5Hash { std::array GetRawDigest() const { @@ -185,7 +185,9 @@ struct Md5Hash : public Hash std::array RawDigest_; }; -class Md5Hasher : public Hasher +static_assert(Hash); + +class Md5Hasher { public: using HashType = Md5Hash; @@ -326,6 +328,8 @@ private: } }; +static_assert(Hasher); + } // namespace Chaos::Hash::Md5 #endif // CHAOS_HASH_MD5_HPP diff --git a/Chaos/Hash/Sha1.hpp b/Chaos/Hash/Sha1.hpp index 877a0d8..ad743d8 100644 --- a/Chaos/Hash/Sha1.hpp +++ b/Chaos/Hash/Sha1.hpp @@ -129,7 +129,7 @@ private: namespace Chaos::Hash::Sha1 { -struct Sha1Hash : public Hash +struct Sha1Hash { std::array GetRawDigest() const { @@ -155,7 +155,9 @@ struct Sha1Hash : public Hash std::array RawDigest_; }; -class Sha1Hasher : public Hasher +static_assert(Hash); + +class Sha1Hasher { public: using HashType = Sha1Hash; @@ -296,6 +298,8 @@ private: } }; +static_assert(Hasher); + } // namespace Chaos::Hash::Sha1 #endif // CHAOS_HASH_SHA1_HPP diff --git a/Chaos/Mac/Hmac.hpp b/Chaos/Mac/Hmac.hpp index 125bb3a..82ecc7e 100644 --- a/Chaos/Mac/Hmac.hpp +++ b/Chaos/Mac/Hmac.hpp @@ -3,7 +3,6 @@ #include #include -#include #include "Hash/Hasher.hpp" #include "Service/ChaosException.hpp" @@ -11,8 +10,7 @@ namespace Chaos::Mac::Hmac { -template, HasherImpl>>> +template class Hmac { public: diff --git a/Chaos/Padding/Padder.hpp b/Chaos/Padding/Padder.hpp index 5ae50e0..1807d5e 100644 --- a/Chaos/Padding/Padder.hpp +++ b/Chaos/Padding/Padder.hpp @@ -1,38 +1,16 @@ #ifndef CHAOS_PADDING_PADDER_HPP #define CHAOS_PADDING_PADDER_HPP +#include + namespace Chaos::Padding { template -class Padder +concept Padder = requires(uint8_t * begin, uint8_t * end) { -public: - template - void Pad(OutputIt begin, OutputIt end) const - { - Impl().Pad(begin, end); - } - - template - auto ComputeUnpad(InputIt begin, InputIt end) const noexcept - { - return Impl().ComputeUnpad(begin, end); - } - -protected: - Padder() = default; - -private: - const T & Impl() const - { - return static_cast(*this); - } - - T & Impl() - { - return static_cast(*this); - } + T::Pad(begin, end); + { T::ComputeUnpad(begin, end) } noexcept; }; } // namespace Chaos::Padding diff --git a/Chaos/Padding/PadderIso7816.hpp b/Chaos/Padding/PadderIso7816.hpp index c5af640..ee21f27 100644 --- a/Chaos/Padding/PadderIso7816.hpp +++ b/Chaos/Padding/PadderIso7816.hpp @@ -12,7 +12,7 @@ namespace Chaos::Padding { -class PadderIso7816 : public Padder +class PadderIso7816 { public: template @@ -82,6 +82,8 @@ private: using Branchless = Service::Branchless; }; +static_assert(Padder); + } // namespace Chaos::Padding #endif // CHAOS_PADDING_PADDERISO7816_HPP diff --git a/Chaos/Padding/PadderPkcs7.hpp b/Chaos/Padding/PadderPkcs7.hpp index 325a01f..61c4a60 100644 --- a/Chaos/Padding/PadderPkcs7.hpp +++ b/Chaos/Padding/PadderPkcs7.hpp @@ -13,7 +13,7 @@ namespace Chaos::Padding { -class PadderPkcs7 : public Padder +class PadderPkcs7 { public: template @@ -75,6 +75,8 @@ private: using Branchless = Service::Branchless; }; +static_assert(Padder); + } // namespace Chaos::Padding #endif // CHAOS_PADDING_PADDERPKCS7_HPP diff --git a/Chaos/Service/SeArray.hpp b/Chaos/Service/SeArray.hpp index 24c7875..604cc01 100644 --- a/Chaos/Service/SeArray.hpp +++ b/Chaos/Service/SeArray.hpp @@ -2,14 +2,13 @@ #define CHAOS_SERVICE_SEARRAY_HPP #include -#include +#include #include namespace Chaos::Service { -template>> +template class SeArray { public: diff --git a/ChaosTests/Cipher/Block/Des/DesCryptTests.cpp b/ChaosTests/Cipher/Block/Des/DesCryptTests.cpp index 417f765..7d9b86b 100644 --- a/ChaosTests/Cipher/Block/Des/DesCryptTests.cpp +++ b/ChaosTests/Cipher/Block/Des/DesCryptTests.cpp @@ -486,9 +486,9 @@ TEST(DesCryptTests, OutIteratorUsageDecryptTest) } } -template -static std::vector EncryptThroughBase(const Encryptor & enc, - InputIt begin, InputIt end) +template +static std::vector EncryptGeneric(const EncryptorImpl & enc, + InputIt begin, InputIt end) { std::vector result; result.resize(enc.GetBlockSize(), 0); @@ -497,7 +497,7 @@ static std::vector EncryptThroughBase(const Encryptor & enc, return result; } -TEST(DesCryptTests, EncryptThroughBaseTest) +TEST(DesCryptTests, EncryptGenericTest) { std::array key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 }; @@ -507,16 +507,16 @@ TEST(DesCryptTests, EncryptThroughBaseTest) DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::Encryptor enc(desKey); - ASSERT_EQ(expected, EncryptThroughBase(enc, data.begin(), data.end())); + ASSERT_EQ(expected, EncryptGeneric(enc, data.begin(), data.end())); } -template -static uint64_t EncryptUInt64BlockThroughBase(const Encryptor & enc, uint64_t block) +template +static uint64_t EncryptUInt64BlockGeneric(const EncryptorImpl & enc, uint64_t block) { return enc.EncryptBlock(block); } -TEST(DesCryptTests, EncryptUInt64BlockThroughBaseTest) +TEST(DesCryptTests, EncryptUInt64BlockGenericTest) { std::array key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 }; @@ -526,12 +526,12 @@ TEST(DesCryptTests, EncryptUInt64BlockThroughBaseTest) DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::Encryptor enc(desKey); - ASSERT_EQ(expected, EncryptUInt64BlockThroughBase(enc, data)); + ASSERT_EQ(expected, EncryptUInt64BlockGeneric(enc, data)); } -template -static std::vector DecryptThroughBase(const Decryptor & dec, - InputIt begin, InputIt end) +template +static std::vector DecryptGeneric(const DecryptorImpl & dec, + InputIt begin, InputIt end) { std::vector result; result.resize(dec.GetBlockSize(), 0); @@ -540,7 +540,7 @@ static std::vector DecryptThroughBase(const Decryptor & dec, return result; } -TEST(DesCryptTests, DecryptThroughBaseTest) +TEST(DesCryptTests, DecryptGenericTest) { std::array key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 }; @@ -550,16 +550,16 @@ TEST(DesCryptTests, DecryptThroughBaseTest) DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::Decryptor dec(desKey); - ASSERT_EQ(expected, DecryptThroughBase(dec, data.begin(), data.end())); + ASSERT_EQ(expected, DecryptGeneric(dec, data.begin(), data.end())); } -template -static uint64_t DecryptUInt64BlockThroughBase(const Decryptor & dec, uint64_t block) +template +static uint64_t DecryptUInt64BlockGeneric(const DecryptorImpl & dec, uint64_t block) { return dec.DecryptBlock(block); } -TEST(DesCryptTests, DecryptUInt64BlockThroughBaseTest) +TEST(DesCryptTests, DecryptUInt64BlockGenericTest) { std::array key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 }; @@ -569,5 +569,5 @@ TEST(DesCryptTests, DecryptUInt64BlockThroughBaseTest) DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::Decryptor dec(desKey); - ASSERT_EQ(expected, DecryptUInt64BlockThroughBase(dec, data)); + ASSERT_EQ(expected, DecryptUInt64BlockGeneric(dec, data)); } diff --git a/ChaosTests/Padding/PadderIso7816Tests.cpp b/ChaosTests/Padding/PadderIso7816Tests.cpp index 0118032..e917063 100644 --- a/ChaosTests/Padding/PadderIso7816Tests.cpp +++ b/ChaosTests/Padding/PadderIso7816Tests.cpp @@ -109,8 +109,8 @@ TEST(PadIso7816Tests, PadOutIteratorUsageTest) } } -template -void PadThroughBase(const Padder & padder, OutputIt begin, OutputIt end) +template +void PadGeneric(const PadderImpl & padder, OutputIt begin, OutputIt end) { padder.Pad(begin, end); } @@ -127,7 +127,7 @@ TEST(PadIso7816Tests, PadThroughBaseTest) }; const PadderIso7816 padder; - PadThroughBase(padder, fact.begin(), fact.end()); + PadGeneric(padder, fact.begin(), fact.end()); ASSERT_EQ(expected, fact); } @@ -324,8 +324,8 @@ TEST(PadIso7816Tests, UnpadErrorTest) } } -template -auto ComputeUnpadThroughBase(const Padder & padder, OutputIt begin, OutputIt end) +template +auto ComputeUnpadGeneric(const PadderImpl & padder, OutputIt begin, OutputIt end) { return padder.ComputeUnpad(begin, end); } @@ -336,7 +336,7 @@ TEST(PadIso7816Tests, ComputeUnpadThroughBaseTest) std::array data = { 0x80, 0x00, 0x00, 0x00, 0x00 }; const PadderIso7816 padder; - auto result = ComputeUnpadThroughBase(padder, data.begin(), data.end()); + auto result = ComputeUnpadGeneric(padder, data.begin(), data.end()); ASSERT_TRUE(result.IsOkay_); ASSERT_EQ(5, result.PadSize_); diff --git a/ChaosTests/Padding/PadderPkcs7Tests.cpp b/ChaosTests/Padding/PadderPkcs7Tests.cpp index 112918b..57240ac 100644 --- a/ChaosTests/Padding/PadderPkcs7Tests.cpp +++ b/ChaosTests/Padding/PadderPkcs7Tests.cpp @@ -137,8 +137,8 @@ TEST(PadPkcs7Tests, PadOutIteratorUsageTest) } } -template -void PadThroughBase(const Padder & padder, OutputIt begin, OutputIt end) +template +void PadGeneric(const PadderImpl & padder, OutputIt begin, OutputIt end) { padder.Pad(begin, end); } @@ -153,7 +153,7 @@ TEST(PadPkcs7Tests, PadThroughBaseTest) }; const PadderPkcs7 padder; - PadThroughBase(padder, fact.begin(), fact.end()); + PadGeneric(padder, fact.begin(), fact.end()); ASSERT_EQ(expected, fact); } @@ -347,8 +347,8 @@ TEST(PadPkcs7Tests, UnpadErrorTest) } } -template -auto ComputeUnpadThroughBase(const Padder & padder, OutputIt begin, OutputIt end) +template +auto ComputeUnpadGeneric(const PadderImpl & padder, OutputIt begin, OutputIt end) { return padder.ComputeUnpad(begin, end); } @@ -359,7 +359,7 @@ TEST(PadPkcs7Tests, ComputeUnpadThroughBaseTest) std::array data = { 0x05, 0x05, 0x05, 0x05, 0x05 }; const PadderPkcs7 padder; - auto result = ComputeUnpadThroughBase(padder, data.begin(), data.end()); + auto result = ComputeUnpadGeneric(padder, data.begin(), data.end()); ASSERT_TRUE(result.IsOkay_); ASSERT_EQ(5, result.PadSize_);