status update, probably last commit
[rofl0r-kripto.git] / test / block / twofish.c
blob3517cbc87e9a794793791a554a0c41a24aa6e5d2
1 /*
2 * Written in 2013 by Gregor Pintar <grpintar@gmail.com>
4 * To the extent possible under law, the author(s) have dedicated
5 * all copyright and related and neighboring rights to this software
6 * to the public domain worldwide.
7 *
8 * This software is distributed without any warranty.
10 * You should have received a copy of the CC0 Public Domain Dedication.
11 * If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
14 #include <stdio.h>
15 #include <stdint.h>
17 #include <kripto/block.h>
18 #include <kripto/block/twofish.h>
20 #if defined(_TEST) || defined(_PERF)
22 #ifdef _PERF
24 #ifndef _CPU
25 #define _CPU 2000
26 #endif
28 #ifndef ITERATIONS
29 #define ITERATIONS 10000000
30 #endif
32 #include <time.h>
34 #endif
36 #include <stdio.h>
38 int main(void)
40 kripto_block_twofish_t *s;
41 unsigned int i;
42 uint8_t t[16] = {
43 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
44 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
46 const uint8_t k[32] = {
47 0xD4, 0x3B, 0xB7, 0x55, 0x6E, 0xA3, 0x2E, 0x46,
48 0xF2, 0xA2, 0x82, 0xB7, 0xD4, 0x5B, 0x4E, 0x0D,
49 0x57, 0xFF, 0x73, 0x9D, 0x4D, 0xC9, 0x2C, 0x1B,
50 0xD7, 0xFC, 0x01, 0x70, 0x0C, 0xC8, 0x21, 0x6F
52 #ifdef _TEST
53 unsigned int n;
54 const uint8_t pt[16] = {
55 0x90, 0xaf, 0xe9, 0x1b, 0xb2, 0x88, 0x54, 0x4f,
56 0x2c, 0x32, 0xdc, 0x23, 0x9b, 0x26, 0x35, 0xe6
58 const uint8_t ct[32][16] = {
59 {0x5a, 0x26, 0xc8, 0xea, 0x0f, 0xcc, 0x38, 0xcf,
60 0x5c, 0xa2, 0x33, 0xbf, 0x15, 0xd1, 0x83, 0x33},
61 {0x43, 0xbf, 0x15, 0x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4a, 0x92, 0x02, 0x5d, 0x10, 0x98, 0xd9, 0x2d,
84 0x09, 0xbf, 0xe3, 0xa2, 0xf5, 0xf5, 0xb1, 0x8d},
85 {0x0e, 0x1e, 0x27, 0x31, 0xd1, 0x29, 0x5d, 0x41,
86 0xec, 0x18, 0x3b, 0x04, 0x68, 0xd6, 0xfe, 0xd8},
87 {0xfa, 0x77, 0x40, 0x00, 0x19, 0xce, 0x1d, 0xf4,
88 0x8e, 0x12, 0xde, 0x33, 0x80, 0xc2, 0x7d, 0xd6},
89 {0x0d, 0x42, 0xc9, 0x17, 0x72, 0x75, 0x43, 0xe2,
90 0x3e, 0x2b, 0x17, 0x3d, 0x33, 0xdd, 0x3a, 0x6f},
91 {0x6f, 0x51, 0xca, 0x42, 0xc2, 0x5b, 0x26, 0x00,
92 0xe8, 0x34, 0x44, 0x1b, 0x03, 0x87, 0xa0, 0x7f},
93 {0xf9, 0x28, 0x60, 0xa1, 0xf6, 0x32, 0xe8, 0x53,
94 0x3a, 0x47, 0x39, 0x3e, 0xa1, 0x3c, 0x67, 0x17},
95 {0x44, 0x36, 0x83, 0xf8, 0x32, 0x81, 0x58, 0x8c,
96 0xc9, 0xf9, 0x34, 0x5b, 0x41, 0xae, 0x64, 0x44},
97 {0x12, 0x8f, 0x93, 0xe6, 0x47, 0xc8, 0xfc, 0xfd,
98 0xc8, 0xbf, 0xee, 0xfa, 0xa8, 0x53, 0xe9, 0x78},
99 {0xce, 0xf7, 0xcf, 0x6b, 0x0e, 0x3c, 0xb5, 0xc7,
100 0x8b, 0xed, 0x26, 0xce, 0xc6, 0x98, 0x9c, 0x87},
101 {0x95, 0xa7, 0x46, 0x69, 0xd4, 0x8e, 0x69, 0xc9,
102 0x35, 0xf5, 0xe2, 0x6b, 0x70, 0x8c, 0x97, 0xed},
103 {0x59, 0xd9, 0x9b, 0xba, 0x60, 0xc9, 0x6d, 0x1c,
104 0xac, 0x5a, 0xfa, 0x99, 0x81, 0xef, 0x4f, 0x9a},
105 {0x6c, 0x4b, 0x35, 0x8b, 0xbf, 0x3b, 0xdc, 0xcd,
106 0x4e, 0xfd, 0x24, 0x5d, 0xa9, 0x18, 0x39, 0xfb},
107 {0x4d, 0x49, 0x84, 0xef, 0xd9, 0x38, 0xe6, 0xc7,
108 0x78, 0xeb, 0xaf, 0x1f, 0xf1, 0x03, 0x1f, 0x20},
109 {0xf0, 0x4d, 0x1e, 0x67, 0xb7, 0x4a, 0x2b, 0x1c,
110 0x0e, 0x62, 0xd1, 0x42, 0x94, 0x98, 0xaf, 0x8e},
111 {0x2d, 0xa8, 0x98, 0x24, 0xe7, 0x5c, 0x84, 0xa6,
112 0xe0, 0x5b, 0x97, 0x03, 0x40, 0x77, 0xe5, 0x6a},
113 {0x6e, 0x56, 0x43, 0x2c, 0x44, 0x6f, 0xcd, 0xb5,
114 0xa4, 0xe1, 0x48, 0x51, 0x58, 0x38, 0x61, 0xd4},
115 {0xb7, 0xe9, 0xd6, 0xf6, 0x98, 0x94, 0x26, 0xc9,
116 0xf3, 0x25, 0xee, 0x44, 0x17, 0x0a, 0x9e, 0x16},
117 {0x8e, 0xb8, 0x49, 0x66, 0x78, 0x55, 0x8e, 0x94,
118 0x08, 0x18, 0x39, 0x76, 0x8e, 0x3c, 0x4a, 0xe5},
119 {0x54, 0x43, 0x61, 0x0c, 0xee, 0x22, 0x7c, 0xef,
120 0xc8, 0xc5, 0xbf, 0x6b, 0xb9, 0x98, 0x6b, 0xcc},
121 {0x6c, 0xb4, 0x56, 0x1c, 0x40, 0xbf, 0x0a, 0x97,
122 0x05, 0x93, 0x1c, 0xb6, 0xd4, 0x08, 0xe7, 0xfa}
124 #endif
125 #ifdef _PERF
126 clock_t c;
127 #endif
129 puts("Twofish");
131 #ifdef _TEST
132 for(n = 1; n <= 32; n++)
134 s = kripto_block_twofish_new(k, n, TWOFISH_DEFAULT_ROUNDS);
136 kripto_block_twofish_encrypt(s, pt, t);
137 for(i = 0; i < 16; i++) if(t[i] != ct[n - 1][i])
139 printf("%u-bit key encrypt: FAIL\n", n * 8);
140 break;
142 if(i == 16) printf("%u-bit key encrypt: OK\n", n * 8);
143 kripto_block_twofish_decrypt(s, ct[n - 1], t);
144 for(i = 0; i < 16; i++) if(t[i] != pt[i])
146 printf("%u-bit key decrypt: FAIL\n", n * 8);
147 break;
149 if(i == 16) printf("%u-bit key decrypt: OK\n", n * 8);
151 kripto_block_twofish_delete(s);
153 #endif
155 #ifdef _PERF
156 s = kripto_block_twofish_new(k, 32, TWOFISH_DEFAULT_ROUNDS);
158 c = clock();
159 for(i = 0; i < ITERATIONS; i++) kripto_block_twofish_encrypt(s, t, t);
160 c = clock() - c;
162 printf("encrypt: %.1f cycles/byte, %.1f MB/s\n",
163 (float)c / (float)(ITERATIONS * 16) * _CPU,
164 (float)(ITERATIONS * 16) / ((float)c / (float)CLOCKS_PER_SEC) / 1000000.0);
166 c = clock();
167 for(i = 0; i < ITERATIONS; i++) kripto_block_twofish_decrypt(s, t, t);
168 c = clock() - c;
170 printf("encrypt: %.1f cycles/byte, %.1f MB/s\n",
171 (float)c / (float)(ITERATIONS * 16) * _CPU,
172 (float)(ITERATIONS * 16) / ((float)c / (float)CLOCKS_PER_SEC) / 1000000.0);
174 kripto_block_twofish_delete(s);
175 #endif
177 return(0);
180 #endif