]> pd.if.org Git - zpackage/blob - libtomcrypt/src/ciphers/twofish/twofish.c
b1584d1955a92798ad908a924a3a7ff9194904de
[zpackage] / libtomcrypt / src / ciphers / twofish / twofish.c
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2  *
3  * LibTomCrypt is a library that provides various cryptographic
4  * algorithms in a highly modular and flexible manner.
5  *
6  * The library is free for all purposes without any express
7  * guarantee it works.
8  */
9
10  /**
11    @file twofish.c
12    Implementation of Twofish by Tom St Denis
13  */
14 #include "tomcrypt.h"
15
16 #ifdef LTC_TWOFISH
17
18 /* first LTC_TWOFISH_ALL_TABLES must ensure LTC_TWOFISH_TABLES is defined */
19 #ifdef LTC_TWOFISH_ALL_TABLES
20 #ifndef LTC_TWOFISH_TABLES
21 #define LTC_TWOFISH_TABLES
22 #endif
23 #endif
24
25 const struct ltc_cipher_descriptor twofish_desc =
26 {
27     "twofish",
28     7,
29     16, 32, 16, 16,
30     &twofish_setup,
31     &twofish_ecb_encrypt,
32     &twofish_ecb_decrypt,
33     &twofish_test,
34     &twofish_done,
35     &twofish_keysize,
36     NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
37 };
38
39 /* the two polynomials */
40 #define MDS_POLY          0x169
41 #define RS_POLY           0x14D
42
43 /* The 4x8 RS Linear Transform */
44 static const unsigned char RS[4][8] = {
45     { 0x01, 0xA4, 0x55, 0x87, 0x5A, 0x58, 0xDB, 0x9E },
46     { 0xA4, 0x56, 0x82, 0xF3, 0X1E, 0XC6, 0X68, 0XE5 },
47     { 0X02, 0XA1, 0XFC, 0XC1, 0X47, 0XAE, 0X3D, 0X19 },
48     { 0XA4, 0X55, 0X87, 0X5A, 0X58, 0XDB, 0X9E, 0X03 }
49 };
50
51 #ifdef LTC_TWOFISH_SMALL
52 /* sbox usage orderings */
53 static const unsigned char qord[4][5] = {
54    { 1, 1, 0, 0, 1 },
55    { 0, 1, 1, 0, 0 },
56    { 0, 0, 0, 1, 1 },
57    { 1, 0, 1, 1, 0 }
58 };
59 #endif /* LTC_TWOFISH_SMALL */
60
61 #ifdef LTC_TWOFISH_TABLES
62
63 #define __LTC_TWOFISH_TAB_C__
64 #include "twofish_tab.c"
65
66 #define sbox(i, x) ((ulong32)SBOX[i][(x)&255])
67
68 #else
69
70 /* The Q-box tables */
71 static const unsigned char qbox[2][4][16] = {
72 {
73    { 0x8, 0x1, 0x7, 0xD, 0x6, 0xF, 0x3, 0x2, 0x0, 0xB, 0x5, 0x9, 0xE, 0xC, 0xA, 0x4 },
74    { 0xE, 0XC, 0XB, 0X8, 0X1, 0X2, 0X3, 0X5, 0XF, 0X4, 0XA, 0X6, 0X7, 0X0, 0X9, 0XD },
75    { 0XB, 0XA, 0X5, 0XE, 0X6, 0XD, 0X9, 0X0, 0XC, 0X8, 0XF, 0X3, 0X2, 0X4, 0X7, 0X1 },
76    { 0XD, 0X7, 0XF, 0X4, 0X1, 0X2, 0X6, 0XE, 0X9, 0XB, 0X3, 0X0, 0X8, 0X5, 0XC, 0XA }
77 },
78 {
79    { 0X2, 0X8, 0XB, 0XD, 0XF, 0X7, 0X6, 0XE, 0X3, 0X1, 0X9, 0X4, 0X0, 0XA, 0XC, 0X5 },
80    { 0X1, 0XE, 0X2, 0XB, 0X4, 0XC, 0X3, 0X7, 0X6, 0XD, 0XA, 0X5, 0XF, 0X9, 0X0, 0X8 },
81    { 0X4, 0XC, 0X7, 0X5, 0X1, 0X6, 0X9, 0XA, 0X0, 0XE, 0XD, 0X8, 0X2, 0XB, 0X3, 0XF },
82    { 0xB, 0X9, 0X5, 0X1, 0XC, 0X3, 0XD, 0XE, 0X6, 0X4, 0X7, 0XF, 0X2, 0X0, 0X8, 0XA }
83 }
84 };
85
86 /* computes S_i[x] */
87 #ifdef LTC_CLEAN_STACK
88 static ulong32 _sbox(int i, ulong32 x)
89 #else
90 static ulong32 sbox(int i, ulong32 x)
91 #endif
92 {
93    unsigned char a0,b0,a1,b1,a2,b2,a3,b3,a4,b4,y;
94
95    /* a0,b0 = [x/16], x mod 16 */
96    a0 = (unsigned char)((x>>4)&15);
97    b0 = (unsigned char)((x)&15);
98
99    /* a1 = a0 ^ b0 */
100    a1 = a0 ^ b0;
101
102    /* b1 = a0 ^ ROR(b0, 1) ^ 8a0 */
103    b1 = (a0 ^ ((b0<<3)|(b0>>1)) ^ (a0<<3)) & 15;
104
105    /* a2,b2 = t0[a1], t1[b1] */
106    a2 = qbox[i][0][(int)a1];
107    b2 = qbox[i][1][(int)b1];
108
109    /* a3 = a2 ^ b2 */
110    a3 = a2 ^ b2;
111
112    /* b3 = a2 ^ ROR(b2, 1) ^ 8a2 */
113    b3 = (a2 ^ ((b2<<3)|(b2>>1)) ^ (a2<<3)) & 15;
114
115    /* a4,b4 = t2[a3], t3[b3] */
116    a4 = qbox[i][2][(int)a3];
117    b4 = qbox[i][3][(int)b3];
118
119    /* y = 16b4 + a4 */
120    y = (b4 << 4) + a4;
121
122    /* return result */
123    return (ulong32)y;
124 }
125
126 #ifdef LTC_CLEAN_STACK
127 static ulong32 sbox(int i, ulong32 x)
128 {
129    ulong32 y;
130    y = _sbox(i, x);
131    burn_stack(sizeof(unsigned char) * 11);
132    return y;
133 }
134 #endif /* LTC_CLEAN_STACK */
135
136 #endif /* LTC_TWOFISH_TABLES */
137
138 /* computes ab mod p */
139 static ulong32 gf_mult(ulong32 a, ulong32 b, ulong32 p)
140 {
141    ulong32 result, B[2], P[2];
142
143    P[1] = p;
144    B[1] = b;
145    result = P[0] = B[0] = 0;
146
147    /* unrolled branchless GF multiplier */
148    result ^= B[a&1]; a >>= 1;  B[1] = P[B[1]>>7] ^ (B[1] << 1);
149    result ^= B[a&1]; a >>= 1;  B[1] = P[B[1]>>7] ^ (B[1] << 1);
150    result ^= B[a&1]; a >>= 1;  B[1] = P[B[1]>>7] ^ (B[1] << 1);
151    result ^= B[a&1]; a >>= 1;  B[1] = P[B[1]>>7] ^ (B[1] << 1);
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];
156
157    return result;
158 }
159
160 /* computes [y0 y1 y2 y3] = MDS . [x0] */
161 #ifndef LTC_TWOFISH_TABLES
162 static ulong32 mds_column_mult(unsigned char in, int col)
163 {
164    ulong32 x01, x5B, xEF;
165
166    x01 = in;
167    x5B = gf_mult(in, 0x5B, MDS_POLY);
168    xEF = gf_mult(in, 0xEF, MDS_POLY);
169
170    switch (col) {
171        case 0:
172           return (x01 << 0 ) |
173                  (x5B << 8 ) |
174                  (xEF << 16) |
175                  (xEF << 24);
176        case 1:
177           return (xEF << 0 ) |
178                  (xEF << 8 ) |
179                  (x5B << 16) |
180                  (x01 << 24);
181        case 2:
182           return (x5B << 0 ) |
183                  (xEF << 8 ) |
184                  (x01 << 16) |
185                  (xEF << 24);
186        case 3:
187           return (x5B << 0 ) |
188                  (x01 << 8 ) |
189                  (xEF << 16) |
190                  (x5B << 24);
191    }
192    /* avoid warnings, we'd never get here normally but just to calm compiler warnings... */
193    return 0;
194 }
195
196 #else /* !LTC_TWOFISH_TABLES */
197
198 #define mds_column_mult(x, i) mds_tab[i][x]
199
200 #endif /* LTC_TWOFISH_TABLES */
201
202 /* Computes [y0 y1 y2 y3] = MDS . [x0 x1 x2 x3] */
203 static void mds_mult(const unsigned char *in, unsigned char *out)
204 {
205   int x;
206   ulong32 tmp;
207   for (tmp = x = 0; x < 4; x++) {
208       tmp ^= mds_column_mult(in[x], x);
209   }
210   STORE32L(tmp, out);
211 }
212
213 #ifdef LTC_TWOFISH_ALL_TABLES
214 /* computes [y0 y1 y2 y3] = RS . [x0 x1 x2 x3 x4 x5 x6 x7] */
215 static void rs_mult(const unsigned char *in, unsigned char *out)
216 {
217    ulong32 tmp;
218    tmp = rs_tab0[in[0]] ^ rs_tab1[in[1]] ^ rs_tab2[in[2]] ^ rs_tab3[in[3]] ^
219          rs_tab4[in[4]] ^ rs_tab5[in[5]] ^ rs_tab6[in[6]] ^ rs_tab7[in[7]];
220    STORE32L(tmp, out);
221 }
222
223 #else /* !LTC_TWOFISH_ALL_TABLES */
224
225 /* computes [y0 y1 y2 y3] = RS . [x0 x1 x2 x3 x4 x5 x6 x7] */
226 static void rs_mult(const unsigned char *in, unsigned char *out)
227 {
228   int x, y;
229   for (x = 0; x < 4; x++) {
230       out[x] = 0;
231       for (y = 0; y < 8; y++) {
232           out[x] ^= gf_mult(in[y], RS[x][y], RS_POLY);
233       }
234   }
235 }
236
237 #endif
238
239 /* computes h(x) */
240 static void h_func(const unsigned char *in, unsigned char *out, unsigned char *M, int k, int offset)
241 {
242   int x;
243   unsigned char y[4];
244   for (x = 0; x < 4; x++) {
245       y[x] = in[x];
246   }
247   switch (k) {
248      case 4:
249             y[0] = (unsigned char)(sbox(1, (ulong32)y[0]) ^ M[4 * (6 + offset) + 0]);
250             y[1] = (unsigned char)(sbox(0, (ulong32)y[1]) ^ M[4 * (6 + offset) + 1]);
251             y[2] = (unsigned char)(sbox(0, (ulong32)y[2]) ^ M[4 * (6 + offset) + 2]);
252             y[3] = (unsigned char)(sbox(1, (ulong32)y[3]) ^ M[4 * (6 + offset) + 3]);
253             /* FALLTHROUGH */
254      case 3:
255             y[0] = (unsigned char)(sbox(1, (ulong32)y[0]) ^ M[4 * (4 + offset) + 0]);
256             y[1] = (unsigned char)(sbox(1, (ulong32)y[1]) ^ M[4 * (4 + offset) + 1]);
257             y[2] = (unsigned char)(sbox(0, (ulong32)y[2]) ^ M[4 * (4 + offset) + 2]);
258             y[3] = (unsigned char)(sbox(0, (ulong32)y[3]) ^ M[4 * (4 + offset) + 3]);
259             /* FALLTHROUGH */
260      case 2:
261             y[0] = (unsigned char)(sbox(1, sbox(0, sbox(0, (ulong32)y[0]) ^ M[4 * (2 + offset) + 0]) ^ M[4 * (0 + offset) + 0]));
262             y[1] = (unsigned char)(sbox(0, sbox(0, sbox(1, (ulong32)y[1]) ^ M[4 * (2 + offset) + 1]) ^ M[4 * (0 + offset) + 1]));
263             y[2] = (unsigned char)(sbox(1, sbox(1, sbox(0, (ulong32)y[2]) ^ M[4 * (2 + offset) + 2]) ^ M[4 * (0 + offset) + 2]));
264             y[3] = (unsigned char)(sbox(0, sbox(1, sbox(1, (ulong32)y[3]) ^ M[4 * (2 + offset) + 3]) ^ M[4 * (0 + offset) + 3]));
265             /* FALLTHROUGH */
266   }
267   mds_mult(y, out);
268 }
269
270 #ifndef LTC_TWOFISH_SMALL
271
272 /* for GCC we don't use pointer aliases */
273 #if defined(__GNUC__)
274     #define S1 skey->twofish.S[0]
275     #define S2 skey->twofish.S[1]
276     #define S3 skey->twofish.S[2]
277     #define S4 skey->twofish.S[3]
278 #endif
279
280 /* the G function */
281 #define g_func(x, dum)  (S1[byte(x,0)] ^ S2[byte(x,1)] ^ S3[byte(x,2)] ^ S4[byte(x,3)])
282 #define g1_func(x, dum) (S2[byte(x,0)] ^ S3[byte(x,1)] ^ S4[byte(x,2)] ^ S1[byte(x,3)])
283
284 #else
285
286 #ifdef LTC_CLEAN_STACK
287 static ulong32 _g_func(ulong32 x, symmetric_key *key)
288 #else
289 static ulong32 g_func(ulong32 x, symmetric_key *key)
290 #endif
291 {
292    unsigned char g, i, y, z;
293    ulong32 res;
294
295    res = 0;
296    for (y = 0; y < 4; y++) {
297        z = key->twofish.start;
298
299        /* do unkeyed substitution */
300        g = sbox(qord[y][z++], (x >> (8*y)) & 255);
301
302        /* first subkey */
303        i = 0;
304
305        /* do key mixing+sbox until z==5 */
306        while (z != 5) {
307           g = g ^ key->twofish.S[4*i++ + y];
308           g = sbox(qord[y][z++], g);
309        }
310
311        /* multiply g by a column of the MDS */
312        res ^= mds_column_mult(g, y);
313    }
314    return res;
315 }
316
317 #define g1_func(x, key) g_func(ROLc(x, 8), key)
318
319 #ifdef LTC_CLEAN_STACK
320 static ulong32 g_func(ulong32 x, symmetric_key *key)
321 {
322     ulong32 y;
323     y = _g_func(x, key);
324     burn_stack(sizeof(unsigned char) * 4 + sizeof(ulong32));
325     return y;
326 }
327 #endif /* LTC_CLEAN_STACK */
328
329 #endif /* LTC_TWOFISH_SMALL */
330
331  /**
332     Initialize the Twofish block cipher
333     @param key The symmetric key you wish to pass
334     @param keylen The key length in bytes
335     @param num_rounds The number of rounds desired (0 for default)
336     @param skey The key in as scheduled by this function.
337     @return CRYPT_OK if successful
338  */
339 #ifdef LTC_CLEAN_STACK
340 static int _twofish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
341 #else
342 int twofish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
343 #endif
344 {
345 #ifndef LTC_TWOFISH_SMALL
346    unsigned char S[4*4], tmpx0, tmpx1;
347 #endif
348    int k, x, y;
349    unsigned char tmp[4], tmp2[4], M[8*4];
350    ulong32 A, B;
351
352    LTC_ARGCHK(key  != NULL);
353    LTC_ARGCHK(skey != NULL);
354
355    /* invalid arguments? */
356    if (num_rounds != 16 && num_rounds != 0) {
357       return CRYPT_INVALID_ROUNDS;
358    }
359
360    if (keylen != 16 && keylen != 24 && keylen != 32) {
361       return CRYPT_INVALID_KEYSIZE;
362    }
363
364    /* k = keysize/64 [but since our keysize is in bytes...] */
365    k = keylen / 8;
366
367    /* copy the key into M */
368    for (x = 0; x < keylen; x++) {
369        M[x] = key[x] & 255;
370    }
371
372    /* create the S[..] words */
373 #ifndef LTC_TWOFISH_SMALL
374    for (x = 0; x < k; x++) {
375        rs_mult(M+(x*8), S+(x*4));
376    }
377 #else
378    for (x = 0; x < k; x++) {
379        rs_mult(M+(x*8), skey->twofish.S+(x*4));
380    }
381 #endif
382
383    /* make subkeys */
384    for (x = 0; x < 20; x++) {
385        /* A = h(p * 2x, Me) */
386        for (y = 0; y < 4; y++) {
387            tmp[y] = x+x;
388        }
389        h_func(tmp, tmp2, M, k, 0);
390        LOAD32L(A, tmp2);
391
392        /* B = ROL(h(p * (2x + 1), Mo), 8) */
393        for (y = 0; y < 4; y++) {
394            tmp[y] = (unsigned char)(x+x+1);
395        }
396        h_func(tmp, tmp2, M, k, 1);
397        LOAD32L(B, tmp2);
398        B = ROLc(B, 8);
399
400        /* K[2i]   = A + B */
401        skey->twofish.K[x+x] = (A + B) & 0xFFFFFFFFUL;
402
403        /* K[2i+1] = (A + 2B) <<< 9 */
404        skey->twofish.K[x+x+1] = ROLc(B + B + A, 9);
405    }
406
407 #ifndef LTC_TWOFISH_SMALL
408    /* make the sboxes (large ram variant) */
409    if (k == 2) {
410         for (x = 0; x < 256; x++) {
411            tmpx0 = (unsigned char)sbox(0, x);
412            tmpx1 = (unsigned char)sbox(1, x);
413            skey->twofish.S[0][x] = mds_column_mult(sbox(1, (sbox(0, tmpx0 ^ S[0]) ^ S[4])),0);
414            skey->twofish.S[1][x] = mds_column_mult(sbox(0, (sbox(0, tmpx1 ^ S[1]) ^ S[5])),1);
415            skey->twofish.S[2][x] = mds_column_mult(sbox(1, (sbox(1, tmpx0 ^ S[2]) ^ S[6])),2);
416            skey->twofish.S[3][x] = mds_column_mult(sbox(0, (sbox(1, tmpx1 ^ S[3]) ^ S[7])),3);
417         }
418    } else if (k == 3) {
419         for (x = 0; x < 256; x++) {
420            tmpx0 = (unsigned char)sbox(0, x);
421            tmpx1 = (unsigned char)sbox(1, x);
422            skey->twofish.S[0][x] = mds_column_mult(sbox(1, (sbox(0, sbox(0, tmpx1 ^ S[0]) ^ S[4]) ^ S[8])),0);
423            skey->twofish.S[1][x] = mds_column_mult(sbox(0, (sbox(0, sbox(1, tmpx1 ^ S[1]) ^ S[5]) ^ S[9])),1);
424            skey->twofish.S[2][x] = mds_column_mult(sbox(1, (sbox(1, sbox(0, tmpx0 ^ S[2]) ^ S[6]) ^ S[10])),2);
425            skey->twofish.S[3][x] = mds_column_mult(sbox(0, (sbox(1, sbox(1, tmpx0 ^ S[3]) ^ S[7]) ^ S[11])),3);
426         }
427    } else {
428         for (x = 0; x < 256; x++) {
429            tmpx0 = (unsigned char)sbox(0, x);
430            tmpx1 = (unsigned char)sbox(1, x);
431            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);
432            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);
433            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);
434            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);
435         }
436    }
437 #else
438    /* where to start in the sbox layers */
439    /* small ram variant */
440    switch (k) {
441          case 4 : skey->twofish.start = 0; break;
442          case 3 : skey->twofish.start = 1; break;
443          default: skey->twofish.start = 2; break;
444    }
445 #endif
446    return CRYPT_OK;
447 }
448
449 #ifdef LTC_CLEAN_STACK
450 int twofish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
451 {
452    int x;
453    x = _twofish_setup(key, keylen, num_rounds, skey);
454    burn_stack(sizeof(int) * 7 + sizeof(unsigned char) * 56 + sizeof(ulong32) * 2);
455    return x;
456 }
457 #endif
458
459 /**
460   Encrypts a block of text with Twofish
461   @param pt The input plaintext (16 bytes)
462   @param ct The output ciphertext (16 bytes)
463   @param skey The key as scheduled
464   @return CRYPT_OK if successful
465 */
466 #ifdef LTC_CLEAN_STACK
467 static int _twofish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
468 #else
469 int twofish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
470 #endif
471 {
472     ulong32 a,b,c,d,ta,tb,tc,td,t1,t2, *k;
473     int r;
474 #if !defined(LTC_TWOFISH_SMALL) && !defined(__GNUC__)
475     ulong32 *S1, *S2, *S3, *S4;
476 #endif
477
478     LTC_ARGCHK(pt   != NULL);
479     LTC_ARGCHK(ct   != NULL);
480     LTC_ARGCHK(skey != NULL);
481
482 #if !defined(LTC_TWOFISH_SMALL) && !defined(__GNUC__)
483     S1 = skey->twofish.S[0];
484     S2 = skey->twofish.S[1];
485     S3 = skey->twofish.S[2];
486     S4 = skey->twofish.S[3];
487 #endif
488
489     LOAD32L(a,&pt[0]); LOAD32L(b,&pt[4]);
490     LOAD32L(c,&pt[8]); LOAD32L(d,&pt[12]);
491     a ^= skey->twofish.K[0];
492     b ^= skey->twofish.K[1];
493     c ^= skey->twofish.K[2];
494     d ^= skey->twofish.K[3];
495
496     k  = skey->twofish.K + 8;
497     for (r = 8; r != 0; --r) {
498         t2 = g1_func(b, skey);
499         t1 = g_func(a, skey) + t2;
500         c  = RORc(c ^ (t1 + k[0]), 1);
501         d  = ROLc(d, 1) ^ (t2 + t1 + k[1]);
502
503         t2 = g1_func(d, skey);
504         t1 = g_func(c, skey) + t2;
505         a  = RORc(a ^ (t1 + k[2]), 1);
506         b  = ROLc(b, 1) ^ (t2 + t1 + k[3]);
507         k += 4;
508     }
509
510     /* output with "undo last swap" */
511     ta = c ^ skey->twofish.K[4];
512     tb = d ^ skey->twofish.K[5];
513     tc = a ^ skey->twofish.K[6];
514     td = b ^ skey->twofish.K[7];
515
516     /* store output */
517     STORE32L(ta,&ct[0]); STORE32L(tb,&ct[4]);
518     STORE32L(tc,&ct[8]); STORE32L(td,&ct[12]);
519
520     return CRYPT_OK;
521 }
522
523 #ifdef LTC_CLEAN_STACK
524 int twofish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
525 {
526    int err = _twofish_ecb_encrypt(pt, ct, skey);
527    burn_stack(sizeof(ulong32) * 10 + sizeof(int));
528    return err;
529 }
530 #endif
531
532 /**
533   Decrypts a block of text with Twofish
534   @param ct The input ciphertext (16 bytes)
535   @param pt The output plaintext (16 bytes)
536   @param skey The key as scheduled
537   @return CRYPT_OK if successful
538 */
539 #ifdef LTC_CLEAN_STACK
540 static int _twofish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
541 #else
542 int twofish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
543 #endif
544 {
545     ulong32 a,b,c,d,ta,tb,tc,td,t1,t2, *k;
546     int r;
547 #if !defined(LTC_TWOFISH_SMALL) && !defined(__GNUC__)
548     ulong32 *S1, *S2, *S3, *S4;
549 #endif
550
551     LTC_ARGCHK(pt   != NULL);
552     LTC_ARGCHK(ct   != NULL);
553     LTC_ARGCHK(skey != NULL);
554
555 #if !defined(LTC_TWOFISH_SMALL) && !defined(__GNUC__)
556     S1 = skey->twofish.S[0];
557     S2 = skey->twofish.S[1];
558     S3 = skey->twofish.S[2];
559     S4 = skey->twofish.S[3];
560 #endif
561
562     /* load input */
563     LOAD32L(ta,&ct[0]); LOAD32L(tb,&ct[4]);
564     LOAD32L(tc,&ct[8]); LOAD32L(td,&ct[12]);
565
566     /* undo undo final swap */
567     a = tc ^ skey->twofish.K[6];
568     b = td ^ skey->twofish.K[7];
569     c = ta ^ skey->twofish.K[4];
570     d = tb ^ skey->twofish.K[5];
571
572     k = skey->twofish.K + 36;
573     for (r = 8; r != 0; --r) {
574         t2 = g1_func(d, skey);
575         t1 = g_func(c, skey) + t2;
576         a = ROLc(a, 1) ^ (t1 + k[2]);
577         b = RORc(b ^ (t2 + t1 + k[3]), 1);
578
579         t2 = g1_func(b, skey);
580         t1 = g_func(a, skey) + t2;
581         c = ROLc(c, 1) ^ (t1 + k[0]);
582         d = RORc(d ^ (t2 +  t1 + k[1]), 1);
583         k -= 4;
584     }
585
586     /* pre-white */
587     a ^= skey->twofish.K[0];
588     b ^= skey->twofish.K[1];
589     c ^= skey->twofish.K[2];
590     d ^= skey->twofish.K[3];
591
592     /* store */
593     STORE32L(a, &pt[0]); STORE32L(b, &pt[4]);
594     STORE32L(c, &pt[8]); STORE32L(d, &pt[12]);
595     return CRYPT_OK;
596 }
597
598 #ifdef LTC_CLEAN_STACK
599 int twofish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
600 {
601    int err =_twofish_ecb_decrypt(ct, pt, skey);
602    burn_stack(sizeof(ulong32) * 10 + sizeof(int));
603    return err;
604 }
605 #endif
606
607 /**
608   Performs a self-test of the Twofish block cipher
609   @return CRYPT_OK if functional, CRYPT_NOP if self-test has been disabled
610 */
611 int twofish_test(void)
612 {
613  #ifndef LTC_TEST
614     return CRYPT_NOP;
615  #else
616  static const struct {
617      int keylen;
618      unsigned char key[32], pt[16], ct[16];
619  } tests[] = {
620    { 16,
621      { 0x9F, 0x58, 0x9F, 0x5C, 0xF6, 0x12, 0x2C, 0x32,
622        0xB6, 0xBF, 0xEC, 0x2F, 0x2A, 0xE8, 0xC3, 0x5A },
623      { 0xD4, 0x91, 0xDB, 0x16, 0xE7, 0xB1, 0xC3, 0x9E,
624        0x86, 0xCB, 0x08, 0x6B, 0x78, 0x9F, 0x54, 0x19 },
625      { 0x01, 0x9F, 0x98, 0x09, 0xDE, 0x17, 0x11, 0x85,
626        0x8F, 0xAA, 0xC3, 0xA3, 0xBA, 0x20, 0xFB, 0xC3 }
627    }, {
628      24,
629      { 0x88, 0xB2, 0xB2, 0x70, 0x6B, 0x10, 0x5E, 0x36,
630        0xB4, 0x46, 0xBB, 0x6D, 0x73, 0x1A, 0x1E, 0x88,
631        0xEF, 0xA7, 0x1F, 0x78, 0x89, 0x65, 0xBD, 0x44 },
632      { 0x39, 0xDA, 0x69, 0xD6, 0xBA, 0x49, 0x97, 0xD5,
633        0x85, 0xB6, 0xDC, 0x07, 0x3C, 0xA3, 0x41, 0xB2 },
634      { 0x18, 0x2B, 0x02, 0xD8, 0x14, 0x97, 0xEA, 0x45,
635        0xF9, 0xDA, 0xAC, 0xDC, 0x29, 0x19, 0x3A, 0x65 }
636    }, {
637      32,
638      { 0xD4, 0x3B, 0xB7, 0x55, 0x6E, 0xA3, 0x2E, 0x46,
639        0xF2, 0xA2, 0x82, 0xB7, 0xD4, 0x5B, 0x4E, 0x0D,
640        0x57, 0xFF, 0x73, 0x9D, 0x4D, 0xC9, 0x2C, 0x1B,
641        0xD7, 0xFC, 0x01, 0x70, 0x0C, 0xC8, 0x21, 0x6F },
642      { 0x90, 0xAF, 0xE9, 0x1B, 0xB2, 0x88, 0x54, 0x4F,
643        0x2C, 0x32, 0xDC, 0x23, 0x9B, 0x26, 0x35, 0xE6 },
644      { 0x6C, 0xB4, 0x56, 0x1C, 0x40, 0xBF, 0x0A, 0x97,
645        0x05, 0x93, 0x1C, 0xB6, 0xD4, 0x08, 0xE7, 0xFA }
646    }
647 };
648
649
650   symmetric_key key;
651   unsigned char tmp[2][16];
652   int err, i, y;
653
654   for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
655     if ((err = twofish_setup(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) {
656        return err;
657     }
658     twofish_ecb_encrypt(tests[i].pt, tmp[0], &key);
659     twofish_ecb_decrypt(tmp[0], tmp[1], &key);
660     if (compare_testvector(tmp[0], 16, tests[i].ct, 16, "Twofish Encrypt", i) != 0 ||
661           compare_testvector(tmp[1], 16, tests[i].pt, 16, "Twofish Decrypt", i) != 0) {
662        return CRYPT_FAIL_TESTVECTOR;
663     }
664     /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
665     for (y = 0; y < 16; y++) tmp[0][y] = 0;
666     for (y = 0; y < 1000; y++) twofish_ecb_encrypt(tmp[0], tmp[0], &key);
667     for (y = 0; y < 1000; y++) twofish_ecb_decrypt(tmp[0], tmp[0], &key);
668     for (y = 0; y < 16; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR;
669   }
670   return CRYPT_OK;
671 #endif
672 }
673
674 /** Terminate the context
675    @param skey    The scheduled key
676 */
677 void twofish_done(symmetric_key *skey)
678 {
679   LTC_UNUSED_PARAM(skey);
680 }
681
682 /**
683   Gets suitable key size
684   @param keysize [in/out] The length of the recommended key (in bytes).  This function will store the suitable size back in this variable.
685   @return CRYPT_OK if the input key size is acceptable.
686 */
687 int twofish_keysize(int *keysize)
688 {
689    LTC_ARGCHK(keysize);
690    if (*keysize < 16)
691       return CRYPT_INVALID_KEYSIZE;
692    if (*keysize < 24) {
693       *keysize = 16;
694       return CRYPT_OK;
695    } else if (*keysize < 32) {
696       *keysize = 24;
697       return CRYPT_OK;
698    } else {
699       *keysize = 32;
700       return CRYPT_OK;
701    }
702 }
703
704 #endif
705
706
707
708
709 /* ref:         $Format:%D$ */
710 /* git commit:  $Format:%H$ */
711 /* commit time: $Format:%ai$ */