]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-twister512-test.c
now comes twister-512
[avr-crypto-lib.git] / test_src / main-twister512-test.c
1 /* main-twister512-test.c */
2 /*
3     This file is part of the Crypto-avr-lib/microcrypt-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-512";
39
40 /*****************************************************************************
41  *  additional validation-functions                                                                                      *
42  *****************************************************************************/
43 void twister512_init_dummy(void* ctx){
44         twister_big_init(ctx, 512);
45 }
46
47 void twister512_next_dummy(void* buffer, void* ctx){
48         twister_big_nextBlock(ctx, buffer);
49 }
50
51 void twister512_last_dummy(void* buffer, uint16_t size_b, void* ctx){
52         twister_big_lastBlock(ctx, buffer, size_b);
53 }
54
55 void twister512_ctx2hash_dummy(void* buffer, void* ctx){
56         twister_big_ctx2hash(buffer, ctx, 512);
57 }
58
59
60 void testrun_nessie_twister512(void){
61         nessie_hash_ctx.hashsize_b  = 512;
62         nessie_hash_ctx.blocksize_B = 512/8;
63         nessie_hash_ctx.ctx_size_B  = sizeof(twister_big_ctx_t);
64         nessie_hash_ctx.name = algo_name;
65         nessie_hash_ctx.hash_init = (nessie_hash_init_fpt)twister512_init_dummy;
66         nessie_hash_ctx.hash_next = (nessie_hash_next_fpt)twister512_next_dummy;
67         nessie_hash_ctx.hash_last = (nessie_hash_last_fpt)twister512_last_dummy;
68         nessie_hash_ctx.hash_conv = (nessie_hash_conv_fpt)twister512_ctx2hash_dummy;
69         
70         nessie_hash_run();
71 }
72
73 /******************************************************************************   
74  * selftests
75  ******************************************************************************/
76
77 void print_hash(void* hash){
78         uart_hexdump(hash, 256/8);
79         uart_putstr_P(PSTR("\r\n\t"));
80         uart_hexdump((uint8_t*)hash+256/8, 256/8);
81         
82 }
83
84 void testrun_twister512(void){
85         twister512_hash_t hash;
86         char* testv[]={
87                 "", 
88                 "a", 
89                 "abc", 
90                 "message digest", 
91                 "abcdefghijklmnopqrstuvwxyz", 
92                 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 
93                 "12345678901234567890123456789012345678901234567890123456789012345678901234567890"};
94         uint32_t i;
95         
96         uart_putstr_P(PSTR("\r\n=== TWISTER-512 test suit (MD5 test values) ==="));
97         for(i=0; i<7; ++i){
98                 uart_putstr_P(PSTR("\r\n TWISTER-512 (\""));
99                 uart_putstr(testv[i]);
100                 uart_putstr_P(PSTR("\") = \r\n\t"));
101                 twister512(&hash, testv[i], strlen(testv[i])*8);
102                 print_hash(hash);
103         //      return;
104         }
105         
106         uart_putstr_P(PSTR("\r\n\r\n=== TWISTER-512 test suit (short test values) ==="));
107         uint8_t stestv[]= {0x00, 0x00, 0xC0, 0xC0, 0x80, 0x48, 0x50};
108         uint8_t stestl[]= {   0,    1,    2,    3,    4,    5,    6};   
109         for(i=0; i<7; ++i){
110                 uart_putstr_P(PSTR("\r\n TWISTER-512 (\""));
111                 uart_hexdump(&(stestv[i]), 1);
112                 uart_putstr_P(PSTR("\") = \r\n\t"));
113                 twister512(hash, &(stestv[i]), stestl[i]);
114                 print_hash(hash);
115         }
116
117         uart_putstr_P(PSTR("\r\n\r\n=== TWISTER-512 test suit (long test) ==="));
118         char* ltest= "abcdefghbcdefghicdefghijdefghijk"
119                            "efghijklfghijklmghijklmnhijklmno";  
120         twister512_ctx_t ctx;
121         twister512_init(&ctx);  
122         uart_putstr_P(PSTR("\r\n TWISTER-512 ( 16777216 x \""));
123         uart_putstr(ltest);     
124         uart_putstr_P(PSTR("\") = \r\n\t"));
125         for(i=0; i<16777216; ++i){
126                 twister512_nextBlock(&ctx, ltest);
127         }
128         twister512_ctx2hash(hash, &ctx);
129         print_hash(hash);
130 }
131
132
133 void testrun_performance_twister512(void){
134         uint64_t t;
135         char str[16];
136         uint8_t data[64];
137         twister_big_ctx_t ctx;
138         
139         calibrateTimer();
140         print_overhead();
141         
142         memset(data, 0, 64);
143         
144         startTimer(1);
145         twister_big_init(&ctx, 512);
146         t = stopTimer();
147         uart_putstr_P(PSTR("\r\n\tctx-gen time: "));
148         ultoa((unsigned long)t, str, 10);
149         uart_putstr(str);
150         
151         
152         startTimer(1);
153         twister_big_nextBlock(&ctx, data);
154         t = stopTimer();
155         uart_putstr_P(PSTR("\r\n\tone-block time: "));
156         ultoa((unsigned long)t, str, 10);
157         uart_putstr(str);
158         
159         
160         startTimer(1);
161         twister_big_lastBlock(&ctx, data, 0);
162         t = stopTimer();
163         uart_putstr_P(PSTR("\r\n\tlast block time: "));
164         ultoa((unsigned long)t, str, 10);
165         uart_putstr(str);
166         
167         startTimer(1);
168         twister_big_ctx2hash(data, &ctx, 512);
169         t = stopTimer();
170         uart_putstr_P(PSTR("\r\n\tctx2hash time: "));
171         ultoa((unsigned long)t, str, 10);
172         uart_putstr(str);
173
174         uart_putstr_P(PSTR("\r\n"));
175 }
176
177
178 /*****************************************************************************
179  * main
180  *
181  *****************************************************************************/
182
183 int main (void){
184         char str[20];
185
186         
187         DEBUG_INIT();
188         uart_putstr_P(PSTR("\r\n"));
189
190         uart_putstr_P(PSTR("\r\n\r\nCrypto-VS (")); 
191         uart_putstr(algo_name);
192         uart_putstr_P(PSTR(")\r\nloaded and running\r\n"));
193         PGM_P    u   = PSTR("nessie\0test\0performance\0");
194         void_fpt v[] = { testrun_nessie_twister512, 
195                          testrun_twister512, 
196                          testrun_performance_twister512 };
197
198         while(1){ 
199                 if (!getnextwordn(str,20)){DEBUG_S("DBG: W1\r\n"); goto error;}
200                 if(execcommand_d0_P(str, u, v)<0){
201                         uart_putstr_P(PSTR("\r\nunknown command\r\n"));
202                 }
203                 continue;
204         error:
205                 uart_putstr("ERROR\r\n");
206         }
207 }
208