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