]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-rabbit-test.c
a lot of fixes
[avr-crypto-lib.git] / test_src / main-rabbit-test.c
1 /* main-rabbit-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 "rabbit.h"
27 #include "cli.h"
28 #include "performance_test.h"
29
30 #include "scal_rabbit.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 #include <avr/pgmspace.h>
39
40 const char* algo_name = "Rabbit";
41
42 /*****************************************************************************
43  *  additional validation-functions                                                                                      *
44  *****************************************************************************/
45
46 void test_vector(void* key, void* iv, const void* ref){
47         rabbit_ctx_t ctx;
48         uint8_t fail=0;
49         cli_putstr_P(PSTR("\r\n testing with\r\n key:  "));
50         cli_hexdump(key, 16);
51         cli_putstr_P(PSTR("\r\n iv:   "));
52         if(iv){
53                 cli_hexdump(iv, 8);
54         }else{
55                 cli_putstr_P(PSTR("[no iv]"));
56         }
57         rabbit_init(key, 128, iv, &ctx);
58         rabbit_gen(&ctx);
59         if(!ref || (memcmp_P(ctx.buffer, ref, 16))!=0){
60                 fail = 1;
61                 cli_putstr_P(PSTR("\r\n S[0]: "));
62                 cli_hexdump(ctx.buffer, 16);
63         }
64         ctx.buffer_idx=16;
65         rabbit_gen(&ctx);
66         if(!ref || (memcmp_P(ctx.buffer, ref, 16))!=0){
67                 fail = 1;
68                 cli_putstr_P(PSTR("\r\n S[0]: "));
69                 cli_hexdump(ctx.buffer, 16);
70         }
71         ctx.buffer_idx=16;
72         rabbit_gen(&ctx);
73         if(!ref || (memcmp_P(ctx.buffer, ref, 16))!=0){
74                 fail = 1;
75                 cli_putstr_P(PSTR("\r\n S[0]: "));
76                 cli_hexdump(ctx.buffer, 16);
77         }
78         if(ref){
79                 cli_putstr_P(fail?PSTR("\r\n [fail]"):PSTR("\r\n [ok]"));
80         }
81         cli_putstr_P(PSTR("\r\n"));
82 }
83
84 void nessie_first(void){
85         uint8_t key[16];
86     uint8_t iv[8];
87
88         memset(iv, 0, 8);
89         memset(key, 0, 16);
90         key[0] = 0x80;
91         test_vector(key, iv, NULL);
92         key[0] = 0x00;
93
94         key[15] = 0x80;
95         test_vector(key, iv, NULL);
96
97 }
98
99
100 const uint8_t spec_key1[] PROGMEM = {
101         0x91, 0x28, 0x13, 0x29, 0x2E, /* 0xED */ 0x3D, 0x36, 0xFE,
102         0x3B, 0xFC, 0x62, 0xF1, 0xDC, 0x51, 0xC3, 0xAC
103 };
104
105 const uint8_t spec_key2[] PROGMEM = {
106         0x83, 0x95, 0x74, 0x15, 0x87, 0xE0, 0xC7, 0x33,
107         0xE9, 0xE9, 0xAB, 0x01, 0xC0, 0x9B, 0x00, 0x43
108 };
109
110 const uint8_t spec_iv1[] PROGMEM = {
111         0xC3, 0x73, 0xF5, 0x75, 0xC1, 0x26, 0x7E, 0x59
112 };
113
114 const uint8_t spec_iv2[] PROGMEM = {
115         0xA6, 0xEB, 0x56, 0x1A, 0xD2, 0xF4, 0x17, 0x27
116 };
117
118 const uint8_t spec_ref[] PROGMEM = {
119         0xB1, 0x57, 0x54, 0xF0, 0x36, 0xA5, 0xD6, 0xEC, 0xF5, 0x6B, 0x45, 0x26, 0x1C, 0x4A, 0xF7, 0x02,
120         0x88, 0xE8, 0xD8, 0x15, 0xC5, 0x9C, 0x0C, 0x39, 0x7B, 0x69, 0x6C, 0x47, 0x89, 0xC6, 0x8A, 0xA7,
121         0xF4, 0x16, 0xA1, 0xC3, 0x70, 0x0C, 0xD4, 0x51, 0xDA, 0x68, 0xD1, 0x88, 0x16, 0x73, 0xD6, 0x96,
122
123         0x3D, 0x2D, 0xF3, 0xC8, 0x3E, 0xF6, 0x27, 0xA1, 0xE9, 0x7F, 0xC3, 0x84, 0x87, 0xE2, 0x51, 0x9C,
124         0xF5, 0x76, 0xCD, 0x61, 0xF4, 0x40, 0x5B, 0x88, 0x96, 0xBF, 0x53, 0xAA, 0x85, 0x54, 0xFC, 0x19,
125         0xE5, 0x54, 0x74, 0x73, 0xFB, 0xDB, 0x43, 0x50, 0x8A, 0xE5, 0x3B, 0x20, 0x20, 0x4D, 0x4C, 0x5E,
126
127         0x0C, 0xB1, 0x0D, 0xCD, 0xA0, 0x41, 0xCD, 0xAC, 0x32, 0xEB, 0x5C, 0xFD, 0x02, 0xD0, 0x60, 0x9B,
128         0x95, 0xFC, 0x9F, 0xCA, 0x0F, 0x17, 0x01, 0x5A, 0x7B, 0x70, 0x92, 0x11, 0x4C, 0xFF, 0x3E, 0xAD,
129         0x96, 0x49, 0xE5, 0xDE, 0x8B, 0xFC, 0x7F, 0x3F, 0x92, 0x41, 0x47, 0xAD, 0x3A, 0x94, 0x74, 0x28,
130
131         0xC6, 0xA7, 0x27, 0x5E, 0xF8, 0x54, 0x95, 0xD8, 0x7C, 0xCD, 0x5D, 0x37, 0x67, 0x05, 0xB7, 0xED,
132         0x5F, 0x29, 0xA6, 0xAC, 0x04, 0xF5, 0xEF, 0xD4, 0x7B, 0x8F, 0x29, 0x32, 0x70, 0xDC, 0x4A, 0x8D,
133         0x2A, 0xDE, 0x82, 0x2B, 0x29, 0xDE, 0x6C, 0x1E, 0xE5, 0x2B, 0xDB, 0x8A, 0x47, 0xBF, 0x8F, 0x66,
134
135         0x1F, 0xCD, 0x4E, 0xB9, 0x58, 0x00, 0x12, 0xE2, 0xE0, 0xDC, 0xCC, 0x92, 0x22, 0x01, 0x7D, 0x6D,
136         0xA7, 0x5F, 0x4E, 0x10, 0xD1, 0x21, 0x25, 0x01, 0x7B, 0x24, 0x99, 0xFF, 0xED, 0x93, 0x6F, 0x2E,
137         0xEB, 0xC1, 0x12, 0xC3, 0x93, 0xE7, 0x38, 0x39, 0x23, 0x56, 0xBD, 0xD0, 0x12, 0x02, 0x9B, 0xA7,
138
139         0x44, 0x5A, 0xD8, 0xC8, 0x05, 0x85, 0x8D, 0xBF, 0x70, 0xB6, 0xAF, 0x23, 0xA1, 0x51, 0x10, 0x4D,
140         0x96, 0xC8, 0xF2, 0x79, 0x47, 0xF4, 0x2C, 0x5B, 0xAE, 0xAE, 0x67, 0xC6, 0xAC, 0xC3, 0x5B, 0x03,
141         0x9F, 0xCB, 0xFC, 0x89, 0x5F, 0xA7, 0x1C, 0x17, 0x31, 0x3D, 0xF0, 0x34, 0xF0, 0x15, 0x51, 0xCB
142 };
143
144 void spec_test(void){
145         uint8_t key[16];
146     uint8_t iv[8];
147     const uint8_t *ref;
148     ref = spec_ref;
149
150         memset(key, 0, 16);
151         test_vector(key, NULL, ref);
152         memcpy_P(key, spec_key1, 16);
153         ref += 48;
154         test_vector(key, NULL, ref);
155         memcpy_P(key, spec_key2, 16);
156         ref += 48;
157         test_vector(key, NULL, ref);
158
159
160         memset(key, 0, 16);
161         memset(iv, 0, 8);
162         ref += 48;
163         test_vector(key, iv, ref);
164         memcpy_P(iv,  spec_iv1,   8);
165         ref += 48;
166         test_vector(key, iv, ref);
167         memcpy_P(iv,  spec_iv2,   8);
168         ref += 48;
169         test_vector(key, iv, ref);
170 }
171
172
173 void testrun_nessie_rabbit(void){
174         scal_nessie_set_estream(1);
175         scal_nessie_run(&rabbit_desc);
176 }
177
178
179
180 void testrun_performance_rabbit(void){
181         uint64_t t;
182         char str[16];
183         uint8_t key[16];
184         rabbit_ctx_t ctx;
185
186         calibrateTimer();
187         print_overhead();
188
189         memset(key,  0, 16);
190
191         startTimer(1);
192         rabbit_init(key, 128, NULL, &ctx);
193         t = stopTimer();
194         cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
195         ultoa((unsigned long)t, str, 10);
196         cli_putstr(str);
197
198         startTimer(1);
199         rabbit_gen(&ctx);
200         t = stopTimer();
201         cli_putstr_P(PSTR("\r\n\tencrypt time: "));
202         ultoa((unsigned long)t, str, 10);
203         cli_putstr(str);
204
205         cli_putstr_P(PSTR("\r\n"));
206 }
207
208
209 /*****************************************************************************
210  *  main                                                                                                                                         *
211  *****************************************************************************/
212
213 const char nessie_str[]      PROGMEM = "nessie";
214 const char first_str[]       PROGMEM = "first";
215 const char test_str[]        PROGMEM = "test";
216 const char performance_str[] PROGMEM = "performance";
217 const char echo_str[]        PROGMEM = "echo";
218
219 const cmdlist_entry_t cmdlist[] PROGMEM = {
220         { nessie_str,      NULL, testrun_nessie_rabbit},
221         { performance_str, NULL, testrun_performance_rabbit},
222         { first_str,       NULL, nessie_first},
223         { test_str,        NULL, spec_test},
224         { echo_str,    (void*)1, (void_fpt)echo_ctrl},
225         { NULL,            NULL, NULL}
226 };
227
228 int main (void){
229         DEBUG_INIT();
230
231         cli_rx = (cli_rx_fpt)uart0_getc;
232         cli_tx = (cli_tx_fpt)uart0_putc;
233         for(;;){
234                 cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
235                 cli_putstr(algo_name);
236                 cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
237                 cmd_interface(cmdlist);
238         }
239 }
240