]> git.cryptolib.org Git - avr-crypto-lib.git/blob - main-cast5-test.c
+serpent bitslice; modifyed cast5 testings
[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
14 #include <stdint.h>
15 #include <string.h>
16
17 char* cipher_name = "cast-128 (cast5)";
18
19 /*****************************************************************************
20  *  additional validation-functions                                                                                      *
21  *****************************************************************************/
22 /*
23         void cast5_init(cast5_ctx_t* s, uint8_t* key, uint8_t keylength);
24         void cast5_enc(cast5_ctx_t *s, void* block);
25         void cast5_dec(cast5_ctx_t *s, void* block);
26 */ 
27
28 void cast5_init_dummy(uint8_t* key, uint8_t keylength_b, cast5_ctx_t* ctx){
29         cast5_init(ctx, key, keylength_b);
30 }
31
32 void cast5_enc_dummy(void* buffer, cast5_ctx_t* ctx){
33         cast5_enc(ctx, buffer);
34 }
35
36 void cast5_dec_dummy(void* buffer, cast5_ctx_t* ctx){
37         cast5_dec(ctx, buffer);
38 }
39
40 void test_nessie_cast5(void){
41         nessie_bc_ctx.blocksize_B =   8;
42         nessie_bc_ctx.keysize_b   = 128;
43         nessie_bc_ctx.name        = cipher_name;
44         nessie_bc_ctx.ctx_size_B  = sizeof(cast5_ctx_t);
45         nessie_bc_ctx.cipher_enc  = (nessie_bc_enc_fpt)cast5_enc_dummy;
46         nessie_bc_ctx.cipher_dec  = (nessie_bc_dec_fpt)cast5_dec_dummy;
47         nessie_bc_ctx.cipher_genctx  = (nessie_bc_gen_fpt)cast5_init_dummy;
48         
49         nessie_bc_run();
50 }
51
52 /*****************************************************************************
53  *  self tests                                                                                                                           *
54  *****************************************************************************/
55
56 void cast5_ctx_dump(cast5_ctx_t *s){
57         uint8_t i;
58         uart_putstr("\r\n==== cast5_ctx_dump ====\r\n shortkey: ");
59         uart_putstr(s->shortkey?"yes":"no");
60         for(i=0;i<16;++i){
61                 uint8_t r;
62                 uart_putstr("\r\n Km"); uart_hexdump(&i, 1); uart_putc(':');
63                 uart_hexdump(&(s->mask[i]), 4);
64                 uart_putstr("\r\n Kr"); uart_hexdump(&i, 1); uart_putc(':');
65                 r = (s->rotl[i/2]);
66                 if (i&0x01) r >>= 4;
67                 r &= 0xf;
68                 r += (s->roth[i>>3]&(1<<(i&0x7)))?0x10:0x00;
69                 uart_hexdump(&r, 1);
70         }
71 }
72
73
74 void test_encrypt(uint8_t *block, uint8_t *key, uint8_t keylength, bool print){
75         cast5_ctx_t s;
76         if (print){
77                 uart_putstr("\r\nCAST5:\r\n key:\t");
78                 uart_hexdump(key, keylength/8);
79                 uart_putstr("\r\n plaintext:\t");
80                 uart_hexdump(block, 8);
81         }
82         cast5_init(&s, key, keylength);
83 //      cast5_ctx_dump(&s);
84         cast5_enc(&s, block);
85         if (print){
86                 uart_putstr("\r\n ciphertext:\t");
87                 uart_hexdump(block, 8);
88         }
89
90
91 void test_decrypt(uint8_t *block, uint8_t *key, uint8_t keylength, bool print){
92         cast5_ctx_t s;
93         if (print){
94                 uart_putstr("\r\nCAST5:\r\n key:\t");
95                 uart_hexdump(key, keylength/8);
96                 uart_putstr("\r\n ciphertext:\t");
97                 uart_hexdump(block, 8);
98         }
99         cast5_init(&s, key, keylength);
100 //      cast5_ctx_dump(&s);
101         cast5_dec(&s, block);
102         if (print){
103                 uart_putstr("\r\n plaintext:\t");
104                 uart_hexdump(block, 8);
105         }
106
107
108 void testrun_cast5(void){
109         uint8_t block[8];
110         uint8_t key[16];
111         uint8_t *tda = (uint8_t*)"\x01\x23\x45\x67\x89\xAB\xCD\xEF",
112                 *tka = (uint8_t*)"\x01\x23\x45\x67\x12\x34\x56\x78\x23\x45\x67\x89\x34\x56\x78\x9A";
113         memcpy(block, tda, 8);
114         memcpy(key, tka, 16);
115         test_encrypt(block, key, 128, true);
116         test_decrypt(block, key, 128, true);
117         memcpy(block, tda, 8);
118         memcpy(key, tka, 16);
119         test_encrypt(block, key, 80, true);
120         test_decrypt(block, key, 80, true);
121         memcpy(block, tda, 8);
122         memcpy(key, tka, 16);
123         test_encrypt(block, key, 40, true);
124         test_decrypt(block, key, 40, true);
125         
126 /**** long test *****/
127         uart_putstr("\r\nmaintance-test");
128         uint8_t a[16]= {0x01, 0x23, 0x45, 0x67, 0x12,
129                                     0x34, 0x56, 0x78, 0x23, 0x45, 
130                                     0x67, 0x89, 0x34, 0x56, 0x78, 
131                                     0x9A}, 
132                         b[16]= {0x01, 0x23, 0x45, 0x67, 0x12,
133                                     0x34, 0x56, 0x78, 0x23, 0x45, 
134                                     0x67, 0x89, 0x34, 0x56, 0x78, 
135                                     0x9A};
136         uint32_t i;
137         for(i=0;i<1000000; ++i){
138                 test_encrypt(&(a[0]), &(b[0]), 128, false);
139                 test_encrypt(&(a[8]), &(b[0]), 128, false);
140                 test_encrypt(&(b[0]), &(a[0]), 128, false);
141                 test_encrypt(&(b[8]), &(a[0]), 128, false);
142                 if ((i&0x000000ff) == 0){
143                         uart_putstr("\r\n");
144                         uart_hexdump(&i, 4);
145                 }
146         }
147         uart_putstr("\r\na = "); uart_hexdump(a, 16);
148         uart_putstr("\r\nb = "); uart_hexdump(b, 16);
149
150 }
151
152
153
154 /*****************************************************************************
155  *  main                                                                                                                                         *
156  *****************************************************************************/
157
158 int main (void){
159         char str[20];
160
161         
162         DEBUG_INIT();
163         uart_putstr("\r\n");
164
165         uart_putstr("\r\n\r\nCrypto-VS\r\nloaded and running\r\n");
166 restart:
167         while(1){ 
168                 if (!getnextwordn(str,20))  {DEBUG_S("DBG: W1\r\n"); goto error;}
169                 if (strcmp(str, "nessie")) {DEBUG_S("DBG: 1b\r\n"); goto error;}
170                 //      testrun_cast5();
171                         test_nessie_cast5();
172                 goto restart;           
173                 continue;
174         error:
175                 uart_putstr("ERROR\r\n");
176         } /* while (1) */
177 }
178