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 testrun_nessie_cast5(void){
45 nessie_bc_ctx.blocksize_B = 8;
46 nessie_bc_ctx.keysize_b = 128;
47 nessie_bc_ctx.name = cipher_name;
48 nessie_bc_ctx.ctx_size_B = sizeof(cast5_ctx_t);
49 nessie_bc_ctx.cipher_enc = (nessie_bc_enc_fpt)cast5_enc;
50 nessie_bc_ctx.cipher_dec = (nessie_bc_dec_fpt)cast5_dec;
51 nessie_bc_ctx.cipher_genctx = (nessie_bc_gen_fpt)cast5_init;
56 /*****************************************************************************
58 *****************************************************************************/
60 void cast5_ctx_dump(cast5_ctx_t *s){
62 uart_putstr("\r\n==== cast5_ctx_dump ====\r\n shortkey: ");
63 uart_putstr(s->shortkey?"yes":"no");
66 uart_putstr("\r\n Km"); uart_hexdump(&i, 1); uart_putc(':');
67 uart_hexdump(&(s->mask[i]), 4);
68 uart_putstr("\r\n Kr"); uart_hexdump(&i, 1); uart_putc(':');
72 r += (s->roth[i>>3]&(1<<(i&0x7)))?0x10:0x00;
78 void test_encrypt(uint8_t *block, uint8_t *key, uint8_t keylength, bool print){
81 uart_putstr("\r\nCAST5:\r\n key:\t");
82 uart_hexdump(key, keylength/8);
83 uart_putstr("\r\n plaintext:\t");
84 uart_hexdump(block, 8);
86 cast5_init(key, keylength, &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(key, keylength, &s);
103 cast5_dec(block, &s);
105 uart_putstr("\r\n plaintext:\t");
106 uart_hexdump(block, 8);
110 void testrun_cast5(void){
113 uint8_t *tda = (uint8_t*)"\x01\x23\x45\x67\x89\xAB\xCD\xEF",
114 *tka = (uint8_t*)"\x01\x23\x45\x67\x12\x34\x56\x78\x23\x45\x67\x89\x34\x56\x78\x9A";
115 memcpy(block, tda, 8);
116 memcpy(key, tka, 16);
117 test_encrypt(block, key, 128, true);
118 test_decrypt(block, key, 128, true);
119 memcpy(block, tda, 8);
120 memcpy(key, tka, 16);
121 test_encrypt(block, key, 80, true);
122 test_decrypt(block, key, 80, true);
123 memcpy(block, tda, 8);
124 memcpy(key, tka, 16);
125 test_encrypt(block, key, 40, true);
126 test_decrypt(block, key, 40, true);
128 /**** long test *****/
129 uart_putstr("\r\nmaintance-test");
130 uint8_t a[16]= {0x01, 0x23, 0x45, 0x67, 0x12,
131 0x34, 0x56, 0x78, 0x23, 0x45,
132 0x67, 0x89, 0x34, 0x56, 0x78,
134 b[16]= {0x01, 0x23, 0x45, 0x67, 0x12,
135 0x34, 0x56, 0x78, 0x23, 0x45,
136 0x67, 0x89, 0x34, 0x56, 0x78,
139 for(i=0;i<1000000; ++i){
140 test_encrypt(&(a[0]), &(b[0]), 128, false);
141 test_encrypt(&(a[8]), &(b[0]), 128, false);
142 test_encrypt(&(b[0]), &(a[0]), 128, false);
143 test_encrypt(&(b[8]), &(a[0]), 128, false);
144 if ((i&0x000000ff) == 0){
149 uart_putstr("\r\na = "); uart_hexdump(a, 16);
150 uart_putstr("\r\nb = "); uart_hexdump(b, 16);
154 void testrun_performance_cast5(void){
157 uint8_t key[16], data[16];
167 cast5_init(key, 128, &ctx);
169 uart_putstr_P(PSTR("\r\n\tctx-gen time: "));
170 ultoa((unsigned long)t, str, 10);
175 cast5_enc(data, &ctx);
177 uart_putstr_P(PSTR("\r\n\tencrypt time: "));
178 ultoa((unsigned long)t, str, 10);
183 cast5_dec(data, &ctx);
185 uart_putstr_P(PSTR("\r\n\tdecrypt time: "));
186 ultoa((unsigned long)t, str, 10);
189 uart_putstr_P(PSTR("\r\n"));
192 /*****************************************************************************
194 *****************************************************************************/
203 uart_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
204 uart_putstr(cipher_name);
205 uart_putstr_P(PSTR(")\r\nloaded and running\r\n"));
207 PGM_P u = PSTR("nessie\0test\0performance\0");
208 void_fpt v[] = {testrun_nessie_cast5, testrun_cast5, testrun_performance_cast5};
211 if (!getnextwordn(str,20)){DEBUG_S("DBG: W1\r\n"); goto error;}
212 if(execcommand_d0_P(str, u, v)<0){
213 uart_putstr_P(PSTR("\r\nunknown command\r\n"));
217 uart_putstr("ERROR\r\n");