1 /* nessie_mac_test.c */
3 This file is part of the AVR-Crypto-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
31 #include <avr/pgmspace.h>
32 #include "nessie_mac_test.h"
33 #include "nessie_common.h"
34 #include "dbz_strings.h"
37 nessie_mac_ctx_t nessie_mac_ctx;
39 #define KEYSIZE_B ((nessie_mac_ctx.keysize_b+7)/8)
40 #define MACSIZE_B ((nessie_mac_ctx.macsize_b+7)/8)
41 #define BLOCKSIZE_B (nessie_mac_ctx.blocksize_B)
43 #define PRINTKEY nessie_print_item("key", key, KEYSIZE_B)
44 #define PRINTMAC nessie_print_item("MAC", mac, MACSIZE_B)
47 static uint8_t keyproto[] PROGMEM = {
48 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
49 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
50 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
53 void ascii_mac_P(PGM_P data, PGM_P desc, uint8_t* key){
54 uint8_t ctx[nessie_mac_ctx.ctx_size_B];
55 uint8_t mac[MACSIZE_B];
57 uint8_t buffer[BLOCKSIZE_B];
59 NESSIE_PUTSTR_P(PSTR("\r\n message="));
60 NESSIE_PUTSTR_P(desc);
62 nessie_mac_ctx.mac_init(ctx, key, nessie_mac_ctx.keysize_b);
64 while(sl>nessie_mac_ctx.blocksize_B){
65 memcpy_P(buffer, data, BLOCKSIZE_B);
66 nessie_mac_ctx.mac_next(ctx, buffer);
70 memcpy_P(buffer, data, sl);
71 nessie_mac_ctx.mac_last(ctx, buffer, sl*8);
72 nessie_mac_ctx.mac_conv(mac, ctx);
76 // message=1 million times "a"
79 void amillion_mac(uint8_t* key){
80 uint8_t ctx[nessie_mac_ctx.ctx_size_B];
81 uint8_t mac[MACSIZE_B];
82 uint8_t block[nessie_mac_ctx.blocksize_B];
86 NESSIE_PUTSTR_P(PSTR("\r\n message="));
87 NESSIE_PUTSTR_P(PSTR("1 million times \"a\""));
90 memset(block, 'a', nessie_mac_ctx.blocksize_B);
91 nessie_mac_ctx.mac_init(ctx, key, nessie_mac_ctx.keysize_b);
92 while(n>=nessie_mac_ctx.blocksize_B){
93 nessie_mac_ctx.mac_next(ctx, block);
94 n -= nessie_mac_ctx.blocksize_B;
95 NESSIE_SEND_ALIVE_A(i++);
97 nessie_mac_ctx.mac_last(ctx, block, n*8);
98 nessie_mac_ctx.mac_conv(mac, ctx);
104 void zero_mac(uint16_t n, uint8_t* key){
105 uint8_t ctx[nessie_mac_ctx.ctx_size_B];
106 uint8_t mac[MACSIZE_B];
107 uint8_t block[nessie_mac_ctx.blocksize_B];
109 NESSIE_PUTSTR_P(PSTR("\r\n message="));
111 NESSIE_PUTC('0'+n/10000);
113 NESSIE_PUTC('0'+(n/1000)%10);
115 NESSIE_PUTC('0'+(n/100)%10);
117 NESSIE_PUTC('0'+(n/10)%10);
118 NESSIE_PUTC('0'+n%10);
119 NESSIE_PUTSTR_P(PSTR(" zero bits"));
122 memset(block, 0, nessie_mac_ctx.blocksize_B);
123 nessie_mac_ctx.mac_init(ctx, key, nessie_mac_ctx.keysize_b);
124 while(n>nessie_mac_ctx.blocksize_B*8){
125 nessie_mac_ctx.mac_next(ctx, block);
126 n -= nessie_mac_ctx.blocksize_B*8;
128 nessie_mac_ctx.mac_last(ctx, block, n);
129 nessie_mac_ctx.mac_conv(mac, ctx);
134 void one_in512_mac(uint16_t pos, uint8_t* key){
135 uint8_t ctx[nessie_mac_ctx.ctx_size_B];
136 uint8_t mac[MACSIZE_B];
137 uint8_t block[nessie_mac_ctx.blocksize_B];
139 char* tab[8]={"80", "40", "20", "10",
140 "08", "04", "02", "01" };
143 NESSIE_PUTSTR_P(PSTR("\r\n message="));
144 NESSIE_PUTSTR_P(PSTR("512-bit string: "));
146 NESSIE_PUTC('0'+(pos/8/10)%10);
150 NESSIE_PUTC('0'+(pos/8)%10);
151 NESSIE_PUTSTR_P(PSTR("*00,"));
152 NESSIE_PUTSTR(tab[pos&7]);
155 NESSIE_PUTC('0'+((63-pos/8)/10)%10);
159 NESSIE_PUTC('0'+(63-pos/8)%10);
160 NESSIE_PUTSTR_P(PSTR("*00"));
163 /* now the real stuff */
164 memset(block, 0, 512/8);
165 block[pos>>3] = 0x80>>(pos&0x7);
168 nessie_mac_ctx.mac_init(ctx, key, nessie_mac_ctx.keysize_b);
169 while(n>nessie_mac_ctx.blocksize_B*8){
170 nessie_mac_ctx.mac_next(ctx, bp);
171 n -= nessie_mac_ctx.blocksize_B*8;
172 bp += nessie_mac_ctx.blocksize_B;
174 nessie_mac_ctx.mac_last(ctx, bp, n);
175 nessie_mac_ctx.mac_conv(mac, ctx);
181 uint8_t ctx[nessie_mac_ctx.ctx_size_B];
182 uint8_t mac[MACSIZE_B];
183 uint8_t block[MACSIZE_B];
184 uint8_t key[KEYSIZE_B];
185 uint16_t n=MACSIZE_B*8;
189 NESSIE_PUTSTR_P(PSTR("\r\n message="));
190 utoa(MACSIZE_B*8, str, 10);
192 NESSIE_PUTSTR_P(PSTR(" zero bits"));
193 memset(block, 0, MACSIZE_B);
194 for(i=0; i<KEYSIZE_B; ++i)
195 key[i] = pgm_read_byte(&(keyproto[i%(3*8)]));
196 nessie_print_item("key", key, KEYSIZE_B);
197 nessie_mac_ctx.mac_init(ctx, key, nessie_mac_ctx.keysize_b);
198 while(n>nessie_mac_ctx.blocksize_B*8){
199 nessie_mac_ctx.mac_next(ctx, block);
200 n -= nessie_mac_ctx.blocksize_B*8;
202 nessie_mac_ctx.mac_last(ctx, block, n);
203 nessie_mac_ctx.mac_conv(mac, ctx);
205 for(i=1; i<100000L; ++i){ /* this assumes BLOCKSIZE >= HASHSIZE */
206 nessie_mac_ctx.mac_init(ctx, key, nessie_mac_ctx.keysize_b);
207 nessie_mac_ctx.mac_last(ctx, mac, nessie_mac_ctx.macsize_b);
208 nessie_mac_ctx.mac_conv(mac, ctx);
209 NESSIE_SEND_ALIVE_A(i);
211 nessie_print_item("iterated 100000 times", mac, MACSIZE_B);
214 void nessie_mac_run(void){
217 uint8_t key[KEYSIZE_B];
219 nessie_print_header(nessie_mac_ctx.name, nessie_mac_ctx.keysize_b, 0, 0,
220 nessie_mac_ctx.macsize_b, 0);
222 char* challange_dbz= PSTR(
224 "\"\" (empty string)\0"
230 "\"message digest\"\0"
231 "abcdefghijklmnopqrstuvwxyz\0"
232 "\"abcdefghijklmnopqrstuvwxyz\"\0"
233 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\0"
234 "\"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\"\0"
235 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
236 "abcdefghijklmnopqrstuvwxyz"
238 "\"A...Za...z0...9\"\0"
239 "1234567890123456789012345678901234567890"
240 "1234567890123456789012345678901234567890\0"
241 "8 times \"1234567890\"\0"
242 "Now is the time for all \0"
243 "\"Now is the time for all \"\0"
244 "Now is the time for it\0"
245 "\"Now is the time for it\"\0"
249 nessie_print_setheader(set);
250 for(i=0; i<KEYSIZE_B; ++i){
251 key[i] = pgm_read_byte(&(keyproto[i%sizeof(keyproto)]));
254 dbz_splitup_P(challange_dbz, challange);
256 nessie_print_set_vector(set, i);
257 ascii_mac_P(challange[2*i], challange[2*i+1], key);
259 nessie_print_set_vector(set, i);
261 for(i=0; i<KEYSIZE_B; ++i){
262 key[i] = pgm_read_byte(&(keyproto[0x10+i%0x8]));
265 nessie_print_set_vector(set, 11+i);
266 ascii_mac_P(challange[2*i], challange[2*i+1], key);
268 nessie_print_set_vector(set, 11+i);
272 for(i=0; i<KEYSIZE_B; ++i){
273 key[i] = pgm_read_byte(&(keyproto[i%sizeof(keyproto)]));
275 nessie_print_setheader(set);
276 for(i=0; i<1024; ++i){
277 nessie_print_set_vector(set, i);
282 nessie_print_setheader(set);
283 /* we use the same key as above */
284 for(i=0; i<512; ++i){
285 nessie_print_set_vector(set, i);
286 one_in512_mac(i, key);
290 nessie_print_setheader(set);
291 /* we use the same key as above */
292 nessie_print_set_vector(set, 0);
296 nessie_print_setheader(set);
297 for(i=0; i<nessie_mac_ctx.keysize_b; ++i){
298 nessie_print_set_vector(set, i);
299 memset(key, 0, KEYSIZE_B);
300 key[i>>3]=0x80>>(i&0x7);
301 ascii_mac_P(PSTR("ABC"), PSTR("\"ABC\""), key);
303 nessie_print_footer();