/*
 * 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_AESNI_X86_64_H
#define AES_AESNI_X86_64_H

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

#ifdef __cplusplus
extern "C" {
#endif

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

void SYSV_ABI aesni_encrypt(const uint8_t *pt, uint8_t *ct,
			    const struct aes_aesni_block_ctx *aes_ctx);
void SYSV_ABI aesni_decrypt(const uint8_t *ct, uint8_t *pt,
			    const struct aes_aesni_block_ctx *aes_ctx);

int SYSV_ABI aesni_set_encrypt_key(const uint8_t *key, const unsigned int bits,
				   struct aes_aesni_block_ctx *aes_ctx);
int SYSV_ABI aesni_set_decrypt_key(const uint8_t *key, const unsigned int bits,
				   struct aes_aesni_block_ctx *aes_ctx);

void SYSV_ABI aesni_ctr32_encrypt_blocks(
	const uint8_t *in, uint8_t *out, size_t blocks,
	const struct aes_aesni_block_ctx *aes_ctx, const uint8_t *iv);

void SYSV_ABI aesni_ecb_encrypt(const uint8_t *in, uint8_t *out, size_t length,
				const struct aes_aesni_block_ctx *aes_ctx,
				int enc);
void SYSV_ABI aesni_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length,
				const struct aes_aesni_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 SYSV_ABI aesni_xts_encrypt(const uint8_t *pt, uint8_t *ct, size_t length,
				const struct aes_aesni_block_ctx *key1,
				const struct aes_aesni_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 aesni_xts_encrypt
 * applies here as well.
 */
void SYSV_ABI aesni_xts_decrypt(const uint8_t *ct, uint8_t *pt, size_t length,
				const struct aes_aesni_block_ctx *key1,
				const struct aes_aesni_block_ctx *key2,
				const uint8_t iv[16]);

#if 0
void aesni_ccm64_encrypt_blocks(const uint8_t *pt, uint8_t *ct, size_t blocks,
				const void *key, const uint8_t iv[16],
				uint8_t cmac[16]);
void aesni_ccm64_decrypt_blocks(const uint8_t *ct, uint8_t *pt, size_t blocks,
				const void *key, const uint8_t iv[16],
				uint8_t cmac[16]);

void aesni_ocb_encrypt(const uint8_t *pt,uint8_t *ct,
		       size_t blocks, const void *key,
		       size_t start_block_num,
		       uint8_t offset_i[16],
		       const uint8_t L_[][16],
		       uint8_t checksum[16]);
void aesni_ocb_decrypt(const uint8_t *in, uint8_t *out,
		       size_t blocks, const void *key,
		       size_t start_block_num,
		       uint8_t offset_i[16],
		       const uint8_t L_[][16],
		       uint8_t checksum[16]);
#endif

#ifdef __cplusplus
}
#endif

#endif /* AES_AESNI_X86_64_H */
