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