[PATCH] x86: Stop deleting nt
[linux-2.6/mini2440.git] / crypto / aes.c
blob5df92888ef5a81b78fc34482c6d7261eb586ab2d
1 /*
2 * Cryptographic API.
4 * AES Cipher Algorithm.
6 * Based on Brian Gladman's code.
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).
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.
19 * ---------------------------------------------------------------------------
20 * Copyright (c) 2002, Dr Brian Gladman <brg@gladman.me.uk>, Worcester, UK.
21 * All rights reserved.
23 * LICENSE TERMS
25 * The free distribution and use of this software in both source and binary
26 * form is allowed (with or without changes) provided that:
28 * 1. distributions of this source code include the above copyright
29 * notice, this list of conditions and the following disclaimer;
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;
35 * 3. the copyright holder's name is not used to endorse products
36 * built using this software without specific written permission.
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.
42 * DISCLAIMER
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 * ---------------------------------------------------------------------------
50 /* Some changes from the Gladman version:
51 s/RIJNDAEL(e_key)/E_KEY/g
52 s/RIJNDAEL(d_key)/D_KEY/g
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
68 * #define byte(x, nr) ((unsigned char)((x) >> (nr*8)))
70 static inline u8
71 byte(const u32 x, const unsigned n)
73 return x >> (n << 3);
76 #define u32_in(x) le32_to_cpu(*(const u32 *)(x))
77 #define u32_out(to, from) (*(u32 *)(to) = cpu_to_le32(from))
79 struct aes_ctx {
80 int key_length;
81 u32 E[60];
82 u32 D[60];
85 #define E_KEY ctx->E
86 #define D_KEY ctx->D
88 static u8 pow_tab[256] __initdata;
89 static u8 log_tab[256] __initdata;
90 static u8 sbx_tab[256] __initdata;
91 static u8 isb_tab[256] __initdata;
92 static u32 rco_tab[10];
93 static u32 ft_tab[4][256];
94 static u32 it_tab[4][256];
96 static u32 fl_tab[4][256];
97 static u32 il_tab[4][256];
99 static inline u8 __init
100 f_mult (u8 a, u8 b)
102 u8 aa = log_tab[a], cc = aa + log_tab[b];
104 return pow_tab[cc + (cc < aa ? 1 : 0)];
107 #define ff_mult(a,b) (a && b ? f_mult(a, b) : 0)
109 #define f_rn(bo, bi, n, k) \
110 bo[n] = ft_tab[0][byte(bi[n],0)] ^ \
111 ft_tab[1][byte(bi[(n + 1) & 3],1)] ^ \
112 ft_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
113 ft_tab[3][byte(bi[(n + 3) & 3],3)] ^ *(k + n)
115 #define i_rn(bo, bi, n, k) \
116 bo[n] = it_tab[0][byte(bi[n],0)] ^ \
117 it_tab[1][byte(bi[(n + 3) & 3],1)] ^ \
118 it_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
119 it_tab[3][byte(bi[(n + 1) & 3],3)] ^ *(k + n)
121 #define ls_box(x) \
122 ( fl_tab[0][byte(x, 0)] ^ \
123 fl_tab[1][byte(x, 1)] ^ \
124 fl_tab[2][byte(x, 2)] ^ \
125 fl_tab[3][byte(x, 3)] )
127 #define f_rl(bo, bi, n, k) \
128 bo[n] = fl_tab[0][byte(bi[n],0)] ^ \
129 fl_tab[1][byte(bi[(n + 1) & 3],1)] ^ \
130 fl_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
131 fl_tab[3][byte(bi[(n + 3) & 3],3)] ^ *(k + n)
133 #define i_rl(bo, bi, n, k) \
134 bo[n] = il_tab[0][byte(bi[n],0)] ^ \
135 il_tab[1][byte(bi[(n + 3) & 3],1)] ^ \
136 il_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
137 il_tab[3][byte(bi[(n + 1) & 3],3)] ^ *(k + n)
139 static void __init
140 gen_tabs (void)
142 u32 i, t;
143 u8 p, q;
145 /* log and power tables for GF(2**8) finite field with
146 0x011b as modular polynomial - the simplest primitive
147 root is 0x03, used here to generate the tables */
149 for (i = 0, p = 1; i < 256; ++i) {
150 pow_tab[i] = (u8) p;
151 log_tab[p] = (u8) i;
153 p ^= (p << 1) ^ (p & 0x80 ? 0x01b : 0);
156 log_tab[1] = 0;
158 for (i = 0, p = 1; i < 10; ++i) {
159 rco_tab[i] = p;
161 p = (p << 1) ^ (p & 0x80 ? 0x01b : 0);
164 for (i = 0; i < 256; ++i) {
165 p = (i ? pow_tab[255 - log_tab[i]] : 0);
166 q = ((p >> 7) | (p << 1)) ^ ((p >> 6) | (p << 2));
167 p ^= 0x63 ^ q ^ ((q >> 6) | (q << 2));
168 sbx_tab[i] = p;
169 isb_tab[p] = (u8) i;
172 for (i = 0; i < 256; ++i) {
173 p = sbx_tab[i];
175 t = p;
176 fl_tab[0][i] = t;
177 fl_tab[1][i] = rol32(t, 8);
178 fl_tab[2][i] = rol32(t, 16);
179 fl_tab[3][i] = rol32(t, 24);
181 t = ((u32) ff_mult (2, p)) |
182 ((u32) p << 8) |
183 ((u32) p << 16) | ((u32) ff_mult (3, p) << 24);
185 ft_tab[0][i] = t;
186 ft_tab[1][i] = rol32(t, 8);
187 ft_tab[2][i] = rol32(t, 16);
188 ft_tab[3][i] = rol32(t, 24);
190 p = isb_tab[i];
192 t = p;
193 il_tab[0][i] = t;
194 il_tab[1][i] = rol32(t, 8);
195 il_tab[2][i] = rol32(t, 16);
196 il_tab[3][i] = rol32(t, 24);
198 t = ((u32) ff_mult (14, p)) |
199 ((u32) ff_mult (9, p) << 8) |
200 ((u32) ff_mult (13, p) << 16) |
201 ((u32) ff_mult (11, p) << 24);
203 it_tab[0][i] = t;
204 it_tab[1][i] = rol32(t, 8);
205 it_tab[2][i] = rol32(t, 16);
206 it_tab[3][i] = rol32(t, 24);
210 #define star_x(x) (((x) & 0x7f7f7f7f) << 1) ^ ((((x) & 0x80808080) >> 7) * 0x1b)
212 #define imix_col(y,x) \
213 u = star_x(x); \
214 v = star_x(u); \
215 w = star_x(v); \
216 t = w ^ (x); \
217 (y) = u ^ v ^ w; \
218 (y) ^= ror32(u ^ t, 8) ^ \
219 ror32(v ^ t, 16) ^ \
220 ror32(t,24)
222 /* initialise the key schedule from the user supplied key */
224 #define loop4(i) \
225 { t = ror32(t, 8); t = ls_box(t) ^ rco_tab[i]; \
226 t ^= E_KEY[4 * i]; E_KEY[4 * i + 4] = t; \
227 t ^= E_KEY[4 * i + 1]; E_KEY[4 * i + 5] = t; \
228 t ^= E_KEY[4 * i + 2]; E_KEY[4 * i + 6] = t; \
229 t ^= E_KEY[4 * i + 3]; E_KEY[4 * i + 7] = t; \
232 #define loop6(i) \
233 { t = ror32(t, 8); t = ls_box(t) ^ rco_tab[i]; \
234 t ^= E_KEY[6 * i]; E_KEY[6 * i + 6] = t; \
235 t ^= E_KEY[6 * i + 1]; E_KEY[6 * i + 7] = t; \
236 t ^= E_KEY[6 * i + 2]; E_KEY[6 * i + 8] = t; \
237 t ^= E_KEY[6 * i + 3]; E_KEY[6 * i + 9] = t; \
238 t ^= E_KEY[6 * i + 4]; E_KEY[6 * i + 10] = t; \
239 t ^= E_KEY[6 * i + 5]; E_KEY[6 * i + 11] = t; \
242 #define loop8(i) \
243 { t = ror32(t, 8); ; t = ls_box(t) ^ rco_tab[i]; \
244 t ^= E_KEY[8 * i]; E_KEY[8 * i + 8] = t; \
245 t ^= E_KEY[8 * i + 1]; E_KEY[8 * i + 9] = t; \
246 t ^= E_KEY[8 * i + 2]; E_KEY[8 * i + 10] = t; \
247 t ^= E_KEY[8 * i + 3]; E_KEY[8 * i + 11] = t; \
248 t = E_KEY[8 * i + 4] ^ ls_box(t); \
249 E_KEY[8 * i + 12] = t; \
250 t ^= E_KEY[8 * i + 5]; E_KEY[8 * i + 13] = t; \
251 t ^= E_KEY[8 * i + 6]; E_KEY[8 * i + 14] = t; \
252 t ^= E_KEY[8 * i + 7]; E_KEY[8 * i + 15] = t; \
255 static int
256 aes_set_key(void *ctx_arg, const u8 *in_key, unsigned int key_len, u32 *flags)
258 struct aes_ctx *ctx = ctx_arg;
259 u32 i, t, u, v, w;
261 if (key_len != 16 && key_len != 24 && key_len != 32) {
262 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
263 return -EINVAL;
266 ctx->key_length = key_len;
268 E_KEY[0] = u32_in (in_key);
269 E_KEY[1] = u32_in (in_key + 4);
270 E_KEY[2] = u32_in (in_key + 8);
271 E_KEY[3] = u32_in (in_key + 12);
273 switch (key_len) {
274 case 16:
275 t = E_KEY[3];
276 for (i = 0; i < 10; ++i)
277 loop4 (i);
278 break;
280 case 24:
281 E_KEY[4] = u32_in (in_key + 16);
282 t = E_KEY[5] = u32_in (in_key + 20);
283 for (i = 0; i < 8; ++i)
284 loop6 (i);
285 break;
287 case 32:
288 E_KEY[4] = u32_in (in_key + 16);
289 E_KEY[5] = u32_in (in_key + 20);
290 E_KEY[6] = u32_in (in_key + 24);
291 t = E_KEY[7] = u32_in (in_key + 28);
292 for (i = 0; i < 7; ++i)
293 loop8 (i);
294 break;
297 D_KEY[0] = E_KEY[0];
298 D_KEY[1] = E_KEY[1];
299 D_KEY[2] = E_KEY[2];
300 D_KEY[3] = E_KEY[3];
302 for (i = 4; i < key_len + 24; ++i) {
303 imix_col (D_KEY[i], E_KEY[i]);
306 return 0;
309 /* encrypt a block of text */
311 #define f_nround(bo, bi, k) \
312 f_rn(bo, bi, 0, k); \
313 f_rn(bo, bi, 1, k); \
314 f_rn(bo, bi, 2, k); \
315 f_rn(bo, bi, 3, k); \
316 k += 4
318 #define f_lround(bo, bi, k) \
319 f_rl(bo, bi, 0, k); \
320 f_rl(bo, bi, 1, k); \
321 f_rl(bo, bi, 2, k); \
322 f_rl(bo, bi, 3, k)
324 static void aes_encrypt(void *ctx_arg, u8 *out, const u8 *in)
326 const struct aes_ctx *ctx = ctx_arg;
327 u32 b0[4], b1[4];
328 const u32 *kp = E_KEY + 4;
330 b0[0] = u32_in (in) ^ E_KEY[0];
331 b0[1] = u32_in (in + 4) ^ E_KEY[1];
332 b0[2] = u32_in (in + 8) ^ E_KEY[2];
333 b0[3] = u32_in (in + 12) ^ E_KEY[3];
335 if (ctx->key_length > 24) {
336 f_nround (b1, b0, kp);
337 f_nround (b0, b1, kp);
340 if (ctx->key_length > 16) {
341 f_nround (b1, b0, kp);
342 f_nround (b0, b1, kp);
345 f_nround (b1, b0, kp);
346 f_nround (b0, b1, kp);
347 f_nround (b1, b0, kp);
348 f_nround (b0, b1, kp);
349 f_nround (b1, b0, kp);
350 f_nround (b0, b1, kp);
351 f_nround (b1, b0, kp);
352 f_nround (b0, b1, kp);
353 f_nround (b1, b0, kp);
354 f_lround (b0, b1, kp);
356 u32_out (out, b0[0]);
357 u32_out (out + 4, b0[1]);
358 u32_out (out + 8, b0[2]);
359 u32_out (out + 12, b0[3]);
362 /* decrypt a block of text */
364 #define i_nround(bo, bi, k) \
365 i_rn(bo, bi, 0, k); \
366 i_rn(bo, bi, 1, k); \
367 i_rn(bo, bi, 2, k); \
368 i_rn(bo, bi, 3, k); \
369 k -= 4
371 #define i_lround(bo, bi, k) \
372 i_rl(bo, bi, 0, k); \
373 i_rl(bo, bi, 1, k); \
374 i_rl(bo, bi, 2, k); \
375 i_rl(bo, bi, 3, k)
377 static void aes_decrypt(void *ctx_arg, u8 *out, const u8 *in)
379 const struct aes_ctx *ctx = ctx_arg;
380 u32 b0[4], b1[4];
381 const int key_len = ctx->key_length;
382 const u32 *kp = D_KEY + key_len + 20;
384 b0[0] = u32_in (in) ^ E_KEY[key_len + 24];
385 b0[1] = u32_in (in + 4) ^ E_KEY[key_len + 25];
386 b0[2] = u32_in (in + 8) ^ E_KEY[key_len + 26];
387 b0[3] = u32_in (in + 12) ^ E_KEY[key_len + 27];
389 if (key_len > 24) {
390 i_nround (b1, b0, kp);
391 i_nround (b0, b1, kp);
394 if (key_len > 16) {
395 i_nround (b1, b0, kp);
396 i_nround (b0, b1, kp);
399 i_nround (b1, b0, kp);
400 i_nround (b0, b1, kp);
401 i_nround (b1, b0, kp);
402 i_nround (b0, b1, kp);
403 i_nround (b1, b0, kp);
404 i_nround (b0, b1, kp);
405 i_nround (b1, b0, kp);
406 i_nround (b0, b1, kp);
407 i_nround (b1, b0, kp);
408 i_lround (b0, b1, kp);
410 u32_out (out, b0[0]);
411 u32_out (out + 4, b0[1]);
412 u32_out (out + 8, b0[2]);
413 u32_out (out + 12, b0[3]);
417 static struct crypto_alg aes_alg = {
418 .cra_name = "aes",
419 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
420 .cra_blocksize = AES_BLOCK_SIZE,
421 .cra_ctxsize = sizeof(struct aes_ctx),
422 .cra_module = THIS_MODULE,
423 .cra_list = LIST_HEAD_INIT(aes_alg.cra_list),
424 .cra_u = {
425 .cipher = {
426 .cia_min_keysize = AES_MIN_KEY_SIZE,
427 .cia_max_keysize = AES_MAX_KEY_SIZE,
428 .cia_setkey = aes_set_key,
429 .cia_encrypt = aes_encrypt,
430 .cia_decrypt = aes_decrypt
435 static int __init aes_init(void)
437 gen_tabs();
438 return crypto_register_alg(&aes_alg);
441 static void __exit aes_fini(void)
443 crypto_unregister_alg(&aes_alg);
446 module_init(aes_init);
447 module_exit(aes_fini);
449 MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm");
450 MODULE_LICENSE("Dual BSD/GPL");