]> git.cryptolib.org Git - avr-crypto-lib.git/blob - main-cast5-test.c
2a3841b8e7e086e23fcc28294959dcba4560e030
[avr-crypto-lib.git] / main-cast5-test.c
1 /*
2  * cast5 test-suit
3  * 
4 */
5
6 #include "config.h"
7 #include "serial-tools.h"
8 #include "uart.h"
9 #include "debug.h"
10
11 #include "cast5.h"
12 #include "nessie_bc_test.h"
13 #include "performance_test.h"
14 #include "cli.h"
15
16 #include <stdint.h>
17 #include <string.h>
18 #include <stdlib.h>
19
20 char* cipher_name = "cast-128 (cast5)";
21
22 /*****************************************************************************
23  *  additional validation-functions                                                                                      *
24  *****************************************************************************/
25 /*
26         void cast5_init(cast5_ctx_t* s, uint8_t* key, uint8_t keylength);
27         void cast5_enc(cast5_ctx_t *s, void* block);
28         void cast5_dec(cast5_ctx_t *s, void* block);
29 */ 
30
31 void cast5_init_dummy(uint8_t* key, uint8_t keylength_b, cast5_ctx_t* ctx){
32         cast5_init(ctx, key, keylength_b);
33 }
34
35 void cast5_enc_dummy(void* buffer, cast5_ctx_t* ctx){
36         cast5_enc(ctx, buffer);
37 }
38
39 void cast5_dec_dummy(void* buffer, cast5_ctx_t* ctx){
40         cast5_dec(ctx, buffer);
41 }
42
43 void test_nessie_cast5(void){
44         nessie_bc_ctx.blocksize_B =   8;
45         nessie_bc_ctx.keysize_b   = 128;
46         nessie_bc_ctx.name        = cipher_name;
47         nessie_bc_ctx.ctx_size_B  = sizeof(cast5_ctx_t);
48         nessie_bc_ctx.cipher_enc  = (nessie_bc_enc_fpt)cast5_enc_dummy;
49         nessie_bc_ctx.cipher_dec  = (nessie_bc_dec_fpt)cast5_dec_dummy;
50         nessie_bc_ctx.cipher_genctx  = (nessie_bc_gen_fpt)cast5_init_dummy;
51         
52         nessie_bc_run();
53 }
54
55 /*****************************************************************************
56  *  self tests                                                                                                                           *
57  *****************************************************************************/
58
59 void cast5_ctx_dump(cast5_ctx_t *s){
60         uint8_t i;
61         uart_putstr("\r\n==== cast5_ctx_dump ====\r\n shortkey: ");
62         uart_putstr(s->shortkey?"yes":"no");
63         for(i=0;i<16;++i){
64                 uint8_t r;
65                 uart_putstr("\r\n Km"); uart_hexdump(&i, 1); uart_putc(':');
66                 uart_hexdump(&(s->mask[i]), 4);
67                 uart_putstr("\r\n Kr"); uart_hexdump(&i, 1); uart_putc(':');
68                 r = (s->rotl[i/2]);
69                 if (i&0x01) r >>= 4;
70                 r &= 0xf;
71                 r += (s->roth[i>>3]&(1<<(i&0x7)))?0x10:0x00;
72                 uart_hexdump(&r, 1);
73         }
74 }
75
76
77 void test_encrypt(uint8_t *block, uint8_t *key, uint8_t keylength, bool print){
78         cast5_ctx_t s;
79         if (print){
80                 uart_putstr("\r\nCAST5:\r\n key:\t");
81                 uart_hexdump(key, keylength/8);
82                 uart_putstr("\r\n plaintext:\t");
83                 uart_hexdump(block, 8);
84         }
85         cast5_init(&s, key, keylength);
86 //      cast5_ctx_dump(&s);
87         cast5_enc(&s, block);
88         if (print){
89                 uart_putstr("\r\n ciphertext:\t");
90                 uart_hexdump(block, 8);
91         }
92
93
94 void test_decrypt(uint8_t *block, uint8_t *key, uint8_t keylength, bool print){
95         cast5_ctx_t s;
96         if (print){
97                 uart_putstr("\r\nCAST5:\r\n key:\t");
98                 uart_hexdump(key, keylength/8);
99                 uart_putstr("\r\n ciphertext:\t");
100                 uart_hexdump(block, 8);
101         }
102         cast5_init(&s, key, keylength);
103 //      cast5_ctx_dump(&s);
104         cast5_dec(&s, block);
105         if (print){
106                 uart_putstr("\r\n plaintext:\t");
107                 uart_hexdump(block, 8);
108         }
109
110
111 void testrun_cast5(void){
112         uint8_t block[8];
113         uint8_t key[16];
114         uint8_t *tda = (uint8_t*)"\x01\x23\x45\x67\x89\xAB\xCD\xEF",
115                 *tka = (uint8_t*)"\x01\x23\x45\x67\x12\x34\x56\x78\x23\x45\x67\x89\x34\x56\x78\x9A";
116         memcpy(block, tda, 8);
117         memcpy(key, tka, 16);
118         test_encrypt(block, key, 128, true);
119         test_decrypt(block, key, 128, true);
120         memcpy(block, tda, 8);
121         memcpy(key, tka, 16);
122         test_encrypt(block, key, 80, true);
123         test_decrypt(block, key, 80, true);
124         memcpy(block, tda, 8);
125         memcpy(key, tka, 16);
126         test_encrypt(block, key, 40, true);
127         test_decrypt(block, key, 40, true);
128         
129 /**** long test *****/
130         uart_putstr("\r\nmaintance-test");
131         uint8_t a[16]= {0x01, 0x23, 0x45, 0x67, 0x12,
132                                     0x34, 0x56, 0x78, 0x23, 0x45, 
133                                     0x67, 0x89, 0x34, 0x56, 0x78, 
134                                     0x9A}, 
135                         b[16]= {0x01, 0x23, 0x45, 0x67, 0x12,
136                                     0x34, 0x56, 0x78, 0x23, 0x45, 
137                                     0x67, 0x89, 0x34, 0x56, 0x78, 
138                                     0x9A};
139         uint32_t i;
140         for(i=0;i<1000000; ++i){
141                 test_encrypt(&(a[0]), &(b[0]), 128, false);
142                 test_encrypt(&(a[8]), &(b[0]), 128, false);
143                 test_encrypt(&(b[0]), &(a[0]), 128, false);
144                 test_encrypt(&(b[8]), &(a[0]), 128, false);
145                 if ((i&0x000000ff) == 0){
146                         uart_putstr("\r\n");
147                         uart_hexdump(&i, 4);
148                 }
149         }
150         uart_putstr("\r\na = "); uart_hexdump(a, 16);
151         uart_putstr("\r\nb = "); uart_hexdump(b, 16);
152
153 }
154
155 void test_performance_cast5(void){
156         uint16_t i,c;
157         uint64_t t;
158         char str[6];
159         uint8_t key[16], data[16];
160         cast5_ctx_t ctx;
161         
162         calibrateTimer();
163         getOverhead(&c, &i);
164         uart_putstr_P(PSTR("\r\n\r\n=== benchmark ==="));
165         utoa(c, str, 10);
166         uart_putstr_P(PSTR("\r\n\tconst overhead:     "));
167         uart_putstr(str);
168         utoa(i, str, 10);
169         uart_putstr_P(PSTR("\r\n\tinterrupt overhead: "));
170         uart_putstr(str);
171         
172         memset(key,  0, 16);
173         memset(data, 0, 16);
174         
175         startTimer(1);
176         cast5_init(&ctx, key, 128);
177         t = stopTimer();
178         uart_putstr_P(PSTR("\r\n\tctx-gen time: "));
179         ultoa((unsigned long)t, str, 10);
180         uart_putstr(str);
181         
182         
183         startTimer(1);
184         cast5_enc(&ctx, data);
185         t = stopTimer();
186         uart_putstr_P(PSTR("\r\n\tencrypt time: "));
187         ultoa((unsigned long)t, str, 10);
188         uart_putstr(str);
189         
190         
191         startTimer(1);
192         cast5_dec(&ctx, data);
193         t = stopTimer();
194         uart_putstr_P(PSTR("\r\n\tdecrypt time: "));
195         ultoa((unsigned long)t, str, 10);
196         uart_putstr(str);
197         
198         uart_putstr_P(PSTR("\r\n"));
199 }
200
201 /*****************************************************************************
202  *  main                                                                                                                                         *
203  *****************************************************************************/
204
205 int main (void){
206         char str[20];
207
208         
209         DEBUG_INIT();
210         uart_putstr("\r\n");
211
212         uart_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
213         uart_putstr(cipher_name);
214         uart_putstr_P(PSTR(")\r\nloaded and running\r\n"));
215
216         PGM_P    u   = PSTR("nessie\0test\0performance\0");
217         void_fpt v[] = {test_nessie_cast5, test_nessie_cast5, test_performance_cast5};
218         
219         while(1){ 
220                 if (!getnextwordn(str,20)){DEBUG_S("DBG: W1\r\n"); goto error;}
221                 if(execcommand_d0_P(str, u, v)<0){
222                         uart_putstr_P(PSTR("\r\nunknown command\r\n"));
223                 }
224                 continue;
225         error:
226                 uart_putstr("ERROR\r\n");
227         }
228 }
229