]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-aes-test.c
aes_keyschdule speed up
[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_aes(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_performance_aes128(void){
117         uint64_t t;
118         char str[16];
119         uint8_t key[32], data[16];
120         aes128_ctx_t ctx;
121         
122         calibrateTimer();
123         print_overhead();
124         
125         memset(key,  0, 32);
126         memset(data, 0, 16);
127         
128         startTimer(1);
129         aes128_init(key, &ctx);
130         t = stopTimer();
131         uart_putstr_P(PSTR("\r\n\tctx-gen time: "));
132         ultoa((unsigned long)t, str, 10);
133         uart_putstr(str);
134         
135         
136         startTimer(1);
137         aes128_enc(data, &ctx);
138         t = stopTimer();
139         uart_putstr_P(PSTR("\r\n\tencrypt time: "));
140         ultoa((unsigned long)t, str, 10);
141         uart_putstr(str);
142         
143         
144         startTimer(1);
145         aes128_dec(data, &ctx);
146         t = stopTimer();
147         uart_putstr_P(PSTR("\r\n\tdecrypt time: "));
148         ultoa((unsigned long)t, str, 10);
149         uart_putstr(str);
150         
151         uart_putstr_P(PSTR("\r\n"));
152 }
153
154
155 void testrun_performance_aes192(void){
156         uint64_t t;
157         char str[16];
158         uint8_t key[32], data[16];
159         aes192_ctx_t ctx;
160         
161         calibrateTimer();
162         print_overhead();
163         
164         memset(key,  0, 32);
165         memset(data, 0, 16);
166         
167         startTimer(1);
168         aes192_init(key, &ctx);
169         t = stopTimer();
170         uart_putstr_P(PSTR("\r\n\tctx-gen time: "));
171         ultoa((unsigned long)t, str, 10);
172         uart_putstr(str);
173         
174         
175         startTimer(1);
176         aes192_enc(data, &ctx);
177         t = stopTimer();
178         uart_putstr_P(PSTR("\r\n\tencrypt time: "));
179         ultoa((unsigned long)t, str, 10);
180         uart_putstr(str);
181         
182         
183         startTimer(1);
184         aes192_dec(data, &ctx);
185         t = stopTimer();
186         uart_putstr_P(PSTR("\r\n\tdecrypt time: "));
187         ultoa((unsigned long)t, str, 10);
188         uart_putstr(str);
189         
190         uart_putstr_P(PSTR("\r\n"));
191 }
192
193
194 void testrun_performance_aes256(void){
195         uint64_t t;
196         char str[16];
197         uint8_t key[32], data[16];
198         aes256_ctx_t ctx;
199         
200         calibrateTimer();
201         print_overhead();
202         
203         memset(key,  0, 32);
204         memset(data, 0, 16);
205         
206         startTimer(1);
207         aes256_init(key, &ctx);
208         t = stopTimer();
209         uart_putstr_P(PSTR("\r\n\tctx-gen time: "));
210         ultoa((unsigned long)t, str, 10);
211         uart_putstr(str);
212         
213         
214         startTimer(1);
215         aes256_enc(data, &ctx);
216         t = stopTimer();
217         uart_putstr_P(PSTR("\r\n\tencrypt time: "));
218         ultoa((unsigned long)t, str, 10);
219         uart_putstr(str);
220         
221         
222         startTimer(1);
223         aes256_dec(data, &ctx);
224         t = stopTimer();
225         uart_putstr_P(PSTR("\r\n\tdecrypt time: "));
226         ultoa((unsigned long)t, str, 10);
227         uart_putstr(str);
228         
229         uart_putstr_P(PSTR("\r\n"));
230 }
231
232 void testrun_performance_aes(void){
233         uart_putstr_P(PSTR("\r\n -=AES Performance Test=-\r\n"));
234         uart_putstr_P(PSTR("\r\n       AES-128\r\n"));
235         testrun_performance_aes128();
236         uart_putstr_P(PSTR("\r\n       AES-192\r\n"));
237         testrun_performance_aes192();
238         uart_putstr_P(PSTR("\r\n       AES-256\r\n"));
239         testrun_performance_aes256();
240 }
241 /*****************************************************************************
242  *  main                                                                                                                                         *
243  *****************************************************************************/
244
245 int main (void){
246         char  str[20];
247         DEBUG_INIT();
248         uart_putstr("\r\n");
249
250         uart_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
251         uart_putstr(cipher_name);
252         uart_putstr_P(PSTR(")\r\nloaded and running\r\n"));
253
254         PGM_P    u   = PSTR("nessie\0test\0testkey\0performance\0");
255         void_fpt v[] = {testrun_nessie_aes, 
256                         testrun_test_aes, 
257                         testrun_testkey_aes, 
258                         testrun_performance_aes};
259
260         while(1){ 
261                 if (!getnextwordn(str,20)){DEBUG_S("DBG: W1\r\n"); goto error;}
262                 if(execcommand_d0_P(str, u, v)<0){
263                         uart_putstr_P(PSTR("\r\nunknown command\r\n"));
264                 }
265                 continue;
266         error:
267                 uart_putstr("ERROR\r\n");
268         }
269         
270 }
271