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>
39 #include <linux/kernel.h>
40 #include <linux/module.h>
41 #include <linux/init.h>
42 #include <linux/types.h>
43 #include <linux/crypto.h>
44 #include <linux/linkage.h>
46 asmlinkage void aes_enc_blk(const u8 *src, u8 *dst, void *ctx);
47 asmlinkage void aes_dec_blk(const u8 *src, u8 *dst, void *ctx);
49 #define AES_MIN_KEY_SIZE 16
50 #define AES_MAX_KEY_SIZE 32
51 #define AES_BLOCK_SIZE 16
52 #define AES_KS_LENGTH 4 * AES_BLOCK_SIZE
56 u32 ekey[AES_KS_LENGTH];
58 u32 dkey[AES_KS_LENGTH];
62 #define u32_in(x) le32_to_cpup((const __le32 *)(x))
63 #define bytes2word(b0, b1, b2, b3) \
64 (((u32)(b3) << 24) | ((u32)(b2) << 16) | ((u32)(b1) << 8) | (b0))
66 /* define the finite field multiplies required for Rijndael */
67 #define f2(x) ((x) ? pow[log[x] + 0x19] : 0)
68 #define f3(x) ((x) ? pow[log[x] + 0x01] : 0)
69 #define f9(x) ((x) ? pow[log[x] + 0xc7] : 0)
70 #define fb(x) ((x) ? pow[log[x] + 0x68] : 0)
71 #define fd(x) ((x) ? pow[log[x] + 0xee] : 0)
72 #define fe(x) ((x) ? pow[log[x] + 0xdf] : 0)
73 #define fi(x) ((x) ? pow[255 - log[x]]: 0)
75 static inline u32 upr(u32 x, int n)
77 return (x << 8 * n) | (x >> (32 - 8 * n));
80 static inline u8 bval(u32 x, int n)
85 /* The forward and inverse affine transformations used in the S-box */
86 #define fwd_affine(x) \
87 (w = (u32)x, w ^= (w<<1)^(w<<2)^(w<<3)^(w<<4), 0x63^(u8)(w^(w>>8)))
89 #define inv_affine(x) \
90 (w = (u32)x, w = (w<<1)^(w<<3)^(w<<6), 0x05^(u8)(w^(w>>8)))
92 static u32 rcon_tab[RC_LENGTH];
96 static u32 ls_tab[4][256];
97 static u32 im_tab[4][256];
101 static void gen_tabs(void)
104 u8 pow[512], log[256];
107 * log and power tables for GF(2^8) finite field with
108 * WPOLY as modular polynomial - the simplest primitive
109 * root is 0x03, used here to generate the tables.
115 pow[i + 255] = (u8)w;
117 w ^= (w << 1) ^ (w & 0x80 ? WPOLY : 0);
120 for(i = 0, w = 1; i < RC_LENGTH; ++i) {
121 rcon_tab[i] = bytes2word(w, 0, 0, 0);
125 for(i = 0; i < 256; ++i) {
128 b = fwd_affine(fi((u8)i));
129 w = bytes2word(f2(b), b, b, f3(b));
131 /* tables for a normal encryption round */
133 ft_tab[1][i] = upr(w, 1);
134 ft_tab[2][i] = upr(w, 2);
135 ft_tab[3][i] = upr(w, 3);
136 w = bytes2word(b, 0, 0, 0);
139 * tables for last encryption round
140 * (may also be used in the key schedule)
143 fl_tab[1][i] = upr(w, 1);
144 fl_tab[2][i] = upr(w, 2);
145 fl_tab[3][i] = upr(w, 3);
148 * table for key schedule if fl_tab above is
149 * not of the required form
152 ls_tab[1][i] = upr(w, 1);
153 ls_tab[2][i] = upr(w, 2);
154 ls_tab[3][i] = upr(w, 3);
156 b = fi(inv_affine((u8)i));
157 w = bytes2word(fe(b), f9(b), fd(b), fb(b));
159 /* tables for the inverse mix column operation */
161 im_tab[1][b] = upr(w, 1);
162 im_tab[2][b] = upr(w, 2);
163 im_tab[3][b] = upr(w, 3);
165 /* tables for a normal decryption round */
167 it_tab[1][i] = upr(w,1);
168 it_tab[2][i] = upr(w,2);
169 it_tab[3][i] = upr(w,3);
171 w = bytes2word(b, 0, 0, 0);
173 /* tables for last decryption round */
175 il_tab[1][i] = upr(w,1);
176 il_tab[2][i] = upr(w,2);
177 il_tab[3][i] = upr(w,3);
181 #define four_tables(x,tab,vf,rf,c) \
182 ( tab[0][bval(vf(x,0,c),rf(0,c))] ^ \
183 tab[1][bval(vf(x,1,c),rf(1,c))] ^ \
184 tab[2][bval(vf(x,2,c),rf(2,c))] ^ \
185 tab[3][bval(vf(x,3,c),rf(3,c))] \
188 #define vf1(x,r,c) (x)
190 #define rf2(r,c) ((r-c)&3)
192 #define inv_mcol(x) four_tables(x,im_tab,vf1,rf1,0)
193 #define ls_box(x,c) four_tables(x,fl_tab,vf1,rf2,c)
195 #define ff(x) inv_mcol(x)
199 k[4*(i)+4] = ss[0] ^= ls_box(ss[3],3) ^ rcon_tab[i]; \
200 k[4*(i)+5] = ss[1] ^= ss[0]; \
201 k[4*(i)+6] = ss[2] ^= ss[1]; \
202 k[4*(i)+7] = ss[3] ^= ss[2]; \
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]; k[4*(i)+7] = ss[3] ^= ss[2]; \
214 k[6*(i)+ 6] = ss[0] ^= ls_box(ss[5],3) ^ rcon_tab[i]; \
215 k[6*(i)+ 7] = ss[1] ^= ss[0]; \
216 k[6*(i)+ 8] = ss[2] ^= ss[1]; \
217 k[6*(i)+ 9] = ss[3] ^= ss[2]; \
218 k[6*(i)+10] = ss[4] ^= ss[3]; \
219 k[6*(i)+11] = ss[5] ^= ss[4]; \
224 k[6*(i)+ 6] = ss[0] ^= ls_box(ss[5],3) ^ rcon_tab[i]; \
225 k[6*(i)+ 7] = ss[1] ^= ss[0]; \
226 k[6*(i)+ 8] = ss[2] ^= ss[1]; \
227 k[6*(i)+ 9] = ss[3] ^= ss[2]; \
232 k[8*(i)+ 8] = ss[0] ^= ls_box(ss[7],3) ^ rcon_tab[i]; \
233 k[8*(i)+ 9] = ss[1] ^= ss[0]; \
234 k[8*(i)+10] = ss[2] ^= ss[1]; \
235 k[8*(i)+11] = ss[3] ^= ss[2]; \
236 k[8*(i)+12] = ss[4] ^= ls_box(ss[3],0); \
237 k[8*(i)+13] = ss[5] ^= ss[4]; \
238 k[8*(i)+14] = ss[6] ^= ss[5]; \
239 k[8*(i)+15] = ss[7] ^= ss[6]; \
244 k[8*(i)+ 8] = ss[0] ^= ls_box(ss[7],3) ^ rcon_tab[i]; \
245 k[8*(i)+ 9] = ss[1] ^= ss[0]; \
246 k[8*(i)+10] = ss[2] ^= ss[1]; \
247 k[8*(i)+11] = ss[3] ^= ss[2]; \
252 ss[0] = ss[0] ^ ss[2] ^ ss[1] ^ ss[3]; \
253 ss[1] = ss[1] ^ ss[3]; \
254 ss[2] = ss[2] ^ ss[3]; \
256 ss[4] = ls_box(ss[(i+3) % 4], 3) ^ rcon_tab[i]; \
257 ss[i % 4] ^= ss[4]; \
259 k[4*(i)+4] = ff(ss[4]); \
260 ss[4] ^= k[4*(i)+1]; \
261 k[4*(i)+5] = ff(ss[4]); \
262 ss[4] ^= k[4*(i)+2]; \
263 k[4*(i)+6] = ff(ss[4]); \
264 ss[4] ^= k[4*(i)+3]; \
265 k[4*(i)+7] = ff(ss[4]); \
270 ss[4] = ls_box(ss[(i+3) % 4], 3) ^ rcon_tab[i]; \
271 ss[i % 4] ^= ss[4]; \
273 k[4*(i)+4] = ss[4] ^= k[4*(i)]; \
274 k[4*(i)+5] = ss[4] ^= k[4*(i)+1]; \
275 k[4*(i)+6] = ss[4] ^= k[4*(i)+2]; \
276 k[4*(i)+7] = ss[4] ^= k[4*(i)+3]; \
281 ss[4] = ls_box(ss[(i+3) % 4], 3) ^ rcon_tab[i]; \
282 ss[i % 4] ^= ss[4]; \
283 k[4*(i)+4] = (ss[0] ^= ss[1]) ^ ss[2] ^ ss[3]; \
284 k[4*(i)+5] = ss[1] ^ ss[3]; \
285 k[4*(i)+6] = ss[0]; \
286 k[4*(i)+7] = ss[1]; \
291 ss[0] ^= ls_box(ss[5],3) ^ rcon_tab[i]; \
292 k[6*(i)+ 6] = ff(ss[0]); \
294 k[6*(i)+ 7] = ff(ss[1]); \
296 k[6*(i)+ 8] = ff(ss[2]); \
298 k[6*(i)+ 9] = ff(ss[3]); \
300 k[6*(i)+10] = ff(ss[4]); \
302 k[6*(i)+11] = ff(ss[5]); \
307 ss[6] = ls_box(ss[5],3) ^ rcon_tab[i]; \
308 ss[0] ^= ss[6]; ss[6] = ff(ss[6]); \
309 k[6*(i)+ 6] = ss[6] ^= k[6*(i)]; \
311 k[6*(i)+ 7] = ss[6] ^= k[6*(i)+ 1]; \
313 k[6*(i)+ 8] = ss[6] ^= k[6*(i)+ 2]; \
315 k[6*(i)+ 9] = ss[6] ^= k[6*(i)+ 3]; \
317 k[6*(i)+10] = ss[6] ^= k[6*(i)+ 4]; \
319 k[6*(i)+11] = ss[6] ^= k[6*(i)+ 5]; \
324 ss[0] ^= ls_box(ss[5],3) ^ rcon_tab[i]; \
325 k[6*(i)+ 6] = ss[0]; \
327 k[6*(i)+ 7] = ss[1]; \
329 k[6*(i)+ 8] = ss[2]; \
331 k[6*(i)+ 9] = ss[3]; \
336 ss[0] ^= ls_box(ss[7],3) ^ rcon_tab[i]; \
337 k[8*(i)+ 8] = ff(ss[0]); \
339 k[8*(i)+ 9] = ff(ss[1]); \
341 k[8*(i)+10] = ff(ss[2]); \
343 k[8*(i)+11] = ff(ss[3]); \
344 ss[4] ^= ls_box(ss[3],0); \
345 k[8*(i)+12] = ff(ss[4]); \
347 k[8*(i)+13] = ff(ss[5]); \
349 k[8*(i)+14] = ff(ss[6]); \
351 k[8*(i)+15] = ff(ss[7]); \
356 u32 __g = ls_box(ss[7],3) ^ rcon_tab[i]; \
359 k[8*(i)+ 8] = __g ^= k[8*(i)]; \
361 k[8*(i)+ 9] = __g ^= k[8*(i)+ 1]; \
363 k[8*(i)+10] = __g ^= k[8*(i)+ 2]; \
365 k[8*(i)+11] = __g ^= k[8*(i)+ 3]; \
366 __g = ls_box(ss[3],0); \
369 k[8*(i)+12] = __g ^= k[8*(i)+ 4]; \
371 k[8*(i)+13] = __g ^= k[8*(i)+ 5]; \
373 k[8*(i)+14] = __g ^= k[8*(i)+ 6]; \
375 k[8*(i)+15] = __g ^= k[8*(i)+ 7]; \
380 ss[0] ^= ls_box(ss[7],3) ^ rcon_tab[i]; \
381 k[8*(i)+ 8] = ss[0]; \
383 k[8*(i)+ 9] = ss[1]; \
385 k[8*(i)+10] = ss[2]; \
387 k[8*(i)+11] = ss[3]; \
391 aes_set_key(void *ctx_arg, const u8 *in_key, unsigned int key_len, u32 *flags)
395 struct aes_ctx *ctx = ctx_arg;
397 /* encryption schedule */
399 ctx->ekey[0] = ss[0] = u32_in(in_key);
400 ctx->ekey[1] = ss[1] = u32_in(in_key + 4);
401 ctx->ekey[2] = ss[2] = u32_in(in_key + 8);
402 ctx->ekey[3] = ss[3] = u32_in(in_key + 12);
406 for (i = 0; i < 9; i++)
413 ctx->ekey[4] = ss[4] = u32_in(in_key + 16);
414 ctx->ekey[5] = ss[5] = u32_in(in_key + 20);
415 for (i = 0; i < 7; i++)
422 ctx->ekey[4] = ss[4] = u32_in(in_key + 16);
423 ctx->ekey[5] = ss[5] = u32_in(in_key + 20);
424 ctx->ekey[6] = ss[6] = u32_in(in_key + 24);
425 ctx->ekey[7] = ss[7] = u32_in(in_key + 28);
426 for (i = 0; i < 6; i++)
433 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
437 /* decryption schedule */
439 ctx->dkey[0] = ss[0] = u32_in(in_key);
440 ctx->dkey[1] = ss[1] = u32_in(in_key + 4);
441 ctx->dkey[2] = ss[2] = u32_in(in_key + 8);
442 ctx->dkey[3] = ss[3] = u32_in(in_key + 12);
447 for (i = 1; i < 9; i++)
453 ctx->dkey[4] = ff(ss[4] = u32_in(in_key + 16));
454 ctx->dkey[5] = ff(ss[5] = u32_in(in_key + 20));
456 for (i = 1; i < 7; i++)
462 ctx->dkey[4] = ff(ss[4] = u32_in(in_key + 16));
463 ctx->dkey[5] = ff(ss[5] = u32_in(in_key + 20));
464 ctx->dkey[6] = ff(ss[6] = u32_in(in_key + 24));
465 ctx->dkey[7] = ff(ss[7] = u32_in(in_key + 28));
467 for (i = 1; i < 6; i++)
475 static inline void aes_encrypt(void *ctx, u8 *dst, const u8 *src)
477 aes_enc_blk(src, dst, ctx);
479 static inline void aes_decrypt(void *ctx, u8 *dst, const u8 *src)
481 aes_dec_blk(src, dst, ctx);
485 static struct crypto_alg aes_alg = {
487 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
488 .cra_blocksize = AES_BLOCK_SIZE,
489 .cra_ctxsize = sizeof(struct aes_ctx),
490 .cra_module = THIS_MODULE,
491 .cra_list = LIST_HEAD_INIT(aes_alg.cra_list),
494 .cia_min_keysize = AES_MIN_KEY_SIZE,
495 .cia_max_keysize = AES_MAX_KEY_SIZE,
496 .cia_setkey = aes_set_key,
497 .cia_encrypt = aes_encrypt,
498 .cia_decrypt = aes_decrypt
503 static int __init aes_init(void)
506 return crypto_register_alg(&aes_alg);
509 static void __exit aes_fini(void)
511 crypto_unregister_alg(&aes_alg);
514 module_init(aes_init);
515 module_exit(aes_fini);
517 MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm, i586 asm optimized");
518 MODULE_LICENSE("Dual BSD/GPL");
519 MODULE_AUTHOR("Fruhwirth Clemens, James Morris, Brian Gladman, Adam Richter");