]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-base64-test.c
clean up
[avr-crypto-lib.git] / test_src / main-base64-test.c
1 /* main-base64-test.c */
2 /*
3     This file is part of the AVR-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 "config.h"
25
26 #include "uart_i.h"
27 #include "debug.h"
28
29 #include "noekeon/noekeon.h"
30 #include "noekeon/noekeon_prng.h"
31 #include "base64_enc.h"
32 #include "base64_dec.h"
33
34 #include "cli.h"
35 #include "performance_test.h"
36
37 #include <stdint.h>
38 #include <string.h>
39 #include <stdlib.h>
40
41 char* algo_name = "Base64";
42
43 /*****************************************************************************
44  *  additional validation-functions                                                                                      *
45  *****************************************************************************/
46
47 void testrun_stdtest_base64(void){
48         uint8_t fail=0;
49         uint8_t l,i; 
50         int     sl;
51         char str[10];
52         uint8_t bin_buffer[256];
53         char    b64_buffer[256*4/3+10];
54         uint8_t bin_buffer2[256];
55         random_seed(bin_buffer);
56         
57         for(l=0; l<255; ++l){
58                 cli_putstr_P(PSTR("\r\nTest "));
59                 utoa(l, str, 10);
60                 cli_putstr(str);
61                 
62                 for(i=0; i<l; ++i){
63                         bin_buffer[i] = random8();
64                 }
65                 cli_putstr_P(PSTR("\r\n bin: "));
66                 cli_hexdump(bin_buffer, l);
67                 
68                 base64enc(b64_buffer, bin_buffer, l);
69                 cli_putstr_P(PSTR("\r\n b64: "));
70                 cli_putstr(b64_buffer);
71                 
72                 sl = base64_binlength(b64_buffer, 1);
73                 
74                 if(sl!=l){
75                         cli_putstr_P(PSTR("\r\n ERROR length "));
76                         cli_putstr(str);
77                         if(sl!=-1){
78                                 cli_putstr_P(PSTR(" != "));
79                                 utoa(sl, str, 10);
80                                 cli_putstr(str);
81                         }else{
82                                 cli_putstr_P(PSTR(" != -1"));
83                         }
84                         fail=1;
85                 }else{
86                         cli_putstr_P(PSTR("\r\n length ok"));
87                 }
88                 base64dec(bin_buffer2, b64_buffer, 1);
89                 if(memcmp(bin_buffer, bin_buffer2, l)){
90                         cli_putstr_P(PSTR("\r\n ERROR value\r\n out: "));
91                         cli_hexdump(bin_buffer2, l);
92                         fail=1;
93                 }else{
94                         cli_putstr_P(PSTR("\r\n value ok"));
95                 }
96                 if(fail)
97                         break;
98         }       
99         cli_putstr_P(fail?PSTR("\r\n [FAIL]\r\n"):PSTR("\r\n [OK]\r\n"));
100 }
101
102 void testrun_performance_base64(void){
103 /*
104         uint64_t t;
105         char str[16];
106         uint8_t key[16], data[16];
107         noekeon_ctx_t ctx;
108         
109         calibrateTimer();
110         print_overhead();
111         
112         memset(key,  0, 16);
113         memset(data, 0, 16);
114         
115         startTimer(1);
116         noekeon_init(key, &ctx);
117         t = stopTimer();
118         cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
119         ultoa((unsigned long)t, str, 10);
120         cli_putstr(str);        
121         
122         startTimer(1);
123         noekeon_enc(data, &ctx);
124         t = stopTimer();
125         cli_putstr_P(PSTR("\r\n\tencrypt time: "));
126         ultoa((unsigned long)t, str, 10);
127         cli_putstr(str);        
128         
129         startTimer(1);
130         noekeon_dec(data, &ctx);
131         t = stopTimer();
132         cli_putstr_P(PSTR("\r\n\tdecrypt time: "));
133         ultoa((unsigned long)t, str, 10);
134         cli_putstr(str);
135         
136         cli_putstr_P(PSTR("\r\n"));
137 */
138 }
139 /*****************************************************************************
140  *  main                                                                                                                                         *
141  *****************************************************************************/
142
143 const char test_str[]        PROGMEM = "test";
144 const char performance_str[] PROGMEM = "performance";
145 const char echo_str[]        PROGMEM = "echo";
146
147 cmdlist_entry_t cmdlist[] PROGMEM = {
148         { test_str,        NULL, testrun_stdtest_base64},
149         { performance_str, NULL, testrun_performance_base64},
150         { echo_str,    (void*)1, (void_fpt)echo_ctrl},
151         { NULL,            NULL, NULL}
152 };
153
154 int main (void){
155         DEBUG_INIT();
156         
157         cli_rx = (cli_rx_fpt)uart0_getc;
158         cli_tx = (cli_tx_fpt)uart0_putc;                
159         for(;;){
160                 cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
161                 cli_putstr(algo_name);
162                 cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
163                 cmd_interface(cmdlist);
164         }
165 }