/*
 * Copyright (C) 2023 - 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.
 */

#ifndef AES_ARMV8_CE_H
#define AES_ARMV8_CE_H

#include "../../aes_internal.h"
#include "ext_headers_internal.h"

#ifdef __cplusplus
extern "C" {
#endif

/* AES block algorithm context */
struct aes_v8_block_ctx {
	uint8_t RoundKey[240];
	uint32_t rounds;
};

int aes_v8_set_encrypt_key(const uint8_t *key, const unsigned int bits,
			   struct aes_v8_block_ctx *aes_ctx);
int aes_v8_set_decrypt_key(const uint8_t *userKey, const unsigned int bits,
			   struct aes_v8_block_ctx *aes_ctx);

void aes_v8_encrypt(const uint8_t *pt, uint8_t *ct,
		    const struct aes_v8_block_ctx *aes_ctx);
void aes_v8_decrypt(const uint8_t *ct, uint8_t *pt,
		    const struct aes_v8_block_ctx *aes_ctx);

void aes_v8_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, size_t blocks,
				 const struct aes_v8_block_ctx *aes_ctx,
				 const uint8_t *iv);

void aes_v8_ecb_encrypt(const uint8_t *in, uint8_t *out, size_t length,
			const struct aes_v8_block_ctx *aes_ctx, int enc);
void aes_v8_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length,
			const struct aes_v8_block_ctx *aes_ctx, uint8_t *iv,
			int enc);

/**
 * @brief XTS encryption operation
 *
 * @param [in] pt Plaintext to be encrypted
 * @param [out] ct Ciphertext to be generated - can be the same buffer as \p pt
 * @param [in] length Size of the \p pt and \p ct buffers
 * @param [in] key1 AES encryption key
 * @param [in] key2 XTS tweak encryption key - currently unused (see note below)
 * @param [in] iv IV for the XTS operation (see note below)
 *
 * \note This implementation currently requires the IV to be already tweaked!
 * I.e. it has to be encrypted with the tweak key already before calling this
 * function. Therefore \p key2 is currently unused. You could re-enable the
 * case where the ASM implementation tweaks the key by removing the
 * unconditional jump at the beginning of the function.
 */
void aes_v8_xts_encrypt(const uint8_t *pt, uint8_t *ct, size_t length,
			const struct aes_v8_block_ctx *key1,
			const struct aes_v8_block_ctx *key2,
			const uint8_t iv[16]);

/**
 * @brief XTS decryption operation
 *
 * @param [in] ct Ciphertext to be decrypted
 * @param [out] pt Plaintext to be generated - can be the same buffer as \p ct
 * @param [in] length Size of the \p pt and \p ct buffers
 * @param [in] key1 AES encryption key
 * @param [in] key2 XTS tweak encryption key - currently unused (see note below)
 * @param [in] iv IV for the XTS operation (see note below)
 *
 * \note The note regarding the tweaked IV given for \p aes_v8_xts_encrypt
 * applies here as well.
 */
void aes_v8_xts_decrypt(const uint8_t *ct, uint8_t *pt, size_t length,
			const struct aes_v8_block_ctx *key1,
			const struct aes_v8_block_ctx *key2,
			const uint8_t iv[16]);

#ifdef __cplusplus
}
#endif

#endif /* AES_ARMV8_CE_H */
