blob: 332a0f0722f82bbcd5a8d332d60f9b442dc45d9d [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
Gilles Peskine9c682e72023-03-16 17:21:33 +010035/* Can we do AESNI with inline assembly?
36 * (Only implemented with gas syntax, only for 64-bit.)
37 */
Jerry Yu8189f322023-08-10 13:53:41 +080038#if !defined(MBEDTLS_HAVE_X86_64) && \
39 (defined(__amd64__) || defined(__x86_64__) || \
Jerry Yu35b59d72023-08-17 10:34:15 +080040 defined(_M_X64) || defined(_M_AMD64)) && \
41 !defined(_M_ARM64EC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#define MBEDTLS_HAVE_X86_64
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010043#endif
44
Jerry Yue62ff092023-08-16 14:15:00 +080045#if !defined(MBEDTLS_HAVE_X86) && \
46 (defined(__i386__) || defined(_M_IX86))
47#define MBEDTLS_HAVE_X86
48#endif
49
50#if defined(MBEDTLS_AESNI_C) && \
51 (defined(MBEDTLS_HAVE_X86_64) || defined(MBEDTLS_HAVE_X86))
Gilles Peskine9af58cd2023-03-10 22:29:32 +010052
Gilles Peskine9c682e72023-03-16 17:21:33 +010053/* Can we do AESNI with intrinsics?
Gilles Peskine30e9f2a2023-03-17 17:29:58 +010054 * (Only implemented with certain compilers, only for certain targets.)
Gilles Peskine9c682e72023-03-16 17:21:33 +010055 */
56#undef MBEDTLS_AESNI_HAVE_INTRINSICS
Gilles Peskine9af58cd2023-03-10 22:29:32 +010057#if defined(_MSC_VER)
Gilles Peskine9c682e72023-03-16 17:21:33 +010058/* Visual Studio supports AESNI intrinsics since VS 2008 SP1. We only support
59 * VS 2013 and up for other reasons anyway, so no need to check the version. */
60#define MBEDTLS_AESNI_HAVE_INTRINSICS
Gilles Peskine9af58cd2023-03-10 22:29:32 +010061#endif
Gilles Peskine9c682e72023-03-16 17:21:33 +010062/* GCC-like compilers: currently, we only support intrinsics if the requisite
63 * target flag is enabled when building the library (e.g. `gcc -mpclmul -msse2`
64 * or `clang -maes -mpclmul`). */
65#if defined(__GNUC__) && defined(__AES__) && defined(__PCLMUL__)
66#define MBEDTLS_AESNI_HAVE_INTRINSICS
Gilles Peskine9af58cd2023-03-10 22:29:32 +010067#endif
68
Dave Rodgmane07c6702023-06-16 13:21:28 +010069/* Choose the implementation of AESNI, if one is available.
70 *
71 * Favor the intrinsics-based implementation if it's available, for better
Dave Rodgman6365a682023-05-22 11:14:36 +010072 * maintainability.
73 * Performance is about the same (see #7380).
74 * In the long run, we will likely remove the assembly implementation. */
75#if defined(MBEDTLS_AESNI_HAVE_INTRINSICS)
Gilles Peskine9c682e72023-03-16 17:21:33 +010076#define MBEDTLS_AESNI_HAVE_CODE 2 // via intrinsics
Jerry Yue62ff092023-08-16 14:15:00 +080077#elif defined(MBEDTLS_HAVE_ASM) && \
78 defined(__GNUC__) && defined(MBEDTLS_HAVE_X86_64)
Dave Rodgman6365a682023-05-22 11:14:36 +010079#define MBEDTLS_AESNI_HAVE_CODE 1 // via assembly
Jerry Yu3ce03982023-08-16 17:22:18 +080080#elif defined(__GNUC__)
81# error "Must use `-mpclmul -msse2 -maes` for MBEDTLS_AESNI_C"
Jerry Yu8189f322023-08-10 13:53:41 +080082#else
83#error "MBEDTLS_AESNI_C defined, but neither intrinsics nor assembly available"
Gilles Peskine9af58cd2023-03-10 22:29:32 +010084#endif
85
86#if defined(MBEDTLS_AESNI_HAVE_CODE)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010087
Manuel Pégourié-Gonnard1a901472015-03-10 16:12:29 +000088#ifdef __cplusplus
89extern "C" {
90#endif
91
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010092/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050093 * \brief Internal function to detect the AES-NI feature in CPUs.
94 *
95 * \note This function is only for internal use by other library
96 * functions; you must not call it directly.
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010097 *
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010098 * \param what The feature to detect
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 * (MBEDTLS_AESNI_AES or MBEDTLS_AESNI_CLMUL)
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +0100100 *
101 * \return 1 if CPU has support for the feature, 0 otherwise
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100102 */
Jerry Yu0a6272d2023-08-18 17:35:59 +0800103#if !defined(MBEDTLS_AES_USE_HARDWARE_ONLY)
Gilles Peskine449bd832023-01-11 14:50:10 +0100104int mbedtls_aesni_has_support(unsigned int what);
Jerry Yu0d4f4e52023-03-31 14:32:47 +0800105#else
Jerry Yu9c0b7d12023-08-04 17:25:59 +0800106#define mbedtls_aesni_has_support(what) 1
Jerry Yu0d4f4e52023-03-31 14:32:47 +0800107#endif
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100108
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100109/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500110 * \brief Internal AES-NI AES-ECB block encryption and decryption
111 *
112 * \note This function is only for internal use by other library
113 * functions; you must not call it directly.
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100114 *
115 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100117 * \param input 16-byte input block
118 * \param output 16-byte output block
119 *
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100120 * \return 0 on success (cannot fail)
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100121 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100122int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
123 int mode,
124 const unsigned char input[16],
125 unsigned char output[16]);
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100126
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100127/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500128 * \brief Internal GCM multiplication: c = a * b in GF(2^128)
129 *
130 * \note This function is only for internal use by other library
131 * functions; you must not call it directly.
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100132 *
133 * \param c Result
134 * \param a First operand
135 * \param b Second operand
136 *
137 * \note Both operands and result are bit strings interpreted as
138 * elements of GF(2^128) as per the GCM spec.
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100139 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100140void mbedtls_aesni_gcm_mult(unsigned char c[16],
141 const unsigned char a[16],
142 const unsigned char b[16]);
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100143
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100144/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500145 * \brief Internal round key inversion. This function computes
146 * decryption round keys from the encryption round keys.
147 *
148 * \note This function is only for internal use by other library
149 * functions; you must not call it directly.
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100150 *
151 * \param invkey Round keys for the equivalent inverse cipher
152 * \param fwdkey Original round keys (for encryption)
153 * \param nr Number of rounds (that is, number of round keys minus one)
154 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100155void mbedtls_aesni_inverse_key(unsigned char *invkey,
156 const unsigned char *fwdkey,
157 int nr);
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100158
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100159/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500160 * \brief Internal key expansion for encryption
161 *
162 * \note This function is only for internal use by other library
163 * functions; you must not call it directly.
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100164 *
165 * \param rk Destination buffer where the round keys are written
166 * \param key Encryption key
167 * \param bits Key size in bits (must be 128, 192 or 256)
168 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100170 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100171int mbedtls_aesni_setkey_enc(unsigned char *rk,
172 const unsigned char *key,
173 size_t bits);
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100174
Manuel Pégourié-Gonnard1a901472015-03-10 16:12:29 +0000175#ifdef __cplusplus
176}
177#endif
178
Gilles Peskine9af58cd2023-03-10 22:29:32 +0100179#endif /* MBEDTLS_AESNI_HAVE_CODE */
180#endif /* MBEDTLS_AESNI_C */
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182#endif /* MBEDTLS_AESNI_H */