diff --git a/Chaos/Cipher/Block/Decryptor.hpp b/Chaos/Cipher/Block/Decryptor.hpp index ea90e7b..cd21712 100644 --- a/Chaos/Cipher/Block/Decryptor.hpp +++ b/Chaos/Cipher/Block/Decryptor.hpp @@ -20,6 +20,11 @@ public: return Impl().DecryptBlock(block); } + auto GetBlockSize() + { + return Impl().GetBlockSize(); + } + protected: Decryptor() = default; diff --git a/Chaos/Cipher/Block/Des/DesCrypt.hpp b/Chaos/Cipher/Block/Des/DesCrypt.hpp index 50b540d..1328573 100644 --- a/Chaos/Cipher/Block/Des/DesCrypt.hpp +++ b/Chaos/Cipher/Block/Des/DesCrypt.hpp @@ -196,6 +196,9 @@ class DesCrypt { public: using Block = uint64_t; + static constexpr size_t BlockSize = 8; + + static_assert(BlockSize == sizeof(Block)); DesCrypt() = delete; @@ -227,6 +230,8 @@ public: class DesEncryptor : public Encryptor { public: + static constexpr size_t BlockSize = DesCrypt::BlockSize; + DesEncryptor(const Key & key) : Schedule_(Inner_::KeySchedule::Direction::Encrypt, key.Key_) { } @@ -255,6 +260,11 @@ public: return DesCrypt::ProcessBlock(block, Schedule_); } + constexpr size_t GetBlockSize() + { + return BlockSize; + } + private: Inner_::KeySchedule Schedule_; }; @@ -262,6 +272,8 @@ public: class DesDecryptor : public Decryptor { public: + static constexpr size_t BlockSize = DesCrypt::BlockSize; + DesDecryptor(const Key & key) : Schedule_(Inner_::KeySchedule::Direction::Decrypt, key.Key_) { } @@ -290,6 +302,11 @@ public: return DesCrypt::ProcessBlock(block, Schedule_); } + constexpr size_t GetBlockSize() + { + return BlockSize; + } + private: Inner_::KeySchedule Schedule_; }; diff --git a/Chaos/Cipher/Block/Encryptor.hpp b/Chaos/Cipher/Block/Encryptor.hpp index bf15e5c..41fc034 100644 --- a/Chaos/Cipher/Block/Encryptor.hpp +++ b/Chaos/Cipher/Block/Encryptor.hpp @@ -20,6 +20,11 @@ public: return Impl().EncryptBlock(block); } + auto GetBlockSize() + { + return Impl().GetBlockSize(); + } + protected: Encryptor() = default; diff --git a/ChaosTests/Cipher/DesCryptTests.cpp b/ChaosTests/Cipher/DesCryptTests.cpp index f7e4139..cde9961 100644 --- a/ChaosTests/Cipher/DesCryptTests.cpp +++ b/ChaosTests/Cipher/DesCryptTests.cpp @@ -43,10 +43,10 @@ TEST(DesCryptTests, EncryptTest) { struct Helper { - std::array operator()(const std::array & data, - const std::array & key) const + std::array operator()(const std::array & data, + const std::array & key) const { - std::array result; + std::array result; result.fill(0); DesCrypt::Key desKey(key.begin(), key.end()); @@ -60,28 +60,28 @@ TEST(DesCryptTests, EncryptTest) Helper des; { - std::array data = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef }; + std::array data = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef }; std::array key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 }; - std::array expected = { 0x85, 0xe8, 0x13, 0x54, 0x0f, 0x0a, 0xb4, 0x05 }; + std::array expected = { 0x85, 0xe8, 0x13, 0x54, 0x0f, 0x0a, 0xb4, 0x05 }; ASSERT_EQ(expected, des(data, key)); } { - std::array data = { 0xaa, 0xf3, 0x83, 0x16, 0x2d, 0x2e, 0x6b, 0xcb }; + std::array data = { 0xaa, 0xf3, 0x83, 0x16, 0x2d, 0x2e, 0x6b, 0xcb }; std::array key = { 0x44, 0xbf, 0x32, 0x19, 0x99, 0x25, 0x81, 0x51 }; - std::array expected = { 0x07, 0xe8, 0x7f, 0xaa, 0xb3, 0x17, 0x13, 0x18 }; + std::array expected = { 0x07, 0xe8, 0x7f, 0xaa, 0xb3, 0x17, 0x13, 0x18 }; ASSERT_EQ(expected, des(data, key)); } { - std::array data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44 }; + std::array data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44 }; std::array key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab }; - std::array expected = { 0x42, 0x27, 0x88, 0xa6, 0x7b, 0x6c, 0x18, 0xed }; + std::array expected = { 0x42, 0x27, 0x88, 0xa6, 0x7b, 0x6c, 0x18, 0xed }; ASSERT_EQ(expected, des(data, key)); } @@ -139,7 +139,7 @@ TEST(DesCryptTests, EncryptShortDataTest) const std::vector & key) { std::vector result; - result.resize(8, 0); + result.resize(DesCrypt::BlockSize, 0); DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::DesEncryptor enc(desKey); @@ -173,7 +173,7 @@ TEST(DesCryptTests, EncryptLongDataTest) const std::vector & key) { std::vector result; - result.resize(8, 0); + result.resize(DesCrypt::BlockSize, 0); DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::DesEncryptor enc(desKey); @@ -203,10 +203,10 @@ TEST(DesCryptTests, DecryptTest) { struct Helper { - std::array operator()(const std::array & data, - const std::array & key) const + std::array operator()(const std::array & data, + const std::array & key) const { - std::array result; + std::array result; result.fill(0); DesCrypt::Key desKey(key.begin(), key.end()); @@ -220,28 +220,28 @@ TEST(DesCryptTests, DecryptTest) Helper des; { - std::array data = { 0x85, 0xe8, 0x13, 0x54, 0x0f, 0x0a, 0xb4, 0x05 }; + std::array data = { 0x85, 0xe8, 0x13, 0x54, 0x0f, 0x0a, 0xb4, 0x05 }; std::array key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 }; - std::array expected = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef }; + std::array expected = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef }; ASSERT_EQ(expected, des(data, key)); } { - std::array data = { 0x07, 0xe8, 0x7f, 0xaa, 0xb3, 0x17, 0x13, 0x18 }; + std::array data = { 0x07, 0xe8, 0x7f, 0xaa, 0xb3, 0x17, 0x13, 0x18 }; std::array key = { 0x44, 0xbf, 0x32, 0x19, 0x99, 0x25, 0x81, 0x51 }; - std::array expected = { 0xaa, 0xf3, 0x83, 0x16, 0x2d, 0x2e, 0x6b, 0xcb }; + std::array expected = { 0xaa, 0xf3, 0x83, 0x16, 0x2d, 0x2e, 0x6b, 0xcb }; ASSERT_EQ(expected, des(data, key)); } { - std::array data = { 0x42, 0x27, 0x88, 0xa6, 0x7b, 0x6c, 0x18, 0xed }; + std::array data = { 0x42, 0x27, 0x88, 0xa6, 0x7b, 0x6c, 0x18, 0xed }; std::array key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab }; - std::array expected = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44 }; + std::array expected = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44 }; ASSERT_EQ(expected, des(data, key)); } @@ -299,7 +299,7 @@ TEST(DesCryptTests, DecryptShortDataTest) const std::vector & key) { std::vector result; - result.resize(8, 0); + result.resize(DesCrypt::BlockSize, 0); DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::DesDecryptor dec(desKey); @@ -333,7 +333,7 @@ TEST(DesCryptTests, DecryptLongDataTest) const std::vector & key) { std::vector result; - result.resize(8, 0); + result.resize(DesCrypt::BlockSize, 0); DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::DesDecryptor dec(desKey); @@ -404,7 +404,7 @@ TEST(DesCryptTests, OutIteratorUsageEncryptTest) }; { - std::array data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44 }; + std::array data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44 }; std::array key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab }; size_t asteriskCalls = 0; @@ -465,7 +465,7 @@ TEST(DesCryptTests, OutIteratorUsageDecryptTest) }; { - std::array data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44 }; + std::array data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44 }; std::array key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab }; size_t asteriskCalls = 0; @@ -498,10 +498,12 @@ TEST(DesCryptTests, OutIteratorUsageDecryptTest) } template -static std::array EncryptThroughBase(Encryptor & enc, - InputIt begin, InputIt end) +static std::vector EncryptThroughBase(Encryptor & enc, + InputIt begin, InputIt end) { - std::array result; + std::vector result; + result.resize(enc.GetBlockSize(), 0); + enc.EncryptBlock(result.begin(), begin, end); return result; } @@ -510,8 +512,8 @@ TEST(DesCryptTests, EncryptThroughBaseTest) { std::array key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 }; - std::array data = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef }; - std::array expected = { 0x85, 0xe8, 0x13, 0x54, 0x0f, 0x0a, 0xb4, 0x05 }; + std::vector data = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef }; + std::vector expected = { 0x85, 0xe8, 0x13, 0x54, 0x0f, 0x0a, 0xb4, 0x05 }; DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::DesEncryptor enc(desKey); @@ -539,10 +541,12 @@ TEST(DesCryptTests, EncryptUInt64BlockThroughBaseTest) } template -static std::array DecryptThroughBase(Decryptor & dec, - InputIt begin, InputIt end) +static std::vector DecryptThroughBase(Decryptor & dec, + InputIt begin, InputIt end) { - std::array result; + std::vector result; + result.resize(dec.GetBlockSize(), 0); + dec.DecryptBlock(result.begin(), begin, end); return result; } @@ -551,8 +555,8 @@ TEST(DesCryptTests, DecryptThroughBaseTest) { std::array key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 }; - std::array data = { 0x85, 0xe8, 0x13, 0x54, 0x0f, 0x0a, 0xb4, 0x05 }; - std::array expected = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef }; + std::vector data = { 0x85, 0xe8, 0x13, 0x54, 0x0f, 0x0a, 0xb4, 0x05 }; + std::vector expected = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef }; DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::DesDecryptor dec(desKey);