local Array = require("lockbox.util.array");
local Bit = require("lockbox.util.bit");

local XOR = Bit.bxor;

local SBOX = {
 [0] = 0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76,
 0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0,
 0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15,
 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, 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, 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4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35,
0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f,
0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04,
0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63,
0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd,
0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d};


local GMUL = function(A, B)
    if(A == 0x01) then return B; end
    if(B == 0x01) then return A; end
    if(A == 0x00) then return 0; end
    if(B == 0x00) then return 0; end

    local LA = LTABLE[A];
    local LB = LTABLE[B];

    local sum = LA + LB;
    if (sum > 0xFF) then sum = sum - 0xFF; end

    return ETABLE[sum];
end

local byteSub = Array.substitute;

local shiftRow = Array.permute;

local mixCol = function(i, mix)
    local out = {};

    local a, b, c, d;

    a = GMUL(i[ 1], mix[ 1]);
    b = GMUL(i[ 2], mix[ 2]);
    c = GMUL(i[ 3], mix[ 3]);
    d = GMUL(i[ 4], mix[ 4]);
    out[ 1] = XOR(XOR(a, b), XOR(c, d));
    a = GMUL(i[ 1], mix[ 5]);
    b = GMUL(i[ 2], mix[ 6]);
    c = GMUL(i[ 3], mix[ 7]);
    d = GMUL(i[ 4], mix[ 8]);
    out[ 2] = XOR(XOR(a, b), XOR(c, d));
    a = GMUL(i[ 1], mix[ 9]);
    b = GMUL(i[ 2], mix[10]);
    c = GMUL(i[ 3], mix[11]);
    d = GMUL(i[ 4], mix[12]);
    out[ 3] = XOR(XOR(a, b), XOR(c, d));
    a = GMUL(i[ 1], mix[13]);
    b = GMUL(i[ 2], mix[14]);
    c = GMUL(i[ 3], mix[15]);
    d = GMUL(i[ 4], mix[16]);
    out[ 4] = XOR(XOR(a, b), XOR(c, d));


    a = GMUL(i[ 5], mix[ 1]);
    b = GMUL(i[ 6], mix[ 2]);
    c = GMUL(i[ 7], mix[ 3]);
    d = GMUL(i[ 8], mix[ 4]);
    out[ 5] = XOR(XOR(a, b), XOR(c, d));
    a = GMUL(i[ 5], mix[ 5]);
    b = GMUL(i[ 6], mix[ 6]);
    c = GMUL(i[ 7], mix[ 7]);
    d = GMUL(i[ 8], mix[ 8]);
    out[ 6] = XOR(XOR(a, b), XOR(c, d));
    a = GMUL(i[ 5], mix[ 9]);
    b = GMUL(i[ 6], mix[10]);
    c = GMUL(i[ 7], mix[11]);
    d = GMUL(i[ 8], mix[12]);
    out[ 7] = XOR(XOR(a, b), XOR(c, d));
    a = GMUL(i[ 5], mix[13]);
    b = GMUL(i[ 6], mix[14]);
    c = GMUL(i[ 7], mix[15]);
    d = GMUL(i[ 8], mix[16]);
    out[ 8] = XOR(XOR(a, b), XOR(c, d));


    a = GMUL(i[ 9], mix[ 1]);
    b = GMUL(i[10], mix[ 2]);
    c = GMUL(i[11], mix[ 3]);
    d = GMUL(i[12], mix[ 4]);
    out[ 9] = XOR(XOR(a, b), XOR(c, d));
    a = GMUL(i[ 9], mix[ 5]);
    b = GMUL(i[10], mix[ 6]);
    c = GMUL(i[11], mix[ 7]);
    d = GMUL(i[12], mix[ 8]);
    out[10] = XOR(XOR(a, b), XOR(c, d));
    a = GMUL(i[ 9], mix[ 9]);
    b = GMUL(i[10], mix[10]);
    c = GMUL(i[11], mix[11]);
    d = GMUL(i[12], mix[12]);
    out[11] = XOR(XOR(a, b), XOR(c, d));
    a = GMUL(i[ 9], mix[13]);
    b = GMUL(i[10], mix[14]);
    c = GMUL(i[11], mix[15]);
    d = GMUL(i[12], mix[16]);
    out[12] = XOR(XOR(a, b), XOR(c, d));


    a = GMUL(i[13], mix[ 1]);
    b = GMUL(i[14], mix[ 2]);
    c = GMUL(i[15], mix[ 3]);
    d = GMUL(i[16], mix[ 4]);
    out[13] = XOR(XOR(a, b), XOR(c, d));
    a = GMUL(i[13], mix[ 5]);
    b = GMUL(i[14], mix[ 6]);
    c = GMUL(i[15], mix[ 7]);
    d = GMUL(i[16], mix[ 8]);
    out[14] = XOR(XOR(a, b), XOR(c, d));
    a = GMUL(i[13], mix[ 9]);
    b = GMUL(i[14], mix[10]);
    c = GMUL(i[15], mix[11]);
    d = GMUL(i[16], mix[12]);
    out[15] = XOR(XOR(a, b), XOR(c, d));
    a = GMUL(i[13], mix[13]);
    b = GMUL(i[14], mix[14]);
    c = GMUL(i[15], mix[15]);
    d = GMUL(i[16], mix[16]);
    out[16] = XOR(XOR(a, b), XOR(c, d));

    return out;
