~ubuntu-branches/ubuntu/utopic/dropbear/utopic-proposed

« back to all changes in this revision

Viewing changes to libtomcrypt/twofish.c

  • Committer: Bazaar Package Importer
  • Author(s): Matt Johnston
  • Date: 2005-12-08 19:20:21 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051208192021-nyp9rwnt77nsg6ty
Tags: 0.47-1
* New upstream release.
* SECURITY: Fix incorrect buffer sizing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 * Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
10
 
 */
11
 
 
12
 
/* Implementation of Twofish by Tom St Denis */
13
 
#include "mycrypt.h"
14
 
 
15
 
#ifdef TWOFISH
16
 
 
17
 
/* first TWOFISH_ALL_TABLES must ensure TWOFISH_TABLES is defined */
18
 
#ifdef TWOFISH_ALL_TABLES
19
 
#ifndef TWOFISH_TABLES
20
 
#define TWOFISH_TABLES
21
 
#endif
22
 
#endif
23
 
 
24
 
const struct _cipher_descriptor twofish_desc =
25
 
{
26
 
    "twofish",
27
 
    7,
28
 
    16, 32, 16, 16,
29
 
    &twofish_setup,
30
 
    &twofish_ecb_encrypt,
31
 
    &twofish_ecb_decrypt,
32
 
    &twofish_test,
33
 
    &twofish_keysize
34
 
};
35
 
 
36
 
/* the two polynomials */
37
 
#define MDS_POLY          0x169
38
 
#define RS_POLY           0x14D
39
 
 
40
 
/* The 4x4 MDS Linear Transform */
41
 
#if 0
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 }
47
 
};
48
 
#endif
49
 
 
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 }
56
 
};
57
 
 
58
 
/* sbox usage orderings */
59
 
static const unsigned char qord[4][5] = {
60
 
   { 1, 1, 0, 0, 1 },
61
 
   { 0, 1, 1, 0, 0 },
62
 
   { 0, 0, 0, 1, 1 },
63
 
   { 1, 0, 1, 1, 0 }
64
 
};
65
 
 
66
 
#ifdef TWOFISH_TABLES
67
 
 
68
 
#include "twofish_tab.c"
69
 
 
70
 
#define sbox(i, x) ((ulong32)SBOX[i][(x)&255])
71
 
 
72
 
#else
73
 
 
74
 
/* The Q-box tables */
75
 
static const unsigned char qbox[2][4][16] = {
76
 
{
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 }
81
 
},
82
 
{
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 }
87
 
}
88
 
};
89
 
 
90
 
/* computes S_i[x] */
91
 
#ifdef CLEAN_STACK
92
 
static ulong32 _sbox(int i, ulong32 x)
93
 
#else
94
 
static ulong32 sbox(int i, ulong32 x)
95
 
#endif
96
 
{
97
 
   unsigned char a0,b0,a1,b1,a2,b2,a3,b3,a4,b4,y;
98
 
 
99
 
   /* a0,b0 = [x/16], x mod 16 */
100
 
   a0 = (unsigned char)((x>>4)&15);
101
 
   b0 = (unsigned char)((x)&15);
102
 
 
103
 
   /* a1 = a0 ^ b0 */
104
 
   a1 = a0 ^ b0;
105
 
 
106
 
   /* b1 = a0 ^ ROR(b0, 1) ^ 8a0 */
107
 
   b1 = (a0 ^ ((b0<<3)|(b0>>1)) ^ (a0<<3)) & 15;
108
 
 
109
 
   /* a2,b2 = t0[a1], t1[b1] */
110
 
   a2 = qbox[i][0][(int)a1];
111
 
   b2 = qbox[i][1][(int)b1];
112
 
 
113
 
   /* a3 = a2 ^ b2 */
114
 
   a3 = a2 ^ b2;
115
 
 
116
 
   /* b3 = a2 ^ ROR(b2, 1) ^ 8a2 */
117
 
   b3 = (a2 ^ ((b2<<3)|(b2>>1)) ^ (a2<<3)) & 15;
118
 
 
119
 
   /* a4,b4 = t2[a3], t3[b3] */
120
 
   a4 = qbox[i][2][(int)a3];
121
 
   b4 = qbox[i][3][(int)b3];
122
 
 
123
 
   /* y = 16b4 + a4 */
124
 
   y = (b4 << 4) + a4;
125
 
 
126
 
   /* return result */
127
 
   return (ulong32)y;
128
 
}
129
 
 
130
 
