]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-cscipher-test.c
adding CS-Cipher
[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 "nessie_bc_test.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* 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         nessie_bc_init();
57         nessie_bc_ctx.blocksize_B =   8;
58         nessie_bc_ctx.keysize_b   = 128;
59         nessie_bc_ctx.name        = algo_name;
60         nessie_bc_ctx.ctx_size_B  = sizeof(cscipher_ctx_t);
61         nessie_bc_ctx.cipher_enc  = (nessie_bc_enc_fpt)cscipher_enc;
62         nessie_bc_ctx.cipher_dec  = (nessie_bc_dec_fpt)cscipher_dec;
63         nessie_bc_ctx.cipher_free = (nessie_bc_free_fpt)NULL;
64         nessie_bc_ctx.cipher_genctx  = (nessie_bc_gen_fpt)cscipher_init_dummy;
65         
66         nessie_bc_run();
67 }
68
69 void testrun_cscipher(void){
70         uint8_t data[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
71         uint8_t key[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
72                                          0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
73         cscipher_ctx_t ctx;
74         cli_putstr_P(PSTR("\r\n== CS-Cipher test==\r\nkey: "));
75         cli_hexdump(key, 16);
76         memset(&ctx, 0, 8*9);
77         cscipher_init(key, &ctx);
78         cli_putstr_P(PSTR("\r\nround keys:\r\n"));
79         cli_hexdump_block(&ctx, 8*9, 4, 8);
80         cli_putstr_P(PSTR("\r\nplain:  "));
81         cli_hexdump(data, 8);
82         cscipher_enc(data, &ctx);
83         cli_putstr_P(PSTR("\r\ncipher: "));
84         cli_hexdump(data, 8);
85         cscipher_dec(data, &ctx);
86         cli_putstr_P(PSTR("\r\nplain:  "));
87         cli_hexdump(data, 8);
88 }
89
90 void testrun_long_cscipher(void){
91         uint8_t data[8];
92         char str[10];
93         uint16_t i;
94         uint8_t j;
95         uint8_t key[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
96                                          0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
97         cscipher_ctx_t ctx;
98         cli_putstr_P(PSTR("\r\n== CS-Cipher test==\r\nkey: "));
99         cli_hexdump(key, 16);
100         cscipher_init(key, &ctx);
101         memset(data, 0, 8);
102         cli_putstr_P(PSTR("\r\nplain:  "));
103         cli_hexdump(data, 8);
104         cli_putstr_P(PSTR("\r\nencrypting 1,000,000 times:\r\n"));
105         for(i=0; i<10000;++i){
106                 for(j=0;j<100;++j){
107                         cscipher_enc(data, &ctx);
108                 }
109                 if(i%128==0){
110                         itoa(i, str, 10);
111                         cli_putstr_P(PSTR("\r\n"));
112                         cli_putstr(str);
113                         cli_putstr_P(PSTR(": "));
114                 }
115                 cli_putc('*');
116
117         }
118         cli_putstr_P(PSTR("\r\ncipher: "));
119         cli_hexdump(data, 8);
120         cli_putstr_P(PSTR("\r\ndecrypting 1,000,000 times:"));
121         for(i=0; i<10000;++i){
122                 for(j=0;j<100;++j){
123                         cscipher_dec(data, &ctx);
124                 }
125                 if(i%128==0){
126                         itoa(i, str, 10);
127                         cli_putstr_P(PSTR("\r\n"));
128                         cli_putstr(str);
129                         cli_putstr_P(PSTR(": "));
130                 }
131                 cli_putc('*');
132         }
133         cli_putstr_P(PSTR("\r\nplain:  "));
134         cli_hexdump(data, 8);
135 }
136
137
138 void testrun_performance_cscipher(void){
139         bcal_performance_multiple(algolist);
140 }
141 /*****************************************************************************
142  *  main                                                                                                                                         *
143  *****************************************************************************/
144
145 const char nessie_str[]      PROGMEM = "nessie";
146 const char test_str[]        PROGMEM = "test";
147 const char longtest_str[]    PROGMEM = "longtest";
148 const char performance_str[] PROGMEM = "performance";
149 const char echo_str[]        PROGMEM = "echo";
150
151 cmdlist_entry_t cmdlist[] PROGMEM = {
152         { nessie_str,      NULL, testrun_nessie_cscipher },
153         { test_str,        NULL, testrun_cscipher},
154         { longtest_str,    NULL, testrun_long_cscipher},
155         { performance_str, NULL, testrun_performance_cscipher},
156         { echo_str,    (void*)1, (void_fpt)echo_ctrl},
157         { NULL,            NULL, NULL}
158 };
159
160 int main (void){
161         DEBUG_INIT();
162         
163         cli_rx = (cli_rx_fpt)uart0_getc;
164         cli_tx = (cli_tx_fpt)uart0_putc;                
165         for(;;){
166                 cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
167                 cli_putstr(algo_name);
168                 cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
169                 cmd_interface(cmdlist);
170         }
171 }