/*
 * 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 "lc_ctr_drbg.h"
#include "compare.h"
#include "test_helper_common.h"
#include "visibility.h"

static int
ctr_drbg_tester(struct lc_rng_ctx *drbg, struct lc_rng_ctx *drbg_stack,
		const uint8_t *ent_nonce, size_t ent_nonce_len,
		const uint8_t *pers, size_t pers_len, const uint8_t *addtl1,
		size_t addtl1_len, const uint8_t *addtl2, size_t addtl2_len,
		const uint8_t *reseed_ent_nonce, size_t reseed_ent_nonce_len,
		const uint8_t *reseed_addtl, size_t reseed_addtl_len,
		const uint8_t *exp, uint8_t *act, size_t exp_len)
{
	int ret = 0;

	CKINT(lc_rng_seed(drbg_stack, ent_nonce, ent_nonce_len, pers,
			  pers_len));
	if (reseed_ent_nonce) {
		CKINT(lc_rng_seed(drbg_stack, reseed_ent_nonce,
				  reseed_ent_nonce_len, reseed_addtl,
				  reseed_addtl_len));
	}
	CKINT(lc_rng_generate(drbg_stack, addtl1, addtl1_len, act, exp_len));
	CKINT(lc_rng_generate(drbg_stack, addtl2, addtl2_len, act, exp_len));
	lc_rng_zero(drbg_stack);
	ret = lc_compare(act, exp, exp_len, "CTR DRBG AES-256 1");
	if (ret)
		goto out;

	/* Rerun to verify that drbg_zero works properly */
	CKINT(lc_rng_seed(drbg_stack, ent_nonce, ent_nonce_len, pers,
			  pers_len));
	if (reseed_ent_nonce) {
		CKINT(lc_rng_seed(drbg_stack, reseed_ent_nonce,
				  reseed_ent_nonce_len, reseed_addtl,
				  reseed_addtl_len));
	}

	CKINT(lc_rng_generate(drbg_stack, addtl1, addtl1_len, act, exp_len));
	CKINT(lc_rng_generate(drbg_stack, addtl2, addtl2_len, act, exp_len));
	lc_rng_zero(drbg_stack);
	ret = lc_compare(act, exp, exp_len, "CTR DRBG AES-256 2");
	if (ret)
		goto out;

	CKINT(lc_rng_seed(drbg, ent_nonce, ent_nonce_len, pers, pers_len));
	if (reseed_ent_nonce) {
		CKINT(lc_rng_seed(drbg, reseed_ent_nonce, reseed_ent_nonce_len,
				  reseed_addtl, reseed_addtl_len));
	}
	CKINT(lc_rng_generate(drbg, addtl1, addtl1_len, act, exp_len));
	CKINT(lc_rng_generate(drbg, addtl2, addtl2_len, act, exp_len));
	ret = lc_compare(act, exp, exp_len, "CTR DRBG AES-256 3");

out:
#ifdef LC_FIPS140_DEBUG
	if (ret == -EOPNOTSUPP)
		ret = 0;
#endif
	return ret;
}

/*
 * All test vectors obtained from successful ACVP test run
 */
static int ctr_drbg_df_tester_1(void)
{
	static const uint8_t ent_nonce[] = {
		0x36, 0x40, 0x19, 0x40, 0xfa, 0x8b, 0x1f, 0xba, 0x91, 0xa1,
		0x66, 0x1f, 0x21, 0x1d, 0x78, 0xa0, 0xb9, 0x38, 0x9a, 0x74,
		0xe5, 0xbc, 0xcf, 0xec, 0xe8, 0xd7, 0x66, 0xaf, 0x1a, 0x6d,
		0x3b, 0x14, 0x49, 0x6f, 0x25, 0xb0, 0xf1, 0x30, 0x1b, 0x4f,
		0x50, 0x1b, 0xe3, 0x03, 0x80, 0xa1, 0x37, 0xeb,
	};
	static const uint8_t exp[] = {
		0x58, 0x62, 0xeb, 0x38, 0xbd, 0x55, 0x8d, 0xd9, 0x78, 0xa6,
		0x96, 0xe6, 0xdf, 0x16, 0x47, 0x82, 0xdd, 0xd8, 0x87, 0xe7,
		0xe9, 0xa6, 0xc9, 0xf3, 0xf1, 0xfb, 0xaf, 0xb7, 0x89, 0x41,
		0xb5, 0x35, 0xa6, 0x49, 0x12, 0xdf, 0xd2, 0x24, 0xc6, 0xdc,
		0x74, 0x54, 0xe5, 0x25, 0x0b, 0x3d, 0x97, 0x16, 0x5e, 0x16,
		0x26, 0x0c, 0x2f, 0xaf, 0x1c, 0xc7, 0x73, 0x5c, 0xb7, 0x5f,
		0xb4, 0xf0, 0x7e, 0x1d
	};
	uint8_t act[sizeof(exp)];
	struct lc_rng_ctx *drbg = NULL;
	int ret;
	LC_DRBG_CTR_USE_DF_CTX_ON_STACK(drbg_stack);

	printf("ctr DRBG DF ctx len %lu\n", LC_DRBG_CTR_CTX_SIZE_USE_DF);

	CKINT(lc_drbg_ctr_use_df_alloc(&drbg));

	ret = ctr_drbg_tester(drbg, drbg_stack, ent_nonce, sizeof(ent_nonce),
			      NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, exp,
			      act, sizeof(exp));

out:
	lc_rng_zero_free(drbg);
	return ret;
}