#ifdef CLEAN_STACK
131
 
static ulong32 sbox(int i, ulong32 x)
132
 
{
133
 
   ulong32 y;
134
 
   y = _sbox(i, x);
135
 
   burn_stack(sizeof(unsigned char) * 11);
136
 
   return y;
137
 
}
138
 
#endif /* CLEAN_STACK */
139
 
 
140
 
#endif /* TWOFISH_TABLES */
141
 
 
142
 
/* computes ab mod p */
143
 
static ulong32 gf_mult(ulong32 a, ulong32 b, ulong32 p)
144
 
{
145
 
   ulong32 result, B[2], P[2];
146
 
 
147
 
   P[1] = p;
148
 
   B[1] = b;
149
 
   result = P[0] = B[0] = 0;
150
 
 
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); 
159
 
   result ^= B[a&1]; 
160
 
 
161
 
   return result;
162
 
}
163
 
 
164
 
/* computes [y0 y1 y2 y3] = MDS . [x0] */
165
 
#ifndef TWOFISH_TABLES
166
 
static ulong32 mds_column_mult(unsigned char in, int col)
167
 
{
168
 
   ulong32 x01, x5B, xEF;
169
 
 
170
 
   x01 = in;
171
 
   x5B = gf_mult(in, 0x5B, MDS_POLY);
172
 
   xEF = gf_mult(in, 0xEF, MDS_POLY);
173
 
 
174
 
   switch (col) {
175
 
       case 0:
176
 
          return (x01 << 0 ) |
177
 
                 (x5B << 8 ) |
178
 
                 (xEF << 16) |
179
 
                 (xEF << 24);
180
 
       case 1:
181
 
          return (xEF << 0 ) |
182
 
                 (xEF << 8 ) |
183
 
                 (x5B << 16) |
184
 
                 (x01 << 24);
185
 
       case 2:
186
 
          return (x5B << 0 ) |
187
 
                 (xEF << 8 ) |
188
 
                 (x01 << 16) |
189
 
                 (xEF << 24);
190
 
       case 3:
191
 
          return (x5B << 0 ) |
192
 
                 (x01 << 8 ) |
193
 
                 (xEF << 16) |
194
 
                 (x5B << 24);
195
 
   }
196
 
   /* avoid warnings, we'd never get here normally but just to calm compiler warnings... */
197
 
   return 0;
198
 
}
199
 
 
200
 
#else /* !TWOFISH_TABLES */
201
 
 
202
 
#define mds_column_mult(x, i) mds_tab[i][x]
203
 
 
204
 
#endif /* TWOFISH_TABLES */
205
 
 
206
 
/* Computes [y0 y1 y2 y3] = MDS . [x0 x1 x2 x3] */
207
 
static void mds_mult(const unsigned char *in, unsigned char *out)
208
 
{
209
 
  int x;
210
 
  ulong32 tmp;
211
 
  for (tmp = x = 0; x < 4; x++) {
212
 
      tmp ^= mds_column_mult(in[x], x);
213
 
  }
214
 
  STORE32L(tmp, out);
215
 
}
216
 
 
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)
220
 
{
221
 
   ulong32 tmp;
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]];
224
 
   STORE32L(tmp, out);
225
 
}
226
 
 
227
 
#else /* !TWOFISH_ALL_TABLES */
228
 
 
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)
231
 
