blob: bb514ca6d43c91bfa90230dd0189c76f2f35abb2 [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 */
24#ifndef POLARSSL_AESNI_H
25#define POLARSSL_AESNI_H
26
27#include "aes.h"
28
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010029#define POLARSSL_AESNI_AES 0x02000000u
30#define POLARSSL_AESNI_CLMUL 0x00000002u
31
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010032#if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && \
33 ( defined(__amd64__) || defined(__x86_64__) ) && \
34 ! defined(POLARSSL_HAVE_X86_64)
35#define POLARSSL_HAVE_X86_64
36#endif
37
38#if defined(POLARSSL_HAVE_X86_64)
39
40/**
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010041 * \brief AES-NI features detection routine
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010042 *
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010043 * \param what The feature to detect
44 * (POLARSSL_AESNI_AES or POLARSSL_AESNI_CLMUL)
45 *
46 * \return 1 if CPU has support for the feature, 0 otherwise
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010047 */
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010048int aesni_supports( unsigned int what );
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010049
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010050/**
51 * \brief AES-NI AES-ECB block en(de)cryption
52 *
53 * \param ctx AES context
54 * \param mode AES_ENCRYPT or AES_DECRYPT
55 * \param input 16-byte input block
56 * \param output 16-byte output block
57 *
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +010058 * \return 0 on success (cannot fail)
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010059 */
60int aesni_crypt_ecb( aes_context *ctx,
61 int mode,
62 const unsigned char input[16],
63 unsigned char output[16] );
64
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +010065/**
66 * \brief GCM multiplication: c = a * b in GF(2^128)
67 *
68 * \param c Result
69 * \param a First operand
70 * \param b Second operand
71 *
72 * \note Both operands and result are bit strings interpreted as
73 * elements of GF(2^128) as per the GCM spec.
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +010074 */
Manuel Pégourié-Gonnardd4588cf2013-12-30 13:54:23 +010075void aesni_gcm_mult( unsigned char c[16],
76 const unsigned char a[16],
77 const unsigned char b[16] );
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +010078
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +010079/**
80 * \brief Compute decryption round keys from encryption round keys
81 *
82 * \param invkey Round keys for the equivalent inverse cipher
83 * \param fwdkey Original round keys (for encryption)
84 * \param nr Number of rounds (that is, number of round keys minus one)
85 */
86void aesni_inverse_key( unsigned char *invkey,
87 const unsigned char *fwdkey, int nr );
88
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +010089/**
90 * \brief Perform key expansion (for encryption)
91 *
92 * \param rk Destination buffer where the round keys are written
93 * \param key Encryption key
94 * \param bits Key size in bits (must be 128, 192 or 256)
95 *
96 * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
97 */
98int aesni_setkey_enc( unsigned char *rk,
99 const unsigned char *key,
100 size_t bits );
101
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100102#endif /* POLARSSL_HAVE_X86_64 */
103
104#endif /* POLARSSL_AESNI_H */