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