From 67709e5361846f75d34630593a0d4be730531890 Mon Sep 17 00:00:00 2001 From: hashlag Date: Mon, 2 Feb 2026 00:52:08 +0300 Subject: [PATCH 1/9] Encryptor: Require end of the output span to be indicated explicitly. Usage becomes much less error-prone. --- Chaos/Cipher/Block/Des/DesCrypt.hpp | 15 +++++++- Chaos/Cipher/Block/Encryptor.hpp | 5 ++- ChaosTests/Cipher/DesCryptTests.cpp | 58 ++++++++--------------------- 3 files changed, 31 insertions(+), 47 deletions(-) diff --git a/Chaos/Cipher/Block/Des/DesCrypt.hpp b/Chaos/Cipher/Block/Des/DesCrypt.hpp index d25fd81..9026dcd 100644 --- a/Chaos/Cipher/Block/Des/DesCrypt.hpp +++ b/Chaos/Cipher/Block/Des/DesCrypt.hpp @@ -90,6 +90,16 @@ struct Bitwise *out++ = (value >> (56 - (i * 8))) & Mask<8>(); } } + + template + static void CrunchUInt64(OutputIt outBegin, OutputIt outEnd, uint64_t value) + { + int_fast8_t i = 0; + for (OutputIt out = outBegin; i < 8 && out != outEnd; ++i, ++out) + { + *out = (value >> (56 - (i * 8))) & Mask<8>(); + } + } }; using RawKey = Service::SeArray; @@ -237,7 +247,8 @@ public: { } template - void EncryptBlock(OutputIt out, InputIt inBegin, InputIt inEnd) const + void EncryptBlock(OutputIt outBegin, OutputIt outEnd, + InputIt inBegin, InputIt inEnd) const { RawBlockArray block; @@ -252,7 +263,7 @@ public: block.End()), Schedule_); - Inner_::Bitwise::CrunchUInt64(out, encrypted); + Inner_::Bitwise::CrunchUInt64(outBegin, outEnd, encrypted); } Block EncryptBlock(Block block) const diff --git a/Chaos/Cipher/Block/Encryptor.hpp b/Chaos/Cipher/Block/Encryptor.hpp index 18f9872..ba3f9f5 100644 --- a/Chaos/Cipher/Block/Encryptor.hpp +++ b/Chaos/Cipher/Block/Encryptor.hpp @@ -9,9 +9,10 @@ class Encryptor { public: template - void EncryptBlock(OutputIt out, InputIt inBegin, InputIt inEnd) const + void EncryptBlock(OutputIt outBegin, OutputIt outEnd, + InputIt inBegin, InputIt inEnd) const { - Impl().EncryptBlock(out, inBegin, inEnd); + Impl().EncryptBlock(outBegin, outEnd, inBegin, inEnd); } template diff --git a/ChaosTests/Cipher/DesCryptTests.cpp b/ChaosTests/Cipher/DesCryptTests.cpp index 1df1028..0338c32 100644 --- a/ChaosTests/Cipher/DesCryptTests.cpp +++ b/ChaosTests/Cipher/DesCryptTests.cpp @@ -51,7 +51,7 @@ TEST(DesCryptTests, EncryptTest) DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::DesEncryptor enc(desKey); - enc.EncryptBlock(result.begin(), data.begin(), data.end()); + enc.EncryptBlock(result.begin(), result.end(), data.begin(), data.end()); return result; } @@ -143,7 +143,7 @@ TEST(DesCryptTests, EncryptShortDataTest) DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::DesEncryptor enc(desKey); - enc.EncryptBlock(result.begin(), data.begin(), data.end()); + enc.EncryptBlock(result.begin(), result.end(), data.begin(), data.end()); return result; } @@ -177,7 +177,7 @@ TEST(DesCryptTests, EncryptLongDataTest) DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::DesEncryptor enc(desKey); - enc.EncryptBlock(result.begin(), data.begin(), data.end()); + enc.EncryptBlock(result.begin(), result.end(), data.begin(), data.end()); return result; } @@ -377,62 +377,34 @@ TEST(DesCryptTests, LongKeyTest) TEST(DesCryptTests, OutIteratorUsageEncryptTest) { - 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); + std::array fact = {}; + // Last 3 bytes should be untouched. + std::array expected = { 0x42, 0x27, 0x88, 0xa6, 0x7b, 0x00, 0x00, 0x00 }; DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::DesEncryptor enc(desKey); - enc.EncryptBlock(it, data.begin(), data.end()); + enc.EncryptBlock(fact.begin(), fact.end() - 3, data.begin(), data.end()); - ASSERT_EQ(8, asteriskCalls); - ASSERT_EQ(8, incrementCalls); + ASSERT_EQ(fact, expected); } { - std::array data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f }; + std::array data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44, 0x44, 0x44 }; std::array key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab }; - size_t asteriskCalls = 0; - size_t incrementCalls = 0; - OutputItMock it(asteriskCalls, incrementCalls); + std::array fact = {}; + // Last 4 bytes should be untouched. + std::array expected = { 0x42, 0x27, 0x88, 0xa6, 0x00, 0x00, 0x00, 0x00 }; DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::DesEncryptor enc(desKey); - enc.EncryptBlock(it, data.begin(), data.end()); + enc.EncryptBlock(fact.begin(), fact.end() - 4, data.begin(), data.end()); - ASSERT_EQ(8, asteriskCalls); - ASSERT_EQ(8, incrementCalls); + ASSERT_EQ(fact, expected); } } @@ -504,7 +476,7 @@ static std::vector EncryptThroughBase(const Encryptor & enc, std::vector result; result.resize(enc.GetBlockSize(), 0); - enc.EncryptBlock(result.begin(), begin, end); + enc.EncryptBlock(result.begin(), result.end(), begin, end); return result; } From a0f17ea1270235dfbcc28f5185c35ac8666e8e3a Mon Sep 17 00:00:00 2001 From: hashlag Date: Mon, 2 Feb 2026 01:10:22 +0300 Subject: [PATCH 2/9] Decryptor: Require end of the output span to be indicated explicitly. Usage becomes much less error-prone. --- Chaos/Cipher/Block/Decryptor.hpp | 5 ++- Chaos/Cipher/Block/Des/DesCrypt.hpp | 5 ++- ChaosTests/Cipher/DesCryptTests.cpp | 58 ++++++++--------------------- 3 files changed, 21 insertions(+), 47 deletions(-) diff --git a/Chaos/Cipher/Block/Decryptor.hpp b/Chaos/Cipher/Block/Decryptor.hpp index 01ac308..e27c130 100644 --- a/Chaos/Cipher/Block/Decryptor.hpp +++ b/Chaos/Cipher/Block/Decryptor.hpp @@ -9,9 +9,10 @@ class Decryptor { public: template - void DecryptBlock(OutputIt out, InputIt inBegin, InputIt inEnd) const + void DecryptBlock(OutputIt outBegin, OutputIt outEnd, + InputIt inBegin, InputIt inEnd) const { - Impl().DecryptBlock(out, inBegin, inEnd); + Impl().DecryptBlock(outBegin, outEnd, inBegin, inEnd); } template diff --git a/Chaos/Cipher/Block/Des/DesCrypt.hpp b/Chaos/Cipher/Block/Des/DesCrypt.hpp index 9026dcd..06c83fe 100644 --- a/Chaos/Cipher/Block/Des/DesCrypt.hpp +++ b/Chaos/Cipher/Block/Des/DesCrypt.hpp @@ -290,7 +290,8 @@ public: { } template - void DecryptBlock(OutputIt out, InputIt inBegin, InputIt inEnd) const + void DecryptBlock(OutputIt outBegin, OutputIt outEnd, + InputIt inBegin, InputIt inEnd) const { RawBlockArray block; @@ -305,7 +306,7 @@ public: block.End()), Schedule_); - Inner_::Bitwise::CrunchUInt64(out, decrypted); + Inner_::Bitwise::CrunchUInt64(outBegin, outEnd, decrypted); } Block DecryptBlock(Block block) const diff --git a/ChaosTests/Cipher/DesCryptTests.cpp b/ChaosTests/Cipher/DesCryptTests.cpp index 0338c32..de6dd65 100644 --- a/ChaosTests/Cipher/DesCryptTests.cpp +++ b/ChaosTests/Cipher/DesCryptTests.cpp @@ -211,7 +211,7 @@ TEST(DesCryptTests, DecryptTest) DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::DesDecryptor dec(desKey); - dec.DecryptBlock(result.begin(), data.begin(), data.end()); + dec.DecryptBlock(result.begin(), result.end(), data.begin(), data.end()); return result; } @@ -303,7 +303,7 @@ TEST(DesCryptTests, DecryptShortDataTest) DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::DesDecryptor dec(desKey); - dec.DecryptBlock(result.begin(), data.begin(), data.end()); + dec.DecryptBlock(result.begin(), result.end(), data.begin(), data.end()); return result; } @@ -337,7 +337,7 @@ TEST(DesCryptTests, DecryptLongDataTest) DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::DesDecryptor dec(desKey); - dec.DecryptBlock(result.begin(), data.begin(), data.end()); + dec.DecryptBlock(result.begin(), result.end(), data.begin(), data.end()); return result; } @@ -410,62 +410,34 @@ TEST(DesCryptTests, OutIteratorUsageEncryptTest) 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); + std::array fact = {}; + // Last 3 bytes should be untouched. + std::array expected = { 0x45, 0x69, 0x71, 0x17, 0x13, 0x00, 0x00, 0x00 }; DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::DesDecryptor dec(desKey); - dec.DecryptBlock(it, data.begin(), data.end()); + dec.DecryptBlock(fact.begin(), fact.end() - 3, data.begin(), data.end()); - ASSERT_EQ(8, asteriskCalls); - ASSERT_EQ(8, incrementCalls); + ASSERT_EQ(expected, fact); } { - std::array data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f }; + std::array data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44, 0x44, 0x44 }; std::array key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab }; - size_t asteriskCalls = 0; - size_t incrementCalls = 0; - OutputItMock it(asteriskCalls, incrementCalls); + std::array fact = {}; + // Last 4 bytes should be untouched. + std::array expected = { 0x45, 0x69, 0x71, 0x17, 0x00, 0x00, 0x00, 0x00 }; DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::DesDecryptor dec(desKey); - dec.DecryptBlock(it, data.begin(), data.end()); + dec.DecryptBlock(fact.begin(), fact.end() - 4, data.begin(), data.end()); - ASSERT_EQ(8, asteriskCalls); - ASSERT_EQ(8, incrementCalls); + ASSERT_EQ(expected, fact); } } @@ -519,7 +491,7 @@ static std::vector DecryptThroughBase(const Decryptor & dec, std::vector result; result.resize(dec.GetBlockSize(), 0); - dec.DecryptBlock(result.begin(), begin, end); + dec.DecryptBlock(result.begin(), result.end(), begin, end); return result; } From 3de69d3298d6512b05c237557224d7d8c09acbfe Mon Sep 17 00:00:00 2001 From: hashlag Date: Mon, 2 Feb 2026 01:15:38 +0300 Subject: [PATCH 3/9] Fix assertion style. It should be expectation-first... --- ChaosTests/Cipher/DesCryptTests.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ChaosTests/Cipher/DesCryptTests.cpp b/ChaosTests/Cipher/DesCryptTests.cpp index de6dd65..1c8539f 100644 --- a/ChaosTests/Cipher/DesCryptTests.cpp +++ b/ChaosTests/Cipher/DesCryptTests.cpp @@ -389,7 +389,7 @@ TEST(DesCryptTests, OutIteratorUsageEncryptTest) DesCrypt::DesEncryptor enc(desKey); enc.EncryptBlock(fact.begin(), fact.end() - 3, data.begin(), data.end()); - ASSERT_EQ(fact, expected); + ASSERT_EQ(expected, fact); } { @@ -404,7 +404,7 @@ TEST(DesCryptTests, OutIteratorUsageEncryptTest) DesCrypt::DesEncryptor enc(desKey); enc.EncryptBlock(fact.begin(), fact.end() - 4, data.begin(), data.end()); - ASSERT_EQ(fact, expected); + ASSERT_EQ(expected, fact); } } From ccdf14d31aa80b1f345ce82ac228b73e955a8a58 Mon Sep 17 00:00:00 2001 From: hashlag Date: Wed, 4 Feb 2026 00:25:15 +0300 Subject: [PATCH 4/9] Add Arc4GenTests, GenerateOutIteratorUsageTest. Harden against past-the-end write. --- ChaosTests/Cipher/Arc4GenTests.cpp | 56 ++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/ChaosTests/Cipher/Arc4GenTests.cpp b/ChaosTests/Cipher/Arc4GenTests.cpp index 1baab07..9448ba1 100644 --- a/ChaosTests/Cipher/Arc4GenTests.cpp +++ b/ChaosTests/Cipher/Arc4GenTests.cpp @@ -353,3 +353,59 @@ TEST(Arc4GenTests, UninitializedGenTest) ASSERT_THROW(gen.Drop(256), Chaos::Service::ChaosException); } } + +TEST(Arc4GenTests, GenerateOutIteratorUsageTest) +{ + { + uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05 }; + Arc4Gen gen(key, key + std::size(key)); + + std::array out; + out.fill(0); + + std::array expected = + { + 0xb2, 0x39, 0x63, 0x05, 0xf0, 0x3d, 0xc0, 0x27, + 0xcc, 0xc3, 0x52, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00 + }; + + gen.Generate(out.begin(), 11); + + ASSERT_EQ(expected, out); + } + + { + uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05 }; + Arc4Gen gen(key, key + std::size(key)); + + std::array out; + out.fill(0); + + std::array expected = + { + 0xb2, 0x39, 0x63, 0x05, 0xf0, 0x3d, 0xc0, 0x27, + 0xcc, 0xc3, 0x52, 0x4a, 0x0a, 0x11, 0x18, 0xa8, + 0x69, 0x82, 0x00, 0x00 + }; + + gen.Generate(out.begin(), 18); + + ASSERT_EQ(expected, out); + } + + { + uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05 }; + Arc4Gen gen(key, key + std::size(key)); + + std::array out; + out.fill(0); + + std::array expected; + expected.fill(0); + + gen.Generate(out.begin(), 0); + + ASSERT_EQ(expected, out); + } +} From 2d64389d663a7f9ad1579d4a3110e55e04dfcf74 Mon Sep 17 00:00:00 2001 From: hashlag Date: Wed, 4 Feb 2026 00:43:40 +0300 Subject: [PATCH 5/9] Add Arc4CryptTests, EncryptOutIteratorUsageTest. Harden against past-the-end write. --- ChaosTests/Cipher/Arc4CryptTests.cpp | 63 ++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/ChaosTests/Cipher/Arc4CryptTests.cpp b/ChaosTests/Cipher/Arc4CryptTests.cpp index b2192bc..67de73f 100644 --- a/ChaosTests/Cipher/Arc4CryptTests.cpp +++ b/ChaosTests/Cipher/Arc4CryptTests.cpp @@ -107,3 +107,66 @@ TEST(Arc4CryptTests, RekeyTest) ciphertext); } } + +TEST(Arc4CryptTests, EncryptOutIteratorUsageTest) +{ + const std::vector data = StrToU8Vec("The quick brown fox jumps over the lazy dog."); + + { + std::array key = { 0x01, 0x02, 0x03, 0x04, 0x05 }; + + Arc4Crypt crypt(key.begin(), key.end()); + + std::array out; + out.fill(0); + + std::array expected = + { + 0xe6, 0x51, 0x06, 0x25, 0x81, 0x48, 0xa9, 0x44, 0xa7, 0xe3, 0x30, + 0x38, 0x65, 0x66, 0x76, 0x88, 0x0f, 0xed, 0xec, 0x6f, 0x72, 0x89, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }; + + crypt.Encrypt(out.begin(), data.begin(), 22); + + ASSERT_EQ(expected, out); + } + + { + std::array key = { 0x01, 0x02, 0x03, 0x04, 0x05 }; + + Arc4Crypt crypt(key.begin(), key.end()); + + std::array out; + out.fill(0); + + std::array expected = + { + 0xe6, 0x51, 0x06, 0x25, 0x81, 0x48, 0xa9, 0x44, 0xa7, 0xe3, 0x30, + 0x38, 0x65, 0x66, 0x76, 0x88, 0x0f, 0xed, 0xec, 0x6f, 0x72, 0x89, + 0xef, 0xa5, 0xfa, 0xe4, 0x6c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }; + + crypt.Encrypt(out.begin(), data.begin(), 27); + + ASSERT_EQ(expected, out); + } + + { + std::array key = { 0x01, 0x02, 0x03, 0x04, 0x05 }; + + Arc4Crypt crypt(key.begin(), key.end()); + + std::array out; + out.fill(0); + + std::array expected; + expected.fill(0); + + crypt.Encrypt(out.begin(), data.begin(), 0); + + ASSERT_EQ(expected, out); + } +} From 1bf91a90d0bdd6403bf203bfc2cf5fccc2112ada Mon Sep 17 00:00:00 2001 From: hashlag Date: Wed, 4 Feb 2026 01:01:54 +0300 Subject: [PATCH 6/9] Add Arc4CryptTests, DecryptOutIteratorUsageTest. Harden against past-the-end write. --- ChaosTests/Cipher/Arc4CryptTests.cpp | 55 ++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/ChaosTests/Cipher/Arc4CryptTests.cpp b/ChaosTests/Cipher/Arc4CryptTests.cpp index 67de73f..8abc503 100644 --- a/ChaosTests/Cipher/Arc4CryptTests.cpp +++ b/ChaosTests/Cipher/Arc4CryptTests.cpp @@ -170,3 +170,58 @@ TEST(Arc4CryptTests, EncryptOutIteratorUsageTest) ASSERT_EQ(expected, out); } } + +TEST(Arc4CryptTests, DecryptOutIteratorUsageTest) +{ + const std::array data = { 0x45, 0xA0, 0x1F, 0x64, 0x5F, 0xC3, 0x5B, + 0x38, 0x35, 0x52, 0x54, 0x4B, 0x9B, 0xF5 }; + const std::vector key = StrToU8Vec("Secret"); + + { + Arc4Crypt crypt(key.begin(), key.end()); + + std::array out; + out.fill(0); + + std::array expected = + { + 'A', 't', 't', 'a', 'c', 'k', ' ', 'a', 't', ' ', 'd', 'a', + 0x00, 0x00 + }; + + crypt.Decrypt(out.begin(), data.begin(), 12); + + ASSERT_EQ(expected, out); + } + + { + Arc4Crypt crypt(key.begin(), key.end()); + + std::array out; + out.fill(0); + + std::array expected = + { + 'A', 't', 't', 'a', 'c', 'k', ' ', + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }; + + crypt.Decrypt(out.begin(), data.begin(), 7); + + ASSERT_EQ(expected, out); + } + + { + Arc4Crypt crypt(key.begin(), key.end()); + + std::array out; + out.fill(0); + + std::array expected; + expected.fill(0); + + crypt.Decrypt(out.begin(), data.begin(), 0); + + ASSERT_EQ(expected, out); + } +} From 15d841893c4937054ae649f3ad304777ebbb1b64 Mon Sep 17 00:00:00 2001 From: hashlag Date: Wed, 4 Feb 2026 23:31:14 +0300 Subject: [PATCH 7/9] IteratorUsage tests: Add some canary nulled space before begin. Hardening against write-before-begin. --- ChaosTests/Cipher/Arc4CryptTests.cpp | 32 ++++++++++++++++------------ ChaosTests/Cipher/Arc4GenTests.cpp | 16 ++++++++------ ChaosTests/Cipher/DesCryptTests.cpp | 32 ++++++++++++++-------------- 3 files changed, 43 insertions(+), 37 deletions(-) diff --git a/ChaosTests/Cipher/Arc4CryptTests.cpp b/ChaosTests/Cipher/Arc4CryptTests.cpp index 8abc503..6edee4c 100644 --- a/ChaosTests/Cipher/Arc4CryptTests.cpp +++ b/ChaosTests/Cipher/Arc4CryptTests.cpp @@ -117,18 +117,19 @@ TEST(Arc4CryptTests, EncryptOutIteratorUsageTest) Arc4Crypt crypt(key.begin(), key.end()); - std::array out; + std::array out; out.fill(0); - std::array expected = + std::array expected = { + 0x00, 0x00, 0x00, 0xe6, 0x51, 0x06, 0x25, 0x81, 0x48, 0xa9, 0x44, 0xa7, 0xe3, 0x30, 0x38, 0x65, 0x66, 0x76, 0x88, 0x0f, 0xed, 0xec, 0x6f, 0x72, 0x89, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; - crypt.Encrypt(out.begin(), data.begin(), 22); + crypt.Encrypt(out.begin() + 3, data.begin(), 22); ASSERT_EQ(expected, out); } @@ -138,18 +139,19 @@ TEST(Arc4CryptTests, EncryptOutIteratorUsageTest) Arc4Crypt crypt(key.begin(), key.end()); - std::array out; + std::array out; out.fill(0); - std::array expected = + std::array expected = { + 0x00, 0x00, 0x00, 0xe6, 0x51, 0x06, 0x25, 0x81, 0x48, 0xa9, 0x44, 0xa7, 0xe3, 0x30, 0x38, 0x65, 0x66, 0x76, 0x88, 0x0f, 0xed, 0xec, 0x6f, 0x72, 0x89, 0xef, 0xa5, 0xfa, 0xe4, 0x6c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; - crypt.Encrypt(out.begin(), data.begin(), 27); + crypt.Encrypt(out.begin() + 3, data.begin(), 27); ASSERT_EQ(expected, out); } @@ -165,7 +167,7 @@ TEST(Arc4CryptTests, EncryptOutIteratorUsageTest) std::array expected; expected.fill(0); - crypt.Encrypt(out.begin(), data.begin(), 0); + crypt.Encrypt(out.begin() + 3, data.begin(), 0); ASSERT_EQ(expected, out); } @@ -180,16 +182,17 @@ TEST(Arc4CryptTests, DecryptOutIteratorUsageTest) { Arc4Crypt crypt(key.begin(), key.end()); - std::array out; + std::array out; out.fill(0); - std::array expected = + std::array expected = { + 0x00, 0x00, 0x00, 'A', 't', 't', 'a', 'c', 'k', ' ', 'a', 't', ' ', 'd', 'a', 0x00, 0x00 }; - crypt.Decrypt(out.begin(), data.begin(), 12); + crypt.Decrypt(out.begin() + 3, data.begin(), 12); ASSERT_EQ(expected, out); } @@ -197,16 +200,17 @@ TEST(Arc4CryptTests, DecryptOutIteratorUsageTest) { Arc4Crypt crypt(key.begin(), key.end()); - std::array out; + std::array out; out.fill(0); - std::array expected = + std::array expected = { + 0x00, 0x00, 0x00, 'A', 't', 't', 'a', 'c', 'k', ' ', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; - crypt.Decrypt(out.begin(), data.begin(), 7); + crypt.Decrypt(out.begin() + 3, data.begin(), 7); ASSERT_EQ(expected, out); } @@ -220,7 +224,7 @@ TEST(Arc4CryptTests, DecryptOutIteratorUsageTest) std::array expected; expected.fill(0); - crypt.Decrypt(out.begin(), data.begin(), 0); + crypt.Decrypt(out.begin() + 3, data.begin(), 0); ASSERT_EQ(expected, out); } diff --git a/ChaosTests/Cipher/Arc4GenTests.cpp b/ChaosTests/Cipher/Arc4GenTests.cpp index 9448ba1..29ba990 100644 --- a/ChaosTests/Cipher/Arc4GenTests.cpp +++ b/ChaosTests/Cipher/Arc4GenTests.cpp @@ -360,17 +360,18 @@ TEST(Arc4GenTests, GenerateOutIteratorUsageTest) uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05 }; Arc4Gen gen(key, key + std::size(key)); - std::array out; + std::array out; out.fill(0); - std::array expected = + std::array expected = { + 0x00, 0x00, 0x00, 0xb2, 0x39, 0x63, 0x05, 0xf0, 0x3d, 0xc0, 0x27, 0xcc, 0xc3, 0x52, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; - gen.Generate(out.begin(), 11); + gen.Generate(out.begin() + 3, 11); ASSERT_EQ(expected, out); } @@ -379,17 +380,18 @@ TEST(Arc4GenTests, GenerateOutIteratorUsageTest) uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05 }; Arc4Gen gen(key, key + std::size(key)); - std::array out; + std::array out; out.fill(0); - std::array expected = + std::array expected = { + 0x00, 0x00, 0x00, 0xb2, 0x39, 0x63, 0x05, 0xf0, 0x3d, 0xc0, 0x27, 0xcc, 0xc3, 0x52, 0x4a, 0x0a, 0x11, 0x18, 0xa8, 0x69, 0x82, 0x00, 0x00 }; - gen.Generate(out.begin(), 18); + gen.Generate(out.begin() + 3, 18); ASSERT_EQ(expected, out); } @@ -404,7 +406,7 @@ TEST(Arc4GenTests, GenerateOutIteratorUsageTest) std::array expected; expected.fill(0); - gen.Generate(out.begin(), 0); + gen.Generate(out.begin() + 3, 0); ASSERT_EQ(expected, out); } diff --git a/ChaosTests/Cipher/DesCryptTests.cpp b/ChaosTests/Cipher/DesCryptTests.cpp index 1c8539f..31ef191 100644 --- a/ChaosTests/Cipher/DesCryptTests.cpp +++ b/ChaosTests/Cipher/DesCryptTests.cpp @@ -381,13 +381,13 @@ TEST(DesCryptTests, OutIteratorUsageEncryptTest) std::array data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44 }; std::array key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab }; - std::array fact = {}; - // Last 3 bytes should be untouched. - std::array expected = { 0x42, 0x27, 0x88, 0xa6, 0x7b, 0x00, 0x00, 0x00 }; + std::array fact = {}; + // First and last 3 bytes should be untouched. + std::array expected = { 0x00, 0x00, 0x00, 0x42, 0x27, 0x88, 0xa6, 0x7b, 0x00, 0x00, 0x00 }; DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::DesEncryptor enc(desKey); - enc.EncryptBlock(fact.begin(), fact.end() - 3, data.begin(), data.end()); + enc.EncryptBlock(fact.begin() + 3, fact.end() - 3, data.begin(), data.end()); ASSERT_EQ(expected, fact); } @@ -396,13 +396,13 @@ TEST(DesCryptTests, OutIteratorUsageEncryptTest) std::array data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44, 0x44, 0x44 }; std::array key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab }; - std::array fact = {}; - // Last 4 bytes should be untouched. - std::array expected = { 0x42, 0x27, 0x88, 0xa6, 0x00, 0x00, 0x00, 0x00 }; + std::array fact = {}; + // First and last 4 bytes should be untouched. + std::array expected = { 0x00, 0x00, 0x00, 0x00, 0x42, 0x27, 0x88, 0xa6, 0x00, 0x00, 0x00, 0x00 }; DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::DesEncryptor enc(desKey); - enc.EncryptBlock(fact.begin(), fact.end() - 4, data.begin(), data.end()); + enc.EncryptBlock(fact.begin() + 4, fact.end() - 4, data.begin(), data.end()); ASSERT_EQ(expected, fact); } @@ -414,13 +414,13 @@ TEST(DesCryptTests, OutIteratorUsageDecryptTest) std::array data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44 }; std::array key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab }; - std::array fact = {}; - // Last 3 bytes should be untouched. - std::array expected = { 0x45, 0x69, 0x71, 0x17, 0x13, 0x00, 0x00, 0x00 }; + std::array fact = {}; + // First and last 3 bytes should be untouched. + std::array expected = { 0x00, 0x00, 0x00, 0x45, 0x69, 0x71, 0x17, 0x13, 0x00, 0x00, 0x00 }; DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::DesDecryptor dec(desKey); - dec.DecryptBlock(fact.begin(), fact.end() - 3, data.begin(), data.end()); + dec.DecryptBlock(fact.begin() + 3, fact.end() - 3, data.begin(), data.end()); ASSERT_EQ(expected, fact); } @@ -429,13 +429,13 @@ TEST(DesCryptTests, OutIteratorUsageDecryptTest) std::array data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44, 0x44, 0x44 }; std::array key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab }; - std::array fact = {}; - // Last 4 bytes should be untouched. - std::array expected = { 0x45, 0x69, 0x71, 0x17, 0x00, 0x00, 0x00, 0x00 }; + std::array fact = {}; + // First and last 4 bytes should be untouched. + std::array expected = { 0x00, 0x00, 0x00, 0x00, 0x45, 0x69, 0x71, 0x17, 0x00, 0x00, 0x00, 0x00 }; DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::DesDecryptor dec(desKey); - dec.DecryptBlock(fact.begin(), fact.end() - 4, data.begin(), data.end()); + dec.DecryptBlock(fact.begin() + 4, fact.end() - 4, data.begin(), data.end()); ASSERT_EQ(expected, fact); } From b34fda75efb83f44bc5ac3385004d25d979f36fb Mon Sep 17 00:00:00 2001 From: hashlag Date: Wed, 4 Feb 2026 23:37:05 +0300 Subject: [PATCH 8/9] DesCryptTests: IteratorUsage tests: Add testcases with begin == end. --- ChaosTests/Cipher/DesCryptTests.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/ChaosTests/Cipher/DesCryptTests.cpp b/ChaosTests/Cipher/DesCryptTests.cpp index 31ef191..94c06e7 100644 --- a/ChaosTests/Cipher/DesCryptTests.cpp +++ b/ChaosTests/Cipher/DesCryptTests.cpp @@ -406,6 +406,20 @@ TEST(DesCryptTests, OutIteratorUsageEncryptTest) ASSERT_EQ(expected, fact); } + + { + std::array data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44, 0x44, 0x44 }; + std::array key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab }; + + std::array fact = {}; + std::array expected = {}; + + DesCrypt::Key desKey(key.begin(), key.end()); + DesCrypt::DesEncryptor enc(desKey); + enc.EncryptBlock(fact.begin() + 3, fact.begin() + 3, data.begin(), data.end()); + + ASSERT_EQ(expected, fact); + } } TEST(DesCryptTests, OutIteratorUsageDecryptTest) @@ -439,6 +453,20 @@ TEST(DesCryptTests, OutIteratorUsageDecryptTest) ASSERT_EQ(expected, fact); } + + { + std::array data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44, 0x44, 0x44 }; + std::array key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab }; + + std::array fact = {}; + std::array expected = {}; + + DesCrypt::Key desKey(key.begin(), key.end()); + DesCrypt::DesDecryptor dec(desKey); + dec.DecryptBlock(fact.begin() + 3, fact.begin() + 3, data.begin(), data.end()); + + ASSERT_EQ(expected, fact); + } } template From 65eb51c133700cfd0f46dd976bf88c12f0689b4b Mon Sep 17 00:00:00 2001 From: hashlag Date: Wed, 4 Feb 2026 23:47:45 +0300 Subject: [PATCH 9/9] Remove unused CrunchUInt64() overload. --- Chaos/Cipher/Block/Des/DesCrypt.hpp | 9 --------- 1 file changed, 9 deletions(-) diff --git a/Chaos/Cipher/Block/Des/DesCrypt.hpp b/Chaos/Cipher/Block/Des/DesCrypt.hpp index 06c83fe..8459b19 100644 --- a/Chaos/Cipher/Block/Des/DesCrypt.hpp +++ b/Chaos/Cipher/Block/Des/DesCrypt.hpp @@ -82,15 +82,6 @@ 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>(); - } - } - template static void CrunchUInt64(OutputIt outBegin, OutputIt outEnd, uint64_t value) {