]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-threefish-test.c
a lot of fixes
[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 "cli.h"
31 #include "performance_test.h"
32 #include "bcal-performance.h"
33 #include "bcal-nessie.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* const 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 testrun_nessie_threefish(void){
69         bcal_nessie_multiple(algolist);
70 }
71
72 void testrun_stdtest_threefish256(void){
73         uint8_t key[32], data[32];
74         uint8_t tweak[16];
75         uint8_t i;
76         threefish256_ctx_t ctx;
77         
78         cli_putstr_P(PSTR("\r\n\r\nTest vectors for block cipher Threefish (256 bits):"));
79         memset(key,  0, 32);
80         memset(data, 0, 32);
81         memset(tweak, 0, 16);
82         
83         cli_putstr_P(PSTR("\r\nkey:    "));
84         cli_hexdump_block(key, 32, 4, 16);
85         cli_putstr_P(PSTR("\r\ntweak:  "));
86         cli_hexdump_block(tweak, 16, 4, 16);
87         cli_putstr_P(PSTR("\r\nplain:  "));
88         cli_hexdump_block(data, 32, 4, 16);
89         threefish256_init(key, tweak, &ctx);
90         threefish256_enc(data, &ctx);
91         cli_putstr_P(PSTR("\r\ncipher: "));
92         cli_hexdump_block(data, 32, 4, 16);
93         cli_putstr_P(PSTR("\r\ndecipher: "));
94         threefish256_dec(data, &ctx);
95         cli_hexdump_block(data, 32, 4, 16);
96         
97         /* second test */
98         for(i=0; i<32; ++i){
99                 key[i] = 0x10+i;
100                 data[i] = 0xFF-i;
101         }
102         for(i=0; i<16; ++i){
103                 tweak[i] = i;
104         }
105         cli_putstr_P(PSTR("\r\nkey:    "));
106         cli_hexdump_block(key, 32, 4, 16);
107         cli_putstr_P(PSTR("\r\ntweak:  "));
108         cli_hexdump_block(tweak, 16, 4, 16);
109         cli_putstr_P(PSTR("\r\nplain:  "));
110         cli_hexdump_block(data, 32, 4, 16);
111         threefish256_init(key, tweak, &ctx);
112         threefish256_enc(data, &ctx);
113         cli_putstr_P(PSTR("\r\ncipher: "));
114         cli_hexdump_block(data, 32, 4, 16);
115         cli_putstr_P(PSTR("\r\ndecipher: "));
116         threefish256_dec(data, &ctx);
117         cli_hexdump_block(data, 32, 4, 16);
118 }
119
120 void testrun_stdtest_threefish512(void){
121         uint8_t key[64], data[64];
122         uint8_t tweak[16];
123         uint8_t i;
124         threefish512_ctx_t ctx;
125         
126         cli_putstr_P(PSTR("\r\n\r\nTest vectors for block cipher Threefish (512 bits) :"));
127         memset(key,  0, 64);
128         memset(data, 0, 64);
129         memset(tweak, 0, 16);
130         
131         cli_putstr_P(PSTR("\r\nkey:    "));
132         cli_hexdump_block(key, 32, 4, 16);
133         cli_putstr_P(PSTR("\r\n        "));
134         cli_hexdump_block(key+32, 32, 4, 16);
135         cli_putstr_P(PSTR("\r\ntweak:  "));
136         cli_hexdump_block(tweak, 16, 4, 16);
137         cli_putstr_P(PSTR("\r\nplain:  "));
138         cli_hexdump_block(data, 64, 4, 16);
139         threefish512_init(key, tweak, &ctx);
140         threefish512_enc(data, &ctx);
141         cli_putstr_P(PSTR("\r\ncipher: "));
142         cli_hexdump_block(data, 64, 4, 16);
143         threefish512_dec(data, &ctx);
144         cli_putstr_P(PSTR("\r\ndecipher: "));
145         cli_hexdump_block(data, 64, 4, 16);
146         
147         
148         for(i=0; i<64; ++i){
149                 key[i] = 0x10+i;
150                 data[i] = 0xFF-i;
151         }
152         for(i=0; i<16; ++i){
153                 tweak[i] = i;
154         }
155         cli_putstr_P(PSTR("\r\nkey:    "));
156         cli_hexdump_block(key, 32, 4, 16);
157         cli_putstr_P(PSTR("\r\n        "));
158         cli_hexdump_block(key+32, 32, 4, 16);
159         cli_putstr_P(PSTR("\r\ntweak:  "));
160         cli_hexdump_block(tweak, 16, 4, 16);
161         cli_putstr_P(PSTR("\r\nplain:  "));
162         cli_hexdump_block(data, 64, 4, 16);
163         threefish512_init(key, tweak, &ctx);
164         threefish512_enc(data, &ctx);
165         cli_putstr_P(PSTR("\r\ncipher: "));
166         cli_hexdump_block(data, 64, 4, 16);
167         threefish512_dec(data, &ctx);
168         cli_putstr_P(PSTR("\r\ndecipher: "));
169         cli_hexdump_block(data, 64, 4, 16);
170         
171 }
172
173 void testrun_stdtest_threefish1024(void){
174         uint8_t key[128], data[128];
175         uint8_t tweak[16];
176         uint8_t i;
177         threefish1024_ctx_t ctx;
178         
179         cli_putstr_P(PSTR("\r\n\r\nTest vectors for block cipher Threefish (1024 bits) :"));
180         memset(key,  0, 128);
181         memset(data, 0, 128);
182         memset(tweak, 0, 16);
183         
184         cli_putstr_P(PSTR("\r\nkey:    "));
185         cli_hexdump_block(key, 128, 4, 16);
186         cli_putstr_P(PSTR("\r\ntweak:  "));
187         cli_hexdump_block(tweak, 16, 4, 16);
188         cli_putstr_P(PSTR("\r\nplain:  "));
189         cli_hexdump_block(data, 128, 4, 16);
190         threefish1024_init(key, tweak, &ctx);
191         threefish1024_enc(data, &ctx);
192         cli_putstr_P(PSTR("\r\ncipher: "));
193         cli_hexdump_block(data, 128, 4, 16);
194         threefish1024_dec(data, &ctx);
195         cli_putstr_P(PSTR("\r\ndecipher: "));
196         cli_hexdump_block(data, 128, 4, 16);
197
198         for(i=0; i<128; ++i){
199                 key[i] = 0x10+i;
200                 data[i] = 0xFF-i;
201         }
202         for(i=0; i<16; ++i){
203                 tweak[i] = i;
204         }
205         cli_putstr_P(PSTR("\r\nkey:    "));
206         cli_hexdump_block(key, 128, 4, 16);
207         cli_putstr_P(PSTR("\r\ntweak:  "));
208         cli_hexdump_block(tweak, 16, 4, 16);
209         cli_putstr_P(PSTR("\r\nplain:  "));
210         cli_hexdump_block(data, 128, 4, 16);
211         threefish1024_init(key, tweak, &ctx);
212         threefish1024_enc(data, &ctx);
213         cli_putstr_P(PSTR("\r\ncipher: "));
214         cli_hexdump_block(data, 128, 4, 16);
215         threefish1024_dec(data, &ctx);
216         cli_putstr_P(PSTR("\r\ndecipher: "));
217         cli_hexdump_block(data, 128, 4, 16);
218 }
219
220
221 void testrun_stdtest_threefish(void){
222         testrun_stdtest_threefish256();
223         testrun_stdtest_threefish512();
224         testrun_stdtest_threefish1024();
225 }
226
227 void testrun_performance_threefish(void){
228         bcal_performance_multiple(algolist);
229 }
230
231 void init_test(void){
232         threefish256_ctx_t ctx;
233         uint8_t key[32], tweak[16];
234         memset(key, 0,32);
235         memset(tweak, 0,16);
236         threefish256_init(key, tweak, &ctx);
237         cli_putstr_P(PSTR("\r\n ctx: \r\n\tk:"));
238         cli_hexdump(ctx.k, 5*8);
239         cli_putstr_P(PSTR("\r\n\tt:"));
240         cli_hexdump(ctx.t, 3*8);
241 }
242
243
244 /*****************************************************************************
245  *  main                                                                                                                                         *
246  *****************************************************************************/
247
248 const char nessie_str[]      PROGMEM = "nessie";
249 const char test_str[]        PROGMEM = "test";
250 const char test256_str[]     PROGMEM = "test256";
251 const char test512_str[]     PROGMEM = "test512";
252 const char test1024_str[]    PROGMEM = "test1024";
253 const char inittest_str[]    PROGMEM = "inittest";
254 const char performance_str[] PROGMEM = "performance";
255 const char echo_str[]        PROGMEM = "echo";
256
257 const cmdlist_entry_t cmdlist[] PROGMEM = {
258         { nessie_str,      NULL, testrun_nessie_threefish},
259         { test_str,        NULL, testrun_stdtest_threefish},
260     { test256_str,     NULL, testrun_stdtest_threefish256},
261     { test512_str,     NULL, testrun_stdtest_threefish512},
262     { test1024_str,    NULL, testrun_stdtest_threefish1024},
263         { inittest_str,    NULL, init_test},
264         { performance_str, NULL, testrun_performance_threefish},
265         { echo_str,    (void*)1, (void_fpt)echo_ctrl},
266         { NULL,            NULL, NULL}
267 };
268
269 int main (void){
270         DEBUG_INIT();
271         
272         cli_rx = (cli_rx_fpt)uart0_getc;
273         cli_tx = (cli_tx_fpt)uart0_putc;                
274         for(;;){
275                 cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
276                 cli_putstr(algo_name);
277                 cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
278                 cmd_interface(cmdlist);
279         }
280 }