blob: 49bc9e17c06313b0ca0da391a2b1aeb1a3875f9d [file] [log] [blame]
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +01001/**
2 * \file aesni.h
3 *
4 * \brief AES-NI for hardware AES acceleration on some Intel processors
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05005 *
6 * \warning These functions are only for internal use by other library
7 * functions; you must not call them directly.
Darryl Greena40a1012018-01-05 15:33:17 +00008 */
9/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020010 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020011 * SPDX-License-Identifier: Apache-2.0
12 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010024 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#ifndef MBEDTLS_AESNI_H
26#define MBEDTLS_AESNI_H
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010027
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050028#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010029#include "mbedtls/config.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050030#else
31#include MBEDTLS_CONFIG_FILE
32#endif
33
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010034#include "mbedtls/aes.h"
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#define MBEDTLS_AESNI_AES 0x02000000u
37#define MBEDTLS_AESNI_CLMUL 0x00000002u
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010038
Pengyu Lve707dc12023-09-13 18:09:24 +080039#if !defined(MBEDTLS_HAVE_X86_64) && \
40 (defined(__amd64__) || defined(__x86_64__) || \
41 defined(_M_X64) || defined(_M_AMD64)) && \
42 !defined(_M_ARM64EC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#define MBEDTLS_HAVE_X86_64
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010044#endif
45
Pengyu Lv5a091592023-09-13 17:37:53 +080046#if !defined(MBEDTLS_HAVE_X86) && \
47 (defined(__i386__) || defined(_M_IX86))
48#define MBEDTLS_HAVE_X86
49#endif
50
Pengyu Lvdc5a88b2023-09-13 17:40:25 +080051#if defined(MBEDTLS_AESNI_C) && \
52 (defined(MBEDTLS_HAVE_X86_64) || defined(MBEDTLS_HAVE_X86))
Gilles Peskine5511a342023-03-10 22:29:32 +010053
Gilles Peskine6dec5412023-03-16 17:21:33 +010054/* Can we do AESNI with intrinsics?
Gilles Peskine3efd3142023-03-17 17:29:58 +010055 * (Only implemented with certain compilers, only for certain targets.)
Tom Cosgrove779199f2023-03-17 17:16:53 +000056 *
57 * NOTE: MBEDTLS_AESNI_HAVE_INTRINSICS and MBEDTLS_AESNI_HAVE_CODE are internal
58 * macros that may change in future releases.
Gilles Peskine6dec5412023-03-16 17:21:33 +010059 */
60#undef MBEDTLS_AESNI_HAVE_INTRINSICS
Gilles Peskine5511a342023-03-10 22:29:32 +010061#if defined(_MSC_VER)
Gilles Peskine6dec5412023-03-16 17:21:33 +010062/* Visual Studio supports AESNI intrinsics since VS 2008 SP1. We only support
63 * VS 2013 and up for other reasons anyway, so no need to check the version. */
64#define MBEDTLS_AESNI_HAVE_INTRINSICS
Gilles Peskine5511a342023-03-10 22:29:32 +010065#endif
Gilles Peskine6dec5412023-03-16 17:21:33 +010066/* GCC-like compilers: currently, we only support intrinsics if the requisite
67 * target flag is enabled when building the library (e.g. `gcc -mpclmul -msse2`
68 * or `clang -maes -mpclmul`). */
69#if defined(__GNUC__) && defined(__AES__) && defined(__PCLMUL__)
70#define MBEDTLS_AESNI_HAVE_INTRINSICS
Gilles Peskine5511a342023-03-10 22:29:32 +010071#endif
72
Gilles Peskine6dec5412023-03-16 17:21:33 +010073/* Choose the implementation of AESNI, if one is available. */
74#undef MBEDTLS_AESNI_HAVE_CODE
75/* To minimize disruption when releasing the intrinsics-based implementation,
76 * favor the assembly-based implementation if it's available. We intend to
77 * revise this in a later release of Mbed TLS 3.x. In the long run, we will
78 * likely remove the assembly implementation. */
Pengyu Lve707dc12023-09-13 18:09:24 +080079#if defined(MBEDTLS_HAVE_ASM) && \
80 defined(__GNUC__) && defined(MBEDTLS_HAVE_X86_64)
81/* Can we do AESNI with inline assembly?
82 * (Only implemented with gas syntax, only for 64-bit.)
83 */
Gilles Peskine6dec5412023-03-16 17:21:33 +010084#define MBEDTLS_AESNI_HAVE_CODE 1 // via assembly
Gilles Peskine9494a992023-03-17 17:30:29 +010085#elif defined(MBEDTLS_AESNI_HAVE_INTRINSICS)
Gilles Peskine6dec5412023-03-16 17:21:33 +010086#define MBEDTLS_AESNI_HAVE_CODE 2 // via intrinsics
Gilles Peskine5511a342023-03-10 22:29:32 +010087#endif
88
89#if defined(MBEDTLS_AESNI_HAVE_CODE)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010090
Manuel Pégourié-Gonnard1a901472015-03-10 16:12:29 +000091#ifdef __cplusplus
92extern "C" {
93#endif
94
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010095/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050096 * \brief Internal function to detect the AES-NI feature in CPUs.
97 *
98 * \note This function is only for internal use by other library
99 * functions; you must not call it directly.
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100100 *
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +0100101 * \param what The feature to detect
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 * (MBEDTLS_AESNI_AES or MBEDTLS_AESNI_CLMUL)
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +0100103 *
104 * \return 1 if CPU has support for the feature, 0 otherwise
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100105 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106int mbedtls_aesni_has_support(unsigned int what);
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100107
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100108/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500109 * \brief Internal AES-NI AES-ECB block encryption and decryption
110 *
111 * \note This function is only for internal use by other library
112 * functions; you must not call it directly.
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100113 *
114 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100116 * \param input 16-byte input block
117 * \param output 16-byte output block
118 *
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100119 * \return 0 on success (cannot fail)
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100120 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100121int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
122 int mode,
123 const unsigned char input[16],
124 unsigned char output[16]);
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100125
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100126/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500127 * \brief Internal GCM multiplication: c = a * b in GF(2^128)
128 *
129 * \note This function is only for internal use by other library
130 * functions; you must not call it directly.
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100131 *
132 * \param c Result
133 * \param a First operand
134 * \param b Second operand
135 *
136 * \note Both operands and result are bit strings interpreted as
137 * elements of GF(2^128) as per the GCM spec.
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100138 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100139void mbedtls_aesni_gcm_mult(unsigned char c[16],
140 const unsigned char a[16],
141 const unsigned char b[16]);
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100142
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100143/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500144 * \brief Internal round key inversion. This function computes
145 * decryption round keys from the encryption round keys.
146 *
147 * \note This function is only for internal use by other library
148 * functions; you must not call it directly.
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100149 *
150 * \param invkey Round keys for the equivalent inverse cipher
151 * \param fwdkey Original round keys (for encryption)
152 * \param nr Number of rounds (that is, number of round keys minus one)
153 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100154void mbedtls_aesni_inverse_key(unsigned char *invkey,
155 const unsigned char *fwdkey,
156 int nr);
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100157
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100158/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500159 * \brief Internal key expansion for encryption
160 *
161 * \note This function is only for internal use by other library
162 * functions; you must not call it directly.
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100163 *
164 * \param rk Destination buffer where the round keys are written
165 * \param key Encryption key
166 * \param bits Key size in bits (must be 128, 192 or 256)
167 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100169 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100170int mbedtls_aesni_setkey_enc(unsigned char *rk,
171 const unsigned char *key,
172 size_t bits);
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100173
Manuel Pégourié-Gonnard1a901472015-03-10 16:12:29 +0000174#ifdef __cplusplus
175}
176#endif
177
Gilles Peskine5511a342023-03-10 22:29:32 +0100178#endif /* MBEDTLS_AESNI_HAVE_CODE */
179#endif /* MBEDTLS_AESNI_C */
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181#endif /* MBEDTLS_AESNI_H */