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