3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
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.
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.
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/>.
31 #include "nessie_bc_test.h"
33 #include "performance_test.h"
36 #include "bcal_aes128.h"
37 #include "bcal_aes192.h"
38 #include "bcal_aes256.h"
40 #include "bcal-cfb_byte.h"
41 #include "bcal-cfb_bit.h"
48 #include <avr/pgmspace.h>
50 char* algo_name = "AES";
52 /*****************************************************************************
53 * additional validation-functions *
54 *****************************************************************************/
56 void testrun_nessie_aes(void){
57 nessie_bc_ctx.blocksize_B = 16;
58 nessie_bc_ctx.keysize_b = 128;
59 nessie_bc_ctx.name = algo_name;
60 nessie_bc_ctx.ctx_size_B = sizeof(aes128_ctx_t);
61 nessie_bc_ctx.cipher_enc = (nessie_bc_enc_fpt)aes128_enc;
62 nessie_bc_ctx.cipher_dec = (nessie_bc_dec_fpt)aes128_dec;
63 nessie_bc_ctx.cipher_genctx = (nessie_bc_gen_fpt)aes_init;
66 nessie_bc_ctx.keysize_b = 192;
67 nessie_bc_ctx.ctx_size_B = sizeof(aes192_ctx_t);
68 nessie_bc_ctx.cipher_enc = (nessie_bc_enc_fpt)aes192_enc;
69 nessie_bc_ctx.cipher_dec = (nessie_bc_dec_fpt)aes192_dec;
72 nessie_bc_ctx.keysize_b = 256;
73 nessie_bc_ctx.ctx_size_B = sizeof(aes256_ctx_t);
74 nessie_bc_ctx.cipher_enc = (nessie_bc_enc_fpt)aes256_enc;
75 nessie_bc_ctx.cipher_dec = (nessie_bc_dec_fpt)aes256_dec;
79 void testrun_test_aes(void){
80 uint8_t key[16] = { 0x2b, 0x7e, 0x15, 0x16,
81 0x28, 0xae, 0xd2, 0xa6,
82 0xab, 0xf7, 0x15, 0x88,
83 0x09, 0xcf, 0x4f, 0x3c };
84 uint8_t data[16] = { 0x32, 0x43, 0xf6, 0xa8,
85 0x88, 0x5a, 0x30, 0x8d,
86 0x31, 0x31, 0x98, 0xa2,
87 0xe0, 0x37, 0x07, 0x34 };
89 aes128_init(key, &ctx);
90 cli_putstr_P(PSTR("\r\n\r\n cipher test (FIPS 197):\r\n key: "));
92 cli_putstr_P(PSTR("\r\n plaintext: "));
93 cli_hexdump(data, 16);
94 aes128_enc(data, &ctx);
95 cli_putstr_P(PSTR("\r\n ciphertext: "));
96 cli_hexdump(data, 16);
97 aes128_dec(data, &ctx);
98 cli_putstr_P(PSTR("\r\n plaintext: "));
99 cli_hexdump(data, 16);
100 cli_putstr(PSTR("\r\n testing bcal:"));
101 bcgen_ctx_t bcal_ctx;
103 r = bcal_cipher_init(&aes128_desc, key, 128, &bcal_ctx);
104 cli_putstr_P(PSTR("\r\n init = 0x"));
107 bcal_cipher_enc(data, &bcal_ctx);
108 cli_putstr_P(PSTR("\r\n ciphertext: "));
109 cli_hexdump(data, 16);
110 bcal_cipher_dec(data, &bcal_ctx);
111 cli_putstr_P(PSTR("\r\n plaintext: "));
112 cli_hexdump(data, 16);
113 bcal_cipher_free(&bcal_ctx);
116 void testrun_testkey_aes128(void){
117 uint8_t key[16] = { 0x2b, 0x7e, 0x15, 0x16,
118 0x28, 0xae, 0xd2, 0xa6,
119 0xab, 0xf7, 0x15, 0x88,
120 0x09, 0xcf, 0x4f, 0x3c};
123 aes128_init(key, &ctx);
124 cli_putstr_P(PSTR("\r\n\r\n keyschedule test (FIPS 197):\r\n key: "));
125 cli_hexdump(key, 16);
127 cli_putstr_P(PSTR("\r\n index: "));
130 cli_putstr_P(PSTR(" roundkey "));
131 cli_hexdump(ctx.key[i].ks, 16);
135 void testrun_testkey_aes192(void){
136 uint8_t key[24] = { 0x8e, 0x73, 0xb0, 0xf7,
137 0xda, 0x0e, 0x64, 0x52,
138 0xc8, 0x10, 0xf3, 0x2b,
139 0x80, 0x90, 0x79, 0xe5,
140 0x62, 0xf8, 0xea, 0xd2,
141 0x52, 0x2c, 0x6b, 0x7b};
144 memset(&ctx, 0, sizeof(aes192_ctx_t));
145 aes192_init(key, &ctx);
146 cli_putstr_P(PSTR("\r\n\r\n keyschedule test (FIPS 197):\r\n key: "));
147 cli_hexdump(key, 24);
149 cli_putstr_P(PSTR("\r\n index: "));
152 cli_putstr_P(PSTR(" roundkey "));
153 cli_hexdump(ctx.key[i].ks, 16);
158 void testrun_testkey_aes256(void){
159 uint8_t key[32] = { 0x60, 0x3d, 0xeb, 0x10,
160 0x15, 0xca, 0x71, 0xbe,
161 0x2b, 0x73, 0xae, 0xf0,
162 0x85, 0x7d, 0x77, 0x81,
163 0x1f, 0x35, 0x2c, 0x07,
164 0x3b, 0x61, 0x08, 0xd7,
165 0x2d, 0x98, 0x10, 0xa3,
166 0x09, 0x14, 0xdf, 0xf4};
169 memset(&ctx, 0, sizeof(aes256_ctx_t));
170 aes256_init(key, &ctx);
171 cli_putstr_P(PSTR("\r\n\r\n keyschedule test (FIPS 197):\r\n key: "));
172 cli_hexdump(key, 32);
174 cli_putstr_P(PSTR("\r\n index: "));
177 cli_putstr_P(PSTR(" roundkey "));
178 cli_hexdump(ctx.key[i].ks, 16);
182 void testrun_testkey_aes(void){
183 testrun_testkey_aes128();
184 testrun_testkey_aes192();
185 testrun_testkey_aes256();
188 uint8_t modes_key[] PROGMEM = {
189 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
190 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c,
191 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
192 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
194 uint8_t modes_iv[] PROGMEM = {
195 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
196 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
199 uint8_t modes_ctriv[] PROGMEM = {
200 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
201 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
204 uint8_t modes_plain[] PROGMEM = {
205 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
206 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
208 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
209 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
211 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
212 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
214 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
215 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
219 void testrun_aes128_cbc(void){
227 memcpy_P(key, modes_key, 16);
228 memcpy_P(iv, modes_iv, 16);
229 memcpy_P(plain, modes_plain, 64);
231 cli_putstr_P(PSTR("\r\n** AES128-CBC-TEST **"));
232 r = bcal_cbc_init(&aes128_desc, key, 128, &ctx);
233 cli_putstr_P(PSTR("\r\n init = 0x"));
235 cli_putstr_P(PSTR("\r\n key: "));
236 cli_hexdump(key, 128/8);
237 cli_putstr_P(PSTR("\r\n IV: "));
238 cli_hexdump(iv, 128/8);
239 cli_putstr_P(PSTR("\r\n plaintext:"));
240 cli_hexdump_block(plain, 4*128/8, 4, 16);
243 bcal_cbc_encMsg(iv, plain, 4, &ctx);
244 cli_putstr_P(PSTR("\r\n ciphertext: "));
245 cli_hexdump_block(plain, 4*128/8, 4, 16);
246 bcal_cbc_decMsg(iv, plain, 4, &ctx);
247 cli_putstr_P(PSTR("\r\n plaintext: "));
248 cli_hexdump_block(plain, 4*128/8, 4, 16);
252 void testrun_aes128_cfb8(void){
257 bcal_cfb_B_ctx_t ctx;
260 memcpy_P(key, modes_key, 16);
261 memcpy_P(iv, modes_iv, 16);
262 memcpy_P(plain, modes_plain, 64);
264 cli_putstr_P(PSTR("\r\n** AES128-CFB8-TEST **"));
265 r = bcal_cfb_B_init(&aes128_desc, key, 128, 8, &ctx);
266 cli_putstr_P(PSTR("\r\n init = 0x"));
268 cli_putstr_P(PSTR("\r\n key: "));
269 cli_hexdump(key, 128/8);
270 cli_putstr_P(PSTR("\r\n IV: "));
271 cli_hexdump(iv, 128/8);
272 cli_putstr_P(PSTR("\r\n plaintext:"));
273 cli_hexdump_block(plain, 4*128/8, 4, 16);
276 bcal_cfb_B_encMsg(iv, plain, 64, &ctx);
277 cli_putstr_P(PSTR("\r\n ciphertext: "));
278 cli_hexdump_block(plain, 64, 4, 16);
280 bcal_cfb_B_decMsg(iv, plain, 64, &ctx);
281 cli_putstr_P(PSTR("\r\n plaintext: "));
282 cli_hexdump_block(plain, 64, 4, 16);
284 bcal_cfb_B_free(&ctx);
288 void testrun_aes128_cfb1(void){
293 bcal_cfb_b_ctx_t ctx;
296 memcpy_P(key, modes_key, 16);
297 memcpy_P(iv, modes_iv, 16);
298 memcpy_P(plain, modes_plain, 64);
300 cli_putstr_P(PSTR("\r\n** AES128-CFB1-TEST **"));
301 r = bcal_cfb_b_init(&aes128_desc, key, 128, 1, &ctx);
302 cli_putstr_P(PSTR("\r\n init = 0x"));
304 cli_putstr_P(PSTR("\r\n key: "));
305 cli_hexdump(key, 128/8);
306 cli_putstr_P(PSTR("\r\n IV: "));
307 cli_hexdump(iv, 128/8);
308 cli_putstr_P(PSTR("\r\n plaintext:"));
309 cli_hexdump_block(plain, 2, 4, 16);
312 uint8_t i, bit_offset, byte_offset;
313 bcal_cfb_b_loadIV(iv, &ctx);
317 cli_putstr_P(PSTR("\r\n plain bit: "));
318 cli_putc((plain[byte_offset]&(1<<(7-bit_offset)))?'1':'0');
319 bcal_cfb_b_encNext(plain+byte_offset, bit_offset, &ctx);
320 cli_putstr_P(PSTR("\r\n cipher bit: "));
321 cli_putc((plain[byte_offset]&(1<<(7-bit_offset)))?'1':'0');
323 cli_putstr_P(PSTR("\r\n ciphertext: "));
324 cli_hexdump_block(plain, 2, 4, 16);
326 bcal_cfb_b_loadIV(iv, &ctx);
330 cli_putstr_P(PSTR("\r\n plain bit: "));
331 cli_putc((plain[byte_offset]&(1<<(7-bit_offset)))?'1':'0');
332 bcal_cfb_b_decNext(plain+byte_offset, bit_offset, &ctx);
333 cli_putstr_P(PSTR("\r\n cipher bit: "));
334 cli_putc((plain[byte_offset]&(1<<(7-bit_offset)))?'1':'0');
336 cli_putstr_P(PSTR("\r\n plaintext: "));
337 cli_hexdump_block(plain, 2, 4, 16);
340 bcal_cfb_b_encMsg(iv, plain, 0, 64*8, &ctx);
341 cli_putstr_P(PSTR("\r\n ciphertext: "));
342 cli_hexdump_block(plain, 64, 4, 16);
344 bcal_cfb_b_decMsg(iv, plain, 0, 64*8, &ctx);
345 cli_putstr_P(PSTR("\r\n plaintext: "));
346 cli_hexdump_block(plain, 64, 4, 16);
348 bcal_cfb_b_free(&ctx);
351 void testrun_aes128_ofb(void){
359 memcpy_P(key, modes_key, 16);
360 memcpy_P(iv, modes_iv, 16);
361 memcpy_P(plain, modes_plain, 64);
363 cli_putstr_P(PSTR("\r\n** AES128-OFB-TEST **"));
364 r = bcal_ofb_init(&aes128_desc, key, 128, &ctx);
365 cli_putstr_P(PSTR("\r\n init = 0x"));
367 cli_putstr_P(PSTR("\r\n key: "));
368 cli_hexdump(key, 128/8);
369 cli_putstr_P(PSTR("\r\n IV: "));
370 cli_hexdump(iv, 128/8);
371 cli_putstr_P(PSTR("\r\n plaintext:"));
372 cli_hexdump_block(plain, 4*128/8, 4, 16);
375 bcal_ofb_encMsg(iv, plain, 4*128, &ctx);
376 cli_putstr_P(PSTR("\r\n ciphertext: "));
377 cli_hexdump_block(plain, 4*128/8, 4, 16);
378 bcal_ofb_decMsg(iv, plain, 4*128, &ctx);
379 cli_putstr_P(PSTR("\r\n plaintext: "));
380 cli_hexdump_block(plain, 4*128/8, 4, 16);
384 void testrun_aes128_ctr(void){
392 memcpy_P(key, modes_key, 16);
393 memcpy_P(iv, modes_ctriv, 16);
394 memcpy_P(plain, modes_plain, 64);
396 cli_putstr_P(PSTR("\r\n** AES128-CTR-TEST **"));
397 r = bcal_ctr_init(&aes128_desc, key, 128, NULL, &ctx);
398 cli_putstr_P(PSTR("\r\n init = 0x"));
400 cli_putstr_P(PSTR("\r\n key: "));
401 cli_hexdump(key, 128/8);
402 cli_putstr_P(PSTR("\r\n IV: "));
403 cli_hexdump(iv, 128/8);
404 cli_putstr_P(PSTR("\r\n plaintext:"));
405 cli_hexdump_block(plain, 4*128/8, 4, 16);
408 bcal_ctr_encMsg(iv, plain, 4*128, &ctx);
409 cli_putstr_P(PSTR("\r\n ciphertext: "));
410 cli_hexdump_block(plain, 4*128/8, 4, 16);
411 bcal_ctr_decMsg(iv, plain, 4*128, &ctx);
412 cli_putstr_P(PSTR("\r\n plaintext: "));
413 cli_hexdump_block(plain, 4*128/8, 4, 16);
417 /*****************************************************************************/
419 void testrun_performance_aes128(void){
422 uint8_t key[32], data[16];
432 aes128_init(key, &ctx);
434 cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
435 ultoa((unsigned long)t, str, 10);
440 aes128_enc(data, &ctx);
442 cli_putstr_P(PSTR("\r\n\tencrypt time: "));
443 ultoa((unsigned long)t, str, 10);
448 aes128_dec(data, &ctx);
450 cli_putstr_P(PSTR("\r\n\tdecrypt time: "));
451 ultoa((unsigned long)t, str, 10);
454 cli_putstr_P(PSTR("\r\n"));
458 void testrun_performance_aes192(void){
461 uint8_t key[32], data[16];
471 aes192_init(key, &ctx);
473 cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
474 ultoa((unsigned long)t, str, 10);
479 aes192_enc(data, &ctx);
481 cli_putstr_P(PSTR("\r\n\tencrypt time: "));
482 ultoa((unsigned long)t, str, 10);
487 aes192_dec(data, &ctx);
489 cli_putstr_P(PSTR("\r\n\tdecrypt time: "));
490 ultoa((unsigned long)t, str, 10);
493 cli_putstr_P(PSTR("\r\n"));
497 void testrun_performance_aes256(void){
500 uint8_t key[32], data[16];
510 aes256_init(key, &ctx);
512 cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
513 ultoa((unsigned long)t, str, 10);
518 aes256_enc(data, &ctx);
520 cli_putstr_P(PSTR("\r\n\tencrypt time: "));
521 ultoa((unsigned long)t, str, 10);
526 aes256_dec(data, &ctx);
528 cli_putstr_P(PSTR("\r\n\tdecrypt time: "));
529 ultoa((unsigned long)t, str, 10);
532 cli_putstr_P(PSTR("\r\n"));
535 void testrun_performance_aes(void){
536 cli_putstr_P(PSTR("\r\n -=AES Performance Test=-\r\n"));
537 cli_putstr_P(PSTR("\r\n AES-128\r\n"));
538 testrun_performance_aes128();
539 cli_putstr_P(PSTR("\r\n AES-192\r\n"));
540 testrun_performance_aes192();
541 cli_putstr_P(PSTR("\r\n AES-256\r\n"));
542 testrun_performance_aes256();
544 /*****************************************************************************
546 *****************************************************************************/
548 const char nessie_str[] PROGMEM = "nessie";
549 const char test_str[] PROGMEM = "test";
550 const char testkey_str[] PROGMEM = "testkey";
551 const char testcbc_str[] PROGMEM = "testcbc";
552 const char testcfb8_str[] PROGMEM = "testcfb8";
553 const char testcfb1_str[] PROGMEM = "testcfb1";
554 const char testofb_str[] PROGMEM = "testofb";
555 const char testctr_str[] PROGMEM = "testctr";
556 const char performance_str[] PROGMEM = "performance";
557 const char dump_str[] PROGMEM = "dump";
558 const char echo_str[] PROGMEM = "echo";
560 cmdlist_entry_t cmdlist[] PROGMEM = {
561 { nessie_str, NULL, testrun_nessie_aes },
562 { test_str, NULL, testrun_test_aes},
563 { testkey_str, NULL, testrun_testkey_aes},
564 { testcbc_str, NULL, testrun_aes128_cbc},
565 { testcfb8_str, NULL, testrun_aes128_cfb8},
566 { testcfb1_str, NULL, testrun_aes128_cfb1},
567 { testofb_str, NULL, testrun_aes128_ofb},
568 { testctr_str, NULL, testrun_aes128_ctr},
569 { performance_str, NULL, testrun_performance_aes},
570 { dump_str, (void*)1, (void_fpt)dump},
571 { echo_str, (void*)1, (void_fpt)echo_ctrl},
579 cli_rx = (cli_rx_fpt)uart0_getc;
580 cli_tx = (cli_tx_fpt)uart0_putc;
582 cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
583 cli_putstr(algo_name);
584 cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
585 cmd_interface(cmdlist);