]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-aes-test.c
a first look at aes assembly
[avr-crypto-lib.git] / test_src / main-aes-test.c
1 /* main-aes-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  * 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         
94         
95 }
96
97 void testrun_testkey_aes128(void){
98         uint8_t key[16] = { 0x2b, 0x7e, 0x15, 0x16, 
99                             0x28, 0xae, 0xd2, 0xa6,
100                             0xab, 0xf7, 0x15, 0x88,
101                             0x09, 0xcf, 0x4f, 0x3c};
102         aes128_ctx_t ctx;
103         uint8_t i;
104         aes128_init(key, &ctx);
105         uart_putstr_P(PSTR("\r\n\r\n keyschedule test (FIPS 197):\r\n key:   "));
106         uart_hexdump(key, 16);
107         for(i=0; i<11; ++i){
108                 uart_putstr_P(PSTR("\r\n index: "));
109                 uart_putc('0'+i/10);
110                 uart_putc('0'+i%10);
111                 uart_putstr_P(PSTR(" roundkey "));
112                 uart_hexdump(ctx.key[i].ks, 16);
113         }
114 }
115
116 void testrun_testkey_aes192(void){
117         uint8_t key[24] = { 0x8e, 0x73, 0xb0, 0xf7, 
118                             0xda, 0x0e, 0x64, 0x52,
119                             0xc8, 0x10, 0xf3, 0x2b, 
120                             0x80, 0x90, 0x79, 0xe5, 
121                             0x62, 0xf8, 0xea, 0xd2, 
122                             0x52, 0x2c, 0x6b, 0x7b};
123         aes192_ctx_t ctx;
124         uint8_t i;
125         memset(&ctx, 0, sizeof(aes192_ctx_t));
126         aes192_init(key, &ctx);
127         uart_putstr_P(PSTR("\r\n\r\n keyschedule test (FIPS 197):\r\n key:   "));
128         uart_hexdump(key, 24);
129         for(i=0; i<13; ++i){
130                 uart_putstr_P(PSTR("\r\n index: "));
131                 uart_putc('0'+i/10);
132                 uart_putc('0'+i%10);
133                 uart_putstr_P(PSTR(" roundkey "));
134                 uart_hexdump(ctx.key[i].ks, 16);
135         }
136 }
137
138
139 void testrun_testkey_aes256(void){
140         uint8_t key[32] = { 0x60, 0x3d, 0xeb, 0x10, 
141                             0x15, 0xca, 0x71, 0xbe, 
142                             0x2b, 0x73, 0xae, 0xf0, 
143                             0x85, 0x7d, 0x77, 0x81, 
144                             0x1f, 0x35, 0x2c, 0x07, 
145                             0x3b, 0x61, 0x08, 0xd7, 
146                             0x2d, 0x98, 0x10, 0xa3, 
147                             0x09, 0x14, 0xdf, 0xf4};
148         aes256_ctx_t ctx;
149         uint8_t i;
150         memset(&ctx, 0, sizeof(aes256_ctx_t));
151         aes256_init(key, &ctx);
152         uart_putstr_P(PSTR("\r\n\r\n keyschedule test (FIPS 197):\r\n key:   "));
153         uart_hexdump(key, 32);
154         for(i=0; i<15; ++i){
155                 uart_putstr_P(PSTR("\r\n index: "));
156                 uart_putc('0'+i/10);
157                 uart_putc('0'+i%10);
158                 uart_putstr_P(PSTR(" roundkey "));
159                 uart_hexdump(ctx.key[i].ks, 16);
160         }
161 }
162
163 void testrun_testkey_aes(void){
164         testrun_testkey_aes128();
165         testrun_testkey_aes192();
166         testrun_testkey_aes256();
167 }
168 /*****************************************************************************/
169
170 void testrun_performance_aes128(void){
171         uint64_t t;
172         char str[16];
173         uint8_t key[32], data[16];
174         aes128_ctx_t ctx;
175         
176         calibrateTimer();
177         print_overhead();
178         
179         memset(key,  0, 32);
180         memset(data, 0, 16);
181         
182         startTimer(1);
183         aes128_init(key, &ctx);
184         t = stopTimer();
185         uart_putstr_P(PSTR("\r\n\tctx-gen time: "));
186         ultoa((unsigned long)t, str, 10);
187         uart_putstr(str);
188         
189         
190         startTimer(1);
191         aes128_enc(data, &ctx);
192         t = stopTimer();
193         uart_putstr_P(PSTR("\r\n\tencrypt time: "));
194         ultoa((unsigned long)t, str, 10);
195         uart_putstr(str);
196         
197         
198         startTimer(1);
199         aes128_dec(data, &ctx);
200         t = stopTimer();
201         uart_putstr_P(PSTR("\r\n\tdecrypt time: "));
202         ultoa((unsigned long)t, str, 10);
203         uart_putstr(str);
204         
205         uart_putstr_P(PSTR("\r\n"));
206 }
207
208
209 void testrun_performance_aes192(void){
210         uint64_t t;
211         char str[16];
212         uint8_t key[32], data[16];
213         aes192_ctx_t ctx;
214         
215         calibrateTimer();
216         print_overhead();
217         
218         memset(key,  0, 32);
219         memset(data, 0, 16);
220         
221         startTimer(1);
222         aes192_init(key, &ctx);
223         t = stopTimer();
224         uart_putstr_P(PSTR("\r\n\tctx-gen time: "));
225         ultoa((unsigned long)t, str, 10);
226         uart_putstr(str);
227         
228         
229         startTimer(1);
230         aes192_enc(data, &ctx);
231         t = stopTimer();
232         uart_putstr_P(PSTR("\r\n\tencrypt time: "));
233         ultoa((unsigned long)t, str, 10);
234         uart_putstr(str);
235         
236         
237         startTimer(1);
238         aes192_dec(data, &ctx);
239         t = stopTimer();
240         uart_putstr_P(PSTR("\r\n\tdecrypt time: "));
241         ultoa((unsigned long)t, str, 10);
242         uart_putstr(str);
243         
244         uart_putstr_P(PSTR("\r\n"));
245 }
246
247
248 void testrun_performance_aes256(void){
249         uint64_t t;
250         char str[16];
251         uint8_t key[32], data[16];
252         aes256_ctx_t ctx;
253         
254         calibrateTimer();
255         print_overhead();
256         
257         memset(key,  0, 32);
258         memset(data, 0, 16);
259         
260         startTimer(1);
261         aes256_init(key, &ctx);
262         t = stopTimer();
263         uart_putstr_P(PSTR("\r\n\tctx-gen time: "));
264         ultoa((unsigned long)t, str, 10);
265         uart_putstr(str);
266         
267         
268         startTimer(1);
269         aes256_enc(data, &ctx);
270         t = stopTimer();
271         uart_putstr_P(PSTR("\r\n\tencrypt time: "));
272         ultoa((unsigned long)t, str, 10);
273         uart_putstr(str);
274         
275         
276         startTimer(1);
277         aes256_dec(data, &ctx);
278         t = stopTimer();
279         uart_putstr_P(PSTR("\r\n\tdecrypt time: "));
280         ultoa((unsigned long)t, str, 10);
281         uart_putstr(str);
282         
283         uart_putstr_P(PSTR("\r\n"));
284 }
285
286 void testrun_performance_aes(void){
287         uart_putstr_P(PSTR("\r\n -=AES Performance Test=-\r\n"));
288         uart_putstr_P(PSTR("\r\n       AES-128\r\n"));
289         testrun_performance_aes128();
290         uart_putstr_P(PSTR("\r\n       AES-192\r\n"));
291         testrun_performance_aes192();
292         uart_putstr_P(PSTR("\r\n       AES-256\r\n"));
293         testrun_performance_aes256();
294 }
295 /*****************************************************************************
296  *  main                                                                                                                                         *
297  *****************************************************************************/
298
299 int main (void){
300         char  str[20];
301         DEBUG_INIT();
302         uart_putstr("\r\n");
303
304         uart_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
305         uart_putstr(cipher_name);
306         uart_putstr_P(PSTR(")\r\nloaded and running\r\n"));
307
308         PGM_P    u   = PSTR("nessie\0test\0testkey\0performance\0");
309         void_fpt v[] = {testrun_nessie_aes, 
310                         testrun_test_aes, 
311                         testrun_testkey_aes, 
312                         testrun_performance_aes};
313
314         while(1){ 
315                 if (!getnextwordn(str,20)){DEBUG_S("DBG: W1\r\n"); goto error;}
316                 if(execcommand_d0_P(str, u, v)<0){
317                         uart_putstr_P(PSTR("\r\nunknown command\r\n"));
318                 }
319                 continue;
320         error:
321                 uart_putstr("ERROR\r\n");
322         }
323         
324 }
325