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