From e884ae7cd412ad763440a4ed710959aa396be786 Mon Sep 17 00:00:00 2001 From: hashlag <90853356+hashlag@users.noreply.github.com> Date: Mon, 25 Aug 2025 00:07:24 +0300 Subject: [PATCH] Add the Arc4 PRNG draft implementation --- Chaos/Cipher/Arc4.hpp | 103 +++++++++ Chaos/Service/ChaosException.hpp | 31 +++ ChaosTests/CMakeLists.txt | 3 +- ChaosTests/Cipher/Arc4Tests.cpp | 350 +++++++++++++++++++++++++++++++ 4 files changed, 486 insertions(+), 1 deletion(-) create mode 100644 Chaos/Service/ChaosException.hpp diff --git a/Chaos/Cipher/Arc4.hpp b/Chaos/Cipher/Arc4.hpp index b4c3cfc..e651533 100644 --- a/Chaos/Cipher/Arc4.hpp +++ b/Chaos/Cipher/Arc4.hpp @@ -1,9 +1,112 @@ #ifndef CHAOS_CIPHER_ARC4_HPP #define CHAOS_CIPHER_ARC4_HPP +#include +#include +#include + +#include "Service/ChaosException.hpp" + namespace Chaos::Cipher::Arc4 { +class Arc4Gen +{ +public: + Arc4Gen() + : IsInitialized_(false) + { } + + template + Arc4Gen(InputIt keyBegin, InputIt keyEnd) + { + RekeyImpl(keyBegin, keyEnd); + } + + template + void Rekey(InputIt keyBegin, InputIt keyEnd) + { + RekeyImpl(keyBegin, keyEnd); + } + + template + void Generate(OutputIt out, uint64_t bytesCount) + { + EnsureInitialized(); + + for (uint64_t cnt = 0; cnt < bytesCount; ++cnt) + { + Step(); + *out++ = Lookup_[static_cast(Lookup_[I_] + Lookup_[J_])]; + } + } + + void Drop(uint64_t bytesCount) + { + EnsureInitialized(); + + for (uint64_t cnt = 0; cnt < bytesCount; ++cnt) + { + Step(); + } + } + +private: + bool IsInitialized_; + + uint8_t I_; + uint8_t J_; + std::array Lookup_; + + void EnsureInitialized() const + { + if (!IsInitialized_) + { + throw Service::ChaosException("Arc4Gen: not initialized"); + } + } + + template + void RekeyImpl(InputIt keyBegin, InputIt keyEnd) + { + I_ = 0; + J_ = 0; + + for (uint64_t idx = 0; idx < Lookup_.size(); ++idx) + { + Lookup_[idx] = static_cast(idx); + } + + std::vector key(keyBegin, keyEnd); + + if (key.size() < 5) + { + throw Service::ChaosException("Arc4Gen: key is too small"); + } + + uint8_t a = 0; + uint8_t b = 0; + + for (uint64_t idx = 0; idx < Lookup_.size(); ++idx) + { + a = static_cast(idx); + b = b + Lookup_[a] + key[a % key.size()]; + + std::swap(Lookup_[a], Lookup_[b]); + } + + IsInitialized_ = true; + } + + void Step() + { + I_ = I_ + 1; + J_ = J_ + Lookup_[I_]; + + std::swap(Lookup_[I_], Lookup_[J_]); + } +}; + } // namespace Chaos::Cipher::Arc4 #endif diff --git a/Chaos/Service/ChaosException.hpp b/Chaos/Service/ChaosException.hpp new file mode 100644 index 0000000..a989037 --- /dev/null +++ b/Chaos/Service/ChaosException.hpp @@ -0,0 +1,31 @@ +#ifndef CHAOS_SERVICE_CHAOSEXCEPTION_HPP +#define CHAOS_SERVICE_CHAOSEXCEPTION_HPP + +#include + +namespace Chaos::Service +{ + +class ChaosException +{ +public: + ChaosException(std::string && message) + : Message_(std::move(message)) + { } + + ChaosException(const std::string & message) + : Message_(message) + { } + + const std::string & GetMessage() const + { + return Message_; + } + +private: + std::string Message_; +}; + +} // namespace Chaos::Service + +#endif diff --git a/ChaosTests/CMakeLists.txt b/ChaosTests/CMakeLists.txt index 64c2058..c34befa 100644 --- a/ChaosTests/CMakeLists.txt +++ b/ChaosTests/CMakeLists.txt @@ -13,7 +13,8 @@ FetchContent_MakeAvailable(googletest) set(ChaosTests_SOURCE Hash/Md4HasherTests.cpp Hash/Md5HasherTests.cpp - Mac/HmacTests.cpp) + Mac/HmacTests.cpp + Cipher/Arc4Tests.cpp) add_executable(ChaosTests ${ChaosTests_SOURCE}) target_link_libraries(ChaosTests gtest gtest_main) diff --git a/ChaosTests/Cipher/Arc4Tests.cpp b/ChaosTests/Cipher/Arc4Tests.cpp index 3a01f9e..4eb786e 100644 --- a/ChaosTests/Cipher/Arc4Tests.cpp +++ b/ChaosTests/Cipher/Arc4Tests.cpp @@ -3,3 +3,353 @@ #include "Cipher/Arc4.hpp" using namespace Chaos::Cipher::Arc4; + +TEST(Arc4GenTests, RFCTest) +{ + Arc4Gen gen; + + // Key: 0x0102030405 (40 bits) + + { + uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05 }; + gen.Rekey(key, key + std::size(key)); + + std::array expected = + { + 0xb2, 0x39, 0x63, 0x05, 0xf0, 0x3d, 0xc0, 0x27, + 0xcc, 0xc3, 0x52, 0x4a, 0x0a, 0x11, 0x18, 0xa8, + 0x69, 0x82, 0x94, 0x4f, 0x18, 0xfc, 0x82, 0xd5, + 0x89, 0xc4, 0x03, 0xa4, 0x7a, 0x0d, 0x09, 0x19 + }; + + std::array fact; + + gen.Generate(fact.begin(), fact.size()); + + ASSERT_EQ(expected, fact); + } + + { + uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05 }; + gen.Rekey(key, key + std::size(key)); + + std::array expected = + { + 0x28, 0xcb, 0x11, 0x32, 0xc9, 0x6c, 0xe2, 0x86, + 0x42, 0x1d, 0xca, 0xad, 0xb8, 0xb6, 0x9e, 0xae, + 0x1c, 0xfc, 0xf6, 0x2b, 0x03, 0xed, 0xdb, 0x64, + 0x1d, 0x77, 0xdf, 0xcf, 0x7f, 0x8d, 0x8c, 0x93 + }; + + std::array fact; + + gen.Drop(240); + gen.Generate(fact.begin(), fact.size()); + + ASSERT_EQ(expected, fact); + } + + { + uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05 }; + gen.Rekey(key, key + std::size(key)); + + std::array expected = + { + 0x42, 0xb7, 0xd0, 0xcd, 0xd9, 0x18, 0xa8, 0xa3, + 0x3d, 0xd5, 0x17, 0x81, 0xc8, 0x1f, 0x40, 0x41, + 0x64, 0x59, 0x84, 0x44, 0x32, 0xa7, 0xda, 0x92, + 0x3c, 0xfb, 0x3e, 0xb4, 0x98, 0x06, 0x61, 0xf6 + }; + + std::array fact; + + gen.Drop(496); + gen.Generate(fact.begin(), fact.size()); + + ASSERT_EQ(expected, fact); + } + + { + uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05 }; + gen.Rekey(key, key + std::size(key)); + + std::array expected = + { + 0x45, 0x12, 0x90, 0x48, 0xe6, 0xa0, 0xed, 0x0b, + 0x56, 0xb4, 0x90, 0x33, 0x8f, 0x07, 0x8d, 0xa5, + 0x30, 0xab, 0xbc, 0xc7, 0xc2, 0x0b, 0x01, 0x60, + 0x9f, 0x23, 0xee, 0x2d, 0x5f, 0x6b, 0xb7, 0xdf + }; + + std::array fact; + + gen.Drop(1008); + gen.Generate(fact.begin(), fact.size()); + + ASSERT_EQ(expected, fact); + } + + { + uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05 }; + gen.Rekey(key, key + std::size(key)); + + std::array expected = + { + 0x1e, 0xb1, 0x4a, 0x0c, 0x13, 0xb3, 0xbf, 0x47, + 0xfa, 0x2a, 0x0b, 0xa9, 0x3a, 0xd4, 0x5b, 0x8b, + 0xcc, 0x58, 0x2f, 0x8b, 0xa9, 0xf2, 0x65, 0xe2, + 0xb1, 0xbe, 0x91, 0x12, 0xe9, 0x75, 0xd2, 0xd7 + }; + + std::array fact; + + gen.Drop(2032); + gen.Generate(fact.begin(), fact.size()); + + ASSERT_EQ(expected, fact); + } + + { + uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05 }; + gen.Rekey(key, key + std::size(key)); + + std::array expected = + { + 0x06, 0x83, 0x26, 0xa2, 0x11, 0x84, 0x16, 0xd2, + 0x1f, 0x9d, 0x04, 0xb2, 0xcd, 0x1c, 0xa0, 0x50, + 0xff, 0x25, 0xb5, 0x89, 0x95, 0x99, 0x67, 0x07, + 0xe5, 0x1f, 0xbd, 0xf0, 0x8b, 0x34, 0xd8, 0x75 + }; + + std::array fact; + + gen.Drop(4080); + gen.Generate(fact.begin(), fact.size()); + + ASSERT_EQ(expected, fact); + } + + { + uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05 }; + gen.Rekey(key, key + std::size(key)); + + std::array expected = + { + 0x06, 0x83, 0x26, 0xa2, 0x11, 0x84, 0x16, 0xd2, + 0x1f, 0x9d, 0x04, 0xb2, 0xcd, 0x1c, 0xa0, 0x50, + 0xff, 0x25, 0xb5, 0x89, 0x95, 0x99, 0x67, 0x07, + 0xe5, 0x1f, 0xbd, 0xf0, 0x8b, 0x34, 0xd8, 0x75 + }; + + std::array fact; + + gen.Drop(4080); + gen.Generate(fact.begin(), fact.size()); + + ASSERT_EQ(expected, fact); + } + + // Key: 0x0102030405060708090a (80 bits) + + { + uint8_t key[] = + { + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a + }; + + gen.Rekey(key, key + std::size(key)); + + std::array expected = + { + 0xed, 0xe3, 0xb0, 0x46, 0x43, 0xe5, 0x86, 0xcc, + 0x90, 0x7d, 0xc2, 0x18, 0x51, 0x70, 0x99, 0x02, + 0x03, 0x51, 0x6b, 0xa7, 0x8f, 0x41, 0x3b, 0xeb, + 0x22, 0x3a, 0xa5, 0xd4, 0xd2, 0xdf, 0x67, 0x11 + }; + + std::array fact; + + gen.Generate(fact.begin(), fact.size()); + + ASSERT_EQ(expected, fact); + } + + { + uint8_t key[] = + { + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a + }; + + gen.Rekey(key, key + std::size(key)); + + std::array expected = + { + 0xb5, 0x5a, 0x0a, 0x5b, 0x51, 0xa1, 0x2a, 0x8b, + 0xe3, 0x48, 0x99, 0xc3, 0xe0, 0x47, 0x51, 0x1a, + 0xd9, 0xa0, 0x9c, 0xea, 0x3c, 0xe7, 0x5f, 0xe3, + 0x96, 0x98, 0x07, 0x03, 0x17, 0xa7, 0x13, 0x39 + }; + + std::array fact; + + gen.Drop(2032); + gen.Generate(fact.begin(), fact.size()); + + ASSERT_EQ(expected, fact); + } + + // Key: 0x0102030405060708090a0b0c0d0e0f10 (128 bits) + + { + uint8_t key[] = + { + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, + 0x0f, 0x10 + }; + + gen.Rekey(key, key + std::size(key)); + + std::array expected = + { + 0x9a, 0xc7, 0xcc, 0x9a, 0x60, 0x9d, 0x1e, 0xf7, + 0xb2, 0x93, 0x28, 0x99, 0xcd, 0xe4, 0x1b, 0x97, + 0x52, 0x48, 0xc4, 0x95, 0x90, 0x14, 0x12, 0x6a, + 0x6e, 0x8a, 0x84, 0xf1, 0x1d, 0x1a, 0x9e, 0x1c + }; + + std::array fact; + + gen.Generate(fact.begin(), fact.size()); + + ASSERT_EQ(expected, fact); + } + + { + uint8_t key[] = + { + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, + 0x0f, 0x10 + }; + + gen.Rekey(key, key + std::size(key)); + + std::array expected = + { + 0xd0, 0x3d, 0x1b, 0xc0, 0x3c, 0xd3, 0x3d, 0x70, + 0xdf, 0xf9, 0xfa, 0x5d, 0x71, 0x96, 0x3e, 0xbd, + 0x8a, 0x44, 0x12, 0x64, 0x11, 0xea, 0xa7, 0x8b, + 0xd5, 0x1e, 0x8d, 0x87, 0xa8, 0x87, 0x9b, 0xf5 + }; + + std::array fact; + + gen.Drop(2032); + gen.Generate(fact.begin(), fact.size()); + + ASSERT_EQ(expected, fact); + } + + // Key: 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 (256 bits) + + { + uint8_t key[] = + { + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, + 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, + 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, + 0x1d, 0x1e, 0x1f, 0x20 + }; + + gen.Rekey(key, key + std::size(key)); + + std::array expected = + { + 0xea, 0xa6, 0xbd, 0x25, 0x88, 0x0b, 0xf9, 0x3d, + 0x3f, 0x5d, 0x1e, 0x4c, 0xa2, 0x61, 0x1d, 0x91, + 0xcf, 0xa4, 0x5c, 0x9f, 0x7e, 0x71, 0x4b, 0x54, + 0xbd, 0xfa, 0x80, 0x02, 0x7c, 0xb1, 0x43, 0x80 + }; + + std::array fact; + + gen.Generate(fact.begin(), fact.size()); + + ASSERT_EQ(expected, fact); + } + + { + uint8_t key[] = + { + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, + 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, + 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, + 0x1d, 0x1e, 0x1f, 0x20 + }; + + gen.Rekey(key, key + std::size(key)); + + std::array expected = + { + 0xf6, 0x56, 0xab, 0xcc, 0xf8, 0x8d, 0xd8, 0x27, + 0x02, 0x7b, 0x2c, 0xe9, 0x17, 0xd4, 0x64, 0xec, + 0x18, 0xb6, 0x25, 0x03, 0xbf, 0xbc, 0x07, 0x7f, + 0xba, 0xbb, 0x98, 0xf2, 0x0d, 0x98, 0xab, 0x34 + }; + + std::array fact; + + gen.Drop(2032); + gen.Generate(fact.begin(), fact.size()); + + ASSERT_EQ(expected, fact); + } +} + +TEST(Arc4GenTests, TooSmallKeyTest) +{ + { + const char * key = "smal"; + + Arc4Gen gen; + ASSERT_THROW(gen.Rekey(key, key + strlen(key)), Chaos::Service::ChaosException); + + ASSERT_THROW(Arc4Gen(key, key + strlen(key)), Chaos::Service::ChaosException); + } +} + +TEST(Arc4GenTests, UninitializedGenTest) +{ + std::array out; + + { + Arc4Gen gen; + + ASSERT_THROW(gen.Generate(out.begin(), out.size()), Chaos::Service::ChaosException); + + ASSERT_THROW(gen.Drop(0), Chaos::Service::ChaosException); + ASSERT_THROW(gen.Drop(1), Chaos::Service::ChaosException); + ASSERT_THROW(gen.Drop(20), Chaos::Service::ChaosException); + ASSERT_THROW(gen.Drop(256), Chaos::Service::ChaosException); + } + + { + const char * key = "smal"; + + Arc4Gen gen; + + ASSERT_THROW(gen.Rekey(key, key + strlen(key)), Chaos::Service::ChaosException); + + ASSERT_THROW(gen.Generate(out.begin(), out.size()), Chaos::Service::ChaosException); + + ASSERT_THROW(gen.Drop(0), Chaos::Service::ChaosException); + ASSERT_THROW(gen.Drop(1), Chaos::Service::ChaosException); + ASSERT_THROW(gen.Drop(20), Chaos::Service::ChaosException); + ASSERT_THROW(gen.Drop(256), Chaos::Service::ChaosException); + } +}