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 | |
Gilles Peskine | 9c682e7 | 2023-03-16 17:21:33 +0100 | [diff] [blame] | 35 | /* Can we do AESNI with inline assembly? |
| 36 | * (Only implemented with gas syntax, only for 64-bit.) |
| 37 | */ |
Gilles Peskine | 9af58cd | 2023-03-10 22:29:32 +0100 | [diff] [blame] | 38 | #if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 39 | (defined(__amd64__) || defined(__x86_64__)) && \ |
| 40 | !defined(MBEDTLS_HAVE_X86_64) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 41 | #define MBEDTLS_HAVE_X86_64 |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 42 | #endif |
| 43 | |
Gilles Peskine | 9af58cd | 2023-03-10 22:29:32 +0100 | [diff] [blame] | 44 | #if defined(MBEDTLS_AESNI_C) |
| 45 | |
Gilles Peskine | 9c682e7 | 2023-03-16 17:21:33 +0100 | [diff] [blame] | 46 | /* Can we do AESNI with intrinsics? |
Gilles Peskine | 30e9f2a | 2023-03-17 17:29:58 +0100 | [diff] [blame] | 47 | * (Only implemented with certain compilers, only for certain targets.) |
Gilles Peskine | 9c682e7 | 2023-03-16 17:21:33 +0100 | [diff] [blame] | 48 | */ |
| 49 | #undef MBEDTLS_AESNI_HAVE_INTRINSICS |
Gilles Peskine | 9af58cd | 2023-03-10 22:29:32 +0100 | [diff] [blame] | 50 | #if defined(_MSC_VER) |
Gilles Peskine | 9c682e7 | 2023-03-16 17:21:33 +0100 | [diff] [blame] | 51 | /* Visual Studio supports AESNI intrinsics since VS 2008 SP1. We only support |
| 52 | * VS 2013 and up for other reasons anyway, so no need to check the version. */ |
| 53 | #define MBEDTLS_AESNI_HAVE_INTRINSICS |
Gilles Peskine | 9af58cd | 2023-03-10 22:29:32 +0100 | [diff] [blame] | 54 | #endif |
Gilles Peskine | 9c682e7 | 2023-03-16 17:21:33 +0100 | [diff] [blame] | 55 | /* GCC-like compilers: currently, we only support intrinsics if the requisite |
| 56 | * target flag is enabled when building the library (e.g. `gcc -mpclmul -msse2` |
| 57 | * or `clang -maes -mpclmul`). */ |
| 58 | #if defined(__GNUC__) && defined(__AES__) && defined(__PCLMUL__) |
| 59 | #define MBEDTLS_AESNI_HAVE_INTRINSICS |
Gilles Peskine | 9af58cd | 2023-03-10 22:29:32 +0100 | [diff] [blame] | 60 | #endif |
| 61 | |
Gilles Peskine | 9c682e7 | 2023-03-16 17:21:33 +0100 | [diff] [blame] | 62 | /* Choose the implementation of AESNI, if one is available. */ |
| 63 | #undef MBEDTLS_AESNI_HAVE_CODE |
| 64 | /* To minimize disruption when releasing the intrinsics-based implementation, |
| 65 | * favor the assembly-based implementation if it's available. We intend to |
| 66 | * revise this in a later release of Mbed TLS 3.x. In the long run, we will |
| 67 | * likely remove the assembly implementation. */ |
| 68 | #if defined(MBEDTLS_HAVE_X86_64) |
| 69 | #define MBEDTLS_AESNI_HAVE_CODE 1 // via assembly |
Gilles Peskine | 36b9e47 | 2023-03-17 17:30:29 +0100 | [diff] [blame] | 70 | #elif defined(MBEDTLS_AESNI_HAVE_INTRINSICS) |
Gilles Peskine | 9c682e7 | 2023-03-16 17:21:33 +0100 | [diff] [blame] | 71 | #define MBEDTLS_AESNI_HAVE_CODE 2 // via intrinsics |
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 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 91 | int mbedtls_aesni_has_support(unsigned int what); |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 92 | |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 93 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 94 | * \brief Internal AES-NI AES-ECB block encryption and decryption |
| 95 | * |
| 96 | * \note This function is only for internal use by other library |
| 97 | * functions; you must not call it directly. |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 98 | * |
| 99 | * \param ctx AES context |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 100 | * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 101 | * \param input 16-byte input block |
| 102 | * \param output 16-byte output block |
| 103 | * |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 104 | * \return 0 on success (cannot fail) |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 105 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 106 | int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx, |
| 107 | int mode, |
| 108 | const unsigned char input[16], |
| 109 | unsigned char output[16]); |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 110 | |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 111 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 112 | * \brief Internal GCM multiplication: c = a * b in GF(2^128) |
| 113 | * |
| 114 | * \note This function is only for internal use by other library |
| 115 | * functions; you must not call it directly. |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 116 | * |
| 117 | * \param c Result |
| 118 | * \param a First operand |
| 119 | * \param b Second operand |
| 120 | * |
| 121 | * \note Both operands and result are bit strings interpreted as |
| 122 | * elements of GF(2^128) as per the GCM spec. |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 123 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 124 | void mbedtls_aesni_gcm_mult(unsigned char c[16], |
| 125 | const unsigned char a[16], |
| 126 | const unsigned char b[16]); |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 127 | |
Manuel Pégourié-Gonnard | 01e31bb | 2013-12-28 15:58:30 +0100 | [diff] [blame] | 128 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 129 | * \brief Internal round key inversion. This function computes |
| 130 | * decryption round keys from the encryption round keys. |
| 131 | * |
| 132 | * \note This function is only for internal use by other library |
| 133 | * functions; you must not call it directly. |
Manuel Pégourié-Gonnard | 01e31bb | 2013-12-28 15:58:30 +0100 | [diff] [blame] | 134 | * |
| 135 | * \param invkey Round keys for the equivalent inverse cipher |
| 136 | * \param fwdkey Original round keys (for encryption) |
| 137 | * \param nr Number of rounds (that is, number of round keys minus one) |
| 138 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 139 | void mbedtls_aesni_inverse_key(unsigned char *invkey, |
| 140 | const unsigned char *fwdkey, |
| 141 | int nr); |
Manuel Pégourié-Gonnard | 01e31bb | 2013-12-28 15:58:30 +0100 | [diff] [blame] | 142 | |
Manuel Pégourié-Gonnard | 47a3536 | 2013-12-28 20:45:04 +0100 | [diff] [blame] | 143 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 144 | * \brief Internal key expansion for encryption |
| 145 | * |
| 146 | * \note This function is only for internal use by other library |
| 147 | * functions; you must not call it directly. |
Manuel Pégourié-Gonnard | 47a3536 | 2013-12-28 20:45:04 +0100 | [diff] [blame] | 148 | * |
| 149 | * \param rk Destination buffer where the round keys are written |
| 150 | * \param key Encryption key |
| 151 | * \param bits Key size in bits (must be 128, 192 or 256) |
| 152 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 153 | * \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] | 154 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 155 | int mbedtls_aesni_setkey_enc(unsigned char *rk, |
| 156 | const unsigned char *key, |
| 157 | size_t bits); |
Manuel Pégourié-Gonnard | 47a3536 | 2013-12-28 20:45:04 +0100 | [diff] [blame] | 158 | |
Manuel Pégourié-Gonnard | 1a90147 | 2015-03-10 16:12:29 +0000 | [diff] [blame] | 159 | #ifdef __cplusplus |
| 160 | } |
| 161 | #endif |
| 162 | |
Gilles Peskine | 9af58cd | 2023-03-10 22:29:32 +0100 | [diff] [blame] | 163 | #endif /* MBEDTLS_AESNI_HAVE_CODE */ |
| 164 | #endif /* MBEDTLS_AESNI_C */ |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 165 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 166 | #endif /* MBEDTLS_AESNI_H */ |