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(const u8
*src
, u8
*dst
, void *ctx
);
49 asmlinkage
void aes_dec_blk(const u8
*src
, u8
*dst
, void *ctx
);
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 ls_tab
[4][256];
98 static u32 im_tab
[4][256];
102 static void gen_tabs(void)
105 u8 pow
[512], log
[256];
108 * log and power tables for GF(2^8) finite field with
109 * WPOLY as modular polynomial - the simplest primitive
110 * root is 0x03, used here to generate the tables.
116 pow
[i
+ 255] = (u8
)w
;
118 w
^= (w
<< 1) ^ (w
& 0x80 ? WPOLY
: 0);
121 for(i
= 0, w
= 1; i
< RC_LENGTH
; ++i
) {
122 rcon_tab
[i
] = bytes2word(w
, 0, 0, 0);
126 for(i
= 0; i
< 256; ++i
) {
129 b
= fwd_affine(fi((u8
)i
));
130 w
= bytes2word(f2(b
), b
, b
, f3(b
));
132 /* tables for a normal encryption round */
134 ft_tab
[1][i
] = upr(w
, 1);
135 ft_tab
[2][i
] = upr(w
, 2);
136 ft_tab
[3][i
] = upr(w
, 3);
137 w
= bytes2word(b
, 0, 0, 0);
140 * tables for last encryption round
141 * (may also be used in the key schedule)
144 fl_tab
[1][i
] = upr(w
, 1);
145 fl_tab
[2][i
] = upr(w
, 2);
146 fl_tab
[3][i
] = upr(w
, 3);
149 * table for key schedule if fl_tab above is
150 * not of the required form
153 ls_tab
[1][i
] = upr(w
, 1);
154 ls_tab
[2][i
] = upr(w
, 2);
155 ls_tab
[3][i
] = upr(w
, 3);
157 b
= fi(inv_affine((u8
)i
));
158 w
= bytes2word(fe(b
), f9(b
), fd(b
), fb(b
));
160 /* tables for the inverse mix column operation */
162 im_tab
[1][b
] = upr(w
, 1);
163 im_tab
[2][b
] = upr(w
, 2);
164 im_tab
[3][b
] = upr(w
, 3);
166 /* tables for a normal decryption round */
168 it_tab
[1][i
] = upr(w
,1);
169 it_tab
[2][i
] = upr(w
,2);
170 it_tab
[3][i
] = upr(w
,3);
172 w
= bytes2word(b
, 0, 0, 0);
174 /* tables for last decryption round */
176 il_tab
[1][i
] = upr(w
,1);
177 il_tab
[2][i
] = upr(w
,2);
178 il_tab
[3][i
] = upr(w
,3);
182 #define four_tables(x,tab,vf,rf,c) \
183 ( tab[0][bval(vf(x,0,c),rf(0,c))] ^ \
184 tab[1][bval(vf(x,1,c),rf(1,c))] ^ \
185 tab[2][bval(vf(x,2,c),rf(2,c))] ^ \
186 tab[3][bval(vf(x,3,c),rf(3,c))] \
189 #define vf1(x,r,c) (x)
191 #define rf2(r,c) ((r-c)&3)
193 #define inv_mcol(x) four_tables(x,im_tab,vf1,rf1,0)
194 #define ls_box(x,c) four_tables(x,fl_tab,vf1,rf2,c)
196 #define ff(x) inv_mcol(x)
200 k[4*(i)+4] = ss[0] ^= ls_box(ss[3],3) ^ rcon_tab[i]; \
201 k[4*(i)+5] = ss[1] ^= ss[0]; \
202 k[4*(i)+6] = ss[2] ^= ss[1]; \
203 k[4*(i)+7] = ss[3] ^= ss[2]; \
208 k[4*(i)+4] = ss[0] ^= ls_box(ss[3],3) ^ rcon_tab[i]; \
209 k[4*(i)+5] = ss[1] ^= ss[0]; \
210 k[4*(i)+6] = ss[2] ^= ss[1]; k[4*(i)+7] = ss[3] ^= ss[2]; \
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]; \
219 k[6*(i)+10] = ss[4] ^= ss[3]; \
220 k[6*(i)+11] = ss[5] ^= ss[4]; \
225 k[6*(i)+ 6] = ss[0] ^= ls_box(ss[5],3) ^ rcon_tab[i]; \
226 k[6*(i)+ 7] = ss[1] ^= ss[0]; \
227 k[6*(i)+ 8] = ss[2] ^= ss[1]; \
228 k[6*(i)+ 9] = ss[3] ^= ss[2]; \
233 k[8*(i)+ 8] = ss[0] ^= ls_box(ss[7],3) ^ rcon_tab[i]; \
234 k[8*(i)+ 9] = ss[1] ^= ss[0]; \
235 k[8*(i)+10] = ss[2] ^= ss[1]; \
236 k[8*(i)+11] = ss[3] ^= ss[2]; \
237 k[8*(i)+12] = ss[4] ^= ls_box(ss[3],0); \
238 k[8*(i)+13] = ss[5] ^= ss[4]; \
239 k[8*(i)+14] = ss[6] ^= ss[5]; \
240 k[8*(i)+15] = ss[7] ^= ss[6]; \
245 k[8*(i)+ 8] = ss[0] ^= ls_box(ss[7],3) ^ rcon_tab[i]; \
246 k[8*(i)+ 9] = ss[1] ^= ss[0]; \
247 k[8*(i)+10] = ss[2] ^= ss[1]; \
248 k[8*(i)+11] = ss[3] ^= ss[2]; \
253 ss[0] = ss[0] ^ ss[2] ^ ss[1] ^ ss[3]; \
254 ss[1] = ss[1] ^ ss[3]; \
255 ss[2] = ss[2] ^ ss[3]; \
257 ss[4] = ls_box(ss[(i+3) % 4], 3) ^ rcon_tab[i]; \
258 ss[i % 4] ^= ss[4]; \
260 k[4*(i)+4] = ff(ss[4]); \
261 ss[4] ^= k[4*(i)+1]; \
262 k[4*(i)+5] = ff(ss[4]); \
263 ss[4] ^= k[4*(i)+2]; \
264 k[4*(i)+6] = ff(ss[4]); \
265 ss[4] ^= k[4*(i)+3]; \
266 k[4*(i)+7] = ff(ss[4]); \
271 ss[4] = ls_box(ss[(i+3) % 4], 3) ^ rcon_tab[i]; \
272 ss[i % 4] ^= ss[4]; \
274 k[4*(i)+4] = ss[4] ^= k[4*(i)]; \
275 k[4*(i)+5] = ss[4] ^= k[4*(i)+1]; \
276 k[4*(i)+6] = ss[4] ^= k[4*(i)+2]; \
277 k[4*(i)+7] = ss[4] ^= k[4*(i)+3]; \
282 ss[4] = ls_box(ss[(i+3) % 4], 3) ^ rcon_tab[i]; \
283 ss[i % 4] ^= ss[4]; \
284 k[4*(i)+4] = (ss[0] ^= ss[1]) ^ ss[2] ^ ss[3]; \
285 k[4*(i)+5] = ss[1] ^ ss[3]; \
286 k[4*(i)+6] = ss[0]; \
287 k[4*(i)+7] = ss[1]; \
292 ss[0] ^= ls_box(ss[5],3) ^ rcon_tab[i]; \
293 k[6*(i)+ 6] = ff(ss[0]); \
295 k[6*(i)+ 7] = ff(ss[1]); \
297 k[6*(i)+ 8] = ff(ss[2]); \
299 k[6*(i)+ 9] = ff(ss[3]); \
301 k[6*(i)+10] = ff(ss[4]); \
303 k[6*(i)+11] = ff(ss[5]); \
308 ss[6] = ls_box(ss[5],3) ^ rcon_tab[i]; \
309 ss[0] ^= ss[6]; ss[6] = ff(ss[6]); \
310 k[6*(i)+ 6] = ss[6] ^= k[6*(i)]; \
312 k[6*(i)+ 7] = ss[6] ^= k[6*(i)+ 1]; \
314 k[6*(i)+ 8] = ss[6] ^= k[6*(i)+ 2]; \
316 k[6*(i)+ 9] = ss[6] ^= k[6*(i)+ 3]; \
318 k[6*(i)+10] = ss[6] ^= k[6*(i)+ 4]; \
320 k[6*(i)+11] = ss[6] ^= k[6*(i)+ 5]; \
325 ss[0] ^= ls_box(ss[5],3) ^ rcon_tab[i]; \
326 k[6*(i)+ 6] = ss[0]; \
328 k[6*(i)+ 7] = ss[1]; \
330 k[6*(i)+ 8] = ss[2]; \
332 k[6*(i)+ 9] = ss[3]; \
337 ss[0] ^= ls_box(ss[7],3) ^ rcon_tab[i]; \
338 k[8*(i)+ 8] = ff(ss[0]); \
340 k[8*(i)+ 9] = ff(ss[1]); \
342 k[8*(i)+10] = ff(ss[2]); \
344 k[8*(i)+11] = ff(ss[3]); \
345 ss[4] ^= ls_box(ss[3],0); \
346 k[8*(i)+12] = ff(ss[4]); \
348 k[8*(i)+13] = ff(ss[5]); \
350 k[8*(i)+14] = ff(ss[6]); \
352 k[8*(i)+15] = ff(ss[7]); \
357 u32 __g = ls_box(ss[7],3) ^ rcon_tab[i]; \
360 k[8*(i)+ 8] = __g ^= k[8*(i)]; \
362 k[8*(i)+ 9] = __g ^= k[8*(i)+ 1]; \
364 k[8*(i)+10] = __g ^= k[8*(i)+ 2]; \
366 k[8*(i)+11] = __g ^= k[8*(i)+ 3]; \
367 __g = ls_box(ss[3],0); \
370 k[8*(i)+12] = __g ^= k[8*(i)+ 4]; \
372 k[8*(i)+13] = __g ^= k[8*(i)+ 5]; \
374 k[8*(i)+14] = __g ^= k[8*(i)+ 6]; \
376 k[8*(i)+15] = __g ^= k[8*(i)+ 7]; \
381 ss[0] ^= ls_box(ss[7],3) ^ rcon_tab[i]; \
382 k[8*(i)+ 8] = ss[0]; \
384 k[8*(i)+ 9] = ss[1]; \
386 k[8*(i)+10] = ss[2]; \
388 k[8*(i)+11] = ss[3]; \
392 aes_set_key(void *ctx_arg
, const u8
*in_key
, unsigned int key_len
, u32
*flags
)
396 struct aes_ctx
*ctx
= ctx_arg
;
397 const __le32
*key
= (const __le32
*)in_key
;
399 /* encryption schedule */
401 ctx
->ekey
[0] = ss
[0] = le32_to_cpu(key
[0]);
402 ctx
->ekey
[1] = ss
[1] = le32_to_cpu(key
[1]);
403 ctx
->ekey
[2] = ss
[2] = le32_to_cpu(key
[2]);
404 ctx
->ekey
[3] = ss
[3] = le32_to_cpu(key
[3]);
408 for (i
= 0; i
< 9; i
++)
415 ctx
->ekey
[4] = ss
[4] = le32_to_cpu(key
[4]);
416 ctx
->ekey
[5] = ss
[5] = le32_to_cpu(key
[5]);
417 for (i
= 0; i
< 7; i
++)
424 ctx
->ekey
[4] = ss
[4] = le32_to_cpu(key
[4]);
425 ctx
->ekey
[5] = ss
[5] = le32_to_cpu(key
[5]);
426 ctx
->ekey
[6] = ss
[6] = le32_to_cpu(key
[6]);
427 ctx
->ekey
[7] = ss
[7] = le32_to_cpu(key
[7]);
428 for (i
= 0; i
< 6; i
++)
435 *flags
|= CRYPTO_TFM_RES_BAD_KEY_LEN
;
439 /* decryption schedule */
441 ctx
->dkey
[0] = ss
[0] = le32_to_cpu(key
[0]);
442 ctx
->dkey
[1] = ss
[1] = le32_to_cpu(key
[1]);
443 ctx
->dkey
[2] = ss
[2] = le32_to_cpu(key
[2]);
444 ctx
->dkey
[3] = ss
[3] = le32_to_cpu(key
[3]);
449 for (i
= 1; i
< 9; 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]));
458 for (i
= 1; i
< 7; i
++)
464 ctx
->dkey
[4] = ff(ss
[4] = le32_to_cpu(key
[4]));
465 ctx
->dkey
[5] = ff(ss
[5] = le32_to_cpu(key
[5]));
466 ctx
->dkey
[6] = ff(ss
[6] = le32_to_cpu(key
[6]));
467 ctx
->dkey
[7] = ff(ss
[7] = le32_to_cpu(key
[7]));
469 for (i
= 1; i
< 6; i
++)
477 static inline void aes_encrypt(void *ctx
, u8
*dst
, const u8
*src
)
479 aes_enc_blk(src
, dst
, ctx
);
481 static inline void aes_decrypt(void *ctx
, u8
*dst
, const u8
*src
)
483 aes_dec_blk(src
, dst
, ctx
);
487 static struct crypto_alg aes_alg
= {
489 .cra_flags
= CRYPTO_ALG_TYPE_CIPHER
,
490 .cra_blocksize
= AES_BLOCK_SIZE
,
491 .cra_ctxsize
= sizeof(struct aes_ctx
),
492 .cra_module
= THIS_MODULE
,
493 .cra_list
= LIST_HEAD_INIT(aes_alg
.cra_list
),
496 .cia_min_keysize
= AES_MIN_KEY_SIZE
,
497 .cia_max_keysize
= AES_MAX_KEY_SIZE
,
498 .cia_setkey
= aes_set_key
,
499 .cia_encrypt
= aes_encrypt
,
500 .cia_decrypt
= aes_decrypt
505 static int __init
aes_init(void)
508 return crypto_register_alg(&aes_alg
);
511 static void __exit
aes_fini(void)
513 crypto_unregister_alg(&aes_alg
);
516 module_init(aes_init
);
517 module_exit(aes_fini
);
519 MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm, i586 asm optimized");
520 MODULE_LICENSE("Dual BSD/GPL");
521 MODULE_AUTHOR("Fruhwirth Clemens, James Morris, Brian Gladman, Adam Richter");