From 3c98b75986f2273f784ffcc034c82bacb43f61a3 Mon Sep 17 00:00:00 2001 From: hashlag Date: Fri, 30 Jan 2026 01:01:48 +0300 Subject: [PATCH 1/4] Create dedicated Cipher/Block directory for block ciphers Move Des there. --- Chaos/Cipher/{ => Block}/Des/DesCrypt.hpp | 14 +++++++------- ChaosTests/Cipher/DesCryptTests.cpp | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) rename Chaos/Cipher/{ => Block}/Des/DesCrypt.hpp (97%) diff --git a/Chaos/Cipher/Des/DesCrypt.hpp b/Chaos/Cipher/Block/Des/DesCrypt.hpp similarity index 97% rename from Chaos/Cipher/Des/DesCrypt.hpp rename to Chaos/Cipher/Block/Des/DesCrypt.hpp index 96a5fb1..6bc14c0 100644 --- a/Chaos/Cipher/Des/DesCrypt.hpp +++ b/Chaos/Cipher/Block/Des/DesCrypt.hpp @@ -1,5 +1,5 @@ -#ifndef CHAOS_CIPHER_DES_DESCRYPT_HPP -#define CHAOS_CIPHER_DES_DESCRYPT_HPP +#ifndef CHAOS_CIPHER_BLOCK_DES_DESCRYPT_HPP +#define CHAOS_CIPHER_BLOCK_DES_DESCRYPT_HPP #include #include @@ -7,7 +7,7 @@ #include "Service/ChaosException.hpp" #include "Service/SeArray.hpp" -namespace Chaos::Cipher::Des::Inner_ +namespace Chaos::Cipher::Block::Des::Inner_ { struct Bitwise @@ -184,9 +184,9 @@ private: } }; -} // namespace Chaos::Cipher::Des::Inner_ +} // namespace Chaos::Cipher::Block::Des::Inner_ -namespace Chaos::Cipher::Des +namespace Chaos::Cipher::Block::Des { class DesCrypt @@ -483,6 +483,6 @@ private: } }; -} // namespace Chaos::Cipher::Des +} // namespace Chaos::Cipher::Block::Des -#endif // CHAOS_CIPHER_DES_DESCRYPT_HPP +#endif // CHAOS_CIPHER_BLOCK_DES_DESCRYPT_HPP diff --git a/ChaosTests/Cipher/DesCryptTests.cpp b/ChaosTests/Cipher/DesCryptTests.cpp index b98a502..1fb117f 100644 --- a/ChaosTests/Cipher/DesCryptTests.cpp +++ b/ChaosTests/Cipher/DesCryptTests.cpp @@ -1,8 +1,8 @@ #include -#include "Cipher/Des/DesCrypt.hpp" +#include "Cipher/Block/Des/DesCrypt.hpp" -using namespace Chaos::Cipher::Des; +using namespace Chaos::Cipher::Block::Des; TEST(DesCryptTests, KeyScheduleTest) { From ba70abd2af583546b17a7bcd3b80038dc6813fa4 Mon Sep 17 00:00:00 2001 From: hashlag Date: Sat, 31 Jan 2026 17:18:25 +0300 Subject: [PATCH 2/4] Rename DesCrypt::Encryptor --> DesEncryptor, DesCrypt::Decryptor --> DesDecryptor. Maintain naming consistency with Hash subproject. --- Chaos/Cipher/Block/Des/DesCrypt.hpp | 8 ++++---- ChaosTests/Cipher/DesCryptTests.cpp | 24 ++++++++++++------------ 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/Chaos/Cipher/Block/Des/DesCrypt.hpp b/Chaos/Cipher/Block/Des/DesCrypt.hpp index 6bc14c0..c2430cf 100644 --- a/Chaos/Cipher/Block/Des/DesCrypt.hpp +++ b/Chaos/Cipher/Block/Des/DesCrypt.hpp @@ -221,10 +221,10 @@ public: Inner_::RawKey Key_; }; - class Encryptor + class DesEncryptor { public: - Encryptor(const Key & key) + DesEncryptor(const Key & key) : Schedule_(Inner_::KeySchedule::Direction::Encrypt, key.Key_) { } @@ -256,10 +256,10 @@ public: Inner_::KeySchedule Schedule_; }; - class Decryptor + class DesDecryptor { public: - Decryptor(const Key & key) + DesDecryptor(const Key & key) : Schedule_(Inner_::KeySchedule::Direction::Decrypt, key.Key_) { } diff --git a/ChaosTests/Cipher/DesCryptTests.cpp b/ChaosTests/Cipher/DesCryptTests.cpp index 1fb117f..c10ac14 100644 --- a/ChaosTests/Cipher/DesCryptTests.cpp +++ b/ChaosTests/Cipher/DesCryptTests.cpp @@ -48,7 +48,7 @@ TEST(DesCryptTests, EncryptTest) result.fill(0); DesCrypt::Key desKey(key.begin(), key.end()); - DesCrypt::Encryptor enc(desKey); + DesCrypt::DesEncryptor enc(desKey); enc.EncryptBlock(result.begin(), data.begin(), data.end()); return result; @@ -93,7 +93,7 @@ TEST(DesCryptTests, EncryptUInt64BlockTest) const std::array & key) const { DesCrypt::Key desKey(key.begin(), key.end()); - DesCrypt::Encryptor enc(desKey); + DesCrypt::DesEncryptor enc(desKey); return enc.EncryptBlock(data); } @@ -140,7 +140,7 @@ TEST(DesCryptTests, EncryptShortDataTest) result.resize(8, 0); DesCrypt::Key desKey(key.begin(), key.end()); - DesCrypt::Encryptor enc(desKey); + DesCrypt::DesEncryptor enc(desKey); enc.EncryptBlock(result.begin(), data.begin(), data.end()); return result; @@ -174,7 +174,7 @@ TEST(DesCryptTests, EncryptLongDataTest) result.resize(8, 0); DesCrypt::Key desKey(key.begin(), key.end()); - DesCrypt::Encryptor enc(desKey); + DesCrypt::DesEncryptor enc(desKey); enc.EncryptBlock(result.begin(), data.begin(), data.end()); return result; @@ -208,7 +208,7 @@ TEST(DesCryptTests, DecryptTest) result.fill(0); DesCrypt::Key desKey(key.begin(), key.end()); - DesCrypt::Decryptor dec(desKey); + DesCrypt::DesDecryptor dec(desKey); dec.DecryptBlock(result.begin(), data.begin(), data.end()); return result; @@ -253,7 +253,7 @@ TEST(DesCryptTests, DecryptUInt64BlockTest) const std::array & key) const { DesCrypt::Key desKey(key.begin(), key.end()); - DesCrypt::Decryptor dec(desKey); + DesCrypt::DesDecryptor dec(desKey); return dec.DecryptBlock(data); } @@ -300,7 +300,7 @@ TEST(DesCryptTests, DecryptShortDataTest) result.resize(8, 0); DesCrypt::Key desKey(key.begin(), key.end()); - DesCrypt::Decryptor dec(desKey); + DesCrypt::DesDecryptor dec(desKey); dec.DecryptBlock(result.begin(), data.begin(), data.end()); return result; @@ -334,7 +334,7 @@ TEST(DesCryptTests, DecryptLongDataTest) result.resize(8, 0); DesCrypt::Key desKey(key.begin(), key.end()); - DesCrypt::Decryptor dec(desKey); + DesCrypt::DesDecryptor dec(desKey); dec.DecryptBlock(result.begin(), data.begin(), data.end()); return result; @@ -410,7 +410,7 @@ TEST(DesCryptTests, OutIteratorUsageEncryptTest) OutputItMock it(asteriskCalls, incrementCalls); DesCrypt::Key desKey(key.begin(), key.end()); - DesCrypt::Encryptor enc(desKey); + DesCrypt::DesEncryptor enc(desKey); enc.EncryptBlock(it, data.begin(), data.end()); ASSERT_EQ(8, asteriskCalls); @@ -426,7 +426,7 @@ TEST(DesCryptTests, OutIteratorUsageEncryptTest) OutputItMock it(asteriskCalls, incrementCalls); DesCrypt::Key desKey(key.begin(), key.end()); - DesCrypt::Encryptor enc(desKey); + DesCrypt::DesEncryptor enc(desKey); enc.EncryptBlock(it, data.begin(), data.end()); ASSERT_EQ(8, asteriskCalls); @@ -471,7 +471,7 @@ TEST(DesCryptTests, OutIteratorUsageDecryptTest) OutputItMock it(asteriskCalls, incrementCalls); DesCrypt::Key desKey(key.begin(), key.end()); - DesCrypt::Decryptor dec(desKey); + DesCrypt::DesDecryptor dec(desKey); dec.DecryptBlock(it, data.begin(), data.end()); ASSERT_EQ(8, asteriskCalls); @@ -487,7 +487,7 @@ TEST(DesCryptTests, OutIteratorUsageDecryptTest) OutputItMock it(asteriskCalls, incrementCalls); DesCrypt::Key desKey(key.begin(), key.end()); - DesCrypt::Decryptor dec(desKey); + DesCrypt::DesDecryptor dec(desKey); dec.DecryptBlock(it, data.begin(), data.end()); ASSERT_EQ(8, asteriskCalls); From 4cf79b61adf93ee06ea6c410292f73ebd1e4576f Mon Sep 17 00:00:00 2001 From: hashlag Date: Sat, 31 Jan 2026 18:12:43 +0300 Subject: [PATCH 3/4] Introduce Block::Encryptor<> CRTP base class. Inherit DesEncryptor from it. --- Chaos/Cipher/Block/Des/DesCrypt.hpp | 4 ++- Chaos/Cipher/Block/Encryptor.hpp | 40 +++++++++++++++++++++++++++ ChaosTests/Cipher/DesCryptTests.cpp | 43 +++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 Chaos/Cipher/Block/Encryptor.hpp diff --git a/Chaos/Cipher/Block/Des/DesCrypt.hpp b/Chaos/Cipher/Block/Des/DesCrypt.hpp index c2430cf..d4a2c11 100644 --- a/Chaos/Cipher/Block/Des/DesCrypt.hpp +++ b/Chaos/Cipher/Block/Des/DesCrypt.hpp @@ -7,6 +7,8 @@ #include "Service/ChaosException.hpp" #include "Service/SeArray.hpp" +#include "Cipher/Block/Encryptor.hpp" + namespace Chaos::Cipher::Block::Des::Inner_ { @@ -221,7 +223,7 @@ public: Inner_::RawKey Key_; }; - class DesEncryptor + class DesEncryptor : public Encryptor { public: DesEncryptor(const Key & key) diff --git a/Chaos/Cipher/Block/Encryptor.hpp b/Chaos/Cipher/Block/Encryptor.hpp new file mode 100644 index 0000000..bf15e5c --- /dev/null +++ b/Chaos/Cipher/Block/Encryptor.hpp @@ -0,0 +1,40 @@ +#ifndef CHAOS_CIPHER_BLOCK_ENCRYPTOR_HPP +#define CHAOS_CIPHER_BLOCK_ENCRYPTOR_HPP + +namespace Chaos::Cipher::Block +{ + +template +class Encryptor +{ +public: + template + void EncryptBlock(OutputIt out, InputIt inBegin, InputIt inEnd) + { + Impl().EncryptBlock(out, inBegin, inEnd); + } + + template + auto EncryptBlock(Block block) + { + return Impl().EncryptBlock(block); + } + +protected: + Encryptor() = default; + +private: + const T & Impl() const + { + return static_cast(*this); + } + + T & Impl() + { + return static_cast(*this); + } +}; + +} // namespace Chaos::Cipher::Block + +#endif // CHAOS_CIPHER_BLOCK_ENCRYPTOR_HPP diff --git a/ChaosTests/Cipher/DesCryptTests.cpp b/ChaosTests/Cipher/DesCryptTests.cpp index c10ac14..6b16c80 100644 --- a/ChaosTests/Cipher/DesCryptTests.cpp +++ b/ChaosTests/Cipher/DesCryptTests.cpp @@ -1,8 +1,10 @@ #include #include "Cipher/Block/Des/DesCrypt.hpp" +#include "Cipher/Block/Encryptor.hpp" using namespace Chaos::Cipher::Block::Des; +using namespace Chaos::Cipher::Block; TEST(DesCryptTests, KeyScheduleTest) { @@ -494,3 +496,44 @@ TEST(DesCryptTests, OutIteratorUsageDecryptTest) ASSERT_EQ(8, incrementCalls); } } + +template +static std::array EncryptThroughBase(Encryptor & enc, + InputIt begin, InputIt end) +{ + std::array result; + enc.EncryptBlock(result.begin(), begin, end); + return result; +} + +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 }; + + DesCrypt::Key desKey(key.begin(), key.end()); + DesCrypt::DesEncryptor enc(desKey); + + ASSERT_EQ(expected, EncryptThroughBase(enc, data.begin(), data.end())); +} + +template +static uint64_t EncryptUInt64BlockThroughBase(Encryptor & enc, uint64_t block) +{ + return enc.EncryptBlock(block); +} + +TEST(DesCryptTests, EncryptUInt64BlockThroughBaseTest) +{ + std::array key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 }; + + uint64_t data = 0x0123456789abcdef; + uint64_t expected = 0x85e813540f0ab405; + + DesCrypt::Key desKey(key.begin(), key.end()); + DesCrypt::DesEncryptor enc(desKey); + + ASSERT_EQ(expected, EncryptUInt64BlockThroughBase(enc, data)); +} From 73c455901be25e503ddbf5554655c7dc80a8a1ab Mon Sep 17 00:00:00 2001 From: hashlag Date: Sat, 31 Jan 2026 18:35:23 +0300 Subject: [PATCH 4/4] Introduce Block::Decryptor<> CRTP base class. Inherit DesDecryptor from it. --- Chaos/Cipher/Block/Decryptor.hpp | 40 ++++++++++++++++++++++++++++ Chaos/Cipher/Block/Des/DesCrypt.hpp | 3 ++- ChaosTests/Cipher/DesCryptTests.cpp | 41 +++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 Chaos/Cipher/Block/Decryptor.hpp diff --git a/Chaos/Cipher/Block/Decryptor.hpp b/Chaos/Cipher/Block/Decryptor.hpp new file mode 100644 index 0000000..ea90e7b --- /dev/null +++ b/Chaos/Cipher/Block/Decryptor.hpp @@ -0,0 +1,40 @@ +#ifndef CHAOS_CIPHER_BLOCK_DECRYPTOR_HPP +#define CHAOS_CIPHER_BLOCK_DECRYPTOR_HPP + +namespace Chaos::Cipher::Block +{ + +template +class Decryptor +{ +public: + template + void DecryptBlock(OutputIt out, InputIt inBegin, InputIt inEnd) + { + Impl().DecryptBlock(out, inBegin, inEnd); + } + + template + auto DecryptBlock(Block block) + { + return Impl().DecryptBlock(block); + } + +protected: + Decryptor() = default; + +private: + const T & Impl() const + { + return static_cast(*this); + } + + T & Impl() + { + return static_cast(*this); + } +}; + +} // namespace Chaos::Cipher::Block + +#endif // CHAOS_CIPHER_BLOCK_DECRYPTOR_HPP diff --git a/Chaos/Cipher/Block/Des/DesCrypt.hpp b/Chaos/Cipher/Block/Des/DesCrypt.hpp index d4a2c11..50b540d 100644 --- a/Chaos/Cipher/Block/Des/DesCrypt.hpp +++ b/Chaos/Cipher/Block/Des/DesCrypt.hpp @@ -8,6 +8,7 @@ #include "Service/SeArray.hpp" #include "Cipher/Block/Encryptor.hpp" +#include "Cipher/Block/Decryptor.hpp" namespace Chaos::Cipher::Block::Des::Inner_ { @@ -258,7 +259,7 @@ public: Inner_::KeySchedule Schedule_; }; - class DesDecryptor + class DesDecryptor : public Decryptor { public: DesDecryptor(const Key & key) diff --git a/ChaosTests/Cipher/DesCryptTests.cpp b/ChaosTests/Cipher/DesCryptTests.cpp index 6b16c80..f7e4139 100644 --- a/ChaosTests/Cipher/DesCryptTests.cpp +++ b/ChaosTests/Cipher/DesCryptTests.cpp @@ -537,3 +537,44 @@ TEST(DesCryptTests, EncryptUInt64BlockThroughBaseTest) ASSERT_EQ(expected, EncryptUInt64BlockThroughBase(enc, data)); } + +template +static std::array DecryptThroughBase(Decryptor & dec, + InputIt begin, InputIt end) +{ + std::array result; + dec.DecryptBlock(result.begin(), begin, end); + return result; +} + +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 }; + + DesCrypt::Key desKey(key.begin(), key.end()); + DesCrypt::DesDecryptor dec(desKey); + + ASSERT_EQ(expected, DecryptThroughBase(dec, data.begin(), data.end())); +} + +template +static uint64_t DecryptUInt64BlockThroughBase(Decryptor & dec, uint64_t block) +{ + return dec.DecryptBlock(block); +} + +TEST(DesCryptTests, DecryptUInt64BlockThroughBaseTest) +{ + std::array key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 }; + + uint64_t data = 0x85e813540f0ab405; + uint64_t expected = 0x0123456789abcdef; + + DesCrypt::Key desKey(key.begin(), key.end()); + DesCrypt::DesDecryptor dec(desKey); + + ASSERT_EQ(expected, DecryptUInt64BlockThroughBase(dec, data)); +}