1 /* main-threefish-test.c */
3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "serial-tools.h"
29 #include "threefish.h"
30 #include "nessie_bc_test.h"
32 #include "performance_test.h"
38 char* algo_name = "Threefish";
40 /*****************************************************************************
41 * additional validation-functions *
42 *****************************************************************************/
43 void testrun_stdtest_threefish256(void){
44 uint8_t key[32], data[32];
47 threefish256_ctx_t ctx;
49 cli_putstr_P(PSTR("\r\n\r\nTest vectors for block cipher Threefish (256 bits):"));
54 cli_putstr_P(PSTR("\r\nkey: "));
56 cli_putstr_P(PSTR("\r\ntweak: "));
57 cli_hexdump(tweak, 16);
58 cli_putstr_P(PSTR("\r\nplain: "));
59 cli_hexdump(data, 32);
60 threefish256_init(key, tweak, &ctx);
61 threefish256_enc(data, &ctx);
62 cli_putstr_P(PSTR("\r\ncipher: "));
63 cli_hexdump(data, 32);
65 cli_hexdump_rev(data, 8);
67 cli_hexdump_rev(data+8, 8);
69 cli_hexdump_rev(data+16, 8);
71 cli_hexdump_rev(data+24, 8);
82 cli_putstr_P(PSTR("\r\n\r\nkey: "));
84 cli_putstr_P(PSTR("\r\ntweak: "));
85 cli_hexdump(tweak, 16);
86 cli_putstr_P(PSTR("\r\nplain: "));
87 cli_hexdump(data, 32);
88 threefish256_init(key, tweak, &ctx);
89 threefish256_enc(data, &ctx);
90 cli_putstr_P(PSTR("\r\ncipher: "));
91 cli_hexdump(data, 32);
94 void testrun_stdtest_threefish512(void){
95 uint8_t key[64], data[64];
98 threefish512_ctx_t ctx;
100 cli_putstr_P(PSTR("\r\n\r\nTest vectors for block cipher Threefish (512 bits) :"));
103 memset(tweak, 0, 16);
105 cli_putstr_P(PSTR("\r\nkey: "));
106 cli_hexdump(key, 32);
107 cli_putstr_P(PSTR("\r\n "));
108 cli_hexdump(key+32, 32);
109 cli_putstr_P(PSTR("\r\ntweak: "));
110 cli_hexdump(tweak, 16);
111 cli_putstr_P(PSTR("\r\nplain: "));
112 cli_hexdump(data, 32);
113 cli_putstr_P(PSTR("\r\n "));
114 cli_hexdump(data+32, 32);
115 threefish512_init(key, tweak, &ctx);
116 threefish512_enc(data, &ctx);
117 cli_putstr_P(PSTR("\r\ncipher: "));
118 cli_hexdump(data, 32);
119 cli_putstr_P(PSTR("\r\n "));
120 cli_hexdump(data+32, 32);
129 cli_putstr_P(PSTR("\r\n\r\nkey: "));
130 cli_hexdump(key, 32);
131 cli_putstr_P(PSTR("\r\n "));
132 cli_hexdump(key+32, 32);
133 cli_putstr_P(PSTR("\r\ntweak: "));
134 cli_hexdump(tweak, 16);
135 cli_putstr_P(PSTR("\r\nplain: "));
136 cli_hexdump(data, 32);
137 cli_putstr_P(PSTR("\r\n "));
138 cli_hexdump(data+32, 32);
139 threefish512_init(key, tweak, &ctx);
140 threefish512_enc(data, &ctx);
141 cli_putstr_P(PSTR("\r\ncipher: "));
142 cli_hexdump(data, 32);
143 cli_putstr_P(PSTR("\r\n "));
144 cli_hexdump(data+32, 32);
147 void testrun_stdtest_threefish1024(void){
148 uint8_t key[128], data[128];
151 threefish1024_ctx_t ctx;
153 cli_putstr_P(PSTR("\r\n\r\nTest vectors for block cipher Threefish (1024 bits) :"));
155 memset(data, 0, 128);
156 memset(tweak, 0, 16);
158 cli_putstr_P(PSTR("\r\nkey: "));
159 cli_hexdump(key, 32);
160 cli_putstr_P(PSTR("\r\n "));
161 cli_hexdump(key+32, 32);
162 cli_putstr_P(PSTR("\r\n "));
163 cli_hexdump(key+64, 32);
164 cli_putstr_P(PSTR("\r\n "));
165 cli_hexdump(key+96, 32);
166 cli_putstr_P(PSTR("\r\ntweak: "));
167 cli_hexdump(tweak, 16);
168 cli_putstr_P(PSTR("\r\nplain: "));
169 cli_hexdump(data, 32);
170 cli_putstr_P(PSTR("\r\n "));
171 cli_hexdump(data+32, 32);
172 cli_putstr_P(PSTR("\r\n "));
173 cli_hexdump(data+64, 32);
174 cli_putstr_P(PSTR("\r\n "));
175 cli_hexdump(data+96, 32);
176 threefish1024_init(key, tweak, &ctx);
177 threefish1024_enc(data, &ctx);
178 cli_putstr_P(PSTR("\r\ncipher: "));
179 cli_hexdump(data, 32);
180 cli_putstr_P(PSTR("\r\n "));
181 cli_hexdump(data+32, 32);
182 cli_putstr_P(PSTR("\r\n "));
183 cli_hexdump(data+64, 32);
184 cli_putstr_P(PSTR("\r\n "));
185 cli_hexdump(data+96, 32);
187 for(i=0; i<128; ++i){
194 cli_putstr_P(PSTR("\r\n\r\nkey: "));
195 cli_hexdump(key, 32);
196 cli_putstr_P(PSTR("\r\n "));
197 cli_hexdump(key+32, 32);
198 cli_putstr_P(PSTR("\r\n "));
199 cli_hexdump(key+64, 32);
200 cli_putstr_P(PSTR("\r\n "));
201 cli_hexdump(key+96, 32);
202 cli_putstr_P(PSTR("\r\ntweak: "));
203 cli_hexdump(tweak, 16);
204 cli_putstr_P(PSTR("\r\nplain: "));
205 cli_hexdump(data, 32);
206 cli_putstr_P(PSTR("\r\n "));
207 cli_hexdump(data+32, 32);
208 cli_putstr_P(PSTR("\r\n "));
209 cli_hexdump(data+64, 32);
210 cli_putstr_P(PSTR("\r\n "));
211 cli_hexdump(data+96, 32);
212 threefish1024_init(key, tweak, &ctx);
213 threefish1024_enc(data, &ctx);
214 cli_putstr_P(PSTR("\r\ncipher: "));
215 cli_hexdump(data, 32);
216 cli_putstr_P(PSTR("\r\n "));
217 cli_hexdump(data+32, 32);
218 cli_putstr_P(PSTR("\r\n "));
219 cli_hexdump(data+64, 32);
220 cli_putstr_P(PSTR("\r\n "));
221 cli_hexdump(data+96, 32);
225 void testrun_stdtest_threefish(void){
226 testrun_stdtest_threefish256();
227 testrun_stdtest_threefish512();
228 testrun_stdtest_threefish1024();
231 void testrun_performance_threefish256(void){
234 uint8_t key[THREEFISH256_BLOCKSIZE_B];
235 uint8_t data[THREEFISH256_BLOCKSIZE_B];
237 threefish256_ctx_t ctx;
239 cli_putstr_P(PSTR("\r\nThreefish-256 performance:"));
244 // memset(key, 0, THREEFISH256_BLOCKSIZE_B);
245 // memset(tweak, 0, 16);
248 threefish256_init(key, tweak, &ctx);
250 cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
251 ultoa((unsigned long)t, str, 10);
255 threefish256_enc(data, &ctx);
257 cli_putstr_P(PSTR("\r\n\tencrypt time: "));
258 ultoa((unsigned long)t, str, 10);
261 cli_putstr_P(PSTR("\r\n"));
264 void testrun_performance_threefish512(void){
267 uint8_t key[THREEFISH512_BLOCKSIZE_B];
268 uint8_t data[THREEFISH512_BLOCKSIZE_B];
270 threefish512_ctx_t ctx;
272 cli_putstr_P(PSTR("\r\nThreefish-512 performance:"));
277 // memset(key, 0, THREEFISH512_BLOCKSIZE_B);
278 // memset(tweak, 0, 16);
281 threefish512_init(key, tweak, &ctx);
283 cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
284 ultoa((unsigned long)t, str, 10);
288 threefish512_enc(data, &ctx);
290 cli_putstr_P(PSTR("\r\n\tencrypt time: "));
291 ultoa((unsigned long)t, str, 10);
294 cli_putstr_P(PSTR("\r\n"));
297 void testrun_performance_threefish1024(void){
300 uint8_t key[THREEFISH1024_BLOCKSIZE_B];
301 uint8_t data[THREEFISH1024_BLOCKSIZE_B];
303 threefish1024_ctx_t ctx;
305 cli_putstr_P(PSTR("\r\nThreefish-1024 performance:"));
310 // memset(key, 0, THREEFISH1024_BLOCKSIZE_B);
311 // memset(tweak, 0, 16);
314 threefish1024_init(key, tweak, &ctx);
316 cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
317 ultoa((unsigned long)t, str, 10);
321 threefish1024_enc(data, &ctx);
323 cli_putstr_P(PSTR("\r\n\tencrypt time: "));
324 ultoa((unsigned long)t, str, 10);
327 cli_putstr_P(PSTR("\r\n"));
330 void testrun_performance_threefish(void){
331 testrun_performance_threefish256();
332 testrun_performance_threefish512();
333 testrun_performance_threefish1024();
336 void init_test(void){
337 threefish256_ctx_t ctx;
338 uint8_t key[32], tweak[16];
341 threefish256_init(key, tweak, &ctx);
342 cli_putstr_P(PSTR("\r\n ctx: \r\n\tk:"));
343 cli_hexdump(ctx.k, 5*8);
344 cli_putstr_P(PSTR("\r\n\tt:"));
345 cli_hexdump(ctx.t, 3*8);
349 /*****************************************************************************
351 *****************************************************************************/
353 const char nessie_str[] PROGMEM = "nessie";
354 const char test_str[] PROGMEM = "test";
355 const char inittest_str[] PROGMEM = "inittest";
356 const char performance_str[] PROGMEM = "performance";
357 const char echo_str[] PROGMEM = "echo";
359 cmdlist_entry_t cmdlist[] PROGMEM = {
360 // { nessie_str, NULL, testrun_nessie_noekeon},
361 { test_str, NULL, testrun_stdtest_threefish},
362 { inittest_str, NULL, init_test},
363 { performance_str, NULL, testrun_performance_threefish},
364 { echo_str, (void*)1, (void_fpt)echo_ctrl},
374 cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
375 cli_putstr(algo_name);
376 cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
377 cmd_interface(cmdlist);