'''
*   Author: Adnan Omar Khalaf Hleihel
*   Created at: 19/12/2020
*   Tile: AES implementation in python (Sub_Bytes, Shift_Rows, Mix_Columns, Add_Round_Key)
'''

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


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


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

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

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


def mixcolumns(state) :
    mul1 = bytearray(4)
    mul2 = 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

        state[i]   = mul2[0] ^ mul1[3] ^ mul1[2] ^ mul2[1] ^ mul1[1]
        state[i+1] = mul2[1] ^ mul1[0] ^ mul1[3] ^ mul2[2] ^ mul1[2]
        state[i+2] = mul2[2] ^ mul1[1] ^ mul1[0] ^ mul2[3] ^ mul1[3]
        state[i+3] = mul2[3] ^ mul1[2] ^ mul1[1] ^ mul2[0] ^ mul1[0]


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


def encrypt_one_state(state, key, round_key) :
#   initial round
    addroundkey(state, key)

#   9 main rounds
    for i in range(9) :
        subbytes(state, 16)
        shiftrows(state)
        mixcolumns(state)
        addroundkey(state, round_key[i])

#   final round
    subbytes(state, 16)
    shiftrows(state)
    addroundkey(state, round_key[9])
