]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-threefish-test.c
backporting uart_i and cli
[avr-crypto-lib.git] / test_src / main-threefish-test.c
1 /* main-threefish-test.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2008  Daniel Otte (daniel.otte@rub.de)
5
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.
10
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.
15
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/>.
18 */
19 /*
20  * threefish test-suit
21  * 
22 */
23
24 #include "config.h"
25 #include "serial-tools.h"
26 #include "uart_i.h"
27 #include "debug.h"
28
29 #include "threefish.h"
30 #include "nessie_bc_test.h"
31 #include "cli.h"
32 #include "performance_test.h"
33
34 #include <stdint.h>
35 #include <string.h>
36 #include <stdlib.h>
37
38 char* algo_name = "Threefish";
39
40 /*****************************************************************************
41  *  additional validation-functions                                                                                      *
42  *****************************************************************************/
43
44 void threefish256_dump(threefish256_ctx_t* ctx){
45         uint8_t i;
46         cli_putstr_P(PSTR("\r\n=== ctx dump (256) === \r\n k: "));
47         for(i=0; i<5; ++i){
48                 cli_hexdump(&(ctx->k[i]), 8);
49                 cli_putc(' ');
50         }
51         cli_putstr_P(PSTR("\r\n t: "));
52         for(i=0; i<3; ++i){
53                 cli_hexdump(&(ctx->t[i]), 8);
54                 cli_putc(' ');
55         }
56 }
57
58 void threefish256_dummy_init(const uint8_t* key, uint16_t keysize_b, void* ctx){
59         threefish256_init(key, NULL, ctx);
60 }
61
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;
71         
72         nessie_bc_run();
73 }
74
75 void threefish512_dummy_init(const uint8_t* key, uint16_t keysize_b, void* ctx){
76         threefish512_init(key, NULL, ctx);
77 }
78
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;
88         
89         nessie_bc_run();
90 }
91
92 void threefish1024_dummy_init(const uint8_t* key, uint16_t keysize_b, void* ctx){
93         threefish1024_init(key, NULL, ctx);
94 }
95
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;
105         
106         nessie_bc_run();
107 }
108
109 void testrun_nessie_threefish(void){
110         testrun_nessie_threefish256();
111         testrun_nessie_threefish512();
112         testrun_nessie_threefish1024();
113 }
114
115 void testrun_stdtest_threefish256(void){
116         uint8_t key[32], data[32];
117         uint8_t tweak[16];
118         uint8_t i;
119         threefish256_ctx_t ctx;
120         
121         cli_putstr_P(PSTR("\r\n\r\nTest vectors for block cipher Threefish (256 bits):"));
122         memset(key,  0, 32);
123         memset(data, 0, 32);
124         memset(tweak, 0, 16);
125         
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);
139         
140         /* second test */
141         for(i=0; i<32; ++i){
142                 key[i] = 0x10+i;
143                 data[i] = 0xFF-i;
144         }
145         for(i=0; i<16; ++i){
146                 tweak[i] = i;
147         }
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);
161 }
162
163 void testrun_stdtest_threefish512(void){
164         uint8_t key[64], data[64];
165         uint8_t tweak[16];
166         uint8_t i;
167         threefish512_ctx_t ctx;
168         
169         cli_putstr_P(PSTR("\r\n\r\nTest vectors for block cipher Threefish (512 bits) :"));
170         memset(key,  0, 64);
171         memset(data, 0, 64);
172         memset(tweak, 0, 16);
173         
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);
189         
190         
191         for(i=0; i<64; ++i){
192                 key[i] = 0x10+i;
193                 data[i] = 0xFF-i;
194         }
195         for(i=0; i<16; ++i){
196                 tweak[i] = i;
197         }
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);
213         
214 }
215
216 void testrun_stdtest_threefish1024(void){
217         uint8_t key[128], data[128];
218         uint8_t tweak[16];
219         uint8_t i;
220         threefish1024_ctx_t ctx;
221         
222         cli_putstr_P(PSTR("\r\n\r\nTest vectors for block cipher Threefish (1024 bits) :"));
223         memset(key,  0, 128);
224         memset(data, 0, 128);
225         memset(tweak, 0, 16);
226         
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);
240
241         for(i=0; i<128; ++i){
242                 key[i] = 0x10+i;
243                 data[i] = 0xFF-i;
244         }
245         for(i=0; i<16; ++i){
246                 tweak[i] = i;
247         }
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);
261 }
262
263
264 void testrun_stdtest_threefish(void){
265         testrun_stdtest_threefish256();
266         testrun_stdtest_threefish512();
267         testrun_stdtest_threefish1024();
268 }
269
270 void testrun_performance_threefish256(void){
271         uint64_t t;
272         char str[16];
273         uint8_t key[THREEFISH256_BLOCKSIZE_B];
274         uint8_t data[THREEFISH256_BLOCKSIZE_B];
275         uint8_t tweak[16];
276         threefish256_ctx_t ctx;
277         
278         cli_putstr_P(PSTR("\r\nThreefish-256 performance:"));
279         
280         calibrateTimer();
281         print_overhead();       
282         
283 //      memset(key,  0, THREEFISH256_BLOCKSIZE_B);
284 //      memset(tweak, 0, 16);
285         
286         startTimer(1);
287         threefish256_init(key, tweak, &ctx);
288         t = stopTimer();
289         cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
290         ultoa((unsigned long)t, str, 10);
291         cli_putstr(str);        
292         
293         startTimer(1);
294         threefish256_enc(data, &ctx);
295         t = stopTimer();
296         cli_putstr_P(PSTR("\r\n\tencrypt time: "));
297         ultoa((unsigned long)t, str, 10);
298         cli_putstr(str);        
299         
300         startTimer(1);
301         threefish256_dec(data, &ctx);
302         t = stopTimer();
303         cli_putstr_P(PSTR("\r\n\tdecrypt time: "));
304         ultoa((unsigned long)t, str, 10);
305         cli_putstr(str);        
306         cli_putstr_P(PSTR("\r\n"));     
307 }
308
309 void testrun_performance_threefish512(void){
310         uint64_t t;
311         char str[16];
312         uint8_t key[THREEFISH512_BLOCKSIZE_B];
313         uint8_t data[THREEFISH512_BLOCKSIZE_B];
314         uint8_t tweak[16];
315         threefish512_ctx_t ctx;
316         
317         cli_putstr_P(PSTR("\r\nThreefish-512 performance:"));
318         
319         calibrateTimer();
320         print_overhead();       
321         
322 //      memset(key,  0, THREEFISH512_BLOCKSIZE_B);
323 //      memset(tweak, 0, 16);
324         
325         startTimer(1);
326         threefish512_init(key, tweak, &ctx);
327         t = stopTimer();
328         cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
329         ultoa((unsigned long)t, str, 10);
330         cli_putstr(str);        
331         
332         startTimer(1);
333         threefish512_enc(data, &ctx);
334         t = stopTimer();
335         cli_putstr_P(PSTR("\r\n\tencrypt time: "));
336         ultoa((unsigned long)t, str, 10);
337         cli_putstr(str);        
338         
339         startTimer(1);
340         threefish512_dec(data, &ctx);
341         t = stopTimer();
342         cli_putstr_P(PSTR("\r\n\tdecrypt time: "));
343         ultoa((unsigned long)t, str, 10);
344         cli_putstr(str);        
345         
346         cli_putstr_P(PSTR("\r\n"));     
347 }
348
349 void testrun_performance_threefish1024(void){
350         uint64_t t;
351         char str[16];
352         uint8_t key[THREEFISH1024_BLOCKSIZE_B];
353         uint8_t data[THREEFISH1024_BLOCKSIZE_B];
354         uint8_t tweak[16];
355         threefish1024_ctx_t ctx;
356         
357         cli_putstr_P(PSTR("\r\nThreefish-1024 performance:"));
358         
359         calibrateTimer();
360         print_overhead();       
361         
362 //      memset(key,  0, THREEFISH1024_BLOCKSIZE_B);
363 //      memset(tweak, 0, 16);
364         
365         startTimer(1);
366         threefish1024_init(key, tweak, &ctx);
367         t = stopTimer();
368         cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
369         ultoa((unsigned long)t, str, 10);
370         cli_putstr(str);        
371         
372         startTimer(1);
373         threefish1024_enc(data, &ctx);
374         t = stopTimer();
375         cli_putstr_P(PSTR("\r\n\tencrypt time: "));
376         ultoa((unsigned long)t, str, 10);
377         cli_putstr(str);        
378         
379         startTimer(1);
380         threefish1024_dec(data, &ctx);
381         t = stopTimer();
382         cli_putstr_P(PSTR("\r\n\tdecrypt time: "));
383         ultoa((unsigned long)t, str, 10);
384         cli_putstr(str);        
385         
386         cli_putstr_P(PSTR("\r\n"));     
387 }
388
389 void testrun_performance_threefish(void){
390         testrun_performance_threefish256();
391         testrun_performance_threefish512();
392         testrun_performance_threefish1024();
393 }
394
395 void init_test(void){
396         threefish256_ctx_t ctx;
397         uint8_t key[32], tweak[16];
398         memset(key, 0,32);
399         memset(tweak, 0,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);
405 }
406
407
408 /*****************************************************************************
409  *  main                                                                                                                                         *
410  *****************************************************************************/
411
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";
420
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},
430         { NULL,            NULL, NULL}
431 };
432
433 int main (void){
434         DEBUG_INIT();
435         
436         cli_rx = (cli_rx_fpt)uart0_getc;
437         cli_tx = (cli_tx_fpt)uart0_putc;                
438         for(;;){
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);
443         }
444 }