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

static mr_config _cfg{ true };

TEST(AesKdf, 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
	};
	uint8_t output[16];

	auto mr_ctx = mr_ctx_create(&_cfg);

	auto result = kdf_compute(mr_ctx, key, sizeof(key), iv, sizeof(iv), output, sizeof(output));
	EXPECT_EQ(MR_E_SUCCESS, result);

	mr_ctx_destroy(mr_ctx);
}

TEST(AesKdf, 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
	};
	uint8_t output[16];

	auto mr_ctx = mr_ctx_create(&_cfg);

	auto result = kdf_compute(mr_ctx, key, sizeof(key), iv, sizeof(iv), output, sizeof(output));
	EXPECT_EQ(MR_E_SUCCESS, result);

	mr_ctx_destroy(mr_ctx);
}

void test_process(const uint8_t* key, uint32_t keysize, const uint8_t* info, uint32_t infosize, const uint8_t* expected, uint32_t expectedsize)
{
	uint8_t* output = new uint8_t[expectedsize];
	auto mr_ctx = mr_ctx_create(&_cfg);

	auto result = kdf_compute(mr_ctx, key, keysize, info, infosize, output, expectedsize);
	EXPECT_EQ(MR_E_SUCCESS, result);
	EXPECT_BUFFEREQ(output, expectedsize, expected, expectedsize);

	delete[] output;
	mr_ctx_destroy(mr_ctx);
}

TEST(AesKdf, ReferenceTest1) {
	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 };
	uint8_t info[]{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
	uint8_t expected[]{ 0x37, 0xe4, 0x95, 0xd0, 0x0e, 0x00, 0xdb, 0xed };

	test_process(key, sizeof(key), info, sizeof(info), expected, sizeof(expected));
}

TEST(AesKdf, ReferenceTest2) {
	uint8_t key[]{ 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e, 0xa2, 0xfb, 0x71 };
	uint8_t info[]{ 0xb2, 0x59, 0x65, 0x23, 0x3c, 0x91, 0x3c, 0x3d, 0xeb, 0x22, 0x2e, 0x79, 0x86, 0x68, 0x4c, 0xe6 };
	uint8_t expected[]{ 0x78, 0x88, 0x8f, 0xbd, 0x04, 0x44, 0xbf, 0x99, 0xfa, 0xf9, 0x56, 0x9a, 0x3f, 0x87, 0x41, 0xbe };

	test_process(key, sizeof(key), info, sizeof(info), expected, sizeof(expected));
}

TEST(AesKdf, ReferenceTest3) {
	uint8_t key[]{ 0x09, 0x19, 0x3f, 0x99, 0x2f, 0xdc, 0xc9, 0x8c, 0xb5, 0x82, 0xdd, 0x05, 0xe1, 0xd0, 0x26, 0x99 };
	uint8_t info[]{ 0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e, 0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71 };
	uint8_t expected[]{ 0x07, 0xab, 0xa0, 0x21, 0xc9, 0xd4, 0xd4, 0xce };

	test_process(key, sizeof(key), info, sizeof(info), expected, sizeof(expected));
}

