xref: /optee_os/lib/libmbedtls/mbedtls/library/aesni.h (revision 32b3180828fa15a49ccc86ecb4be9d274c140c89)
1*32b31808SJens Wiklander /**
2*32b31808SJens Wiklander  * \file aesni.h
3*32b31808SJens Wiklander  *
4*32b31808SJens Wiklander  * \brief AES-NI for hardware AES acceleration on some Intel processors
5*32b31808SJens Wiklander  *
6*32b31808SJens Wiklander  * \warning These functions are only for internal use by other library
7*32b31808SJens Wiklander  *          functions; you must not call them directly.
8*32b31808SJens Wiklander  */
9*32b31808SJens Wiklander /*
10*32b31808SJens Wiklander  *  Copyright The Mbed TLS Contributors
11*32b31808SJens Wiklander  *  SPDX-License-Identifier: Apache-2.0
12*32b31808SJens Wiklander  *
13*32b31808SJens Wiklander  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
14*32b31808SJens Wiklander  *  not use this file except in compliance with the License.
15*32b31808SJens Wiklander  *  You may obtain a copy of the License at
16*32b31808SJens Wiklander  *
17*32b31808SJens Wiklander  *  http://www.apache.org/licenses/LICENSE-2.0
18*32b31808SJens Wiklander  *
19*32b31808SJens Wiklander  *  Unless required by applicable law or agreed to in writing, software
20*32b31808SJens Wiklander  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21*32b31808SJens Wiklander  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22*32b31808SJens Wiklander  *  See the License for the specific language governing permissions and
23*32b31808SJens Wiklander  *  limitations under the License.
24*32b31808SJens Wiklander  */
25*32b31808SJens Wiklander #ifndef MBEDTLS_AESNI_H
26*32b31808SJens Wiklander #define MBEDTLS_AESNI_H
27*32b31808SJens Wiklander 
28*32b31808SJens Wiklander #include "mbedtls/build_info.h"
29*32b31808SJens Wiklander 
30*32b31808SJens Wiklander #include "mbedtls/aes.h"
31*32b31808SJens Wiklander 
32*32b31808SJens Wiklander #define MBEDTLS_AESNI_AES      0x02000000u
33*32b31808SJens Wiklander #define MBEDTLS_AESNI_CLMUL    0x00000002u
34*32b31808SJens Wiklander 
35*32b31808SJens Wiklander /* Can we do AESNI with inline assembly?
36*32b31808SJens Wiklander  * (Only implemented with gas syntax, only for 64-bit.)
37*32b31808SJens Wiklander  */
38*32b31808SJens Wiklander #if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && \
39*32b31808SJens Wiklander     (defined(__amd64__) || defined(__x86_64__))   &&  \
40*32b31808SJens Wiklander     !defined(MBEDTLS_HAVE_X86_64)
41*32b31808SJens Wiklander #define MBEDTLS_HAVE_X86_64
42*32b31808SJens Wiklander #endif
43*32b31808SJens Wiklander 
44*32b31808SJens Wiklander #if defined(MBEDTLS_AESNI_C)
45*32b31808SJens Wiklander 
46*32b31808SJens Wiklander /* Can we do AESNI with intrinsics?
47*32b31808SJens Wiklander  * (Only implemented with certain compilers, only for certain targets.)
48*32b31808SJens Wiklander  */
49*32b31808SJens Wiklander #undef MBEDTLS_AESNI_HAVE_INTRINSICS
50*32b31808SJens Wiklander #if defined(_MSC_VER)
51*32b31808SJens Wiklander /* Visual Studio supports AESNI intrinsics since VS 2008 SP1. We only support
52*32b31808SJens Wiklander  * VS 2013 and up for other reasons anyway, so no need to check the version. */
53*32b31808SJens Wiklander #define MBEDTLS_AESNI_HAVE_INTRINSICS
54*32b31808SJens Wiklander #endif
55*32b31808SJens Wiklander /* GCC-like compilers: currently, we only support intrinsics if the requisite
56*32b31808SJens Wiklander  * target flag is enabled when building the library (e.g. `gcc -mpclmul -msse2`
57*32b31808SJens Wiklander  * or `clang -maes -mpclmul`). */
58*32b31808SJens Wiklander #if defined(__GNUC__) && defined(__AES__) && defined(__PCLMUL__)
59*32b31808SJens Wiklander #define MBEDTLS_AESNI_HAVE_INTRINSICS
60*32b31808SJens Wiklander #endif
61*32b31808SJens Wiklander 
62*32b31808SJens Wiklander /* Choose the implementation of AESNI, if one is available. */
63*32b31808SJens Wiklander #undef MBEDTLS_AESNI_HAVE_CODE
64*32b31808SJens Wiklander /* To minimize disruption when releasing the intrinsics-based implementation,
65*32b31808SJens Wiklander  * favor the assembly-based implementation if it's available. We intend to
66*32b31808SJens Wiklander  * revise this in a later release of Mbed TLS 3.x. In the long run, we will
67*32b31808SJens Wiklander  * likely remove the assembly implementation. */
68*32b31808SJens Wiklander #if defined(MBEDTLS_HAVE_X86_64)
69*32b31808SJens Wiklander #define MBEDTLS_AESNI_HAVE_CODE 1 // via assembly
70*32b31808SJens Wiklander #elif defined(MBEDTLS_AESNI_HAVE_INTRINSICS)
71*32b31808SJens Wiklander #define MBEDTLS_AESNI_HAVE_CODE 2 // via intrinsics
72*32b31808SJens Wiklander #endif
73*32b31808SJens Wiklander 
74*32b31808SJens Wiklander #if defined(MBEDTLS_AESNI_HAVE_CODE)
75*32b31808SJens Wiklander 
76*32b31808SJens Wiklander #ifdef __cplusplus
77*32b31808SJens Wiklander extern "C" {
78*32b31808SJens Wiklander #endif
79*32b31808SJens Wiklander 
80*32b31808SJens Wiklander /**
81*32b31808SJens Wiklander  * \brief          Internal function to detect the AES-NI feature in CPUs.
82*32b31808SJens Wiklander  *
83*32b31808SJens Wiklander  * \note           This function is only for internal use by other library
84*32b31808SJens Wiklander  *                 functions; you must not call it directly.
85*32b31808SJens Wiklander  *
86*32b31808SJens Wiklander  * \param what     The feature to detect
87*32b31808SJens Wiklander  *                 (MBEDTLS_AESNI_AES or MBEDTLS_AESNI_CLMUL)
88*32b31808SJens Wiklander  *
89*32b31808SJens Wiklander  * \return         1 if CPU has support for the feature, 0 otherwise
90*32b31808SJens Wiklander  */
91*32b31808SJens Wiklander int mbedtls_aesni_has_support(unsigned int what);
92*32b31808SJens Wiklander 
93*32b31808SJens Wiklander /**
94*32b31808SJens Wiklander  * \brief          Internal AES-NI AES-ECB block encryption and decryption
95*32b31808SJens Wiklander  *
96*32b31808SJens Wiklander  * \note           This function is only for internal use by other library
97*32b31808SJens Wiklander  *                 functions; you must not call it directly.
98*32b31808SJens Wiklander  *
99*32b31808SJens Wiklander  * \param ctx      AES context
100*32b31808SJens Wiklander  * \param mode     MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
101*32b31808SJens Wiklander  * \param input    16-byte input block
102*32b31808SJens Wiklander  * \param output   16-byte output block
103*32b31808SJens Wiklander  *
104*32b31808SJens Wiklander  * \return         0 on success (cannot fail)
105*32b31808SJens Wiklander  */
106*32b31808SJens Wiklander int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
107*32b31808SJens Wiklander                             int mode,
108*32b31808SJens Wiklander                             const unsigned char input[16],
109*32b31808SJens Wiklander                             unsigned char output[16]);
110*32b31808SJens Wiklander 
111*32b31808SJens Wiklander /**
112*32b31808SJens Wiklander  * \brief          Internal GCM multiplication: c = a * b in GF(2^128)
113*32b31808SJens Wiklander  *
114*32b31808SJens Wiklander  * \note           This function is only for internal use by other library
115*32b31808SJens Wiklander  *                 functions; you must not call it directly.
116*32b31808SJens Wiklander  *
117*32b31808SJens Wiklander  * \param c        Result
118*32b31808SJens Wiklander  * \param a        First operand
119*32b31808SJens Wiklander  * \param b        Second operand
120*32b31808SJens Wiklander  *
121*32b31808SJens Wiklander  * \note           Both operands and result are bit strings interpreted as
122*32b31808SJens Wiklander  *                 elements of GF(2^128) as per the GCM spec.
123*32b31808SJens Wiklander  */
124*32b31808SJens Wiklander void mbedtls_aesni_gcm_mult(unsigned char c[16],
125*32b31808SJens Wiklander                             const unsigned char a[16],
126*32b31808SJens Wiklander                             const unsigned char b[16]);
127*32b31808SJens Wiklander 
128*32b31808SJens Wiklander /**
129*32b31808SJens Wiklander  * \brief           Internal round key inversion. This function computes
130*32b31808SJens Wiklander  *                  decryption round keys from the encryption round keys.
131*32b31808SJens Wiklander  *
132*32b31808SJens Wiklander  * \note            This function is only for internal use by other library
133*32b31808SJens Wiklander  *                  functions; you must not call it directly.
134*32b31808SJens Wiklander  *
135*32b31808SJens Wiklander  * \param invkey    Round keys for the equivalent inverse cipher
136*32b31808SJens Wiklander  * \param fwdkey    Original round keys (for encryption)
137*32b31808SJens Wiklander  * \param nr        Number of rounds (that is, number of round keys minus one)
138*32b31808SJens Wiklander  */
139*32b31808SJens Wiklander void mbedtls_aesni_inverse_key(unsigned char *invkey,
140*32b31808SJens Wiklander                                const unsigned char *fwdkey,
141*32b31808SJens Wiklander                                int nr);
142*32b31808SJens Wiklander 
143*32b31808SJens Wiklander /**
144*32b31808SJens Wiklander  * \brief           Internal key expansion for encryption
145*32b31808SJens Wiklander  *
146*32b31808SJens Wiklander  * \note            This function is only for internal use by other library
147*32b31808SJens Wiklander  *                  functions; you must not call it directly.
148*32b31808SJens Wiklander  *
149*32b31808SJens Wiklander  * \param rk        Destination buffer where the round keys are written
150*32b31808SJens Wiklander  * \param key       Encryption key
151*32b31808SJens Wiklander  * \param bits      Key size in bits (must be 128, 192 or 256)
152*32b31808SJens Wiklander  *
153*32b31808SJens Wiklander  * \return          0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
154*32b31808SJens Wiklander  */
155*32b31808SJens Wiklander int mbedtls_aesni_setkey_enc(unsigned char *rk,
156*32b31808SJens Wiklander                              const unsigned char *key,
157*32b31808SJens Wiklander                              size_t bits);
158*32b31808SJens Wiklander 
159*32b31808SJens Wiklander #ifdef __cplusplus
160*32b31808SJens Wiklander }
161*32b31808SJens Wiklander #endif
162*32b31808SJens Wiklander 
163*32b31808SJens Wiklander #endif /* MBEDTLS_AESNI_HAVE_CODE */
164*32b31808SJens Wiklander #endif  /* MBEDTLS_AESNI_C */
165*32b31808SJens Wiklander 
166*32b31808SJens Wiklander #endif /* MBEDTLS_AESNI_H */
167