4 * Serpent Cipher Algorithm.
6 * Copyright (C) 2002 Dag Arne Osvik <osvik@ii.uib.no>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/errno.h>
17 #include <asm/byteorder.h>
18 #include <linux/crypto.h>
20 /* Key is padded to the maximum of 256 bits before round key generation.
21 * Any key length <= 256 bits (32 bytes) is allowed by the algorithm.
24 #define SERPENT_MIN_KEY_SIZE 0
25 #define SERPENT_MAX_KEY_SIZE 32
26 #define SERPENT_EXPKEY_WORDS 132
27 #define SERPENT_BLOCK_SIZE 16
29 #define PHI 0x9e3779b9UL
30 #define ROL(x,r) ((x) = ((x) << (r)) | ((x) >> (32-(r))))
31 #define ROR(x,r) ((x) = ((x) >> (r)) | ((x) << (32-(r))))
33 #define keyiter(a,b,c,d,i,j) \
34 b ^= d; b ^= c; b ^= a; b ^= PHI ^ i; ROL(b,11); k[j] = b;
36 #define loadkeys(x0,x1,x2,x3,i) \
37 x0=k[i]; x1=k[i+1]; x2=k[i+2]; x3=k[i+3];
39 #define storekeys(x0,x1,x2,x3,i) \
40 k[i]=x0; k[i+1]=x1; k[i+2]=x2; k[i+3]=x3;
42 #define K(x0,x1,x2,x3,i) \
43 x3 ^= k[4*(i)+3]; x2 ^= k[4*(i)+2]; \
44 x1 ^= k[4*(i)+1]; x0 ^= k[4*(i)+0];
46 #define LK(x0,x1,x2,x3,x4,i) \
48 ROL(x2,3); x1 ^= x0; x4 = x0 << 3; \
50 ROL(x1,1); x3 ^= x4; \
52 x0 ^= x1; x4 <<= 7; x2 ^= x3; \
53 x0 ^= x3; x2 ^= x4; x3 ^= k[4*i+3]; \
54 x1 ^= k[4*i+1]; ROL(x0,5); ROL(x2,22); \
55 x0 ^= k[4*i+0]; x2 ^= k[4*i+2];
57 #define KL(x0,x1,x2,x3,x4,i) \
58 x0 ^= k[4*i+0]; x1 ^= k[4*i+1]; x2 ^= k[4*i+2]; \
59 x3 ^= k[4*i+3]; ROR(x0,5); ROR(x2,22); \
60 x4 = x1; x2 ^= x3; x0 ^= x3; \
61 x4 <<= 7; x0 ^= x1; ROR(x1,1); \
62 x2 ^= x4; ROR(x3,7); x4 = x0 << 3; \
63 x1 ^= x0; x3 ^= x4; ROR(x0,13); \
64 x1 ^= x2; x3 ^= x2; ROR(x2,3);
66 #define S0(x0,x1,x2,x3,x4) \
68 x3 |= x0; x0 ^= x4; x4 ^= x2; \
69 x4 =~ x4; x3 ^= x1; x1 &= x0; \
70 x1 ^= x4; x2 ^= x0; x0 ^= x3; \
71 x4 |= x0; x0 ^= x2; x2 &= x1; \
72 x3 ^= x2; x1 =~ x1; x2 ^= x4; \
75 #define S1(x0,x1,x2,x3,x4) \
77 x1 ^= x0; x0 ^= x3; x3 =~ x3; \
78 x4 &= x1; x0 |= x1; x3 ^= x2; \
79 x0 ^= x3; x1 ^= x3; x3 ^= x4; \
80 x1 |= x4; x4 ^= x2; x2 &= x0; \
81 x2 ^= x1; x1 |= x0; x0 =~ x0; \
84 #define S2(x0,x1,x2,x3,x4) \
86 x1 ^= x0; x4 = x0; x0 &= x2; \
87 x0 ^= x3; x3 |= x4; x2 ^= x1; \
88 x3 ^= x1; x1 &= x0; x0 ^= x2; \
89 x2 &= x3; x3 |= x1; x0 =~ x0; \
90 x3 ^= x0; x4 ^= x0; x0 ^= x2; \
93 #define S3(x0,x1,x2,x3,x4) \
95 x1 ^= x3; x3 |= x0; x4 &= x0; \
96 x0 ^= x2; x2 ^= x1; x1 &= x3; \
97 x2 ^= x3; x0 |= x4; x4 ^= x3; \
98 x1 ^= x0; x0 &= x3; x3 &= x4; \
99 x3 ^= x2; x4 |= x1; x2 &= x1; \
100 x4 ^= x3; x0 ^= x3; x3 ^= x2;
102 #define S4(x0,x1,x2,x3,x4) \
104 x3 &= x0; x0 ^= x4; \
105 x3 ^= x2; x2 |= x4; x0 ^= x1; \
106 x4 ^= x3; x2 |= x0; \
107 x2 ^= x1; x1 &= x0; \
108 x1 ^= x4; x4 &= x2; x2 ^= x3; \
109 x4 ^= x0; x3 |= x1; x1 =~ x1; \
112 #define S5(x0,x1,x2,x3,x4) \
114 x2 ^= x1; x3 =~ x3; x4 ^= x0; \
115 x0 ^= x2; x1 &= x4; x4 |= x3; \
116 x4 ^= x0; x0 &= x3; x1 ^= x3; \
117 x3 ^= x2; x0 ^= x1; x2 &= x4; \
118 x1 ^= x2; x2 &= x0; \
121 #define S6(x0,x1,x2,x3,x4) \
123 x3 ^= x0; x1 ^= x2; x2 ^= x0; \
124 x0 &= x3; x1 |= x3; x4 =~ x4; \
125 x0 ^= x1; x1 ^= x2; \
126 x3 ^= x4; x4 ^= x0; x2 &= x0; \
127 x4 ^= x1; x2 ^= x3; x3 &= x1; \
130 #define S7(x0,x1,x2,x3,x4) \
132 x4 = x1; x0 =~ x0; x1 &= x2; \
133 x1 ^= x3; x3 |= x4; x4 ^= x2; \
134 x2 ^= x3; x3 ^= x0; x0 |= x1; \
135 x2 &= x0; x0 ^= x4; x4 ^= x3; \
136 x3 &= x0; x4 ^= x1; \
137 x2 ^= x4; x3 ^= x1; x4 |= x0; \
140 #define SI0(x0,x1,x2,x3,x4) \
142 x3 |= x1; x4 ^= x1; x0 =~ x0; \
143 x2 ^= x3; x3 ^= x0; x0 &= x1; \
144 x0 ^= x2; x2 &= x3; x3 ^= x4; \
145 x2 ^= x3; x1 ^= x3; x3 &= x0; \
146 x1 ^= x0; x0 ^= x2; x4 ^= x3;
148 #define SI1(x0,x1,x2,x3,x4) \
150 x0 ^= x2; x2 =~ x2; x4 |= x1; \
151 x4 ^= x3; x3 &= x1; x1 ^= x2; \
152 x2 &= x4; x4 ^= x1; x1 |= x3; \
153 x3 ^= x0; x2 ^= x0; x0 |= x4; \
154 x2 ^= x4; x1 ^= x0; \
157 #define SI2(x0,x1,x2,x3,x4) \
158 x2 ^= x1; x4 = x3; x3 =~ x3; \
159 x3 |= x2; x2 ^= x4; x4 ^= x0; \
160 x3 ^= x1; x1 |= x2; x2 ^= x0; \
161 x1 ^= x4; x4 |= x3; x2 ^= x3; \
162 x4 ^= x2; x2 &= x1; \
163 x2 ^= x3; x3 ^= x4; x4 ^= x0;
165 #define SI3(x0,x1,x2,x3,x4) \
168 x1 ^= x0; x0 |= x4; x4 ^= x3; \
169 x0 ^= x3; x3 |= x1; x1 ^= x2; \
170 x1 ^= x3; x0 ^= x2; x2 ^= x3; \
171 x3 &= x1; x1 ^= x0; x0 &= x2; \
172 x4 ^= x3; x3 ^= x0; x0 ^= x1;
174 #define SI4(x0,x1,x2,x3,x4) \
175 x2 ^= x3; x4 = x0; x0 &= x1; \
176 x0 ^= x2; x2 |= x3; x4 =~ x4; \
177 x1 ^= x0; x0 ^= x2; x2 &= x4; \
178 x2 ^= x0; x0 |= x4; \
179 x0 ^= x3; x3 &= x2; \
180 x4 ^= x3; x3 ^= x1; x1 &= x0; \
183 #define SI5(x0,x1,x2,x3,x4) \
185 x2 ^= x4; x1 ^= x3; x3 &= x4; \
186 x2 ^= x3; x3 |= x0; x0 =~ x0; \
187 x3 ^= x2; x2 |= x0; x4 ^= x1; \
188 x2 ^= x4; x4 &= x0; x0 ^= x1; \
189 x1 ^= x3; x0 &= x2; x2 ^= x3; \
190 x0 ^= x2; x2 ^= x4; x4 ^= x3;
192 #define SI6(x0,x1,x2,x3,x4) \
194 x4 = x0; x0 &= x3; x2 ^= x3; \
195 x0 ^= x2; x3 ^= x1; x2 |= x4; \
196 x2 ^= x3; x3 &= x0; x0 =~ x0; \
197 x3 ^= x1; x1 &= x2; x4 ^= x0; \
198 x3 ^= x4; x4 ^= x2; x0 ^= x1; \
201 #define SI7(x0,x1,x2,x3,x4) \
202 x4 = x3; x3 &= x0; x0 ^= x2; \
203 x2 |= x4; x4 ^= x1; x0 =~ x0; \
204 x1 |= x3; x4 ^= x0; x0 &= x2; \
205 x0 ^= x1; x1 &= x2; x3 ^= x2; \
206 x4 ^= x3; x2 &= x3; x3 |= x0; \
207 x1 ^= x4; x3 ^= x4; x4 &= x0; \
211 u8 iv
[SERPENT_BLOCK_SIZE
];
212 u32 expkey
[SERPENT_EXPKEY_WORDS
];
215 static int setkey(void *ctx
, const u8
*key
, unsigned int keylen
, u32
*flags
)
217 u32
*k
= ((struct serpent_ctx
*)ctx
)->expkey
;
222 if ((keylen
< SERPENT_MIN_KEY_SIZE
)
223 || (keylen
> SERPENT_MAX_KEY_SIZE
))
225 *flags
|= CRYPTO_TFM_RES_BAD_KEY_LEN
;
229 /* Copy key, add padding */
231 for (i
= 0; i
< keylen
; ++i
)
233 if (i
< SERPENT_MAX_KEY_SIZE
)
235 while (i
< SERPENT_MAX_KEY_SIZE
)
238 /* Expand key using polynomial */
240 r0
= le32_to_cpu(k
[3]);
241 r1
= le32_to_cpu(k
[4]);
242 r2
= le32_to_cpu(k
[5]);
243 r3
= le32_to_cpu(k
[6]);
244 r4
= le32_to_cpu(k
[7]);
246 keyiter(le32_to_cpu(k
[0]),r0
,r4
,r2
,0,0);
247 keyiter(le32_to_cpu(k
[1]),r1
,r0
,r3
,1,1);
248 keyiter(le32_to_cpu(k
[2]),r2
,r1
,r4
,2,2);
249 keyiter(le32_to_cpu(k
[3]),r3
,r2
,r0
,3,3);
250 keyiter(le32_to_cpu(k
[4]),r4
,r3
,r1
,4,4);
251 keyiter(le32_to_cpu(k
[5]),r0
,r4
,r2
,5,5);
252 keyiter(le32_to_cpu(k
[6]),r1
,r0
,r3
,6,6);
253 keyiter(le32_to_cpu(k
[7]),r2
,r1
,r4
,7,7);
255 keyiter(k
[ 0],r3
,r2
,r0
, 8, 8); keyiter(k
[ 1],r4
,r3
,r1
, 9, 9);
256 keyiter(k
[ 2],r0
,r4
,r2
, 10, 10); keyiter(k
[ 3],r1
,r0
,r3
, 11, 11);
257 keyiter(k
[ 4],r2
,r1
,r4
, 12, 12); keyiter(k
[ 5],r3
,r2
,r0
, 13, 13);
258 keyiter(k
[ 6],r4
,r3
,r1
, 14, 14); keyiter(k
[ 7],r0
,r4
,r2
, 15, 15);
259 keyiter(k
[ 8],r1
,r0
,r3
, 16, 16); keyiter(k
[ 9],r2
,r1
,r4
, 17, 17);
260 keyiter(k
[ 10],r3
,r2
,r0
, 18, 18); keyiter(k
[ 11],r4
,r3
,r1
, 19, 19);
261 keyiter(k
[ 12],r0
,r4
,r2
, 20, 20); keyiter(k
[ 13],r1
,r0
,r3
, 21, 21);
262 keyiter(k
[ 14],r2
,r1
,r4
, 22, 22); keyiter(k
[ 15],r3
,r2
,r0
, 23, 23);
263 keyiter(k
[ 16],r4
,r3
,r1
, 24, 24); keyiter(k
[ 17],r0
,r4
,r2
, 25, 25);
264 keyiter(k
[ 18],r1
,r0
,r3
, 26, 26); keyiter(k
[ 19],r2
,r1
,r4
, 27, 27);
265 keyiter(k
[ 20],r3
,r2
,r0
, 28, 28); keyiter(k
[ 21],r4
,r3
,r1
, 29, 29);
266 keyiter(k
[ 22],r0
,r4
,r2
, 30, 30); keyiter(k
[ 23],r1
,r0
,r3
, 31, 31);
270 keyiter(k
[-26],r2
,r1
,r4
, 32,-18); keyiter(k
[-25],r3
,r2
,r0
, 33,-17);
271 keyiter(k
[-24],r4
,r3
,r1
, 34,-16); keyiter(k
[-23],r0
,r4
,r2
, 35,-15);
272 keyiter(k
[-22],r1
,r0
,r3
, 36,-14); keyiter(k
[-21],r2
,r1
,r4
, 37,-13);
273 keyiter(k
[-20],r3
,r2
,r0
, 38,-12); keyiter(k
[-19],r4
,r3
,r1
, 39,-11);
274 keyiter(k
[-18],r0
,r4
,r2
, 40,-10); keyiter(k
[-17],r1
,r0
,r3
, 41, -9);
275 keyiter(k
[-16],r2
,r1
,r4
, 42, -8); keyiter(k
[-15],r3
,r2
,r0
, 43, -7);
276 keyiter(k
[-14],r4
,r3
,r1
, 44, -6); keyiter(k
[-13],r0
,r4
,r2
, 45, -5);
277 keyiter(k
[-12],r1
,r0
,r3
, 46, -4); keyiter(k
[-11],r2
,r1
,r4
, 47, -3);
278 keyiter(k
[-10],r3
,r2
,r0
, 48, -2); keyiter(k
[ -9],r4
,r3
,r1
, 49, -1);
279 keyiter(k
[ -8],r0
,r4
,r2
, 50, 0); keyiter(k
[ -7],r1
,r0
,r3
, 51, 1);
280 keyiter(k
[ -6],r2
,r1
,r4
, 52, 2); keyiter(k
[ -5],r3
,r2
,r0
, 53, 3);
281 keyiter(k
[ -4],r4
,r3
,r1
, 54, 4); keyiter(k
[ -3],r0
,r4
,r2
, 55, 5);
282 keyiter(k
[ -2],r1
,r0
,r3
, 56, 6); keyiter(k
[ -1],r2
,r1
,r4
, 57, 7);
283 keyiter(k
[ 0],r3
,r2
,r0
, 58, 8); keyiter(k
[ 1],r4
,r3
,r1
, 59, 9);
284 keyiter(k
[ 2],r0
,r4
,r2
, 60, 10); keyiter(k
[ 3],r1
,r0
,r3
, 61, 11);
285 keyiter(k
[ 4],r2
,r1
,r4
, 62, 12); keyiter(k
[ 5],r3
,r2
,r0
, 63, 13);
286 keyiter(k
[ 6],r4
,r3
,r1
, 64, 14); keyiter(k
[ 7],r0
,r4
,r2
, 65, 15);
287 keyiter(k
[ 8],r1
,r0
,r3
, 66, 16); keyiter(k
[ 9],r2
,r1
,r4
, 67, 17);
288 keyiter(k
[ 10],r3
,r2
,r0
, 68, 18); keyiter(k
[ 11],r4
,r3
,r1
, 69, 19);
289 keyiter(k
[ 12],r0
,r4
,r2
, 70, 20); keyiter(k
[ 13],r1
,r0
,r3
, 71, 21);
290 keyiter(k
[ 14],r2
,r1
,r4
, 72, 22); keyiter(k
[ 15],r3
,r2
,r0
, 73, 23);
291 keyiter(k
[ 16],r4
,r3
,r1
, 74, 24); keyiter(k
[ 17],r0
,r4
,r2
, 75, 25);
292 keyiter(k
[ 18],r1
,r0
,r3
, 76, 26); keyiter(k
[ 19],r2
,r1
,r4
, 77, 27);
293 keyiter(k
[ 20],r3
,r2
,r0
, 78, 28); keyiter(k
[ 21],r4
,r3
,r1
, 79, 29);
294 keyiter(k
[ 22],r0
,r4
,r2
, 80, 30); keyiter(k
[ 23],r1
,r0
,r3
, 81, 31);
298 keyiter(k
[-26],r2
,r1
,r4
, 82,-18); keyiter(k
[-25],r3
,r2
,r0
, 83,-17);
299 keyiter(k
[-24],r4
,r3
,r1
, 84,-16); keyiter(k
[-23],r0
,r4
,r2
, 85,-15);
300 keyiter(k
[-22],r1
,r0
,r3
, 86,-14); keyiter(k
[-21],r2
,r1
,r4
, 87,-13);
301 keyiter(k
[-20],r3
,r2
,r0
, 88,-12); keyiter(k
[-19],r4
,r3
,r1
, 89,-11);
302 keyiter(k
[-18],r0
,r4
,r2
, 90,-10); keyiter(k
[-17],r1
,r0
,r3
, 91, -9);
303 keyiter(k
[-16],r2
,r1
,r4
, 92, -8); keyiter(k
[-15],r3
,r2
,r0
, 93, -7);
304 keyiter(k
[-14],r4
,r3
,r1
, 94, -6); keyiter(k
[-13],r0
,r4
,r2
, 95, -5);
305 keyiter(k
[-12],r1
,r0
,r3
, 96, -4); keyiter(k
[-11],r2
,r1
,r4
, 97, -3);
306 keyiter(k
[-10],r3
,r2
,r0
, 98, -2); keyiter(k
[ -9],r4
,r3
,r1
, 99, -1);
307 keyiter(k
[ -8],r0
,r4
,r2
,100, 0); keyiter(k
[ -7],r1
,r0
,r3
,101, 1);
308 keyiter(k
[ -6],r2
,r1
,r4
,102, 2); keyiter(k
[ -5],r3
,r2
,r0
,103, 3);
309 keyiter(k
[ -4],r4
,r3
,r1
,104, 4); keyiter(k
[ -3],r0
,r4
,r2
,105, 5);
310 keyiter(k
[ -2],r1
,r0
,r3
,106, 6); keyiter(k
[ -1],r2
,r1
,r4
,107, 7);
311 keyiter(k
[ 0],r3
,r2
,r0
,108, 8); keyiter(k
[ 1],r4
,r3
,r1
,109, 9);
312 keyiter(k
[ 2],r0
,r4
,r2
,110, 10); keyiter(k
[ 3],r1
,r0
,r3
,111, 11);
313 keyiter(k
[ 4],r2
,r1
,r4
,112, 12); keyiter(k
[ 5],r3
,r2
,r0
,113, 13);
314 keyiter(k
[ 6],r4
,r3
,r1
,114, 14); keyiter(k
[ 7],r0
,r4
,r2
,115, 15);
315 keyiter(k
[ 8],r1
,r0
,r3
,116, 16); keyiter(k
[ 9],r2
,r1
,r4
,117, 17);
316 keyiter(k
[ 10],r3
,r2
,r0
,118, 18); keyiter(k
[ 11],r4
,r3
,r1
,119, 19);
317 keyiter(k
[ 12],r0
,r4
,r2
,120, 20); keyiter(k
[ 13],r1
,r0
,r3
,121, 21);
318 keyiter(k
[ 14],r2
,r1
,r4
,122, 22); keyiter(k
[ 15],r3
,r2
,r0
,123, 23);
319 keyiter(k
[ 16],r4
,r3
,r1
,124, 24); keyiter(k
[ 17],r0
,r4
,r2
,125, 25);
320 keyiter(k
[ 18],r1
,r0
,r3
,126, 26); keyiter(k
[ 19],r2
,r1
,r4
,127, 27);
321 keyiter(k
[ 20],r3
,r2
,r0
,128, 28); keyiter(k
[ 21],r4
,r3
,r1
,129, 29);
322 keyiter(k
[ 22],r0
,r4
,r2
,130, 30); keyiter(k
[ 23],r1
,r0
,r3
,131, 31);
326 S3(r3
,r4
,r0
,r1
,r2
); storekeys(r1
,r2
,r4
,r3
, 28); loadkeys(r1
,r2
,r4
,r3
, 24);
327 S4(r1
,r2
,r4
,r3
,r0
); storekeys(r2
,r4
,r3
,r0
, 24); loadkeys(r2
,r4
,r3
,r0
, 20);
328 S5(r2
,r4
,r3
,r0
,r1
); storekeys(r1
,r2
,r4
,r0
, 20); loadkeys(r1
,r2
,r4
,r0
, 16);
329 S6(r1
,r2
,r4
,r0
,r3
); storekeys(r4
,r3
,r2
,r0
, 16); loadkeys(r4
,r3
,r2
,r0
, 12);
330 S7(r4
,r3
,r2
,r0
,r1
); storekeys(r1
,r2
,r0
,r4
, 12); loadkeys(r1
,r2
,r0
,r4
, 8);
331 S0(r1
,r2
,r0
,r4
,r3
); storekeys(r0
,r2
,r4
,r1
, 8); loadkeys(r0
,r2
,r4
,r1
, 4);
332 S1(r0
,r2
,r4
,r1
,r3
); storekeys(r3
,r4
,r1
,r0
, 4); loadkeys(r3
,r4
,r1
,r0
, 0);
333 S2(r3
,r4
,r1
,r0
,r2
); storekeys(r2
,r4
,r3
,r0
, 0); loadkeys(r2
,r4
,r3
,r0
, -4);
334 S3(r2
,r4
,r3
,r0
,r1
); storekeys(r0
,r1
,r4
,r2
, -4); loadkeys(r0
,r1
,r4
,r2
, -8);
335 S4(r0
,r1
,r4
,r2
,r3
); storekeys(r1
,r4
,r2
,r3
, -8); loadkeys(r1
,r4
,r2
,r3
,-12);
336 S5(r1
,r4
,r2
,r3
,r0
); storekeys(r0
,r1
,r4
,r3
,-12); loadkeys(r0
,r1
,r4
,r3
,-16);
337 S6(r0
,r1
,r4
,r3
,r2
); storekeys(r4
,r2
,r1
,r3
,-16); loadkeys(r4
,r2
,r1
,r3
,-20);
338 S7(r4
,r2
,r1
,r3
,r0
); storekeys(r0
,r1
,r3
,r4
,-20); loadkeys(r0
,r1
,r3
,r4
,-24);
339 S0(r0
,r1
,r3
,r4
,r2
); storekeys(r3
,r1
,r4
,r0
,-24); loadkeys(r3
,r1
,r4
,r0
,-28);
341 S1(r3
,r1
,r4
,r0
,r2
); storekeys(r2
,r4
,r0
,r3
, 22); loadkeys(r2
,r4
,r0
,r3
, 18);
342 S2(r2
,r4
,r0
,r3
,r1
); storekeys(r1
,r4
,r2
,r3
, 18); loadkeys(r1
,r4
,r2
,r3
, 14);
343 S3(r1
,r4
,r2
,r3
,r0
); storekeys(r3
,r0
,r4
,r1
, 14); loadkeys(r3
,r0
,r4
,r1
, 10);
344 S4(r3
,r0
,r4
,r1
,r2
); storekeys(r0
,r4
,r1
,r2
, 10); loadkeys(r0
,r4
,r1
,r2
, 6);
345 S5(r0
,r4
,r1
,r2
,r3
); storekeys(r3
,r0
,r4
,r2
, 6); loadkeys(r3
,r0
,r4
,r2
, 2);
346 S6(r3
,r0
,r4
,r2
,r1
); storekeys(r4
,r1
,r0
,r2
, 2); loadkeys(r4
,r1
,r0
,r2
, -2);
347 S7(r4
,r1
,r0
,r2
,r3
); storekeys(r3
,r0
,r2
,r4
, -2); loadkeys(r3
,r0
,r2
,r4
, -6);
348 S0(r3
,r0
,r2
,r4
,r1
); storekeys(r2
,r0
,r4
,r3
, -6); loadkeys(r2
,r0
,r4
,r3
,-10);
349 S1(r2
,r0
,r4
,r3
,r1
); storekeys(r1
,r4
,r3
,r2
,-10); loadkeys(r1
,r4
,r3
,r2
,-14);
350 S2(r1
,r4
,r3
,r2
,r0
); storekeys(r0
,r4
,r1
,r2
,-14); loadkeys(r0
,r4
,r1
,r2
,-18);
351 S3(r0
,r4
,r1
,r2
,r3
); storekeys(r2
,r3
,r4
,r0
,-18); loadkeys(r2
,r3
,r4
,r0
,-22);
353 S4(r2
,r3
,r4
,r0
,r1
); storekeys(r3
,r4
,r0
,r1
, 28); loadkeys(r3
,r4
,r0
,r1
, 24);
354 S5(r3
,r4
,r0
,r1
,r2
); storekeys(r2
,r3
,r4
,r1
, 24); loadkeys(r2
,r3
,r4
,r1
, 20);
355 S6(r2
,r3
,r4
,r1
,r0
); storekeys(r4
,r0
,r3
,r1
, 20); loadkeys(r4
,r0
,r3
,r1
, 16);
356 S7(r4
,r0
,r3
,r1
,r2
); storekeys(r2
,r3
,r1
,r4
, 16); loadkeys(r2
,r3
,r1
,r4
, 12);
357 S0(r2
,r3
,r1
,r4
,r0
); storekeys(r1
,r3
,r4
,r2
, 12); loadkeys(r1
,r3
,r4
,r2
, 8);
358 S1(r1
,r3
,r4
,r2
,r0
); storekeys(r0
,r4
,r2
,r1
, 8); loadkeys(r0
,r4
,r2
,r1
, 4);
359 S2(r0
,r4
,r2
,r1
,r3
); storekeys(r3
,r4
,r0
,r1
, 4); loadkeys(r3
,r4
,r0
,r1
, 0);
360 S3(r3
,r4
,r0
,r1
,r2
); storekeys(r1
,r2
,r4
,r3
, 0);
365 static void encrypt(void *ctx
, u8
*dst
, const u8
*src
)
368 *k
= ((struct serpent_ctx
*)ctx
)->expkey
,
369 *s
= (const u32
*)src
;
374 * Note: The conversions between u8* and u32* might cause trouble
375 * on architectures with stricter alignment rules than x86
378 r0
= le32_to_cpu(s
[0]);
379 r1
= le32_to_cpu(s
[1]);
380 r2
= le32_to_cpu(s
[2]);
381 r3
= le32_to_cpu(s
[3]);
384 S0(r0
,r1
,r2
,r3
,r4
); LK(r2
,r1
,r3
,r0
,r4
,1);
385 S1(r2
,r1
,r3
,r0
,r4
); LK(r4
,r3
,r0
,r2
,r1
,2);
386 S2(r4
,r3
,r0
,r2
,r1
); LK(r1
,r3
,r4
,r2
,r0
,3);
387 S3(r1
,r3
,r4
,r2
,r0
); LK(r2
,r0
,r3
,r1
,r4
,4);
388 S4(r2
,r0
,r3
,r1
,r4
); LK(r0
,r3
,r1
,r4
,r2
,5);
389 S5(r0
,r3
,r1
,r4
,r2
); LK(r2
,r0
,r3
,r4
,r1
,6);
390 S6(r2
,r0
,r3
,r4
,r1
); LK(r3
,r1
,r0
,r4
,r2
,7);
391 S7(r3
,r1
,r0
,r4
,r2
); LK(r2
,r0
,r4
,r3
,r1
,8);
392 S0(r2
,r0
,r4
,r3
,r1
); LK(r4
,r0
,r3
,r2
,r1
,9);
393 S1(r4
,r0
,r3
,r2
,r1
); LK(r1
,r3
,r2
,r4
,r0
,10);
394 S2(r1
,r3
,r2
,r4
,r0
); LK(r0
,r3
,r1
,r4
,r2
,11);
395 S3(r0
,r3
,r1
,r4
,r2
); LK(r4
,r2
,r3
,r0
,r1
,12);
396 S4(r4
,r2
,r3
,r0
,r1
); LK(r2
,r3
,r0
,r1
,r4
,13);
397 S5(r2
,r3
,r0
,r1
,r4
); LK(r4
,r2
,r3
,r1
,r0
,14);
398 S6(r4
,r2
,r3
,r1
,r0
); LK(r3
,r0
,r2
,r1
,r4
,15);
399 S7(r3
,r0
,r2
,r1
,r4
); LK(r4
,r2
,r1
,r3
,r0
,16);
400 S0(r4
,r2
,r1
,r3
,r0
); LK(r1
,r2
,r3
,r4
,r0
,17);
401 S1(r1
,r2
,r3
,r4
,r0
); LK(r0
,r3
,r4
,r1
,r2
,18);
402 S2(r0
,r3
,r4
,r1
,r2
); LK(r2
,r3
,r0
,r1
,r4
,19);
403 S3(r2
,r3
,r0
,r1
,r4
); LK(r1
,r4
,r3
,r2
,r0
,20);
404 S4(r1
,r4
,r3
,r2
,r0
); LK(r4
,r3
,r2
,r0
,r1
,21);
405 S5(r4
,r3
,r2
,r0
,r1
); LK(r1
,r4
,r3
,r0
,r2
,22);
406 S6(r1
,r4
,r3
,r0
,r2
); LK(r3
,r2
,r4
,r0
,r1
,23);
407 S7(r3
,r2
,r4
,r0
,r1
); LK(r1
,r4
,r0
,r3
,r2
,24);
408 S0(r1
,r4
,r0
,r3
,r2
); LK(r0
,r4
,r3
,r1
,r2
,25);
409 S1(r0
,r4
,r3
,r1
,r2
); LK(r2
,r3
,r1
,r0
,r4
,26);
410 S2(r2
,r3
,r1
,r0
,r4
); LK(r4
,r3
,r2
,r0
,r1
,27);
411 S3(r4
,r3
,r2
,r0
,r1
); LK(r0
,r1
,r3
,r4
,r2
,28);
412 S4(r0
,r1
,r3
,r4
,r2
); LK(r1
,r3
,r4
,r2
,r0
,29);
413 S5(r1
,r3
,r4
,r2
,r0
); LK(r0
,r1
,r3
,r2
,r4
,30);
414 S6(r0
,r1
,r3
,r2
,r4
); LK(r3
,r4
,r1
,r2
,r0
,31);
415 S7(r3
,r4
,r1
,r2
,r0
); K(r0
,r1
,r2
,r3
,32);
417 d
[0] = cpu_to_le32(r0
);
418 d
[1] = cpu_to_le32(r1
);
419 d
[2] = cpu_to_le32(r2
);
420 d
[3] = cpu_to_le32(r3
);
423 static void decrypt(void *ctx
, u8
*dst
, const u8
*src
)
426 *k
= ((struct serpent_ctx
*)ctx
)->expkey
,
427 *s
= (const u32
*)src
;
431 r0
= le32_to_cpu(s
[0]);
432 r1
= le32_to_cpu(s
[1]);
433 r2
= le32_to_cpu(s
[2]);
434 r3
= le32_to_cpu(s
[3]);
437 SI7(r0
,r1
,r2
,r3
,r4
); KL(r1
,r3
,r0
,r4
,r2
,31);
438 SI6(r1
,r3
,r0
,r4
,r2
); KL(r0
,r2
,r4
,r1
,r3
,30);
439 SI5(r0
,r2
,r4
,r1
,r3
); KL(r2
,r3
,r0
,r4
,r1
,29);
440 SI4(r2
,r3
,r0
,r4
,r1
); KL(r2
,r0
,r1
,r4
,r3
,28);
441 SI3(r2
,r0
,r1
,r4
,r3
); KL(r1
,r2
,r3
,r4
,r0
,27);
442 SI2(r1
,r2
,r3
,r4
,r0
); KL(r2
,r0
,r4
,r3
,r1
,26);
443 SI1(r2
,r0
,r4
,r3
,r1
); KL(r1
,r0
,r4
,r3
,r2
,25);
444 SI0(r1
,r0
,r4
,r3
,r2
); KL(r4
,r2
,r0
,r1
,r3
,24);
445 SI7(r4
,r2
,r0
,r1
,r3
); KL(r2
,r1
,r4
,r3
,r0
,23);
446 SI6(r2
,r1
,r4
,r3
,r0
); KL(r4
,r0
,r3
,r2
,r1
,22);
447 SI5(r4
,r0
,r3
,r2
,r1
); KL(r0
,r1
,r4
,r3
,r2
,21);
448 SI4(r0
,r1
,r4
,r3
,r2
); KL(r0
,r4
,r2
,r3
,r1
,20);
449 SI3(r0
,r4
,r2
,r3
,r1
); KL(r2
,r0
,r1
,r3
,r4
,19);
450 SI2(r2
,r0
,r1
,r3
,r4
); KL(r0
,r4
,r3
,r1
,r2
,18);
451 SI1(r0
,r4
,r3
,r1
,r2
); KL(r2
,r4
,r3
,r1
,r0
,17);
452 SI0(r2
,r4
,r3
,r1
,r0
); KL(r3
,r0
,r4
,r2
,r1
,16);
453 SI7(r3
,r0
,r4
,r2
,r1
); KL(r0
,r2
,r3
,r1
,r4
,15);
454 SI6(r0
,r2
,r3
,r1
,r4
); KL(r3
,r4
,r1
,r0
,r2
,14);
455 SI5(r3
,r4
,r1
,r0
,r2
); KL(r4
,r2
,r3
,r1
,r0
,13);
456 SI4(r4
,r2
,r3
,r1
,r0
); KL(r4
,r3
,r0
,r1
,r2
,12);
457 SI3(r4
,r3
,r0
,r1
,r2
); KL(r0
,r4
,r2
,r1
,r3
,11);
458 SI2(r0
,r4
,r2
,r1
,r3
); KL(r4
,r3
,r1
,r2
,r0
,10);
459 SI1(r4
,r3
,r1
,r2
,r0
); KL(r0
,r3
,r1
,r2
,r4
,9);
460 SI0(r0
,r3
,r1
,r2
,r4
); KL(r1
,r4
,r3
,r0
,r2
,8);
461 SI7(r1
,r4
,r3
,r0
,r2
); KL(r4
,r0
,r1
,r2
,r3
,7);
462 SI6(r4
,r0
,r1
,r2
,r3
); KL(r1
,r3
,r2
,r4
,r0
,6);
463 SI5(r1
,r3
,r2
,r4
,r0
); KL(r3
,r0
,r1
,r2
,r4
,5);
464 SI4(r3
,r0
,r1
,r2
,r4
); KL(r3
,r1
,r4
,r2
,r0
,4);
465 SI3(r3
,r1
,r4
,r2
,r0
); KL(r4
,r3
,r0
,r2
,r1
,3);
466 SI2(r4
,r3
,r0
,r2
,r1
); KL(r3
,r1
,r2
,r0
,r4
,2);
467 SI1(r3
,r1
,r2
,r0
,r4
); KL(r4
,r1
,r2
,r0
,r3
,1);
468 SI0(r4
,r1
,r2
,r0
,r3
); K(r2
,r3
,r1
,r4
,0);
470 d
[0] = cpu_to_le32(r2
);
471 d
[1] = cpu_to_le32(r3
);
472 d
[2] = cpu_to_le32(r1
);
473 d
[3] = cpu_to_le32(r4
);
476 static struct crypto_alg serpent_alg
= {
477 .cra_name
= "serpent",
478 .cra_flags
= CRYPTO_ALG_TYPE_CIPHER
,
479 .cra_blocksize
= SERPENT_BLOCK_SIZE
,
480 .cra_ctxsize
= sizeof(struct serpent_ctx
),
481 .cra_module
= THIS_MODULE
,
482 .cra_list
= LIST_HEAD_INIT(serpent_alg
.cra_list
),
483 .cra_u
= { .cipher
= {
484 .cia_min_keysize
= SERPENT_MIN_KEY_SIZE
,
485 .cia_max_keysize
= SERPENT_MAX_KEY_SIZE
,
486 .cia_setkey
= setkey
,
487 .cia_encrypt
= encrypt
,
488 .cia_decrypt
= decrypt
} }
491 static int __init
init(void)
493 return crypto_register_alg(&serpent_alg
);
496 static void __exit
fini(void)
498 crypto_unregister_alg(&serpent_alg
);
504 MODULE_LICENSE("GPL");
505 MODULE_DESCRIPTION("Serpent Cipher Algorithm");
506 MODULE_AUTHOR("Dag Arne Osvik <osvik@ii.uib.no>");