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