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