]> git.cryptolib.org Git - avr-crypto-lib.git/blob - nessie_bc_test.c
fix of a little bug
[avr-crypto-lib.git] / nessie_bc_test.c
1 /**
2  * 
3  * author: Daniel Otte
4  * email:  daniel.otte@rub.de
5  * license: GPLv3
6  * 
7  * a suit for running the nessie-tests for blockciphers
8  * 
9  * */
10 #include <stdint.h>
11 #include <string.h>
12 #include "nessie_bc_test.h"
13 #include "uart.h"
14
15
16
17 nessie_ctx_t nessie_ctx;
18
19 static void printblock(uint8_t* block, uint16_t blocksize_bit){
20         char tab [] = {'0', '1', '2', '3', 
21                                    '4', '5', '6', '7', 
22                                    '8', '9', 'A', 'B', 
23                                    'C', 'D', 'E', 'F'};
24         uint16_t i;
25         for(i=0; i<(blocksize_bit+7)/8; ++i){
26                 uart_putc(tab[(block[i])>>4]);
27                 uart_putc(tab[(block[i])&0xf]);
28         }                                  
29 }
30
31 #define SPACES 31
32 #define BYTESPERLINE 16
33
34 static void printitem(char* name, uint8_t* buffer, uint16_t size_B){
35         uint8_t name_len;
36         uint8_t i;
37         name_len=strlen(name);
38         if(name_len>SPACES-1){
39                 uart_putstr_P(PSTR("\r\n!!! formatting error !!!\r\n"));
40                 return;
41         }
42         uart_putstr_P(PSTR("\r\n"));
43         for(i=0; i<SPACES-name_len-1; ++i){
44                 uart_putc(' ');
45         }
46         uart_putstr(name);
47         uart_putc('=');
48         /* now the data printing begins */
49         if(size_B<=BYTESPERLINE){
50                 /* one line seems sufficient */
51                 printblock(buffer, size_B*8);
52         } else {
53                 /* we need more lines */
54                 printblock(buffer, BYTESPERLINE*8); /* first line */
55                 int16_t toprint = size_B - BYTESPERLINE;
56                 buffer += BYTESPERLINE;
57                 while(toprint > 0){
58                         uart_putstr_P(PSTR("\r\n"));
59                         for(i=0; i<SPACES; ++i){
60                                 uart_putc(' ');
61                         }
62                         printblock(buffer, ((toprint>BYTESPERLINE)?BYTESPERLINE:toprint)*8);
63                         buffer  += BYTESPERLINE;
64                         toprint -= BYTESPERLINE;
65                 }
66         }
67
68
69 void nessie_enc(uint8_t* key, uint8_t* pt){
70         uint8_t ctx[nessie_ctx.ctx_size_B];
71         uint8_t buffer[nessie_ctx.blocksize_B];
72         uint16_t i;
73         
74         /* single test */
75         printitem("key", key, (nessie_ctx.keysize+7)/8);
76         nessie_ctx.cipher_genctx(key, nessie_ctx.keysize, ctx);
77         memcpy(buffer, pt, nessie_ctx.blocksize_B);
78         printitem("plain", buffer, nessie_ctx.blocksize_B);
79         nessie_ctx.cipher_enc(buffer, ctx);
80         printitem("cipher", buffer, nessie_ctx.blocksize_B);
81         nessie_ctx.cipher_dec(buffer, ctx);
82         printitem("decrypted", buffer, nessie_ctx.blocksize_B);
83         
84         /* 100 times test */
85         memcpy(buffer, pt, nessie_ctx.blocksize_B);
86         for(i=0; i<100; ++i){
87                 nessie_ctx.cipher_enc(buffer, ctx);
88         }
89         printitem("Iterated 100 times", buffer, nessie_ctx.blocksize_B);
90 #ifndef NESSIE_NO1KTEST 
91         /* 1000 times test, we use the 100 precedig steps to fasten things a bit */
92         for(; i<1000; ++i){
93                 nessie_ctx.cipher_enc(buffer, ctx);
94         }
95         printitem("Iterated 1000 times", buffer, nessie_ctx.blocksize_B);
96 #endif
97 }
98
99 void nessie_dec(uint8_t* key, uint8_t* ct){
100         uint8_t ctx[nessie_ctx.ctx_size_B];
101         uint8_t buffer[nessie_ctx.blocksize_B];
102         
103         /* single test */
104         printitem("key", key, (nessie_ctx.keysize+7)/8);
105         nessie_ctx.cipher_genctx(key, nessie_ctx.keysize, ctx);
106         memcpy(buffer, ct, nessie_ctx.blocksize_B);
107         printitem("cipher", buffer, nessie_ctx.blocksize_B);
108         nessie_ctx.cipher_dec(buffer, ctx);
109         printitem("plain", buffer, nessie_ctx.blocksize_B);
110         nessie_ctx.cipher_enc(buffer, ctx);
111         printitem("encrypted", buffer, nessie_ctx.blocksize_B);
112         
113 }
114
115 static void print_set_vector(uint8_t set, uint16_t vector){
116         uart_putstr_P(PSTR("\r\n\r\nSet "));
117         uart_putc('0'+set%10);
118         uart_putstr_P(PSTR(", vector#"));
119         uart_putc((vector<100)?' ':'0'+vector/100);
120         uart_putc((vector<10 )?' ':'0'+(vector/10)%10);
121         uart_putc('0'+vector%10);
122         uart_putc(':');
123 }
124
125 /* example:
126 Test vectors -- set 3
127 =====================
128  */ 
129 static void print_setheader(uint8_t set){
130         uart_putstr_P(PSTR("\r\n\r\nTest vectors -- set "));
131         uart_putc('0'+set%10);
132         uart_putstr_P(PSTR("\r\n====================="));
133 }
134
135 /* example:
136 ********************************************************************************
137 *Project NESSIE - New European Schemes for Signature, Integrity, and Encryption*
138 ********************************************************************************
139
140 Primitive Name: Serpent
141 =======================
142 Key size: 256 bits
143 Block size: 128 bits
144 */
145
146 static void print_header(void){
147         uint16_t i;
148         uart_putstr_P(PSTR("\r\n\r\n"
149         "********************************************************************************\r\n"
150         "* micro-cryt - crypto primitives for microcontrolles by Daniel Otte            *\r\n"
151         "********************************************************************************\r\n"
152         "\r\n"));
153         uart_putstr_P(PSTR("Primitive Name: "));
154         uart_putstr(nessie_ctx.name);
155         uart_putstr_P(PSTR("\r\n"));
156         for(i=0; i<16+strlen(nessie_ctx.name); ++i){
157                 uart_putc('=');
158         }
159         uart_putstr_P(PSTR("\r\nKey size: "));
160         if(nessie_ctx.keysize>100){
161                 uart_putc('0'+nessie_ctx.keysize/100);
162         }
163         if(nessie_ctx.keysize>10){
164                 uart_putc('0'+(nessie_ctx.keysize/10)%10);
165         }
166         uart_putc('0'+nessie_ctx.keysize%10);
167         uart_putstr_P(PSTR(" bits\r\nBlock size: "));
168         if(nessie_ctx.blocksize_B*8>100){
169                 uart_putc('0'+(nessie_ctx.blocksize_B*8)/100);
170         }
171         if(nessie_ctx.blocksize_B*8>10){
172                 uart_putc('0'+((nessie_ctx.blocksize_B*8)/10)%10);
173         }
174         uart_putc('0'+(nessie_ctx.blocksize_B*8)%10);
175         uart_putstr_P(PSTR(" bits"));
176 }
177
178 static void print_footer(void){
179         uart_putstr_P(PSTR("\r\n\r\n\r\n\r\nEnd of test vectors"));
180 }
181
182 void nessie_run(void){
183         uint16_t i;
184         uint8_t set;
185         uint8_t key[(nessie_ctx.keysize+7)/8];
186         uint8_t buffer[nessie_ctx.blocksize_B];
187         
188         print_header();
189         /* test set 1 */
190         set=1;
191         print_setheader(set);
192         for(i=0; i<nessie_ctx.keysize; ++i){
193                 print_set_vector(set, i);
194                 memset(key, 0, (nessie_ctx.keysize+7)/8);
195                 key[i/8] |= 0x80>>(i%8);
196                 memset(buffer, 0, nessie_ctx.blocksize_B);
197                 nessie_enc(key, buffer);
198         }
199         /* test set 2 */
200         set=2;
201         print_setheader(set);
202         for(i=0; i<nessie_ctx.blocksize_B*8; ++i){
203                 print_set_vector(set, i);
204                 memset(key, 0, (nessie_ctx.keysize+7)/8);
205                 memset(buffer, 0, nessie_ctx.blocksize_B);
206                 buffer[i/8] |= 0x80>>(i%8);
207                 nessie_enc(key, buffer);
208         }
209         /* test set 3 */
210         set=3;
211         print_setheader(set);
212         for(i=0; i<256; ++i){
213                 print_set_vector(set, i);
214                 memset(key, i, (nessie_ctx.keysize+7)/8);
215                 memset(buffer, i, nessie_ctx.blocksize_B);
216                 nessie_enc(key, buffer);
217         }
218         /* test set 4 */
219         set=4;
220         print_setheader(set);
221         /* 4 - 0*/
222         print_set_vector(set, 0);
223         for(i=0; i<(nessie_ctx.keysize+7)/8; ++i){
224                 key[i]=i;
225         }
226         for(i=0; i<nessie_ctx.blocksize_B; ++i){
227                 buffer[i]=i*0x11;
228         }
229         nessie_enc(key, buffer);
230         /* 4 - 1 */
231         print_set_vector(set, 1);       
232     /* This is the test vectors in Kasumi */
233     static uint8_t kasumi_key[] = {
234            0x2B, 0xD6, 0x45, 0x9F, 0x82, 0xC5, 0xB3, 0x00,
235            0x95, 0x2C, 0x49, 0x10, 0x48, 0x81, 0xFF, 0x48 };
236     static uint8_t kasumi_plain[]={
237            0xEA, 0x02, 0x47, 0x14, 0xAD, 0x5C, 0x4D, 0x84 };
238         for(i=0; i<(nessie_ctx.keysize+7)/8; ++i){
239                 key[i]=kasumi_key[i%sizeof(kasumi_key)];
240         }
241         for(i=0; i<nessie_ctx.blocksize_B; ++i){
242                 buffer[i]=kasumi_plain[i%sizeof(kasumi_plain)];
243         }
244         /* half done ;-) */
245         /* test set 5 */
246         set=1;
247         print_setheader(set);
248         for(i=0; i<nessie_ctx.keysize; ++i){
249                 print_set_vector(set, i);
250                 memset(key, 0, (nessie_ctx.keysize+7)/8);
251                 key[i/8] |= 0x80>>(i%8);
252                 memset(buffer, 0, nessie_ctx.blocksize_B);
253                 nessie_dec(key, buffer);
254         }
255         /* test set 6 */
256         set=6;
257         print_setheader(set);
258         for(i=0; i<nessie_ctx.blocksize_B*8; ++i){
259                 print_set_vector(set, i);
260                 memset(key, 0, (nessie_ctx.keysize+7)/8);
261                 memset(buffer, 0, nessie_ctx.blocksize_B);
262                 buffer[i/8] |= 0x80>>(i%8);
263                 nessie_dec(key, buffer);
264         }
265         /* test set 7 */
266         set=7;
267         print_setheader(set);
268         for(i=0; i<256; ++i){
269                 print_set_vector(set, i);
270                 memset(key, i, (nessie_ctx.keysize+7)/8);
271                 memset(buffer, i, nessie_ctx.blocksize_B);
272                 nessie_dec(key, buffer);
273         }
274         /* test set 8 */
275         set=8;
276         print_setheader(set);
277         /* 8 - 0*/
278         print_set_vector(set, 0);
279         for(i=0; i<(nessie_ctx.keysize+7)/8; ++i){
280                 key[i]=i;
281         }
282         for(i=0; i<nessie_ctx.blocksize_B; ++i){
283                 buffer[i]=i*0x11;
284         }
285         nessie_dec(key, buffer);
286         /* 8 - 1 */
287         print_set_vector(set, 1);       
288         for(i=0; i<(nessie_ctx.keysize+7)/8; ++i){
289                 key[i]=kasumi_key[i%sizeof(kasumi_key)];
290         }
291         for(i=0; i<nessie_ctx.blocksize_B; ++i){
292                 buffer[i]=kasumi_plain[i%sizeof(kasumi_plain)];
293         }
294         print_footer();
295 }