4 * AES Cipher Algorithm.
6 * Based on Brian Gladman's code.
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.
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.
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)))
71 byte(const u32 x
, const unsigned n
)
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
98 u8 aa
= log_tab
[a
], cc
= aa
+ log_tab
[b
];
100 return pow_tab
[cc
+ (cc
< aa
? 1 : 0)];
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)
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)
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
) {
149 p
^= (p
<< 1) ^ (p
& 0x80 ? 0x01b : 0);
154 for (i
= 0, p
= 1; i
< 10; ++i
) {
157 p
= (p
<< 1) ^ (p
& 0x80 ? 0x01b : 0);
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));
168 for (i
= 0; i
< 256; ++i
) {
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
)) |
179 ((u32
) p
<< 16) | ((u32
) ff_mult (3, p
) << 24);
182 ft_tab
[1][i
] = rol32(t
, 8);
183 ft_tab
[2][i
] = rol32(t
, 16);
184 ft_tab
[3][i
] = rol32(t
, 24);
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);
200 it_tab
[1][i
] = rol32(t
, 8);
201 it_tab
[2][i
] = rol32(t
, 16);
202 it_tab
[3][i
] = rol32(t
, 24);
206 #define star_x(x) (((x) & 0x7f7f7f7f) << 1) ^ ((((x) & 0x80808080) >> 7) * 0x1b)
208 #define imix_col(y,x) \
214 (y) ^= ror32(u ^ t, 8) ^ \
218 /* initialise the key schedule from the user supplied key */
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; \
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; \
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; \
252 aes_set_key(void *ctx_arg
, const u8
*in_key
, unsigned int key_len
, u32
*flags
)
254 struct aes_ctx
*ctx
= ctx_arg
;
255 const __le32
*key
= (const __le32
*)in_key
;
258 if (key_len
!= 16 && key_len
!= 24 && key_len
!= 32) {
259 *flags
|= CRYPTO_TFM_RES_BAD_KEY_LEN
;
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]);
273 for (i
= 0; i
< 10; ++i
)
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
)
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
)
299 for (i
= 4; i
< key_len
+ 24; ++i
) {
300 imix_col (D_KEY
[i
], E_KEY
[i
]);
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); \
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); \
321 static void aes_encrypt(void *ctx_arg
, u8
*out
, const u8
*in
)
323 const struct aes_ctx
*ctx
= ctx_arg
;
324 const __le32
*src
= (const __le32
*)in
;
325 __le32
*dst
= (__le32
*)out
;
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
);
339 if (ctx
->key_length
> 16) {
340 f_nround (b1
, b0
, kp
);
341 f_nround (b0
, b1
, kp
);
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]);
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); \
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); \
376 static void aes_decrypt(void *ctx_arg
, u8
*out
, const u8
*in
)
378 const struct aes_ctx
*ctx
= ctx_arg
;
379 const __le32
*src
= (const __le32
*)in
;
380 __le32
*dst
= (__le32
*)out
;
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];
391 i_nround (b1
, b0
, kp
);
392 i_nround (b0
, b1
, kp
);
396 i_nround (b1
, b0
, kp
);
397 i_nround (b0
, b1
, kp
);
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]);
418 static struct crypto_alg aes_alg
= {
420 .cra_driver_name
= "aes-generic",
422 .cra_flags
= CRYPTO_ALG_TYPE_CIPHER
,
423 .cra_blocksize
= AES_BLOCK_SIZE
,
424 .cra_ctxsize
= sizeof(struct aes_ctx
),
426 .cra_module
= THIS_MODULE
,
427 .cra_list
= LIST_HEAD_INIT(aes_alg
.cra_list
),
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
439 static int __init
aes_init(void)
442 return crypto_register_alg(&aes_alg
);
445 static void __exit
aes_fini(void)
447 crypto_unregister_alg(&aes_alg
);
450 module_init(aes_init
);
451 module_exit(aes_fini
);
453 MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm");
454 MODULE_LICENSE("Dual BSD/GPL");