38 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
45 #if (SERPENT_SUPPORT == ENABLED)
48 #define PHI 0x9E3779B9
51 #define SBOX0(r0, r1, r2, r3) \
63 r3 = r0; r0 = r1; r1 = r4; \
67 #define SBOX0_INV(r0, r1, r2, r3) \
85 #define SBOX1(r0, r1, r2, r3) \
98 r4 = r0; r0 = r2; r2 = r3; r3 = r1; r1 = r4; \
102 #define SBOX1_INV(r0, r1, r2, r3) \
106 r3 &= r1; r4 ^= r2; \
107 r3 ^= r0; r0 |= r1; \
108 r2 ^= r3; r0 ^= r4; \
109 r0 |= r2; r1 ^= r3; \
110 r0 ^= r1; r1 |= r3; \
111 r1 ^= r0; r4 = ~r4; \
112 r4 ^= r1; r1 |= r0; \
116 r1 = r0; r0 = r4; r4 = r2; r2 = r3; r3 = r4; \
120 #define SBOX2(r0, r1, r2, r3) \
124 r0 ^= r3; r2 ^= r1; \
125 r2 ^= r0; r3 |= r4; \
126 r3 ^= r1; r4 ^= r2; \
128 r3 ^= r0; r0 &= r1; \
129 r4 ^= r0; r1 ^= r3; \
130 r1 ^= r4; r4 = ~r4; \
131 r0 = r2; r2 = r1; r1 = r3; r3 = r4; \
135 #define SBOX2_INV(r0, r1, r2, r3) \
138 r2 ^= r3; r3 ^= r0; \
140 r3 ^= r1; r1 |= r2; \
141 r1 ^= r4; r4 &= r3; \
142 r2 ^= r3; r4 &= r0; \
143 r4 ^= r2; r2 &= r1; \
144 r2 |= r0; r3 = ~r3; \
145 r2 ^= r3; r0 ^= r3; \
146 r0 &= r1; r3 ^= r4; \
152 #define SBOX3(r0, r1, r2, r3) \
156 r3 ^= r1; r1 &= r4; \
157 r4 ^= r2; r2 ^= r3; \
158 r3 &= r0; r4 |= r1; \
159 r3 ^= r4; r0 ^= r1; \
160 r4 &= r0; r1 ^= r3; \
161 r4 ^= r2; r1 |= r0; \
162 r1 ^= r2; r0 ^= r3; \
165 r0 = r1; r1 = r2; r2 = r3; r3 = r4; \
169 #define SBOX3_INV(r0, r1, r2, r3) \
173 r0 ^= r2; r4 &= r2; \
174 r4 ^= r0; r0 &= r1; \
175 r1 ^= r3; r3 |= r4; \
176 r2 ^= r3; r0 ^= r3; \
177 r1 ^= r4; r3 &= r2; \
178 r3 ^= r1; r1 ^= r0; \
179 r1 |= r2; r0 ^= r3; \
182 r4 = r0; r0 = r2; r2 = r3; r3 = r4; \
186 #define SBOX4(r0, r1, r2, r3) \
189 r1 ^= r3; r3 = ~r3; \
190 r2 ^= r3; r3 ^= r0; \
192 r1 ^= r2; r4 ^= r3; \
193 r0 ^= r4; r2 &= r4; \
194 r2 ^= r0; r0 &= r1; \
195 r3 ^= r0; r4 |= r1; \
196 r4 ^= r0; r0 |= r3; \
197 r0 ^= r2; r2 &= r3; \
198 r0 = ~r0; r4 ^= r2; \
199 r2 = r0; r0 = r1; r1 = r4; \
204 #define SBOX4_INV(r0, r1, r2, r3) \
208 r2 ^= r1; r1 |= r3; \
209 r1 &= r0; r4 ^= r2; \
210 r4 ^= r1; r1 &= r2; \
211 r0 = ~r0; r3 ^= r4; \
212 r1 ^= r3; r3 &= r0; \
213 r3 ^= r2; r0 ^= r1; \
214 r2 &= r0; r3 ^= r0; \
216 r2 |= r3; r3 ^= r0; \
222 #define SBOX5(r0, r1, r2, r3) \
225 r0 ^= r1; r1 ^= r3; \
227 r1 &= r0; r2 ^= r3; \
228 r1 ^= r2; r2 |= r4; \
229 r4 ^= r3; r3 &= r1; \
230 r3 ^= r0; r4 ^= r1; \
231 r4 ^= r2; r2 ^= r0; \
232 r0 &= r3; r2 = ~r2; \
233 r0 ^= r4; r4 |= r3; \
235 r4 = r0; r0 = r1; r1 = r3; r3 = r2; r2 = r4; \
239 #define SBOX5_INV(r0, r1, r2, r3) \
243 r2 ^= r1; r3 |= r0; \
244 r3 ^= r2; r2 |= r1; \
245 r2 &= r0; r4 ^= r3; \
246 r2 ^= r4; r4 |= r0; \
247 r4 ^= r1; r1 &= r2; \
248 r1 ^= r3; r4 ^= r2; \
249 r3 &= r4; r4 ^= r1; \
250 r3 ^= r4; r4 = ~r4; \
252 r0 = r1; r1 = r4; r4 = r2; r2 = r3; r3 = r4; \
256 #define SBOX6(r0, r1, r2, r3) \
260 r3 &= r0; r0 ^= r4; \
261 r3 ^= r2; r2 |= r4; \
262 r1 ^= r3; r2 ^= r0; \
263 r0 |= r1; r2 ^= r1; \
264 r4 ^= r0; r0 |= r3; \
265 r0 ^= r2; r4 ^= r3; \
266 r4 ^= r0; r3 = ~r3; \
273 #define SBOX6_INV(r0, r1, r2, r3) \
277 r2 &= r0; r4 ^= r3; \
278 r2 = ~r2; r3 ^= r1; \
279 r2 ^= r3; r4 |= r0; \
280 r0 ^= r2; r3 ^= r4; \
281 r4 ^= r1; r1 &= r3; \
282 r1 ^= r0; r0 ^= r3; \
283 r0 |= r2; r3 ^= r1; \
285 r0 = r1; r1 = r2; r2 = r4; \
289 #define SBOX7(r0, r1, r2, r3) \
293 r1 ^= r3; r4 ^= r2; \
294 r2 ^= r1; r3 |= r4; \
295 r3 &= r0; r4 ^= r2; \
296 r3 ^= r1; r1 |= r4; \
297 r1 ^= r0; r0 |= r4; \
298 r0 ^= r2; r1 ^= r4; \
299 r2 ^= r1; r1 &= r0; \
300 r1 ^= r4; r2 = ~r2; \
303 r2 = r1; r1 = r3; r3 = r0; r0 = r4; \
307 #define SBOX7_INV(r0, r1, r2, r3) \
311 r0 &= r3; r4 |= r3; \
312 r2 = ~r2; r3 ^= r1; \
313 r1 |= r0; r0 ^= r2; \
314 r2 &= r4; r3 &= r4; \
315 r1 ^= r2; r2 ^= r0; \
316 r0 |= r2; r4 ^= r1; \
317 r0 ^= r3; r3 ^= r4; \
318 r4 |= r0; r3 ^= r2; \
320 r2 = r1; r1 = r0; r0 = r3; r3 = r4; \
324 #define LT(x0, x1, x2, x3) \
326 x0 = ROL32(x0, 13); \
329 x3 ^= x2 ^ (x0 << 3); \
333 x2 ^= x3 ^ (x1 << 7); \
335 x2 = ROL32(x2, 22); \
339 #define LT_INV(x0, x1, x2, x3) \
341 x2 = ROR32(x2, 22); \
343 x2 ^= x3 ^ (x1 << 7); \
347 x3 ^= x2 ^ (x0 << 3); \
350 x0 = ROR32(x0, 13); \
354 #define XOR(x0, x1, x2, x3, k) \
363 #define ROUND(n, x0, x1, x2, x3, k) \
365 XOR(x0, x1, x2, x3, k); \
366 SBOX##n(x0, x1, x2, x3); \
367 LT(x0, x1, x2, x3); \
371 #define ROUND_INV(n, x0, x1, x2, x3, k) \
373 LT_INV(x0, x1, x2, x3); \
374 SBOX##n##_INV(x0, x1, x2, x3); \
375 XOR(x0, x1, x2, x3, k); \
410 if(context == NULL || key == NULL)
414 if(keyLen != 16 && keyLen != 24 && keyLen != 32)
421 for(i = 0; i < keyLen; i++)
440 w = (uint32_t *) context->
k;
443 t =
p[0] ^
p[3] ^
p[5] ^
p[7] ^
PHI ^ 0;
445 t =
p[1] ^
p[4] ^
p[6] ^ w[0] ^
PHI ^ 1;
447 t =
p[2] ^
p[5] ^
p[7] ^ w[1] ^
PHI ^ 2;
449 t =
p[3] ^
p[6] ^ w[0] ^ w[2] ^
PHI ^ 3;
451 t =
p[4] ^
p[7] ^ w[1] ^ w[3] ^
PHI ^ 4;
453 t =
p[5] ^ w[0] ^ w[2] ^ w[4] ^
PHI ^ 5;
455 t =
p[6] ^ w[1] ^ w[3] ^ w[5] ^
PHI ^ 6;
457 t =
p[7] ^ w[2] ^ w[4] ^ w[6] ^
PHI ^ 7;
461 for(i = 8; i < 132; i++)
463 t = w[i - 8] ^ w[i - 5] ^ w[i - 3] ^ w[i - 1] ^
PHI ^ i;
468 for(i = 0; i < 128; i += 32)
470 SBOX3(w[i + 0], w[i + 1], w[i + 2], w[i + 3]);
471 SBOX2(w[i + 4], w[i + 5], w[i + 6], w[i + 7]);
472 SBOX1(w[i + 8], w[i + 9], w[i + 10], w[i + 11]);
473 SBOX0(w[i + 12], w[i + 13], w[i + 14], w[i + 15]);
474 SBOX7(w[i + 16], w[i + 17], w[i + 18], w[i + 19]);
475 SBOX6(w[i + 20], w[i + 21], w[i + 22], w[i + 23]);
476 SBOX5(w[i + 24], w[i + 25], w[i + 26], w[i + 27]);
477 SBOX4(w[i + 28], w[i + 29], w[i + 30], w[i + 31]);
481 SBOX3(w[128], w[129], w[130], w[131]);
511 for(i = 0; i < 32; i += 8)
513 ROUND(0, r0, r1, r2, r3, context->
k[i]);
514 ROUND(1, r0, r1, r2, r3, context->
k[i + 1]);
515 ROUND(2, r0, r1, r2, r3, context->
k[i + 2]);
516 ROUND(3, r0, r1, r2, r3, context->
k[i + 3]);
517 ROUND(4, r0, r1, r2, r3, context->
k[i + 4]);
518 ROUND(5, r0, r1, r2, r3, context->
k[i + 5]);
519 ROUND(6, r0, r1, r2, r3, context->
k[i + 6]);
520 ROUND(7, r0, r1, r2, r3, context->
k[i + 7]);
526 XOR(r0, r1, r2, r3, context->
k[32]);
560 XOR(r0, r1, r2, r3, context->
k[32]);
566 for(i = 0; i < 32; i += 8)
568 ROUND_INV(7, r0, r1, r2, r3, context->
k[31 - i]);
569 ROUND_INV(6, r0, r1, r2, r3, context->
k[30 - i]);
570 ROUND_INV(5, r0, r1, r2, r3, context->
k[29 - i]);
571 ROUND_INV(4, r0, r1, r2, r3, context->
k[28 - i]);
572 ROUND_INV(3, r0, r1, r2, r3, context->
k[27 - i]);
573 ROUND_INV(2, r0, r1, r2, r3, context->
k[26 - i]);
574 ROUND_INV(1, r0, r1, r2, r3, context->
k[25 - i]);
575 ROUND_INV(0, r0, r1, r2, r3, context->
k[24 - i]);