]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-twister384-test.c
+Shabal
[avr-crypto-lib.git] / test_src / main-twister384-test.c
1 /* main-twister384-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  * twister 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 "twister-big.h"
30 #include "nessie_hash_test.h"
31 #include "performance_test.h"
32
33 #include <stdint.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include "cli.h"
37
38 char* algo_name = "TWISTER-384";
39
40 /*****************************************************************************
41  *  additional validation-functions                                                                                      *
42  *****************************************************************************/
43 void twister384_init_dummy(void* ctx){
44         twister_big_init(ctx, 384);
45 }
46
47 void twister384_ctx2hash_dummy(void* buffer, void* ctx){
48         twister_big_ctx2hash(buffer, ctx, 384);
49 }
50
51
52 void testrun_nessie_twister384(void){
53         nessie_hash_ctx.hashsize_b  = 384;
54         nessie_hash_ctx.blocksize_B = 512/8;
55         nessie_hash_ctx.ctx_size_B  = sizeof(twister_big_ctx_t);
56         nessie_hash_ctx.name = algo_name;
57         nessie_hash_ctx.hash_init = (nessie_hash_init_fpt)twister384_init_dummy;
58         nessie_hash_ctx.hash_next = (nessie_hash_next_fpt)twister_big_nextBlock;
59         nessie_hash_ctx.hash_last = (nessie_hash_last_fpt)twister_big_lastBlock;
60         nessie_hash_ctx.hash_conv = (nessie_hash_conv_fpt)twister384_ctx2hash_dummy;
61         
62         nessie_hash_run();
63 }
64
65 /******************************************************************************   
66  * selftests
67  ******************************************************************************/
68
69 void print_hash(void* hash){
70         cli_hexdump(hash, 256/8);
71         cli_putstr_P(PSTR("\r\n\t"));
72         cli_hexdump((uint8_t*)hash+256/8, 128/8);
73         
74 }
75
76 void testrun_twister384(void){
77         twister384_hash_t hash;
78         char* testv[]={
79                 "", 
80                 "a", 
81                 "abc", 
82                 "message digest", 
83                 "abcdefghijklmnopqrstuvwxyz", 
84                 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 
85                 "12345678901234567890123456789012345678901234567890123456789012345678901234567890"};
86         uint32_t i;
87         
88         cli_putstr_P(PSTR("\r\n=== TWISTER-384 test suit (MD5 test values) ==="));
89         for(i=0; i<7; ++i){
90                 cli_putstr_P(PSTR("\r\n TWISTER-384 (\""));
91                 cli_putstr(testv[i]);
92                 cli_putstr_P(PSTR("\") = \r\n\t"));
93                 twister384(&hash, testv[i], strlen(testv[i])*8);
94                 print_hash(hash);
95         //      return;
96         }
97         
98         cli_putstr_P(PSTR("\r\n\r\n=== TWISTER-384 test suit (short test values) ==="));
99         uint8_t stestv[]= {0x00, 0x00, 0xC0, 0xC0, 0x80, 0x48, 0x50};
100         uint8_t stestl[]= {   0,    1,    2,    3,    4,    5,    6};   
101         for(i=0; i<7; ++i){
102                 cli_putstr_P(PSTR("\r\n TWISTER-384 (\""));
103                 cli_hexdump(&(stestv[i]), 1);
104                 cli_putstr_P(PSTR("\") = \r\n\t"));
105                 twister384(hash, &(stestv[i]), stestl[i]);
106                 print_hash(hash);
107         }
108         
109 #ifdef TWISTER_LONGTEST
110         cli_putstr_P(PSTR("\r\n\r\n=== TWISTER-384 test suit (long test) ==="));
111         char* ltest= "abcdefghbcdefghicdefghijdefghijk"
112                            "efghijklfghijklmghijklmnhijklmno";  
113         twister384_ctx_t ctx;
114         twister384_init(&ctx);  
115         cli_putstr_P(PSTR("\r\n TWISTER-384 ( 16777216 x \""));
116         cli_putstr(ltest);      
117         cli_putstr_P(PSTR("\") = \r\n\t"));
118         for(i=0; i<16777216; ++i){
119                 twister384_nextBlock(&ctx, ltest);
120         }
121         twister384_ctx2hash(hash, &ctx);
122         print_hash(hash);
123 #endif
124 }
125
126
127 void testrun_performance_twister384(void){
128         uint64_t t;
129         char str[16];
130         uint8_t data[64];
131         twister_big_ctx_t ctx;
132         
133         calibrateTimer();
134         print_overhead();
135         
136         memset(data, 0, 64);
137         
138         startTimer(1);
139         twister_big_init(&ctx, 384);
140         t = stopTimer();
141         cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
142         ultoa((unsigned long)t, str, 10);
143         cli_putstr(str);
144         
145         
146         startTimer(1);
147         twister_big_nextBlock(&ctx, data);
148         t = stopTimer();
149         cli_putstr_P(PSTR("\r\n\tone-block time: "));
150         ultoa((unsigned long)t, str, 10);
151         cli_putstr(str);
152         
153         
154         startTimer(1);
155         twister_big_lastBlock(&ctx, data, 0);
156         t = stopTimer();
157         cli_putstr_P(PSTR("\r\n\tlast block time: "));
158         ultoa((unsigned long)t, str, 10);
159         cli_putstr(str);
160         
161         startTimer(1);
162         twister_big_ctx2hash(data, &ctx, 384);
163         t = stopTimer();
164         cli_putstr_P(PSTR("\r\n\tctx2hash time: "));
165         ultoa((unsigned long)t, str, 10);
166         cli_putstr(str);
167
168         cli_putstr_P(PSTR("\r\n"));
169 }
170
171
172 /*****************************************************************************
173  * main
174  *
175  *****************************************************************************/
176
177 const char nessie_str[]      PROGMEM = "nessie";
178 const char test_str[]        PROGMEM = "test";
179 const char performance_str[] PROGMEM = "performance";
180 const char echo_str[]        PROGMEM = "echo";
181
182 cmdlist_entry_t cmdlist[] PROGMEM = {
183         { nessie_str,      NULL, testrun_nessie_twister384},
184         { test_str,        NULL, testrun_twister384},
185         { performance_str, NULL, testrun_performance_twister384},
186         { echo_str,    (void*)1, (void_fpt)echo_ctrl},
187         { NULL,            NULL, NULL}
188 };
189
190 int main (void){
191         DEBUG_INIT();
192         
193         cli_rx = uart_getc;
194         cli_tx = uart_putc;             
195         for(;;){
196                 cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
197                 cli_putstr(algo_name);
198                 cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
199                 cmd_interface(cmdlist);
200         }
201 }