#include "pch.h"
#include <microratchet.h>
#include <internal.h>
#include "support.h"

static mr_config _cfg{ true };

TEST(AesCtr, Init256) {
	const uint8_t key[]{
		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
		0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
		0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
	};
	const uint8_t iv[]{
		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
		0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
	};

	auto mr_ctx = mr_ctx_create(&_cfg);
	auto aes = mr_aes_create(mr_ctx);
	_mr_aesctr_ctx aesctr;
	EXPECT_NE(nullptr, aes);
	int result = mr_aes_init(aes, key, SIZEOF(key));
	EXPECT_EQ(MR_E_SUCCESS, result);
	result = aesctr_init(&aesctr, aes, iv, SIZEOF(iv));
	EXPECT_EQ(MR_E_SUCCESS, result);

	mr_aes_destroy(aes);
	mr_ctx_destroy(mr_ctx);
}

TEST(AesCtr, Init128) {
	const uint8_t key[]{
		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
		0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
	};
	const uint8_t iv[]{
		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
		0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
	};

	auto mr_ctx = mr_ctx_create(&_cfg);
	auto aes = mr_aes_create(mr_ctx);
	_mr_aesctr_ctx aesctr;
	EXPECT_NE(nullptr, aes);
	int result = mr_aes_init(aes, key, SIZEOF(key));
	EXPECT_EQ(MR_E_SUCCESS, result);
	result = aesctr_init(&aesctr, aes, iv, SIZEOF(iv));
	EXPECT_EQ(MR_E_SUCCESS, result);

	mr_aes_destroy(aes);
	mr_ctx_destroy(mr_ctx);
}

void processTestInternal(
	const uint8_t* key, uint32_t keysize,
	const uint8_t* iv, uint32_t ivsize,
	const uint8_t* input, uint32_t inputsize,
	const uint8_t* expected, uint32_t expectedsize)
{
	uint8_t* output = new uint8_t[inputsize];
	uint32_t outputsize = inputsize;

	auto mr_ctx = mr_ctx_create(&_cfg);
	auto aes = mr_aes_create(mr_ctx);
	_mr_aesctr_ctx aesctr;
	EXPECT_NE(nullptr, aes);
	int result = mr_aes_init(aes, key, keysize);
	EXPECT_EQ(MR_E_SUCCESS, result);
	result = aesctr_init(&aesctr, aes, iv, ivsize);
	EXPECT_EQ(MR_E_SUCCESS, result);

	result = aesctr_process(&aesctr, input, inputsize, output, outputsize);
	EXPECT_EQ(MR_E_SUCCESS, result);
	EXPECT_BUFFEREQ(output, outputsize, expected, expectedsize);

	mr_aes_destroy(aes);
	mr_ctx_destroy(mr_ctx);
	delete[] output;
}

void processTest(
	const uint8_t* key, uint32_t keysize,
	const uint8_t* iv, uint32_t ivsize,
	const uint8_t* input, uint32_t inputsize,
	const uint8_t* expected, uint32_t expectedsize)
{
	processTestInternal(key, keysize, iv, ivsize, input, inputsize, expected, expectedsize);
	processTestInternal(key, keysize, iv, ivsize, expected, expectedsize, input, inputsize);
}

TEST(AesCtr, Process256) {
	const uint8_t key[]{
		0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e,
		0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71,
		0xdc, 0xc9, 0x8c, 0xb5, 0x82, 0xdd, 0x05, 0xe1,
		0xd0, 0x26, 0x57, 0x7a, 0x92, 0xb1, 0x56, 0x99
	};
	const uint8_t iv[]{
		0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e,
		0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71
	};
	const uint8_t input[]{
		1, 2, 3, 4, 5, 6, 7, 8
	};
	const uint8_t expected[]{
		0x7c, 0x06, 0xcc, 0xe2, 0x24, 0x3b, 0xda, 0x53
	};

	processTest(key, sizeof(key), iv, sizeof(iv), input, sizeof(input), expected, sizeof(expected));
}

