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