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