{
232
 
  int x, y;
233
 
  for (x = 0; x < 4; x++) {
234
 
      out[x] = 0;
235
 
      for (y = 0; y < 8; y++) {
236
 
          out[x] ^= gf_mult(in[y], RS[x][y], RS_POLY);
237
 
      }
238
 
  }
239
 
}
240
 
 
241
 
#endif
242
 
 
243
 
/* computes h(x) */
244
 
static void h_func(const unsigned char *in, unsigned char *out, unsigned char *M, int k, int offset)
245
 
{
246
 
  int x;
247
 
  unsigned char y[4];
248
 
  for (x = 0; x < 4; x++) {
249
 
      y[x] = in[x];
250
 
 }
251
 
  switch (k) {
252
 
     case 4:
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]);
257
 
     case 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]);
262
 
     case 2:
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]));
267
 
  }
268
 
  mds_mult(y, out);
269
 
}
270
 
 
271
 
#ifndef TWOFISH_SMALL
272
 
 
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]
279
 
#endif
280
 
 
281
 
/* the G function */
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)])
284
 
 
285
 
#else
286
 
 
287
 
#ifdef CLEAN_STACK
288
 
static ulong32 _g_func(ulong32 x, symmetric_key *key)
289
 
#else
290
 
static ulong32 g_func(ulong32 x, symmetric_key *key)
291
 
#endif
292
 
{
293
 
   unsigned char g, i, y, z;
294
 
   ulong32 res;
295
 
 
296
 
   res = 0;
297
 
   for (y = 0; y < 4; y++) {
298
 
       z = key->twofish.start;
299
 
 
300
 
       /* do unkeyed substitution */
301
 
       g = sbox(qord[y][z++], (x >> (8*y)) & 255);
302
 
 
303
 
       /* first subkey */
304
 
       i = 0;
305
 
 
306
 
       /* do key mixing+sbox until z==5 */
307
 
       while (z != 5) {
308
 
          g = g ^ key->twofish.S[4*i++ + y];
309
 
          g = sbox(qord[y][z++], g);
310
 
       }
311
 
 
312
 
       /* multiply g by a column of the MDS */
313
 
       res ^= mds_column_mult(g, y);
314
 
   }
315
 
   return res;
316
 
}
317
 
 
318
 
#define g1_func(x, key) g_func(ROL(x, 8), key)
319
 
 
320
 
#ifdef CLEAN_STACK
321
 
static ulong32 g_func(ulong32 x, symmetric_key *key)
322
 
{
323
 
    ulong32 y;
324
 
    y = _g_func(x, key);
325
 
    burn_stack(sizeof(unsigned char) * 4 + sizeof(ulong32));
326
 
    return y;
327
 
}
328
 
#endif /* CLEAN_STACK */
329
 
 
330
 
#endif /* TWOFISH_SMALL */
331
 
 
332
 
#ifdef CLEAN_STACK
333
 
static int _twofish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
334
 
#else
335
 
int twofish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
336
 
#endif
337
 
{
338
 
#ifndef TWOFISH_SMALL
339
 
   unsigned char S[4*4], tmpx0, tmpx1;
340
 
#endif
341
 
   int k, x, y;
342
 
   unsigned char tmp[4], tmp2[4], M[8*4];
343
 
   ulong32 A, B;
344
 
 
345
 
   _ARGCHK(key  != NULL);
346
 
   _ARGCHK(skey != NULL);
347
 
 
348
 
   /* invalid arguments? */
349
 
   if (num_rounds != 16 && num_rounds != 0) {
350
 
      return CRYPT_INVALID_ROUNDS;
351
 
   }
352
 
 
353
 
   if (keylen != 16 && keylen != 24 && keylen != 32) {
354
 
      return CRYPT_INVALID_KEYSIZE;
355
 
   }
356
 
 
357
 
   /* k = keysize/64 [but since our keysize is in bytes...] */
358
 
   k = keylen / 8;
359
 
 
360
 
   /* copy the key into M */
361
 
   for (x = 0; x < keylen; x++) {
362
 
       M[x] = key[x] & 255;
363
 
   }
364
 
 
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));
369
 
   }
