]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-aes-test.c
new cli system
[avr-crypto-lib.git] / test_src / main-aes-test.c
1 /* main-aes-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  * AES 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 "aes.h"
30 #include "aes128_enc.h"
31 #include "aes128_dec.h"
32 #include "aes192_enc.h"
33 #include "aes192_dec.h"
34 #include "aes256_enc.h"
35 #include "aes256_dec.h"
36 #include "aes_keyschedule.h"
37
38 #include "nessie_bc_test.h"
39 #include "cli.h"
40 #include "performance_test.h"
41
42 #include <stdint.h>
43 #include <string.h>
44 #include <stdlib.h>
45 #include <avr/pgmspace.h>
46
47 char* algo_name = "AES";
48
49 /*****************************************************************************
50  *  additional validation-functions                                                                                      *
51  *****************************************************************************/
52
53 void testrun_nessie_aes(void){
54         nessie_bc_ctx.blocksize_B =  16;
55         nessie_bc_ctx.keysize_b   = 128;
56         nessie_bc_ctx.name        = algo_name;
57         nessie_bc_ctx.ctx_size_B  = sizeof(aes128_ctx_t);
58         nessie_bc_ctx.cipher_enc  = (nessie_bc_enc_fpt)aes128_enc;
59         nessie_bc_ctx.cipher_dec  = (nessie_bc_dec_fpt)aes128_dec;
60         nessie_bc_ctx.cipher_genctx  = (nessie_bc_gen_fpt)aes_init;
61         nessie_bc_run();
62         
63         nessie_bc_ctx.keysize_b   = 192;
64         nessie_bc_ctx.ctx_size_B  = sizeof(aes192_ctx_t);
65         nessie_bc_ctx.cipher_enc  = (nessie_bc_enc_fpt)aes192_enc;
66         nessie_bc_ctx.cipher_dec  = (nessie_bc_dec_fpt)aes192_dec;
67         nessie_bc_run();
68         
69         nessie_bc_ctx.keysize_b   = 256;
70         nessie_bc_ctx.ctx_size_B  = sizeof(aes256_ctx_t);
71         nessie_bc_ctx.cipher_enc  = (nessie_bc_enc_fpt)aes256_enc;
72         nessie_bc_ctx.cipher_dec  = (nessie_bc_dec_fpt)aes256_dec;
73         nessie_bc_run(); 
74 }
75
76 void testrun_test_aes(void){
77         uint8_t key[16] = { 0x2b, 0x7e, 0x15, 0x16, 
78                             0x28, 0xae, 0xd2, 0xa6,
79                             0xab, 0xf7, 0x15, 0x88,
80                             0x09, 0xcf, 0x4f, 0x3c };
81         uint8_t data[16] = { 0x32, 0x43, 0xf6, 0xa8,
82                              0x88, 0x5a, 0x30, 0x8d, 
83                              0x31, 0x31, 0x98, 0xa2, 
84                              0xe0, 0x37, 0x07, 0x34 };
85         aes128_ctx_t ctx;
86         aes128_init(key, &ctx);
87         uart_putstr_P(PSTR("\r\n\r\n cipher test (FIPS 197):\r\n key:        "));
88         uart_hexdump(key, 16);
89         uart_putstr_P(PSTR("\r\n plaintext:  "));
90         uart_hexdump(data, 16);
91         aes128_enc(data, &ctx);
92         uart_putstr_P(PSTR("\r\n ciphertext: "));
93         uart_hexdump(data, 16);
94         aes128_dec(data, &ctx);
95         uart_putstr_P(PSTR("\r\n plaintext:  "));
96         uart_hexdump(data, 16);
97         
98         
99 }
100
101 void testrun_testkey_aes128(void){
102         uint8_t key[16] = { 0x2b, 0x7e, 0x15, 0x16, 
103                             0x28, 0xae, 0xd2, 0xa6,
104                             0xab, 0xf7, 0x15, 0x88,
105                             0x09, 0xcf, 0x4f, 0x3c};
106         aes128_ctx_t ctx;
107         uint8_t i;
108         aes128_init(key, &ctx);
109         uart_putstr_P(PSTR("\r\n\r\n keyschedule test (FIPS 197):\r\n key:   "));
110         uart_hexdump(key, 16);
111         for(i=0; i<11; ++i){
112                 uart_putstr_P(PSTR("\r\n index: "));
113                 uart_putc('0'+i/10);
114                 uart_putc('0'+i%10);
115                 uart_putstr_P(PSTR(" roundkey "));
116                 uart_hexdump(ctx.key[i].ks, 16);
117         }
118 }
119
120 void testrun_testkey_aes192(void){
121         uint8_t key[24] = { 0x8e, 0x73, 0xb0, 0xf7, 
122                             0xda, 0x0e, 0x64, 0x52,
123                             0xc8, 0x10, 0xf3, 0x2b, 
124                             0x80, 0x90, 0x79, 0xe5, 
125                             0x62, 0xf8, 0xea, 0xd2, 
126                             0x52, 0x2c, 0x6b, 0x7b};
127         aes192_ctx_t ctx;
128         uint8_t i;
129         memset(&ctx, 0, sizeof(aes192_ctx_t));
130         aes192_init(key, &ctx);
131         uart_putstr_P(PSTR("\r\n\r\n keyschedule test (FIPS 197):\r\n key:   "));
132         uart_hexdump(key, 24);
133         for(i=0; i<13; ++i){
134                 uart_putstr_P(PSTR("\r\n index: "));
135                 uart_putc('0'+i/10);
136                 uart_putc('0'+i%10);
137                 uart_putstr_P(PSTR(" roundkey "));
138                 uart_hexdump(ctx.key[i].ks, 16);
139         }
140 }
141
142
143 void testrun_testkey_aes256(void){
144         uint8_t key[32] = { 0x60, 0x3d, 0xeb, 0x10, 
145                             0x15, 0xca, 0x71, 0xbe, 
146                             0x2b, 0x73, 0xae, 0xf0, 
147                             0x85, 0x7d, 0x77, 0x81, 
148                             0x1f, 0x35, 0x2c, 0x07, 
149                             0x3b, 0x61, 0x08, 0xd7, 
150                             0x2d, 0x98, 0x10, 0xa3, 
151                             0x09, 0x14, 0xdf, 0xf4};
152         aes256_ctx_t ctx;
153         uint8_t i;
154         memset(&ctx, 0, sizeof(aes256_ctx_t));
155         aes256_init(key, &ctx);
156         uart_putstr_P(PSTR("\r\n\r\n keyschedule test (FIPS 197):\r\n key:   "));
157         uart_hexdump(key, 32);
158         for(i=0; i<15; ++i){
159                 uart_putstr_P(PSTR("\r\n index: "));
160                 uart_putc('0'+i/10);
161                 uart_putc('0'+i%10);
162                 uart_putstr_P(PSTR(" roundkey "));
163                 uart_hexdump(ctx.key[i].ks, 16);
164         }
165 }
166
167 void testrun_testkey_aes(void){
168         testrun_testkey_aes128();
169         testrun_testkey_aes192();
170         testrun_testkey_aes256();
171 }
172 /*****************************************************************************/
173
174 void testrun_performance_aes128(void){
175         uint64_t t;
176         char str[16];
177         uint8_t key[32], data[16];
178         aes128_ctx_t ctx;
179         
180         calibrateTimer();
181         print_overhead();
182         
183         memset(key,  0, 32);
184         memset(data, 0, 16);
185         
186         startTimer(1);
187         aes128_init(key, &ctx);
188         t = stopTimer();
189         uart_putstr_P(PSTR("\r\n\tctx-gen time: "));
190         ultoa((unsigned long)t, str, 10);
191         uart_putstr(str);
192         
193         
194         startTimer(1);
195         aes128_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         
202         startTimer(1);
203         aes128_dec(data, &ctx);
204         t = stopTimer();
205         uart_putstr_P(PSTR("\r\n\tdecrypt time: "));
206         ultoa((unsigned long)t, str, 10);
207         uart_putstr(str);
208         
209         uart_putstr_P(PSTR("\r\n"));
210 }
211
212
213 void testrun_performance_aes192(void){
214         uint64_t t;
215         char str[16];
216         uint8_t key[32], data[16];
217         aes192_ctx_t ctx;
218         
219         calibrateTimer();
220         print_overhead();
221         
222         memset(key,  0, 32);
223         memset(data, 0, 16);
224         
225         startTimer(1);
226         aes192_init(key, &ctx);
227         t = stopTimer();
228         uart_putstr_P(PSTR("\r\n\tctx-gen time: "));
229         ultoa((unsigned long)t, str, 10);
230         uart_putstr(str);
231         
232         
233         startTimer(1);
234         aes192_enc(data, &ctx);
235         t = stopTimer();
236         uart_putstr_P(PSTR("\r\n\tencrypt time: "));
237         ultoa((unsigned long)t, str, 10);
238         uart_putstr(str);
239         
240         
241         startTimer(1);
242         aes192_dec(data, &ctx);
243         t = stopTimer();
244         uart_putstr_P(PSTR("\r\n\tdecrypt time: "));
245         ultoa((unsigned long)t, str, 10);
246         uart_putstr(str);
247         
248         uart_putstr_P(PSTR("\r\n"));
249 }
250
251
252 void testrun_performance_aes256(void){
253         uint64_t t;
254         char str[16];
255         uint8_t key[32], data[16];
256         aes256_ctx_t ctx;
257         
258         calibrateTimer();
259         print_overhead();
260         
261         memset(key,  0, 32);
262         memset(data, 0, 16);
263         
264         startTimer(1);
265         aes256_init(key, &ctx);
266         t = stopTimer();
267         uart_putstr_P(PSTR("\r\n\tctx-gen time: "));
268         ultoa((unsigned long)t, str, 10);
269         uart_putstr(str);
270         
271         
272         startTimer(1);
273         aes256_enc(data, &ctx);
274         t = stopTimer();
275         uart_putstr_P(PSTR("\r\n\tencrypt time: "));
276         ultoa((unsigned long)t, str, 10);
277         uart_putstr(str);
278         
279         
280         startTimer(1);
281         aes256_dec(data, &ctx);
282         t = stopTimer();
283         uart_putstr_P(PSTR("\r\n\tdecrypt time: "));
284         ultoa((unsigned long)t, str, 10);
285         uart_putstr(str);
286         
287         uart_putstr_P(PSTR("\r\n"));
288 }
289
290 void testrun_performance_aes(void){
291         uart_putstr_P(PSTR("\r\n -=AES Performance Test=-\r\n"));
292         uart_putstr_P(PSTR("\r\n       AES-128\r\n"));
293         testrun_performance_aes128();
294         uart_putstr_P(PSTR("\r\n       AES-192\r\n"));
295         testrun_performance_aes192();
296         uart_putstr_P(PSTR("\r\n       AES-256\r\n"));
297         testrun_performance_aes256();
298 }
299 /*****************************************************************************
300  *  main                                                                                                                                         *
301  *****************************************************************************/
302
303 const char nessie_str[]      PROGMEM = "nessie";
304 const char test_str[]        PROGMEM = "test";
305 const char testkey_str[]     PROGMEM = "testkey";
306 const char performance_str[] PROGMEM = "performance";
307 const char echo_str[]        PROGMEM = "echo";
308
309 cmdlist_entry_t cmdlist[] PROGMEM = {
310         { nessie_str,      NULL, testrun_nessie_aes },
311         { test_str,        NULL, testrun_test_aes},
312         { testkey_str,     NULL, testrun_testkey_aes},
313         { performance_str, NULL, testrun_performance_aes},
314         { echo_str,    (void*)1, (void_fpt)echo_ctrl},
315         { NULL,            NULL, NULL}
316 };
317  
318
319 int main (void){
320         DEBUG_INIT();
321         uart_putstr("\r\n");
322         cli_rx = uart_getc;
323         cli_tx = uart_putc;             
324         for(;;){
325                 uart_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
326                 uart_putstr(algo_name);
327                 uart_putstr_P(PSTR(")\r\nloaded and running\r\n"));
328                 cmd_interface(cmdlist);
329         }
330 }
331