static int ctr_drbg_df_tester_2(void)
{
	static const uint8_t ent_nonce[] = {
		0xDF, 0xCA, 0x2B, 0xD1, 0x9D, 0x55, 0xCB, 0xE4, 0xEF, 0xA5,
		0x22, 0x92, 0x0F, 0x5F, 0x17, 0x7E, 0xA8, 0x80, 0x2A, 0xCD,
		0x32, 0xF9, 0xC4, 0x0B, 0x85, 0xE4, 0x1E, 0xF5, 0x68, 0x94,
		0x64, 0x94, 0xDB, 0x0E, 0x17, 0x77, 0x98, 0x12, 0x47, 0x0F,
		0x36, 0x7B, 0x1C, 0xC5, 0x89, 0xEE, 0xC7, 0xEA
	};
	static const uint8_t addtl1[] = { 0xCA, 0x82, 0xF8, 0xC7, 0xA0, 0xCF,
					  0x40, 0x5D, 0xE5, 0x61, 0x6C, 0xCB,
					  0x8D, 0xA0, 0xF4, 0x4E, 0x51, 0x0D,
					  0xCB, 0x59, 0x58, 0x92, 0x6D, 0xD5,
					  0x7B, 0x72, 0xA8, 0xFB, 0xA8, 0x89,
					  0x58, 0xAE };
	static const uint8_t addtl2[] = { 0xDD, 0xB4, 0xD2, 0xD8, 0xBB, 0x4A,
					  0xA4, 0xF7, 0x41, 0xC4, 0xE4, 0xE5,
					  0x64, 0x02, 0x5F, 0x0C, 0x6A, 0xAB,
					  0x81, 0xD7, 0x4A, 0xF4, 0x32, 0x7A,
					  0x59, 0xE5, 0x18, 0xD6, 0xF0, 0x06,
					  0xCF, 0x69 };
	static const uint8_t reseed_ent_nonce[] = {
		0x19, 0x37, 0x7A, 0x7B, 0xA2, 0xD6, 0x0F, 0xB0, 0x56, 0x80,
		0xB7, 0x32, 0xE3, 0x49, 0x1F, 0xF4, 0x2C, 0xAE, 0x40, 0xDF,
		0xD8, 0x72, 0xE5, 0x09, 0xEC, 0x28, 0x2F, 0x60, 0x61, 0x6F,
		0xB6, 0xBC, 0x0C, 0x4D, 0x01, 0xEA, 0xA3, 0x1A, 0xAF, 0x21,
		0x9F, 0x38, 0xBB, 0x99, 0x68, 0xC7, 0x79, 0x0A
	};
	static const uint8_t reseed_addtl[] = {
		0x8F, 0x71, 0x86, 0x58, 0x51, 0xB0, 0x19, 0x36,
		0x04, 0xD9, 0x69, 0xB7, 0xCA, 0xAE, 0xED, 0x01,
		0xE1, 0xC0, 0x5F, 0x49, 0xB4, 0x5E, 0xDA, 0x51,
		0xDB, 0x49, 0x78, 0xB9, 0x06, 0x44, 0x36, 0x4F
	};
	static const uint8_t exp[] = { 0x8d, 0xba, 0x3a, 0xb3, 0x17, 0xad, 0xfe,
				       0x54, 0x37, 0x63, 0x78, 0x19, 0xae, 0x07,
				       0x76, 0xb2, 0x00, 0x66, 0xc3, 0x80, 0x8d,
				       0x83, 0x14, 0x9f, 0xd1, 0x51, 0x0a, 0x8d,
				       0x4c, 0xb7, 0x66, 0x3d };
	uint8_t act[sizeof(exp)];
	struct lc_rng_ctx *drbg = NULL;
	int ret;
	LC_DRBG_CTR_USE_DF_CTX_ON_STACK(drbg_stack);

	CKINT(lc_drbg_ctr_use_df_alloc(&drbg));

	ret = ctr_drbg_tester(drbg, drbg_stack, ent_nonce, sizeof(ent_nonce),
			      NULL, 0, addtl1, sizeof(addtl1), addtl2,
			      sizeof(addtl2), reseed_ent_nonce,
			      sizeof(reseed_ent_nonce), reseed_addtl,
			      sizeof(reseed_addtl), exp, act, sizeof(exp));

out:
	lc_rng_zero_free(drbg);
	return ret;
}