370
 
#else
371
 
   for (x = 0; x < k; x++) {
372
 
       rs_mult(M+(x*8), skey->twofish.S+(x*4));
373
 
   }
374
 
#endif
375
 
 
376
 
   /* make subkeys */
377
 
   for (x = 0; x < 20; x++) {
378
 
       /* A = h(p * 2x, Me) */
379
 
       for (y = 0; y < 4; y++) {
380
 
           tmp[y] = x+x;
381
 
       }
382
 
       h_func(tmp, tmp2, M, k, 0);
383
 
       LOAD32L(A, tmp2);
384
 
 
385
 
       /* B = ROL(h(p * (2x + 1), Mo), 8) */
386
 
       for (y = 0; y < 4; y++) {
387
 
           tmp[y] = (unsigned char)(x+x+1);
388
 
       }
389
 
       h_func(tmp, tmp2, M, k, 1);
390
 
       LOAD32L(B, tmp2);
391
 
       B = ROL(B, 8);
392
 
 
393
 
       /* K[2i]   = A + B */
394
 
       skey->twofish.K[x+x] = (A + B) & 0xFFFFFFFFUL;
395
 
 
396
 
       /* K[2i+1] = (A + 2B) <<< 9 */
397
 
       skey->twofish.K[x+x+1] = ROL(B + B + A, 9);
398
 
   }
399
 
 
400
 
#ifndef TWOFISH_SMALL
401
 
   /* make the sboxes (large ram variant) */
402
 
   if (k == 2) {
403
 
        for (x = 0; x < 256; x++) {
404
 
           tmpx0 = sbox(0, x);
405
 
           tmpx1 = sbox(1, 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);
410
 
        }
411
 
   } else if (k == 3) {
412
 
        for (x = 0; x < 256; x++) {
413
 
           tmpx0 = sbox(0, x);
414
 
           tmpx1 = sbox(1, 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);
419
 
        }
420
 
   } else {
421
 
        for (x = 0; x < 256; x++) {
422
 
           tmpx0 = sbox(0, x);
423
 
           tmpx1 = sbox(1, 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);
428
 
        }
429
 
   }
430
 
#else
431
 
   /* where to start in the sbox layers */
432
 
   /* small ram variant */
433
 
   switch (k) {
434
 
         case 4 : skey->twofish.start = 0; break;
435
 
         case 3 : skey->twofish.start = 1; break; 
436
 
         default: skey->twofish.start = 2; break;
437
 
   }
438
 
#endif
439
 
   return CRYPT_OK;
440
 
}
441
 
 
442
 
#ifdef CLEAN_STACK
443
 
int twofish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
444
 
{
445
 
   int x;
446
 
   x = _twofish_setup(key, keylen, num_rounds, skey);
447
 
   burn_stack(sizeof(int) * 7 + sizeof(unsigned char) * 56 + sizeof(ulong32) * 2);
448
 
   return x;
449
 
}
450
 
#endif
451
 
 
452
 
#ifdef CLEAN_STACK
453
 
static void _twofish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
454
 
#else
455
 
void twofish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
456
 
#endif
457
 
{
458
 
    ulong32 a,b,c,d,ta,tb,tc,td,t1,t2, *k;
459
 
    int r;
460
 
#if !defined(TWOFISH_SMALL) && !defined(__GNUC__)
461
 
    ulong32 *S1, *S2, *S3, *S4;
462
 
#endif    
463
 
 
464
 
    _ARGCHK(pt  != NULL);
465
 
    _ARGCHK(ct  != NULL);
466
 
    _ARGCHK(key != NULL);
467
 
    
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];
473
 
#endif    
474
 
 
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];
481
 
    
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]);
488
 
        
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]);
493
 
        k += 4;
494
 
   }
495
 
 
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];
501
 
 
502
 
    /* store output */