end

local keyRound = function(key, round)
    local i = (round - 1) * 32;
    local out = key;

    out[33 + i] = XOR(key[ 1 + i], XOR(SBOX[key[30 + i]], RCON[round]));
    out[34 + i] = XOR(key[ 2 + i], SBOX[key[31 + i]]);
    out[35 + i] = XOR(key[ 3 + i], SBOX[key[32 + i]]);
    out[36 + i] = XOR(key[ 4 + i], SBOX[key[29 + i]]);

    out[37 + i] = XOR(out[33 + i], key[ 5 + i]);
    out[38 + i] = XOR(out[34 + i], key[ 6 + i]);
    out[39 + i] = XOR(out[35 + i], key[ 7 + i]);
    out[40 + i] = XOR(out[36 + i], key[ 8 + i]);

    out[41 + i] = XOR(out[37 + i], key[ 9 + i]);
    out[42 + i] = XOR(out[38 + i], key[10 + i]);
    out[43 + i] = XOR(out[39 + i], key[11 + i]);
    out[44 + i] = XOR(out[40 + i], key[12 + i]);

    out[45 + i] = XOR(out[41 + i], key[13 + i]);
    out[46 + i] = XOR(out[42 + i], key[14 + i]);
    out[47 + i] = XOR(out[43 + i], key[15 + i]);
    out[48 + i] = XOR(out[44 + i], key[16 + i]);


    out[49 + i] = XOR(SBOX[out[45 + i]], key[17 + i]);
    out[50 + i] = XOR(SBOX[out[46 + i]], key[18 + i]);
    out[51 + i] = XOR(SBOX[out[47 + i]], key[19 + i]);
    out[52 + i] = XOR(SBOX[out[48 + i]], key[20 + i]);

    out[53 + i] = XOR(out[49 + i], key[21 + i]);
    out[54 + i] = XOR(out[50 + i], key[22 + i]);
    out[55 + i] = XOR(out[51 + i], key[23 + i]);
    out[56 + i] = XOR(out[52 + i], key[24 + i]);

    out[57 + i] = XOR(out[53 + i], key[25 + i]);
    out[58 + i] = XOR(out[54 + i], key[26 + i]);
    out[59 + i] = XOR(out[55 + i], key[27 + i]);
    out[60 + i] = XOR(out[56 + i], key[28 + i]);

    out[61 + i] = XOR(out[57 + i], key[29 + i]);
    out[62 + i] = XOR(out[58 + i], key[30 + i]);
    out[63 + i] = XOR(out[59 + i], key[31 + i]);
    out[64 + i] = XOR(out[60 + i], key[32 + i]);

    return out;
end

local keyExpand = function(key)
    local bytes = Array.copy(key);

    for i = 1, 7 do
        keyRound(bytes, i);
    end

    local keys = {};

    keys[ 1] = Array.slice(bytes, 1, 16);
    keys[ 2] = Array.slice(bytes, 17, 32);
    keys[ 3] = Array.slice(bytes, 33, 48);
    keys[ 4] = Array.slice(bytes, 49, 64);
    keys[ 5] = Array.slice(bytes, 65, 80);
    keys[ 6] = Array.slice(bytes, 81, 96);
    keys[ 7] = Array.slice(bytes, 97, 112);
    keys[ 8] = Array.slice(bytes, 113, 128);
    keys[ 9] = Array.slice(bytes, 129, 144);
    keys[10] = Array.slice(bytes, 145, 160);
    keys[11] = Array.slice(bytes, 161, 176);
    keys[12] = Array.slice(bytes, 177, 192);
    keys[13] = Array.slice(bytes, 193, 208);
    keys[14] = Array.slice(bytes, 209, 224);
    keys[15] = Array.slice(bytes, 225, 240);

    return keys;

end

local addKey = Array.XOR;



local AES = {};

AES.blockSize = 16;

