1 /* main-camellia-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"
33 #include <avr/pgmspace.h>
42 typedef enum{false=0,true=1} bool;
49 /*****************************************************************************
50 * additional validation-functions *
51 *****************************************************************************/
53 /*****************************************************************************
55 *****************************************************************************/
56 void camellia128_ctx_dump(camellia128_ctx_t *s);
58 void test_encrypt(uint8_t *block, uint8_t *key, uint16_t keylength, bool print){
61 uart_putstr("\r\nCamellia (enc):\r\n key:\t\t");
62 uart_hexdump(key, keylength/8);
63 uart_putstr("\r\n plaintext:\t");
64 uart_hexdump(block, 16);
67 camellia128_init(&s, key);;
68 camellia128_enc(&s, block);
70 uart_putstr("\r\n ciphertext:\t");
71 uart_hexdump(block, 16);
75 void test_decrypt(uint8_t *block, uint8_t *key, uint16_t keylength, bool print){
78 uart_putstr("\r\nCamellia (dec):\r\n key:\t\t");
79 uart_hexdump(key, keylength/8);
80 uart_putstr("\r\n ciphertext:\t");
81 uart_hexdump(block, 16);
83 camellia128_init(&s, key);
84 camellia128_dec(&s, block);
86 uart_putstr("\r\n plaintext:\t");
87 uart_hexdump(block, 16);
91 void nessie_test_iterate(uint8_t *block, uint8_t *key){
93 test_encrypt(block, key, 128, true);
94 test_decrypt(block, key, 128, true);
95 uart_putstr("\r\n100 times:");
97 test_encrypt(block, key, 128, false);
99 test_encrypt(block, key, 128, true);
100 uart_putstr("\r\n1000 times:");
101 for(i=0; i<(999-100); ++i){
102 test_encrypt(block, key, 128, false);
104 test_encrypt(block, key, 128, true);
107 void nessie_test_iterate_inv(uint8_t *block, uint8_t *key){
109 test_decrypt(block, key, 128, true);
110 test_encrypt(block, key, 128, true);
111 uart_putstr("\r\n100 times:");
113 test_decrypt(block, key, 128, false);
115 test_encrypt(block, key, 128, true);
116 uart_putstr("\r\n1000 times:");
117 for(i=0; i<(999-100); ++i){
118 test_decrypt(block, key, 128, false);
120 test_decrypt(block, key, 128, true);
123 prog_uint8_t ntt_test_values_in[16] = {
124 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
125 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
128 prog_uint8_t ntt_test_values_out[16] = {
129 0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73,
130 0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43
132 /* memcmp_P() is now implemented in avr-libc
133 int memcmp_P(const void *s1, PGM_P s2, size_t n){
136 b = pgm_read_byte_near(s2);
137 if( *((uint8_t*)s1) != b)
138 return(*((uint8_t*)s1)-b);
144 void testrun_camellia(void){
145 /* we run the NESSIE test for Camellia here see
146 * https://www.cosic.esat.kuleuven.be/nessie/testvectors/bc/camellia/Camellia-128-128.verified.test-vectors
152 memcpy_P(block, ntt_test_values_in, 16);
153 memcpy_P(key, ntt_test_values_in, 16);
154 test_encrypt(block, key, 128, true);
155 if(memcmp_P(block, ntt_test_values_out, 16)){
156 uart_putstr("\t[FAILED]\r\n");
159 uart_putstr("\t[OK]");
160 test_decrypt(block, key, 128, true);
161 if(memcmp_P(block, ntt_test_values_in, 16)){
162 uart_putstr("\t[FAILED]\r\n");
165 uart_putstr("\t[OK]");
167 /* test set #1 & #2 */
169 for(setn=1; setn<=2; ++setn){
170 for(j=0; j<128; ++j){
171 uart_putstr("\r\n\r\n### SET: ");
172 uart_hexdump(&setn,1);
173 uart_putstr(" Vector: ");
176 memset(block, 0, 16);
178 ((setn&0x1)?key:block)[j>>3] = 1<<(((~j)&0x7));
179 nessie_test_iterate(block, key);
183 for(j=0; j<256; ++j){
184 uart_putstr("\r\n### SET: ");
185 uart_hexdump(&setn,1);
186 uart_putstr(" Vector: ");
189 memset(block, j, 16);
191 nessie_test_iterate(block, key);
194 /* test set #4 (some strange values*/
196 /* test ser #5 & #6 (same as 1&2 but enc and dec exchanged)*/
197 for(setn=5; setn<=6; ++setn){
198 for(j=0; j<128; ++j){
199 uart_putstr("\r\n\r\n### SET: ");
200 uart_hexdump(&setn,1);
201 uart_putstr(" Vector: ");
204 memset(block, 0, 16);
206 ((setn&0x1)?key:block)[j>>3] = 1<<(((~j)&0x7));
207 nessie_test_iterate_inv(block, key);
211 for(j=0; j<256; ++j){
212 uart_putstr("\r\n### SET: ");
213 uart_hexdump(&setn,1);
214 uart_putstr(" Vector: ");
217 memset(block, j, 16);
219 nessie_test_iterate_inv(block, key);
222 /* test set #4 (some strange values*/
228 /*****************************************************************************
230 *****************************************************************************/
239 uart_putstr("\r\n\r\nCrypto-VS (Camellia)\r\nloaded and running\r\n");
242 if (!getnextwordn(str,20)) {DEBUG_S("DBG: W1\r\n"); goto error;}
243 if (strcmp(str, "test")) {DEBUG_S("DBG: 1b\r\n"); goto error;}
248 uart_putstr("ERROR\r\n");