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