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 *****************************************************************************/
44 void threefish256_dummy_init(const uint8_t* key, uint16_t keysize_b, void* ctx){
47 threefish256_init(key, null, ctx);
50 void testrun_nessie_threefish(void){
51 nessie_bc_ctx.keysize_b = 256;
52 nessie_bc_ctx.blocksize_B = 32;
53 nessie_bc_ctx.ctx_size_B = sizeof(threefish256_ctx_t);
54 nessie_bc_ctx.name = "Threefish256";
55 nessie_bc_ctx.cipher_genctx = threefish256_dummy_init;
56 nessie_bc_ctx.cipher_enc = (nessie_bc_enc_fpt)threefish256_enc;
57 nessie_bc_ctx.cipher_dec = (nessie_bc_dec_fpt)threefish256_dec;
58 nessie_bc_ctx.cipher_free = NULL;
63 void testrun_stdtest_threefish256(void){
64 uint8_t key[32], data[32];
67 threefish256_ctx_t ctx;
69 cli_putstr_P(PSTR("\r\n\r\nTest vectors for block cipher Threefish (256 bits):"));
74 cli_putstr_P(PSTR("\r\nkey: "));
76 cli_putstr_P(PSTR("\r\ntweak: "));
77 cli_hexdump(tweak, 16);
78 cli_putstr_P(PSTR("\r\nplain: "));
79 cli_hexdump(data, 32);
80 threefish256_init(key, tweak, &ctx);
81 threefish256_enc(data, &ctx);
82 cli_putstr_P(PSTR("\r\ncipher: "));
83 cli_hexdump(data, 32);
85 cli_hexdump_rev(data, 8);
87 cli_hexdump_rev(data+8, 8);
89 cli_hexdump_rev(data+16, 8);
91 cli_hexdump_rev(data+24, 8);
102 cli_putstr_P(PSTR("\r\n\r\nkey: "));
103 cli_hexdump(key, 32);
104 cli_putstr_P(PSTR("\r\ntweak: "));
105 cli_hexdump(tweak, 16);
106 cli_putstr_P(PSTR("\r\nplain: "));
107 cli_hexdump(data, 32);
108 threefish256_init(key, tweak, &ctx);
109 threefish256_enc(data, &ctx);
110 cli_putstr_P(PSTR("\r\ncipher: "));
111 cli_hexdump(data, 32);
114 void testrun_stdtest_threefish512(void){
115 uint8_t key[64], data[64];
118 threefish512_ctx_t ctx;
120 cli_putstr_P(PSTR("\r\n\r\nTest vectors for block cipher Threefish (512 bits) :"));
123 memset(tweak, 0, 16);
125 cli_putstr_P(PSTR("\r\nkey: "));
126 cli_hexdump(key, 32);
127 cli_putstr_P(PSTR("\r\n "));
128 cli_hexdump(key+32, 32);
129 cli_putstr_P(PSTR("\r\ntweak: "));
130 cli_hexdump(tweak, 16);
131 cli_putstr_P(PSTR("\r\nplain: "));
132 cli_hexdump(data, 32);
133 cli_putstr_P(PSTR("\r\n "));
134 cli_hexdump(data+32, 32);
135 threefish512_init(key, tweak, &ctx);
136 threefish512_enc(data, &ctx);
137 cli_putstr_P(PSTR("\r\ncipher: "));
138 cli_hexdump(data, 32);
139 cli_putstr_P(PSTR("\r\n "));
140 cli_hexdump(data+32, 32);
149 cli_putstr_P(PSTR("\r\n\r\nkey: "));
150 cli_hexdump(key, 32);
151 cli_putstr_P(PSTR("\r\n "));
152 cli_hexdump(key+32, 32);
153 cli_putstr_P(PSTR("\r\ntweak: "));
154 cli_hexdump(tweak, 16);
155 cli_putstr_P(PSTR("\r\nplain: "));
156 cli_hexdump(data, 32);
157 cli_putstr_P(PSTR("\r\n "));
158 cli_hexdump(data+32, 32);
159 threefish512_init(key, tweak, &ctx);
160 threefish512_enc(data, &ctx);
161 cli_putstr_P(PSTR("\r\ncipher: "));
162 cli_hexdump(data, 32);
163 cli_putstr_P(PSTR("\r\n "));
164 cli_hexdump(data+32, 32);
167 void testrun_stdtest_threefish1024(void){
168 uint8_t key[128], data[128];
171 threefish1024_ctx_t ctx;
173 cli_putstr_P(PSTR("\r\n\r\nTest vectors for block cipher Threefish (1024 bits) :"));
175 memset(data, 0, 128);
176 memset(tweak, 0, 16);
178 cli_putstr_P(PSTR("\r\nkey: "));
179 cli_hexdump(key, 32);
180 cli_putstr_P(PSTR("\r\n "));
181 cli_hexdump(key+32, 32);
182 cli_putstr_P(PSTR("\r\n "));
183 cli_hexdump(key+64, 32);
184 cli_putstr_P(PSTR("\r\n "));
185 cli_hexdump(key+96, 32);
186 cli_putstr_P(PSTR("\r\ntweak: "));
187 cli_hexdump(tweak, 16);
188 cli_putstr_P(PSTR("\r\nplain: "));
189 cli_hexdump(data, 32);
190 cli_putstr_P(PSTR("\r\n "));
191 cli_hexdump(data+32, 32);
192 cli_putstr_P(PSTR("\r\n "));
193 cli_hexdump(data+64, 32);
194 cli_putstr_P(PSTR("\r\n "));
195 cli_hexdump(data+96, 32);
196 threefish1024_init(key, tweak, &ctx);
197 threefish1024_enc(data, &ctx);
198 cli_putstr_P(PSTR("\r\ncipher: "));
199 cli_hexdump(data, 32);
200 cli_putstr_P(PSTR("\r\n "));
201 cli_hexdump(data+32, 32);
202 cli_putstr_P(PSTR("\r\n "));
203 cli_hexdump(data+64, 32);
204 cli_putstr_P(PSTR("\r\n "));
205 cli_hexdump(data+96, 32);
207 for(i=0; i<128; ++i){
214 cli_putstr_P(PSTR("\r\n\r\nkey: "));
215 cli_hexdump(key, 32);
216 cli_putstr_P(PSTR("\r\n "));
217 cli_hexdump(key+32, 32);
218 cli_putstr_P(PSTR("\r\n "));
219 cli_hexdump(key+64, 32);
220 cli_putstr_P(PSTR("\r\n "));
221 cli_hexdump(key+96, 32);
222 cli_putstr_P(PSTR("\r\ntweak: "));
223 cli_hexdump(tweak, 16);
224 cli_putstr_P(PSTR("\r\nplain: "));
225 cli_hexdump(data, 32);
226 cli_putstr_P(PSTR("\r\n "));
227 cli_hexdump(data+32, 32);
228 cli_putstr_P(PSTR("\r\n "));
229 cli_hexdump(data+64, 32);
230 cli_putstr_P(PSTR("\r\n "));
231 cli_hexdump(data+96, 32);
232 threefish1024_init(key, tweak, &ctx);
233 threefish1024_enc(data, &ctx);
234 cli_putstr_P(PSTR("\r\ncipher: "));
235 cli_hexdump(data, 32);
236 cli_putstr_P(PSTR("\r\n "));
237 cli_hexdump(data+32, 32);
238 cli_putstr_P(PSTR("\r\n "));
239 cli_hexdump(data+64, 32);
240 cli_putstr_P(PSTR("\r\n "));
241 cli_hexdump(data+96, 32);
245 void testrun_stdtest_threefish(void){
246 testrun_stdtest_threefish256();
247 testrun_stdtest_threefish512();
248 testrun_stdtest_threefish1024();
251 void testrun_performance_threefish256(void){
254 uint8_t key[THREEFISH256_BLOCKSIZE_B];
255 uint8_t data[THREEFISH256_BLOCKSIZE_B];
257 threefish256_ctx_t ctx;
259 cli_putstr_P(PSTR("\r\nThreefish-256 performance:"));
264 // memset(key, 0, THREEFISH256_BLOCKSIZE_B);
265 // memset(tweak, 0, 16);
268 threefish256_init(key, tweak, &ctx);
270 cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
271 ultoa((unsigned long)t, str, 10);
275 threefish256_enc(data, &ctx);
277 cli_putstr_P(PSTR("\r\n\tencrypt time: "));
278 ultoa((unsigned long)t, str, 10);
281 cli_putstr_P(PSTR("\r\n"));
284 void testrun_performance_threefish512(void){
287 uint8_t key[THREEFISH512_BLOCKSIZE_B];
288 uint8_t data[THREEFISH512_BLOCKSIZE_B];
290 threefish512_ctx_t ctx;
292 cli_putstr_P(PSTR("\r\nThreefish-512 performance:"));
297 // memset(key, 0, THREEFISH512_BLOCKSIZE_B);
298 // memset(tweak, 0, 16);
301 threefish512_init(key, tweak, &ctx);
303 cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
304 ultoa((unsigned long)t, str, 10);
308 threefish512_enc(data, &ctx);
310 cli_putstr_P(PSTR("\r\n\tencrypt time: "));
311 ultoa((unsigned long)t, str, 10);
314 cli_putstr_P(PSTR("\r\n"));
317 void testrun_performance_threefish1024(void){
320 uint8_t key[THREEFISH1024_BLOCKSIZE_B];
321 uint8_t data[THREEFISH1024_BLOCKSIZE_B];
323 threefish1024_ctx_t ctx;
325 cli_putstr_P(PSTR("\r\nThreefish-1024 performance:"));
330 // memset(key, 0, THREEFISH1024_BLOCKSIZE_B);
331 // memset(tweak, 0, 16);
334 threefish1024_init(key, tweak, &ctx);
336 cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
337 ultoa((unsigned long)t, str, 10);
341 threefish1024_enc(data, &ctx);
343 cli_putstr_P(PSTR("\r\n\tencrypt time: "));
344 ultoa((unsigned long)t, str, 10);
347 cli_putstr_P(PSTR("\r\n"));
350 void testrun_performance_threefish(void){
351 testrun_performance_threefish256();
352 testrun_performance_threefish512();
353 testrun_performance_threefish1024();
356 void init_test(void){
357 threefish256_ctx_t ctx;
358 uint8_t key[32], tweak[16];
361 threefish256_init(key, tweak, &ctx);
362 cli_putstr_P(PSTR("\r\n ctx: \r\n\tk:"));
363 cli_hexdump(ctx.k, 5*8);
364 cli_putstr_P(PSTR("\r\n\tt:"));
365 cli_hexdump(ctx.t, 3*8);
369 /*****************************************************************************
371 *****************************************************************************/
373 const char nessie_str[] PROGMEM = "nessie";
374 const char test_str[] PROGMEM = "test";
375 const char inittest_str[] PROGMEM = "inittest";
376 const char performance_str[] PROGMEM = "performance";
377 const char echo_str[] PROGMEM = "echo";
379 cmdlist_entry_t cmdlist[] PROGMEM = {
380 { nessie_str, NULL, testrun_nessie_threefish},
381 { test_str, NULL, testrun_stdtest_threefish},
382 { inittest_str, NULL, init_test},
383 { performance_str, NULL, testrun_performance_threefish},
384 { echo_str, (void*)1, (void_fpt)echo_ctrl},
394 cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
395 cli_putstr(algo_name);
396 cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
397 cmd_interface(cmdlist);