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);
153 nessie_mac_ctx.mac_init(key, nessie_mac_ctx.keysize_b, ctx);
154 while(n>nessie_mac_ctx.blocksize_B*8){
155 nessie_mac_ctx.mac_next(bp, ctx);
156 n -= nessie_mac_ctx.blocksize_B*8;
157 bp += nessie_mac_ctx.blocksize_B;
159 nessie_mac_ctx.mac_last(bp, n, key, nessie_mac_ctx.keysize_b, ctx);
160 nessie_mac_ctx.mac_conv(mac, ctx);
165 void tv4_mac(uint8_t* key){
166 uint8_t ctx[nessie_mac_ctx.ctx_size_B];
167 uint8_t mac[MACSIZE_B];
168 uint8_t block[256/8];
172 uart_putstr_P(PSTR("\r\n message="));
173 uart_putstr(PSTR("256 zero bits"));
174 memset(block, 0, 256/8);
176 nessie_mac_ctx.mac_init(key, nessie_mac_ctx.keysize_b, ctx);;
177 while(n>nessie_mac_ctx.blocksize_B*8){
178 nessie_mac_ctx.mac_next(block, ctx);
179 n -= nessie_mac_ctx.blocksize_B*8;
181 nessie_mac_ctx.mac_last(block, n, key, nessie_mac_ctx.keysize_b, ctx);
182 nessie_mac_ctx.mac_conv(mac, ctx);
184 for(i=1; i<100000L; ++i){ /* this assumes BLOCKSIZE >= HASHSIZE */
185 nessie_mac_ctx.mac_init(key, nessie_mac_ctx.keysize_b, ctx);;
186 nessie_mac_ctx.mac_last(mac, nessie_mac_ctx.macsize_b, key, nessie_mac_ctx.keysize_b, ctx);
187 nessie_mac_ctx.mac_conv(mac, ctx);
189 nessie_print_item("iterated 100000 times", mac, MACSIZE_B);
193 void nessie_mac_run(void){
196 uint8_t keyproto[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
197 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
198 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
199 uint8_t key[KEYSIZE_B];
201 nessie_print_header(nessie_mac_ctx.name, nessie_mac_ctx.keysize_b, 0, 0,
202 nessie_mac_ctx.macsize_b, 0);
204 char* challange[10][2]= {
205 {"", "\"\" (empty string)"},
208 {"message digest", "\"message digest\""},
209 {"abcdefghijklmnopqrstuvwxyz","\"abcdefghijklmnopqrstuvwxyz\""},
210 {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
211 "\"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\""},
212 {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
213 "abcdefghijklmnopqrstuvwxyz"
214 "0123456789" , "\"A...Za...z0...9\""},
215 {"1234567890" "1234567890" "1234567890" "1234567890"
216 "1234567890" "1234567890" "1234567890" "1234567890",
217 "8 times \"1234567890\""},
218 {"Now is the time for all ", "\"Now is the time for all \""},
219 {"Now is the time for it", "\"Now is the time for it\""}
223 nessie_print_setheader(set);
224 for(i=0; i<KEYSIZE_B; ++i){
225 key[i] = keyproto[i%sizeof(keyproto)];
228 nessie_print_set_vector(set, i);
229 ascii_mac(challange[i][0], challange[i][1], key);
231 nessie_print_set_vector(set, i);
233 for(i=0; i<KEYSIZE_B; ++i){
234 key[i] = keyproto[16+i%8];
237 nessie_print_set_vector(set, 11+i);
238 ascii_mac(challange[i][0], challange[i][1], key);
240 nessie_print_set_vector(set, 11+i);
244 for(i=0; i<KEYSIZE_B; ++i){
245 key[i] = keyproto[i%sizeof(keyproto)];
247 nessie_print_setheader(set);
248 for(i=0; i<1024; ++i){
249 nessie_print_set_vector(set, i);
254 nessie_print_setheader(set);
255 /* we use the same key as above */
256 for(i=0; i<512; ++i){
257 nessie_print_set_vector(set, i);
258 one_in512_mac(i, key);
262 nessie_print_setheader(set);
263 /* we use the same key as above */
264 nessie_print_set_vector(set, 0);
267 for(i=0; i<nessie_mac_ctx.keysize_b; ++i){
268 nessie_print_set_vector(set, i);
269 memset(key, 0, KEYSIZE_B);
270 key[i>>3]=0x80>>(i&0x7);
271 ascii_mac("ABC", "\"ABC\"", key);
273 nessie_print_footer();