/*
 * Copyright (C) 2025 - 2026, Stephan Mueller <smueller@chronox.de>
 *
 * License: see COPYING 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 "chacha20_internal.h"
#include "chacha20_avx2.h"
#include "ext_headers_x86.h"
#include "lc_chacha20.h"
#include "lc_chacha20_private.h"
#include "lc_memset_secure.h"
#include "status_algorithms.h"
#include "lc_sym.h"
#include "visibility.h"

#include "asm/AVX2/chacha20_asm_avx2.h"

static int cc20_crypt_avx2(struct lc_sym_state *ctx, const uint8_t *in,
			   uint8_t *out, size_t len)
{
	int ret = cc20_check_overflow(ctx, len);

	if (ret)
		return ret;

	/*
	 * cc20_crypt_bytes_avx2 can handle the partial blocks, but we
	 * deliberately handle partial blocks here as we want to keep the
	 * unused keystream.
	 */
	cc20_crypt_remaining(ctx, &in, &out, &len);

	while (len > LC_CC20_BLOCK_SIZE) {
		size_t todo = len & ~(LC_CC20_BLOCK_SIZE - 1);
		size_t blocks = len / LC_CC20_BLOCK_SIZE;

		/*
		 * Identify a wrap of the counter and only perform the
		 * operation up to the wrap.
		 */
		if (ctx->counter[0] + blocks < ctx->counter[0]) {
			blocks = 0 - ctx->counter[0];
			todo = blocks * LC_CC20_BLOCK_SIZE;
		}

		LC_FPU_ENABLE;
		ret = cc20_crypt_bytes_avx2(ctx->key.u, in, out, todo);
		LC_FPU_DISABLE;

		if (ret) {
			lc_memset_secure(out, 0, len);
			return ret;
		}

		in += todo;
		out += todo;
		len -= todo;
	}

	if (len) {
		LC_FPU_ENABLE;
		ret = cc20_crypt_bytes_avx2(ctx->key.u, NULL, ctx->keystream.b,
					    LC_CC20_BLOCK_SIZE);
		LC_FPU_DISABLE;

		if (ret) {
			lc_memset_secure(out, 0, len);
			return ret;
		}

		if (in != out)
			memcpy(out, in, len);

		xor_64(out, ctx->keystream.b, len);

		/* When we are in this loop, the keystream_ptr was zero */
		ctx->keystream_ptr = (uint8_t)len;
	}

	return 0;
}

static int cc20_crypt_iv_avx2(const struct lc_sym_state *ctx, const uint8_t *in,
			      uint8_t *out, size_t len, uint8_t *iv,
			      size_t ivlen)
{
	struct lc_sym_state local_ctx = { 0 };
	int ret;

	CKNULL(ctx, -EINVAL);
	CKNULL(len, -EINVAL);

	/* Set up local context */
	cc20_init_constants(&local_ctx);
	CKINT(cc20_setiv(&local_ctx, iv, ivlen));
	memcpy(local_ctx.key.b, ctx->key.b, sizeof(local_ctx.key));
	cc20_resetkey(&local_ctx);

	/* Encrypt local context */
	CKINT(cc20_crypt_avx2(&local_ctx, in, out, len));

	/* Get the IV */
	CKINT(cc20_getiv(&local_ctx, iv, ivlen));

out:
	lc_memset_secure(&local_ctx, 0, sizeof(local_ctx));
	return ret;
}

static const struct lc_sym _lc_chacha20_avx2 = {
	.init = cc20_init,
	.setkey = cc20_setkey,
	.setiv = cc20_setiv,
	.getiv = cc20_getiv,
	.encrypt = cc20_crypt_avx2,
	.decrypt = cc20_crypt_avx2,

	.init_iv = cc20_init_iv,
	.encrypt_iv = cc20_crypt_iv_avx2,
	.decrypt_iv = cc20_crypt_iv_avx2,

	.statesize = LC_CC20_STATE_SIZE,
	.blocksize = 1,
	.algorithm_type = LC_ALG_STATUS_CHACHA20
};
LC_INTERFACE_SYMBOL(const struct lc_sym *,
		    lc_chacha20_avx2) = &_lc_chacha20_avx2;