AES.encrypt = function(key, block)

    local keySchedule = keyExpand(key);

    --round 0
    block = addKey(block, keySchedule[1]);

    --round 1
    block = byteSub(block, SBOX);
    block = shiftRow(block, ROW_SHIFT);
    block = mixCol(block, MIXTABLE);
    block = addKey(block, keySchedule[2]);

    --round 2
    block = byteSub(block, SBOX);
    block = shiftRow(block, ROW_SHIFT);
    block = mixCol(block, MIXTABLE);
    block = addKey(block, keySchedule[3]);

    --round 3
    block = byteSub(block, SBOX);
    block = shiftRow(block, ROW_SHIFT);
    block = mixCol(block, MIXTABLE);
    block = addKey(block, keySchedule[4]);

    --round 4
    block = byteSub(block, SBOX);
    block = shiftRow(block, ROW_SHIFT);
    block = mixCol(block, MIXTABLE);
    block = addKey(block, keySchedule[5]);

    --round 5
    block = byteSub(block, SBOX);
    block = shiftRow(block, ROW_SHIFT);
    block = mixCol(block, MIXTABLE);
    block = addKey(block, keySchedule[6]);

    --round 6
    block = byteSub(block, SBOX);
    block = shiftRow(block, ROW_SHIFT);
    block = mixCol(block, MIXTABLE);
    block = addKey(block, keySchedule[7]);

    --round 7
    block = byteSub(block, SBOX);
    block = shiftRow(block, ROW_SHIFT);
    block = mixCol(block, MIXTABLE);
    block = addKey(block, keySchedule[8]);

    --round 8
    block = byteSub(block, SBOX);
    block = shiftRow(block, ROW_SHIFT);
    block = mixCol(block, MIXTABLE);
    block = addKey(block, keySchedule[9]);

    --round 9
    block = byteSub(block, SBOX);
    block = shiftRow(block, ROW_SHIFT);
    block = mixCol(block, MIXTABLE);
    block = addKey(block, keySchedule[10]);

    --round 10
    block = byteSub(block, SBOX);
    block = shiftRow(block, ROW_SHIFT);
    block = mixCol(block, MIXTABLE);
    block = addKey(block, keySchedule[11]);

    --round 11
    block = byteSub(block, SBOX);
    block = shiftRow(block, ROW_SHIFT);
    block = mixCol(block, MIXTABLE);
    block = addKey(block, keySchedule[12]);

    --round 12
    block = byteSub(block, SBOX);
    block = shiftRow(block, ROW_SHIFT);
    block = mixCol(block, MIXTABLE);
    block = addKey(block, keySchedule[13]);

    --round 13
    block = byteSub(block, SBOX);
    block = shiftRow(block, ROW_SHIFT);
    block = mixCol(block, MIXTABLE);
    block = addKey(block, keySchedule[14]);

    --round 14
    block = byteSub(block, SBOX);
    block = shiftRow(block, ROW_SHIFT);
    block = addKey(block, keySchedule[15]);

    return block;

end

AES.decrypt = function(key, block)

    local keySchedule = keyExpand(key);

    --round 0
    block = addKey(block, keySchedule[15]);

    --round 1
    block = shiftRow(block, IROW_SHIFT);
    block = byteSub(block, ISBOX);
    block = addKey(block, keySchedule[14]);
    block = mixCol(block, IMIXTABLE);

    --round 2
    block = shiftRow(block, IROW_SHIFT);
    block = byteSub(block, ISBOX);
    block = addKey(block, keySchedule[13]);
    block = mixCol(block, IMIXTABLE);

    --round 3
    block = shiftRow(block, IROW_SHIFT);
    block = byteSub(block, ISBOX);
    block = addKey(block, keySchedule[12]);
    block = mixCol(block, IMIXTABLE);

    --round 4
    block = shiftRow(block, IROW_SHIFT);
    block = byteSub(block, ISBOX);
    block = addKey(block, keySchedule[11]);
    block = mixCol(block, IMIXTABLE);

    --round 5
    block = shiftRow(block, IROW_SHIFT);
    block = byteSub(block, ISBOX);
    block = addKey(block, keySchedule[10]);
    block = mixCol(block, IMIXTABLE);

    --round 6
    block = shiftRow(block, IROW_SHIFT);
    block = byteSub(block, ISBOX);
    block = addKey(block, keySchedule[9]);
    block = mixCol(block, IMIXTABLE);

    --round 7
    block = shiftRow(block, IROW_SHIFT);
    block = byteSub(block, ISBOX);
    block = addKey(block, keySchedule[8]);
    block = mixCol(block, IMIXTABLE);

    --round 8
    block = shiftRow(block, IROW_SHIFT);
    block = byteSub(block, ISBOX);
    block = addKey(block, keySchedule[7]);
    block = mixCol(block, IMIXTABLE);

    --round 9
    block = shiftRow(block, IROW_SHIFT);
    block = byteSub(block, ISBOX);
    block = addKey(block, keySchedule[6]);
    block = mixCol(block, IMIXTABLE);

    --round 10
    block = shiftRow(block, IROW_SHIFT);
    block = byteSub(block, ISBOX);
    block = addKey(block, keySchedule[5]);
    block = mixCol(block, IMIXTABLE);

    --round 11
    block = shiftRow(block, IROW_SHIFT);
    block = byteSub(block, ISBOX);
    block = addKey(block, keySchedule[4]);
    block = mixCol(block, IMIXTABLE);

    --round 12
    block = shiftRow(block, IROW_SHIFT);
    block = byteSub(block, ISBOX);
    block = addKey(block, keySchedule[3]);
    block = mixCol(block, IMIXTABLE);

    --round 13
    block = shiftRow(block, IROW_SHIFT);
    block = byteSub(block, ISBOX);
    block = addKey(block, keySchedule[2]);
    block = mixCol(block, IMIXTABLE);

    --round 14
    block = shiftRow(block, IROW_SHIFT);
    block = byteSub(block, ISBOX);
    block = addKey(block, keySchedule[1]);

    return block;
end

return AES;
