]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-noekeon-test.c
new cli system
[avr-crypto-lib.git] / test_src / main-noekeon-test.c
1 /* main-noekeon-test.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2008  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  * serpent test-suit
21  * 
22 */
23
24 #include "config.h"
25 #include "serial-tools.h"
26 #include "uart.h"
27 #include "debug.h"
28
29 #include <noekeon.h>
30 #include "nessie_bc_test.h"
31 #include "cli.h"
32 #include "performance_test.h"
33
34 #include <stdint.h>
35 #include <string.h>
36 #include <stdlib.h>
37
38 char* algo_name = "Noekeon";
39
40 /*****************************************************************************
41  *  additional validation-functions                                                                                      *
42  *****************************************************************************/
43 void noekeon_genctx_dummy(uint8_t* key, uint16_t keysize, void* ctx){
44         noekeon_init(key, ctx);
45 }
46
47 void testrun_nessie_noekeon_indirect(void){
48         char str[strlen(algo_name)+10];
49         strcpy(str, algo_name);
50         strcat(str, "-indirect");
51         
52         nessie_bc_ctx.blocksize_B =  16;
53         nessie_bc_ctx.keysize_b   = 128;
54         nessie_bc_ctx.name        = str;
55         nessie_bc_ctx.ctx_size_B  = sizeof(noekeon_ctx_t);
56         nessie_bc_ctx.cipher_enc  = (nessie_bc_enc_fpt)noekeon_enc;
57         nessie_bc_ctx.cipher_dec  = (nessie_bc_dec_fpt)noekeon_dec;
58         nessie_bc_ctx.cipher_genctx  = (nessie_bc_gen_fpt)noekeon_genctx_dummy;
59         
60         nessie_bc_run();
61 }
62
63 void noekeon_genctx_dummy_direct(uint8_t* key, uint16_t keysize, void* ctx){
64         memcpy(ctx, key, 16);
65 }
66
67 void testrun_nessie_noekeon_direct(void){
68         char str[strlen(algo_name)+10];
69         strcpy(str, algo_name);
70         strcat(str, "-Direct");
71         
72         nessie_bc_ctx.blocksize_B =  16;
73         nessie_bc_ctx.keysize_b   = 128;
74         nessie_bc_ctx.name        = str;
75         nessie_bc_ctx.ctx_size_B  = sizeof(noekeon_ctx_t);
76         nessie_bc_ctx.cipher_enc  = (nessie_bc_enc_fpt)noekeon_enc;
77         nessie_bc_ctx.cipher_dec  = (nessie_bc_dec_fpt)noekeon_dec;
78         nessie_bc_ctx.cipher_genctx  = (nessie_bc_gen_fpt)noekeon_genctx_dummy_direct;
79         
80         nessie_bc_run();
81 }
82
83 void testrun_nessie_noekeon(void){
84         testrun_nessie_noekeon_direct();
85         testrun_nessie_noekeon_indirect();
86 }
87
88
89 void testrun_stdtest_rundirect(void* data, void* key){
90         uart_putstr_P(PSTR("\r\n                     "));
91         uart_putstr_P(PSTR("k = "));
92         uart_hexdump(key,16);
93         
94         uart_putstr_P(PSTR("\r\n                     "));
95         uart_putstr_P(PSTR("a = "));
96         uart_hexdump(data,16);
97         
98         noekeon_enc(data, key);
99         uart_putstr_P(PSTR("\r\nafter NESSIEencrypt, b = "));
100         uart_hexdump(data,16);
101         
102         noekeon_dec(data, key);
103         uart_putstr_P(PSTR("\r\nafter NESSIEdecrypt, a?= "));
104         uart_hexdump(data,16);
105         uart_putstr_P(PSTR("\r\n"));
106 }
107
108 void testrun_stdtest_runindirect(void* data, void* key){
109         noekeon_ctx_t ctx;
110         uart_putstr_P(PSTR("\r\n                     "));
111         uart_putstr_P(PSTR("k = "));
112         uart_hexdump(key,16);
113         
114         uart_putstr_P(PSTR("\r\n                     "));
115         uart_putstr_P(PSTR("a = "));
116         uart_hexdump(data,16);
117         noekeon_init(key, &ctx);
118         noekeon_enc(data, &ctx);
119         uart_putstr_P(PSTR("\r\nafter NESSIEencrypt, b = "));
120         uart_hexdump(data,16);
121         
122         noekeon_dec(data, &ctx);
123         uart_putstr_P(PSTR("\r\nafter NESSIEdecrypt, a?= "));
124         uart_hexdump(data,16);
125         uart_putstr_P(PSTR("\r\n"));
126 }
127
128 void testrun_stdtest_noekeon(void){
129         uint8_t key[16], data[16];
130         uint8_t key3[16];
131         noekeon_ctx_t ctx;
132         
133         uart_putstr_P(PSTR("\r\nTest vectors for block cipher Noekeon in Indirect-Key Mode:\r\n"));
134         
135         memset(key,  0, 16);
136         memset(data, 0, 16);
137         testrun_stdtest_runindirect(data, key);
138         
139         memset(key,  0xFF, 16);
140         memset(data, 0xFF, 16);
141         testrun_stdtest_runindirect(data, key);
142         
143         memset(key,  0, 16);
144         memset(data, 0, 16);
145         noekeon_init(key, &ctx);
146         noekeon_enc(data, &ctx);
147         memcpy(key3, data, 16);
148         memset(key,  0xFF, 16);
149         memset(data, 0xFF, 16);
150         noekeon_init(key, &ctx);
151         noekeon_enc(data, &ctx);
152         testrun_stdtest_runindirect(data, key3);
153         
154         uart_putstr_P(PSTR("\r\nTest vectors for block cipher Noekeon in Direct-Key Mode:\r\n"));
155         
156         memset(key,  0, 16);
157         memset(data, 0, 16);
158         testrun_stdtest_rundirect(data, key);
159         
160         memset(key,  0xFF, 16);
161         memset(data, 0xFF, 16);
162         testrun_stdtest_rundirect(data, key);
163         
164         memset(key,  0, 16);
165         memset(data, 0, 16);
166         noekeon_enc(data, key);
167         memcpy(key3, data, 16);
168         memset(key,  0xFF, 16);
169         memset(data, 0xFF, 16);
170         noekeon_enc(data, key);
171         testrun_stdtest_rundirect(data, key3);
172         
173 }
174
175 void testrun_performance_noekeon(void){
176         uint64_t t;
177         char str[16];
178         uint8_t key[16], data[16];
179         noekeon_ctx_t ctx;
180         
181         calibrateTimer();
182         print_overhead();
183         
184         memset(key,  0, 16);
185         memset(data, 0, 16);
186         
187         startTimer(1);
188         noekeon_init(key, &ctx);
189         t = stopTimer();
190         uart_putstr_P(PSTR("\r\n\tctx-gen time: "));
191         ultoa((unsigned long)t, str, 10);
192         uart_putstr(str);       
193         
194         startTimer(1);
195         noekeon_enc(data, &ctx);
196         t = stopTimer();
197         uart_putstr_P(PSTR("\r\n\tencrypt time: "));
198         ultoa((unsigned long)t, str, 10);
199         uart_putstr(str);       
200         
201         startTimer(1);
202         noekeon_dec(data, &ctx);
203         t = stopTimer();
204         uart_putstr_P(PSTR("\r\n\tdecrypt time: "));
205         ultoa((unsigned long)t, str, 10);
206         uart_putstr(str);
207         
208         uart_putstr_P(PSTR("\r\n"));
209 }
210 /*****************************************************************************
211  *  main                                                                                                                                         *
212  *****************************************************************************/
213
214 const char nessie_str[]      PROGMEM = "nessie";
215 const char test_str[]        PROGMEM = "test";
216 const char direct_str[]      PROGMEM = "direct";
217 const char indirect_str[]    PROGMEM = "indirect";
218 const char performance_str[] PROGMEM = "performance";
219 const char echo_str[]        PROGMEM = "echo";
220
221 cmdlist_entry_t cmdlist[] PROGMEM = {
222         { nessie_str,      NULL, testrun_nessie_noekeon},
223         { test_str,        NULL, testrun_stdtest_noekeon},
224         { direct_str,      NULL, testrun_nessie_noekeon_direct},
225         { indirect_str,    NULL, testrun_nessie_noekeon_indirect},
226         { performance_str, NULL, testrun_performance_noekeon},
227         { echo_str,    (void*)1, (void_fpt)echo_ctrl},
228         { NULL,            NULL, NULL}
229 };
230
231 int main (void){
232         DEBUG_INIT();
233         uart_putstr("\r\n");
234         cli_rx = uart_getc;
235         cli_tx = uart_putc;             
236         for(;;){
237                 uart_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
238                 uart_putstr(algo_name);
239                 uart_putstr_P(PSTR(")\r\nloaded and running\r\n"));
240                 cmd_interface(cmdlist);
241         }
242 }