]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-cscipher-test.c
eb49a4500281a58f391dc85a4849bd0584058496
[avr-crypto-lib.git] / test_src / main-cscipher-test.c
1 /* main-cscipher-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  * cscipher test-suit
21  * 
22 */
23
24 #include "config.h"
25
26 #include "uart_i.h"
27 #include "debug.h"
28
29 #include "cscipher.h"
30 #include "bcal-nessie.h"
31 #include "cli.h"
32 #include "performance_test.h"
33 #include "bcal-performance.h"
34 #include "bcal_cscipher.h"
35
36 #include <stdint.h>
37 #include <string.h>
38 #include <stdlib.h>
39
40 char* algo_name = "CS-Cipher";
41
42 const bcdesc_t* const algolist[] PROGMEM = {
43         (bcdesc_t*)&cscipher_desc,
44         NULL
45 };
46
47 /*****************************************************************************
48  *  additional validation-functions                                                                                      *
49  *****************************************************************************/
50
51 void cscipher_init_dummy(const uint8_t* key, uint16_t keysize_b, void* ctx){
52         cscipher_init(key, ctx);
53 }
54
55 void testrun_nessie_cscipher(void){
56         bcal_nessie(&cscipher_desc);
57 }
58
59 void testrun_cscipher(void){
60         uint8_t data[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
61         uint8_t key[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
62                                          0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
63         cscipher_ctx_t ctx;
64         cli_putstr_P(PSTR("\r\n== CS-Cipher test==\r\nkey: "));
65         cli_hexdump(key, 16);
66         memset(&ctx, 0, 8*9);
67         cscipher_init(key, &ctx);
68         cli_putstr_P(PSTR("\r\nround keys:\r\n"));
69         cli_hexdump_block(&ctx, 8*9, 4, 8);
70         cli_putstr_P(PSTR("\r\nplain:  "));
71         cli_hexdump(data, 8);
72         cscipher_enc(data, &ctx);
73         cli_putstr_P(PSTR("\r\ncipher: "));
74         cli_hexdump(data, 8);
75         cscipher_dec(data, &ctx);
76         cli_putstr_P(PSTR("\r\nplain:  "));
77         cli_hexdump(data, 8);
78 }
79
80 void testrun_long_cscipher(void){
81         uint8_t data[8];
82         char str[10];
83         uint16_t i;
84         uint8_t j;
85         uint8_t key[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
86                                          0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
87         cscipher_ctx_t ctx;
88         cli_putstr_P(PSTR("\r\n== CS-Cipher test==\r\nkey: "));
89         cli_hexdump(key, 16);
90         cscipher_init(key, &ctx);
91         memset(data, 0, 8);
92         cli_putstr_P(PSTR("\r\nplain:  "));
93         cli_hexdump(data, 8);
94         cli_putstr_P(PSTR("\r\nencrypting 1,000,000 times:\r\n"));
95         for(i=0; i<10000;++i){
96                 for(j=0;j<100;++j){
97                         cscipher_enc(data, &ctx);
98                 }
99                 if(i%128==0){
100                         itoa(i, str, 10);
101                         cli_putstr_P(PSTR("\r\n"));
102                         cli_putstr(str);
103                         cli_putstr_P(PSTR(": "));
104                 }
105                 cli_putc('*');
106
107         }
108         cli_putstr_P(PSTR("\r\ncipher: "));
109         cli_hexdump(data, 8);
110         cli_putstr_P(PSTR("\r\ndecrypting 1,000,000 times:"));
111         for(i=0; i<10000;++i){
112                 for(j=0;j<100;++j){
113                         cscipher_dec(data, &ctx);
114                 }
115                 if(i%128==0){
116                         itoa(i, str, 10);
117                         cli_putstr_P(PSTR("\r\n"));
118                         cli_putstr(str);
119                         cli_putstr_P(PSTR(": "));
120                 }
121                 cli_putc('*');
122         }
123         cli_putstr_P(PSTR("\r\nplain:  "));
124         cli_hexdump(data, 8);
125 }
126
127
128 void testrun_performance_cscipher(void){
129         bcal_performance_multiple(algolist);
130 }
131 /*****************************************************************************
132  *  main                                                                                                                                         *
133  *****************************************************************************/
134
135 const char nessie_str[]      PROGMEM = "nessie";
136 const char test_str[]        PROGMEM = "test";
137 const char longtest_str[]    PROGMEM = "longtest";
138 const char performance_str[] PROGMEM = "performance";
139 const char echo_str[]        PROGMEM = "echo";
140
141 const cmdlist_entry_t cmdlist[] PROGMEM = {
142         { nessie_str,      NULL, testrun_nessie_cscipher },
143         { test_str,        NULL, testrun_cscipher},
144         { longtest_str,    NULL, testrun_long_cscipher},
145         { performance_str, NULL, testrun_performance_cscipher},
146         { echo_str,    (void*)1, (void_fpt)echo_ctrl},
147         { NULL,            NULL, NULL}
148 };
149
150 int main (void){
151         DEBUG_INIT();
152         
153         cli_rx = (cli_rx_fpt)uart0_getc;
154         cli_tx = (cli_tx_fpt)uart0_putc;                
155         for(;;){
156                 cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
157                 cli_putstr(algo_name);
158                 cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
159                 cmd_interface(cmdlist);
160         }
161 }