32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
39 #if (TWOFISH_SUPPORT == ENABLED)
42 static const uint8_t mds[4][4] =
44 {0x01, 0xEF, 0x5B, 0x5B},
45 {0x5B, 0xEF, 0xEF, 0x01},
46 {0xEF, 0x5B, 0x01, 0xEF},
47 {0xEF, 0x01, 0xEF, 0x5B}
51 static const uint8_t rs[4][8] =
53 {0x01, 0xA4, 0x55, 0x87, 0x5A, 0x58, 0xDB, 0x9E},
54 {0xA4, 0x56, 0x82, 0xF3, 0x1E, 0xC6, 0x68, 0xE5},
55 {0x02, 0xA1, 0xFC, 0xC1, 0x47, 0xAE, 0x3D, 0x19},
56 {0xA4, 0x55, 0x87, 0x5A, 0x58, 0xDB, 0x9E, 0x03}
60 static const uint8_t q0[256] =
62 0xA9, 0x67, 0xB3, 0xE8, 0x04, 0xFD, 0xA3, 0x76, 0x9A, 0x92, 0x80, 0x78, 0xE4, 0xDD, 0xD1, 0x38,
63 0x0D, 0xC6, 0x35, 0x98, 0x18, 0xF7, 0xEC, 0x6C, 0x43, 0x75, 0x37, 0x26, 0xFA, 0x13, 0x94, 0x48,
64 0xF2, 0xD0, 0x8B, 0x30, 0x84, 0x54, 0xDF, 0x23, 0x19, 0x5B, 0x3D, 0x59, 0xF3, 0xAE, 0xA2, 0x82,
65 0x63, 0x01, 0x83, 0x2E, 0xD9, 0x51, 0x9B, 0x7C, 0xA6, 0xEB, 0xA5, 0xBE, 0x16, 0x0C, 0xE3, 0x61,
66 0xC0, 0x8C, 0x3A, 0xF5, 0x73, 0x2C, 0x25, 0x0B, 0xBB, 0x4E, 0x89, 0x6B, 0x53, 0x6A, 0xB4, 0xF1,
67 0xE1, 0xE6, 0xBD, 0x45, 0xE2, 0xF4, 0xB6, 0x66, 0xCC, 0x95, 0x03, 0x56, 0xD4, 0x1C, 0x1E, 0xD7,
68 0xFB, 0xC3, 0x8E, 0xB5, 0xE9, 0xCF, 0xBF, 0xBA, 0xEA, 0x77, 0x39, 0xAF,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x43, 0x69,
96 0x29, 0x2E, 0xAC, 0x15, 0x59, 0xA8, 0x0A, 0x9E, 0x6E, 0x47, 0xDF, 0x34, 0x35, 0x6A, 0xCF, 0xDC,
97 0x22, 0xC9, 0xC0, 0x9B, 0x89, 0xD4, 0xED, 0xAB, 0x12, 0xA2, 0x0D, 0x52, 0xBB, 0x02, 0x2F, 0xA9,
98 0xD7, 0x61, 0x1E, 0xB4, 0x50, 0x04, 0xF6, 0xC2, 0x16, 0x25, 0x86, 0x56, 0x55, 0x09, 0xBE, 0x91
125 static uint32_t GF_MUL(uint8_t
a, uint8_t
b, uint8_t
p)
134 for(i = 0; i < 8; i++)
172 static uint32_t H_SUB(uint8_t
x,
const uint32_t *
l,
uint_t k,
uint_t i)
180 x = (i == 1 || i == 2) ? q0[
x] : q1[
x];
182 x ^= (
l[3] >> (8 * i)) & 0xFF;
189 x = (i == 2 || i == 3) ? q0[
x] : q1[
x];
191 x ^= (
l[2] >> (8 * i)) & 0xFF;
195 x = (i == 0 || i == 2) ? q0[
x] : q1[
x];
196 x ^= (
l[1] >> (8 * i)) & 0xFF;
197 x = (i == 0 || i == 1) ? q0[
x] : q1[
x];
198 x ^= (
l[0] >> (8 * i)) & 0xFF;
201 x = (i == 1 || i == 3) ? q0[
x] : q1[
x];
204 z = GF_MUL(mds[0][i],
x, 0x69);
205 z |= GF_MUL(mds[1][i],
x, 0x69) << 8;
206 z |= GF_MUL(mds[2][i],
x, 0x69) << 16;
207 z |= GF_MUL(mds[3][i],
x, 0x69) << 24;
222 static uint32_t
H(uint8_t
x,
const uint32_t *
l,
uint_t k)
231 for(i = 0; i < 4; i++)
233 z ^= H_SUB(
x,
l, k, i);
262 if(context == NULL || key == NULL)
266 if(keyLen != 16 && keyLen != 24 && keyLen != 32)
274 for(i = 0; i < k; i++)
281 for(i = 0; i < k; i++)
287 for(j = 0; j < 4; j++)
291 for(
a = 0,
n = 0;
n < 8;
n++)
293 a ^= GF_MUL(rs[j][
n], key[8 * i +
n], 0x4D);
297 s[k - i - 1] |=
a << (8 * j);
302 for(i = 0; i < 256; i++)
304 context->
s1[i] = H_SUB(i,
s, k, 0);
305 context->
s2[i] = H_SUB(i,
s, k, 1);
306 context->
s3[i] = H_SUB(i,
s, k, 2);
307 context->
s4[i] = H_SUB(i,
s, k, 3);
311 for(i = 0; i < 20; i++)
315 b =
H(2 * i + 1, mo, k);
318 context->
k[2 * i] =
a;
320 context->
k[2 * i + 1] =
ROL32(
a, 9);
360 for(i = 0; i < 32; i += 4)
363 t0 = context->
s1[r0 & 0xFF];
364 t0 ^= context->
s2[(r0 >> 8) & 0xFF];
365 t0 ^= context->
s3[(r0 >> 16) & 0xFF];
366 t0 ^= context->
s4[(r0 >> 24) & 0xFF];
368 t1 = context->
s2[r1 & 0xFF];
369 t1 ^= context->
s3[(r1 >> 8) & 0xFF];
370 t1 ^= context->
s4[(r1 >> 16) & 0xFF];
371 t1 ^= context->
s1[(r1 >> 24) & 0xFF];
373 r2 ^= t0 +
t1 + context->
k[8 + i];
376 r3 ^= (t0 +
t1 +
t1 + context->
k[9 + i]);
379 t0 = context->
s1[r2 & 0xFF];
380 t0 ^= context->
s2[(r2 >> 8) & 0xFF];
381 t0 ^= context->
s3[(r2 >> 16) & 0xFF];
382 t0 ^= context->
s4[(r2 >> 24) & 0xFF];
384 t1 = context->
s2[r3 & 0xFF];
385 t1 ^= context->
s3[(r3 >> 8) & 0xFF];
386 t1 ^= context->
s4[(r3 >> 16) & 0xFF];
387 t1 ^= context->
s1[(r3 >> 24) & 0xFF];
389 r0 ^= t0 +
t1 + context->
k[10 + i];
392 r1 ^= (t0 +
t1 +
t1 + context->
k[11 + i]);
442 for(i = 0; i < 32; i += 4)
445 t0 = context->
s1[r2 & 0xFF];
446 t0 ^= context->
s2[(r2 >> 8) & 0xFF];
447 t0 ^= context->
s3[(r2 >> 16) & 0xFF];
448 t0 ^= context->
s4[(r2 >> 24) & 0xFF];
450 t1 = context->
s2[r3 & 0xFF];
451 t1 ^= context->
s3[(r3 >> 8) & 0xFF];
452 t1 ^= context->
s4[(r3 >> 16) & 0xFF];
453 t1 ^= context->
s1[(r3 >> 24) & 0xFF];
456 r0 ^= t0 +
t1 + context->
k[38 - i];
457 r1 ^= (t0 +
t1 +
t1 + context->
k[39 - i]);
461 t0 = context->
s1[r0 & 0xFF];
462 t0 ^= context->
s2[(r0 >> 8) & 0xFF];
463 t0 ^= context->
s3[(r0 >> 16) & 0xFF];
464 t0 ^= context->
s4[(r0 >> 24) & 0xFF];
466 t1 = context->
s2[r1 & 0xFF];
467 t1 ^= context->
s3[(r1 >> 8) & 0xFF];
468 t1 ^= context->
s4[(r1 >> 16) & 0xFF];
469 t1 ^= context->
s1[(r1 >> 24) & 0xFF];
472 r2 ^= t0 +
t1 + context->
k[36 - i];
473 r3 ^= (t0 +
t1 +
t1 + context->
k[37 - i]);