/*
 *  Copyright (c) 2005-2026, Karsten Ohme
 *  This file is part of GlobalPlatform.
 *
 *  GlobalPlatform is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  GlobalPlatform is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with GlobalPlatform.  If not, see <https://www.gnu.org/licenses/>.
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations
 * including the two.
 * You must obey the GNU Lesser General Public License in all respects
 * for all of the code used other than OpenSSL.  If you modify
 * file(s) with this exception, you may extend this exception to your
 * version of the file(s), but you are not obligated to do so.  If you
 * do not wish to do so, delete this exception statement from your
 * version.
 */
#include <stdlib.h>
#include <setjmp.h>
#include <stdio.h>
#include <string.h>
#include <cmocka.h>
#include "crypto.h"
#include "testUtil.h"

TCHAR *exec_path;

#ifndef WIN32
#define _tcsrchr strrchr
#endif

static void test_aes_cmac_128(void **state) {
	OPGP_ERROR_STATUS status;
	// examples from https, 0x//nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38b.pdf
	BYTE message1[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
	BYTE message2[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
	0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
	0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11};


	BYTE key[] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c};
	BYTE expected_mac_1[] = {0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44, 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c};
	BYTE expected_mac_2[] = {0xdf, 0xa6, 0x67, 0x47, 0xde, 0x9a, 0xe6, 0x30, 0x30, 0xca, 0x32, 0x61, 0x14, 0x97, 0xc8, 0x27};
	BYTE mac[16];

	status = calculate_CMAC_aes(key, sizeof(key), message1, sizeof(message1), NULL, mac);
	assert_int_equal(status.errorStatus, OPGP_ERROR_STATUS_SUCCESS);
	assert_memory_equal(mac, expected_mac_1, 16);

	status = calculate_CMAC_aes(key, sizeof(key), message2, sizeof(message2), NULL, mac);
	assert_int_equal(status.errorStatus, OPGP_ERROR_STATUS_SUCCESS);
	assert_memory_equal(mac, expected_mac_2, 16);
}