TEST(AesKdf, ReferenceTest4) {
	uint8_t key[]{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
	uint8_t info[]{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
	uint8_t expected[]{ 0x5c, 0xf1, 0x91, 0x76, 0x25, 0xb1, 0x16, 0x5f };

	test_process(key, sizeof(key), info, sizeof(info), expected, sizeof(expected));
}

TEST(AesKdf, ReferenceTest5) {
	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 };
	uint8_t info[]{ 0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e, 0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71 };
	uint8_t expected[]{ 0xce, 0x3c, 0xa5, 0x38, 0xf5, 0xd6, 0x80, 0x8d };

	test_process(key, sizeof(key), info, sizeof(info), expected, sizeof(expected));
}

TEST(AesKdf, ReferenceTest6) {
	uint8_t key[]{ 0x0b, 0x19, 0x3f, 0x99, 0x2f, 0xdc, 0xc9, 0x8c, 0xb5, 0x82, 0xdd, 0x05, 0xe1, 0xd0, 0x26, 0x99 };
	uint8_t info[]{ 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 };
	uint8_t expected[]{ 0xbe, 0x20, 0x74, 0x12, 0xd8, 0xe0, 0x6b, 0xe5 };

	test_process(key, sizeof(key), info, sizeof(info), expected, sizeof(expected));
}

TEST(AesKdf, ReferenceTest7) {
	uint8_t key[]{ 0x07, 0x0a, 0x0b, 0x0c, 0x0d, 0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e, 0xa2, 0xfb, 0x71 };
	uint8_t info[]{ 0xb2, 0x59, 0x65, 0x23, 0x3c, 0x91, 0x3c, 0x3d, 0xeb, 0x22, 0x2e, 0x79, 0x86, 0x68, 0x4c, 0xe6 };
	uint8_t expected[]{ 0xa8, 0x76, 0x14, 0x7a, 0x94, 0x66, 0x0f, 0xa5, 0x1b, 0x80, 0xf9, 0x29, 0xe0, 0x98, 0x85, 0x38 };

	test_process(key, sizeof(key), info, sizeof(info), expected, sizeof(expected));
}

TEST(AesKdf, ReferenceTest8) {
	uint8_t key[]{ 0x0a, 0x19, 0x3f, 0x99, 0x2f, 0xdc, 0xc9, 0x8c, 0xb5, 0x82, 0xdd, 0x05, 0xe1, 0xd0, 0x26, 0x99 };
	uint8_t info[]{ 0x07, 0x45, 0x19, 0x3f };
	uint8_t expected[]{ 0x33, 0x8c, 0xd0, 0xfc, 0x25, 0x27, 0x24, 0x98 };

	test_process(key, sizeof(key), info, sizeof(info), expected, sizeof(expected));
}

TEST(AesKdf, ReferenceTest9) {
	uint8_t key[]{ 0x08, 0x19, 0x3f, 0x99, 0x2f, 0xdc, 0xc9, 0x8c, 0xb5, 0x82, 0xdd, 0x05, 0xe1, 0xd0, 0x26, 0x99 };
	uint8_t info[]{ 0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e, 0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71 };
	uint8_t expected[]{ 0xa8, 0x64, 0x81, 0xde, 0x7e, 0x14, 0xd2, 0xe2 };

	test_process(key, sizeof(key), info, sizeof(info), expected, sizeof(expected));
}

TEST(AesKdf, ReferenceTest10) {
	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 };
	uint8_t info[]{ 0xb2, 0x59, 0x65, 0x23, 0x3c, 0x91, 0x3c, 0x3d, 0xeb, 0x22, 0x2e, 0x79, 0x86, 0x68, 0x4c, 0xe6 };
	uint8_t expected[]{ 0xef, 0x1d, 0x15, 0xca, 0x50, 0xf5, 0x28, 0x83, 0xc4, 0xf9, 0xf2, 0x32, 0xfc, 0x4e, 0x8d, 0xd3 };

	test_process(key, sizeof(key), info, sizeof(info), expected, sizeof(expected));
}
#include "pch.h"
#include <microratchet.h>
#include <internal.h>
#include "support.h"

static mr_config _cfg{ true };

TEST(AesKdf, 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
	};
	uint8_t output[16];

	auto mr_ctx = mr_ctx_create(&_cfg);

	auto result = kdf_compute(mr_ctx, key, sizeof(key), iv, sizeof(iv), output, sizeof(output));
	EXPECT_EQ(MR_E_SUCCESS, result);

	mr_ctx_destroy(mr_ctx);
}

TEST(AesKdf, 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
	};
	uint8_t output[16];

	auto mr_ctx = mr_ctx_create(&_cfg);

	auto result = kdf_compute(mr_ctx, key, sizeof(key), iv, sizeof(iv), output, sizeof(output));
	EXPECT_EQ(MR_E_SUCCESS, result);

	mr_ctx_destroy(mr_ctx);
}

void test_process(const uint8_t* key, uint32_t keysize, const uint8_t* info, uint32_t infosize, const uint8_t* expected, uint32_t expectedsize)
{
	uint8_t* output = new uint8_t[expectedsize];
	auto mr_ctx = mr_ctx_create(&_cfg);

	auto result = kdf_compute(mr_ctx, key, keysize, info, infosize, output, expectedsize);
	EXPECT_EQ(MR_E_SUCCESS, result);
	EXPECT_BUFFEREQ(output, expectedsize, expected, expectedsize);

	delete[] output;
	mr_ctx_destroy(mr_ctx);
}

TEST(AesKdf, ReferenceTest1) {
	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 };
	uint8_t info[]{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
	uint8_t expected[]{ 0x37, 0xe4, 0x95, 0xd0, 0x0e, 0x00, 0xdb, 0xed };

	test_process(key, sizeof(key), info, sizeof(info), expected, sizeof(expected));
}

TEST(AesKdf, ReferenceTest2) {
	uint8_t key[]{ 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e, 0xa2, 0xfb, 0x71 };
	uint8_t info[]{ 0xb2, 0x59, 0x65, 0x23, 0x3c, 0x91, 0x3c, 0x3d, 0xeb, 0x22, 0x2e, 0x79, 0x86, 0x68, 0x4c, 0xe6 };
	uint8_t expected[]{ 0x78, 0x88, 0x8f, 0xbd, 0x04, 0x44, 0xbf, 0x99, 0xfa, 0xf9, 0x56, 0x9a, 0x3f, 0x87, 0x41, 0xbe };

	test_process(key, sizeof(key), info, sizeof(info), expected, sizeof(expected));
}

