Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file aesni.h |
| 3 | * |
| 4 | * \brief AES-NI for hardware AES acceleration on some Intel processors |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 5 | * |
| 6 | * \warning These functions are only for internal use by other library |
| 7 | * functions; you must not call them directly. |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 8 | */ |
| 9 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 10 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 7ff7965 | 2023-11-03 12:04:52 +0000 | [diff] [blame^] | 11 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 12 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 13 | #ifndef MBEDTLS_AESNI_H |
| 14 | #define MBEDTLS_AESNI_H |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 15 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 16 | #if !defined(MBEDTLS_CONFIG_FILE) |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 17 | #include "mbedtls/config.h" |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 18 | #else |
| 19 | #include MBEDTLS_CONFIG_FILE |
| 20 | #endif |
| 21 | |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 22 | #include "mbedtls/aes.h" |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 23 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 24 | #define MBEDTLS_AESNI_AES 0x02000000u |
| 25 | #define MBEDTLS_AESNI_CLMUL 0x00000002u |
Manuel Pégourié-Gonnard | 8eaf20b | 2013-12-18 19:14:53 +0100 | [diff] [blame] | 26 | |
Pengyu Lv | e707dc1 | 2023-09-13 18:09:24 +0800 | [diff] [blame] | 27 | #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é-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 31 | #define MBEDTLS_HAVE_X86_64 |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 32 | #endif |
| 33 | |
Pengyu Lv | 5a09159 | 2023-09-13 17:37:53 +0800 | [diff] [blame] | 34 | #if !defined(MBEDTLS_HAVE_X86) && \ |
| 35 | (defined(__i386__) || defined(_M_IX86)) |
| 36 | #define MBEDTLS_HAVE_X86 |
| 37 | #endif |
| 38 | |
Pengyu Lv | dc5a88b | 2023-09-13 17:40:25 +0800 | [diff] [blame] | 39 | #if defined(MBEDTLS_AESNI_C) && \ |
| 40 | (defined(MBEDTLS_HAVE_X86_64) || defined(MBEDTLS_HAVE_X86)) |
Gilles Peskine | 5511a34 | 2023-03-10 22:29:32 +0100 | [diff] [blame] | 41 | |
Gilles Peskine | 6dec541 | 2023-03-16 17:21:33 +0100 | [diff] [blame] | 42 | /* Can we do AESNI with intrinsics? |
Gilles Peskine | 3efd314 | 2023-03-17 17:29:58 +0100 | [diff] [blame] | 43 | * (Only implemented with certain compilers, only for certain targets.) |
Tom Cosgrove | 779199f | 2023-03-17 17:16:53 +0000 | [diff] [blame] | 44 | * |
| 45 | * NOTE: MBEDTLS_AESNI_HAVE_INTRINSICS and MBEDTLS_AESNI_HAVE_CODE are internal |
| 46 | * macros that may change in future releases. |
Gilles Peskine | 6dec541 | 2023-03-16 17:21:33 +0100 | [diff] [blame] | 47 | */ |
| 48 | #undef MBEDTLS_AESNI_HAVE_INTRINSICS |
Sergey Markelov | 9902a6b | 2023-10-16 12:54:48 -0700 | [diff] [blame] | 49 | #if defined(_MSC_VER) && !defined(__clang__) |
Gilles Peskine | 6dec541 | 2023-03-16 17:21:33 +0100 | [diff] [blame] | 50 | /* 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 Peskine | 5511a34 | 2023-03-10 22:29:32 +0100 | [diff] [blame] | 53 | #endif |
Gilles Peskine | 6dec541 | 2023-03-16 17:21:33 +0100 | [diff] [blame] | 54 | /* 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 Markelov | 9902a6b | 2023-10-16 12:54:48 -0700 | [diff] [blame] | 57 | #if (defined(__GNUC__) || defined(__clang__)) && defined(__AES__) && defined(__PCLMUL__) |
Gilles Peskine | 6dec541 | 2023-03-16 17:21:33 +0100 | [diff] [blame] | 58 | #define MBEDTLS_AESNI_HAVE_INTRINSICS |
Gilles Peskine | 5511a34 | 2023-03-10 22:29:32 +0100 | [diff] [blame] | 59 | #endif |
| 60 | |
Gilles Peskine | 6dec541 | 2023-03-16 17:21:33 +0100 | [diff] [blame] | 61 | /* 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 Lv | e707dc1 | 2023-09-13 18:09:24 +0800 | [diff] [blame] | 67 | #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 Peskine | 6dec541 | 2023-03-16 17:21:33 +0100 | [diff] [blame] | 72 | #define MBEDTLS_AESNI_HAVE_CODE 1 // via assembly |
Gilles Peskine | 9494a99 | 2023-03-17 17:30:29 +0100 | [diff] [blame] | 73 | #elif defined(MBEDTLS_AESNI_HAVE_INTRINSICS) |
Gilles Peskine | 6dec541 | 2023-03-16 17:21:33 +0100 | [diff] [blame] | 74 | #define MBEDTLS_AESNI_HAVE_CODE 2 // via intrinsics |
Gilles Peskine | 5511a34 | 2023-03-10 22:29:32 +0100 | [diff] [blame] | 75 | #endif |
| 76 | |
| 77 | #if defined(MBEDTLS_AESNI_HAVE_CODE) |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 78 | |
Manuel Pégourié-Gonnard | 1a90147 | 2015-03-10 16:12:29 +0000 | [diff] [blame] | 79 | #ifdef __cplusplus |
| 80 | extern "C" { |
| 81 | #endif |
| 82 | |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 83 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 84 | * \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é-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 88 | * |
Manuel Pégourié-Gonnard | 8eaf20b | 2013-12-18 19:14:53 +0100 | [diff] [blame] | 89 | * \param what The feature to detect |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 90 | * (MBEDTLS_AESNI_AES or MBEDTLS_AESNI_CLMUL) |
Manuel Pégourié-Gonnard | 8eaf20b | 2013-12-18 19:14:53 +0100 | [diff] [blame] | 91 | * |
| 92 | * \return 1 if CPU has support for the feature, 0 otherwise |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 93 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 94 | int mbedtls_aesni_has_support(unsigned int what); |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 95 | |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 96 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 97 | * \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é-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 101 | * |
| 102 | * \param ctx AES context |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 103 | * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 104 | * \param input 16-byte input block |
| 105 | * \param output 16-byte output block |
| 106 | * |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 107 | * \return 0 on success (cannot fail) |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 108 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 109 | int 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é-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 113 | |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 114 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 115 | * \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é-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 119 | * |
| 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é-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 126 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 127 | void mbedtls_aesni_gcm_mult(unsigned char c[16], |
| 128 | const unsigned char a[16], |
| 129 | const unsigned char b[16]); |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 130 | |
Manuel Pégourié-Gonnard | 01e31bb | 2013-12-28 15:58:30 +0100 | [diff] [blame] | 131 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 132 | * \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é-Gonnard | 01e31bb | 2013-12-28 15:58:30 +0100 | [diff] [blame] | 137 | * |
| 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 Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 142 | void mbedtls_aesni_inverse_key(unsigned char *invkey, |
| 143 | const unsigned char *fwdkey, |
| 144 | int nr); |
Manuel Pégourié-Gonnard | 01e31bb | 2013-12-28 15:58:30 +0100 | [diff] [blame] | 145 | |
Manuel Pégourié-Gonnard | 47a3536 | 2013-12-28 20:45:04 +0100 | [diff] [blame] | 146 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 147 | * \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é-Gonnard | 47a3536 | 2013-12-28 20:45:04 +0100 | [diff] [blame] | 151 | * |
| 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é-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 156 | * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH |
Manuel Pégourié-Gonnard | 47a3536 | 2013-12-28 20:45:04 +0100 | [diff] [blame] | 157 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 158 | int mbedtls_aesni_setkey_enc(unsigned char *rk, |
| 159 | const unsigned char *key, |
| 160 | size_t bits); |
Manuel Pégourié-Gonnard | 47a3536 | 2013-12-28 20:45:04 +0100 | [diff] [blame] | 161 | |
Manuel Pégourié-Gonnard | 1a90147 | 2015-03-10 16:12:29 +0000 | [diff] [blame] | 162 | #ifdef __cplusplus |
| 163 | } |
| 164 | #endif |
| 165 | |
Gilles Peskine | 5511a34 | 2023-03-10 22:29:32 +0100 | [diff] [blame] | 166 | #endif /* MBEDTLS_AESNI_HAVE_CODE */ |
Pengyu Lv | 20384f4 | 2023-09-21 10:14:16 +0800 | [diff] [blame] | 167 | #endif /* MBEDTLS_AESNI_C && (MBEDTLS_HAVE_X86_64 || MBEDTLS_HAVE_X86) */ |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 168 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 169 | #endif /* MBEDTLS_AESNI_H */ |