blob: e2bf58a311b2de0798dbfae6396742db676b1b57 [file] [log] [blame]
Jerry Yu49231312023-01-10 16:57:21 +08001/**
2 * \file aesce.h
3 *
Dave Rodgmanf918d422023-03-17 17:52:23 +00004 * \brief Support hardware AES acceleration on Armv8-A processors with
Dave Rodgmanece803b2023-10-08 20:24:48 +01005 * the Armv8-A Cryptographic Extension.
Jerry Yu49231312023-01-10 16:57:21 +08006 *
7 * \warning These functions are only for internal use by other library
8 * functions; you must not call them directly.
9 */
10/*
11 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +000012 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Jerry Yu49231312023-01-10 16:57:21 +080013 */
14#ifndef MBEDTLS_AESCE_H
15#define MBEDTLS_AESCE_H
16
17#include "mbedtls/build_info.h"
Dave Rodgman410ad442023-11-28 13:42:17 +000018#include "common.h"
Jerry Yu49231312023-01-10 16:57:21 +080019
20#include "mbedtls/aes.h"
21
Jerry Yu07d28d82023-03-20 18:12:36 +080022
Dave Rodgman410ad442023-11-28 13:42:17 +000023#if defined(MBEDTLS_AESCE_C) && defined(MBEDTLS_ARCH_IS_ARMV8_A) && defined(__ARM_NEON) \
24 && (defined(MBEDTLS_COMPILER_IS_GCC) || defined(__clang__) || defined(MSC_VER))
Jerry Yu72fd0bd2023-08-18 16:31:01 +080025
Dave Rodgman410ad442023-11-28 13:42:17 +000026/* MBEDTLS_AESCE_HAVE_CODE is defined if we have a suitable target platform, and a
27 * potentially suitable compiler (compiler version & flags are not checked when defining
28 * this). */
Jerry Yu72fd0bd2023-08-18 16:31:01 +080029#define MBEDTLS_AESCE_HAVE_CODE
Jerry Yu49231312023-01-10 16:57:21 +080030
31#ifdef __cplusplus
32extern "C" {
33#endif
Jerry Yub95c7762023-01-10 16:59:51 +080034
Dave Rodgman45661322023-08-04 12:31:58 +010035#if defined(__linux__) && !defined(MBEDTLS_AES_USE_HARDWARE_ONLY)
36
Dave Rodgmanb30adce2023-08-04 12:52:51 +010037extern signed char mbedtls_aesce_has_support_result;
Dave Rodgman45661322023-08-04 12:31:58 +010038
Jerry Yub95c7762023-01-10 16:59:51 +080039/**
Jerry Yuc8bcdc82023-02-21 14:49:02 +080040 * \brief Internal function to detect the crypto extension in CPUs.
Jerry Yub95c7762023-01-10 16:59:51 +080041 *
42 * \return 1 if CPU has support for the feature, 0 otherwise
43 */
Dave Rodgman45661322023-08-04 12:31:58 +010044int mbedtls_aesce_has_support_impl(void);
Jerry Yu0d4f4e52023-03-31 14:32:47 +080045
Dave Rodgmanf2249ec2023-08-04 14:27:58 +010046#define MBEDTLS_AESCE_HAS_SUPPORT() (mbedtls_aesce_has_support_result == -1 ? \
Dave Rodgman45661322023-08-04 12:31:58 +010047 mbedtls_aesce_has_support_impl() : \
48 mbedtls_aesce_has_support_result)
49
50#else /* defined(__linux__) && !defined(MBEDTLS_AES_USE_HARDWARE_ONLY) */
51
52/* 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 */
Dave Rodgmanf2249ec2023-08-04 14:27:58 +010055#define MBEDTLS_AESCE_HAS_SUPPORT() 1
Dave Rodgman45661322023-08-04 12:31:58 +010056
57#endif /* defined(__linux__) && !defined(MBEDTLS_AES_USE_HARDWARE_ONLY) */
Jerry Yub95c7762023-01-10 16:59:51 +080058
Jerry Yu2bb3d812023-01-10 17:38:26 +080059/**
60 * \brief Internal AES-ECB block encryption and decryption
61 *
Dave Rodgman48fd2ab2023-06-16 09:36:50 +010062 * \warning This assumes that the context specifies either 10, 12 or 14
63 * rounds and will behave incorrectly if this is not the case.
Dave Rodgman96fdfb82023-06-15 16:21:31 +010064 *
Jerry Yu2bb3d812023-01-10 17:38:26 +080065 * \param ctx AES context
66 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
67 * \param input 16-byte input block
68 * \param output 16-byte output block
69 *
70 * \return 0 on success (cannot fail)
71 */
72int mbedtls_aesce_crypt_ecb(mbedtls_aes_context *ctx,
73 int mode,
74 const unsigned char input[16],
75 unsigned char output[16]);
76
Jerry Yu3f2fb712023-01-10 17:05:42 +080077/**
Jerry Yudf87a122023-01-10 18:17:15 +080078 * \brief Internal GCM multiplication: c = a * b in GF(2^128)
79 *
80 * \note This function is only for internal use by other library
81 * functions; you must not call it directly.
82 *
83 * \param c Result
84 * \param a First operand
85 * \param b Second operand
86 *
87 * \note Both operands and result are bit strings interpreted as
88 * elements of GF(2^128) as per the GCM spec.
89 */
90void mbedtls_aesce_gcm_mult(unsigned char c[16],
91 const unsigned char a[16],
92 const unsigned char b[16]);
93
94
Yanray Wangb67b4742023-10-31 17:10:32 +080095#if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
Jerry Yudf87a122023-01-10 18:17:15 +080096/**
Jerry Yue096da12023-01-10 17:07:01 +080097 * \brief Internal round key inversion. This function computes
98 * decryption round keys from the encryption round keys.
99 *
100 * \param invkey Round keys for the equivalent inverse cipher
101 * \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,
105 const unsigned char *fwdkey,
106 int nr);
Yanray Wangb67b4742023-10-31 17:10:32 +0800107#endif /* !MBEDTLS_BLOCK_CIPHER_NO_DECRYPT */
Jerry Yue096da12023-01-10 17:07:01 +0800108
109/**
Jerry Yu3f2fb712023-01-10 17:05:42 +0800110 * \brief Internal key expansion for encryption
111 *
112 * \param rk Destination buffer where the round keys are written
113 * \param key Encryption key
114 * \param bits Key size in bits (must be 128, 192 or 256)
115 *
116 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
117 */
118int mbedtls_aesce_setkey_enc(unsigned char *rk,
119 const unsigned char *key,
120 size_t bits);
121
Jerry Yu49231312023-01-10 16:57:21 +0800122#ifdef __cplusplus
123}
124#endif
125
Dave Rodgmanece803b2023-10-08 20:24:48 +0100126#else
127
Dave Rodgman9fd1b522023-10-10 15:23:44 +0100128#if defined(MBEDTLS_AES_USE_HARDWARE_ONLY) && defined(MBEDTLS_ARCH_IS_ARMV8_A)
Dave Rodgman410ad442023-11-28 13:42:17 +0000129#error "AES hardware acceleration not supported on this platform / compiler"
Dave Rodgmanece803b2023-10-08 20:24:48 +0100130#endif
131
Dave Rodgman410ad442023-11-28 13:42:17 +0000132#endif /* MBEDTLS_AESCE_C && MBEDTLS_ARCH_IS_ARMV8_A && __ARM_NEON &&
133 (MBEDTLS_COMPILER_IS_GCC || __clang__ || MSC_VER) */
Jerry Yu49231312023-01-10 16:57:21 +0800134
135#endif /* MBEDTLS_AESCE_H */