]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-cast5-test.c
3cdb70355ac5665f59679c4f05cf7e8f2a9ae178
[avr-crypto-lib.git] / test_src / main-cast5-test.c
1 /* main-cast5-test.c */
2 /*
3     This file is part of the This file is part of the AVR-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  * 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 testrun_nessie_cast5(void){
45         nessie_bc_ctx.blocksize_B =   8;
46         nessie_bc_ctx.keysize_b   = 128;
47         nessie_bc_ctx.name        = cipher_name;
48         nessie_bc_ctx.ctx_size_B  = sizeof(cast5_ctx_t);
49         nessie_bc_ctx.cipher_enc  = (nessie_bc_enc_fpt)cast5_enc;
50         nessie_bc_ctx.cipher_dec  = (nessie_bc_dec_fpt)cast5_dec;
51         nessie_bc_ctx.cipher_genctx  = (nessie_bc_gen_fpt)cast5_init;
52         
53         nessie_bc_run();
54 }
55
56 /*****************************************************************************
57  *  self tests                                                                                                                           *
58  *****************************************************************************/
59
60 void cast5_ctx_dump(cast5_ctx_t *s){
61         uint8_t i;
62         uart_putstr("\r\n==== cast5_ctx_dump ====\r\n shortkey: ");
63         uart_putstr(s->shortkey?"yes":"no");
64         for(i=0;i<16;++i){
65                 uint8_t r;
66                 uart_putstr("\r\n Km"); uart_hexdump(&i, 1); uart_putc(':');
67                 uart_hexdump(&(s->mask[i]), 4);
68                 uart_putstr("\r\n Kr"); uart_hexdump(&i, 1); uart_putc(':');
69                 r = (s->rotl[i/2]);
70                 if (i&0x01) r >>= 4;
71                 r &= 0xf;
72                 r += (s->roth[i>>3]&(1<<(i&0x7)))?0x10:0x00;
73                 uart_hexdump(&r, 1);
74         }
75 }
76
77
78 void test_encrypt(uint8_t *block, uint8_t *key, uint8_t keylength, bool print){
79         cast5_ctx_t s;
80         if (print){
81                 uart_putstr("\r\nCAST5:\r\n key:\t");
82                 uart_hexdump(key, keylength/8);
83                 uart_putstr("\r\n plaintext:\t");
84                 uart_hexdump(block, 8);
85         }
86         cast5_init(key, keylength, &s);
87         cast5_enc(block, &s);
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(key, keylength, &s);
103         cast5_dec(block, &s);
104         if (print){
105                 uart_putstr("\r\n plaintext:\t");
106                 uart_hexdump(block, 8);
107         }
108
109
110 void testrun_cast5(void){
111         uint8_t block[8];
112         uint8_t key[16];
113         uint8_t *tda = (uint8_t*)"\x01\x23\x45\x67\x89\xAB\xCD\xEF",
114                 *tka = (uint8_t*)"\x01\x23\x45\x67\x12\x34\x56\x78\x23\x45\x67\x89\x34\x56\x78\x9A";
115         memcpy(block, tda, 8);
116         memcpy(key, tka, 16);
117         test_encrypt(block, key, 128, true);
118         test_decrypt(block, key, 128, true);
119         memcpy(block, tda, 8);
120         memcpy(key, tka, 16);
121         test_encrypt(block, key, 80, true);
122         test_decrypt(block, key, 80, true);
123         memcpy(block, tda, 8);
124         memcpy(key, tka, 16);
125         test_encrypt(block, key, 40, true);
126         test_decrypt(block, key, 40, true);
127         
128 /**** long test *****/
129         uart_putstr("\r\nmaintance-test");
130         uint8_t a[16]= {0x01, 0x23, 0x45, 0x67, 0x12,
131                                     0x34, 0x56, 0x78, 0x23, 0x45, 
132                                     0x67, 0x89, 0x34, 0x56, 0x78, 
133                                     0x9A}, 
134                         b[16]= {0x01, 0x23, 0x45, 0x67, 0x12,
135                                     0x34, 0x56, 0x78, 0x23, 0x45, 
136                                     0x67, 0x89, 0x34, 0x56, 0x78, 
137                                     0x9A};
138         uint32_t i;
139         for(i=0;i<1000000; ++i){
140                 test_encrypt(&(a[0]), &(b[0]), 128, false);
141                 test_encrypt(&(a[8]), &(b[0]), 128, false);
142                 test_encrypt(&(b[0]), &(a[0]), 128, false);
143                 test_encrypt(&(b[8]), &(a[0]), 128, false);
144                 if ((i&0x000000ff) == 0){
145                         uart_putstr("\r\n");
146                         uart_hexdump(&i, 4);
147                 }
148         }
149         uart_putstr("\r\na = "); uart_hexdump(a, 16);
150         uart_putstr("\r\nb = "); uart_hexdump(b, 16);
151
152 }
153
154 void testrun_performance_cast5(void){
155         uint64_t t;
156         char str[6];
157         uint8_t key[16], data[16];
158         cast5_ctx_t ctx;
159         
160         calibrateTimer();
161         print_overhead();
162         
163         memset(key,  0, 16);
164         memset(data, 0, 16);
165         
166         startTimer(1);
167         cast5_init(key, 128, &ctx);
168         t = stopTimer();
169         uart_putstr_P(PSTR("\r\n\tctx-gen time: "));
170         ultoa((unsigned long)t, str, 10);
171         uart_putstr(str);
172         
173         
174         startTimer(1);
175         cast5_enc(data, &ctx);
176         t = stopTimer();
177         uart_putstr_P(PSTR("\r\n\tencrypt time: "));
178         ultoa((unsigned long)t, str, 10);
179         uart_putstr(str);
180         
181         
182         startTimer(1);
183         cast5_dec(data, &ctx);
184         t = stopTimer();
185         uart_putstr_P(PSTR("\r\n\tdecrypt time: "));
186         ultoa((unsigned long)t, str, 10);
187         uart_putstr(str);
188         
189         uart_putstr_P(PSTR("\r\n"));
190 }
191
192 /*****************************************************************************
193  *  main                                                                                                                                         *
194  *****************************************************************************/
195
196 int main (void){
197         char str[20];
198
199         
200         DEBUG_INIT();
201         uart_putstr("\r\n");
202
203         uart_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
204         uart_putstr(cipher_name);
205         uart_putstr_P(PSTR(")\r\nloaded and running\r\n"));
206
207         PGM_P    u   = PSTR("nessie\0test\0performance\0");
208         void_fpt v[] = {testrun_nessie_cast5, testrun_cast5, testrun_performance_cast5};
209         
210         while(1){ 
211                 if (!getnextwordn(str,20)){DEBUG_S("DBG: W1\r\n"); goto error;}
212                 if(execcommand_d0_P(str, u, v)<0){
213                         uart_putstr_P(PSTR("\r\nunknown command\r\n"));
214                 }
215                 continue;
216         error:
217                 uart_putstr("ERROR\r\n");
218         }
219 }
220