TEST(AesCtr, Process256Reverse) {
	const uint8_t key[]{
		0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e,
		0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71,
		0xdc, 0xc9, 0x8c, 0xb5, 0x82, 0xdd, 0x05, 0xe1,
		0xd0, 0x26, 0x57, 0x7a, 0x92, 0xb1, 0x56, 0x99
	};
	const uint8_t iv[]{
		0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e,
		0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71
	};
	const uint8_t expected[]{
		1, 2, 3, 4, 5, 6, 7, 8
	};
	const uint8_t input[]{
		0x7c, 0x06, 0xcc, 0xe2, 0x24, 0x3b, 0xda, 0x53
	};

	processTest(key, sizeof(key), iv, sizeof(iv), input, sizeof(input), expected, sizeof(expected));
}

TEST(AesCtr, Process128) {
	const uint8_t key[]{
		0x06, 0x19, 0x3f, 0x99, 0x2f, 0xdc, 0xc9, 0x8c,
		0xb5, 0x82, 0xdd, 0x05, 0xe1, 0xd0, 0x26, 0x99
	};
	const uint8_t iv[]{
		0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e,
		0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71
	};
	const uint8_t input[]{
		1, 2, 3, 4, 5, 6, 7, 8
	};
	const uint8_t expected[]{
		0xa7, 0x93, 0xaf, 0xc8, 0x3b, 0x08, 0x87, 0x5d
	};

	processTest(key, sizeof(key), iv, sizeof(iv), input, sizeof(input), expected, sizeof(expected));
}

TEST(AesCtr, Process128Reverse) {
	const uint8_t key[]{
		0x06, 0x19, 0x3f, 0x99, 0x2f, 0xdc, 0xc9, 0x8c,
		0xb5, 0x82, 0xdd, 0x05, 0xe1, 0xd0, 0x26, 0x99
	};
	const uint8_t iv[]{
		0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e,
		0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71
	};
	const uint8_t expected[]{
		1, 2, 3, 4, 5, 6, 7, 8
	};
	const uint8_t input[]{
		0xa7, 0x93, 0xaf, 0xc8, 0x3b, 0x08, 0x87, 0x5d
	};

	processTest(key, sizeof(key), iv, sizeof(iv), input, sizeof(input), expected, sizeof(expected));
}

TEST(AesCtr, ProcessBlank256) {
	const uint8_t key[]{
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	};
	const uint8_t iv[]{
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	};
	const uint8_t input[]{
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	};
	const uint8_t expected[]{
		0xdc, 0x95, 0xc0, 0x78, 0xa2, 0x40, 0x89, 0x89
	};

	processTest(key, sizeof(key), iv, sizeof(iv), input, sizeof(input), expected, sizeof(expected));
}

TEST(AesCtr, ProcessBlank128) {
	const uint8_t key[]{
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	};
	const uint8_t iv[]{
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	};
	const uint8_t input[]{
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	};
	const uint8_t expected[]{
		0x66, 0xe9, 0x4b, 0xd4, 0xef, 0x8a, 0x2c, 0x3b
	};

	processTest(key, sizeof(key), iv, sizeof(iv), input, sizeof(input), expected, sizeof(expected));
}

TEST(AesCtr, ReferenceTest1) {
	const uint8_t key[]{
		0x07, 0x0a, 0x0b, 0x0c, 0x0d, 0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e, 0xa2, 0xfb, 0x71
	};
	const uint8_t iv[]{
		0xb2, 0x59, 0x65, 0x23, 0x3c, 0x91, 0x3c, 0x3d, 0xeb, 0x22, 0x2e, 0x79, 0x86, 0x68, 0x4c, 0xe6
	};
	const uint8_t input[]{
		0xe2, 0xc0, 0x8b, 0x2c, 0xaa, 0xc1, 0x4c, 0x46, 0xc8, 0x85, 0xc7, 0xb2, 0x1e, 0x24, 0xf6, 0x7d
	};
	const uint8_t expected[]{
		0x8d, 0x0e, 0x1d, 0x3a, 0xff, 0xf9, 0x72, 0x5f, 0xa9, 0xf0, 0xee, 0x58, 0x63, 0x82, 0x4d, 0x07
	};

	processTest(key, sizeof(key), iv, sizeof(iv), input, sizeof(input), expected, sizeof(expected));
}

