void AesGenTables(void) { unsigned i; for (i = 0; i < 256; i++) InvS[Sbox[i]] = (Byte)i; for (i = 0; i < 256; i++) { { UInt32 a1 = Sbox[i]; UInt32 a2 = xtime(a1); UInt32 a3 = xtime(a1) ^ a1; T[ i] = Ui32(a2, a1, a1, a3); T[0x100 + i] = Ui32(a3, a2, a1, a1); T[0x200 + i] = Ui32(a1, a3, a2, a1); T[0x300 + i] = Ui32(a1, a1, a3, a2); } { UInt32 a1 = InvS[i]; UInt32 a2 = xtime(a1); UInt32 a4 = xtime(a2); UInt32 a8 = xtime(a4); UInt32 a9 = a8 ^ a1; UInt32 aB = a8 ^ a2 ^ a1; UInt32 aD = a8 ^ a4 ^ a1; UInt32 aE = a8 ^ a4 ^ a2; D[ i] = Ui32(aE, a9, aD, aB); D[0x100 + i] = Ui32(aB, aE, a9, aD); D[0x200 + i] = Ui32(aD, aB, aE, a9); D[0x300 + i] = Ui32(a9, aD, aB, aE); } } }
void MY_FAST_CALL Aes_SetKey_Enc(UInt32 *w, const Byte *key, unsigned keySize) { unsigned i, wSize; wSize = keySize + 28; keySize /= 4; w[0] = ((UInt32)keySize / 2) + 3; w += 4; for (i = 0; i < keySize; i++, key += 4) w[i] = GetUi32(key); for (; i < wSize; i++) { UInt32 t = w[i - 1]; unsigned rem = i % keySize; if (rem == 0) t = Ui32(Sbox[gb1(t)] ^ Rcon[i / keySize], Sbox[gb2(t)], Sbox[gb3(t)], Sbox[gb0(t)]); else if (keySize > 6 && rem == 4) t = Ui32(Sbox[gb0(t)], Sbox[gb1(t)], Sbox[gb2(t)], Sbox[gb3(t)]); w[i] = w[i - keySize] ^ t; } }
void Aes_SetKeyEncode(CAes *p, const Byte *key, unsigned keySize) { unsigned i, wSize; UInt32 *w; keySize /= 4; p->numRounds2 = keySize / 2 + 3; wSize = (p->numRounds2 * 2 + 1) * 4; w = p->rkey; for (i = 0; i < keySize; i++, key += 4) w[i] = Ui32(key[0], key[1], key[2], key[3]); for (; i < wSize; i++) { UInt32 t = w[i - 1]; unsigned rem = i % keySize; if (rem == 0) t = Ui32(Sbox[gb1(t)] ^ Rcon[i / keySize], Sbox[gb2(t)], Sbox[gb3(t)], Sbox[gb0(t)]); else if (keySize > 6 && rem == 4) t = Ui32(Sbox[gb0(t)], Sbox[gb1(t)], Sbox[gb2(t)], Sbox[gb3(t)]); w[i] = w[i - keySize] ^ t; } }
void CAesAlg::_SetKeyEncode(AesCTX &aes, const Byte *key, unsigned keySize) { unsigned i, wSize; UInt32 *w; keySize /= 4; aes.numRounds2 = keySize / 2 + 3; wSize = (aes.numRounds2 * 2 + 1) * 4; w = aes.rkey; for (i = 0; i < keySize; i++, key += 4) w[i] = Ui32(key[0], key[1], key[2], key[3]); for (; i < wSize; i++) { UInt32 t = w[i - 1]; unsigned rem = i % keySize; if (rem == 0) t = Ui32(m_Sbox[gb1(t)] ^ m_Rcon[i / keySize], m_Sbox[gb2(t)], m_Sbox[gb3(t)], m_Sbox[gb0(t)]); else if (keySize > 6 && rem == 4) t = Ui32(m_Sbox[gb0(t)], m_Sbox[gb1(t)], m_Sbox[gb2(t)], m_Sbox[gb3(t)]); w[i] = w[i - keySize] ^ t; } }
void AesGenTables(void) { unsigned i; for (i = 0; i < 256; i++) InvS[Sbox[i]] = (Byte)i; for (i = 0; i < 256; i++) { { UInt32 a1 = Sbox[i]; UInt32 a2 = xtime(a1); UInt32 a3 = a2 ^ a1; T[ i] = Ui32(a2, a1, a1, a3); T[0x100 + i] = Ui32(a3, a2, a1, a1); T[0x200 + i] = Ui32(a1, a3, a2, a1); T[0x300 + i] = Ui32(a1, a1, a3, a2); } { UInt32 a1 = InvS[i]; UInt32 a2 = xtime(a1); UInt32 a4 = xtime(a2); UInt32 a8 = xtime(a4); UInt32 a9 = a8 ^ a1; UInt32 aB = a8 ^ a2 ^ a1; UInt32 aD = a8 ^ a4 ^ a1; UInt32 aE = a8 ^ a4 ^ a2; D[ i] = Ui32(aE, a9, aD, aB); D[0x100 + i] = Ui32(aB, aE, a9, aD); D[0x200 + i] = Ui32(aD, aB, aE, a9); D[0x300 + i] = Ui32(a9, aD, aB, aE); } } g_AesCbc_Encode = AesCbc_Encode; g_AesCbc_Decode = AesCbc_Decode; g_AesCtr_Code = AesCtr_Code; #ifdef MY_CPU_X86_OR_AMD64 #ifdef P7ZIP_USE_ASM if (CPU_Is_Aes_Supported()) { g_AesCbc_Encode = AesCbc_Encode_Intel; g_AesCbc_Decode = AesCbc_Decode_Intel; g_AesCtr_Code = AesCtr_Code_Intel; } #endif #endif }