package aes

import (
	"bytes"
	"testing"
)

func TestShiftRows(t *testing.T) {
	input := Block{
		0, 1, 2, 3,
		4, 5, 6, 7,
		8, 9, 10, 11,
		12, 13, 14, 15,
	}
	expected := Block{
		0, 1, 2, 3,
		5, 6, 7, 4,
		10, 11, 8, 9,
		15, 12, 13, 14,
	}
	actual := shiftRows(input)
	if !bytes.Equal(expected, actual) {
		t.Errorf(
			"failed to get right ShiftRows:\n\texpected: % x\n\t  actual: % x",
			expected,
			actual,
		)

	}
}

func TestSubBytes(t *testing.T) {
	input := Block{
		0x19, 0xa0, 0x9a, 0xe9,
		0x3d, 0xf4, 0xc6, 0xf8,
		0xe3, 0xe2, 0x8d, 0x48,
		0xbe, 0x2b, 0x2a, 0x08,
	}
	expected := Block{
		0xd4, 0xe0, 0xb8, 0x1e,
		0x27, 0xbf, 0xb4, 0x41,
		0x11, 0x98, 0x5d, 0x52,
		0xae, 0xf1, 0xe5, 0x30,
	}
	actual := subBytes(input)
	if !bytes.Equal(expected, actual) {
		t.Errorf(
			"failed to get right ShiftRows:\n\texpected: % x\n\t  actual: % x",
			expected,
			actual,
		)
	}
}

func TestSplitBytes(t *testing.T) {
	input := byte(0xab)
	expected1 := byte(0xa)
	expected2 := byte(0xb)
	actual1, actual2 := splitBytes(input)
	if expected1 != actual1 || expected2 != actual2 {
		t.Errorf(
			"failed to get SplitBytes:\n\texpected: 0x%x 0x%x\n\t  actual: 0x%x 0x%x",
			expected1,
			expected2,
			actual1,
			actual2,
		)
	}
}

func TestXtime(t *testing.T) {
	input := byte(0x14)
	expected := []byte{
		0x14, 0x28, 0x50, 0xa0,
		0x5b, 0xb6, 0x77, 0xee,
	}
	actual := xtime(input)
	if !bytes.Equal(expected, actual) {
		t.Errorf(
			"failed to get Xtime:\n\texpected: % x\n\tactual: % x",
			expected,
			actual,
		)
	}
}

func TestFFmult(t *testing.T) {
	input := xtime(0x14)
	expected1 := byte(0x14)
	expected2 := byte(0x28)
	expected3 := byte(0x3c)
	actual1 := ffmult(input, 1)
	actual2 := ffmult(input, 2)
	actual3 := ffmult(input, 3)
	if expected1 != actual1 || expected2 != actual2 || expected3 != actual3 {
		t.Errorf(
			"failed to get FFmult:\n\texpected: 0x% x 0x% x 0x% x\n\t actual:0x% x 0x% x 0x% x",
			expected1,
			expected2,
			expected3,
			actual1,
			actual2,
			actual3,
		)
	}
}

func TestMixColumns(t *testing.T) {
	input := Block{
		0xdb, 0xf2, 0x2d, 0x01,
		0x13, 0x0a, 0x26, 0x01,
		0x53, 0x22, 0x31, 0x01,
		0x45, 0x5c, 0x4c, 0x01,
	}
	expected := Block{
		0x8e, 0x9f, 0x4d, 0x01,
		0x4d, 0xdc, 0x7e, 0x01,
		0xa1, 0x58, 0xbd, 0x01,
		0xbc, 0x9d, 0xf8, 0x01,
	}
	actual := mixColumns(input)
	for i := 0; i < 16; i++ {
		if actual[i] != expected[i] {
			t.Errorf(
				"failed to get MixColumn:\n\texpected: %v \n\tactual: %v", expected, actual,
			)
		}
	}
}