503
 
    STORE32L(ta,&ct[0]); STORE32L(tb,&ct[4]);
504
 
    STORE32L(tc,&ct[8]); STORE32L(td,&ct[12]);
505
 
}
506
 
 
507
 
#ifdef CLEAN_STACK
508
 
void twofish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
509
 
{
510
 
   _twofish_ecb_encrypt(pt, ct, key);
511
 
   burn_stack(sizeof(ulong32) * 10 + sizeof(int));
512
 
}
513
 
#endif
514
 
 
515
 
#ifdef CLEAN_STACK
516
 
static void _twofish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
517
 
#else
518
 
void twofish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
519
 
#endif
520
 
{
521
 
    ulong32 a,b,c,d,ta,tb,tc,td,t1,t2, *k;
522
 
    int r;
523
 
#if !defined(TWOFISH_SMALL) && !defined(__GNUC__)
524
 
    ulong32 *S1, *S2, *S3, *S4;
525
 
#endif    
526
 
 
527
 
    _ARGCHK(pt != NULL);
528
 
    _ARGCHK(ct != NULL);
529
 
    _ARGCHK(key != NULL);
530
 
    
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];
536
 
#endif    
537
 
 
538
 
    /* load input */
539
 
    LOAD32L(ta,&ct[0]); LOAD32L(tb,&ct[4]);
540
 
    LOAD32L(tc,&ct[8]); LOAD32L(td,&ct[12]);
541
 
 
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];
547
 
 
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);
554
 
 
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);
559
 
        k -= 4;
560
 
    }
561
 
 
562
 
    /* pre-white */
563
 
    a ^= key->twofish.K[0];
564
 
    b ^= key->twofish.K[1];
565
 
    c ^= key->twofish.K[2];
566
 
    d ^= key->twofish.K[3];
567
 
    
568
 
    /* store */
569
 
    STORE32L(a, &pt[0]); STORE32L(b, &pt[4]);
570
 
    STORE32L(c, &pt[8]); STORE32L(d, &pt[12]);
571
 
}
572
 
 
573
 
#ifdef CLEAN_STACK
574
 
void twofish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
575
 
{
576
 
   _twofish_ecb_decrypt(ct, pt, key);
577
 
   burn_stack(sizeof(ulong32) * 10 + sizeof(int));
578
 
}
579
 
#endif
580
 
 
581
 
int twofish_test(void)
582
 
{
583
 
 #ifndef LTC_TEST
584
 
    return CRYPT_NOP;
585
 
 #else    
586
 
 static const struct { 
587
 
     int keylen;
588
 
     unsigned char key[32], pt[16], ct[16];
589
 
 } tests[] = {
590
 
   { 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 }
597
 
   }, {
598
 
     24,
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 }
606
 
   }, { 
607
 
     32,
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 }
616
 
   }
617
 
};
618
 
 
619
 
 
620
 
 symmetric_key key;
621
 
 unsigned char tmp[2][16];
622
 
 int err, i, y;
623
 
 
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) {
626
 
       return err;
627
 
    }
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;
632
 
    }
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;
638
 
 }    
639
 
 return CRYPT_OK;
640
 
#endif 
641
 
}
642
 
 
643
 
int twofish_keysize(int *desired_keysize)
644
 
{
645
 
   _ARGCHK(desired_keysize);
646
 
   if (*desired_keysize < 16)
647
 
      return CRYPT_INVALID_KEYSIZE;
648
 
   if (*desired_keysize < 24) {
649
 
      *desired_keysize = 16;
650
 
      return CRYPT_OK;
651
 
   } else if (*desired_keysize < 32) {
652
 
      *desired_keysize = 24;
653
 
      return CRYPT_OK;
654
 
   } else {
655
 
      *desired_keysize = 32;
656
 
      return CRYPT_OK;
657
 
   }
658
 
}
659
 
 
660
 
#endif
661
 
 
662
 
 
663