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