1
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
3
* LibTomCrypt is a library that provides various cryptographic
4
* algorithms in a highly modular and flexible manner.
6
* The library is free for all purposes without any express
9
* Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
12
/* Implementation of Twofish by Tom St Denis */
17
/* first TWOFISH_ALL_TABLES must ensure TWOFISH_TABLES is defined */
18
#ifdef TWOFISH_ALL_TABLES
19
#ifndef TWOFISH_TABLES
20
#define TWOFISH_TABLES
24
const struct _cipher_descriptor twofish_desc =
36
/* the two polynomials */
37
#define MDS_POLY 0x169
40
/* The 4x4 MDS Linear Transform */
42
static const unsigned char MDS[4][4] = {
43
{ 0x01, 0xEF, 0x5B, 0x5B },
44
{ 0x5B, 0xEF, 0xEF, 0x01 },
45
{ 0xEF, 0x5B, 0x01, 0xEF },
46
{ 0xEF, 0x01, 0xEF, 0x5B }
50
/* The 4x8 RS Linear Transform */
51
static const unsigned char RS[4][8] = {
52
{ 0x01, 0xA4, 0x55, 0x87, 0x5A, 0x58, 0xDB, 0x9E },
53
{ 0xA4, 0x56, 0x82, 0xF3, 0X1E, 0XC6, 0X68, 0XE5 },
54
{ 0X02, 0XA1, 0XFC, 0XC1, 0X47, 0XAE, 0X3D, 0X19 },
55
{ 0XA4, 0X55, 0X87, 0X5A, 0X58, 0XDB, 0X9E, 0X03 }
58
/* sbox usage orderings */
59
static const unsigned char qord[4][5] = {
68
#include "twofish_tab.c"
70
#define sbox(i, x) ((ulong32)SBOX[i][(x)&255])
74
/* The Q-box tables */
75
static const unsigned char qbox[2][4][16] = {
77
{ 0x8, 0x1, 0x7, 0xD, 0x6, 0xF, 0x3, 0x2, 0x0, 0xB, 0x5, 0x9, 0xE, 0xC, 0xA, 0x4 },
78
{ 0xE, 0XC, 0XB, 0X8, 0X1, 0X2, 0X3, 0X5, 0XF, 0X4, 0XA, 0X6, 0X7, 0X0, 0X9, 0XD },
79
{ 0XB, 0XA, 0X5, 0XE, 0X6, 0XD, 0X9, 0X0, 0XC, 0X8, 0XF, 0X3, 0X2, 0X4, 0X7, 0X1 },
80
{ 0XD, 0X7, 0XF, 0X4, 0X1, 0X2, 0X6, 0XE, 0X9, 0XB, 0X3, 0X0, 0X8, 0X5, 0XC, 0XA }
83
{ 0X2, 0X8, 0XB, 0XD, 0XF, 0X7, 0X6, 0XE, 0X3, 0X1, 0X9, 0X4, 0X0, 0XA, 0XC, 0X5 },
84
{ 0X1, 0XE, 0X2, 0XB, 0X4, 0XC, 0X3, 0X7, 0X6, 0XD, 0XA, 0X5, 0XF, 0X9, 0X0, 0X8 },
85
{ 0X4, 0XC, 0X7, 0X5, 0X1, 0X6, 0X9, 0XA, 0X0, 0XE, 0XD, 0X8, 0X2, 0XB, 0X3, 0XF },
86
{ 0xB, 0X9, 0X5, 0X1, 0XC, 0X3, 0XD, 0XE, 0X6, 0X4, 0X7, 0XF, 0X2, 0X0, 0X8, 0XA }
92
static ulong32 _sbox(int i, ulong32 x)
94
static ulong32 sbox(int i, ulong32 x)
97
unsigned char a0,b0,a1,b1,a2,b2,a3,b3,a4,b4,y;
99
/* a0,b0 = [x/16], x mod 16 */
100
a0 = (unsigned char)((x>>4)&15);
101
b0 = (unsigned char)((x)&15);
106
/* b1 = a0 ^ ROR(b0, 1) ^ 8a0 */
107
b1 = (a0 ^ ((b0<<3)|(b0>>1)) ^ (a0<<3)) & 15;
109
/* a2,b2 = t0[a1], t1[b1] */
110
a2 = qbox[i][0][(int)a1];
111
b2 = qbox[i][1][(int)b1];
116
/* b3 = a2 ^ ROR(b2, 1) ^ 8a2 */
117
b3 = (a2 ^ ((b2<<3)|(b2>>1)) ^ (a2<<3)) & 15;
119
/* a4,b4 = t2[a3], t3[b3] */
120
a4 = qbox[i][2][(int)a3];
121
b4 = qbox[i][3][(int)b3];
131
static ulong32 sbox(int i, ulong32 x)
135
burn_stack(sizeof(unsigned char) * 11);
138
#endif /* CLEAN_STACK */
140
#endif /* TWOFISH_TABLES */
142
/* computes ab mod p */
143
static ulong32 gf_mult(ulong32 a, ulong32 b, ulong32 p)
145
ulong32 result, B[2], P[2];
149
result = P[0] = B[0] = 0;
151
/* unrolled branchless GF multiplier */
152
result ^= B[a&1]; a >>= 1; B[1] = P[B[1]>>7] ^ (B[1] << 1);
153
result ^= B[a&1]; a >>= 1; B[1] = P[B[1]>>7] ^ (B[1] << 1);
154
result ^= B[a&1]; a >>= 1; B[1] = P[B[1]>>7] ^ (B[1] << 1);
155
result ^= B[a&1]; a >>= 1; B[1] = P[B[1]>>7] ^ (B[1] << 1);
156
result ^= B[a&1]; a >>= 1; B[1] = P[B[1]>>7] ^ (B[1] << 1);
157
result ^= B[a&1]; a >>= 1; B[1] = P[B[1]>>7] ^ (B[1] << 1);
158
result ^= B[a&1]; a >>= 1; B[1] = P[B[1]>>7] ^ (B[1] << 1);
164
/* computes [y0 y1 y2 y3] = MDS . [x0] */
165
#ifndef TWOFISH_TABLES
166
static ulong32 mds_column_mult(unsigned char in, int col)
168
ulong32 x01, x5B, xEF;
171
x5B = gf_mult(in, 0x5B, MDS_POLY);
172
xEF = gf_mult(in, 0xEF, MDS_POLY);
196
/* avoid warnings, we'd never get here normally but just to calm compiler warnings... */
200
#else /* !TWOFISH_TABLES */
202
#define mds_column_mult(x, i) mds_tab[i][x]
204
#endif /* TWOFISH_TABLES */
206
/* Computes [y0 y1 y2 y3] = MDS . [x0 x1 x2 x3] */
207
static void mds_mult(const unsigned char *in, unsigned char *out)
211
for (tmp = x = 0; x < 4; x++) {
212
tmp ^= mds_column_mult(in[x], x);
217
#ifdef TWOFISH_ALL_TABLES
218
/* computes [y0 y1 y2 y3] = RS . [x0 x1 x2 x3 x4 x5 x6 x7] */
219
static void rs_mult(const unsigned char *in, unsigned char *out)
222
tmp = rs_tab0[in[0]] ^ rs_tab1[in[1]] ^ rs_tab2[in[2]] ^ rs_tab3[in[3]] ^
223
rs_tab4[in[4]] ^ rs_tab5[in[5]] ^ rs_tab6[in[6]] ^ rs_tab7[in[7]];
227
#else /* !TWOFISH_ALL_TABLES */
229
/* computes [y0 y1 y2 y3] = RS . [x0 x1 x2 x3 x4 x5 x6 x7] */
230
static void rs_mult(const unsigned char *in, unsigned char *out)
233
for (x = 0; x < 4; x++) {
235
for (y = 0; y < 8; y++) {
236
out[x] ^= gf_mult(in[y], RS[x][y], RS_POLY);
244
static void h_func(const unsigned char *in, unsigned char *out, unsigned char *M, int k, int offset)
248
for (x = 0; x < 4; x++) {
253
y[0] = (unsigned char)(sbox(1, (ulong32)y[0]) ^ M[4 * (6 + offset) + 0]);
254
y[1] = (unsigned char)(sbox(0, (ulong32)y[1]) ^ M[4 * (6 + offset) + 1]);
255
y[2] = (unsigned char)(sbox(0, (ulong32)y[2]) ^ M[4 * (6 + offset) + 2]);
256
y[3] = (unsigned char)(sbox(1, (ulong32)y[3]) ^ M[4 * (6 + offset) + 3]);
258
y[0] = (unsigned char)(sbox(1, (ulong32)y[0]) ^ M[4 * (4 + offset) + 0]);
259
y[1] = (unsigned char)(sbox(1, (ulong32)y[1]) ^ M[4 * (4 + offset) + 1]);
260
y[2] = (unsigned char)(sbox(0, (ulong32)y[2]) ^ M[4 * (4 + offset) + 2]);
261
y[3] = (unsigned char)(sbox(0, (ulong32)y[3]) ^ M[4 * (4 + offset) + 3]);
263
y[0] = (unsigned char)(sbox(1, sbox(0, sbox(0, (ulong32)y[0]) ^ M[4 * (2 + offset) + 0]) ^ M[4 * (0 + offset) + 0]));
264
y[1] = (unsigned char)(sbox(0, sbox(0, sbox(1, (ulong32)y[1]) ^ M[4 * (2 + offset) + 1]) ^ M[4 * (0 + offset) + 1]));
265
y[2] = (unsigned char)(sbox(1, sbox(1, sbox(0, (ulong32)y[2]) ^ M[4 * (2 + offset) + 2]) ^ M[4 * (0 + offset) + 2]));
266
y[3] = (unsigned char)(sbox(0, sbox(1, sbox(1, (ulong32)y[3]) ^ M[4 * (2 + offset) + 3]) ^ M[4 * (0 + offset) + 3]));
271
#ifndef TWOFISH_SMALL
273
/* for GCC we don't use pointer aliases */
274
#if defined(__GNUC__)
275
#define S1 key->twofish.S[0]
276
#define S2 key->twofish.S[1]
277
#define S3 key->twofish.S[2]
278
#define S4 key->twofish.S[3]
282
#define g_func(x, dum) (S1[byte(x,0)] ^ S2[byte(x,1)] ^ S3[byte(x,2)] ^ S4[byte(x,3)])
283
#define g1_func(x, dum) (S2[byte(x,0)] ^ S3[byte(x,1)] ^ S4[byte(x,2)] ^ S1[byte(x,3)])
288
static ulong32 _g_func(ulong32 x, symmetric_key *key)
290
static ulong32 g_func(ulong32 x, symmetric_key *key)
293
unsigned char g, i, y, z;
297
for (y = 0; y < 4; y++) {
298
z = key->twofish.start;
300
/* do unkeyed substitution */
301
g = sbox(qord[y][z++], (x >> (8*y)) & 255);
306
/* do key mixing+sbox until z==5 */
308
g = g ^ key->twofish.S[4*i++ + y];
309
g = sbox(qord[y][z++], g);
312
/* multiply g by a column of the MDS */
313
res ^= mds_column_mult(g, y);
318
#define g1_func(x, key) g_func(ROL(x, 8), key)
321
static ulong32 g_func(ulong32 x, symmetric_key *key)
325
burn_stack(sizeof(unsigned char) * 4 + sizeof(ulong32));
328
#endif /* CLEAN_STACK */
330
#endif /* TWOFISH_SMALL */
333
static int _twofish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
335
int twofish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
338
#ifndef TWOFISH_SMALL
339
unsigned char S[4*4], tmpx0, tmpx1;
342
unsigned char tmp[4], tmp2[4], M[8*4];
345
_ARGCHK(key != NULL);
346
_ARGCHK(skey != NULL);
348
/* invalid arguments? */
349
if (num_rounds != 16 && num_rounds != 0) {
350
return CRYPT_INVALID_ROUNDS;
353
if (keylen != 16 && keylen != 24 && keylen != 32) {
354
return CRYPT_INVALID_KEYSIZE;
357
/* k = keysize/64 [but since our keysize is in bytes...] */
360
/* copy the key into M */
361
for (x = 0; x < keylen; x++) {
365
/* create the S[..] words */
366
#ifndef TWOFISH_SMALL
367
for (x = 0; x < k; x++) {
368
rs_mult(M+(x*8), S+(x*4));
371
for (x = 0; x < k; x++) {
372
rs_mult(M+(x*8), skey->twofish.S+(x*4));
377
for (x = 0; x < 20; x++) {
378
/* A = h(p * 2x, Me) */
379
for (y = 0; y < 4; y++) {
382
h_func(tmp, tmp2, M, k, 0);
385
/* B = ROL(h(p * (2x + 1), Mo), 8) */
386
for (y = 0; y < 4; y++) {
387
tmp[y] = (unsigned char)(x+x+1);
389
h_func(tmp, tmp2, M, k, 1);
394
skey->twofish.K[x+x] = (A + B) & 0xFFFFFFFFUL;
396
/* K[2i+1] = (A + 2B) <<< 9 */
397
skey->twofish.K[x+x+1] = ROL(B + B + A, 9);
400
#ifndef TWOFISH_SMALL
401
/* make the sboxes (large ram variant) */
403
for (x = 0; x < 256; x++) {
406
skey->twofish.S[0][x] = mds_column_mult(sbox(1, (sbox(0, tmpx0 ^ S[0]) ^ S[4])),0);
407
skey->twofish.S[1][x] = mds_column_mult(sbox(0, (sbox(0, tmpx1 ^ S[1]) ^ S[5])),1);
408
skey->twofish.S[2][x] = mds_column_mult(sbox(1, (sbox(1, tmpx0 ^ S[2]) ^ S[6])),2);
409
skey->twofish.S[3][x] = mds_column_mult(sbox(0, (sbox(1, tmpx1 ^ S[3]) ^ S[7])),3);
412
for (x = 0; x < 256; x++) {
415
skey->twofish.S[0][x] = mds_column_mult(sbox(1, (sbox(0, sbox(0, tmpx1 ^ S[0]) ^ S[4]) ^ S[8])),0);
416
skey->twofish.S[1][x] = mds_column_mult(sbox(0, (sbox(0, sbox(1, tmpx1 ^ S[1]) ^ S[5]) ^ S[9])),1);
417
skey->twofish.S[2][x] = mds_column_mult(sbox(1, (sbox(1, sbox(0, tmpx0 ^ S[2]) ^ S[6]) ^ S[10])),2);
418
skey->twofish.S[3][x] = mds_column_mult(sbox(0, (sbox(1, sbox(1, tmpx0 ^ S[3]) ^ S[7]) ^ S[11])),3);
421
for (x = 0; x < 256; x++) {
424
skey->twofish.S[0][x] = mds_column_mult(sbox(1, (sbox(0, sbox(0, sbox(1, tmpx1 ^ S[0]) ^ S[4]) ^ S[8]) ^ S[12])),0);
425
skey->twofish.S[1][x] = mds_column_mult(sbox(0, (sbox(0, sbox(1, sbox(1, tmpx0 ^ S[1]) ^ S[5]) ^ S[9]) ^ S[13])),1);
426
skey->twofish.S[2][x] = mds_column_mult(sbox(1, (sbox(1, sbox(0, sbox(0, tmpx0 ^ S[2]) ^ S[6]) ^ S[10]) ^ S[14])),2);
427
skey->twofish.S[3][x] = mds_column_mult(sbox(0, (sbox(1, sbox(1, sbox(0, tmpx1 ^ S[3]) ^ S[7]) ^ S[11]) ^ S[15])),3);
431
/* where to start in the sbox layers */
432
/* small ram variant */
434
case 4 : skey->twofish.start = 0; break;
435
case 3 : skey->twofish.start = 1; break;
436
default: skey->twofish.start = 2; break;
443
int twofish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
446
x = _twofish_setup(key, keylen, num_rounds, skey);
447
burn_stack(sizeof(int) * 7 + sizeof(unsigned char) * 56 + sizeof(ulong32) * 2);
453
static void _twofish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
455
void twofish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
458
ulong32 a,b,c,d,ta,tb,tc,td,t1,t2, *k;
460
#if !defined(TWOFISH_SMALL) && !defined(__GNUC__)
461
ulong32 *S1, *S2, *S3, *S4;
466
_ARGCHK(key != NULL);
468
#if !defined(TWOFISH_SMALL) && !defined(__GNUC__)
469
S1 = key->twofish.S[0];
470
S2 = key->twofish.S[1];
471
S3 = key->twofish.S[2];
472
S4 = key->twofish.S[3];
475
LOAD32L(a,&pt[0]); LOAD32L(b,&pt[4]);
476
LOAD32L(c,&pt[8]); LOAD32L(d,&pt[12]);
477
a ^= key->twofish.K[0];
478
b ^= key->twofish.K[1];
479
c ^= key->twofish.K[2];
480
d ^= key->twofish.K[3];
482
k = key->twofish.K + 8;
483
for (r = 8; r != 0; --r) {
484
t2 = g1_func(b, key);
485
t1 = g_func(a, key) + t2;
486
c = ROR(c ^ (t1 + k[0]), 1);
487
d = ROL(d, 1) ^ (t2 + t1 + k[1]);
489
t2 = g1_func(d, key);
490
t1 = g_func(c, key) + t2;
491
a = ROR(a ^ (t1 + k[2]), 1);
492
b = ROL(b, 1) ^ (t2 + t1 + k[3]);
496
/* output with "undo last swap" */
497
ta = c ^ key->twofish.K[4];
498
tb = d ^ key->twofish.K[5];
499
tc = a ^ key->twofish.K[6];
500
td = b ^ key->twofish.K[7];
503
STORE32L(ta,&ct[0]); STORE32L(tb,&ct[4]);
504
STORE32L(tc,&ct[8]); STORE32L(td,&ct[12]);
508
void twofish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
510
_twofish_ecb_encrypt(pt, ct, key);
511
burn_stack(sizeof(ulong32) * 10 + sizeof(int));
516
static void _twofish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
518
void twofish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
521
ulong32 a,b,c,d,ta,tb,tc,td,t1,t2, *k;
523
#if !defined(TWOFISH_SMALL) && !defined(__GNUC__)
524
ulong32 *S1, *S2, *S3, *S4;
529
_ARGCHK(key != NULL);
531
#if !defined(TWOFISH_SMALL) && !defined(__GNUC__)
532
S1 = key->twofish.S[0];
533
S2 = key->twofish.S[1];
534
S3 = key->twofish.S[2];
535
S4 = key->twofish.S[3];
539
LOAD32L(ta,&ct[0]); LOAD32L(tb,&ct[4]);
540
LOAD32L(tc,&ct[8]); LOAD32L(td,&ct[12]);
542
/* undo undo final swap */
543
a = tc ^ key->twofish.K[6];
544
b = td ^ key->twofish.K[7];
545
c = ta ^ key->twofish.K[4];
546
d = tb ^ key->twofish.K[5];
548
k = key->twofish.K + 36;
549
for (r = 8; r != 0; --r) {
550
t2 = g1_func(d, key);
551
t1 = g_func(c, key) + t2;
552
a = ROL(a, 1) ^ (t1 + k[2]);
553
b = ROR(b ^ (t2 + t1 + k[3]), 1);
555
t2 = g1_func(b, key);
556
t1 = g_func(a, key) + t2;
557
c = ROL(c, 1) ^ (t1 + k[0]);
558
d = ROR(d ^ (t2 + t1 + k[1]), 1);
563
a ^= key->twofish.K[0];
564
b ^= key->twofish.K[1];
565
c ^= key->twofish.K[2];
566
d ^= key->twofish.K[3];
569
STORE32L(a, &pt[0]); STORE32L(b, &pt[4]);
570
STORE32L(c, &pt[8]); STORE32L(d, &pt[12]);
574
void twofish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
576
_twofish_ecb_decrypt(ct, pt, key);
577
burn_stack(sizeof(ulong32) * 10 + sizeof(int));
581
int twofish_test(void)
586
static const struct {
588
unsigned char key[32], pt[16], ct[16];
591
{ 0x9F, 0x58, 0x9F, 0x5C, 0xF6, 0x12, 0x2C, 0x32,
592
0xB6, 0xBF, 0xEC, 0x2F, 0x2A, 0xE8, 0xC3, 0x5A },
593
{ 0xD4, 0x91, 0xDB, 0x16, 0xE7, 0xB1, 0xC3, 0x9E,
594
0x86, 0xCB, 0x08, 0x6B, 0x78, 0x9F, 0x54, 0x19 },
595
{ 0x01, 0x9F, 0x98, 0x09, 0xDE, 0x17, 0x11, 0x85,
596
0x8F, 0xAA, 0xC3, 0xA3, 0xBA, 0x20, 0xFB, 0xC3 }
599
{ 0x88, 0xB2, 0xB2, 0x70, 0x6B, 0x10, 0x5E, 0x36,
600
0xB4, 0x46, 0xBB, 0x6D, 0x73, 0x1A, 0x1E, 0x88,
601
0xEF, 0xA7, 0x1F, 0x78, 0x89, 0x65, 0xBD, 0x44 },
602
{ 0x39, 0xDA, 0x69, 0xD6, 0xBA, 0x49, 0x97, 0xD5,
603
0x85, 0xB6, 0xDC, 0x07, 0x3C, 0xA3, 0x41, 0xB2 },
604
{ 0x18, 0x2B, 0x02, 0xD8, 0x14, 0x97, 0xEA, 0x45,
605
0xF9, 0xDA, 0xAC, 0xDC, 0x29, 0x19, 0x3A, 0x65 }
608
{ 0xD4, 0x3B, 0xB7, 0x55, 0x6E, 0xA3, 0x2E, 0x46,
609
0xF2, 0xA2, 0x82, 0xB7, 0xD4, 0x5B, 0x4E, 0x0D,
610
0x57, 0xFF, 0x73, 0x9D, 0x4D, 0xC9, 0x2C, 0x1B,
611
0xD7, 0xFC, 0x01, 0x70, 0x0C, 0xC8, 0x21, 0x6F },
612
{ 0x90, 0xAF, 0xE9, 0x1B, 0xB2, 0x88, 0x54, 0x4F,
613
0x2C, 0x32, 0xDC, 0x23, 0x9B, 0x26, 0x35, 0xE6 },
614
{ 0x6C, 0xB4, 0x56, 0x1C, 0x40, 0xBF, 0x0A, 0x97,
615
0x05, 0x93, 0x1C, 0xB6, 0xD4, 0x08, 0xE7, 0xFA }
621
unsigned char tmp[2][16];
624
for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
625
if ((err = twofish_setup(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) {
628
twofish_ecb_encrypt(tests[i].pt, tmp[0], &key);
629
twofish_ecb_decrypt(tmp[0], tmp[1], &key);
630
if (memcmp(tmp[0], tests[i].ct, 16) != 0 || memcmp(tmp[1], tests[i].pt, 16) != 0) {
631
return CRYPT_FAIL_TESTVECTOR;
633
/* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
634
for (y = 0; y < 16; y++) tmp[0][y] = 0;
635
for (y = 0; y < 1000; y++) twofish_ecb_encrypt(tmp[0], tmp[0], &key);
636
for (y = 0; y < 1000; y++) twofish_ecb_decrypt(tmp[0], tmp[0], &key);
637
for (y = 0; y < 16; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR;
643
int twofish_keysize(int *desired_keysize)
645
_ARGCHK(desired_keysize);
646
if (*desired_keysize < 16)
647
return CRYPT_INVALID_KEYSIZE;
648
if (*desired_keysize < 24) {
649
*desired_keysize = 16;
651
} else if (*desired_keysize < 32) {
652
*desired_keysize = 24;
655
*desired_keysize = 32;