blob: 7e9c12a3cae6b7232572c744230b0d7866759986 [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 Yu07d28d82023-03-20 18:12:36 +080033#if !defined(MBEDTLS_HAVE_ARM64)
Jerry Yu61c4cfa2023-04-26 11:06:51 +080034#if defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC)
Jerry Yu07d28d82023-03-20 18:12:36 +080035#define MBEDTLS_HAVE_ARM64
Jerry Yu36606232023-04-19 10:44:29 +080036#if !defined(MBEDTLS_AESCE_C) && defined(MBEDTLS_AES_USE_HARDWARE_ONLY)
Jerry Yu8840a8c2023-04-19 10:18:50 +080037#error "MBEDTLS_AES_C defined, but not all prerequisites"
Jerry Yu4d030f32023-04-18 11:25:18 +080038#endif
Jerry Yu07d28d82023-03-20 18:12:36 +080039#endif
40#endif
41
Jerry Yu49231312023-01-10 16:57:21 +080042#if defined(MBEDTLS_HAVE_ARM64)
43
44#ifdef __cplusplus
45extern "C" {
46#endif
Jerry Yub95c7762023-01-10 16:59:51 +080047
48/**
Jerry Yuc8bcdc82023-02-21 14:49:02 +080049 * \brief Internal function to detect the crypto extension in CPUs.
Jerry Yub95c7762023-01-10 16:59:51 +080050 *
51 * \return 1 if CPU has support for the feature, 0 otherwise
52 */
Jerry Yu36606232023-04-19 10:44:29 +080053#if !defined(MBEDTLS_AES_USE_HARDWARE_ONLY)
Jerry Yub95c7762023-01-10 16:59:51 +080054int mbedtls_aesce_has_support(void);
Jerry Yu0d4f4e52023-03-31 14:32:47 +080055#else
56#define /* no-check-names */ mbedtls_aesce_has_support() 1
57#endif
58
Jerry Yub95c7762023-01-10 16:59:51 +080059
Jerry Yu2bb3d812023-01-10 17:38:26 +080060/**
61 * \brief Internal AES-ECB block encryption and decryption
62 *
Dave Rodgman48fd2ab2023-06-16 09:36:50 +010063 * \warning This assumes that the context specifies either 10, 12 or 14
64 * rounds and will behave incorrectly if this is not the case.
Dave Rodgman96fdfb82023-06-15 16:21:31 +010065 *
Jerry Yu2bb3d812023-01-10 17:38:26 +080066 * \param ctx AES context
67 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
68 * \param input 16-byte input block
69 * \param output 16-byte output block
70 *
71 * \return 0 on success (cannot fail)
72 */
73int mbedtls_aesce_crypt_ecb(mbedtls_aes_context *ctx,
74 int mode,
75 const unsigned char input[16],
76 unsigned char output[16]);
77
Jerry Yu3f2fb712023-01-10 17:05:42 +080078/**
Jerry Yudf87a122023-01-10 18:17:15 +080079 * \brief Internal GCM multiplication: c = a * b in GF(2^128)
80 *
81 * \note This function is only for internal use by other library
82 * functions; you must not call it directly.
83 *
84 * \param c Result
85 * \param a First operand
86 * \param b Second operand
87 *
88 * \note Both operands and result are bit strings interpreted as
89 * elements of GF(2^128) as per the GCM spec.
90 */
91void mbedtls_aesce_gcm_mult(unsigned char c[16],
92 const unsigned char a[16],
93 const unsigned char b[16]);
94
95
96/**
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);
107
108/**
Jerry Yu3f2fb712023-01-10 17:05:42 +0800109 * \brief Internal key expansion for encryption
110 *
111 * \param rk Destination buffer where the round keys are written
112 * \param key Encryption key
113 * \param bits Key size in bits (must be 128, 192 or 256)
114 *
115 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
116 */
117int mbedtls_aesce_setkey_enc(unsigned char *rk,
118 const unsigned char *key,
119 size_t bits);
120
Jerry Yu49231312023-01-10 16:57:21 +0800121#ifdef __cplusplus
122}
123#endif
124
125#endif /* MBEDTLS_HAVE_ARM64 */
126
127#endif /* MBEDTLS_AESCE_H */