ia64/linux-2.6.18-xen.hg

view crypto/aes.c @ 854:950b9eb27661

usbback: fix urb interval value for interrupt urbs.

Signed-off-by: Noboru Iwamatsu <n_iwamatsu@jp.fujitsu.com>
author Keir Fraser <keir.fraser@citrix.com>
date Mon Apr 06 13:51:20 2009 +0100 (2009-04-06)
parents 831230e53067
children
line source
1 /*
2 * Cryptographic API.
3 *
4 * AES Cipher Algorithm.
5 *
6 * Based on Brian Gladman's code.
7 *
8 * Linux developers:
9 * Alexander Kjeldaas <astor@fast.no>
10 * Herbert Valerio Riedel <hvr@hvrlab.org>
11 * Kyle McMartin <kyle@debian.org>
12 * Adam J. Richter <adam@yggdrasil.com> (conversion to 2.5 API).
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * ---------------------------------------------------------------------------
20 * Copyright (c) 2002, Dr Brian Gladman <brg@gladman.me.uk>, Worcester, UK.
21 * All rights reserved.
22 *
23 * LICENSE TERMS
24 *
25 * The free distribution and use of this software in both source and binary
26 * form is allowed (with or without changes) provided that:
27 *
28 * 1. distributions of this source code include the above copyright
29 * notice, this list of conditions and the following disclaimer;
30 *
31 * 2. distributions in binary form include the above copyright
32 * notice, this list of conditions and the following disclaimer
33 * in the documentation and/or other associated materials;
34 *
35 * 3. the copyright holder's name is not used to endorse products
36 * built using this software without specific written permission.
37 *
38 * ALTERNATIVELY, provided that this notice is retained in full, this product
39 * may be distributed under the terms of the GNU General Public License (GPL),
40 * in which case the provisions of the GPL apply INSTEAD OF those given above.
41 *
42 * DISCLAIMER
43 *
44 * This software is provided 'as is' with no explicit or implied warranties
45 * in respect of its properties, including, but not limited to, correctness
46 * and/or fitness for purpose.
47 * ---------------------------------------------------------------------------
48 */
50 /* Some changes from the Gladman version:
51 s/RIJNDAEL(e_key)/E_KEY/g
52 s/RIJNDAEL(d_key)/D_KEY/g
53 */
55 #include <linux/module.h>
56 #include <linux/init.h>
57 #include <linux/types.h>
58 #include <linux/errno.h>
59 #include <linux/crypto.h>
60 #include <asm/byteorder.h>
62 #define AES_MIN_KEY_SIZE 16
63 #define AES_MAX_KEY_SIZE 32
65 #define AES_BLOCK_SIZE 16
67 /*
68 * #define byte(x, nr) ((unsigned char)((x) >> (nr*8)))
69 */
70 static inline u8
71 byte(const u32 x, const unsigned n)
72 {
73 return x >> (n << 3);
74 }
76 struct aes_ctx {
77 int key_length;
78 u32 buf[120];
79 };
81 #define E_KEY (&ctx->buf[0])
82 #define D_KEY (&ctx->buf[60])
84 static u8 pow_tab[256] __initdata;
85 static u8 log_tab[256] __initdata;
86 static u8 sbx_tab[256] __initdata;
87 static u8 isb_tab[256] __initdata;
88 static u32 rco_tab[10];
89 static u32 ft_tab[4][256];
90 static u32 it_tab[4][256];
92 static u32 fl_tab[4][256];
93 static u32 il_tab[4][256];
95 static inline u8 __init
96 f_mult (u8 a, u8 b)
97 {
98 u8 aa = log_tab[a], cc = aa + log_tab[b];
100 return pow_tab[cc + (cc < aa ? 1 : 0)];
101 }
103 #define ff_mult(a,b) (a && b ? f_mult(a, b) : 0)
105 #define f_rn(bo, bi, n, k) \
106 bo[n] = ft_tab[0][byte(bi[n],0)] ^ \
107 ft_tab[1][byte(bi[(n + 1) & 3],1)] ^ \
108 ft_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
109 ft_tab[3][byte(bi[(n + 3) & 3],3)] ^ *(k + n)
111 #define i_rn(bo, bi, n, k) \
112 bo[n] = it_tab[0][byte(bi[n],0)] ^ \
113 it_tab[1][byte(bi[(n + 3) & 3],1)] ^ \
114 it_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
115 it_tab[3][byte(bi[(n + 1) & 3],3)] ^ *(k + n)
117 #define ls_box(x) \
118 ( fl_tab[0][byte(x, 0)] ^ \
119 fl_tab[1][byte(x, 1)] ^ \
120 fl_tab[2][byte(x, 2)] ^ \
121 fl_tab[3][byte(x, 3)] )
123 #define f_rl(bo, bi, n, k) \
124 bo[n] = fl_tab[0][byte(bi[n],0)] ^ \
125 fl_tab[1][byte(bi[(n + 1) & 3],1)] ^ \
126 fl_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
127 fl_tab[3][byte(bi[(n + 3) & 3],3)] ^ *(k + n)
129 #define i_rl(bo, bi, n, k) \
130 bo[n] = il_tab[0][byte(bi[n],0)] ^ \
131 il_tab[1][byte(bi[(n + 3) & 3],1)] ^ \
132 il_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
133 il_tab[3][byte(bi[(n + 1) & 3],3)] ^ *(k + n)
135 static void __init
136 gen_tabs (void)
137 {
138 u32 i, t;
139 u8 p, q;
141 /* log and power tables for GF(2**8) finite field with
142 0x011b as modular polynomial - the simplest primitive
143 root is 0x03, used here to generate the tables */
145 for (i = 0, p = 1; i < 256; ++i) {
146 pow_tab[i] = (u8) p;
147 log_tab[p] = (u8) i;
149 p ^= (p << 1) ^ (p & 0x80 ? 0x01b : 0);
150 }
152 log_tab[1] = 0;
154 for (i = 0, p = 1; i < 10; ++i) {
155 rco_tab[i] = p;
157 p = (p << 1) ^ (p & 0x80 ? 0x01b : 0);
158 }
160 for (i = 0; i < 256; ++i) {
161 p = (i ? pow_tab[255 - log_tab[i]] : 0);
162 q = ((p >> 7) | (p << 1)) ^ ((p >> 6) | (p << 2));
163 p ^= 0x63 ^ q ^ ((q >> 6) | (q << 2));
164 sbx_tab[i] = p;
165 isb_tab[p] = (u8) i;
166 }
168 for (i = 0; i < 256; ++i) {
169 p = sbx_tab[i];
171 t = p;
172 fl_tab[0][i] = t;
173 fl_tab[1][i] = rol32(t, 8);
174 fl_tab[2][i] = rol32(t, 16);
175 fl_tab[3][i] = rol32(t, 24);
177 t = ((u32) ff_mult (2, p)) |
178 ((u32) p << 8) |
179 ((u32) p << 16) | ((u32) ff_mult (3, p) << 24);
181 ft_tab[0][i] = t;
182 ft_tab[1][i] = rol32(t, 8);
183 ft_tab[2][i] = rol32(t, 16);
184 ft_tab[3][i] = rol32(t, 24);
186 p = isb_tab[i];
188 t = p;
189 il_tab[0][i] = t;
190 il_tab[1][i] = rol32(t, 8);
191 il_tab[2][i] = rol32(t, 16);
192 il_tab[3][i] = rol32(t, 24);
194 t = ((u32) ff_mult (14, p)) |
195 ((u32) ff_mult (9, p) << 8) |
196 ((u32) ff_mult (13, p) << 16) |
197 ((u32) ff_mult (11, p) << 24);
199 it_tab[0][i] = t;
200 it_tab[1][i] = rol32(t, 8);
201 it_tab[2][i] = rol32(t, 16);
202 it_tab[3][i] = rol32(t, 24);
203 }
204 }
206 #define star_x(x) (((x) & 0x7f7f7f7f) << 1) ^ ((((x) & 0x80808080) >> 7) * 0x1b)
208 #define imix_col(y,x) \
209 u = star_x(x); \
210 v = star_x(u); \
211 w = star_x(v); \
212 t = w ^ (x); \
213 (y) = u ^ v ^ w; \
214 (y) ^= ror32(u ^ t, 8) ^ \
215 ror32(v ^ t, 16) ^ \
216 ror32(t,24)
218 /* initialise the key schedule from the user supplied key */
220 #define loop4(i) \
221 { t = ror32(t, 8); t = ls_box(t) ^ rco_tab[i]; \
222 t ^= E_KEY[4 * i]; E_KEY[4 * i + 4] = t; \
223 t ^= E_KEY[4 * i + 1]; E_KEY[4 * i + 5] = t; \
224 t ^= E_KEY[4 * i + 2]; E_KEY[4 * i + 6] = t; \
225 t ^= E_KEY[4 * i + 3]; E_KEY[4 * i + 7] = t; \
226 }
228 #define loop6(i) \
229 { t = ror32(t, 8); t = ls_box(t) ^ rco_tab[i]; \
230 t ^= E_KEY[6 * i]; E_KEY[6 * i + 6] = t; \
231 t ^= E_KEY[6 * i + 1]; E_KEY[6 * i + 7] = t; \
232 t ^= E_KEY[6 * i + 2]; E_KEY[6 * i + 8] = t; \
233 t ^= E_KEY[6 * i + 3]; E_KEY[6 * i + 9] = t; \
234 t ^= E_KEY[6 * i + 4]; E_KEY[6 * i + 10] = t; \
235 t ^= E_KEY[6 * i + 5]; E_KEY[6 * i + 11] = t; \
236 }
238 #define loop8(i) \
239 { t = ror32(t, 8); ; t = ls_box(t) ^ rco_tab[i]; \
240 t ^= E_KEY[8 * i]; E_KEY[8 * i + 8] = t; \
241 t ^= E_KEY[8 * i + 1]; E_KEY[8 * i + 9] = t; \
242 t ^= E_KEY[8 * i + 2]; E_KEY[8 * i + 10] = t; \
243 t ^= E_KEY[8 * i + 3]; E_KEY[8 * i + 11] = t; \
244 t = E_KEY[8 * i + 4] ^ ls_box(t); \
245 E_KEY[8 * i + 12] = t; \
246 t ^= E_KEY[8 * i + 5]; E_KEY[8 * i + 13] = t; \
247 t ^= E_KEY[8 * i + 6]; E_KEY[8 * i + 14] = t; \
248 t ^= E_KEY[8 * i + 7]; E_KEY[8 * i + 15] = t; \
249 }
251 static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
252 unsigned int key_len, u32 *flags)
253 {
254 struct aes_ctx *ctx = crypto_tfm_ctx(tfm);
255 const __le32 *key = (const __le32 *)in_key;
256 u32 i, t, u, v, w;
258 if (key_len != 16 && key_len != 24 && key_len != 32) {
259 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
260 return -EINVAL;
261 }
263 ctx->key_length = key_len;
265 E_KEY[0] = le32_to_cpu(key[0]);
266 E_KEY[1] = le32_to_cpu(key[1]);
267 E_KEY[2] = le32_to_cpu(key[2]);
268 E_KEY[3] = le32_to_cpu(key[3]);
270 switch (key_len) {
271 case 16:
272 t = E_KEY[3];
273 for (i = 0; i < 10; ++i)
274 loop4 (i);
275 break;
277 case 24:
278 E_KEY[4] = le32_to_cpu(key[4]);
279 t = E_KEY[5] = le32_to_cpu(key[5]);
280 for (i = 0; i < 8; ++i)
281 loop6 (i);
282 break;
284 case 32:
285 E_KEY[4] = le32_to_cpu(key[4]);
286 E_KEY[5] = le32_to_cpu(key[5]);
287 E_KEY[6] = le32_to_cpu(key[6]);
288 t = E_KEY[7] = le32_to_cpu(key[7]);
289 for (i = 0; i < 7; ++i)
290 loop8 (i);
291 break;
292 }
294 D_KEY[0] = E_KEY[0];
295 D_KEY[1] = E_KEY[1];
296 D_KEY[2] = E_KEY[2];
297 D_KEY[3] = E_KEY[3];
299 for (i = 4; i < key_len + 24; ++i) {
300 imix_col (D_KEY[i], E_KEY[i]);
301 }
303 return 0;
304 }
306 /* encrypt a block of text */
308 #define f_nround(bo, bi, k) \
309 f_rn(bo, bi, 0, k); \
310 f_rn(bo, bi, 1, k); \
311 f_rn(bo, bi, 2, k); \
312 f_rn(bo, bi, 3, k); \
313 k += 4
315 #define f_lround(bo, bi, k) \
316 f_rl(bo, bi, 0, k); \
317 f_rl(bo, bi, 1, k); \
318 f_rl(bo, bi, 2, k); \
319 f_rl(bo, bi, 3, k)
321 static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
322 {
323 const struct aes_ctx *ctx = crypto_tfm_ctx(tfm);
324 const __le32 *src = (const __le32 *)in;
325 __le32 *dst = (__le32 *)out;
326 u32 b0[4], b1[4];
327 const u32 *kp = E_KEY + 4;
329 b0[0] = le32_to_cpu(src[0]) ^ E_KEY[0];
330 b0[1] = le32_to_cpu(src[1]) ^ E_KEY[1];
331 b0[2] = le32_to_cpu(src[2]) ^ E_KEY[2];
332 b0[3] = le32_to_cpu(src[3]) ^ E_KEY[3];
334 if (ctx->key_length > 24) {
335 f_nround (b1, b0, kp);
336 f_nround (b0, b1, kp);
337 }
339 if (ctx->key_length > 16) {
340 f_nround (b1, b0, kp);
341 f_nround (b0, b1, kp);
342 }
344 f_nround (b1, b0, kp);
345 f_nround (b0, b1, kp);
346 f_nround (b1, b0, kp);
347 f_nround (b0, b1, kp);
348 f_nround (b1, b0, kp);
349 f_nround (b0, b1, kp);
350 f_nround (b1, b0, kp);
351 f_nround (b0, b1, kp);
352 f_nround (b1, b0, kp);
353 f_lround (b0, b1, kp);
355 dst[0] = cpu_to_le32(b0[0]);
356 dst[1] = cpu_to_le32(b0[1]);
357 dst[2] = cpu_to_le32(b0[2]);
358 dst[3] = cpu_to_le32(b0[3]);
359 }
361 /* decrypt a block of text */
363 #define i_nround(bo, bi, k) \
364 i_rn(bo, bi, 0, k); \
365 i_rn(bo, bi, 1, k); \
366 i_rn(bo, bi, 2, k); \
367 i_rn(bo, bi, 3, k); \
368 k -= 4
370 #define i_lround(bo, bi, k) \
371 i_rl(bo, bi, 0, k); \
372 i_rl(bo, bi, 1, k); \
373 i_rl(bo, bi, 2, k); \
374 i_rl(bo, bi, 3, k)
376 static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
377 {
378 const struct aes_ctx *ctx = crypto_tfm_ctx(tfm);
379 const __le32 *src = (const __le32 *)in;
380 __le32 *dst = (__le32 *)out;
381 u32 b0[4], b1[4];
382 const int key_len = ctx->key_length;
383 const u32 *kp = D_KEY + key_len + 20;
385 b0[0] = le32_to_cpu(src[0]) ^ E_KEY[key_len + 24];
386 b0[1] = le32_to_cpu(src[1]) ^ E_KEY[key_len + 25];
387 b0[2] = le32_to_cpu(src[2]) ^ E_KEY[key_len + 26];
388 b0[3] = le32_to_cpu(src[3]) ^ E_KEY[key_len + 27];
390 if (key_len > 24) {
391 i_nround (b1, b0, kp);
392 i_nround (b0, b1, kp);
393 }
395 if (key_len > 16) {
396 i_nround (b1, b0, kp);
397 i_nround (b0, b1, kp);
398 }
400 i_nround (b1, b0, kp);
401 i_nround (b0, b1, kp);
402 i_nround (b1, b0, kp);
403 i_nround (b0, b1, kp);
404 i_nround (b1, b0, kp);
405 i_nround (b0, b1, kp);
406 i_nround (b1, b0, kp);
407 i_nround (b0, b1, kp);
408 i_nround (b1, b0, kp);
409 i_lround (b0, b1, kp);
411 dst[0] = cpu_to_le32(b0[0]);
412 dst[1] = cpu_to_le32(b0[1]);
413 dst[2] = cpu_to_le32(b0[2]);
414 dst[3] = cpu_to_le32(b0[3]);
415 }
418 static struct crypto_alg aes_alg = {
419 .cra_name = "aes",
420 .cra_driver_name = "aes-generic",
421 .cra_priority = 100,
422 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
423 .cra_blocksize = AES_BLOCK_SIZE,
424 .cra_ctxsize = sizeof(struct aes_ctx),
425 .cra_alignmask = 3,
426 .cra_module = THIS_MODULE,
427 .cra_list = LIST_HEAD_INIT(aes_alg.cra_list),
428 .cra_u = {
429 .cipher = {
430 .cia_min_keysize = AES_MIN_KEY_SIZE,
431 .cia_max_keysize = AES_MAX_KEY_SIZE,
432 .cia_setkey = aes_set_key,
433 .cia_encrypt = aes_encrypt,
434 .cia_decrypt = aes_decrypt
435 }
436 }
437 };
439 static int __init aes_init(void)
440 {
441 gen_tabs();
442 return crypto_register_alg(&aes_alg);
443 }
445 static void __exit aes_fini(void)
446 {
447 crypto_unregister_alg(&aes_alg);
448 }
450 module_init(aes_init);
451 module_exit(aes_fini);
453 MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm");
454 MODULE_LICENSE("Dual BSD/GPL");