Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 1 | /** |
| 2 | * \file aesni.h |
| 3 | * |
| 4 | * \brief AES-NI for hardware AES acceleration on some Intel processors |
| 5 | * |
Jens Wiklander | 3d3b059 | 2019-03-20 15:30:29 +0100 | [diff] [blame] | 6 | * \warning These functions are only for internal use by other library |
| 7 | * functions; you must not call them directly. |
| 8 | */ |
| 9 | /* |
Jerome Forissier | 7901324 | 2021-07-28 10:24:04 +0200 | [diff] [blame] | 10 | * Copyright The Mbed TLS Contributors |
Tom Van Eyck | c163317 | 2024-04-09 18:44:13 +0200 | [diff] [blame] | 11 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 12 | */ |
| 13 | #ifndef MBEDTLS_AESNI_H |
| 14 | #define MBEDTLS_AESNI_H |
| 15 | |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 16 | #include "mbedtls/build_info.h" |
Jerome Forissier | 5b25c76 | 2020-04-07 11:18:49 +0200 | [diff] [blame] | 17 | |
Jerome Forissier | 11fa71b | 2020-04-20 17:17:56 +0200 | [diff] [blame] | 18 | #include "mbedtls/aes.h" |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 19 | |
| 20 | #define MBEDTLS_AESNI_AES 0x02000000u |
| 21 | #define MBEDTLS_AESNI_CLMUL 0x00000002u |
| 22 | |
Tom Van Eyck | c163317 | 2024-04-09 18:44:13 +0200 | [diff] [blame] | 23 | #if defined(MBEDTLS_AESNI_C) && \ |
| 24 | (defined(MBEDTLS_ARCH_IS_X64) || defined(MBEDTLS_ARCH_IS_X86)) |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 25 | |
| 26 | /* Can we do AESNI with intrinsics? |
| 27 | * (Only implemented with certain compilers, only for certain targets.) |
| 28 | */ |
| 29 | #undef MBEDTLS_AESNI_HAVE_INTRINSICS |
Tom Van Eyck | c163317 | 2024-04-09 18:44:13 +0200 | [diff] [blame] | 30 | #if defined(_MSC_VER) && !defined(__clang__) |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 31 | /* Visual Studio supports AESNI intrinsics since VS 2008 SP1. We only support |
| 32 | * VS 2013 and up for other reasons anyway, so no need to check the version. */ |
| 33 | #define MBEDTLS_AESNI_HAVE_INTRINSICS |
| 34 | #endif |
| 35 | /* GCC-like compilers: currently, we only support intrinsics if the requisite |
| 36 | * target flag is enabled when building the library (e.g. `gcc -mpclmul -msse2` |
| 37 | * or `clang -maes -mpclmul`). */ |
Tom Van Eyck | c163317 | 2024-04-09 18:44:13 +0200 | [diff] [blame] | 38 | #if (defined(__GNUC__) || defined(__clang__)) && defined(__AES__) && defined(__PCLMUL__) |
| 39 | #define MBEDTLS_AESNI_HAVE_INTRINSICS |
| 40 | #endif |
| 41 | /* For 32-bit, we only support intrinsics */ |
| 42 | #if defined(MBEDTLS_ARCH_IS_X86) && (defined(__GNUC__) || defined(__clang__)) |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 43 | #define MBEDTLS_AESNI_HAVE_INTRINSICS |
| 44 | #endif |
| 45 | |
Tom Van Eyck | c163317 | 2024-04-09 18:44:13 +0200 | [diff] [blame] | 46 | /* Choose the implementation of AESNI, if one is available. |
| 47 | * |
| 48 | * Favor the intrinsics-based implementation if it's available, for better |
| 49 | * maintainability. |
| 50 | * Performance is about the same (see #7380). |
| 51 | * In the long run, we will likely remove the assembly implementation. */ |
| 52 | #if defined(MBEDTLS_AESNI_HAVE_INTRINSICS) |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 53 | #define MBEDTLS_AESNI_HAVE_CODE 2 // via intrinsics |
Tom Van Eyck | c163317 | 2024-04-09 18:44:13 +0200 | [diff] [blame] | 54 | #elif defined(MBEDTLS_HAVE_ASM) && \ |
| 55 | (defined(__GNUC__) || defined(__clang__)) && defined(MBEDTLS_ARCH_IS_X64) |
| 56 | /* Can we do AESNI with inline assembly? |
| 57 | * (Only implemented with gas syntax, only for 64-bit.) |
| 58 | */ |
| 59 | #define MBEDTLS_AESNI_HAVE_CODE 1 // via assembly |
| 60 | #else |
| 61 | #error "MBEDTLS_AESNI_C defined, but neither intrinsics nor assembly available" |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 62 | #endif |
| 63 | |
| 64 | #if defined(MBEDTLS_AESNI_HAVE_CODE) |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 65 | |
| 66 | #ifdef __cplusplus |
| 67 | extern "C" { |
| 68 | #endif |
| 69 | |
| 70 | /** |
Jens Wiklander | 3d3b059 | 2019-03-20 15:30:29 +0100 | [diff] [blame] | 71 | * \brief Internal function to detect the AES-NI feature in CPUs. |
| 72 | * |
| 73 | * \note This function is only for internal use by other library |
| 74 | * functions; you must not call it directly. |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 75 | * |
| 76 | * \param what The feature to detect |
| 77 | * (MBEDTLS_AESNI_AES or MBEDTLS_AESNI_CLMUL) |
| 78 | * |
| 79 | * \return 1 if CPU has support for the feature, 0 otherwise |
| 80 | */ |
Tom Van Eyck | c163317 | 2024-04-09 18:44:13 +0200 | [diff] [blame] | 81 | #if !defined(MBEDTLS_AES_USE_HARDWARE_ONLY) |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 82 | int mbedtls_aesni_has_support(unsigned int what); |
Tom Van Eyck | c163317 | 2024-04-09 18:44:13 +0200 | [diff] [blame] | 83 | #else |
| 84 | #define mbedtls_aesni_has_support(what) 1 |
| 85 | #endif |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 86 | |
| 87 | /** |
Jens Wiklander | 3d3b059 | 2019-03-20 15:30:29 +0100 | [diff] [blame] | 88 | * \brief Internal AES-NI AES-ECB block encryption and decryption |
| 89 | * |
| 90 | * \note This function is only for internal use by other library |
| 91 | * functions; you must not call it directly. |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 92 | * |
| 93 | * \param ctx AES context |
| 94 | * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT |
| 95 | * \param input 16-byte input block |
| 96 | * \param output 16-byte output block |
| 97 | * |
| 98 | * \return 0 on success (cannot fail) |
| 99 | */ |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 100 | int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx, |
| 101 | int mode, |
| 102 | const unsigned char input[16], |
| 103 | unsigned char output[16]); |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 104 | |
| 105 | /** |
Jens Wiklander | 3d3b059 | 2019-03-20 15:30:29 +0100 | [diff] [blame] | 106 | * \brief Internal GCM multiplication: c = a * b in GF(2^128) |
| 107 | * |
| 108 | * \note This function is only for internal use by other library |
| 109 | * functions; you must not call it directly. |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 110 | * |
| 111 | * \param c Result |
| 112 | * \param a First operand |
| 113 | * \param b Second operand |
| 114 | * |
| 115 | * \note Both operands and result are bit strings interpreted as |
| 116 | * elements of GF(2^128) as per the GCM spec. |
| 117 | */ |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 118 | void mbedtls_aesni_gcm_mult(unsigned char c[16], |
| 119 | const unsigned char a[16], |
| 120 | const unsigned char b[16]); |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 121 | |
Tom Van Eyck | c163317 | 2024-04-09 18:44:13 +0200 | [diff] [blame] | 122 | #if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT) |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 123 | /** |
Jens Wiklander | 3d3b059 | 2019-03-20 15:30:29 +0100 | [diff] [blame] | 124 | * \brief Internal round key inversion. This function computes |
| 125 | * decryption round keys from the encryption round keys. |
| 126 | * |
| 127 | * \note This function is only for internal use by other library |
| 128 | * functions; you must not call it directly. |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 129 | * |
| 130 | * \param invkey Round keys for the equivalent inverse cipher |
| 131 | * \param fwdkey Original round keys (for encryption) |
| 132 | * \param nr Number of rounds (that is, number of round keys minus one) |
| 133 | */ |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 134 | void mbedtls_aesni_inverse_key(unsigned char *invkey, |
| 135 | const unsigned char *fwdkey, |
| 136 | int nr); |
Tom Van Eyck | c163317 | 2024-04-09 18:44:13 +0200 | [diff] [blame] | 137 | #endif /* !MBEDTLS_BLOCK_CIPHER_NO_DECRYPT */ |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 138 | |
| 139 | /** |
Jens Wiklander | 3d3b059 | 2019-03-20 15:30:29 +0100 | [diff] [blame] | 140 | * \brief Internal key expansion for encryption |
| 141 | * |
| 142 | * \note This function is only for internal use by other library |
| 143 | * functions; you must not call it directly. |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 144 | * |
| 145 | * \param rk Destination buffer where the round keys are written |
| 146 | * \param key Encryption key |
| 147 | * \param bits Key size in bits (must be 128, 192 or 256) |
| 148 | * |
| 149 | * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH |
| 150 | */ |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 151 | int mbedtls_aesni_setkey_enc(unsigned char *rk, |
| 152 | const unsigned char *key, |
| 153 | size_t bits); |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 154 | |
| 155 | #ifdef __cplusplus |
| 156 | } |
| 157 | #endif |
| 158 | |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 159 | #endif /* MBEDTLS_AESNI_HAVE_CODE */ |
Tom Van Eyck | c163317 | 2024-04-09 18:44:13 +0200 | [diff] [blame] | 160 | #endif /* MBEDTLS_AESNI_C && (MBEDTLS_ARCH_IS_X64 || MBEDTLS_ARCH_IS_X86) */ |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 161 | |
| 162 | #endif /* MBEDTLS_AESNI_H */ |