]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-shabal-test.c
more shabal
[avr-crypto-lib.git] / test_src / main-shabal-test.c
1 /* main-shabal-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  * shabal 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 "shabal.h"
30 #include "cli.h"
31 #include "hfal_shabal.h"
32 #include "shavs.h"
33 #include "nessie_hash_test.h"
34 #include "performance_test.h"
35
36 #include <stdint.h>
37 #include <string.h>
38 #include <stdlib.h>
39
40 char* algo_name = "Shabal";
41
42 /*****************************************************************************
43  *  additional validation-functions                                                                                      *
44  *****************************************************************************/
45 void testrun_stdtest_shabal192(void* msg, uint16_t size_b){
46         uint8_t hash[192/8];
47                 
48         cli_putstr_P(PSTR("\r\n\r\nTest vectors for Shabal (192 bits):"));
49
50         cli_putstr_P(PSTR("\r\nmessage:"));
51         cli_hexdump_block(msg, (size_b+7)/8, 4, 16);
52         shabal192(hash, msg, size_b);
53         cli_putstr_P(PSTR("\r\nhash:"));
54         cli_hexdump_block(hash, 192/8, 4, 16);
55 }
56
57 void testrun_stdtest_shabal224(void* msg, uint16_t size_b){
58         uint8_t hash[224/8];
59                 
60         cli_putstr_P(PSTR("\r\n\r\nTest vectors for Shabal (224 bits):"));
61
62         cli_putstr_P(PSTR("\r\nmessage:"));
63         cli_hexdump_block(msg, (size_b+7)/8, 4, 16);
64         shabal224(hash, msg, size_b);
65         cli_putstr_P(PSTR("\r\nhash:"));
66         cli_hexdump_block(hash, 224/8, 4, 16);
67 }
68
69 void testrun_stdtest_shabal256(void* msg, uint16_t size_b){
70         uint8_t hash[256/8];
71                 
72         cli_putstr_P(PSTR("\r\n\r\nTest vectors for Shabal (256 bits):"));
73
74         cli_putstr_P(PSTR("\r\nmessage:"));
75         cli_hexdump_block(msg, (size_b+7)/8, 4, 16);
76         shabal256(hash, msg, size_b);
77         cli_putstr_P(PSTR("\r\nhash:"));
78         cli_hexdump_block(hash, 256/8, 4, 16);
79 }
80
81 void testrun_stdtest_shabal384(void* msg, uint16_t size_b){
82         uint8_t hash[384/8];
83                 
84         cli_putstr_P(PSTR("\r\n\r\nTest vectors for Shabal (384 bits):"));
85
86         cli_putstr_P(PSTR("\r\nmessage:"));
87         cli_hexdump_block(msg, (size_b+7)/8, 4, 16);
88         shabal384(hash, msg, size_b);
89         cli_putstr_P(PSTR("\r\nhash:"));
90         cli_hexdump_block(hash, 384/8, 4, 16);
91 }
92
93 void testrun_stdtest_shabal512(void* msg, uint16_t size_b){
94         uint8_t hash[512/8];
95                 
96         cli_putstr_P(PSTR("\r\n\r\nTest vectors for Shabal (512 bits):"));
97
98         cli_putstr_P(PSTR("\r\nmessage:"));
99         cli_hexdump_block(msg, (size_b+7)/8, 4, 16);
100         shabal512(hash, msg, size_b);
101         cli_putstr_P(PSTR("\r\nhash:"));
102         cli_hexdump_block(hash, 512/8, 4, 16);
103 }
104
105 void testrun_stdtest_shabal(void){
106         uint8_t ma[64];
107         char*   mb= "abcdefghijklmnopqrstuvwxyz-"
108                     "0123456789-"
109                             "ABCDEFGHIJKLMNOPQRSTUVWXYZ-"
110                 "0123456789-"
111                                 "abcdefghijklmnopqrstuvwxyz";
112
113         memset(ma, 0, 64);
114         testrun_stdtest_shabal192(ma, 64*8);
115         testrun_stdtest_shabal192(mb, strlen(mb)*8);
116         testrun_stdtest_shabal224(ma, 64*8);
117         testrun_stdtest_shabal224(mb, strlen(mb)*8);
118         testrun_stdtest_shabal256(ma, 64*8);
119         testrun_stdtest_shabal256(mb, strlen(mb)*8);
120         testrun_stdtest_shabal384(ma, 64*8);
121         testrun_stdtest_shabal384(mb, strlen(mb)*8);
122         testrun_stdtest_shabal512(ma, 64*8);
123         testrun_stdtest_shabal512(mb, strlen(mb)*8);
124 }
125
126 void testshort(void){
127         uint8_t ma[64];
128         memset(ma, 0, 64);
129         testrun_stdtest_shabal192(ma, 64*8);
130 }
131
132 void shabal_ctx_dump(shabal_ctx_t* ctx){
133         uint8_t i;
134         void* p;
135         cli_putstr_P(PSTR("\r\n=== shabal ctx dump ===\r\n  size = "));
136         i=sizeof(shabal_ctx_t);
137         if(i>=100)
138                 cli_putc('0'+i/100);
139         if(i>=10)
140                 cli_putc('0'+(i/10)%10);
141         cli_putc('0'+i%10);     
142         cli_putstr_P(PSTR("\r\n  a = "));
143         cli_hexdump_block(ctx->a, 12*4, 5, 4*8);
144         cli_putstr_P(PSTR("\r\n  b_buffer = "));
145         cli_hexdump_block(ctx->b_buffer, 12*4, 5, 4*8);
146         cli_putstr_P(PSTR("\r\n  c_buffer = "));
147         cli_hexdump_block(ctx->c_buffer, 12*4, 5, 4*8);
148         if(ctx->b == &(ctx->b_buffer[0]))
149                 cli_putstr_P(PSTR("\r\nb --> b_buffer"));
150         if(ctx->b == &(ctx->c_buffer[0]))
151                 cli_putstr_P(PSTR("\r\nb --> c_buffer"));       
152         if(ctx->c == &(ctx->b_buffer[0]))
153                 cli_putstr_P(PSTR("\r\nc --> b_buffer"));
154         if(ctx->c == &(ctx->c_buffer[0]))
155                 cli_putstr_P(PSTR("\r\nc --> c_buffer"));
156         cli_putstr_P(PSTR("\r\n b = "));
157         cli_hexdump(&(ctx->b), 2);
158         p = ctx->b_buffer;
159         cli_putstr_P(PSTR("\r\n b (should) = "));
160         cli_hexdump(&p, 2);
161         cli_putstr_P(PSTR("\r\n c = "));
162         cli_hexdump(&(ctx->c), 2);      
163         p = ctx->c_buffer;
164         cli_putstr_P(PSTR("\r\n c (should) = "));
165         cli_hexdump(&p, 2);
166 }
167
168
169 void testinit_192(void){
170         shabal_ctx_t ctx;
171         shabal192_init(&ctx);
172         shabal_ctx_dump(&ctx);
173 }
174
175 void testinit_224(void){
176         shabal_ctx_t ctx;
177         shabal224_init(&ctx);
178         shabal_ctx_dump(&ctx);
179 }
180
181 void testinit_256(void){
182         shabal_ctx_t ctx;
183         shabal256_init(&ctx);
184         shabal_ctx_dump(&ctx);
185 }
186
187 void testinit_384(void){
188         shabal_ctx_t ctx;
189         shabal384_init(&ctx);
190         shabal_ctx_dump(&ctx);
191 }
192
193 void testinit_512(void){
194         shabal_ctx_t ctx;
195         shabal512_init(&ctx);
196         shabal_ctx_dump(&ctx);
197 }
198 void testinit(void){
199         testinit_192();
200         testinit_224();
201         testinit_256();
202         testinit_384();
203         testinit_512();
204 }
205
206 void performance_shabal(void){
207         uint64_t t;
208         char str[16];
209         uint8_t data[64];
210         uint8_t hash[512/8];
211         shabal_ctx_t ctx;
212         
213         calibrateTimer();
214         print_overhead();
215         
216         memset(data, 0, 64);
217         
218         startTimer(1);
219         shabal192_init(&ctx);
220         t = stopTimer();
221         cli_putstr_P(PSTR("\r\n\tctx-gen time (192): "));
222         ultoa((unsigned long)t, str, 10);
223         cli_putstr(str);
224         
225         startTimer(1);
226         shabal224_init(&ctx);
227         t = stopTimer();
228         cli_putstr_P(PSTR("\r\n\tctx-gen time (224): "));
229         ultoa((unsigned long)t, str, 10);
230         cli_putstr(str);
231         
232         startTimer(1);
233         shabal256_init(&ctx);
234         t = stopTimer();
235         cli_putstr_P(PSTR("\r\n\tctx-gen time (256): "));
236         ultoa((unsigned long)t, str, 10);
237         cli_putstr(str);
238         
239         startTimer(1);
240         shabal384_init(&ctx);
241         t = stopTimer();
242         cli_putstr_P(PSTR("\r\n\tctx-gen time (384): "));
243         ultoa((unsigned long)t, str, 10);
244         cli_putstr(str);
245         
246         startTimer(1);
247         shabal512_init(&ctx);
248         t = stopTimer();
249         cli_putstr_P(PSTR("\r\n\tctx-gen time (512): "));
250         ultoa((unsigned long)t, str, 10);
251         cli_putstr(str);
252                 
253         startTimer(1);
254         shabal_nextBlock(&ctx, data);
255         t = stopTimer();
256         cli_putstr_P(PSTR("\r\n\tone-block time: "));
257         ultoa((unsigned long)t, str, 10);
258         cli_putstr(str);
259         
260         
261         startTimer(1);
262         shabal_lastBlock(&ctx, data, 0);
263         t = stopTimer();
264         cli_putstr_P(PSTR("\r\n\tlast block time: "));
265         ultoa((unsigned long)t, str, 10);
266         cli_putstr(str);
267         
268         startTimer(1);
269         shabal192_ctx2hash(hash, &ctx);
270         t = stopTimer();
271         cli_putstr_P(PSTR("\r\n\tctx2hash time (192): "));
272         ultoa((unsigned long)t, str, 10);
273         cli_putstr(str);
274         
275         startTimer(1);
276         shabal224_ctx2hash(hash, &ctx);
277         t = stopTimer();
278         cli_putstr_P(PSTR("\r\n\tctx2hash time (224): "));
279         ultoa((unsigned long)t, str, 10);
280         cli_putstr(str);
281         
282         startTimer(1);
283         shabal256_ctx2hash(hash, &ctx);
284         t = stopTimer();
285         cli_putstr_P(PSTR("\r\n\tctx2hash time (256): "));
286         ultoa((unsigned long)t, str, 10);
287         cli_putstr(str);
288         
289         startTimer(1);
290         shabal384_ctx2hash(hash, &ctx);
291         t = stopTimer();
292         cli_putstr_P(PSTR("\r\n\tctx2hash time (384): "));
293         ultoa((unsigned long)t, str, 10);
294         cli_putstr(str);
295         
296         startTimer(1);
297         shabal512_ctx2hash(hash, &ctx);
298         t = stopTimer();
299         cli_putstr_P(PSTR("\r\n\tctx2hash time (512): "));
300         ultoa((unsigned long)t, str, 10);
301         cli_putstr(str);
302                 
303         cli_putstr_P(PSTR("\r\n"));
304
305 }
306
307 void testrun_nessie_shabal(void){
308         nessie_hash_ctx.hashsize_b  = 192;
309         nessie_hash_ctx.blocksize_B = 512/8;
310         nessie_hash_ctx.ctx_size_B  = sizeof(shabal_ctx_t);
311         nessie_hash_ctx.name = "Shabal-192";
312         nessie_hash_ctx.hash_init = (nessie_hash_init_fpt)shabal192_init;
313         nessie_hash_ctx.hash_next = (nessie_hash_next_fpt)shabal_nextBlock;
314         nessie_hash_ctx.hash_last = (nessie_hash_last_fpt)shabal_lastBlock;
315         nessie_hash_ctx.hash_conv = (nessie_hash_conv_fpt)shabal192_ctx2hash;
316         
317         nessie_hash_run();
318         
319         nessie_hash_ctx.hashsize_b  = 224;
320         nessie_hash_ctx.name = "Shabal-224";
321         nessie_hash_ctx.hash_init = (nessie_hash_init_fpt)shabal224_init;
322         nessie_hash_ctx.hash_conv = (nessie_hash_conv_fpt)shabal224_ctx2hash;
323         
324         nessie_hash_run();
325         
326         nessie_hash_ctx.hashsize_b  = 256;
327         nessie_hash_ctx.name = "Shabal-256";
328         nessie_hash_ctx.hash_init = (nessie_hash_init_fpt)shabal256_init;
329         nessie_hash_ctx.hash_conv = (nessie_hash_conv_fpt)shabal256_ctx2hash;
330         
331         nessie_hash_run();
332         
333         nessie_hash_ctx.hashsize_b  = 384;
334         nessie_hash_ctx.name = "Shabal-384";
335         nessie_hash_ctx.hash_init = (nessie_hash_init_fpt)shabal384_init;
336         nessie_hash_ctx.hash_conv = (nessie_hash_conv_fpt)shabal384_ctx2hash;
337         
338         nessie_hash_run();
339         
340         nessie_hash_ctx.hashsize_b  = 512;
341         nessie_hash_ctx.name = "Shabal-512";
342         nessie_hash_ctx.hash_init = (nessie_hash_init_fpt)shabal512_init;
343         nessie_hash_ctx.hash_conv = (nessie_hash_conv_fpt)shabal512_ctx2hash;
344         
345         nessie_hash_run();
346         
347 }
348
349 /*****************************************************************************
350  *  main                                                                                                                                         *
351  *****************************************************************************/
352
353 const hfdesc_t* algolist[] PROGMEM = {
354         (hfdesc_t*)&shabal192_desc,
355         (hfdesc_t*)&shabal224_desc,
356         (hfdesc_t*)&shabal256_desc,
357         (hfdesc_t*)&shabal384_desc,
358         (hfdesc_t*)&shabal512_desc,     
359         NULL
360 };
361
362 const char nessie_str[]      PROGMEM = "nessie";
363 const char test_str[]        PROGMEM = "test";
364 const char testinit192_str[] PROGMEM = "testinit192";
365 const char testinit_str[]    PROGMEM = "testinit";
366 const char testshort_str[]   PROGMEM = "short";
367 const char ztest_str[]       PROGMEM = "zerotest";
368 const char performance_str[] PROGMEM = "performance";
369 const char echo_str[]        PROGMEM = "echo";
370 const char shavs_list_str[]  PROGMEM = "shavs_list";
371 const char shavs_set_str[]   PROGMEM = "shavs_set";
372 const char shavs_test1_str[] PROGMEM = "shavs_test1";
373
374 cmdlist_entry_t cmdlist[] PROGMEM = {
375         { nessie_str,          NULL, testrun_nessie_shabal},
376         { test_str,            NULL, testrun_stdtest_shabal},
377         { testinit192_str,     NULL, testinit_192},
378         { testinit_str,        NULL, testinit},
379         { testshort_str,       NULL, testshort},
380         { performance_str,     NULL, performance_shabal},
381         { shavs_list_str,      NULL, shavs_listalgos},
382         { shavs_set_str,   (void*)1, (void_fpt)shavs_setalgo},
383         { shavs_test1_str,     NULL, shavs_test1},
384         { echo_str,        (void*)1, (void_fpt)echo_ctrl},
385         { NULL,                NULL, NULL}
386 };
387
388 int main (void){
389         DEBUG_INIT();
390         
391         cli_rx = uart_getc;
392         cli_tx = uart_putc;             
393         shavs_algolist=(hfdesc_t**)algolist;
394         shavs_algo=(hfdesc_t*)&shabal256_desc;
395         for(;;){
396                 cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
397                 cli_putstr(algo_name);
398                 cli_putstr_P(PSTR("; "));
399                 cli_putstr(__DATE__);
400                 cli_putstr_P(PSTR(" "));
401                 cli_putstr(__TIME__);
402                 cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
403                 
404                 cmd_interface(cmdlist);
405         }
406 }