blob: 51b770f31657a15d2bd75132ddbc1172634f8f83 [file] [log] [blame]
Jens Wiklander817466c2018-05-22 13:49:31 +02001/**
2 * \file aesni.h
3 *
4 * \brief AES-NI for hardware AES acceleration on some Intel processors
5 *
Jens Wiklander3d3b0592019-03-20 15:30:29 +01006 * \warning These functions are only for internal use by other library
7 * functions; you must not call them directly.
8 */
9/*
Jerome Forissier79013242021-07-28 10:24:04 +020010 * Copyright The Mbed TLS Contributors
11 * SPDX-License-Identifier: Apache-2.0
Jens Wiklander817466c2018-05-22 13:49:31 +020012 *
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.
Jens Wiklander817466c2018-05-22 13:49:31 +020024 */
25#ifndef MBEDTLS_AESNI_H
26#define MBEDTLS_AESNI_H
27
Jens Wiklander32b31802023-10-06 16:59:46 +020028#include "mbedtls/build_info.h"
Jerome Forissier5b25c762020-04-07 11:18:49 +020029
Jerome Forissier11fa71b2020-04-20 17:17:56 +020030#include "mbedtls/aes.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020031
32#define MBEDTLS_AESNI_AES 0x02000000u
33#define MBEDTLS_AESNI_CLMUL 0x00000002u
34
Jens Wiklander32b31802023-10-06 16:59:46 +020035/* Can we do AESNI with inline assembly?
36 * (Only implemented with gas syntax, only for 64-bit.)
37 */
38#if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && \
39 (defined(__amd64__) || defined(__x86_64__)) && \
40 !defined(MBEDTLS_HAVE_X86_64)
Jens Wiklander817466c2018-05-22 13:49:31 +020041#define MBEDTLS_HAVE_X86_64
42#endif
43
Jens Wiklander32b31802023-10-06 16:59:46 +020044#if defined(MBEDTLS_AESNI_C)
45
46/* Can we do AESNI with intrinsics?
47 * (Only implemented with certain compilers, only for certain targets.)
48 */
49#undef MBEDTLS_AESNI_HAVE_INTRINSICS
50#if defined(_MSC_VER)
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
54#endif
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
60#endif
61
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. */
Jens Wiklander817466c2018-05-22 13:49:31 +020068#if defined(MBEDTLS_HAVE_X86_64)
Jens Wiklander32b31802023-10-06 16:59:46 +020069#define MBEDTLS_AESNI_HAVE_CODE 1 // via assembly
70#elif defined(MBEDTLS_AESNI_HAVE_INTRINSICS)
71#define MBEDTLS_AESNI_HAVE_CODE 2 // via intrinsics
72#endif
73
74#if defined(MBEDTLS_AESNI_HAVE_CODE)
Jens Wiklander817466c2018-05-22 13:49:31 +020075
76#ifdef __cplusplus
77extern "C" {
78#endif
79
80/**
Jens Wiklander3d3b0592019-03-20 15:30:29 +010081 * \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.
Jens Wiklander817466c2018-05-22 13:49:31 +020085 *
86 * \param what The feature to detect
87 * (MBEDTLS_AESNI_AES or MBEDTLS_AESNI_CLMUL)
88 *
89 * \return 1 if CPU has support for the feature, 0 otherwise
90 */
Jens Wiklander32b31802023-10-06 16:59:46 +020091int mbedtls_aesni_has_support(unsigned int what);
Jens Wiklander817466c2018-05-22 13:49:31 +020092
93/**
Jens Wiklander3d3b0592019-03-20 15:30:29 +010094 * \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.
Jens Wiklander817466c2018-05-22 13:49:31 +020098 *
99 * \param ctx AES context
100 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
101 * \param input 16-byte input block
102 * \param output 16-byte output block
103 *
104 * \return 0 on success (cannot fail)
105 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200106int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
107 int mode,
108 const unsigned char input[16],
109 unsigned char output[16]);
Jens Wiklander817466c2018-05-22 13:49:31 +0200110
111/**
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100112 * \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.
Jens Wiklander817466c2018-05-22 13:49:31 +0200116 *
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.
123 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200124void mbedtls_aesni_gcm_mult(unsigned char c[16],
125 const unsigned char a[16],
126 const unsigned char b[16]);
Jens Wiklander817466c2018-05-22 13:49:31 +0200127
128/**
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100129 * \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.
Jens Wiklander817466c2018-05-22 13:49:31 +0200134 *
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 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200139void mbedtls_aesni_inverse_key(unsigned char *invkey,
140 const unsigned char *fwdkey,
141 int nr);
Jens Wiklander817466c2018-05-22 13:49:31 +0200142
143/**
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100144 * \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.
Jens Wiklander817466c2018-05-22 13:49:31 +0200148 *
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 *
153 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
154 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200155int mbedtls_aesni_setkey_enc(unsigned char *rk,
156 const unsigned char *key,
157 size_t bits);
Jens Wiklander817466c2018-05-22 13:49:31 +0200158
159#ifdef __cplusplus
160}
161#endif
162
Jens Wiklander32b31802023-10-06 16:59:46 +0200163#endif /* MBEDTLS_AESNI_HAVE_CODE */
164#endif /* MBEDTLS_AESNI_C */
Jens Wiklander817466c2018-05-22 13:49:31 +0200165
166#endif /* MBEDTLS_AESNI_H */