55 #include <linux/module.h>
57 #include <linux/types.h>
58 #include <linux/errno.h>
61 #include <asm/byteorder.h>
63 #define AES_MIN_KEY_SIZE 16
64 #define AES_MAX_KEY_SIZE 32
66 #define AES_BLOCK_SIZE 16
69 u32 generic_rotr32 (
const u32 x,
const unsigned bits)
71 const unsigned n = bits % 32;
72 return (x >> n) | (x << (32 -
n));
76 u32 generic_rotl32 (
const u32 x,
const unsigned bits)
78 const unsigned n = bits % 32;
79 return (x << n) | (x >> (32 -
n));
82 #define rotl generic_rotl32
83 #define rotr generic_rotr32
89 byte(
const u32 x,
const unsigned n)
94 #define u32_in(x) le32_to_cpu(*(const u32 *)(x))
95 #define u32_out(to, from) (*(u32 *)(to) = cpu_to_le32(from))
110 static u32 rco_tab[10];
111 static u32 ft_tab[4][256];
112 static u32 it_tab[4][256];
114 static u32 fl_tab[4][256];
115 static u32 il_tab[4][256];
120 u8 aa = log_tab[
a],
cc = aa + log_tab[
b];
122 return pow_tab[cc + (cc < aa ? 1 : 0)];
125 #define ff_mult(a,b) (a && b ? f_mult(a, b) : 0)
127 #define f_rn(bo, bi, n, k) \
128 bo[n] = ft_tab[0][byte(bi[n],0)] ^ \
129 ft_tab[1][byte(bi[(n + 1) & 3],1)] ^ \
130 ft_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
131 ft_tab[3][byte(bi[(n + 3) & 3],3)] ^ *(k + n)
133 #define i_rn(bo, bi, n, k) \
134 bo[n] = it_tab[0][byte(bi[n],0)] ^ \
135 it_tab[1][byte(bi[(n + 3) & 3],1)] ^ \
136 it_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
137 it_tab[3][byte(bi[(n + 1) & 3],3)] ^ *(k + n)
140 ( fl_tab[0][byte(x, 0)] ^ \
141 fl_tab[1][byte(x, 1)] ^ \
142 fl_tab[2][byte(x, 2)] ^ \
143 fl_tab[3][byte(x, 3)] )
145 #define f_rl(bo, bi, n, k) \
146 bo[n] = fl_tab[0][byte(bi[n],0)] ^ \
147 fl_tab[1][byte(bi[(n + 1) & 3],1)] ^ \
148 fl_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
149 fl_tab[3][byte(bi[(n + 3) & 3],3)] ^ *(k + n)
151 #define i_rl(bo, bi, n, k) \
152 bo[n] = il_tab[0][byte(bi[n],0)] ^ \
153 il_tab[1][byte(bi[(n + 3) & 3],1)] ^ \
154 il_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
155 il_tab[3][byte(bi[(n + 1) & 3],3)] ^ *(k + n)
167 for (i = 0, p = 1; i < 256; ++
i) {
171 p ^= (p << 1) ^ (p & 0x80 ? 0x01b : 0);
176 for (i = 0, p = 1; i < 10; ++
i) {
179 p = (p << 1) ^ (p & 0x80 ? 0x01b : 0);
182 for (i = 0; i < 256; ++
i) {
183 p = (i ? pow_tab[255 - log_tab[
i]] : 0);
184 q = ((p >> 7) | (p << 1)) ^ ((p >> 6) | (p << 2));
185 p ^= 0x63 ^ q ^ ((q >> 6) | (q << 2));
190 for (i = 0; i < 256; ++
i) {
195 fl_tab[1][
i] =
rotl (t, 8);
196 fl_tab[2][
i] =
rotl (t, 16);
197 fl_tab[3][
i] =
rotl (t, 24);
204 ft_tab[1][
i] =
rotl (t, 8);
205 ft_tab[2][
i] =
rotl (t, 16);
206 ft_tab[3][
i] =
rotl (t, 24);
212 il_tab[1][
i] =
rotl (t, 8);
213 il_tab[2][
i] =
rotl (t, 16);
214 il_tab[3][
i] =
rotl (t, 24);
222 it_tab[1][
i] =
rotl (t, 8);
223 it_tab[2][
i] =
rotl (t, 16);
224 it_tab[3][
i] =
rotl (t, 24);
228 #define star_x(x) (((x) & 0x7f7f7f7f) << 1) ^ ((((x) & 0x80808080) >> 7) * 0x1b)
230 #define imix_col(y,x) \
236 (y) ^= rotr(u ^ t, 8) ^ \
243 { t = rotr(t, 8); t = ls_box(t) ^ rco_tab[i]; \
244 t ^= E_KEY[4 * i]; E_KEY[4 * i + 4] = t; \
245 t ^= E_KEY[4 * i + 1]; E_KEY[4 * i + 5] = t; \
246 t ^= E_KEY[4 * i + 2]; E_KEY[4 * i + 6] = t; \
247 t ^= E_KEY[4 * i + 3]; E_KEY[4 * i + 7] = t; \
251 { t = rotr(t, 8); t = ls_box(t) ^ rco_tab[i]; \
252 t ^= E_KEY[6 * i]; E_KEY[6 * i + 6] = t; \
253 t ^= E_KEY[6 * i + 1]; E_KEY[6 * i + 7] = t; \
254 t ^= E_KEY[6 * i + 2]; E_KEY[6 * i + 8] = t; \
255 t ^= E_KEY[6 * i + 3]; E_KEY[6 * i + 9] = t; \
256 t ^= E_KEY[6 * i + 4]; E_KEY[6 * i + 10] = t; \
257 t ^= E_KEY[6 * i + 5]; E_KEY[6 * i + 11] = t; \
261 { t = rotr(t, 8); ; t = ls_box(t) ^ rco_tab[i]; \
262 t ^= E_KEY[8 * i]; E_KEY[8 * i + 8] = t; \
263 t ^= E_KEY[8 * i + 1]; E_KEY[8 * i + 9] = t; \
264 t ^= E_KEY[8 * i + 2]; E_KEY[8 * i + 10] = t; \
265 t ^= E_KEY[8 * i + 3]; E_KEY[8 * i + 11] = t; \
266 t = E_KEY[8 * i + 4] ^ ls_box(t); \
267 E_KEY[8 * i + 12] = t; \
268 t ^= E_KEY[8 * i + 5]; E_KEY[8 * i + 13] = t; \
269 t ^= E_KEY[8 * i + 6]; E_KEY[8 * i + 14] = t; \
270 t ^= E_KEY[8 * i + 7]; E_KEY[8 * i + 15] = t; \
279 if (key_len != 16 && key_len != 24 && key_len != 32) {
294 for (i = 0; i < 10; ++
i)
301 for (i = 0; i < 8; ++
i)
310 for (i = 0; i < 7; ++
i)
320 for (i = 4; i < key_len + 24; ++
i) {
329 #define f_nround(bo, bi, k) \
330 f_rn(bo, bi, 0, k); \
331 f_rn(bo, bi, 1, k); \
332 f_rn(bo, bi, 2, k); \
333 f_rn(bo, bi, 3, k); \
336 #define f_lround(bo, bi, k) \
337 f_rl(bo, bi, 0, k); \
338 f_rl(bo, bi, 1, k); \
339 f_rl(bo, bi, 2, k); \
342 static void aes_encrypt(
void *ctx_arg,
u8 *
out,
const u8 *
in)
382 #define i_nround(bo, bi, k) \
383 i_rn(bo, bi, 0, k); \
384 i_rn(bo, bi, 1, k); \
385 i_rn(bo, bi, 2, k); \
386 i_rn(bo, bi, 3, k); \
389 #define i_lround(bo, bi, k) \
390 i_rl(bo, bi, 0, k); \
391 i_rl(bo, bi, 1, k); \
392 i_rl(bo, bi, 2, k); \
395 static void aes_decrypt(
void *ctx_arg,
u8 *
out,
const u8 *
in)
400 const u32 *kp =
D_KEY + key_len + 20;
439 .cra_ctxsize =
sizeof(
struct aes_ctx),
446 .cia_setkey = aes_set_key,
447 .cia_encrypt = aes_encrypt,
448 .cia_decrypt = aes_decrypt
453 static int __init aes_init(
void)
459 static void __exit aes_fini(
void)