TEST(AesCtr, ReferenceTest2) {
	const uint8_t key[]{
		0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e, 0xa2, 0xfb, 0x71
	};
	const uint8_t iv[]{
		0xb2, 0x59, 0x65, 0x23, 0x3c, 0x91, 0x3c, 0x3d, 0xeb, 0x22, 0x2e, 0x79, 0x86, 0x68, 0x4c, 0xe6
	};
	const uint8_t input[]{
		0x3d, 0xde, 0xd7, 0x5d, 0x30, 0x4e, 0x18, 0x5c, 0xb7, 0xf9, 0x9b, 0xe2, 0xac, 0x97, 0x91, 0x3f
	};
	const uint8_t expected[]{
		0x21, 0xa5, 0x77, 0xd9, 0xf9, 0xb5, 0x13, 0x3a, 0x7f, 0x28, 0xbc, 0x41, 0x03, 0x6c, 0xda, 0xfd
	};

	processTest(key, sizeof(key), iv, sizeof(iv), input, sizeof(input), expected, sizeof(expected));
}

TEST(AesCtr, ReferenceTest3) {
	const uint8_t key[]{
		0x08, 0x19, 0x3f, 0x99, 0x2f, 0xdc, 0xc9, 0x8c, 0xb5, 0x82, 0xdd, 0x05, 0xe1, 0xd0, 0x26, 0x99
	};
	const uint8_t iv[]{
		0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e, 0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71
	};
	const uint8_t input[]{
		0xe4, 0x9e, 0xbb, 0xd9, 0xa7, 0x0e, 0x5b, 0xa1
	};
	const uint8_t expected[]{
		0x24, 0xdf, 0x56, 0xd8, 0xe5, 0x38, 0x5f, 0xeb
	};

	processTest(key, sizeof(key), iv, sizeof(iv), input, sizeof(input), expected, sizeof(expected));
}

TEST(AesCtr, ReferenceTest4) {
	const uint8_t key[]{
		0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e, 0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71
	};
	const uint8_t iv[]{
		0xb2, 0x59, 0x65, 0x23, 0x3c, 0x91, 0x3c, 0x3d, 0xeb, 0x22, 0x2e, 0x79, 0x86, 0x68, 0x4c, 0xe6
	};
	const uint8_t input[]{
		0x3d, 0xde, 0xd7, 0x5d, 0x30, 0x4e, 0x18, 0x5c, 0xb7, 0xf9, 0x9b, 0xe2, 0xac, 0x97, 0x91, 0x3f
	};
	const uint8_t expected[]{
		0x63, 0x09, 0x94, 0xb9, 0x2d, 0xa2, 0xcf, 0x94, 0x2c, 0xc1, 0x08, 0xdc, 0x8e, 0x18, 0xa5, 0xec
	};

	processTest(key, sizeof(key), iv, sizeof(iv), input, sizeof(input), expected, sizeof(expected));
}

TEST(AesCtr, ReferenceTest6) {
	const uint8_t key[]{
		0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e, 0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71, 0xdc, 0xc9, 0x8c, 0xb5, 0x82, 0xdd, 0x05, 0xe1, 0xd0, 0x26, 0x57, 0x7a, 0x92, 0xb1, 0x56, 0x99
	};
	const uint8_t iv[]{
		0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e, 0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71
	};
	const uint8_t input[]{
		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
	};
	const uint8_t expected[]{
		0x7c, 0x06, 0xcc, 0xe2, 0x24, 0x3b, 0xda, 0x53
	};

	processTest(key, sizeof(key), iv, sizeof(iv), input, sizeof(input), expected, sizeof(expected));
}

