]> git.cryptolib.org Git - avr-crypto-lib.git/blob - main-camellia-test.c
insereated GPLv3 stub
[avr-crypto-lib.git] / main-camellia-test.c
1 /* main-camellia-test.c */
2 /*
3     This file is part of the Crypto-avr-lib/microcrypt-lib.
4     Copyright (C) 2008  Daniel Otte (daniel.otte@rub.de)
5
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.
10
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.
15
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/>.
18 */
19 /*
20  * camellia test-suit
21  * 
22  */
23
24 #include "config.h"
25 #include "serial-tools.h"
26 #include "uart.h"
27 #include "debug.h"
28
29 #include "camellia.h"
30
31 #include <stdint.h>
32 #include <string.h>
33 #include <avr/pgmspace.h>
34
35
36 #ifndef BOOL
37 #define BOOL
38  #ifndef __BOOL
39  #define __BOOL
40   #ifndef __BOOL__
41   #define __BOOL__
42         typedef enum{false=0,true=1} bool;
43   #endif
44  #endif
45 #endif
46
47
48
49 /*****************************************************************************
50  *  additional validation-functions                                                                                      *
51  *****************************************************************************/
52
53 /*****************************************************************************
54  *  self tests                                                                                                                           *
55  *****************************************************************************/
56 void camellia128_ctx_dump(camellia128_ctx_t *s);
57
58 void test_encrypt(uint8_t *block, uint8_t *key, uint16_t keylength, bool print){
59         camellia128_ctx_t s;
60         if (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);
65         }
66         
67         camellia128_init(&s, key);;
68         camellia128_enc(&s, block);
69         if (print){
70                 uart_putstr("\r\n ciphertext:\t");
71                 uart_hexdump(block, 16);
72         }
73
74
75 void test_decrypt(uint8_t *block, uint8_t *key, uint16_t keylength, bool print){
76         camellia128_ctx_t s;
77         if (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);
82         }
83         camellia128_init(&s, key);
84         camellia128_dec(&s, block);
85         if (print){
86                 uart_putstr("\r\n plaintext:\t");
87                 uart_hexdump(block, 16);
88         }
89
90
91 void nessie_test_iterate(uint8_t *block, uint8_t *key){
92         uint16_t i;
93         test_encrypt(block, key, 128, true);
94         test_decrypt(block, key, 128, true);
95         uart_putstr("\r\n100 times:");
96         for(i=0; i<99; ++i){
97                 test_encrypt(block, key, 128, false);
98         }
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);
103         }
104         test_encrypt(block, key, 128, true);
105 }
106
107 void nessie_test_iterate_inv(uint8_t *block, uint8_t *key){
108         uint16_t i;
109         test_decrypt(block, key, 128, true);
110         test_encrypt(block, key, 128, true);
111         uart_putstr("\r\n100 times:");
112         for(i=0; i<99; ++i){
113                 test_decrypt(block, key, 128, false);
114         }
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);
119         }
120         test_decrypt(block, key, 128, true);
121 }
122
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
126 };
127
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
131 };
132 /* memcmp_P() is now implemented in avr-libc
133 int memcmp_P(const void *s1, PGM_P s2, size_t n){
134         uint8_t b;
135         while(n--){
136                 b = pgm_read_byte_near(s2);
137                 if( *((uint8_t*)s1) != b)
138                         return(*((uint8_t*)s1)-b);
139                 ++s1; ++s2;
140         }
141         return 0;
142 }
143 */
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
147          * for the vectors 
148          */
149         unsigned j, setn;
150         uint8_t block[16];
151         uint8_t key[16];
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");
157                 return;
158         }
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");
163                 return;
164         }
165         uart_putstr("\t[OK]");
166         
167 /* test set #1 & #2 */
168         setn=1;
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: ");
174                         uart_hexdump(&j,1);
175                         
176                         memset(block, 0, 16);
177                         memset(key, 0, 16);
178                         ((setn&0x1)?key:block)[j>>3] = 1<<(((~j)&0x7));
179                         nessie_test_iterate(block, key);
180                 }
181         }
182 /* test set #3 */       
183         for(j=0; j<256; ++j){
184                 uart_putstr("\r\n### SET: ");
185                 uart_hexdump(&setn,1);
186                 uart_putstr(" Vector: ");
187                 uart_hexdump(&j,1);
188                 
189                 memset(block, j, 16);
190                 memset(key, 0, 16);
191                 nessie_test_iterate(block, key);
192         }
193         setn++;
194 /* test set #4 (some strange values*/
195         setn++;
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: ");
202                         uart_hexdump(&j,1);
203                         
204                         memset(block, 0, 16);
205                         memset(key, 0, 16);
206                         ((setn&0x1)?key:block)[j>>3] = 1<<(((~j)&0x7));
207                         nessie_test_iterate_inv(block, key);
208                 }
209         }
210 /* test set #7 */       
211         for(j=0; j<256; ++j){
212                 uart_putstr("\r\n### SET: ");
213                 uart_hexdump(&setn,1);
214                 uart_putstr(" Vector: ");
215                 uart_hexdump(&j,1);
216                 
217                 memset(block, j, 16);
218                 memset(key, 0, 16);
219                 nessie_test_iterate_inv(block, key);
220         }
221         setn++;
222 /* test set #4 (some strange values*/
223         setn++;
224 }
225
226
227
228 /*****************************************************************************
229  *  main                                                                                                                                         *
230  *****************************************************************************/
231
232 int main (void){
233         char str[20];
234
235         
236         DEBUG_INIT();
237         uart_putstr("\r\n");
238
239         uart_putstr("\r\n\r\nCrypto-VS (Camellia)\r\nloaded and running\r\n");
240 restart:
241         while(1){ 
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;}
244                         testrun_camellia();
245                 goto restart;           
246                 continue;
247         error:
248                 uart_putstr("ERROR\r\n");
249         } /* while (1) */
250 }
251