blob: b16ccc3e727205e54b907bdb0ae73d363efdb343 [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
Gilles Peskine6dec5412023-03-16 17:21:33 +010039/* Can we do AESNI with inline assembly?
40 * (Only implemented with gas syntax, only for 64-bit.)
41 */
Gilles Peskine5511a342023-03-10 22:29:32 +010042#if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010043 (defined(__amd64__) || defined(__x86_64__)) && \
44 !defined(MBEDTLS_HAVE_X86_64)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#define MBEDTLS_HAVE_X86_64
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010046#endif
47
Pengyu Lv5a091592023-09-13 17:37:53 +080048#if !defined(MBEDTLS_HAVE_X86) && \
49 (defined(__i386__) || defined(_M_IX86))
50#define MBEDTLS_HAVE_X86
51#endif
52
Pengyu Lvdc5a88b2023-09-13 17:40:25 +080053#if defined(MBEDTLS_AESNI_C) && \
54 (defined(MBEDTLS_HAVE_X86_64) || defined(MBEDTLS_HAVE_X86))
Gilles Peskine5511a342023-03-10 22:29:32 +010055
Gilles Peskine6dec5412023-03-16 17:21:33 +010056/* Can we do AESNI with intrinsics?
Gilles Peskine3efd3142023-03-17 17:29:58 +010057 * (Only implemented with certain compilers, only for certain targets.)
Tom Cosgrove779199f2023-03-17 17:16:53 +000058 *
59 * NOTE: MBEDTLS_AESNI_HAVE_INTRINSICS and MBEDTLS_AESNI_HAVE_CODE are internal
60 * macros that may change in future releases.
Gilles Peskine6dec5412023-03-16 17:21:33 +010061 */
62#undef MBEDTLS_AESNI_HAVE_INTRINSICS
Gilles Peskine5511a342023-03-10 22:29:32 +010063#if defined(_MSC_VER)
Gilles Peskine6dec5412023-03-16 17:21:33 +010064/* Visual Studio supports AESNI intrinsics since VS 2008 SP1. We only support
65 * VS 2013 and up for other reasons anyway, so no need to check the version. */
66#define MBEDTLS_AESNI_HAVE_INTRINSICS
Gilles Peskine5511a342023-03-10 22:29:32 +010067#endif
Gilles Peskine6dec5412023-03-16 17:21:33 +010068/* GCC-like compilers: currently, we only support intrinsics if the requisite
69 * target flag is enabled when building the library (e.g. `gcc -mpclmul -msse2`
70 * or `clang -maes -mpclmul`). */
71#if defined(__GNUC__) && defined(__AES__) && defined(__PCLMUL__)
72#define MBEDTLS_AESNI_HAVE_INTRINSICS
Gilles Peskine5511a342023-03-10 22:29:32 +010073#endif
74
Gilles Peskine6dec5412023-03-16 17:21:33 +010075/* Choose the implementation of AESNI, if one is available. */
76#undef MBEDTLS_AESNI_HAVE_CODE
77/* To minimize disruption when releasing the intrinsics-based implementation,
78 * favor the assembly-based implementation if it's available. We intend to
79 * revise this in a later release of Mbed TLS 3.x. In the long run, we will
80 * likely remove the assembly implementation. */
81#if defined(MBEDTLS_HAVE_X86_64)
82#define MBEDTLS_AESNI_HAVE_CODE 1 // via assembly
Gilles Peskine9494a992023-03-17 17:30:29 +010083#elif defined(MBEDTLS_AESNI_HAVE_INTRINSICS)
Gilles Peskine6dec5412023-03-16 17:21:33 +010084#define MBEDTLS_AESNI_HAVE_CODE 2 // via intrinsics
Gilles Peskine5511a342023-03-10 22:29:32 +010085#endif
86
87#if defined(MBEDTLS_AESNI_HAVE_CODE)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010088
Manuel Pégourié-Gonnard1a901472015-03-10 16:12:29 +000089#ifdef __cplusplus
90extern "C" {
91#endif
92
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010093/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050094 * \brief Internal function to detect the AES-NI feature in CPUs.
95 *
96 * \note This function is only for internal use by other library
97 * functions; you must not call it directly.
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010098 *
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010099 * \param what The feature to detect
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 * (MBEDTLS_AESNI_AES or MBEDTLS_AESNI_CLMUL)
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +0100101 *
102 * \return 1 if CPU has support for the feature, 0 otherwise
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100103 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100104int mbedtls_aesni_has_support(unsigned int what);
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100105
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100106/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500107 * \brief Internal AES-NI AES-ECB block encryption and decryption
108 *
109 * \note This function is only for internal use by other library
110 * functions; you must not call it directly.
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100111 *
112 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100114 * \param input 16-byte input block
115 * \param output 16-byte output block
116 *
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100117 * \return 0 on success (cannot fail)
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100118 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100119int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
120 int mode,
121 const unsigned char input[16],
122 unsigned char output[16]);
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100123
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100124/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500125 * \brief Internal GCM multiplication: c = a * b in GF(2^128)
126 *
127 * \note This function is only for internal use by other library
128 * functions; you must not call it directly.
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100129 *
130 * \param c Result
131 * \param a First operand
132 * \param b Second operand
133 *
134 * \note Both operands and result are bit strings interpreted as
135 * elements of GF(2^128) as per the GCM spec.
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100136 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100137void mbedtls_aesni_gcm_mult(unsigned char c[16],
138 const unsigned char a[16],
139 const unsigned char b[16]);
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100140
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100141/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500142 * \brief Internal round key inversion. This function computes
143 * decryption round keys from the encryption round keys.
144 *
145 * \note This function is only for internal use by other library
146 * functions; you must not call it directly.
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100147 *
148 * \param invkey Round keys for the equivalent inverse cipher
149 * \param fwdkey Original round keys (for encryption)
150 * \param nr Number of rounds (that is, number of round keys minus one)
151 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100152void mbedtls_aesni_inverse_key(unsigned char *invkey,
153 const unsigned char *fwdkey,
154 int nr);
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100155
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100156/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500157 * \brief Internal key expansion for encryption
158 *
159 * \note This function is only for internal use by other library
160 * functions; you must not call it directly.
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100161 *
162 * \param rk Destination buffer where the round keys are written
163 * \param key Encryption key
164 * \param bits Key size in bits (must be 128, 192 or 256)
165 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100167 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100168int mbedtls_aesni_setkey_enc(unsigned char *rk,
169 const unsigned char *key,
170 size_t bits);
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100171
Manuel Pégourié-Gonnard1a901472015-03-10 16:12:29 +0000172#ifdef __cplusplus
173}
174#endif
175
Gilles Peskine5511a342023-03-10 22:29:32 +0100176#endif /* MBEDTLS_AESNI_HAVE_CODE */
177#endif /* MBEDTLS_AESNI_C */
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#endif /* MBEDTLS_AESNI_H */