TEST(AesCtr, ReferenceTest7) {
	const uint8_t key[]{
		0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e, 0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71
	};
	const uint8_t iv[]{
		0xb2, 0x59, 0x65, 0x23, 0x3c, 0x91, 0x3c, 0x3d, 0xeb, 0x22, 0x2e, 0x79, 0x86, 0x68, 0x4c, 0xe6
	};
	const uint8_t input[]{
		0xe2, 0xc0, 0x8b, 0x2c, 0xaa, 0xc1, 0x4c, 0x46, 0xc8, 0x85, 0xc7, 0xb2, 0x1e, 0x24, 0xf6, 0x7d
	};
	const uint8_t expected[]{
		0xbc, 0x17, 0xc8, 0xc8, 0xb7, 0x2d, 0x9b, 0x8e, 0x53, 0xbd, 0x54, 0x8c, 0x3c, 0xab, 0xc2, 0xae
	};

	processTest(key, sizeof(key), iv, sizeof(iv), input, sizeof(input), expected, sizeof(expected));
}

TEST(AesCtr, ReferenceTest8) {
	const uint8_t key[]{
		0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e, 0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71, 0xdc, 0xc9, 0x8c, 0xb5, 0x82, 0xdd, 0x05, 0xe1, 0xd0, 0x26, 0x57, 0x7a, 0x92, 0xb1, 0x56, 0x99
	};
	const uint8_t iv[]{
		0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e, 0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71
	};
	const uint8_t input[]{
		0xe4, 0x9e, 0xbb, 0xd9, 0xa7, 0x0e, 0x5b, 0xa1
	};
	const uint8_t expected[]{
		0x99, 0x9a, 0x74, 0x3f, 0x86, 0x33, 0x86, 0xfa
	};

	processTest(key, sizeof(key), iv, sizeof(iv), input, sizeof(input), expected, sizeof(expected));
}

TEST(AesCtr, ReferenceTest9) {
	const uint8_t key[]{
		0x09, 0x19, 0x3f, 0x99, 0x2f, 0xdc, 0xc9, 0x8c, 0xb5, 0x82, 0xdd, 0x05, 0xe1, 0xd0, 0x26, 0x99
	};
	const uint8_t iv[]{
		0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e, 0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71
	};
	const uint8_t input[]{
		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
	};
	const uint8_t expected[]{
		0xa1, 0x25, 0x4c, 0x70, 0xd9, 0xab, 0x60, 0x30
	};

	processTest(key, sizeof(key), iv, sizeof(iv), input, sizeof(input), expected, sizeof(expected));
}

TEST(AesCtr, ReferenceTest10) {
	const uint8_t key[]{
		0x0a, 0x19, 0x3f, 0x99, 0x2f, 0xdc, 0xc9, 0x8c, 0xb5, 0x82, 0xdd, 0x05, 0xe1, 0xd0, 0x26, 0x99
	};
	const uint8_t iv[]{
		0x07, 0x45, 0x19, 0x3f
	};
	const uint8_t input[]{
		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
	};
	const uint8_t expected[]{
		0xc3, 0xea, 0xcd, 0x0e, 0x1b, 0x9d, 0x6b, 0x91
	};

	processTest(key, sizeof(key), iv, sizeof(iv), input, sizeof(input), expected, sizeof(expected));
}

TEST(AesCtr, ReferenceTest11) {
	const uint8_t key[]{
		0x0b, 0x19, 0x3f, 0x99, 0x2f, 0xdc, 0xc9, 0x8c, 0xb5, 0x82, 0xdd, 0x05, 0xe1, 0xd0, 0x26, 0x99
	};
	const uint8_t iv[]{
		0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e, 0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71, 0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e, 0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71
	};
	const uint8_t input[]{
		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
	};
	const uint8_t expected[]{
		0x7b, 0xf9, 0x38, 0x63, 0xec, 0xe8, 0x09, 0xf8
	};

	processTest(key, sizeof(key), iv, sizeof(iv), input, sizeof(input), expected, sizeof(expected));
}

TEST(AesCtr, ReferenceTest12) {
	const uint8_t key[]{
		0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
	};
	const uint8_t iv[]{
		0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
	};
	const uint8_t input[]{
		0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
	};
	const uint8_t expected[]{
		0x87, 0x4d, 0x61, 0x91, 0xb6, 0x20, 0xe3, 0x26, 0x1b, 0xef, 0x68, 0x64, 0x99, 0x0d, 0xb6, 0xce,
	};

	processTest(key, sizeof(key), iv, sizeof(iv), input, sizeof(input), expected, sizeof(expected));
}
#include "pch.h"
#include <microratchet.h>
#include <internal.h>
#include "support.h"

