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/>.
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_dump(threefish256_ctx_t* ctx){
46 cli_putstr_P(PSTR("\r\n=== ctx dump (256) === \r\n k: "));
48 cli_hexdump(&(ctx->k[i]), 8);
51 cli_putstr_P(PSTR("\r\n t: "));
53 cli_hexdump(&(ctx->t[i]), 8);
58 void threefish256_dummy_init(const uint8_t* key, uint16_t keysize_b, void* ctx){
59 threefish256_init(key, NULL, ctx);
62 void testrun_nessie_threefish256(void){
63 nessie_bc_ctx.keysize_b = 256;
64 nessie_bc_ctx.blocksize_B = 32;
65 nessie_bc_ctx.ctx_size_B = sizeof(threefish256_ctx_t);
66 nessie_bc_ctx.name = "Threefish256";
67 nessie_bc_ctx.cipher_genctx = threefish256_dummy_init;
68 nessie_bc_ctx.cipher_enc = (nessie_bc_enc_fpt)threefish256_enc;
69 nessie_bc_ctx.cipher_dec = (nessie_bc_dec_fpt)threefish256_dec;
70 nessie_bc_ctx.cipher_free = NULL;
75 void threefish512_dummy_init(const uint8_t* key, uint16_t keysize_b, void* ctx){
76 threefish512_init(key, NULL, ctx);
79 void testrun_nessie_threefish512(void){
80 nessie_bc_ctx.keysize_b = 512;
81 nessie_bc_ctx.blocksize_B = 64;
82 nessie_bc_ctx.ctx_size_B = sizeof(threefish512_ctx_t);
83 nessie_bc_ctx.name = "Threefish512";
84 nessie_bc_ctx.cipher_genctx = threefish512_dummy_init;
85 nessie_bc_ctx.cipher_enc = (nessie_bc_enc_fpt)threefish512_enc;
86 nessie_bc_ctx.cipher_dec = (nessie_bc_dec_fpt)threefish512_dec;
87 nessie_bc_ctx.cipher_free = NULL;
92 void threefish1024_dummy_init(const uint8_t* key, uint16_t keysize_b, void* ctx){
93 threefish1024_init(key, NULL, ctx);
96 void testrun_nessie_threefish1024(void){
97 nessie_bc_ctx.keysize_b = 1024;
98 nessie_bc_ctx.blocksize_B = 128;
99 nessie_bc_ctx.ctx_size_B = sizeof(threefish1024_ctx_t);
100 nessie_bc_ctx.name = "Threefish1024";
101 nessie_bc_ctx.cipher_genctx = threefish1024_dummy_init;
102 nessie_bc_ctx.cipher_enc = (nessie_bc_enc_fpt)threefish1024_enc;
103 nessie_bc_ctx.cipher_dec = (nessie_bc_dec_fpt)threefish1024_dec;
104 nessie_bc_ctx.cipher_free = NULL;
109 void testrun_nessie_threefish(void){
110 testrun_nessie_threefish256();
111 testrun_nessie_threefish512();
112 testrun_nessie_threefish1024();
115 void testrun_stdtest_threefish256(void){
116 uint8_t key[32], data[32];
119 threefish256_ctx_t ctx;
121 cli_putstr_P(PSTR("\r\n\r\nTest vectors for block cipher Threefish (256 bits):"));
124 memset(tweak, 0, 16);
126 cli_putstr_P(PSTR("\r\nkey: "));
127 cli_hexdump_block(key, 32, 4, 16);
128 cli_putstr_P(PSTR("\r\ntweak: "));
129 cli_hexdump_block(tweak, 16, 4, 16);
130 cli_putstr_P(PSTR("\r\nplain: "));
131 cli_hexdump_block(data, 32, 4, 16);
132 threefish256_init(key, tweak, &ctx);
133 threefish256_enc(data, &ctx);
134 cli_putstr_P(PSTR("\r\ncipher: "));
135 cli_hexdump_block(data, 32, 4, 16);
136 cli_putstr_P(PSTR("\r\ndecipher: "));
137 threefish256_dec(data, &ctx);
138 cli_hexdump_block(data, 32, 4, 16);
148 cli_putstr_P(PSTR("\r\nkey: "));
149 cli_hexdump_block(key, 32, 4, 16);
150 cli_putstr_P(PSTR("\r\ntweak: "));
151 cli_hexdump_block(tweak, 16, 4, 16);
152 cli_putstr_P(PSTR("\r\nplain: "));
153 cli_hexdump_block(data, 32, 4, 16);
154 threefish256_init(key, tweak, &ctx);
155 threefish256_enc(data, &ctx);
156 cli_putstr_P(PSTR("\r\ncipher: "));
157 cli_hexdump_block(data, 32, 4, 16);
158 cli_putstr_P(PSTR("\r\ndecipher: "));
159 threefish256_dec(data, &ctx);
160 cli_hexdump_block(data, 32, 4, 16);
163 void testrun_stdtest_threefish512(void){
164 uint8_t key[64], data[64];
167 threefish512_ctx_t ctx;
169 cli_putstr_P(PSTR("\r\n\r\nTest vectors for block cipher Threefish (512 bits) :"));
172 memset(tweak, 0, 16);
174 cli_putstr_P(PSTR("\r\nkey: "));
175 cli_hexdump_block(key, 32, 4, 16);
176 cli_putstr_P(PSTR("\r\n "));
177 cli_hexdump_block(key+32, 32, 4, 16);
178 cli_putstr_P(PSTR("\r\ntweak: "));
179 cli_hexdump_block(tweak, 16, 4, 16);
180 cli_putstr_P(PSTR("\r\nplain: "));
181 cli_hexdump_block(data, 64, 4, 16);
182 threefish512_init(key, tweak, &ctx);
183 threefish512_enc(data, &ctx);
184 cli_putstr_P(PSTR("\r\ncipher: "));
185 cli_hexdump_block(data, 64, 4, 16);
186 threefish512_dec(data, &ctx);
187 cli_putstr_P(PSTR("\r\ndecipher: "));
188 cli_hexdump_block(data, 64, 4, 16);
198 cli_putstr_P(PSTR("\r\nkey: "));
199 cli_hexdump_block(key, 32, 4, 16);
200 cli_putstr_P(PSTR("\r\n "));
201 cli_hexdump_block(key+32, 32, 4, 16);
202 cli_putstr_P(PSTR("\r\ntweak: "));
203 cli_hexdump_block(tweak, 16, 4, 16);
204 cli_putstr_P(PSTR("\r\nplain: "));
205 cli_hexdump_block(data, 64, 4, 16);
206 threefish512_init(key, tweak, &ctx);
207 threefish512_enc(data, &ctx);
208 cli_putstr_P(PSTR("\r\ncipher: "));
209 cli_hexdump_block(data, 64, 4, 16);
210 threefish512_dec(data, &ctx);
211 cli_putstr_P(PSTR("\r\ndecipher: "));
212 cli_hexdump_block(data, 64, 4, 16);
216 void testrun_stdtest_threefish1024(void){
217 uint8_t key[128], data[128];
220 threefish1024_ctx_t ctx;
222 cli_putstr_P(PSTR("\r\n\r\nTest vectors for block cipher Threefish (1024 bits) :"));
224 memset(data, 0, 128);
225 memset(tweak, 0, 16);
227 cli_putstr_P(PSTR("\r\nkey: "));
228 cli_hexdump_block(key, 128, 4, 16);
229 cli_putstr_P(PSTR("\r\ntweak: "));
230 cli_hexdump_block(tweak, 16, 4, 16);
231 cli_putstr_P(PSTR("\r\nplain: "));
232 cli_hexdump_block(data, 128, 4, 16);
233 threefish1024_init(key, tweak, &ctx);
234 threefish1024_enc(data, &ctx);
235 cli_putstr_P(PSTR("\r\ncipher: "));
236 cli_hexdump_block(data, 128, 4, 16);
237 threefish1024_dec(data, &ctx);
238 cli_putstr_P(PSTR("\r\ndecipher: "));
239 cli_hexdump_block(data, 128, 4, 16);
241 for(i=0; i<128; ++i){
248 cli_putstr_P(PSTR("\r\nkey: "));
249 cli_hexdump_block(key, 128, 4, 16);
250 cli_putstr_P(PSTR("\r\ntweak: "));
251 cli_hexdump_block(tweak, 16, 4, 16);
252 cli_putstr_P(PSTR("\r\nplain: "));
253 cli_hexdump_block(data, 128, 4, 16);
254 threefish1024_init(key, tweak, &ctx);
255 threefish1024_enc(data, &ctx);
256 cli_putstr_P(PSTR("\r\ncipher: "));
257 cli_hexdump_block(data, 128, 4, 16);
258 threefish1024_dec(data, &ctx);
259 cli_putstr_P(PSTR("\r\ndecipher: "));
260 cli_hexdump_block(data, 128, 4, 16);
264 void testrun_stdtest_threefish(void){
265 testrun_stdtest_threefish256();
266 testrun_stdtest_threefish512();
267 testrun_stdtest_threefish1024();
270 void testrun_performance_threefish256(void){
273 uint8_t key[THREEFISH256_BLOCKSIZE_B];
274 uint8_t data[THREEFISH256_BLOCKSIZE_B];
276 threefish256_ctx_t ctx;
278 cli_putstr_P(PSTR("\r\nThreefish-256 performance:"));
283 // memset(key, 0, THREEFISH256_BLOCKSIZE_B);
284 // memset(tweak, 0, 16);
287 threefish256_init(key, tweak, &ctx);
289 cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
290 ultoa((unsigned long)t, str, 10);
294 threefish256_enc(data, &ctx);
296 cli_putstr_P(PSTR("\r\n\tencrypt time: "));
297 ultoa((unsigned long)t, str, 10);
301 threefish256_dec(data, &ctx);
303 cli_putstr_P(PSTR("\r\n\tdecrypt time: "));
304 ultoa((unsigned long)t, str, 10);
306 cli_putstr_P(PSTR("\r\n"));
309 void testrun_performance_threefish512(void){
312 uint8_t key[THREEFISH512_BLOCKSIZE_B];
313 uint8_t data[THREEFISH512_BLOCKSIZE_B];
315 threefish512_ctx_t ctx;
317 cli_putstr_P(PSTR("\r\nThreefish-512 performance:"));
322 // memset(key, 0, THREEFISH512_BLOCKSIZE_B);
323 // memset(tweak, 0, 16);
326 threefish512_init(key, tweak, &ctx);
328 cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
329 ultoa((unsigned long)t, str, 10);
333 threefish512_enc(data, &ctx);
335 cli_putstr_P(PSTR("\r\n\tencrypt time: "));
336 ultoa((unsigned long)t, str, 10);
340 threefish512_dec(data, &ctx);
342 cli_putstr_P(PSTR("\r\n\tdecrypt time: "));
343 ultoa((unsigned long)t, str, 10);
346 cli_putstr_P(PSTR("\r\n"));
349 void testrun_performance_threefish1024(void){
352 uint8_t key[THREEFISH1024_BLOCKSIZE_B];
353 uint8_t data[THREEFISH1024_BLOCKSIZE_B];
355 threefish1024_ctx_t ctx;
357 cli_putstr_P(PSTR("\r\nThreefish-1024 performance:"));
362 // memset(key, 0, THREEFISH1024_BLOCKSIZE_B);
363 // memset(tweak, 0, 16);
366 threefish1024_init(key, tweak, &ctx);
368 cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
369 ultoa((unsigned long)t, str, 10);
373 threefish1024_enc(data, &ctx);
375 cli_putstr_P(PSTR("\r\n\tencrypt time: "));
376 ultoa((unsigned long)t, str, 10);
380 threefish1024_dec(data, &ctx);
382 cli_putstr_P(PSTR("\r\n\tdecrypt time: "));
383 ultoa((unsigned long)t, str, 10);
386 cli_putstr_P(PSTR("\r\n"));
389 void testrun_performance_threefish(void){
390 testrun_performance_threefish256();
391 testrun_performance_threefish512();
392 testrun_performance_threefish1024();
395 void init_test(void){
396 threefish256_ctx_t ctx;
397 uint8_t key[32], tweak[16];
400 threefish256_init(key, tweak, &ctx);
401 cli_putstr_P(PSTR("\r\n ctx: \r\n\tk:"));
402 cli_hexdump(ctx.k, 5*8);
403 cli_putstr_P(PSTR("\r\n\tt:"));
404 cli_hexdump(ctx.t, 3*8);
408 /*****************************************************************************
410 *****************************************************************************/
412 const char nessie_str[] PROGMEM = "nessie";
413 const char test_str[] PROGMEM = "test";
414 const char test256_str[] PROGMEM = "test256";
415 const char test512_str[] PROGMEM = "test512";
416 const char test1024_str[] PROGMEM = "test1024";
417 const char inittest_str[] PROGMEM = "inittest";
418 const char performance_str[] PROGMEM = "performance";
419 const char echo_str[] PROGMEM = "echo";
421 cmdlist_entry_t cmdlist[] PROGMEM = {
422 { nessie_str, NULL, testrun_nessie_threefish},
423 { test_str, NULL, testrun_stdtest_threefish},
424 { test256_str, NULL, testrun_stdtest_threefish256},
425 { test512_str, NULL, testrun_stdtest_threefish512},
426 { test1024_str, NULL, testrun_stdtest_threefish1024},
427 { inittest_str, NULL, init_test},
428 { performance_str, NULL, testrun_performance_threefish},
429 { echo_str, (void*)1, (void_fpt)echo_ctrl},
436 cli_rx = (cli_rx_fpt)uart0_getc;
437 cli_tx = (cli_tx_fpt)uart0_putc;
439 cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
440 cli_putstr(algo_name);
441 cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
442 cmd_interface(cmdlist);