7 #include "serial-tools.h"
12 #include "nessie_bc_test.h"
13 #include "performance_test.h"
20 char* cipher_name = "cast-128 (cast5)";
22 /*****************************************************************************
23 * additional validation-functions *
24 *****************************************************************************/
26 void cast5_init(cast5_ctx_t* s, uint8_t* key, uint8_t keylength);
27 void cast5_enc(cast5_ctx_t *s, void* block);
28 void cast5_dec(cast5_ctx_t *s, void* block);
31 void cast5_init_dummy(uint8_t* key, uint8_t keylength_b, cast5_ctx_t* ctx){
32 cast5_init(ctx, key, keylength_b);
35 void cast5_enc_dummy(void* buffer, cast5_ctx_t* ctx){
36 cast5_enc(ctx, buffer);
39 void cast5_dec_dummy(void* buffer, cast5_ctx_t* ctx){
40 cast5_dec(ctx, buffer);
43 void test_nessie_cast5(void){
44 nessie_bc_ctx.blocksize_B = 8;
45 nessie_bc_ctx.keysize_b = 128;
46 nessie_bc_ctx.name = cipher_name;
47 nessie_bc_ctx.ctx_size_B = sizeof(cast5_ctx_t);
48 nessie_bc_ctx.cipher_enc = (nessie_bc_enc_fpt)cast5_enc_dummy;
49 nessie_bc_ctx.cipher_dec = (nessie_bc_dec_fpt)cast5_dec_dummy;
50 nessie_bc_ctx.cipher_genctx = (nessie_bc_gen_fpt)cast5_init_dummy;
55 /*****************************************************************************
57 *****************************************************************************/
59 void cast5_ctx_dump(cast5_ctx_t *s){
61 uart_putstr("\r\n==== cast5_ctx_dump ====\r\n shortkey: ");
62 uart_putstr(s->shortkey?"yes":"no");
65 uart_putstr("\r\n Km"); uart_hexdump(&i, 1); uart_putc(':');
66 uart_hexdump(&(s->mask[i]), 4);
67 uart_putstr("\r\n Kr"); uart_hexdump(&i, 1); uart_putc(':');
71 r += (s->roth[i>>3]&(1<<(i&0x7)))?0x10:0x00;
77 void test_encrypt(uint8_t *block, uint8_t *key, uint8_t keylength, bool print){
80 uart_putstr("\r\nCAST5:\r\n key:\t");
81 uart_hexdump(key, keylength/8);
82 uart_putstr("\r\n plaintext:\t");
83 uart_hexdump(block, 8);
85 cast5_init(&s, key, keylength);
86 // cast5_ctx_dump(&s);
89 uart_putstr("\r\n ciphertext:\t");
90 uart_hexdump(block, 8);
94 void test_decrypt(uint8_t *block, uint8_t *key, uint8_t keylength, bool print){
97 uart_putstr("\r\nCAST5:\r\n key:\t");
98 uart_hexdump(key, keylength/8);
99 uart_putstr("\r\n ciphertext:\t");
100 uart_hexdump(block, 8);
102 cast5_init(&s, key, keylength);
103 // cast5_ctx_dump(&s);
104 cast5_dec(&s, block);
106 uart_putstr("\r\n plaintext:\t");
107 uart_hexdump(block, 8);
111 void testrun_cast5(void){
114 uint8_t *tda = (uint8_t*)"\x01\x23\x45\x67\x89\xAB\xCD\xEF",
115 *tka = (uint8_t*)"\x01\x23\x45\x67\x12\x34\x56\x78\x23\x45\x67\x89\x34\x56\x78\x9A";
116 memcpy(block, tda, 8);
117 memcpy(key, tka, 16);
118 test_encrypt(block, key, 128, true);
119 test_decrypt(block, key, 128, true);
120 memcpy(block, tda, 8);
121 memcpy(key, tka, 16);
122 test_encrypt(block, key, 80, true);
123 test_decrypt(block, key, 80, true);
124 memcpy(block, tda, 8);
125 memcpy(key, tka, 16);
126 test_encrypt(block, key, 40, true);
127 test_decrypt(block, key, 40, true);
129 /**** long test *****/
130 uart_putstr("\r\nmaintance-test");
131 uint8_t a[16]= {0x01, 0x23, 0x45, 0x67, 0x12,
132 0x34, 0x56, 0x78, 0x23, 0x45,
133 0x67, 0x89, 0x34, 0x56, 0x78,
135 b[16]= {0x01, 0x23, 0x45, 0x67, 0x12,
136 0x34, 0x56, 0x78, 0x23, 0x45,
137 0x67, 0x89, 0x34, 0x56, 0x78,
140 for(i=0;i<1000000; ++i){
141 test_encrypt(&(a[0]), &(b[0]), 128, false);
142 test_encrypt(&(a[8]), &(b[0]), 128, false);
143 test_encrypt(&(b[0]), &(a[0]), 128, false);
144 test_encrypt(&(b[8]), &(a[0]), 128, false);
145 if ((i&0x000000ff) == 0){
150 uart_putstr("\r\na = "); uart_hexdump(a, 16);
151 uart_putstr("\r\nb = "); uart_hexdump(b, 16);
155 void test_performance_cast5(void){
159 uint8_t key[16], data[16];
164 uart_putstr_P(PSTR("\r\n\r\n=== benchmark ==="));
166 uart_putstr_P(PSTR("\r\n\tconst overhead: "));
169 uart_putstr_P(PSTR("\r\n\tinterrupt overhead: "));
176 cast5_init(&ctx, key, 128);
178 uart_putstr_P(PSTR("\r\n\tctx-gen time: "));
183 cast5_enc(&ctx, data);
185 uart_putstr_P(PSTR("\r\n\tencrypt time: "));
190 cast5_dec(&ctx, data);
192 uart_putstr_P(PSTR("\r\n\tdecrypt time: "));
194 uart_putstr_P(PSTR("\r\n"));
197 /*****************************************************************************
199 *****************************************************************************/
201 typedef void(*void_fpt)(void);
210 uart_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
211 uart_putstr(cipher_name);
212 uart_putstr_P(PSTR(")\r\nloaded and running\r\n"));
214 PGM_P u = PSTR("nessie\0test\0performance\0");
215 void_fpt v[] = {test_nessie_cast5, test_nessie_cast5, test_performance_cast5};
218 if (!getnextwordn(str,20)){DEBUG_S("DBG: W1\r\n"); goto error;}
219 if(execcommand_d0_P(str, u, v)<0){
220 uart_putstr_P(PSTR("\r\nunknown command\r\n"));
224 uart_putstr("ERROR\r\n");