4 * email: daniel.otte@rub.de
7 * a suit for running the nessie-tests for blockciphers
12 #include "nessie_bc_test.h"
17 nessie_bc_ctx_t nessie_bc_ctx;
19 static void printblock(uint8_t* block, uint16_t blocksize_bit){
20 char tab [] = {'0', '1', '2', '3',
25 for(i=0; i<(blocksize_bit+7)/8; ++i){
26 uart_putc(tab[(block[i])>>4]);
27 uart_putc(tab[(block[i])&0xf]);
32 #define BYTESPERLINE 16
34 static void printitem(char* name, uint8_t* buffer, uint16_t size_B){
37 name_len=strlen(name);
38 if(name_len>SPACES-1){
39 uart_putstr_P(PSTR("\r\n!!! formatting error !!!\r\n"));
42 uart_putstr_P(PSTR("\r\n"));
43 for(i=0; i<SPACES-name_len-1; ++i){
48 /* now the data printing begins */
49 if(size_B<=BYTESPERLINE){
50 /* one line seems sufficient */
51 printblock(buffer, size_B*8);
53 /* we need more lines */
54 printblock(buffer, BYTESPERLINE*8); /* first line */
55 int16_t toprint = size_B - BYTESPERLINE;
56 buffer += BYTESPERLINE;
58 uart_putstr_P(PSTR("\r\n"));
59 for(i=0; i<SPACES; ++i){
62 printblock(buffer, ((toprint>BYTESPERLINE)?BYTESPERLINE:toprint)*8);
63 buffer += BYTESPERLINE;
64 toprint -= BYTESPERLINE;
69 void nessie_bc_enc(uint8_t* key, uint8_t* pt){
70 uint8_t ctx[nessie_bc_ctx.ctx_size_B];
71 uint8_t buffer[nessie_bc_ctx.blocksize_B];
75 printitem("key", key, (nessie_bc_ctx.keysize_b+7)/8);
76 nessie_bc_ctx.cipher_genctx(key, nessie_bc_ctx.keysize_b, ctx);
77 memcpy(buffer, pt, nessie_bc_ctx.blocksize_B);
78 printitem("plain", buffer, nessie_bc_ctx.blocksize_B);
79 nessie_bc_ctx.cipher_enc(buffer, ctx);
80 printitem("cipher", buffer, nessie_bc_ctx.blocksize_B);
81 nessie_bc_ctx.cipher_dec(buffer, ctx);
82 printitem("decrypted", buffer, nessie_bc_ctx.blocksize_B);
85 memcpy(buffer, pt, nessie_bc_ctx.blocksize_B);
87 nessie_bc_ctx.cipher_enc(buffer, ctx);
89 printitem("Iterated 100 times", buffer, nessie_bc_ctx.blocksize_B);
90 #ifndef NESSIE_NO1KTEST
91 /* 1000 times test, we use the 100 precedig steps to fasten things a bit */
93 nessie_bc_ctx.cipher_enc(buffer, ctx);
95 printitem("Iterated 1000 times", buffer, nessie_bc_ctx.blocksize_B);
99 void nessie_bc_dec(uint8_t* key, uint8_t* ct){
100 uint8_t ctx[nessie_bc_ctx.ctx_size_B];
101 uint8_t buffer[nessie_bc_ctx.blocksize_B];
104 printitem("key", key, (nessie_bc_ctx.keysize_b+7)/8);
105 nessie_bc_ctx.cipher_genctx(key, nessie_bc_ctx.keysize_b, ctx);
106 memcpy(buffer, ct, nessie_bc_ctx.blocksize_B);
107 printitem("cipher", buffer, nessie_bc_ctx.blocksize_B);
108 nessie_bc_ctx.cipher_dec(buffer, ctx);
109 printitem("plain", buffer, nessie_bc_ctx.blocksize_B);
110 nessie_bc_ctx.cipher_enc(buffer, ctx);
111 printitem("encrypted", buffer, nessie_bc_ctx.blocksize_B);
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);
126 Test vectors -- set 3
127 =====================
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====================="));
136 ********************************************************************************
137 *Project NESSIE - New European Schemes for Signature, Integrity, and Encryption*
138 ********************************************************************************
140 Primitive Name: Serpent
141 =======================
146 static void print_header(void){
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"
153 uart_putstr_P(PSTR("Primitive Name: "));
154 uart_putstr(nessie_bc_ctx.name);
155 uart_putstr_P(PSTR("\r\n"));
156 for(i=0; i<16+strlen(nessie_bc_ctx.name); ++i){
159 uart_putstr_P(PSTR("\r\nKey size: "));
160 if(nessie_bc_ctx.keysize_b>100){
161 uart_putc('0'+nessie_bc_ctx.keysize_b/100);
163 if(nessie_bc_ctx.keysize_b>10){
164 uart_putc('0'+(nessie_bc_ctx.keysize_b/10)%10);
166 uart_putc('0'+nessie_bc_ctx.keysize_b%10);
167 uart_putstr_P(PSTR(" bits\r\nBlock size: "));
168 if(nessie_bc_ctx.blocksize_B*8>100){
169 uart_putc('0'+(nessie_bc_ctx.blocksize_B*8)/100);
171 if(nessie_bc_ctx.blocksize_B*8>10){
172 uart_putc('0'+((nessie_bc_ctx.blocksize_B*8)/10)%10);
174 uart_putc('0'+(nessie_bc_ctx.blocksize_B*8)%10);
175 uart_putstr_P(PSTR(" bits"));
178 static void print_footer(void){
179 uart_putstr_P(PSTR("\r\n\r\n\r\n\r\nEnd of test vectors\r\n\r\n"));
182 void nessie_bc_run(void){
185 uint8_t key[(nessie_bc_ctx.keysize_b+7)/8];
186 uint8_t buffer[nessie_bc_ctx.blocksize_B];
191 print_setheader(set);
192 for(i=0; i<nessie_bc_ctx.keysize_b; ++i){
193 print_set_vector(set, i);
194 memset(key, 0, (nessie_bc_ctx.keysize_b+7)/8);
195 key[i/8] |= 0x80>>(i%8);
196 memset(buffer, 0, nessie_bc_ctx.blocksize_B);
197 nessie_bc_enc(key, buffer);
201 print_setheader(set);
202 for(i=0; i<nessie_bc_ctx.blocksize_B*8; ++i){
203 print_set_vector(set, i);
204 memset(key, 0, (nessie_bc_ctx.keysize_b+7)/8);
205 memset(buffer, 0, nessie_bc_ctx.blocksize_B);
206 buffer[i/8] |= 0x80>>(i%8);
207 nessie_bc_enc(key, buffer);
211 print_setheader(set);
212 for(i=0; i<256; ++i){
213 print_set_vector(set, i);
214 memset(key, i, (nessie_bc_ctx.keysize_b+7)/8);
215 memset(buffer, i, nessie_bc_ctx.blocksize_B);
216 nessie_bc_enc(key, buffer);
220 print_setheader(set);
222 print_set_vector(set, 0);
223 for(i=0; i<(nessie_bc_ctx.keysize_b+7)/8; ++i){
226 for(i=0; i<nessie_bc_ctx.blocksize_B; ++i){
229 nessie_bc_enc(key, buffer);
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_bc_ctx.keysize_b+7)/8; ++i){
239 key[i]=kasumi_key[i%sizeof(kasumi_key)];
241 for(i=0; i<nessie_bc_ctx.blocksize_B; ++i){
242 buffer[i]=kasumi_plain[i%sizeof(kasumi_plain)];
244 nessie_bc_enc(key, buffer);
248 print_setheader(set);
249 for(i=0; i<nessie_bc_ctx.keysize_b; ++i){
250 print_set_vector(set, i);
251 memset(key, 0, (nessie_bc_ctx.keysize_b+7)/8);
252 key[i/8] |= 0x80>>(i%8);
253 memset(buffer, 0, nessie_bc_ctx.blocksize_B);
254 nessie_bc_dec(key, buffer);
258 print_setheader(set);
259 for(i=0; i<nessie_bc_ctx.blocksize_B*8; ++i){
260 print_set_vector(set, i);
261 memset(key, 0, (nessie_bc_ctx.keysize_b+7)/8);
262 memset(buffer, 0, nessie_bc_ctx.blocksize_B);
263 buffer[i/8] |= 0x80>>(i%8);
264 nessie_bc_dec(key, buffer);
268 print_setheader(set);
269 for(i=0; i<256; ++i){
270 print_set_vector(set, i);
271 memset(key, i, (nessie_bc_ctx.keysize_b+7)/8);
272 memset(buffer, i, nessie_bc_ctx.blocksize_B);
273 nessie_bc_dec(key, buffer);
277 print_setheader(set);
279 print_set_vector(set, 0);
280 for(i=0; i<(nessie_bc_ctx.keysize_b+7)/8; ++i){
283 for(i=0; i<nessie_bc_ctx.blocksize_B; ++i){
286 nessie_bc_dec(key, buffer);
288 print_set_vector(set, 1);
289 for(i=0; i<(nessie_bc_ctx.keysize_b+7)/8; ++i){
290 key[i]=kasumi_key[i%sizeof(kasumi_key)];
292 for(i=0; i<nessie_bc_ctx.blocksize_B; ++i){
293 buffer[i]=kasumi_plain[i%sizeof(kasumi_plain)];
295 nessie_bc_dec(key, buffer);