Loading src/CMakeLists.txt +2 −0 Changes for src/CMakeLists.txt: 2 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -7,6 +7,7 @@ list(APPEND netplussrc crypto/base64.cpp crypto/des.cpp crypto/aes.cpp crypto/rc4.cpp ) if(${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Windows") Loading Loading @@ -45,6 +46,7 @@ set(headers crypto/base64.h crypto/des.h crypto/aes.h crypto/rc4.h ) add_library(netplus SHARED ${netplussrc} ${headers}) Loading src/crypto/rc4.cpp 0 → 100644 +112 −0 Changes for src/crypto/rc4.cpp: 112 added lines, 0 removed lines. Original line number Diff line number Diff line /******************************************************************************* * Copyright (c) 2025, Jan Koester jan.koester@gmx.net * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of the <organization> nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ #include "rc4.h" #include <cstring> // Helper function to swap two elements void netplus::rc4::swap(uint8_t& a, uint8_t& b) { uint8_t temp = a; a = b; b = temp; } /** * Key-Scheduling Algorithm (KSA) * Initializes the state vector S based on the key. */ void netplus::rc4::ksa(const uint8_t* key, size_t key_len) { if (key_len == 0 || key_len > SBOX_SIZE) { throw std::invalid_argument("rc4 Key-Länge muss zwischen 1 und 256 Bytes liegen."); } // 1. Initialization of the S-Box: S[i] = i for (int i = 0; i < SBOX_SIZE; ++i) { S[i] = (uint8_t)i; } // 2. Initial Permutation of S int j = 0; for (int i = 0; i < SBOX_SIZE; ++i) { // j = (j + S[i] + K[i mod key_len]) mod 256 j = (j + S[i] + key[i % key_len]) % SBOX_SIZE; swap(S[i], S[j]); } // After KSA, the PRGA state is reset to 0. reset_prga_state(); } /** * Konstruktor: Initialisiert KSA. */ netplus::rc4::rc4(const std::vector<uint8_t>& key) { // Ruft KSA mit den Schlüsselbytes auf ksa(key.data(), key.size()); } /** * Pseudo-Random Generation Algorithm (PRGA) - generates one keystream byte * Generates the keystream output based on the S-box. */ uint8_t netplus::rc4::PRGA_byte() { // i = (i + 1) mod 256 i_prga = (i_prga + 1) % SBOX_SIZE; // j = (j + S[i]) mod 256 j_prga = (j_prga + S[i_prga]) % SBOX_SIZE; // Swap S[i] and S[j] swap(S[i_prga], S[j_prga]); // t = (S[i] + S[j]) mod 256 int t = (S[i_prga] + S[j_prga]) % SBOX_SIZE; // Keystream-Byte = S[t] return S[t]; } /** * Encrypts/Decrypts data */ std::vector<uint8_t> netplus::rc4::crypt(const std::vector<uint8_t>& data) { std::vector<uint8_t> output; output.reserve(data.size()); for (uint8_t data_byte : data) { // Keystream-Byte generieren und mit dem Datenbyte XORen output.push_back(data_byte ^ PRGA_byte()); } return output; } /** * Resets the PRGA state */ void netplus::rc4::reset_prga_state() { i_prga = 0; j_prga = 0; } src/crypto/rc4.h 0 → 100644 +87 −0 Changes for src/crypto/rc4.h: 87 added lines, 0 removed lines. Original line number Diff line number Diff line /******************************************************************************* * Copyright (c) 2025, Jan Koester jan.koester@gmx.net * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of the <organization> nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ #include <vector> #include <string> #include <cstdint> #include <stdexcept> // Using #pragma once is common but often combined with include guards for max portability. // For simplicity here, we'll keep the structure you provided. #pragma once // The size of the state vector S is always 256 bytes #define SBOX_SIZE 256 namespace netplus { class rc4 { private: // The internal state vector S (S-Box) // Holds a permutation of the numbers 0 to 255. uint8_t S[SBOX_SIZE]; // Indices for the PRGA part (Pseudo-Random Generation Algorithm) int i_prga = 0; int j_prga = 0; // Helper function to swap two elements void swap(uint8_t& a, uint8_t& b); /** * @brief Initializes the state vector S (Key-Scheduling Algorithm - KSA). * @param key The secret key. * @param key_len The length of the key. */ void ksa(const uint8_t* key, size_t key_len); // ksa name is used here /** * @brief Generates a single keystream byte (Pseudo-Random Generation Algorithm - PRGA). * @return The next keystream byte. */ uint8_t PRGA_byte(); public: /** * @brief Constructor, initializes the RC4 instance with a key. * @param key The secret key as a std::vector of bytes. */ rc4(const std::vector<uint8_t>& key); /** * @brief Encrypts or decrypts data. * Since RC4 is a stream cipher and uses XOR, the function is symmetric. * @param data The data to be encrypted/decrypted. * @return The encrypted/decrypted data. */ std::vector<uint8_t> crypt(const std::vector<uint8_t>& data); /** * @brief Resets the PRGA state to start a new encryption/decryption * with the same key and the same initialized S-box state. * The S-box itself remains preserved. */ void reset_prga_state(); }; } test/CMakeLists.txt +9 −0 Changes for test/CMakeLists.txt: 9 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -12,6 +12,10 @@ add_executable(aes aes.cpp) target_link_libraries(aes netplus-static) add_executable(rc4 rc4.cpp) target_link_libraries(rc4 netplus-static) add_test( NAME dest_test COMMAND des Loading @@ -21,3 +25,8 @@ add_test( NAME aes_test COMMAND aes ) add_test( NAME rc4_test COMMAND rc4 ) test/rc4.cpp 0 → 100644 +125 −0 Changes for test/rc4.cpp: 125 added lines, 0 removed lines. Original line number Diff line number Diff line /******************************************************************************* * Copyright (c) 2025, Jan Koester jan.koester@gmx.net * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of the <organization> nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ #include <iostream> #include <vector> #include <algorithm> #include "../src/crypto/rc4.h" #define ASSERT_TRUE(condition, message) if (!(condition)) { \ std::cerr << "Assertion Failed: " << message << std::endl; \ throw std::runtime_error("Assertion Failed"); \ } // --- Helper Functions --- // Converts string literal to byte vector std::vector<uint8_t> string_to_bytes(const std::string& str) { return std::vector<uint8_t>(str.begin(), str.end()); } // Converts a hex string (e.g., "BBF316...") to a byte vector std::vector<uint8_t> hex_to_bytes(const std::string& hex_str) { std::vector<uint8_t> bytes; for (size_t i = 0; i < hex_str.length(); i += 2) { std::string byte_str = hex_str.substr(i, 2); bytes.push_back((uint8_t)std::stoul(byte_str, nullptr, 16)); } return bytes; } // --- Unit Tests --- void test_RC4_StandardVectorTest() { // Standard RC4 Test Vector: // Key: "Key" // Plaintext: "Plaintext" // Expected Ciphertext (Hex): BBF316E8D940AF0AD3 try { std::vector<uint8_t> key = string_to_bytes("Key"); std::vector<uint8_t> plaintext = string_to_bytes("Plaintext"); std::vector<uint8_t> expected_ciphertext = hex_to_bytes("BBF316E8D940AF0AD3"); netplus::rc4 cipher_enc(key); std::vector<uint8_t> actual_ciphertext = cipher_enc.crypt(plaintext); // Test 1: Check if ciphertext matches the standard vector ASSERT_TRUE(actual_ciphertext.size() == expected_ciphertext.size(), "Ciphertext size mismatch."); ASSERT_TRUE(std::equal(actual_ciphertext.begin(), actual_ciphertext.end(), expected_ciphertext.begin()), "Ciphertext content mismatch against standard test vector."); // Test 2: Check decryption (must reset PRGA state first) netplus::rc4 cipher_dec(key); std::vector<uint8_t> actual_decrypted = cipher_dec.crypt(actual_ciphertext); ASSERT_TRUE(std::equal(actual_decrypted.begin(), actual_decrypted.end(), plaintext.begin()), "Decryption failed. Result does not match original plaintext."); std::cout << "PASSED [RC4_StandardVectorTest]" << std::endl; }catch(const std::exception& e){ std::cerr << "FAILED [test_RC4_StandardVectorTest]: Exception: " << e.what() << std::endl; } } void test_RC4_LongKeyTest() { try { // RC4 works with keys up to 256 bytes. std::string long_key(256, 'A'); std::vector<uint8_t> key = string_to_bytes(long_key); std::vector<uint8_t> data = string_to_bytes("Test Data"); // ENCRYPTION: Use a cipher instance netplus::rc4 cipher_enc(key); std::vector<uint8_t> encrypted = cipher_enc.crypt(data); // Check if encryption resulted in some data ASSERT_TRUE(encrypted.size() == data.size(), "Long key encryption failed."); // DECRYPTION: Use a brand new cipher instance to reset the S-Box netplus::rc4 cipher_dec(key); // <--- FIX: Create new object for clean state std::vector<uint8_t> decrypted = cipher_dec.crypt(encrypted); ASSERT_TRUE(std::equal(decrypted.begin(), decrypted.end(), data.begin()), "Long key decryption failed."); std::cout << "PASSED [RC4_LongKeyTest]" << std::endl; } catch(const std::exception& e) { std::cerr << "FAILED [test_RC4_LongKeyTest]: Exception: " << e.what() << std::endl; } } // --- Main Runner --- int main() { std::cout << "--- Running RC4 Unit Tests ---" << std::endl; test_RC4_StandardVectorTest(); test_RC4_LongKeyTest(); std::cout << "--- Tests Complete ---" << std::endl; return 0; } Loading
src/CMakeLists.txt +2 −0 Changes for src/CMakeLists.txt: 2 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -7,6 +7,7 @@ list(APPEND netplussrc crypto/base64.cpp crypto/des.cpp crypto/aes.cpp crypto/rc4.cpp ) if(${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Windows") Loading Loading @@ -45,6 +46,7 @@ set(headers crypto/base64.h crypto/des.h crypto/aes.h crypto/rc4.h ) add_library(netplus SHARED ${netplussrc} ${headers}) Loading
src/crypto/rc4.cpp 0 → 100644 +112 −0 Changes for src/crypto/rc4.cpp: 112 added lines, 0 removed lines. Original line number Diff line number Diff line /******************************************************************************* * Copyright (c) 2025, Jan Koester jan.koester@gmx.net * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of the <organization> nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ #include "rc4.h" #include <cstring> // Helper function to swap two elements void netplus::rc4::swap(uint8_t& a, uint8_t& b) { uint8_t temp = a; a = b; b = temp; } /** * Key-Scheduling Algorithm (KSA) * Initializes the state vector S based on the key. */ void netplus::rc4::ksa(const uint8_t* key, size_t key_len) { if (key_len == 0 || key_len > SBOX_SIZE) { throw std::invalid_argument("rc4 Key-Länge muss zwischen 1 und 256 Bytes liegen."); } // 1. Initialization of the S-Box: S[i] = i for (int i = 0; i < SBOX_SIZE; ++i) { S[i] = (uint8_t)i; } // 2. Initial Permutation of S int j = 0; for (int i = 0; i < SBOX_SIZE; ++i) { // j = (j + S[i] + K[i mod key_len]) mod 256 j = (j + S[i] + key[i % key_len]) % SBOX_SIZE; swap(S[i], S[j]); } // After KSA, the PRGA state is reset to 0. reset_prga_state(); } /** * Konstruktor: Initialisiert KSA. */ netplus::rc4::rc4(const std::vector<uint8_t>& key) { // Ruft KSA mit den Schlüsselbytes auf ksa(key.data(), key.size()); } /** * Pseudo-Random Generation Algorithm (PRGA) - generates one keystream byte * Generates the keystream output based on the S-box. */ uint8_t netplus::rc4::PRGA_byte() { // i = (i + 1) mod 256 i_prga = (i_prga + 1) % SBOX_SIZE; // j = (j + S[i]) mod 256 j_prga = (j_prga + S[i_prga]) % SBOX_SIZE; // Swap S[i] and S[j] swap(S[i_prga], S[j_prga]); // t = (S[i] + S[j]) mod 256 int t = (S[i_prga] + S[j_prga]) % SBOX_SIZE; // Keystream-Byte = S[t] return S[t]; } /** * Encrypts/Decrypts data */ std::vector<uint8_t> netplus::rc4::crypt(const std::vector<uint8_t>& data) { std::vector<uint8_t> output; output.reserve(data.size()); for (uint8_t data_byte : data) { // Keystream-Byte generieren und mit dem Datenbyte XORen output.push_back(data_byte ^ PRGA_byte()); } return output; } /** * Resets the PRGA state */ void netplus::rc4::reset_prga_state() { i_prga = 0; j_prga = 0; }
src/crypto/rc4.h 0 → 100644 +87 −0 Changes for src/crypto/rc4.h: 87 added lines, 0 removed lines. Original line number Diff line number Diff line /******************************************************************************* * Copyright (c) 2025, Jan Koester jan.koester@gmx.net * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of the <organization> nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ #include <vector> #include <string> #include <cstdint> #include <stdexcept> // Using #pragma once is common but often combined with include guards for max portability. // For simplicity here, we'll keep the structure you provided. #pragma once // The size of the state vector S is always 256 bytes #define SBOX_SIZE 256 namespace netplus { class rc4 { private: // The internal state vector S (S-Box) // Holds a permutation of the numbers 0 to 255. uint8_t S[SBOX_SIZE]; // Indices for the PRGA part (Pseudo-Random Generation Algorithm) int i_prga = 0; int j_prga = 0; // Helper function to swap two elements void swap(uint8_t& a, uint8_t& b); /** * @brief Initializes the state vector S (Key-Scheduling Algorithm - KSA). * @param key The secret key. * @param key_len The length of the key. */ void ksa(const uint8_t* key, size_t key_len); // ksa name is used here /** * @brief Generates a single keystream byte (Pseudo-Random Generation Algorithm - PRGA). * @return The next keystream byte. */ uint8_t PRGA_byte(); public: /** * @brief Constructor, initializes the RC4 instance with a key. * @param key The secret key as a std::vector of bytes. */ rc4(const std::vector<uint8_t>& key); /** * @brief Encrypts or decrypts data. * Since RC4 is a stream cipher and uses XOR, the function is symmetric. * @param data The data to be encrypted/decrypted. * @return The encrypted/decrypted data. */ std::vector<uint8_t> crypt(const std::vector<uint8_t>& data); /** * @brief Resets the PRGA state to start a new encryption/decryption * with the same key and the same initialized S-box state. * The S-box itself remains preserved. */ void reset_prga_state(); }; }
test/CMakeLists.txt +9 −0 Changes for test/CMakeLists.txt: 9 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -12,6 +12,10 @@ add_executable(aes aes.cpp) target_link_libraries(aes netplus-static) add_executable(rc4 rc4.cpp) target_link_libraries(rc4 netplus-static) add_test( NAME dest_test COMMAND des Loading @@ -21,3 +25,8 @@ add_test( NAME aes_test COMMAND aes ) add_test( NAME rc4_test COMMAND rc4 )
test/rc4.cpp 0 → 100644 +125 −0 Changes for test/rc4.cpp: 125 added lines, 0 removed lines. Original line number Diff line number Diff line /******************************************************************************* * Copyright (c) 2025, Jan Koester jan.koester@gmx.net * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of the <organization> nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ #include <iostream> #include <vector> #include <algorithm> #include "../src/crypto/rc4.h" #define ASSERT_TRUE(condition, message) if (!(condition)) { \ std::cerr << "Assertion Failed: " << message << std::endl; \ throw std::runtime_error("Assertion Failed"); \ } // --- Helper Functions --- // Converts string literal to byte vector std::vector<uint8_t> string_to_bytes(const std::string& str) { return std::vector<uint8_t>(str.begin(), str.end()); } // Converts a hex string (e.g., "BBF316...") to a byte vector std::vector<uint8_t> hex_to_bytes(const std::string& hex_str) { std::vector<uint8_t> bytes; for (size_t i = 0; i < hex_str.length(); i += 2) { std::string byte_str = hex_str.substr(i, 2); bytes.push_back((uint8_t)std::stoul(byte_str, nullptr, 16)); } return bytes; } // --- Unit Tests --- void test_RC4_StandardVectorTest() { // Standard RC4 Test Vector: // Key: "Key" // Plaintext: "Plaintext" // Expected Ciphertext (Hex): BBF316E8D940AF0AD3 try { std::vector<uint8_t> key = string_to_bytes("Key"); std::vector<uint8_t> plaintext = string_to_bytes("Plaintext"); std::vector<uint8_t> expected_ciphertext = hex_to_bytes("BBF316E8D940AF0AD3"); netplus::rc4 cipher_enc(key); std::vector<uint8_t> actual_ciphertext = cipher_enc.crypt(plaintext); // Test 1: Check if ciphertext matches the standard vector ASSERT_TRUE(actual_ciphertext.size() == expected_ciphertext.size(), "Ciphertext size mismatch."); ASSERT_TRUE(std::equal(actual_ciphertext.begin(), actual_ciphertext.end(), expected_ciphertext.begin()), "Ciphertext content mismatch against standard test vector."); // Test 2: Check decryption (must reset PRGA state first) netplus::rc4 cipher_dec(key); std::vector<uint8_t> actual_decrypted = cipher_dec.crypt(actual_ciphertext); ASSERT_TRUE(std::equal(actual_decrypted.begin(), actual_decrypted.end(), plaintext.begin()), "Decryption failed. Result does not match original plaintext."); std::cout << "PASSED [RC4_StandardVectorTest]" << std::endl; }catch(const std::exception& e){ std::cerr << "FAILED [test_RC4_StandardVectorTest]: Exception: " << e.what() << std::endl; } } void test_RC4_LongKeyTest() { try { // RC4 works with keys up to 256 bytes. std::string long_key(256, 'A'); std::vector<uint8_t> key = string_to_bytes(long_key); std::vector<uint8_t> data = string_to_bytes("Test Data"); // ENCRYPTION: Use a cipher instance netplus::rc4 cipher_enc(key); std::vector<uint8_t> encrypted = cipher_enc.crypt(data); // Check if encryption resulted in some data ASSERT_TRUE(encrypted.size() == data.size(), "Long key encryption failed."); // DECRYPTION: Use a brand new cipher instance to reset the S-Box netplus::rc4 cipher_dec(key); // <--- FIX: Create new object for clean state std::vector<uint8_t> decrypted = cipher_dec.crypt(encrypted); ASSERT_TRUE(std::equal(decrypted.begin(), decrypted.end(), data.begin()), "Long key decryption failed."); std::cout << "PASSED [RC4_LongKeyTest]" << std::endl; } catch(const std::exception& e) { std::cerr << "FAILED [test_RC4_LongKeyTest]: Exception: " << e.what() << std::endl; } } // --- Main Runner --- int main() { std::cout << "--- Running RC4 Unit Tests ---" << std::endl; test_RC4_StandardVectorTest(); test_RC4_LongKeyTest(); std::cout << "--- Tests Complete ---" << std::endl; return 0; }