blob: 92b23cd6b9b414f5e808076eb0990e56a62dabee [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 *
6 * Copyright (C) 2013, Brainspark B.V.
7 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
9 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
10 *
11 * All rights reserved.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 */
27#ifndef POLARSSL_AESNI_H
28#define POLARSSL_AESNI_H
29
30#include "aes.h"
31
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010032#define POLARSSL_AESNI_AES 0x02000000u
33#define POLARSSL_AESNI_CLMUL 0x00000002u
34
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010035#if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && \
36 ( defined(__amd64__) || defined(__x86_64__) ) && \
37 ! defined(POLARSSL_HAVE_X86_64)
38#define POLARSSL_HAVE_X86_64
39#endif
40
41#if defined(POLARSSL_HAVE_X86_64)
42
43/**
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010044 * \brief AES-NI features detection routine
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010045 *
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010046 * \param what The feature to detect
47 * (POLARSSL_AESNI_AES or POLARSSL_AESNI_CLMUL)
48 *
49 * \return 1 if CPU has support for the feature, 0 otherwise
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010050 */
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010051int aesni_supports( unsigned int what );
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010052
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010053/**
54 * \brief AES-NI AES-ECB block en(de)cryption
55 *
56 * \param ctx AES context
57 * \param mode AES_ENCRYPT or AES_DECRYPT
58 * \param input 16-byte input block
59 * \param output 16-byte output block
60 *
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +010061 * \return 0 on success (cannot fail)
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010062 */
63int aesni_crypt_ecb( aes_context *ctx,
64 int mode,
65 const unsigned char input[16],
66 unsigned char output[16] );
67
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +010068/**
69 * \brief GCM multiplication: c = a * b in GF(2^128)
70 *
71 * \param c Result
72 * \param a First operand
73 * \param b Second operand
74 *
75 * \note Both operands and result are bit strings interpreted as
76 * elements of GF(2^128) as per the GCM spec.
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +010077 */
Manuel Pégourié-Gonnardd4588cf2013-12-30 13:54:23 +010078void aesni_gcm_mult( unsigned char c[16],
79 const unsigned char a[16],
80 const unsigned char b[16] );
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +010081
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +010082/**
83 * \brief Compute decryption round keys from encryption round keys
84 *
85 * \param invkey Round keys for the equivalent inverse cipher
86 * \param fwdkey Original round keys (for encryption)
87 * \param nr Number of rounds (that is, number of round keys minus one)
88 */
89void aesni_inverse_key( unsigned char *invkey,
90 const unsigned char *fwdkey, int nr );
91
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +010092/**
93 * \brief Perform key expansion (for encryption)
94 *
95 * \param rk Destination buffer where the round keys are written
96 * \param key Encryption key
97 * \param bits Key size in bits (must be 128, 192 or 256)
98 *
99 * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
100 */
101int aesni_setkey_enc( unsigned char *rk,
102 const unsigned char *key,
103 size_t bits );
104
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100105#endif /* POLARSSL_HAVE_X86_64 */
106
107#endif /* POLARSSL_AESNI_H */