1 /* main-threefish-test.c */
3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
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.
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.
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/>.
29 #include "threefish.h"
30 #include "nessie_bc_test.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"
42 char* algo_name = "Threefish";
44 const bcdesc_t* algolist[] PROGMEM = {
45 (bcdesc_t*)&threefish256_desc,
46 (bcdesc_t*)&threefish512_desc,
47 (bcdesc_t*)&threefish1024_desc,
50 /*****************************************************************************
51 * additional validation-functions *
52 *****************************************************************************/
54 void threefish256_dump(threefish256_ctx_t* ctx){
56 cli_putstr_P(PSTR("\r\n=== ctx dump (256) === \r\n k: "));
58 cli_hexdump(&(ctx->k[i]), 8);
61 cli_putstr_P(PSTR("\r\n t: "));
63 cli_hexdump(&(ctx->t[i]), 8);
68 void threefish256_dummy_init(const uint8_t* key, uint16_t keysize_b, void* ctx){
69 threefish256_init(key, NULL, ctx);
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;
85 void threefish512_dummy_init(const uint8_t* key, uint16_t keysize_b, void* ctx){
86 threefish512_init(key, NULL, ctx);
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;
102 void threefish1024_dummy_init(const uint8_t* key, uint16_t keysize_b, void* ctx){
103 threefish1024_init(key, NULL, ctx);
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;
119 void testrun_nessie_threefish(void){
120 testrun_nessie_threefish256();
121 testrun_nessie_threefish512();
122 testrun_nessie_threefish1024();
125 void testrun_stdtest_threefish256(void){
126 uint8_t key[32], data[32];
129 threefish256_ctx_t ctx;
131 cli_putstr_P(PSTR("\r\n\r\nTest vectors for block cipher Threefish (256 bits):"));
134 memset(tweak, 0, 16);
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);
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);
173 void testrun_stdtest_threefish512(void){
174 uint8_t key[64], data[64];
177 threefish512_ctx_t ctx;
179 cli_putstr_P(PSTR("\r\n\r\nTest vectors for block cipher Threefish (512 bits) :"));
182 memset(tweak, 0, 16);
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);
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);
226 void testrun_stdtest_threefish1024(void){
227 uint8_t key[128], data[128];
230 threefish1024_ctx_t ctx;
232 cli_putstr_P(PSTR("\r\n\r\nTest vectors for block cipher Threefish (1024 bits) :"));
234 memset(data, 0, 128);
235 memset(tweak, 0, 16);
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);
251 for(i=0; i<128; ++i){
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);
274 void testrun_stdtest_threefish(void){
275 testrun_stdtest_threefish256();
276 testrun_stdtest_threefish512();
277 testrun_stdtest_threefish1024();
280 void testrun_performance_threefish(void){
281 bcal_performance_multiple(algolist);
284 void init_test(void){
285 threefish256_ctx_t ctx;
286 uint8_t key[32], tweak[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);
297 /*****************************************************************************
299 *****************************************************************************/
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";
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},
325 cli_rx = (cli_rx_fpt)uart0_getc;
326 cli_tx = (cli_tx_fpt)uart0_putc;
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);