]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-aes-test.c
"new cbc-mode for bcal; docu comming soon
[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 #include "dump.h"
35
36 #include "bcal_aes128.h"
37 #include "bcal_aes192.h"
38 #include "bcal_aes256.h"
39 #include "bcal-cbc.h"
40
41 #include <stdint.h>
42 #include <string.h>
43 #include <stdlib.h>
44 #include <avr/pgmspace.h>
45
46 char* algo_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        = algo_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         cli_putstr_P(PSTR("\r\n\r\n cipher test (FIPS 197):\r\n key:        "));
87         cli_hexdump(key, 16);
88         cli_putstr_P(PSTR("\r\n plaintext:  "));
89         cli_hexdump(data, 16);
90         aes128_enc(data, &ctx);
91         cli_putstr_P(PSTR("\r\n ciphertext: "));
92         cli_hexdump(data, 16);
93         aes128_dec(data, &ctx);
94         cli_putstr_P(PSTR("\r\n plaintext:  "));
95         cli_hexdump(data, 16);
96         cli_putstr(PSTR("\r\n testing bcal:"));
97         bcgen_ctx_t bcal_ctx;
98         uint8_t r;
99         r = bcal_cipher_init(&aes128_desc, key, 128, &bcal_ctx);
100         cli_putstr_P(PSTR("\r\n init = 0x"));
101         cli_hexdump(&r, 1);
102
103         bcal_cipher_enc(data, &bcal_ctx);
104         cli_putstr_P(PSTR("\r\n ciphertext: "));
105         cli_hexdump(data, 16);
106         bcal_cipher_dec(data, &bcal_ctx);
107         cli_putstr_P(PSTR("\r\n plaintext:  "));
108         cli_hexdump(data, 16);
109 }
110
111 void testrun_testkey_aes128(void){
112         uint8_t key[16] = { 0x2b, 0x7e, 0x15, 0x16,
113                             0x28, 0xae, 0xd2, 0xa6,
114                             0xab, 0xf7, 0x15, 0x88,
115                             0x09, 0xcf, 0x4f, 0x3c};
116         aes128_ctx_t ctx;
117         uint8_t i;
118         aes128_init(key, &ctx);
119         cli_putstr_P(PSTR("\r\n\r\n keyschedule test (FIPS 197):\r\n key:   "));
120         cli_hexdump(key, 16);
121         for(i=0; i<11; ++i){
122                 cli_putstr_P(PSTR("\r\n index: "));
123                 cli_putc('0'+i/10);
124                 cli_putc('0'+i%10);
125                 cli_putstr_P(PSTR(" roundkey "));
126                 cli_hexdump(ctx.key[i].ks, 16);
127         }
128 }
129
130 void testrun_testkey_aes192(void){
131         uint8_t key[24] = { 0x8e, 0x73, 0xb0, 0xf7,
132                             0xda, 0x0e, 0x64, 0x52,
133                             0xc8, 0x10, 0xf3, 0x2b,
134                             0x80, 0x90, 0x79, 0xe5,
135                             0x62, 0xf8, 0xea, 0xd2,
136                             0x52, 0x2c, 0x6b, 0x7b};
137         aes192_ctx_t ctx;
138         uint8_t i;
139         memset(&ctx, 0, sizeof(aes192_ctx_t));
140         aes192_init(key, &ctx);
141         cli_putstr_P(PSTR("\r\n\r\n keyschedule test (FIPS 197):\r\n key:   "));
142         cli_hexdump(key, 24);
143         for(i=0; i<13; ++i){
144                 cli_putstr_P(PSTR("\r\n index: "));
145                 cli_putc('0'+i/10);
146                 cli_putc('0'+i%10);
147                 cli_putstr_P(PSTR(" roundkey "));
148                 cli_hexdump(ctx.key[i].ks, 16);
149         }
150 }
151
152
153 void testrun_testkey_aes256(void){
154         uint8_t key[32] = { 0x60, 0x3d, 0xeb, 0x10,
155                             0x15, 0xca, 0x71, 0xbe,
156                             0x2b, 0x73, 0xae, 0xf0,
157                             0x85, 0x7d, 0x77, 0x81,
158                             0x1f, 0x35, 0x2c, 0x07,
159                             0x3b, 0x61, 0x08, 0xd7,
160                             0x2d, 0x98, 0x10, 0xa3,
161                             0x09, 0x14, 0xdf, 0xf4};
162         aes256_ctx_t ctx;
163         uint8_t i;
164         memset(&ctx, 0, sizeof(aes256_ctx_t));
165         aes256_init(key, &ctx);
166         cli_putstr_P(PSTR("\r\n\r\n keyschedule test (FIPS 197):\r\n key:   "));
167         cli_hexdump(key, 32);
168         for(i=0; i<15; ++i){
169                 cli_putstr_P(PSTR("\r\n index: "));
170                 cli_putc('0'+i/10);
171                 cli_putc('0'+i%10);
172                 cli_putstr_P(PSTR(" roundkey "));
173                 cli_hexdump(ctx.key[i].ks, 16);
174         }
175 }
176
177 void testrun_testkey_aes(void){
178         testrun_testkey_aes128();
179         testrun_testkey_aes192();
180         testrun_testkey_aes256();
181 }
182
183
184 void testrun_aes128_cbc(void){
185         uint8_t key[]     = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
186                                       0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c };
187         uint8_t iv[]      = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
188                                       0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
189         uint8_t plain[]   = { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
190                                       0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
191                                       /* --- */
192                                       0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
193                                       0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
194                                       /* --- */
195                                       0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
196                                       0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
197                                       /* --- */
198                                       0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
199                                       0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 };
200         bcal_cbc_ctx_t ctx;
201         uint8_t r;
202         cli_putstr_P(PSTR("\r\n** AES128-CBC-TEST **"));
203         r = bcal_cbc_init(&aes128_desc, key, 128, &ctx);
204         cli_putstr_P(PSTR("\r\n  init = 0x"));
205         cli_hexdump(&r, 1);
206         cli_putstr_P(PSTR("\r\n  key:   "));
207         cli_hexdump(key, 128/8);
208         cli_putstr_P(PSTR("\r\n  IV:    "));
209         cli_hexdump(iv, 128/8);
210         cli_putstr_P(PSTR("\r\n  plaintext:"));
211         cli_hexdump_block(plain, 4*128/8, 4, 8);
212         if(r)
213                 return;
214         bcal_cbc_encMsg(iv, plain, 4, &ctx);
215         cli_putstr_P(PSTR("\r\n  ciphertext:  "));
216         cli_hexdump_block(plain, 4*128/8, 4, 8);
217         bcal_cbc_decMsg(iv, plain, 4, &ctx);
218         cli_putstr_P(PSTR("\r\n  plaintext:   "));
219         cli_hexdump_block(plain, 4*128/8, 4, 8);
220         bcal_cbc_free(&ctx);
221 }
222
223
224 /*****************************************************************************/
225
226 void testrun_performance_aes128(void){
227         uint64_t t;
228         char str[16];
229         uint8_t key[32], data[16];
230         aes128_ctx_t ctx;
231
232         calibrateTimer();
233         print_overhead();
234
235         memset(key,  0, 32);
236         memset(data, 0, 16);
237
238         startTimer(1);
239         aes128_init(key, &ctx);
240         t = stopTimer();
241         cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
242         ultoa((unsigned long)t, str, 10);
243         cli_putstr(str);
244
245
246         startTimer(1);
247         aes128_enc(data, &ctx);
248         t = stopTimer();
249         cli_putstr_P(PSTR("\r\n\tencrypt time: "));
250         ultoa((unsigned long)t, str, 10);
251         cli_putstr(str);
252
253
254         startTimer(1);
255         aes128_dec(data, &ctx);
256         t = stopTimer();
257         cli_putstr_P(PSTR("\r\n\tdecrypt time: "));
258         ultoa((unsigned long)t, str, 10);
259         cli_putstr(str);
260
261         cli_putstr_P(PSTR("\r\n"));
262 }
263
264
265 void testrun_performance_aes192(void){
266         uint64_t t;
267         char str[16];
268         uint8_t key[32], data[16];
269         aes192_ctx_t ctx;
270
271         calibrateTimer();
272         print_overhead();
273
274         memset(key,  0, 32);
275         memset(data, 0, 16);
276
277         startTimer(1);
278         aes192_init(key, &ctx);
279         t = stopTimer();
280         cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
281         ultoa((unsigned long)t, str, 10);
282         cli_putstr(str);
283
284
285         startTimer(1);
286         aes192_enc(data, &ctx);
287         t = stopTimer();
288         cli_putstr_P(PSTR("\r\n\tencrypt time: "));
289         ultoa((unsigned long)t, str, 10);
290         cli_putstr(str);
291
292
293         startTimer(1);
294         aes192_dec(data, &ctx);
295         t = stopTimer();
296         cli_putstr_P(PSTR("\r\n\tdecrypt time: "));
297         ultoa((unsigned long)t, str, 10);
298         cli_putstr(str);
299
300         cli_putstr_P(PSTR("\r\n"));
301 }
302
303
304 void testrun_performance_aes256(void){
305         uint64_t t;
306         char str[16];
307         uint8_t key[32], data[16];
308         aes256_ctx_t ctx;
309
310         calibrateTimer();
311         print_overhead();
312
313         memset(key,  0, 32);
314         memset(data, 0, 16);
315
316         startTimer(1);
317         aes256_init(key, &ctx);
318         t = stopTimer();
319         cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
320         ultoa((unsigned long)t, str, 10);
321         cli_putstr(str);
322
323
324         startTimer(1);
325         aes256_enc(data, &ctx);
326         t = stopTimer();
327         cli_putstr_P(PSTR("\r\n\tencrypt time: "));
328         ultoa((unsigned long)t, str, 10);
329         cli_putstr(str);
330
331
332         startTimer(1);
333         aes256_dec(data, &ctx);
334         t = stopTimer();
335         cli_putstr_P(PSTR("\r\n\tdecrypt time: "));
336         ultoa((unsigned long)t, str, 10);
337         cli_putstr(str);
338
339         cli_putstr_P(PSTR("\r\n"));
340 }
341
342 void testrun_performance_aes(void){
343         cli_putstr_P(PSTR("\r\n -=AES Performance Test=-\r\n"));
344         cli_putstr_P(PSTR("\r\n       AES-128\r\n"));
345         testrun_performance_aes128();
346         cli_putstr_P(PSTR("\r\n       AES-192\r\n"));
347         testrun_performance_aes192();
348         cli_putstr_P(PSTR("\r\n       AES-256\r\n"));
349         testrun_performance_aes256();
350 }
351 /*****************************************************************************
352  *  main                                                                                                                                         *
353  *****************************************************************************/
354
355 const char nessie_str[]      PROGMEM = "nessie";
356 const char test_str[]        PROGMEM = "test";
357 const char testkey_str[]     PROGMEM = "testkey";
358 const char testcbc128_str[]  PROGMEM = "testcbc128";
359 const char performance_str[] PROGMEM = "performance";
360 const char dump_str[]        PROGMEM = "dump";
361 const char echo_str[]        PROGMEM = "echo";
362
363 cmdlist_entry_t cmdlist[] PROGMEM = {
364         { nessie_str,      NULL, testrun_nessie_aes },
365         { test_str,        NULL, testrun_test_aes},
366         { testkey_str,     NULL, testrun_testkey_aes},
367         { testcbc128_str,  NULL, testrun_aes128_cbc},
368         { performance_str, NULL, testrun_performance_aes},
369         { dump_str,    (void*)1, (void_fpt)dump},
370         { echo_str,    (void*)1, (void_fpt)echo_ctrl},
371         { NULL,            NULL, NULL}
372 };
373
374
375 int main (void){
376         DEBUG_INIT();
377
378         cli_rx = (cli_rx_fpt)uart0_getc;
379         cli_tx = (cli_tx_fpt)uart0_putc;
380         for(;;){
381                 cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
382                 cli_putstr(algo_name);
383                 cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
384                 cmd_interface(cmdlist);
385         }
386 }
387