/*
 * Copyright (C) 2025 - 2026, Stephan Mueller <smueller@chronox.de>
 *
 * License: see LICENSE file in root directory
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
 * WHICH ARE HEREBY DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */

#include "aes_aesni.h"
#include "aes_armce.h"
#include "aes_c.h"
#include "aes_riscv64.h"
#include "aes_internal.h"
#include "lc_aes.h"
#include "lc_init.h"
#include "compare.h"
#include "ret_checkers.h"
#include "test_helper_common.h"
#include "timecop.h"
#include "visibility.h"

#define LC_EXEC_ONE_TEST(aes_impl)                                             \
	if (aes_impl)                                                          \
	ret += test_xts(aes_impl, #aes_impl)

/*
 * XTS test where the last block is a full AES block - i.e. no ciphertext
 * stealing
 */
static const uint8_t key256[] = {
	0x27, 0x18, 0x28, 0x18, 0x28, 0x45, 0x90, 0x45, 0x23, 0x53, 0x60,
	0x28, 0x74, 0x71, 0x35, 0x26, 0x62, 0x49, 0x77, 0x57, 0x24, 0x70,
	0x93, 0x69, 0x99, 0x59, 0x57, 0x49, 0x66, 0x96, 0x76, 0x27, 0x31,
	0x41, 0x59, 0x26, 0x53, 0x58, 0x97, 0x93, 0x23, 0x84, 0x62, 0x64,
	0x33, 0x83, 0x27, 0x95, 0x02, 0x88, 0x41, 0x97, 0x16, 0x93, 0x99,
	0x37, 0x51, 0x05, 0x82, 0x09, 0x74, 0x94, 0x45, 0x92
};
static const uint8_t pt256[] = {
	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
	0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
	0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
	0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
	0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53,
	0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
	0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b,
	0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
	0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
	0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
	0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b,
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2, 0x94, 0x52, 0x9f, 0x86, 0x14, 0xdc,
	0xd2, 0xab, 0xa9, 0x91, 0x92, 0x5f, 0xed, 0xc4, 0xae, 0x74, 0xff, 0xac,
	0x6e, 0x33, 0x3b, 0x93, 0xeb, 0x4a, 0xff, 0x04, 0x79, 0xda, 0x9a, 0x41,
	0x0e, 0x44, 0x50, 0xe0, 0xdd, 0x7a, 0xe4, 0xc6, 0xe2, 0x91, 0x09, 0x00,
	0x57, 0x5d, 0xa4, 0x01, 0xfc, 0x07, 0x05, 0x9f, 0x64, 0x5e, 0x8b, 0x7e,
	0x9b, 0xfd, 0xef, 0x33, 0x94, 0x30, 0x54, 0xff, 0x84, 0x01, 0x14, 0x93,
	0xc2, 0x7b, 0x34, 0x29, 0xea, 0xed, 0xb4, 0xed, 0x53, 0x76, 0x44, 0x1a,
	0x77, 0xed, 0x43, 0x85, 0x1a, 0xd7, 0x7f, 0x16, 0xf5, 0x41, 0xdf, 0xd2,
	0x69, 0xd5, 0x0d, 0x6a, 0x5f, 0x14, 0xfb, 0x0a, 0xab, 0x1c, 0xbb, 0x4c,
	0x15, 0x50, 0xbe, 0x97, 0xf7, 0xab, 0x40, 0x66, 0x19, 0x3c, 0x4c, 0xaa,
	0x77, 0x3d, 0xad, 0x38, 0x01, 0x4b, 0xd2, 0x09, 0x2f, 0xa7, 0x55, 0xc8,
	0x24, 0xbb, 0x5e, 0x54, 0xc4, 0xf3, 0x6f, 0xfd, 0xa9, 0xfc, 0xea, 0x70,
	0xb9, 0xc6, 0xe6, 0x93, 0xe1, 0x48, 0xc1, 0x51
};
static const uint8_t iv256[] = {
	0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
static const uint8_t expected_iv[] = { 0xd7, 0xcf, 0x75, 0x24, 0x5c, 0x26,
				       0x75, 0x77, 0xa9, 0x0c, 0xaa, 0xd6,
				       0xae, 0x98, 0x8e, 0x22 };

/*
 * XTS test where the last block is not a full AES block - i.e. ciphertext
 * stealing is applied
 */
static const uint8_t key256_2[] = {
	0x27, 0x18, 0x28, 0x18, 0x28, 0x45, 0x90, 0x45, 0x23, 0x53, 0x60,
	0x28, 0x74, 0x71, 0x35, 0x26, 0x62, 0x49, 0x77, 0x57, 0x24, 0x70,
	0x93, 0x69, 0x99, 0x59, 0x57, 0x49, 0x66, 0x96, 0x76, 0x27, 0x31,
	0x41, 0x59, 0x26, 0x53, 0x58, 0x97, 0x93, 0x23, 0x84, 0x62, 0x64,
	0x33, 0x83, 0x27, 0x95, 0x02, 0x88, 0x41, 0x97, 0x16, 0x93, 0x99,
	0x37, 0x51, 0x05, 0x82, 0x09, 0x74, 0x94, 0x45, 0x92
};
static const uint8_t pt256_2[] = {
	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
	0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
	0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
	0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
	0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
	0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41,
	0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c,
	0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
	0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61
};
static const uint8_t ct256_2[] = {
	0x3a, 0x06, 0x0a, 0x8c, 0xad, 0x11, 0x5a, 0x6f, 0x44, 0x57, 0x2e,
	0x37, 0x59, 0xe4, 0x3c, 0x8f, 0xca, 0xd8, 0xbf, 0xcb, 0x23, 0x3f,
	0xf6, 0xad, 0x71, 0xb7, 0xc1, 0xe7, 0xca, 0x65, 0x15, 0x08, 0x86,
	0x0a, 0xed, 0x34, 0xec, 0x95, 0x06, 0xd3, 0x68, 0xaa, 0x50, 0x27,
	0x4a, 0x31, 0xc1, 0x6d, 0x2d, 0xea, 0xe4, 0xd6, 0x4c, 0x2a, 0x80,
	0x96, 0x09, 0x1c, 0x09, 0x3f, 0x38, 0x20, 0xfb, 0x6d, 0x21, 0x08,
	0x9b, 0xce, 0xda, 0xac, 0x36, 0x1e, 0x3f, 0xeb, 0xe7, 0x06, 0xca,
	0xfe, 0x14, 0xb9, 0xbc, 0x89, 0xde, 0x34, 0x25, 0x8d, 0x32, 0xec,
	0x3a, 0xd5, 0x9f, 0xd2, 0x98, 0x6a, 0x40, 0x1e, 0x6b, 0xff
};
static const uint8_t iv256_2[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
				   0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
				   0x00, 0x00, 0x00, 0x00 };
static const uint8_t expiv_2[] = { 0x4e, 0xc6, 0x0b, 0x35, 0x9f, 0xe0,
				   0x7b, 0x7a, 0x80, 0xae, 0xb1, 0x0f,
				   0x4a, 0x3e, 0x17, 0x05 };

static int test_encrypt_xts_one(struct lc_sym_ctx *ctx, const uint8_t *key,
				size_t keylen, const uint8_t *pt, size_t ptlen,
				const uint8_t *ct, const uint8_t *iv,
				const uint8_t *expiv, size_t ivlen)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wvla"
	uint8_t out[ptlen], out2[ptlen], ivout[ivlen], extiv[ivlen];
#pragma GCC diagnostic pop
	size_t cutlen;
	int ret, rc;

	/*
	 * Test multi-staged XTS - the code below invokes XTS in stream mode.
	 * Yet, only the last block is allowed to be non-aligned for generating
	 * the ciphertext stealing.
	 */
	if (ptlen < 3 * AES_BLOCKLEN)
		return -EINVAL;

	/* Unpoison key to let implementation poison it */
	unpoison(key, keylen);

	/* Encrypt */
	CKINT(lc_sym_init(ctx));
	CKINT(lc_sym_setkey(ctx, key, keylen));
	CKINT(lc_sym_setiv(ctx, iv, ivlen));
	lc_sym_encrypt(ctx, pt, out, AES_BLOCKLEN);
	lc_sym_encrypt(ctx, pt + AES_BLOCKLEN, out + AES_BLOCKLEN,
		       2 * AES_BLOCKLEN);
	lc_sym_encrypt(ctx, pt + 3 * AES_BLOCKLEN, out + 3 * AES_BLOCKLEN,
		       ptlen - 3 * AES_BLOCKLEN);
	lc_sym_getiv(ctx, ivout, sizeof(ivout));
	rc = lc_compare(out, ct, ptlen, "AES-XTS encrypt ciphertext");
	rc += lc_compare(ivout, expiv, ivlen, "AES-XTS encrypt IV");

	/* Encrypt with external IV */
	memcpy(extiv, iv, ivlen);
	CKINT(lc_sym_init_iv(ctx, extiv, sizeof(extiv)));
	cutlen = ptlen & (AES_BLOCKLEN - 1);
	if (cutlen) {
		cutlen += AES_BLOCKLEN;
		CKINT(lc_sym_encrypt_iv(ctx, pt, out, ptlen - cutlen, extiv,
					sizeof(extiv)));
		CKINT(lc_sym_encrypt_iv(ctx, pt + ptlen - cutlen,
					out + ptlen - cutlen, cutlen, extiv,
					sizeof(extiv)));
	} else {
		CKINT(lc_sym_encrypt_iv(ctx, pt, out, AES_BLOCKLEN, extiv,
					sizeof(extiv)));
		CKINT(lc_sym_encrypt_iv(
			ctx, pt + AES_BLOCKLEN, out + AES_BLOCKLEN,
			ptlen - AES_BLOCKLEN, extiv, sizeof(extiv)));
	}
	rc += lc_compare(out, ct, ptlen,
			 "AES-XTS encrypt external IV ciphertext");
	unpoison(extiv, sizeof(extiv));
	rc += lc_compare(ivout, extiv, sizeof(extiv),
			 "AES-XTS encrypt external IV");

	/* Decrypt */
	CKINT(lc_sym_init(ctx));
	CKINT(lc_sym_setkey(ctx, key, keylen));
	CKINT(lc_sym_setiv(ctx, iv, ivlen));
	lc_sym_decrypt(ctx, out, out2, AES_BLOCKLEN);
	lc_sym_decrypt(ctx, out + AES_BLOCKLEN, out2 + AES_BLOCKLEN,
		       ptlen - AES_BLOCKLEN);
	lc_sym_getiv(ctx, ivout, sizeof(ivout));
	rc += lc_compare(out2, pt, ptlen, "AES-XTS decrypt plaintext");
	rc += lc_compare(expiv, ivout, ivlen, "AES-XTS decrypt IV");

	/* Decrypt with external IV */
	memcpy(extiv, iv, ivlen);
	CKINT(lc_sym_init_iv(ctx, extiv, sizeof(extiv)));
	cutlen = ptlen & (AES_BLOCKLEN - 1);
	if (cutlen) {
		cutlen += AES_BLOCKLEN;
		CKINT(lc_sym_decrypt_iv(ctx, out, out2, ptlen - cutlen, extiv,
					sizeof(extiv)));
		CKINT(lc_sym_decrypt_iv(ctx, out + ptlen - cutlen,
					out2 + ptlen - cutlen, cutlen, extiv,
					sizeof(extiv)));
	} else {
		CKINT(lc_sym_decrypt_iv(ctx, out, out2, AES_BLOCKLEN, extiv,
					sizeof(extiv)));
		CKINT(lc_sym_decrypt_iv(
			ctx, out + AES_BLOCKLEN, out2 + AES_BLOCKLEN,
			ptlen - AES_BLOCKLEN, extiv, sizeof(extiv)));
	}
	rc += lc_compare(out2, pt, ptlen,
			 "AES-XTS external IV decrypt plaintext");
	unpoison(extiv, sizeof(extiv));
	rc += lc_compare(extiv, expiv, sizeof(extiv),
			 "AES-XTS decrypt external IV");

out:
	return ret ? !!ret : !!rc;
}

static int test_xts(const struct lc_sym *aes, const char *name)
{
	int ret = 0;
	LC_SYM_CTX_ON_STACK(aes_xts, aes);

	printf("AES XTS ctx %s (%s implementation) len %u\n", name,
	       aes == lc_aes_xts_c ? "C" : "accelerated",
	       (unsigned int)LC_SYM_CTX_SIZE);

	ret += test_encrypt_xts_one(aes_xts, key256, sizeof(key256), pt256,
				    sizeof(pt256), ct256, iv256, expected_iv,
				    sizeof(iv256));

	ret += test_encrypt_xts_one(aes_xts, key256_2, sizeof(key256_2),
				    pt256_2, sizeof(pt256_2), ct256_2, iv256_2,
				    expiv_2, sizeof(iv256_2));

	lc_sym_zero(aes_xts);

	return ret;
}

static int test_xts_common(void)
{
	int ret = 0;
	LC_AES_XTS_CTX_ON_STACK(aes_xts);

	ret += test_encrypt_xts_one(aes_xts, key256, sizeof(key256), pt256,
				    sizeof(pt256), ct256, iv256, expected_iv,
				    sizeof(iv256));

	ret += test_encrypt_xts_one(aes_xts, key256_2, sizeof(key256_2),
				    pt256_2, sizeof(pt256_2), ct256_2, iv256_2,
				    expiv_2, sizeof(iv256_2));

	lc_sym_zero(aes_xts);

	return ret;
}

LC_TEST_FUNC(int, main, int argc, char *argv[])
{
	int ret = 0;

	(void)argv;

	if (argc >= 2)
		lc_init(LC_INIT_AES_SBOX);

	LC_EXEC_ONE_TEST(lc_aes_xts);
	LC_EXEC_ONE_TEST(lc_aes_xts_c);
	LC_EXEC_ONE_TEST(lc_aes_xts_aesni);
	LC_EXEC_ONE_TEST(lc_aes_xts_armce);
	LC_EXEC_ONE_TEST(lc_aes_xts_riscv64);

	ret += test_xts_common();

	if (!(lc_sym_alg_status(lc_aes_xts) &
	      (lc_alg_status_self_test_passed | lc_alg_status_fips_approved))) {
		printf("lc_sym_alg_status failure\n");
		ret++;
	} else {
		printf("lc_sym_alg_status pass\n");
	}

	ret = test_validate_status(ret, lc_sym_alg_status(lc_aes_xts), 1);
	ret = test_validate_status(ret, lc_sym_alg_status(lc_aes_xts_aesni),
				   lc_aes_xts == lc_aes_xts_aesni);
	ret = test_validate_status(ret, lc_sym_alg_status(lc_aes_xts_armce),
				   lc_aes_xts == lc_aes_xts_armce);
	ret = test_validate_status(ret, lc_sym_alg_status(lc_aes_xts_c),
				   lc_aes_xts == lc_aes_xts_c);
	ret = test_validate_status(ret, lc_sym_alg_status(lc_aes_xts_riscv64),
				   lc_aes_xts == lc_aes_xts_riscv64);
	ret += test_print_status();

	return ret;
}
