]> git.cryptolib.org Git - avr-crypto-lib.git/blob - main-camellia-test.c
649cf214213766bc0cc6107d08d4bf8203925ddf
[avr-crypto-lib.git] / main-camellia-test.c
1 /*
2  * camellia test-suit
3  * 
4  */
5
6 #include "config.h"
7 #include "serial-tools.h"
8 #include "uart.h"
9 #include "debug.h"
10
11 #include "camellia.h"
12
13 #include <stdint.h>
14 #include <string.h>
15 #include <avr/pgmspace.h>
16
17
18 #ifndef BOOL
19 #define BOOL
20  #ifndef __BOOL
21  #define __BOOL
22   #ifndef __BOOL__
23   #define __BOOL__
24         typedef enum{false=0,true=1} bool;
25   #endif
26  #endif
27 #endif
28
29
30
31 /*****************************************************************************
32  *  additional validation-functions                                                                                      *
33  *****************************************************************************/
34
35 /*****************************************************************************
36  *  self tests                                                                                                                           *
37  *****************************************************************************/
38 void camellia128_ctx_dump(camellia128_ctx_t *s);
39
40 void test_encrypt(uint8_t *block, uint8_t *key, uint16_t keylength, bool print){
41         camellia128_ctx_t s;
42         if (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);
47         }
48         
49         camellia128_init(&s, key);;
50         camellia128_enc(&s, block);
51         if (print){
52                 uart_putstr("\r\n ciphertext:\t");
53                 uart_hexdump(block, 16);
54         }
55
56
57 void test_decrypt(uint8_t *block, uint8_t *key, uint16_t keylength, bool print){
58         camellia128_ctx_t s;
59         if (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);
64         }
65         camellia128_init(&s, key);
66         camellia128_dec(&s, block);
67         if (print){
68                 uart_putstr("\r\n plaintext:\t");
69                 uart_hexdump(block, 16);
70         }
71
72
73 void nessie_test_iterate(uint8_t *block, uint8_t *key){
74         uint16_t i;
75         test_encrypt(block, key, 128, true);
76         test_decrypt(block, key, 128, true);
77         uart_putstr("\r\n100 times:");
78         for(i=0; i<99; ++i){
79                 test_encrypt(block, key, 128, false);
80         }
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);
85         }
86         test_encrypt(block, key, 128, true);
87 }
88
89 void nessie_test_iterate_inv(uint8_t *block, uint8_t *key){
90         uint16_t i;
91         test_decrypt(block, key, 128, true);
92         test_encrypt(block, key, 128, true);
93         uart_putstr("\r\n100 times:");
94         for(i=0; i<99; ++i){
95                 test_decrypt(block, key, 128, false);
96         }
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);
101         }
102         test_decrypt(block, key, 128, true);
103 }
104
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
108 };
109
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
113 };
114 /* memcmp_P() is now implemented in avr-libc
115 int memcmp_P(const void *s1, PGM_P s2, size_t n){
116         uint8_t b;
117         while(n--){
118                 b = pgm_read_byte_near(s2);
119                 if( *((uint8_t*)s1) != b)
120                         return(*((uint8_t*)s1)-b);
121                 ++s1; ++s2;
122         }
123         return 0;
124 }
125 */
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
129          * for the vectors 
130          */
131         unsigned j, setn;
132         uint8_t block[16];
133         uint8_t key[16];
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");
139                 return;
140         }
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");
145                 return;
146         }
147         uart_putstr("\t[OK]");
148         
149 /* test set #1 & #2 */
150         setn=1;
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: ");
156                         uart_hexdump(&j,1);
157                         
158                         memset(block, 0, 16);
159                         memset(key, 0, 16);
160                         ((setn&0x1)?key:block)[j>>3] = 1<<(((~j)&0x7));
161                         nessie_test_iterate(block, key);
162                 }
163         }
164 /* test set #3 */       
165         for(j=0; j<256; ++j){
166                 uart_putstr("\r\n### SET: ");
167                 uart_hexdump(&setn,1);
168                 uart_putstr(" Vector: ");
169                 uart_hexdump(&j,1);
170                 
171                 memset(block, j, 16);
172                 memset(key, 0, 16);
173                 nessie_test_iterate(block, key);
174         }
175         setn++;
176 /* test set #4 (some strange values*/
177         setn++;
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: ");
184                         uart_hexdump(&j,1);
185                         
186                         memset(block, 0, 16);
187                         memset(key, 0, 16);
188                         ((setn&0x1)?key:block)[j>>3] = 1<<(((~j)&0x7));
189                         nessie_test_iterate_inv(block, key);
190                 }
191         }
192 /* test set #7 */       
193         for(j=0; j<256; ++j){
194                 uart_putstr("\r\n### SET: ");
195                 uart_hexdump(&setn,1);
196                 uart_putstr(" Vector: ");
197                 uart_hexdump(&j,1);
198                 
199                 memset(block, j, 16);
200                 memset(key, 0, 16);
201                 nessie_test_iterate_inv(block, key);
202         }
203         setn++;
204 /* test set #4 (some strange values*/
205         setn++;
206 }
207
208
209
210 /*****************************************************************************
211  *  main                                                                                                                                         *
212  *****************************************************************************/
213
214 int main (void){
215         char str[20];
216
217         
218         DEBUG_INIT();
219         uart_putstr("\r\n");
220
221         uart_putstr("\r\n\r\nCrypto-VS (Camellia)\r\nloaded and running\r\n");
222 restart:
223         while(1){ 
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;}
226                         testrun_camellia();
227                 goto restart;           
228                 continue;
229         error:
230                 uart_putstr("ERROR\r\n");
231         } /* while (1) */
232 }
233