1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
12 Implementation of Twofish by Tom St Denis
18 /* first LTC_TWOFISH_ALL_TABLES must ensure LTC_TWOFISH_TABLES is defined */
19 #ifdef LTC_TWOFISH_ALL_TABLES
20 #ifndef LTC_TWOFISH_TABLES
21 #define LTC_TWOFISH_TABLES
25 const struct ltc_cipher_descriptor twofish_desc =
36 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
39 /* the two polynomials */
40 #define MDS_POLY 0x169
43 /* The 4x8 RS Linear Transform */
44 static const unsigned char RS[4][8] = {
45 { 0x01, 0xA4, 0x55, 0x87, 0x5A, 0x58, 0xDB, 0x9E },
46 { 0xA4, 0x56, 0x82, 0xF3, 0X1E, 0XC6, 0X68, 0XE5 },
47 { 0X02, 0XA1, 0XFC, 0XC1, 0X47, 0XAE, 0X3D, 0X19 },
48 { 0XA4, 0X55, 0X87, 0X5A, 0X58, 0XDB, 0X9E, 0X03 }
51 #ifdef LTC_TWOFISH_SMALL
52 /* sbox usage orderings */
53 static const unsigned char qord[4][5] = {
59 #endif /* LTC_TWOFISH_SMALL */
61 #ifdef LTC_TWOFISH_TABLES
63 #define __LTC_TWOFISH_TAB_C__
64 #include "twofish_tab.c"
66 #define sbox(i, x) ((ulong32)SBOX[i][(x)&255])
70 /* The Q-box tables */
71 static const unsigned char qbox[2][4][16] = {
73 { 0x8, 0x1, 0x7, 0xD, 0x6, 0xF, 0x3, 0x2, 0x0, 0xB, 0x5, 0x9, 0xE, 0xC, 0xA, 0x4 },
74 { 0xE, 0XC, 0XB, 0X8, 0X1, 0X2, 0X3, 0X5, 0XF, 0X4, 0XA, 0X6, 0X7, 0X0, 0X9, 0XD },
75 { 0XB, 0XA, 0X5, 0XE, 0X6, 0XD, 0X9, 0X0, 0XC, 0X8, 0XF, 0X3, 0X2, 0X4, 0X7, 0X1 },
76 { 0XD, 0X7, 0XF, 0X4, 0X1, 0X2, 0X6, 0XE, 0X9, 0XB, 0X3, 0X0, 0X8, 0X5, 0XC, 0XA }
79 { 0X2, 0X8, 0XB, 0XD, 0XF, 0X7, 0X6, 0XE, 0X3, 0X1, 0X9, 0X4, 0X0, 0XA, 0XC, 0X5 },
80 { 0X1, 0XE, 0X2, 0XB, 0X4, 0XC, 0X3, 0X7, 0X6, 0XD, 0XA, 0X5, 0XF, 0X9, 0X0, 0X8 },
81 { 0X4, 0XC, 0X7, 0X5, 0X1, 0X6, 0X9, 0XA, 0X0, 0XE, 0XD, 0X8, 0X2, 0XB, 0X3, 0XF },
82 { 0xB, 0X9, 0X5, 0X1, 0XC, 0X3, 0XD, 0XE, 0X6, 0X4, 0X7, 0XF, 0X2, 0X0, 0X8, 0XA }
87 #ifdef LTC_CLEAN_STACK
88 static ulong32 _sbox(int i, ulong32 x)
90 static ulong32 sbox(int i, ulong32 x)
93 unsigned char a0,b0,a1,b1,a2,b2,a3,b3,a4,b4,y;
95 /* a0,b0 = [x/16], x mod 16 */
96 a0 = (unsigned char)((x>>4)&15);
97 b0 = (unsigned char)((x)&15);
102 /* b1 = a0 ^ ROR(b0, 1) ^ 8a0 */
103 b1 = (a0 ^ ((b0<<3)|(b0>>1)) ^ (a0<<3)) & 15;
105 /* a2,b2 = t0[a1], t1[b1] */
106 a2 = qbox[i][0][(int)a1];
107 b2 = qbox[i][1][(int)b1];
112 /* b3 = a2 ^ ROR(b2, 1) ^ 8a2 */
113 b3 = (a2 ^ ((b2<<3)|(b2>>1)) ^ (a2<<3)) & 15;
115 /* a4,b4 = t2[a3], t3[b3] */
116 a4 = qbox[i][2][(int)a3];
117 b4 = qbox[i][3][(int)b3];
126 #ifdef LTC_CLEAN_STACK
127 static ulong32 sbox(int i, ulong32 x)
131 burn_stack(sizeof(unsigned char) * 11);
134 #endif /* LTC_CLEAN_STACK */
136 #endif /* LTC_TWOFISH_TABLES */
138 /* computes ab mod p */
139 static ulong32 gf_mult(ulong32 a, ulong32 b, ulong32 p)
141 ulong32 result, B[2], P[2];
145 result = P[0] = B[0] = 0;
147 /* unrolled branchless GF multiplier */
148 result ^= B[a&1]; a >>= 1; B[1] = P[B[1]>>7] ^ (B[1] << 1);
149 result ^= B[a&1]; a >>= 1; B[1] = P[B[1]>>7] ^ (B[1] << 1);
150 result ^= B[a&1]; a >>= 1; B[1] = P[B[1]>>7] ^ (B[1] << 1);
151 result ^= B[a&1]; a >>= 1; B[1] = P[B[1]>>7] ^ (B[1] << 1);
152 result ^= B[a&1]; a >>= 1; B[1] = P[B[1]>>7] ^ (B[1] << 1);
153 result ^= B[a&1]; a >>= 1; B[1] = P[B[1]>>7] ^ (B[1] << 1);
154 result ^= B[a&1]; a >>= 1; B[1] = P[B[1]>>7] ^ (B[1] << 1);
160 /* computes [y0 y1 y2 y3] = MDS . [x0] */
161 #ifndef LTC_TWOFISH_TABLES
162 static ulong32 mds_column_mult(unsigned char in, int col)
164 ulong32 x01, x5B, xEF;
167 x5B = gf_mult(in, 0x5B, MDS_POLY);
168 xEF = gf_mult(in, 0xEF, MDS_POLY);
192 /* avoid warnings, we'd never get here normally but just to calm compiler warnings... */
196 #else /* !LTC_TWOFISH_TABLES */
198 #define mds_column_mult(x, i) mds_tab[i][x]
200 #endif /* LTC_TWOFISH_TABLES */
202 /* Computes [y0 y1 y2 y3] = MDS . [x0 x1 x2 x3] */
203 static void mds_mult(const unsigned char *in, unsigned char *out)
207 for (tmp = x = 0; x < 4; x++) {
208 tmp ^= mds_column_mult(in[x], x);
213 #ifdef LTC_TWOFISH_ALL_TABLES
214 /* computes [y0 y1 y2 y3] = RS . [x0 x1 x2 x3 x4 x5 x6 x7] */
215 static void rs_mult(const unsigned char *in, unsigned char *out)
218 tmp = rs_tab0[in[0]] ^ rs_tab1[in[1]] ^ rs_tab2[in[2]] ^ rs_tab3[in[3]] ^
219 rs_tab4[in[4]] ^ rs_tab5[in[5]] ^ rs_tab6[in[6]] ^ rs_tab7[in[7]];
223 #else /* !LTC_TWOFISH_ALL_TABLES */
225 /* computes [y0 y1 y2 y3] = RS . [x0 x1 x2 x3 x4 x5 x6 x7] */
226 static void rs_mult(const unsigned char *in, unsigned char *out)
229 for (x = 0; x < 4; x++) {
231 for (y = 0; y < 8; y++) {
232 out[x] ^= gf_mult(in[y], RS[x][y], RS_POLY);
240 static void h_func(const unsigned char *in, unsigned char *out, unsigned char *M, int k, int offset)
244 for (x = 0; x < 4; x++) {
249 y[0] = (unsigned char)(sbox(1, (ulong32)y[0]) ^ M[4 * (6 + offset) + 0]);
250 y[1] = (unsigned char)(sbox(0, (ulong32)y[1]) ^ M[4 * (6 + offset) + 1]);
251 y[2] = (unsigned char)(sbox(0, (ulong32)y[2]) ^ M[4 * (6 + offset) + 2]);
252 y[3] = (unsigned char)(sbox(1, (ulong32)y[3]) ^ M[4 * (6 + offset) + 3]);
255 y[0] = (unsigned char)(sbox(1, (ulong32)y[0]) ^ M[4 * (4 + offset) + 0]);
256 y[1] = (unsigned char)(sbox(1, (ulong32)y[1]) ^ M[4 * (4 + offset) + 1]);
257 y[2] = (unsigned char)(sbox(0, (ulong32)y[2]) ^ M[4 * (4 + offset) + 2]);
258 y[3] = (unsigned char)(sbox(0, (ulong32)y[3]) ^ M[4 * (4 + offset) + 3]);
261 y[0] = (unsigned char)(sbox(1, sbox(0, sbox(0, (ulong32)y[0]) ^ M[4 * (2 + offset) + 0]) ^ M[4 * (0 + offset) + 0]));
262 y[1] = (unsigned char)(sbox(0, sbox(0, sbox(1, (ulong32)y[1]) ^ M[4 * (2 + offset) + 1]) ^ M[4 * (0 + offset) + 1]));
263 y[2] = (unsigned char)(sbox(1, sbox(1, sbox(0, (ulong32)y[2]) ^ M[4 * (2 + offset) + 2]) ^ M[4 * (0 + offset) + 2]));
264 y[3] = (unsigned char)(sbox(0, sbox(1, sbox(1, (ulong32)y[3]) ^ M[4 * (2 + offset) + 3]) ^ M[4 * (0 + offset) + 3]));
270 #ifndef LTC_TWOFISH_SMALL
272 /* for GCC we don't use pointer aliases */
273 #if defined(__GNUC__)
274 #define S1 skey->twofish.S[0]
275 #define S2 skey->twofish.S[1]
276 #define S3 skey->twofish.S[2]
277 #define S4 skey->twofish.S[3]
281 #define g_func(x, dum) (S1[byte(x,0)] ^ S2[byte(x,1)] ^ S3[byte(x,2)] ^ S4[byte(x,3)])
282 #define g1_func(x, dum) (S2[byte(x,0)] ^ S3[byte(x,1)] ^ S4[byte(x,2)] ^ S1[byte(x,3)])
286 #ifdef LTC_CLEAN_STACK
287 static ulong32 _g_func(ulong32 x, symmetric_key *key)
289 static ulong32 g_func(ulong32 x, symmetric_key *key)
292 unsigned char g, i, y, z;
296 for (y = 0; y < 4; y++) {
297 z = key->twofish.start;
299 /* do unkeyed substitution */
300 g = sbox(qord[y][z++], (x >> (8*y)) & 255);
305 /* do key mixing+sbox until z==5 */
307 g = g ^ key->twofish.S[4*i++ + y];
308 g = sbox(qord[y][z++], g);
311 /* multiply g by a column of the MDS */
312 res ^= mds_column_mult(g, y);
317 #define g1_func(x, key) g_func(ROLc(x, 8), key)
319 #ifdef LTC_CLEAN_STACK
320 static ulong32 g_func(ulong32 x, symmetric_key *key)
324 burn_stack(sizeof(unsigned char) * 4 + sizeof(ulong32));
327 #endif /* LTC_CLEAN_STACK */
329 #endif /* LTC_TWOFISH_SMALL */
332 Initialize the Twofish block cipher
333 @param key The symmetric key you wish to pass
334 @param keylen The key length in bytes
335 @param num_rounds The number of rounds desired (0 for default)
336 @param skey The key in as scheduled by this function.
337 @return CRYPT_OK if successful
339 #ifdef LTC_CLEAN_STACK
340 static int _twofish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
342 int twofish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
345 #ifndef LTC_TWOFISH_SMALL
346 unsigned char S[4*4], tmpx0, tmpx1;
349 unsigned char tmp[4], tmp2[4], M[8*4];
352 LTC_ARGCHK(key != NULL);
353 LTC_ARGCHK(skey != NULL);
355 /* invalid arguments? */
356 if (num_rounds != 16 && num_rounds != 0) {
357 return CRYPT_INVALID_ROUNDS;
360 if (keylen != 16 && keylen != 24 && keylen != 32) {
361 return CRYPT_INVALID_KEYSIZE;
364 /* k = keysize/64 [but since our keysize is in bytes...] */
367 /* copy the key into M */
368 for (x = 0; x < keylen; x++) {
372 /* create the S[..] words */
373 #ifndef LTC_TWOFISH_SMALL
374 for (x = 0; x < k; x++) {
375 rs_mult(M+(x*8), S+(x*4));
378 for (x = 0; x < k; x++) {
379 rs_mult(M+(x*8), skey->twofish.S+(x*4));
384 for (x = 0; x < 20; x++) {
385 /* A = h(p * 2x, Me) */
386 for (y = 0; y < 4; y++) {
389 h_func(tmp, tmp2, M, k, 0);
392 /* B = ROL(h(p * (2x + 1), Mo), 8) */
393 for (y = 0; y < 4; y++) {
394 tmp[y] = (unsigned char)(x+x+1);
396 h_func(tmp, tmp2, M, k, 1);
401 skey->twofish.K[x+x] = (A + B) & 0xFFFFFFFFUL;
403 /* K[2i+1] = (A + 2B) <<< 9 */
404 skey->twofish.K[x+x+1] = ROLc(B + B + A, 9);
407 #ifndef LTC_TWOFISH_SMALL
408 /* make the sboxes (large ram variant) */
410 for (x = 0; x < 256; x++) {
411 tmpx0 = (unsigned char)sbox(0, x);
412 tmpx1 = (unsigned char)sbox(1, x);
413 skey->twofish.S[0][x] = mds_column_mult(sbox(1, (sbox(0, tmpx0 ^ S[0]) ^ S[4])),0);
414 skey->twofish.S[1][x] = mds_column_mult(sbox(0, (sbox(0, tmpx1 ^ S[1]) ^ S[5])),1);
415 skey->twofish.S[2][x] = mds_column_mult(sbox(1, (sbox(1, tmpx0 ^ S[2]) ^ S[6])),2);
416 skey->twofish.S[3][x] = mds_column_mult(sbox(0, (sbox(1, tmpx1 ^ S[3]) ^ S[7])),3);
419 for (x = 0; x < 256; x++) {
420 tmpx0 = (unsigned char)sbox(0, x);
421 tmpx1 = (unsigned char)sbox(1, x);
422 skey->twofish.S[0][x] = mds_column_mult(sbox(1, (sbox(0, sbox(0, tmpx1 ^ S[0]) ^ S[4]) ^ S[8])),0);
423 skey->twofish.S[1][x] = mds_column_mult(sbox(0, (sbox(0, sbox(1, tmpx1 ^ S[1]) ^ S[5]) ^ S[9])),1);
424 skey->twofish.S[2][x] = mds_column_mult(sbox(1, (sbox(1, sbox(0, tmpx0 ^ S[2]) ^ S[6]) ^ S[10])),2);
425 skey->twofish.S[3][x] = mds_column_mult(sbox(0, (sbox(1, sbox(1, tmpx0 ^ S[3]) ^ S[7]) ^ S[11])),3);
428 for (x = 0; x < 256; x++) {
429 tmpx0 = (unsigned char)sbox(0, x);
430 tmpx1 = (unsigned char)sbox(1, x);
431 skey->twofish.S[0][x] = mds_column_mult(sbox(1, (sbox(0, sbox(0, sbox(1, tmpx1 ^ S[0]) ^ S[4]) ^ S[8]) ^ S[12])),0);
432 skey->twofish.S[1][x] = mds_column_mult(sbox(0, (sbox(0, sbox(1, sbox(1, tmpx0 ^ S[1]) ^ S[5]) ^ S[9]) ^ S[13])),1);
433 skey->twofish.S[2][x] = mds_column_mult(sbox(1, (sbox(1, sbox(0, sbox(0, tmpx0 ^ S[2]) ^ S[6]) ^ S[10]) ^ S[14])),2);
434 skey->twofish.S[3][x] = mds_column_mult(sbox(0, (sbox(1, sbox(1, sbox(0, tmpx1 ^ S[3]) ^ S[7]) ^ S[11]) ^ S[15])),3);
438 /* where to start in the sbox layers */
439 /* small ram variant */
441 case 4 : skey->twofish.start = 0; break;
442 case 3 : skey->twofish.start = 1; break;
443 default: skey->twofish.start = 2; break;
449 #ifdef LTC_CLEAN_STACK
450 int twofish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
453 x = _twofish_setup(key, keylen, num_rounds, skey);
454 burn_stack(sizeof(int) * 7 + sizeof(unsigned char) * 56 + sizeof(ulong32) * 2);
460 Encrypts a block of text with Twofish
461 @param pt The input plaintext (16 bytes)
462 @param ct The output ciphertext (16 bytes)
463 @param skey The key as scheduled
464 @return CRYPT_OK if successful
466 #ifdef LTC_CLEAN_STACK
467 static int _twofish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
469 int twofish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
472 ulong32 a,b,c,d,ta,tb,tc,td,t1,t2, *k;
474 #if !defined(LTC_TWOFISH_SMALL) && !defined(__GNUC__)
475 ulong32 *S1, *S2, *S3, *S4;
478 LTC_ARGCHK(pt != NULL);
479 LTC_ARGCHK(ct != NULL);
480 LTC_ARGCHK(skey != NULL);
482 #if !defined(LTC_TWOFISH_SMALL) && !defined(__GNUC__)
483 S1 = skey->twofish.S[0];
484 S2 = skey->twofish.S[1];
485 S3 = skey->twofish.S[2];
486 S4 = skey->twofish.S[3];
489 LOAD32L(a,&pt[0]); LOAD32L(b,&pt[4]);
490 LOAD32L(c,&pt[8]); LOAD32L(d,&pt[12]);
491 a ^= skey->twofish.K[0];
492 b ^= skey->twofish.K[1];
493 c ^= skey->twofish.K[2];
494 d ^= skey->twofish.K[3];
496 k = skey->twofish.K + 8;
497 for (r = 8; r != 0; --r) {
498 t2 = g1_func(b, skey);
499 t1 = g_func(a, skey) + t2;
500 c = RORc(c ^ (t1 + k[0]), 1);
501 d = ROLc(d, 1) ^ (t2 + t1 + k[1]);
503 t2 = g1_func(d, skey);
504 t1 = g_func(c, skey) + t2;
505 a = RORc(a ^ (t1 + k[2]), 1);
506 b = ROLc(b, 1) ^ (t2 + t1 + k[3]);
510 /* output with "undo last swap" */
511 ta = c ^ skey->twofish.K[4];
512 tb = d ^ skey->twofish.K[5];
513 tc = a ^ skey->twofish.K[6];
514 td = b ^ skey->twofish.K[7];
517 STORE32L(ta,&ct[0]); STORE32L(tb,&ct[4]);
518 STORE32L(tc,&ct[8]); STORE32L(td,&ct[12]);
523 #ifdef LTC_CLEAN_STACK
524 int twofish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
526 int err = _twofish_ecb_encrypt(pt, ct, skey);
527 burn_stack(sizeof(ulong32) * 10 + sizeof(int));
533 Decrypts a block of text with Twofish
534 @param ct The input ciphertext (16 bytes)
535 @param pt The output plaintext (16 bytes)
536 @param skey The key as scheduled
537 @return CRYPT_OK if successful
539 #ifdef LTC_CLEAN_STACK
540 static int _twofish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
542 int twofish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
545 ulong32 a,b,c,d,ta,tb,tc,td,t1,t2, *k;
547 #if !defined(LTC_TWOFISH_SMALL) && !defined(__GNUC__)
548 ulong32 *S1, *S2, *S3, *S4;
551 LTC_ARGCHK(pt != NULL);
552 LTC_ARGCHK(ct != NULL);
553 LTC_ARGCHK(skey != NULL);
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];
563 LOAD32L(ta,&ct[0]); LOAD32L(tb,&ct[4]);
564 LOAD32L(tc,&ct[8]); LOAD32L(td,&ct[12]);
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];
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);
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);
587 a ^= skey->twofish.K[0];
588 b ^= skey->twofish.K[1];
589 c ^= skey->twofish.K[2];
590 d ^= skey->twofish.K[3];
593 STORE32L(a, &pt[0]); STORE32L(b, &pt[4]);
594 STORE32L(c, &pt[8]); STORE32L(d, &pt[12]);
598 #ifdef LTC_CLEAN_STACK
599 int twofish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
601 int err =_twofish_ecb_decrypt(ct, pt, skey);
602 burn_stack(sizeof(ulong32) * 10 + sizeof(int));
608 Performs a self-test of the Twofish block cipher
609 @return CRYPT_OK if functional, CRYPT_NOP if self-test has been disabled
611 int twofish_test(void)
616 static const struct {
618 unsigned char key[32], pt[16], ct[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 }
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 }
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 }
651 unsigned char tmp[2][16];
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) {
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;
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;
674 /** Terminate the context
675 @param skey The scheduled key
677 void twofish_done(symmetric_key *skey)
679 LTC_UNUSED_PARAM(skey);
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.
687 int twofish_keysize(int *keysize)
691 return CRYPT_INVALID_KEYSIZE;
695 } else if (*keysize < 32) {
709 /* ref: $Format:%D$ */
710 /* git commit: $Format:%H$ */
711 /* commit time: $Format:%ai$ */