blob: c1c4bdd8f8e5d5fcdb49a6e5210e505e0805d995 [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 Peskine9af58cd2023-03-10 22:29:32 +010035#if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && \
Gilles Peskine449bd832023-01-11 14:50:10 +010036 (defined(__amd64__) || defined(__x86_64__)) && \
37 !defined(MBEDTLS_HAVE_X86_64)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#define MBEDTLS_HAVE_X86_64
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010039#endif
40
Gilles Peskine9af58cd2023-03-10 22:29:32 +010041#if defined(MBEDTLS_AESNI_C)
42
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_HAVE_X86_64)
Gilles Peskine9af58cd2023-03-10 22:29:32 +010044#define MBEDTLS_AESNI_HAVE_CODE // via assembly
45#endif
46
47#if defined(_MSC_VER)
48#define MBEDTLS_HAVE_AESNI_INTRINSICS
49#endif
50#if defined(__GNUC__) && defined(__AES__)
51#define MBEDTLS_HAVE_AESNI_INTRINSICS
52#endif
53
54#if defined(MBEDTLS_HAVE_AESNI_INTRINSICS)
55#define MBEDTLS_AESNI_HAVE_CODE // via intrinsics
56#endif
57
58#if defined(MBEDTLS_AESNI_HAVE_CODE)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010059
Manuel Pégourié-Gonnard1a901472015-03-10 16:12:29 +000060#ifdef __cplusplus
61extern "C" {
62#endif
63
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010064/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050065 * \brief Internal function to detect the AES-NI feature in CPUs.
66 *
67 * \note This function is only for internal use by other library
68 * functions; you must not call it directly.
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010069 *
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010070 * \param what The feature to detect
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071 * (MBEDTLS_AESNI_AES or MBEDTLS_AESNI_CLMUL)
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010072 *
73 * \return 1 if CPU has support for the feature, 0 otherwise
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010074 */
Gilles Peskine449bd832023-01-11 14:50:10 +010075int mbedtls_aesni_has_support(unsigned int what);
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010076
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010077/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050078 * \brief Internal AES-NI AES-ECB block encryption and decryption
79 *
80 * \note This function is only for internal use by other library
81 * functions; you must not call it directly.
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010082 *
83 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010085 * \param input 16-byte input block
86 * \param output 16-byte output block
87 *
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +010088 * \return 0 on success (cannot fail)
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010089 */
Gilles Peskine449bd832023-01-11 14:50:10 +010090int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
91 int mode,
92 const unsigned char input[16],
93 unsigned char output[16]);
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010094
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +010095/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050096 * \brief Internal GCM multiplication: c = a * b in GF(2^128)
97 *
98 * \note This function is only for internal use by other library
99 * functions; you must not call it directly.
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100100 *
101 * \param c Result
102 * \param a First operand
103 * \param b Second operand
104 *
105 * \note Both operands and result are bit strings interpreted as
106 * elements of GF(2^128) as per the GCM spec.
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100107 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100108void mbedtls_aesni_gcm_mult(unsigned char c[16],
109 const unsigned char a[16],
110 const unsigned char b[16]);
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100111
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100112/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500113 * \brief Internal round key inversion. This function computes
114 * decryption round keys from the encryption round keys.
115 *
116 * \note This function is only for internal use by other library
117 * functions; you must not call it directly.
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100118 *
119 * \param invkey Round keys for the equivalent inverse cipher
120 * \param fwdkey Original round keys (for encryption)
121 * \param nr Number of rounds (that is, number of round keys minus one)
122 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100123void mbedtls_aesni_inverse_key(unsigned char *invkey,
124 const unsigned char *fwdkey,
125 int nr);
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100126
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100127/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500128 * \brief Internal key expansion for encryption
129 *
130 * \note This function is only for internal use by other library
131 * functions; you must not call it directly.
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100132 *
133 * \param rk Destination buffer where the round keys are written
134 * \param key Encryption key
135 * \param bits Key size in bits (must be 128, 192 or 256)
136 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100138 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100139int mbedtls_aesni_setkey_enc(unsigned char *rk,
140 const unsigned char *key,
141 size_t bits);
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100142
Manuel Pégourié-Gonnard1a901472015-03-10 16:12:29 +0000143#ifdef __cplusplus
144}
145#endif
146
Gilles Peskine9af58cd2023-03-10 22:29:32 +0100147#endif /* MBEDTLS_AESNI_HAVE_CODE */
148#endif /* MBEDTLS_AESNI_C */
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150#endif /* MBEDTLS_AESNI_H */