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