]> git.cryptolib.org Git - avr-crypto-lib.git/blob - nessie_mac_test.c
5e2a88d196137cdf6113671748874c7939298112
[avr-crypto-lib.git] / nessie_mac_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 MACs
8  * 
9  * */
10 #include <stdint.h>
11 #include <string.h>
12 #include "nessie_mac_test.h"
13 #include "nessie_common.h"
14 #include "uart.h"
15
16 nessie_mac_ctx_t nessie_mac_ctx;
17
18 #define KEYSIZE_B ((nessie_mac_ctx.keysize_b+7)/8)
19 #define MACSIZE_B ((nessie_mac_ctx.macsize_b+7)/8)
20
21 #define PRINTKEY nessie_print_item("key", key, KEYSIZE_B)
22 #define PRINTMAC nessie_print_item("MAC", mac, MACSIZE_B)
23
24 static
25 void ascii_mac(char* data, char* desc, uint8_t* key){
26         uint8_t ctx[nessie_mac_ctx.ctx_size_B];
27         uint8_t mac[MACSIZE_B];
28         uint16_t sl;
29         
30         uart_putstr_P(PSTR("\r\n                       message="));
31         uart_putstr(desc);
32         PRINTKEY;
33         nessie_mac_ctx.mac_init(key, nessie_mac_ctx.keysize_b, ctx);
34         sl = strlen(data);
35         while(sl>=nessie_mac_ctx.blocksize_B){
36                 nessie_mac_ctx.mac_next(data, ctx);
37                 data += nessie_mac_ctx.blocksize_B;
38                 sl   -= nessie_mac_ctx.blocksize_B;
39         }
40         nessie_mac_ctx.mac_last(data, sl*8, key, nessie_mac_ctx.keysize_b, ctx);
41         nessie_mac_ctx.mac_conv(mac, ctx);
42         PRINTMAC;
43 }
44
45 // message=1 million times "a"
46
47 static
48 void amillion_mac(uint8_t* key){
49         uint8_t ctx[nessie_mac_ctx.ctx_size_B];
50         uint8_t mac[MACSIZE_B];
51         uint8_t block[nessie_mac_ctx.blocksize_B];
52         uint32_t n=1000000LL;
53         
54         uart_putstr_P(PSTR("\r\n                       message="));
55         uart_putstr_P(PSTR("1 million times \"a\""));
56         PRINTKEY;
57         
58         memset(block, 'a', nessie_mac_ctx.blocksize_B);
59         nessie_mac_ctx.mac_init(key, nessie_mac_ctx.keysize_b, ctx);
60         while(n>=nessie_mac_ctx.blocksize_B){
61                 nessie_mac_ctx.mac_next(block, ctx);
62                 n    -= nessie_mac_ctx.blocksize_B;
63         }
64         nessie_mac_ctx.mac_last(block, n*8, key, nessie_mac_ctx.keysize_b, ctx);
65         nessie_mac_ctx.mac_conv(mac, ctx);
66         PRINTMAC;
67 }
68
69
70 static
71 void zero_mac(uint16_t n, uint8_t* key){
72         uint8_t ctx[nessie_mac_ctx.ctx_size_B];
73         uint8_t mac[MACSIZE_B];
74         uint8_t block[nessie_mac_ctx.blocksize_B];
75         
76         uart_putstr_P(PSTR("\r\n                       message="));
77         if(n>=10000)
78                 uart_putc('0'+n/10000);
79         if(n>=1000)
80                 uart_putc('0'+(n/1000)%10);
81         if(n>=100)
82                 uart_putc('0'+(n/100)%10);
83         if(n>=10)
84                 uart_putc('0'+(n/10)%10);
85         uart_putc('0'+n%10);
86         uart_putstr_P(PSTR(" zero bits"));
87         PRINTKEY;
88         
89         memset(block, 0, nessie_mac_ctx.blocksize_B); 
90         nessie_mac_ctx.mac_init(key, nessie_mac_ctx.keysize_b,ctx);;
91         while(n>=nessie_mac_ctx.blocksize_B*8){
92                 nessie_mac_ctx.mac_next(block, ctx);
93                 n   -= nessie_mac_ctx.blocksize_B*8;
94         }
95         nessie_mac_ctx.mac_last(block, n, key, nessie_mac_ctx.keysize_b, ctx);
96         nessie_mac_ctx.mac_conv(mac, ctx);
97         PRINTMAC;
98 }
99
100 static
101 void one_in512_mac(uint16_t pos, uint8_t* key){
102         uint8_t ctx[nessie_mac_ctx.ctx_size_B];
103         uint8_t mac[MACSIZE_B];
104         uint8_t block[nessie_mac_ctx.blocksize_B];
105         uint16_t n=512;
106         char* tab[8]={"80", "40", "20", "10", 
107                       "08", "04", "02", "01" };
108
109         pos&=511;
110         uart_putstr_P(PSTR("\r\n                       message="));
111         uart_putstr_P(PSTR("512-bit string: "));
112         if((pos/8) >=10){
113                 uart_putc('0'+(pos/8/10)%10);
114         } else {
115                 uart_putc(' ');
116         }
117         uart_putc('0'+(pos/8)%10);
118         uart_putstr_P(PSTR("*00,"));
119         uart_putstr(tab[pos&7]);
120         uart_putc(',');
121         if(63-(pos/8) >=10){
122                 uart_putc('0'+((63-pos/8)/10)%10);
123         } else {
124                 uart_putc(' ');
125         }
126         uart_putc('0'+(63-pos/8)%10);
127         uart_putstr_P(PSTR("*00"));
128         PRINTKEY;
129         
130         /* now the real stuff */
131         memset(block, 0, 512/8);
132         block[pos>>3] = 0x80>>(pos&0x7);
133         nessie_mac_ctx.mac_init(key, nessie_mac_ctx.keysize_b, ctx);;
134         while(n>=nessie_mac_ctx.blocksize_B*8){
135                 nessie_mac_ctx.mac_next(block, ctx);
136                 n   -= nessie_mac_ctx.blocksize_B*8;
137         }
138         nessie_mac_ctx.mac_last(block, n, key, nessie_mac_ctx.keysize_b, ctx);
139         nessie_mac_ctx.mac_conv(mac, ctx);
140         PRINTMAC;
141 }
142
143 static
144 void tv4_mac(uint8_t* key){
145         uint8_t ctx[nessie_mac_ctx.ctx_size_B];
146         uint8_t mac[MACSIZE_B];
147         uint8_t block[256/8];
148         uint16_t n=256;
149         uint32_t i;
150         
151         uart_putstr_P(PSTR("\r\n                       message="));
152         uart_putstr(PSTR("256 zero bits"));
153         memset(block, 0, 256/8);
154         
155         nessie_mac_ctx.mac_init(key, nessie_mac_ctx.keysize_b, ctx);;
156         while(n>=nessie_mac_ctx.blocksize_B*8){
157                 nessie_mac_ctx.mac_next(block, ctx);
158                 n    -= nessie_mac_ctx.blocksize_B*8;
159         }
160         nessie_mac_ctx.mac_last(block, n, key, nessie_mac_ctx.keysize_b, ctx);
161         nessie_mac_ctx.mac_conv(mac, ctx);
162         PRINTMAC;
163         for(i=1; i<100000L; ++i){ /* this assumes BLOCKSIZE >= HASHSIZE */
164                 nessie_mac_ctx.mac_init(key, nessie_mac_ctx.keysize_b, ctx);;
165                 nessie_mac_ctx.mac_last(mac, nessie_mac_ctx.macsize_b, key, nessie_mac_ctx.keysize_b, ctx);
166                 nessie_mac_ctx.mac_conv(mac, ctx);
167         }
168         nessie_print_item("iterated 100000 times", mac, MACSIZE_B);
169 }
170
171
172 void nessie_mac_run(void){
173         uint16_t i;
174         uint8_t set;
175         uint8_t keyproto[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
176                           0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
177                           0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
178         uint8_t key[KEYSIZE_B];
179         
180         nessie_print_header(nessie_mac_ctx.name, nessie_mac_ctx.keysize_b, 0, 0,
181                             nessie_mac_ctx.macsize_b, 0);
182         /* test set 1 */
183         char* challange[10][2]= {
184                 {"", "\"\" (empty string)"},
185                 {"a", "\"a\""},
186                 {"abc", "\"abc\""},
187                 {"message digest", "\"message digest\""},
188                 {"abcdefghijklmnopqrstuvwxyz","\"abcdefghijklmnopqrstuvwxyz\""},
189                 {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
190                         "\"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\""},
191                 {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
192                  "abcdefghijklmnopqrstuvwxyz"
193                  "0123456789"   , "\"A...Za...z0...9\""},
194                 {"1234567890" "1234567890" "1234567890" "1234567890" 
195                  "1234567890" "1234567890" "1234567890" "1234567890",
196                  "8 times \"1234567890\""},
197                 {"Now is the time for all ", "\"Now is the time for all \""},
198                 {"Now is the time for it", "\"Now is the time for it\""}
199         };
200         set=1;
201         nessie_print_setheader(set);
202         for(i=0; i<KEYSIZE_B; ++i){
203                 key[i] = keyproto[i%sizeof(keyproto)];
204         }
205         for(i=0; i<10; ++i){
206                 nessie_print_set_vector(set, i);
207                 ascii_mac(challange[i][0], challange[i][1], key);
208         }
209         nessie_print_set_vector(set, i);
210         amillion_mac(key);
211         for(i=0; i<KEYSIZE_B; ++i){
212                 key[i] = keyproto[16+i%8];
213         }
214         for(i=0; i<10; ++i){
215                 nessie_print_set_vector(set, 11+i);
216                 ascii_mac(challange[i][0], challange[i][1], key);
217         }
218         nessie_print_set_vector(set, 11+i);
219         amillion_mac(key);
220         /* test set 2 */
221         set=2;
222         for(i=0; i<KEYSIZE_B; ++i){
223                 key[i] = keyproto[i%sizeof(keyproto)];
224         }
225         nessie_print_setheader(set);
226         for(i=0; i<1024; ++i){
227                 nessie_print_set_vector(set, i);
228                 zero_mac(i, key);
229         }
230         /* test set 3 */
231         set=3;
232         nessie_print_setheader(set);
233         /* we use the same key as above */
234         for(i=0; i<512; ++i){
235                 nessie_print_set_vector(set, i);
236                 one_in512_mac(i, key);
237         }
238         /* test set 4 */
239         set=4;
240         nessie_print_setheader(set);
241         /* we use the same key as above */
242         nessie_print_set_vector(set, 0);
243         tv4_mac(key);
244         /* test set 5 */
245         for(i=0; i<nessie_mac_ctx.keysize_b; ++i){
246                 nessie_print_set_vector(set, i);
247                 memset(key, 0, KEYSIZE_B);
248                 key[i>>3]=0x80>>(i&0x7);
249                 ascii_mac("ABC", "\"ABC\"", key);
250         }
251         nessie_print_footer();
252 }