aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/aes_generic.c
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/aes_generic.c')
-rw-r--r--crypto/aes_generic.c325
1 files changed, 176 insertions, 149 deletions
diff --git a/crypto/aes_generic.c b/crypto/aes_generic.c
index 6683260475f9..df8df4d346d2 100644
--- a/crypto/aes_generic.c
+++ b/crypto/aes_generic.c
@@ -63,8 +63,7 @@
63/* 63/*
64 * #define byte(x, nr) ((unsigned char)((x) >> (nr*8))) 64 * #define byte(x, nr) ((unsigned char)((x) >> (nr*8)))
65 */ 65 */
66static inline u8 66static inline u8 byte(const u32 x, const unsigned n)
67byte(const u32 x, const unsigned n)
68{ 67{
69 return x >> (n << 3); 68 return x >> (n << 3);
70} 69}
@@ -88,55 +87,25 @@ static u32 it_tab[4][256];
88static u32 fl_tab[4][256]; 87static u32 fl_tab[4][256];
89static u32 il_tab[4][256]; 88static u32 il_tab[4][256];
90 89
91static inline u8 __init 90static inline u8 __init f_mult(u8 a, u8 b)
92f_mult (u8 a, u8 b)
93{ 91{
94 u8 aa = log_tab[a], cc = aa + log_tab[b]; 92 u8 aa = log_tab[a], cc = aa + log_tab[b];
95 93
96 return pow_tab[cc + (cc < aa ? 1 : 0)]; 94 return pow_tab[cc + (cc < aa ? 1 : 0)];
97} 95}
98 96
99#define ff_mult(a,b) (a && b ? f_mult(a, b) : 0) 97#define ff_mult(a, b) (a && b ? f_mult(a, b) : 0)
100 98
101#define f_rn(bo, bi, n, k) \ 99static void __init gen_tabs(void)
102 bo[n] = ft_tab[0][byte(bi[n],0)] ^ \
103 ft_tab[1][byte(bi[(n + 1) & 3],1)] ^ \
104 ft_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
105 ft_tab[3][byte(bi[(n + 3) & 3],3)] ^ *(k + n)
106
107#define i_rn(bo, bi, n, k) \
108 bo[n] = it_tab[0][byte(bi[n],0)] ^ \
109 it_tab[1][byte(bi[(n + 3) & 3],1)] ^ \
110 it_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
111 it_tab[3][byte(bi[(n + 1) & 3],3)] ^ *(k + n)
112
113#define ls_box(x) \
114 ( fl_tab[0][byte(x, 0)] ^ \
115 fl_tab[1][byte(x, 1)] ^ \
116 fl_tab[2][byte(x, 2)] ^ \
117 fl_tab[3][byte(x, 3)] )
118
119#define f_rl(bo, bi, n, k) \
120 bo[n] = fl_tab[0][byte(bi[n],0)] ^ \
121 fl_tab[1][byte(bi[(n + 1) & 3],1)] ^ \
122 fl_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
123 fl_tab[3][byte(bi[(n + 3) & 3],3)] ^ *(k + n)
124
125#define i_rl(bo, bi, n, k) \
126 bo[n] = il_tab[0][byte(bi[n],0)] ^ \
127 il_tab[1][byte(bi[(n + 3) & 3],1)] ^ \
128 il_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
129 il_tab[3][byte(bi[(n + 1) & 3],3)] ^ *(k + n)
130
131static void __init
132gen_tabs (void)
133{ 100{
134 u32 i, t; 101 u32 i, t;
135 u8 p, q; 102 u8 p, q;
136 103
137 /* log and power tables for GF(2**8) finite field with 104 /*
138 0x011b as modular polynomial - the simplest primitive 105 * log and power tables for GF(2**8) finite field with
139 root is 0x03, used here to generate the tables */ 106 * 0x011b as modular polynomial - the simplest primitive
107 * root is 0x03, used here to generate the tables
108 */
140 109
141 for (i = 0, p = 1; i < 256; ++i) { 110 for (i = 0, p = 1; i < 256; ++i) {
142 pow_tab[i] = (u8) p; 111 pow_tab[i] = (u8) p;
@@ -170,9 +139,9 @@ gen_tabs (void)
170 fl_tab[2][i] = rol32(t, 16); 139 fl_tab[2][i] = rol32(t, 16);
171 fl_tab[3][i] = rol32(t, 24); 140 fl_tab[3][i] = rol32(t, 24);
172 141
173 t = ((u32) ff_mult (2, p)) | 142 t = ((u32) ff_mult(2, p)) |
174 ((u32) p << 8) | 143 ((u32) p << 8) |
175 ((u32) p << 16) | ((u32) ff_mult (3, p) << 24); 144 ((u32) p << 16) | ((u32) ff_mult(3, p) << 24);
176 145
177 ft_tab[0][i] = t; 146 ft_tab[0][i] = t;
178 ft_tab[1][i] = rol32(t, 8); 147 ft_tab[1][i] = rol32(t, 8);
@@ -187,10 +156,10 @@ gen_tabs (void)
187 il_tab[2][i] = rol32(t, 16); 156 il_tab[2][i] = rol32(t, 16);
188 il_tab[3][i] = rol32(t, 24); 157 il_tab[3][i] = rol32(t, 24);
189 158
190 t = ((u32) ff_mult (14, p)) | 159 t = ((u32) ff_mult(14, p)) |
191 ((u32) ff_mult (9, p) << 8) | 160 ((u32) ff_mult(9, p) << 8) |
192 ((u32) ff_mult (13, p) << 16) | 161 ((u32) ff_mult(13, p) << 16) |
193 ((u32) ff_mult (11, p) << 24); 162 ((u32) ff_mult(11, p) << 24);
194 163
195 it_tab[0][i] = t; 164 it_tab[0][i] = t;
196 it_tab[1][i] = rol32(t, 8); 165 it_tab[1][i] = rol32(t, 8);
@@ -199,53 +168,80 @@ gen_tabs (void)
199 } 168 }
200} 169}
201 170
202#define star_x(x) (((x) & 0x7f7f7f7f) << 1) ^ ((((x) & 0x80808080) >> 7) * 0x1b)
203
204#define imix_col(y,x) \
205 u = star_x(x); \
206 v = star_x(u); \
207 w = star_x(v); \
208 t = w ^ (x); \
209 (y) = u ^ v ^ w; \
210 (y) ^= ror32(u ^ t, 8) ^ \
211 ror32(v ^ t, 16) ^ \
212 ror32(t,24)
213
214/* initialise the key schedule from the user supplied key */ 171/* initialise the key schedule from the user supplied key */
215 172
216#define loop4(i) \ 173#define star_x(x) (((x) & 0x7f7f7f7f) << 1) ^ ((((x) & 0x80808080) >> 7) * 0x1b)
217{ t = ror32(t, 8); t = ls_box(t) ^ rco_tab[i]; \
218 t ^= E_KEY[4 * i]; E_KEY[4 * i + 4] = t; \
219 t ^= E_KEY[4 * i + 1]; E_KEY[4 * i + 5] = t; \
220 t ^= E_KEY[4 * i + 2]; E_KEY[4 * i + 6] = t; \
221 t ^= E_KEY[4 * i + 3]; E_KEY[4 * i + 7] = t; \
222}
223
224#define loop6(i) \
225{ t = ror32(t, 8); t = ls_box(t) ^ rco_tab[i]; \
226 t ^= E_KEY[6 * i]; E_KEY[6 * i + 6] = t; \
227 t ^= E_KEY[6 * i + 1]; E_KEY[6 * i + 7] = t; \
228 t ^= E_KEY[6 * i + 2]; E_KEY[6 * i + 8] = t; \
229 t ^= E_KEY[6 * i + 3]; E_KEY[6 * i + 9] = t; \
230 t ^= E_KEY[6 * i + 4]; E_KEY[6 * i + 10] = t; \
231 t ^= E_KEY[6 * i + 5]; E_KEY[6 * i + 11] = t; \
232}
233 174
234#define loop8(i) \ 175#define imix_col(y,x) do { \
235{ t = ror32(t, 8); ; t = ls_box(t) ^ rco_tab[i]; \ 176 u = star_x(x); \
236 t ^= E_KEY[8 * i]; E_KEY[8 * i + 8] = t; \ 177 v = star_x(u); \
237 t ^= E_KEY[8 * i + 1]; E_KEY[8 * i + 9] = t; \ 178 w = star_x(v); \
238 t ^= E_KEY[8 * i + 2]; E_KEY[8 * i + 10] = t; \ 179 t = w ^ (x); \
239 t ^= E_KEY[8 * i + 3]; E_KEY[8 * i + 11] = t; \ 180 (y) = u ^ v ^ w; \
240 t = E_KEY[8 * i + 4] ^ ls_box(t); \ 181 (y) ^= ror32(u ^ t, 8) ^ \
241 E_KEY[8 * i + 12] = t; \ 182 ror32(v ^ t, 16) ^ \
242 t ^= E_KEY[8 * i + 5]; E_KEY[8 * i + 13] = t; \ 183 ror32(t, 24); \
243 t ^= E_KEY[8 * i + 6]; E_KEY[8 * i + 14] = t; \ 184} while (0)
244 t ^= E_KEY[8 * i + 7]; E_KEY[8 * i + 15] = t; \ 185
245} 186#define ls_box(x) \
187 fl_tab[0][byte(x, 0)] ^ \
188 fl_tab[1][byte(x, 1)] ^ \
189 fl_tab[2][byte(x, 2)] ^ \
190 fl_tab[3][byte(x, 3)]
191
192#define loop4(i) do { \
193 t = ror32(t, 8); \
194 t = ls_box(t) ^ rco_tab[i]; \
195 t ^= E_KEY[4 * i]; \
196 E_KEY[4 * i + 4] = t; \
197 t ^= E_KEY[4 * i + 1]; \
198 E_KEY[4 * i + 5] = t; \
199 t ^= E_KEY[4 * i + 2]; \
200 E_KEY[4 * i + 6] = t; \
201 t ^= E_KEY[4 * i + 3]; \
202 E_KEY[4 * i + 7] = t; \
203} while (0)
204
205#define loop6(i) do { \
206 t = ror32(t, 8); \
207 t = ls_box(t) ^ rco_tab[i]; \
208 t ^= E_KEY[6 * i]; \
209 E_KEY[6 * i + 6] = t; \
210 t ^= E_KEY[6 * i + 1]; \
211 E_KEY[6 * i + 7] = t; \
212 t ^= E_KEY[6 * i + 2]; \
213 E_KEY[6 * i + 8] = t; \
214 t ^= E_KEY[6 * i + 3]; \
215 E_KEY[6 * i + 9] = t; \
216 t ^= E_KEY[6 * i + 4]; \
217 E_KEY[6 * i + 10] = t; \
218 t ^= E_KEY[6 * i + 5]; \
219 E_KEY[6 * i + 11] = t; \
220} while (0)
221
222#define loop8(i) do { \
223 t = ror32(t, 8); \
224 t = ls_box(t) ^ rco_tab[i]; \
225 t ^= E_KEY[8 * i]; \
226 E_KEY[8 * i + 8] = t; \
227 t ^= E_KEY[8 * i + 1]; \
228 E_KEY[8 * i + 9] = t; \
229 t ^= E_KEY[8 * i + 2]; \
230 E_KEY[8 * i + 10] = t; \
231 t ^= E_KEY[8 * i + 3]; \
232 E_KEY[8 * i + 11] = t; \
233 t = E_KEY[8 * i + 4] ^ ls_box(t); \
234 E_KEY[8 * i + 12] = t; \
235 t ^= E_KEY[8 * i + 5]; \
236 E_KEY[8 * i + 13] = t; \
237 t ^= E_KEY[8 * i + 6]; \
238 E_KEY[8 * i + 14] = t; \
239 t ^= E_KEY[8 * i + 7]; \
240 E_KEY[8 * i + 15] = t; \
241} while (0)
246 242
247static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key, 243static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
248 unsigned int key_len) 244 unsigned int key_len)
249{ 245{
250 struct aes_ctx *ctx = crypto_tfm_ctx(tfm); 246 struct aes_ctx *ctx = crypto_tfm_ctx(tfm);
251 const __le32 *key = (const __le32 *)in_key; 247 const __le32 *key = (const __le32 *)in_key;
@@ -268,14 +264,14 @@ static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
268 case 16: 264 case 16:
269 t = E_KEY[3]; 265 t = E_KEY[3];
270 for (i = 0; i < 10; ++i) 266 for (i = 0; i < 10; ++i)
271 loop4 (i); 267 loop4(i);
272 break; 268 break;
273 269
274 case 24: 270 case 24:
275 E_KEY[4] = le32_to_cpu(key[4]); 271 E_KEY[4] = le32_to_cpu(key[4]);
276 t = E_KEY[5] = le32_to_cpu(key[5]); 272 t = E_KEY[5] = le32_to_cpu(key[5]);
277 for (i = 0; i < 8; ++i) 273 for (i = 0; i < 8; ++i)
278 loop6 (i); 274 loop6(i);
279 break; 275 break;
280 276
281 case 32: 277 case 32:
@@ -284,7 +280,7 @@ static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
284 E_KEY[6] = le32_to_cpu(key[6]); 280 E_KEY[6] = le32_to_cpu(key[6]);
285 t = E_KEY[7] = le32_to_cpu(key[7]); 281 t = E_KEY[7] = le32_to_cpu(key[7]);
286 for (i = 0; i < 7; ++i) 282 for (i = 0; i < 7; ++i)
287 loop8 (i); 283 loop8(i);
288 break; 284 break;
289 } 285 }
290 286
@@ -294,7 +290,7 @@ static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
294 D_KEY[3] = E_KEY[3]; 290 D_KEY[3] = E_KEY[3];
295 291
296 for (i = 4; i < key_len + 24; ++i) { 292 for (i = 4; i < key_len + 24; ++i) {
297 imix_col (D_KEY[i], E_KEY[i]); 293 imix_col(D_KEY[i], E_KEY[i]);
298 } 294 }
299 295
300 return 0; 296 return 0;
@@ -302,18 +298,34 @@ static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
302 298
303/* encrypt a block of text */ 299/* encrypt a block of text */
304 300
305#define f_nround(bo, bi, k) \ 301#define f_rn(bo, bi, n, k) do { \
306 f_rn(bo, bi, 0, k); \ 302 bo[n] = ft_tab[0][byte(bi[n], 0)] ^ \
307 f_rn(bo, bi, 1, k); \ 303 ft_tab[1][byte(bi[(n + 1) & 3], 1)] ^ \
308 f_rn(bo, bi, 2, k); \ 304 ft_tab[2][byte(bi[(n + 2) & 3], 2)] ^ \
309 f_rn(bo, bi, 3, k); \ 305 ft_tab[3][byte(bi[(n + 3) & 3], 3)] ^ *(k + n); \
310 k += 4 306} while (0)
311 307
312#define f_lround(bo, bi, k) \ 308#define f_nround(bo, bi, k) do {\
313 f_rl(bo, bi, 0, k); \ 309 f_rn(bo, bi, 0, k); \
314 f_rl(bo, bi, 1, k); \ 310 f_rn(bo, bi, 1, k); \
315 f_rl(bo, bi, 2, k); \ 311 f_rn(bo, bi, 2, k); \
316 f_rl(bo, bi, 3, k) 312 f_rn(bo, bi, 3, k); \
313 k += 4; \
314} while (0)
315
316#define f_rl(bo, bi, n, k) do { \
317 bo[n] = fl_tab[0][byte(bi[n], 0)] ^ \
318 fl_tab[1][byte(bi[(n + 1) & 3], 1)] ^ \
319 fl_tab[2][byte(bi[(n + 2) & 3], 2)] ^ \
320 fl_tab[3][byte(bi[(n + 3) & 3], 3)] ^ *(k + n); \
321} while (0)
322
323#define f_lround(bo, bi, k) do {\
324 f_rl(bo, bi, 0, k); \
325 f_rl(bo, bi, 1, k); \
326 f_rl(bo, bi, 2, k); \
327 f_rl(bo, bi, 3, k); \
328} while (0)
317 329
318static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) 330static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
319{ 331{
@@ -329,25 +341,25 @@ static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
329 b0[3] = le32_to_cpu(src[3]) ^ E_KEY[3]; 341 b0[3] = le32_to_cpu(src[3]) ^ E_KEY[3];
330 342
331 if (ctx->key_length > 24) { 343 if (ctx->key_length > 24) {
332 f_nround (b1, b0, kp); 344 f_nround(b1, b0, kp);
333 f_nround (b0, b1, kp); 345 f_nround(b0, b1, kp);
334 } 346 }
335 347
336 if (ctx->key_length > 16) { 348 if (ctx->key_length > 16) {
337 f_nround (b1, b0, kp); 349 f_nround(b1, b0, kp);
338 f_nround (b0, b1, kp); 350 f_nround(b0, b1, kp);
339 } 351 }
340 352
341 f_nround (b1, b0, kp); 353 f_nround(b1, b0, kp);
342 f_nround (b0, b1, kp); 354 f_nround(b0, b1, kp);
343 f_nround (b1, b0, kp); 355 f_nround(b1, b0, kp);
344 f_nround (b0, b1, kp); 356 f_nround(b0, b1, kp);
345 f_nround (b1, b0, kp); 357 f_nround(b1, b0, kp);
346 f_nround (b0, b1, kp); 358 f_nround(b0, b1, kp);
347 f_nround (b1, b0, kp); 359 f_nround(b1, b0, kp);
348 f_nround (b0, b1, kp); 360 f_nround(b0, b1, kp);
349 f_nround (b1, b0, kp); 361 f_nround(b1, b0, kp);
350 f_lround (b0, b1, kp); 362 f_lround(b0, b1, kp);
351 363
352 dst[0] = cpu_to_le32(b0[0]); 364 dst[0] = cpu_to_le32(b0[0]);
353 dst[1] = cpu_to_le32(b0[1]); 365 dst[1] = cpu_to_le32(b0[1]);
@@ -357,18 +369,34 @@ static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
357 369
358/* decrypt a block of text */ 370/* decrypt a block of text */
359 371
360#define i_nround(bo, bi, k) \ 372#define i_rn(bo, bi, n, k) do { \
361 i_rn(bo, bi, 0, k); \ 373 bo[n] = it_tab[0][byte(bi[n], 0)] ^ \
362 i_rn(bo, bi, 1, k); \ 374 it_tab[1][byte(bi[(n + 3) & 3], 1)] ^ \
363 i_rn(bo, bi, 2, k); \ 375 it_tab[2][byte(bi[(n + 2) & 3], 2)] ^ \
364 i_rn(bo, bi, 3, k); \ 376 it_tab[3][byte(bi[(n + 1) & 3], 3)] ^ *(k + n); \
365 k -= 4 377} while (0)
366 378
367#define i_lround(bo, bi, k) \ 379#define i_nround(bo, bi, k) do {\
368 i_rl(bo, bi, 0, k); \ 380 i_rn(bo, bi, 0, k); \
369 i_rl(bo, bi, 1, k); \ 381 i_rn(bo, bi, 1, k); \
370 i_rl(bo, bi, 2, k); \ 382 i_rn(bo, bi, 2, k); \
371 i_rl(bo, bi, 3, k) 383 i_rn(bo, bi, 3, k); \
384 k -= 4; \
385} while (0)
386
387#define i_rl(bo, bi, n, k) do { \
388 bo[n] = il_tab[0][byte(bi[n], 0)] ^ \
389 il_tab[1][byte(bi[(n + 3) & 3], 1)] ^ \
390 il_tab[2][byte(bi[(n + 2) & 3], 2)] ^ \
391 il_tab[3][byte(bi[(n + 1) & 3], 3)] ^ *(k + n); \
392} while (0)
393
394#define i_lround(bo, bi, k) do {\
395 i_rl(bo, bi, 0, k); \
396 i_rl(bo, bi, 1, k); \
397 i_rl(bo, bi, 2, k); \
398 i_rl(bo, bi, 3, k); \
399} while (0)
372 400
373static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) 401static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
374{ 402{
@@ -385,25 +413,25 @@ static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
385 b0[3] = le32_to_cpu(src[3]) ^ E_KEY[key_len + 27]; 413 b0[3] = le32_to_cpu(src[3]) ^ E_KEY[key_len + 27];
386 414
387 if (key_len > 24) { 415 if (key_len > 24) {
388 i_nround (b1, b0, kp); 416 i_nround(b1, b0, kp);
389 i_nround (b0, b1, kp); 417 i_nround(b0, b1, kp);
390 } 418 }
391 419
392 if (key_len > 16) { 420 if (key_len > 16) {
393 i_nround (b1, b0, kp); 421 i_nround(b1, b0, kp);
394 i_nround (b0, b1, kp); 422 i_nround(b0, b1, kp);
395 } 423 }
396 424
397 i_nround (b1, b0, kp); 425 i_nround(b1, b0, kp);
398 i_nround (b0, b1, kp); 426 i_nround(b0, b1, kp);
399 i_nround (b1, b0, kp); 427 i_nround(b1, b0, kp);
400 i_nround (b0, b1, kp); 428 i_nround(b0, b1, kp);
401 i_nround (b1, b0, kp); 429 i_nround(b1, b0, kp);
402 i_nround (b0, b1, kp); 430 i_nround(b0, b1, kp);
403 i_nround (b1, b0, kp); 431 i_nround(b1, b0, kp);
404 i_nround (b0, b1, kp); 432 i_nround(b0, b1, kp);
405 i_nround (b1, b0, kp); 433 i_nround(b1, b0, kp);
406 i_lround (b0, b1, kp); 434 i_lround(b0, b1, kp);
407 435
408 dst[0] = cpu_to_le32(b0[0]); 436 dst[0] = cpu_to_le32(b0[0]);
409 dst[1] = cpu_to_le32(b0[1]); 437 dst[1] = cpu_to_le32(b0[1]);
@@ -411,7 +439,6 @@ static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
411 dst[3] = cpu_to_le32(b0[3]); 439 dst[3] = cpu_to_le32(b0[3]);
412} 440}
413 441
414
415static struct crypto_alg aes_alg = { 442static struct crypto_alg aes_alg = {
416 .cra_name = "aes", 443 .cra_name = "aes",
417 .cra_driver_name = "aes-generic", 444 .cra_driver_name = "aes-generic",
@@ -426,9 +453,9 @@ static struct crypto_alg aes_alg = {
426 .cipher = { 453 .cipher = {
427 .cia_min_keysize = AES_MIN_KEY_SIZE, 454 .cia_min_keysize = AES_MIN_KEY_SIZE,
428 .cia_max_keysize = AES_MAX_KEY_SIZE, 455 .cia_max_keysize = AES_MAX_KEY_SIZE,
429 .cia_setkey = aes_set_key, 456 .cia_setkey = aes_set_key,
430 .cia_encrypt = aes_encrypt, 457 .cia_encrypt = aes_encrypt,
431 .cia_decrypt = aes_decrypt 458 .cia_decrypt = aes_decrypt
432 } 459 }
433 } 460 }
434}; 461};