]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/nessie_mac_test.c
90e33de553d9ffcb14e3c2c551ee7d2485ef3f39
[avr-crypto-lib.git] / test_src / nessie_mac_test.c
1 /* nessie_mac_test.c */
2 /*
3     This file is part of the AVR-Crypto-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         NESSIE_PUTSTR_P(PSTR("\r\n                       message="));
49         NESSIE_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         uint16_t i=0;
72         
73         NESSIE_PUTSTR_P(PSTR("\r\n                       message="));
74         NESSIE_PUTSTR_P(PSTR("1 million times \"a\""));
75         PRINTKEY;
76         
77         memset(block, 'a', nessie_mac_ctx.blocksize_B);
78         nessie_mac_ctx.mac_init(key, nessie_mac_ctx.keysize_b, ctx);
79         while(n>nessie_mac_ctx.blocksize_B){
80                 nessie_mac_ctx.mac_next(block, ctx);
81                 n    -= nessie_mac_ctx.blocksize_B;
82                 NESSIE_SEND_ALIVE_A(i++);
83         }
84         nessie_mac_ctx.mac_last(block, n*8, key, nessie_mac_ctx.keysize_b, ctx);
85         nessie_mac_ctx.mac_conv(mac, ctx);
86         PRINTMAC;
87 }
88
89
90 static
91 void zero_mac(uint16_t n, uint8_t* key){
92         uint8_t ctx[nessie_mac_ctx.ctx_size_B];
93         uint8_t mac[MACSIZE_B];
94         uint8_t block[nessie_mac_ctx.blocksize_B];
95         
96         NESSIE_PUTSTR_P(PSTR("\r\n                       message="));
97         if(n>=10000)
98                 NESSIE_PUTC('0'+n/10000);
99         if(n>=1000)
100                 NESSIE_PUTC('0'+(n/1000)%10);
101         if(n>=100)
102                 NESSIE_PUTC('0'+(n/100)%10);
103         if(n>=10)
104                 NESSIE_PUTC('0'+(n/10)%10);
105         NESSIE_PUTC('0'+n%10);
106         NESSIE_PUTSTR_P(PSTR(" zero bits"));
107         PRINTKEY;
108         
109         memset(block, 0, nessie_mac_ctx.blocksize_B); 
110         nessie_mac_ctx.mac_init(key, nessie_mac_ctx.keysize_b,ctx);;
111         while(n>nessie_mac_ctx.blocksize_B*8){
112                 nessie_mac_ctx.mac_next(block, ctx);
113                 n   -= nessie_mac_ctx.blocksize_B*8;
114         }
115         nessie_mac_ctx.mac_last(block, n, key, nessie_mac_ctx.keysize_b, ctx);
116         nessie_mac_ctx.mac_conv(mac, ctx);
117         PRINTMAC;
118 }
119
120 static
121 void one_in512_mac(uint16_t pos, uint8_t* key){
122         uint8_t ctx[nessie_mac_ctx.ctx_size_B];
123         uint8_t mac[MACSIZE_B];
124         uint8_t block[nessie_mac_ctx.blocksize_B];
125         uint16_t n=512;
126         char* tab[8]={"80", "40", "20", "10", 
127                       "08", "04", "02", "01" };
128
129         pos&=511;
130         NESSIE_PUTSTR_P(PSTR("\r\n                       message="));
131         NESSIE_PUTSTR_P(PSTR("512-bit string: "));
132         if((pos/8) >=10){
133                 NESSIE_PUTC('0'+(pos/8/10)%10);
134         } else {
135                 NESSIE_PUTC(' ');
136         }
137         NESSIE_PUTC('0'+(pos/8)%10);
138         NESSIE_PUTSTR_P(PSTR("*00,"));
139         NESSIE_PUTSTR(tab[pos&7]);
140         NESSIE_PUTC(',');
141         if(63-(pos/8) >=10){
142                 NESSIE_PUTC('0'+((63-pos/8)/10)%10);
143         } else {
144                 NESSIE_PUTC(' ');
145         }
146         NESSIE_PUTC('0'+(63-pos/8)%10);
147         NESSIE_PUTSTR_P(PSTR("*00"));
148         PRINTKEY;
149         
150         /* now the real stuff */
151         memset(block, 0, 512/8);
152         block[pos>>3] = 0x80>>(pos&0x7);
153         uint8_t* bp;
154         bp = block;
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(bp, ctx);
158                 n   -= nessie_mac_ctx.blocksize_B*8;
159                 bp  += nessie_mac_ctx.blocksize_B;
160         }
161         nessie_mac_ctx.mac_last(bp, n, key, nessie_mac_ctx.keysize_b, ctx);
162         nessie_mac_ctx.mac_conv(mac, ctx);
163         PRINTMAC;
164 }
165
166 static
167 void tv4_mac(uint8_t* key){
168         uint8_t ctx[nessie_mac_ctx.ctx_size_B];
169         uint8_t mac[MACSIZE_B];
170         uint8_t block[256/8];
171         uint16_t n=256;
172         uint32_t i;
173         
174         NESSIE_PUTSTR_P(PSTR("\r\n                       message="));
175         NESSIE_PUTSTR(PSTR("256 zero bits"));
176         memset(block, 0, 256/8);
177         
178         nessie_mac_ctx.mac_init(key, nessie_mac_ctx.keysize_b, ctx);;
179         while(n>nessie_mac_ctx.blocksize_B*8){
180                 nessie_mac_ctx.mac_next(block, ctx);
181                 n    -= nessie_mac_ctx.blocksize_B*8;
182         }
183         nessie_mac_ctx.mac_last(block, n, key, nessie_mac_ctx.keysize_b, ctx);
184         nessie_mac_ctx.mac_conv(mac, ctx);
185         PRINTMAC;
186         for(i=1; i<100000L; ++i){ /* this assumes BLOCKSIZE >= HASHSIZE */
187                 nessie_mac_ctx.mac_init(key, nessie_mac_ctx.keysize_b, ctx);;
188                 nessie_mac_ctx.mac_last(mac, nessie_mac_ctx.macsize_b, key, nessie_mac_ctx.keysize_b, ctx);
189                 nessie_mac_ctx.mac_conv(mac, ctx);
190                 NESSIE_SEND_ALIVE_A(i);
191         }
192         nessie_print_item("iterated 100000 times", mac, MACSIZE_B);
193 }
194
195
196 void nessie_mac_run(void){
197         uint16_t i;
198         uint8_t set;
199         uint8_t keyproto[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
200                           0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
201                           0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
202         uint8_t key[KEYSIZE_B];
203         
204         nessie_print_header(nessie_mac_ctx.name, nessie_mac_ctx.keysize_b, 0, 0,
205                             nessie_mac_ctx.macsize_b, 0);
206         /* test set 1 */
207         char* challange[10][2]= {
208                 {"", "\"\" (empty string)"},
209                 {"a", "\"a\""},
210                 {"abc", "\"abc\""},
211                 {"message digest", "\"message digest\""},
212                 {"abcdefghijklmnopqrstuvwxyz","\"abcdefghijklmnopqrstuvwxyz\""},
213                 {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
214                         "\"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\""},
215                 {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
216                  "abcdefghijklmnopqrstuvwxyz"
217                  "0123456789"   , "\"A...Za...z0...9\""},
218                 {"1234567890" "1234567890" "1234567890" "1234567890" 
219                  "1234567890" "1234567890" "1234567890" "1234567890",
220                  "8 times \"1234567890\""},
221                 {"Now is the time for all ", "\"Now is the time for all \""},
222                 {"Now is the time for it", "\"Now is the time for it\""}
223         };
224
225         set=1;
226         nessie_print_setheader(set);
227         for(i=0; i<KEYSIZE_B; ++i){
228                 key[i] = keyproto[i%sizeof(keyproto)];
229         }
230         for(i=0; i<10; ++i){
231                 nessie_print_set_vector(set, i);
232                 ascii_mac(challange[i][0], challange[i][1], key);
233         }
234         nessie_print_set_vector(set, i);
235         amillion_mac(key);
236         for(i=0; i<KEYSIZE_B; ++i){
237                 key[i] = keyproto[16+i%8];
238         }
239         for(i=0; i<10; ++i){
240                 nessie_print_set_vector(set, 11+i);
241                 ascii_mac(challange[i][0], challange[i][1], key);
242         }
243         nessie_print_set_vector(set, 11+i);
244         amillion_mac(key);
245         /* test set 2 */
246         set=2;
247         for(i=0; i<KEYSIZE_B; ++i){
248                 key[i] = keyproto[i%sizeof(keyproto)];
249         }
250         nessie_print_setheader(set);
251         for(i=0; i<1024; ++i){
252                 nessie_print_set_vector(set, i);
253                 zero_mac(i, key);
254         }
255         /* test set 3 */
256         set=3;
257         nessie_print_setheader(set);
258         /* we use the same key as above */
259         for(i=0; i<512; ++i){
260                 nessie_print_set_vector(set, i);
261                 one_in512_mac(i, key);
262         }
263         /* test set 4 */
264         set=4;
265         nessie_print_setheader(set);
266         /* we use the same key as above */
267         nessie_print_set_vector(set, 0);
268         tv4_mac(key);
269         /* test set 5 */
270         for(i=0; i<nessie_mac_ctx.keysize_b; ++i){
271                 nessie_print_set_vector(set, i);
272                 memset(key, 0, KEYSIZE_B);
273                 key[i>>3]=0x80>>(i&0x7);
274                 ascii_mac("ABC", "\"ABC\"", key);
275         }
276         nessie_print_footer();
277 }