aboutsummaryrefslogtreecommitdiffstats
path: root/arch
diff options
context:
space:
mode:
authorSebastian Siewior <sebastian@breakpoint.cc>2007-11-10 06:07:16 -0500
committerHerbert Xu <herbert@gondor.apana.org.au>2008-01-10 16:16:10 -0500
commit5157dea8139cf0edc4834d528531e642c0d27e37 (patch)
tree33d14cadc04c60ea95449c6bdc64ec0102eb823d /arch
parentb345cee90a3ffec5eca6d6c1c59bd0d1feb453d4 (diff)
[CRYPTO] aes-i586: Remove setkey
The setkey() function can be shared with the generic algorithm. Signed-off-by: Sebastian Siewior <sebastian@breakpoint.cc> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'arch')
-rw-r--r--arch/x86/crypto/aes-i586-asm_32.S89
-rw-r--r--arch/x86/crypto/aes_32.c461
2 files changed, 46 insertions, 504 deletions
diff --git a/arch/x86/crypto/aes-i586-asm_32.S b/arch/x86/crypto/aes-i586-asm_32.S
index f942f0c8f630..1093bede3e0a 100644
--- a/arch/x86/crypto/aes-i586-asm_32.S
+++ b/arch/x86/crypto/aes-i586-asm_32.S
@@ -46,9 +46,9 @@
46#define in_blk 16 46#define in_blk 16
47 47
48/* offsets in crypto_tfm structure */ 48/* offsets in crypto_tfm structure */
49#define ekey (crypto_tfm_ctx_offset + 0) 49#define klen (crypto_tfm_ctx_offset + 0)
50#define nrnd (crypto_tfm_ctx_offset + 256) 50#define ekey (crypto_tfm_ctx_offset + 4)
51#define dkey (crypto_tfm_ctx_offset + 260) 51#define dkey (crypto_tfm_ctx_offset + 244)
52 52
53// register mapping for encrypt and decrypt subroutines 53// register mapping for encrypt and decrypt subroutines
54 54
@@ -221,8 +221,8 @@
221 221
222.global aes_enc_blk 222.global aes_enc_blk
223 223
224.extern ft_tab 224.extern crypto_ft_tab
225.extern fl_tab 225.extern crypto_fl_tab
226 226
227.align 4 227.align 4
228 228
@@ -236,7 +236,7 @@ aes_enc_blk:
2361: push %ebx 2361: push %ebx
237 mov in_blk+4(%esp),%r2 237 mov in_blk+4(%esp),%r2
238 push %esi 238 push %esi
239 mov nrnd(%ebp),%r3 // number of rounds 239 mov klen(%ebp),%r3 // key size
240 push %edi 240 push %edi
241#if ekey != 0 241#if ekey != 0
242 lea ekey(%ebp),%ebp // key pointer 242 lea ekey(%ebp),%ebp // key pointer
@@ -255,26 +255,26 @@ aes_enc_blk:
255 255
256 sub $8,%esp // space for register saves on stack 256 sub $8,%esp // space for register saves on stack
257 add $16,%ebp // increment to next round key 257 add $16,%ebp // increment to next round key
258 cmp $12,%r3 258 cmp $24,%r3
259 jb 4f // 10 rounds for 128-bit key 259 jb 4f // 10 rounds for 128-bit key
260 lea 32(%ebp),%ebp 260 lea 32(%ebp),%ebp
261 je 3f // 12 rounds for 192-bit key 261 je 3f // 12 rounds for 192-bit key
262 lea 32(%ebp),%ebp 262 lea 32(%ebp),%ebp
263 263
2642: fwd_rnd1( -64(%ebp) ,ft_tab) // 14 rounds for 256-bit key 2642: fwd_rnd1( -64(%ebp), crypto_ft_tab) // 14 rounds for 256-bit key
265 fwd_rnd2( -48(%ebp) ,ft_tab) 265 fwd_rnd2( -48(%ebp), crypto_ft_tab)
2663: fwd_rnd1( -32(%ebp) ,ft_tab) // 12 rounds for 192-bit key 2663: fwd_rnd1( -32(%ebp), crypto_ft_tab) // 12 rounds for 192-bit key
267 fwd_rnd2( -16(%ebp) ,ft_tab) 267 fwd_rnd2( -16(%ebp), crypto_ft_tab)
2684: fwd_rnd1( (%ebp) ,ft_tab) // 10 rounds for 128-bit key 2684: fwd_rnd1( (%ebp), crypto_ft_tab) // 10 rounds for 128-bit key
269 fwd_rnd2( +16(%ebp) ,ft_tab) 269 fwd_rnd2( +16(%ebp), crypto_ft_tab)
270 fwd_rnd1( +32(%ebp) ,ft_tab) 270 fwd_rnd1( +32(%ebp), crypto_ft_tab)
271 fwd_rnd2( +48(%ebp) ,ft_tab) 271 fwd_rnd2( +48(%ebp), crypto_ft_tab)
272 fwd_rnd1( +64(%ebp) ,ft_tab) 272 fwd_rnd1( +64(%ebp), crypto_ft_tab)
273 fwd_rnd2( +80(%ebp) ,ft_tab) 273 fwd_rnd2( +80(%ebp), crypto_ft_tab)
274 fwd_rnd1( +96(%ebp) ,ft_tab) 274 fwd_rnd1( +96(%ebp), crypto_ft_tab)
275 fwd_rnd2(+112(%ebp) ,ft_tab) 275 fwd_rnd2(+112(%ebp), crypto_ft_tab)
276 fwd_rnd1(+128(%ebp) ,ft_tab) 276 fwd_rnd1(+128(%ebp), crypto_ft_tab)
277 fwd_rnd2(+144(%ebp) ,fl_tab) // last round uses a different table 277 fwd_rnd2(+144(%ebp), crypto_fl_tab) // last round uses a different table
278 278
279// move final values to the output array. CAUTION: the 279// move final values to the output array. CAUTION: the
280// order of these assigns rely on the register mappings 280// order of these assigns rely on the register mappings
@@ -297,8 +297,8 @@ aes_enc_blk:
297 297
298.global aes_dec_blk 298.global aes_dec_blk
299 299
300.extern it_tab 300.extern crypto_it_tab
301.extern il_tab 301.extern crypto_il_tab
302 302
303.align 4 303.align 4
304 304
@@ -312,14 +312,11 @@ aes_dec_blk:
3121: push %ebx 3121: push %ebx
313 mov in_blk+4(%esp),%r2 313 mov in_blk+4(%esp),%r2
314 push %esi 314 push %esi
315 mov nrnd(%ebp),%r3 // number of rounds 315 mov klen(%ebp),%r3 // key size
316 push %edi 316 push %edi
317#if dkey != 0 317#if dkey != 0
318 lea dkey(%ebp),%ebp // key pointer 318 lea dkey(%ebp),%ebp // key pointer
319#endif 319#endif
320 mov %r3,%r0
321 shl $4,%r0
322 add %r0,%ebp
323 320
324// input four columns and xor in first round key 321// input four columns and xor in first round key
325 322
@@ -333,27 +330,27 @@ aes_dec_blk:
333 xor 12(%ebp),%r5 330 xor 12(%ebp),%r5
334 331
335 sub $8,%esp // space for register saves on stack 332 sub $8,%esp // space for register saves on stack
336 sub $16,%ebp // increment to next round key 333 add $16,%ebp // increment to next round key
337 cmp $12,%r3 334 cmp $24,%r3
338 jb 4f // 10 rounds for 128-bit key 335 jb 4f // 10 rounds for 128-bit key
339 lea -32(%ebp),%ebp 336 lea 32(%ebp),%ebp
340 je 3f // 12 rounds for 192-bit key 337 je 3f // 12 rounds for 192-bit key
341 lea -32(%ebp),%ebp 338 lea 32(%ebp),%ebp
342 339
3432: inv_rnd1( +64(%ebp), it_tab) // 14 rounds for 256-bit key 3402: inv_rnd1( -64(%ebp), crypto_it_tab) // 14 rounds for 256-bit key
344 inv_rnd2( +48(%ebp), it_tab) 341 inv_rnd2( -48(%ebp), crypto_it_tab)
3453: inv_rnd1( +32(%ebp), it_tab) // 12 rounds for 192-bit key 3423: inv_rnd1( -32(%ebp), crypto_it_tab) // 12 rounds for 192-bit key
346 inv_rnd2( +16(%ebp), it_tab) 343 inv_rnd2( -16(%ebp), crypto_it_tab)
3474: inv_rnd1( (%ebp), it_tab) // 10 rounds for 128-bit key 3444: inv_rnd1( (%ebp), crypto_it_tab) // 10 rounds for 128-bit key
348 inv_rnd2( -16(%ebp), it_tab) 345 inv_rnd2( +16(%ebp), crypto_it_tab)
349 inv_rnd1( -32(%ebp), it_tab) 346 inv_rnd1( +32(%ebp), crypto_it_tab)
350 inv_rnd2( -48(%ebp), it_tab) 347 inv_rnd2( +48(%ebp), crypto_it_tab)
351 inv_rnd1( -64(%ebp), it_tab) 348 inv_rnd1( +64(%ebp), crypto_it_tab)
352 inv_rnd2( -80(%ebp), it_tab) 349 inv_rnd2( +80(%ebp), crypto_it_tab)
353 inv_rnd1( -96(%ebp), it_tab) 350 inv_rnd1( +96(%ebp), crypto_it_tab)
354 inv_rnd2(-112(%ebp), it_tab) 351 inv_rnd2(+112(%ebp), crypto_it_tab)
355 inv_rnd1(-128(%ebp), it_tab) 352 inv_rnd1(+128(%ebp), crypto_it_tab)
356 inv_rnd2(-144(%ebp), il_tab) // last round uses a different table 353 inv_rnd2(+144(%ebp), crypto_il_tab) // last round uses a different table
357 354
358// move final values to the output array. CAUTION: the 355// move final values to the output array. CAUTION: the
359// order of these assigns rely on the register mappings 356// order of these assigns rely on the register mappings
diff --git a/arch/x86/crypto/aes_32.c b/arch/x86/crypto/aes_32.c
index 9b0ab50394b0..8556d9561c20 100644
--- a/arch/x86/crypto/aes_32.c
+++ b/arch/x86/crypto/aes_32.c
@@ -1,468 +1,14 @@
1/* 1/*
2 *
3 * Glue Code for optimized 586 assembler version of AES 2 * Glue Code for optimized 586 assembler version of AES
4 *
5 * Copyright (c) 2002, Dr Brian Gladman <>, Worcester, UK.
6 * All rights reserved.
7 *
8 * LICENSE TERMS
9 *
10 * The free distribution and use of this software in both source and binary
11 * form is allowed (with or without changes) provided that:
12 *
13 * 1. distributions of this source code include the above copyright
14 * notice, this list of conditions and the following disclaimer;
15 *
16 * 2. distributions in binary form include the above copyright
17 * notice, this list of conditions and the following disclaimer
18 * in the documentation and/or other associated materials;
19 *
20 * 3. the copyright holder's name is not used to endorse products
21 * built using this software without specific written permission.
22 *
23 * ALTERNATIVELY, provided that this notice is retained in full, this product
24 * may be distributed under the terms of the GNU General Public License (GPL),
25 * in which case the provisions of the GPL apply INSTEAD OF those given above.
26 *
27 * DISCLAIMER
28 *
29 * This software is provided 'as is' with no explicit or implied warranties
30 * in respect of its properties, including, but not limited to, correctness
31 * and/or fitness for purpose.
32 *
33 * Copyright (c) 2003, Adam J. Richter <adam@yggdrasil.com> (conversion to
34 * 2.5 API).
35 * Copyright (c) 2003, 2004 Fruhwirth Clemens <clemens@endorphin.org>
36 * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
37 *
38 */ 3 */
39 4
40#include <asm/byteorder.h>
41#include <crypto/aes.h> 5#include <crypto/aes.h>
42#include <linux/kernel.h>
43#include <linux/module.h> 6#include <linux/module.h>
44#include <linux/init.h>
45#include <linux/types.h>
46#include <linux/crypto.h> 7#include <linux/crypto.h>
47#include <linux/linkage.h>
48 8
49asmlinkage void aes_enc_blk(struct crypto_tfm *tfm, u8 *dst, const u8 *src); 9asmlinkage void aes_enc_blk(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
50asmlinkage void aes_dec_blk(struct crypto_tfm *tfm, u8 *dst, const u8 *src); 10asmlinkage void aes_dec_blk(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
51 11
52#define AES_KS_LENGTH 4 * AES_BLOCK_SIZE
53#define RC_LENGTH 29
54
55struct aes_ctx {
56 u32 ekey[AES_KS_LENGTH];
57 u32 rounds;
58 u32 dkey[AES_KS_LENGTH];
59};
60
61#define WPOLY 0x011b
62#define bytes2word(b0, b1, b2, b3) \
63 (((u32)(b3) << 24) | ((u32)(b2) << 16) | ((u32)(b1) << 8) | (b0))
64
65/* define the finite field multiplies required for Rijndael */
66#define f2(x) ((x) ? pow[log[x] + 0x19] : 0)
67#define f3(x) ((x) ? pow[log[x] + 0x01] : 0)
68#define f9(x) ((x) ? pow[log[x] + 0xc7] : 0)
69#define fb(x) ((x) ? pow[log[x] + 0x68] : 0)
70#define fd(x) ((x) ? pow[log[x] + 0xee] : 0)
71#define fe(x) ((x) ? pow[log[x] + 0xdf] : 0)
72#define fi(x) ((x) ? pow[255 - log[x]]: 0)
73
74static inline u32 upr(u32 x, int n)
75{
76 return (x << 8 * n) | (x >> (32 - 8 * n));
77}
78
79static inline u8 bval(u32 x, int n)
80{
81 return x >> 8 * n;
82}
83
84/* The forward and inverse affine transformations used in the S-box */
85#define fwd_affine(x) \
86 (w = (u32)x, w ^= (w<<1)^(w<<2)^(w<<3)^(w<<4), 0x63^(u8)(w^(w>>8)))
87
88#define inv_affine(x) \
89 (w = (u32)x, w = (w<<1)^(w<<3)^(w<<6), 0x05^(u8)(w^(w>>8)))
90
91static u32 rcon_tab[RC_LENGTH];
92
93u32 ft_tab[4][256];
94u32 fl_tab[4][256];
95static u32 im_tab[4][256];
96u32 il_tab[4][256];
97u32 it_tab[4][256];
98
99static void gen_tabs(void)
100{
101 u32 i, w;
102 u8 pow[512], log[256];
103
104 /*
105 * log and power tables for GF(2^8) finite field with
106 * WPOLY as modular polynomial - the simplest primitive
107 * root is 0x03, used here to generate the tables.
108 */
109 i = 0; w = 1;
110
111 do {
112 pow[i] = (u8)w;
113 pow[i + 255] = (u8)w;
114 log[w] = (u8)i++;
115 w ^= (w << 1) ^ (w & 0x80 ? WPOLY : 0);
116 } while (w != 1);
117
118 for(i = 0, w = 1; i < RC_LENGTH; ++i) {
119 rcon_tab[i] = bytes2word(w, 0, 0, 0);
120 w = f2(w);
121 }
122
123 for(i = 0; i < 256; ++i) {
124 u8 b;
125
126 b = fwd_affine(fi((u8)i));
127 w = bytes2word(f2(b), b, b, f3(b));
128
129 /* tables for a normal encryption round */
130 ft_tab[0][i] = w;
131 ft_tab[1][i] = upr(w, 1);
132 ft_tab[2][i] = upr(w, 2);
133 ft_tab[3][i] = upr(w, 3);
134 w = bytes2word(b, 0, 0, 0);
135
136 /*
137 * tables for last encryption round
138 * (may also be used in the key schedule)
139 */
140 fl_tab[0][i] = w;
141 fl_tab[1][i] = upr(w, 1);
142 fl_tab[2][i] = upr(w, 2);
143 fl_tab[3][i] = upr(w, 3);
144
145 b = fi(inv_affine((u8)i));
146 w = bytes2word(fe(b), f9(b), fd(b), fb(b));
147
148 /* tables for the inverse mix column operation */
149 im_tab[0][b] = w;
150 im_tab[1][b] = upr(w, 1);
151 im_tab[2][b] = upr(w, 2);
152 im_tab[3][b] = upr(w, 3);
153
154 /* tables for a normal decryption round */
155 it_tab[0][i] = w;
156 it_tab[1][i] = upr(w,1);
157 it_tab[2][i] = upr(w,2);
158 it_tab[3][i] = upr(w,3);
159
160 w = bytes2word(b, 0, 0, 0);
161
162 /* tables for last decryption round */
163 il_tab[0][i] = w;
164 il_tab[1][i] = upr(w,1);
165 il_tab[2][i] = upr(w,2);
166 il_tab[3][i] = upr(w,3);
167 }
168}
169
170#define four_tables(x,tab,vf,rf,c) \
171( tab[0][bval(vf(x,0,c),rf(0,c))] ^ \
172 tab[1][bval(vf(x,1,c),rf(1,c))] ^ \
173 tab[2][bval(vf(x,2,c),rf(2,c))] ^ \
174 tab[3][bval(vf(x,3,c),rf(3,c))] \
175)
176
177#define vf1(x,r,c) (x)
178#define rf1(r,c) (r)
179#define rf2(r,c) ((r-c)&3)
180
181#define inv_mcol(x) four_tables(x,im_tab,vf1,rf1,0)
182#define ls_box(x,c) four_tables(x,fl_tab,vf1,rf2,c)
183
184#define ff(x) inv_mcol(x)
185
186#define ke4(k,i) \
187{ \
188 k[4*(i)+4] = ss[0] ^= ls_box(ss[3],3) ^ rcon_tab[i]; \
189 k[4*(i)+5] = ss[1] ^= ss[0]; \
190 k[4*(i)+6] = ss[2] ^= ss[1]; \
191 k[4*(i)+7] = ss[3] ^= ss[2]; \
192}
193
194#define kel4(k,i) \
195{ \
196 k[4*(i)+4] = ss[0] ^= ls_box(ss[3],3) ^ rcon_tab[i]; \
197 k[4*(i)+5] = ss[1] ^= ss[0]; \
198 k[4*(i)+6] = ss[2] ^= ss[1]; k[4*(i)+7] = ss[3] ^= ss[2]; \
199}
200
201#define ke6(k,i) \
202{ \
203 k[6*(i)+ 6] = ss[0] ^= ls_box(ss[5],3) ^ rcon_tab[i]; \
204 k[6*(i)+ 7] = ss[1] ^= ss[0]; \
205 k[6*(i)+ 8] = ss[2] ^= ss[1]; \
206 k[6*(i)+ 9] = ss[3] ^= ss[2]; \
207 k[6*(i)+10] = ss[4] ^= ss[3]; \
208 k[6*(i)+11] = ss[5] ^= ss[4]; \
209}
210
211#define kel6(k,i) \
212{ \
213 k[6*(i)+ 6] = ss[0] ^= ls_box(ss[5],3) ^ rcon_tab[i]; \
214 k[6*(i)+ 7] = ss[1] ^= ss[0]; \
215 k[6*(i)+ 8] = ss[2] ^= ss[1]; \
216 k[6*(i)+ 9] = ss[3] ^= ss[2]; \
217}
218
219#define ke8(k,i) \
220{ \
221 k[8*(i)+ 8] = ss[0] ^= ls_box(ss[7],3) ^ rcon_tab[i]; \
222 k[8*(i)+ 9] = ss[1] ^= ss[0]; \
223 k[8*(i)+10] = ss[2] ^= ss[1]; \
224 k[8*(i)+11] = ss[3] ^= ss[2]; \
225 k[8*(i)+12] = ss[4] ^= ls_box(ss[3],0); \
226 k[8*(i)+13] = ss[5] ^= ss[4]; \
227 k[8*(i)+14] = ss[6] ^= ss[5]; \
228 k[8*(i)+15] = ss[7] ^= ss[6]; \
229}
230
231#define kel8(k,i) \
232{ \
233 k[8*(i)+ 8] = ss[0] ^= ls_box(ss[7],3) ^ rcon_tab[i]; \
234 k[8*(i)+ 9] = ss[1] ^= ss[0]; \
235 k[8*(i)+10] = ss[2] ^= ss[1]; \
236 k[8*(i)+11] = ss[3] ^= ss[2]; \
237}
238
239#define kdf4(k,i) \
240{ \
241 ss[0] = ss[0] ^ ss[2] ^ ss[1] ^ ss[3]; \
242 ss[1] = ss[1] ^ ss[3]; \
243 ss[2] = ss[2] ^ ss[3]; \
244 ss[3] = ss[3]; \
245 ss[4] = ls_box(ss[(i+3) % 4], 3) ^ rcon_tab[i]; \
246 ss[i % 4] ^= ss[4]; \
247 ss[4] ^= k[4*(i)]; \
248 k[4*(i)+4] = ff(ss[4]); \
249 ss[4] ^= k[4*(i)+1]; \
250 k[4*(i)+5] = ff(ss[4]); \
251 ss[4] ^= k[4*(i)+2]; \
252 k[4*(i)+6] = ff(ss[4]); \
253 ss[4] ^= k[4*(i)+3]; \
254 k[4*(i)+7] = ff(ss[4]); \
255}
256
257#define kd4(k,i) \
258{ \
259 ss[4] = ls_box(ss[(i+3) % 4], 3) ^ rcon_tab[i]; \
260 ss[i % 4] ^= ss[4]; \
261 ss[4] = ff(ss[4]); \
262 k[4*(i)+4] = ss[4] ^= k[4*(i)]; \
263 k[4*(i)+5] = ss[4] ^= k[4*(i)+1]; \
264 k[4*(i)+6] = ss[4] ^= k[4*(i)+2]; \
265 k[4*(i)+7] = ss[4] ^= k[4*(i)+3]; \
266}
267
268#define kdl4(k,i) \
269{ \
270 ss[4] = ls_box(ss[(i+3) % 4], 3) ^ rcon_tab[i]; \
271 ss[i % 4] ^= ss[4]; \
272 k[4*(i)+4] = (ss[0] ^= ss[1]) ^ ss[2] ^ ss[3]; \
273 k[4*(i)+5] = ss[1] ^ ss[3]; \
274 k[4*(i)+6] = ss[0]; \
275 k[4*(i)+7] = ss[1]; \
276}
277
278#define kdf6(k,i) \
279{ \
280 ss[0] ^= ls_box(ss[5],3) ^ rcon_tab[i]; \
281 k[6*(i)+ 6] = ff(ss[0]); \
282 ss[1] ^= ss[0]; \
283 k[6*(i)+ 7] = ff(ss[1]); \
284 ss[2] ^= ss[1]; \
285 k[6*(i)+ 8] = ff(ss[2]); \
286 ss[3] ^= ss[2]; \
287 k[6*(i)+ 9] = ff(ss[3]); \
288 ss[4] ^= ss[3]; \
289 k[6*(i)+10] = ff(ss[4]); \
290 ss[5] ^= ss[4]; \
291 k[6*(i)+11] = ff(ss[5]); \
292}
293
294#define kd6(k,i) \
295{ \
296 ss[6] = ls_box(ss[5],3) ^ rcon_tab[i]; \
297 ss[0] ^= ss[6]; ss[6] = ff(ss[6]); \
298 k[6*(i)+ 6] = ss[6] ^= k[6*(i)]; \
299 ss[1] ^= ss[0]; \
300 k[6*(i)+ 7] = ss[6] ^= k[6*(i)+ 1]; \
301 ss[2] ^= ss[1]; \
302 k[6*(i)+ 8] = ss[6] ^= k[6*(i)+ 2]; \
303 ss[3] ^= ss[2]; \
304 k[6*(i)+ 9] = ss[6] ^= k[6*(i)+ 3]; \
305 ss[4] ^= ss[3]; \
306 k[6*(i)+10] = ss[6] ^= k[6*(i)+ 4]; \
307 ss[5] ^= ss[4]; \
308 k[6*(i)+11] = ss[6] ^= k[6*(i)+ 5]; \
309}
310
311#define kdl6(k,i) \
312{ \
313 ss[0] ^= ls_box(ss[5],3) ^ rcon_tab[i]; \
314 k[6*(i)+ 6] = ss[0]; \
315 ss[1] ^= ss[0]; \
316 k[6*(i)+ 7] = ss[1]; \
317 ss[2] ^= ss[1]; \
318 k[6*(i)+ 8] = ss[2]; \
319 ss[3] ^= ss[2]; \
320 k[6*(i)+ 9] = ss[3]; \
321}
322
323#define kdf8(k,i) \
324{ \
325 ss[0] ^= ls_box(ss[7],3) ^ rcon_tab[i]; \
326 k[8*(i)+ 8] = ff(ss[0]); \
327 ss[1] ^= ss[0]; \
328 k[8*(i)+ 9] = ff(ss[1]); \
329 ss[2] ^= ss[1]; \
330 k[8*(i)+10] = ff(ss[2]); \
331 ss[3] ^= ss[2]; \
332 k[8*(i)+11] = ff(ss[3]); \
333 ss[4] ^= ls_box(ss[3],0); \
334 k[8*(i)+12] = ff(ss[4]); \
335 ss[5] ^= ss[4]; \
336 k[8*(i)+13] = ff(ss[5]); \
337 ss[6] ^= ss[5]; \
338 k[8*(i)+14] = ff(ss[6]); \
339 ss[7] ^= ss[6]; \
340 k[8*(i)+15] = ff(ss[7]); \
341}
342
343#define kd8(k,i) \
344{ \
345 u32 __g = ls_box(ss[7],3) ^ rcon_tab[i]; \
346 ss[0] ^= __g; \
347 __g = ff(__g); \
348 k[8*(i)+ 8] = __g ^= k[8*(i)]; \
349 ss[1] ^= ss[0]; \
350 k[8*(i)+ 9] = __g ^= k[8*(i)+ 1]; \
351 ss[2] ^= ss[1]; \
352 k[8*(i)+10] = __g ^= k[8*(i)+ 2]; \
353 ss[3] ^= ss[2]; \
354 k[8*(i)+11] = __g ^= k[8*(i)+ 3]; \
355 __g = ls_box(ss[3],0); \
356 ss[4] ^= __g; \
357 __g = ff(__g); \
358 k[8*(i)+12] = __g ^= k[8*(i)+ 4]; \
359 ss[5] ^= ss[4]; \
360 k[8*(i)+13] = __g ^= k[8*(i)+ 5]; \
361 ss[6] ^= ss[5]; \
362 k[8*(i)+14] = __g ^= k[8*(i)+ 6]; \
363 ss[7] ^= ss[6]; \
364 k[8*(i)+15] = __g ^= k[8*(i)+ 7]; \
365}
366
367#define kdl8(k,i) \
368{ \
369 ss[0] ^= ls_box(ss[7],3) ^ rcon_tab[i]; \
370 k[8*(i)+ 8] = ss[0]; \
371 ss[1] ^= ss[0]; \
372 k[8*(i)+ 9] = ss[1]; \
373 ss[2] ^= ss[1]; \
374 k[8*(i)+10] = ss[2]; \
375 ss[3] ^= ss[2]; \
376 k[8*(i)+11] = ss[3]; \
377}
378
379static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
380 unsigned int key_len)
381{
382 int i;
383 u32 ss[8];
384 struct aes_ctx *ctx = crypto_tfm_ctx(tfm);
385 const __le32 *key = (const __le32 *)in_key;
386 u32 *flags = &tfm->crt_flags;
387
388 /* encryption schedule */
389
390 ctx->ekey[0] = ss[0] = le32_to_cpu(key[0]);
391 ctx->ekey[1] = ss[1] = le32_to_cpu(key[1]);
392 ctx->ekey[2] = ss[2] = le32_to_cpu(key[2]);
393 ctx->ekey[3] = ss[3] = le32_to_cpu(key[3]);
394
395 switch(key_len) {
396 case 16:
397 for (i = 0; i < 9; i++)
398 ke4(ctx->ekey, i);
399 kel4(ctx->ekey, 9);
400 ctx->rounds = 10;
401 break;
402
403 case 24:
404 ctx->ekey[4] = ss[4] = le32_to_cpu(key[4]);
405 ctx->ekey[5] = ss[5] = le32_to_cpu(key[5]);
406 for (i = 0; i < 7; i++)
407 ke6(ctx->ekey, i);
408 kel6(ctx->ekey, 7);
409 ctx->rounds = 12;
410 break;
411
412 case 32:
413 ctx->ekey[4] = ss[4] = le32_to_cpu(key[4]);
414 ctx->ekey[5] = ss[5] = le32_to_cpu(key[5]);
415 ctx->ekey[6] = ss[6] = le32_to_cpu(key[6]);
416 ctx->ekey[7] = ss[7] = le32_to_cpu(key[7]);
417 for (i = 0; i < 6; i++)
418 ke8(ctx->ekey, i);
419 kel8(ctx->ekey, 6);
420 ctx->rounds = 14;
421 break;
422
423 default:
424 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
425 return -EINVAL;
426 }
427
428 /* decryption schedule */
429
430 ctx->dkey[0] = ss[0] = le32_to_cpu(key[0]);
431 ctx->dkey[1] = ss[1] = le32_to_cpu(key[1]);
432 ctx->dkey[2] = ss[2] = le32_to_cpu(key[2]);
433 ctx->dkey[3] = ss[3] = le32_to_cpu(key[3]);
434
435 switch (key_len) {
436 case 16:
437 kdf4(ctx->dkey, 0);
438 for (i = 1; i < 9; i++)
439 kd4(ctx->dkey, i);
440 kdl4(ctx->dkey, 9);
441 break;
442
443 case 24:
444 ctx->dkey[4] = ff(ss[4] = le32_to_cpu(key[4]));
445 ctx->dkey[5] = ff(ss[5] = le32_to_cpu(key[5]));
446 kdf6(ctx->dkey, 0);
447 for (i = 1; i < 7; i++)
448 kd6(ctx->dkey, i);
449 kdl6(ctx->dkey, 7);
450 break;
451
452 case 32:
453 ctx->dkey[4] = ff(ss[4] = le32_to_cpu(key[4]));
454 ctx->dkey[5] = ff(ss[5] = le32_to_cpu(key[5]));
455 ctx->dkey[6] = ff(ss[6] = le32_to_cpu(key[6]));
456 ctx->dkey[7] = ff(ss[7] = le32_to_cpu(key[7]));
457 kdf8(ctx->dkey, 0);
458 for (i = 1; i < 6; i++)
459 kd8(ctx->dkey, i);
460 kdl8(ctx->dkey, 6);
461 break;
462 }
463 return 0;
464}
465
466static void aes_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) 12static void aes_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
467{ 13{
468 aes_enc_blk(tfm, dst, src); 14 aes_enc_blk(tfm, dst, src);
@@ -479,14 +25,14 @@ static struct crypto_alg aes_alg = {
479 .cra_priority = 200, 25 .cra_priority = 200,
480 .cra_flags = CRYPTO_ALG_TYPE_CIPHER, 26 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
481 .cra_blocksize = AES_BLOCK_SIZE, 27 .cra_blocksize = AES_BLOCK_SIZE,
482 .cra_ctxsize = sizeof(struct aes_ctx), 28 .cra_ctxsize = sizeof(struct crypto_aes_ctx),
483 .cra_module = THIS_MODULE, 29 .cra_module = THIS_MODULE,
484 .cra_list = LIST_HEAD_INIT(aes_alg.cra_list), 30 .cra_list = LIST_HEAD_INIT(aes_alg.cra_list),
485 .cra_u = { 31 .cra_u = {
486 .cipher = { 32 .cipher = {
487 .cia_min_keysize = AES_MIN_KEY_SIZE, 33 .cia_min_keysize = AES_MIN_KEY_SIZE,
488 .cia_max_keysize = AES_MAX_KEY_SIZE, 34 .cia_max_keysize = AES_MAX_KEY_SIZE,
489 .cia_setkey = aes_set_key, 35 .cia_setkey = crypto_aes_set_key,
490 .cia_encrypt = aes_encrypt, 36 .cia_encrypt = aes_encrypt,
491 .cia_decrypt = aes_decrypt 37 .cia_decrypt = aes_decrypt
492 } 38 }
@@ -495,7 +41,6 @@ static struct crypto_alg aes_alg = {
495 41
496static int __init aes_init(void) 42static int __init aes_init(void)
497{ 43{
498 gen_tabs();
499 return crypto_register_alg(&aes_alg); 44 return crypto_register_alg(&aes_alg);
500} 45}
501 46