blob: c5a102b2e1c18af5777385487292f850aaa15f04 [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
5 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2013, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +01007 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00008 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +01009 *
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#ifndef MBEDTLS_AESNI_H
25#define MBEDTLS_AESNI_H
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010026
27#include "aes.h"
28
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#define MBEDTLS_AESNI_AES 0x02000000u
30#define MBEDTLS_AESNI_CLMUL 0x00000002u
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && \
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010033 ( defined(__amd64__) || defined(__x86_64__) ) && \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034 ! defined(MBEDTLS_HAVE_X86_64)
35#define MBEDTLS_HAVE_X86_64
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010036#endif
37
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_HAVE_X86_64)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010039
Manuel Pégourié-Gonnard1a901472015-03-10 16:12:29 +000040#ifdef __cplusplus
41extern "C" {
42#endif
43
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010044/**
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010045 * \brief AES-NI features detection routine
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010046 *
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010047 * \param what The feature to detect
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048 * (MBEDTLS_AESNI_AES or MBEDTLS_AESNI_CLMUL)
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010049 *
50 * \return 1 if CPU has support for the feature, 0 otherwise
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010051 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +010052int mbedtls_aesni_has_support( unsigned int what );
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010053
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010054/**
55 * \brief AES-NI AES-ECB block en(de)cryption
56 *
57 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010059 * \param input 16-byte input block
60 * \param output 16-byte output block
61 *
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +010062 * \return 0 on success (cannot fail)
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010063 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064int mbedtls_aesni_crypt_ecb( mbedtls_aes_context *ctx,
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010065 int mode,
66 const unsigned char input[16],
67 unsigned char output[16] );
68
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +010069/**
70 * \brief GCM multiplication: c = a * b in GF(2^128)
71 *
72 * \param c Result
73 * \param a First operand
74 * \param b Second operand
75 *
76 * \note Both operands and result are bit strings interpreted as
77 * elements of GF(2^128) as per the GCM spec.
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +010078 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079void mbedtls_aesni_gcm_mult( unsigned char c[16],
Manuel Pégourié-Gonnardd4588cf2013-12-30 13:54:23 +010080 const unsigned char a[16],
81 const unsigned char b[16] );
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +010082
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +010083/**
84 * \brief Compute decryption round keys from encryption round keys
85 *
86 * \param invkey Round keys for the equivalent inverse cipher
87 * \param fwdkey Original round keys (for encryption)
88 * \param nr Number of rounds (that is, number of round keys minus one)
89 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090void mbedtls_aesni_inverse_key( unsigned char *invkey,
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +010091 const unsigned char *fwdkey, int nr );
92
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +010093/**
94 * \brief Perform key expansion (for encryption)
95 *
96 * \param rk Destination buffer where the round keys are written
97 * \param key Encryption key
98 * \param bits Key size in bits (must be 128, 192 or 256)
99 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100101 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102int mbedtls_aesni_setkey_enc( unsigned char *rk,
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100103 const unsigned char *key,
104 size_t bits );
105
Manuel Pégourié-Gonnard1a901472015-03-10 16:12:29 +0000106#ifdef __cplusplus
107}
108#endif
109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110#endif /* MBEDTLS_HAVE_X86_64 */
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112#endif /* MBEDTLS_AESNI_H */