1 /* main-cast5-test.c */
3 This file is part of the Crypto-avr-lib/microcrypt-lib.
4 Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
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.
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.
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/>.
25 #include "serial-tools.h"
30 #include "nessie_bc_test.h"
31 #include "performance_test.h"
38 char* cipher_name = "cast-128 (cast5)";
40 /*****************************************************************************
41 * additional validation-functions *
42 *****************************************************************************/
44 void cast5_init(cast5_ctx_t* s, uint8_t* key, uint8_t keylength);
45 void cast5_enc(cast5_ctx_t *s, void* block);
46 void cast5_dec(cast5_ctx_t *s, void* block);
49 void cast5_init_dummy(uint8_t* key, uint8_t keylength_b, cast5_ctx_t* ctx){
50 cast5_init(ctx, key, keylength_b);
53 void cast5_enc_dummy(void* buffer, cast5_ctx_t* ctx){
54 cast5_enc(ctx, buffer);
57 void cast5_dec_dummy(void* buffer, cast5_ctx_t* ctx){
58 cast5_dec(ctx, buffer);
61 void test_nessie_cast5(void){
62 nessie_bc_ctx.blocksize_B = 8;
63 nessie_bc_ctx.keysize_b = 128;
64 nessie_bc_ctx.name = cipher_name;
65 nessie_bc_ctx.ctx_size_B = sizeof(cast5_ctx_t);
66 nessie_bc_ctx.cipher_enc = (nessie_bc_enc_fpt)cast5_enc_dummy;
67 nessie_bc_ctx.cipher_dec = (nessie_bc_dec_fpt)cast5_dec_dummy;
68 nessie_bc_ctx.cipher_genctx = (nessie_bc_gen_fpt)cast5_init_dummy;
73 /*****************************************************************************
75 *****************************************************************************/
77 void cast5_ctx_dump(cast5_ctx_t *s){
79 uart_putstr("\r\n==== cast5_ctx_dump ====\r\n shortkey: ");
80 uart_putstr(s->shortkey?"yes":"no");
83 uart_putstr("\r\n Km"); uart_hexdump(&i, 1); uart_putc(':');
84 uart_hexdump(&(s->mask[i]), 4);
85 uart_putstr("\r\n Kr"); uart_hexdump(&i, 1); uart_putc(':');
89 r += (s->roth[i>>3]&(1<<(i&0x7)))?0x10:0x00;
95 void test_encrypt(uint8_t *block, uint8_t *key, uint8_t keylength, bool print){
98 uart_putstr("\r\nCAST5:\r\n key:\t");
99 uart_hexdump(key, keylength/8);
100 uart_putstr("\r\n plaintext:\t");
101 uart_hexdump(block, 8);
103 cast5_init(&s, key, keylength);
104 // cast5_ctx_dump(&s);
105 cast5_enc(&s, block);
107 uart_putstr("\r\n ciphertext:\t");
108 uart_hexdump(block, 8);
112 void test_decrypt(uint8_t *block, uint8_t *key, uint8_t keylength, bool print){
115 uart_putstr("\r\nCAST5:\r\n key:\t");
116 uart_hexdump(key, keylength/8);
117 uart_putstr("\r\n ciphertext:\t");
118 uart_hexdump(block, 8);
120 cast5_init(&s, key, keylength);
121 // cast5_ctx_dump(&s);
122 cast5_dec(&s, block);
124 uart_putstr("\r\n plaintext:\t");
125 uart_hexdump(block, 8);
129 void testrun_cast5(void){
132 uint8_t *tda = (uint8_t*)"\x01\x23\x45\x67\x89\xAB\xCD\xEF",
133 *tka = (uint8_t*)"\x01\x23\x45\x67\x12\x34\x56\x78\x23\x45\x67\x89\x34\x56\x78\x9A";
134 memcpy(block, tda, 8);
135 memcpy(key, tka, 16);
136 test_encrypt(block, key, 128, true);
137 test_decrypt(block, key, 128, true);
138 memcpy(block, tda, 8);
139 memcpy(key, tka, 16);
140 test_encrypt(block, key, 80, true);
141 test_decrypt(block, key, 80, true);
142 memcpy(block, tda, 8);
143 memcpy(key, tka, 16);
144 test_encrypt(block, key, 40, true);
145 test_decrypt(block, key, 40, true);
147 /**** long test *****/
148 uart_putstr("\r\nmaintance-test");
149 uint8_t a[16]= {0x01, 0x23, 0x45, 0x67, 0x12,
150 0x34, 0x56, 0x78, 0x23, 0x45,
151 0x67, 0x89, 0x34, 0x56, 0x78,
153 b[16]= {0x01, 0x23, 0x45, 0x67, 0x12,
154 0x34, 0x56, 0x78, 0x23, 0x45,
155 0x67, 0x89, 0x34, 0x56, 0x78,
158 for(i=0;i<1000000; ++i){
159 test_encrypt(&(a[0]), &(b[0]), 128, false);
160 test_encrypt(&(a[8]), &(b[0]), 128, false);
161 test_encrypt(&(b[0]), &(a[0]), 128, false);
162 test_encrypt(&(b[8]), &(a[0]), 128, false);
163 if ((i&0x000000ff) == 0){
168 uart_putstr("\r\na = "); uart_hexdump(a, 16);
169 uart_putstr("\r\nb = "); uart_hexdump(b, 16);
173 void test_performance_cast5(void){
177 uint8_t key[16], data[16];
182 uart_putstr_P(PSTR("\r\n\r\n=== benchmark ==="));
184 uart_putstr_P(PSTR("\r\n\tconst overhead: "));
187 uart_putstr_P(PSTR("\r\n\tinterrupt overhead: "));
194 cast5_init(&ctx, key, 128);
196 uart_putstr_P(PSTR("\r\n\tctx-gen time: "));
197 ultoa((unsigned long)t, str, 10);
202 cast5_enc(&ctx, data);
204 uart_putstr_P(PSTR("\r\n\tencrypt time: "));
205 ultoa((unsigned long)t, str, 10);
210 cast5_dec(&ctx, data);
212 uart_putstr_P(PSTR("\r\n\tdecrypt time: "));
213 ultoa((unsigned long)t, str, 10);
216 uart_putstr_P(PSTR("\r\n"));
219 /*****************************************************************************
221 *****************************************************************************/
230 uart_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
231 uart_putstr(cipher_name);
232 uart_putstr_P(PSTR(")\r\nloaded and running\r\n"));
234 PGM_P u = PSTR("nessie\0test\0performance\0");
235 void_fpt v[] = {test_nessie_cast5, test_nessie_cast5, test_performance_cast5};
238 if (!getnextwordn(str,20)){DEBUG_S("DBG: W1\r\n"); goto error;}
239 if(execcommand_d0_P(str, u, v)<0){
240 uart_putstr_P(PSTR("\r\nunknown command\r\n"));
244 uart_putstr("ERROR\r\n");