/*
 * Copyright (C) 2020 - 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 "compare.h"
#include "lc_cshake.h"
#include "lc_sha3.h"
#include "test_helper_common.h"
#include "visibility.h"

#include "sha3_c.h"
#include "sha3_arm_asm.h"
#include "sha3_arm_ce.h"
#include "sha3_arm_neon.h"
#include "sha3_avx2.h"
#include "sha3_avx512.h"
#include "sha3_riscv_asm.h"

#define LC_EXEC_ONE_TEST(sha3_impl)                                            \
	if (sha3_impl)                                                         \
	ret += _shake_128_tester(sha3_impl, #sha3_impl)

static int _shake_128_tester(const struct lc_hash *shake_128, const char *name)
{
	static const uint8_t msg1[] = { 0xBE, 0x94, 0xD8, 0x3D, 0x37,
					0x66, 0xCF, 0x3E, 0xD3, 0x0A,
					0x11, 0x0C, 0x47, 0xA2 };
	static const uint8_t exp1[] = { 0xB0, 0x46, 0x01, 0xAA, 0x4D, 0x2C,
					0x30, 0xF6, 0x5F, 0x94, 0xD7, 0x02,
					0x5D, 0xBD, 0x22, 0x39 };
	uint8_t act1[sizeof(exp1)];
	static const uint8_t msg2[] = { 0xD7, 0x32, 0x8A, 0x5C, 0x57, 0xDB,
					0x97, 0xF3, 0x98, 0xC8, 0x4F, 0x68,
					0x2F, 0xE9, 0xCC, 0xA8 };
	static const uint8_t exp2[] = {
		0x02, 0x4B, 0x9D, 0x1B, 0x92, 0xEC, 0x6D, 0x85, 0x96, 0x58,
		0x31, 0x2F, 0xD3, 0xA7, 0xD8, 0x55, 0xDA, 0x88, 0x79, 0xC2,
		0x71, 0x47, 0xEE, 0xB4, 0xEF, 0x02, 0xE0, 0x51, 0x63, 0xB4,
		0x88, 0xC5, 0x49, 0x0A, 0x4B, 0x39, 0xAE, 0xBE, 0xB9, 0x61,
		0x94, 0x37, 0x48, 0xE4, 0x2E, 0xF8, 0x19, 0x6C, 0xD6, 0x15,
		0x17, 0xC1, 0x7C, 0x20, 0xF8, 0x24, 0xE1, 0x52, 0x8C, 0xFB,
		0x0E, 0x55, 0x3B, 0x04, 0xAB, 0x37, 0x73, 0x30, 0xA8, 0x0C,
		0x81, 0x97, 0xD8, 0xE1, 0xBF, 0x23, 0x29, 0x26, 0x66, 0xD2,
		0x36, 0xFF, 0x7E, 0xD3, 0xDD, 0xBA, 0x69, 0xBA, 0xEC, 0x05,
		0x09, 0x2E, 0x14, 0x1A, 0xF7, 0x9D, 0x8C, 0x91, 0xA5, 0xD2,
		0x9F, 0x59, 0x42, 0xBC, 0x8F, 0x8C, 0x80, 0xCE, 0x6A, 0xEA,
		0xAE, 0xDB, 0x69, 0x7A, 0x99, 0xCA, 0xCB, 0x63, 0xDA, 0xCE,
		0xFE, 0xD5, 0x5D, 0x07, 0x5D, 0x48, 0xFB, 0xEA, 0x0D, 0x95,
		0x87, 0x35, 0x5F, 0x5F, 0x25, 0x19, 0xF3, 0x74, 0xDA, 0xEE,
		0x63, 0x3F, 0xDE, 0xC6, 0xBE, 0xA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
	};
	uint8_t act2[sizeof(exp2)];
	int ret;
	LC_HASH_CTX_ON_STACK(ctx, shake_128);
	LC_SHAKE_128_CTX_ON_STACK(shake128_stack);

	printf("hash ctx %s (%s implementation) len %u\n", name,
	       shake_128 == lc_shake128_c ? "C" : "accelerated",
	       (unsigned int)LC_HASH_CTX_SIZE);

	if (lc_hash_init(ctx))
		return 1;
	lc_hash_update(ctx, msg1, sizeof(msg1));
	CKINT(lc_hash_set_digestsize(ctx, sizeof(act1)));
	lc_hash_final(ctx, act1);
	ret = lc_compare(act1, exp1, sizeof(act1), "SHAKE128 1");
	lc_hash_zero(ctx);

	if (ret)
		return ret;

	if (lc_hash_init(shake128_stack))
		return 1;
	lc_hash_update(shake128_stack, msg1, sizeof(msg1));
	CKINT(lc_hash_set_digestsize(shake128_stack, sizeof(act1)));
	lc_hash_final(shake128_stack, act1);
	ret = lc_compare(act1, exp1, sizeof(act1), "SHAKE128 1 - separate ctx");
	lc_hash_zero(shake128_stack);
	if (ret)
		return ret;

	if (lc_hash_init(ctx))
		return 1;
	lc_hash_update(ctx, msg2, sizeof(msg2));
	CKINT(lc_hash_set_digestsize(ctx, sizeof(act2)));
	lc_hash_final(ctx, act2);
	ret = lc_compare(act2, exp2, sizeof(act2), "SHAKE128 2");
	lc_hash_zero(ctx);

	if (ret)
		return ret;

out:
	return ret;
}

static int shake128_tester(void)
{
	int ret = 0;

	LC_EXEC_ONE_TEST(lc_shake128);
	LC_EXEC_ONE_TEST(lc_shake128_c);
	LC_EXEC_ONE_TEST(lc_shake128_arm_asm);
	LC_EXEC_ONE_TEST(lc_shake128_arm_ce);
	LC_EXEC_ONE_TEST(lc_shake128_arm_neon);
	LC_EXEC_ONE_TEST(lc_shake128_avx2);
	LC_EXEC_ONE_TEST(lc_shake128_avx512);
	LC_EXEC_ONE_TEST(lc_shake128_riscv_asm);
	LC_EXEC_ONE_TEST(lc_shake128_riscv_asm_zbb);

	return ret;
}

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

	(void)argc;
	(void)argv;

	ret = shake128_tester();

	ret = test_validate_status(ret, LC_ALG_STATUS_SHAKE, 1);
	ret += test_print_status();

	return ret;
}
