/* XDRBG with Ascon XOF support functions
 *
 * Copyright (C) 2024 - 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 "alignment.h"
#include "compare.h"
#include "fips_mode.h"
#include "lc_xdrbg.h"
#include "visibility.h"
#include "xdrbg_internal.h"

/********************************** Selftest **********************************/

static void xdrbg128_drng_selftest(void)
{
	LC_FIPS_RODATA_SECTION
	static const uint8_t seed[] = {
		FIPS140_MOD(0x00),
		0x01,
		0x02,
		0x03,
		0x04,
		0x05,
		0x06,
		0x07,
		0x08,
	};
	LC_FIPS_RODATA_SECTION
	static const uint8_t exp[] = {
		0x90, 0xfc, 0x06, 0x53, 0xfb, 0x53, 0xac, 0x68, 0xe9, 0x84,
		0xc5, 0x7f, 0x99, 0xdf, 0x5b, 0x33, 0x8d, 0xba, 0xcd, 0xcb,
		0x8b, 0x87, 0x07, 0x94, 0xe5, 0x36, 0x5e, 0x1c, 0xc1, 0x12,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		0x23, 0x29, 0xfe, 0x1c, 0x36, 0x67, 0xa2, 0x8f, 0x2d, 0xba,
		0x4b, 0x69, 0xfd, 0x24, 0x2d, 0x3a, 0x36, 0xc2, 0xea, 0x5e,
		0x65, 0x21, 0x44, 0x23, 0xfc, 0x25, 0x2b, 0x07, 0x1e, 0xcf,
		0x55, 0x92, 0x3e, 0x6a, 0x8a, 0x21, 0xb1
	};
	uint8_t act[sizeof(exp)] __align(sizeof(uint32_t));

	LC_SELFTEST_RUN(lc_xdrbg128_drng->algorithm_type);

	LC_XDRBG128_DRNG_CTX_ON_STACK(shake_ctx);

	if (lc_xdrbg_drng_seed_nocheck(shake_ctx->rng_state, seed, sizeof(seed),
				       NULL, 0))
		goto out;
	lc_rng_generate(shake_ctx, NULL, 0, act, sizeof(act));

out:
	lc_compare_selftest(lc_xdrbg128_drng->algorithm_type, act, exp,
			    sizeof(exp), "Ascon XOF XDRBG");
	lc_rng_zero(shake_ctx);
}

static int lc_xdrbg128_drng_seed(void *_state, const uint8_t *seed,
				 size_t seedlen, const uint8_t *alpha,
				 size_t alphalen)
{
	xdrbg128_drng_selftest();
	LC_SELFTEST_COMPLETED(lc_xdrbg128_drng->algorithm_type);

	return lc_xdrbg_drng_seed_nocheck(_state, seed, seedlen, alpha,
					  alphalen);
}

LC_INTERFACE_FUNCTION(int, lc_xdrbg128_drng_alloc, struct lc_rng_ctx **state)
{
	struct lc_rng_ctx *out_state = NULL;
	int ret;

	if (!state)
		return -EINVAL;

	ret = lc_alloc_aligned_secure((void *)&out_state,
				      LC_HASH_COMMON_ALIGNMENT,
				      LC_XDRBG128_DRNG_CTX_SIZE);
	if (ret)
		return -ret;

	LC_XDRBG128_RNG_CTX(out_state);

	*state = out_state;

	return 0;
}

static const struct lc_rng _lc_xdrbg128_drng = {
	.generate = lc_xdrbg_drng_generate,
	.seed = lc_xdrbg128_drng_seed,
	.zero = lc_xdrbg_drng_zero,
	.algorithm_type = LC_ALG_STATUS_XDRBG128,
};
LC_INTERFACE_SYMBOL(const struct lc_rng *,
		    lc_xdrbg128_drng) = &_lc_xdrbg128_drng;
