blob: 93f067304d89f03139167509190b37b0b7639e03 [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
Dave Rodgman7ff79652023-11-03 12:04:52 +000011 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010012 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020013#ifndef MBEDTLS_AESNI_H
14#define MBEDTLS_AESNI_H
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010015
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050016#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010017#include "mbedtls/config.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050018#else
19#include MBEDTLS_CONFIG_FILE
20#endif
21
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010022#include "mbedtls/aes.h"
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#define MBEDTLS_AESNI_AES 0x02000000u
25#define MBEDTLS_AESNI_CLMUL 0x00000002u
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010026
Pengyu Lve707dc12023-09-13 18:09:24 +080027#if !defined(MBEDTLS_HAVE_X86_64) && \
28 (defined(__amd64__) || defined(__x86_64__) || \
29 defined(_M_X64) || defined(_M_AMD64)) && \
30 !defined(_M_ARM64EC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#define MBEDTLS_HAVE_X86_64
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010032#endif
33
Pengyu Lv5a091592023-09-13 17:37:53 +080034#if !defined(MBEDTLS_HAVE_X86) && \
35 (defined(__i386__) || defined(_M_IX86))
36#define MBEDTLS_HAVE_X86
37#endif
38
Pengyu Lvdc5a88b2023-09-13 17:40:25 +080039#if defined(MBEDTLS_AESNI_C) && \
40 (defined(MBEDTLS_HAVE_X86_64) || defined(MBEDTLS_HAVE_X86))
Gilles Peskine5511a342023-03-10 22:29:32 +010041
Gilles Peskine6dec5412023-03-16 17:21:33 +010042/* Can we do AESNI with intrinsics?
Gilles Peskine3efd3142023-03-17 17:29:58 +010043 * (Only implemented with certain compilers, only for certain targets.)
Tom Cosgrove779199f2023-03-17 17:16:53 +000044 *
45 * NOTE: MBEDTLS_AESNI_HAVE_INTRINSICS and MBEDTLS_AESNI_HAVE_CODE are internal
46 * macros that may change in future releases.
Gilles Peskine6dec5412023-03-16 17:21:33 +010047 */
48#undef MBEDTLS_AESNI_HAVE_INTRINSICS
Sergey Markelov9902a6b2023-10-16 12:54:48 -070049#if defined(_MSC_VER) && !defined(__clang__)
Gilles Peskine6dec5412023-03-16 17:21:33 +010050/* Visual Studio supports AESNI intrinsics since VS 2008 SP1. We only support
51 * VS 2013 and up for other reasons anyway, so no need to check the version. */
52#define MBEDTLS_AESNI_HAVE_INTRINSICS
Gilles Peskine5511a342023-03-10 22:29:32 +010053#endif
Gilles Peskine6dec5412023-03-16 17:21:33 +010054/* GCC-like compilers: currently, we only support intrinsics if the requisite
55 * target flag is enabled when building the library (e.g. `gcc -mpclmul -msse2`
56 * or `clang -maes -mpclmul`). */
Sergey Markelov9902a6b2023-10-16 12:54:48 -070057#if (defined(__GNUC__) || defined(__clang__)) && defined(__AES__) && defined(__PCLMUL__)
Gilles Peskine6dec5412023-03-16 17:21:33 +010058#define MBEDTLS_AESNI_HAVE_INTRINSICS
Gilles Peskine5511a342023-03-10 22:29:32 +010059#endif
60
Gilles Peskine6dec5412023-03-16 17:21:33 +010061/* Choose the implementation of AESNI, if one is available. */
62#undef MBEDTLS_AESNI_HAVE_CODE
63/* To minimize disruption when releasing the intrinsics-based implementation,
64 * favor the assembly-based implementation if it's available. We intend to
65 * revise this in a later release of Mbed TLS 3.x. In the long run, we will
66 * likely remove the assembly implementation. */
Pengyu Lve707dc12023-09-13 18:09:24 +080067#if defined(MBEDTLS_HAVE_ASM) && \
68 defined(__GNUC__) && defined(MBEDTLS_HAVE_X86_64)
69/* Can we do AESNI with inline assembly?
70 * (Only implemented with gas syntax, only for 64-bit.)
71 */
Gilles Peskine6dec5412023-03-16 17:21:33 +010072#define MBEDTLS_AESNI_HAVE_CODE 1 // via assembly
Gilles Peskine9494a992023-03-17 17:30:29 +010073#elif defined(MBEDTLS_AESNI_HAVE_INTRINSICS)
Gilles Peskine6dec5412023-03-16 17:21:33 +010074#define MBEDTLS_AESNI_HAVE_CODE 2 // via intrinsics
Gilles Peskine5511a342023-03-10 22:29:32 +010075#endif
76
77#if defined(MBEDTLS_AESNI_HAVE_CODE)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010078
Manuel Pégourié-Gonnard1a901472015-03-10 16:12:29 +000079#ifdef __cplusplus
80extern "C" {
81#endif
82
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010083/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050084 * \brief Internal function to detect the AES-NI feature in CPUs.
85 *
86 * \note This function is only for internal use by other library
87 * functions; you must not call it directly.
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010088 *
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010089 * \param what The feature to detect
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 * (MBEDTLS_AESNI_AES or MBEDTLS_AESNI_CLMUL)
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010091 *
92 * \return 1 if CPU has support for the feature, 0 otherwise
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010093 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010094int mbedtls_aesni_has_support(unsigned int what);
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010095
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010096/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050097 * \brief Internal AES-NI AES-ECB block encryption and decryption
98 *
99 * \note This function is only for internal use by other library
100 * functions; you must not call it directly.
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100101 *
102 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100104 * \param input 16-byte input block
105 * \param output 16-byte output block
106 *
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100107 * \return 0 on success (cannot fail)
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100108 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100109int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
110 int mode,
111 const unsigned char input[16],
112 unsigned char output[16]);
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100113
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100114/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500115 * \brief Internal GCM multiplication: c = a * b in GF(2^128)
116 *
117 * \note This function is only for internal use by other library
118 * functions; you must not call it directly.
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100119 *
120 * \param c Result
121 * \param a First operand
122 * \param b Second operand
123 *
124 * \note Both operands and result are bit strings interpreted as
125 * elements of GF(2^128) as per the GCM spec.
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100126 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100127void mbedtls_aesni_gcm_mult(unsigned char c[16],
128 const unsigned char a[16],
129 const unsigned char b[16]);
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100130
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100131/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500132 * \brief Internal round key inversion. This function computes
133 * decryption round keys from the encryption round keys.
134 *
135 * \note This function is only for internal use by other library
136 * functions; you must not call it directly.
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100137 *
138 * \param invkey Round keys for the equivalent inverse cipher
139 * \param fwdkey Original round keys (for encryption)
140 * \param nr Number of rounds (that is, number of round keys minus one)
141 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100142void mbedtls_aesni_inverse_key(unsigned char *invkey,
143 const unsigned char *fwdkey,
144 int nr);
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100145
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100146/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500147 * \brief Internal key expansion for encryption
148 *
149 * \note This function is only for internal use by other library
150 * functions; you must not call it directly.
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100151 *
152 * \param rk Destination buffer where the round keys are written
153 * \param key Encryption key
154 * \param bits Key size in bits (must be 128, 192 or 256)
155 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100157 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100158int mbedtls_aesni_setkey_enc(unsigned char *rk,
159 const unsigned char *key,
160 size_t bits);
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100161
Manuel Pégourié-Gonnard1a901472015-03-10 16:12:29 +0000162#ifdef __cplusplus
163}
164#endif
165
Gilles Peskine5511a342023-03-10 22:29:32 +0100166#endif /* MBEDTLS_AESNI_HAVE_CODE */
Pengyu Lv20384f42023-09-21 10:14:16 +0800167#endif /* MBEDTLS_AESNI_C && (MBEDTLS_HAVE_X86_64 || MBEDTLS_HAVE_X86) */
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#endif /* MBEDTLS_AESNI_H */