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