1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 
4  /**
5    @file twofish.c
6    Implementation of Twofish by Tom St Denis
7  */
8 #include "tomcrypt_private.h"
9 
10 #ifdef LTC_TWOFISH
11 
12 /* first LTC_TWOFISH_ALL_TABLES must ensure LTC_TWOFISH_TABLES is defined */
13 #ifdef LTC_TWOFISH_ALL_TABLES
14 #ifndef LTC_TWOFISH_TABLES
15 #define LTC_TWOFISH_TABLES
16 #endif
17 #endif
18 
19 const struct ltc_cipher_descriptor twofish_desc =
20 {
21     "twofish",
22     7,
23     16, 32, 16, 16,
24     &twofish_setup,
25     &twofish_ecb_encrypt,
26     &twofish_ecb_decrypt,
27     &twofish_test,
28     &twofish_done,
29     &twofish_keysize,
30     NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
31 };
32 
33 /* the two polynomials */
34 #ifndef LTC_TWOFISH_TABLES
35 #define MDS_POLY          0x169
36 #endif
37 #ifndef LTC_TWOFISH_ALL_TABLES
38 #define RS_POLY           0x14D
39 #endif
40 
41 /* The 4x8 RS Linear Transform */
42 static const unsigned char RS[4][8] = {
43     { 0x01, 0xA4, 0x55, 0x87, 0x5A, 0x58, 0xDB, 0x9E },
44     { 0xA4, 0x56, 0x82, 0xF3, 0X1E, 0XC6, 0X68, 0XE5 },
45     { 0X02, 0XA1, 0XFC, 0XC1, 0X47, 0XAE, 0X3D, 0X19 },
46     { 0XA4, 0X55, 0X87, 0X5A, 0X58, 0XDB, 0X9E, 0X03 }
47 };
48 
49 #ifdef LTC_TWOFISH_SMALL
50 /* sbox usage orderings */
51 static const unsigned char qord[4][5] = {
52    { 1, 1, 0, 0, 1 },
53    { 0, 1, 1, 0, 0 },
54    { 0, 0, 0, 1, 1 },
55    { 1, 0, 1, 1, 0 }
56 };
57 #endif /* LTC_TWOFISH_SMALL */
58 
59 #ifdef LTC_TWOFISH_TABLES
60 
61 #define LTC_TWOFISH_TAB_C
62 #include "twofish_tab.c"
63 
64 #define sbox(i, x) ((ulong32)SBOX[i][(x)&255])
65 
66 #else
67 
68 /* The Q-box tables */
69 static const unsigned char qbox[2][4][16] = {
70 {
71    { 0x8, 0x1, 0x7, 0xD, 0x6, 0xF, 0x3, 0x2, 0x0, 0xB, 0x5, 0x9, 0xE, 0xC, 0xA, 0x4 },
72    { 0xE, 0XC, 0XB, 0X8, 0X1, 0X2, 0X3, 0X5, 0XF, 0X4, 0XA, 0X6, 0X7, 0X0, 0X9, 0XD },
73    { 0XB, 0XA, 0X5, 0XE, 0X6, 0XD, 0X9, 0X0, 0XC, 0X8, 0XF, 0X3, 0X2, 0X4, 0X7, 0X1 },
74    { 0XD, 0X7, 0XF, 0X4, 0X1, 0X2, 0X6, 0XE, 0X9, 0XB, 0X3, 0X0, 0X8, 0X5, 0XC, 0XA }
75 },
76 {
77    { 0X2, 0X8, 0XB, 0XD, 0XF, 0X7, 0X6, 0XE, 0X3, 0X1, 0X9, 0X4, 0X0, 0XA, 0XC, 0X5 },
78    { 0X1, 0XE, 0X2, 0XB, 0X4, 0XC, 0X3, 0X7, 0X6, 0XD, 0XA, 0X5, 0XF, 0X9, 0X0, 0X8 },
79    { 0X4, 0XC, 0X7, 0X5, 0X1, 0X6, 0X9, 0XA, 0X0, 0XE, 0XD, 0X8, 0X2, 0XB, 0X3, 0XF },
80    { 0xB, 0X9, 0X5, 0X1, 0XC, 0X3, 0XD, 0XE, 0X6, 0X4, 0X7, 0XF, 0X2, 0X0, 0X8, 0XA }
81 }
82 };
83 
84 /* computes S_i[x] */
85 #ifdef LTC_CLEAN_STACK
s_sbox(int i,ulong32 x)86 static ulong32 s_sbox(int i, ulong32 x)
87 #else
88 static ulong32 sbox(int i, ulong32 x)
89 #endif
90 {
91    unsigned char a0,b0,a1,b1,a2,b2,a3,b3,a4,b4,y;
92 
93    /* a0,b0 = [x/16], x mod 16 */
94    a0 = (unsigned char)((x>>4)&15);
95    b0 = (unsigned char)((x)&15);
96 
97    /* a1 = a0 ^ b0 */
98    a1 = a0 ^ b0;
99 
100    /* b1 = a0 ^ ROR(b0, 1) ^ 8a0 */
101    b1 = (a0 ^ ((b0<<3)|(b0>>1)) ^ (a0<<3)) & 15;
102 
103    /* a2,b2 = t0[a1], t1[b1] */
104    a2 = qbox[i][0][(int)a1];
105    b2 = qbox[i][1][(int)b1];
106 
107    /* a3 = a2 ^ b2 */
108    a3 = a2 ^ b2;
109 
110    /* b3 = a2 ^ ROR(b2, 1) ^ 8a2 */
111    b3 = (a2 ^ ((b2<<3)|(b2>>1)) ^ (a2<<3)) & 15;
112 
113    /* a4,b4 = t2[a3], t3[b3] */
114    a4 = qbox[i][2][(int)a3];
115    b4 = qbox[i][3][(int)b3];
116 
117    /* y = 16b4 + a4 */
118    y = (b4 << 4) + a4;
119 
120    /* return result */
121    return (ulong32)y;
122 }
123 
124 #ifdef LTC_CLEAN_STACK
sbox(int i,ulong32 x)125 static ulong32 sbox(int i, ulong32 x)
126 {
127    ulong32 y;
128    y = s_sbox(i, x);
129    burn_stack(sizeof(unsigned char) * 11);
130    return y;
131 }
132 #endif /* LTC_CLEAN_STACK */
133 
134 #endif /* LTC_TWOFISH_TABLES */
135 
136 /* computes ab mod p */
gf_mult(ulong32 a,ulong32 b,ulong32 p)137 static ulong32 gf_mult(ulong32 a, ulong32 b, ulong32 p)
138 {
139    ulong32 result, B[2], P[2];
140 
141    P[1] = p;
142    B[1] = b;
143    result = P[0] = B[0] = 0;
144 
145    /* unrolled branchless GF multiplier */
146    result ^= B[a&1]; a >>= 1;  B[1] = P[B[1]>>7] ^ (B[1] << 1);
147    result ^= B[a&1]; a >>= 1;  B[1] = P[B[1]>>7] ^ (B[1] << 1);
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];
154 
155    return result;
156 }
157 
158 /* computes [y0 y1 y2 y3] = MDS . [x0] */
159 #ifndef LTC_TWOFISH_TABLES
mds_column_mult(unsigned char in,int col)160 static ulong32 mds_column_mult(unsigned char in, int col)
161 {
162    ulong32 x01, x5B, xEF;
163 
164    x01 = in;
165    x5B = gf_mult(in, 0x5B, MDS_POLY);
166    xEF = gf_mult(in, 0xEF, MDS_POLY);
167 
168    switch (col) {
169        case 0:
170           return (x01 << 0 ) |
171                  (x5B << 8 ) |
172                  (xEF << 16) |
173                  (xEF << 24);
174        case 1:
175           return (xEF << 0 ) |
176                  (xEF << 8 ) |
177                  (x5B << 16) |
178                  (x01 << 24);
179        case 2:
180           return (x5B << 0 ) |
181                  (xEF << 8 ) |
182                  (x01 << 16) |
183                  (xEF << 24);
184        case 3:
185           return (x5B << 0 ) |
186                  (x01 << 8 ) |
187                  (xEF << 16) |
188                  (x5B << 24);
189    }
190    /* avoid warnings, we'd never get here normally but just to calm compiler warnings... */
191    return 0;
192 }
193 
194 #else /* !LTC_TWOFISH_TABLES */
195 
196 #define mds_column_mult(x, i) mds_tab[i][x]
197 
198 #endif /* LTC_TWOFISH_TABLES */
199 
200 /* Computes [y0 y1 y2 y3] = MDS . [x0 x1 x2 x3] */
mds_mult(const unsigned char * in,unsigned char * out)201 static void mds_mult(const unsigned char *in, unsigned char *out)
202 {
203   int x;
204   ulong32 tmp;
205   for (tmp = x = 0; x < 4; x++) {
206       tmp ^= mds_column_mult(in[x], x);
207   }
208   STORE32L(tmp, out);
209 }
210 
211 #ifdef LTC_TWOFISH_ALL_TABLES
212 /* computes [y0 y1 y2 y3] = RS . [x0 x1 x2 x3 x4 x5 x6 x7] */
rs_mult(const unsigned char * in,unsigned char * out)213 static void rs_mult(const unsigned char *in, unsigned char *out)
214 {
215    ulong32 tmp;
216    tmp = rs_tab0[in[0]] ^ rs_tab1[in[1]] ^ rs_tab2[in[2]] ^ rs_tab3[in[3]] ^
217          rs_tab4[in[4]] ^ rs_tab5[in[5]] ^ rs_tab6[in[6]] ^ rs_tab7[in[7]];
218    STORE32L(tmp, out);
219 }
220 
221 #else /* !LTC_TWOFISH_ALL_TABLES */
222 
223 /* computes [y0 y1 y2 y3] = RS . [x0 x1 x2 x3 x4 x5 x6 x7] */
rs_mult(const unsigned char * in,unsigned char * out)224 static void rs_mult(const unsigned char *in, unsigned char *out)
225 {
226   int x, y;
227   for (x = 0; x < 4; x++) {
228       out[x] = 0;
229       for (y = 0; y < 8; y++) {
230           out[x] ^= gf_mult(in[y], RS[x][y], RS_POLY);
231       }
232   }
233 }
234 
235 #endif
236 
237 /* computes h(x) */
h_func(const unsigned char * in,unsigned char * out,const unsigned char * M,int k,int offset)238 static void h_func(const unsigned char *in, unsigned char *out, const unsigned char *M, int k, int offset)
239 {
240   int x;
241   unsigned char y[4];
242   for (x = 0; x < 4; x++) {
243       y[x] = in[x];
244   }
245   switch (k) {
246      case 4:
247             y[0] = (unsigned char)(sbox(1, (ulong32)y[0]) ^ M[4 * (6 + offset) + 0]);
248             y[1] = (unsigned char)(sbox(0, (ulong32)y[1]) ^ M[4 * (6 + offset) + 1]);
249             y[2] = (unsigned char)(sbox(0, (ulong32)y[2]) ^ M[4 * (6 + offset) + 2]);
250             y[3] = (unsigned char)(sbox(1, (ulong32)y[3]) ^ M[4 * (6 + offset) + 3]);
251             /* FALLTHROUGH */
252      case 3:
253             y[0] = (unsigned char)(sbox(1, (ulong32)y[0]) ^ M[4 * (4 + offset) + 0]);
254             y[1] = (unsigned char)(sbox(1, (ulong32)y[1]) ^ M[4 * (4 + offset) + 1]);
255             y[2] = (unsigned char)(sbox(0, (ulong32)y[2]) ^ M[4 * (4 + offset) + 2]);
256             y[3] = (unsigned char)(sbox(0, (ulong32)y[3]) ^ M[4 * (4 + offset) + 3]);
257             /* FALLTHROUGH */
258      case 2:
259             y[0] = (unsigned char)(sbox(1, sbox(0, sbox(0, (ulong32)y[0]) ^ M[4 * (2 + offset) + 0]) ^ M[4 * (0 + offset) + 0]));
260             y[1] = (unsigned char)(sbox(0, sbox(0, sbox(1, (ulong32)y[1]) ^ M[4 * (2 + offset) + 1]) ^ M[4 * (0 + offset) + 1]));
261             y[2] = (unsigned char)(sbox(1, sbox(1, sbox(0, (ulong32)y[2]) ^ M[4 * (2 + offset) + 2]) ^ M[4 * (0 + offset) + 2]));
262             y[3] = (unsigned char)(sbox(0, sbox(1, sbox(1, (ulong32)y[3]) ^ M[4 * (2 + offset) + 3]) ^ M[4 * (0 + offset) + 3]));
263             /* FALLTHROUGH */
264   }
265   mds_mult(y, out);
266 }
267 
268 #ifndef LTC_TWOFISH_SMALL
269 
270 /* for GCC we don't use pointer aliases */
271 #if defined(__GNUC__)
272     #define S1 skey->twofish.S[0]
273     #define S2 skey->twofish.S[1]
274     #define S3 skey->twofish.S[2]
275     #define S4 skey->twofish.S[3]
276 #endif
277 
278 /* the G function */
279 #define g_func(x, dum)  (S1[LTC_BYTE(x,0)] ^ S2[LTC_BYTE(x,1)] ^ S3[LTC_BYTE(x,2)] ^ S4[LTC_BYTE(x,3)])
280 #define g1_func(x, dum) (S2[LTC_BYTE(x,0)] ^ S3[LTC_BYTE(x,1)] ^ S4[LTC_BYTE(x,2)] ^ S1[LTC_BYTE(x,3)])
281 
282 #else
283 
284 #ifdef LTC_CLEAN_STACK
s_g_func(ulong32 x,const symmetric_key * key)285 static ulong32 s_g_func(ulong32 x, const symmetric_key *key)
286 #else
287 static ulong32 g_func(ulong32 x, const symmetric_key *key)
288 #endif
289 {
290    unsigned char g, i, y, z;
291    ulong32 res;
292 
293    res = 0;
294    for (y = 0; y < 4; y++) {
295        z = key->twofish.start;
296 
297        /* do unkeyed substitution */
298        g = sbox(qord[y][z++], (x >> (8*y)) & 255);
299 
300        /* first subkey */
301        i = 0;
302 
303        /* do key mixing+sbox until z==5 */
304        while (z != 5) {
305           g = g ^ key->twofish.S[4*i++ + y];
306           g = sbox(qord[y][z++], g);
307        }
308 
309        /* multiply g by a column of the MDS */
310        res ^= mds_column_mult(g, y);
311    }
312    return res;
313 }
314 
315 #define g1_func(x, key) g_func(ROLc(x, 8), key)
316 
317 #ifdef LTC_CLEAN_STACK
g_func(ulong32 x,const symmetric_key * key)318 static ulong32 g_func(ulong32 x, const symmetric_key *key)
319 {
320     ulong32 y;
321     y = s_g_func(x, key);
322     burn_stack(sizeof(unsigned char) * 4 + sizeof(ulong32));
323     return y;
324 }
325 #endif /* LTC_CLEAN_STACK */
326 
327 #endif /* LTC_TWOFISH_SMALL */
328 
329  /**
330     Initialize the Twofish block cipher
331     @param key The symmetric key you wish to pass
332     @param keylen The key length in bytes
333     @param num_rounds The number of rounds desired (0 for default)
334     @param skey The key in as scheduled by this function.
335     @return CRYPT_OK if successful
336  */
337 #ifdef LTC_CLEAN_STACK
s_twofish_setup(const unsigned char * key,int keylen,int num_rounds,symmetric_key * skey)338 static int s_twofish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
339 #else
340 int twofish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
341 #endif
342 {
343 #ifndef LTC_TWOFISH_SMALL
344    unsigned char S[4*4], tmpx0, tmpx1;
345 #endif
346    int k, x, y;
347    unsigned char tmp[4], tmp2[4], M[8*4];
348    ulong32 A, B;
349 
350    LTC_ARGCHK(key  != NULL);
351    LTC_ARGCHK(skey != NULL);
352 
353    /* invalid arguments? */
354    if (num_rounds != 16 && num_rounds != 0) {
355       return CRYPT_INVALID_ROUNDS;
356    }
357 
358    if (keylen != 16 && keylen != 24 && keylen != 32) {
359       return CRYPT_INVALID_KEYSIZE;
360    }
361 
362    /* k = keysize/64 [but since our keysize is in bytes...] */
363    k = keylen / 8;
364 
365    /* copy the key into M */
366    for (x = 0; x < keylen; x++) {
367        M[x] = key[x] & 255;
368    }
369 
370    /* create the S[..] words */
371 #ifndef LTC_TWOFISH_SMALL
372    for (x = 0; x < k; x++) {
373        rs_mult(M+(x*8), S+(x*4));
374    }
375 #else
376    for (x = 0; x < k; x++) {
377        rs_mult(M+(x*8), skey->twofish.S+(x*4));
378    }
379 #endif
380 
381    /* make subkeys */
382    for (x = 0; x < 20; x++) {
383        /* A = h(p * 2x, Me) */
384        for (y = 0; y < 4; y++) {
385            tmp[y] = x+x;
386        }
387        h_func(tmp, tmp2, M, k, 0);
388        LOAD32L(A, tmp2);
389 
390        /* B = ROL(h(p * (2x + 1), Mo), 8) */
391        for (y = 0; y < 4; y++) {
392            tmp[y] = (unsigned char)(x+x+1);
393        }
394        h_func(tmp, tmp2, M, k, 1);
395        LOAD32L(B, tmp2);
396        B = ROLc(B, 8);
397 
398        /* K[2i]   = A + B */
399        skey->twofish.K[x+x] = (A + B) & 0xFFFFFFFFUL;
400 
401        /* K[2i+1] = (A + 2B) <<< 9 */
402        skey->twofish.K[x+x+1] = ROLc(B + B + A, 9);
403    }
404 
405 #ifndef LTC_TWOFISH_SMALL
406    /* make the sboxes (large ram variant) */
407    if (k == 2) {
408         for (x = 0; x < 256; x++) {
409            tmpx0 = (unsigned char)sbox(0, x);
410            tmpx1 = (unsigned char)sbox(1, x);
411            skey->twofish.S[0][x] = mds_column_mult(sbox(1, (sbox(0, tmpx0 ^ S[0]) ^ S[4])),0);
412            skey->twofish.S[1][x] = mds_column_mult(sbox(0, (sbox(0, tmpx1 ^ S[1]) ^ S[5])),1);
413            skey->twofish.S[2][x] = mds_column_mult(sbox(1, (sbox(1, tmpx0 ^ S[2]) ^ S[6])),2);
414            skey->twofish.S[3][x] = mds_column_mult(sbox(0, (sbox(1, tmpx1 ^ S[3]) ^ S[7])),3);
415         }
416    } else if (k == 3) {
417         for (x = 0; x < 256; x++) {
418            tmpx0 = (unsigned char)sbox(0, x);
419            tmpx1 = (unsigned char)sbox(1, x);
420            skey->twofish.S[0][x] = mds_column_mult(sbox(1, (sbox(0, sbox(0, tmpx1 ^ S[0]) ^ S[4]) ^ S[8])),0);
421            skey->twofish.S[1][x] = mds_column_mult(sbox(0, (sbox(0, sbox(1, tmpx1 ^ S[1]) ^ S[5]) ^ S[9])),1);
422            skey->twofish.S[2][x] = mds_column_mult(sbox(1, (sbox(1, sbox(0, tmpx0 ^ S[2]) ^ S[6]) ^ S[10])),2);
423            skey->twofish.S[3][x] = mds_column_mult(sbox(0, (sbox(1, sbox(1, tmpx0 ^ S[3]) ^ S[7]) ^ S[11])),3);
424         }
425    } else {
426         for (x = 0; x < 256; x++) {
427            tmpx0 = (unsigned char)sbox(0, x);
428            tmpx1 = (unsigned char)sbox(1, x);
429            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);
430            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);
431            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);
432            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);
433         }
434    }
435 #else
436    /* where to start in the sbox layers */
437    /* small ram variant */
438    switch (k) {
439          case 4 : skey->twofish.start = 0; break;
440          case 3 : skey->twofish.start = 1; break;
441          default: skey->twofish.start = 2; break;
442    }
443 #endif
444    return CRYPT_OK;
445 }
446 
447 #ifdef LTC_CLEAN_STACK
twofish_setup(const unsigned char * key,int keylen,int num_rounds,symmetric_key * skey)448 int twofish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
449 {
450    int x;
451    x = s_twofish_setup(key, keylen, num_rounds, skey);
452    burn_stack(sizeof(int) * 7 + sizeof(unsigned char) * 56 + sizeof(ulong32) * 2);
453    return x;
454 }
455 #endif
456 
457 /**
458   Encrypts a block of text with Twofish
459   @param pt The input plaintext (16 bytes)
460   @param ct The output ciphertext (16 bytes)
461   @param skey The key as scheduled
462   @return CRYPT_OK if successful
463 */
464 #ifdef LTC_CLEAN_STACK
s_twofish_ecb_encrypt(const unsigned char * pt,unsigned char * ct,const symmetric_key * skey)465 static int s_twofish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
466 #else
467 int twofish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
468 #endif
469 {
470     ulong32 a,b,c,d,ta,tb,tc,td,t1,t2;
471     const ulong32 *k;
472     int r;
473 #if !defined(LTC_TWOFISH_SMALL) && !defined(__GNUC__)
474     const ulong32 *S1, *S2, *S3, *S4;
475 #endif
476 
477     LTC_ARGCHK(pt   != NULL);
478     LTC_ARGCHK(ct   != NULL);
479     LTC_ARGCHK(skey != NULL);
480 
481 #if !defined(LTC_TWOFISH_SMALL) && !defined(__GNUC__)
482     S1 = skey->twofish.S[0];
483     S2 = skey->twofish.S[1];
484     S3 = skey->twofish.S[2];
485     S4 = skey->twofish.S[3];
486 #endif
487 
488     LOAD32L(a,&pt[0]); LOAD32L(b,&pt[4]);
489     LOAD32L(c,&pt[8]); LOAD32L(d,&pt[12]);
490     a ^= skey->twofish.K[0];
491     b ^= skey->twofish.K[1];
492     c ^= skey->twofish.K[2];
493     d ^= skey->twofish.K[3];
494 
495     k  = skey->twofish.K + 8;
496     for (r = 8; r != 0; --r) {
497         t2 = g1_func(b, skey);
498         t1 = g_func(a, skey) + t2;
499         c  = RORc(c ^ (t1 + k[0]), 1);
500         d  = ROLc(d, 1) ^ (t2 + t1 + k[1]);
501 
502         t2 = g1_func(d, skey);
503         t1 = g_func(c, skey) + t2;
504         a  = RORc(a ^ (t1 + k[2]), 1);
505         b  = ROLc(b, 1) ^ (t2 + t1 + k[3]);
506         k += 4;
507     }
508 
509     /* output with "undo last swap" */
510     ta = c ^ skey->twofish.K[4];
511     tb = d ^ skey->twofish.K[5];
512     tc = a ^ skey->twofish.K[6];
513     td = b ^ skey->twofish.K[7];
514 
515     /* store output */
516     STORE32L(ta,&ct[0]); STORE32L(tb,&ct[4]);
517     STORE32L(tc,&ct[8]); STORE32L(td,&ct[12]);
518 
519     return CRYPT_OK;
520 }
521 
522 #ifdef LTC_CLEAN_STACK
twofish_ecb_encrypt(const unsigned char * pt,unsigned char * ct,const symmetric_key * skey)523 int twofish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
524 {
525    int err = s_twofish_ecb_encrypt(pt, ct, skey);
526    burn_stack(sizeof(ulong32) * 10 + sizeof(int));
527    return err;
528 }
529 #endif
530 
531 /**
532   Decrypts a block of text with Twofish
533   @param ct The input ciphertext (16 bytes)
534   @param pt The output plaintext (16 bytes)
535   @param skey The key as scheduled
536   @return CRYPT_OK if successful
537 */
538 #ifdef LTC_CLEAN_STACK
s_twofish_ecb_decrypt(const unsigned char * ct,unsigned char * pt,const symmetric_key * skey)539 static int s_twofish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
540 #else
541 int twofish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
542 #endif
543 {
544     ulong32 a,b,c,d,ta,tb,tc,td,t1,t2;
545     const ulong32 *k;
546     int r;
547 #if !defined(LTC_TWOFISH_SMALL) && !defined(__GNUC__)
548     const 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
twofish_ecb_decrypt(const unsigned char * ct,unsigned char * pt,const symmetric_key * skey)599 int twofish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
600 {
601    int err = s_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 */
twofish_test(void)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 */
twofish_done(symmetric_key * skey)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 */
twofish_keysize(int * keysize)687 int twofish_keysize(int *keysize)
688 {
689    LTC_ARGCHK(keysize);
690    if (*keysize < 16) {
691       return CRYPT_INVALID_KEYSIZE;
692    }
693    if (*keysize < 24) {
694       *keysize = 16;
695       return CRYPT_OK;
696    }
697    if (*keysize < 32) {
698       *keysize = 24;
699       return CRYPT_OK;
700    }
701    *keysize = 32;
702    return CRYPT_OK;
703 }
704 
705 #endif
706 
707