static mr_config _cfg{ true };

TEST(AesCtr, Init256) {
	const uint8_t key[]{
		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
		0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
		0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
	};
	const uint8_t iv[]{
		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
		0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
	};

	auto mr_ctx = mr_ctx_create(&_cfg);
	auto aes = mr_aes_create(mr_ctx);
	_mr_aesctr_ctx aesctr;
	EXPECT_NE(nullptr, aes);
	int result = mr_aes_init(aes, key, SIZEOF(key));
	EXPECT_EQ(MR_E_SUCCESS, result);
	result = aesctr_init(&aesctr, aes, iv, SIZEOF(iv));
	EXPECT_EQ(MR_E_SUCCESS, result);

	mr_aes_destroy(aes);
	mr_ctx_destroy(mr_ctx);
}

TEST(AesCtr, Init128) {
	const uint8_t key[]{
		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
		0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
	};
	const uint8_t iv[]{
		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
		0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
	};

	auto mr_ctx = mr_ctx_create(&_cfg);
	auto aes = mr_aes_create(mr_ctx);
	_mr_aesctr_ctx aesctr;
	EXPECT_NE(nullptr, aes);
	int result = mr_aes_init(aes, key, SIZEOF(key));
	EXPECT_EQ(MR_E_SUCCESS, result);
	result = aesctr_init(&aesctr, aes, iv, SIZEOF(iv));
	EXPECT_EQ(MR_E_SUCCESS, result);

	mr_aes_destroy(aes);
	mr_ctx_destroy(mr_ctx);
}

void processTestInternal(
	const uint8_t* key, uint32_t keysize,
	const uint8_t* iv, uint32_t ivsize,
	const uint8_t* input, uint32_t inputsize,
	const uint8_t* expected, uint32_t expectedsize)
{
	uint8_t* output = new uint8_t[inputsize];
	uint32_t outputsize = inputsize;

	auto mr_ctx = mr_ctx_create(&_cfg);
	auto aes = mr_aes_create(mr_ctx);
	_mr_aesctr_ctx aesctr;
	EXPECT_NE(nullptr, aes);
	int result = mr_aes_init(aes, key, keysize);
	EXPECT_EQ(MR_E_SUCCESS, result);
	result = aesctr_init(&aesctr, aes, iv, ivsize);
	EXPECT_EQ(MR_E_SUCCESS, result);

	result = aesctr_process(&aesctr, input, inputsize, output, outputsize);
	EXPECT_EQ(MR_E_SUCCESS, result);
	EXPECT_BUFFEREQ(output, outputsize, expected, expectedsize);

	mr_aes_destroy(aes);
	mr_ctx_destroy(mr_ctx);
	delete[] output;
}

void processTest(
	const uint8_t* key, uint32_t keysize,
	const uint8_t* iv, uint32_t ivsize,
	const uint8_t* input, uint32_t inputsize,
	const uint8_t* expected, uint32_t expectedsize)
{
	processTestInternal(key, keysize, iv, ivsize, input, inputsize, expected, expectedsize);
	processTestInternal(key, keysize, iv, ivsize, expected, expectedsize, input, inputsize);
}

TEST(AesCtr, Process256) {
	const uint8_t key[]{
		0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e,
		0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71,
		0xdc, 0xc9, 0x8c, 0xb5, 0x82, 0xdd, 0x05, 0xe1,
		0xd0, 0x26, 0x57, 0x7a, 0x92, 0xb1, 0x56, 0x99
	};
	const uint8_t iv[]{
		0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e,
		0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71
	};
	const uint8_t input[]{
		1, 2, 3, 4, 5, 6, 7, 8
	};
	const uint8_t expected[]{
		0x7c, 0x06, 0xcc, 0xe2, 0x24, 0x3b, 0xda, 0x53
	};

	processTest(key, sizeof(key), iv, sizeof(iv), input, sizeof(input), expected, sizeof(expected));
}

