]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-threefish-test.c
+threefis256 asm +ubi256 asm + skein256 asm
[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 void testrun_stdtest_threefish256(void){
44         uint8_t key[32], data[32];
45         uint8_t tweak[16];
46         uint8_t i;
47         threefish256_ctx_t ctx;
48         
49         cli_putstr_P(PSTR("\r\n\r\nTest vectors for block cipher Threefish (256 bits):"));
50         memset(key,  0, 32);
51         memset(data, 0, 32);
52         memset(tweak, 0, 16);
53         
54         cli_putstr_P(PSTR("\r\nkey:    "));
55         cli_hexdump(key, 32);
56         cli_putstr_P(PSTR("\r\ntweak:  "));
57         cli_hexdump(tweak, 16);
58         cli_putstr_P(PSTR("\r\nplain:  "));
59         cli_hexdump(data, 32);
60         threefish256_init(key, tweak, &ctx);
61         threefish256_enc(data, &ctx);
62         cli_putstr_P(PSTR("\r\ncipher: "));
63         cli_hexdump(data, 32);
64         /* 
65         cli_hexdump_rev(data, 8);
66         cli_putc(' ');
67         cli_hexdump_rev(data+8, 8);
68         cli_putc(' ');
69         cli_hexdump_rev(data+16, 8);
70         cli_putc(' ');
71         cli_hexdump_rev(data+24, 8);
72         cli_putc(' ');
73         */
74         /* second test */
75         for(i=0; i<32; ++i){
76                 key[i] = 0x10+i;
77                 data[i] = 0xFF-i;
78         }
79         for(i=0; i<16; ++i){
80                 tweak[i] = i;
81         }
82         cli_putstr_P(PSTR("\r\n\r\nkey:    "));
83         cli_hexdump(key, 32);
84         cli_putstr_P(PSTR("\r\ntweak:  "));
85         cli_hexdump(tweak, 16);
86         cli_putstr_P(PSTR("\r\nplain:  "));
87         cli_hexdump(data, 32);
88         threefish256_init(key, tweak, &ctx);
89         threefish256_enc(data, &ctx);
90         cli_putstr_P(PSTR("\r\ncipher: "));
91         cli_hexdump(data, 32);
92 }
93
94 void testrun_stdtest_threefish512(void){
95         uint8_t key[64], data[64];
96         uint8_t tweak[16];
97         uint8_t i;
98         threefish512_ctx_t ctx;
99         
100         cli_putstr_P(PSTR("\r\n\r\nTest vectors for block cipher Threefish (512 bits) :"));
101         memset(key,  0, 64);
102         memset(data, 0, 64);
103         memset(tweak, 0, 16);
104         
105         cli_putstr_P(PSTR("\r\nkey:    "));
106         cli_hexdump(key, 32);
107         cli_putstr_P(PSTR("\r\n        "));
108         cli_hexdump(key+32, 32);
109         cli_putstr_P(PSTR("\r\ntweak:  "));
110         cli_hexdump(tweak, 16);
111         cli_putstr_P(PSTR("\r\nplain:  "));
112         cli_hexdump(data, 32);
113         cli_putstr_P(PSTR("\r\n        "));
114         cli_hexdump(data+32, 32);
115         threefish512_init(key, tweak, &ctx);
116         threefish512_enc(data, &ctx);
117         cli_putstr_P(PSTR("\r\ncipher: "));
118         cli_hexdump(data, 32);
119         cli_putstr_P(PSTR("\r\n        "));
120         cli_hexdump(data+32, 32);
121         
122         for(i=0; i<64; ++i){
123                 key[i] = 0x10+i;
124                 data[i] = 0xFF-i;
125         }
126         for(i=0; i<16; ++i){
127                 tweak[i] = i;
128         }
129         cli_putstr_P(PSTR("\r\n\r\nkey:    "));
130         cli_hexdump(key, 32);
131         cli_putstr_P(PSTR("\r\n        "));
132         cli_hexdump(key+32, 32);
133         cli_putstr_P(PSTR("\r\ntweak:  "));
134         cli_hexdump(tweak, 16);
135         cli_putstr_P(PSTR("\r\nplain:  "));
136         cli_hexdump(data, 32);
137         cli_putstr_P(PSTR("\r\n        "));
138         cli_hexdump(data+32, 32);
139         threefish512_init(key, tweak, &ctx);
140         threefish512_enc(data, &ctx);
141         cli_putstr_P(PSTR("\r\ncipher: "));
142         cli_hexdump(data, 32);
143         cli_putstr_P(PSTR("\r\n        "));
144         cli_hexdump(data+32, 32);
145 }
146
147 void testrun_stdtest_threefish1024(void){
148         uint8_t key[128], data[128];
149         uint8_t tweak[16];
150         uint8_t i;
151         threefish1024_ctx_t ctx;
152         
153         cli_putstr_P(PSTR("\r\n\r\nTest vectors for block cipher Threefish (1024 bits) :"));
154         memset(key,  0, 128);
155         memset(data, 0, 128);
156         memset(tweak, 0, 16);
157         
158         cli_putstr_P(PSTR("\r\nkey:    "));
159         cli_hexdump(key, 32);
160         cli_putstr_P(PSTR("\r\n        "));
161         cli_hexdump(key+32, 32);
162         cli_putstr_P(PSTR("\r\n        "));
163         cli_hexdump(key+64, 32);
164         cli_putstr_P(PSTR("\r\n        "));
165         cli_hexdump(key+96, 32);
166         cli_putstr_P(PSTR("\r\ntweak:  "));
167         cli_hexdump(tweak, 16);
168         cli_putstr_P(PSTR("\r\nplain:  "));
169         cli_hexdump(data, 32);
170         cli_putstr_P(PSTR("\r\n        "));
171         cli_hexdump(data+32, 32);
172         cli_putstr_P(PSTR("\r\n        "));
173         cli_hexdump(data+64, 32);
174         cli_putstr_P(PSTR("\r\n        "));
175         cli_hexdump(data+96, 32);
176         threefish1024_init(key, tweak, &ctx);
177         threefish1024_enc(data, &ctx);
178         cli_putstr_P(PSTR("\r\ncipher: "));
179         cli_hexdump(data, 32);
180         cli_putstr_P(PSTR("\r\n        "));
181         cli_hexdump(data+32, 32);
182         cli_putstr_P(PSTR("\r\n        "));
183         cli_hexdump(data+64, 32);
184         cli_putstr_P(PSTR("\r\n        "));
185         cli_hexdump(data+96, 32);
186         
187         for(i=0; i<128; ++i){
188                 key[i] = 0x10+i;
189                 data[i] = 0xFF-i;
190         }
191         for(i=0; i<16; ++i){
192                 tweak[i] = i;
193         }
194         cli_putstr_P(PSTR("\r\n\r\nkey:    "));
195         cli_hexdump(key, 32);
196         cli_putstr_P(PSTR("\r\n        "));
197         cli_hexdump(key+32, 32);
198         cli_putstr_P(PSTR("\r\n        "));
199         cli_hexdump(key+64, 32);
200         cli_putstr_P(PSTR("\r\n        "));
201         cli_hexdump(key+96, 32);
202         cli_putstr_P(PSTR("\r\ntweak:  "));
203         cli_hexdump(tweak, 16);
204         cli_putstr_P(PSTR("\r\nplain:  "));
205         cli_hexdump(data, 32);
206         cli_putstr_P(PSTR("\r\n        "));
207         cli_hexdump(data+32, 32);
208         cli_putstr_P(PSTR("\r\n        "));
209         cli_hexdump(data+64, 32);
210         cli_putstr_P(PSTR("\r\n        "));
211         cli_hexdump(data+96, 32);
212         threefish1024_init(key, tweak, &ctx);
213         threefish1024_enc(data, &ctx);
214         cli_putstr_P(PSTR("\r\ncipher: "));
215         cli_hexdump(data, 32);
216         cli_putstr_P(PSTR("\r\n        "));
217         cli_hexdump(data+32, 32);
218         cli_putstr_P(PSTR("\r\n        "));
219         cli_hexdump(data+64, 32);
220         cli_putstr_P(PSTR("\r\n        "));
221         cli_hexdump(data+96, 32);
222         }
223
224
225 void testrun_stdtest_threefish(void){
226         testrun_stdtest_threefish256();
227         testrun_stdtest_threefish512();
228         testrun_stdtest_threefish1024();
229 }
230
231 void testrun_performance_threefish256(void){
232         uint64_t t;
233         char str[16];
234         uint8_t key[THREEFISH256_BLOCKSIZE_B];
235         uint8_t data[THREEFISH256_BLOCKSIZE_B];
236         uint8_t tweak[16];
237         threefish256_ctx_t ctx;
238         
239         cli_putstr_P(PSTR("\r\nThreefish-256 performance:"));
240         
241         calibrateTimer();
242         print_overhead();       
243         
244 //      memset(key,  0, THREEFISH256_BLOCKSIZE_B);
245 //      memset(tweak, 0, 16);
246         
247         startTimer(1);
248         threefish256_init(key, tweak, &ctx);
249         t = stopTimer();
250         cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
251         ultoa((unsigned long)t, str, 10);
252         cli_putstr(str);        
253         
254         startTimer(1);
255         threefish256_enc(data, &ctx);
256         t = stopTimer();
257         cli_putstr_P(PSTR("\r\n\tencrypt time: "));
258         ultoa((unsigned long)t, str, 10);
259         cli_putstr(str);        
260         
261         cli_putstr_P(PSTR("\r\n"));     
262 }
263
264 void testrun_performance_threefish512(void){
265         uint64_t t;
266         char str[16];
267         uint8_t key[THREEFISH512_BLOCKSIZE_B];
268         uint8_t data[THREEFISH512_BLOCKSIZE_B];
269         uint8_t tweak[16];
270         threefish512_ctx_t ctx;
271         
272         cli_putstr_P(PSTR("\r\nThreefish-512 performance:"));
273         
274         calibrateTimer();
275         print_overhead();       
276         
277 //      memset(key,  0, THREEFISH512_BLOCKSIZE_B);
278 //      memset(tweak, 0, 16);
279         
280         startTimer(1);
281         threefish512_init(key, tweak, &ctx);
282         t = stopTimer();
283         cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
284         ultoa((unsigned long)t, str, 10);
285         cli_putstr(str);        
286         
287         startTimer(1);
288         threefish512_enc(data, &ctx);
289         t = stopTimer();
290         cli_putstr_P(PSTR("\r\n\tencrypt time: "));
291         ultoa((unsigned long)t, str, 10);
292         cli_putstr(str);        
293         
294         cli_putstr_P(PSTR("\r\n"));     
295 }
296
297 void testrun_performance_threefish1024(void){
298         uint64_t t;
299         char str[16];
300         uint8_t key[THREEFISH1024_BLOCKSIZE_B];
301         uint8_t data[THREEFISH1024_BLOCKSIZE_B];
302         uint8_t tweak[16];
303         threefish1024_ctx_t ctx;
304         
305         cli_putstr_P(PSTR("\r\nThreefish-1024 performance:"));
306         
307         calibrateTimer();
308         print_overhead();       
309         
310 //      memset(key,  0, THREEFISH1024_BLOCKSIZE_B);
311 //      memset(tweak, 0, 16);
312         
313         startTimer(1);
314         threefish1024_init(key, tweak, &ctx);
315         t = stopTimer();
316         cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
317         ultoa((unsigned long)t, str, 10);
318         cli_putstr(str);        
319         
320         startTimer(1);
321         threefish1024_enc(data, &ctx);
322         t = stopTimer();
323         cli_putstr_P(PSTR("\r\n\tencrypt time: "));
324         ultoa((unsigned long)t, str, 10);
325         cli_putstr(str);        
326         
327         cli_putstr_P(PSTR("\r\n"));     
328 }
329
330 void testrun_performance_threefish(void){
331         testrun_performance_threefish256();
332         testrun_performance_threefish512();
333         testrun_performance_threefish1024();
334 }
335
336 void init_test(void){
337         threefish256_ctx_t ctx;
338         uint8_t key[32], tweak[16];
339         memset(key, 0,32);
340         memset(tweak, 0,16);
341         threefish256_init(key, tweak, &ctx);
342         cli_putstr_P(PSTR("\r\n ctx: \r\n\tk:"));
343         cli_hexdump(ctx.k, 5*8);
344         cli_putstr_P(PSTR("\r\n\tt:"));
345         cli_hexdump(ctx.t, 3*8);
346 }
347
348
349 /*****************************************************************************
350  *  main                                                                                                                                         *
351  *****************************************************************************/
352
353 const char nessie_str[]      PROGMEM = "nessie";
354 const char test_str[]        PROGMEM = "test";
355 const char inittest_str[]    PROGMEM = "inittest";
356 const char performance_str[] PROGMEM = "performance";
357 const char echo_str[]        PROGMEM = "echo";
358
359 cmdlist_entry_t cmdlist[] PROGMEM = {
360 //      { nessie_str,      NULL, testrun_nessie_noekeon},
361         { test_str,        NULL, testrun_stdtest_threefish},
362         { inittest_str,    NULL, init_test},
363         { performance_str, NULL, testrun_performance_threefish},
364         { echo_str,    (void*)1, (void_fpt)echo_ctrl},
365         { NULL,            NULL, NULL}
366 };
367
368 int main (void){
369         DEBUG_INIT();
370         
371         cli_rx = uart_getc;
372         cli_tx = uart_putc;             
373         for(;;){
374                 cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
375                 cli_putstr(algo_name);
376                 cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
377                 cmd_interface(cmdlist);
378         }
379 }