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