]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-threefish-test.c
+threefish256 decryption
[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_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;
59         
60         nessie_bc_run();
61 }
62
63 void testrun_stdtest_threefish256(void){
64         uint8_t key[32], data[32];
65         uint8_t tweak[16];
66         uint8_t i;
67         threefish256_ctx_t ctx;
68         
69         cli_putstr_P(PSTR("\r\n\r\nTest vectors for block cipher Threefish (256 bits):"));
70         memset(key,  0, 32);
71         memset(data, 0, 32);
72         memset(tweak, 0, 16);
73         
74         cli_putstr_P(PSTR("\r\nkey:    "));
75         cli_hexdump(key, 32);
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);
84         /* 
85         cli_hexdump_rev(data, 8);
86         cli_putc(' ');
87         cli_hexdump_rev(data+8, 8);
88         cli_putc(' ');
89         cli_hexdump_rev(data+16, 8);
90         cli_putc(' ');
91         cli_hexdump_rev(data+24, 8);
92         cli_putc(' ');
93         */
94         /* second test */
95         for(i=0; i<32; ++i){
96                 key[i] = 0x10+i;
97                 data[i] = 0xFF-i;
98         }
99         for(i=0; i<16; ++i){
100                 tweak[i] = i;
101         }
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);
112 }
113
114 void testrun_stdtest_threefish512(void){
115         uint8_t key[64], data[64];
116         uint8_t tweak[16];
117         uint8_t i;
118         threefish512_ctx_t ctx;
119         
120         cli_putstr_P(PSTR("\r\n\r\nTest vectors for block cipher Threefish (512 bits) :"));
121         memset(key,  0, 64);
122         memset(data, 0, 64);
123         memset(tweak, 0, 16);
124         
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);
141         
142         for(i=0; i<64; ++i){
143                 key[i] = 0x10+i;
144                 data[i] = 0xFF-i;
145         }
146         for(i=0; i<16; ++i){
147                 tweak[i] = i;
148         }
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);
165 }
166
167 void testrun_stdtest_threefish1024(void){
168         uint8_t key[128], data[128];
169         uint8_t tweak[16];
170         uint8_t i;
171         threefish1024_ctx_t ctx;
172         
173         cli_putstr_P(PSTR("\r\n\r\nTest vectors for block cipher Threefish (1024 bits) :"));
174         memset(key,  0, 128);
175         memset(data, 0, 128);
176         memset(tweak, 0, 16);
177         
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);
206         
207         for(i=0; i<128; ++i){
208                 key[i] = 0x10+i;
209                 data[i] = 0xFF-i;
210         }
211         for(i=0; i<16; ++i){
212                 tweak[i] = i;
213         }
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);
242         }
243
244
245 void testrun_stdtest_threefish(void){
246         testrun_stdtest_threefish256();
247         testrun_stdtest_threefish512();
248         testrun_stdtest_threefish1024();
249 }
250
251 void testrun_performance_threefish256(void){
252         uint64_t t;
253         char str[16];
254         uint8_t key[THREEFISH256_BLOCKSIZE_B];
255         uint8_t data[THREEFISH256_BLOCKSIZE_B];
256         uint8_t tweak[16];
257         threefish256_ctx_t ctx;
258         
259         cli_putstr_P(PSTR("\r\nThreefish-256 performance:"));
260         
261         calibrateTimer();
262         print_overhead();       
263         
264 //      memset(key,  0, THREEFISH256_BLOCKSIZE_B);
265 //      memset(tweak, 0, 16);
266         
267         startTimer(1);
268         threefish256_init(key, tweak, &ctx);
269         t = stopTimer();
270         cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
271         ultoa((unsigned long)t, str, 10);
272         cli_putstr(str);        
273         
274         startTimer(1);
275         threefish256_enc(data, &ctx);
276         t = stopTimer();
277         cli_putstr_P(PSTR("\r\n\tencrypt time: "));
278         ultoa((unsigned long)t, str, 10);
279         cli_putstr(str);        
280         
281         cli_putstr_P(PSTR("\r\n"));     
282 }
283
284 void testrun_performance_threefish512(void){
285         uint64_t t;
286         char str[16];
287         uint8_t key[THREEFISH512_BLOCKSIZE_B];
288         uint8_t data[THREEFISH512_BLOCKSIZE_B];
289         uint8_t tweak[16];
290         threefish512_ctx_t ctx;
291         
292         cli_putstr_P(PSTR("\r\nThreefish-512 performance:"));
293         
294         calibrateTimer();
295         print_overhead();       
296         
297 //      memset(key,  0, THREEFISH512_BLOCKSIZE_B);
298 //      memset(tweak, 0, 16);
299         
300         startTimer(1);
301         threefish512_init(key, tweak, &ctx);
302         t = stopTimer();
303         cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
304         ultoa((unsigned long)t, str, 10);
305         cli_putstr(str);        
306         
307         startTimer(1);
308         threefish512_enc(data, &ctx);
309         t = stopTimer();
310         cli_putstr_P(PSTR("\r\n\tencrypt time: "));
311         ultoa((unsigned long)t, str, 10);
312         cli_putstr(str);        
313         
314         cli_putstr_P(PSTR("\r\n"));     
315 }
316
317 void testrun_performance_threefish1024(void){
318         uint64_t t;
319         char str[16];
320         uint8_t key[THREEFISH1024_BLOCKSIZE_B];
321         uint8_t data[THREEFISH1024_BLOCKSIZE_B];
322         uint8_t tweak[16];
323         threefish1024_ctx_t ctx;
324         
325         cli_putstr_P(PSTR("\r\nThreefish-1024 performance:"));
326         
327         calibrateTimer();
328         print_overhead();       
329         
330 //      memset(key,  0, THREEFISH1024_BLOCKSIZE_B);
331 //      memset(tweak, 0, 16);
332         
333         startTimer(1);
334         threefish1024_init(key, tweak, &ctx);
335         t = stopTimer();
336         cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
337         ultoa((unsigned long)t, str, 10);
338         cli_putstr(str);        
339         
340         startTimer(1);
341         threefish1024_enc(data, &ctx);
342         t = stopTimer();
343         cli_putstr_P(PSTR("\r\n\tencrypt time: "));
344         ultoa((unsigned long)t, str, 10);
345         cli_putstr(str);        
346         
347         cli_putstr_P(PSTR("\r\n"));     
348 }
349
350 void testrun_performance_threefish(void){
351         testrun_performance_threefish256();
352         testrun_performance_threefish512();
353         testrun_performance_threefish1024();
354 }
355
356 void init_test(void){
357         threefish256_ctx_t ctx;
358         uint8_t key[32], tweak[16];
359         memset(key, 0,32);
360         memset(tweak, 0,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);
366 }
367
368
369 /*****************************************************************************
370  *  main                                                                                                                                         *
371  *****************************************************************************/
372
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";
378
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},
385         { NULL,            NULL, NULL}
386 };
387
388 int main (void){
389         DEBUG_INIT();
390         
391         cli_rx = uart_getc;
392         cli_tx = uart_putc;             
393         for(;;){
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);
398         }
399 }