7 #include "serial-tools.h"
15 #include <avr/pgmspace.h>
24 typedef enum{false=0,true=1} bool;
31 /*****************************************************************************
32 * additional validation-functions *
33 *****************************************************************************/
35 /*****************************************************************************
37 *****************************************************************************/
38 void camellia128_ctx_dump(camellia128_ctx_t *s);
40 void test_encrypt(uint8_t *block, uint8_t *key, uint16_t keylength, bool print){
43 uart_putstr("\r\nCamellia (enc):\r\n key:\t\t");
44 uart_hexdump(key, keylength/8);
45 uart_putstr("\r\n plaintext:\t");
46 uart_hexdump(block, 16);
49 camellia128_init(&s, key);;
50 camellia128_enc(&s, block);
52 uart_putstr("\r\n ciphertext:\t");
53 uart_hexdump(block, 16);
57 void test_decrypt(uint8_t *block, uint8_t *key, uint16_t keylength, bool print){
60 uart_putstr("\r\nCamellia (dec):\r\n key:\t\t");
61 uart_hexdump(key, keylength/8);
62 uart_putstr("\r\n ciphertext:\t");
63 uart_hexdump(block, 16);
65 camellia128_init(&s, key);
66 camellia128_dec(&s, block);
68 uart_putstr("\r\n plaintext:\t");
69 uart_hexdump(block, 16);
73 void nessie_test_iterate(uint8_t *block, uint8_t *key){
75 test_encrypt(block, key, 128, true);
76 test_decrypt(block, key, 128, true);
77 uart_putstr("\r\n100 times:");
79 test_encrypt(block, key, 128, false);
81 test_encrypt(block, key, 128, true);
82 uart_putstr("\r\n1000 times:");
83 for(i=0; i<(999-100); ++i){
84 test_encrypt(block, key, 128, false);
86 test_encrypt(block, key, 128, true);
89 void nessie_test_iterate_inv(uint8_t *block, uint8_t *key){
91 test_decrypt(block, key, 128, true);
92 test_encrypt(block, key, 128, true);
93 uart_putstr("\r\n100 times:");
95 test_decrypt(block, key, 128, false);
97 test_encrypt(block, key, 128, true);
98 uart_putstr("\r\n1000 times:");
99 for(i=0; i<(999-100); ++i){
100 test_decrypt(block, key, 128, false);
102 test_decrypt(block, key, 128, true);
105 prog_uint8_t ntt_test_values_in[16] = {
106 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
107 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
110 prog_uint8_t ntt_test_values_out[16] = {
111 0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73,
112 0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43
115 int memcmp_P(const void *s1, PGM_P s2, size_t n){
118 b = pgm_read_byte_near(s2);
119 if( *((uint8_t*)s1) != b)
120 return(*((uint8_t*)s1)-b);
126 void testrun_camellia(void){
127 /* we run the NESSIE test for Camellia here see
128 * https://www.cosic.esat.kuleuven.be/nessie/testvectors/bc/camellia/Camellia-128-128.verified.test-vectors
134 memcpy_P(block, ntt_test_values_in, 16);
135 memcpy_P(key, ntt_test_values_in, 16);
136 test_encrypt(block, key, 128, true);
137 if(memcmp_P(block, ntt_test_values_out, 16)){
138 uart_putstr("\t[FAILED]\r\n");
141 uart_putstr("\t[OK]");
142 test_decrypt(block, key, 128, true);
143 if(memcmp_P(block, ntt_test_values_in, 16)){
144 uart_putstr("\t[FAILED]\r\n");
147 uart_putstr("\t[OK]");
149 /* test set #1 & #2 */
151 for(setn=1; setn<=2; ++setn){
152 for(j=0; j<128; ++j){
153 uart_putstr("\r\n\r\n### SET: ");
154 uart_hexdump(&setn,1);
155 uart_putstr(" Vector: ");
158 memset(block, 0, 16);
160 ((setn&0x1)?key:block)[j>>3] = 1<<(((~j)&0x7));
161 nessie_test_iterate(block, key);
165 for(j=0; j<256; ++j){
166 uart_putstr("\r\n### SET: ");
167 uart_hexdump(&setn,1);
168 uart_putstr(" Vector: ");
171 memset(block, j, 16);
173 nessie_test_iterate(block, key);
176 /* test set #4 (some strange values*/
178 /* test ser #5 & #6 (same as 1&2 but enc and dec exchanged)*/
179 for(setn=5; setn<=6; ++setn){
180 for(j=0; j<128; ++j){
181 uart_putstr("\r\n\r\n### SET: ");
182 uart_hexdump(&setn,1);
183 uart_putstr(" Vector: ");
186 memset(block, 0, 16);
188 ((setn&0x1)?key:block)[j>>3] = 1<<(((~j)&0x7));
189 nessie_test_iterate_inv(block, key);
193 for(j=0; j<256; ++j){
194 uart_putstr("\r\n### SET: ");
195 uart_hexdump(&setn,1);
196 uart_putstr(" Vector: ");
199 memset(block, j, 16);
201 nessie_test_iterate_inv(block, key);
204 /* test set #4 (some strange values*/
210 /*****************************************************************************
212 *****************************************************************************/
221 uart_putstr("\r\n\r\nCrypto-VS (Camellia)\r\nloaded and running\r\n");
224 if (!getnextwordn(str,20)) {DEBUG_S("DBG: W1\r\n"); goto error;}
225 if (strcmp(str, "test")) {DEBUG_S("DBG: 1b\r\n"); goto error;}
230 uart_putstr("ERROR\r\n");