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