TEST(AesCtr, Process256Reverse) {
	const uint8_t key[]{
		0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e,
		0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71,
		0xdc, 0xc9, 0x8c, 0xb5, 0x82, 0xdd, 0x05, 0xe1,
		0xd0, 0x26, 0x57, 0x7a, 0x92, 0xb1, 0x56, 0x99
	};
	const uint8_t iv[]{
		0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e,
		0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71
	};
	const uint8_t expected[]{
		1, 2, 3, 4, 5, 6, 7, 8
	};
	const uint8_t input[]{
		0x7c, 0x06, 0xcc, 0xe2, 0x24, 0x3b, 0xda, 0x53
	};

	processTest(key, sizeof(key), iv, sizeof(iv), input, sizeof(input), expected, sizeof(expected));
}

TEST(AesCtr, Process128) {
	const uint8_t key[]{
		0x06, 0x19, 0x3f, 0x99, 0x2f, 0xdc, 0xc9, 0x8c,
		0xb5, 0x82, 0xdd, 0x05, 0xe1, 0xd0, 0x26, 0x99
	};
	const uint8_t iv[]{
		0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e,
		0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71
	};
	const uint8_t input[]{
		1, 2, 3, 4, 5, 6, 7, 8
	};
	const uint8_t expected[]{
		0xa7, 0x93, 0xaf, 0xc8, 0x3b, 0x08, 0x87, 0x5d
	};

	processTest(key, sizeof(key), iv, sizeof(iv), input, sizeof(input), expected, sizeof(expected));
}

TEST(AesCtr, Process128Reverse) {
	const uint8_t key[]{
		0x06, 0x19, 0x3f, 0x99, 0x2f, 0xdc, 0xc9, 0x8c,
		0xb5, 0x82, 0xdd, 0x05, 0xe1, 0xd0, 0x26, 0x99
	};
	const uint8_t iv[]{
		0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e,
		0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71
	};
	const uint8_t expected[]{
		1, 2, 3, 4, 5, 6, 7, 8
	};
	const uint8_t input[]{
		0xa7, 0x93, 0xaf, 0xc8, 0x3b, 0x08, 0x87, 0x5d
	};

	processTest(key, sizeof(key), iv, sizeof(iv), input, sizeof(input), expected, sizeof(expected));
}

TEST(AesCtr, ProcessBlank256) {
	const uint8_t key[]{
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	};
	const uint8_t iv[]{
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	};
	const uint8_t input[]{
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	};
	const uint8_t expected[]{
		0xdc, 0x95, 0xc0, 0x78, 0xa2, 0x40, 0x89, 0x89
	};

	processTest(key, sizeof(key), iv, sizeof(iv), input, sizeof(input), expected, sizeof(expected));
}

TEST(AesCtr, ProcessBlank128) {
	const uint8_t key[]{
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	};
	const uint8_t iv[]{
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	};
	const uint8_t input[]{
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	};
	const uint8_t expected[]{
		0x66, 0xe9, 0x4b, 0xd4, 0xef, 0x8a, 0x2c, 0x3b
	};

	processTest(key, sizeof(key), iv, sizeof(iv), input, sizeof(input), expected, sizeof(expected));
}

TEST(AesCtr, ReferenceTest1) {
	const uint8_t key[]{
		0x07, 0x0a, 0x0b, 0x0c, 0x0d, 0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e, 0xa2, 0xfb, 0x71
	};
	const uint8_t iv[]{
		0xb2, 0x59, 0x65, 0x23, 0x3c, 0x91, 0x3c, 0x3d, 0xeb, 0x22, 0x2e, 0x79, 0x86, 0x68, 0x4c, 0xe6
	};
	const uint8_t input[]{
		0xe2, 0xc0, 0x8b, 0x2c, 0xaa, 0xc1, 0x4c, 0x46, 0xc8, 0x85, 0xc7, 0xb2, 0x1e, 0x24, 0xf6, 0x7d
	};
	const uint8_t expected[]{
		0x8d, 0x0e, 0x1d, 0x3a, 0xff, 0xf9, 0x72, 0x5f, 0xa9, 0xf0, 0xee, 0x58, 0x63, 0x82, 0x4d, 0x07
	};

	processTest(key, sizeof(key), iv, sizeof(iv), input, sizeof(input), expected, sizeof(expected));
}

