]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-rabbit-test.c
first impression of Rabbit
[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 char* algo_name = "Rabbit";
39
40 /*****************************************************************************
41  *  additional validation-functions                                                                                      *
42  *****************************************************************************/
43
44 void test_vector(void* key, void* iv){
45         rabbit_ctx_t ctx;
46         cli_putstr_P(PSTR("\r\n testing with key: "));
47         cli_hexdump(key, 16);
48         cli_putstr_P(PSTR("\r\n              iv:  "));
49         if(iv){
50                 cli_hexdump(iv, 8);
51         }else{
52                 cli_putstr_P(PSTR("[no iv]"));
53         }
54         rabbit_init(key, 128, iv, &ctx);
55         cli_putstr_P(PSTR("\r\n S[0]: "));
56         rabbit_gen(&ctx);
57         cli_hexdump(ctx.buffer, 16);
58         cli_putstr_P(PSTR("\r\n S[1]: "));
59         ctx.buffer_idx=16;
60         rabbit_gen(&ctx);
61         cli_hexdump(ctx.buffer, 16);
62         cli_putstr_P(PSTR("\r\n S[2]: "));
63         ctx.buffer_idx=16;
64         rabbit_gen(&ctx);
65         cli_hexdump(ctx.buffer, 16);
66 //      dump_ctx(&ctx);
67         ctx.buffer_idx=16;
68         rabbit_gen(&ctx);
69 //      dump_ctx(&ctx);
70         cli_putstr_P(PSTR("\r\n S[3]: "));
71         cli_hexdump(ctx.buffer, 16);
72         ctx.buffer_idx=16;
73         rabbit_gen(&ctx);
74         cli_putstr_P(PSTR("\r\n S[4]: "));
75         cli_hexdump(ctx.buffer, 16);
76         cli_putstr_P(PSTR("\r\n"));
77 }
78
79 void nessie_first(void){
80         uint8_t key[16];
81     uint8_t iv[8];
82
83         memset(iv, 0, 8);
84         memset(key, 0, 16);
85         key[0] = 0x80;
86         test_vector(key, iv);
87         key[0] = 0x00;
88
89         key[15] = 0x80;
90         test_vector(key, iv);
91
92 }
93
94
95 const uint8_t spec_key1[] PROGMEM = {
96         0x91, 0x28, 0x13, 0x29, 0x2E, /* 0xED */ 0x3D, 0x36, 0xFE,
97         0x3B, 0xFC, 0x62, 0xF1, 0xDC, 0x51, 0xC3, 0xAC
98 };
99
100 const uint8_t spec_key2[] PROGMEM = {
101         0x83, 0x95, 0x74, 0x15, 0x87, 0xE0, 0xC7, 0x33,
102         0xE9, 0xE9, 0xAB, 0x01, 0xC0, 0x9B, 0x00, 0x43
103 };
104
105 const uint8_t spec_iv1[] PROGMEM = {
106         0xC3, 0x73, 0xF5, 0x75, 0xC1, 0x26, 0x7E, 0x59
107 };
108
109 const uint8_t spec_iv2[] PROGMEM = {
110         0xA6, 0xEB, 0x56, 0x1A, 0xD2, 0xF4, 0x17, 0x27
111 };
112
113 void spec_test(void){
114         uint8_t key[16];
115     uint8_t iv[8];
116
117         memset(key, 0, 16);
118         test_vector(key, NULL);
119         memcpy_P(key, spec_key1, 16);
120         test_vector(key, NULL);
121         memcpy_P(key, spec_key2, 16);
122         test_vector(key, NULL);
123
124
125         memset(key, 0, 16);
126         memset(iv, 0, 8);
127         test_vector(key, iv);
128         memcpy_P(iv,  spec_iv1,   8);
129         test_vector(key, iv);
130         memcpy_P(iv,  spec_iv2,   8);
131         test_vector(key, iv);
132 }
133
134
135 void testrun_nessie_rabbit(void){
136         scal_nessie_set_estream(1);
137         scal_nessie_run(&rabbit_desc);
138 }
139
140
141
142 void testrun_performance_rabbit(void){
143         uint64_t t;
144         char str[16];
145         uint8_t key[16];
146         rabbit_ctx_t ctx;
147
148         calibrateTimer();
149         print_overhead();
150
151         memset(key,  0, 16);
152
153         startTimer(1);
154         rabbit_init(key, 128, NULL, &ctx);
155         t = stopTimer();
156         cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
157         ultoa((unsigned long)t, str, 10);
158         cli_putstr(str);
159
160         startTimer(1);
161         rabbit_gen(&ctx);
162         t = stopTimer();
163         cli_putstr_P(PSTR("\r\n\tencrypt time: "));
164         ultoa((unsigned long)t, str, 10);
165         cli_putstr(str);
166
167         cli_putstr_P(PSTR("\r\n"));
168 }
169
170
171 /*****************************************************************************
172  *  main                                                                                                                                         *
173  *****************************************************************************/
174
175 const char nessie_str[]      PROGMEM = "nessie";
176 const char first_str[]       PROGMEM = "first";
177 const char test_str[]        PROGMEM = "test";
178 const char performance_str[] PROGMEM = "performance";
179 const char echo_str[]        PROGMEM = "echo";
180
181 cmdlist_entry_t cmdlist[] PROGMEM = {
182         { nessie_str,      NULL, testrun_nessie_rabbit},
183         { performance_str, NULL, testrun_performance_rabbit},
184         { first_str,       NULL, nessie_first},
185         { test_str,        NULL, spec_test},
186         { echo_str,    (void*)1, (void_fpt)echo_ctrl},
187         { NULL,            NULL, NULL}
188 };
189
190 int main (void){
191         DEBUG_INIT();
192
193         cli_rx = (cli_rx_fpt)uart0_getc;
194         cli_tx = (cli_tx_fpt)uart0_putc;
195         for(;;){
196                 cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
197                 cli_putstr(algo_name);
198                 cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
199                 cmd_interface(cmdlist);
200         }
201 }
202