From 5e35462667ee09e6d83ea2b881ba8da9869ed51b Mon Sep 17 00:00:00 2001 From: hashlag Date: Wed, 14 Jan 2026 01:31:26 +0300 Subject: [PATCH 01/13] Add DES key schedule pre-draft implementation --- Chaos/Cipher/Des/DesCrypt.hpp | 162 ++++++++++++++++++++++++++++ ChaosTests/CMakeLists.txt | 1 + ChaosTests/Cipher/DesCryptTests.cpp | 38 +++++++ 3 files changed, 201 insertions(+) create mode 100644 Chaos/Cipher/Des/DesCrypt.hpp create mode 100644 ChaosTests/Cipher/DesCryptTests.cpp diff --git a/Chaos/Cipher/Des/DesCrypt.hpp b/Chaos/Cipher/Des/DesCrypt.hpp new file mode 100644 index 0000000..c2bf738 --- /dev/null +++ b/Chaos/Cipher/Des/DesCrypt.hpp @@ -0,0 +1,162 @@ +#ifndef CHAOS_CIPHER_DES_DESCRYPT_HPP +#define CHAOS_CIPHER_DES_DESCRYPT_HPP + +#include "Service/SeArray.hpp" + +namespace Chaos::Cipher::Des::Inner_ +{ + +struct Bitwise +{ + template + static uint8_t GetBit(uint64_t value, int_fast8_t bitNumber) + { + return (value >> (BitsUsed - bitNumber)) & 0b1; + } + + template + static void SetBit(uint64_t & value, int_fast8_t bitNumber) + { + value |= (static_cast(0b1) << (BitsUsed - bitNumber)); + } + + template + static uint64_t TableChoice(uint64_t value, InputIt tableBegin, InputIt tableEnd) + { + uint64_t result = 0; + + int_fast8_t i = 1; + for (InputIt it = tableBegin; it != tableEnd; ++it, ++i) + { + if (GetBit(value, *it)) + { + SetBit(result, i); + } + } + + return result; + } + + template + static constexpr uint64_t Mask() + { + return (static_cast(0b1) << Bits) - static_cast(0b1); + } + + template + static void Rotl(uint64_t & value, int_fast8_t shift) + { + value = ((value << shift) | (value >> (BitsUsed - shift))) & Mask(); + } + + template + static std::pair Split(uint64_t value) + { + return { value >> BitsUsedOut, value & Mask() }; + } + + template + static uint64_t Merge(uint64_t lhs, uint64_t rhs) + { + return (lhs << BitsUsedIn) | rhs; + } +}; + +using RawKeyArray = Service::SeArray; + +class KeySchedule +{ +public: + using Key64 = uint64_t; + using Key56 = uint64_t; + + using RoundKey48 = uint64_t; + + KeySchedule(const RawKeyArray & rawKeyArray) + { + Key64 key64 = 0; + + key64 |= static_cast(rawKeyArray[7]) << 0; + key64 |= static_cast(rawKeyArray[6]) << 8; + key64 |= static_cast(rawKeyArray[5]) << 16; + key64 |= static_cast(rawKeyArray[4]) << 24; + key64 |= static_cast(rawKeyArray[3]) << 32; + key64 |= static_cast(rawKeyArray[2]) << 40; + key64 |= static_cast(rawKeyArray[1]) << 48; + key64 |= static_cast(rawKeyArray[0]) << 56; + + Key56 key56 = Pc1(key64); + + auto [c28, d28] = Bitwise::Split<28>(key56); + + for (int_fast8_t i = 0; i < Schedule_.Size(); ++i) + { + if (i == 0 || i == 1 || i == 8 || i == 15) + { + Bitwise::Rotl<28>(c28, 1); + Bitwise::Rotl<28>(d28, 1); + } + else + { + Bitwise::Rotl<28>(c28, 2); + Bitwise::Rotl<28>(d28, 2); + } + + Schedule_[i] = Pc2(Bitwise::Merge<28>(c28, d28)); + } + } + + RoundKey48 operator[](int_fast8_t i) const + { + return Schedule_[i]; + } + +private: + Service::SeArray Schedule_; + + static Key56 Pc1(Key64 key) + { + constexpr int_fast8_t PC1_TABLE[] = + { + 57, 49, 41, 33, 25, 17, 9, + 1, 58, 50, 42, 34, 26, 18, + 10, 2, 59, 51, 43, 35, 27, + 19, 11, 3, 60, 52, 44, 36, + 63, 55, 47, 39, 31, 23, 15, + 7, 62, 54, 46, 38, 30, 22, + 14, 6, 61, 53, 45, 37, 29, + 21, 13, 5, 28, 20, 12, 4 + }; + + static_assert(std::size(PC1_TABLE) == 56); + + return Bitwise::TableChoice<64, 56>(key, + PC1_TABLE, + PC1_TABLE + std::size(PC1_TABLE)); + } + + static RoundKey48 Pc2(Key56 key) + { + constexpr int_fast8_t PC2_TABLE[] = + { + 14, 17, 11, 24, 1, 5, + 3, 28, 15, 6, 21, 10, + 23, 19, 12, 4, 26, 8, + 16, 7, 27, 20, 13, 2, + 41, 52, 31, 37, 47, 55, + 30, 40, 51, 45, 33, 48, + 44, 49, 39, 56, 34, 53, + 46, 42, 50, 36, 29, 32 + }; + + static_assert(std::size(PC2_TABLE) == 48); + + return Bitwise::TableChoice<56, 48>(key, + PC2_TABLE, + PC2_TABLE + std::size(PC2_TABLE)); + } +}; + +} // namespace Chaos::Cipher::Des::Inner_ + +#endif // CHAOS_CIPHER_DES_DESCRYPT_HPP diff --git a/ChaosTests/CMakeLists.txt b/ChaosTests/CMakeLists.txt index ec4e058..58d83f8 100644 --- a/ChaosTests/CMakeLists.txt +++ b/ChaosTests/CMakeLists.txt @@ -17,6 +17,7 @@ set(ChaosTests_SOURCE Hash/Md4HasherTests.cpp Mac/HmacTests.cpp Cipher/Arc4GenTests.cpp Cipher/Arc4CryptTests.cpp + Cipher/DesCryptTests.cpp Service/SeArrayTests.cpp) add_executable(ChaosTests ${ChaosTests_SOURCE}) diff --git a/ChaosTests/Cipher/DesCryptTests.cpp b/ChaosTests/Cipher/DesCryptTests.cpp new file mode 100644 index 0000000..1816b7e --- /dev/null +++ b/ChaosTests/Cipher/DesCryptTests.cpp @@ -0,0 +1,38 @@ +#include + +#include "Cipher/Des/DesCrypt.hpp" + +using namespace Chaos::Cipher::Des; + +TEST(DesCryptTests, KeyScheduleTest) +{ + Inner_::RawKeyArray key; + + key[0] = 0b00010011; + key[1] = 0b00110100; + key[2] = 0b01010111; + key[3] = 0b01111001; + key[4] = 0b10011011; + key[5] = 0b10111100; + key[6] = 0b11011111; + key[7] = 0b11110001; + + Inner_::KeySchedule schedule(key); + + ASSERT_EQ(0b000110110000001011101111111111000111000001110010ULL, schedule[0]); + ASSERT_EQ(0b011110011010111011011001110110111100100111100101ULL, schedule[1]); + ASSERT_EQ(0b010101011111110010001010010000101100111110011001ULL, schedule[2]); + ASSERT_EQ(0b011100101010110111010110110110110011010100011101ULL, schedule[3]); + ASSERT_EQ(0b011111001110110000000111111010110101001110101000ULL, schedule[4]); + ASSERT_EQ(0b011000111010010100111110010100000111101100101111ULL, schedule[5]); + ASSERT_EQ(0b111011001000010010110111111101100001100010111100ULL, schedule[6]); + ASSERT_EQ(0b111101111000101000111010110000010011101111111011ULL, schedule[7]); + ASSERT_EQ(0b111000001101101111101011111011011110011110000001ULL, schedule[8]); + ASSERT_EQ(0b101100011111001101000111101110100100011001001111ULL, schedule[9]); + ASSERT_EQ(0b001000010101111111010011110111101101001110000110ULL, schedule[10]); + ASSERT_EQ(0b011101010111000111110101100101000110011111101001ULL, schedule[11]); + ASSERT_EQ(0b100101111100010111010001111110101011101001000001ULL, schedule[12]); + ASSERT_EQ(0b010111110100001110110111111100101110011100111010ULL, schedule[13]); + ASSERT_EQ(0b101111111001000110001101001111010011111100001010ULL, schedule[14]); + ASSERT_EQ(0b110010110011110110001011000011100001011111110101ULL, schedule[15]); +} From 51c2da1e4866fa503b1dc14a1b8666ae29a48483 Mon Sep 17 00:00:00 2001 From: hashlag Date: Wed, 14 Jan 2026 01:33:35 +0300 Subject: [PATCH 02/13] Explicitly #include in DesCrypt.hpp (std::pair<> used) --- Chaos/Cipher/Des/DesCrypt.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Chaos/Cipher/Des/DesCrypt.hpp b/Chaos/Cipher/Des/DesCrypt.hpp index c2bf738..4af001c 100644 --- a/Chaos/Cipher/Des/DesCrypt.hpp +++ b/Chaos/Cipher/Des/DesCrypt.hpp @@ -1,6 +1,8 @@ #ifndef CHAOS_CIPHER_DES_DESCRYPT_HPP #define CHAOS_CIPHER_DES_DESCRYPT_HPP +#include + #include "Service/SeArray.hpp" namespace Chaos::Cipher::Des::Inner_ From d63855a505bbb11517ea32902c2670c7c5a763bc Mon Sep 17 00:00:00 2001 From: hashlag Date: Thu, 15 Jan 2026 03:14:45 +0300 Subject: [PATCH 03/13] Add Bitwise::PackUInt64(<...>) utility function --- Chaos/Cipher/Des/DesCrypt.hpp | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/Chaos/Cipher/Des/DesCrypt.hpp b/Chaos/Cipher/Des/DesCrypt.hpp index 4af001c..07cac48 100644 --- a/Chaos/Cipher/Des/DesCrypt.hpp +++ b/Chaos/Cipher/Des/DesCrypt.hpp @@ -62,6 +62,20 @@ struct Bitwise { return (lhs << BitsUsedIn) | rhs; } + + template + static uint64_t PackUInt64(InputIt begin, InputIt end) + { + uint64_t result = 0; + + int_fast8_t i = 0; + for (InputIt it = begin; i < 8 && it != end; ++i, ++it) + { + result |= static_cast(*it) << (56 - (i * 8)); + } + + return result; + } }; using RawKeyArray = Service::SeArray; @@ -76,18 +90,7 @@ public: KeySchedule(const RawKeyArray & rawKeyArray) { - Key64 key64 = 0; - - key64 |= static_cast(rawKeyArray[7]) << 0; - key64 |= static_cast(rawKeyArray[6]) << 8; - key64 |= static_cast(rawKeyArray[5]) << 16; - key64 |= static_cast(rawKeyArray[4]) << 24; - key64 |= static_cast(rawKeyArray[3]) << 32; - key64 |= static_cast(rawKeyArray[2]) << 40; - key64 |= static_cast(rawKeyArray[1]) << 48; - key64 |= static_cast(rawKeyArray[0]) << 56; - - Key56 key56 = Pc1(key64); + Key56 key56 = Pc1(Bitwise::PackUInt64(rawKeyArray.Begin(), rawKeyArray.End())); auto [c28, d28] = Bitwise::Split<28>(key56); From 9e67a7e4f4055b0ff275ba3ff970bae7159acf51 Mon Sep 17 00:00:00 2001 From: hashlag Date: Mon, 19 Jan 2026 02:30:06 +0300 Subject: [PATCH 04/13] Add DES main algorithm draft implementation --- Chaos/Cipher/Des/DesCrypt.hpp | 256 ++++++++++++++++++++++++++++ ChaosTests/Cipher/DesCryptTests.cpp | 188 ++++++++++++++++++++ 2 files changed, 444 insertions(+) diff --git a/Chaos/Cipher/Des/DesCrypt.hpp b/Chaos/Cipher/Des/DesCrypt.hpp index 07cac48..67e7f3d 100644 --- a/Chaos/Cipher/Des/DesCrypt.hpp +++ b/Chaos/Cipher/Des/DesCrypt.hpp @@ -3,6 +3,7 @@ #include +#include "Service/ChaosException.hpp" #include "Service/SeArray.hpp" namespace Chaos::Cipher::Des::Inner_ @@ -76,6 +77,15 @@ struct Bitwise return result; } + + template + static void CrunchUInt64(OutputIt out, uint64_t value) + { + for (int_fast8_t i = 0; i < 8; ++i) + { + *out++ = (value >> (56 - (i * 8))) & Mask<8>(); + } + } }; using RawKeyArray = Service::SeArray; @@ -164,4 +174,250 @@ private: } // namespace Chaos::Cipher::Des::Inner_ +namespace Chaos::Cipher::Des +{ + +class DesCrypt +{ +public: + DesCrypt() = delete; + + class Key + { + friend class DesCrypt; + public: + template + Key(InputIt keyBegin, InputIt keyEnd) + { + int_fast8_t i = 0; + InputIt keyIt = keyBegin; + for (; i < Key_.Size() && keyIt != keyEnd; ++i, ++keyIt) + { + Key_[i] = *keyIt; + } + + if (i != Key_.Size() || keyIt != keyEnd) + { + throw Service::ChaosException("DesCrypt::Key: invalid key length " + "(8 bytes required)"); + } + } + + private: + Inner_::RawKeyArray Key_; + }; + + template + static void EncryptBlock(OutputIt out, InputIt inBegin, InputIt inEnd, const Key & key) + { + RawBlockArray block; + + int_fast8_t i = 0; + for (InputIt in = inBegin; i < block.Size() && in != inEnd; ++i, ++in) + { + block[i] = *in; + } + + Block encrypted = EncryptBlock(Inner_::Bitwise::PackUInt64(block.Begin(), block.End()), key); + + Inner_::Bitwise::CrunchUInt64(out, encrypted); + } + +private: + using Block = uint64_t; + using BlockHalf = uint32_t; + using RawBlockArray = Service::SeArray; + using Data48 = uint64_t; + using Data32 = uint32_t; + using Data6 = uint8_t; + using Data4 = uint8_t; + + static Data48 E(Data32 value) + { + constexpr int_fast8_t E_TABLE[] = + { + 32, 1, 2, 3, 4, 5, + 4, 5, 6, 7, 8, 9, + 8, 9, 10, 11, 12, 13, + 12, 13, 14, 15, 16, 17, + 16, 17, 18, 19, 20, 21, + 20, 21, 22, 23, 24, 25, + 24, 25, 26, 27, 28, 29, + 28, 29, 30, 31, 32, 1 + }; + + static_assert(std::size(E_TABLE) == 48); + + return Inner_::Bitwise::TableChoice<32, 48>(value, + E_TABLE, + E_TABLE + std::size(E_TABLE)); + } + + static Data32 SBlock(Data48 value) + { + constexpr Data4 SBOX_TABLES[][64] = + { + { + 14, 0, 4, 15, 13, 7, 1, 4, 2, 14, 15, 2, 11, 13, 8, 1, + 3, 10, 10, 6, 6, 12, 12, 11, 5, 9, 9, 5, 0, 3, 7, 8, + 4, 15, 1, 12, 14, 8, 8, 2, 13, 4, 6, 9, 2, 1, 11, 7, + 15, 5, 12, 11, 9, 3, 7, 14, 3, 10, 10, 0, 5, 6, 0, 13 + }, + { + 15, 3, 1, 13, 8, 4, 14, 7, 6, 15, 11, 2, 3, 8, 4, 14, + 9, 12, 7, 0, 2, 1, 13, 10, 12, 6, 0, 9, 5, 11, 10, 5, + 0, 13, 14, 8, 7, 10, 11, 1, 10, 3, 4, 15, 13, 4, 1, 2, + 5, 11, 8, 6, 12, 7, 6, 12, 9, 0, 3, 5, 2, 14, 15, 9 + }, + { + 10, 13, 0, 7, 9, 0, 14, 9, 6, 3, 3, 4, 15, 6, 5, 10, + 1, 2, 13, 8, 12, 5, 7, 14, 11, 12, 4, 11, 2, 15, 8, 1, + 13, 1, 6, 10, 4, 13, 9, 0, 8, 6, 15, 9, 3, 8, 0, 7, + 11, 4, 1, 15, 2, 14, 12, 3, 5, 11, 10, 5, 14, 2, 7, 12 + }, + { + 7, 13, 13, 8, 14, 11, 3, 5, 0, 6, 6, 15, 9, 0, 10, 3, + 1, 4, 2, 7, 8, 2, 5, 12, 11, 1, 12, 10, 4, 14, 15, 9, + 10, 3, 6, 15, 9, 0, 0, 6, 12, 10, 11, 1, 7, 13, 13, 8, + 15, 9, 1, 4, 3, 5, 14, 11, 5, 12, 2, 7, 8, 2, 4, 14 + }, + { + 2, 14, 12, 11, 4, 2, 1, 12, 7, 4, 10, 7, 11, 13, 6, 1, + 8, 5, 5, 0, 3, 15, 15, 10, 13, 3, 0, 9, 14, 8, 9, 6, + 4, 11, 2, 8, 1, 12, 11, 7, 10, 1, 13, 14, 7, 2, 8, 13, + 15, 6, 9, 15, 12, 0, 5, 9, 6, 10, 3, 4, 0, 5, 14, 3 + }, + { + 12, 10, 1, 15, 10, 4, 15, 2, 9, 7, 2, 12, 6, 9, 8, 5, + 0, 6, 13, 1, 3, 13, 4, 14, 14, 0, 7, 11, 5, 3, 11, 8, + 9, 4, 14, 3, 15, 2, 5, 12, 2, 9, 8, 5, 12, 15, 3, 10, + 7, 11, 0, 14, 4, 1, 10, 7, 1, 6, 13, 0, 11, 8, 6, 13 + }, + { + 4, 13, 11, 0, 2, 11, 14, 7, 15, 4, 0, 9, 8, 1, 13, 10, + 3, 14, 12, 3, 9, 5, 7, 12, 5, 2, 10, 15, 6, 8, 1, 6, + 1, 6, 4, 11, 11, 13, 13, 8, 12, 1, 3, 4, 7, 10, 14, 7, + 10, 9, 15, 5, 6, 0, 8, 15, 0, 14, 5, 2, 9, 3, 2, 12 + }, + { + 13, 1, 2, 15, 8, 13, 4, 8, 6, 10, 15, 3, 11, 7, 1, 4, + 10, 12, 9, 5, 3, 6, 14, 11, 5, 0, 0, 14, 12, 9, 7, 2, + 7, 2, 11, 1, 4, 14, 1, 7, 9, 4, 12, 10, 14, 8, 2, 13, + 0, 15, 6, 12, 10, 9, 13, 0, 15, 3, 3, 5, 5, 6, 8, 11 + } + }; + + static_assert(std::size(SBOX_TABLES) == 8); + + Data32 result = 0; + + for (int_fast8_t i = 0; i < 8; ++i) + { + Data6 input = (value >> (42 - (i * 6))) & Inner_::Bitwise::Mask<6>(); + result |= static_cast(SBOX_TABLES[i][input]) << (28 - (i * 4)); + } + + return result; + } + + static Data32 P(Data32 value) + { + constexpr int_fast8_t P_TABLE[] = + { + 16, 7, 20, 21, + 29, 12, 28, 17, + 1, 15, 23, 26, + 5, 18, 31, 10, + 2, 8, 24, 14, + 32, 27, 3, 9, + 19, 13, 30, 6, + 22, 11, 4, 25 + }; + + static_assert(std::size(P_TABLE) == 32); + + return Inner_::Bitwise::TableChoice<32, 32>(value, + P_TABLE, + P_TABLE + std::size(P_TABLE)); + } + + static BlockHalf F(BlockHalf value, Inner_::KeySchedule::RoundKey48 roundKey) + { + Data48 expanded = E(value); + expanded = (expanded ^ roundKey) & Inner_::Bitwise::Mask<48>(); + + return P(SBlock(expanded)); + } + + static Block Ip(Block block) + { + constexpr int_fast8_t IP_TABLE[] = + { + 58, 50, 42, 34, 26, 18, 10, 2, + 60, 52, 44, 36, 28, 20, 12, 4, + 62, 54, 46, 38, 30, 22, 14, 6, + 64, 56, 48, 40, 32, 24, 16, 8, + 57, 49, 41, 33, 25, 17, 9, 1, + 59, 51, 43, 35, 27, 19, 11, 3, + 61, 53, 45, 37, 29, 21, 13, 5, + 63, 55, 47, 39, 31, 23, 15, 7 + }; + + static_assert(std::size(IP_TABLE) == 64); + + return Inner_::Bitwise::TableChoice<64, 64>(block, + IP_TABLE, + IP_TABLE + std::size(IP_TABLE)); + } + + static Block Fp(Block block) + { + constexpr int_fast8_t FP_TABLE[] = + { + 40, 8, 48, 16, 56, 24, 64, 32, + 39, 7, 47, 15, 55, 23, 63, 31, + 38, 6, 46, 14, 54, 22, 62, 30, + 37, 5, 45, 13, 53, 21, 61, 29, + 36, 4, 44, 12, 52, 20, 60, 28, + 35, 3, 43, 11, 51, 19, 59, 27, + 34, 2, 42, 10, 50, 18, 58, 26, + 33, 1, 41, 9, 49, 17, 57, 25 + }; + + static_assert(std::size(FP_TABLE) == 64); + + return Inner_::Bitwise::TableChoice<64, 64>(block, + FP_TABLE, + FP_TABLE + std::size(FP_TABLE)); + } + + static Block EncryptBlock(Block block, const Key & key) + { + Inner_::KeySchedule schedule(key.Key_); + + block = Ip(block); + + uint32_t l32; + uint32_t r32; + + { + auto [l, r] = Inner_::Bitwise::Split<32>(block); + l32 = static_cast(l); + r32 = static_cast(r); + } + + for (int_fast8_t i = 0; i < 16; ++i) + { + uint32_t l32Old = l32; + + l32 = r32; + r32 = l32Old ^ F(r32, schedule[i]); + } + + return Fp(Inner_::Bitwise::Merge<32>(r32, l32)); + } +}; + +} // namespace Chaos::Cipher::Des + #endif // CHAOS_CIPHER_DES_DESCRYPT_HPP diff --git a/ChaosTests/Cipher/DesCryptTests.cpp b/ChaosTests/Cipher/DesCryptTests.cpp index 1816b7e..6dbabff 100644 --- a/ChaosTests/Cipher/DesCryptTests.cpp +++ b/ChaosTests/Cipher/DesCryptTests.cpp @@ -36,3 +36,191 @@ TEST(DesCryptTests, KeyScheduleTest) ASSERT_EQ(0b101111111001000110001101001111010011111100001010ULL, schedule[14]); ASSERT_EQ(0b110010110011110110001011000011100001011111110101ULL, schedule[15]); } + +TEST(DesCryptTests, EncryptTest) +{ + struct Helper + { + std::array operator()(const std::array & data, + const std::array & key) const + { + std::array result; + result.fill(0); + + DesCrypt::Key desKey(key.begin(), key.end()); + DesCrypt::EncryptBlock(result.begin(), data.begin(), data.end(), desKey); + + return result; + } + }; + + Helper des; + + { + 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 }; + + ASSERT_EQ(expected, des(data, key)); + } + + { + 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 }; + + ASSERT_EQ(expected, des(data, key)); + } + + { + 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 }; + + ASSERT_EQ(expected, des(data, key)); + } +} + +TEST(DesCryptTests, EncryptShortDataTest) +{ + struct Helper + { + std::vector operator()(const std::vector & data, + const std::vector & key) + { + std::vector result; + result.resize(8, 0); + + DesCrypt::Key desKey(key.begin(), key.end()); + DesCrypt::EncryptBlock(result.begin(), data.begin(), data.end(), desKey); + + return result; + } + }; + + Helper des; + + { + // treated as { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0x00, 0x00, 0x00 } + std::vector dataShort = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19 }; + + std::vector data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0x00, 0x00, 0x00 }; + std::vector key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab }; + + std::vector expected = { 0xd8, 0xa8, 0xb8, 0xb4, 0xc0, 0x9b, 0x04, 0x09 }; + + ASSERT_EQ(expected, des(data, key)); + ASSERT_EQ(expected, des(dataShort, key)); + } +} + +TEST(DesCryptTests, EncryptLongDataTest) +{ + struct Helper + { + std::vector operator()(const std::vector & data, + const std::vector & key) + { + std::vector result; + result.resize(8, 0); + + DesCrypt::Key desKey(key.begin(), key.end()); + DesCrypt::EncryptBlock(result.begin(), data.begin(), data.end(), desKey); + + return result; + } + }; + + Helper des; + + { + // treated as { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44 } + std::vector dataLong = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44, 0xaa, 0xbb }; + + std::vector data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44 }; + std::vector key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab }; + + std::vector expected = { 0x42, 0x27, 0x88, 0xa6, 0x7b, 0x6c, 0x18, 0xed }; + + ASSERT_EQ(expected, des(data, key)); + ASSERT_EQ(expected, des(dataLong, key)); + } +} + +TEST(DesCryptTests, ShortKeyTest) +{ + { + std::array key = {}; + ASSERT_THROW(DesCrypt::Key(key.begin(), key.end()), Chaos::Service::ChaosException); + } +} + +TEST(DesCryptTests, LongKeyTest) +{ + { + std::array key = {}; + ASSERT_THROW(DesCrypt::Key(key.begin(), key.end()), Chaos::Service::ChaosException); + } +} + +TEST(DesCryptTests, OutIteratorUsageTest) +{ + struct OutputItMock + { + OutputItMock(size_t & asteriskCalls, size_t & incrementCalls) + : AsteriskCalls_(asteriskCalls) + , IncrementCalls_(incrementCalls) + { } + + uint8_t & operator*() + { + ++AsteriskCalls_; + + static uint8_t dummy = 0; + return dummy; + } + + OutputItMock operator++(int) + { + ++IncrementCalls_; + + return *this; + } + + size_t & AsteriskCalls_; + size_t & IncrementCalls_; + }; + + { + 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; + size_t incrementCalls = 0; + OutputItMock it(asteriskCalls, incrementCalls); + + DesCrypt::Key desKey(key.begin(), key.end()); + DesCrypt::EncryptBlock(it, data.begin(), data.end(), desKey); + + ASSERT_EQ(8, asteriskCalls); + ASSERT_EQ(8, incrementCalls); + } + + { + std::array data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f }; + std::array key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab }; + + size_t asteriskCalls = 0; + size_t incrementCalls = 0; + OutputItMock it(asteriskCalls, incrementCalls); + + DesCrypt::Key desKey(key.begin(), key.end()); + DesCrypt::EncryptBlock(it, data.begin(), data.end(), desKey); + + ASSERT_EQ(8, asteriskCalls); + ASSERT_EQ(8, incrementCalls); + } +} From fa042e7abf601aae07c51a09b425e54db1e88010 Mon Sep 17 00:00:00 2001 From: hashlag Date: Wed, 21 Jan 2026 23:35:15 +0300 Subject: [PATCH 05/13] Rename RawKeyArray --> RawKey since it's not an 'array of raw keys' --- Chaos/Cipher/Des/DesCrypt.hpp | 8 ++++---- ChaosTests/Cipher/DesCryptTests.cpp | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Chaos/Cipher/Des/DesCrypt.hpp b/Chaos/Cipher/Des/DesCrypt.hpp index 67e7f3d..247993b 100644 --- a/Chaos/Cipher/Des/DesCrypt.hpp +++ b/Chaos/Cipher/Des/DesCrypt.hpp @@ -88,7 +88,7 @@ struct Bitwise } }; -using RawKeyArray = Service::SeArray; +using RawKey = Service::SeArray; class KeySchedule { @@ -98,9 +98,9 @@ public: using RoundKey48 = uint64_t; - KeySchedule(const RawKeyArray & rawKeyArray) + KeySchedule(const RawKey & rawKey) { - Key56 key56 = Pc1(Bitwise::PackUInt64(rawKeyArray.Begin(), rawKeyArray.End())); + Key56 key56 = Pc1(Bitwise::PackUInt64(rawKey.Begin(), rawKey.End())); auto [c28, d28] = Bitwise::Split<28>(key56); @@ -204,7 +204,7 @@ public: } private: - Inner_::RawKeyArray Key_; + Inner_::RawKey Key_; }; template diff --git a/ChaosTests/Cipher/DesCryptTests.cpp b/ChaosTests/Cipher/DesCryptTests.cpp index 6dbabff..73828ec 100644 --- a/ChaosTests/Cipher/DesCryptTests.cpp +++ b/ChaosTests/Cipher/DesCryptTests.cpp @@ -6,7 +6,7 @@ using namespace Chaos::Cipher::Des; TEST(DesCryptTests, KeyScheduleTest) { - Inner_::RawKeyArray key; + Inner_::RawKey key; key[0] = 0b00010011; key[1] = 0b00110100; From 3a2a6650312a2c8c950cfc46b66beb5ff2fe06f7 Mon Sep 17 00:00:00 2001 From: hashlag Date: Thu, 22 Jan 2026 22:36:14 +0300 Subject: [PATCH 06/13] Fix a layering issue: private EncryptBlock() should use a private key representation --- Chaos/Cipher/Des/DesCrypt.hpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Chaos/Cipher/Des/DesCrypt.hpp b/Chaos/Cipher/Des/DesCrypt.hpp index 247993b..374225a 100644 --- a/Chaos/Cipher/Des/DesCrypt.hpp +++ b/Chaos/Cipher/Des/DesCrypt.hpp @@ -218,7 +218,8 @@ public: block[i] = *in; } - Block encrypted = EncryptBlock(Inner_::Bitwise::PackUInt64(block.Begin(), block.End()), key); + Inner_::KeySchedule schedule(key.Key_); + Block encrypted = EncryptBlock(Inner_::Bitwise::PackUInt64(block.Begin(), block.End()), schedule); Inner_::Bitwise::CrunchUInt64(out, encrypted); } @@ -391,10 +392,8 @@ private: FP_TABLE + std::size(FP_TABLE)); } - static Block EncryptBlock(Block block, const Key & key) + static Block EncryptBlock(Block block, const Inner_::KeySchedule & schedule) { - Inner_::KeySchedule schedule(key.Key_); - block = Ip(block); uint32_t l32; From da5eaff18268316c1aa9621d7f45849f5a4c9922 Mon Sep 17 00:00:00 2001 From: hashlag Date: Fri, 23 Jan 2026 21:31:04 +0300 Subject: [PATCH 07/13] Introduce stateful DesCrypt::Encryptor class, remove static DesCrypt::EncryptBlock() --- Chaos/Cipher/Des/DesCrypt.hpp | 32 +++++++++++++++++++---------- ChaosTests/Cipher/DesCryptTests.cpp | 15 +++++++++----- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/Chaos/Cipher/Des/DesCrypt.hpp b/Chaos/Cipher/Des/DesCrypt.hpp index 374225a..6d0f9a4 100644 --- a/Chaos/Cipher/Des/DesCrypt.hpp +++ b/Chaos/Cipher/Des/DesCrypt.hpp @@ -207,22 +207,32 @@ public: Inner_::RawKey Key_; }; - template - static void EncryptBlock(OutputIt out, InputIt inBegin, InputIt inEnd, const Key & key) + class Encryptor { - RawBlockArray block; + public: + Encryptor(const Key & key) + : Schedule_(key.Key_) + { } - int_fast8_t i = 0; - for (InputIt in = inBegin; i < block.Size() && in != inEnd; ++i, ++in) + template + void EncryptBlock(OutputIt out, InputIt inBegin, InputIt inEnd) { - block[i] = *in; + RawBlockArray block; + + int_fast8_t i = 0; + for (InputIt in = inBegin; i < block.Size() && in != inEnd; ++i, ++in) + { + block[i] = *in; + } + + Block encrypted = DesCrypt::EncryptBlock(Inner_::Bitwise::PackUInt64(block.Begin(), block.End()), Schedule_); + + Inner_::Bitwise::CrunchUInt64(out, encrypted); } - Inner_::KeySchedule schedule(key.Key_); - Block encrypted = EncryptBlock(Inner_::Bitwise::PackUInt64(block.Begin(), block.End()), schedule); - - Inner_::Bitwise::CrunchUInt64(out, encrypted); - } + private: + Inner_::KeySchedule Schedule_; + }; private: using Block = uint64_t; diff --git a/ChaosTests/Cipher/DesCryptTests.cpp b/ChaosTests/Cipher/DesCryptTests.cpp index 73828ec..d5c6b61 100644 --- a/ChaosTests/Cipher/DesCryptTests.cpp +++ b/ChaosTests/Cipher/DesCryptTests.cpp @@ -48,7 +48,8 @@ TEST(DesCryptTests, EncryptTest) result.fill(0); DesCrypt::Key desKey(key.begin(), key.end()); - DesCrypt::EncryptBlock(result.begin(), data.begin(), data.end(), desKey); + DesCrypt::Encryptor enc(desKey); + enc.EncryptBlock(result.begin(), data.begin(), data.end()); return result; } @@ -95,7 +96,8 @@ TEST(DesCryptTests, EncryptShortDataTest) result.resize(8, 0); DesCrypt::Key desKey(key.begin(), key.end()); - DesCrypt::EncryptBlock(result.begin(), data.begin(), data.end(), desKey); + DesCrypt::Encryptor enc(desKey); + enc.EncryptBlock(result.begin(), data.begin(), data.end()); return result; } @@ -128,7 +130,8 @@ TEST(DesCryptTests, EncryptLongDataTest) result.resize(8, 0); DesCrypt::Key desKey(key.begin(), key.end()); - DesCrypt::EncryptBlock(result.begin(), data.begin(), data.end(), desKey); + DesCrypt::Encryptor enc(desKey); + enc.EncryptBlock(result.begin(), data.begin(), data.end()); return result; } @@ -203,7 +206,8 @@ TEST(DesCryptTests, OutIteratorUsageTest) OutputItMock it(asteriskCalls, incrementCalls); DesCrypt::Key desKey(key.begin(), key.end()); - DesCrypt::EncryptBlock(it, data.begin(), data.end(), desKey); + DesCrypt::Encryptor enc(desKey); + enc.EncryptBlock(it, data.begin(), data.end()); ASSERT_EQ(8, asteriskCalls); ASSERT_EQ(8, incrementCalls); @@ -218,7 +222,8 @@ TEST(DesCryptTests, OutIteratorUsageTest) OutputItMock it(asteriskCalls, incrementCalls); DesCrypt::Key desKey(key.begin(), key.end()); - DesCrypt::EncryptBlock(it, data.begin(), data.end(), desKey); + DesCrypt::Encryptor enc(desKey); + enc.EncryptBlock(it, data.begin(), data.end()); ASSERT_EQ(8, asteriskCalls); ASSERT_EQ(8, incrementCalls); From 5fd92e0c9dea3f1ec5ac519c60552410aea96070 Mon Sep 17 00:00:00 2001 From: hashlag Date: Fri, 23 Jan 2026 21:32:57 +0300 Subject: [PATCH 08/13] Fix long statement formatting --- Chaos/Cipher/Des/DesCrypt.hpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Chaos/Cipher/Des/DesCrypt.hpp b/Chaos/Cipher/Des/DesCrypt.hpp index 6d0f9a4..6e6584b 100644 --- a/Chaos/Cipher/Des/DesCrypt.hpp +++ b/Chaos/Cipher/Des/DesCrypt.hpp @@ -225,7 +225,10 @@ public: block[i] = *in; } - Block encrypted = DesCrypt::EncryptBlock(Inner_::Bitwise::PackUInt64(block.Begin(), block.End()), Schedule_); + Block encrypted + = DesCrypt::EncryptBlock(Inner_::Bitwise::PackUInt64(block.Begin(), + block.End()), + Schedule_); Inner_::Bitwise::CrunchUInt64(out, encrypted); } From 15dd7398d21ad23a46c1861b972a6bf54086de06 Mon Sep 17 00:00:00 2001 From: hashlag Date: Fri, 23 Jan 2026 22:22:28 +0300 Subject: [PATCH 09/13] Implement DES decryption --- Chaos/Cipher/Des/DesCrypt.hpp | 50 +++++++- ChaosTests/Cipher/DesCryptTests.cpp | 181 +++++++++++++++++++++++++++- 2 files changed, 225 insertions(+), 6 deletions(-) diff --git a/Chaos/Cipher/Des/DesCrypt.hpp b/Chaos/Cipher/Des/DesCrypt.hpp index 6e6584b..a6881c3 100644 --- a/Chaos/Cipher/Des/DesCrypt.hpp +++ b/Chaos/Cipher/Des/DesCrypt.hpp @@ -1,6 +1,7 @@ #ifndef CHAOS_CIPHER_DES_DESCRYPT_HPP #define CHAOS_CIPHER_DES_DESCRYPT_HPP +#include #include #include "Service/ChaosException.hpp" @@ -98,7 +99,13 @@ public: using RoundKey48 = uint64_t; - KeySchedule(const RawKey & rawKey) + enum class Direction + { + Encrypt, + Decrypt + }; + + KeySchedule(Direction direction, const RawKey & rawKey) { Key56 key56 = Pc1(Bitwise::PackUInt64(rawKey.Begin(), rawKey.End())); @@ -119,6 +126,11 @@ public: Schedule_[i] = Pc2(Bitwise::Merge<28>(c28, d28)); } + + if (direction == Direction::Decrypt) + { + std::reverse(Schedule_.Begin(), Schedule_.End()); + } } RoundKey48 operator[](int_fast8_t i) const @@ -211,7 +223,7 @@ public: { public: Encryptor(const Key & key) - : Schedule_(key.Key_) + : Schedule_(Inner_::KeySchedule::Direction::Encrypt, key.Key_) { } template @@ -226,7 +238,7 @@ public: } Block encrypted - = DesCrypt::EncryptBlock(Inner_::Bitwise::PackUInt64(block.Begin(), + = DesCrypt::ProcessBlock(Inner_::Bitwise::PackUInt64(block.Begin(), block.End()), Schedule_); @@ -237,6 +249,36 @@ public: Inner_::KeySchedule Schedule_; }; + class Decryptor + { + public: + Decryptor(const Key & key) + : Schedule_(Inner_::KeySchedule::Direction::Decrypt, key.Key_) + { } + + template + void DecryptBlock(OutputIt out, InputIt inBegin, InputIt inEnd) + { + RawBlockArray block; + + int_fast8_t i = 0; + for (InputIt in = inBegin; i < block.Size() && in != inEnd; ++i, ++in) + { + block[i] = *in; + } + + Block decrypted + = DesCrypt::ProcessBlock(Inner_::Bitwise::PackUInt64(block.Begin(), + block.End()), + Schedule_); + + Inner_::Bitwise::CrunchUInt64(out, decrypted); + } + + private: + Inner_::KeySchedule Schedule_; + }; + private: using Block = uint64_t; using BlockHalf = uint32_t; @@ -405,7 +447,7 @@ private: FP_TABLE + std::size(FP_TABLE)); } - static Block EncryptBlock(Block block, const Inner_::KeySchedule & schedule) + static Block ProcessBlock(Block block, const Inner_::KeySchedule & schedule) { block = Ip(block); diff --git a/ChaosTests/Cipher/DesCryptTests.cpp b/ChaosTests/Cipher/DesCryptTests.cpp index d5c6b61..21f1d17 100644 --- a/ChaosTests/Cipher/DesCryptTests.cpp +++ b/ChaosTests/Cipher/DesCryptTests.cpp @@ -17,7 +17,7 @@ TEST(DesCryptTests, KeyScheduleTest) key[6] = 0b11011111; key[7] = 0b11110001; - Inner_::KeySchedule schedule(key); + Inner_::KeySchedule schedule(Inner_::KeySchedule::Direction::Encrypt, key); ASSERT_EQ(0b000110110000001011101111111111000111000001110010ULL, schedule[0]); ASSERT_EQ(0b011110011010111011011001110110111100100111100101ULL, schedule[1]); @@ -153,6 +153,122 @@ TEST(DesCryptTests, EncryptLongDataTest) } } +TEST(DesCryptTests, DecryptTest) +{ + struct Helper + { + std::array operator()(const std::array & data, + const std::array & key) const + { + std::array result; + result.fill(0); + + DesCrypt::Key desKey(key.begin(), key.end()); + DesCrypt::Decryptor dec(desKey); + dec.DecryptBlock(result.begin(), data.begin(), data.end()); + + return result; + } + }; + + Helper des; + + { + 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 }; + + ASSERT_EQ(expected, des(data, key)); + } + + { + 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 }; + + ASSERT_EQ(expected, des(data, key)); + } + + { + 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 }; + + ASSERT_EQ(expected, des(data, key)); + } +} + +TEST(DesCryptTests, DecryptShortDataTest) +{ + struct Helper + { + std::vector operator()(const std::vector & data, + const std::vector & key) + { + std::vector result; + result.resize(8, 0); + + DesCrypt::Key desKey(key.begin(), key.end()); + DesCrypt::Decryptor dec(desKey); + dec.DecryptBlock(result.begin(), data.begin(), data.end()); + + return result; + } + }; + + Helper des; + + { + // treated as { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0x00, 0x00, 0x00 } + std::vector dataShort = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19 }; + + std::vector data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0x00, 0x00, 0x00 }; + std::vector key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab }; + + std::vector expected = { 0x90, 0xe5, 0xf6, 0x5b, 0xe1, 0xd3, 0x8a, 0x64 }; + + ASSERT_EQ(expected, des(data, key)); + ASSERT_EQ(expected, des(dataShort, key)); + } +} + +TEST(DesCryptTests, DecryptLongDataTest) +{ + struct Helper + { + std::vector operator()(const std::vector & data, + const std::vector & key) + { + std::vector result; + result.resize(8, 0); + + DesCrypt::Key desKey(key.begin(), key.end()); + DesCrypt::Decryptor dec(desKey); + dec.DecryptBlock(result.begin(), data.begin(), data.end()); + + return result; + } + }; + + Helper des; + + { + // treated as { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44 } + std::vector dataLong = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44, 0xaa, 0xbb }; + + std::vector data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44 }; + std::vector key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab }; + + std::vector expected = { 0x45, 0x69, 0x71, 0x17, 0x13, 0xfb, 0x3e, 0xee }; + + ASSERT_EQ(expected, des(data, key)); + ASSERT_EQ(expected, des(dataLong, key)); + } +} + TEST(DesCryptTests, ShortKeyTest) { { @@ -169,7 +285,7 @@ TEST(DesCryptTests, LongKeyTest) } } -TEST(DesCryptTests, OutIteratorUsageTest) +TEST(DesCryptTests, OutIteratorUsageEncryptTest) { struct OutputItMock { @@ -229,3 +345,64 @@ TEST(DesCryptTests, OutIteratorUsageTest) ASSERT_EQ(8, incrementCalls); } } + +TEST(DesCryptTests, OutIteratorUsageDecryptTest) +{ + struct OutputItMock + { + OutputItMock(size_t & asteriskCalls, size_t & incrementCalls) + : AsteriskCalls_(asteriskCalls) + , IncrementCalls_(incrementCalls) + { } + + uint8_t & operator*() + { + ++AsteriskCalls_; + + static uint8_t dummy = 0; + return dummy; + } + + OutputItMock operator++(int) + { + ++IncrementCalls_; + + return *this; + } + + size_t & AsteriskCalls_; + size_t & IncrementCalls_; + }; + + { + 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; + size_t incrementCalls = 0; + OutputItMock it(asteriskCalls, incrementCalls); + + DesCrypt::Key desKey(key.begin(), key.end()); + DesCrypt::Decryptor dec(desKey); + dec.DecryptBlock(it, data.begin(), data.end()); + + ASSERT_EQ(8, asteriskCalls); + ASSERT_EQ(8, incrementCalls); + } + + { + std::array data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f }; + std::array key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab }; + + size_t asteriskCalls = 0; + size_t incrementCalls = 0; + OutputItMock it(asteriskCalls, incrementCalls); + + DesCrypt::Key desKey(key.begin(), key.end()); + DesCrypt::Decryptor dec(desKey); + dec.DecryptBlock(it, data.begin(), data.end()); + + ASSERT_EQ(8, asteriskCalls); + ASSERT_EQ(8, incrementCalls); + } +} From 5a2d802a25ae3f36cfa2c5c1d7bddadf088f201a Mon Sep 17 00:00:00 2001 From: hashlag Date: Sat, 24 Jan 2026 23:14:03 +0300 Subject: [PATCH 10/13] Overload EncryptBlock() for the whole block as UInt64 --- Chaos/Cipher/Des/DesCrypt.hpp | 5 ++++ ChaosTests/Cipher/DesCryptTests.cpp | 44 +++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/Chaos/Cipher/Des/DesCrypt.hpp b/Chaos/Cipher/Des/DesCrypt.hpp index a6881c3..e9ac70f 100644 --- a/Chaos/Cipher/Des/DesCrypt.hpp +++ b/Chaos/Cipher/Des/DesCrypt.hpp @@ -245,6 +245,11 @@ public: Inner_::Bitwise::CrunchUInt64(out, encrypted); } + uint64_t EncryptBlock(uint64_t block) + { + return DesCrypt::ProcessBlock(block, Schedule_); + } + private: Inner_::KeySchedule Schedule_; }; diff --git a/ChaosTests/Cipher/DesCryptTests.cpp b/ChaosTests/Cipher/DesCryptTests.cpp index 21f1d17..aad6d27 100644 --- a/ChaosTests/Cipher/DesCryptTests.cpp +++ b/ChaosTests/Cipher/DesCryptTests.cpp @@ -85,6 +85,50 @@ TEST(DesCryptTests, EncryptTest) } } +TEST(DesCryptTests, EncryptUInt64BlockTest) +{ + struct Helper + { + uint64_t operator()(uint64_t data, + const std::array & key) const + { + DesCrypt::Key desKey(key.begin(), key.end()); + DesCrypt::Encryptor enc(desKey); + + return enc.EncryptBlock(data); + } + }; + + Helper des; + + { + uint64_t data = 0x0123456789abcdef; + std::array key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 }; + + uint64_t expected = 0x85e813540f0ab405; + + ASSERT_EQ(expected, des(data, key)); + } + + { + uint64_t data = 0xaaf383162d2e6bcb; + std::array key = { 0x44, 0xbf, 0x32, 0x19, 0x99, 0x25, 0x81, 0x51 }; + + uint64_t expected = 0x07e87faab3171318; + + ASSERT_EQ(expected, des(data, key)); + } + + { + uint64_t data = 0xe51a9fd419a79344; + std::array key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab }; + + uint64_t expected = 0x422788a67b6c18ed; + + ASSERT_EQ(expected, des(data, key)); + } +} + TEST(DesCryptTests, EncryptShortDataTest) { struct Helper From e8d0b93c5e19eeec2eb91a1c20b1eb17b54f78fb Mon Sep 17 00:00:00 2001 From: hashlag Date: Sat, 24 Jan 2026 23:24:44 +0300 Subject: [PATCH 11/13] Overload DecryptBlock() for the whole block as UInt64 --- Chaos/Cipher/Des/DesCrypt.hpp | 5 ++++ ChaosTests/Cipher/DesCryptTests.cpp | 44 +++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/Chaos/Cipher/Des/DesCrypt.hpp b/Chaos/Cipher/Des/DesCrypt.hpp index e9ac70f..b2dda46 100644 --- a/Chaos/Cipher/Des/DesCrypt.hpp +++ b/Chaos/Cipher/Des/DesCrypt.hpp @@ -280,6 +280,11 @@ public: Inner_::Bitwise::CrunchUInt64(out, decrypted); } + uint64_t DecryptBlock(uint64_t block) + { + return DesCrypt::ProcessBlock(block, Schedule_); + } + private: Inner_::KeySchedule Schedule_; }; diff --git a/ChaosTests/Cipher/DesCryptTests.cpp b/ChaosTests/Cipher/DesCryptTests.cpp index aad6d27..b98a502 100644 --- a/ChaosTests/Cipher/DesCryptTests.cpp +++ b/ChaosTests/Cipher/DesCryptTests.cpp @@ -245,6 +245,50 @@ TEST(DesCryptTests, DecryptTest) } } +TEST(DesCryptTests, DecryptUInt64BlockTest) +{ + struct Helper + { + uint64_t operator()(uint64_t data, + const std::array & key) const + { + DesCrypt::Key desKey(key.begin(), key.end()); + DesCrypt::Decryptor dec(desKey); + + return dec.DecryptBlock(data); + } + }; + + Helper des; + + { + uint64_t data = 0x85e813540f0ab405; + std::array key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 }; + + uint64_t expected = 0x0123456789abcdef; + + ASSERT_EQ(expected, des(data, key)); + } + + { + uint64_t data = 0x07e87faab3171318; + std::array key = { 0x44, 0xbf, 0x32, 0x19, 0x99, 0x25, 0x81, 0x51 }; + + uint64_t expected = 0xaaf383162d2e6bcb; + + ASSERT_EQ(expected, des(data, key)); + } + + { + uint64_t data = 0x422788a67b6c18ed; + std::array key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab }; + + uint64_t expected = 0xe51a9fd419a79344; + + ASSERT_EQ(expected, des(data, key)); + } +} + TEST(DesCryptTests, DecryptShortDataTest) { struct Helper From 66413e2cfc4c7149730a38731d739a5f97de0477 Mon Sep 17 00:00:00 2001 From: hashlag Date: Sat, 24 Jan 2026 23:44:57 +0300 Subject: [PATCH 12/13] Add .cache/ to .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index e524d79..2994ef0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ +.cache/ .vscode/ build/ From 0ca97cc33631d2d0d8ec783bfd6555a5fb9a60e7 Mon Sep 17 00:00:00 2001 From: hashlag Date: Sat, 24 Jan 2026 23:53:04 +0300 Subject: [PATCH 13/13] Use DesCrypt::Block type alias in EncryptBlock()/DecryptBlock() signatures --- Chaos/Cipher/Des/DesCrypt.hpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Chaos/Cipher/Des/DesCrypt.hpp b/Chaos/Cipher/Des/DesCrypt.hpp index b2dda46..96a5fb1 100644 --- a/Chaos/Cipher/Des/DesCrypt.hpp +++ b/Chaos/Cipher/Des/DesCrypt.hpp @@ -192,6 +192,8 @@ namespace Chaos::Cipher::Des class DesCrypt { public: + using Block = uint64_t; + DesCrypt() = delete; class Key @@ -245,7 +247,7 @@ public: Inner_::Bitwise::CrunchUInt64(out, encrypted); } - uint64_t EncryptBlock(uint64_t block) + Block EncryptBlock(Block block) { return DesCrypt::ProcessBlock(block, Schedule_); } @@ -280,7 +282,7 @@ public: Inner_::Bitwise::CrunchUInt64(out, decrypted); } - uint64_t DecryptBlock(uint64_t block) + Block DecryptBlock(Block block) { return DesCrypt::ProcessBlock(block, Schedule_); } @@ -290,7 +292,6 @@ public: }; private: - using Block = uint64_t; using BlockHalf = uint32_t; using RawBlockArray = Service::SeArray; using Data48 = uint64_t;