[CRYPTO] aes: Move common defines into a header file
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / x86 / crypto / aes_32.c
blob9b0ab50394b0c46d35a439e43282a53308eeb6f4
1 /*
2 *
3 * Glue Code for optimized 586 assembler version of AES
5 * Copyright (c) 2002, Dr Brian Gladman <>, Worcester, UK.
6 * All rights reserved.
8 * LICENSE TERMS
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.
27 * DISCLAIMER
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
34 * 2.5 API).
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
53 #define RC_LENGTH 29
55 struct aes_ctx {
56 u32 ekey[AES_KS_LENGTH];
57 u32 rounds;
58 u32 dkey[AES_KS_LENGTH];
61 #define WPOLY 0x011b
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)
81 return x >> 8 * 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];
93 u32 ft_tab[4][256];
94 u32 fl_tab[4][256];
95 static u32 im_tab[4][256];
96 u32 il_tab[4][256];
97 u32 it_tab[4][256];
99 static void gen_tabs(void)
101 u32 i, w;
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.
109 i = 0; w = 1;
111 do {
112 pow[i] = (u8)w;
113 pow[i + 255] = (u8)w;
114 log[w] = (u8)i++;
115 w ^= (w << 1) ^ (w & 0x80 ? WPOLY : 0);
116 } while (w != 1);
118 for(i = 0, w = 1; i < RC_LENGTH; ++i) {
119 rcon_tab[i] = bytes2word(w, 0, 0, 0);
120 w = f2(w);
123 for(i = 0; i < 256; ++i) {
124 u8 b;
126 b = fwd_affine(fi((u8)i));
127 w = bytes2word(f2(b), b, b, f3(b));
129 /* tables for a normal encryption round */
130 ft_tab[0][i] = w;
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)
140 fl_tab[0][i] = w;
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 */
149 im_tab[0][b] = w;
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 */
155 it_tab[0][i] = w;
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 */
163 il_tab[0][i] = w;
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)
178 #define rf1(r,c) (r)
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)
186 #define ke4(k,i) \
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]; \
194 #define kel4(k,i) \
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]; \
201 #define ke6(k,i) \
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]; \
211 #define kel6(k,i) \
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]; \
219 #define ke8(k,i) \
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]; \
231 #define kel8(k,i) \
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]; \
239 #define kdf4(k,i) \
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]; \
244 ss[3] = ss[3]; \
245 ss[4] = ls_box(ss[(i+3) % 4], 3) ^ rcon_tab[i]; \
246 ss[i % 4] ^= ss[4]; \
247 ss[4] ^= k[4*(i)]; \
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]); \
257 #define kd4(k,i) \
259 ss[4] = ls_box(ss[(i+3) % 4], 3) ^ rcon_tab[i]; \
260 ss[i % 4] ^= ss[4]; \
261 ss[4] = ff(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]; \
268 #define kdl4(k,i) \
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]; \
278 #define kdf6(k,i) \
280 ss[0] ^= ls_box(ss[5],3) ^ rcon_tab[i]; \
281 k[6*(i)+ 6] = ff(ss[0]); \
282 ss[1] ^= ss[0]; \
283 k[6*(i)+ 7] = ff(ss[1]); \
284 ss[2] ^= ss[1]; \
285 k[6*(i)+ 8] = ff(ss[2]); \
286 ss[3] ^= ss[2]; \
287 k[6*(i)+ 9] = ff(ss[3]); \
288 ss[4] ^= ss[3]; \
289 k[6*(i)+10] = ff(ss[4]); \
290 ss[5] ^= ss[4]; \
291 k[6*(i)+11] = ff(ss[5]); \
294 #define kd6(k,i) \
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)]; \
299 ss[1] ^= ss[0]; \
300 k[6*(i)+ 7] = ss[6] ^= k[6*(i)+ 1]; \
301 ss[2] ^= ss[1]; \
302 k[6*(i)+ 8] = ss[6] ^= k[6*(i)+ 2]; \
303 ss[3] ^= ss[2]; \
304 k[6*(i)+ 9] = ss[6] ^= k[6*(i)+ 3]; \
305 ss[4] ^= ss[3]; \
306 k[6*(i)+10] = ss[6] ^= k[6*(i)+ 4]; \
307 ss[5] ^= ss[4]; \
308 k[6*(i)+11] = ss[6] ^= k[6*(i)+ 5]; \
311 #define kdl6(k,i) \
313 ss[0] ^= ls_box(ss[5],3) ^ rcon_tab[i]; \
314 k[6*(i)+ 6] = ss[0]; \
315 ss[1] ^= ss[0]; \
316 k[6*(i)+ 7] = ss[1]; \
317 ss[2] ^= ss[1]; \
318 k[6*(i)+ 8] = ss[2]; \
319 ss[3] ^= ss[2]; \
320 k[6*(i)+ 9] = ss[3]; \
323 #define kdf8(k,i) \
325 ss[0] ^= ls_box(ss[7],3) ^ rcon_tab[i]; \
326 k[8*(i)+ 8] = ff(ss[0]); \
327 ss[1] ^= ss[0]; \
328 k[8*(i)+ 9] = ff(ss[1]); \
329 ss[2] ^= ss[1]; \
330 k[8*(i)+10] = ff(ss[2]); \
331 ss[3] ^= 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]); \
335 ss[5] ^= ss[4]; \
336 k[8*(i)+13] = ff(ss[5]); \
337 ss[6] ^= ss[5]; \
338 k[8*(i)+14] = ff(ss[6]); \
339 ss[7] ^= ss[6]; \
340 k[8*(i)+15] = ff(ss[7]); \
343 #define kd8(k,i) \
345 u32 __g = ls_box(ss[7],3) ^ rcon_tab[i]; \
346 ss[0] ^= __g; \
347 __g = ff(__g); \
348 k[8*(i)+ 8] = __g ^= k[8*(i)]; \
349 ss[1] ^= ss[0]; \
350 k[8*(i)+ 9] = __g ^= k[8*(i)+ 1]; \
351 ss[2] ^= ss[1]; \
352 k[8*(i)+10] = __g ^= k[8*(i)+ 2]; \
353 ss[3] ^= ss[2]; \
354 k[8*(i)+11] = __g ^= k[8*(i)+ 3]; \
355 __g = ls_box(ss[3],0); \
356 ss[4] ^= __g; \
357 __g = ff(__g); \
358 k[8*(i)+12] = __g ^= k[8*(i)+ 4]; \
359 ss[5] ^= ss[4]; \
360 k[8*(i)+13] = __g ^= k[8*(i)+ 5]; \
361 ss[6] ^= ss[5]; \
362 k[8*(i)+14] = __g ^= k[8*(i)+ 6]; \
363 ss[7] ^= ss[6]; \
364 k[8*(i)+15] = __g ^= k[8*(i)+ 7]; \
367 #define kdl8(k,i) \
369 ss[0] ^= ls_box(ss[7],3) ^ rcon_tab[i]; \
370 k[8*(i)+ 8] = ss[0]; \
371 ss[1] ^= ss[0]; \
372 k[8*(i)+ 9] = ss[1]; \
373 ss[2] ^= ss[1]; \
374 k[8*(i)+10] = ss[2]; \
375 ss[3] ^= 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)
382 int i;
383 u32 ss[8];
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]);
395 switch(key_len) {
396 case 16:
397 for (i = 0; i < 9; i++)
398 ke4(ctx->ekey, i);
399 kel4(ctx->ekey, 9);
400 ctx->rounds = 10;
401 break;
403 case 24:
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++)
407 ke6(ctx->ekey, i);
408 kel6(ctx->ekey, 7);
409 ctx->rounds = 12;
410 break;
412 case 32:
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++)
418 ke8(ctx->ekey, i);
419 kel8(ctx->ekey, 6);
420 ctx->rounds = 14;
421 break;
423 default:
424 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
425 return -EINVAL;
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]);
435 switch (key_len) {
436 case 16:
437 kdf4(ctx->dkey, 0);
438 for (i = 1; i < 9; i++)
439 kd4(ctx->dkey, i);
440 kdl4(ctx->dkey, 9);
441 break;
443 case 24:
444 ctx->dkey[4] = ff(ss[4] = le32_to_cpu(key[4]));
445 ctx->dkey[5] = ff(ss[5] = le32_to_cpu(key[5]));
446 kdf6(ctx->dkey, 0);
447 for (i = 1; i < 7; i++)
448 kd6(ctx->dkey, i);
449 kdl6(ctx->dkey, 7);
450 break;
452 case 32:
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]));
457 kdf8(ctx->dkey, 0);
458 for (i = 1; i < 6; i++)
459 kd8(ctx->dkey, i);
460 kdl8(ctx->dkey, 6);
461 break;
463 return 0;
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 = {
477 .cra_name = "aes",
478 .cra_driver_name = "aes-i586",
479 .cra_priority = 200,
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),
485 .cra_u = {
486 .cipher = {
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)
498 gen_tabs();
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");
513 MODULE_ALIAS("aes");