crypto: testmgr - add xts(serpent) test vectors
[linux-3.10.git] / crypto / serpent.c
1 /*
2  * Cryptographic API.
3  *
4  * Serpent Cipher Algorithm.
5  *
6  * Copyright (C) 2002 Dag Arne Osvik <osvik@ii.uib.no>
7  *               2003 Herbert Valerio Riedel <hvr@gnu.org>
8  *
9  * Added tnepres support: Ruben Jesus Garcia Hernandez <ruben@ugr.es>, 18.10.2004
10  *               Based on code by hvr
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  */
17
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/errno.h>
21 #include <asm/byteorder.h>
22 #include <linux/crypto.h>
23 #include <linux/types.h>
24 #include <crypto/serpent.h>
25
26 /* Key is padded to the maximum of 256 bits before round key generation.
27  * Any key length <= 256 bits (32 bytes) is allowed by the algorithm.
28  */
29
30 #define PHI 0x9e3779b9UL
31
32 #define keyiter(a,b,c,d,i,j) \
33         b ^= d; b ^= c; b ^= a; b ^= PHI ^ i; b = rol32(b,11); k[j] = b;
34
35 #define loadkeys(x0,x1,x2,x3,i) \
36         x0=k[i]; x1=k[i+1]; x2=k[i+2]; x3=k[i+3];
37
38 #define storekeys(x0,x1,x2,x3,i) \
39         k[i]=x0; k[i+1]=x1; k[i+2]=x2; k[i+3]=x3;
40
41 #define K(x0,x1,x2,x3,i)                                \
42         x3 ^= k[4*(i)+3];        x2 ^= k[4*(i)+2];      \
43         x1 ^= k[4*(i)+1];        x0 ^= k[4*(i)+0];
44
45 #define LK(x0,x1,x2,x3,x4,i)                            \
46                                         x0=rol32(x0,13);\
47         x2=rol32(x2,3); x1 ^= x0;       x4  = x0 << 3;  \
48         x3 ^= x2;       x1 ^= x2;                       \
49         x1=rol32(x1,1); x3 ^= x4;                       \
50         x3=rol32(x3,7); x4  = x1;                       \
51         x0 ^= x1;       x4 <<= 7;       x2 ^= x3;       \
52         x0 ^= x3;       x2 ^= x4;       x3 ^= k[4*i+3]; \
53         x1 ^= k[4*i+1]; x0=rol32(x0,5); x2=rol32(x2,22);\
54         x0 ^= k[4*i+0]; x2 ^= k[4*i+2];
55
56 #define KL(x0,x1,x2,x3,x4,i)                            \
57         x0 ^= k[4*i+0]; x1 ^= k[4*i+1]; x2 ^= k[4*i+2]; \
58         x3 ^= k[4*i+3]; x0=ror32(x0,5); x2=ror32(x2,22);\
59         x4 =  x1;       x2 ^= x3;       x0 ^= x3;       \
60         x4 <<= 7;       x0 ^= x1;       x1=ror32(x1,1); \
61         x2 ^= x4;       x3=ror32(x3,7); x4 = x0 << 3;   \
62         x1 ^= x0;       x3 ^= x4;       x0=ror32(x0,13);\
63         x1 ^= x2;       x3 ^= x2;       x2=ror32(x2,3);
64
65 #define S0(x0,x1,x2,x3,x4)                              \
66                                         x4  = x3;       \
67         x3 |= x0;       x0 ^= x4;       x4 ^= x2;       \
68         x4 =~ x4;       x3 ^= x1;       x1 &= x0;       \
69         x1 ^= x4;       x2 ^= x0;       x0 ^= x3;       \
70         x4 |= x0;       x0 ^= x2;       x2 &= x1;       \
71         x3 ^= x2;       x1 =~ x1;       x2 ^= x4;       \
72         x1 ^= x2;
73
74 #define S1(x0,x1,x2,x3,x4)                              \
75                                         x4  = x1;       \
76         x1 ^= x0;       x0 ^= x3;       x3 =~ x3;       \
77         x4 &= x1;       x0 |= x1;       x3 ^= x2;       \
78         x0 ^= x3;       x1 ^= x3;       x3 ^= x4;       \
79         x1 |= x4;       x4 ^= x2;       x2 &= x0;       \
80         x2 ^= x1;       x1 |= x0;       x0 =~ x0;       \
81         x0 ^= x2;       x4 ^= x1;
82
83 #define S2(x0,x1,x2,x3,x4)                              \
84                                         x3 =~ x3;       \
85         x1 ^= x0;       x4  = x0;       x0 &= x2;       \
86         x0 ^= x3;       x3 |= x4;       x2 ^= x1;       \
87         x3 ^= x1;       x1 &= x0;       x0 ^= x2;       \
88         x2 &= x3;       x3 |= x1;       x0 =~ x0;       \
89         x3 ^= x0;       x4 ^= x0;       x0 ^= x2;       \
90         x1 |= x2;
91
92 #define S3(x0,x1,x2,x3,x4)                              \
93                                         x4  = x1;       \
94         x1 ^= x3;       x3 |= x0;       x4 &= x0;       \
95         x0 ^= x2;       x2 ^= x1;       x1 &= x3;       \
96         x2 ^= x3;       x0 |= x4;       x4 ^= x3;       \
97         x1 ^= x0;       x0 &= x3;       x3 &= x4;       \
98         x3 ^= x2;       x4 |= x1;       x2 &= x1;       \
99         x4 ^= x3;       x0 ^= x3;       x3 ^= x2;
100
101 #define S4(x0,x1,x2,x3,x4)                              \
102                                         x4  = x3;       \
103         x3 &= x0;       x0 ^= x4;                       \
104         x3 ^= x2;       x2 |= x4;       x0 ^= x1;       \
105         x4 ^= x3;       x2 |= x0;                       \
106         x2 ^= x1;       x1 &= x0;                       \
107         x1 ^= x4;       x4 &= x2;       x2 ^= x3;       \
108         x4 ^= x0;       x3 |= x1;       x1 =~ x1;       \
109         x3 ^= x0;
110
111 #define S5(x0,x1,x2,x3,x4)                              \
112         x4  = x1;       x1 |= x0;                       \
113         x2 ^= x1;       x3 =~ x3;       x4 ^= x0;       \
114         x0 ^= x2;       x1 &= x4;       x4 |= x3;       \
115         x4 ^= x0;       x0 &= x3;       x1 ^= x3;       \
116         x3 ^= x2;       x0 ^= x1;       x2 &= x4;       \
117         x1 ^= x2;       x2 &= x0;                       \
118         x3 ^= x2;
119
120 #define S6(x0,x1,x2,x3,x4)                              \
121                                         x4  = x1;       \
122         x3 ^= x0;       x1 ^= x2;       x2 ^= x0;       \
123         x0 &= x3;       x1 |= x3;       x4 =~ x4;       \
124         x0 ^= x1;       x1 ^= x2;                       \
125         x3 ^= x4;       x4 ^= x0;       x2 &= x0;       \
126         x4 ^= x1;       x2 ^= x3;       x3 &= x1;       \
127         x3 ^= x0;       x1 ^= x2;
128
129 #define S7(x0,x1,x2,x3,x4)                              \
130                                         x1 =~ x1;       \
131         x4  = x1;       x0 =~ x0;       x1 &= x2;       \
132         x1 ^= x3;       x3 |= x4;       x4 ^= x2;       \
133         x2 ^= x3;       x3 ^= x0;       x0 |= x1;       \
134         x2 &= x0;       x0 ^= x4;       x4 ^= x3;       \
135         x3 &= x0;       x4 ^= x1;                       \
136         x2 ^= x4;       x3 ^= x1;       x4 |= x0;       \
137         x4 ^= x1;
138
139 #define SI0(x0,x1,x2,x3,x4)                             \
140                         x4  = x3;       x1 ^= x0;       \
141         x3 |= x1;       x4 ^= x1;       x0 =~ x0;       \
142         x2 ^= x3;       x3 ^= x0;       x0 &= x1;       \
143         x0 ^= x2;       x2 &= x3;       x3 ^= x4;       \
144         x2 ^= x3;       x1 ^= x3;       x3 &= x0;       \
145         x1 ^= x0;       x0 ^= x2;       x4 ^= x3;
146
147 #define SI1(x0,x1,x2,x3,x4)                             \
148         x1 ^= x3;       x4  = x0;                       \
149         x0 ^= x2;       x2 =~ x2;       x4 |= x1;       \
150         x4 ^= x3;       x3 &= x1;       x1 ^= x2;       \
151         x2 &= x4;       x4 ^= x1;       x1 |= x3;       \
152         x3 ^= x0;       x2 ^= x0;       x0 |= x4;       \
153         x2 ^= x4;       x1 ^= x0;                       \
154         x4 ^= x1;
155
156 #define SI2(x0,x1,x2,x3,x4)                             \
157         x2 ^= x1;       x4  = x3;       x3 =~ x3;       \
158         x3 |= x2;       x2 ^= x4;       x4 ^= x0;       \
159         x3 ^= x1;       x1 |= x2;       x2 ^= x0;       \
160         x1 ^= x4;       x4 |= x3;       x2 ^= x3;       \
161         x4 ^= x2;       x2 &= x1;                       \
162         x2 ^= x3;       x3 ^= x4;       x4 ^= x0;
163
164 #define SI3(x0,x1,x2,x3,x4)                             \
165                                         x2 ^= x1;       \
166         x4  = x1;       x1 &= x2;                       \
167         x1 ^= x0;       x0 |= x4;       x4 ^= x3;       \
168         x0 ^= x3;       x3 |= x1;       x1 ^= x2;       \
169         x1 ^= x3;       x0 ^= x2;       x2 ^= x3;       \
170         x3 &= x1;       x1 ^= x0;       x0 &= x2;       \
171         x4 ^= x3;       x3 ^= x0;       x0 ^= x1;
172
173 #define SI4(x0,x1,x2,x3,x4)                             \
174         x2 ^= x3;       x4  = x0;       x0 &= x1;       \
175         x0 ^= x2;       x2 |= x3;       x4 =~ x4;       \
176         x1 ^= x0;       x0 ^= x2;       x2 &= x4;       \
177         x2 ^= x0;       x0 |= x4;                       \
178         x0 ^= x3;       x3 &= x2;                       \
179         x4 ^= x3;       x3 ^= x1;       x1 &= x0;       \
180         x4 ^= x1;       x0 ^= x3;
181
182 #define SI5(x0,x1,x2,x3,x4)                             \
183                         x4  = x1;       x1 |= x2;       \
184         x2 ^= x4;       x1 ^= x3;       x3 &= x4;       \
185         x2 ^= x3;       x3 |= x0;       x0 =~ x0;       \
186         x3 ^= x2;       x2 |= x0;       x4 ^= x1;       \
187         x2 ^= x4;       x4 &= x0;       x0 ^= x1;       \
188         x1 ^= x3;       x0 &= x2;       x2 ^= x3;       \
189         x0 ^= x2;       x2 ^= x4;       x4 ^= x3;
190
191 #define SI6(x0,x1,x2,x3,x4)                             \
192                         x0 ^= x2;                       \
193         x4  = x0;       x0 &= x3;       x2 ^= x3;       \
194         x0 ^= x2;       x3 ^= x1;       x2 |= x4;       \
195         x2 ^= x3;       x3 &= x0;       x0 =~ x0;       \
196         x3 ^= x1;       x1 &= x2;       x4 ^= x0;       \
197         x3 ^= x4;       x4 ^= x2;       x0 ^= x1;       \
198         x2 ^= x0;
199
200 #define SI7(x0,x1,x2,x3,x4)                             \
201         x4  = x3;       x3 &= x0;       x0 ^= x2;       \
202         x2 |= x4;       x4 ^= x1;       x0 =~ x0;       \
203         x1 |= x3;       x4 ^= x0;       x0 &= x2;       \
204         x0 ^= x1;       x1 &= x2;       x3 ^= x2;       \
205         x4 ^= x3;       x2 &= x3;       x3 |= x0;       \
206         x1 ^= x4;       x3 ^= x4;       x4 &= x0;       \
207         x4 ^= x2;
208
209 int serpent_setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
210 {
211         struct serpent_ctx *ctx = crypto_tfm_ctx(tfm);
212         u32 *k = ctx->expkey;
213         u8  *k8 = (u8 *)k;
214         u32 r0,r1,r2,r3,r4;
215         int i;
216
217         /* Copy key, add padding */
218
219         for (i = 0; i < keylen; ++i)
220                 k8[i] = key[i];
221         if (i < SERPENT_MAX_KEY_SIZE)
222                 k8[i++] = 1;
223         while (i < SERPENT_MAX_KEY_SIZE)
224                 k8[i++] = 0;
225
226         /* Expand key using polynomial */
227
228         r0 = le32_to_cpu(k[3]);
229         r1 = le32_to_cpu(k[4]);
230         r2 = le32_to_cpu(k[5]);
231         r3 = le32_to_cpu(k[6]);
232         r4 = le32_to_cpu(k[7]);
233
234         keyiter(le32_to_cpu(k[0]),r0,r4,r2,0,0);
235         keyiter(le32_to_cpu(k[1]),r1,r0,r3,1,1);
236         keyiter(le32_to_cpu(k[2]),r2,r1,r4,2,2);
237         keyiter(le32_to_cpu(k[3]),r3,r2,r0,3,3);
238         keyiter(le32_to_cpu(k[4]),r4,r3,r1,4,4);
239         keyiter(le32_to_cpu(k[5]),r0,r4,r2,5,5);
240         keyiter(le32_to_cpu(k[6]),r1,r0,r3,6,6);
241         keyiter(le32_to_cpu(k[7]),r2,r1,r4,7,7);
242
243         keyiter(k[  0],r3,r2,r0,  8,  8); keyiter(k[  1],r4,r3,r1,  9,  9);
244         keyiter(k[  2],r0,r4,r2, 10, 10); keyiter(k[  3],r1,r0,r3, 11, 11);
245         keyiter(k[  4],r2,r1,r4, 12, 12); keyiter(k[  5],r3,r2,r0, 13, 13);
246         keyiter(k[  6],r4,r3,r1, 14, 14); keyiter(k[  7],r0,r4,r2, 15, 15);
247         keyiter(k[  8],r1,r0,r3, 16, 16); keyiter(k[  9],r2,r1,r4, 17, 17);
248         keyiter(k[ 10],r3,r2,r0, 18, 18); keyiter(k[ 11],r4,r3,r1, 19, 19);
249         keyiter(k[ 12],r0,r4,r2, 20, 20); keyiter(k[ 13],r1,r0,r3, 21, 21);
250         keyiter(k[ 14],r2,r1,r4, 22, 22); keyiter(k[ 15],r3,r2,r0, 23, 23);
251         keyiter(k[ 16],r4,r3,r1, 24, 24); keyiter(k[ 17],r0,r4,r2, 25, 25);
252         keyiter(k[ 18],r1,r0,r3, 26, 26); keyiter(k[ 19],r2,r1,r4, 27, 27);
253         keyiter(k[ 20],r3,r2,r0, 28, 28); keyiter(k[ 21],r4,r3,r1, 29, 29);
254         keyiter(k[ 22],r0,r4,r2, 30, 30); keyiter(k[ 23],r1,r0,r3, 31, 31);
255
256         k += 50;
257
258         keyiter(k[-26],r2,r1,r4, 32,-18); keyiter(k[-25],r3,r2,r0, 33,-17);
259         keyiter(k[-24],r4,r3,r1, 34,-16); keyiter(k[-23],r0,r4,r2, 35,-15);
260         keyiter(k[-22],r1,r0,r3, 36,-14); keyiter(k[-21],r2,r1,r4, 37,-13);
261         keyiter(k[-20],r3,r2,r0, 38,-12); keyiter(k[-19],r4,r3,r1, 39,-11);
262         keyiter(k[-18],r0,r4,r2, 40,-10); keyiter(k[-17],r1,r0,r3, 41, -9);
263         keyiter(k[-16],r2,r1,r4, 42, -8); keyiter(k[-15],r3,r2,r0, 43, -7);
264         keyiter(k[-14],r4,r3,r1, 44, -6); keyiter(k[-13],r0,r4,r2, 45, -5);
265         keyiter(k[-12],r1,r0,r3, 46, -4); keyiter(k[-11],r2,r1,r4, 47, -3);
266         keyiter(k[-10],r3,r2,r0, 48, -2); keyiter(k[ -9],r4,r3,r1, 49, -1);
267         keyiter(k[ -8],r0,r4,r2, 50,  0); keyiter(k[ -7],r1,r0,r3, 51,  1);
268         keyiter(k[ -6],r2,r1,r4, 52,  2); keyiter(k[ -5],r3,r2,r0, 53,  3);
269         keyiter(k[ -4],r4,r3,r1, 54,  4); keyiter(k[ -3],r0,r4,r2, 55,  5);
270         keyiter(k[ -2],r1,r0,r3, 56,  6); keyiter(k[ -1],r2,r1,r4, 57,  7);
271         keyiter(k[  0],r3,r2,r0, 58,  8); keyiter(k[  1],r4,r3,r1, 59,  9);
272         keyiter(k[  2],r0,r4,r2, 60, 10); keyiter(k[  3],r1,r0,r3, 61, 11);
273         keyiter(k[  4],r2,r1,r4, 62, 12); keyiter(k[  5],r3,r2,r0, 63, 13);
274         keyiter(k[  6],r4,r3,r1, 64, 14); keyiter(k[  7],r0,r4,r2, 65, 15);
275         keyiter(k[  8],r1,r0,r3, 66, 16); keyiter(k[  9],r2,r1,r4, 67, 17);
276         keyiter(k[ 10],r3,r2,r0, 68, 18); keyiter(k[ 11],r4,r3,r1, 69, 19);
277         keyiter(k[ 12],r0,r4,r2, 70, 20); keyiter(k[ 13],r1,r0,r3, 71, 21);
278         keyiter(k[ 14],r2,r1,r4, 72, 22); keyiter(k[ 15],r3,r2,r0, 73, 23);
279         keyiter(k[ 16],r4,r3,r1, 74, 24); keyiter(k[ 17],r0,r4,r2, 75, 25);
280         keyiter(k[ 18],r1,r0,r3, 76, 26); keyiter(k[ 19],r2,r1,r4, 77, 27);
281         keyiter(k[ 20],r3,r2,r0, 78, 28); keyiter(k[ 21],r4,r3,r1, 79, 29);
282         keyiter(k[ 22],r0,r4,r2, 80, 30); keyiter(k[ 23],r1,r0,r3, 81, 31);
283
284         k += 50;
285
286         keyiter(k[-26],r2,r1,r4, 82,-18); keyiter(k[-25],r3,r2,r0, 83,-17);
287         keyiter(k[-24],r4,r3,r1, 84,-16); keyiter(k[-23],r0,r4,r2, 85,-15);
288         keyiter(k[-22],r1,r0,r3, 86,-14); keyiter(k[-21],r2,r1,r4, 87,-13);
289         keyiter(k[-20],r3,r2,r0, 88,-12); keyiter(k[-19],r4,r3,r1, 89,-11);
290         keyiter(k[-18],r0,r4,r2, 90,-10); keyiter(k[-17],r1,r0,r3, 91, -9);
291         keyiter(k[-16],r2,r1,r4, 92, -8); keyiter(k[-15],r3,r2,r0, 93, -7);
292         keyiter(k[-14],r4,r3,r1, 94, -6); keyiter(k[-13],r0,r4,r2, 95, -5);
293         keyiter(k[-12],r1,r0,r3, 96, -4); keyiter(k[-11],r2,r1,r4, 97, -3);
294         keyiter(k[-10],r3,r2,r0, 98, -2); keyiter(k[ -9],r4,r3,r1, 99, -1);
295         keyiter(k[ -8],r0,r4,r2,100,  0); keyiter(k[ -7],r1,r0,r3,101,  1);
296         keyiter(k[ -6],r2,r1,r4,102,  2); keyiter(k[ -5],r3,r2,r0,103,  3);
297         keyiter(k[ -4],r4,r3,r1,104,  4); keyiter(k[ -3],r0,r4,r2,105,  5);
298         keyiter(k[ -2],r1,r0,r3,106,  6); keyiter(k[ -1],r2,r1,r4,107,  7);
299         keyiter(k[  0],r3,r2,r0,108,  8); keyiter(k[  1],r4,r3,r1,109,  9);
300         keyiter(k[  2],r0,r4,r2,110, 10); keyiter(k[  3],r1,r0,r3,111, 11);
301         keyiter(k[  4],r2,r1,r4,112, 12); keyiter(k[  5],r3,r2,r0,113, 13);
302         keyiter(k[  6],r4,r3,r1,114, 14); keyiter(k[  7],r0,r4,r2,115, 15);
303         keyiter(k[  8],r1,r0,r3,116, 16); keyiter(k[  9],r2,r1,r4,117, 17);
304         keyiter(k[ 10],r3,r2,r0,118, 18); keyiter(k[ 11],r4,r3,r1,119, 19);
305         keyiter(k[ 12],r0,r4,r2,120, 20); keyiter(k[ 13],r1,r0,r3,121, 21);
306         keyiter(k[ 14],r2,r1,r4,122, 22); keyiter(k[ 15],r3,r2,r0,123, 23);
307         keyiter(k[ 16],r4,r3,r1,124, 24); keyiter(k[ 17],r0,r4,r2,125, 25);
308         keyiter(k[ 18],r1,r0,r3,126, 26); keyiter(k[ 19],r2,r1,r4,127, 27);
309         keyiter(k[ 20],r3,r2,r0,128, 28); keyiter(k[ 21],r4,r3,r1,129, 29);
310         keyiter(k[ 22],r0,r4,r2,130, 30); keyiter(k[ 23],r1,r0,r3,131, 31);
311
312         /* Apply S-boxes */
313
314         S3(r3,r4,r0,r1,r2); storekeys(r1,r2,r4,r3, 28); loadkeys(r1,r2,r4,r3, 24);
315         S4(r1,r2,r4,r3,r0); storekeys(r2,r4,r3,r0, 24); loadkeys(r2,r4,r3,r0, 20);
316         S5(r2,r4,r3,r0,r1); storekeys(r1,r2,r4,r0, 20); loadkeys(r1,r2,r4,r0, 16);
317         S6(r1,r2,r4,r0,r3); storekeys(r4,r3,r2,r0, 16); loadkeys(r4,r3,r2,r0, 12);
318         S7(r4,r3,r2,r0,r1); storekeys(r1,r2,r0,r4, 12); loadkeys(r1,r2,r0,r4,  8);
319         S0(r1,r2,r0,r4,r3); storekeys(r0,r2,r4,r1,  8); loadkeys(r0,r2,r4,r1,  4);
320         S1(r0,r2,r4,r1,r3); storekeys(r3,r4,r1,r0,  4); loadkeys(r3,r4,r1,r0,  0);
321         S2(r3,r4,r1,r0,r2); storekeys(r2,r4,r3,r0,  0); loadkeys(r2,r4,r3,r0, -4);
322         S3(r2,r4,r3,r0,r1); storekeys(r0,r1,r4,r2, -4); loadkeys(r0,r1,r4,r2, -8);
323         S4(r0,r1,r4,r2,r3); storekeys(r1,r4,r2,r3, -8); loadkeys(r1,r4,r2,r3,-12);
324         S5(r1,r4,r2,r3,r0); storekeys(r0,r1,r4,r3,-12); loadkeys(r0,r1,r4,r3,-16);
325         S6(r0,r1,r4,r3,r2); storekeys(r4,r2,r1,r3,-16); loadkeys(r4,r2,r1,r3,-20);
326         S7(r4,r2,r1,r3,r0); storekeys(r0,r1,r3,r4,-20); loadkeys(r0,r1,r3,r4,-24);
327         S0(r0,r1,r3,r4,r2); storekeys(r3,r1,r4,r0,-24); loadkeys(r3,r1,r4,r0,-28);
328         k -= 50;
329         S1(r3,r1,r4,r0,r2); storekeys(r2,r4,r0,r3, 22); loadkeys(r2,r4,r0,r3, 18);
330         S2(r2,r4,r0,r3,r1); storekeys(r1,r4,r2,r3, 18); loadkeys(r1,r4,r2,r3, 14);
331         S3(r1,r4,r2,r3,r0); storekeys(r3,r0,r4,r1, 14); loadkeys(r3,r0,r4,r1, 10);
332         S4(r3,r0,r4,r1,r2); storekeys(r0,r4,r1,r2, 10); loadkeys(r0,r4,r1,r2,  6);
333         S5(r0,r4,r1,r2,r3); storekeys(r3,r0,r4,r2,  6); loadkeys(r3,r0,r4,r2,  2);
334         S6(r3,r0,r4,r2,r1); storekeys(r4,r1,r0,r2,  2); loadkeys(r4,r1,r0,r2, -2);
335         S7(r4,r1,r0,r2,r3); storekeys(r3,r0,r2,r4, -2); loadkeys(r3,r0,r2,r4, -6);
336         S0(r3,r0,r2,r4,r1); storekeys(r2,r0,r4,r3, -6); loadkeys(r2,r0,r4,r3,-10);
337         S1(r2,r0,r4,r3,r1); storekeys(r1,r4,r3,r2,-10); loadkeys(r1,r4,r3,r2,-14);
338         S2(r1,r4,r3,r2,r0); storekeys(r0,r4,r1,r2,-14); loadkeys(r0,r4,r1,r2,-18);
339         S3(r0,r4,r1,r2,r3); storekeys(r2,r3,r4,r0,-18); loadkeys(r2,r3,r4,r0,-22);
340         k -= 50;
341         S4(r2,r3,r4,r0,r1); storekeys(r3,r4,r0,r1, 28); loadkeys(r3,r4,r0,r1, 24);
342         S5(r3,r4,r0,r1,r2); storekeys(r2,r3,r4,r1, 24); loadkeys(r2,r3,r4,r1, 20);
343         S6(r2,r3,r4,r1,r0); storekeys(r4,r0,r3,r1, 20); loadkeys(r4,r0,r3,r1, 16);
344         S7(r4,r0,r3,r1,r2); storekeys(r2,r3,r1,r4, 16); loadkeys(r2,r3,r1,r4, 12);
345         S0(r2,r3,r1,r4,r0); storekeys(r1,r3,r4,r2, 12); loadkeys(r1,r3,r4,r2,  8);
346         S1(r1,r3,r4,r2,r0); storekeys(r0,r4,r2,r1,  8); loadkeys(r0,r4,r2,r1,  4);
347         S2(r0,r4,r2,r1,r3); storekeys(r3,r4,r0,r1,  4); loadkeys(r3,r4,r0,r1,  0);
348         S3(r3,r4,r0,r1,r2); storekeys(r1,r2,r4,r3,  0);
349
350         return 0;
351 }
352 EXPORT_SYMBOL_GPL(serpent_setkey);
353
354 void __serpent_encrypt(struct serpent_ctx *ctx, u8 *dst, const u8 *src)
355 {
356         const u32 *k = ctx->expkey;
357         const __le32 *s = (const __le32 *)src;
358         __le32  *d = (__le32 *)dst;
359         u32     r0, r1, r2, r3, r4;
360
361 /*
362  * Note: The conversions between u8* and u32* might cause trouble
363  * on architectures with stricter alignment rules than x86
364  */
365
366         r0 = le32_to_cpu(s[0]);
367         r1 = le32_to_cpu(s[1]);
368         r2 = le32_to_cpu(s[2]);
369         r3 = le32_to_cpu(s[3]);
370
371                                  K(r0,r1,r2,r3,0);
372         S0(r0,r1,r2,r3,r4);     LK(r2,r1,r3,r0,r4,1);
373         S1(r2,r1,r3,r0,r4);     LK(r4,r3,r0,r2,r1,2);
374         S2(r4,r3,r0,r2,r1);     LK(r1,r3,r4,r2,r0,3);
375         S3(r1,r3,r4,r2,r0);     LK(r2,r0,r3,r1,r4,4);
376         S4(r2,r0,r3,r1,r4);     LK(r0,r3,r1,r4,r2,5);
377         S5(r0,r3,r1,r4,r2);     LK(r2,r0,r3,r4,r1,6);
378         S6(r2,r0,r3,r4,r1);     LK(r3,r1,r0,r4,r2,7);
379         S7(r3,r1,r0,r4,r2);     LK(r2,r0,r4,r3,r1,8);
380         S0(r2,r0,r4,r3,r1);     LK(r4,r0,r3,r2,r1,9);
381         S1(r4,r0,r3,r2,r1);     LK(r1,r3,r2,r4,r0,10);
382         S2(r1,r3,r2,r4,r0);     LK(r0,r3,r1,r4,r2,11);
383         S3(r0,r3,r1,r4,r2);     LK(r4,r2,r3,r0,r1,12);
384         S4(r4,r2,r3,r0,r1);     LK(r2,r3,r0,r1,r4,13);
385         S5(r2,r3,r0,r1,r4);     LK(r4,r2,r3,r1,r0,14);
386         S6(r4,r2,r3,r1,r0);     LK(r3,r0,r2,r1,r4,15);
387         S7(r3,r0,r2,r1,r4);     LK(r4,r2,r1,r3,r0,16);
388         S0(r4,r2,r1,r3,r0);     LK(r1,r2,r3,r4,r0,17);
389         S1(r1,r2,r3,r4,r0);     LK(r0,r3,r4,r1,r2,18);
390         S2(r0,r3,r4,r1,r2);     LK(r2,r3,r0,r1,r4,19);
391         S3(r2,r3,r0,r1,r4);     LK(r1,r4,r3,r2,r0,20);
392         S4(r1,r4,r3,r2,r0);     LK(r4,r3,r2,r0,r1,21);
393         S5(r4,r3,r2,r0,r1);     LK(r1,r4,r3,r0,r2,22);
394         S6(r1,r4,r3,r0,r2);     LK(r3,r2,r4,r0,r1,23);
395         S7(r3,r2,r4,r0,r1);     LK(r1,r4,r0,r3,r2,24);
396         S0(r1,r4,r0,r3,r2);     LK(r0,r4,r3,r1,r2,25);
397         S1(r0,r4,r3,r1,r2);     LK(r2,r3,r1,r0,r4,26);
398         S2(r2,r3,r1,r0,r4);     LK(r4,r3,r2,r0,r1,27);
399         S3(r4,r3,r2,r0,r1);     LK(r0,r1,r3,r4,r2,28);
400         S4(r0,r1,r3,r4,r2);     LK(r1,r3,r4,r2,r0,29);
401         S5(r1,r3,r4,r2,r0);     LK(r0,r1,r3,r2,r4,30);
402         S6(r0,r1,r3,r2,r4);     LK(r3,r4,r1,r2,r0,31);
403         S7(r3,r4,r1,r2,r0);      K(r0,r1,r2,r3,32);
404
405         d[0] = cpu_to_le32(r0);
406         d[1] = cpu_to_le32(r1);
407         d[2] = cpu_to_le32(r2);
408         d[3] = cpu_to_le32(r3);
409 }
410 EXPORT_SYMBOL_GPL(__serpent_encrypt);
411
412 static void serpent_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
413 {
414         struct serpent_ctx *ctx = crypto_tfm_ctx(tfm);
415
416         __serpent_encrypt(ctx, dst, src);
417 }
418
419 void __serpent_decrypt(struct serpent_ctx *ctx, u8 *dst, const u8 *src)
420 {
421         const u32 *k = ctx->expkey;
422         const __le32 *s = (const __le32 *)src;
423         __le32  *d = (__le32 *)dst;
424         u32     r0, r1, r2, r3, r4;
425
426         r0 = le32_to_cpu(s[0]);
427         r1 = le32_to_cpu(s[1]);
428         r2 = le32_to_cpu(s[2]);
429         r3 = le32_to_cpu(s[3]);
430
431                                 K(r0,r1,r2,r3,32);
432         SI7(r0,r1,r2,r3,r4);    KL(r1,r3,r0,r4,r2,31);
433         SI6(r1,r3,r0,r4,r2);    KL(r0,r2,r4,r1,r3,30);
434         SI5(r0,r2,r4,r1,r3);    KL(r2,r3,r0,r4,r1,29);
435         SI4(r2,r3,r0,r4,r1);    KL(r2,r0,r1,r4,r3,28);
436         SI3(r2,r0,r1,r4,r3);    KL(r1,r2,r3,r4,r0,27);
437         SI2(r1,r2,r3,r4,r0);    KL(r2,r0,r4,r3,r1,26);
438         SI1(r2,r0,r4,r3,r1);    KL(r1,r0,r4,r3,r2,25);
439         SI0(r1,r0,r4,r3,r2);    KL(r4,r2,r0,r1,r3,24);
440         SI7(r4,r2,r0,r1,r3);    KL(r2,r1,r4,r3,r0,23);
441         SI6(r2,r1,r4,r3,r0);    KL(r4,r0,r3,r2,r1,22);
442         SI5(r4,r0,r3,r2,r1);    KL(r0,r1,r4,r3,r2,21);
443         SI4(r0,r1,r4,r3,r2);    KL(r0,r4,r2,r3,r1,20);
444         SI3(r0,r4,r2,r3,r1);    KL(r2,r0,r1,r3,r4,19);
445         SI2(r2,r0,r1,r3,r4);    KL(r0,r4,r3,r1,r2,18);
446         SI1(r0,r4,r3,r1,r2);    KL(r2,r4,r3,r1,r0,17);
447         SI0(r2,r4,r3,r1,r0);    KL(r3,r0,r4,r2,r1,16);
448         SI7(r3,r0,r4,r2,r1);    KL(r0,r2,r3,r1,r4,15);
449         SI6(r0,r2,r3,r1,r4);    KL(r3,r4,r1,r0,r2,14);
450         SI5(r3,r4,r1,r0,r2);    KL(r4,r2,r3,r1,r0,13);
451         SI4(r4,r2,r3,r1,r0);    KL(r4,r3,r0,r1,r2,12);
452         SI3(r4,r3,r0,r1,r2);    KL(r0,r4,r2,r1,r3,11);
453         SI2(r0,r4,r2,r1,r3);    KL(r4,r3,r1,r2,r0,10);
454         SI1(r4,r3,r1,r2,r0);    KL(r0,r3,r1,r2,r4,9);
455         SI0(r0,r3,r1,r2,r4);    KL(r1,r4,r3,r0,r2,8);
456         SI7(r1,r4,r3,r0,r2);    KL(r4,r0,r1,r2,r3,7);
457         SI6(r4,r0,r1,r2,r3);    KL(r1,r3,r2,r4,r0,6);
458         SI5(r1,r3,r2,r4,r0);    KL(r3,r0,r1,r2,r4,5);
459         SI4(r3,r0,r1,r2,r4);    KL(r3,r1,r4,r2,r0,4);
460         SI3(r3,r1,r4,r2,r0);    KL(r4,r3,r0,r2,r1,3);
461         SI2(r4,r3,r0,r2,r1);    KL(r3,r1,r2,r0,r4,2);
462         SI1(r3,r1,r2,r0,r4);    KL(r4,r1,r2,r0,r3,1);
463         SI0(r4,r1,r2,r0,r3);    K(r2,r3,r1,r4,0);
464
465         d[0] = cpu_to_le32(r2);
466         d[1] = cpu_to_le32(r3);
467         d[2] = cpu_to_le32(r1);
468         d[3] = cpu_to_le32(r4);
469 }
470 EXPORT_SYMBOL_GPL(__serpent_decrypt);
471
472 static void serpent_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
473 {
474         struct serpent_ctx *ctx = crypto_tfm_ctx(tfm);
475
476         __serpent_decrypt(ctx, dst, src);
477 }
478
479 static struct crypto_alg serpent_alg = {
480         .cra_name               =       "serpent",
481         .cra_driver_name        =       "serpent-generic",
482         .cra_priority           =       100,
483         .cra_flags              =       CRYPTO_ALG_TYPE_CIPHER,
484         .cra_blocksize          =       SERPENT_BLOCK_SIZE,
485         .cra_ctxsize            =       sizeof(struct serpent_ctx),
486         .cra_alignmask          =       3,
487         .cra_module             =       THIS_MODULE,
488         .cra_list               =       LIST_HEAD_INIT(serpent_alg.cra_list),
489         .cra_u                  =       { .cipher = {
490         .cia_min_keysize        =       SERPENT_MIN_KEY_SIZE,
491         .cia_max_keysize        =       SERPENT_MAX_KEY_SIZE,
492         .cia_setkey             =       serpent_setkey,
493         .cia_encrypt            =       serpent_encrypt,
494         .cia_decrypt            =       serpent_decrypt } }
495 };
496
497 static int tnepres_setkey(struct crypto_tfm *tfm, const u8 *key,
498                           unsigned int keylen)
499 {
500         u8 rev_key[SERPENT_MAX_KEY_SIZE];
501         int i;
502
503         for (i = 0; i < keylen; ++i)
504                 rev_key[keylen - i - 1] = key[i];
505  
506         return serpent_setkey(tfm, rev_key, keylen);
507 }
508
509 static void tnepres_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
510 {
511         const u32 * const s = (const u32 * const)src;
512         u32 * const d = (u32 * const)dst;
513
514         u32 rs[4], rd[4];
515
516         rs[0] = swab32(s[3]);
517         rs[1] = swab32(s[2]);
518         rs[2] = swab32(s[1]);
519         rs[3] = swab32(s[0]);
520
521         serpent_encrypt(tfm, (u8 *)rd, (u8 *)rs);
522
523         d[0] = swab32(rd[3]);
524         d[1] = swab32(rd[2]);
525         d[2] = swab32(rd[1]);
526         d[3] = swab32(rd[0]);
527 }
528
529 static void tnepres_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
530 {
531         const u32 * const s = (const u32 * const)src;
532         u32 * const d = (u32 * const)dst;
533
534         u32 rs[4], rd[4];
535
536         rs[0] = swab32(s[3]);
537         rs[1] = swab32(s[2]);
538         rs[2] = swab32(s[1]);
539         rs[3] = swab32(s[0]);
540
541         serpent_decrypt(tfm, (u8 *)rd, (u8 *)rs);
542
543         d[0] = swab32(rd[3]);
544         d[1] = swab32(rd[2]);
545         d[2] = swab32(rd[1]);
546         d[3] = swab32(rd[0]);
547 }
548
549 static struct crypto_alg tnepres_alg = {
550         .cra_name               =       "tnepres",
551         .cra_flags              =       CRYPTO_ALG_TYPE_CIPHER,
552         .cra_blocksize          =       SERPENT_BLOCK_SIZE,
553         .cra_ctxsize            =       sizeof(struct serpent_ctx),
554         .cra_alignmask          =       3,
555         .cra_module             =       THIS_MODULE,
556         .cra_list               =       LIST_HEAD_INIT(serpent_alg.cra_list),
557         .cra_u                  =       { .cipher = {
558         .cia_min_keysize        =       SERPENT_MIN_KEY_SIZE,
559         .cia_max_keysize        =       SERPENT_MAX_KEY_SIZE,
560         .cia_setkey             =       tnepres_setkey,
561         .cia_encrypt            =       tnepres_encrypt,
562         .cia_decrypt            =       tnepres_decrypt } }
563 };
564
565 static int __init serpent_mod_init(void)
566 {
567         int ret = crypto_register_alg(&serpent_alg);
568
569         if (ret)
570                 return ret;
571
572         ret = crypto_register_alg(&tnepres_alg);
573
574         if (ret)
575                 crypto_unregister_alg(&serpent_alg);
576
577         return ret;
578 }
579
580 static void __exit serpent_mod_fini(void)
581 {
582         crypto_unregister_alg(&tnepres_alg);
583         crypto_unregister_alg(&serpent_alg);
584 }
585
586 module_init(serpent_mod_init);
587 module_exit(serpent_mod_fini);
588
589 MODULE_LICENSE("GPL");
590 MODULE_DESCRIPTION("Serpent and tnepres (kerneli compatible serpent reversed) Cipher Algorithm");
591 MODULE_AUTHOR("Dag Arne Osvik <osvik@ii.uib.no>");
592 MODULE_ALIAS("tnepres");
593 MODULE_ALIAS("serpent");