1 /*******************************************************************************
2   Copyright (c) 2018-2020, Intel Corporation
3 
4   Redistribution and use in source and binary forms, with or without
5   modification, are permitted provided that the following conditions are met:
6 
7       * Redistributions of source code must retain the above copyright notice,
8         this list of conditions and the following disclaimer.
9       * Redistributions in binary form must reproduce the above copyright
10         notice, this list of conditions and the following disclaimer in the
11         documentation and/or other materials provided with the distribution.
12       * Neither the name of Intel Corporation nor the names of its contributors
13         may be used to endorse or promote products derived from this software
14         without specific prior written permission.
15 
16   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20   FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23   CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *******************************************************************************/
27 
28 #ifndef _AESNI_EMU_H_
29 #define _AESNI_EMU_H_
30 #include <stdint.h>
31 
32 /* Interface to AESNI emulation routines */
33 
34 /* XMM type definitions and constants */
35 
36 #define MAX_BYTES_PER_XMM   16
37 #define MAX_WORDS_PER_XMM   8
38 #define MAX_DWORDS_PER_XMM  4
39 #define MAX_QWORDS_PER_XMM  2
40 
41 union xmm_reg {
42         uint8_t  byte[MAX_BYTES_PER_XMM];
43         uint16_t word[MAX_WORDS_PER_XMM];
44         uint32_t dword[MAX_DWORDS_PER_XMM];
45         uint64_t qword[MAX_QWORDS_PER_XMM];
46 };
47 
48 /* AESNI emulation API */
49 
50 /**
51  * @brief AESKEYGENASIST instruction emulation function
52  *
53  * Assist in AES round key generation using an 8 bits Round Constant
54  * (RCON) specified in \a imm8, operating on 128 bits of data
55  *
56  * @param dst pointer to 128 bit buffer to store generated key
57  * @param src pointer to 128 bit src key
58  * @param imm8 round constant used to generate key
59  */
60 IMB_DLL_LOCAL void emulate_AESKEYGENASSIST(union xmm_reg *dst,
61                                            const union xmm_reg *src,
62                                            const uint32_t imm8);
63 
64 /**
65  * @brief AESENC instruction emulation function
66  *
67  * Perform one round of an AES encryption flow
68  *
69  * @param dst pointer to 128 bit data (state) to operate on
70  * @param src pointer to 128 bit round key
71  */
72 IMB_DLL_LOCAL void emulate_AESENC(union xmm_reg *dst,
73                                   const union xmm_reg *src);
74 
75 /**
76  * @brief AESENCLAST instruction emulation function
77  *
78  * Perform last round of an AES encryption flow
79  *
80  * @param dst pointer to 128 bit data (state) to operate on
81  * @param src pointer to 128 bit round key
82  */
83 IMB_DLL_LOCAL void emulate_AESENCLAST(union xmm_reg *dst,
84                                       const union xmm_reg *src);
85 
86 /**
87  * @brief AESDEC instruction emulation function
88  *
89  * Perform one round of an AES decryption flow
90  *
91  * @param dst pointer to 128 bit data (state) to operate on
92  * @param src pointer to 128 bit round key
93  */
94 IMB_DLL_LOCAL void emulate_AESDEC(union xmm_reg *dst,
95                                   const union xmm_reg *src);
96 
97 /**
98  * @brief AESDECLAST instruction emulation function
99  *
100  * Perform last round of an AES decryption flow
101  *
102  * @param dst pointer to 128 bit data (state) to operate on
103  * @param src pointer to 128 bit round key
104  */
105 IMB_DLL_LOCAL void emulate_AESDECLAST(union xmm_reg *dst,
106                                       const union xmm_reg *src);
107 
108 /**
109  * @brief AESIMC instruction emulation function
110  *
111  * Perform the InvMixColumn transformation on
112  * a 128 bit round key
113  *
114  * @param dst pointer to 128 bit buffer to store result
115  * @param src pointer to 128 bit round key
116  */
117 IMB_DLL_LOCAL void emulate_AESIMC(union xmm_reg *dst,
118                                   const union xmm_reg *src);
119 
120 /**
121  * @brief PCLMULQDQ instruction emulation function
122  *
123  * Performs carry-less multiplication of two 64-bit numbers and
124  * returns 128-bit product.
125  *
126  * @param src1_dst pointer to 128 bit input/output buffer
127  * @param src2     pointer to 128 bit input number
128  * @param imm8     constant for selecting quadword
129  */
130 IMB_DLL_LOCAL void emulate_PCLMULQDQ(union xmm_reg *src1_dst,
131                                      const union xmm_reg *src2,
132                                      const uint32_t imm8);
133 
134 #endif /* _AESNI_EMU_H_ */
135