Loading src/crypto/aes.cpp +242 −123 Changes for src/crypto/aes.cpp: 242 added lines, 123 removed lines. Original line number Diff line number Diff line Loading @@ -35,8 +35,7 @@ namespace netplus { // --- Global AES Tables Definition --- // You MUST populate these tables with the correct FIPS 197 standard values. // // FIPS 197 Standard S-Box const uint8_t S_BOX[256] = { 0x63,0x7c,0x77,0x7b,0xf2,0x6b,0x6f,0xc5,0x30,0x01,0x67,0x2b,0xfe,0xd7,0xab,0x76, Loading @@ -45,16 +44,16 @@ const uint8_t S_BOX[256] = { 0x04,0xc7,0x23,0xc3,0x18,0x96,0x05,0x9a,0x07,0x12,0x80,0xe2,0xeb,0x27,0xb2,0x75, 0x09,0x83,0x2c,0x1a,0x1b,0x6e,0x5a,0xa0,0x52,0x3b,0xd6,0xb3,0x29,0xe3,0x2f,0x84, 0x53,0xd1,0x00,0xed,0x20,0xfc,0xb1,0x5b,0x6a,0xcb,0xbe,0x39,0x4a,0x4c,0x58,0xcf, 0xd0, 0xee, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae,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0xa1,0x89,0x0d,0xbf,0xe6,0x42,0x68,0x41,0x99,0x2d,0x0f,0xb0,0x54,0xbb,0x16 }; // FIPS 197 Standard Inverse S-Box Loading @@ -80,72 +79,143 @@ const uint8_t INV_S_BOX[256] = { // FIPS 197 Standard Rcon (only the first byte of each word is non-zero) // Rcon[i] = { Rcon_i, 0x00, 0x00, 0x00 } const uint8_t RCON[10] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36 }; // Logarithm table: log[i] = power 'x' such that g^x = i (g=0x03) // FIPS 197 Standard Logarithm table: log[i] = power 'x' such that g^x = i (g=0x03) const uint8_t LOG_TABLE[256] = { 0x00, 0x00, 0x19, 0x01, 0x32, 0x02, 0x1a, 0xc6, 0x4b, 0xc7, 0x1b, 0x68, 0x33, 0xee, 0xdf, 0x03, 0x64, 0x04, 0xde, 0x34, 0x35, 0x6a, 0x1c, 0x48, 0x69, 0x76, 0x1d, 0xc5, 0x46, 0xd4, 0x47, 0x54, 0x56, 0x05, 0xd3, 0x36, 0x5e, 0x6b, 0x71, 0x06, 0x40, 0x07, 0x86, 0x6c, 0x93, 0xd0, 0x08, 0x88, 0x6d, 0x97, 0x37, 0x8d, 0x96, 0x38, 0x6f, 0xad, 0x09, 0xb3, 0x39, 0x24, 0x98, 0x0a, 0xa3, 0x0b, 0x8f, 0x6e, 0x45, 0x81, 0x41, 0x22, 0x78, 0x9c, 0x7a, 0x1e, 0x49, 0xc1, 0x42, 0x5f, 0x59, 0x0c, 0xb6, 0x43, 0x44, 0x7f, 0x27, 0x82, 0x23, 0x73, 0xb7, 0x4e, 0x83, 0x2b, 0x84, 0x7e, 0x21, 0xd2, 0xda, 0x85, 0xa6, 0x6f, 0x12, 0x87, 0xae, 0xf6, 0x99, 0x8c, 0x9a, 0x1f, 0x4a, 0x4c, 0xc4, 0xc2, 0xfb, 0xca, 0x1c, 0x3c, 0x20, 0x91, 0x65, 0x3c, 0x0d, 0x4f, 0x1c, 0x1c, 0x1c, 0x1c, 0x9d, 0x66, 0x6c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x4d, 0x50, 0xe1, 0x8e, 0x0f, 0xec, 0x80, 0xc8, 0xaa, 0x57, 0xfa, 0x3e, 0x13, 0x72, 0x6a, 0x93, 0x17, 0x05, 0x51, 0x64, 0x49, 0x04, 0x2b, 0x2f, 0x3d, 0x9e, 0xa3, 0x6b, 0x3f, 0xa1, 0x4e, 0x40, 0x8a, 0x8b, 0x0c, 0x8c, 0x0f, 0xec, 0x80, 0x57, 0xfa, 0x3e, 0x13, 0x72, 0x6a, 0x93, 0x17, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x19, 0x01, 0x32, 0x02, 0x1a, 0xc6, 0x4b, 0xc7, 0x1b, 0x68, 0x33, 0xee, 0xdf, 0x03, 0x64, 0x04, 0xde, 0x34, 0xe5, 0x6a, 0x1c, 0x48, 0x69, 0x76, 0x1d, 0xc5, 0x46, 0xd4, 0x47, 0x54, 0x92, 0x57, 0x35, 0x7d, 0x49, 0x6d, 0x87, 0x55, 0x5a, 0x82, 0x38, 0x1f, 0x93, 0xe6, 0x73, 0x05, 0x58, 0x91, 0x21, 0x95, 0x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, 0x5e, 0xe2, 0x3d, 0x4d, 0xd7, 0xf2, 0x3b, 0x5d }; // --- Helper Functions --- // Simple Galois Field multiplication (gmul) for MixColumns uint8_t aes::gmul(uint8_t a, uint8_t b) { if (a == 0 || b == 0) { return 0; static void aes_self_check() { // 1) S-Box / InvS-Box must be inverse permutations for (int x = 0; x < 256; ++x) { uint8_t y = S_BOX[x]; uint8_t z = INV_S_BOX[y]; if (z != (uint8_t)x) { std::cerr << "AES SELF-CHECK FAILED: INV_S_BOX[S_BOX[" << std::hex << x << "]] != " << x << " (got " << (int)z << ")\n" << std::dec; std::abort(); } } // 2) GF(2^8) multiplication sanity check from FIPS-197 examples // 0x57 * 0x13 = 0xFE in Rijndael field if (aes::gmul(0x57, 0x13) != 0xFE) { std::cerr << "AES SELF-CHECK FAILED: gmul(0x57,0x13) != 0xFE (got 0x" << std::hex << (int)aes::gmul(0x57, 0x13) << ")\n" << std::dec; std::abort(); } // 1. Convert factors to their logarithms uint16_t log_a = LOG_TABLE[a]; uint16_t log_b = LOG_TABLE[b]; // 3) KeyExpansion spot-check for FIPS-197 Appendix C.1 key: W[4] = 0xA0FAFE17 const key128 key = { 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6, 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c }; auto bytesToWord = [] (const uint8_t* b) -> uint32_t { return (uint32_t(b[0]) << 24) | (uint32_t(b[1]) << 16) | (uint32_t(b[2]) << 8) | uint32_t(b[3]); }; uint32_t w0 = bytesToWord(&key[0]); uint32_t w3 = bytesToWord(&key[12]); uint32_t rotated = aes::rotWord(w3); uint32_t subbed = aes::subWord(rotated); uint32_t temp = subbed ^ (uint32_t(RCON[0]) << 24); uint32_t w4 = w0 ^ temp; if (w4 != 0xA0FAFE17u) { std::cerr << "AES SELF-CHECK FAILED: KeyExpansion W[4] != 0xA0FAFE17 (got 0x" << std::hex << w4 << ")\n" << std::dec; std::abort(); } } static inline uint8_t xtime(uint8_t x) { return (uint8_t)((x << 1) ^ ((x & 0x80) ? 0x1B : 0x00)); } static inline uint8_t mul03(uint8_t x) { // multiply by 0x03 in GF(2^8): (02*x) XOR x return (uint8_t)(xtime(x) ^ x); } static uint8_t LOG_TABLE_GEN[256]; static uint8_t ALOG_TABLE_GEN[256]; static bool GF_TABLES_READY = false; static void init_gf_tables() { if (GF_TABLES_READY) return; // log(0) is undefined; keep 0 as a placeholder std::memset(LOG_TABLE_GEN, 0, sizeof(LOG_TABLE_GEN)); // 2. Add logarithms (this is the multiplication step) uint16_t sum = log_a + log_b; // ALOG[i] = 0x03^i in GF(2^8) uint8_t x = 0x01; for (int i = 0; i < 255; ++i) { ALOG_TABLE_GEN[i] = x; LOG_TABLE_GEN[x] = (uint8_t)i; x = mul03(x); } // Common convenience: duplicate wrap value ALOG_TABLE_GEN[255] = ALOG_TABLE_GEN[0]; // 3. Modulo 255 reduction: (g^255 = g^0 = 1). Since log values only go up to 254. // The tables are only valid up to index 254 (0xFF). if (sum > 254) { sum = sum % 255; GF_TABLES_READY = true; } // 4. Convert back using antilogarithm table return ALOG_TABLE[sum]; // Simple Galois Field multiplication (gmul) for MixColumns uint8_t aes::gmul(uint8_t a, uint8_t b) { init_gf_tables(); if (a == 0 || b == 0) return 0; uint16_t la = LOG_TABLE_GEN[a]; uint16_t lb = LOG_TABLE_GEN[b]; uint16_t sum = la + lb; sum %= 255; return ALOG_TABLE_GEN[sum]; } // RotWord (Cyclic shift left by 1 byte) Loading Loading @@ -195,26 +265,58 @@ void aes::keyExpansion(const key128& key) { i++; } // START DEBUG PRINTS // std::cout << "DEBUG: --- Key Expansion Trace Start ---" << std::endl; // std::cout << std::hex << std::setw(0) << std::setfill(' '); // Resetting hex formatting // std::cout << "DEBUG: Initial Key Words (W[0]-W[3]):" << std::endl; // for(int k=0; k<Nk; ++k) { // std::cout << "W[" << k << "]: 0x" << std::setw(8) << std::setfill('0') << w[k] << std::endl; // } // std::cout << "DEBUG: Standard NIST W[4] expected: 0xb066f81f" << std::endl; // END DEBUG PRINTS // 2. Generate the remaining words while (i < KEY_WORDS) { uint32_t temp = w[i - 1]; // Apply transformation to the first word of every key (when i is a multiple of Nk, i.e., 4, 8, 12, ...) if (i % Nk == 0) { // RotWord: Cyclic shift left by 1 byte temp = rotWord(temp); uint32_t rotated = rotWord(temp); // SubWord: Substitute each byte using the S-Box temp = subWord(temp); uint32_t subbed = subWord(rotated); // XOR with Rcon[i/Nk - 1] (The Rcon table is only 10 bytes long) // The Rcon value is only applied to the MSB of the word. uint32_t rcon_value = static_cast<uint32_t>(RCON[i / Nk - 1]) << 24; temp ^= rcon_value; temp = subbed ^ rcon_value; // START DEBUG PRINTS for W[4] // if (i == Nk) { // std::cout << "DEBUG: Rcon index: " << std::dec << (i/Nk - 1) << std::endl; // std::cout << std::hex << std::setw(0) << std::setfill(' '); // Resetting hex formatting // std::cout << "DEBUG: T(W3) Input (W[3]): 0x" << std::setw(8) << std::setfill('0') << w[i - 1] << std::endl; // std::cout << "DEBUG: RotWord result: 0x" << std::setw(8) << std::setfill('0') << rotated << std::endl; // std::cout << "DEBUG: SubWord result: 0x" << std::setw(8) << std::setfill('0') << subbed << std::endl; // std::cout << "DEBUG: Rcon value: 0x" << std::setw(8) << std::setfill('0') << rcon_value << std::endl; // std::cout << "DEBUG: T(W3) Final (temp): 0x" << std::setw(8) << std::setfill('0') << temp << std::endl; // } // END DEBUG PRINTS } // The new word is the previous word XORed with the word Nk positions before (w[i-Nk]) w[i] = w[i - Nk] ^ temp; // START DEBUG PRINTS for W[4] // if (i == Nk) { // std::cout << "DEBUG: W[0] (XOR base): 0x" << std::setw(8) << std::setfill('0') << w[i - Nk] << std::endl; // std::cout << "DEBUG: Calculated W[4]: 0x" << std::setw(8) << std::setfill('0') << w[i] << std::endl; // } // END DEBUG PRINTS i++; } Loading @@ -227,11 +329,17 @@ void aes::keyExpansion(const key128& key) { } m_roundKeys.push_back(roundKey); } // std::cout << "DEBUG: --- Key Expansion Trace End ---" << std::endl; // Final marker } // --- Constructor --- aes::aes(const key128& key) { static bool checked = false; if (!checked) { aes_self_check(); checked = true; } if (key.size() != 16) { throw std::invalid_argument("aes-128 key must be 16 bytes."); } Loading @@ -252,18 +360,16 @@ void aes::subBytes(block128& state){ } void aes::shiftRows(block128& state){ // The state is conceptually a 4x4 matrix, stored column-major: // [0] [4] [8] [12] // [1] [5] [9] [13] // [2] [6] [10] [14] // [3] [7] [11] [15] // We use a temporary array because the shifts must be applied simultaneously. // ... (copy to temp is correct) ... uint8_t temp[16]; std::copy(state.begin(), state.end(), temp); // Row 0: No shift // temp[0] = state[0], temp[4] = state[4], temp[8] = state[8], temp[12] = state[12]; // Row 0: No shift (copy back to state) // Indices: 0, 4, 8, 12 state[0] = temp[0]; state[4] = temp[4]; state[8] = temp[8]; state[12] = temp[12]; // Row 1: Shift left by 1 byte (indices 1, 5, 9, 13) // New indices: 5, 9, 13, 1 Loading @@ -287,67 +393,45 @@ void aes::shiftRows(block128& state){ state[15] = temp[11]; } // In aes.cpp // Multiplies by 0x02 (left shift and conditional reduction by 0x1b) uint8_t gmul_02(uint8_t a) { uint8_t result = a << 1; // Check if MSB was set before the shift if (a & 0x80) { result ^= 0x1B; // XOR with the irreducible polynomial 0x11B (00011011) } return result; } // Multiplies by 0x03 (gmul_02(a) XOR a) uint8_t gmul_03(uint8_t a) { return gmul_02(a) ^ a; } void aes::mixColumns(block128& state){ uint8_t temp[16]; std::copy(state.begin(), state.end(), temp); // Iterate over the four columns (c = 0 to 3) for (int c = 0; c < 4; ++c) { // Shorthands for the original column bytes uint8_t s0 = temp[4 * c + 0]; uint8_t s1 = temp[4 * c + 1]; uint8_t s2 = temp[4 * c + 2]; uint8_t s3 = temp[4 * c + 3]; // The four equations from the matrix multiplication: // s'0 = (02)s0 + (03)s1 + (01)s2 + (01)s3 state[4 * c + 0] = gmul(0x02, s0) ^ gmul(0x03, s1) ^ gmul(0x01, s2) ^ gmul(0x01, s3); state[4 * c + 0] = gmul_02(s0) ^ gmul_03(s1) ^ s2 ^ s3; // s'1 = (01)s0 + (02)s1 + (03)s2 + (01)s3 state[4 * c + 1] = gmul(0x01, s0) ^ gmul(0x02, s1) ^ gmul(0x03, s2) ^ gmul(0x01, s3); state[4 * c + 1] = s0 ^ gmul_02(s1) ^ gmul_03(s2) ^ s3; // s'2 = (01)s0 + (01)s1 + (02)s2 + (03)s3 state[4 * c + 2] = gmul(0x01, s0) ^ gmul(0x01, s1) ^ gmul(0x02, s2) ^ gmul(0x03, s3); state[4 * c + 2] = s0 ^ s1 ^ gmul_02(s2) ^ gmul_03(s3); // s'3 = (03)s0 + (01)s1 + (01)s2 + (02)s3 state[4 * c + 3] = gmul(0x03, s0) ^ gmul(0x01, s1) ^ gmul(0x01, s2) ^ gmul(0x02, s3); state[4 * c + 3] = gmul_03(s0) ^ s1 ^ s2 ^ gmul_02(s3); } } block128 aes::encrypt(const block128& plaintext){ block128 state = plaintext; const int NUM_ROUNDS = 10; // 1. Initial AddRoundKey (Round 0) addRoundKey(state, m_roundKeys[0]); // 2. Main Rounds (1 to 9) for (int i = 1; i < NUM_ROUNDS; ++i) { subBytes(state); shiftRows(state); mixColumns(state); addRoundKey(state, m_roundKeys[i]); if (i == 1) { // Print state after Round 1 for comparison std::cout << "DEBUG: State after Round 1 (i=" << i << "): "; // Use a temporary print function here // print_block(state); // std::cout << std::endl; } for (const auto& byte : state) { std::cout << std::hex << std::setw(2) << std::setfill('0') << (int)byte; } } // 3. Final Round (Round 10 - No MixColumns) subBytes(state); shiftRows(state); addRoundKey(state, m_roundKeys[NUM_ROUNDS]); return state; } // --- Decryption Implementation --- void aes::invSubBytes(block128& state){ for (int i = 0; i < 16; ++i) { Loading Loading @@ -388,7 +472,6 @@ void aes::invShiftRows(block128& state){ // In aes.cpp (Note: The function signature below is NON-const, // as it must modify the 'state' passed by reference) void aes::invMixColumns(block128& state){ uint8_t temp[16]; // Copy the original state to a temporary buffer before modification. Loading Loading @@ -421,29 +504,65 @@ void aes::invMixColumns(block128& state){ } } block128 aes::encrypt(const block128& plaintext) { // FIPS-197 test vectors are already in AES State byte order (column-major), // so DO NOT transpose. block128 state = plaintext; const int NUM_ROUNDS = 10; // Round 0 addRoundKey(state, m_roundKeys[0]); // Rounds 1..9 for (int r = 1; r < NUM_ROUNDS; ++r) { subBytes(state); shiftRows(state); mixColumns(state); addRoundKey(state, m_roundKeys[r]); // if (r == 1) { // std::cout << "DEBUG: State after Round 1 (r=" << r << "): "; // for (uint8_t b : state) { // std::cout << std::hex << std::setw(2) << std::setfill('0') << (int)b; // } // std::cout << std::dec << "\n"; // } } // Round 10 (no MixColumns) subBytes(state); shiftRows(state); addRoundKey(state, m_roundKeys[NUM_ROUNDS]); return state; // returned in AES State byte order (matches FIPS vector hex) } block128 aes::decrypt(const block128& ciphertext) { // Same rule: ciphertext is already in AES State byte order (column-major). block128 state = ciphertext; const int NUM_ROUNDS = 10; // 1. Initial AddRoundKey (using the last round key) // Round 10 key addRoundKey(state, m_roundKeys[NUM_ROUNDS]); // 2. Inverse Final Round (Round 9) // Inverse Round 10 (i.e., undo final round) invShiftRows(state); invSubBytes(state); // 3. Inverse Main Rounds (9 down to 1) for (int i = NUM_ROUNDS - 1; i >= 1; --i) { addRoundKey(state, m_roundKeys[i]); // Rounds 9..1 for (int r = NUM_ROUNDS - 1; r >= 1; --r) { addRoundKey(state, m_roundKeys[r]); invMixColumns(state); invShiftRows(state); invSubBytes(state); } // 4. Final AddRoundKey (Round 0) // Round 0 key addRoundKey(state, m_roundKeys[0]); return state; return state; // plaintext in AES State byte order (matches how you pass vectors) } // In aes.cpp Loading src/crypto/aes.h +6 −3 Changes for src/crypto/aes.h: 6 added lines, 3 removed lines. Original line number Diff line number Diff line Loading @@ -43,6 +43,8 @@ extern const uint8_t S_BOX[256]; extern const uint8_t INV_S_BOX[256]; extern const uint8_t RCON[10]; // Rcon values for key expansion (only 10 needed for AES-128) static void aes_self_check(); class aes { private: // Stores the expanded round keys (11 keys * 16 bytes = 176 bytes for AES-128) Loading @@ -52,8 +54,8 @@ private: // Key Expansion and related helpers void keyExpansion(const key128& key); uint32_t subWord(uint32_t word); uint32_t rotWord(uint32_t word); static uint32_t subWord(uint32_t word); static uint32_t rotWord(uint32_t word); // Round operations void subBytes(block128& state); Loading @@ -67,8 +69,9 @@ private: void invMixColumns(block128& state); // Helper function for Galois Field multiplication in mixColumns uint8_t gmul(uint8_t a, uint8_t b); static uint8_t gmul(uint8_t a, uint8_t b); friend void aes_self_check(); public: /** * @brief Constructor for the aes class (AES-128). Performs key expansion. Loading test/aes.cpp +7 −7 File changed.Preview size limit exceeded, changes collapsed. Show changes test/des.cpp +1 −0 Changes for test/des.cpp: 1 added line, 0 removed lines. Original line number Diff line number Diff line Loading @@ -334,3 +334,4 @@ int main() { std::cout << "\nAll unit tests passed." << std::endl; return 0; // Success } Loading
src/crypto/aes.cpp +242 −123 Changes for src/crypto/aes.cpp: 242 added lines, 123 removed lines. Original line number Diff line number Diff line Loading @@ -35,8 +35,7 @@ namespace netplus { // --- Global AES Tables Definition --- // You MUST populate these tables with the correct FIPS 197 standard values. // // FIPS 197 Standard S-Box const uint8_t S_BOX[256] = { 0x63,0x7c,0x77,0x7b,0xf2,0x6b,0x6f,0xc5,0x30,0x01,0x67,0x2b,0xfe,0xd7,0xab,0x76, Loading @@ -45,16 +44,16 @@ const uint8_t S_BOX[256] = { 0x04,0xc7,0x23,0xc3,0x18,0x96,0x05,0x9a,0x07,0x12,0x80,0xe2,0xeb,0x27,0xb2,0x75, 0x09,0x83,0x2c,0x1a,0x1b,0x6e,0x5a,0xa0,0x52,0x3b,0xd6,0xb3,0x29,0xe3,0x2f,0x84, 0x53,0xd1,0x00,0xed,0x20,0xfc,0xb1,0x5b,0x6a,0xcb,0xbe,0x39,0x4a,0x4c,0x58,0xcf, 0xd0, 0xee, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, 0x51, 0xa3, 0x40, 0x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0x88,0x46,0xee,0xb8,0x14,0xde,0x5e,0x0b,0xdb, 0xe0,0x32,0x3a,0x0a,0x49,0x06,0x24,0x5c,0xc2,0xd3,0xac,0x62,0x91,0x95,0xe4,0x79, 0xe7,0xc8,0x37,0x6d,0x8d,0xd5,0x4e,0xa9,0x6c,0x56,0xf4,0xea,0x65,0x7a,0xae,0x08, 0xba,0x78,0x25,0x2e,0x1c,0xa6,0xb4,0xc6,0xe8,0xdd,0x74,0x1f,0x4b,0xbd,0x8b,0x8a, 0x70,0x3e,0xb5,0x66,0x48,0x03,0xf6,0x0e,0x61,0x35,0x57,0xb9,0x86,0xc1,0x1d,0x9e, 0xe1,0xf8,0x98,0x11,0x69,0xd9,0x8e,0x94,0x9b,0x1e,0x87,0xe9,0xce,0x55,0x28,0xdf, 0x8c,0xa1,0x89,0x0d,0xbf,0xe6,0x42,0x68,0x41,0x99,0x2d,0x0f,0xb0,0x54,0xbb,0x16 }; // FIPS 197 Standard Inverse S-Box Loading @@ -80,72 +79,143 @@ const uint8_t INV_S_BOX[256] = { // FIPS 197 Standard Rcon (only the first byte of each word is non-zero) // Rcon[i] = { Rcon_i, 0x00, 0x00, 0x00 } const uint8_t RCON[10] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36 }; // Logarithm table: log[i] = power 'x' such that g^x = i (g=0x03) // FIPS 197 Standard Logarithm table: log[i] = power 'x' such that g^x = i (g=0x03) const uint8_t LOG_TABLE[256] = { 0x00, 0x00, 0x19, 0x01, 0x32, 0x02, 0x1a, 0xc6, 0x4b, 0xc7, 0x1b, 0x68, 0x33, 0xee, 0xdf, 0x03, 0x64, 0x04, 0xde, 0x34, 0x35, 0x6a, 0x1c, 0x48, 0x69, 0x76, 0x1d, 0xc5, 0x46, 0xd4, 0x47, 0x54, 0x56, 0x05, 0xd3, 0x36, 0x5e, 0x6b, 0x71, 0x06, 0x40, 0x07, 0x86, 0x6c, 0x93, 0xd0, 0x08, 0x88, 0x6d, 0x97, 0x37, 0x8d, 0x96, 0x38, 0x6f, 0xad, 0x09, 0xb3, 0x39, 0x24, 0x98, 0x0a, 0xa3, 0x0b, 0x8f, 0x6e, 0x45, 0x81, 0x41, 0x22, 0x78, 0x9c, 0x7a, 0x1e, 0x49, 0xc1, 0x42, 0x5f, 0x59, 0x0c, 0xb6, 0x43, 0x44, 0x7f, 0x27, 0x82, 0x23, 0x73, 0xb7, 0x4e, 0x83, 0x2b, 0x84, 0x7e, 0x21, 0xd2, 0xda, 0x85, 0xa6, 0x6f, 0x12, 0x87, 0xae, 0xf6, 0x99, 0x8c, 0x9a, 0x1f, 0x4a, 0x4c, 0xc4, 0xc2, 0xfb, 0xca, 0x1c, 0x3c, 0x20, 0x91, 0x65, 0x3c, 0x0d, 0x4f, 0x1c, 0x1c, 0x1c, 0x1c, 0x9d, 0x66, 0x6c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x4d, 0x50, 0xe1, 0x8e, 0x0f, 0xec, 0x80, 0xc8, 0xaa, 0x57, 0xfa, 0x3e, 0x13, 0x72, 0x6a, 0x93, 0x17, 0x05, 0x51, 0x64, 0x49, 0x04, 0x2b, 0x2f, 0x3d, 0x9e, 0xa3, 0x6b, 0x3f, 0xa1, 0x4e, 0x40, 0x8a, 0x8b, 0x0c, 0x8c, 0x0f, 0xec, 0x80, 0x57, 0xfa, 0x3e, 0x13, 0x72, 0x6a, 0x93, 0x17, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x19, 0x01, 0x32, 0x02, 0x1a, 0xc6, 0x4b, 0xc7, 0x1b, 0x68, 0x33, 0xee, 0xdf, 0x03, 0x64, 0x04, 0xde, 0x34, 0xe5, 0x6a, 0x1c, 0x48, 0x69, 0x76, 0x1d, 0xc5, 0x46, 0xd4, 0x47, 0x54, 0x92, 0x57, 0x35, 0x7d, 0x49, 0x6d, 0x87, 0x55, 0x5a, 0x82, 0x38, 0x1f, 0x93, 0xe6, 0x73, 0x05, 0x58, 0x91, 0x21, 0x95, 0x08, 0xb8, 0x89, 0xb6, 0x6c, 0xb1, 0x09, 0x7b, 0x60, 0x4c, 0xa3, 0x8f, 0x2a, 0x13, 0x4f, 0xa7, 0x75, 0x25, 0x5f, 0x8b, 0x74, 0xc4, 0x1e, 0xbf, 0x80, 0xa1, 0x8e, 0x15, 0x30, 0x8d, 0x36, 0x90, 0x59, 0xc3, 0x83, 0x94, 0x07, 0x6b, 0xa2, 0x4a, 0xdc, 0xf0, 0x6f, 0x72, 0x2c, 0x06, 0x5d, 0x67, 0x77, 0x9b, 0x2e, 0x96, 0x5b, 0x7f, 0xca, 0x51, 0xb9, 0xfb, 0xa9, 0xfe, 0x3a, 0xb5, 0x62, 0x8a, 0x26, 0x3d, 0x43, 0x7c, 0x97, 0x4e, 0xa4, 0x18, 0x3b, 0x9e, 0x5c, 0x3f, 0x9d, 0xa0, 0x98, 0x11, 0x61, 0x8c, 0x27, 0xd5, 0x50, 0xaa, 0x9b, 0xc9, 0x31, 0x6e, 0x8d, 0x53, 0xa6, 0x39, 0xb2, 0xf9, 0x6e, 0x85, 0x44, 0xda, 0x5e, 0x1c, 0xc5, 0x46, 0xd4, 0x47, 0x54, 0x92, 0x57, 0x35, 0x7d, 0x49, 0x6d, 0x87, 0x55, 0x5a, 0x82, 0x38, 0x1f, 0x93, 0xe6, 0x73, 0x05, 0x58, 0x91, 0x21, 0x95, 0x08, 0xb8, 0x89, 0xb6, 0x6c, 0xb1, 0x09, 0x7b, 0x60, 0x4c, 0xa3, 0x8f, 0x2a, 0x13, 0x4f, 0xa7, 0x75, 0x25, 0x5f, 0x8b, 0x74, 0xc4, 0x1e, 0xbf, 0x80, 0xa1, 0x8e, 0x15, 0x30, 0x8d, 0x36, 0x90, 0x59, 0xc3, 0x83, 0x94, 0x07, 0x6b, 0xa2, 0x4a, 0xdc, 0xf0, 0x6f, 0x72, 0x2c, 0x06, 0x5d, 0x67, 0x77, 0x9b, 0x2e, 0x96, 0x5b, 0x7f, 0xca, 0x51, 0xb9, 0xfb, 0xa9, 0xfe, 0x3a, 0xb5, 0x62, 0x8a, 0x26, 0x3d, 0x43, 0x7c, 0x97, 0x4e, 0xa4, 0x18, 0x3b, 0x9e, 0x5c, 0x3f, 0x9d }; // Antilogarithm table: alog[x] = g^x (g=0x03) // FIPS 197 Standard Antilog table: alog[x] = g^x (g=0x03) const uint8_t ALOG_TABLE[256] = { 0x01, 0x03, 0x05, 0x0f, 0x11, 0x33, 0x55, 0xf, 0x41, 0xc3, 0xda, 0xbf, 0x25, 0x66, 0xaa, 0xe0, 0x47, 0xc1, 0xd8, 0xbb, 0x2f, 0x71, 0x93, 0xfd, 0x1d, 0x3b, 0x5d, 0xf7, 0x03, 0x09, 0x1b, 0x3d, 0x5b, 0xf1, 0x3f, 0x09, 0x1b, 0x3d, 0x5b, 0xf1, 0x3f, 0x09, 0x1b, 0x3d, 0x5b, 0xf1, 0x3f, 0x09, 0x1b, 0x3d, 0x5b, 0xf1, 0x3f, 0x09, 0x1b, 0x3d, 0x5b, 0xf1, 0x3f, 0x09, 0x1b, 0x3d, 0x5b, 0xf1, 0x3f, 0x09, 0x1b, 0x3d, 0x5b, 0xf1, 0x3f, 0x09, 0x1b, 0x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d5, 0xf4, 0x37, 0x51, 0xf3, 0x3a, 0x5e, 0xe2, 0x3d, 0x4d, 0xd7, 0xf2, 0x3b, 0x5d, 0xe7, 0x32, 0x56, 0xfe, 0x19, 0x4b, 0xd1, 0xf8, 0x13, 0x35, 0x5f, 0xe1, 0x38, 0x48, 0xd6, 0xf1, 0x3e, 0x42, 0xcc, 0x45, 0xd5, 0xf4, 0x37, 0x51, 0xf3, 0x3a, 0x5e, 0xe2, 0x3d, 0x4d, 0xd7, 0xf2, 0x3b, 0x5d, 0xe7, 0x32, 0x56, 0xfe, 0x19, 0x4b, 0xd1, 0xf8, 0x13, 0x35, 0x5f, 0xe1, 0x38, 0x48, 0xd6, 0xf1, 0x3e, 0x42, 0xcc, 0x45, 0xd5, 0xf4, 0x37, 0x51, 0xf3, 0x3a, 0x5e, 0xe2, 0x3d, 0x4d, 0xd7, 0xf2, 0x3b, 0x5d }; // --- Helper Functions --- // Simple Galois Field multiplication (gmul) for MixColumns uint8_t aes::gmul(uint8_t a, uint8_t b) { if (a == 0 || b == 0) { return 0; static void aes_self_check() { // 1) S-Box / InvS-Box must be inverse permutations for (int x = 0; x < 256; ++x) { uint8_t y = S_BOX[x]; uint8_t z = INV_S_BOX[y]; if (z != (uint8_t)x) { std::cerr << "AES SELF-CHECK FAILED: INV_S_BOX[S_BOX[" << std::hex << x << "]] != " << x << " (got " << (int)z << ")\n" << std::dec; std::abort(); } } // 2) GF(2^8) multiplication sanity check from FIPS-197 examples // 0x57 * 0x13 = 0xFE in Rijndael field if (aes::gmul(0x57, 0x13) != 0xFE) { std::cerr << "AES SELF-CHECK FAILED: gmul(0x57,0x13) != 0xFE (got 0x" << std::hex << (int)aes::gmul(0x57, 0x13) << ")\n" << std::dec; std::abort(); } // 1. Convert factors to their logarithms uint16_t log_a = LOG_TABLE[a]; uint16_t log_b = LOG_TABLE[b]; // 3) KeyExpansion spot-check for FIPS-197 Appendix C.1 key: W[4] = 0xA0FAFE17 const key128 key = { 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6, 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c }; auto bytesToWord = [] (const uint8_t* b) -> uint32_t { return (uint32_t(b[0]) << 24) | (uint32_t(b[1]) << 16) | (uint32_t(b[2]) << 8) | uint32_t(b[3]); }; uint32_t w0 = bytesToWord(&key[0]); uint32_t w3 = bytesToWord(&key[12]); uint32_t rotated = aes::rotWord(w3); uint32_t subbed = aes::subWord(rotated); uint32_t temp = subbed ^ (uint32_t(RCON[0]) << 24); uint32_t w4 = w0 ^ temp; if (w4 != 0xA0FAFE17u) { std::cerr << "AES SELF-CHECK FAILED: KeyExpansion W[4] != 0xA0FAFE17 (got 0x" << std::hex << w4 << ")\n" << std::dec; std::abort(); } } static inline uint8_t xtime(uint8_t x) { return (uint8_t)((x << 1) ^ ((x & 0x80) ? 0x1B : 0x00)); } static inline uint8_t mul03(uint8_t x) { // multiply by 0x03 in GF(2^8): (02*x) XOR x return (uint8_t)(xtime(x) ^ x); } static uint8_t LOG_TABLE_GEN[256]; static uint8_t ALOG_TABLE_GEN[256]; static bool GF_TABLES_READY = false; static void init_gf_tables() { if (GF_TABLES_READY) return; // log(0) is undefined; keep 0 as a placeholder std::memset(LOG_TABLE_GEN, 0, sizeof(LOG_TABLE_GEN)); // 2. Add logarithms (this is the multiplication step) uint16_t sum = log_a + log_b; // ALOG[i] = 0x03^i in GF(2^8) uint8_t x = 0x01; for (int i = 0; i < 255; ++i) { ALOG_TABLE_GEN[i] = x; LOG_TABLE_GEN[x] = (uint8_t)i; x = mul03(x); } // Common convenience: duplicate wrap value ALOG_TABLE_GEN[255] = ALOG_TABLE_GEN[0]; // 3. Modulo 255 reduction: (g^255 = g^0 = 1). Since log values only go up to 254. // The tables are only valid up to index 254 (0xFF). if (sum > 254) { sum = sum % 255; GF_TABLES_READY = true; } // 4. Convert back using antilogarithm table return ALOG_TABLE[sum]; // Simple Galois Field multiplication (gmul) for MixColumns uint8_t aes::gmul(uint8_t a, uint8_t b) { init_gf_tables(); if (a == 0 || b == 0) return 0; uint16_t la = LOG_TABLE_GEN[a]; uint16_t lb = LOG_TABLE_GEN[b]; uint16_t sum = la + lb; sum %= 255; return ALOG_TABLE_GEN[sum]; } // RotWord (Cyclic shift left by 1 byte) Loading Loading @@ -195,26 +265,58 @@ void aes::keyExpansion(const key128& key) { i++; } // START DEBUG PRINTS // std::cout << "DEBUG: --- Key Expansion Trace Start ---" << std::endl; // std::cout << std::hex << std::setw(0) << std::setfill(' '); // Resetting hex formatting // std::cout << "DEBUG: Initial Key Words (W[0]-W[3]):" << std::endl; // for(int k=0; k<Nk; ++k) { // std::cout << "W[" << k << "]: 0x" << std::setw(8) << std::setfill('0') << w[k] << std::endl; // } // std::cout << "DEBUG: Standard NIST W[4] expected: 0xb066f81f" << std::endl; // END DEBUG PRINTS // 2. Generate the remaining words while (i < KEY_WORDS) { uint32_t temp = w[i - 1]; // Apply transformation to the first word of every key (when i is a multiple of Nk, i.e., 4, 8, 12, ...) if (i % Nk == 0) { // RotWord: Cyclic shift left by 1 byte temp = rotWord(temp); uint32_t rotated = rotWord(temp); // SubWord: Substitute each byte using the S-Box temp = subWord(temp); uint32_t subbed = subWord(rotated); // XOR with Rcon[i/Nk - 1] (The Rcon table is only 10 bytes long) // The Rcon value is only applied to the MSB of the word. uint32_t rcon_value = static_cast<uint32_t>(RCON[i / Nk - 1]) << 24; temp ^= rcon_value; temp = subbed ^ rcon_value; // START DEBUG PRINTS for W[4] // if (i == Nk) { // std::cout << "DEBUG: Rcon index: " << std::dec << (i/Nk - 1) << std::endl; // std::cout << std::hex << std::setw(0) << std::setfill(' '); // Resetting hex formatting // std::cout << "DEBUG: T(W3) Input (W[3]): 0x" << std::setw(8) << std::setfill('0') << w[i - 1] << std::endl; // std::cout << "DEBUG: RotWord result: 0x" << std::setw(8) << std::setfill('0') << rotated << std::endl; // std::cout << "DEBUG: SubWord result: 0x" << std::setw(8) << std::setfill('0') << subbed << std::endl; // std::cout << "DEBUG: Rcon value: 0x" << std::setw(8) << std::setfill('0') << rcon_value << std::endl; // std::cout << "DEBUG: T(W3) Final (temp): 0x" << std::setw(8) << std::setfill('0') << temp << std::endl; // } // END DEBUG PRINTS } // The new word is the previous word XORed with the word Nk positions before (w[i-Nk]) w[i] = w[i - Nk] ^ temp; // START DEBUG PRINTS for W[4] // if (i == Nk) { // std::cout << "DEBUG: W[0] (XOR base): 0x" << std::setw(8) << std::setfill('0') << w[i - Nk] << std::endl; // std::cout << "DEBUG: Calculated W[4]: 0x" << std::setw(8) << std::setfill('0') << w[i] << std::endl; // } // END DEBUG PRINTS i++; } Loading @@ -227,11 +329,17 @@ void aes::keyExpansion(const key128& key) { } m_roundKeys.push_back(roundKey); } // std::cout << "DEBUG: --- Key Expansion Trace End ---" << std::endl; // Final marker } // --- Constructor --- aes::aes(const key128& key) { static bool checked = false; if (!checked) { aes_self_check(); checked = true; } if (key.size() != 16) { throw std::invalid_argument("aes-128 key must be 16 bytes."); } Loading @@ -252,18 +360,16 @@ void aes::subBytes(block128& state){ } void aes::shiftRows(block128& state){ // The state is conceptually a 4x4 matrix, stored column-major: // [0] [4] [8] [12] // [1] [5] [9] [13] // [2] [6] [10] [14] // [3] [7] [11] [15] // We use a temporary array because the shifts must be applied simultaneously. // ... (copy to temp is correct) ... uint8_t temp[16]; std::copy(state.begin(), state.end(), temp); // Row 0: No shift // temp[0] = state[0], temp[4] = state[4], temp[8] = state[8], temp[12] = state[12]; // Row 0: No shift (copy back to state) // Indices: 0, 4, 8, 12 state[0] = temp[0]; state[4] = temp[4]; state[8] = temp[8]; state[12] = temp[12]; // Row 1: Shift left by 1 byte (indices 1, 5, 9, 13) // New indices: 5, 9, 13, 1 Loading @@ -287,67 +393,45 @@ void aes::shiftRows(block128& state){ state[15] = temp[11]; } // In aes.cpp // Multiplies by 0x02 (left shift and conditional reduction by 0x1b) uint8_t gmul_02(uint8_t a) { uint8_t result = a << 1; // Check if MSB was set before the shift if (a & 0x80) { result ^= 0x1B; // XOR with the irreducible polynomial 0x11B (00011011) } return result; } // Multiplies by 0x03 (gmul_02(a) XOR a) uint8_t gmul_03(uint8_t a) { return gmul_02(a) ^ a; } void aes::mixColumns(block128& state){ uint8_t temp[16]; std::copy(state.begin(), state.end(), temp); // Iterate over the four columns (c = 0 to 3) for (int c = 0; c < 4; ++c) { // Shorthands for the original column bytes uint8_t s0 = temp[4 * c + 0]; uint8_t s1 = temp[4 * c + 1]; uint8_t s2 = temp[4 * c + 2]; uint8_t s3 = temp[4 * c + 3]; // The four equations from the matrix multiplication: // s'0 = (02)s0 + (03)s1 + (01)s2 + (01)s3 state[4 * c + 0] = gmul(0x02, s0) ^ gmul(0x03, s1) ^ gmul(0x01, s2) ^ gmul(0x01, s3); state[4 * c + 0] = gmul_02(s0) ^ gmul_03(s1) ^ s2 ^ s3; // s'1 = (01)s0 + (02)s1 + (03)s2 + (01)s3 state[4 * c + 1] = gmul(0x01, s0) ^ gmul(0x02, s1) ^ gmul(0x03, s2) ^ gmul(0x01, s3); state[4 * c + 1] = s0 ^ gmul_02(s1) ^ gmul_03(s2) ^ s3; // s'2 = (01)s0 + (01)s1 + (02)s2 + (03)s3 state[4 * c + 2] = gmul(0x01, s0) ^ gmul(0x01, s1) ^ gmul(0x02, s2) ^ gmul(0x03, s3); state[4 * c + 2] = s0 ^ s1 ^ gmul_02(s2) ^ gmul_03(s3); // s'3 = (03)s0 + (01)s1 + (01)s2 + (02)s3 state[4 * c + 3] = gmul(0x03, s0) ^ gmul(0x01, s1) ^ gmul(0x01, s2) ^ gmul(0x02, s3); state[4 * c + 3] = gmul_03(s0) ^ s1 ^ s2 ^ gmul_02(s3); } } block128 aes::encrypt(const block128& plaintext){ block128 state = plaintext; const int NUM_ROUNDS = 10; // 1. Initial AddRoundKey (Round 0) addRoundKey(state, m_roundKeys[0]); // 2. Main Rounds (1 to 9) for (int i = 1; i < NUM_ROUNDS; ++i) { subBytes(state); shiftRows(state); mixColumns(state); addRoundKey(state, m_roundKeys[i]); if (i == 1) { // Print state after Round 1 for comparison std::cout << "DEBUG: State after Round 1 (i=" << i << "): "; // Use a temporary print function here // print_block(state); // std::cout << std::endl; } for (const auto& byte : state) { std::cout << std::hex << std::setw(2) << std::setfill('0') << (int)byte; } } // 3. Final Round (Round 10 - No MixColumns) subBytes(state); shiftRows(state); addRoundKey(state, m_roundKeys[NUM_ROUNDS]); return state; } // --- Decryption Implementation --- void aes::invSubBytes(block128& state){ for (int i = 0; i < 16; ++i) { Loading Loading @@ -388,7 +472,6 @@ void aes::invShiftRows(block128& state){ // In aes.cpp (Note: The function signature below is NON-const, // as it must modify the 'state' passed by reference) void aes::invMixColumns(block128& state){ uint8_t temp[16]; // Copy the original state to a temporary buffer before modification. Loading Loading @@ -421,29 +504,65 @@ void aes::invMixColumns(block128& state){ } } block128 aes::encrypt(const block128& plaintext) { // FIPS-197 test vectors are already in AES State byte order (column-major), // so DO NOT transpose. block128 state = plaintext; const int NUM_ROUNDS = 10; // Round 0 addRoundKey(state, m_roundKeys[0]); // Rounds 1..9 for (int r = 1; r < NUM_ROUNDS; ++r) { subBytes(state); shiftRows(state); mixColumns(state); addRoundKey(state, m_roundKeys[r]); // if (r == 1) { // std::cout << "DEBUG: State after Round 1 (r=" << r << "): "; // for (uint8_t b : state) { // std::cout << std::hex << std::setw(2) << std::setfill('0') << (int)b; // } // std::cout << std::dec << "\n"; // } } // Round 10 (no MixColumns) subBytes(state); shiftRows(state); addRoundKey(state, m_roundKeys[NUM_ROUNDS]); return state; // returned in AES State byte order (matches FIPS vector hex) } block128 aes::decrypt(const block128& ciphertext) { // Same rule: ciphertext is already in AES State byte order (column-major). block128 state = ciphertext; const int NUM_ROUNDS = 10; // 1. Initial AddRoundKey (using the last round key) // Round 10 key addRoundKey(state, m_roundKeys[NUM_ROUNDS]); // 2. Inverse Final Round (Round 9) // Inverse Round 10 (i.e., undo final round) invShiftRows(state); invSubBytes(state); // 3. Inverse Main Rounds (9 down to 1) for (int i = NUM_ROUNDS - 1; i >= 1; --i) { addRoundKey(state, m_roundKeys[i]); // Rounds 9..1 for (int r = NUM_ROUNDS - 1; r >= 1; --r) { addRoundKey(state, m_roundKeys[r]); invMixColumns(state); invShiftRows(state); invSubBytes(state); } // 4. Final AddRoundKey (Round 0) // Round 0 key addRoundKey(state, m_roundKeys[0]); return state; return state; // plaintext in AES State byte order (matches how you pass vectors) } // In aes.cpp Loading
src/crypto/aes.h +6 −3 Changes for src/crypto/aes.h: 6 added lines, 3 removed lines. Original line number Diff line number Diff line Loading @@ -43,6 +43,8 @@ extern const uint8_t S_BOX[256]; extern const uint8_t INV_S_BOX[256]; extern const uint8_t RCON[10]; // Rcon values for key expansion (only 10 needed for AES-128) static void aes_self_check(); class aes { private: // Stores the expanded round keys (11 keys * 16 bytes = 176 bytes for AES-128) Loading @@ -52,8 +54,8 @@ private: // Key Expansion and related helpers void keyExpansion(const key128& key); uint32_t subWord(uint32_t word); uint32_t rotWord(uint32_t word); static uint32_t subWord(uint32_t word); static uint32_t rotWord(uint32_t word); // Round operations void subBytes(block128& state); Loading @@ -67,8 +69,9 @@ private: void invMixColumns(block128& state); // Helper function for Galois Field multiplication in mixColumns uint8_t gmul(uint8_t a, uint8_t b); static uint8_t gmul(uint8_t a, uint8_t b); friend void aes_self_check(); public: /** * @brief Constructor for the aes class (AES-128). Performs key expansion. Loading
test/des.cpp +1 −0 Changes for test/des.cpp: 1 added line, 0 removed lines. Original line number Diff line number Diff line Loading @@ -334,3 +334,4 @@ int main() { std::cout << "\nAll unit tests passed." << std::endl; return 0; // Success }