]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-skein-test.c
7faaa685c0f33ef7d28f2b454f40f92593755f9c
[avr-crypto-lib.git] / test_src / main-skein-test.c
1 /* main-skein-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  * threefish 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 "skein.h"
30 #include "cli.h"
31 #include "performance_test.h"
32
33 #include <stdint.h>
34 #include <string.h>
35 #include <stdlib.h>
36
37 char* algo_name = "Skein";
38
39 /*****************************************************************************
40  *  additional validation-functions                                                                                      *
41  *****************************************************************************/
42 void testrun_stdtest_skein256(uint16_t outsize_b){
43         uint8_t message[64];
44         uint8_t hash[(outsize_b+7)/8];
45         uint8_t i;
46         skein256_ctx_t ctx;
47                 
48         cli_putstr_P(PSTR("\r\n\r\nTest vectors for Skein (256 bits):"));
49         for(i=0; i<64; ++i)
50                 message[i] = 0xFF-i;
51         
52         cli_putstr_P(PSTR("\r\nmessage:    "));
53         cli_hexdump(message, 1);
54         skein256_init(&ctx, outsize_b);
55         skein256_lastBlock(&ctx, message, 8);
56         skein256_ctx2hash(hash, &ctx);
57         cli_putstr_P(PSTR("\r\nhash:"));
58         cli_hexdump_block(hash, (outsize_b+7)/8, 4, 16);
59         
60         cli_putstr_P(PSTR("\r\nmessage:"));
61         cli_hexdump_block(message, 32, 4, 16);
62         skein256_init(&ctx, outsize_b);
63         skein256_lastBlock(&ctx, message, 32*8);
64         skein256_ctx2hash(hash, &ctx);
65         cli_putstr_P(PSTR("\r\nhash:"));
66         cli_hexdump_block(hash, (outsize_b+7)/8, 4, 16);
67         
68         cli_putstr_P(PSTR("\r\nmessage:"));
69         cli_hexdump_block(message, 64, 4, 16);
70         skein256_init(&ctx, outsize_b);
71         skein256_lastBlock(&ctx, message, 64*8);
72         skein256_ctx2hash(hash, &ctx);
73         cli_putstr_P(PSTR("\r\nhash:"));
74         cli_hexdump_block(hash, (outsize_b+7)/8, 4, 16);
75 }
76
77 void testrun_stdtest_skein512(uint16_t outsize_b){
78         uint8_t message[128];
79         uint8_t hash[(outsize_b+7)/8];
80         uint8_t i;
81         skein512_ctx_t ctx;
82                 
83         cli_putstr_P(PSTR("\r\n\r\nTest vectors for Skein (512 bits):"));
84         for(i=0; i<128; ++i)
85                 message[i] = 0xFF-i;
86         
87         cli_putstr_P(PSTR("\r\nmessage:    "));
88         cli_hexdump(message, 1);
89         skein512_init(&ctx, outsize_b);
90         skein512_lastBlock(&ctx, message, 8);
91         skein512_ctx2hash(hash, &ctx);
92         cli_putstr_P(PSTR("\r\nhash:"));
93         cli_hexdump_block(hash, (outsize_b+7)/8, 4, 16);
94         
95         cli_putstr_P(PSTR("\r\nmessage:"));
96         cli_hexdump_block(message, 64, 4, 16);
97         skein512_init(&ctx, outsize_b);
98         skein512_lastBlock(&ctx, message, 64*8);
99         skein512_ctx2hash(hash, &ctx);
100         cli_putstr_P(PSTR("\r\nhash:"));
101         cli_hexdump_block(hash, (outsize_b+7)/8, 4, 16);
102         
103         cli_putstr_P(PSTR("\r\nmessage:"));
104         cli_hexdump_block(message, 128, 4, 16);
105         skein512_init(&ctx, outsize_b);
106         skein512_lastBlock(&ctx, message, 128*8);
107         skein512_ctx2hash(hash, &ctx);
108         cli_putstr_P(PSTR("\r\nhash:"));
109         cli_hexdump_block(hash, (outsize_b+7)/8, 4, 16);
110 }
111
112 void testrun_stdtest_skein1024(uint16_t outsize_b){
113         uint8_t message[256];
114         uint8_t hash[(outsize_b+7)/8];
115         uint16_t i;
116         skein1024_ctx_t ctx;
117                 
118         cli_putstr_P(PSTR("\r\n\r\nTest vectors for Skein (1024 bits):"));
119         for(i=0; i<256; ++i)
120                 message[i] = 0xFF-i;
121         
122         cli_putstr_P(PSTR("\r\nmessage:    "));
123         cli_hexdump(message, 1);
124         skein1024_init(&ctx, outsize_b);
125         skein1024_lastBlock(&ctx, message, 8);
126         skein1024_ctx2hash(hash, &ctx);
127         cli_putstr_P(PSTR("\r\nhash:"));
128         cli_hexdump_block(hash, (outsize_b+7)/8, 4, 16);
129         
130         cli_putstr_P(PSTR("\r\nmessage:"));
131         cli_hexdump_block(message, 128, 4, 16);
132         skein1024_init(&ctx, outsize_b);
133         skein1024_lastBlock(&ctx, message, 128*8);
134         skein1024_ctx2hash(hash, &ctx);
135         cli_putstr_P(PSTR("\r\nhash:"));
136         cli_hexdump_block(hash, (outsize_b+7)/8, 4, 16);
137         
138         cli_putstr_P(PSTR("\r\nmessage:"));
139         cli_hexdump_block(message, 256, 4, 16);
140         skein1024_init(&ctx, outsize_b);
141         skein1024_lastBlock(&ctx, message, 256*8);
142         skein1024_ctx2hash(hash, &ctx);
143         cli_putstr_P(PSTR("\r\nhash:"));
144         cli_hexdump_block(hash, (outsize_b+7)/8, 4, 16);
145 }
146
147 void testrun_stdtest_skein(void){
148         testrun_stdtest_skein256(256);
149         testrun_stdtest_skein512(512);
150         testrun_stdtest_skein1024(1024);
151 }
152 /*
153 void testrun_performance_threefish256(void){
154         uint64_t t;
155         char str[16];
156         uint8_t key[THREEFISH256_BLOCKSIZE_B];
157         uint8_t data[THREEFISH256_BLOCKSIZE_B];
158         uint8_t tweak[16];
159         threefish256_ctx_t ctx;
160         
161         cli_putstr_P(PSTR("\r\nThreefish-256 performance:"));
162         
163         calibrateTimer();
164         print_overhead();       
165         
166 //      memset(key,  0, THREEFISH256_BLOCKSIZE_B);
167 //      memset(tweak, 0, 16);
168         
169         startTimer(1);
170         threefish256_init(key, tweak, &ctx);
171         t = stopTimer();
172         cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
173         ultoa((unsigned long)t, str, 10);
174         cli_putstr(str);        
175         
176         startTimer(1);
177         threefish256_enc(data, &ctx);
178         t = stopTimer();
179         cli_putstr_P(PSTR("\r\n\tencrypt time: "));
180         ultoa((unsigned long)t, str, 10);
181         cli_putstr(str);        
182         
183         cli_putstr_P(PSTR("\r\n"));     
184 }
185
186 void testrun_performance_threefish512(void){
187         uint64_t t;
188         char str[16];
189         uint8_t key[THREEFISH512_BLOCKSIZE_B];
190         uint8_t data[THREEFISH512_BLOCKSIZE_B];
191         uint8_t tweak[16];
192         threefish512_ctx_t ctx;
193         
194         cli_putstr_P(PSTR("\r\nThreefish-512 performance:"));
195         
196         calibrateTimer();
197         print_overhead();       
198         
199 //      memset(key,  0, THREEFISH512_BLOCKSIZE_B);
200 //      memset(tweak, 0, 16);
201         
202         startTimer(1);
203         threefish512_init(key, tweak, &ctx);
204         t = stopTimer();
205         cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
206         ultoa((unsigned long)t, str, 10);
207         cli_putstr(str);        
208         
209         startTimer(1);
210         threefish512_enc(data, &ctx);
211         t = stopTimer();
212         cli_putstr_P(PSTR("\r\n\tencrypt time: "));
213         ultoa((unsigned long)t, str, 10);
214         cli_putstr(str);        
215         
216         cli_putstr_P(PSTR("\r\n"));     
217 }
218
219 void testrun_performance_threefish1024(void){
220         uint64_t t;
221         char str[16];
222         uint8_t key[THREEFISH1024_BLOCKSIZE_B];
223         uint8_t data[THREEFISH1024_BLOCKSIZE_B];
224         uint8_t tweak[16];
225         threefish1024_ctx_t ctx;
226         
227         cli_putstr_P(PSTR("\r\nThreefish-1024 performance:"));
228         
229         calibrateTimer();
230         print_overhead();       
231         
232 //      memset(key,  0, THREEFISH1024_BLOCKSIZE_B);
233 //      memset(tweak, 0, 16);
234         
235         startTimer(1);
236         threefish1024_init(key, tweak, &ctx);
237         t = stopTimer();
238         cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
239         ultoa((unsigned long)t, str, 10);
240         cli_putstr(str);        
241         
242         startTimer(1);
243         threefish1024_enc(data, &ctx);
244         t = stopTimer();
245         cli_putstr_P(PSTR("\r\n\tencrypt time: "));
246         ultoa((unsigned long)t, str, 10);
247         cli_putstr(str);        
248         
249         cli_putstr_P(PSTR("\r\n"));     
250 }
251
252 void testrun_performance_threefish(void){
253         testrun_performance_threefish256();
254         testrun_performance_threefish512();
255         testrun_performance_threefish1024();
256 }
257 */
258 /*****************************************************************************
259  *  main                                                                                                                                         *
260  *****************************************************************************/
261
262 const char nessie_str[]      PROGMEM = "nessie";
263 const char test_str[]        PROGMEM = "test";
264 const char performance_str[] PROGMEM = "performance";
265 const char echo_str[]        PROGMEM = "echo";
266
267 cmdlist_entry_t cmdlist[] PROGMEM = {
268 //      { nessie_str,      NULL, testrun_nessie_noekeon},
269         { test_str,        NULL, testrun_stdtest_skein},
270 //      { performance_str, NULL, testrun_performance_threefish},
271         { echo_str,    (void*)1, (void_fpt)echo_ctrl},
272         { NULL,            NULL, NULL}
273 };
274
275 int main (void){
276         DEBUG_INIT();
277         
278         cli_rx = uart_getc;
279         cli_tx = uart_putc;             
280         for(;;){
281                 cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
282                 cli_putstr(algo_name);
283                 cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
284                 cmd_interface(cmdlist);
285         }
286 }