func TestCipher(t *testing.T) {
	key := Block{
		0x2b, 0x28, 0xab, 0x09,
		0x7e, 0xae, 0xf7, 0xcf,
		0x15, 0xd2, 0x15, 0x4f,
		0x16, 0xa6, 0x88, 0x3c,
	}
	input := Block{
		0x32, 0x88, 0x31, 0xe0,
		0x43, 0x5a, 0x31, 0x37,
		0xf6, 0x30, 0x98, 0x07,
		0xa8, 0x8d, 0xa2, 0x34,
	}
	expected := Block{
		0x39, 0x02, 0xdc, 0x19,
		0x25, 0xdc, 0x11, 0x6a,
		0x84, 0x09, 0x85, 0x0b,
		0x1d, 0xfb, 0x97, 0x32,
	}
	actual := Cipher(input, 128, key)
	for i := 0; i < 16; i++ {
		if actual[i] != expected[i] {
			t.Errorf(
				"failed to get Cipher:\n\texpected: %v\n\tactual: %v", expected, actual,
			)
		}
	}

	//Test case for 128 from FIPS

	key = Block{
		0x00, 0x04, 0x08, 0x0c,
		0x01, 0x05, 0x09, 0x0d,
		0x02, 0x06, 0x0a, 0x0e,
		0x03, 0x07, 0x0b, 0x0f,
	}
	input = Block{
		0x00, 0x44, 0x88, 0xcc,
		0x11, 0x55, 0x99, 0xdd,
		0x22, 0x66, 0xaa, 0xee,
		0x33, 0x77, 0xbb, 0xff,
	}
	expected = Block{
		0x69, 0x6a, 0xd8, 0x70,
		0xc4, 0x7b, 0xcd, 0xb4,
		0xe0, 0x04, 0xb7, 0xc5,
		0xd8, 0x30, 0x80, 0x5a,
	}
	actual = Cipher(input, 128, key)
	for i := 0; i < 16; i++ {
		if actual[i] != expected[i] {
			t.Errorf(
				"failed to get Cipher:\n\texpected: %v\n\tactual: %v", expected, actual,
			)
		}
	}

	//Test case for 192 from FIPS
	key = Block{
		0x00, 0x04, 0x08, 0x0c, 0x10, 0x14,
		0x01, 0x05, 0x09, 0x0d, 0x11, 0x15,
		0x02, 0x06, 0x0a, 0x0e, 0x12, 0x16,
		0x03, 0x07, 0x0b, 0x0f, 0x13, 0x17,
	}
	input = Block{
		0x00, 0x44, 0x88, 0xcc,
		0x11, 0x55, 0x99, 0xdd,
		0x22, 0x66, 0xaa, 0xee,
		0x33, 0x77, 0xbb, 0xff,
	}
	expected = Block{
		0xdd, 0x86, 0x6e, 0xec,
		0xa9, 0x4c, 0xaf, 0x0d,
		0x7c, 0xdf, 0x70, 0x71,
		0xa4, 0xe0, 0xa0, 0x91,
	}
	actual = Cipher(input, 192, key)
	for i := 0; i < 16; i++ {
		if actual[i] != expected[i] {
			t.Errorf(
				"failed to get Cipher:\n\texpected: %v\n\tactual: %v", expected, actual,
			)
		}
	}

	//Test case for 256 from FIPS
	key = Block{
		0x00, 0x04, 0x08, 0x0c, 0x10, 0x14, 0x18, 0x1c,
		0x01, 0x05, 0x09, 0x0d, 0x11, 0x15, 0x19, 0x1d,
		0x02, 0x06, 0x0a, 0x0e, 0x12, 0x16, 0x1a, 0x1e,
		0x03, 0x07, 0x0b, 0x0f, 0x13, 0x17, 0x1b, 0x1f,
	}
	input = Block{
		0x00, 0x44, 0x88, 0xcc,
		0x11, 0x55, 0x99, 0xdd,
		0x22, 0x66, 0xaa, 0xee,
		0x33, 0x77, 0xbb, 0xff,
	}
	expected = Block{
		0x8e, 0x51, 0xea, 0x4b,
		0xa2, 0x67, 0xfc, 0x49,
		0xb7, 0x45, 0x49, 0x60,
		0xca, 0xbf, 0x90, 0x89,
	}
	actual = Cipher(input, 256, key)
	for i := 0; i < 16; i++ {
		if actual[i] != expected[i] {
			t.Errorf(
				"failed to get Cipher:\n\texpected: %v\n\tactual: %v", expected, actual,
			)
		}
	}
}

