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 #include <asm/byteorder.h>
41 #include <linux/kernel.h>
42 #include <linux/module.h>
43 #include <linux/init.h>
44 #include <linux/types.h>
45 #include <linux/crypto.h>
46 #include <linux/linkage.h>
48 asmlinkage
void aes_enc_blk(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
49 asmlinkage
void aes_dec_blk(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
51 #define AES_MIN_KEY_SIZE 16
52 #define AES_MAX_KEY_SIZE 32
53 #define AES_BLOCK_SIZE 16
54 #define AES_KS_LENGTH 4 * AES_BLOCK_SIZE
58 u32 ekey
[AES_KS_LENGTH
];
60 u32 dkey
[AES_KS_LENGTH
];
64 #define bytes2word(b0, b1, b2, b3) \
65 (((u32)(b3) << 24) | ((u32)(b2) << 16) | ((u32)(b1) << 8) | (b0))
67 /* define the finite field multiplies required for Rijndael */
68 #define f2(x) ((x) ? pow[log[x] + 0x19] : 0)
69 #define f3(x) ((x) ? pow[log[x] + 0x01] : 0)
70 #define f9(x) ((x) ? pow[log[x] + 0xc7] : 0)
71 #define fb(x) ((x) ? pow[log[x] + 0x68] : 0)
72 #define fd(x) ((x) ? pow[log[x] + 0xee] : 0)
73 #define fe(x) ((x) ? pow[log[x] + 0xdf] : 0)
74 #define fi(x) ((x) ? pow[255 - log[x]]: 0)
76 static inline u32
upr(u32 x
, int n
)
78 return (x
<< 8 * n
) | (x
>> (32 - 8 * n
));
81 static inline u8
bval(u32 x
, int n
)
86 /* The forward and inverse affine transformations used in the S-box */
87 #define fwd_affine(x) \
88 (w = (u32)x, w ^= (w<<1)^(w<<2)^(w<<3)^(w<<4), 0x63^(u8)(w^(w>>8)))
90 #define inv_affine(x) \
91 (w = (u32)x, w = (w<<1)^(w<<3)^(w<<6), 0x05^(u8)(w^(w>>8)))
93 static u32 rcon_tab
[RC_LENGTH
];
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);
147 b
= fi(inv_affine((u8
)i
));
148 w
= bytes2word(fe(b
), f9(b
), fd(b
), fb(b
));
150 /* tables for the inverse mix column operation */
152 im_tab
[1][b
] = upr(w
, 1);
153 im_tab
[2][b
] = upr(w
, 2);
154 im_tab
[3][b
] = upr(w
, 3);
156 /* tables for a normal decryption round */
158 it_tab
[1][i
] = upr(w
,1);
159 it_tab
[2][i
] = upr(w
,2);
160 it_tab
[3][i
] = upr(w
,3);
162 w
= bytes2word(b
, 0, 0, 0);
164 /* tables for last decryption round */
166 il_tab
[1][i
] = upr(w
,1);
167 il_tab
[2][i
] = upr(w
,2);
168 il_tab
[3][i
] = upr(w
,3);
172 #define four_tables(x,tab,vf,rf,c) \
173 ( tab[0][bval(vf(x,0,c),rf(0,c))] ^ \
174 tab[1][bval(vf(x,1,c),rf(1,c))] ^ \
175 tab[2][bval(vf(x,2,c),rf(2,c))] ^ \
176 tab[3][bval(vf(x,3,c),rf(3,c))] \
179 #define vf1(x,r,c) (x)
181 #define rf2(r,c) ((r-c)&3)
183 #define inv_mcol(x) four_tables(x,im_tab,vf1,rf1,0)
184 #define ls_box(x,c) four_tables(x,fl_tab,vf1,rf2,c)
186 #define ff(x) inv_mcol(x)
190 k[4*(i)+4] = ss[0] ^= ls_box(ss[3],3) ^ rcon_tab[i]; \
191 k[4*(i)+5] = ss[1] ^= ss[0]; \
192 k[4*(i)+6] = ss[2] ^= ss[1]; \
193 k[4*(i)+7] = ss[3] ^= ss[2]; \
198 k[4*(i)+4] = ss[0] ^= ls_box(ss[3],3) ^ rcon_tab[i]; \
199 k[4*(i)+5] = ss[1] ^= ss[0]; \
200 k[4*(i)+6] = ss[2] ^= ss[1]; k[4*(i)+7] = ss[3] ^= ss[2]; \
205 k[6*(i)+ 6] = ss[0] ^= ls_box(ss[5],3) ^ rcon_tab[i]; \
206 k[6*(i)+ 7] = ss[1] ^= ss[0]; \
207 k[6*(i)+ 8] = ss[2] ^= ss[1]; \
208 k[6*(i)+ 9] = ss[3] ^= ss[2]; \
209 k[6*(i)+10] = ss[4] ^= ss[3]; \
210 k[6*(i)+11] = ss[5] ^= ss[4]; \
215 k[6*(i)+ 6] = ss[0] ^= ls_box(ss[5],3) ^ rcon_tab[i]; \
216 k[6*(i)+ 7] = ss[1] ^= ss[0]; \
217 k[6*(i)+ 8] = ss[2] ^= ss[1]; \
218 k[6*(i)+ 9] = ss[3] ^= ss[2]; \
223 k[8*(i)+ 8] = ss[0] ^= ls_box(ss[7],3) ^ rcon_tab[i]; \
224 k[8*(i)+ 9] = ss[1] ^= ss[0]; \
225 k[8*(i)+10] = ss[2] ^= ss[1]; \
226 k[8*(i)+11] = ss[3] ^= ss[2]; \
227 k[8*(i)+12] = ss[4] ^= ls_box(ss[3],0); \
228 k[8*(i)+13] = ss[5] ^= ss[4]; \
229 k[8*(i)+14] = ss[6] ^= ss[5]; \
230 k[8*(i)+15] = ss[7] ^= ss[6]; \
235 k[8*(i)+ 8] = ss[0] ^= ls_box(ss[7],3) ^ rcon_tab[i]; \
236 k[8*(i)+ 9] = ss[1] ^= ss[0]; \
237 k[8*(i)+10] = ss[2] ^= ss[1]; \
238 k[8*(i)+11] = ss[3] ^= ss[2]; \
243 ss[0] = ss[0] ^ ss[2] ^ ss[1] ^ ss[3]; \
244 ss[1] = ss[1] ^ ss[3]; \
245 ss[2] = ss[2] ^ ss[3]; \
247 ss[4] = ls_box(ss[(i+3) % 4], 3) ^ rcon_tab[i]; \
248 ss[i % 4] ^= ss[4]; \
250 k[4*(i)+4] = ff(ss[4]); \
251 ss[4] ^= k[4*(i)+1]; \
252 k[4*(i)+5] = ff(ss[4]); \
253 ss[4] ^= k[4*(i)+2]; \
254 k[4*(i)+6] = ff(ss[4]); \
255 ss[4] ^= k[4*(i)+3]; \
256 k[4*(i)+7] = ff(ss[4]); \
261 ss[4] = ls_box(ss[(i+3) % 4], 3) ^ rcon_tab[i]; \
262 ss[i % 4] ^= ss[4]; \
264 k[4*(i)+4] = ss[4] ^= k[4*(i)]; \
265 k[4*(i)+5] = ss[4] ^= k[4*(i)+1]; \
266 k[4*(i)+6] = ss[4] ^= k[4*(i)+2]; \
267 k[4*(i)+7] = ss[4] ^= k[4*(i)+3]; \
272 ss[4] = ls_box(ss[(i+3) % 4], 3) ^ rcon_tab[i]; \
273 ss[i % 4] ^= ss[4]; \
274 k[4*(i)+4] = (ss[0] ^= ss[1]) ^ ss[2] ^ ss[3]; \
275 k[4*(i)+5] = ss[1] ^ ss[3]; \
276 k[4*(i)+6] = ss[0]; \
277 k[4*(i)+7] = ss[1]; \
282 ss[0] ^= ls_box(ss[5],3) ^ rcon_tab[i]; \
283 k[6*(i)+ 6] = ff(ss[0]); \
285 k[6*(i)+ 7] = ff(ss[1]); \
287 k[6*(i)+ 8] = ff(ss[2]); \
289 k[6*(i)+ 9] = ff(ss[3]); \
291 k[6*(i)+10] = ff(ss[4]); \
293 k[6*(i)+11] = ff(ss[5]); \
298 ss[6] = ls_box(ss[5],3) ^ rcon_tab[i]; \
299 ss[0] ^= ss[6]; ss[6] = ff(ss[6]); \
300 k[6*(i)+ 6] = ss[6] ^= k[6*(i)]; \
302 k[6*(i)+ 7] = ss[6] ^= k[6*(i)+ 1]; \
304 k[6*(i)+ 8] = ss[6] ^= k[6*(i)+ 2]; \
306 k[6*(i)+ 9] = ss[6] ^= k[6*(i)+ 3]; \
308 k[6*(i)+10] = ss[6] ^= k[6*(i)+ 4]; \
310 k[6*(i)+11] = ss[6] ^= k[6*(i)+ 5]; \
315 ss[0] ^= ls_box(ss[5],3) ^ rcon_tab[i]; \
316 k[6*(i)+ 6] = ss[0]; \
318 k[6*(i)+ 7] = ss[1]; \
320 k[6*(i)+ 8] = ss[2]; \
322 k[6*(i)+ 9] = ss[3]; \
327 ss[0] ^= ls_box(ss[7],3) ^ rcon_tab[i]; \
328 k[8*(i)+ 8] = ff(ss[0]); \
330 k[8*(i)+ 9] = ff(ss[1]); \
332 k[8*(i)+10] = ff(ss[2]); \
334 k[8*(i)+11] = ff(ss[3]); \
335 ss[4] ^= ls_box(ss[3],0); \
336 k[8*(i)+12] = ff(ss[4]); \
338 k[8*(i)+13] = ff(ss[5]); \
340 k[8*(i)+14] = ff(ss[6]); \
342 k[8*(i)+15] = ff(ss[7]); \
347 u32 __g = ls_box(ss[7],3) ^ rcon_tab[i]; \
350 k[8*(i)+ 8] = __g ^= k[8*(i)]; \
352 k[8*(i)+ 9] = __g ^= k[8*(i)+ 1]; \
354 k[8*(i)+10] = __g ^= k[8*(i)+ 2]; \
356 k[8*(i)+11] = __g ^= k[8*(i)+ 3]; \
357 __g = ls_box(ss[3],0); \
360 k[8*(i)+12] = __g ^= k[8*(i)+ 4]; \
362 k[8*(i)+13] = __g ^= k[8*(i)+ 5]; \
364 k[8*(i)+14] = __g ^= k[8*(i)+ 6]; \
366 k[8*(i)+15] = __g ^= k[8*(i)+ 7]; \
371 ss[0] ^= ls_box(ss[7],3) ^ rcon_tab[i]; \
372 k[8*(i)+ 8] = ss[0]; \
374 k[8*(i)+ 9] = ss[1]; \
376 k[8*(i)+10] = ss[2]; \
378 k[8*(i)+11] = ss[3]; \
381 static int aes_set_key(struct crypto_tfm
*tfm
, const u8
*in_key
,
382 unsigned int key_len
)
386 struct aes_ctx
*ctx
= crypto_tfm_ctx(tfm
);
387 const __le32
*key
= (const __le32
*)in_key
;
388 u32
*flags
= &tfm
->crt_flags
;
390 /* encryption schedule */
392 ctx
->ekey
[0] = ss
[0] = le32_to_cpu(key
[0]);
393 ctx
->ekey
[1] = ss
[1] = le32_to_cpu(key
[1]);
394 ctx
->ekey
[2] = ss
[2] = le32_to_cpu(key
[2]);
395 ctx
->ekey
[3] = ss
[3] = le32_to_cpu(key
[3]);
399 for (i
= 0; i
< 9; i
++)
406 ctx
->ekey
[4] = ss
[4] = le32_to_cpu(key
[4]);
407 ctx
->ekey
[5] = ss
[5] = le32_to_cpu(key
[5]);
408 for (i
= 0; i
< 7; i
++)
415 ctx
->ekey
[4] = ss
[4] = le32_to_cpu(key
[4]);
416 ctx
->ekey
[5] = ss
[5] = le32_to_cpu(key
[5]);
417 ctx
->ekey
[6] = ss
[6] = le32_to_cpu(key
[6]);
418 ctx
->ekey
[7] = ss
[7] = le32_to_cpu(key
[7]);
419 for (i
= 0; i
< 6; i
++)
426 *flags
|= CRYPTO_TFM_RES_BAD_KEY_LEN
;
430 /* decryption schedule */
432 ctx
->dkey
[0] = ss
[0] = le32_to_cpu(key
[0]);
433 ctx
->dkey
[1] = ss
[1] = le32_to_cpu(key
[1]);
434 ctx
->dkey
[2] = ss
[2] = le32_to_cpu(key
[2]);
435 ctx
->dkey
[3] = ss
[3] = le32_to_cpu(key
[3]);
440 for (i
= 1; i
< 9; i
++)
446 ctx
->dkey
[4] = ff(ss
[4] = le32_to_cpu(key
[4]));
447 ctx
->dkey
[5] = ff(ss
[5] = le32_to_cpu(key
[5]));
449 for (i
= 1; i
< 7; i
++)
455 ctx
->dkey
[4] = ff(ss
[4] = le32_to_cpu(key
[4]));
456 ctx
->dkey
[5] = ff(ss
[5] = le32_to_cpu(key
[5]));
457 ctx
->dkey
[6] = ff(ss
[6] = le32_to_cpu(key
[6]));
458 ctx
->dkey
[7] = ff(ss
[7] = le32_to_cpu(key
[7]));
460 for (i
= 1; i
< 6; i
++)
468 static void aes_encrypt(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
)
470 aes_enc_blk(tfm
, dst
, src
);
473 static void aes_decrypt(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
)
475 aes_dec_blk(tfm
, dst
, src
);
478 static struct crypto_alg aes_alg
= {
480 .cra_driver_name
= "aes-i586",
482 .cra_flags
= CRYPTO_ALG_TYPE_CIPHER
,
483 .cra_blocksize
= AES_BLOCK_SIZE
,
484 .cra_ctxsize
= sizeof(struct aes_ctx
),
485 .cra_module
= THIS_MODULE
,
486 .cra_list
= LIST_HEAD_INIT(aes_alg
.cra_list
),
489 .cia_min_keysize
= AES_MIN_KEY_SIZE
,
490 .cia_max_keysize
= AES_MAX_KEY_SIZE
,
491 .cia_setkey
= aes_set_key
,
492 .cia_encrypt
= aes_encrypt
,
493 .cia_decrypt
= aes_decrypt
498 static int __init
aes_init(void)
501 return crypto_register_alg(&aes_alg
);
504 static void __exit
aes_fini(void)
506 crypto_unregister_alg(&aes_alg
);
509 module_init(aes_init
);
510 module_exit(aes_fini
);
512 MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm, i586 asm optimized");
513 MODULE_LICENSE("Dual BSD/GPL");
514 MODULE_AUTHOR("Fruhwirth Clemens, James Morris, Brian Gladman, Adam Richter");