blob: 7916e522ce7881650670c3c2fba3bd833aab667d [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 Yu8f0e3d42023-04-25 10:24:53 +080034#if defined(__GNUC__) && defined(__aarch64__)
Jerry Yu49231312023-01-10 16:57:21 +080035#define MBEDTLS_HAVE_ARM64
36#endif
37
Jerry Yuf015a932023-04-25 10:38:03 +080038/* MSVC
39 * TODO: We haven't verified msvc from 1920 to 1928. If someone verified that,
40 * please update this and document of `MBEDTLS_AESCE_C` in
41 * `mbedtls_config.h`
42 */
Jerry Yu8f0e3d42023-04-25 10:24:53 +080043#if defined(_MSC_VER) && _MSC_VER >=1929 && \
44 (defined(_M_ARM64) || defined(_M_ARM64EC))
Jerry Yu07d28d82023-03-20 18:12:36 +080045#define MBEDTLS_HAVE_ARM64
46#endif
47#endif
48
49
Jerry Yu49231312023-01-10 16:57:21 +080050#if defined(MBEDTLS_HAVE_ARM64)
51
52#ifdef __cplusplus
53extern "C" {
54#endif
Jerry Yub95c7762023-01-10 16:59:51 +080055
56/**
Jerry Yuc8bcdc82023-02-21 14:49:02 +080057 * \brief Internal function to detect the crypto extension in CPUs.
Jerry Yub95c7762023-01-10 16:59:51 +080058 *
59 * \return 1 if CPU has support for the feature, 0 otherwise
60 */
61int mbedtls_aesce_has_support(void);
62
Jerry Yu2bb3d812023-01-10 17:38:26 +080063/**
64 * \brief Internal AES-ECB block encryption and decryption
65 *
66 * \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 */