]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-aes-test.c
cmac+testvectors
[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 #include "bcal-cfb_byte.h"
41 #include "bcal-cfb_bit.h"
42 #include "bcal-ofb.h"
43 #include "bcal-ctr.h"
44 #include "bcal-cmac.h"
45 #include "cmacvs.h"
46
47 #include <stdint.h>
48 #include <string.h>
49 #include <stdlib.h>
50 #include <avr/pgmspace.h>
51
52 char* algo_name = "AES";
53
54 const bcdesc_t* algolist[] PROGMEM = {
55         (bcdesc_t*)&aes128_desc,
56         (bcdesc_t*)&aes192_desc,
57         (bcdesc_t*)&aes256_desc,
58         NULL
59 };
60
61 /*****************************************************************************
62  *  additional validation-functions                                                                                      *
63  *****************************************************************************/
64
65 void testrun_nessie_aes(void){
66         nessie_bc_ctx.blocksize_B =  16;
67         nessie_bc_ctx.keysize_b   = 128;
68         nessie_bc_ctx.name        = algo_name;
69         nessie_bc_ctx.ctx_size_B  = sizeof(aes128_ctx_t);
70         nessie_bc_ctx.cipher_enc  = (nessie_bc_enc_fpt)aes128_enc;
71         nessie_bc_ctx.cipher_dec  = (nessie_bc_dec_fpt)aes128_dec;
72         nessie_bc_ctx.cipher_genctx  = (nessie_bc_gen_fpt)aes_init;
73         nessie_bc_run();
74
75         nessie_bc_ctx.keysize_b   = 192;
76         nessie_bc_ctx.ctx_size_B  = sizeof(aes192_ctx_t);
77         nessie_bc_ctx.cipher_enc  = (nessie_bc_enc_fpt)aes192_enc;
78         nessie_bc_ctx.cipher_dec  = (nessie_bc_dec_fpt)aes192_dec;
79         nessie_bc_run();
80
81         nessie_bc_ctx.keysize_b   = 256;
82         nessie_bc_ctx.ctx_size_B  = sizeof(aes256_ctx_t);
83         nessie_bc_ctx.cipher_enc  = (nessie_bc_enc_fpt)aes256_enc;
84         nessie_bc_ctx.cipher_dec  = (nessie_bc_dec_fpt)aes256_dec;
85         nessie_bc_run();
86 }
87
88 void testrun_test_aes(void){
89         uint8_t key[16] = { 0x2b, 0x7e, 0x15, 0x16,
90                             0x28, 0xae, 0xd2, 0xa6,
91                             0xab, 0xf7, 0x15, 0x88,
92                             0x09, 0xcf, 0x4f, 0x3c };
93         uint8_t data[16] = { 0x32, 0x43, 0xf6, 0xa8,
94                              0x88, 0x5a, 0x30, 0x8d,
95                              0x31, 0x31, 0x98, 0xa2,
96                              0xe0, 0x37, 0x07, 0x34 };
97         aes128_ctx_t ctx;
98         aes128_init(key, &ctx);
99         cli_putstr_P(PSTR("\r\n\r\n cipher test (FIPS 197):\r\n key:        "));
100         cli_hexdump(key, 16);
101         cli_putstr_P(PSTR("\r\n plaintext:  "));
102         cli_hexdump(data, 16);
103         aes128_enc(data, &ctx);
104         cli_putstr_P(PSTR("\r\n ciphertext: "));
105         cli_hexdump(data, 16);
106         aes128_dec(data, &ctx);
107         cli_putstr_P(PSTR("\r\n plaintext:  "));
108         cli_hexdump(data, 16);
109         cli_putstr(PSTR("\r\n testing bcal:"));
110         bcgen_ctx_t bcal_ctx;
111         uint8_t r;
112         r = bcal_cipher_init(&aes128_desc, key, 128, &bcal_ctx);
113         cli_putstr_P(PSTR("\r\n init = 0x"));
114         cli_hexdump(&r, 1);
115
116         bcal_cipher_enc(data, &bcal_ctx);
117         cli_putstr_P(PSTR("\r\n ciphertext: "));
118         cli_hexdump(data, 16);
119         bcal_cipher_dec(data, &bcal_ctx);
120         cli_putstr_P(PSTR("\r\n plaintext:  "));
121         cli_hexdump(data, 16);
122         bcal_cipher_free(&bcal_ctx);
123 }
124
125 void testrun_testkey_aes128(void){
126         uint8_t key[16] = { 0x2b, 0x7e, 0x15, 0x16,
127                             0x28, 0xae, 0xd2, 0xa6,
128                             0xab, 0xf7, 0x15, 0x88,
129                             0x09, 0xcf, 0x4f, 0x3c};
130         aes128_ctx_t ctx;
131         uint8_t i;
132         aes128_init(key, &ctx);
133         cli_putstr_P(PSTR("\r\n\r\n keyschedule test (FIPS 197):\r\n key:   "));
134         cli_hexdump(key, 16);
135         for(i=0; i<11; ++i){
136                 cli_putstr_P(PSTR("\r\n index: "));
137                 cli_putc('0'+i/10);
138                 cli_putc('0'+i%10);
139                 cli_putstr_P(PSTR(" roundkey "));
140                 cli_hexdump(ctx.key[i].ks, 16);
141         }
142 }
143
144 void testrun_testkey_aes192(void){
145         uint8_t key[24] = { 0x8e, 0x73, 0xb0, 0xf7,
146                             0xda, 0x0e, 0x64, 0x52,
147                             0xc8, 0x10, 0xf3, 0x2b,
148                             0x80, 0x90, 0x79, 0xe5,
149                             0x62, 0xf8, 0xea, 0xd2,
150                             0x52, 0x2c, 0x6b, 0x7b};
151         aes192_ctx_t ctx;
152         uint8_t i;
153         memset(&ctx, 0, sizeof(aes192_ctx_t));
154         aes192_init(key, &ctx);
155         cli_putstr_P(PSTR("\r\n\r\n keyschedule test (FIPS 197):\r\n key:   "));
156         cli_hexdump(key, 24);
157         for(i=0; i<13; ++i){
158                 cli_putstr_P(PSTR("\r\n index: "));
159                 cli_putc('0'+i/10);
160                 cli_putc('0'+i%10);
161                 cli_putstr_P(PSTR(" roundkey "));
162                 cli_hexdump(ctx.key[i].ks, 16);
163         }
164 }
165
166
167 void testrun_testkey_aes256(void){
168         uint8_t key[32] = { 0x60, 0x3d, 0xeb, 0x10,
169                             0x15, 0xca, 0x71, 0xbe,
170                             0x2b, 0x73, 0xae, 0xf0,
171                             0x85, 0x7d, 0x77, 0x81,
172                             0x1f, 0x35, 0x2c, 0x07,
173                             0x3b, 0x61, 0x08, 0xd7,
174                             0x2d, 0x98, 0x10, 0xa3,
175                             0x09, 0x14, 0xdf, 0xf4};
176         aes256_ctx_t ctx;
177         uint8_t i;
178         memset(&ctx, 0, sizeof(aes256_ctx_t));
179         aes256_init(key, &ctx);
180         cli_putstr_P(PSTR("\r\n\r\n keyschedule test (FIPS 197):\r\n key:   "));
181         cli_hexdump(key, 32);
182         for(i=0; i<15; ++i){
183                 cli_putstr_P(PSTR("\r\n index: "));
184                 cli_putc('0'+i/10);
185                 cli_putc('0'+i%10);
186                 cli_putstr_P(PSTR(" roundkey "));
187                 cli_hexdump(ctx.key[i].ks, 16);
188         }
189 }
190
191 void testrun_testkey_aes(void){
192         testrun_testkey_aes128();
193         testrun_testkey_aes192();
194         testrun_testkey_aes256();
195 }
196
197 uint8_t modes_key[]   PROGMEM = {
198                 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
199                 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c,
200                 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
201                 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
202         };
203 uint8_t modes_iv[]    PROGMEM = {
204                 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
205                 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
206     };
207
208 uint8_t modes_ctriv[] PROGMEM = {
209                 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
210                 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
211         };
212
213 uint8_t modes_plain[] PROGMEM = {
214                 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
215                 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
216                 /* --- */
217                 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
218                 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
219                 /* --- */
220                 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
221                 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
222                 /* --- */
223                 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
224                 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
225     };
226
227
228 void testrun_aes128_cbc(void){
229         uint8_t key[16];
230         uint8_t iv[16];
231         uint8_t plain[64];
232
233         bcal_cbc_ctx_t ctx;
234         uint8_t r;
235
236         memcpy_P(key,   modes_key,   16);
237         memcpy_P(iv,    modes_iv,    16);
238         memcpy_P(plain, modes_plain, 64);
239
240         cli_putstr_P(PSTR("\r\n** AES128-CBC-TEST **"));
241         r = bcal_cbc_init(&aes128_desc, key, 128, &ctx);
242         cli_putstr_P(PSTR("\r\n  init = 0x"));
243         cli_hexdump(&r, 1);
244         cli_putstr_P(PSTR("\r\n  key:   "));
245         cli_hexdump(key, 128/8);
246         cli_putstr_P(PSTR("\r\n  IV:    "));
247         cli_hexdump(iv, 128/8);
248         cli_putstr_P(PSTR("\r\n  plaintext:"));
249         cli_hexdump_block(plain, 4*128/8, 4, 16);
250         if(r)
251                 return;
252         bcal_cbc_encMsg(iv, plain, 4, &ctx);
253         cli_putstr_P(PSTR("\r\n  ciphertext:  "));
254         cli_hexdump_block(plain, 4*128/8, 4, 16);
255         bcal_cbc_decMsg(iv, plain, 4, &ctx);
256         cli_putstr_P(PSTR("\r\n  plaintext:   "));
257         cli_hexdump_block(plain, 4*128/8, 4, 16);
258         bcal_cbc_free(&ctx);
259 }
260
261 void testrun_aes128_cfb8(void){
262         uint8_t key[16];
263         uint8_t iv[16];
264         uint8_t plain[64];
265
266         bcal_cfb_B_ctx_t ctx;
267         uint8_t r;
268
269         memcpy_P(key,   modes_key,   16);
270         memcpy_P(iv,    modes_iv,    16);
271         memcpy_P(plain, modes_plain, 64);
272
273         cli_putstr_P(PSTR("\r\n** AES128-CFB8-TEST **"));
274         r = bcal_cfb_B_init(&aes128_desc, key, 128, 8, &ctx);
275         cli_putstr_P(PSTR("\r\n  init = 0x"));
276         cli_hexdump(&r, 1);
277         cli_putstr_P(PSTR("\r\n  key:   "));
278         cli_hexdump(key, 128/8);
279         cli_putstr_P(PSTR("\r\n  IV:    "));
280         cli_hexdump(iv, 128/8);
281         cli_putstr_P(PSTR("\r\n  plaintext:"));
282         cli_hexdump_block(plain, 4*128/8, 4, 16);
283         if(r)
284                 return;
285         bcal_cfb_B_encMsg(iv, plain, 64, &ctx);
286         cli_putstr_P(PSTR("\r\n  ciphertext:  "));
287         cli_hexdump_block(plain, 64, 4, 16);
288
289         bcal_cfb_B_decMsg(iv, plain, 64, &ctx);
290         cli_putstr_P(PSTR("\r\n  plaintext:   "));
291         cli_hexdump_block(plain, 64, 4, 16);
292
293         bcal_cfb_B_free(&ctx);
294
295 }
296
297 void testrun_aes128_cfb1(void){
298         uint8_t key[16];
299         uint8_t iv[16];
300         uint8_t plain[64];
301
302         bcal_cfb_b_ctx_t ctx;
303         uint8_t r;
304
305         memcpy_P(key,   modes_key,   16);
306         memcpy_P(iv,    modes_iv,    16);
307         memcpy_P(plain, modes_plain, 64);
308
309         cli_putstr_P(PSTR("\r\n** AES128-CFB1-TEST **"));
310         r = bcal_cfb_b_init(&aes128_desc, key, 128, 1, &ctx);
311         cli_putstr_P(PSTR("\r\n  init = 0x"));
312         cli_hexdump(&r, 1);
313         cli_putstr_P(PSTR("\r\n  key:   "));
314         cli_hexdump(key, 128/8);
315         cli_putstr_P(PSTR("\r\n  IV:    "));
316         cli_hexdump(iv, 128/8);
317         cli_putstr_P(PSTR("\r\n  plaintext:"));
318         cli_hexdump_block(plain, 2, 4, 16);
319         if(r)
320                 return;
321         uint8_t i, bit_offset, byte_offset;
322         bcal_cfb_b_loadIV(iv, &ctx);
323         for(i=0; i<16; ++i){
324                 byte_offset = i/8;
325                 bit_offset = i&7;
326                 cli_putstr_P(PSTR("\r\n  plain bit:   "));
327                 cli_putc((plain[byte_offset]&(1<<(7-bit_offset)))?'1':'0');
328                 bcal_cfb_b_encNext(plain+byte_offset, bit_offset, &ctx);
329                 cli_putstr_P(PSTR("\r\n  cipher bit:  "));
330                 cli_putc((plain[byte_offset]&(1<<(7-bit_offset)))?'1':'0');
331         }
332         cli_putstr_P(PSTR("\r\n  ciphertext:  "));
333         cli_hexdump_block(plain, 2, 4, 16);
334
335         bcal_cfb_b_loadIV(iv, &ctx);
336         for(i=0; i<16; ++i){
337                 byte_offset = i/8;
338                 bit_offset = i&7;
339                 cli_putstr_P(PSTR("\r\n  plain bit:   "));
340                 cli_putc((plain[byte_offset]&(1<<(7-bit_offset)))?'1':'0');
341                 bcal_cfb_b_decNext(plain+byte_offset, bit_offset, &ctx);
342                 cli_putstr_P(PSTR("\r\n  cipher bit:  "));
343                 cli_putc((plain[byte_offset]&(1<<(7-bit_offset)))?'1':'0');
344         }
345         cli_putstr_P(PSTR("\r\n  plaintext:   "));
346         cli_hexdump_block(plain, 2, 4, 16);
347
348
349         bcal_cfb_b_encMsg(iv, plain, 0, 64*8, &ctx);
350         cli_putstr_P(PSTR("\r\n  ciphertext:  "));
351         cli_hexdump_block(plain, 64, 4, 16);
352
353         bcal_cfb_b_decMsg(iv, plain, 0, 64*8, &ctx);
354         cli_putstr_P(PSTR("\r\n  plaintext:   "));
355         cli_hexdump_block(plain, 64, 4, 16);
356
357         bcal_cfb_b_free(&ctx);
358 }
359
360 void testrun_aes128_ofb(void){
361         uint8_t key[16];
362         uint8_t iv[16];
363         uint8_t plain[64];
364
365         bcal_ofb_ctx_t ctx;
366         uint8_t r;
367
368         memcpy_P(key,   modes_key,   16);
369         memcpy_P(iv,    modes_iv,    16);
370         memcpy_P(plain, modes_plain, 64);
371
372         cli_putstr_P(PSTR("\r\n** AES128-OFB-TEST **"));
373         r = bcal_ofb_init(&aes128_desc, key, 128, &ctx);
374         cli_putstr_P(PSTR("\r\n  init = 0x"));
375         cli_hexdump(&r, 1);
376         cli_putstr_P(PSTR("\r\n  key:   "));
377         cli_hexdump(key, 128/8);
378         cli_putstr_P(PSTR("\r\n  IV:    "));
379         cli_hexdump(iv, 128/8);
380         cli_putstr_P(PSTR("\r\n  plaintext:"));
381         cli_hexdump_block(plain, 4*128/8, 4, 16);
382         if(r)
383                 return;
384         bcal_ofb_encMsg(iv, plain, 4*128, &ctx);
385         cli_putstr_P(PSTR("\r\n  ciphertext:  "));
386         cli_hexdump_block(plain, 4*128/8, 4, 16);
387         bcal_ofb_decMsg(iv, plain, 4*128, &ctx);
388         cli_putstr_P(PSTR("\r\n  plaintext:   "));
389         cli_hexdump_block(plain, 4*128/8, 4, 16);
390         bcal_ofb_free(&ctx);
391 }
392
393 void testrun_aes128_ctr(void){
394         uint8_t key[16];
395         uint8_t iv[16];
396         uint8_t plain[64];
397
398         bcal_ctr_ctx_t ctx;
399         uint8_t r;
400
401         memcpy_P(key,   modes_key,   16);
402         memcpy_P(iv,    modes_ctriv, 16);
403         memcpy_P(plain, modes_plain, 64);
404
405         cli_putstr_P(PSTR("\r\n** AES128-CTR-TEST **"));
406         r = bcal_ctr_init(&aes128_desc, key, 128, NULL, &ctx);
407         cli_putstr_P(PSTR("\r\n  init = 0x"));
408         cli_hexdump(&r, 1);
409         cli_putstr_P(PSTR("\r\n  key:   "));
410         cli_hexdump(key, 128/8);
411         cli_putstr_P(PSTR("\r\n  IV:    "));
412         cli_hexdump(iv, 128/8);
413         cli_putstr_P(PSTR("\r\n  plaintext:"));
414         cli_hexdump_block(plain, 4*128/8, 4, 16);
415         if(r)
416                 return;
417         bcal_ctr_encMsg(iv, plain, 4*128, &ctx);
418         cli_putstr_P(PSTR("\r\n  ciphertext:  "));
419         cli_hexdump_block(plain, 4*128/8, 4, 16);
420         bcal_ctr_decMsg(iv, plain, 4*128, &ctx);
421         cli_putstr_P(PSTR("\r\n  plaintext:   "));
422         cli_hexdump_block(plain, 4*128/8, 4, 16);
423         bcal_ctr_free(&ctx);
424 }
425
426 void testrun_aes128_cmac(void){
427         uint8_t key[16];
428         uint8_t tag[16];
429         uint8_t plain[64];
430         uint16_t length[] = { 0, 128, 320, 512 };
431         bcal_cmac_ctx_t ctx;
432         uint8_t r,i;
433
434         memcpy_P(key,   modes_key,   16);
435         memcpy_P(plain, modes_plain, 64);
436
437         cli_putstr_P(PSTR("\r\n** AES128-CMAC-TEST **"));
438
439         cli_putstr_P(PSTR("\r\n  key:   "));
440         cli_hexdump(key, 128/8);
441         for(i=0; i<4; ++i){
442                 r = bcal_cmac_init(&aes128_desc, key, 128, &ctx);
443                 cli_putstr_P(PSTR("\r\n  init = 0x"));
444                 cli_hexdump(&r, 1);
445                 cli_putstr_P(PSTR("\r\n  message: "));
446                 cli_hexdump_block(plain, length[i]/8, 4, 16);
447                 if(r)
448                         return;
449                 bcal_cmac(tag, 128, plain, length[i], &ctx);
450                 cli_putstr_P(PSTR("\r\n  tag:     "));
451                 cli_hexdump_block(tag, 128/8, 4, 16);
452                 bcal_cmac_free(&ctx);
453         }
454 }
455 /*
456 Klen = 16
457 Mlen = 18
458 Tlen = 2
459 Key = 3250974e306b4b678f914b514d1e90f6
460 Msg = cf132fd4ebc25fd3866f1a95a6193a1a9cdf
461 */
462 void testrun_aes128_cmac72(void){
463         uint8_t key[16]= {
464                         0x32, 0x50, 0x97, 0x4e, 0x30, 0x6b, 0x4b, 0x67,
465                         0x8f, 0x91, 0x4b, 0x51, 0x4d, 0x1e, 0x90, 0xf6
466         };
467         uint8_t tag[2];
468         uint8_t plain[18] = {
469                         0xcf, 0x13, 0x2f, 0xd4, 0xeb, 0xc2, 0x5f, 0xd3,
470                         0x86, 0x6f, 0x1a, 0x95, 0xa6, 0x19, 0x3a, 0x1a,
471                         0x9c, 0xdf,
472         };
473         bcal_cmac_ctx_t ctx;
474         uint8_t r;
475
476
477         cli_putstr_P(PSTR("\r\n** AES128-CMAC-72-TEST **"));
478
479         cli_putstr_P(PSTR("\r\n  key:   "));
480         cli_hexdump(key, 128/8);
481         r = bcal_cmac_init(&aes128_desc, key, 128, &ctx);
482         cli_putstr_P(PSTR("\r\n  init = 0x"));
483         cli_hexdump(&r, 1);
484         cli_putstr_P(PSTR("\r\n  message: "));
485         cli_hexdump_block(plain, 18, 4, 16);
486         if(r)
487                 return;
488         bcal_cmac(tag, 16, plain, 18*8, &ctx);
489         cli_putstr_P(PSTR("\r\n  tag:     "));
490         cli_hexdump_block(tag, 2, 4, 16);
491         bcal_cmac_free(&ctx);
492 }
493 /*
494 Count = 0
495 Klen = 24
496 Mlen = 0
497 Tlen = 2
498 Key = 2b2aaa666be161ed16648e862ac9bd1e317f71bc69e268b5
499 Msg = 00
500 */
501 void testrun_aes192_cmac0(void){
502         uint8_t key[24]= {
503                         0x2b, 0x2a, 0xaa, 0x66, 0x6b, 0xe1, 0x61, 0xed,
504                         0x16, 0x64, 0x8e, 0x86, 0x2a, 0xc9, 0xbd, 0x1e,
505                         0x31, 0x7f, 0x71, 0xbc, 0x69, 0xe2, 0x68, 0xb5
506         };
507         uint8_t tag[2];
508         uint8_t plain[1] = {
509                         0x00
510         };
511         bcal_cmac_ctx_t ctx;
512         uint8_t r;
513
514
515         cli_putstr_P(PSTR("\r\n** AES192-CMAC-0-TEST **"));
516
517         cli_putstr_P(PSTR("\r\n  key:   "));
518         cli_hexdump(key, 192/8);
519         r = bcal_cmac_init(&aes192_desc, key, 192, &ctx);
520         cli_putstr_P(PSTR("\r\n  init = 0x"));
521         cli_hexdump(&r, 1);
522         if(r)
523                 return;
524         bcal_cmac(tag, 16, plain, 0*8, &ctx);
525         cli_putstr_P(PSTR("\r\n  tag:     "));
526         cli_hexdump_block(tag, 2, 4, 16);
527         bcal_cmac_free(&ctx);
528 }
529
530 /*****************************************************************************/
531
532 void testrun_performance_aes128(void){
533         uint64_t t;
534         char str[16];
535         uint8_t key[32], data[16];
536         aes128_ctx_t ctx;
537
538         calibrateTimer();
539         print_overhead();
540
541         memset(key,  0, 32);
542         memset(data, 0, 16);
543
544         startTimer(1);
545         aes128_init(key, &ctx);
546         t = stopTimer();
547         cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
548         ultoa((unsigned long)t, str, 10);
549         cli_putstr(str);
550
551
552         startTimer(1);
553         aes128_enc(data, &ctx);
554         t = stopTimer();
555         cli_putstr_P(PSTR("\r\n\tencrypt time: "));
556         ultoa((unsigned long)t, str, 10);
557         cli_putstr(str);
558
559
560         startTimer(1);
561         aes128_dec(data, &ctx);
562         t = stopTimer();
563         cli_putstr_P(PSTR("\r\n\tdecrypt time: "));
564         ultoa((unsigned long)t, str, 10);
565         cli_putstr(str);
566
567         cli_putstr_P(PSTR("\r\n"));
568 }
569
570
571 void testrun_performance_aes192(void){
572         uint64_t t;
573         char str[16];
574         uint8_t key[32], data[16];
575         aes192_ctx_t ctx;
576
577         calibrateTimer();
578         print_overhead();
579
580         memset(key,  0, 32);
581         memset(data, 0, 16);
582
583         startTimer(1);
584         aes192_init(key, &ctx);
585         t = stopTimer();
586         cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
587         ultoa((unsigned long)t, str, 10);
588         cli_putstr(str);
589
590
591         startTimer(1);
592         aes192_enc(data, &ctx);
593         t = stopTimer();
594         cli_putstr_P(PSTR("\r\n\tencrypt time: "));
595         ultoa((unsigned long)t, str, 10);
596         cli_putstr(str);
597
598
599         startTimer(1);
600         aes192_dec(data, &ctx);
601         t = stopTimer();
602         cli_putstr_P(PSTR("\r\n\tdecrypt time: "));
603         ultoa((unsigned long)t, str, 10);
604         cli_putstr(str);
605
606         cli_putstr_P(PSTR("\r\n"));
607 }
608
609
610 void testrun_performance_aes256(void){
611         uint64_t t;
612         char str[16];
613         uint8_t key[32], data[16];
614         aes256_ctx_t ctx;
615
616         calibrateTimer();
617         print_overhead();
618
619         memset(key,  0, 32);
620         memset(data, 0, 16);
621
622         startTimer(1);
623         aes256_init(key, &ctx);
624         t = stopTimer();
625         cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
626         ultoa((unsigned long)t, str, 10);
627         cli_putstr(str);
628
629
630         startTimer(1);
631         aes256_enc(data, &ctx);
632         t = stopTimer();
633         cli_putstr_P(PSTR("\r\n\tencrypt time: "));
634         ultoa((unsigned long)t, str, 10);
635         cli_putstr(str);
636
637
638         startTimer(1);
639         aes256_dec(data, &ctx);
640         t = stopTimer();
641         cli_putstr_P(PSTR("\r\n\tdecrypt time: "));
642         ultoa((unsigned long)t, str, 10);
643         cli_putstr(str);
644
645         cli_putstr_P(PSTR("\r\n"));
646 }
647
648 void testrun_performance_aes(void){
649         cli_putstr_P(PSTR("\r\n -=AES Performance Test=-\r\n"));
650         cli_putstr_P(PSTR("\r\n       AES-128\r\n"));
651         testrun_performance_aes128();
652         cli_putstr_P(PSTR("\r\n       AES-192\r\n"));
653         testrun_performance_aes192();
654         cli_putstr_P(PSTR("\r\n       AES-256\r\n"));
655         testrun_performance_aes256();
656 }
657 /*****************************************************************************
658  *  main                                                                                                                                         *
659  *****************************************************************************/
660
661 const char nessie_str[]       PROGMEM = "nessie";
662 const char test_str[]         PROGMEM = "test";
663 const char testkey_str[]      PROGMEM = "testkey";
664 const char testcbc_str[]      PROGMEM = "testcbc";
665 const char testcfb8_str[]     PROGMEM = "testcfb8";
666 const char testcfb1_str[]     PROGMEM = "testcfb1";
667 const char testofb_str[]      PROGMEM = "testofb";
668 const char testctr_str[]      PROGMEM = "testctr";
669 const char testcmac_str[]     PROGMEM = "testcmac";
670 const char testcmac72_str[]   PROGMEM = "testcmac72";
671 const char testcmac0_str[]    PROGMEM = "testcmac0";
672 const char cmacvs_list_str[]  PROGMEM = "cmacvs_list";
673 const char cmacvs_set_str[]   PROGMEM = "cmacvs_set";
674 const char cmacvs_test1_str[] PROGMEM = "cmacvs_test1";
675 const char cmacvs_test2_str[] PROGMEM = "cmacvs_test2";
676 const char performance_str[]  PROGMEM = "performance";
677 const char dump_str[]         PROGMEM = "dump";
678 const char echo_str[]         PROGMEM = "echo";
679
680 cmdlist_entry_t cmdlist[] PROGMEM = {
681         { nessie_str,          NULL, testrun_nessie_aes              },
682         { test_str,            NULL, testrun_test_aes                },
683         { testkey_str,         NULL, testrun_testkey_aes             },
684         { testcbc_str,         NULL, testrun_aes128_cbc              },
685         { testcfb8_str,        NULL, testrun_aes128_cfb8             },
686         { testcfb1_str,        NULL, testrun_aes128_cfb1             },
687         { testofb_str,         NULL, testrun_aes128_ofb              },
688         { testctr_str,         NULL, testrun_aes128_ctr              },
689         { testcmac_str,        NULL, testrun_aes128_cmac             },
690         { testcmac72_str,      NULL, testrun_aes128_cmac72           },
691         { testcmac0_str,       NULL, testrun_aes192_cmac0            },
692         { cmacvs_list_str,     NULL, cmacvs_listalgos                },
693         { cmacvs_set_str,  (void*)1, (void_fpt)cmacvs_setalgo        },
694         { cmacvs_test1_str,    NULL, cmacvs_test1                    },
695         { cmacvs_test2_str,    NULL, cmacvs_test2                    },
696         { performance_str,     NULL, testrun_performance_aes         },
697         { dump_str,        (void*)1, (void_fpt)dump                  },
698         { echo_str,        (void*)1, (void_fpt)echo_ctrl             },
699         { NULL,                NULL, NULL                            }
700 };
701
702
703 int main (void){
704         DEBUG_INIT();
705
706         cli_rx = (cli_rx_fpt)uart0_getc;
707         cli_tx = (cli_tx_fpt)uart0_putc;
708         cmacvs_algolist=(bcdesc_t**)algolist;
709         cmacvs_algo=(bcdesc_t*)&aes128_desc;
710         for(;;){
711                 cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
712                 cli_putstr(algo_name);
713                 cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
714                 cmd_interface(cmdlist);
715         }
716 }
717