]> git.cryptolib.org Git - arm-crypto-lib.git/blob - test_src/main-base64-test.c
switching to dedicated endian switching function
[arm-crypto-lib.git] / test_src / main-base64-test.c
1 /* main-base64-test.c */
2 /*
3     This file is part of the ARM-Crypto-Lib.
4     Copyright (C) 2008, 2009  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  * base64 test-suit
21  * 
22 */
23
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <string.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 "noekeon.h"
35 #include "noekeon_prng.h"
36 #include "base64_enc.h"
37 #include "base64_dec.h"
38 #include "string-extras.h"
39 #include "cli.h"
40 #include "performance_test.h"
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 char* algo_name = "Base64";
51
52 /*****************************************************************************
53  *  additional validation-functions                                                                                      *
54  *****************************************************************************/
55 #define TESTLENGTH 4096
56
57 void testrun_stdtest_base64(void){
58         uint8_t fail=0;
59         unsigned l,i;
60         int     sl;
61         char str[32];
62         uint8_t bin_buffer[TESTLENGTH];
63         char    b64_buffer[TESTLENGTH*4/3+10];
64         uint8_t bin_buffer2[TESTLENGTH];
65         random_seed(bin_buffer);
66         
67         for(l=0; l<TESTLENGTH-1; ++l){
68                 cli_putstr("\r\nTest ");
69                 ultoa(l, str, 10);
70                 cli_putstr(str);
71                 
72                 uart_flush(0);
73                 for(i=0; i<l; ++i){
74                         bin_buffer[i] = random8();
75                 }
76                 cli_putstr("\r\n bin: ");
77                 cli_hexdump(bin_buffer, l);
78
79                 uart_flush(0);
80                 base64enc(b64_buffer, bin_buffer, l);
81                 cli_putstr("\r\n b64: ");
82                 cli_putstr(b64_buffer);
83
84                 uart_flush(0);
85                 sl = base64_binlength(b64_buffer, 1);
86                 
87                 if(sl!=l){
88                         cli_putstr("\r\n ERROR length ");
89                         cli_putstr(str);
90                         if(sl!=-1){
91                                 cli_putstr(" != ");
92                                 ultoa(l, str, 10);
93                                 cli_putstr(str);
94                         }else{
95                                 cli_putstr(" != -1");
96                         }
97                         fail=1;
98                 }else{
99                         cli_putstr("\r\n length ok");
100                         uart_flush(0);
101                 }
102                 uart_flush(0);
103                 base64dec(bin_buffer2, b64_buffer, 1);
104                 if(memcmp(bin_buffer, bin_buffer2, l)){
105                         cli_putstr("\r\n ERROR value\r\n out: ");
106                         cli_hexdump(bin_buffer2, l);
107                         fail=1;
108                 }else{
109                         cli_putstr("\r\n value ok");
110                         uart_flush(0);
111                 }
112                 if(fail)
113                         break;
114         }       
115         cli_putstr(fail?"\r\n [FAIL]\r\n":"\r\n [OK]\r\n");
116 }
117
118 void testrun_performance_base64(void){
119 /*
120         uint64_t t;
121         char str[16];
122         uint8_t key[16], data[16];
123         noekeon_ctx_t ctx;
124         
125         calibrateTimer();
126         print_overhead();
127         
128         memset(key,  0, 16);
129         memset(data, 0, 16);
130         
131         startTimer(1);
132         noekeon_init(key, &ctx);
133         t = stopTimer();
134         cli_putstr("\r\n\tctx-gen time: ");
135         ultoa((unsigned long)t, str, 10);
136         cli_putstr(str);        
137         
138         startTimer(1);
139         noekeon_enc(data, &ctx);
140         t = stopTimer();
141         cli_putstr("\r\n\tencrypt time: ");
142         ultoa((unsigned long)t, str, 10);
143         cli_putstr(str);        
144         
145         startTimer(1);
146         noekeon_dec(data, &ctx);
147         t = stopTimer();
148         cli_putstr("\r\n\tdecrypt time: ");
149         ultoa((unsigned long)t, str, 10);
150         cli_putstr(str);
151         
152         cli_putstr("\r\n");
153 */
154 }
155 /*****************************************************************************
156  *  main                                                                                                                                         *
157  *****************************************************************************/
158
159 const char test_str[]        = "test";
160 const char performance_str[] = "performance";
161 const char echo_str[]        = "echo";
162
163 const cmdlist_entry_t cmdlist[] = {
164         { test_str,        NULL, testrun_stdtest_base64},
165         { performance_str, NULL, testrun_performance_base64},
166         { echo_str,    (void*)1, (void_fpt)echo_ctrl},
167         { NULL,            NULL, NULL}
168 };
169
170 int main (void){
171         sysclk_set_freq(SYS_FREQ);
172         sysclk_mosc_verify_enable();
173         uart_init(UART_0, 115200, 8, UART_PARATY_NONE, UART_STOPBITS_ONE);
174         gptm_set_timer_32periodic(TIMER0);
175
176         cli_rx = uart0_getc;
177         cli_tx = uart0_putc;
178         
179         for(;;){
180                 cli_putstr("\r\n\r\nARM-Crypto-Lib VS (");
181                 cli_putstr(algo_name);
182                 cli_putstr("; ");
183                 cli_putstr(__DATE__);
184                 cli_putc(' ');
185                 cli_putstr(__TIME__);
186                 cli_putstr(")\r\nloaded and running\r\n");
187                 cmd_interface(cmdlist);
188         }
189 }