]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-twister256-test.c
e6bb300a258b4a7c9c72446efb7e6feb626a802a
[avr-crypto-lib.git] / test_src / main-twister256-test.c
1 /* main-twister256-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-small.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-256";
39
40 /*****************************************************************************
41  *  additional validation-functions                                                                                      *
42  *****************************************************************************/
43 void twister256_init_dummy(void* ctx){
44         twister_small_init(ctx, 256);
45 }
46
47 void twister256_next_dummy(void* buffer, void* ctx){
48         twister_small_nextBlock(ctx, buffer);
49 }
50
51 void twister256_last_dummy(void* buffer, uint16_t size_b, void* ctx){
52         twister_small_lastBlock(ctx, buffer, size_b);
53 }
54
55 void twister256_ctx2hash_dummy(void* buffer, void* ctx){
56         twister_small_ctx2hash(buffer, ctx, 256);
57 }
58
59
60 void testrun_nessie_twister256(void){
61         nessie_hash_ctx.hashsize_b  = 256;
62         nessie_hash_ctx.blocksize_B = 512/8;
63         nessie_hash_ctx.ctx_size_B  = sizeof(twister_state_t);
64         nessie_hash_ctx.name = algo_name;
65         nessie_hash_ctx.hash_init = (nessie_hash_init_fpt)twister256_init_dummy;
66         nessie_hash_ctx.hash_next = (nessie_hash_next_fpt)twister256_next_dummy;
67         nessie_hash_ctx.hash_last = (nessie_hash_last_fpt)twister256_last_dummy;
68         nessie_hash_ctx.hash_conv = (nessie_hash_conv_fpt)twister256_ctx2hash_dummy;
69         
70         nessie_hash_run();
71 }
72
73 /*****************************************************************************
74  *  self tests                                                                                                                           *
75  *****************************************************************************/
76
77 void testrun_twister256(void){
78         twister256_hash_t hash;
79         char* testv[]={
80                 "", 
81                 "a", 
82                 "abc", 
83                 "message digest", 
84                 "abcdefghijklmnopqrstuvwxyz", 
85                 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 
86                 "12345678901234567890123456789012345678901234567890123456789012345678901234567890"};
87         uint32_t i;
88         
89         uart_putstr_P(PSTR("\r\n=== TWISTER-256 test suit (MD5 test values) ==="));
90         for(i=0; i<7; ++i){
91                 uart_putstr_P(PSTR("\r\n TWISTER-256 (\""));
92                 uart_putstr(testv[i]);
93                 uart_putstr_P(PSTR("\") = \r\n\t"));
94                 twister256(&hash, testv[i], strlen(testv[i])*8);
95                 uart_hexdump(hash, 256/8);
96         }
97         
98         uart_putstr_P(PSTR("\r\n\r\n=== TWISTER-256 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                 uart_putstr_P(PSTR("\r\n TWISTER-256 (\""));
103                 uart_hexdump(&(stestv[i]), 1);
104                 uart_putstr_P(PSTR("\") = \r\n\t"));
105                 twister256(&hash, &(stestv[i]), stestl[i]);
106                 uart_hexdump(hash, 256/8);
107         }
108
109         uart_putstr_P(PSTR("\r\n\r\n=== TWISTER-256 test suit (long test) ==="));
110         char* ltest= "abcdefghbcdefghicdefghijdefghijk"
111                            "efghijklfghijklmghijklmnhijklmno";  
112         twister256_ctx_t ctx;
113         twister256_init(&ctx);  
114         uart_putstr_P(PSTR("\r\n TWISTER-256 ( 16777216 x \""));
115         uart_putstr(ltest);     
116         uart_putstr_P(PSTR("\") = \r\n\t"));
117         for(i=0; i<16777216; ++i){
118                 twister224_nextBlock(&ctx, ltest);
119         }
120         twister256_ctx2hash(hash, &ctx);
121         uart_hexdump(hash, 256/8);
122 }
123
124
125 void testrun_performance_twister256(void){
126         uint64_t t;
127         char str[16];
128         uint8_t data[64];
129         twister_state_t ctx;
130         
131         calibrateTimer();
132         print_overhead();
133         
134         memset(data, 0, 64);
135         
136         startTimer(1);
137         twister_small_init(&ctx, 256);
138         t = stopTimer();
139         uart_putstr_P(PSTR("\r\n\tctx-gen time: "));
140         ultoa((unsigned long)t, str, 10);
141         uart_putstr(str);
142         
143         
144         startTimer(1);
145         twister_small_nextBlock(&ctx, data);
146         t = stopTimer();
147         uart_putstr_P(PSTR("\r\n\tone-block time: "));
148         ultoa((unsigned long)t, str, 10);
149         uart_putstr(str);
150         
151         
152         startTimer(1);
153         twister_small_lastBlock(&ctx, data, 0);
154         t = stopTimer();
155         uart_putstr_P(PSTR("\r\n\tlast block time: "));
156         ultoa((unsigned long)t, str, 10);
157         uart_putstr(str);
158         
159         startTimer(1);
160         twister_small_ctx2hash(data, &ctx, 256);
161         t = stopTimer();
162         uart_putstr_P(PSTR("\r\n\tctx2hash time: "));
163         ultoa((unsigned long)t, str, 10);
164         uart_putstr(str);
165
166         uart_putstr_P(PSTR("\r\n"));
167 }
168
169
170 /*****************************************************************************
171  * main                                                                                                                                  *
172  *****************************************************************************/
173
174 int main (void){
175         char str[20];
176
177         
178         DEBUG_INIT();
179         uart_putstr_P(PSTR("\r\n"));
180
181         uart_putstr_P(PSTR("\r\n\r\nCrypto-VS (")); 
182         uart_putstr(algo_name);
183         uart_putstr_P(PSTR(")\r\nloaded and running\r\n"));
184         PGM_P    u   = PSTR("nessie\0test\0performance\0");
185         void_fpt v[] = { testrun_nessie_twister256, 
186                          testrun_twister256, 
187                          testrun_performance_twister256 };
188
189         while(1){ 
190                 if (!getnextwordn(str,20)){DEBUG_S("DBG: W1\r\n"); goto error;}
191                 if(execcommand_d0_P(str, u, v)<0){
192                         uart_putstr_P(PSTR("\r\nunknown command\r\n"));
193                 }
194                 continue;
195         error:
196                 uart_putstr("ERROR\r\n");
197         }
198 }
199