emp-toolkit
aes.h
Go to the documentation of this file.
1 /* crypto/aes/aes.h -*- mode:C; c-file-style: "eay" -*- */
2 /* ====================================================================
3  * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. All advertising materials mentioning features or use of this
18  * software must display the following acknowledgment:
19  * "This product includes software developed by the OpenSSL Project
20  * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21  *
22  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23  * endorse or promote products derived from this software without
24  * prior written permission. For written permission, please contact
25  * openssl-core@openssl.org.
26  *
27  * 5. Products derived from this software may not be called "OpenSSL"
28  * nor may "OpenSSL" appear in their names without prior written
29  * permission of the OpenSSL Project.
30  *
31  * 6. Redistributions of any form whatsoever must retain the following
32  * acknowledgment:
33  * "This product includes software developed by the OpenSSL Project
34  * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35  *
36  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47  * OF THE POSSIBILITY OF SUCH DAMAGE.
48  * ====================================================================
49  *
50  */
51 
52 #ifndef LIBGARBLE_AES_H
53 #define LIBGARBLE_AES_H
54 
55 #include "block.h"
56 
57 typedef struct { block rd_key[11]; unsigned int rounds; } AES_KEY;
58 
59 #define EXPAND_ASSIST(v1,v2,v3,v4,shuff_const,aes_const) \
60  v2 = _mm_aeskeygenassist_si128(v4,aes_const); \
61  v3 = _mm_castps_si128(_mm_shuffle_ps(_mm_castsi128_ps(v3), \
62  _mm_castsi128_ps(v1), 16)); \
63  v1 = _mm_xor_si128(v1,v3); \
64  v3 = _mm_castps_si128(_mm_shuffle_ps(_mm_castsi128_ps(v3), \
65  _mm_castsi128_ps(v1), 140)); \
66  v1 = _mm_xor_si128(v1,v3); \
67  v2 = _mm_shuffle_epi32(v2,shuff_const); \
68  v1 = _mm_xor_si128(v1,v2)
69 
70 static inline void
71 AES_set_encrypt_key(const block userkey, AES_KEY *key)
72 {
73  block x0, x1, x2;
74  block *kp = key->rd_key;
75  kp[0] = x0 = userkey;
76  x2 = _mm_setzero_si128();
77  EXPAND_ASSIST(x0, x1, x2, x0, 255, 1);
78  kp[1] = x0;
79  EXPAND_ASSIST(x0, x1, x2, x0, 255, 2);
80  kp[2] = x0;
81  EXPAND_ASSIST(x0, x1, x2, x0, 255, 4);
82  kp[3] = x0;
83  EXPAND_ASSIST(x0, x1, x2, x0, 255, 8);
84  kp[4] = x0;
85  EXPAND_ASSIST(x0, x1, x2, x0, 255, 16);
86  kp[5] = x0;
87  EXPAND_ASSIST(x0, x1, x2, x0, 255, 32);
88  kp[6] = x0;
89  EXPAND_ASSIST(x0, x1, x2, x0, 255, 64);
90  kp[7] = x0;
91  EXPAND_ASSIST(x0, x1, x2, x0, 255, 128);
92  kp[8] = x0;
93  EXPAND_ASSIST(x0, x1, x2, x0, 255, 27);
94  kp[9] = x0;
95  EXPAND_ASSIST(x0, x1, x2, x0, 255, 54);
96  kp[10] = x0;
97  key->rounds = 10;
98 }
99 
100 static inline void
101 AES_ecb_encrypt_blks(block *blks, unsigned int nblks, const AES_KEY *key)
102 {
103  for (unsigned int i = 0; i < nblks; ++i)
104  blks[i] = _mm_xor_si128(blks[i], key->rd_key[0]);
105  for (unsigned int j = 1; j < key->rounds; ++j)
106  for (unsigned int i = 0; i < nblks; ++i)
107  blks[i] = _mm_aesenc_si128(blks[i], key->rd_key[j]);
108  for (unsigned int i = 0; i < nblks; ++i)
109  blks[i] = _mm_aesenclast_si128(blks[i], key->rd_key[key->rounds]);
110 }
111 
112 static inline void
113 AES_set_decrypt_key_fast(AES_KEY *dkey, const AES_KEY *ekey)
114 {
115  int j = 0;
116  int i = ekey->rounds;
117 #if (OCB_KEY_LEN == 0)
118  dkey->rounds = i;
119 #endif
120  dkey->rd_key[i--] = ekey->rd_key[j++];
121  while (i)
122  dkey->rd_key[i--] = _mm_aesimc_si128(ekey->rd_key[j++]);
123  dkey->rd_key[i] = ekey->rd_key[j];
124 }
125 
126 static inline void
127 AES_set_decrypt_key(block userkey, AES_KEY *key)
128 {
129  AES_KEY temp_key;
130  AES_set_encrypt_key(userkey, &temp_key);
131  AES_set_decrypt_key_fast(key, &temp_key);
132 }
133 
134 static inline void
135 AES_ecb_decrypt_blks(block *blks, unsigned nblks, const AES_KEY *key)
136 {
137  unsigned i, j, rnds = key->rounds;
138  for (i = 0; i < nblks; ++i)
139  blks[i] = _mm_xor_si128(blks[i], key->rd_key[0]);
140  for (j = 1; j < rnds; ++j)
141  for (i = 0; i < nblks; ++i)
142  blks[i] = _mm_aesdec_si128(blks[i], key->rd_key[j]);
143  for (i = 0; i < nblks; ++i)
144  blks[i] = _mm_aesdeclast_si128(blks[i], key->rd_key[j]);
145 }
146 
147 #endif
__m128i block
Definition: block.h:8
unsigned int rounds
Definition: aes.h:57
Definition: aes.h:57
#define EXPAND_ASSIST(v1, v2, v3, v4, shuff_const, aes_const)
Definition: aes.h:59
block rd_key[11]
Definition: aes.h:57