3 * Glue Code for optimized 586 assembler version of AES
5 * Copyright (c) 2002, Dr Brian Gladman <>, Worcester, UK.
10 * The free distribution and use of this software in both source and binary
11 * form is allowed (with or without changes) provided that:
13 * 1. distributions of this source code include the above copyright
14 * notice, this list of conditions and the following disclaimer;
16 * 2. distributions in binary form include the above copyright
17 * notice, this list of conditions and the following disclaimer
18 * in the documentation and/or other associated materials;
20 * 3. the copyright holder's name is not used to endorse products
21 * built using this software without specific written permission.
23 * ALTERNATIVELY, provided that this notice is retained in full, this product
24 * may be distributed under the terms of the GNU General Public License (GPL),
25 * in which case the provisions of the GPL apply INSTEAD OF those given above.
29 * This software is provided 'as is' with no explicit or implied warranties
30 * in respect of its properties, including, but not limited to, correctness
31 * and/or fitness for purpose.
33 * Copyright (c) 2003, Adam J. Richter <adam@yggdrasil.com> (conversion to
35 * Copyright (c) 2003, 2004 Fruhwirth Clemens <clemens@endorphin.org>
36 * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
40 #define BUILD_FOR_L4 1
43 #include <asm/byteorder.h>
44 #include <linux/kernel.h>
45 #include <linux/module.h>
46 #include <linux/init.h>
47 #include <linux/types.h>
48 #include <linux/crypto.h>
49 #include <linux/linkage.h>
53 #define __LIBCRYPTO_INTERNAL__
56 #endif /* BUILD_FOR_L4 */
59 asmlinkage void aes_enc_blk(const u8 *src, u8 *dst, void *ctx);
60 asmlinkage void aes_dec_blk(const u8 *src, u8 *dst, void *ctx);
64 #define AES_MIN_KEY_SIZE 16
65 #define AES_MAX_KEY_SIZE 32
66 #define AES_BLOCK_SIZE 16
67 #define AES_KS_LENGTH 4 * AES_BLOCK_SIZE
71 u32 ekey[AES_KS_LENGTH];
73 u32 dkey[AES_KS_LENGTH];
76 # define aes_ctx aes_586_ctx
81 #define bytes2word(b0, b1, b2, b3) \
82 (((u32)(b3) << 24) | ((u32)(b2) << 16) | ((u32)(b1) << 8) | (b0))
84 /* define the finite field multiplies required for Rijndael */
85 #define f2(x) ((x) ? pow[log[x] + 0x19] : 0)
86 #define f3(x) ((x) ? pow[log[x] + 0x01] : 0)
87 #define f9(x) ((x) ? pow[log[x] + 0xc7] : 0)
88 #define fb(x) ((x) ? pow[log[x] + 0x68] : 0)
89 #define fd(x) ((x) ? pow[log[x] + 0xee] : 0)
90 #define fe(x) ((x) ? pow[log[x] + 0xdf] : 0)
91 #define fi(x) ((x) ? pow[255 - log[x]]: 0)
93 static inline u32 upr(u32 x, int n)
95 return (x << 8 * n) | (x >> (32 - 8 * n));
98 static inline u8 bval(u32 x, int n)
103 /* The forward and inverse affine transformations used in the S-box */
104 #define fwd_affine(x) \
105 (w = (u32)x, w ^= (w<<1)^(w<<2)^(w<<3)^(w<<4), 0x63^(u8)(w^(w>>8)))
107 #define inv_affine(x) \
108 (w = (u32)x, w = (w<<1)^(w<<3)^(w<<6), 0x05^(u8)(w^(w>>8)))
110 static u32 rcon_tab[RC_LENGTH];
114 static u32 im_tab[4][256];
118 static void gen_tabs(void)
121 u8 pow[512], log[256];
124 * log and power tables for GF(2^8) finite field with
125 * WPOLY as modular polynomial - the simplest primitive
126 * root is 0x03, used here to generate the tables.
132 pow[i + 255] = (u8)w;
134 w ^= (w << 1) ^ (w & 0x80 ? WPOLY : 0);
137 for(i = 0, w = 1; i < RC_LENGTH; ++i) {
138 rcon_tab[i] = bytes2word(w, 0, 0, 0);
142 for(i = 0; i < 256; ++i) {
145 b = fwd_affine(fi((u8)i));
146 w = bytes2word(f2(b), b, b, f3(b));
148 /* tables for a normal encryption round */
150 ft_tab[1][i] = upr(w, 1);
151 ft_tab[2][i] = upr(w, 2);
152 ft_tab[3][i] = upr(w, 3);
153 w = bytes2word(b, 0, 0, 0);
156 * tables for last encryption round
157 * (may also be used in the key schedule)
160 fl_tab[1][i] = upr(w, 1);
161 fl_tab[2][i] = upr(w, 2);
162 fl_tab[3][i] = upr(w, 3);
164 b = fi(inv_affine((u8)i));
165 w = bytes2word(fe(b), f9(b), fd(b), fb(b));
167 /* tables for the inverse mix column operation */
169 im_tab[1][b] = upr(w, 1);
170 im_tab[2][b] = upr(w, 2);
171 im_tab[3][b] = upr(w, 3);
173 /* tables for a normal decryption round */
175 it_tab[1][i] = upr(w,1);
176 it_tab[2][i] = upr(w,2);
177 it_tab[3][i] = upr(w,3);
179 w = bytes2word(b, 0, 0, 0);
181 /* tables for last decryption round */
183 il_tab[1][i] = upr(w,1);
184 il_tab[2][i] = upr(w,2);
185 il_tab[3][i] = upr(w,3);
189 #define four_tables(x,tab,vf,rf,c) \
190 ( tab[0][bval(vf(x,0,c),rf(0,c))] ^ \
191 tab[1][bval(vf(x,1,c),rf(1,c))] ^ \
192 tab[2][bval(vf(x,2,c),rf(2,c))] ^ \
193 tab[3][bval(vf(x,3,c),rf(3,c))] \
196 #define vf1(x,r,c) (x)
198 #define rf2(r,c) ((r-c)&3)
200 #define inv_mcol(x) four_tables(x,im_tab,vf1,rf1,0)
201 #define ls_box(x,c) four_tables(x,fl_tab,vf1,rf2,c)
203 #define ff(x) inv_mcol(x)
207 k[4*(i)+4] = ss[0] ^= ls_box(ss[3],3) ^ rcon_tab[i]; \
208 k[4*(i)+5] = ss[1] ^= ss[0]; \
209 k[4*(i)+6] = ss[2] ^= ss[1]; \
210 k[4*(i)+7] = ss[3] ^= ss[2]; \
215 k[4*(i)+4] = ss[0] ^= ls_box(ss[3],3) ^ rcon_tab[i]; \
216 k[4*(i)+5] = ss[1] ^= ss[0]; \
217 k[4*(i)+6] = ss[2] ^= ss[1]; k[4*(i)+7] = ss[3] ^= ss[2]; \
222 k[6*(i)+ 6] = ss[0] ^= ls_box(ss[5],3) ^ rcon_tab[i]; \
223 k[6*(i)+ 7] = ss[1] ^= ss[0]; \
224 k[6*(i)+ 8] = ss[2] ^= ss[1]; \
225 k[6*(i)+ 9] = ss[3] ^= ss[2]; \
226 k[6*(i)+10] = ss[4] ^= ss[3]; \
227 k[6*(i)+11] = ss[5] ^= ss[4]; \
232 k[6*(i)+ 6] = ss[0] ^= ls_box(ss[5],3) ^ rcon_tab[i]; \
233 k[6*(i)+ 7] = ss[1] ^= ss[0]; \
234 k[6*(i)+ 8] = ss[2] ^= ss[1]; \
235 k[6*(i)+ 9] = ss[3] ^= ss[2]; \
240 k[8*(i)+ 8] = ss[0] ^= ls_box(ss[7],3) ^ rcon_tab[i]; \
241 k[8*(i)+ 9] = ss[1] ^= ss[0]; \
242 k[8*(i)+10] = ss[2] ^= ss[1]; \
243 k[8*(i)+11] = ss[3] ^= ss[2]; \
244 k[8*(i)+12] = ss[4] ^= ls_box(ss[3],0); \
245 k[8*(i)+13] = ss[5] ^= ss[4]; \
246 k[8*(i)+14] = ss[6] ^= ss[5]; \
247 k[8*(i)+15] = ss[7] ^= ss[6]; \
252 k[8*(i)+ 8] = ss[0] ^= ls_box(ss[7],3) ^ rcon_tab[i]; \
253 k[8*(i)+ 9] = ss[1] ^= ss[0]; \
254 k[8*(i)+10] = ss[2] ^= ss[1]; \
255 k[8*(i)+11] = ss[3] ^= ss[2]; \
260 ss[0] = ss[0] ^ ss[2] ^ ss[1] ^ ss[3]; \
261 ss[1] = ss[1] ^ ss[3]; \
262 ss[2] = ss[2] ^ ss[3]; \
264 ss[4] = ls_box(ss[(i+3) % 4], 3) ^ rcon_tab[i]; \
265 ss[i % 4] ^= ss[4]; \
267 k[4*(i)+4] = ff(ss[4]); \
268 ss[4] ^= k[4*(i)+1]; \
269 k[4*(i)+5] = ff(ss[4]); \
270 ss[4] ^= k[4*(i)+2]; \
271 k[4*(i)+6] = ff(ss[4]); \
272 ss[4] ^= k[4*(i)+3]; \
273 k[4*(i)+7] = ff(ss[4]); \
278 ss[4] = ls_box(ss[(i+3) % 4], 3) ^ rcon_tab[i]; \
279 ss[i % 4] ^= ss[4]; \
281 k[4*(i)+4] = ss[4] ^= k[4*(i)]; \
282 k[4*(i)+5] = ss[4] ^= k[4*(i)+1]; \
283 k[4*(i)+6] = ss[4] ^= k[4*(i)+2]; \
284 k[4*(i)+7] = ss[4] ^= k[4*(i)+3]; \
289 ss[4] = ls_box(ss[(i+3) % 4], 3) ^ rcon_tab[i]; \
290 ss[i % 4] ^= ss[4]; \
291 k[4*(i)+4] = (ss[0] ^= ss[1]) ^ ss[2] ^ ss[3]; \
292 k[4*(i)+5] = ss[1] ^ ss[3]; \
293 k[4*(i)+6] = ss[0]; \
294 k[4*(i)+7] = ss[1]; \
299 ss[0] ^= ls_box(ss[5],3) ^ rcon_tab[i]; \
300 k[6*(i)+ 6] = ff(ss[0]); \
302 k[6*(i)+ 7] = ff(ss[1]); \
304 k[6*(i)+ 8] = ff(ss[2]); \
306 k[6*(i)+ 9] = ff(ss[3]); \
308 k[6*(i)+10] = ff(ss[4]); \
310 k[6*(i)+11] = ff(ss[5]); \
315 ss[6] = ls_box(ss[5],3) ^ rcon_tab[i]; \
316 ss[0] ^= ss[6]; ss[6] = ff(ss[6]); \
317 k[6*(i)+ 6] = ss[6] ^= k[6*(i)]; \
319 k[6*(i)+ 7] = ss[6] ^= k[6*(i)+ 1]; \
321 k[6*(i)+ 8] = ss[6] ^= k[6*(i)+ 2]; \
323 k[6*(i)+ 9] = ss[6] ^= k[6*(i)+ 3]; \
325 k[6*(i)+10] = ss[6] ^= k[6*(i)+ 4]; \
327 k[6*(i)+11] = ss[6] ^= k[6*(i)+ 5]; \
332 ss[0] ^= ls_box(ss[5],3) ^ rcon_tab[i]; \
333 k[6*(i)+ 6] = ss[0]; \
335 k[6*(i)+ 7] = ss[1]; \
337 k[6*(i)+ 8] = ss[2]; \
339 k[6*(i)+ 9] = ss[3]; \
344 ss[0] ^= ls_box(ss[7],3) ^ rcon_tab[i]; \
345 k[8*(i)+ 8] = ff(ss[0]); \
347 k[8*(i)+ 9] = ff(ss[1]); \
349 k[8*(i)+10] = ff(ss[2]); \
351 k[8*(i)+11] = ff(ss[3]); \
352 ss[4] ^= ls_box(ss[3],0); \
353 k[8*(i)+12] = ff(ss[4]); \
355 k[8*(i)+13] = ff(ss[5]); \
357 k[8*(i)+14] = ff(ss[6]); \
359 k[8*(i)+15] = ff(ss[7]); \
364 u32 __g = ls_box(ss[7],3) ^ rcon_tab[i]; \
367 k[8*(i)+ 8] = __g ^= k[8*(i)]; \
369 k[8*(i)+ 9] = __g ^= k[8*(i)+ 1]; \
371 k[8*(i)+10] = __g ^= k[8*(i)+ 2]; \
373 k[8*(i)+11] = __g ^= k[8*(i)+ 3]; \
374 __g = ls_box(ss[3],0); \
377 k[8*(i)+12] = __g ^= k[8*(i)+ 4]; \
379 k[8*(i)+13] = __g ^= k[8*(i)+ 5]; \
381 k[8*(i)+14] = __g ^= k[8*(i)+ 6]; \
383 k[8*(i)+15] = __g ^= k[8*(i)+ 7]; \
388 ss[0] ^= ls_box(ss[7],3) ^ rcon_tab[i]; \
389 k[8*(i)+ 8] = ss[0]; \
391 k[8*(i)+ 9] = ss[1]; \
393 k[8*(i)+10] = ss[2]; \
395 k[8*(i)+11] = ss[3]; \
399 aes_set_key(void *ctx_arg, const u8 *in_key, unsigned int key_len, u32 *flags)
403 struct aes_ctx *ctx = ctx_arg;
404 const __le32 *key = (const __le32 *)in_key;
406 /* encryption schedule */
408 ctx->ekey[0] = ss[0] = le32_to_cpu(key[0]);
409 ctx->ekey[1] = ss[1] = le32_to_cpu(key[1]);
410 ctx->ekey[2] = ss[2] = le32_to_cpu(key[2]);
411 ctx->ekey[3] = ss[3] = le32_to_cpu(key[3]);
415 for (i = 0; i < 9; i++)
422 ctx->ekey[4] = ss[4] = le32_to_cpu(key[4]);
423 ctx->ekey[5] = ss[5] = le32_to_cpu(key[5]);
424 for (i = 0; i < 7; i++)
431 ctx->ekey[4] = ss[4] = le32_to_cpu(key[4]);
432 ctx->ekey[5] = ss[5] = le32_to_cpu(key[5]);
433 ctx->ekey[6] = ss[6] = le32_to_cpu(key[6]);
434 ctx->ekey[7] = ss[7] = le32_to_cpu(key[7]);
435 for (i = 0; i < 6; i++)
442 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
446 /* decryption schedule */
448 ctx->dkey[0] = ss[0] = le32_to_cpu(key[0]);
449 ctx->dkey[1] = ss[1] = le32_to_cpu(key[1]);
450 ctx->dkey[2] = ss[2] = le32_to_cpu(key[2]);
451 ctx->dkey[3] = ss[3] = le32_to_cpu(key[3]);
456 for (i = 1; i < 9; i++)
462 ctx->dkey[4] = ff(ss[4] = le32_to_cpu(key[4]));
463 ctx->dkey[5] = ff(ss[5] = le32_to_cpu(key[5]));
465 for (i = 1; i < 7; i++)
471 ctx->dkey[4] = ff(ss[4] = le32_to_cpu(key[4]));
472 ctx->dkey[5] = ff(ss[5] = le32_to_cpu(key[5]));
473 ctx->dkey[6] = ff(ss[6] = le32_to_cpu(key[6]));
474 ctx->dkey[7] = ff(ss[7] = le32_to_cpu(key[7]));
476 for (i = 1; i < 6; i++)
484 static inline void aes_encrypt(void *ctx, u8 *dst, const u8 *src)
486 aes_enc_blk(src, dst, ctx);
488 static inline void aes_decrypt(void *ctx, u8 *dst, const u8 *src)
490 aes_dec_blk(src, dst, ctx);
496 static struct crypto_alg aes_alg = {
498 .cra_driver_name = "aes-i586",
500 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
501 .cra_blocksize = AES_BLOCK_SIZE,
502 .cra_ctxsize = sizeof(struct aes_ctx),
503 .cra_module = THIS_MODULE,
504 .cra_list = LIST_HEAD_INIT(aes_alg.cra_list),
507 .cia_min_keysize = AES_MIN_KEY_SIZE,
508 .cia_max_keysize = AES_MAX_KEY_SIZE,
509 .cia_setkey = aes_set_key,
510 .cia_encrypt = aes_encrypt,
511 .cia_decrypt = aes_decrypt
516 static int __init aes_init(void)
519 return crypto_register_alg(&aes_alg);
522 static void __exit aes_fini(void)
524 crypto_unregister_alg(&aes_alg);
527 module_init(aes_init);
528 module_exit(aes_fini);
530 MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm, i586 asm optimized");
531 MODULE_LICENSE("Dual BSD/GPL");
532 MODULE_AUTHOR("Fruhwirth Clemens, James Morris, Brian Gladman, Adam Richter");
537 crypto_cipher_set_key_fn_t aes_cipher_set_key = (crypto_cipher_set_key_fn_t) aes_set_key;
538 crypto_cipher_encrypt_fn_t aes_cipher_encrypt = (crypto_cipher_encrypt_fn_t) aes_encrypt;
539 crypto_cipher_decrypt_fn_t aes_cipher_decrypt = (crypto_cipher_decrypt_fn_t) aes_decrypt;
541 static void init(void) __attribute__((constructor));
542 static void init(void)
547 #endif /* BUILD_FOR_L4 */