VERSION: Bump version up to 4.10.8...
[Samba.git] / lib / crypto / aes.h
blob00bfa3e26cea8b6011458e32c00b4cd5142e543d
1 /*
2 * Copyright (c) 2003-2004 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 /* $Id$ */
36 #ifndef LIB_CRYPTO_AES_H
37 #define LIB_CRYPTO_AES_H 1
39 #include "aesni.h"
41 #define SAMBA_RIJNDAEL 1
42 #define SAMBA_AES_CBC_ENCRYPT 1
43 #define SAMBA_AES_CFB8_ENCRYPT 1
44 #define SAMBA_AES_BLOCK_XOR 1
46 /* symbol renaming */
47 #define AES_set_encrypt_key samba_AES_set_encrypt_key
48 #define AES_set_decrypt_key samba_AES_decrypt_key
49 #define AES_encrypt samba_AES_encrypt
50 #define AES_decrypt samba_AES_decrypt
51 #define AES_cbc_encrypt samba_AES_cbc_encrypt
52 #define AES_cfb8_encrypt samba_AES_cfb8_encrypt
58 #define AES_BLOCK_SIZE 16
59 #define AES_MAXNR 14
61 #define AES_ENCRYPT 1
62 #define AES_DECRYPT 0
64 struct aes_key_rj {
65 uint32_t key[(AES_MAXNR+1)*4];
66 int rounds;
69 typedef struct aes_key {
70 union {
71 struct aes_key_rj aes_rj;
72 struct crypto_aesni_ctx aes_ni;
73 } u;
74 } AES_KEY;
76 #ifdef __cplusplus
77 extern "C" {
78 #endif
80 int AES_set_encrypt_key(const unsigned char *, const int, AES_KEY *);
81 int AES_set_decrypt_key(const unsigned char *, const int, AES_KEY *);
83 void AES_encrypt(const unsigned char *, unsigned char *, const AES_KEY *);
84 void AES_decrypt(const unsigned char *, unsigned char *, const AES_KEY *);
86 void AES_cbc_encrypt(const unsigned char *, unsigned char *,
87 const unsigned long, const AES_KEY *,
88 unsigned char *, int);
90 void AES_cfb8_encrypt(const unsigned char *in, unsigned char *out,
91 unsigned long size, const AES_KEY *key,
92 unsigned char *iv, int forward_encrypt);
94 #define aes_cfb8_encrypt(in, out, size, key, iv, forward_encrypt) \
95 AES_cfb8_encrypt(in, out, size, key, iv, forward_encrypt)
97 #ifdef __cplusplus
99 #endif
101 #ifdef SAMBA_AES_BLOCK_XOR
102 static inline void aes_block_xor(const uint8_t in1[AES_BLOCK_SIZE],
103 const uint8_t in2[AES_BLOCK_SIZE],
104 uint8_t out[AES_BLOCK_SIZE])
106 #define __IS_ALIGN8(p) ((((uintptr_t)(p)) & 0x7) == 0)
107 #define __IS_ALIGNED(a,b,c) __IS_ALIGN8(\
108 ((uintptr_t)(a)) | \
109 ((uintptr_t)(b)) | \
110 ((uintptr_t)(c)))
111 /* If everything is aligned we can optimize */
112 if (likely(__IS_ALIGNED(in1, in2, out))) {
113 #define __RO64(p) ((const uint64_t *)(p))
114 #define __RW64(p) ((uint64_t *)(p))
115 __RW64(out)[0] = __RO64(in1)[0] ^ __RO64(in2)[0];
116 __RW64(out)[1] = __RO64(in1)[1] ^ __RO64(in2)[1];
117 } else {
118 uint64_t i1[2];
119 uint64_t i2[2];
120 uint64_t o[2];
122 memcpy(i1, in1, AES_BLOCK_SIZE);
123 memcpy(i2, in2, AES_BLOCK_SIZE);
124 o[0] = i1[0] ^ i2[0];
125 o[1] = i1[1] ^ i2[1];
126 memcpy(out, o, AES_BLOCK_SIZE);
129 #endif /* SAMBA_AES_BLOCK_XOR */
131 static inline void aes_block_lshift(const uint8_t in[AES_BLOCK_SIZE],
132 uint8_t out[AES_BLOCK_SIZE])
134 static const struct aes_block_lshift_entry {
135 uint8_t lshift;
136 uint8_t overflow;
137 } aes_block_lshift_table[UINT8_MAX+1] = {
138 [0x00] = { .lshift = 0x00, .overflow = 0x00 },
139 [0x01] = { .lshift = 0x02, .overflow = 0x00 },
140 [0x02] = { .lshift = 0x04, .overflow = 0x00 },
141 [0x03] = { .lshift = 0x06, .overflow = 0x00 },
142 [0x04] = { .lshift = 0x08, .overflow = 0x00 },
143 [0x05] = { .lshift = 0x0a, .overflow = 0x00 },
144 [0x06] = { .lshift = 0x0c, .overflow = 0x00 },
145 [0x07] = { .lshift = 0x0e, .overflow = 0x00 },
146 [0x08] = { .lshift = 0x10, .overflow = 0x00 },
147 [0x09] = { .lshift = 0x12, .overflow = 0x00 },
148 [0x0a] = { .lshift = 0x14, .overflow = 0x00 },
149 [0x0b] = { .lshift = 0x16, .overflow = 0x00 },
150 [0x0c] = { .lshift = 0x18, .overflow = 0x00 },
151 [0x0d] = { .lshift = 0x1a, .overflow = 0x00 },
152 [0x0e] = { .lshift = 0x1c, .overflow = 0x00 },
153 [0x0f] = { .lshift = 0x1e, .overflow = 0x00 },
154 [0x10] = { .lshift = 0x20, .overflow = 0x00 },
155 [0x11] = { .lshift = 0x22, .overflow = 0x00 },
156 [0x12] = { .lshift = 0x24, .overflow = 0x00 },
157 [0x13] = { .lshift = 0x26, .overflow = 0x00 },
158 [0x14] = { .lshift = 0x28, .overflow = 0x00 },
159 [0x15] = { .lshift = 0x2a, .overflow = 0x00 },
160 [0x16] = { .lshift = 0x2c, .overflow = 0x00 },
161 [0x17] = { .lshift = 0x2e, .overflow = 0x00 },
162 [0x18] = { .lshift = 0x30, .overflow = 0x00 },
163 [0x19] = { .lshift = 0x32, .overflow = 0x00 },
164 [0x1a] = { .lshift = 0x34, .overflow = 0x00 },
165 [0x1b] = { .lshift = 0x36, .overflow = 0x00 },
166 [0x1c] = { .lshift = 0x38, .overflow = 0x00 },
167 [0x1d] = { .lshift = 0x3a, .overflow = 0x00 },
168 [0x1e] = { .lshift = 0x3c, .overflow = 0x00 },
169 [0x1f] = { .lshift = 0x3e, .overflow = 0x00 },
170 [0x20] = { .lshift = 0x40, .overflow = 0x00 },
171 [0x21] = { .lshift = 0x42, .overflow = 0x00 },
172 [0x22] = { .lshift = 0x44, .overflow = 0x00 },
173 [0x23] = { .lshift = 0x46, .overflow = 0x00 },
174 [0x24] = { .lshift = 0x48, .overflow = 0x00 },
175 [0x25] = { .lshift = 0x4a, .overflow = 0x00 },
176 [0x26] = { .lshift = 0x4c, .overflow = 0x00 },
177 [0x27] = { .lshift = 0x4e, .overflow = 0x00 },
178 [0x28] = { .lshift = 0x50, .overflow = 0x00 },
179 [0x29] = { .lshift = 0x52, .overflow = 0x00 },
180 [0x2a] = { .lshift = 0x54, .overflow = 0x00 },
181 [0x2b] = { .lshift = 0x56, .overflow = 0x00 },
182 [0x2c] = { .lshift = 0x58, .overflow = 0x00 },
183 [0x2d] = { .lshift = 0x5a, .overflow = 0x00 },
184 [0x2e] = { .lshift = 0x5c, .overflow = 0x00 },
185 [0x2f] = { .lshift = 0x5e, .overflow = 0x00 },
186 [0x30] = { .lshift = 0x60, .overflow = 0x00 },
187 [0x31] = { .lshift = 0x62, .overflow = 0x00 },
188 [0x32] = { .lshift = 0x64, .overflow = 0x00 },
189 [0x33] = { .lshift = 0x66, .overflow = 0x00 },
190 [0x34] = { .lshift = 0x68, .overflow = 0x00 },
191 [0x35] = { .lshift = 0x6a, .overflow = 0x00 },
192 [0x36] = { .lshift = 0x6c, .overflow = 0x00 },
193 [0x37] = { .lshift = 0x6e, .overflow = 0x00 },
194 [0x38] = { .lshift = 0x70, .overflow = 0x00 },
195 [0x39] = { .lshift = 0x72, .overflow = 0x00 },
196 [0x3a] = { .lshift = 0x74, .overflow = 0x00 },
197 [0x3b] = { .lshift = 0x76, .overflow = 0x00 },
198 [0x3c] = { .lshift = 0x78, .overflow = 0x00 },
199 [0x3d] = { .lshift = 0x7a, .overflow = 0x00 },
200 [0x3e] = { .lshift = 0x7c, .overflow = 0x00 },
201 [0x3f] = { .lshift = 0x7e, .overflow = 0x00 },
202 [0x40] = { .lshift = 0x80, .overflow = 0x00 },
203 [0x41] = { .lshift = 0x82, .overflow = 0x00 },
204 [0x42] = { .lshift = 0x84, .overflow = 0x00 },
205 [0x43] = { .lshift = 0x86, .overflow = 0x00 },
206 [0x44] = { .lshift = 0x88, .overflow = 0x00 },
207 [0x45] = { .lshift = 0x8a, .overflow = 0x00 },
208 [0x46] = { .lshift = 0x8c, .overflow = 0x00 },
209 [0x47] = { .lshift = 0x8e, .overflow = 0x00 },
210 [0x48] = { .lshift = 0x90, .overflow = 0x00 },
211 [0x49] = { .lshift = 0x92, .overflow = 0x00 },
212 [0x4a] = { .lshift = 0x94, .overflow = 0x00 },
213 [0x4b] = { .lshift = 0x96, .overflow = 0x00 },
214 [0x4c] = { .lshift = 0x98, .overflow = 0x00 },
215 [0x4d] = { .lshift = 0x9a, .overflow = 0x00 },
216 [0x4e] = { .lshift = 0x9c, .overflow = 0x00 },
217 [0x4f] = { .lshift = 0x9e, .overflow = 0x00 },
218 [0x50] = { .lshift = 0xa0, .overflow = 0x00 },
219 [0x51] = { .lshift = 0xa2, .overflow = 0x00 },
220 [0x52] = { .lshift = 0xa4, .overflow = 0x00 },
221 [0x53] = { .lshift = 0xa6, .overflow = 0x00 },
222 [0x54] = { .lshift = 0xa8, .overflow = 0x00 },
223 [0x55] = { .lshift = 0xaa, .overflow = 0x00 },
224 [0x56] = { .lshift = 0xac, .overflow = 0x00 },
225 [0x57] = { .lshift = 0xae, .overflow = 0x00 },
226 [0x58] = { .lshift = 0xb0, .overflow = 0x00 },
227 [0x59] = { .lshift = 0xb2, .overflow = 0x00 },
228 [0x5a] = { .lshift = 0xb4, .overflow = 0x00 },
229 [0x5b] = { .lshift = 0xb6, .overflow = 0x00 },
230 [0x5c] = { .lshift = 0xb8, .overflow = 0x00 },
231 [0x5d] = { .lshift = 0xba, .overflow = 0x00 },
232 [0x5e] = { .lshift = 0xbc, .overflow = 0x00 },
233 [0x5f] = { .lshift = 0xbe, .overflow = 0x00 },
234 [0x60] = { .lshift = 0xc0, .overflow = 0x00 },
235 [0x61] = { .lshift = 0xc2, .overflow = 0x00 },
236 [0x62] = { .lshift = 0xc4, .overflow = 0x00 },
237 [0x63] = { .lshift = 0xc6, .overflow = 0x00 },
238 [0x64] = { .lshift = 0xc8, .overflow = 0x00 },
239 [0x65] = { .lshift = 0xca, .overflow = 0x00 },
240 [0x66] = { .lshift = 0xcc, .overflow = 0x00 },
241 [0x67] = { .lshift = 0xce, .overflow = 0x00 },
242 [0x68] = { .lshift = 0xd0, .overflow = 0x00 },
243 [0x69] = { .lshift = 0xd2, .overflow = 0x00 },
244 [0x6a] = { .lshift = 0xd4, .overflow = 0x00 },
245 [0x6b] = { .lshift = 0xd6, .overflow = 0x00 },
246 [0x6c] = { .lshift = 0xd8, .overflow = 0x00 },
247 [0x6d] = { .lshift = 0xda, .overflow = 0x00 },
248 [0x6e] = { .lshift = 0xdc, .overflow = 0x00 },
249 [0x6f] = { .lshift = 0xde, .overflow = 0x00 },
250 [0x70] = { .lshift = 0xe0, .overflow = 0x00 },
251 [0x71] = { .lshift = 0xe2, .overflow = 0x00 },
252 [0x72] = { .lshift = 0xe4, .overflow = 0x00 },
253 [0x73] = { .lshift = 0xe6, .overflow = 0x00 },
254 [0x74] = { .lshift = 0xe8, .overflow = 0x00 },
255 [0x75] = { .lshift = 0xea, .overflow = 0x00 },
256 [0x76] = { .lshift = 0xec, .overflow = 0x00 },
257 [0x77] = { .lshift = 0xee, .overflow = 0x00 },
258 [0x78] = { .lshift = 0xf0, .overflow = 0x00 },
259 [0x79] = { .lshift = 0xf2, .overflow = 0x00 },
260 [0x7a] = { .lshift = 0xf4, .overflow = 0x00 },
261 [0x7b] = { .lshift = 0xf6, .overflow = 0x00 },
262 [0x7c] = { .lshift = 0xf8, .overflow = 0x00 },
263 [0x7d] = { .lshift = 0xfa, .overflow = 0x00 },
264 [0x7e] = { .lshift = 0xfc, .overflow = 0x00 },
265 [0x7f] = { .lshift = 0xfe, .overflow = 0x00 },
266 [0x80] = { .lshift = 0x00, .overflow = 0x01 },
267 [0x81] = { .lshift = 0x02, .overflow = 0x01 },
268 [0x82] = { .lshift = 0x04, .overflow = 0x01 },
269 [0x83] = { .lshift = 0x06, .overflow = 0x01 },
270 [0x84] = { .lshift = 0x08, .overflow = 0x01 },
271 [0x85] = { .lshift = 0x0a, .overflow = 0x01 },
272 [0x86] = { .lshift = 0x0c, .overflow = 0x01 },
273 [0x87] = { .lshift = 0x0e, .overflow = 0x01 },
274 [0x88] = { .lshift = 0x10, .overflow = 0x01 },
275 [0x89] = { .lshift = 0x12, .overflow = 0x01 },
276 [0x8a] = { .lshift = 0x14, .overflow = 0x01 },
277 [0x8b] = { .lshift = 0x16, .overflow = 0x01 },
278 [0x8c] = { .lshift = 0x18, .overflow = 0x01 },
279 [0x8d] = { .lshift = 0x1a, .overflow = 0x01 },
280 [0x8e] = { .lshift = 0x1c, .overflow = 0x01 },
281 [0x8f] = { .lshift = 0x1e, .overflow = 0x01 },
282 [0x90] = { .lshift = 0x20, .overflow = 0x01 },
283 [0x91] = { .lshift = 0x22, .overflow = 0x01 },
284 [0x92] = { .lshift = 0x24, .overflow = 0x01 },
285 [0x93] = { .lshift = 0x26, .overflow = 0x01 },
286 [0x94] = { .lshift = 0x28, .overflow = 0x01 },
287 [0x95] = { .lshift = 0x2a, .overflow = 0x01 },
288 [0x96] = { .lshift = 0x2c, .overflow = 0x01 },
289 [0x97] = { .lshift = 0x2e, .overflow = 0x01 },
290 [0x98] = { .lshift = 0x30, .overflow = 0x01 },
291 [0x99] = { .lshift = 0x32, .overflow = 0x01 },
292 [0x9a] = { .lshift = 0x34, .overflow = 0x01 },
293 [0x9b] = { .lshift = 0x36, .overflow = 0x01 },
294 [0x9c] = { .lshift = 0x38, .overflow = 0x01 },
295 [0x9d] = { .lshift = 0x3a, .overflow = 0x01 },
296 [0x9e] = { .lshift = 0x3c, .overflow = 0x01 },
297 [0x9f] = { .lshift = 0x3e, .overflow = 0x01 },
298 [0xa0] = { .lshift = 0x40, .overflow = 0x01 },
299 [0xa1] = { .lshift = 0x42, .overflow = 0x01 },
300 [0xa2] = { .lshift = 0x44, .overflow = 0x01 },
301 [0xa3] = { .lshift = 0x46, .overflow = 0x01 },
302 [0xa4] = { .lshift = 0x48, .overflow = 0x01 },
303 [0xa5] = { .lshift = 0x4a, .overflow = 0x01 },
304 [0xa6] = { .lshift = 0x4c, .overflow = 0x01 },
305 [0xa7] = { .lshift = 0x4e, .overflow = 0x01 },
306 [0xa8] = { .lshift = 0x50, .overflow = 0x01 },
307 [0xa9] = { .lshift = 0x52, .overflow = 0x01 },
308 [0xaa] = { .lshift = 0x54, .overflow = 0x01 },
309 [0xab] = { .lshift = 0x56, .overflow = 0x01 },
310 [0xac] = { .lshift = 0x58, .overflow = 0x01 },
311 [0xad] = { .lshift = 0x5a, .overflow = 0x01 },
312 [0xae] = { .lshift = 0x5c, .overflow = 0x01 },
313 [0xaf] = { .lshift = 0x5e, .overflow = 0x01 },
314 [0xb0] = { .lshift = 0x60, .overflow = 0x01 },
315 [0xb1] = { .lshift = 0x62, .overflow = 0x01 },
316 [0xb2] = { .lshift = 0x64, .overflow = 0x01 },
317 [0xb3] = { .lshift = 0x66, .overflow = 0x01 },
318 [0xb4] = { .lshift = 0x68, .overflow = 0x01 },
319 [0xb5] = { .lshift = 0x6a, .overflow = 0x01 },
320 [0xb6] = { .lshift = 0x6c, .overflow = 0x01 },
321 [0xb7] = { .lshift = 0x6e, .overflow = 0x01 },
322 [0xb8] = { .lshift = 0x70, .overflow = 0x01 },
323 [0xb9] = { .lshift = 0x72, .overflow = 0x01 },
324 [0xba] = { .lshift = 0x74, .overflow = 0x01 },
325 [0xbb] = { .lshift = 0x76, .overflow = 0x01 },
326 [0xbc] = { .lshift = 0x78, .overflow = 0x01 },
327 [0xbd] = { .lshift = 0x7a, .overflow = 0x01 },
328 [0xbe] = { .lshift = 0x7c, .overflow = 0x01 },
329 [0xbf] = { .lshift = 0x7e, .overflow = 0x01 },
330 [0xc0] = { .lshift = 0x80, .overflow = 0x01 },
331 [0xc1] = { .lshift = 0x82, .overflow = 0x01 },
332 [0xc2] = { .lshift = 0x84, .overflow = 0x01 },
333 [0xc3] = { .lshift = 0x86, .overflow = 0x01 },
334 [0xc4] = { .lshift = 0x88, .overflow = 0x01 },
335 [0xc5] = { .lshift = 0x8a, .overflow = 0x01 },
336 [0xc6] = { .lshift = 0x8c, .overflow = 0x01 },
337 [0xc7] = { .lshift = 0x8e, .overflow = 0x01 },
338 [0xc8] = { .lshift = 0x90, .overflow = 0x01 },
339 [0xc9] = { .lshift = 0x92, .overflow = 0x01 },
340 [0xca] = { .lshift = 0x94, .overflow = 0x01 },
341 [0xcb] = { .lshift = 0x96, .overflow = 0x01 },
342 [0xcc] = { .lshift = 0x98, .overflow = 0x01 },
343 [0xcd] = { .lshift = 0x9a, .overflow = 0x01 },
344 [0xce] = { .lshift = 0x9c, .overflow = 0x01 },
345 [0xcf] = { .lshift = 0x9e, .overflow = 0x01 },
346 [0xd0] = { .lshift = 0xa0, .overflow = 0x01 },
347 [0xd1] = { .lshift = 0xa2, .overflow = 0x01 },
348 [0xd2] = { .lshift = 0xa4, .overflow = 0x01 },
349 [0xd3] = { .lshift = 0xa6, .overflow = 0x01 },
350 [0xd4] = { .lshift = 0xa8, .overflow = 0x01 },
351 [0xd5] = { .lshift = 0xaa, .overflow = 0x01 },
352 [0xd6] = { .lshift = 0xac, .overflow = 0x01 },
353 [0xd7] = { .lshift = 0xae, .overflow = 0x01 },
354 [0xd8] = { .lshift = 0xb0, .overflow = 0x01 },
355 [0xd9] = { .lshift = 0xb2, .overflow = 0x01 },
356 [0xda] = { .lshift = 0xb4, .overflow = 0x01 },
357 [0xdb] = { .lshift = 0xb6, .overflow = 0x01 },
358 [0xdc] = { .lshift = 0xb8, .overflow = 0x01 },
359 [0xdd] = { .lshift = 0xba, .overflow = 0x01 },
360 [0xde] = { .lshift = 0xbc, .overflow = 0x01 },
361 [0xdf] = { .lshift = 0xbe, .overflow = 0x01 },
362 [0xe0] = { .lshift = 0xc0, .overflow = 0x01 },
363 [0xe1] = { .lshift = 0xc2, .overflow = 0x01 },
364 [0xe2] = { .lshift = 0xc4, .overflow = 0x01 },
365 [0xe3] = { .lshift = 0xc6, .overflow = 0x01 },
366 [0xe4] = { .lshift = 0xc8, .overflow = 0x01 },
367 [0xe5] = { .lshift = 0xca, .overflow = 0x01 },
368 [0xe6] = { .lshift = 0xcc, .overflow = 0x01 },
369 [0xe7] = { .lshift = 0xce, .overflow = 0x01 },
370 [0xe8] = { .lshift = 0xd0, .overflow = 0x01 },
371 [0xe9] = { .lshift = 0xd2, .overflow = 0x01 },
372 [0xea] = { .lshift = 0xd4, .overflow = 0x01 },
373 [0xeb] = { .lshift = 0xd6, .overflow = 0x01 },
374 [0xec] = { .lshift = 0xd8, .overflow = 0x01 },
375 [0xed] = { .lshift = 0xda, .overflow = 0x01 },
376 [0xee] = { .lshift = 0xdc, .overflow = 0x01 },
377 [0xef] = { .lshift = 0xde, .overflow = 0x01 },
378 [0xf0] = { .lshift = 0xe0, .overflow = 0x01 },
379 [0xf1] = { .lshift = 0xe2, .overflow = 0x01 },
380 [0xf2] = { .lshift = 0xe4, .overflow = 0x01 },
381 [0xf3] = { .lshift = 0xe6, .overflow = 0x01 },
382 [0xf4] = { .lshift = 0xe8, .overflow = 0x01 },
383 [0xf5] = { .lshift = 0xea, .overflow = 0x01 },
384 [0xf6] = { .lshift = 0xec, .overflow = 0x01 },
385 [0xf7] = { .lshift = 0xee, .overflow = 0x01 },
386 [0xf8] = { .lshift = 0xf0, .overflow = 0x01 },
387 [0xf9] = { .lshift = 0xf2, .overflow = 0x01 },
388 [0xfa] = { .lshift = 0xf4, .overflow = 0x01 },
389 [0xfb] = { .lshift = 0xf6, .overflow = 0x01 },
390 [0xfc] = { .lshift = 0xf8, .overflow = 0x01 },
391 [0xfd] = { .lshift = 0xfa, .overflow = 0x01 },
392 [0xfe] = { .lshift = 0xfc, .overflow = 0x01 },
393 [0xff] = { .lshift = 0xfe, .overflow = 0x01 },
395 int8_t i;
396 uint8_t overflow = 0;
398 for (i = AES_BLOCK_SIZE - 1; i >= 0; i--) {
399 const struct aes_block_lshift_entry *e = &aes_block_lshift_table[in[i]];
400 out[i] = e->lshift | overflow;
401 overflow = e->overflow;
405 static inline void aes_block_rshift(const uint8_t in[AES_BLOCK_SIZE],
406 uint8_t out[AES_BLOCK_SIZE])
408 static const struct aes_block_rshift_entry {
409 uint8_t rshift;
410 uint8_t overflow;
411 } aes_block_rshift_table[UINT8_MAX+1] = {
412 [0x00] = { .rshift = 0x00, .overflow = 0x00 },
413 [0x01] = { .rshift = 0x00, .overflow = 0x80 },
414 [0x02] = { .rshift = 0x01, .overflow = 0x00 },
415 [0x03] = { .rshift = 0x01, .overflow = 0x80 },
416 [0x04] = { .rshift = 0x02, .overflow = 0x00 },
417 [0x05] = { .rshift = 0x02, .overflow = 0x80 },
418 [0x06] = { .rshift = 0x03, .overflow = 0x00 },
419 [0x07] = { .rshift = 0x03, .overflow = 0x80 },
420 [0x08] = { .rshift = 0x04, .overflow = 0x00 },
421 [0x09] = { .rshift = 0x04, .overflow = 0x80 },
422 [0x0a] = { .rshift = 0x05, .overflow = 0x00 },
423 [0x0b] = { .rshift = 0x05, .overflow = 0x80 },
424 [0x0c] = { .rshift = 0x06, .overflow = 0x00 },
425 [0x0d] = { .rshift = 0x06, .overflow = 0x80 },
426 [0x0e] = { .rshift = 0x07, .overflow = 0x00 },
427 [0x0f] = { .rshift = 0x07, .overflow = 0x80 },
428 [0x10] = { .rshift = 0x08, .overflow = 0x00 },
429 [0x11] = { .rshift = 0x08, .overflow = 0x80 },
430 [0x12] = { .rshift = 0x09, .overflow = 0x00 },
431 [0x13] = { .rshift = 0x09, .overflow = 0x80 },
432 [0x14] = { .rshift = 0x0a, .overflow = 0x00 },
433 [0x15] = { .rshift = 0x0a, .overflow = 0x80 },
434 [0x16] = { .rshift = 0x0b, .overflow = 0x00 },
435 [0x17] = { .rshift = 0x0b, .overflow = 0x80 },
436 [0x18] = { .rshift = 0x0c, .overflow = 0x00 },
437 [0x19] = { .rshift = 0x0c, .overflow = 0x80 },
438 [0x1a] = { .rshift = 0x0d, .overflow = 0x00 },
439 [0x1b] = { .rshift = 0x0d, .overflow = 0x80 },
440 [0x1c] = { .rshift = 0x0e, .overflow = 0x00 },
441 [0x1d] = { .rshift = 0x0e, .overflow = 0x80 },
442 [0x1e] = { .rshift = 0x0f, .overflow = 0x00 },
443 [0x1f] = { .rshift = 0x0f, .overflow = 0x80 },
444 [0x20] = { .rshift = 0x10, .overflow = 0x00 },
445 [0x21] = { .rshift = 0x10, .overflow = 0x80 },
446 [0x22] = { .rshift = 0x11, .overflow = 0x00 },
447 [0x23] = { .rshift = 0x11, .overflow = 0x80 },
448 [0x24] = { .rshift = 0x12, .overflow = 0x00 },
449 [0x25] = { .rshift = 0x12, .overflow = 0x80 },
450 [0x26] = { .rshift = 0x13, .overflow = 0x00 },
451 [0x27] = { .rshift = 0x13, .overflow = 0x80 },
452 [0x28] = { .rshift = 0x14, .overflow = 0x00 },
453 [0x29] = { .rshift = 0x14, .overflow = 0x80 },
454 [0x2a] = { .rshift = 0x15, .overflow = 0x00 },
455 [0x2b] = { .rshift = 0x15, .overflow = 0x80 },
456 [0x2c] = { .rshift = 0x16, .overflow = 0x00 },
457 [0x2d] = { .rshift = 0x16, .overflow = 0x80 },
458 [0x2e] = { .rshift = 0x17, .overflow = 0x00 },
459 [0x2f] = { .rshift = 0x17, .overflow = 0x80 },
460 [0x30] = { .rshift = 0x18, .overflow = 0x00 },
461 [0x31] = { .rshift = 0x18, .overflow = 0x80 },
462 [0x32] = { .rshift = 0x19, .overflow = 0x00 },
463 [0x33] = { .rshift = 0x19, .overflow = 0x80 },
464 [0x34] = { .rshift = 0x1a, .overflow = 0x00 },
465 [0x35] = { .rshift = 0x1a, .overflow = 0x80 },
466 [0x36] = { .rshift = 0x1b, .overflow = 0x00 },
467 [0x37] = { .rshift = 0x1b, .overflow = 0x80 },
468 [0x38] = { .rshift = 0x1c, .overflow = 0x00 },
469 [0x39] = { .rshift = 0x1c, .overflow = 0x80 },
470 [0x3a] = { .rshift = 0x1d, .overflow = 0x00 },
471 [0x3b] = { .rshift = 0x1d, .overflow = 0x80 },
472 [0x3c] = { .rshift = 0x1e, .overflow = 0x00 },
473 [0x3d] = { .rshift = 0x1e, .overflow = 0x80 },
474 [0x3e] = { .rshift = 0x1f, .overflow = 0x00 },
475 [0x3f] = { .rshift = 0x1f, .overflow = 0x80 },
476 [0x40] = { .rshift = 0x20, .overflow = 0x00 },
477 [0x41] = { .rshift = 0x20, .overflow = 0x80 },
478 [0x42] = { .rshift = 0x21, .overflow = 0x00 },
479 [0x43] = { .rshift = 0x21, .overflow = 0x80 },
480 [0x44] = { .rshift = 0x22, .overflow = 0x00 },
481 [0x45] = { .rshift = 0x22, .overflow = 0x80 },
482 [0x46] = { .rshift = 0x23, .overflow = 0x00 },
483 [0x47] = { .rshift = 0x23, .overflow = 0x80 },
484 [0x48] = { .rshift = 0x24, .overflow = 0x00 },
485 [0x49] = { .rshift = 0x24, .overflow = 0x80 },
486 [0x4a] = { .rshift = 0x25, .overflow = 0x00 },
487 [0x4b] = { .rshift = 0x25, .overflow = 0x80 },
488 [0x4c] = { .rshift = 0x26, .overflow = 0x00 },
489 [0x4d] = { .rshift = 0x26, .overflow = 0x80 },
490 [0x4e] = { .rshift = 0x27, .overflow = 0x00 },
491 [0x4f] = { .rshift = 0x27, .overflow = 0x80 },
492 [0x50] = { .rshift = 0x28, .overflow = 0x00 },
493 [0x51] = { .rshift = 0x28, .overflow = 0x80 },
494 [0x52] = { .rshift = 0x29, .overflow = 0x00 },
495 [0x53] = { .rshift = 0x29, .overflow = 0x80 },
496 [0x54] = { .rshift = 0x2a, .overflow = 0x00 },
497 [0x55] = { .rshift = 0x2a, .overflow = 0x80 },
498 [0x56] = { .rshift = 0x2b, .overflow = 0x00 },
499 [0x57] = { .rshift = 0x2b, .overflow = 0x80 },
500 [0x58] = { .rshift = 0x2c, .overflow = 0x00 },
501 [0x59] = { .rshift = 0x2c, .overflow = 0x80 },
502 [0x5a] = { .rshift = 0x2d, .overflow = 0x00 },
503 [0x5b] = { .rshift = 0x2d, .overflow = 0x80 },
504 [0x5c] = { .rshift = 0x2e, .overflow = 0x00 },
505 [0x5d] = { .rshift = 0x2e, .overflow = 0x80 },
506 [0x5e] = { .rshift = 0x2f, .overflow = 0x00 },
507 [0x5f] = { .rshift = 0x2f, .overflow = 0x80 },
508 [0x60] = { .rshift = 0x30, .overflow = 0x00 },
509 [0x61] = { .rshift = 0x30, .overflow = 0x80 },
510 [0x62] = { .rshift = 0x31, .overflow = 0x00 },
511 [0x63] = { .rshift = 0x31, .overflow = 0x80 },
512 [0x64] = { .rshift = 0x32, .overflow = 0x00 },
513 [0x65] = { .rshift = 0x32, .overflow = 0x80 },
514 [0x66] = { .rshift = 0x33, .overflow = 0x00 },
515 [0x67] = { .rshift = 0x33, .overflow = 0x80 },
516 [0x68] = { .rshift = 0x34, .overflow = 0x00 },
517 [0x69] = { .rshift = 0x34, .overflow = 0x80 },
518 [0x6a] = { .rshift = 0x35, .overflow = 0x00 },
519 [0x6b] = { .rshift = 0x35, .overflow = 0x80 },
520 [0x6c] = { .rshift = 0x36, .overflow = 0x00 },
521 [0x6d] = { .rshift = 0x36, .overflow = 0x80 },
522 [0x6e] = { .rshift = 0x37, .overflow = 0x00 },
523 [0x6f] = { .rshift = 0x37, .overflow = 0x80 },
524 [0x70] = { .rshift = 0x38, .overflow = 0x00 },
525 [0x71] = { .rshift = 0x38, .overflow = 0x80 },
526 [0x72] = { .rshift = 0x39, .overflow = 0x00 },
527 [0x73] = { .rshift = 0x39, .overflow = 0x80 },
528 [0x74] = { .rshift = 0x3a, .overflow = 0x00 },
529 [0x75] = { .rshift = 0x3a, .overflow = 0x80 },
530 [0x76] = { .rshift = 0x3b, .overflow = 0x00 },
531 [0x77] = { .rshift = 0x3b, .overflow = 0x80 },
532 [0x78] = { .rshift = 0x3c, .overflow = 0x00 },
533 [0x79] = { .rshift = 0x3c, .overflow = 0x80 },
534 [0x7a] = { .rshift = 0x3d, .overflow = 0x00 },
535 [0x7b] = { .rshift = 0x3d, .overflow = 0x80 },
536 [0x7c] = { .rshift = 0x3e, .overflow = 0x00 },
537 [0x7d] = { .rshift = 0x3e, .overflow = 0x80 },
538 [0x7e] = { .rshift = 0x3f, .overflow = 0x00 },
539 [0x7f] = { .rshift = 0x3f, .overflow = 0x80 },
540 [0x80] = { .rshift = 0x40, .overflow = 0x00 },
541 [0x81] = { .rshift = 0x40, .overflow = 0x80 },
542 [0x82] = { .rshift = 0x41, .overflow = 0x00 },
543 [0x83] = { .rshift = 0x41, .overflow = 0x80 },
544 [0x84] = { .rshift = 0x42, .overflow = 0x00 },
545 [0x85] = { .rshift = 0x42, .overflow = 0x80 },
546 [0x86] = { .rshift = 0x43, .overflow = 0x00 },
547 [0x87] = { .rshift = 0x43, .overflow = 0x80 },
548 [0x88] = { .rshift = 0x44, .overflow = 0x00 },
549 [0x89] = { .rshift = 0x44, .overflow = 0x80 },
550 [0x8a] = { .rshift = 0x45, .overflow = 0x00 },
551 [0x8b] = { .rshift = 0x45, .overflow = 0x80 },
552 [0x8c] = { .rshift = 0x46, .overflow = 0x00 },
553 [0x8d] = { .rshift = 0x46, .overflow = 0x80 },
554 [0x8e] = { .rshift = 0x47, .overflow = 0x00 },
555 [0x8f] = { .rshift = 0x47, .overflow = 0x80 },
556 [0x90] = { .rshift = 0x48, .overflow = 0x00 },
557 [0x91] = { .rshift = 0x48, .overflow = 0x80 },
558 [0x92] = { .rshift = 0x49, .overflow = 0x00 },
559 [0x93] = { .rshift = 0x49, .overflow = 0x80 },
560 [0x94] = { .rshift = 0x4a, .overflow = 0x00 },
561 [0x95] = { .rshift = 0x4a, .overflow = 0x80 },
562 [0x96] = { .rshift = 0x4b, .overflow = 0x00 },
563 [0x97] = { .rshift = 0x4b, .overflow = 0x80 },
564 [0x98] = { .rshift = 0x4c, .overflow = 0x00 },
565 [0x99] = { .rshift = 0x4c, .overflow = 0x80 },
566 [0x9a] = { .rshift = 0x4d, .overflow = 0x00 },
567 [0x9b] = { .rshift = 0x4d, .overflow = 0x80 },
568 [0x9c] = { .rshift = 0x4e, .overflow = 0x00 },
569 [0x9d] = { .rshift = 0x4e, .overflow = 0x80 },
570 [0x9e] = { .rshift = 0x4f, .overflow = 0x00 },
571 [0x9f] = { .rshift = 0x4f, .overflow = 0x80 },
572 [0xa0] = { .rshift = 0x50, .overflow = 0x00 },
573 [0xa1] = { .rshift = 0x50, .overflow = 0x80 },
574 [0xa2] = { .rshift = 0x51, .overflow = 0x00 },
575 [0xa3] = { .rshift = 0x51, .overflow = 0x80 },
576 [0xa4] = { .rshift = 0x52, .overflow = 0x00 },
577 [0xa5] = { .rshift = 0x52, .overflow = 0x80 },
578 [0xa6] = { .rshift = 0x53, .overflow = 0x00 },
579 [0xa7] = { .rshift = 0x53, .overflow = 0x80 },
580 [0xa8] = { .rshift = 0x54, .overflow = 0x00 },
581 [0xa9] = { .rshift = 0x54, .overflow = 0x80 },
582 [0xaa] = { .rshift = 0x55, .overflow = 0x00 },
583 [0xab] = { .rshift = 0x55, .overflow = 0x80 },
584 [0xac] = { .rshift = 0x56, .overflow = 0x00 },
585 [0xad] = { .rshift = 0x56, .overflow = 0x80 },
586 [0xae] = { .rshift = 0x57, .overflow = 0x00 },
587 [0xaf] = { .rshift = 0x57, .overflow = 0x80 },
588 [0xb0] = { .rshift = 0x58, .overflow = 0x00 },
589 [0xb1] = { .rshift = 0x58, .overflow = 0x80 },
590 [0xb2] = { .rshift = 0x59, .overflow = 0x00 },
591 [0xb3] = { .rshift = 0x59, .overflow = 0x80 },
592 [0xb4] = { .rshift = 0x5a, .overflow = 0x00 },
593 [0xb5] = { .rshift = 0x5a, .overflow = 0x80 },
594 [0xb6] = { .rshift = 0x5b, .overflow = 0x00 },
595 [0xb7] = { .rshift = 0x5b, .overflow = 0x80 },
596 [0xb8] = { .rshift = 0x5c, .overflow = 0x00 },
597 [0xb9] = { .rshift = 0x5c, .overflow = 0x80 },
598 [0xba] = { .rshift = 0x5d, .overflow = 0x00 },
599 [0xbb] = { .rshift = 0x5d, .overflow = 0x80 },
600 [0xbc] = { .rshift = 0x5e, .overflow = 0x00 },
601 [0xbd] = { .rshift = 0x5e, .overflow = 0x80 },
602 [0xbe] = { .rshift = 0x5f, .overflow = 0x00 },
603 [0xbf] = { .rshift = 0x5f, .overflow = 0x80 },
604 [0xc0] = { .rshift = 0x60, .overflow = 0x00 },
605 [0xc1] = { .rshift = 0x60, .overflow = 0x80 },
606 [0xc2] = { .rshift = 0x61, .overflow = 0x00 },
607 [0xc3] = { .rshift = 0x61, .overflow = 0x80 },
608 [0xc4] = { .rshift = 0x62, .overflow = 0x00 },
609 [0xc5] = { .rshift = 0x62, .overflow = 0x80 },
610 [0xc6] = { .rshift = 0x63, .overflow = 0x00 },
611 [0xc7] = { .rshift = 0x63, .overflow = 0x80 },
612 [0xc8] = { .rshift = 0x64, .overflow = 0x00 },
613 [0xc9] = { .rshift = 0x64, .overflow = 0x80 },
614 [0xca] = { .rshift = 0x65, .overflow = 0x00 },
615 [0xcb] = { .rshift = 0x65, .overflow = 0x80 },
616 [0xcc] = { .rshift = 0x66, .overflow = 0x00 },
617 [0xcd] = { .rshift = 0x66, .overflow = 0x80 },
618 [0xce] = { .rshift = 0x67, .overflow = 0x00 },
619 [0xcf] = { .rshift = 0x67, .overflow = 0x80 },
620 [0xd0] = { .rshift = 0x68, .overflow = 0x00 },
621 [0xd1] = { .rshift = 0x68, .overflow = 0x80 },
622 [0xd2] = { .rshift = 0x69, .overflow = 0x00 },
623 [0xd3] = { .rshift = 0x69, .overflow = 0x80 },
624 [0xd4] = { .rshift = 0x6a, .overflow = 0x00 },
625 [0xd5] = { .rshift = 0x6a, .overflow = 0x80 },
626 [0xd6] = { .rshift = 0x6b, .overflow = 0x00 },
627 [0xd7] = { .rshift = 0x6b, .overflow = 0x80 },
628 [0xd8] = { .rshift = 0x6c, .overflow = 0x00 },
629 [0xd9] = { .rshift = 0x6c, .overflow = 0x80 },
630 [0xda] = { .rshift = 0x6d, .overflow = 0x00 },
631 [0xdb] = { .rshift = 0x6d, .overflow = 0x80 },
632 [0xdc] = { .rshift = 0x6e, .overflow = 0x00 },
633 [0xdd] = { .rshift = 0x6e, .overflow = 0x80 },
634 [0xde] = { .rshift = 0x6f, .overflow = 0x00 },
635 [0xdf] = { .rshift = 0x6f, .overflow = 0x80 },
636 [0xe0] = { .rshift = 0x70, .overflow = 0x00 },
637 [0xe1] = { .rshift = 0x70, .overflow = 0x80 },
638 [0xe2] = { .rshift = 0x71, .overflow = 0x00 },
639 [0xe3] = { .rshift = 0x71, .overflow = 0x80 },
640 [0xe4] = { .rshift = 0x72, .overflow = 0x00 },
641 [0xe5] = { .rshift = 0x72, .overflow = 0x80 },
642 [0xe6] = { .rshift = 0x73, .overflow = 0x00 },
643 [0xe7] = { .rshift = 0x73, .overflow = 0x80 },
644 [0xe8] = { .rshift = 0x74, .overflow = 0x00 },
645 [0xe9] = { .rshift = 0x74, .overflow = 0x80 },
646 [0xea] = { .rshift = 0x75, .overflow = 0x00 },
647 [0xeb] = { .rshift = 0x75, .overflow = 0x80 },
648 [0xec] = { .rshift = 0x76, .overflow = 0x00 },
649 [0xed] = { .rshift = 0x76, .overflow = 0x80 },
650 [0xee] = { .rshift = 0x77, .overflow = 0x00 },
651 [0xef] = { .rshift = 0x77, .overflow = 0x80 },
652 [0xf0] = { .rshift = 0x78, .overflow = 0x00 },
653 [0xf1] = { .rshift = 0x78, .overflow = 0x80 },
654 [0xf2] = { .rshift = 0x79, .overflow = 0x00 },
655 [0xf3] = { .rshift = 0x79, .overflow = 0x80 },
656 [0xf4] = { .rshift = 0x7a, .overflow = 0x00 },
657 [0xf5] = { .rshift = 0x7a, .overflow = 0x80 },
658 [0xf6] = { .rshift = 0x7b, .overflow = 0x00 },
659 [0xf7] = { .rshift = 0x7b, .overflow = 0x80 },
660 [0xf8] = { .rshift = 0x7c, .overflow = 0x00 },
661 [0xf9] = { .rshift = 0x7c, .overflow = 0x80 },
662 [0xfa] = { .rshift = 0x7d, .overflow = 0x00 },
663 [0xfb] = { .rshift = 0x7d, .overflow = 0x80 },
664 [0xfc] = { .rshift = 0x7e, .overflow = 0x00 },
665 [0xfd] = { .rshift = 0x7e, .overflow = 0x80 },
666 [0xfe] = { .rshift = 0x7f, .overflow = 0x00 },
667 [0xff] = { .rshift = 0x7f, .overflow = 0x80 },
669 uint8_t i;
670 uint8_t overflow = 0;
672 for (i = 0; i < AES_BLOCK_SIZE; i++) {
673 const struct aes_block_rshift_entry *e = &aes_block_rshift_table[in[i]];
674 out[i] = e->rshift | overflow;
675 overflow = e->overflow;
678 #endif /* LIB_CRYPTO_AES_H */