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