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