]> git.cryptolib.org Git - avr-crypto-lib.git/blob - main-noekeon-test.c
insereated GPLv3 stub
[avr-crypto-lib.git] / main-noekeon-test.c
1 /* main-noekeon-test.c */
2 /*
3     This file is part of the Crypto-avr-lib/microcrypt-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* cipher_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(cipher_name)+10];
49         strcpy(str, cipher_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(cipher_name)+10];
69         strcpy(str, cipher_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_stdtest_rundirect(void* data, void* key){
84         uart_putstr_P(PSTR("\r\n                     "));
85         uart_putstr_P(PSTR("k = "));
86         uart_hexdump(key,16);
87         
88         uart_putstr_P(PSTR("\r\n                     "));
89         uart_putstr_P(PSTR("a = "));
90         uart_hexdump(data,16);
91         
92         noekeon_enc(data, key);
93         uart_putstr_P(PSTR("\r\nafter NESSIEencrypt, b = "));
94         uart_hexdump(data,16);
95         
96         noekeon_dec(data, key);
97         uart_putstr_P(PSTR("\r\nafter NESSIEdecrypt, a?= "));
98         uart_hexdump(data,16);
99         uart_putstr_P(PSTR("\r\n"));
100 }
101
102 void testrun_stdtest_runindirect(void* data, void* key){
103         noekeon_ctx_t ctx;
104         uart_putstr_P(PSTR("\r\n                     "));
105         uart_putstr_P(PSTR("k = "));
106         uart_hexdump(key,16);
107         
108         uart_putstr_P(PSTR("\r\n                     "));
109         uart_putstr_P(PSTR("a = "));
110         uart_hexdump(data,16);
111         noekeon_init(key, &ctx);
112         noekeon_enc(data, &ctx);
113         uart_putstr_P(PSTR("\r\nafter NESSIEencrypt, b = "));
114         uart_hexdump(data,16);
115         
116         noekeon_dec(data, &ctx);
117         uart_putstr_P(PSTR("\r\nafter NESSIEdecrypt, a?= "));
118         uart_hexdump(data,16);
119         uart_putstr_P(PSTR("\r\n"));
120 }
121
122 void testrun_stdtest_noekeon(void){
123         uint8_t key[16], data[16];
124         uint8_t key3[16];
125         noekeon_ctx_t ctx;
126         
127         uart_putstr_P(PSTR("\r\nTest vectors for block cipher Noekeon in Indirect-Key Mode:\r\n"));
128         
129         memset(key,  0, 16);
130         memset(data, 0, 16);
131         testrun_stdtest_runindirect(data, key);
132         
133         memset(key,  0xFF, 16);
134         memset(data, 0xFF, 16);
135         testrun_stdtest_runindirect(data, key);
136         
137         memset(key,  0, 16);
138         memset(data, 0, 16);
139         noekeon_init(key, &ctx);
140         noekeon_enc(data, &ctx);
141         memcpy(key3, data, 16);
142         memset(key,  0xFF, 16);
143         memset(data, 0xFF, 16);
144         noekeon_init(key, &ctx);
145         noekeon_enc(data, &ctx);
146         testrun_stdtest_runindirect(data, key3);
147         
148         uart_putstr_P(PSTR("\r\nTest vectors for block cipher Noekeon in Direct-Key Mode:\r\n"));
149         
150         memset(key,  0, 16);
151         memset(data, 0, 16);
152         testrun_stdtest_rundirect(data, key);
153         
154         memset(key,  0xFF, 16);
155         memset(data, 0xFF, 16);
156         testrun_stdtest_rundirect(data, key);
157         
158         memset(key,  0, 16);
159         memset(data, 0, 16);
160         noekeon_enc(data, key);
161         memcpy(key3, data, 16);
162         memset(key,  0xFF, 16);
163         memset(data, 0xFF, 16);
164         noekeon_enc(data, key);
165         testrun_stdtest_rundirect(data, key3);
166         
167 }
168
169 void testrun_performance_noekeon(void){
170         uint16_t i,c;
171         uint64_t t;
172         char str[16];
173         uint8_t key[16], data[16];
174         noekeon_ctx_t ctx;
175         
176         calibrateTimer();
177         getOverhead(&c, &i);
178         uart_putstr_P(PSTR("\r\n\r\n=== benchmark ==="));
179         utoa(c, str, 10);
180         uart_putstr_P(PSTR("\r\n\tconst overhead:     "));
181         uart_putstr(str);
182         utoa(i, str, 10);
183         uart_putstr_P(PSTR("\r\n\tinterrupt overhead: "));
184         uart_putstr(str);
185         
186         memset(key,  0, 16);
187         memset(data, 0, 16);
188         
189         startTimer(1);
190         noekeon_init(key, &ctx);
191         t = stopTimer();
192         uart_putstr_P(PSTR("\r\n\tctx-gen time: "));
193         ultoa((unsigned long)t, str, 10);
194         uart_putstr(str);       
195         
196         startTimer(1);
197         noekeon_enc(data, &ctx);
198         t = stopTimer();
199         uart_putstr_P(PSTR("\r\n\tencrypt time: "));
200         ultoa((unsigned long)t, str, 10);
201         uart_putstr(str);       
202         
203         startTimer(1);
204         noekeon_dec(data, &ctx);
205         t = stopTimer();
206         uart_putstr_P(PSTR("\r\n\tdecrypt time: "));
207         ultoa((unsigned long)t, str, 10);
208         uart_putstr(str);
209         
210         uart_putstr_P(PSTR("\r\n"));
211 }
212 /*****************************************************************************
213  *  main                                                                                                                                         *
214  *****************************************************************************/
215
216 int main (void){
217         char  str[20];
218         DEBUG_INIT();
219         uart_putstr("\r\n");
220
221         uart_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
222         uart_putstr(cipher_name);
223         uart_putstr_P(PSTR(")\r\nloaded and running\r\n"));
224
225         PGM_P    u   = PSTR("nessie\0test\0direct\0indirect\0performance\0");
226         void_fpt v[] = {testrun_nessie_noekeon_direct, 
227                             testrun_stdtest_noekeon,
228                             testrun_nessie_noekeon_direct, 
229                             testrun_nessie_noekeon_indirect,  
230                             testrun_performance_noekeon};
231
232         while(1){ 
233                 if (!getnextwordn(str,20)){DEBUG_S("DBG: W1\r\n"); goto error;}
234                 if(execcommand_d0_P(str, u, v)<0){
235                         uart_putstr_P(PSTR("\r\nunknown command\r\n"));
236                 }
237                 continue;
238         error:
239                 uart_putstr("ERROR\r\n");
240         }
241         
242 }
243