diff --git a/Chaos/Cipher/Stream/Arc4/Arc4Gen.hpp b/Chaos/Cipher/Stream/Arc4/Arc4Gen.hpp index 98dcaa5..2c5182f 100644 --- a/Chaos/Cipher/Stream/Arc4/Arc4Gen.hpp +++ b/Chaos/Cipher/Stream/Arc4/Arc4Gen.hpp @@ -66,6 +66,8 @@ private: template void RekeyImpl(InputIt keyBegin, InputIt keyEnd) { + IsInitialized_ = false; + I_ = 0; J_ = 0; diff --git a/ChaosTests/Cipher/Stream/Arc4/Arc4GenTests.cpp b/ChaosTests/Cipher/Stream/Arc4/Arc4GenTests.cpp index 14b5b98..1940626 100644 --- a/ChaosTests/Cipher/Stream/Arc4/Arc4GenTests.cpp +++ b/ChaosTests/Cipher/Stream/Arc4/Arc4GenTests.cpp @@ -419,6 +419,55 @@ TEST(Arc4GenTests, UninitializedGenTest) } } +TEST(Arc4GenTests, RekeyFailSafetyTest) +{ + uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05 }; + Arc4Gen gen(key, key + std::size(key)); + + { + std::array out = {}; + std::array expected = { 0xb2, 0x39 }; + + gen.Generate(out.begin(), out.size()); + + ASSERT_EQ(expected, out); + } + + { + std::array out = {}; + + uint8_t smallKey[] = { 0x01, 0x02, 0x03 }; + ASSERT_THROW_EX(gen.Rekey(smallKey, smallKey + std::size(smallKey)), + Chaos::Service::ChaosException, + { + ASSERT_EQ("Arc4Gen: key is too small", ex.GetMessage()); + }); + + ASSERT_THROW_EX(gen.Generate(out.begin(), out.size()), + Chaos::Service::ChaosException, + { + ASSERT_EQ("Arc4Gen: not initialized", ex.GetMessage()); + }); + + ASSERT_THROW_EX(gen.Drop(1), + Chaos::Service::ChaosException, + { + ASSERT_EQ("Arc4Gen: not initialized", ex.GetMessage()); + }); + } + + { + gen.Rekey(key, key + std::size(key)); + + std::array out = {}; + std::array expected = { 0xb2, 0x39, 0x63, 0x05, 0xf0 }; + + gen.Generate(out.begin(), out.size()); + + ASSERT_EQ(expected, out); + } +} + TEST(Arc4GenTests, GenerateOutIteratorUsageTest) { {