1 /* nessie_mac_test.c */
3 This file is part of the Crypto-avr-lib/microcrypt-lib.
4 Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
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.
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.
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/>.
22 * email: daniel.otte@rub.de
25 * a suit for running the nessie-tests for MACs
30 #include "nessie_mac_test.h"
31 #include "nessie_common.h"
34 nessie_mac_ctx_t nessie_mac_ctx;
36 #define KEYSIZE_B ((nessie_mac_ctx.keysize_b+7)/8)
37 #define MACSIZE_B ((nessie_mac_ctx.macsize_b+7)/8)
39 #define PRINTKEY nessie_print_item("key", key, KEYSIZE_B)
40 #define PRINTMAC nessie_print_item("MAC", mac, MACSIZE_B)
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];
48 uart_putstr_P(PSTR("\r\n message="));
51 nessie_mac_ctx.mac_init(key, nessie_mac_ctx.keysize_b, ctx);
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;
58 nessie_mac_ctx.mac_last(data, sl*8, key, nessie_mac_ctx.keysize_b, ctx);
59 nessie_mac_ctx.mac_conv(mac, ctx);
63 // message=1 million times "a"
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];
72 uart_putstr_P(PSTR("\r\n message="));
73 uart_putstr_P(PSTR("1 million times \"a\""));
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;
82 nessie_mac_ctx.mac_last(block, n*8, key, nessie_mac_ctx.keysize_b, ctx);
83 nessie_mac_ctx.mac_conv(mac, ctx);
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];
94 uart_putstr_P(PSTR("\r\n message="));
96 uart_putc('0'+n/10000);
98 uart_putc('0'+(n/1000)%10);
100 uart_putc('0'+(n/100)%10);
102 uart_putc('0'+(n/10)%10);
104 uart_putstr_P(PSTR(" zero bits"));
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;
113 nessie_mac_ctx.mac_last(block, n, key, nessie_mac_ctx.keysize_b, ctx);
114 nessie_mac_ctx.mac_conv(mac, ctx);
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];
124 char* tab[8]={"80", "40", "20", "10",
125 "08", "04", "02", "01" };
128 uart_putstr_P(PSTR("\r\n message="));
129 uart_putstr_P(PSTR("512-bit string: "));
131 uart_putc('0'+(pos/8/10)%10);
135 uart_putc('0'+(pos/8)%10);
136 uart_putstr_P(PSTR("*00,"));
137 uart_putstr(tab[pos&7]);
140 uart_putc('0'+((63-pos/8)/10)%10);
144 uart_putc('0'+(63-pos/8)%10);
145 uart_putstr_P(PSTR("*00"));
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;
156 nessie_mac_ctx.mac_last(block, n, key, nessie_mac_ctx.keysize_b, ctx);
157 nessie_mac_ctx.mac_conv(mac, ctx);
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];
169 uart_putstr_P(PSTR("\r\n message="));
170 uart_putstr(PSTR("256 zero bits"));
171 memset(block, 0, 256/8);
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;
178 nessie_mac_ctx.mac_last(block, n, key, nessie_mac_ctx.keysize_b, ctx);
179 nessie_mac_ctx.mac_conv(mac, ctx);
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);
186 nessie_print_item("iterated 100000 times", mac, MACSIZE_B);
190 void nessie_mac_run(void){
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];
198 nessie_print_header(nessie_mac_ctx.name, nessie_mac_ctx.keysize_b, 0, 0,
199 nessie_mac_ctx.macsize_b, 0);
201 char* challange[10][2]= {
202 {"", "\"\" (empty string)"},
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\""}
219 nessie_print_setheader(set);
220 for(i=0; i<KEYSIZE_B; ++i){
221 key[i] = keyproto[i%sizeof(keyproto)];
224 nessie_print_set_vector(set, i);
225 ascii_mac(challange[i][0], challange[i][1], key);
227 nessie_print_set_vector(set, i);
229 for(i=0; i<KEYSIZE_B; ++i){
230 key[i] = keyproto[16+i%8];
233 nessie_print_set_vector(set, 11+i);
234 ascii_mac(challange[i][0], challange[i][1], key);
236 nessie_print_set_vector(set, 11+i);
240 for(i=0; i<KEYSIZE_B; ++i){
241 key[i] = keyproto[i%sizeof(keyproto)];
243 nessie_print_setheader(set);
244 for(i=0; i<1024; ++i){
245 nessie_print_set_vector(set, i);
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);
258 nessie_print_setheader(set);
259 /* we use the same key as above */
260 nessie_print_set_vector(set, 0);
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);
269 nessie_print_footer();