TEST(AesCtr, ReferenceTest2) {
	const uint8_t key[]{
		0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e, 0xa2, 0xfb, 0x71
	};
	const uint8_t iv[]{
		0xb2, 0x59, 0x65, 0x23, 0x3c, 0x91, 0x3c, 0x3d, 0xeb, 0x22, 0x2e, 0x79, 0x86, 0x68, 0x4c, 0xe6
	};
	const uint8_t input[]{
		0x3d, 0xde, 0xd7, 0x5d, 0x30, 0x4e, 0x18, 0x5c, 0xb7, 0xf9, 0x9b, 0xe2, 0xac, 0x97, 0x91, 0x3f
	};
	const uint8_t expected[]{
		0x21, 0xa5, 0x77, 0xd9, 0xf9, 0xb5, 0x13, 0x3a, 0x7f, 0x28, 0xbc, 0x41, 0x03, 0x6c, 0xda, 0xfd
	};

	processTest(key, sizeof(key), iv, sizeof(iv), input, sizeof(input), expected, sizeof(expected));
}

TEST(AesCtr, ReferenceTest3) {
	const uint8_t key[]{
		0x08, 0x19, 0x3f, 0x99, 0x2f, 0xdc, 0xc9, 0x8c, 0xb5, 0x82, 0xdd, 0x05, 0xe1, 0xd0, 0x26, 0x99
	};
	const uint8_t iv[]{
		0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e, 0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71
	};
	const uint8_t input[]{
		0xe4, 0x9e, 0xbb, 0xd9, 0xa7, 0x0e, 0x5b, 0xa1
	};
	const uint8_t expected[]{
		0x24, 0xdf, 0x56, 0xd8, 0xe5, 0x38, 0x5f, 0xeb
	};

	processTest(key, sizeof(key), iv, sizeof(iv), input, sizeof(input), expected, sizeof(expected));
}

TEST(AesCtr, ReferenceTest4) {
	const uint8_t key[]{
		0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e, 0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71
	};
	const uint8_t iv[]{
		0xb2, 0x59, 0x65, 0x23, 0x3c, 0x91, 0x3c, 0x3d, 0xeb, 0x22, 0x2e, 0x79, 0x86, 0x68, 0x4c, 0xe6
	};
	const uint8_t input[]{
		0x3d, 0xde, 0xd7, 0x5d, 0x30, 0x4e, 0x18, 0x5c, 0xb7, 0xf9, 0x9b, 0xe2, 0xac, 0x97, 0x91, 0x3f
	};
	const uint8_t expected[]{
		0x63, 0x09, 0x94, 0xb9, 0x2d, 0xa2, 0xcf, 0x94, 0x2c, 0xc1, 0x08, 0xdc, 0x8e, 0x18, 0xa5, 0xec
	};

	processTest(key, sizeof(key), iv, sizeof(iv), input, sizeof(input), expected, sizeof(expected));
}

TEST(AesCtr, ReferenceTest6) {
	const uint8_t key[]{
		0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e, 0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71, 0xdc, 0xc9, 0x8c, 0xb5, 0x82, 0xdd, 0x05, 0xe1, 0xd0, 0x26, 0x57, 0x7a, 0x92, 0xb1, 0x56, 0x99
	};
	const uint8_t iv[]{
		0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e, 0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71
	};
	const uint8_t input[]{
		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
	};
	const uint8_t expected[]{
		0x7c, 0x06, 0xcc, 0xe2, 0x24, 0x3b, 0xda, 0x53
	};

	processTest(key, sizeof(key), iv, sizeof(iv), input, sizeof(input), expected, sizeof(expected));
}

