]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-threefish-test.c
31748e45d78e47993e471c21ff8b517154cf04f1
[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
26 #include "uart_i.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 #include "bcal-performance.h"
34 #include "bcal_threefish256.h"
35 #include "bcal_threefish512.h"
36 #include "bcal_threefish1024.h"
37
38 #include <stdint.h>
39 #include <string.h>
40 #include <stdlib.h>
41
42 char* algo_name = "Threefish";
43
44 const bcdesc_t* algolist[] PROGMEM = {
45         (bcdesc_t*)&threefish256_desc,
46         (bcdesc_t*)&threefish512_desc,
47         (bcdesc_t*)&threefish1024_desc,
48         NULL
49 };
50 /*****************************************************************************
51  *  additional validation-functions                                                                                      *
52  *****************************************************************************/
53
54 void threefish256_dump(threefish256_ctx_t* ctx){
55         uint8_t i;
56         cli_putstr_P(PSTR("\r\n=== ctx dump (256) === \r\n k: "));
57         for(i=0; i<5; ++i){
58                 cli_hexdump(&(ctx->k[i]), 8);
59                 cli_putc(' ');
60         }
61         cli_putstr_P(PSTR("\r\n t: "));
62         for(i=0; i<3; ++i){
63                 cli_hexdump(&(ctx->t[i]), 8);
64                 cli_putc(' ');
65         }
66 }
67
68 void threefish256_dummy_init(const uint8_t* key, uint16_t keysize_b, void* ctx){
69         threefish256_init(key, NULL, ctx);
70 }
71
72 void testrun_nessie_threefish256(void){
73         nessie_bc_ctx.keysize_b = 256;
74         nessie_bc_ctx.blocksize_B = 32;
75         nessie_bc_ctx.ctx_size_B = sizeof(threefish256_ctx_t);
76         nessie_bc_ctx.name = "Threefish256";
77         nessie_bc_ctx.cipher_genctx = threefish256_dummy_init;
78         nessie_bc_ctx.cipher_enc = (nessie_bc_enc_fpt)threefish256_enc;
79         nessie_bc_ctx.cipher_dec = (nessie_bc_dec_fpt)threefish256_dec;
80         nessie_bc_ctx.cipher_free = NULL;
81         
82         nessie_bc_run();
83 }
84
85 void threefish512_dummy_init(const uint8_t* key, uint16_t keysize_b, void* ctx){
86         threefish512_init(key, NULL, ctx);
87 }
88
89 void testrun_nessie_threefish512(void){
90         nessie_bc_ctx.keysize_b = 512;
91         nessie_bc_ctx.blocksize_B = 64;
92         nessie_bc_ctx.ctx_size_B = sizeof(threefish512_ctx_t);
93         nessie_bc_ctx.name = "Threefish512";
94         nessie_bc_ctx.cipher_genctx = threefish512_dummy_init;
95         nessie_bc_ctx.cipher_enc = (nessie_bc_enc_fpt)threefish512_enc;
96         nessie_bc_ctx.cipher_dec = (nessie_bc_dec_fpt)threefish512_dec;
97         nessie_bc_ctx.cipher_free = NULL;
98         
99         nessie_bc_run();
100 }
101
102 void threefish1024_dummy_init(const uint8_t* key, uint16_t keysize_b, void* ctx){
103         threefish1024_init(key, NULL, ctx);
104 }
105
106 void testrun_nessie_threefish1024(void){
107         nessie_bc_ctx.keysize_b = 1024;
108         nessie_bc_ctx.blocksize_B = 128;
109         nessie_bc_ctx.ctx_size_B = sizeof(threefish1024_ctx_t);
110         nessie_bc_ctx.name = "Threefish1024";
111         nessie_bc_ctx.cipher_genctx = threefish1024_dummy_init;
112         nessie_bc_ctx.cipher_enc = (nessie_bc_enc_fpt)threefish1024_enc;
113         nessie_bc_ctx.cipher_dec = (nessie_bc_dec_fpt)threefish1024_dec;
114         nessie_bc_ctx.cipher_free = NULL;
115         
116         nessie_bc_run();
117 }
118
119 void testrun_nessie_threefish(void){
120         testrun_nessie_threefish256();
121         testrun_nessie_threefish512();
122         testrun_nessie_threefish1024();
123 }
124
125 void testrun_stdtest_threefish256(void){
126         uint8_t key[32], data[32];
127         uint8_t tweak[16];
128         uint8_t i;
129         threefish256_ctx_t ctx;
130         
131         cli_putstr_P(PSTR("\r\n\r\nTest vectors for block cipher Threefish (256 bits):"));
132         memset(key,  0, 32);
133         memset(data, 0, 32);
134         memset(tweak, 0, 16);
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         cli_putstr_P(PSTR("\r\ndecipher: "));
147         threefish256_dec(data, &ctx);
148         cli_hexdump_block(data, 32, 4, 16);
149         
150         /* second test */
151         for(i=0; i<32; ++i){
152                 key[i] = 0x10+i;
153                 data[i] = 0xFF-i;
154         }
155         for(i=0; i<16; ++i){
156                 tweak[i] = i;
157         }
158         cli_putstr_P(PSTR("\r\nkey:    "));
159         cli_hexdump_block(key, 32, 4, 16);
160         cli_putstr_P(PSTR("\r\ntweak:  "));
161         cli_hexdump_block(tweak, 16, 4, 16);
162         cli_putstr_P(PSTR("\r\nplain:  "));
163         cli_hexdump_block(data, 32, 4, 16);
164         threefish256_init(key, tweak, &ctx);
165         threefish256_enc(data, &ctx);
166         cli_putstr_P(PSTR("\r\ncipher: "));
167         cli_hexdump_block(data, 32, 4, 16);
168         cli_putstr_P(PSTR("\r\ndecipher: "));
169         threefish256_dec(data, &ctx);
170         cli_hexdump_block(data, 32, 4, 16);
171 }
172
173 void testrun_stdtest_threefish512(void){
174         uint8_t key[64], data[64];
175         uint8_t tweak[16];
176         uint8_t i;
177         threefish512_ctx_t ctx;
178         
179         cli_putstr_P(PSTR("\r\n\r\nTest vectors for block cipher Threefish (512 bits) :"));
180         memset(key,  0, 64);
181         memset(data, 0, 64);
182         memset(tweak, 0, 16);
183         
184         cli_putstr_P(PSTR("\r\nkey:    "));
185         cli_hexdump_block(key, 32, 4, 16);
186         cli_putstr_P(PSTR("\r\n        "));
187         cli_hexdump_block(key+32, 32, 4, 16);
188         cli_putstr_P(PSTR("\r\ntweak:  "));
189         cli_hexdump_block(tweak, 16, 4, 16);
190         cli_putstr_P(PSTR("\r\nplain:  "));
191         cli_hexdump_block(data, 64, 4, 16);
192         threefish512_init(key, tweak, &ctx);
193         threefish512_enc(data, &ctx);
194         cli_putstr_P(PSTR("\r\ncipher: "));
195         cli_hexdump_block(data, 64, 4, 16);
196         threefish512_dec(data, &ctx);
197         cli_putstr_P(PSTR("\r\ndecipher: "));
198         cli_hexdump_block(data, 64, 4, 16);
199         
200         
201         for(i=0; i<64; ++i){
202                 key[i] = 0x10+i;
203                 data[i] = 0xFF-i;
204         }
205         for(i=0; i<16; ++i){
206                 tweak[i] = i;
207         }
208         cli_putstr_P(PSTR("\r\nkey:    "));
209         cli_hexdump_block(key, 32, 4, 16);
210         cli_putstr_P(PSTR("\r\n        "));
211         cli_hexdump_block(key+32, 32, 4, 16);
212         cli_putstr_P(PSTR("\r\ntweak:  "));
213         cli_hexdump_block(tweak, 16, 4, 16);
214         cli_putstr_P(PSTR("\r\nplain:  "));
215         cli_hexdump_block(data, 64, 4, 16);
216         threefish512_init(key, tweak, &ctx);
217         threefish512_enc(data, &ctx);
218         cli_putstr_P(PSTR("\r\ncipher: "));
219         cli_hexdump_block(data, 64, 4, 16);
220         threefish512_dec(data, &ctx);
221         cli_putstr_P(PSTR("\r\ndecipher: "));
222         cli_hexdump_block(data, 64, 4, 16);
223         
224 }
225
226 void testrun_stdtest_threefish1024(void){
227         uint8_t key[128], data[128];
228         uint8_t tweak[16];
229         uint8_t i;
230         threefish1024_ctx_t ctx;
231         
232         cli_putstr_P(PSTR("\r\n\r\nTest vectors for block cipher Threefish (1024 bits) :"));
233         memset(key,  0, 128);
234         memset(data, 0, 128);
235         memset(tweak, 0, 16);
236         
237         cli_putstr_P(PSTR("\r\nkey:    "));
238         cli_hexdump_block(key, 128, 4, 16);
239         cli_putstr_P(PSTR("\r\ntweak:  "));
240         cli_hexdump_block(tweak, 16, 4, 16);
241         cli_putstr_P(PSTR("\r\nplain:  "));
242         cli_hexdump_block(data, 128, 4, 16);
243         threefish1024_init(key, tweak, &ctx);
244         threefish1024_enc(data, &ctx);
245         cli_putstr_P(PSTR("\r\ncipher: "));
246         cli_hexdump_block(data, 128, 4, 16);
247         threefish1024_dec(data, &ctx);
248         cli_putstr_P(PSTR("\r\ndecipher: "));
249         cli_hexdump_block(data, 128, 4, 16);
250
251         for(i=0; i<128; ++i){
252                 key[i] = 0x10+i;
253                 data[i] = 0xFF-i;
254         }
255         for(i=0; i<16; ++i){
256                 tweak[i] = i;
257         }
258         cli_putstr_P(PSTR("\r\nkey:    "));
259         cli_hexdump_block(key, 128, 4, 16);
260         cli_putstr_P(PSTR("\r\ntweak:  "));
261         cli_hexdump_block(tweak, 16, 4, 16);
262         cli_putstr_P(PSTR("\r\nplain:  "));
263         cli_hexdump_block(data, 128, 4, 16);
264         threefish1024_init(key, tweak, &ctx);
265         threefish1024_enc(data, &ctx);
266         cli_putstr_P(PSTR("\r\ncipher: "));
267         cli_hexdump_block(data, 128, 4, 16);
268         threefish1024_dec(data, &ctx);
269         cli_putstr_P(PSTR("\r\ndecipher: "));
270         cli_hexdump_block(data, 128, 4, 16);
271 }
272
273
274 void testrun_stdtest_threefish(void){
275         testrun_stdtest_threefish256();
276         testrun_stdtest_threefish512();
277         testrun_stdtest_threefish1024();
278 }
279
280 void testrun_performance_threefish(void){
281         bcal_performance_multiple(algolist);
282 }
283
284 void init_test(void){
285         threefish256_ctx_t ctx;
286         uint8_t key[32], tweak[16];
287         memset(key, 0,32);
288         memset(tweak, 0,16);
289         threefish256_init(key, tweak, &ctx);
290         cli_putstr_P(PSTR("\r\n ctx: \r\n\tk:"));
291         cli_hexdump(ctx.k, 5*8);
292         cli_putstr_P(PSTR("\r\n\tt:"));
293         cli_hexdump(ctx.t, 3*8);
294 }
295
296
297 /*****************************************************************************
298  *  main                                                                                                                                         *
299  *****************************************************************************/
300
301 const char nessie_str[]      PROGMEM = "nessie";
302 const char test_str[]        PROGMEM = "test";
303 const char test256_str[]     PROGMEM = "test256";
304 const char test512_str[]     PROGMEM = "test512";
305 const char test1024_str[]    PROGMEM = "test1024";
306 const char inittest_str[]    PROGMEM = "inittest";
307 const char performance_str[] PROGMEM = "performance";
308 const char echo_str[]        PROGMEM = "echo";
309
310 cmdlist_entry_t cmdlist[] PROGMEM = {
311         { nessie_str,      NULL, testrun_nessie_threefish},
312         { test_str,        NULL, testrun_stdtest_threefish},
313     { test256_str,     NULL, testrun_stdtest_threefish256},
314     { test512_str,     NULL, testrun_stdtest_threefish512},
315     { test1024_str,    NULL, testrun_stdtest_threefish1024},
316         { inittest_str,    NULL, init_test},
317         { performance_str, NULL, testrun_performance_threefish},
318         { echo_str,    (void*)1, (void_fpt)echo_ctrl},
319         { NULL,            NULL, NULL}
320 };
321
322 int main (void){
323         DEBUG_INIT();
324         
325         cli_rx = (cli_rx_fpt)uart0_getc;
326         cli_tx = (cli_tx_fpt)uart0_putc;                
327         for(;;){
328                 cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
329                 cli_putstr(algo_name);
330                 cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
331                 cmd_interface(cmdlist);
332         }
333 }