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 MY_FAST_CALL Aes_SetKey_Dec(UInt32 *w, const Byte *key, unsigned keySize) { unsigned i, num; Aes_SetKey_Enc(w, key, keySize); num = keySize + 20; w += 8; for (i = 0; i < num; i++) { UInt32 r = w[i]; w[i] = D[ Sbox[gb0(r)]] ^ D[0x100 + Sbox[gb1(r)]] ^ D[0x200 + Sbox[gb2(r)]] ^ D[0x300 + Sbox[gb3(r)]]; } }
void Aes_SetKeyDecode(CAes *p, const Byte *key, unsigned keySize) { unsigned i, num; UInt32 *w; Aes_SetKeyEncode(p, key, keySize); num = p->numRounds2 * 8 - 4; w = p->rkey + 4; for (i = 0; i < num; i++) { UInt32 r = w[i]; w[i] = D[ Sbox[gb0(r)]] ^ D[0x100 + Sbox[gb1(r)]] ^ D[0x200 + Sbox[gb2(r)]] ^ D[0x300 + Sbox[gb3(r)]]; } }
void CAesAlg::_SetKeyDecode(const Byte *key, unsigned keySize) { unsigned i, num; UInt32 *w; _SetKeyEncode(m_AesDecodeCTX, key, keySize); num = m_AesDecodeCTX.numRounds2 * 8 - 4; w = m_AesDecodeCTX.rkey + 4; for (i = 0; i < num; i++) { UInt32 r = w[i]; w[i] = m_D[ m_Sbox[gb0(r)]] ^ m_D[0x100 + m_Sbox[gb1(r)]] ^ m_D[0x200 + m_Sbox[gb2(r)]] ^ m_D[0x300 + m_Sbox[gb3(r)]]; } }
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; } }