]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-threefish-test.c
fixing E-Mail-Address & Copyright
[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) 2006-2015 Daniel Otte (bg@nerilex.org)
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
25 #include "main-test-common.h"
26
27 #include "threefish.h"
28 #include "performance_test.h"
29 #include "bcal-performance.h"
30 #include "bcal-nessie.h"
31 #include "bcal_threefish256.h"
32 #include "bcal_threefish512.h"
33 #include "bcal_threefish1024.h"
34
35 char *algo_name = "Threefish";
36
37 const bcdesc_t *const algolist[] PROGMEM = {
38         (bcdesc_t*)&threefish256_desc,
39         (bcdesc_t*)&threefish512_desc,
40         (bcdesc_t*)&threefish1024_desc,
41         NULL
42 };
43 /*****************************************************************************
44  *  additional validation-functions                                                                                      *
45  *****************************************************************************/
46
47 void threefish256_dump(threefish256_ctx_t *ctx){
48         uint8_t i;
49         cli_putstr_P(PSTR("\r\n=== ctx dump (256) === \r\n k: "));
50         for(i=0; i<5; ++i){
51                 cli_hexdump(&(ctx->k[i]), 8);
52                 cli_putc(' ');
53         }
54         cli_putstr_P(PSTR("\r\n t: "));
55         for(i=0; i<3; ++i){
56                 cli_hexdump(&(ctx->t[i]), 8);
57                 cli_putc(' ');
58         }
59 }
60
61 void testrun_nessie_threefish(void){
62         bcal_nessie_multiple(algolist);
63 }
64
65 void testrun_stdtest_threefish256(void){
66         uint8_t key[32], data[32];
67         uint8_t tweak[16];
68         uint8_t i;
69         threefish256_ctx_t ctx;
70         
71         cli_putstr_P(PSTR("\r\n\r\nTest vectors for block cipher Threefish (256 bits):"));
72         memset(key,  0, 32);
73         memset(data, 0, 32);
74         memset(tweak, 0, 16);
75         
76         cli_putstr_P(PSTR("\r\nkey:    "));
77         cli_hexdump_block(key, 32, 4, 16);
78         cli_putstr_P(PSTR("\r\ntweak:  "));
79         cli_hexdump_block(tweak, 16, 4, 16);
80         cli_putstr_P(PSTR("\r\nplain:  "));
81         cli_hexdump_block(data, 32, 4, 16);
82         threefish256_init(key, tweak, &ctx);
83         threefish256_enc(data, &ctx);
84         cli_putstr_P(PSTR("\r\ncipher: "));
85         cli_hexdump_block(data, 32, 4, 16);
86         cli_putstr_P(PSTR("\r\ndecipher: "));
87         threefish256_dec(data, &ctx);
88         cli_hexdump_block(data, 32, 4, 16);
89         
90         /* second test */
91         for(i=0; i<32; ++i){
92                 key[i] = 0x10+i;
93                 data[i] = 0xFF-i;
94         }
95         for(i=0; i<16; ++i){
96                 tweak[i] = i;
97         }
98         cli_putstr_P(PSTR("\r\nkey:    "));
99         cli_hexdump_block(key, 32, 4, 16);
100         cli_putstr_P(PSTR("\r\ntweak:  "));
101         cli_hexdump_block(tweak, 16, 4, 16);
102         cli_putstr_P(PSTR("\r\nplain:  "));
103         cli_hexdump_block(data, 32, 4, 16);
104         threefish256_init(key, tweak, &ctx);
105         threefish256_enc(data, &ctx);
106         cli_putstr_P(PSTR("\r\ncipher: "));
107         cli_hexdump_block(data, 32, 4, 16);
108         cli_putstr_P(PSTR("\r\ndecipher: "));
109         threefish256_dec(data, &ctx);
110         cli_hexdump_block(data, 32, 4, 16);
111 }
112
113 void testrun_stdtest_threefish512(void){
114         uint8_t key[64], data[64];
115         uint8_t tweak[16];
116         uint8_t i;
117         threefish512_ctx_t ctx;
118         
119         cli_putstr_P(PSTR("\r\n\r\nTest vectors for block cipher Threefish (512 bits) :"));
120         memset(key,  0, 64);
121         memset(data, 0, 64);
122         memset(tweak, 0, 16);
123         
124         cli_putstr_P(PSTR("\r\nkey:    "));
125         cli_hexdump_block(key, 32, 4, 16);
126         cli_putstr_P(PSTR("\r\n        "));
127         cli_hexdump_block(key+32, 32, 4, 16);
128         cli_putstr_P(PSTR("\r\ntweak:  "));
129         cli_hexdump_block(tweak, 16, 4, 16);
130         cli_putstr_P(PSTR("\r\nplain:  "));
131         cli_hexdump_block(data, 64, 4, 16);
132         threefish512_init(key, tweak, &ctx);
133         threefish512_enc(data, &ctx);
134         cli_putstr_P(PSTR("\r\ncipher: "));
135         cli_hexdump_block(data, 64, 4, 16);
136         threefish512_dec(data, &ctx);
137         cli_putstr_P(PSTR("\r\ndecipher: "));
138         cli_hexdump_block(data, 64, 4, 16);
139         
140         
141         for(i=0; i<64; ++i){
142                 key[i] = 0x10+i;
143                 data[i] = 0xFF-i;
144         }
145         for(i=0; i<16; ++i){
146                 tweak[i] = i;
147         }
148         cli_putstr_P(PSTR("\r\nkey:    "));
149         cli_hexdump_block(key, 32, 4, 16);
150         cli_putstr_P(PSTR("\r\n        "));
151         cli_hexdump_block(key+32, 32, 4, 16);
152         cli_putstr_P(PSTR("\r\ntweak:  "));
153         cli_hexdump_block(tweak, 16, 4, 16);
154         cli_putstr_P(PSTR("\r\nplain:  "));
155         cli_hexdump_block(data, 64, 4, 16);
156         threefish512_init(key, tweak, &ctx);
157         threefish512_enc(data, &ctx);
158         cli_putstr_P(PSTR("\r\ncipher: "));
159         cli_hexdump_block(data, 64, 4, 16);
160         threefish512_dec(data, &ctx);
161         cli_putstr_P(PSTR("\r\ndecipher: "));
162         cli_hexdump_block(data, 64, 4, 16);
163         
164 }
165
166 void testrun_stdtest_threefish1024(void){
167         uint8_t key[128], data[128];
168         uint8_t tweak[16];
169         uint8_t i;
170         threefish1024_ctx_t ctx;
171         
172         cli_putstr_P(PSTR("\r\n\r\nTest vectors for block cipher Threefish (1024 bits) :"));
173         memset(key,  0, 128);
174         memset(data, 0, 128);
175         memset(tweak, 0, 16);
176         
177         cli_putstr_P(PSTR("\r\nkey:    "));
178         cli_hexdump_block(key, 128, 4, 16);
179         cli_putstr_P(PSTR("\r\ntweak:  "));
180         cli_hexdump_block(tweak, 16, 4, 16);
181         cli_putstr_P(PSTR("\r\nplain:  "));
182         cli_hexdump_block(data, 128, 4, 16);
183         threefish1024_init(key, tweak, &ctx);
184         threefish1024_enc(data, &ctx);
185         cli_putstr_P(PSTR("\r\ncipher: "));
186         cli_hexdump_block(data, 128, 4, 16);
187         threefish1024_dec(data, &ctx);
188         cli_putstr_P(PSTR("\r\ndecipher: "));
189         cli_hexdump_block(data, 128, 4, 16);
190
191         for(i=0; i<128; ++i){
192                 key[i] = 0x10+i;
193                 data[i] = 0xFF-i;
194         }
195         for(i=0; i<16; ++i){
196                 tweak[i] = i;
197         }
198         cli_putstr_P(PSTR("\r\nkey:    "));
199         cli_hexdump_block(key, 128, 4, 16);
200         cli_putstr_P(PSTR("\r\ntweak:  "));
201         cli_hexdump_block(tweak, 16, 4, 16);
202         cli_putstr_P(PSTR("\r\nplain:  "));
203         cli_hexdump_block(data, 128, 4, 16);
204         threefish1024_init(key, tweak, &ctx);
205         threefish1024_enc(data, &ctx);
206         cli_putstr_P(PSTR("\r\ncipher: "));
207         cli_hexdump_block(data, 128, 4, 16);
208         threefish1024_dec(data, &ctx);
209         cli_putstr_P(PSTR("\r\ndecipher: "));
210         cli_hexdump_block(data, 128, 4, 16);
211 }
212
213
214 void testrun_stdtest_threefish(void){
215         testrun_stdtest_threefish256();
216         testrun_stdtest_threefish512();
217         testrun_stdtest_threefish1024();
218 }
219
220 void testrun_performance_threefish(void){
221         bcal_performance_multiple(algolist);
222 }
223
224 void init_test(void){
225         threefish256_ctx_t ctx;
226         uint8_t key[32], tweak[16];
227         memset(key, 0,32);
228         memset(tweak, 0,16);
229         threefish256_init(key, tweak, &ctx);
230         cli_putstr_P(PSTR("\r\n ctx: \r\n\tk:"));
231         cli_hexdump(ctx.k, 5*8);
232         cli_putstr_P(PSTR("\r\n\tt:"));
233         cli_hexdump(ctx.t, 3*8);
234 }
235
236
237 /*****************************************************************************
238  *  main                                                                                                                                         *
239  *****************************************************************************/
240
241 const char nessie_str[]      PROGMEM = "nessie";
242 const char test_str[]        PROGMEM = "test";
243 const char test256_str[]     PROGMEM = "test256";
244 const char test512_str[]     PROGMEM = "test512";
245 const char test1024_str[]    PROGMEM = "test1024";
246 const char inittest_str[]    PROGMEM = "inittest";
247 const char performance_str[] PROGMEM = "performance";
248 const char echo_str[]        PROGMEM = "echo";
249
250 const cmdlist_entry_t cmdlist[] PROGMEM = {
251         { nessie_str,      NULL, testrun_nessie_threefish},
252         { test_str,        NULL, testrun_stdtest_threefish},
253     { test256_str,     NULL, testrun_stdtest_threefish256},
254     { test512_str,     NULL, testrun_stdtest_threefish512},
255     { test1024_str,    NULL, testrun_stdtest_threefish1024},
256         { inittest_str,    NULL, init_test},
257         { performance_str, NULL, testrun_performance_threefish},
258         { echo_str,    (void*)1, (void_fpt)echo_ctrl},
259         { NULL,            NULL, NULL}
260 };
261
262 int main (void){
263     main_setup();
264
265     for(;;){
266         welcome_msg(algo_name);
267         cmd_interface(cmdlist);
268         }
269 }