blob: f007735a62ae11c1900145f2a7e4952b97b969ea [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
Bence Szépkútic662b362021-05-27 11:25:03 +020028#include "mbedtls/build_info.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050029
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010030#include "mbedtls/aes.h"
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#define MBEDTLS_AESNI_AES 0x02000000u
33#define MBEDTLS_AESNI_CLMUL 0x00000002u
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010034
Jerry Yue62ff092023-08-16 14:15:00 +080035#if defined(MBEDTLS_AESNI_C) && \
Jerry Yud6e312d2023-08-18 17:19:51 +080036 (defined(MBEDTLS_ARCH_IS_X64) || defined(MBEDTLS_ARCH_IS_X86))
Gilles Peskine9af58cd2023-03-10 22:29:32 +010037
Gilles Peskine9c682e72023-03-16 17:21:33 +010038/* Can we do AESNI with intrinsics?
Gilles Peskine30e9f2a2023-03-17 17:29:58 +010039 * (Only implemented with certain compilers, only for certain targets.)
Gilles Peskine9c682e72023-03-16 17:21:33 +010040 */
41#undef MBEDTLS_AESNI_HAVE_INTRINSICS
Sergey Markelov3898f102023-10-16 12:54:48 -070042#if defined(_MSC_VER) && !defined(__clang__)
Gilles Peskine9c682e72023-03-16 17:21:33 +010043/* Visual Studio supports AESNI intrinsics since VS 2008 SP1. We only support
44 * VS 2013 and up for other reasons anyway, so no need to check the version. */
45#define MBEDTLS_AESNI_HAVE_INTRINSICS
Gilles Peskine9af58cd2023-03-10 22:29:32 +010046#endif
Gilles Peskine9c682e72023-03-16 17:21:33 +010047/* GCC-like compilers: currently, we only support intrinsics if the requisite
48 * target flag is enabled when building the library (e.g. `gcc -mpclmul -msse2`
49 * or `clang -maes -mpclmul`). */
Sergey Markelov3898f102023-10-16 12:54:48 -070050#if (defined(__GNUC__) || defined(__clang__)) && defined(__AES__) && defined(__PCLMUL__)
Gilles Peskine9c682e72023-03-16 17:21:33 +010051#define MBEDTLS_AESNI_HAVE_INTRINSICS
Gilles Peskine9af58cd2023-03-10 22:29:32 +010052#endif
Beniamin Sandu800f2b72023-10-27 16:58:00 +010053/* For 32-bit, we only support intrinsics */
54#if defined(MBEDTLS_ARCH_IS_X86) && (defined(__GNUC__) || defined(__clang__))
55#define MBEDTLS_AESNI_HAVE_INTRINSICS
56#endif
Gilles Peskine9af58cd2023-03-10 22:29:32 +010057
Dave Rodgmane07c6702023-06-16 13:21:28 +010058/* Choose the implementation of AESNI, if one is available.
59 *
60 * Favor the intrinsics-based implementation if it's available, for better
Dave Rodgman6365a682023-05-22 11:14:36 +010061 * maintainability.
62 * Performance is about the same (see #7380).
63 * In the long run, we will likely remove the assembly implementation. */
64#if defined(MBEDTLS_AESNI_HAVE_INTRINSICS)
Gilles Peskine9c682e72023-03-16 17:21:33 +010065#define MBEDTLS_AESNI_HAVE_CODE 2 // via intrinsics
Jerry Yue62ff092023-08-16 14:15:00 +080066#elif defined(MBEDTLS_HAVE_ASM) && \
Beniamin Sandu800f2b72023-10-27 16:58:00 +010067 (defined(__GNUC__) || defined(__clang__)) && defined(MBEDTLS_ARCH_IS_X64)
Jerry Yuf65f71e2023-08-28 10:58:24 +080068/* Can we do AESNI with inline assembly?
69 * (Only implemented with gas syntax, only for 64-bit.)
70 */
Dave Rodgman6365a682023-05-22 11:14:36 +010071#define MBEDTLS_AESNI_HAVE_CODE 1 // via assembly
Jerry Yu8189f322023-08-10 13:53:41 +080072#else
73#error "MBEDTLS_AESNI_C defined, but neither intrinsics nor assembly available"
Gilles Peskine9af58cd2023-03-10 22:29:32 +010074#endif
75
76#if defined(MBEDTLS_AESNI_HAVE_CODE)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010077
Manuel Pégourié-Gonnard1a901472015-03-10 16:12:29 +000078#ifdef __cplusplus
79extern "C" {
80#endif
81
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010082/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050083 * \brief Internal function to detect the AES-NI feature in CPUs.
84 *
85 * \note This function is only for internal use by other library
86 * functions; you must not call it directly.
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010087 *
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010088 * \param what The feature to detect
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 * (MBEDTLS_AESNI_AES or MBEDTLS_AESNI_CLMUL)
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010090 *
91 * \return 1 if CPU has support for the feature, 0 otherwise
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010092 */
Jerry Yu0a6272d2023-08-18 17:35:59 +080093#if !defined(MBEDTLS_AES_USE_HARDWARE_ONLY)
Gilles Peskine449bd832023-01-11 14:50:10 +010094int mbedtls_aesni_has_support(unsigned int what);
Jerry Yu0d4f4e52023-03-31 14:32:47 +080095#else
Jerry Yu9c0b7d12023-08-04 17:25:59 +080096#define mbedtls_aesni_has_support(what) 1
Jerry Yu0d4f4e52023-03-31 14:32:47 +080097#endif
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010098
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010099/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500100 * \brief Internal AES-NI AES-ECB block encryption and decryption
101 *
102 * \note This function is only for internal use by other library
103 * functions; you must not call it directly.
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100104 *
105 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100107 * \param input 16-byte input block
108 * \param output 16-byte output block
109 *
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100110 * \return 0 on success (cannot fail)
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100111 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100112int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
113 int mode,
114 const unsigned char input[16],
115 unsigned char output[16]);
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100116
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100117/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500118 * \brief Internal GCM multiplication: c = a * b in GF(2^128)
119 *
120 * \note This function is only for internal use by other library
121 * functions; you must not call it directly.
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100122 *
123 * \param c Result
124 * \param a First operand
125 * \param b Second operand
126 *
127 * \note Both operands and result are bit strings interpreted as
128 * elements of GF(2^128) as per the GCM spec.
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100129 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100130void mbedtls_aesni_gcm_mult(unsigned char c[16],
131 const unsigned char a[16],
132 const unsigned char b[16]);
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100133
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100134/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500135 * \brief Internal round key inversion. This function computes
136 * decryption round keys from the encryption round keys.
137 *
138 * \note This function is only for internal use by other library
139 * functions; you must not call it directly.
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100140 *
141 * \param invkey Round keys for the equivalent inverse cipher
142 * \param fwdkey Original round keys (for encryption)
143 * \param nr Number of rounds (that is, number of round keys minus one)
144 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100145void mbedtls_aesni_inverse_key(unsigned char *invkey,
146 const unsigned char *fwdkey,
147 int nr);
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100148
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100149/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500150 * \brief Internal key expansion for encryption
151 *
152 * \note This function is only for internal use by other library
153 * functions; you must not call it directly.
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100154 *
155 * \param rk Destination buffer where the round keys are written
156 * \param key Encryption key
157 * \param bits Key size in bits (must be 128, 192 or 256)
158 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100160 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100161int mbedtls_aesni_setkey_enc(unsigned char *rk,
162 const unsigned char *key,
163 size_t bits);
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100164
Manuel Pégourié-Gonnard1a901472015-03-10 16:12:29 +0000165#ifdef __cplusplus
166}
167#endif
168
Gilles Peskine9af58cd2023-03-10 22:29:32 +0100169#endif /* MBEDTLS_AESNI_HAVE_CODE */
170#endif /* MBEDTLS_AESNI_C */
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172#endif /* MBEDTLS_AESNI_H */