'''
*   Author: Adnan Omar Khalaf Hleihel
*   Created at: 19/12/2020
*   Tile: AES implementation in python (Inverse_Sub_Bytes, Inverse_Shift_Rows, Inverse_Mix_Columns, Add_Round_Key)
'''

invsubtable = [
#     00    10    20    30    40    50    60    70    80    90    A0    B0    C0    D0    E0    F0
	[0x52, 0x7C, 0x54, 0x08, 0x72, 0x6C, 0x90, 0xD0, 0x3A, 0x96, 0x47, 0xFC, 0x1F, 0x60, 0xA0, 0x17],
	[0x09, 0xE3, 0x7B, 0x2E, 0xF8, 0x70, 0xD8, 0x2C, 0x91, 0xAC, 0xF1, 0x56, 0xDD, 0x51, 0xE0, 0x2B],
	[0x6A, 0x39, 0x94, 0xA1, 0xF6, 0x48, 0xAB, 0x1E, 0x11, 0x74, 0x1A, 0x3E, 0xA8, 0x7F, 0x3B, 0x04],
	[0xD5, 0x82, 0x32, 0x66, 0x64, 0x50, 0x00, 0x8F, 0x41, 0x22, 0x71, 0x4B, 0x33, 0xA9, 0x4D, 0x7E],
	[0x30, 0x9B, 0xA6, 0x28, 0x86, 0xFD, 0x8C, 0xCA, 0x4F, 0xE7, 0x1D, 0xC6, 0x88, 0x19, 0xAE, 0xBA],
	[0x36, 0x2F, 0xC2, 0xD9, 0x68, 0xED, 0xBC, 0x3F, 0x67, 0xAD, 0x29, 0xD2, 0x07, 0xB5, 0x2A, 0x77],
	[0xA5, 0xFF, 0x23, 0x24, 0x98, 0xB9, 0xD3, 0x0F, 0xDC, 0x35, 0xC5, 0x79, 0xC7, 0x4A, 0xF5, 0xD6],
	[0x38, 0x87, 0x3D, 0xB2, 0x16, 0xDA, 0x0A, 0x02, 0xEA, 0x85, 0x89, 0x20, 0x31, 0x0D, 0xB0, 0x26],
	[0xBF, 0x34, 0xEE, 0x76, 0xD4, 0x5E, 0xF7, 0xC1, 0x97, 0xE2, 0x6F, 0x9A, 0xB1, 0x2D, 0xC8, 0xE1],
	[0x40, 0x8E, 0x4C, 0x5B, 0xA4, 0x15, 0xE4, 0xAF, 0xF2, 0xF9, 0xB7, 0xDB, 0x12, 0xE5, 0xEB, 0x69],
	[0xA3, 0x43, 0x95, 0xA2, 0x5C, 0x46, 0x58, 0xBD, 0xCF, 0x37, 0x62, 0xC0, 0x10, 0x7A, 0xBB, 0x14],
	[0x9E, 0x44, 0x0B, 0x49, 0xCC, 0x57, 0x05, 0x03, 0xCE, 0xE8, 0x0E, 0xFE, 0x59, 0x9F, 0x3C, 0x63],
	[0x81, 0xC4, 0x42, 0x6D, 0x5D, 0xA7, 0xB8, 0x01, 0xF0, 0x1C, 0xAA, 0x78, 0x27, 0x93, 0x83, 0x55],
	[0xF3, 0xDE, 0xFA, 0x8B, 0x65, 0x8D, 0xB3, 0x13, 0xB4, 0x75, 0x18, 0xCD, 0x80, 0xC9, 0x53, 0x21],
	[0xD7, 0xE9, 0xC3, 0xD1, 0xB6, 0x9D, 0x45, 0x8A, 0xE6, 0xDF, 0xBE, 0x5A, 0xEC, 0x9C, 0x99, 0x0C],
	[0xFB, 0xCB, 0x4E, 0x25, 0x92, 0x84, 0x06, 0x6B, 0x73, 0x6E, 0x1B, 0xF4, 0x5F, 0xEF, 0x61, 0x7D],
	]

def invsubbytes(state, n) :
    col = 0
    row = 0
    for i in range(n) :
        col = (state[i] >> 4) & 0x0f
        row = state[i] & 0x0f
        state[i] = invsubtable[row][col]
        col ^= col
        row ^= row


def invshiftrows(state) :
    tmp = state[13]
    state[13] = state[9]
    state[9] = state[5]
    state[5] = state[1]
    state[1] = tmp

    tmp1 = state[2]
    tmp2 = state[6]
    state[2] = state[10]
    state[6] = state[14]
    state[10] = tmp1
    state[14] = tmp2

    tmp3 = state[3]
    state[3] = state[7]
    state[7] = state[11]
    state[11] = state[15]
    state[15] = tmp3


def invmixcolumns(state) :
    mul1 = bytearray(4)
    mul2 = bytearray(4)
    mul4 = bytearray(4)
    mul8 = bytearray(4)
    mul9 = bytearray(4)
    mulB = bytearray(4)
    mulD = bytearray(4)
    mulE = bytearray(4)

    for i in range(0, 16, 4) :
        for e in range(4) :
            mul1[e] = state[i+e]
            mul2[e] = (state[i+e] << 1) & 0xff
            if (state[i+e] >> 7) == 0x1 :
                mul2[e] ^= 0x1b

            mul4[e] = (mul2[e] << 1) & 0xff
            if (mul2[e] >> 7) == 0x1 :
                mul4[e] ^= 0x1b

            mul8[e] = (mul4[e] << 1) & 0xff
            if (mul4[e] >> 7) == 0x1 :
                mul8[e] ^= 0x1b

            mul9[e] = mul8[e] ^ mul1[e]
            mulB[e] = mul8[e] ^ mul2[e] ^ mul1[e]
            mulD[e] = mul8[e] ^ mul4[e] ^ mul1[e]
            mulE[e] = mul8[e] ^ mul4[e] ^ mul2[e]

        state[i]   = mulE[0] ^ mulB[1] ^ mulD[2] ^ mul9[3]
        state[i+1] = mulE[1] ^ mulB[2] ^ mulD[3] ^ mul9[0]
        state[i+2] = mulE[2] ^ mulB[3] ^ mulD[0] ^ mul9[1]
        state[i+3] = mulE[3] ^ mulB[0] ^ mulD[1] ^ mul9[2]


def addroundkey(state, k) :
    for i in range(0, 16) :
        state[i] ^= k[i]


def print_bytes(arg) :
    for i in range(16) :
        print(hex(arg[i]), end = "\t")
    print("\n")


def decrypt_one_state(state, key, round_key) :
#   initial round
    addroundkey(state, round_key[9])

#   9 main rounds
    for i in range(8, -1, -1) :
        invshiftrows(state)
        invsubbytes(state, 16)
        addroundkey(state, round_key[i])
        invmixcolumns(state)

#   final round
    invsubbytes(state, 16)
    invshiftrows(state)
    addroundkey(state, key)
