text: /*
   * Written in 2013 by Gregor Pintar <grpintar@gmail.com>
   *
   * To the extent possible under law, the author(s) have dedicated
   * all copyright and related and neighboring rights to this software
   * to the public domain worldwide.
   * 
   * This software is distributed without any warranty.
   *
   * You should have received a copy of the CC0 Public Domain Dedication.
   * If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
   */
  
  #include <stdio.h>
  #include <stdint.h>
  
  #include <kripto/block.h>
  #include <kripto/block/twofish.h>
  
  #if defined(_TEST) || defined(_PERF)
  
  #ifdef _PERF
  
  #ifndef _CPU
  #define _CPU 2000
  #endif
  
  #ifndef ITERATIONS
  #define ITERATIONS 10000000
  #endif
  
  #include <time.h>
  
  #endif
  
  #include <stdio.h>
  
  int main(void)
  {
  	kripto_block_twofish_t *s;
  	unsigned int i;
  	uint8_t t[16] = {
  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  	};
  	const uint8_t k[32] = {
  		0xD4, 0x3B, 0xB7, 0x55, 0x6E, 0xA3, 0x2E, 0x46,
  		0xF2, 0xA2, 0x82, 0xB7, 0xD4, 0x5B, 0x4E, 0x0D,
  		0x57, 0xFF, 0x73, 0x9D, 0x4D, 0xC9, 0x2C, 0x1B,
  		0xD7, 0xFC, 0x01, 0x70, 0x0C, 0xC8, 0x21, 0x6F
  	};
  	#ifdef _TEST
  	unsigned int n;
  	const uint8_t pt[16] = {
  		0x90, 0xaf, 0xe9, 0x1b, 0xb2, 0x88, 0x54, 0x4f,
  		0x2c, 0x32, 0xdc, 0x23, 0x9b, 0x26, 0x35, 0xe6
  	};
  	const uint8_t ct[32][16] = {
  		{0x5a, 0x26, 0xc8, 0xea, 0x0f, 0xcc, 0x38, 0xcf,
  		0x5c, 0xa2, 0x33, 0xbf, 0x15, 0xd1, 0x83, 0x33},
  		{0x43, 0xbf, 0x15, 0x7e, 0x73, 0x31, 0x8d, 0xc4,
  		0xda, 0xbf, 0xa6, 0x87, 0x64, 0xe5, 0xc0, 0xd4},
  		{0x5b, 0xfa, 0x64, 0xde, 0xb4, 0xea, 0x28, 0x4f,
  		0x3c, 0xb2, 0x8f, 0xb3, 0xc0, 0x4d, 0xf4, 0xd5},
  		{0x75, 0x9d, 0x05, 0x6c, 0xa7, 0x11, 0x9d, 0x09,
  		0xc3, 0xf5, 0x2e, 0xa9, 0x7f, 0x09, 0x9a, 0x69},
  		{0xa2, 0x88, 0x0a, 0x26, 0x2e, 0xa4, 0x89, 0xd2,
  		0x2e, 0xd0, 0x01, 0xf4, 0xe7, 0xec, 0x8d, 0xb4},
  		{0x99, 0x3c, 0x41, 0xad, 0xba, 0x69, 0xe4, 0xd2,
  		0xc0, 0x65, 0x04, 0x97, 0x3d, 0x3b, 0x06, 0x79},
  		{0xf2, 0x15, 0xc9, 0x12, 0xba, 0xe8, 0xd7, 0xea,
  		0x47, 0x2f, 0xab, 0x2f, 0xf3, 0x9a, 0x99, 0xe8},
  		{0x0a, 0xd6, 0xef, 0x10, 0x6b, 0x82, 0x7b, 0x6b,
  		0xa2, 0x5b, 0x8f, 0xfc, 0xe3, 0x6c, 0x38, 0x69},
  		{0xfe, 0xc2, 0x1f, 0xbf, 0x3a, 0x9e, 0xce, 0x9c,
  		0xad, 0x02, 0x72, 0x2f, 0xf2, 0xf1, 0xbc, 0x33},
  		{0x7b, 0xe1, 0xc4, 0x9e, 0xa2, 0xd3, 0x5d, 0x86,
  		0x63, 0xcc, 0x66, 0x61, 0x19, 0x2c, 0xed, 0x0b},
  		{0x15, 0xbb, 0xea, 0x78, 0xc0, 0x86, 0x2d, 0x67,
  		0xed, 0x02, 0x17, 0x9d, 0xd2, 0x6c, 0x69, 0x3c},
  		{0xf9, 0xd9, 0x24, 0x51, 0x78, 0x4e, 0x6a, 0xa0,
  		0x7e, 0x42, 0x41, 0x2b, 0x1f, 0x35, 0x35, 0xe6},
  		{0x4a, 0x92, 0x02, 0x5d, 0x10, 0x98, 0xd9, 0x2d,
  		0x09, 0xbf, 0xe3, 0xa2, 0xf5, 0xf5, 0xb1, 0x8d},
  		{0x0e, 0x1e, 0x27, 0x31, 0xd1, 0x29, 0x5d, 0x41,
  		0xec, 0x18, 0x3b, 0x04, 0x68, 0xd6, 0xfe, 0xd8},
  		{0xfa, 0x77, 0x40, 0x00, 0x19, 0xce, 0x1d, 0xf4,
  		0x8e, 0x12, 0xde, 0x33, 0x80, 0xc2, 0x7d, 0xd6},
  		{0x0d, 0x42, 0xc9, 0x17, 0x72, 0x75, 0x43, 0xe2,
  		0x3e, 0x2b, 0x17, 0x3d, 0x33, 0xdd, 0x3a, 0x6f},
  		{0x6f, 0x51, 0xca, 0x42, 0xc2, 0x5b, 0x26, 0x00,
  		0xe8, 0x34, 0x44, 0x1b, 0x03, 0x87, 0xa0, 0x7f},
  		{0xf9, 0x28, 0x60, 0xa1, 0xf6, 0x32, 0xe8, 0x53,
  		0x3a, 0x47, 0x39, 0x3e, 0xa1, 0x3c, 0x67, 0x17},
  		{0x44, 0x36, 0x83, 0xf8, 0x32, 0x81, 0x58, 0x8c,
  		0xc9, 0xf9, 0x34, 0x5b, 0x41, 0xae, 0x64, 0x44},
  		{0x12, 0x8f, 0x93, 0xe6, 0x47, 0xc8, 0xfc, 0xfd,
  		0xc8, 0xbf, 0xee, 0xfa, 0xa8, 0x53, 0xe9, 0x78},
  		{0xce, 0xf7, 0xcf, 0x6b, 0x0e, 0x3c, 0xb5, 0xc7,
  		0x8b, 0xed, 0x26, 0xce, 0xc6, 0x98, 0x9c, 0x87},
  		{0x95, 0xa7, 0x46, 0x69, 0xd4, 0x8e, 0x69, 0xc9,
  		0x35, 0xf5, 0xe2, 0x6b, 0x70, 0x8c, 0x97, 0xed},
  		{0x59, 0xd9, 0x9b, 0xba, 0x60, 0xc9, 0x6d, 0x1c,
  		0xac, 0x5a, 0xfa, 0x99, 0x81, 0xef, 0x4f, 0x9a},
  		{0x6c, 0x4b, 0x35, 0x8b, 0xbf, 0x3b, 0xdc, 0xcd,
  		0x4e, 0xfd, 0x24, 0x5d, 0xa9, 0x18, 0x39, 0xfb},
  		{0x4d, 0x49, 0x84, 0xef, 0xd9, 0x38, 0xe6, 0xc7,
  		0x78, 0xeb, 0xaf, 0x1f, 0xf1, 0x03, 0x1f, 0x20},
  		{0xf0, 0x4d, 0x1e, 0x67, 0xb7, 0x4a, 0x2b, 0x1c,
  		0x0e, 0x62, 0xd1, 0x42, 0x94, 0x98, 0xaf, 0x8e},
  		{0x2d, 0xa8, 0x98, 0x24, 0xe7, 0x5c, 0x84, 0xa6,
  		0xe0, 0x5b, 0x97, 0x03, 0x40, 0x77, 0xe5, 0x6a},
  		{0x6e, 0x56, 0x43, 0x2c, 0x44, 0x6f, 0xcd, 0xb5,
  		0xa4, 0xe1, 0x48, 0x51, 0x58, 0x38, 0x61, 0xd4},
  		{0xb7, 0xe9, 0xd6, 0xf6, 0x98, 0x94, 0x26, 0xc9,
  		0xf3, 0x25, 0xee, 0x44, 0x17, 0x0a, 0x9e, 0x16},
  		{0x8e, 0xb8, 0x49, 0x66, 0x78, 0x55, 0x8e, 0x94,
  		0x08, 0x18, 0x39, 0x76, 0x8e, 0x3c, 0x4a, 0xe5},
  		{0x54, 0x43, 0x61, 0x0c, 0xee, 0x22, 0x7c, 0xef,
  		0xc8, 0xc5, 0xbf, 0x6b, 0xb9, 0x98, 0x6b, 0xcc},
  		{0x6c, 0xb4, 0x56, 0x1c, 0x40, 0xbf, 0x0a, 0x97,
  		0x05, 0x93, 0x1c, 0xb6, 0xd4, 0x08, 0xe7, 0xfa}
  	};
  	#endif
  	#ifdef _PERF
  	clock_t c;
  	#endif
  
  	puts("Twofish");
  
  	#ifdef _TEST
  	for(n = 1; n <= 32; n++)
  	{
  		s = kripto_block_twofish_new(k, n, TWOFISH_DEFAULT_ROUNDS);
  
  		kripto_block_twofish_encrypt(s, pt, t);
  		for(i = 0; i < 16; i++) if(t[i] != ct[n - 1][i])
  		{
  			printf("%u-bit key encrypt: FAIL\n", n * 8);
  			break;
  		}
  		if(i == 16) printf("%u-bit key encrypt: OK\n", n * 8);
  		kripto_block_twofish_decrypt(s, ct[n - 1], t);
  		for(i = 0; i < 16; i++) if(t[i] != pt[i])
  		{
  			printf("%u-bit key decrypt: FAIL\n", n * 8);
  			break;
  		}
  		if(i == 16) printf("%u-bit key decrypt: OK\n", n * 8);
  
  		kripto_block_twofish_delete(s);
  	}
  	#endif
  	
  	#ifdef _PERF
  	s = kripto_block_twofish_new(k, 32, TWOFISH_DEFAULT_ROUNDS);
  
  	c = clock();
  	for(i = 0; i < ITERATIONS; i++) kripto_block_twofish_encrypt(s, t, t);
  	c = clock() - c;
  
  	printf("encrypt: %.1f cycles/byte, %.1f MB/s\n",
  		(float)c / (float)(ITERATIONS * 16) * _CPU,
  		(float)(ITERATIONS * 16) / ((float)c / (float)CLOCKS_PER_SEC) / 1000000.0);
  
  	c = clock();
  	for(i = 0; i < ITERATIONS; i++) kripto_block_twofish_decrypt(s, t, t);
  	c = clock() - c;
  
  	printf("encrypt: %.1f cycles/byte, %.1f MB/s\n",
  		(float)c / (float)(ITERATIONS * 16) * _CPU,
  		(float)(ITERATIONS * 16) / ((float)c / (float)CLOCKS_PER_SEC) / 1000000.0);
  
  	kripto_block_twofish_delete(s);
  	#endif
  
  	return(0);
  }
  
  #endif
  