]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-cast5-test.c
3db72a7445a24c73c5edd367608fb49b5b37cf1a
[avr-crypto-lib.git] / test_src / main-cast5-test.c
1 /* main-cast5-test.c */
2 /*
3     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
26 #include "uart_i.h"
27 #include "debug.h"
28
29 #include <cast5.h>
30 #include "nessie_bc_test.h"
31 #include "performance_test.h"
32 #include "bcal-performance.h"
33 #include "bcal_cast5.h"
34 #include "cli.h"
35
36 #include <stdint.h>
37 #include <string.h>
38 #include <stdlib.h>
39
40 char* algo_name = "cast-128 (cast5)";
41
42 const bcdesc_t* algolist[] PROGMEM = {
43         (bcdesc_t*)&cast5_desc,
44         NULL
45 };
46
47 /*****************************************************************************
48  *  additional validation-functions                                                                                      *
49  *****************************************************************************/
50
51 void testrun_nessie_cast5(void){
52         nessie_bc_ctx.blocksize_B =   8;
53         nessie_bc_ctx.keysize_b   = 128;
54         nessie_bc_ctx.name        = algo_name;
55         nessie_bc_ctx.ctx_size_B  = sizeof(cast5_ctx_t);
56         nessie_bc_ctx.cipher_enc  = (nessie_bc_enc_fpt)cast5_enc;
57         nessie_bc_ctx.cipher_dec  = (nessie_bc_dec_fpt)cast5_dec;
58         nessie_bc_ctx.cipher_genctx  = (nessie_bc_gen_fpt)cast5_init;
59         
60         nessie_bc_run();
61 }
62
63 /*****************************************************************************
64  *  self tests                                                                                                                           *
65  *****************************************************************************/
66
67 void cast5_ctx_dump(cast5_ctx_t *s){
68         uint8_t i;
69         cli_putstr("\r\n==== cast5_ctx_dump ====\r\n shortkey: ");
70         cli_putstr(s->shortkey?"yes":"no");
71         for(i=0;i<16;++i){
72                 uint8_t r;
73                 cli_putstr("\r\n Km"); cli_hexdump(&i, 1); cli_putc(':');
74                 cli_hexdump(&(s->mask[i]), 4);
75                 cli_putstr("\r\n Kr"); cli_hexdump(&i, 1); cli_putc(':');
76                 r = (s->rotl[i/2]);
77                 if (i&0x01) r >>= 4;
78                 r &= 0xf;
79                 r += (s->roth[i>>3]&(1<<(i&0x7)))?0x10:0x00;
80                 cli_hexdump(&r, 1);
81         }
82 }
83
84
85 void test_encrypt(uint8_t *block, uint8_t *key, uint8_t keylength, bool print){
86         cast5_ctx_t s;
87         if (print){
88                 cli_putstr("\r\nCAST5:\r\n key:\t");
89                 cli_hexdump(key, keylength/8);
90                 cli_putstr("\r\n plaintext:\t");
91                 cli_hexdump(block, 8);
92         }
93         cast5_init(key, keylength, &s);
94         cast5_enc(block, &s);
95         if (print){
96                 cli_putstr("\r\n ciphertext:\t");
97                 cli_hexdump(block, 8);
98         }
99
100
101 void test_decrypt(uint8_t *block, uint8_t *key, uint8_t keylength, bool print){
102         cast5_ctx_t s;
103         if (print){
104                 cli_putstr("\r\nCAST5:\r\n key:\t");
105                 cli_hexdump(key, keylength/8);
106                 cli_putstr("\r\n ciphertext:\t");
107                 cli_hexdump(block, 8);
108         }
109         cast5_init(key, keylength, &s);
110         cast5_dec(block, &s);
111         if (print){
112                 cli_putstr("\r\n plaintext:\t");
113                 cli_hexdump(block, 8);
114         }
115
116
117 void testrun_cast5(void){
118         uint8_t block[8];
119         uint8_t key[16];
120         uint8_t *tda = (uint8_t*)"\x01\x23\x45\x67\x89\xAB\xCD\xEF",
121                 *tka = (uint8_t*)"\x01\x23\x45\x67\x12\x34\x56\x78\x23\x45\x67\x89\x34\x56\x78\x9A";
122         memcpy(block, tda, 8);
123         memcpy(key, tka, 16);
124         test_encrypt(block, key, 128, true);
125         test_decrypt(block, key, 128, true);
126         memcpy(block, tda, 8);
127         memcpy(key, tka, 16);
128         test_encrypt(block, key, 80, true);
129         test_decrypt(block, key, 80, true);
130         memcpy(block, tda, 8);
131         memcpy(key, tka, 16);
132         test_encrypt(block, key, 40, true);
133         test_decrypt(block, key, 40, true);
134         
135 /**** long test *****/
136         cli_putstr("\r\nmaintance-test");
137         uint8_t a[16]= {0x01, 0x23, 0x45, 0x67, 0x12,
138                                     0x34, 0x56, 0x78, 0x23, 0x45, 
139                                     0x67, 0x89, 0x34, 0x56, 0x78, 
140                                     0x9A}, 
141                         b[16]= {0x01, 0x23, 0x45, 0x67, 0x12,
142                                     0x34, 0x56, 0x78, 0x23, 0x45, 
143                                     0x67, 0x89, 0x34, 0x56, 0x78, 
144                                     0x9A};
145         uint32_t i;
146         for(i=0;i<1000000; ++i){
147                 test_encrypt(&(a[0]), &(b[0]), 128, false);
148                 test_encrypt(&(a[8]), &(b[0]), 128, false);
149                 test_encrypt(&(b[0]), &(a[0]), 128, false);
150                 test_encrypt(&(b[8]), &(a[0]), 128, false);
151                 if ((i&0x000000ff) == 0){
152                         
153                         cli_hexdump(&i, 4);
154                 }
155         }
156         cli_putstr("\r\na = "); cli_hexdump(a, 16);
157         cli_putstr("\r\nb = "); cli_hexdump(b, 16);
158
159 }
160
161 void testrun_performance_cast5(void){
162         bcal_performance_multiple(algolist);
163 }
164
165 /*****************************************************************************
166  *  main                                                                                                                                         *
167  *****************************************************************************/
168
169 const char nessie_str[]      PROGMEM = "nessie";
170 const char test_str[]        PROGMEM = "test";
171 const char performance_str[] PROGMEM = "performance";
172 const char echo_str[]        PROGMEM = "echo";
173
174 cmdlist_entry_t cmdlist[] PROGMEM = {
175         { nessie_str,      NULL, testrun_nessie_cast5},
176         { test_str,        NULL, testrun_cast5},
177         { performance_str, NULL, testrun_performance_cast5},
178         { echo_str,    (void*)1, (void_fpt)echo_ctrl},
179         { NULL,            NULL, NULL}
180 };
181
182 int main (void){
183         DEBUG_INIT();
184         
185         cli_rx = (cli_rx_fpt)uart0_getc;
186         cli_tx = (cli_tx_fpt)uart0_putc;                
187         for(;;){
188                 cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
189                 cli_putstr(algo_name);
190                 cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
191                 cmd_interface(cmdlist);
192         }
193 }