7 #include "serial-tools.h"
12 #include "nessie_bc_test.h"
17 char* cipher_name = "cast-128 (cast5)";
19 /*****************************************************************************
20 * additional validation-functions *
21 *****************************************************************************/
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);
28 void cast5_init_dummy(uint8_t* key, uint8_t keylength_b, cast5_ctx_t* ctx){
29 cast5_init(ctx, key, keylength_b);
32 void cast5_enc_dummy(void* buffer, cast5_ctx_t* ctx){
33 cast5_enc(ctx, buffer);
36 void cast5_dec_dummy(void* buffer, cast5_ctx_t* ctx){
37 cast5_dec(ctx, buffer);
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;
52 /*****************************************************************************
54 *****************************************************************************/
56 void cast5_ctx_dump(cast5_ctx_t *s){
58 uart_putstr("\r\n==== cast5_ctx_dump ====\r\n shortkey: ");
59 uart_putstr(s->shortkey?"yes":"no");
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(':');
68 r += (s->roth[i>>3]&(1<<(i&0x7)))?0x10:0x00;
74 void test_encrypt(uint8_t *block, uint8_t *key, uint8_t keylength, bool 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);
82 cast5_init(&s, key, keylength);
83 // cast5_ctx_dump(&s);
86 uart_putstr("\r\n ciphertext:\t");
87 uart_hexdump(block, 8);
91 void test_decrypt(uint8_t *block, uint8_t *key, uint8_t keylength, bool 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);
99 cast5_init(&s, key, keylength);
100 // cast5_ctx_dump(&s);
101 cast5_dec(&s, block);
103 uart_putstr("\r\n plaintext:\t");
104 uart_hexdump(block, 8);
108 void testrun_cast5(void){
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);
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,
132 b[16]= {0x01, 0x23, 0x45, 0x67, 0x12,
133 0x34, 0x56, 0x78, 0x23, 0x45,
134 0x67, 0x89, 0x34, 0x56, 0x78,
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){
147 uart_putstr("\r\na = "); uart_hexdump(a, 16);
148 uart_putstr("\r\nb = "); uart_hexdump(b, 16);
154 /*****************************************************************************
156 *****************************************************************************/
165 uart_putstr("\r\n\r\nCrypto-VS\r\nloaded and running\r\n");
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;}
175 uart_putstr("ERROR\r\n");