blob: 1c960703f2e0c6d36f0046e68c40f8171ac44cc5 [file] [log] [blame]
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +01001/**
2 * \file aesni.h
3 *
4 * \brief AES-NI for hardware AES acceleration on some Intel processors
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05005 *
6 * \warning These functions are only for internal use by other library
7 * functions; you must not call them directly.
Darryl Greena40a1012018-01-05 15:33:17 +00008 */
9/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020010 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020011 * 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é-Gonnard92ac76f2013-12-16 17:12:53 +010024 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#ifndef MBEDTLS_AESNI_H
26#define MBEDTLS_AESNI_H
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010027
Bence Szépkútic662b362021-05-27 11:25:03 +020028#include "mbedtls/build_info.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050029
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010030#include "mbedtls/aes.h"
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#define MBEDTLS_AESNI_AES 0x02000000u
33#define MBEDTLS_AESNI_CLMUL 0x00000002u
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010034
Gilles Peskine9c682e72023-03-16 17:21:33 +010035/* Can we do AESNI with inline assembly?
36 * (Only implemented with gas syntax, only for 64-bit.)
37 */
Gilles Peskine9af58cd2023-03-10 22:29:32 +010038#if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && \
Gilles Peskine449bd832023-01-11 14:50:10 +010039 (defined(__amd64__) || defined(__x86_64__)) && \
40 !defined(MBEDTLS_HAVE_X86_64)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#define MBEDTLS_HAVE_X86_64
Jerry Yu4d030f32023-04-18 11:25:18 +080042#if !defined(MBEDTLS_AESNI_C) && !defined(MBEDTLS_AES_HAS_NO_PLAIN_C)
43#error "MBEDTLS_AESCE_C defined, but not all prerequisites"
44#endif
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010045#endif
46
Gilles Peskine9af58cd2023-03-10 22:29:32 +010047#if defined(MBEDTLS_AESNI_C)
48
Gilles Peskine9c682e72023-03-16 17:21:33 +010049/* Can we do AESNI with intrinsics?
Gilles Peskine30e9f2a2023-03-17 17:29:58 +010050 * (Only implemented with certain compilers, only for certain targets.)
Gilles Peskine9c682e72023-03-16 17:21:33 +010051 */
52#undef MBEDTLS_AESNI_HAVE_INTRINSICS
Gilles Peskine9af58cd2023-03-10 22:29:32 +010053#if defined(_MSC_VER)
Gilles Peskine9c682e72023-03-16 17:21:33 +010054/* Visual Studio supports AESNI intrinsics since VS 2008 SP1. We only support
55 * VS 2013 and up for other reasons anyway, so no need to check the version. */
56#define MBEDTLS_AESNI_HAVE_INTRINSICS
Gilles Peskine9af58cd2023-03-10 22:29:32 +010057#endif
Gilles Peskine9c682e72023-03-16 17:21:33 +010058/* GCC-like compilers: currently, we only support intrinsics if the requisite
59 * target flag is enabled when building the library (e.g. `gcc -mpclmul -msse2`
60 * or `clang -maes -mpclmul`). */
61#if defined(__GNUC__) && defined(__AES__) && defined(__PCLMUL__)
62#define MBEDTLS_AESNI_HAVE_INTRINSICS
Gilles Peskine9af58cd2023-03-10 22:29:32 +010063#endif
64
Dave Rodgmane07c6702023-06-16 13:21:28 +010065/* Choose the implementation of AESNI, if one is available.
66 *
67 * Favor the intrinsics-based implementation if it's available, for better
Dave Rodgman6365a682023-05-22 11:14:36 +010068 * maintainability.
69 * Performance is about the same (see #7380).
70 * In the long run, we will likely remove the assembly implementation. */
71#if defined(MBEDTLS_AESNI_HAVE_INTRINSICS)
Gilles Peskine9c682e72023-03-16 17:21:33 +010072#define MBEDTLS_AESNI_HAVE_CODE 2 // via intrinsics
Dave Rodgman6365a682023-05-22 11:14:36 +010073#elif defined(MBEDTLS_HAVE_X86_64)
74#define MBEDTLS_AESNI_HAVE_CODE 1 // via assembly
Gilles Peskine9af58cd2023-03-10 22:29:32 +010075#endif
76
77#if defined(MBEDTLS_AESNI_HAVE_CODE)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010078
Manuel Pégourié-Gonnard1a901472015-03-10 16:12:29 +000079#ifdef __cplusplus
80extern "C" {
81#endif
82
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010083/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050084 * \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é-Gonnard92ac76f2013-12-16 17:12:53 +010088 *
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010089 * \param what The feature to detect
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 * (MBEDTLS_AESNI_AES or MBEDTLS_AESNI_CLMUL)
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010091 *
92 * \return 1 if CPU has support for the feature, 0 otherwise
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010093 */
Jerry Yu315fd302023-04-18 11:19:54 +080094#if !defined(MBEDTLS_AES_HAS_NO_PLAIN_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010095int mbedtls_aesni_has_support(unsigned int what);
Jerry Yu0d4f4e52023-03-31 14:32:47 +080096#else
97#define /* no-check-names */ mbedtls_aesni_has_support(what) 1
98#endif
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010099
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100100/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500101 * \brief Internal AES-NI AES-ECB block encryption and decryption
102 *
103 * \note This function is only for internal use by other library
104 * functions; you must not call it directly.
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100105 *
106 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100108 * \param input 16-byte input block
109 * \param output 16-byte output block
110 *
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100111 * \return 0 on success (cannot fail)
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100112 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100113int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
114 int mode,
115 const unsigned char input[16],
116 unsigned char output[16]);
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100117
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100118/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500119 * \brief Internal GCM multiplication: c = a * b in GF(2^128)
120 *
121 * \note This function is only for internal use by other library
122 * functions; you must not call it directly.
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100123 *
124 * \param c Result
125 * \param a First operand
126 * \param b Second operand
127 *
128 * \note Both operands and result are bit strings interpreted as
129 * elements of GF(2^128) as per the GCM spec.
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100130 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100131void mbedtls_aesni_gcm_mult(unsigned char c[16],
132 const unsigned char a[16],
133 const unsigned char b[16]);
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100134
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100135/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500136 * \brief Internal round key inversion. This function computes
137 * decryption round keys from the encryption round keys.
138 *
139 * \note This function is only for internal use by other library
140 * functions; you must not call it directly.
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100141 *
142 * \param invkey Round keys for the equivalent inverse cipher
143 * \param fwdkey Original round keys (for encryption)
144 * \param nr Number of rounds (that is, number of round keys minus one)
145 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100146void mbedtls_aesni_inverse_key(unsigned char *invkey,
147 const unsigned char *fwdkey,
148 int nr);
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100149
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100150/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500151 * \brief Internal key expansion for encryption
152 *
153 * \note This function is only for internal use by other library
154 * functions; you must not call it directly.
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100155 *
156 * \param rk Destination buffer where the round keys are written
157 * \param key Encryption key
158 * \param bits Key size in bits (must be 128, 192 or 256)
159 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100161 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100162int mbedtls_aesni_setkey_enc(unsigned char *rk,
163 const unsigned char *key,
164 size_t bits);
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100165
Manuel Pégourié-Gonnard1a901472015-03-10 16:12:29 +0000166#ifdef __cplusplus
167}
168#endif
169
Gilles Peskine9af58cd2023-03-10 22:29:32 +0100170#endif /* MBEDTLS_AESNI_HAVE_CODE */
171#endif /* MBEDTLS_AESNI_C */
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173#endif /* MBEDTLS_AESNI_H */