blob: 9206a6b3b18eeaf7e337e4ea96da20a2a6163b90 [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
5 * the Armv8-A Cryptographic Extension in AArch64 execution state.
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"
18
19#include "mbedtls/aes.h"
20
Jerry Yu07d28d82023-03-20 18:12:36 +080021
Jerry Yu72fd0bd2023-08-18 16:31:01 +080022#if defined(MBEDTLS_AESCE_C) && defined(MBEDTLS_ARCH_IS_ARM64)
23
24#define MBEDTLS_AESCE_HAVE_CODE
Jerry Yu49231312023-01-10 16:57:21 +080025
26#ifdef __cplusplus
27extern "C" {
28#endif
Jerry Yub95c7762023-01-10 16:59:51 +080029
Dave Rodgman45661322023-08-04 12:31:58 +010030#if defined(__linux__) && !defined(MBEDTLS_AES_USE_HARDWARE_ONLY)
31
Dave Rodgmanb30adce2023-08-04 12:52:51 +010032extern signed char mbedtls_aesce_has_support_result;
Dave Rodgman45661322023-08-04 12:31:58 +010033
Jerry Yub95c7762023-01-10 16:59:51 +080034/**
Jerry Yuc8bcdc82023-02-21 14:49:02 +080035 * \brief Internal function to detect the crypto extension in CPUs.
Jerry Yub95c7762023-01-10 16:59:51 +080036 *
37 * \return 1 if CPU has support for the feature, 0 otherwise
38 */
Dave Rodgman45661322023-08-04 12:31:58 +010039int mbedtls_aesce_has_support_impl(void);
Jerry Yu0d4f4e52023-03-31 14:32:47 +080040
Dave Rodgmanf2249ec2023-08-04 14:27:58 +010041#define MBEDTLS_AESCE_HAS_SUPPORT() (mbedtls_aesce_has_support_result == -1 ? \
Dave Rodgman45661322023-08-04 12:31:58 +010042 mbedtls_aesce_has_support_impl() : \
43 mbedtls_aesce_has_support_result)
44
45#else /* defined(__linux__) && !defined(MBEDTLS_AES_USE_HARDWARE_ONLY) */
46
47/* If we are not on Linux, we can't detect support so assume that it's supported.
48 * Similarly, assume support if MBEDTLS_AES_USE_HARDWARE_ONLY is set.
49 */
Dave Rodgmanf2249ec2023-08-04 14:27:58 +010050#define MBEDTLS_AESCE_HAS_SUPPORT() 1
Dave Rodgman45661322023-08-04 12:31:58 +010051
52#endif /* defined(__linux__) && !defined(MBEDTLS_AES_USE_HARDWARE_ONLY) */
Jerry Yub95c7762023-01-10 16:59:51 +080053
Jerry Yu2bb3d812023-01-10 17:38:26 +080054/**
55 * \brief Internal AES-ECB block encryption and decryption
56 *
Dave Rodgman48fd2ab2023-06-16 09:36:50 +010057 * \warning This assumes that the context specifies either 10, 12 or 14
58 * rounds and will behave incorrectly if this is not the case.
Dave Rodgman96fdfb82023-06-15 16:21:31 +010059 *
Jerry Yu2bb3d812023-01-10 17:38:26 +080060 * \param ctx AES context
61 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
62 * \param input 16-byte input block
63 * \param output 16-byte output block
64 *
65 * \return 0 on success (cannot fail)
66 */
67int mbedtls_aesce_crypt_ecb(mbedtls_aes_context *ctx,
68 int mode,
69 const unsigned char input[16],
70 unsigned char output[16]);
71
Jerry Yu3f2fb712023-01-10 17:05:42 +080072/**
Jerry Yudf87a122023-01-10 18:17:15 +080073 * \brief Internal GCM multiplication: c = a * b in GF(2^128)
74 *
75 * \note This function is only for internal use by other library
76 * functions; you must not call it directly.
77 *
78 * \param c Result
79 * \param a First operand
80 * \param b Second operand
81 *
82 * \note Both operands and result are bit strings interpreted as
83 * elements of GF(2^128) as per the GCM spec.
84 */
85void mbedtls_aesce_gcm_mult(unsigned char c[16],
86 const unsigned char a[16],
87 const unsigned char b[16]);
88
89
90/**
Jerry Yue096da12023-01-10 17:07:01 +080091 * \brief Internal round key inversion. This function computes
92 * decryption round keys from the encryption round keys.
93 *
94 * \param invkey Round keys for the equivalent inverse cipher
95 * \param fwdkey Original round keys (for encryption)
96 * \param nr Number of rounds (that is, number of round keys minus one)
97 */
98void mbedtls_aesce_inverse_key(unsigned char *invkey,
99 const unsigned char *fwdkey,
100 int nr);
101
102/**
Jerry Yu3f2fb712023-01-10 17:05:42 +0800103 * \brief Internal key expansion for encryption
104 *
105 * \param rk Destination buffer where the round keys are written
106 * \param key Encryption key
107 * \param bits Key size in bits (must be 128, 192 or 256)
108 *
109 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
110 */
111int mbedtls_aesce_setkey_enc(unsigned char *rk,
112 const unsigned char *key,
113 size_t bits);
114
Jerry Yu49231312023-01-10 16:57:21 +0800115#ifdef __cplusplus
116}
117#endif
118
Jerry Yu72fd0bd2023-08-18 16:31:01 +0800119#endif /* MBEDTLS_AESCE_C && MBEDTLS_ARCH_IS_ARM64 */
Jerry Yu49231312023-01-10 16:57:21 +0800120
121#endif /* MBEDTLS_AESCE_H */