4 * Serpent Cipher Algorithm.
6 * Copyright (C) 2002 Dag Arne Osvik <osvik@ii.uib.no>
7 * 2003 Herbert Valerio Riedel <hvr@gnu.org>
9 * Added tnepres support: Ruben Jesus Garcia Hernandez <ruben@ugr.es>, 18.10.2004
10 * Based on code by hvr
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.
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>
24 /* Key is padded to the maximum of 256 bits before round key generation.
25 * Any key length <= 256 bits (32 bytes) is allowed by the algorithm.
28 #define SERPENT_MIN_KEY_SIZE 0
29 #define SERPENT_MAX_KEY_SIZE 32
30 #define SERPENT_EXPKEY_WORDS 132
31 #define SERPENT_BLOCK_SIZE 16
33 #define PHI 0x9e3779b9UL
34 #define ROL(x,r) ((x) = ((x) << (r)) | ((x) >> (32-(r))))
35 #define ROR(x,r) ((x) = ((x) >> (r)) | ((x) << (32-(r))))
37 #define keyiter(a,b,c,d,i,j) \
38 b ^= d; b ^= c; b ^= a; b ^= PHI ^ i; ROL(b,11); k[j] = b;
40 #define loadkeys(x0,x1,x2,x3,i) \
41 x0=k[i]; x1=k[i+1]; x2=k[i+2]; x3=k[i+3];
43 #define storekeys(x0,x1,x2,x3,i) \
44 k[i]=x0; k[i+1]=x1; k[i+2]=x2; k[i+3]=x3;
46 #define K(x0,x1,x2,x3,i) \
47 x3 ^= k[4*(i)+3]; x2 ^= k[4*(i)+2]; \
48 x1 ^= k[4*(i)+1]; x0 ^= k[4*(i)+0];
50 #define LK(x0,x1,x2,x3,x4,i) \
52 ROL(x2,3); x1 ^= x0; x4 = x0 << 3; \
54 ROL(x1,1); x3 ^= x4; \
56 x0 ^= x1; x4 <<= 7; x2 ^= x3; \
57 x0 ^= x3; x2 ^= x4; x3 ^= k[4*i+3]; \
58 x1 ^= k[4*i+1]; ROL(x0,5); ROL(x2,22); \
59 x0 ^= k[4*i+0]; x2 ^= k[4*i+2];
61 #define KL(x0,x1,x2,x3,x4,i) \
62 x0 ^= k[4*i+0]; x1 ^= k[4*i+1]; x2 ^= k[4*i+2]; \
63 x3 ^= k[4*i+3]; ROR(x0,5); ROR(x2,22); \
64 x4 = x1; x2 ^= x3; x0 ^= x3; \
65 x4 <<= 7; x0 ^= x1; ROR(x1,1); \
66 x2 ^= x4; ROR(x3,7); x4 = x0 << 3; \
67 x1 ^= x0; x3 ^= x4; ROR(x0,13); \
68 x1 ^= x2; x3 ^= x2; ROR(x2,3);
70 #define S0(x0,x1,x2,x3,x4) \
72 x3 |= x0; x0 ^= x4; x4 ^= x2; \
73 x4 =~ x4; x3 ^= x1; x1 &= x0; \
74 x1 ^= x4; x2 ^= x0; x0 ^= x3; \
75 x4 |= x0; x0 ^= x2; x2 &= x1; \
76 x3 ^= x2; x1 =~ x1; x2 ^= x4; \
79 #define S1(x0,x1,x2,x3,x4) \
81 x1 ^= x0; x0 ^= x3; x3 =~ x3; \
82 x4 &= x1; x0 |= x1; x3 ^= x2; \
83 x0 ^= x3; x1 ^= x3; x3 ^= x4; \
84 x1 |= x4; x4 ^= x2; x2 &= x0; \
85 x2 ^= x1; x1 |= x0; x0 =~ x0; \
88 #define S2(x0,x1,x2,x3,x4) \
90 x1 ^= x0; x4 = x0; x0 &= x2; \
91 x0 ^= x3; x3 |= x4; x2 ^= x1; \
92 x3 ^= x1; x1 &= x0; x0 ^= x2; \
93 x2 &= x3; x3 |= x1; x0 =~ x0; \
94 x3 ^= x0; x4 ^= x0; x0 ^= x2; \
97 #define S3(x0,x1,x2,x3,x4) \
99 x1 ^= x3; x3 |= x0; x4 &= x0; \
100 x0 ^= x2; x2 ^= x1; x1 &= x3; \
101 x2 ^= x3; x0 |= x4; x4 ^= x3; \
102 x1 ^= x0; x0 &= x3; x3 &= x4; \
103 x3 ^= x2; x4 |= x1; x2 &= x1; \
104 x4 ^= x3; x0 ^= x3; x3 ^= x2;
106 #define S4(x0,x1,x2,x3,x4) \
108 x3 &= x0; x0 ^= x4; \
109 x3 ^= x2; x2 |= x4; x0 ^= x1; \
110 x4 ^= x3; x2 |= x0; \
111 x2 ^= x1; x1 &= x0; \
112 x1 ^= x4; x4 &= x2; x2 ^= x3; \
113 x4 ^= x0; x3 |= x1; x1 =~ x1; \
116 #define S5(x0,x1,x2,x3,x4) \
118 x2 ^= x1; x3 =~ x3; x4 ^= x0; \
119 x0 ^= x2; x1 &= x4; x4 |= x3; \
120 x4 ^= x0; x0 &= x3; x1 ^= x3; \
121 x3 ^= x2; x0 ^= x1; x2 &= x4; \
122 x1 ^= x2; x2 &= x0; \
125 #define S6(x0,x1,x2,x3,x4) \
127 x3 ^= x0; x1 ^= x2; x2 ^= x0; \
128 x0 &= x3; x1 |= x3; x4 =~ x4; \
129 x0 ^= x1; x1 ^= x2; \
130 x3 ^= x4; x4 ^= x0; x2 &= x0; \
131 x4 ^= x1; x2 ^= x3; x3 &= x1; \
134 #define S7(x0,x1,x2,x3,x4) \
136 x4 = x1; x0 =~ x0; x1 &= x2; \
137 x1 ^= x3; x3 |= x4; x4 ^= x2; \
138 x2 ^= x3; x3 ^= x0; x0 |= x1; \
139 x2 &= x0; x0 ^= x4; x4 ^= x3; \
140 x3 &= x0; x4 ^= x1; \
141 x2 ^= x4; x3 ^= x1; x4 |= x0; \
144 #define SI0(x0,x1,x2,x3,x4) \
146 x3 |= x1; x4 ^= x1; x0 =~ x0; \
147 x2 ^= x3; x3 ^= x0; x0 &= x1; \
148 x0 ^= x2; x2 &= x3; x3 ^= x4; \
149 x2 ^= x3; x1 ^= x3; x3 &= x0; \
150 x1 ^= x0; x0 ^= x2; x4 ^= x3;
152 #define SI1(x0,x1,x2,x3,x4) \
154 x0 ^= x2; x2 =~ x2; x4 |= x1; \
155 x4 ^= x3; x3 &= x1; x1 ^= x2; \
156 x2 &= x4; x4 ^= x1; x1 |= x3; \
157 x3 ^= x0; x2 ^= x0; x0 |= x4; \
158 x2 ^= x4; x1 ^= x0; \
161 #define SI2(x0,x1,x2,x3,x4) \
162 x2 ^= x1; x4 = x3; x3 =~ x3; \
163 x3 |= x2; x2 ^= x4; x4 ^= x0; \
164 x3 ^= x1; x1 |= x2; x2 ^= x0; \
165 x1 ^= x4; x4 |= x3; x2 ^= x3; \
166 x4 ^= x2; x2 &= x1; \
167 x2 ^= x3; x3 ^= x4; x4 ^= x0;
169 #define SI3(x0,x1,x2,x3,x4) \
172 x1 ^= x0; x0 |= x4; x4 ^= x3; \
173 x0 ^= x3; x3 |= x1; x1 ^= x2; \
174 x1 ^= x3; x0 ^= x2; x2 ^= x3; \
175 x3 &= x1; x1 ^= x0; x0 &= x2; \
176 x4 ^= x3; x3 ^= x0; x0 ^= x1;
178 #define SI4(x0,x1,x2,x3,x4) \
179 x2 ^= x3; x4 = x0; x0 &= x1; \
180 x0 ^= x2; x2 |= x3; x4 =~ x4; \
181 x1 ^= x0; x0 ^= x2; x2 &= x4; \
182 x2 ^= x0; x0 |= x4; \
183 x0 ^= x3; x3 &= x2; \
184 x4 ^= x3; x3 ^= x1; x1 &= x0; \
187 #define SI5(x0,x1,x2,x3,x4) \
189 x2 ^= x4; x1 ^= x3; x3 &= x4; \
190 x2 ^= x3; x3 |= x0; x0 =~ x0; \
191 x3 ^= x2; x2 |= x0; x4 ^= x1; \
192 x2 ^= x4; x4 &= x0; x0 ^= x1; \
193 x1 ^= x3; x0 &= x2; x2 ^= x3; \
194 x0 ^= x2; x2 ^= x4; x4 ^= x3;
196 #define SI6(x0,x1,x2,x3,x4) \
198 x4 = x0; x0 &= x3; x2 ^= x3; \
199 x0 ^= x2; x3 ^= x1; x2 |= x4; \
200 x2 ^= x3; x3 &= x0; x0 =~ x0; \
201 x3 ^= x1; x1 &= x2; x4 ^= x0; \
202 x3 ^= x4; x4 ^= x2; x0 ^= x1; \
205 #define SI7(x0,x1,x2,x3,x4) \
206 x4 = x3; x3 &= x0; x0 ^= x2; \
207 x2 |= x4; x4 ^= x1; x0 =~ x0; \
208 x1 |= x3; x4 ^= x0; x0 &= x2; \
209 x0 ^= x1; x1 &= x2; x3 ^= x2; \
210 x4 ^= x3; x2 &= x3; x3 |= x0; \
211 x1 ^= x4; x3 ^= x4; x4 &= x0; \
215 u8 iv[SERPENT_BLOCK_SIZE];
216 u32 expkey[SERPENT_EXPKEY_WORDS];
220 static int serpent_setkey(void *ctx, const u8 *key, unsigned int keylen, u32 *flags)
222 u32 *k = ((struct serpent_ctx *)ctx)->expkey;
227 if ((keylen < SERPENT_MIN_KEY_SIZE)
228 || (keylen > SERPENT_MAX_KEY_SIZE))
230 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
234 /* Copy key, add padding */
236 for (i = 0; i < keylen; ++i)
238 if (i < SERPENT_MAX_KEY_SIZE)
240 while (i < SERPENT_MAX_KEY_SIZE)
243 /* Expand key using polynomial */
245 r0 = le32_to_cpu(k[3]);
246 r1 = le32_to_cpu(k[4]);
247 r2 = le32_to_cpu(k[5]);
248 r3 = le32_to_cpu(k[6]);
249 r4 = le32_to_cpu(k[7]);
251 keyiter(le32_to_cpu(k[0]),r0,r4,r2,0,0);
252 keyiter(le32_to_cpu(k[1]),r1,r0,r3,1,1);
253 keyiter(le32_to_cpu(k[2]),r2,r1,r4,2,2);
254 keyiter(le32_to_cpu(k[3]),r3,r2,r0,3,3);
255 keyiter(le32_to_cpu(k[4]),r4,r3,r1,4,4);
256 keyiter(le32_to_cpu(k[5]),r0,r4,r2,5,5);
257 keyiter(le32_to_cpu(k[6]),r1,r0,r3,6,6);
258 keyiter(le32_to_cpu(k[7]),r2,r1,r4,7,7);
260 keyiter(k[ 0],r3,r2,r0, 8, 8); keyiter(k[ 1],r4,r3,r1, 9, 9);
261 keyiter(k[ 2],r0,r4,r2, 10, 10); keyiter(k[ 3],r1,r0,r3, 11, 11);
262 keyiter(k[ 4],r2,r1,r4, 12, 12); keyiter(k[ 5],r3,r2,r0, 13, 13);
263 keyiter(k[ 6],r4,r3,r1, 14, 14); keyiter(k[ 7],r0,r4,r2, 15, 15);
264 keyiter(k[ 8],r1,r0,r3, 16, 16); keyiter(k[ 9],r2,r1,r4, 17, 17);
265 keyiter(k[ 10],r3,r2,r0, 18, 18); keyiter(k[ 11],r4,r3,r1, 19, 19);
266 keyiter(k[ 12],r0,r4,r2, 20, 20); keyiter(k[ 13],r1,r0,r3, 21, 21);
267 keyiter(k[ 14],r2,r1,r4, 22, 22); keyiter(k[ 15],r3,r2,r0, 23, 23);
268 keyiter(k[ 16],r4,r3,r1, 24, 24); keyiter(k[ 17],r0,r4,r2, 25, 25);
269 keyiter(k[ 18],r1,r0,r3, 26, 26); keyiter(k[ 19],r2,r1,r4, 27, 27);
270 keyiter(k[ 20],r3,r2,r0, 28, 28); keyiter(k[ 21],r4,r3,r1, 29, 29);
271 keyiter(k[ 22],r0,r4,r2, 30, 30); keyiter(k[ 23],r1,r0,r3, 31, 31);
275 keyiter(k[-26],r2,r1,r4, 32,-18); keyiter(k[-25],r3,r2,r0, 33,-17);
276 keyiter(k[-24],r4,r3,r1, 34,-16); keyiter(k[-23],r0,r4,r2, 35,-15);
277 keyiter(k[-22],r1,r0,r3, 36,-14); keyiter(k[-21],r2,r1,r4, 37,-13);
278 keyiter(k[-20],r3,r2,r0, 38,-12); keyiter(k[-19],r4,r3,r1, 39,-11);
279 keyiter(k[-18],r0,r4,r2, 40,-10); keyiter(k[-17],r1,r0,r3, 41, -9);
280 keyiter(k[-16],r2,r1,r4, 42, -8); keyiter(k[-15],r3,r2,r0, 43, -7);
281 keyiter(k[-14],r4,r3,r1, 44, -6); keyiter(k[-13],r0,r4,r2, 45, -5);
282 keyiter(k[-12],r1,r0,r3, 46, -4); keyiter(k[-11],r2,r1,r4, 47, -3);
283 keyiter(k[-10],r3,r2,r0, 48, -2); keyiter(k[ -9],r4,r3,r1, 49, -1);
284 keyiter(k[ -8],r0,r4,r2, 50, 0); keyiter(k[ -7],r1,r0,r3, 51, 1);
285 keyiter(k[ -6],r2,r1,r4, 52, 2); keyiter(k[ -5],r3,r2,r0, 53, 3);
286 keyiter(k[ -4],r4,r3,r1, 54, 4); keyiter(k[ -3],r0,r4,r2, 55, 5);
287 keyiter(k[ -2],r1,r0,r3, 56, 6); keyiter(k[ -1],r2,r1,r4, 57, 7);
288 keyiter(k[ 0],r3,r2,r0, 58, 8); keyiter(k[ 1],r4,r3,r1, 59, 9);
289 keyiter(k[ 2],r0,r4,r2, 60, 10); keyiter(k[ 3],r1,r0,r3, 61, 11);
290 keyiter(k[ 4],r2,r1,r4, 62, 12); keyiter(k[ 5],r3,r2,r0, 63, 13);
291 keyiter(k[ 6],r4,r3,r1, 64, 14); keyiter(k[ 7],r0,r4,r2, 65, 15);
292 keyiter(k[ 8],r1,r0,r3, 66, 16); keyiter(k[ 9],r2,r1,r4, 67, 17);
293 keyiter(k[ 10],r3,r2,r0, 68, 18); keyiter(k[ 11],r4,r3,r1, 69, 19);
294 keyiter(k[ 12],r0,r4,r2, 70, 20); keyiter(k[ 13],r1,r0,r3, 71, 21);
295 keyiter(k[ 14],r2,r1,r4, 72, 22); keyiter(k[ 15],r3,r2,r0, 73, 23);
296 keyiter(k[ 16],r4,r3,r1, 74, 24); keyiter(k[ 17],r0,r4,r2, 75, 25);
297 keyiter(k[ 18],r1,r0,r3, 76, 26); keyiter(k[ 19],r2,r1,r4, 77, 27);
298 keyiter(k[ 20],r3,r2,r0, 78, 28); keyiter(k[ 21],r4,r3,r1, 79, 29);
299 keyiter(k[ 22],r0,r4,r2, 80, 30); keyiter(k[ 23],r1,r0,r3, 81, 31);
303 keyiter(k[-26],r2,r1,r4, 82,-18); keyiter(k[-25],r3,r2,r0, 83,-17);
304 keyiter(k[-24],r4,r3,r1, 84,-16); keyiter(k[-23],r0,r4,r2, 85,-15);
305 keyiter(k[-22],r1,r0,r3, 86,-14); keyiter(k[-21],r2,r1,r4, 87,-13);
306 keyiter(k[-20],r3,r2,r0, 88,-12); keyiter(k[-19],r4,r3,r1, 89,-11);
307 keyiter(k[-18],r0,r4,r2, 90,-10); keyiter(k[-17],r1,r0,r3, 91, -9);
308 keyiter(k[-16],r2,r1,r4, 92, -8); keyiter(k[-15],r3,r2,r0, 93, -7);
309 keyiter(k[-14],r4,r3,r1, 94, -6); keyiter(k[-13],r0,r4,r2, 95, -5);
310 keyiter(k[-12],r1,r0,r3, 96, -4); keyiter(k[-11],r2,r1,r4, 97, -3);
311 keyiter(k[-10],r3,r2,r0, 98, -2); keyiter(k[ -9],r4,r3,r1, 99, -1);
312 keyiter(k[ -8],r0,r4,r2,100, 0); keyiter(k[ -7],r1,r0,r3,101, 1);
313 keyiter(k[ -6],r2,r1,r4,102, 2); keyiter(k[ -5],r3,r2,r0,103, 3);
314 keyiter(k[ -4],r4,r3,r1,104, 4); keyiter(k[ -3],r0,r4,r2,105, 5);
315 keyiter(k[ -2],r1,r0,r3,106, 6); keyiter(k[ -1],r2,r1,r4,107, 7);
316 keyiter(k[ 0],r3,r2,r0,108, 8); keyiter(k[ 1],r4,r3,r1,109, 9);
317 keyiter(k[ 2],r0,r4,r2,110, 10); keyiter(k[ 3],r1,r0,r3,111, 11);
318 keyiter(k[ 4],r2,r1,r4,112, 12); keyiter(k[ 5],r3,r2,r0,113, 13);
319 keyiter(k[ 6],r4,r3,r1,114, 14); keyiter(k[ 7],r0,r4,r2,115, 15);
320 keyiter(k[ 8],r1,r0,r3,116, 16); keyiter(k[ 9],r2,r1,r4,117, 17);
321 keyiter(k[ 10],r3,r2,r0,118, 18); keyiter(k[ 11],r4,r3,r1,119, 19);
322 keyiter(k[ 12],r0,r4,r2,120, 20); keyiter(k[ 13],r1,r0,r3,121, 21);
323 keyiter(k[ 14],r2,r1,r4,122, 22); keyiter(k[ 15],r3,r2,r0,123, 23);
324 keyiter(k[ 16],r4,r3,r1,124, 24); keyiter(k[ 17],r0,r4,r2,125, 25);
325 keyiter(k[ 18],r1,r0,r3,126, 26); keyiter(k[ 19],r2,r1,r4,127, 27);
326 keyiter(k[ 20],r3,r2,r0,128, 28); keyiter(k[ 21],r4,r3,r1,129, 29);
327 keyiter(k[ 22],r0,r4,r2,130, 30); keyiter(k[ 23],r1,r0,r3,131, 31);
331 S3(r3,r4,r0,r1,r2); storekeys(r1,r2,r4,r3, 28); loadkeys(r1,r2,r4,r3, 24);
332 S4(r1,r2,r4,r3,r0); storekeys(r2,r4,r3,r0, 24); loadkeys(r2,r4,r3,r0, 20);
333 S5(r2,r4,r3,r0,r1); storekeys(r1,r2,r4,r0, 20); loadkeys(r1,r2,r4,r0, 16);
334 S6(r1,r2,r4,r0,r3); storekeys(r4,r3,r2,r0, 16); loadkeys(r4,r3,r2,r0, 12);
335 S7(r4,r3,r2,r0,r1); storekeys(r1,r2,r0,r4, 12); loadkeys(r1,r2,r0,r4, 8);
336 S0(r1,r2,r0,r4,r3); storekeys(r0,r2,r4,r1, 8); loadkeys(r0,r2,r4,r1, 4);
337 S1(r0,r2,r4,r1,r3); storekeys(r3,r4,r1,r0, 4); loadkeys(r3,r4,r1,r0, 0);
338 S2(r3,r4,r1,r0,r2); storekeys(r2,r4,r3,r0, 0); loadkeys(r2,r4,r3,r0, -4);
339 S3(r2,r4,r3,r0,r1); storekeys(r0,r1,r4,r2, -4); loadkeys(r0,r1,r4,r2, -8);
340 S4(r0,r1,r4,r2,r3); storekeys(r1,r4,r2,r3, -8); loadkeys(r1,r4,r2,r3,-12);
341 S5(r1,r4,r2,r3,r0); storekeys(r0,r1,r4,r3,-12); loadkeys(r0,r1,r4,r3,-16);
342 S6(r0,r1,r4,r3,r2); storekeys(r4,r2,r1,r3,-16); loadkeys(r4,r2,r1,r3,-20);
343 S7(r4,r2,r1,r3,r0); storekeys(r0,r1,r3,r4,-20); loadkeys(r0,r1,r3,r4,-24);
344 S0(r0,r1,r3,r4,r2); storekeys(r3,r1,r4,r0,-24); loadkeys(r3,r1,r4,r0,-28);
346 S1(r3,r1,r4,r0,r2); storekeys(r2,r4,r0,r3, 22); loadkeys(r2,r4,r0,r3, 18);
347 S2(r2,r4,r0,r3,r1); storekeys(r1,r4,r2,r3, 18); loadkeys(r1,r4,r2,r3, 14);
348 S3(r1,r4,r2,r3,r0); storekeys(r3,r0,r4,r1, 14); loadkeys(r3,r0,r4,r1, 10);
349 S4(r3,r0,r4,r1,r2); storekeys(r0,r4,r1,r2, 10); loadkeys(r0,r4,r1,r2, 6);
350 S5(r0,r4,r1,r2,r3); storekeys(r3,r0,r4,r2, 6); loadkeys(r3,r0,r4,r2, 2);
351 S6(r3,r0,r4,r2,r1); storekeys(r4,r1,r0,r2, 2); loadkeys(r4,r1,r0,r2, -2);
352 S7(r4,r1,r0,r2,r3); storekeys(r3,r0,r2,r4, -2); loadkeys(r3,r0,r2,r4, -6);
353 S0(r3,r0,r2,r4,r1); storekeys(r2,r0,r4,r3, -6); loadkeys(r2,r0,r4,r3,-10);
354 S1(r2,r0,r4,r3,r1); storekeys(r1,r4,r3,r2,-10); loadkeys(r1,r4,r3,r2,-14);
355 S2(r1,r4,r3,r2,r0); storekeys(r0,r4,r1,r2,-14); loadkeys(r0,r4,r1,r2,-18);
356 S3(r0,r4,r1,r2,r3); storekeys(r2,r3,r4,r0,-18); loadkeys(r2,r3,r4,r0,-22);
358 S4(r2,r3,r4,r0,r1); storekeys(r3,r4,r0,r1, 28); loadkeys(r3,r4,r0,r1, 24);
359 S5(r3,r4,r0,r1,r2); storekeys(r2,r3,r4,r1, 24); loadkeys(r2,r3,r4,r1, 20);
360 S6(r2,r3,r4,r1,r0); storekeys(r4,r0,r3,r1, 20); loadkeys(r4,r0,r3,r1, 16);
361 S7(r4,r0,r3,r1,r2); storekeys(r2,r3,r1,r4, 16); loadkeys(r2,r3,r1,r4, 12);
362 S0(r2,r3,r1,r4,r0); storekeys(r1,r3,r4,r2, 12); loadkeys(r1,r3,r4,r2, 8);
363 S1(r1,r3,r4,r2,r0); storekeys(r0,r4,r2,r1, 8); loadkeys(r0,r4,r2,r1, 4);
364 S2(r0,r4,r2,r1,r3); storekeys(r3,r4,r0,r1, 4); loadkeys(r3,r4,r0,r1, 0);
365 S3(r3,r4,r0,r1,r2); storekeys(r1,r2,r4,r3, 0);
370 static void serpent_encrypt(void *ctx, u8 *dst, const u8 *src)
373 *k = ((struct serpent_ctx *)ctx)->expkey,
374 *s = (const u32 *)src;
379 * Note: The conversions between u8* and u32* might cause trouble
380 * on architectures with stricter alignment rules than x86
383 r0 = le32_to_cpu(s[0]);
384 r1 = le32_to_cpu(s[1]);
385 r2 = le32_to_cpu(s[2]);
386 r3 = le32_to_cpu(s[3]);
389 S0(r0,r1,r2,r3,r4); LK(r2,r1,r3,r0,r4,1);
390 S1(r2,r1,r3,r0,r4); LK(r4,r3,r0,r2,r1,2);
391 S2(r4,r3,r0,r2,r1); LK(r1,r3,r4,r2,r0,3);
392 S3(r1,r3,r4,r2,r0); LK(r2,r0,r3,r1,r4,4);
393 S4(r2,r0,r3,r1,r4); LK(r0,r3,r1,r4,r2,5);
394 S5(r0,r3,r1,r4,r2); LK(r2,r0,r3,r4,r1,6);
395 S6(r2,r0,r3,r4,r1); LK(r3,r1,r0,r4,r2,7);
396 S7(r3,r1,r0,r4,r2); LK(r2,r0,r4,r3,r1,8);
397 S0(r2,r0,r4,r3,r1); LK(r4,r0,r3,r2,r1,9);
398 S1(r4,r0,r3,r2,r1); LK(r1,r3,r2,r4,r0,10);
399 S2(r1,r3,r2,r4,r0); LK(r0,r3,r1,r4,r2,11);
400 S3(r0,r3,r1,r4,r2); LK(r4,r2,r3,r0,r1,12);
401 S4(r4,r2,r3,r0,r1); LK(r2,r3,r0,r1,r4,13);
402 S5(r2,r3,r0,r1,r4); LK(r4,r2,r3,r1,r0,14);
403 S6(r4,r2,r3,r1,r0); LK(r3,r0,r2,r1,r4,15);
404 S7(r3,r0,r2,r1,r4); LK(r4,r2,r1,r3,r0,16);
405 S0(r4,r2,r1,r3,r0); LK(r1,r2,r3,r4,r0,17);
406 S1(r1,r2,r3,r4,r0); LK(r0,r3,r4,r1,r2,18);
407 S2(r0,r3,r4,r1,r2); LK(r2,r3,r0,r1,r4,19);
408 S3(r2,r3,r0,r1,r4); LK(r1,r4,r3,r2,r0,20);
409 S4(r1,r4,r3,r2,r0); LK(r4,r3,r2,r0,r1,21);
410 S5(r4,r3,r2,r0,r1); LK(r1,r4,r3,r0,r2,22);
411 S6(r1,r4,r3,r0,r2); LK(r3,r2,r4,r0,r1,23);
412 S7(r3,r2,r4,r0,r1); LK(r1,r4,r0,r3,r2,24);
413 S0(r1,r4,r0,r3,r2); LK(r0,r4,r3,r1,r2,25);
414 S1(r0,r4,r3,r1,r2); LK(r2,r3,r1,r0,r4,26);
415 S2(r2,r3,r1,r0,r4); LK(r4,r3,r2,r0,r1,27);
416 S3(r4,r3,r2,r0,r1); LK(r0,r1,r3,r4,r2,28);
417 S4(r0,r1,r3,r4,r2); LK(r1,r3,r4,r2,r0,29);
418 S5(r1,r3,r4,r2,r0); LK(r0,r1,r3,r2,r4,30);
419 S6(r0,r1,r3,r2,r4); LK(r3,r4,r1,r2,r0,31);
420 S7(r3,r4,r1,r2,r0); K(r0,r1,r2,r3,32);
422 d[0] = cpu_to_le32(r0);
423 d[1] = cpu_to_le32(r1);
424 d[2] = cpu_to_le32(r2);
425 d[3] = cpu_to_le32(r3);
428 static void serpent_decrypt(void *ctx, u8 *dst, const u8 *src)
431 *k = ((struct serpent_ctx *)ctx)->expkey,
432 *s = (const u32 *)src;
436 r0 = le32_to_cpu(s[0]);
437 r1 = le32_to_cpu(s[1]);
438 r2 = le32_to_cpu(s[2]);
439 r3 = le32_to_cpu(s[3]);
442 SI7(r0,r1,r2,r3,r4); KL(r1,r3,r0,r4,r2,31);
443 SI6(r1,r3,r0,r4,r2); KL(r0,r2,r4,r1,r3,30);
444 SI5(r0,r2,r4,r1,r3); KL(r2,r3,r0,r4,r1,29);
445 SI4(r2,r3,r0,r4,r1); KL(r2,r0,r1,r4,r3,28);
446 SI3(r2,r0,r1,r4,r3); KL(r1,r2,r3,r4,r0,27);
447 SI2(r1,r2,r3,r4,r0); KL(r2,r0,r4,r3,r1,26);
448 SI1(r2,r0,r4,r3,r1); KL(r1,r0,r4,r3,r2,25);
449 SI0(r1,r0,r4,r3,r2); KL(r4,r2,r0,r1,r3,24);
450 SI7(r4,r2,r0,r1,r3); KL(r2,r1,r4,r3,r0,23);
451 SI6(r2,r1,r4,r3,r0); KL(r4,r0,r3,r2,r1,22);
452 SI5(r4,r0,r3,r2,r1); KL(r0,r1,r4,r3,r2,21);
453 SI4(r0,r1,r4,r3,r2); KL(r0,r4,r2,r3,r1,20);
454 SI3(r0,r4,r2,r3,r1); KL(r2,r0,r1,r3,r4,19);
455 SI2(r2,r0,r1,r3,r4); KL(r0,r4,r3,r1,r2,18);
456 SI1(r0,r4,r3,r1,r2); KL(r2,r4,r3,r1,r0,17);
457 SI0(r2,r4,r3,r1,r0); KL(r3,r0,r4,r2,r1,16);
458 SI7(r3,r0,r4,r2,r1); KL(r0,r2,r3,r1,r4,15);
459 SI6(r0,r2,r3,r1,r4); KL(r3,r4,r1,r0,r2,14);
460 SI5(r3,r4,r1,r0,r2); KL(r4,r2,r3,r1,r0,13);
461 SI4(r4,r2,r3,r1,r0); KL(r4,r3,r0,r1,r2,12);
462 SI3(r4,r3,r0,r1,r2); KL(r0,r4,r2,r1,r3,11);
463 SI2(r0,r4,r2,r1,r3); KL(r4,r3,r1,r2,r0,10);
464 SI1(r4,r3,r1,r2,r0); KL(r0,r3,r1,r2,r4,9);
465 SI0(r0,r3,r1,r2,r4); KL(r1,r4,r3,r0,r2,8);
466 SI7(r1,r4,r3,r0,r2); KL(r4,r0,r1,r2,r3,7);
467 SI6(r4,r0,r1,r2,r3); KL(r1,r3,r2,r4,r0,6);
468 SI5(r1,r3,r2,r4,r0); KL(r3,r0,r1,r2,r4,5);
469 SI4(r3,r0,r1,r2,r4); KL(r3,r1,r4,r2,r0,4);
470 SI3(r3,r1,r4,r2,r0); KL(r4,r3,r0,r2,r1,3);
471 SI2(r4,r3,r0,r2,r1); KL(r3,r1,r2,r0,r4,2);
472 SI1(r3,r1,r2,r0,r4); KL(r4,r1,r2,r0,r3,1);
473 SI0(r4,r1,r2,r0,r3); K(r2,r3,r1,r4,0);
475 d[0] = cpu_to_le32(r2);
476 d[1] = cpu_to_le32(r3);
477 d[2] = cpu_to_le32(r1);
478 d[3] = cpu_to_le32(r4);
481 static struct crypto_alg serpent_alg = {
482 .cra_name = "serpent",
483 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
484 .cra_blocksize = SERPENT_BLOCK_SIZE,
485 .cra_ctxsize = sizeof(struct serpent_ctx),
486 .cra_module = THIS_MODULE,
487 .cra_list = LIST_HEAD_INIT(serpent_alg.cra_list),
488 .cra_u = { .cipher = {
489 .cia_min_keysize = SERPENT_MIN_KEY_SIZE,
490 .cia_max_keysize = SERPENT_MAX_KEY_SIZE,
491 .cia_setkey = serpent_setkey,
492 .cia_encrypt = serpent_encrypt,
493 .cia_decrypt = serpent_decrypt } }
496 static int tnepres_setkey(void *ctx, const u8 *key, unsigned int keylen, u32 *flags)
498 u8 rev_key[SERPENT_MAX_KEY_SIZE];
501 if ((keylen < SERPENT_MIN_KEY_SIZE)
502 || (keylen > SERPENT_MAX_KEY_SIZE)) {
503 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
507 for (i = 0; i < keylen; ++i)
508 rev_key[keylen - i - 1] = key[i];
510 return serpent_setkey(ctx, rev_key, keylen, flags);
513 static void tnepres_encrypt(void *ctx, u8 *dst, const u8 *src)
515 const u32 * const s = (const u32 * const)src;
516 u32 * const d = (u32 * const)dst;
520 rs[0] = swab32(s[3]);
521 rs[1] = swab32(s[2]);
522 rs[2] = swab32(s[1]);
523 rs[3] = swab32(s[0]);
525 serpent_encrypt(ctx, (u8 *)rd, (u8 *)rs);
527 d[0] = swab32(rd[3]);
528 d[1] = swab32(rd[2]);
529 d[2] = swab32(rd[1]);
530 d[3] = swab32(rd[0]);
533 static void tnepres_decrypt(void *ctx, u8 *dst, const u8 *src)
535 const u32 * const s = (const u32 * const)src;
536 u32 * const d = (u32 * const)dst;
540 rs[0] = swab32(s[3]);
541 rs[1] = swab32(s[2]);
542 rs[2] = swab32(s[1]);
543 rs[3] = swab32(s[0]);
545 serpent_decrypt(ctx, (u8 *)rd, (u8 *)rs);
547 d[0] = swab32(rd[3]);
548 d[1] = swab32(rd[2]);
549 d[2] = swab32(rd[1]);
550 d[3] = swab32(rd[0]);
553 static struct crypto_alg tnepres_alg = {
554 .cra_name = "tnepres",
555 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
556 .cra_blocksize = SERPENT_BLOCK_SIZE,
557 .cra_ctxsize = sizeof(struct serpent_ctx),
558 .cra_module = THIS_MODULE,
559 .cra_list = LIST_HEAD_INIT(serpent_alg.cra_list),
560 .cra_u = { .cipher = {
561 .cia_min_keysize = SERPENT_MIN_KEY_SIZE,
562 .cia_max_keysize = SERPENT_MAX_KEY_SIZE,
563 .cia_setkey = tnepres_setkey,
564 .cia_encrypt = tnepres_encrypt,
565 .cia_decrypt = tnepres_decrypt } }
568 static int __init init(void)
570 int ret = crypto_register_alg(&serpent_alg);
575 ret = crypto_register_alg(&tnepres_alg);
578 crypto_unregister_alg(&serpent_alg);
583 static void __exit fini(void)
585 crypto_unregister_alg(&tnepres_alg);
586 crypto_unregister_alg(&serpent_alg);
592 MODULE_LICENSE("GPL");
593 MODULE_DESCRIPTION("Serpent and tnepres (kerneli compatible serpent reversed) Cipher Algorithm");
594 MODULE_AUTHOR("Dag Arne Osvik <osvik@ii.uib.no>");