blob: b3d49e43806ca23436316e7b96bad573ea9a19f7 [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
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050028#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010029#include "mbedtls/config.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050030#else
31#include MBEDTLS_CONFIG_FILE
32#endif
33
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010034#include "mbedtls/aes.h"
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#define MBEDTLS_AESNI_AES 0x02000000u
37#define MBEDTLS_AESNI_CLMUL 0x00000002u
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010038
Gilles Peskine5511a342023-03-10 22:29:32 +010039#if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010040 (defined(__amd64__) || defined(__x86_64__)) && \
41 !defined(MBEDTLS_HAVE_X86_64)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#define MBEDTLS_HAVE_X86_64
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010043#endif
44
Gilles Peskine5511a342023-03-10 22:29:32 +010045#if defined(MBEDTLS_AESNI_C)
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_HAVE_X86_64)
Gilles Peskine5511a342023-03-10 22:29:32 +010048#define MBEDTLS_AESNI_HAVE_CODE // via assembly
49#endif
50
51#if defined(_MSC_VER)
52#define MBEDTLS_HAVE_AESNI_INTRINSICS
53#endif
54#if defined(__GNUC__) && defined(__AES__)
55#define MBEDTLS_HAVE_AESNI_INTRINSICS
56#endif
57
58#if defined(MBEDTLS_HAVE_AESNI_INTRINSICS)
59#define MBEDTLS_AESNI_HAVE_CODE // via intrinsics
60#endif
61
62#if defined(MBEDTLS_AESNI_HAVE_CODE)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010063
Manuel Pégourié-Gonnard1a901472015-03-10 16:12:29 +000064#ifdef __cplusplus
65extern "C" {
66#endif
67
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010068/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050069 * \brief Internal function to detect the AES-NI feature in CPUs.
70 *
71 * \note This function is only for internal use by other library
72 * functions; you must not call it directly.
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010073 *
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010074 * \param what The feature to detect
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 * (MBEDTLS_AESNI_AES or MBEDTLS_AESNI_CLMUL)
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010076 *
77 * \return 1 if CPU has support for the feature, 0 otherwise
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010078 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010079int mbedtls_aesni_has_support(unsigned int what);
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010080
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010081/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050082 * \brief Internal AES-NI AES-ECB block encryption and decryption
83 *
84 * \note This function is only for internal use by other library
85 * functions; you must not call it directly.
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010086 *
87 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010089 * \param input 16-byte input block
90 * \param output 16-byte output block
91 *
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +010092 * \return 0 on success (cannot fail)
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010093 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010094int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
95 int mode,
96 const unsigned char input[16],
97 unsigned char output[16]);
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010098
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +010099/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500100 * \brief Internal GCM multiplication: c = a * b in GF(2^128)
101 *
102 * \note This function is only for internal use by other library
103 * functions; you must not call it directly.
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100104 *
105 * \param c Result
106 * \param a First operand
107 * \param b Second operand
108 *
109 * \note Both operands and result are bit strings interpreted as
110 * elements of GF(2^128) as per the GCM spec.
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100111 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100112void mbedtls_aesni_gcm_mult(unsigned char c[16],
113 const unsigned char a[16],
114 const unsigned char b[16]);
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100115
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100116/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500117 * \brief Internal round key inversion. This function computes
118 * decryption round keys from the encryption round keys.
119 *
120 * \note This function is only for internal use by other library
121 * functions; you must not call it directly.
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100122 *
123 * \param invkey Round keys for the equivalent inverse cipher
124 * \param fwdkey Original round keys (for encryption)
125 * \param nr Number of rounds (that is, number of round keys minus one)
126 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100127void mbedtls_aesni_inverse_key(unsigned char *invkey,
128 const unsigned char *fwdkey,
129 int nr);
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100130
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100131/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500132 * \brief Internal key expansion for encryption
133 *
134 * \note This function is only for internal use by other library
135 * functions; you must not call it directly.
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100136 *
137 * \param rk Destination buffer where the round keys are written
138 * \param key Encryption key
139 * \param bits Key size in bits (must be 128, 192 or 256)
140 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100142 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100143int mbedtls_aesni_setkey_enc(unsigned char *rk,
144 const unsigned char *key,
145 size_t bits);
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100146
Manuel Pégourié-Gonnard1a901472015-03-10 16:12:29 +0000147#ifdef __cplusplus
148}
149#endif
150
Gilles Peskine5511a342023-03-10 22:29:32 +0100151#endif /* MBEDTLS_AESNI_HAVE_CODE */
152#endif /* MBEDTLS_AESNI_C */
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154#endif /* MBEDTLS_AESNI_H */