blob: 91a4e0f1160583793e4f5e35cf057d6d11217a91 [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
Manuel Pégourié-Gonnardad54c492018-12-13 11:15:26 +01005 *
Manuel Pégourié-Gonnardb66e7db2018-12-18 09:57:18 +01006 * \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/*
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +020010 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Bence Szépkútif744bd72020-06-05 13:02:18 +020011 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
12 *
13 * This file is provided under the Apache License 2.0, or the
14 * GNU General Public License v2.0 or later.
15 *
16 * **********
17 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020018 *
19 * Licensed under the Apache License, Version 2.0 (the "License"); you may
20 * not use this file except in compliance with the License.
21 * You may obtain a copy of the License at
22 *
23 * http://www.apache.org/licenses/LICENSE-2.0
24 *
25 * Unless required by applicable law or agreed to in writing, software
26 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
27 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28 * See the License for the specific language governing permissions and
29 * limitations under the License.
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010030 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020031 * **********
32 *
33 * **********
34 * GNU General Public License v2.0 or later:
35 *
36 * This program is free software; you can redistribute it and/or modify
37 * it under the terms of the GNU General Public License as published by
38 * the Free Software Foundation; either version 2 of the License, or
39 * (at your option) any later version.
40 *
41 * This program is distributed in the hope that it will be useful,
42 * but WITHOUT ANY WARRANTY; without even the implied warranty of
43 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
44 * GNU General Public License for more details.
45 *
46 * You should have received a copy of the GNU General Public License along
47 * with this program; if not, write to the Free Software Foundation, Inc.,
48 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
49 *
50 * **********
51 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000052 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010053 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#ifndef MBEDTLS_AESNI_H
55#define MBEDTLS_AESNI_H
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010056
Ron Eldor8b0cf2e2018-02-14 16:02:41 +020057#if !defined(MBEDTLS_CONFIG_FILE)
58#include "config.h"
59#else
60#include MBEDTLS_CONFIG_FILE
61#endif
62
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010063#include "aes.h"
64
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#define MBEDTLS_AESNI_AES 0x02000000u
66#define MBEDTLS_AESNI_CLMUL 0x00000002u
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && \
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010069 ( defined(__amd64__) || defined(__x86_64__) ) && \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 ! defined(MBEDTLS_HAVE_X86_64)
71#define MBEDTLS_HAVE_X86_64
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010072#endif
73
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#if defined(MBEDTLS_HAVE_X86_64)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010075
Manuel Pégourié-Gonnard1a901472015-03-10 16:12:29 +000076#ifdef __cplusplus
77extern "C" {
78#endif
79
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010080/**
Manuel Pégourié-Gonnardb66e7db2018-12-18 09:57:18 +010081 * \brief Internal function to detect the AES-NI feature in CPUs.
Manuel Pégourié-Gonnardad54c492018-12-13 11:15:26 +010082 *
83 * \note This function is only for internal use by other library
84 * functions; you must not call it directly.
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010085 *
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010086 * \param what The feature to detect
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 * (MBEDTLS_AESNI_AES or MBEDTLS_AESNI_CLMUL)
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010088 *
89 * \return 1 if CPU has support for the feature, 0 otherwise
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010090 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +010091int mbedtls_aesni_has_support( unsigned int what );
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010092
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010093/**
Manuel Pégourié-Gonnardb66e7db2018-12-18 09:57:18 +010094 * \brief Internal AES-NI AES-ECB block encryption and decryption
Manuel Pégourié-Gonnardad54c492018-12-13 11:15:26 +010095 *
96 * \note This function is only for internal use by other library
97 * functions; you must not call it directly.
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010098 *
99 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100101 * \param input 16-byte input block
102 * \param output 16-byte output block
103 *
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100104 * \return 0 on success (cannot fail)
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100105 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106int mbedtls_aesni_crypt_ecb( mbedtls_aes_context *ctx,
Manuel Pégourié-Gonnardad54c492018-12-13 11:15:26 +0100107 int mode,
108 const unsigned char input[16],
109 unsigned char output[16] );
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100110
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100111/**
Manuel Pégourié-Gonnardad54c492018-12-13 11:15:26 +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.
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100116 *
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.
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100123 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124void mbedtls_aesni_gcm_mult( unsigned char c[16],
Manuel Pégourié-Gonnardad54c492018-12-13 11:15:26 +0100125 const unsigned char a[16],
126 const unsigned char b[16] );
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100127
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100128/**
Manuel Pégourié-Gonnardb66e7db2018-12-18 09:57:18 +0100129 * \brief Internal round key inversion. This function computes
130 * decryption round keys from the encryption round keys.
Manuel Pégourié-Gonnardad54c492018-12-13 11:15:26 +0100131 *
132 * \note This function is only for internal use by other library
133 * functions; you must not call it directly.
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100134 *
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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139void mbedtls_aesni_inverse_key( unsigned char *invkey,
Manuel Pégourié-Gonnardad54c492018-12-13 11:15:26 +0100140 const unsigned char *fwdkey,
141 int nr );
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100142
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100143/**
Manuel Pégourié-Gonnardb66e7db2018-12-18 09:57:18 +0100144 * \brief Internal key expansion for encryption
Manuel Pégourié-Gonnardad54c492018-12-13 11:15:26 +0100145 *
146 * \note This function is only for internal use by other library
147 * functions; you must not call it directly.
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100148 *
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 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100154 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155int mbedtls_aesni_setkey_enc( unsigned char *rk,
Manuel Pégourié-Gonnardad54c492018-12-13 11:15:26 +0100156 const unsigned char *key,
157 size_t bits );
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100158
Manuel Pégourié-Gonnard1a901472015-03-10 16:12:29 +0000159#ifdef __cplusplus
160}
161#endif
162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163#endif /* MBEDTLS_HAVE_X86_64 */
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165#endif /* MBEDTLS_AESNI_H */