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 | e3c0585 | 2023-11-03 12:21:36 +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 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 16 | #include "mbedtls/build_info.h" |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 17 | |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 18 | #include "mbedtls/aes.h" |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 19 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 20 | #define MBEDTLS_AESNI_AES 0x02000000u |
| 21 | #define MBEDTLS_AESNI_CLMUL 0x00000002u |
Manuel Pégourié-Gonnard | 8eaf20b | 2013-12-18 19:14:53 +0100 | [diff] [blame] | 22 | |
Jerry Yu | e62ff09 | 2023-08-16 14:15:00 +0800 | [diff] [blame] | 23 | #if defined(MBEDTLS_AESNI_C) && \ |
Jerry Yu | d6e312d | 2023-08-18 17:19:51 +0800 | [diff] [blame] | 24 | (defined(MBEDTLS_ARCH_IS_X64) || defined(MBEDTLS_ARCH_IS_X86)) |
Gilles Peskine | 9af58cd | 2023-03-10 22:29:32 +0100 | [diff] [blame] | 25 | |
Gilles Peskine | 9c682e7 | 2023-03-16 17:21:33 +0100 | [diff] [blame] | 26 | /* Can we do AESNI with intrinsics? |
Gilles Peskine | 30e9f2a | 2023-03-17 17:29:58 +0100 | [diff] [blame] | 27 | * (Only implemented with certain compilers, only for certain targets.) |
Gilles Peskine | 9c682e7 | 2023-03-16 17:21:33 +0100 | [diff] [blame] | 28 | */ |
| 29 | #undef MBEDTLS_AESNI_HAVE_INTRINSICS |
Gilles Peskine | 9af58cd | 2023-03-10 22:29:32 +0100 | [diff] [blame] | 30 | #if defined(_MSC_VER) |
Gilles Peskine | 9c682e7 | 2023-03-16 17:21:33 +0100 | [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 |
Gilles Peskine | 9af58cd | 2023-03-10 22:29:32 +0100 | [diff] [blame] | 34 | #endif |
Gilles Peskine | 9c682e7 | 2023-03-16 17:21:33 +0100 | [diff] [blame] | 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`). */ |
| 38 | #if defined(__GNUC__) && defined(__AES__) && defined(__PCLMUL__) |
| 39 | #define MBEDTLS_AESNI_HAVE_INTRINSICS |
Gilles Peskine | 9af58cd | 2023-03-10 22:29:32 +0100 | [diff] [blame] | 40 | #endif |
| 41 | |
Dave Rodgman | e07c670 | 2023-06-16 13:21:28 +0100 | [diff] [blame] | 42 | /* Choose the implementation of AESNI, if one is available. |
| 43 | * |
| 44 | * Favor the intrinsics-based implementation if it's available, for better |
Dave Rodgman | 6365a68 | 2023-05-22 11:14:36 +0100 | [diff] [blame] | 45 | * maintainability. |
| 46 | * Performance is about the same (see #7380). |
| 47 | * In the long run, we will likely remove the assembly implementation. */ |
| 48 | #if defined(MBEDTLS_AESNI_HAVE_INTRINSICS) |
Gilles Peskine | 9c682e7 | 2023-03-16 17:21:33 +0100 | [diff] [blame] | 49 | #define MBEDTLS_AESNI_HAVE_CODE 2 // via intrinsics |
Jerry Yu | e62ff09 | 2023-08-16 14:15:00 +0800 | [diff] [blame] | 50 | #elif defined(MBEDTLS_HAVE_ASM) && \ |
Jerry Yu | d6e312d | 2023-08-18 17:19:51 +0800 | [diff] [blame] | 51 | defined(__GNUC__) && defined(MBEDTLS_ARCH_IS_X64) |
Jerry Yu | f65f71e | 2023-08-28 10:58:24 +0800 | [diff] [blame] | 52 | /* Can we do AESNI with inline assembly? |
| 53 | * (Only implemented with gas syntax, only for 64-bit.) |
| 54 | */ |
Dave Rodgman | 6365a68 | 2023-05-22 11:14:36 +0100 | [diff] [blame] | 55 | #define MBEDTLS_AESNI_HAVE_CODE 1 // via assembly |
Jerry Yu | 3ce0398 | 2023-08-16 17:22:18 +0800 | [diff] [blame] | 56 | #elif defined(__GNUC__) |
| 57 | # error "Must use `-mpclmul -msse2 -maes` for MBEDTLS_AESNI_C" |
Jerry Yu | 8189f32 | 2023-08-10 13:53:41 +0800 | [diff] [blame] | 58 | #else |
| 59 | #error "MBEDTLS_AESNI_C defined, but neither intrinsics nor assembly available" |
Gilles Peskine | 9af58cd | 2023-03-10 22:29:32 +0100 | [diff] [blame] | 60 | #endif |
| 61 | |
| 62 | #if defined(MBEDTLS_AESNI_HAVE_CODE) |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 63 | |
Manuel Pégourié-Gonnard | 1a90147 | 2015-03-10 16:12:29 +0000 | [diff] [blame] | 64 | #ifdef __cplusplus |
| 65 | extern "C" { |
| 66 | #endif |
| 67 | |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 68 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 69 | * \brief Internal function to detect the AES-NI feature in CPUs. |
| 70 | * |
| 71 | * \note This function is only for internal use by other library |
| 72 | * functions; you must not call it directly. |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 73 | * |
Manuel Pégourié-Gonnard | 8eaf20b | 2013-12-18 19:14:53 +0100 | [diff] [blame] | 74 | * \param what The feature to detect |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 75 | * (MBEDTLS_AESNI_AES or MBEDTLS_AESNI_CLMUL) |
Manuel Pégourié-Gonnard | 8eaf20b | 2013-12-18 19:14:53 +0100 | [diff] [blame] | 76 | * |
| 77 | * \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] | 78 | */ |
Jerry Yu | 0a6272d | 2023-08-18 17:35:59 +0800 | [diff] [blame] | 79 | #if !defined(MBEDTLS_AES_USE_HARDWARE_ONLY) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 80 | int mbedtls_aesni_has_support(unsigned int what); |
Jerry Yu | 0d4f4e5 | 2023-03-31 14:32:47 +0800 | [diff] [blame] | 81 | #else |
Jerry Yu | 9c0b7d1 | 2023-08-04 17:25:59 +0800 | [diff] [blame] | 82 | #define mbedtls_aesni_has_support(what) 1 |
Jerry Yu | 0d4f4e5 | 2023-03-31 14:32:47 +0800 | [diff] [blame] | 83 | #endif |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 84 | |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 85 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 86 | * \brief Internal AES-NI AES-ECB block encryption and decryption |
| 87 | * |
| 88 | * \note This function is only for internal use by other library |
| 89 | * functions; you must not call it directly. |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 90 | * |
| 91 | * \param ctx AES context |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 92 | * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 93 | * \param input 16-byte input block |
| 94 | * \param output 16-byte output block |
| 95 | * |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 96 | * \return 0 on success (cannot fail) |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 97 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 98 | int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx, |
| 99 | int mode, |
| 100 | const unsigned char input[16], |
| 101 | unsigned char output[16]); |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 102 | |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 103 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 104 | * \brief Internal GCM multiplication: c = a * b in GF(2^128) |
| 105 | * |
| 106 | * \note This function is only for internal use by other library |
| 107 | * functions; you must not call it directly. |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 108 | * |
| 109 | * \param c Result |
| 110 | * \param a First operand |
| 111 | * \param b Second operand |
| 112 | * |
| 113 | * \note Both operands and result are bit strings interpreted as |
| 114 | * elements of GF(2^128) as per the GCM spec. |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 115 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 116 | void mbedtls_aesni_gcm_mult(unsigned char c[16], |
| 117 | const unsigned char a[16], |
| 118 | const unsigned char b[16]); |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 119 | |
Manuel Pégourié-Gonnard | 01e31bb | 2013-12-28 15:58:30 +0100 | [diff] [blame] | 120 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 121 | * \brief Internal round key inversion. This function computes |
| 122 | * decryption round keys from the encryption round keys. |
| 123 | * |
| 124 | * \note This function is only for internal use by other library |
| 125 | * functions; you must not call it directly. |
Manuel Pégourié-Gonnard | 01e31bb | 2013-12-28 15:58:30 +0100 | [diff] [blame] | 126 | * |
| 127 | * \param invkey Round keys for the equivalent inverse cipher |
| 128 | * \param fwdkey Original round keys (for encryption) |
| 129 | * \param nr Number of rounds (that is, number of round keys minus one) |
| 130 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 131 | void mbedtls_aesni_inverse_key(unsigned char *invkey, |
| 132 | const unsigned char *fwdkey, |
| 133 | int nr); |
Manuel Pégourié-Gonnard | 01e31bb | 2013-12-28 15:58:30 +0100 | [diff] [blame] | 134 | |
Manuel Pégourié-Gonnard | 47a3536 | 2013-12-28 20:45:04 +0100 | [diff] [blame] | 135 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 136 | * \brief Internal key expansion for encryption |
| 137 | * |
| 138 | * \note This function is only for internal use by other library |
| 139 | * functions; you must not call it directly. |
Manuel Pégourié-Gonnard | 47a3536 | 2013-12-28 20:45:04 +0100 | [diff] [blame] | 140 | * |
| 141 | * \param rk Destination buffer where the round keys are written |
| 142 | * \param key Encryption key |
| 143 | * \param bits Key size in bits (must be 128, 192 or 256) |
| 144 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 145 | * \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] | 146 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 147 | int mbedtls_aesni_setkey_enc(unsigned char *rk, |
| 148 | const unsigned char *key, |
| 149 | size_t bits); |
Manuel Pégourié-Gonnard | 47a3536 | 2013-12-28 20:45:04 +0100 | [diff] [blame] | 150 | |
Manuel Pégourié-Gonnard | 1a90147 | 2015-03-10 16:12:29 +0000 | [diff] [blame] | 151 | #ifdef __cplusplus |
| 152 | } |
| 153 | #endif |
| 154 | |
Gilles Peskine | 9af58cd | 2023-03-10 22:29:32 +0100 | [diff] [blame] | 155 | #endif /* MBEDTLS_AESNI_HAVE_CODE */ |
| 156 | #endif /* MBEDTLS_AESNI_C */ |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 157 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 158 | #endif /* MBEDTLS_AESNI_H */ |