blob: 845da74e8c164d17bb94c8c74d634835fa7d77d6 [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/*
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02007 * Copyright The Mbed TLS Contributors
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 * **********
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010048 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#ifndef MBEDTLS_AESNI_H
50#define MBEDTLS_AESNI_H
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010051
Ron Eldor0559c662018-02-14 16:02:41 +020052#if !defined(MBEDTLS_CONFIG_FILE)
53#include "config.h"
54#else
55#include MBEDTLS_CONFIG_FILE
56#endif
57
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010058#include "aes.h"
59
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#define MBEDTLS_AESNI_AES 0x02000000u
61#define MBEDTLS_AESNI_CLMUL 0x00000002u
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && \
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010064 ( defined(__amd64__) || defined(__x86_64__) ) && \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065 ! defined(MBEDTLS_HAVE_X86_64)
66#define MBEDTLS_HAVE_X86_64
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010067#endif
68
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if defined(MBEDTLS_HAVE_X86_64)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010070
Manuel Pégourié-Gonnard1a901472015-03-10 16:12:29 +000071#ifdef __cplusplus
72extern "C" {
73#endif
74
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010075/**
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010076 * \brief AES-NI features detection routine
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010077 *
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010078 * \param what The feature to detect
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079 * (MBEDTLS_AESNI_AES or MBEDTLS_AESNI_CLMUL)
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010080 *
81 * \return 1 if CPU has support for the feature, 0 otherwise
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010082 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +010083int mbedtls_aesni_has_support( unsigned int what );
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010084
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010085/**
86 * \brief AES-NI AES-ECB block en(de)cryption
87 *
88 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010090 * \param input 16-byte input block
91 * \param output 16-byte output block
92 *
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +010093 * \return 0 on success (cannot fail)
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010094 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095int mbedtls_aesni_crypt_ecb( mbedtls_aes_context *ctx,
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010096 int mode,
97 const unsigned char input[16],
98 unsigned char output[16] );
99
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100100/**
101 * \brief GCM multiplication: c = a * b in GF(2^128)
102 *
103 * \param c Result
104 * \param a First operand
105 * \param b Second operand
106 *
107 * \note Both operands and result are bit strings interpreted as
108 * elements of GF(2^128) as per the GCM spec.
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110void mbedtls_aesni_gcm_mult( unsigned char c[16],
Manuel Pégourié-Gonnardd4588cf2013-12-30 13:54:23 +0100111 const unsigned char a[16],
112 const unsigned char b[16] );
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100113
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100114/**
115 * \brief Compute decryption round keys from encryption round keys
116 *
117 * \param invkey Round keys for the equivalent inverse cipher
118 * \param fwdkey Original round keys (for encryption)
119 * \param nr Number of rounds (that is, number of round keys minus one)
120 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121void mbedtls_aesni_inverse_key( unsigned char *invkey,
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100122 const unsigned char *fwdkey, int nr );
123
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100124/**
125 * \brief Perform key expansion (for encryption)
126 *
127 * \param rk Destination buffer where the round keys are written
128 * \param key Encryption key
129 * \param bits Key size in bits (must be 128, 192 or 256)
130 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133int mbedtls_aesni_setkey_enc( unsigned char *rk,
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100134 const unsigned char *key,
135 size_t bits );
136
Manuel Pégourié-Gonnard1a901472015-03-10 16:12:29 +0000137#ifdef __cplusplus
138}
139#endif
140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141#endif /* MBEDTLS_HAVE_X86_64 */
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143#endif /* MBEDTLS_AESNI_H */