]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-threefish-test.c
f5d10ebb0b0c9fd1da7c22728890864a50984200
[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.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_dummy_init(const uint8_t* key, uint16_t keysize_b, void* ctx){
45         uint8_t null[16];
46         memset(null, 0, 16);
47         threefish256_init(key, null, ctx);
48 }
49
50 void testrun_nessie_threefish256(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;
59         
60         nessie_bc_run();
61 }
62
63 void threefish512_dummy_init(const uint8_t* key, uint16_t keysize_b, void* ctx){
64         uint8_t null[16];
65         memset(null, 0, 16);
66         threefish512_init(key, null, ctx);
67 }
68
69 void testrun_nessie_threefish512(void){
70         nessie_bc_ctx.keysize_b = 512;
71         nessie_bc_ctx.blocksize_B = 64;
72         nessie_bc_ctx.ctx_size_B = sizeof(threefish512_ctx_t);
73         nessie_bc_ctx.name = "Threefish512";
74         nessie_bc_ctx.cipher_genctx = threefish512_dummy_init;
75         nessie_bc_ctx.cipher_enc = (nessie_bc_enc_fpt)threefish512_enc;
76         nessie_bc_ctx.cipher_dec = (nessie_bc_dec_fpt)threefish512_dec;
77         nessie_bc_ctx.cipher_free = NULL;
78         
79         nessie_bc_run();
80 }
81
82 void threefish1024_dummy_init(const uint8_t* key, uint16_t keysize_b, void* ctx){
83         uint8_t null[16];
84         memset(null, 0, 16);
85         threefish1024_init(key, null, ctx);
86 }
87
88 void testrun_nessie_threefish1024(void){
89         nessie_bc_ctx.keysize_b = 1024;
90         nessie_bc_ctx.blocksize_B = 128;
91         nessie_bc_ctx.ctx_size_B = sizeof(threefish1024_ctx_t);
92         nessie_bc_ctx.name = "Threefish1024";
93         nessie_bc_ctx.cipher_genctx = threefish1024_dummy_init;
94         nessie_bc_ctx.cipher_enc = (nessie_bc_enc_fpt)threefish1024_enc;
95         nessie_bc_ctx.cipher_dec = (nessie_bc_dec_fpt)threefish1024_dec;
96         nessie_bc_ctx.cipher_free = NULL;
97         
98         nessie_bc_run();
99 }
100
101 void testrun_nessie_threefish(void){
102         testrun_nessie_threefish256();
103         testrun_nessie_threefish512();
104         testrun_nessie_threefish1024();
105 }
106
107 void testrun_stdtest_threefish256(void){
108         uint8_t key[32], data[32];
109         uint8_t tweak[16];
110         uint8_t i;
111         threefish256_ctx_t ctx;
112         
113         cli_putstr_P(PSTR("\r\n\r\nTest vectors for block cipher Threefish (256 bits):"));
114         memset(key,  0, 32);
115         memset(data, 0, 32);
116         memset(tweak, 0, 16);
117         
118         cli_putstr_P(PSTR("\r\nkey:    "));
119         cli_hexdump(key, 32);
120         cli_putstr_P(PSTR("\r\ntweak:  "));
121         cli_hexdump(tweak, 16);
122         cli_putstr_P(PSTR("\r\nplain:  "));
123         cli_hexdump(data, 32);
124         threefish256_init(key, tweak, &ctx);
125         threefish256_enc(data, &ctx);
126         cli_putstr_P(PSTR("\r\ncipher: "));
127         cli_hexdump(data, 32);
128         /* second test */
129         for(i=0; i<32; ++i){
130                 key[i] = 0x10+i;
131                 data[i] = 0xFF-i;
132         }
133         for(i=0; i<16; ++i){
134                 tweak[i] = i;
135         }
136         cli_putstr_P(PSTR("\r\n\r\nkey:    "));
137         cli_hexdump(key, 32);
138         cli_putstr_P(PSTR("\r\ntweak:  "));
139         cli_hexdump(tweak, 16);
140         cli_putstr_P(PSTR("\r\nplain:  "));
141         cli_hexdump(data, 32);
142         threefish256_init(key, tweak, &ctx);
143         threefish256_enc(data, &ctx);
144         cli_putstr_P(PSTR("\r\ncipher: "));
145         cli_hexdump(data, 32);
146 }
147
148 void testrun_stdtest_threefish512(void){
149         uint8_t key[64], data[64];
150         uint8_t tweak[16];
151         uint8_t i;
152         threefish512_ctx_t ctx;
153         
154         cli_putstr_P(PSTR("\r\n\r\nTest vectors for block cipher Threefish (512 bits) :"));
155         memset(key,  0, 64);
156         memset(data, 0, 64);
157         memset(tweak, 0, 16);
158         
159         cli_putstr_P(PSTR("\r\nkey:    "));
160         cli_hexdump(key, 32);
161         cli_putstr_P(PSTR("\r\n        "));
162         cli_hexdump(key+32, 32);
163         cli_putstr_P(PSTR("\r\ntweak:  "));
164         cli_hexdump(tweak, 16);
165         cli_putstr_P(PSTR("\r\nplain:  "));
166         cli_hexdump(data, 32);
167         cli_putstr_P(PSTR("\r\n        "));
168         cli_hexdump(data+32, 32);
169         threefish512_init(key, tweak, &ctx);
170         threefish512_enc(data, &ctx);
171         cli_putstr_P(PSTR("\r\ncipher: "));
172         cli_hexdump(data, 32);
173         cli_putstr_P(PSTR("\r\n        "));
174         cli_hexdump(data+32, 32);
175         
176         for(i=0; i<64; ++i){
177                 key[i] = 0x10+i;
178                 data[i] = 0xFF-i;
179         }
180         for(i=0; i<16; ++i){
181                 tweak[i] = i;
182         }
183         cli_putstr_P(PSTR("\r\n\r\nkey:    "));
184         cli_hexdump(key, 32);
185         cli_putstr_P(PSTR("\r\n        "));
186         cli_hexdump(key+32, 32);
187         cli_putstr_P(PSTR("\r\ntweak:  "));
188         cli_hexdump(tweak, 16);
189         cli_putstr_P(PSTR("\r\nplain:  "));
190         cli_hexdump(data, 32);
191         cli_putstr_P(PSTR("\r\n        "));
192         cli_hexdump(data+32, 32);
193         threefish512_init(key, tweak, &ctx);
194         threefish512_enc(data, &ctx);
195         cli_putstr_P(PSTR("\r\ncipher: "));
196         cli_hexdump(data, 32);
197         cli_putstr_P(PSTR("\r\n        "));
198         cli_hexdump(data+32, 32);
199 }
200
201 void testrun_stdtest_threefish1024(void){
202         uint8_t key[128], data[128];
203         uint8_t tweak[16];
204         uint8_t i;
205         threefish1024_ctx_t ctx;
206         
207         cli_putstr_P(PSTR("\r\n\r\nTest vectors for block cipher Threefish (1024 bits) :"));
208         memset(key,  0, 128);
209         memset(data, 0, 128);
210         memset(tweak, 0, 16);
211         
212         cli_putstr_P(PSTR("\r\nkey:    "));
213         cli_hexdump(key, 32);
214         cli_putstr_P(PSTR("\r\n        "));
215         cli_hexdump(key+32, 32);
216         cli_putstr_P(PSTR("\r\n        "));
217         cli_hexdump(key+64, 32);
218         cli_putstr_P(PSTR("\r\n        "));
219         cli_hexdump(key+96, 32);
220         cli_putstr_P(PSTR("\r\ntweak:  "));
221         cli_hexdump(tweak, 16);
222         cli_putstr_P(PSTR("\r\nplain:  "));
223         cli_hexdump(data, 32);
224         cli_putstr_P(PSTR("\r\n        "));
225         cli_hexdump(data+32, 32);
226         cli_putstr_P(PSTR("\r\n        "));
227         cli_hexdump(data+64, 32);
228         cli_putstr_P(PSTR("\r\n        "));
229         cli_hexdump(data+96, 32);
230         threefish1024_init(key, tweak, &ctx);
231         threefish1024_enc(data, &ctx);
232         cli_putstr_P(PSTR("\r\ncipher: "));
233         cli_hexdump(data, 32);
234         cli_putstr_P(PSTR("\r\n        "));
235         cli_hexdump(data+32, 32);
236         cli_putstr_P(PSTR("\r\n        "));
237         cli_hexdump(data+64, 32);
238         cli_putstr_P(PSTR("\r\n        "));
239         cli_hexdump(data+96, 32);
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\n\r\nkey:    "));
249         cli_hexdump(key, 32);
250         cli_putstr_P(PSTR("\r\n        "));
251         cli_hexdump(key+32, 32);
252         cli_putstr_P(PSTR("\r\n        "));
253         cli_hexdump(key+64, 32);
254         cli_putstr_P(PSTR("\r\n        "));
255         cli_hexdump(key+96, 32);
256         cli_putstr_P(PSTR("\r\ntweak:  "));
257         cli_hexdump(tweak, 16);
258         cli_putstr_P(PSTR("\r\nplain:  "));
259         cli_hexdump(data, 32);
260         cli_putstr_P(PSTR("\r\n        "));
261         cli_hexdump(data+32, 32);
262         cli_putstr_P(PSTR("\r\n        "));
263         cli_hexdump(data+64, 32);
264         cli_putstr_P(PSTR("\r\n        "));
265         cli_hexdump(data+96, 32);
266         threefish1024_init(key, tweak, &ctx);
267         threefish1024_enc(data, &ctx);
268         cli_putstr_P(PSTR("\r\ncipher: "));
269         cli_hexdump(data, 32);
270         cli_putstr_P(PSTR("\r\n        "));
271         cli_hexdump(data+32, 32);
272         cli_putstr_P(PSTR("\r\n        "));
273         cli_hexdump(data+64, 32);
274         cli_putstr_P(PSTR("\r\n        "));
275         cli_hexdump(data+96, 32);
276 }
277
278
279 void testrun_stdtest_threefish(void){
280         testrun_stdtest_threefish256();
281         testrun_stdtest_threefish512();
282         testrun_stdtest_threefish1024();
283 }
284
285 void testrun_performance_threefish256(void){
286         uint64_t t;
287         char str[16];
288         uint8_t key[THREEFISH256_BLOCKSIZE_B];
289         uint8_t data[THREEFISH256_BLOCKSIZE_B];
290         uint8_t tweak[16];
291         threefish256_ctx_t ctx;
292         
293         cli_putstr_P(PSTR("\r\nThreefish-256 performance:"));
294         
295         calibrateTimer();
296         print_overhead();       
297         
298 //      memset(key,  0, THREEFISH256_BLOCKSIZE_B);
299 //      memset(tweak, 0, 16);
300         
301         startTimer(1);
302         threefish256_init(key, tweak, &ctx);
303         t = stopTimer();
304         cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
305         ultoa((unsigned long)t, str, 10);
306         cli_putstr(str);        
307         
308         startTimer(1);
309         threefish256_enc(data, &ctx);
310         t = stopTimer();
311         cli_putstr_P(PSTR("\r\n\tencrypt time: "));
312         ultoa((unsigned long)t, str, 10);
313         cli_putstr(str);        
314         
315         cli_putstr_P(PSTR("\r\n"));     
316 }
317
318 void testrun_performance_threefish512(void){
319         uint64_t t;
320         char str[16];
321         uint8_t key[THREEFISH512_BLOCKSIZE_B];
322         uint8_t data[THREEFISH512_BLOCKSIZE_B];
323         uint8_t tweak[16];
324         threefish512_ctx_t ctx;
325         
326         cli_putstr_P(PSTR("\r\nThreefish-512 performance:"));
327         
328         calibrateTimer();
329         print_overhead();       
330         
331 //      memset(key,  0, THREEFISH512_BLOCKSIZE_B);
332 //      memset(tweak, 0, 16);
333         
334         startTimer(1);
335         threefish512_init(key, tweak, &ctx);
336         t = stopTimer();
337         cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
338         ultoa((unsigned long)t, str, 10);
339         cli_putstr(str);        
340         
341         startTimer(1);
342         threefish512_enc(data, &ctx);
343         t = stopTimer();
344         cli_putstr_P(PSTR("\r\n\tencrypt time: "));
345         ultoa((unsigned long)t, str, 10);
346         cli_putstr(str);        
347         
348         cli_putstr_P(PSTR("\r\n"));     
349 }
350
351 void testrun_performance_threefish1024(void){
352         uint64_t t;
353         char str[16];
354         uint8_t key[THREEFISH1024_BLOCKSIZE_B];
355         uint8_t data[THREEFISH1024_BLOCKSIZE_B];
356         uint8_t tweak[16];
357         threefish1024_ctx_t ctx;
358         
359         cli_putstr_P(PSTR("\r\nThreefish-1024 performance:"));
360         
361         calibrateTimer();
362         print_overhead();       
363         
364 //      memset(key,  0, THREEFISH1024_BLOCKSIZE_B);
365 //      memset(tweak, 0, 16);
366         
367         startTimer(1);
368         threefish1024_init(key, tweak, &ctx);
369         t = stopTimer();
370         cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
371         ultoa((unsigned long)t, str, 10);
372         cli_putstr(str);        
373         
374         startTimer(1);
375         threefish1024_enc(data, &ctx);
376         t = stopTimer();
377         cli_putstr_P(PSTR("\r\n\tencrypt time: "));
378         ultoa((unsigned long)t, str, 10);
379         cli_putstr(str);        
380         
381         cli_putstr_P(PSTR("\r\n"));     
382 }
383
384 void testrun_performance_threefish(void){
385         testrun_performance_threefish256();
386         testrun_performance_threefish512();
387         testrun_performance_threefish1024();
388 }
389
390 void init_test(void){
391         threefish256_ctx_t ctx;
392         uint8_t key[32], tweak[16];
393         memset(key, 0,32);
394         memset(tweak, 0,16);
395         threefish256_init(key, tweak, &ctx);
396         cli_putstr_P(PSTR("\r\n ctx: \r\n\tk:"));
397         cli_hexdump(ctx.k, 5*8);
398         cli_putstr_P(PSTR("\r\n\tt:"));
399         cli_hexdump(ctx.t, 3*8);
400 }
401
402
403 /*****************************************************************************
404  *  main                                                                                                                                         *
405  *****************************************************************************/
406
407 const char nessie_str[]      PROGMEM = "nessie";
408 const char test_str[]        PROGMEM = "test";
409 const char test256_str[]     PROGMEM = "test256";
410 const char test512_str[]     PROGMEM = "test512";
411 const char test1024_str[]    PROGMEM = "test1024";
412 const char inittest_str[]    PROGMEM = "inittest";
413 const char performance_str[] PROGMEM = "performance";
414 const char echo_str[]        PROGMEM = "echo";
415
416 cmdlist_entry_t cmdlist[] PROGMEM = {
417         { nessie_str,      NULL, testrun_nessie_threefish},
418         { test_str,        NULL, testrun_stdtest_threefish},
419     { test256_str,     NULL, testrun_stdtest_threefish256},
420     { test512_str,     NULL, testrun_stdtest_threefish512},
421     { test1024_str,    NULL, testrun_stdtest_threefish1024},
422         { inittest_str,    NULL, init_test},
423         { performance_str, NULL, testrun_performance_threefish},
424         { echo_str,    (void*)1, (void_fpt)echo_ctrl},
425         { NULL,            NULL, NULL}
426 };
427
428 int main (void){
429         DEBUG_INIT();
430         
431         cli_rx = uart_getc;
432         cli_tx = uart_putc;             
433         for(;;){
434                 cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
435                 cli_putstr(algo_name);
436                 cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
437                 cmd_interface(cmdlist);
438         }
439 }