static void test_aes_cmac_192(void **state) {
	OPGP_ERROR_STATUS status;
	// examples from https, 0x//nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38b.pdf
	BYTE message1[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
	BYTE message2[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
	0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
	0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11};


	BYTE key[] = {0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52, 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
			0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b};
	BYTE expected_mac_1[] = {0x9e, 0x99, 0xa7, 0xbf, 0x31, 0xe7, 0x10, 0x90, 0x06, 0x62, 0xf6, 0x5e, 0x61, 0x7c, 0x51, 0x84};
	BYTE expected_mac_2[] = {0x8a, 0x1d, 0xe5, 0xbe, 0x2e, 0xb3, 0x1a, 0xad, 0x08, 0x9a, 0x82, 0xe6, 0xee, 0x90, 0x8b, 0x0e};
	BYTE mac[16];

	status = calculate_CMAC_aes(key, sizeof(key), message1, sizeof(message1), NULL, mac);
	assert_int_equal(status.errorStatus, OPGP_ERROR_STATUS_SUCCESS);
	assert_memory_equal(mac, expected_mac_1, 16);

	status = calculate_CMAC_aes(key, sizeof(key), message2, sizeof(message2), NULL, mac);
	assert_int_equal(status.errorStatus, OPGP_ERROR_STATUS_SUCCESS);
	assert_memory_equal(mac, expected_mac_2, 16);
}

static void test_read_public_rsa_key(void **state) {
	OPGP_ERROR_STATUS status;
	TCHAR filename_buffer[2048];
	BYTE n[128];
	DWORD nLength = sizeof(n);
	LONG e = 0;
	BYTE expected_rsa_n[] = {
			 0xc6, 0x3f, 0xaf, 0x41, 0xc3, 0x43, 0xd4, 0xfa, 0xf9, 0x65, 0xe0, 0xdd, 0x32, 0xf3,
			 0xb2, 0xc5, 0xa5, 0xac, 0x31, 0x9f, 0x95, 0x46, 0x4f, 0x25, 0xf8, 0x83, 0xe2, 0xbd, 0x5b,
			 0x32, 0x9f, 0x93, 0xc0, 0xbc, 0xf4, 0x71, 0xfb, 0x66, 0x38, 0xdb, 0xd2, 0xb4, 0x5e, 0xcf,
			 0x55, 0x23, 0x4d, 0x8c, 0xd7, 0x18, 0xe7, 0x07, 0xe7, 0xca, 0x3e, 0x19, 0x1d, 0xad, 0x55,
			 0x23, 0x61, 0xf9, 0xd4, 0x22, 0x1d, 0x80, 0x04, 0x0b, 0xa5, 0x0f, 0xac, 0x08, 0xa5, 0xc0,
			 0x13, 0x7e, 0xa4, 0xa7, 0x57, 0xad, 0x2e, 0x5b, 0x19, 0x13, 0x6b, 0xeb, 0x69, 0xf0, 0xd7,
			 0x07, 0xfe, 0x16, 0x93, 0x75, 0x4c, 0x5a, 0xbe, 0xbf, 0xea, 0x59, 0xa6, 0x88, 0xa6, 0x35,
			 0x60, 0x3e, 0xc8, 0x49, 0xb2, 0x83, 0x1b, 0xa6, 0xbe, 0x26, 0xaf, 0x63, 0x7f, 0x6d, 0x7d,
			 0x1d, 0x63, 0x8c, 0xe7, 0x99, 0xca, 0x02, 0xb4, 0x57
			};
	LONG expected_rsa_e = 0x010001L;
	_sntprintf(filename_buffer, sizeof(filename_buffer), _T("%s/%s"), exec_path, _T("rsa_pub_key_test.pem"));
	status = read_public_rsa_key(filename_buffer, "password", n, &nLength, &e);
	assert_int_equal(status.errorStatus, OPGP_ERROR_STATUS_SUCCESS);
	assert_memory_equal(n, expected_rsa_n, sizeof(n));
	assert_int_equal(e, expected_rsa_e);
}

static void test_read_encrypted_private_rsa_key(void **state) {
	OPGP_ERROR_STATUS status;
	TCHAR filename_buffer[2048];
	BYTE n[256];
	DWORD nLength = sizeof(n);
	LONG e = 0;
	BYTE expected_rsa_n[] = {
			0xaf, 0xc2, 0x5c, 0xbe, 0x6c, 0x09, 0x03, 0xfb, 0x78, 0x51, 0xc6, 0x83, 0x85, 0xb1, 0xaa, 0x61,
			0xce, 0xee, 0x42, 0xe8, 0xc6, 0x14, 0x88, 0xae, 0xda, 0xa0, 0xfb, 0x34, 0xec, 0xcc, 0x40, 0x8e,
			0x55, 0xec, 0x73, 0x96, 0x03, 0x84, 0x59, 0xb2, 0x49, 0xbd, 0xd7, 0xe0, 0x09, 0x8a, 0xd4, 0x87,
			0xec, 0x7d, 0x6d, 0x0c, 0x2e, 0x2e, 0x0d, 0xe9, 0x3f, 0x07, 0xbd, 0x6e, 0xfa, 0xfb, 0x10, 0xc1,
			0x13, 0xbf, 0x0f, 0xad, 0x96, 0xa8, 0xca, 0x0e, 0xa5, 0xb4, 0x27, 0x78, 0x66, 0x20, 0x49, 0x50,
			0xfb, 0x88, 0x4f, 0x9f, 0xa9, 0xb9, 0xc3, 0xa7, 0x44, 0x6d, 0xb8, 0x57, 0x84, 0xf4, 0xbd, 0xab,
			0xb5, 0x5c, 0xad, 0xd3, 0xa6, 0x2e, 0xc5, 0x50, 0xd4, 0x18, 0x1c, 0xc4, 0x01, 0xf3, 0x24, 0xe6,
			0xb5, 0x46, 0xc3, 0xf6, 0x61, 0x5a, 0x18, 0x97, 0xe8, 0x73, 0xd5, 0x9e, 0xf7, 0xb1, 0xe4, 0x21,
			0xfb, 0xa1, 0x26, 0x57, 0x72, 0xd3, 0x8b, 0x83, 0x88, 0x87, 0x12, 0x82, 0xcf, 0xae, 0x55, 0xbf,
			0x49, 0x11, 0x70, 0x65, 0x30, 0x3f, 0xf3, 0xbe, 0x3e, 0xd6, 0x04, 0x97, 0x34, 0xd9, 0xa4, 0x51,
			0x30, 0x61, 0x25, 0x35, 0xc1, 0x4c, 0xa4, 0x48, 0xe9, 0x40, 0xef, 0xd4, 0x0c, 0x0d, 0xe9, 0xfa,
			0x36, 0xeb, 0x8c, 0x8e, 0x0c, 0xd8, 0xad, 0xd7, 0x34, 0x1c, 0xf3, 0x6b, 0x76, 0xf5, 0x39, 0x7f,
			0xbf, 0x91, 0x96, 0x77, 0xba, 0x08, 0xc8, 0x93, 0x95, 0x04, 0x9d, 0x75, 0x34, 0xed, 0xdc, 0xa7,
			0x84, 0x76, 0xd2, 0x0f, 0x6b, 0xda, 0x20, 0x6d, 0xdc, 0x3c, 0x98, 0xc6, 0x30, 0x19, 0x16, 0xcf,
			0xdc, 0x55, 0xd4, 0x70, 0x50, 0xc1, 0x16, 0xda, 0x6a, 0xcc, 0x79, 0x00, 0x28, 0xcf, 0xad, 0x2d,
			0xcb, 0x96, 0x40, 0x07, 0xf0, 0x57, 0x89, 0xd8, 0xd9, 0x25, 0x3b, 0x75, 0x28, 0x2d, 0x6a, 0x81,
	};
	LONG expected_rsa_e = 65537L;
	_sntprintf(filename_buffer, sizeof(filename_buffer), _T("%s/%s"), exec_path, _T("rsa_private_key.pem"));
	status = read_public_rsa_key(filename_buffer, "password", n, &nLength, &e);
	assert_int_equal(status.errorStatus, OPGP_ERROR_STATUS_SUCCESS);
	assert_int_equal(nLength, sizeof(expected_rsa_n));
	assert_memory_equal(n, expected_rsa_n, nLength);
	assert_int_equal(e, expected_rsa_e);
}

static void test_validate_signature_rsa(void **state) {
	OPGP_ERROR_STATUS status;
	TCHAR private_key_file[2048];
	TCHAR public_key_file[2048];
	BYTE message[] = {
		0x44, 0x65, 0x6C, 0x65, 0x67, 0x61, 0x74, 0x65,
		0x64, 0x20, 0x4D, 0x61, 0x6E, 0x61, 0x67, 0x65,
		0x6D, 0x65, 0x6E, 0x74
	};
	BYTE signature[512];
	DWORD signatureLength = sizeof(signature);

	_sntprintf(private_key_file, sizeof(private_key_file), _T("%s/%s"), exec_path, _T("rsa_private_key.pem"));
	_sntprintf(public_key_file, sizeof(public_key_file), _T("%s/%s"), exec_path, _T("rsa_public_key.pem"));

	status = calculate_signature(message, sizeof(message), private_key_file, "password", signature, &signatureLength);
	assert_int_equal(status.errorStatus, OPGP_ERROR_STATUS_SUCCESS);

	status = validate_signature(message, sizeof(message), public_key_file, NULL, signature, signatureLength);
	assert_int_equal(status.errorStatus, OPGP_ERROR_STATUS_SUCCESS);

	message[0] ^= 0x01;
	status = validate_signature(message, sizeof(message), public_key_file, NULL, signature, signatureLength);
	assert_int_equal(status.errorStatus, OPGP_ERROR_STATUS_FAILURE);
	assert_int_equal(status.errorCode, OPGP_ERROR_VALIDATION_FAILED);
}

static void test_validate_signature_ecc(void **state) {
	OPGP_ERROR_STATUS status;
	TCHAR private_key_file[2048];
	TCHAR public_key_file[2048];
	BYTE message[] = {
		0x52, 0x65, 0x63, 0x65, 0x69, 0x70, 0x74, 0x20,
		0x56, 0x61, 0x6C, 0x69, 0x64, 0x61, 0x74, 0x69,
		0x6F, 0x6E
	};
	BYTE signature[512];
	DWORD signatureLength = sizeof(signature);

	_sntprintf(private_key_file, sizeof(private_key_file), _T("%s/%s"), exec_path, _T("ecc_private_key_test.pem"));
	_sntprintf(public_key_file, sizeof(public_key_file), _T("%s/%s"), exec_path, _T("ecc_public_key_test.pem"));

	status = calculate_signature(message, sizeof(message), private_key_file, NULL, signature, &signatureLength);
	assert_int_equal(status.errorStatus, OPGP_ERROR_STATUS_SUCCESS);

	status = validate_signature(message, sizeof(message), public_key_file, NULL, signature, signatureLength);
	assert_int_equal(status.errorStatus, OPGP_ERROR_STATUS_SUCCESS);

	message[0] ^= 0x01;
	status = validate_signature(message, sizeof(message), public_key_file, NULL, signature, signatureLength);
	assert_int_equal(status.errorStatus, OPGP_ERROR_STATUS_FAILURE);
	assert_int_equal(status.errorCode, OPGP_ERROR_VALIDATION_FAILED);
}

static int setup(void **state) {
	return 0;
}

int main(int argc, TCHAR *argv[]) {
	TCHAR *end_path;
	exec_path = argv[0];
	end_path = _tcsrchr(exec_path, _T('/'));
	exec_path[_tcslen(exec_path) - _tcslen(end_path)] = _T('\0');
	const struct CMUnitTest tests[] = {
			cmocka_unit_test(test_aes_cmac_128),
			cmocka_unit_test(test_aes_cmac_192),
			cmocka_unit_test(test_read_public_rsa_key),
			cmocka_unit_test(test_read_encrypted_private_rsa_key),
			cmocka_unit_test(test_validate_signature_rsa),
			cmocka_unit_test(test_validate_signature_ecc),
	};
	return cmocka_run_group_tests_name("crypto", tests, setup, NULL);
}