TEST(AesCtr, ReferenceTest7) {
	const uint8_t key[]{
		0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e, 0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71
	};
	const uint8_t iv[]{
		0xb2, 0x59, 0x65, 0x23, 0x3c, 0x91, 0x3c, 0x3d, 0xeb, 0x22, 0x2e, 0x79, 0x86, 0x68, 0x4c, 0xe6
	};
	const uint8_t input[]{
		0xe2, 0xc0, 0x8b, 0x2c, 0xaa, 0xc1, 0x4c, 0x46, 0xc8, 0x85, 0xc7, 0xb2, 0x1e, 0x24, 0xf6, 0x7d
	};
	const uint8_t expected[]{
		0xbc, 0x17, 0xc8, 0xc8, 0xb7, 0x2d, 0x9b, 0x8e, 0x53, 0xbd, 0x54, 0x8c, 0x3c, 0xab, 0xc2, 0xae
	};

	processTest(key, sizeof(key), iv, sizeof(iv), input, sizeof(input), expected, sizeof(expected));
}

TEST(AesCtr, ReferenceTest8) {
	const uint8_t key[]{
		0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e, 0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71, 0xdc, 0xc9, 0x8c, 0xb5, 0x82, 0xdd, 0x05, 0xe1, 0xd0, 0x26, 0x57, 0x7a, 0x92, 0xb1, 0x56, 0x99
	};
	const uint8_t iv[]{
		0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e, 0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71
	};
	const uint8_t input[]{
		0xe4, 0x9e, 0xbb, 0xd9, 0xa7, 0x0e, 0x5b, 0xa1
	};
	const uint8_t expected[]{
		0x99, 0x9a, 0x74, 0x3f, 0x86, 0x33, 0x86, 0xfa
	};

	processTest(key, sizeof(key), iv, sizeof(iv), input, sizeof(input), expected, sizeof(expected));
}

TEST(AesCtr, ReferenceTest9) {
	const uint8_t key[]{
		0x09, 0x19, 0x3f, 0x99, 0x2f, 0xdc, 0xc9, 0x8c, 0xb5, 0x82, 0xdd, 0x05, 0xe1, 0xd0, 0x26, 0x99
	};
	const uint8_t iv[]{
		0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e, 0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71
	};
	const uint8_t input[]{
		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
	};
	const uint8_t expected[]{
		0xa1, 0x25, 0x4c, 0x70, 0xd9, 0xab, 0x60, 0x30
	};

	processTest(key, sizeof(key), iv, sizeof(iv), input, sizeof(input), expected, sizeof(expected));
}

TEST(AesCtr, ReferenceTest10) {
	const uint8_t key[]{
		0x0a, 0x19, 0x3f, 0x99, 0x2f, 0xdc, 0xc9, 0x8c, 0xb5, 0x82, 0xdd, 0x05, 0xe1, 0xd0, 0x26, 0x99
	};
	const uint8_t iv[]{
		0x07, 0x45, 0x19, 0x3f
	};
	const uint8_t input[]{
		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
	};
	const uint8_t expected[]{
		0xc3, 0xea, 0xcd, 0x0e, 0x1b, 0x9d, 0x6b, 0x91
	};

	processTest(key, sizeof(key), iv, sizeof(iv), input, sizeof(input), expected, sizeof(expected));
}

TEST(AesCtr, ReferenceTest11) {
	const uint8_t key[]{
		0x0b, 0x19, 0x3f, 0x99, 0x2f, 0xdc, 0xc9, 0x8c, 0xb5, 0x82, 0xdd, 0x05, 0xe1, 0xd0, 0x26, 0x99
	};
	const uint8_t iv[]{
		0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e, 0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71, 0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e, 0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71
	};
	const uint8_t input[]{
		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
	};
	const uint8_t expected[]{
		0x7b, 0xf9, 0x38, 0x63, 0xec, 0xe8, 0x09, 0xf8
	};

	processTest(key, sizeof(key), iv, sizeof(iv), input, sizeof(input), expected, sizeof(expected));
}

TEST(AesCtr, ReferenceTest12) {
	const uint8_t key[]{
		0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
	};
	const uint8_t iv[]{
		0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
	};
	const uint8_t input[]{
		0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
	};
	const uint8_t expected[]{
		0x87, 0x4d, 0x61, 0x91, 0xb6, 0x20, 0xe3, 0x26, 0x1b, 0xef, 0x68, 0x64, 0x99, 0x0d, 0xb6, 0xce,
	};

	processTest(key, sizeof(key), iv, sizeof(iv), input, sizeof(input), expected, sizeof(expected));
}
