]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-salsa20-test.c
first impression of Rabbit
[avr-crypto-lib.git] / test_src / main-salsa20-test.c
1 /* main-salsa20-test.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2006-2011 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
21 #include "config.h"
22
23 #include "uart_i.h"
24 #include "debug.h"
25
26 #include "salsa20.h"
27 #include "cli.h"
28 #include "performance_test.h"
29
30 #include "scal_salsa20.h"
31 #include "scal-basic.h"
32 #include "scal-nessie.h"
33
34 #include <stdlib.h>
35 #include <stdint.h>
36 #include <string.h>
37
38 char* algo_name = "Salsa20";
39
40 /*****************************************************************************
41  *  additional validation-functions                                                                                      *
42  *****************************************************************************/
43
44 void nessie_first(void){
45         salsa20_ctx_t ctx;
46         uint8_t key[16];
47
48         memset(key, 0, 16);
49         key[0] = 0x80;
50         cli_putstr_P(PSTR("\r\n testing with key: "));
51         cli_hexdump(key, 16);
52         salsa20_init(key, 128, NULL, &ctx);
53         cli_putstr_P(PSTR("\r\n internal state: "));
54         cli_hexdump_block(ctx.a, 64, 4, 16);
55         salsa20_gen(&ctx);
56         cli_putstr_P(PSTR("\r\n internal state: "));
57         cli_hexdump_block(ctx.a, 64, 4, 16);
58         cli_putstr_P(PSTR("\r\n data: "));
59         cli_hexdump_block(ctx.buffer, 64, 4, 16);
60
61
62         memset(key, 0, 16);
63         key[15] = 0x01;
64         cli_putstr_P(PSTR("\r\n testing with key: "));
65         cli_hexdump(key, 16);
66         cli_hexdump_block(ctx.a, 64, 4, 16);
67         salsa20_init(key, 128, NULL, &ctx);
68         cli_putstr_P(PSTR("\r\n internal state: "));
69         cli_hexdump_block(ctx.a, 64, 4, 16);
70         salsa20_gen(&ctx);
71         cli_putstr_P(PSTR("\r\n internal state: "));
72         cli_hexdump_block(ctx.a, 64, 4, 16);
73         cli_putstr_P(PSTR("\r\n data: "));
74         cli_hexdump_block(ctx.buffer, 64, 4, 16);
75 }
76
77 /*
78 Salsa20(
79         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
80         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
81         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
82         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
83 =(
84         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
85         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
86         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
87         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
88   ).
89
90
91 Salsa20(
92         211,159, 13,115, 76, 55, 82,183, 3,117,222, 37,191,187,234,136,
93         49,237,179, 48, 1,106,178,219,175,199,166, 48, 86, 16,179,207,
94         31,240, 32, 63, 15, 83, 93,161,116,147, 48,113,238, 55,204, 36,
95         79,201,235, 79, 3, 81,156, 47,203, 26,244,243, 88,118,104, 54)
96 = (
97         109, 42,178,168,156,240,248,238,168,196,190,203, 26,110,170,154,
98         29, 29,150, 26,150, 30,235,249,190,163,251, 48, 69,144, 51, 57,
99         118, 40,152,157,180, 57, 27, 94,107, 42,236, 35, 27,111,114,114,
100         219,236,232,135,111,155,110, 18, 24,232, 95,158,179, 19, 48,202
101   ).
102
103 Salsa20(
104         88,118,104, 54, 79,201,235, 79, 3, 81,156, 47,203, 26,244,243,
105         191,187,234,136,211,159, 13,115, 76, 55, 82,183, 3,117,222, 37,
106         86, 16,179,207, 49,237,179, 48, 1,106,178,219,175,199,166, 48,
107         238, 55,204, 36, 31,240, 32, 63, 15, 83, 93,161,116,147, 48,113)
108 = (
109         179, 19, 48,202,219,236,232,135,111,155,110, 18, 24,232, 95,158,
110         26,110,170,154,109, 42,178,168,156,240,248,238,168,196,190,203,
111         69,144, 51, 57, 29, 29,150, 26,150, 30,235,249,190,163,251, 48,
112         27,111,114,114,118, 40,152,157,180, 57, 27, 94,107, 42,236, 35
113   ).
114
115 Salsa20^1000000 (
116         6,124, 83,146, 38,191, 9, 50, 4,161, 47,222,122,182,223,185,
117         75, 27, 0,216, 16,122, 7, 89,162,104,101,147,213, 21, 54, 95,
118         225,253,139,176,105,132, 23,116, 76, 41,176,207,221, 34,157,108,
119         94, 94, 99, 52, 90,117, 91,220,146,190,239,143,196,176,130,186)
120 =(
121         8, 18, 38,199,119, 76,215, 67,173,127,144,162,103,212,176,217,
122         192, 19,233, 33,159,197,154,160,128,243,219, 65,171,136,135,225,
123         123, 11, 68, 86,237, 82, 20,155,133,189, 9, 83,167,116,194, 78,
124         122,127,195,185,185,204,188, 90,245, 9,183,248,226, 85,245,104
125  ).
126  */
127
128 uint8_t Salsa20_spectest0_in[] PROGMEM = {
129         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
130         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
131         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
132         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
133
134 uint8_t Salsa20_spectest0_ref[] PROGMEM = {
135         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
136         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
137         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
138         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
139
140
141 uint8_t Salsa20_spectest1_in[] PROGMEM = {
142         211,159, 13,115, 76, 55, 82,183, 3,117,222, 37,191,187,234,136,
143         49,237,179, 48, 1,106,178,219,175,199,166, 48, 86, 16,179,207,
144         31,240, 32, 63, 15, 83, 93,161,116,147, 48,113,238, 55,204, 36,
145         79,201,235, 79, 3, 81,156, 47,203, 26,244,243, 88,118,104, 54 };
146
147 uint8_t Salsa20_spectest1_ref[] PROGMEM = {
148         109, 42,178,168,156,240,248,238,168,196,190,203, 26,110,170,154,
149         29, 29,150, 26,150, 30,235,249,190,163,251, 48, 69,144, 51, 57,
150         118, 40,152,157,180, 57, 27, 94,107, 42,236, 35, 27,111,114,114,
151         219,236,232,135,111,155,110, 18, 24,232, 95,158,179, 19, 48,202 };
152
153 uint8_t Salsa20_spectest2_in[] PROGMEM = {
154         88,118,104, 54, 79,201,235, 79, 3, 81,156, 47,203, 26,244,243,
155         191,187,234,136,211,159, 13,115, 76, 55, 82,183, 3,117,222, 37,
156         86, 16,179,207, 49,237,179, 48, 1,106,178,219,175,199,166, 48,
157         238, 55,204, 36, 31,240, 32, 63, 15, 83, 93,161,116,147, 48,113 };
158
159 uint8_t Salsa20_spectest2_ref[] PROGMEM = {
160         179, 19, 48,202,219,236,232,135,111,155,110, 18, 24,232, 95,158,
161         26,110,170,154,109, 42,178,168,156,240,248,238,168,196,190,203,
162         69,144, 51, 57, 29, 29,150, 26,150, 30,235,249,190,163,251, 48,
163         27,111,114,114,118, 40,152,157,180, 57, 27, 94,107, 42,236, 35 };
164
165 uint8_t Salsa20_spectest3_in[] PROGMEM = {
166         6,124, 83,146, 38,191, 9, 50, 4,161, 47,222,122,182,223,185,
167         75, 27, 0,216, 16,122, 7, 89,162,104,101,147,213, 21, 54, 95,
168         225,253,139,176,105,132, 23,116, 76, 41,176,207,221, 34,157,108,
169         94, 94, 99, 52, 90,117, 91,220,146,190,239,143,196,176,130,186 };
170
171 uint8_t Salsa20_spectest3_ref[] PROGMEM = {
172         8, 18, 38,199,119, 76,215, 67,173,127,144,162,103,212,176,217,
173         192, 19,233, 33,159,197,154,160,128,243,219, 65,171,136,135,225,
174         123, 11, 68, 86,237, 82, 20,155,133,189, 9, 83,167,116,194, 78,
175         122,127,195,185,185,204,188, 90,245, 9,183,248,226, 85,245,104 };
176
177
178 void spec_test(void){
179         uint8_t buffer[64];
180         nessie_first();
181         cli_putstr_P(PSTR("\r\ntesting with vectors from sepcification"));
182         cli_putstr_P(PSTR("\r\ntest 0: "));
183         memcpy_P(buffer, Salsa20_spectest0_in, 64);
184         salsa20_hash((uint32_t*)buffer);
185         if(memcmp_P(buffer, Salsa20_spectest0_ref, 64)){
186                 cli_putstr_P(PSTR(" [fail]"));
187         }else{
188                 cli_putstr_P(PSTR(" [ok]"));
189         }
190
191         cli_putstr_P(PSTR("\r\ntest 1: "));
192         memcpy_P(buffer, Salsa20_spectest1_in, 64);
193         salsa20_hash((uint32_t*)buffer);
194         if(memcmp_P(buffer, Salsa20_spectest1_ref, 64)){
195                 cli_putstr_P(PSTR(" [fail]"));
196         }else{
197                 cli_putstr_P(PSTR(" [ok]"));
198         }
199
200         cli_putstr_P(PSTR("\r\ntest 2: "));
201         memcpy_P(buffer, Salsa20_spectest2_in, 64);
202         salsa20_hash((uint32_t*)buffer);
203         if(memcmp_P(buffer, Salsa20_spectest2_ref, 64)){
204                 cli_putstr_P(PSTR(" [fail]"));
205         }else{
206                 cli_putstr_P(PSTR(" [ok]"));
207         }
208
209
210
211         uint32_t count=0;
212         cli_putstr_P(PSTR("\r\ntest 3: "));
213         memcpy_P(buffer, Salsa20_spectest3_in, 64);
214         do{
215                 if((count&0xFFFF)==0){
216                         cli_putc('.');
217                         if((count&0x20FFFF)==0){
218                                 cli_putc('\r'); cli_putc('\n');
219                         }
220                 }
221                 salsa20_hash((uint32_t*)buffer);
222         }while(++count<1000000);
223         if(memcmp_P(buffer, Salsa20_spectest0_ref, 64)){
224                 cli_putstr_P(PSTR(" [fail]"));
225         }else{
226                 cli_putstr_P(PSTR(" [ok]"));
227         }
228 }
229
230 void testrun_nessie_salsa20(void){
231         scal_nessie_set_estream(1);
232         scal_nessie_run(&salsa20_desc);
233 }
234
235 void testrun_performance_salsa20(void){
236         uint64_t t;
237         char str[16];
238         uint8_t key[32];
239         salsa20_ctx_t ctx;
240
241         calibrateTimer();
242         print_overhead();
243
244         memset(key,  0, 16);
245
246         startTimer(1);
247         salsa20_init(key, 128, NULL, &ctx);
248         t = stopTimer();
249         cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
250         ultoa((unsigned long)t, str, 10);
251         cli_putstr(str);
252
253         startTimer(1);
254         salsa20_gen(&ctx);
255         t = stopTimer();
256         cli_putstr_P(PSTR("\r\n\tencrypt time: "));
257         ultoa((unsigned long)t, str, 10);
258         cli_putstr(str);
259
260         cli_putstr_P(PSTR("\r\n"));
261 }
262
263
264 /*****************************************************************************
265  *  main                                                                                                                                         *
266  *****************************************************************************/
267
268 const char nessie_str[]      PROGMEM = "nessie";
269 const char test_str[]        PROGMEM = "test";
270 const char performance_str[] PROGMEM = "performance";
271 const char echo_str[]        PROGMEM = "echo";
272
273 cmdlist_entry_t cmdlist[] PROGMEM = {
274         { nessie_str,      NULL, testrun_nessie_salsa20 },
275         { performance_str, NULL, testrun_performance_salsa20},
276         { test_str,        NULL, spec_test},
277         { echo_str,    (void*)1, (void_fpt)echo_ctrl},
278         { NULL,            NULL, NULL}
279 };
280
281 int main (void){
282         DEBUG_INIT();
283
284         cli_rx = (cli_rx_fpt)uart0_getc;
285         cli_tx = (cli_tx_fpt)uart0_putc;
286         for(;;){
287                 cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
288                 cli_putstr(algo_name);
289                 cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
290                 cmd_interface(cmdlist);
291         }
292 }
293