/**1* \file aesce.h2*3* \brief Support hardware AES acceleration on Armv8-A processors with4* the Armv8-A Cryptographic Extension.5*6* \warning These functions are only for internal use by other library7* functions; you must not call them directly.8*/9/*10* Copyright The Mbed TLS Contributors11* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later12*/13#ifndef MBEDTLS_AESCE_H14#define MBEDTLS_AESCE_H1516#include "mbedtls/build_info.h"17#include "common.h"1819#include "mbedtls/aes.h"202122#if defined(MBEDTLS_AESCE_C) \23&& defined(MBEDTLS_ARCH_IS_ARMV8_A) && defined(MBEDTLS_HAVE_NEON_INTRINSICS) \24&& (defined(MBEDTLS_COMPILER_IS_GCC) || defined(__clang__) || defined(MSC_VER))2526/* MBEDTLS_AESCE_HAVE_CODE is defined if we have a suitable target platform, and a27* potentially suitable compiler (compiler version & flags are not checked when defining28* this). */29#define MBEDTLS_AESCE_HAVE_CODE3031#ifdef __cplusplus32extern "C" {33#endif3435#if defined(__linux__) && !defined(MBEDTLS_AES_USE_HARDWARE_ONLY)3637extern signed char mbedtls_aesce_has_support_result;3839/**40* \brief Internal function to detect the crypto extension in CPUs.41*42* \return 1 if CPU has support for the feature, 0 otherwise43*/44int mbedtls_aesce_has_support_impl(void);4546#define MBEDTLS_AESCE_HAS_SUPPORT() (mbedtls_aesce_has_support_result == -1 ? \47mbedtls_aesce_has_support_impl() : \48mbedtls_aesce_has_support_result)4950#else /* defined(__linux__) && !defined(MBEDTLS_AES_USE_HARDWARE_ONLY) */5152/* If we are not on Linux, we can't detect support so assume that it's supported.53* Similarly, assume support if MBEDTLS_AES_USE_HARDWARE_ONLY is set.54*/55#define MBEDTLS_AESCE_HAS_SUPPORT() 15657#endif /* defined(__linux__) && !defined(MBEDTLS_AES_USE_HARDWARE_ONLY) */5859/**60* \brief Internal AES-ECB block encryption and decryption61*62* \warning This assumes that the context specifies either 10, 12 or 1463* rounds and will behave incorrectly if this is not the case.64*65* \param ctx AES context66* \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT67* \param input 16-byte input block68* \param output 16-byte output block69*70* \return 0 on success (cannot fail)71*/72int mbedtls_aesce_crypt_ecb(mbedtls_aes_context *ctx,73int mode,74const unsigned char input[16],75unsigned char output[16]);7677/**78* \brief Internal GCM multiplication: c = a * b in GF(2^128)79*80* \note This function is only for internal use by other library81* functions; you must not call it directly.82*83* \param c Result84* \param a First operand85* \param b Second operand86*87* \note Both operands and result are bit strings interpreted as88* elements of GF(2^128) as per the GCM spec.89*/90void mbedtls_aesce_gcm_mult(unsigned char c[16],91const unsigned char a[16],92const unsigned char b[16]);939495#if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)96/**97* \brief Internal round key inversion. This function computes98* decryption round keys from the encryption round keys.99*100* \param invkey Round keys for the equivalent inverse cipher101* \param fwdkey Original round keys (for encryption)102* \param nr Number of rounds (that is, number of round keys minus one)103*/104void mbedtls_aesce_inverse_key(unsigned char *invkey,105const unsigned char *fwdkey,106int nr);107#endif /* !MBEDTLS_BLOCK_CIPHER_NO_DECRYPT */108109/**110* \brief Internal key expansion for encryption111*112* \param rk Destination buffer where the round keys are written113* \param key Encryption key114* \param bits Key size in bits (must be 128, 192 or 256)115*116* \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH117*/118int mbedtls_aesce_setkey_enc(unsigned char *rk,119const unsigned char *key,120size_t bits);121122#ifdef __cplusplus123}124#endif125126#else127128#if defined(MBEDTLS_AES_USE_HARDWARE_ONLY) && defined(MBEDTLS_ARCH_IS_ARMV8_A)129#error "AES hardware acceleration not supported on this platform / compiler"130#endif131132#endif /* MBEDTLS_AESCE_C && MBEDTLS_ARCH_IS_ARMV8_A && MBEDTLS_HAVE_NEON_INTRINSICS &&133(MBEDTLS_COMPILER_IS_GCC || __clang__ || MSC_VER) */134135#endif /* MBEDTLS_AESCE_H */136137138