1 /**
2  * \file aesni.h
3  *
4  * \brief AES-NI for hardware AES acceleration on some Intel processors
5  */
6 /*
7  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
8  *  SPDX-License-Identifier: GPL-2.0
9  *
10  *  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  *  This file is part of mbed TLS (https://tls.mbed.org)
25  */
26 #ifndef MBEDTLS_AESNI_H
27 #define MBEDTLS_AESNI_H
28 
29 #include "aes.h"
30 
31 #define MBEDTLS_AESNI_AES      0x02000000u
32 #define MBEDTLS_AESNI_CLMUL    0x00000002u
33 
34 #if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) &&  \
35     ( defined(__amd64__) || defined(__x86_64__) )   &&  \
36     ! defined(MBEDTLS_HAVE_X86_64)
37 #define MBEDTLS_HAVE_X86_64
38 #endif
39 
40 #if defined(MBEDTLS_HAVE_X86_64)
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 /**
47  * \brief          AES-NI features detection routine
48  *
49  * \param what     The feature to detect
50  *                 (MBEDTLS_AESNI_AES or MBEDTLS_AESNI_CLMUL)
51  *
52  * \return         1 if CPU has support for the feature, 0 otherwise
53  */
54 int mbedtls_aesni_has_support( unsigned int what );
55 
56 /**
57  * \brief          AES-NI AES-ECB block en(de)cryption
58  *
59  * \param ctx      AES context
60  * \param mode     MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
61  * \param input    16-byte input block
62  * \param output   16-byte output block
63  *
64  * \return         0 on success (cannot fail)
65  */
66 int mbedtls_aesni_crypt_ecb( mbedtls_aes_context *ctx,
67                      int mode,
68                      const unsigned char input[16],
69                      unsigned char output[16] );
70 
71 /**
72  * \brief          GCM multiplication: c = a * b in GF(2^128)
73  *
74  * \param c        Result
75  * \param a        First operand
76  * \param b        Second operand
77  *
78  * \note           Both operands and result are bit strings interpreted as
79  *                 elements of GF(2^128) as per the GCM spec.
80  */
81 void mbedtls_aesni_gcm_mult( unsigned char c[16],
82                      const unsigned char a[16],
83                      const unsigned char b[16] );
84 
85 /**
86  * \brief           Compute decryption round keys from encryption round keys
87  *
88  * \param invkey    Round keys for the equivalent inverse cipher
89  * \param fwdkey    Original round keys (for encryption)
90  * \param nr        Number of rounds (that is, number of round keys minus one)
91  */
92 void mbedtls_aesni_inverse_key( unsigned char *invkey,
93                         const unsigned char *fwdkey, int nr );
94 
95 /**
96  * \brief           Perform key expansion (for encryption)
97  *
98  * \param rk        Destination buffer where the round keys are written
99  * \param key       Encryption key
100  * \param bits      Key size in bits (must be 128, 192 or 256)
101  *
102  * \return          0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
103  */
104 int mbedtls_aesni_setkey_enc( unsigned char *rk,
105                       const unsigned char *key,
106                       size_t bits );
107 
108 #ifdef __cplusplus
109 }
110 #endif
111 
112 #endif /* MBEDTLS_HAVE_X86_64 */
113 
114 #endif /* MBEDTLS_AESNI_H */
115