func TestInvCipher(t *testing.T) {
	key := Block{
		0x2b, 0x28, 0xab, 0x09,
		0x7e, 0xae, 0xf7, 0xcf,
		0x15, 0xd2, 0x15, 0x4f,
		0x16, 0xa6, 0x88, 0x3c,
	}
	input := Block{
		0x39, 0x02, 0xdc, 0x19,
		0x25, 0xdc, 0x11, 0x6a,
		0x84, 0x09, 0x85, 0x0b,
		0x1d, 0xfb, 0x97, 0x32,
	}
	expected := Block{
		0x32, 0x88, 0x31, 0xe0,
		0x43, 0x5a, 0x31, 0x37,
		0xf6, 0x30, 0x98, 0x07,
		0xa8, 0x8d, 0xa2, 0x34,
	}
	actual := InvCipher(input, 128, key)
	for i := 0; i < 16; i++ {
		if actual[i] != expected[i] {
			t.Errorf(
				"failed to get Cipher:\n\texpected: %v\n\tactual: %v", expected, actual,
			)
		}
	}

	//Test case for 128 from FIPS
	key = Block{
		0x00, 0x04, 0x08, 0x0c,
		0x01, 0x05, 0x09, 0x0d,
		0x02, 0x06, 0x0a, 0x0e,
		0x03, 0x07, 0x0b, 0x0f,
	}
	input = Block{
		0x69, 0x6a, 0xd8, 0x70,
		0xc4, 0x7b, 0xcd, 0xb4,
		0xe0, 0x04, 0xb7, 0xc5,
		0xd8, 0x30, 0x80, 0x5a,
	}
	expected = Block{
		0x00, 0x44, 0x88, 0xcc,
		0x11, 0x55, 0x99, 0xdd,
		0x22, 0x66, 0xaa, 0xee,
		0x33, 0x77, 0xbb, 0xff,
	}
	actual = InvCipher(input, 128, key)
	for i := 0; i < 16; i++ {
		if actual[i] != expected[i] {
			t.Errorf(
				"failed to get Cipher:\n\texpected: %v\n\tactual: %v", expected, actual,
			)
		}
	}

	//Test case for 192 from FIPS
	key = Block{
		0x00, 0x04, 0x08, 0x0c, 0x10, 0x14,
		0x01, 0x05, 0x09, 0x0d, 0x11, 0x15,
		0x02, 0x06, 0x0a, 0x0e, 0x12, 0x16,
		0x03, 0x07, 0x0b, 0x0f, 0x13, 0x17,
	}
	input = Block{
		0xdd, 0x86, 0x6e, 0xec,
		0xa9, 0x4c, 0xaf, 0x0d,
		0x7c, 0xdf, 0x70, 0x71,
		0xa4, 0xe0, 0xa0, 0x91,
	}
	expected = Block{
		0x00, 0x44, 0x88, 0xcc,
		0x11, 0x55, 0x99, 0xdd,
		0x22, 0x66, 0xaa, 0xee,
		0x33, 0x77, 0xbb, 0xff,
	}
	actual = InvCipher(input, 192, key)
	for i := 0; i < 16; i++ {
		if actual[i] != expected[i] {
			t.Errorf(
				"failed to get Cipher:\n\texpected: %v \n\tactual:%v", expected, actual,
			)
		}
	}

	//Test case for 256 from FIPS
	key = Block{
		0x00, 0x04, 0x08, 0x0c, 0x10, 0x14, 0x18, 0x1c,
		0x01, 0x05, 0x09, 0x0d, 0x11, 0x15, 0x19, 0x1d,
		0x02, 0x06, 0x0a, 0x0e, 0x12, 0x16, 0x1a, 0x1e,
		0x03, 0x07, 0x0b, 0x0f, 0x13, 0x17, 0x1b, 0x1f,
	}
	input = Block{
		0x8e, 0x51, 0xea, 0x4b,
		0xa2, 0x67, 0xfc, 0x49,
		0xb7, 0x45, 0x49, 0x60,
		0xca, 0xbf, 0x90, 0x89,
	}
	expected = Block{
		0x00, 0x44, 0x88, 0xcc,
		0x11, 0x55, 0x99, 0xdd,
		0x22, 0x66, 0xaa, 0xee,
		0x33, 0x77, 0xbb, 0xff,
	}
	actual = InvCipher(input, 256, key)
	for i := 0; i < 16; i++ {
		if actual[i] != expected[i] {
			t.Errorf(
				"failed to get Cipher:\n\texpected: %v\n\tactual: %v", expected, actual,
			)
		}
	}
}
