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 <crypto/aes.h>
42 #include <linux/kernel.h>
43 #include <linux/module.h>
44 #include <linux/init.h>
45 #include <linux/types.h>
46 #include <linux/crypto.h>
47 #include <linux/linkage.h>
49 asmlinkage
void aes_enc_blk(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
50 asmlinkage
void aes_dec_blk(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
52 #define AES_KS_LENGTH 4 * AES_BLOCK_SIZE
56 u32 ekey
[AES_KS_LENGTH
];
58 u32 dkey
[AES_KS_LENGTH
];
62 #define bytes2word(b0, b1, b2, b3) \
63 (((u32)(b3) << 24) | ((u32)(b2) << 16) | ((u32)(b1) << 8) | (b0))
65 /* define the finite field multiplies required for Rijndael */
66 #define f2(x) ((x) ? pow[log[x] + 0x19] : 0)
67 #define f3(x) ((x) ? pow[log[x] + 0x01] : 0)
68 #define f9(x) ((x) ? pow[log[x] + 0xc7] : 0)
69 #define fb(x) ((x) ? pow[log[x] + 0x68] : 0)
70 #define fd(x) ((x) ? pow[log[x] + 0xee] : 0)
71 #define fe(x) ((x) ? pow[log[x] + 0xdf] : 0)
72 #define fi(x) ((x) ? pow[255 - log[x]]: 0)
74 static inline u32
upr(u32 x
, int n
)
76 return (x
<< 8 * n
) | (x
>> (32 - 8 * n
));
79 static inline u8
bval(u32 x
, int n
)
84 /* The forward and inverse affine transformations used in the S-box */
85 #define fwd_affine(x) \
86 (w = (u32)x, w ^= (w<<1)^(w<<2)^(w<<3)^(w<<4), 0x63^(u8)(w^(w>>8)))
88 #define inv_affine(x) \
89 (w = (u32)x, w = (w<<1)^(w<<3)^(w<<6), 0x05^(u8)(w^(w>>8)))
91 static u32 rcon_tab
[RC_LENGTH
];
95 static u32 im_tab
[4][256];
99 static void gen_tabs(void)
102 u8 pow
[512], log
[256];
105 * log and power tables for GF(2^8) finite field with
106 * WPOLY as modular polynomial - the simplest primitive
107 * root is 0x03, used here to generate the tables.
113 pow
[i
+ 255] = (u8
)w
;
115 w
^= (w
<< 1) ^ (w
& 0x80 ? WPOLY
: 0);
118 for(i
= 0, w
= 1; i
< RC_LENGTH
; ++i
) {
119 rcon_tab
[i
] = bytes2word(w
, 0, 0, 0);
123 for(i
= 0; i
< 256; ++i
) {
126 b
= fwd_affine(fi((u8
)i
));
127 w
= bytes2word(f2(b
), b
, b
, f3(b
));
129 /* tables for a normal encryption round */
131 ft_tab
[1][i
] = upr(w
, 1);
132 ft_tab
[2][i
] = upr(w
, 2);
133 ft_tab
[3][i
] = upr(w
, 3);
134 w
= bytes2word(b
, 0, 0, 0);
137 * tables for last encryption round
138 * (may also be used in the key schedule)
141 fl_tab
[1][i
] = upr(w
, 1);
142 fl_tab
[2][i
] = upr(w
, 2);
143 fl_tab
[3][i
] = upr(w
, 3);
145 b
= fi(inv_affine((u8
)i
));
146 w
= bytes2word(fe(b
), f9(b
), fd(b
), fb(b
));
148 /* tables for the inverse mix column operation */
150 im_tab
[1][b
] = upr(w
, 1);
151 im_tab
[2][b
] = upr(w
, 2);
152 im_tab
[3][b
] = upr(w
, 3);
154 /* tables for a normal decryption round */
156 it_tab
[1][i
] = upr(w
,1);
157 it_tab
[2][i
] = upr(w
,2);
158 it_tab
[3][i
] = upr(w
,3);
160 w
= bytes2word(b
, 0, 0, 0);
162 /* tables for last decryption round */
164 il_tab
[1][i
] = upr(w
,1);
165 il_tab
[2][i
] = upr(w
,2);
166 il_tab
[3][i
] = upr(w
,3);
170 #define four_tables(x,tab,vf,rf,c) \
171 ( tab[0][bval(vf(x,0,c),rf(0,c))] ^ \
172 tab[1][bval(vf(x,1,c),rf(1,c))] ^ \
173 tab[2][bval(vf(x,2,c),rf(2,c))] ^ \
174 tab[3][bval(vf(x,3,c),rf(3,c))] \
177 #define vf1(x,r,c) (x)
179 #define rf2(r,c) ((r-c)&3)
181 #define inv_mcol(x) four_tables(x,im_tab,vf1,rf1,0)
182 #define ls_box(x,c) four_tables(x,fl_tab,vf1,rf2,c)
184 #define ff(x) inv_mcol(x)
188 k[4*(i)+4] = ss[0] ^= ls_box(ss[3],3) ^ rcon_tab[i]; \
189 k[4*(i)+5] = ss[1] ^= ss[0]; \
190 k[4*(i)+6] = ss[2] ^= ss[1]; \
191 k[4*(i)+7] = ss[3] ^= ss[2]; \
196 k[4*(i)+4] = ss[0] ^= ls_box(ss[3],3) ^ rcon_tab[i]; \
197 k[4*(i)+5] = ss[1] ^= ss[0]; \
198 k[4*(i)+6] = ss[2] ^= ss[1]; k[4*(i)+7] = ss[3] ^= ss[2]; \
203 k[6*(i)+ 6] = ss[0] ^= ls_box(ss[5],3) ^ rcon_tab[i]; \
204 k[6*(i)+ 7] = ss[1] ^= ss[0]; \
205 k[6*(i)+ 8] = ss[2] ^= ss[1]; \
206 k[6*(i)+ 9] = ss[3] ^= ss[2]; \
207 k[6*(i)+10] = ss[4] ^= ss[3]; \
208 k[6*(i)+11] = ss[5] ^= ss[4]; \
213 k[6*(i)+ 6] = ss[0] ^= ls_box(ss[5],3) ^ rcon_tab[i]; \
214 k[6*(i)+ 7] = ss[1] ^= ss[0]; \
215 k[6*(i)+ 8] = ss[2] ^= ss[1]; \
216 k[6*(i)+ 9] = ss[3] ^= ss[2]; \
221 k[8*(i)+ 8] = ss[0] ^= ls_box(ss[7],3) ^ rcon_tab[i]; \
222 k[8*(i)+ 9] = ss[1] ^= ss[0]; \
223 k[8*(i)+10] = ss[2] ^= ss[1]; \
224 k[8*(i)+11] = ss[3] ^= ss[2]; \
225 k[8*(i)+12] = ss[4] ^= ls_box(ss[3],0); \
226 k[8*(i)+13] = ss[5] ^= ss[4]; \
227 k[8*(i)+14] = ss[6] ^= ss[5]; \
228 k[8*(i)+15] = ss[7] ^= ss[6]; \
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]; \
241 ss[0] = ss[0] ^ ss[2] ^ ss[1] ^ ss[3]; \
242 ss[1] = ss[1] ^ ss[3]; \
243 ss[2] = ss[2] ^ ss[3]; \
245 ss[4] = ls_box(ss[(i+3) % 4], 3) ^ rcon_tab[i]; \
246 ss[i % 4] ^= ss[4]; \
248 k[4*(i)+4] = ff(ss[4]); \
249 ss[4] ^= k[4*(i)+1]; \
250 k[4*(i)+5] = ff(ss[4]); \
251 ss[4] ^= k[4*(i)+2]; \
252 k[4*(i)+6] = ff(ss[4]); \
253 ss[4] ^= k[4*(i)+3]; \
254 k[4*(i)+7] = ff(ss[4]); \
259 ss[4] = ls_box(ss[(i+3) % 4], 3) ^ rcon_tab[i]; \
260 ss[i % 4] ^= ss[4]; \
262 k[4*(i)+4] = ss[4] ^= k[4*(i)]; \
263 k[4*(i)+5] = ss[4] ^= k[4*(i)+1]; \
264 k[4*(i)+6] = ss[4] ^= k[4*(i)+2]; \
265 k[4*(i)+7] = ss[4] ^= k[4*(i)+3]; \
270 ss[4] = ls_box(ss[(i+3) % 4], 3) ^ rcon_tab[i]; \
271 ss[i % 4] ^= ss[4]; \
272 k[4*(i)+4] = (ss[0] ^= ss[1]) ^ ss[2] ^ ss[3]; \
273 k[4*(i)+5] = ss[1] ^ ss[3]; \
274 k[4*(i)+6] = ss[0]; \
275 k[4*(i)+7] = ss[1]; \
280 ss[0] ^= ls_box(ss[5],3) ^ rcon_tab[i]; \
281 k[6*(i)+ 6] = ff(ss[0]); \
283 k[6*(i)+ 7] = ff(ss[1]); \
285 k[6*(i)+ 8] = ff(ss[2]); \
287 k[6*(i)+ 9] = ff(ss[3]); \
289 k[6*(i)+10] = ff(ss[4]); \
291 k[6*(i)+11] = ff(ss[5]); \
296 ss[6] = ls_box(ss[5],3) ^ rcon_tab[i]; \
297 ss[0] ^= ss[6]; ss[6] = ff(ss[6]); \
298 k[6*(i)+ 6] = ss[6] ^= k[6*(i)]; \
300 k[6*(i)+ 7] = ss[6] ^= k[6*(i)+ 1]; \
302 k[6*(i)+ 8] = ss[6] ^= k[6*(i)+ 2]; \
304 k[6*(i)+ 9] = ss[6] ^= k[6*(i)+ 3]; \
306 k[6*(i)+10] = ss[6] ^= k[6*(i)+ 4]; \
308 k[6*(i)+11] = ss[6] ^= k[6*(i)+ 5]; \
313 ss[0] ^= ls_box(ss[5],3) ^ rcon_tab[i]; \
314 k[6*(i)+ 6] = ss[0]; \
316 k[6*(i)+ 7] = ss[1]; \
318 k[6*(i)+ 8] = ss[2]; \
320 k[6*(i)+ 9] = ss[3]; \
325 ss[0] ^= ls_box(ss[7],3) ^ rcon_tab[i]; \
326 k[8*(i)+ 8] = ff(ss[0]); \
328 k[8*(i)+ 9] = ff(ss[1]); \
330 k[8*(i)+10] = ff(ss[2]); \
332 k[8*(i)+11] = ff(ss[3]); \
333 ss[4] ^= ls_box(ss[3],0); \
334 k[8*(i)+12] = ff(ss[4]); \
336 k[8*(i)+13] = ff(ss[5]); \
338 k[8*(i)+14] = ff(ss[6]); \
340 k[8*(i)+15] = ff(ss[7]); \
345 u32 __g = ls_box(ss[7],3) ^ rcon_tab[i]; \
348 k[8*(i)+ 8] = __g ^= k[8*(i)]; \
350 k[8*(i)+ 9] = __g ^= k[8*(i)+ 1]; \
352 k[8*(i)+10] = __g ^= k[8*(i)+ 2]; \
354 k[8*(i)+11] = __g ^= k[8*(i)+ 3]; \
355 __g = ls_box(ss[3],0); \
358 k[8*(i)+12] = __g ^= k[8*(i)+ 4]; \
360 k[8*(i)+13] = __g ^= k[8*(i)+ 5]; \
362 k[8*(i)+14] = __g ^= k[8*(i)+ 6]; \
364 k[8*(i)+15] = __g ^= k[8*(i)+ 7]; \
369 ss[0] ^= ls_box(ss[7],3) ^ rcon_tab[i]; \
370 k[8*(i)+ 8] = ss[0]; \
372 k[8*(i)+ 9] = ss[1]; \
374 k[8*(i)+10] = ss[2]; \
376 k[8*(i)+11] = ss[3]; \
379 static int aes_set_key(struct crypto_tfm
*tfm
, const u8
*in_key
,
380 unsigned int key_len
)
384 struct aes_ctx
*ctx
= crypto_tfm_ctx(tfm
);
385 const __le32
*key
= (const __le32
*)in_key
;
386 u32
*flags
= &tfm
->crt_flags
;
388 /* encryption schedule */
390 ctx
->ekey
[0] = ss
[0] = le32_to_cpu(key
[0]);
391 ctx
->ekey
[1] = ss
[1] = le32_to_cpu(key
[1]);
392 ctx
->ekey
[2] = ss
[2] = le32_to_cpu(key
[2]);
393 ctx
->ekey
[3] = ss
[3] = le32_to_cpu(key
[3]);
397 for (i
= 0; i
< 9; i
++)
404 ctx
->ekey
[4] = ss
[4] = le32_to_cpu(key
[4]);
405 ctx
->ekey
[5] = ss
[5] = le32_to_cpu(key
[5]);
406 for (i
= 0; i
< 7; i
++)
413 ctx
->ekey
[4] = ss
[4] = le32_to_cpu(key
[4]);
414 ctx
->ekey
[5] = ss
[5] = le32_to_cpu(key
[5]);
415 ctx
->ekey
[6] = ss
[6] = le32_to_cpu(key
[6]);
416 ctx
->ekey
[7] = ss
[7] = le32_to_cpu(key
[7]);
417 for (i
= 0; i
< 6; i
++)
424 *flags
|= CRYPTO_TFM_RES_BAD_KEY_LEN
;
428 /* decryption schedule */
430 ctx
->dkey
[0] = ss
[0] = le32_to_cpu(key
[0]);
431 ctx
->dkey
[1] = ss
[1] = le32_to_cpu(key
[1]);
432 ctx
->dkey
[2] = ss
[2] = le32_to_cpu(key
[2]);
433 ctx
->dkey
[3] = ss
[3] = le32_to_cpu(key
[3]);
438 for (i
= 1; i
< 9; i
++)
444 ctx
->dkey
[4] = ff(ss
[4] = le32_to_cpu(key
[4]));
445 ctx
->dkey
[5] = ff(ss
[5] = le32_to_cpu(key
[5]));
447 for (i
= 1; i
< 7; i
++)
453 ctx
->dkey
[4] = ff(ss
[4] = le32_to_cpu(key
[4]));
454 ctx
->dkey
[5] = ff(ss
[5] = le32_to_cpu(key
[5]));
455 ctx
->dkey
[6] = ff(ss
[6] = le32_to_cpu(key
[6]));
456 ctx
->dkey
[7] = ff(ss
[7] = le32_to_cpu(key
[7]));
458 for (i
= 1; i
< 6; i
++)
466 static void aes_encrypt(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
)
468 aes_enc_blk(tfm
, dst
, src
);
471 static void aes_decrypt(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
)
473 aes_dec_blk(tfm
, dst
, src
);
476 static struct crypto_alg aes_alg
= {
478 .cra_driver_name
= "aes-i586",
480 .cra_flags
= CRYPTO_ALG_TYPE_CIPHER
,
481 .cra_blocksize
= AES_BLOCK_SIZE
,
482 .cra_ctxsize
= sizeof(struct aes_ctx
),
483 .cra_module
= THIS_MODULE
,
484 .cra_list
= LIST_HEAD_INIT(aes_alg
.cra_list
),
487 .cia_min_keysize
= AES_MIN_KEY_SIZE
,
488 .cia_max_keysize
= AES_MAX_KEY_SIZE
,
489 .cia_setkey
= aes_set_key
,
490 .cia_encrypt
= aes_encrypt
,
491 .cia_decrypt
= aes_decrypt
496 static int __init
aes_init(void)
499 return crypto_register_alg(&aes_alg
);
502 static void __exit
aes_fini(void)
504 crypto_unregister_alg(&aes_alg
);
507 module_init(aes_init
);
508 module_exit(aes_fini
);
510 MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm, i586 asm optimized");
511 MODULE_LICENSE("Dual BSD/GPL");
512 MODULE_AUTHOR("Fruhwirth Clemens, James Morris, Brian Gladman, Adam Richter");