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