blob: 59e27afd3ec62586bd59524f0c9215159a4a1074 [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
Dave Rodgman16799db2023-11-02 19:47:20 +000011 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010012 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020013#ifndef MBEDTLS_AESNI_H
14#define MBEDTLS_AESNI_H
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010015
Bence Szépkútic662b362021-05-27 11:25:03 +020016#include "mbedtls/build_info.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050017
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010018#include "mbedtls/aes.h"
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020#define MBEDTLS_AESNI_AES 0x02000000u
21#define MBEDTLS_AESNI_CLMUL 0x00000002u
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010022
Jerry Yue62ff092023-08-16 14:15:00 +080023#if defined(MBEDTLS_AESNI_C) && \
Jerry Yud6e312d2023-08-18 17:19:51 +080024 (defined(MBEDTLS_ARCH_IS_X64) || defined(MBEDTLS_ARCH_IS_X86))
Gilles Peskine9af58cd2023-03-10 22:29:32 +010025
Gilles Peskine9c682e72023-03-16 17:21:33 +010026/* Can we do AESNI with intrinsics?
Gilles Peskine30e9f2a2023-03-17 17:29:58 +010027 * (Only implemented with certain compilers, only for certain targets.)
Gilles Peskine9c682e72023-03-16 17:21:33 +010028 */
29#undef MBEDTLS_AESNI_HAVE_INTRINSICS
Sergey Markelov3898f102023-10-16 12:54:48 -070030#if defined(_MSC_VER) && !defined(__clang__)
Gilles Peskine9c682e72023-03-16 17:21:33 +010031/* 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 Peskine9af58cd2023-03-10 22:29:32 +010034#endif
Gilles Peskine9c682e72023-03-16 17:21:33 +010035/* 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`). */
Sergey Markelov3898f102023-10-16 12:54:48 -070038#if (defined(__GNUC__) || defined(__clang__)) && defined(__AES__) && defined(__PCLMUL__)
Gilles Peskine9c682e72023-03-16 17:21:33 +010039#define MBEDTLS_AESNI_HAVE_INTRINSICS
Gilles Peskine9af58cd2023-03-10 22:29:32 +010040#endif
Beniamin Sandu800f2b72023-10-27 16:58:00 +010041/* For 32-bit, we only support intrinsics */
42#if defined(MBEDTLS_ARCH_IS_X86) && (defined(__GNUC__) || defined(__clang__))
43#define MBEDTLS_AESNI_HAVE_INTRINSICS
44#endif
Gilles Peskine9af58cd2023-03-10 22:29:32 +010045
Dave Rodgmane07c6702023-06-16 13:21:28 +010046/* Choose the implementation of AESNI, if one is available.
47 *
48 * Favor the intrinsics-based implementation if it's available, for better
Dave Rodgman6365a682023-05-22 11:14:36 +010049 * 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)
Gilles Peskine9c682e72023-03-16 17:21:33 +010053#define MBEDTLS_AESNI_HAVE_CODE 2 // via intrinsics
Jerry Yue62ff092023-08-16 14:15:00 +080054#elif defined(MBEDTLS_HAVE_ASM) && \
Beniamin Sandu800f2b72023-10-27 16:58:00 +010055 (defined(__GNUC__) || defined(__clang__)) && defined(MBEDTLS_ARCH_IS_X64)
Jerry Yuf65f71e2023-08-28 10:58:24 +080056/* Can we do AESNI with inline assembly?
57 * (Only implemented with gas syntax, only for 64-bit.)
58 */
Dave Rodgman6365a682023-05-22 11:14:36 +010059#define MBEDTLS_AESNI_HAVE_CODE 1 // via assembly
Jerry Yu8189f322023-08-10 13:53:41 +080060#else
61#error "MBEDTLS_AESNI_C defined, but neither intrinsics nor assembly available"
Gilles Peskine9af58cd2023-03-10 22:29:32 +010062#endif
63
64#if defined(MBEDTLS_AESNI_HAVE_CODE)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010065
Manuel Pégourié-Gonnard1a901472015-03-10 16:12:29 +000066#ifdef __cplusplus
67extern "C" {
68#endif
69
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010070/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050071 * \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.
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010075 *
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010076 * \param what The feature to detect
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 * (MBEDTLS_AESNI_AES or MBEDTLS_AESNI_CLMUL)
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010078 *
79 * \return 1 if CPU has support for the feature, 0 otherwise
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010080 */
Jerry Yu0a6272d2023-08-18 17:35:59 +080081#if !defined(MBEDTLS_AES_USE_HARDWARE_ONLY)
Gilles Peskine449bd832023-01-11 14:50:10 +010082int mbedtls_aesni_has_support(unsigned int what);
Jerry Yu0d4f4e52023-03-31 14:32:47 +080083#else
Jerry Yu9c0b7d12023-08-04 17:25:59 +080084#define mbedtls_aesni_has_support(what) 1
Jerry Yu0d4f4e52023-03-31 14:32:47 +080085#endif
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010086
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010087/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050088 * \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.
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010092 *
93 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010095 * \param input 16-byte input block
96 * \param output 16-byte output block
97 *
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +010098 * \return 0 on success (cannot fail)
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010099 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100100int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
101 int mode,
102 const unsigned char input[16],
103 unsigned char output[16]);
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100104
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100105/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500106 * \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.
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100110 *
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.
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100117 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100118void mbedtls_aesni_gcm_mult(unsigned char c[16],
119 const unsigned char a[16],
120 const unsigned char b[16]);
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100121
Yanray Wangb67b4742023-10-31 17:10:32 +0800122#if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100123/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500124 * \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.
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100129 *
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 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100134void mbedtls_aesni_inverse_key(unsigned char *invkey,
135 const unsigned char *fwdkey,
136 int nr);
Yanray Wangb67b4742023-10-31 17:10:32 +0800137#endif /* !MBEDTLS_BLOCK_CIPHER_NO_DECRYPT */
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100138
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100139/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500140 * \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.
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100144 *
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 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100150 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100151int mbedtls_aesni_setkey_enc(unsigned char *rk,
152 const unsigned char *key,
153 size_t bits);
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100154
Manuel Pégourié-Gonnard1a901472015-03-10 16:12:29 +0000155#ifdef __cplusplus
156}
157#endif
158
Gilles Peskine9af58cd2023-03-10 22:29:32 +0100159#endif /* MBEDTLS_AESNI_HAVE_CODE */
Pengyu Lv6f0259e2023-09-21 10:34:32 +0800160#endif /* MBEDTLS_AESNI_C && (MBEDTLS_ARCH_IS_X64 || MBEDTLS_ARCH_IS_X86) */
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162#endif /* MBEDTLS_AESNI_H */