static int ctr_drbg_nodf_tester_1(void)
{
	static const uint8_t ent_nonce[] = {
		0xA3, 0x80, 0xEA, 0x91, 0x40, 0xF0, 0x96, 0x6D, 0xB7, 0x7E,
		0x52, 0xA5, 0x2B, 0x40, 0xCE, 0x66, 0x39, 0x36, 0xEB, 0x40,
		0x31, 0x60, 0x68, 0x17, 0xF8, 0x8A, 0x3C, 0x30, 0x98, 0x39,
		0x84, 0x96, 0xC0, 0x27, 0x1C, 0xA5, 0x77, 0xD3, 0xCD, 0x93,
		0xFD, 0xCA, 0xD9, 0x56, 0xD7, 0x00, 0xB8, 0xAB
	};
	static const uint8_t perso[] = { 0x2D, 0x67, 0x49, 0x4B, 0x9A, 0x2B,
					 0x81, 0x06, 0x44, 0x1B, 0xFB, 0xDC,
					 0xD7, 0x11, 0x9A, 0x97, 0xA7, 0xBC,
					 0x98, 0x78, 0xED, 0xBF, 0x07, 0x2E,
					 0x6F, 0xF6, 0x1A, 0x7D, 0xA4, 0x5B,
					 0x05, 0x52 };
	static const uint8_t reseed_ent_nonce[] = {
		0x27, 0xB3, 0xB6, 0x29, 0xF2, 0xC6, 0x7A, 0xC0, 0x25, 0x62,
		0x65, 0x0D, 0x83, 0x94, 0x37, 0x90, 0xD4, 0x29, 0xA5, 0xE4,
		0xEC, 0xF5, 0x19, 0xF0, 0x5F, 0xDD, 0x06, 0xAB, 0x21, 0x8D,
		0x7A, 0x5E, 0x98, 0xBF, 0xC5, 0xD0, 0xCA, 0x59, 0xBF, 0x65,
		0x0C, 0x09, 0x9B, 0x91, 0xC4, 0x2C, 0x70, 0x04
	};
	static const uint8_t exp[] = { 0xec, 0x60, 0xec, 0x1a, 0x7f, 0x28, 0x17,
				       0xb0, 0xcc, 0xc2, 0x73, 0x05, 0x69, 0x51,
				       0x75, 0xeb, 0xb2, 0x9f, 0xc6, 0x54, 0x9f,
				       0x05, 0x9c, 0x1c, 0x63, 0x95, 0x16, 0x0d,
				       0x09, 0x85, 0xee, 0xdf };
	uint8_t act[sizeof(exp)];
	struct lc_rng_ctx *drbg = NULL;
	int ret;
	LC_DRBG_CTR_NO_DF_CTX_ON_STACK(drbg_stack);

	printf("ctr DRBG NO DF ctx len %lu\n", LC_DRBG_CTR_CTX_SIZE_NO_DF);

	CKINT(lc_drbg_ctr_no_df_alloc(&drbg));

	ret = ctr_drbg_tester(drbg, drbg_stack, ent_nonce, sizeof(ent_nonce),
			      perso, sizeof(perso), NULL, 0, NULL, 0,
			      reseed_ent_nonce, sizeof(reseed_ent_nonce), NULL,
			      0, exp, act, sizeof(exp));

out:
	lc_rng_zero_free(drbg);
	return ret;
}

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

	(void)argc;
	(void)argv;

	ret += ctr_drbg_df_tester_1();
	ret += ctr_drbg_df_tester_2();
	ret += ctr_drbg_nodf_tester_1();

	ret = test_validate_status(ret, lc_rng_alg_status(lc_ctr_drbg), 0);
	ret = test_validate_status(ret, lc_sym_alg_status(lc_aes_ctr), 1);
	ret += test_print_status();

	return ret;
}
