From 15d841893c4937054ae649f3ad304777ebbb1b64 Mon Sep 17 00:00:00 2001 From: hashlag Date: Wed, 4 Feb 2026 23:31:14 +0300 Subject: [PATCH] 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); }