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/>.
25 #include "serial-tools.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"
38 #include "nessie_bc_test.h"
40 #include "performance_test.h"
45 #include <avr/pgmspace.h>
47 char* algo_name = "AES";
49 /*****************************************************************************
50 * additional validation-functions *
51 *****************************************************************************/
53 void testrun_nessie_aes(void){
54 nessie_bc_ctx.blocksize_B = 16;
55 nessie_bc_ctx.keysize_b = 128;
56 nessie_bc_ctx.name = algo_name;
57 nessie_bc_ctx.ctx_size_B = sizeof(aes128_ctx_t);
58 nessie_bc_ctx.cipher_enc = (nessie_bc_enc_fpt)aes128_enc;
59 nessie_bc_ctx.cipher_dec = (nessie_bc_dec_fpt)aes128_dec;
60 nessie_bc_ctx.cipher_genctx = (nessie_bc_gen_fpt)aes_init;
63 nessie_bc_ctx.keysize_b = 192;
64 nessie_bc_ctx.ctx_size_B = sizeof(aes192_ctx_t);
65 nessie_bc_ctx.cipher_enc = (nessie_bc_enc_fpt)aes192_enc;
66 nessie_bc_ctx.cipher_dec = (nessie_bc_dec_fpt)aes192_dec;
69 nessie_bc_ctx.keysize_b = 256;
70 nessie_bc_ctx.ctx_size_B = sizeof(aes256_ctx_t);
71 nessie_bc_ctx.cipher_enc = (nessie_bc_enc_fpt)aes256_enc;
72 nessie_bc_ctx.cipher_dec = (nessie_bc_dec_fpt)aes256_dec;
76 void testrun_test_aes(void){
77 uint8_t key[16] = { 0x2b, 0x7e, 0x15, 0x16,
78 0x28, 0xae, 0xd2, 0xa6,
79 0xab, 0xf7, 0x15, 0x88,
80 0x09, 0xcf, 0x4f, 0x3c };
81 uint8_t data[16] = { 0x32, 0x43, 0xf6, 0xa8,
82 0x88, 0x5a, 0x30, 0x8d,
83 0x31, 0x31, 0x98, 0xa2,
84 0xe0, 0x37, 0x07, 0x34 };
86 aes128_init(key, &ctx);
87 cli_putstr_P(PSTR("\r\n\r\n cipher test (FIPS 197):\r\n key: "));
89 cli_putstr_P(PSTR("\r\n plaintext: "));
90 cli_hexdump(data, 16);
91 aes128_enc(data, &ctx);
92 cli_putstr_P(PSTR("\r\n ciphertext: "));
93 cli_hexdump(data, 16);
94 aes128_dec(data, &ctx);
95 cli_putstr_P(PSTR("\r\n plaintext: "));
96 cli_hexdump(data, 16);
101 void testrun_testkey_aes128(void){
102 uint8_t key[16] = { 0x2b, 0x7e, 0x15, 0x16,
103 0x28, 0xae, 0xd2, 0xa6,
104 0xab, 0xf7, 0x15, 0x88,
105 0x09, 0xcf, 0x4f, 0x3c};
108 aes128_init(key, &ctx);
109 cli_putstr_P(PSTR("\r\n\r\n keyschedule test (FIPS 197):\r\n key: "));
110 cli_hexdump(key, 16);
112 cli_putstr_P(PSTR("\r\n index: "));
115 cli_putstr_P(PSTR(" roundkey "));
116 cli_hexdump(ctx.key[i].ks, 16);
120 void testrun_testkey_aes192(void){
121 uint8_t key[24] = { 0x8e, 0x73, 0xb0, 0xf7,
122 0xda, 0x0e, 0x64, 0x52,
123 0xc8, 0x10, 0xf3, 0x2b,
124 0x80, 0x90, 0x79, 0xe5,
125 0x62, 0xf8, 0xea, 0xd2,
126 0x52, 0x2c, 0x6b, 0x7b};
129 memset(&ctx, 0, sizeof(aes192_ctx_t));
130 aes192_init(key, &ctx);
131 cli_putstr_P(PSTR("\r\n\r\n keyschedule test (FIPS 197):\r\n key: "));
132 cli_hexdump(key, 24);
134 cli_putstr_P(PSTR("\r\n index: "));
137 cli_putstr_P(PSTR(" roundkey "));
138 cli_hexdump(ctx.key[i].ks, 16);
143 void testrun_testkey_aes256(void){
144 uint8_t key[32] = { 0x60, 0x3d, 0xeb, 0x10,
145 0x15, 0xca, 0x71, 0xbe,
146 0x2b, 0x73, 0xae, 0xf0,
147 0x85, 0x7d, 0x77, 0x81,
148 0x1f, 0x35, 0x2c, 0x07,
149 0x3b, 0x61, 0x08, 0xd7,
150 0x2d, 0x98, 0x10, 0xa3,
151 0x09, 0x14, 0xdf, 0xf4};
154 memset(&ctx, 0, sizeof(aes256_ctx_t));
155 aes256_init(key, &ctx);
156 cli_putstr_P(PSTR("\r\n\r\n keyschedule test (FIPS 197):\r\n key: "));
157 cli_hexdump(key, 32);
159 cli_putstr_P(PSTR("\r\n index: "));
162 cli_putstr_P(PSTR(" roundkey "));
163 cli_hexdump(ctx.key[i].ks, 16);
167 void testrun_testkey_aes(void){
168 testrun_testkey_aes128();
169 testrun_testkey_aes192();
170 testrun_testkey_aes256();
172 /*****************************************************************************/
174 void testrun_performance_aes128(void){
177 uint8_t key[32], data[16];
187 aes128_init(key, &ctx);
189 cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
190 ultoa((unsigned long)t, str, 10);
195 aes128_enc(data, &ctx);
197 cli_putstr_P(PSTR("\r\n\tencrypt time: "));
198 ultoa((unsigned long)t, str, 10);
203 aes128_dec(data, &ctx);
205 cli_putstr_P(PSTR("\r\n\tdecrypt time: "));
206 ultoa((unsigned long)t, str, 10);
209 cli_putstr_P(PSTR("\r\n"));
213 void testrun_performance_aes192(void){
216 uint8_t key[32], data[16];
226 aes192_init(key, &ctx);
228 cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
229 ultoa((unsigned long)t, str, 10);
234 aes192_enc(data, &ctx);
236 cli_putstr_P(PSTR("\r\n\tencrypt time: "));
237 ultoa((unsigned long)t, str, 10);
242 aes192_dec(data, &ctx);
244 cli_putstr_P(PSTR("\r\n\tdecrypt time: "));
245 ultoa((unsigned long)t, str, 10);
248 cli_putstr_P(PSTR("\r\n"));
252 void testrun_performance_aes256(void){
255 uint8_t key[32], data[16];
265 aes256_init(key, &ctx);
267 cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
268 ultoa((unsigned long)t, str, 10);
273 aes256_enc(data, &ctx);
275 cli_putstr_P(PSTR("\r\n\tencrypt time: "));
276 ultoa((unsigned long)t, str, 10);
281 aes256_dec(data, &ctx);
283 cli_putstr_P(PSTR("\r\n\tdecrypt time: "));
284 ultoa((unsigned long)t, str, 10);
287 cli_putstr_P(PSTR("\r\n"));
290 void testrun_performance_aes(void){
291 cli_putstr_P(PSTR("\r\n -=AES Performance Test=-\r\n"));
292 cli_putstr_P(PSTR("\r\n AES-128\r\n"));
293 testrun_performance_aes128();
294 cli_putstr_P(PSTR("\r\n AES-192\r\n"));
295 testrun_performance_aes192();
296 cli_putstr_P(PSTR("\r\n AES-256\r\n"));
297 testrun_performance_aes256();
299 /*****************************************************************************
301 *****************************************************************************/
303 const char nessie_str[] PROGMEM = "nessie";
304 const char test_str[] PROGMEM = "test";
305 const char testkey_str[] PROGMEM = "testkey";
306 const char performance_str[] PROGMEM = "performance";
307 const char echo_str[] PROGMEM = "echo";
309 cmdlist_entry_t cmdlist[] PROGMEM = {
310 { nessie_str, NULL, testrun_nessie_aes },
311 { test_str, NULL, testrun_test_aes},
312 { testkey_str, NULL, testrun_testkey_aes},
313 { performance_str, NULL, testrun_performance_aes},
314 { echo_str, (void*)1, (void_fpt)echo_ctrl},
325 cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
326 cli_putstr(algo_name);
327 cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
328 cmd_interface(cmdlist);