blob: 91aa7d2bb347f98f229a760f05f7bb9f92c535a9 [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
12 * SPDX-License-Identifier: Apache-2.0
13 *
14 * Licensed under the Apache License, Version 2.0 (the "License"); you may
15 * not use this file except in compliance with the License.
16 * You may obtain a copy of the License at
17 *
18 * http://www.apache.org/licenses/LICENSE-2.0
19 *
20 * Unless required by applicable law or agreed to in writing, software
21 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
22 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 * See the License for the specific language governing permissions and
24 * limitations under the License.
25 */
26#ifndef MBEDTLS_AESCE_H
27#define MBEDTLS_AESCE_H
28
29#include "mbedtls/build_info.h"
30
31#include "mbedtls/aes.h"
32
Jerry Yu72fd0bd2023-08-18 16:31:01 +080033#include "common.h"
Jerry Yu07d28d82023-03-20 18:12:36 +080034
Jerry Yu72fd0bd2023-08-18 16:31:01 +080035#if defined(MBEDTLS_AESCE_C) && defined(MBEDTLS_ARCH_IS_ARM64)
36
37#define MBEDTLS_AESCE_HAVE_CODE
Jerry Yu49231312023-01-10 16:57:21 +080038
39#ifdef __cplusplus
40extern "C" {
41#endif
Jerry Yub95c7762023-01-10 16:59:51 +080042
Dave Rodgman45661322023-08-04 12:31:58 +010043#if defined(__linux__) && !defined(MBEDTLS_AES_USE_HARDWARE_ONLY)
44
Dave Rodgmanb30adce2023-08-04 12:52:51 +010045extern signed char mbedtls_aesce_has_support_result;
Dave Rodgman45661322023-08-04 12:31:58 +010046
Jerry Yub95c7762023-01-10 16:59:51 +080047/**
Jerry Yuc8bcdc82023-02-21 14:49:02 +080048 * \brief Internal function to detect the crypto extension in CPUs.
Jerry Yub95c7762023-01-10 16:59:51 +080049 *
50 * \return 1 if CPU has support for the feature, 0 otherwise
51 */
Dave Rodgman45661322023-08-04 12:31:58 +010052int mbedtls_aesce_has_support_impl(void);
Jerry Yu0d4f4e52023-03-31 14:32:47 +080053
Dave Rodgmanf2249ec2023-08-04 14:27:58 +010054#define MBEDTLS_AESCE_HAS_SUPPORT() (mbedtls_aesce_has_support_result == -1 ? \
Dave Rodgman45661322023-08-04 12:31:58 +010055 mbedtls_aesce_has_support_impl() : \
56 mbedtls_aesce_has_support_result)
57
58#else /* defined(__linux__) && !defined(MBEDTLS_AES_USE_HARDWARE_ONLY) */
59
60/* If we are not on Linux, we can't detect support so assume that it's supported.
61 * Similarly, assume support if MBEDTLS_AES_USE_HARDWARE_ONLY is set.
62 */
Dave Rodgmanf2249ec2023-08-04 14:27:58 +010063#define MBEDTLS_AESCE_HAS_SUPPORT() 1
Dave Rodgman45661322023-08-04 12:31:58 +010064
65#endif /* defined(__linux__) && !defined(MBEDTLS_AES_USE_HARDWARE_ONLY) */
Jerry Yub95c7762023-01-10 16:59:51 +080066
Jerry Yu2bb3d812023-01-10 17:38:26 +080067/**
68 * \brief Internal AES-ECB block encryption and decryption
69 *
Dave Rodgman48fd2ab2023-06-16 09:36:50 +010070 * \warning This assumes that the context specifies either 10, 12 or 14
71 * rounds and will behave incorrectly if this is not the case.
Dave Rodgman96fdfb82023-06-15 16:21:31 +010072 *
Jerry Yu2bb3d812023-01-10 17:38:26 +080073 * \param ctx AES context
74 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
75 * \param input 16-byte input block
76 * \param output 16-byte output block
77 *
78 * \return 0 on success (cannot fail)
79 */
80int mbedtls_aesce_crypt_ecb(mbedtls_aes_context *ctx,
81 int mode,
82 const unsigned char input[16],
83 unsigned char output[16]);
84
Jerry Yu3f2fb712023-01-10 17:05:42 +080085/**
Jerry Yudf87a122023-01-10 18:17:15 +080086 * \brief Internal GCM multiplication: c = a * b in GF(2^128)
87 *
88 * \note This function is only for internal use by other library
89 * functions; you must not call it directly.
90 *
91 * \param c Result
92 * \param a First operand
93 * \param b Second operand
94 *
95 * \note Both operands and result are bit strings interpreted as
96 * elements of GF(2^128) as per the GCM spec.
97 */
98void mbedtls_aesce_gcm_mult(unsigned char c[16],
99 const unsigned char a[16],
100 const unsigned char b[16]);
101
102
103/**
Jerry Yue096da12023-01-10 17:07:01 +0800104 * \brief Internal round key inversion. This function computes
105 * decryption round keys from the encryption round keys.
106 *
107 * \param invkey Round keys for the equivalent inverse cipher
108 * \param fwdkey Original round keys (for encryption)
109 * \param nr Number of rounds (that is, number of round keys minus one)
110 */
111void mbedtls_aesce_inverse_key(unsigned char *invkey,
112 const unsigned char *fwdkey,
113 int nr);
114
115/**
Jerry Yu3f2fb712023-01-10 17:05:42 +0800116 * \brief Internal key expansion for encryption
117 *
118 * \param rk Destination buffer where the round keys are written
119 * \param key Encryption key
120 * \param bits Key size in bits (must be 128, 192 or 256)
121 *
122 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
123 */
124int mbedtls_aesce_setkey_enc(unsigned char *rk,
125 const unsigned char *key,
126 size_t bits);
127
Jerry Yu49231312023-01-10 16:57:21 +0800128#ifdef __cplusplus
129}
130#endif
131
Jerry Yu72fd0bd2023-08-18 16:31:01 +0800132#endif /* MBEDTLS_AESCE_C && MBEDTLS_ARCH_IS_ARM64 */
Jerry Yu49231312023-01-10 16:57:21 +0800133
134#endif /* MBEDTLS_AESCE_H */