Fix a logical bug where Arc4Crypt was left in invalid state after a

failed Rekey() call.
This commit is contained in:
hashlag committed 2026-09-20 20:40:55 +03:00
1 parent aefa68f118
commit 58718b3b0a
2 files changed
+56

No files matched your search

+1
View File
@@ -59,6 +59,7 @@ private:
template<typename InputIt> template<typename InputIt>
void RekeyImpl(InputIt keyBegin, InputIt keyEnd) void RekeyImpl(InputIt keyBegin, InputIt keyEnd)
{ {
IsInitialized_ = false;
Gen_.Rekey(keyBegin, keyEnd); Gen_.Rekey(keyBegin, keyEnd);
IsInitialized_ = true; IsInitialized_ = true;
} }
@@ -117,6 +117,61 @@ TEST(Arc4CryptTests, RekeyTest)
} }
} }
TEST(Arc4CryptTests, RekeyFailSafetyTest)
{
const std::vector<uint8_t> key = StrToU8Vec("Secret");
const std::vector<uint8_t> data = StrToU8Vec("Attack at dawn");
Arc4Crypt arc4(key.begin(), key.end());
{
std::vector<uint8_t> ciphertext;
ciphertext.resize(data.size());
arc4.Encrypt(ciphertext.begin(), data.begin(), data.size());
ASSERT_EQ(std::vector<uint8_t>({ 0x45, 0xA0, 0x1F, 0x64, 0x5F, 0xC3, 0x5B,
0x38, 0x35, 0x52, 0x54, 0x4B, 0x9B, 0xF5 }),
ciphertext);
}
{
std::array<uint8_t, 10> in = {};
std::array<uint8_t, 10> out = {};
uint8_t smallKey[] = { 0x01, 0x02, 0x03 };
ASSERT_THROW_EX(arc4.Rekey(smallKey, smallKey + std::size(smallKey)),
Chaos::Service::ChaosException,
{
ASSERT_EQ("Arc4Gen: key is too small", ex.GetMessage());
});
ASSERT_THROW_EX(arc4.Encrypt(out.begin(), in.begin(), in.size()),
Chaos::Service::ChaosException,
{
ASSERT_EQ("Arc4Crypt: not initialized", ex.GetMessage());
});
ASSERT_THROW_EX(arc4.Decrypt(out.begin(), in.begin(), in.size()),
Chaos::Service::ChaosException,
{
ASSERT_EQ("Arc4Crypt: not initialized", ex.GetMessage());
});
}
{
std::vector<uint8_t> ciphertext;
ciphertext.resize(data.size());
arc4.Rekey(key.begin(), key.end());
arc4.Encrypt(ciphertext.begin(), data.begin(), data.size());
ASSERT_EQ(std::vector<uint8_t>({ 0x45, 0xA0, 0x1F, 0x64, 0x5F, 0xC3, 0x5B,
0x38, 0x35, 0x52, 0x54, 0x4B, 0x9B, 0xF5 }),
ciphertext);
}
}
TEST(Arc4CryptTests, EncryptOutIteratorUsageTest) TEST(Arc4CryptTests, EncryptOutIteratorUsageTest)
{ {
const std::vector<uint8_t> data = StrToU8Vec("The quick brown fox jumps over the lazy dog."); const std::vector<uint8_t> data = StrToU8Vec("The quick brown fox jumps over the lazy dog.");