blob: 46ffcb7ae0884923c8eea8d2b8a4d09570a9b007 [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
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02007 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
9 *
10 * This file is provided under the Apache License 2.0, or the
11 * GNU General Public License v2.0 or later.
12 *
13 * **********
14 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020015 *
16 * Licensed under the Apache License, Version 2.0 (the "License"); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010027 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020028 * **********
29 *
30 * **********
31 * GNU General Public License v2.0 or later:
32 *
33 * This program is free software; you can redistribute it and/or modify
34 * it under the terms of the GNU General Public License as published by
35 * the Free Software Foundation; either version 2 of the License, or
36 * (at your option) any later version.
37 *
38 * This program is distributed in the hope that it will be useful,
39 * but WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 * GNU General Public License for more details.
42 *
43 * You should have received a copy of the GNU General Public License along
44 * with this program; if not, write to the Free Software Foundation, Inc.,
45 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
46 *
47 * **********
48 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000049 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010050 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#ifndef MBEDTLS_AESNI_H
52#define MBEDTLS_AESNI_H
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010053
Ron Eldor0559c662018-02-14 16:02:41 +020054#if !defined(MBEDTLS_CONFIG_FILE)
55#include "config.h"
56#else
57#include MBEDTLS_CONFIG_FILE
58#endif
59
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010060#include "aes.h"
61
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#define MBEDTLS_AESNI_AES 0x02000000u
63#define MBEDTLS_AESNI_CLMUL 0x00000002u
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && \
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010066 ( defined(__amd64__) || defined(__x86_64__) ) && \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 ! defined(MBEDTLS_HAVE_X86_64)
68#define MBEDTLS_HAVE_X86_64
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010069#endif
70
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#if defined(MBEDTLS_HAVE_X86_64)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010072
Manuel Pégourié-Gonnard1a901472015-03-10 16:12:29 +000073#ifdef __cplusplus
74extern "C" {
75#endif
76
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010077/**
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010078 * \brief AES-NI features detection routine
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010079 *
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010080 * \param what The feature to detect
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 * (MBEDTLS_AESNI_AES or MBEDTLS_AESNI_CLMUL)
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010082 *
83 * \return 1 if CPU has support for the feature, 0 otherwise
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010084 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +010085int mbedtls_aesni_has_support( unsigned int what );
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010086
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010087/**
88 * \brief AES-NI AES-ECB block en(de)cryption
89 *
90 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010092 * \param input 16-byte input block
93 * \param output 16-byte output block
94 *
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +010095 * \return 0 on success (cannot fail)
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010096 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097int mbedtls_aesni_crypt_ecb( mbedtls_aes_context *ctx,
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010098 int mode,
99 const unsigned char input[16],
100 unsigned char output[16] );
101
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100102/**
103 * \brief GCM multiplication: c = a * b in GF(2^128)
104 *
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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112void mbedtls_aesni_gcm_mult( unsigned char c[16],
Manuel Pégourié-Gonnardd4588cf2013-12-30 13:54:23 +0100113 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/**
117 * \brief Compute decryption round keys from encryption round keys
118 *
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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123void mbedtls_aesni_inverse_key( unsigned char *invkey,
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100124 const unsigned char *fwdkey, int nr );
125
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100126/**
127 * \brief Perform key expansion (for encryption)
128 *
129 * \param rk Destination buffer where the round keys are written
130 * \param key Encryption key
131 * \param bits Key size in bits (must be 128, 192 or 256)
132 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135int mbedtls_aesni_setkey_enc( unsigned char *rk,
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100136 const unsigned char *key,
137 size_t bits );
138
Manuel Pégourié-Gonnard1a901472015-03-10 16:12:29 +0000139#ifdef __cplusplus
140}
141#endif
142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143#endif /* MBEDTLS_HAVE_X86_64 */
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145#endif /* MBEDTLS_AESNI_H */