TEST(AesKdf, ReferenceTest3) {
	uint8_t key[]{ 0x09, 0x19, 0x3f, 0x99, 0x2f, 0xdc, 0xc9, 0x8c, 0xb5, 0x82, 0xdd, 0x05, 0xe1, 0xd0, 0x26, 0x99 };
	uint8_t info[]{ 0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e, 0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71 };
	uint8_t expected[]{ 0x07, 0xab, 0xa0, 0x21, 0xc9, 0xd4, 0xd4, 0xce };

	test_process(key, sizeof(key), info, sizeof(info), expected, sizeof(expected));
}

TEST(AesKdf, ReferenceTest4) {
	uint8_t key[]{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
	uint8_t info[]{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
	uint8_t expected[]{ 0x5c, 0xf1, 0x91, 0x76, 0x25, 0xb1, 0x16, 0x5f };

	test_process(key, sizeof(key), info, sizeof(info), expected, sizeof(expected));
}

TEST(AesKdf, ReferenceTest5) {
	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 };
	uint8_t info[]{ 0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e, 0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71 };
	uint8_t expected[]{ 0xce, 0x3c, 0xa5, 0x38, 0xf5, 0xd6, 0x80, 0x8d };

	test_process(key, sizeof(key), info, sizeof(info), expected, sizeof(expected));
}

TEST(AesKdf, ReferenceTest6) {
	uint8_t key[]{ 0x0b, 0x19, 0x3f, 0x99, 0x2f, 0xdc, 0xc9, 0x8c, 0xb5, 0x82, 0xdd, 0x05, 0xe1, 0xd0, 0x26, 0x99 };
	uint8_t info[]{ 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 };
	uint8_t expected[]{ 0xbe, 0x20, 0x74, 0x12, 0xd8, 0xe0, 0x6b, 0xe5 };

	test_process(key, sizeof(key), info, sizeof(info), expected, sizeof(expected));
}

TEST(AesKdf, ReferenceTest7) {
	uint8_t key[]{ 0x07, 0x0a, 0x0b, 0x0c, 0x0d, 0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e, 0xa2, 0xfb, 0x71 };
	uint8_t info[]{ 0xb2, 0x59, 0x65, 0x23, 0x3c, 0x91, 0x3c, 0x3d, 0xeb, 0x22, 0x2e, 0x79, 0x86, 0x68, 0x4c, 0xe6 };
	uint8_t expected[]{ 0xa8, 0x76, 0x14, 0x7a, 0x94, 0x66, 0x0f, 0xa5, 0x1b, 0x80, 0xf9, 0x29, 0xe0, 0x98, 0x85, 0x38 };

	test_process(key, sizeof(key), info, sizeof(info), expected, sizeof(expected));
}

TEST(AesKdf, ReferenceTest8) {
	uint8_t key[]{ 0x0a, 0x19, 0x3f, 0x99, 0x2f, 0xdc, 0xc9, 0x8c, 0xb5, 0x82, 0xdd, 0x05, 0xe1, 0xd0, 0x26, 0x99 };
	uint8_t info[]{ 0x07, 0x45, 0x19, 0x3f };
	uint8_t expected[]{ 0x33, 0x8c, 0xd0, 0xfc, 0x25, 0x27, 0x24, 0x98 };

	test_process(key, sizeof(key), info, sizeof(info), expected, sizeof(expected));
}

TEST(AesKdf, ReferenceTest9) {
	uint8_t key[]{ 0x08, 0x19, 0x3f, 0x99, 0x2f, 0xdc, 0xc9, 0x8c, 0xb5, 0x82, 0xdd, 0x05, 0xe1, 0xd0, 0x26, 0x99 };
	uint8_t info[]{ 0x07, 0x45, 0x19, 0x3f, 0x99, 0x2f, 0x6f, 0x7e, 0xa2, 0xfb, 0x7d, 0xdb, 0xa0, 0x82, 0x85, 0x71 };
	uint8_t expected[]{ 0xa8, 0x64, 0x81, 0xde, 0x7e, 0x14, 0xd2, 0xe2 };

	test_process(key, sizeof(key), info, sizeof(info), expected, sizeof(expected));
}

TEST(AesKdf, ReferenceTest10) {
	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 };
	uint8_t info[]{ 0xb2, 0x59, 0x65, 0x23, 0x3c, 0x91, 0x3c, 0x3d, 0xeb, 0x22, 0x2e, 0x79, 0x86, 0x68, 0x4c, 0xe6 };
	uint8_t expected[]{ 0xef, 0x1d, 0x15, 0xca, 0x50, 0xf5, 0x28, 0x83, 0xc4, 0xf9, 0xf2, 0x32, 0xfc, 0x4e, 0x8d, 0xd3 };

	test_process(key, sizeof(key), info, sizeof(info), expected, sizeof(expected));
}
