comparison src/ciphers/twofish/twofish.c @ 280:59400faa4b44 libtomcrypt-orig libtomcrypt-1.05

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