1 /* nessie_hash_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 hashes
30 #include "nessie_hash_test.h"
31 #include "nessie_common.h"
32 #include "dbz_strings.h"
34 nessie_hash_ctx_t nessie_hash_ctx;
35 uint8_t nessie_hash_quick=0;
37 #define HASHSIZE_B ((nessie_hash_ctx.hashsize_b+7)/8)
38 #define BLOCKSIZE_B (nessie_hash_ctx.blocksize_B)
41 void ascii_hash_P(PGM_P data, PGM_P desc){
42 uint8_t ctx[nessie_hash_ctx.ctx_size_B];
43 uint8_t hash[HASHSIZE_B];
45 uint8_t buffer[BLOCKSIZE_B];
47 NESSIE_PUTSTR_P(PSTR("\r\n message="));
48 NESSIE_PUTSTR_P(desc);
49 nessie_hash_ctx.hash_init(ctx);
51 while(sl>=BLOCKSIZE_B){
52 memcpy_P(buffer, data, BLOCKSIZE_B);
53 nessie_hash_ctx.hash_next(ctx, buffer);
57 memcpy_P(buffer, data, sl);
58 nessie_hash_ctx.hash_last(ctx, buffer, sl*8);
59 nessie_hash_ctx.hash_conv(hash, ctx);
60 nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
63 // message=1 million times "a"
66 void amillion_hash(void){
67 uint8_t ctx[nessie_hash_ctx.ctx_size_B];
68 uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8];
69 uint8_t block[nessie_hash_ctx.blocksize_B];
73 NESSIE_PUTSTR_P(PSTR("\r\n message="));
74 NESSIE_PUTSTR_P(PSTR("1 million times \"a\""));
75 memset(block, 'a', nessie_hash_ctx.blocksize_B);
76 nessie_hash_ctx.hash_init(ctx);
77 while(n>=nessie_hash_ctx.blocksize_B){
78 nessie_hash_ctx.hash_next(ctx, block);
79 n -= nessie_hash_ctx.blocksize_B;
80 NESSIE_SEND_ALIVE_A(i++);
82 nessie_hash_ctx.hash_last(ctx, block, n*8);
83 nessie_hash_ctx.hash_conv(hash, ctx);
84 nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
89 void zero_hash(uint16_t n){
90 uint8_t ctx[nessie_hash_ctx.ctx_size_B];
91 uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8];
92 uint8_t block[nessie_hash_ctx.blocksize_B];
94 NESSIE_PUTSTR_P(PSTR("\r\n message="));
96 NESSIE_PUTC('0'+n/10000);
98 NESSIE_PUTC('0'+(n/1000)%10);
100 NESSIE_PUTC('0'+(n/100)%10);
102 NESSIE_PUTC('0'+(n/10)%10);
103 NESSIE_PUTC('0'+n%10);
104 NESSIE_PUTSTR_P(PSTR(" zero bits"));
106 memset(block, 0, nessie_hash_ctx.blocksize_B);
107 nessie_hash_ctx.hash_init(ctx);
108 while(n>=nessie_hash_ctx.blocksize_B*8){
109 nessie_hash_ctx.hash_next(ctx, block);
110 n -= nessie_hash_ctx.blocksize_B*8;
112 nessie_hash_ctx.hash_last(ctx, block, n);
113 nessie_hash_ctx.hash_conv(hash, ctx);
114 nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
118 void one_in512_hash(uint16_t pos){
119 uint8_t ctx[nessie_hash_ctx.ctx_size_B];
120 uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8];
121 uint8_t block[nessie_hash_ctx.blocksize_B];
123 char* tab[8]={"80", "40", "20", "10",
124 "08", "04", "02", "01" };
127 NESSIE_PUTSTR_P(PSTR("\r\n message="));
128 NESSIE_PUTSTR_P(PSTR("512-bit string: "));
130 NESSIE_PUTC('0'+(pos/8/10)%10);
134 NESSIE_PUTC('0'+(pos/8)%10);
135 NESSIE_PUTSTR_P(PSTR("*00,"));
136 NESSIE_PUTSTR(tab[pos&7]);
139 NESSIE_PUTC('0'+((63-pos/8)/10)%10);
143 NESSIE_PUTC('0'+(63-pos/8)%10);
144 NESSIE_PUTSTR_P(PSTR("*00"));
146 /* now the real stuff */
147 memset(block, 0, 512/8);
148 block[pos>>3] = 0x80>>(pos&0x7);
149 nessie_hash_ctx.hash_init(ctx);
150 while(n>=nessie_hash_ctx.blocksize_B*8){
151 nessie_hash_ctx.hash_next(ctx, block);
152 n -= nessie_hash_ctx.blocksize_B*8;
154 nessie_hash_ctx.hash_last(ctx, block, n);
155 nessie_hash_ctx.hash_conv(hash, ctx);
156 nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
161 uint8_t ctx[nessie_hash_ctx.ctx_size_B];
162 uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8];
163 uint8_t block[nessie_hash_ctx.hashsize_b/8];
164 uint16_t n=nessie_hash_ctx.hashsize_b;
167 NESSIE_PUTSTR_P(PSTR("\r\n message="));
168 if(nessie_hash_ctx.hashsize_b>=10000)
169 NESSIE_PUTC('0' + (nessie_hash_ctx.hashsize_b/10000)%10);
170 if(nessie_hash_ctx.hashsize_b>=1000)
171 NESSIE_PUTC('0' + (nessie_hash_ctx.hashsize_b/1000)%10);
172 if(nessie_hash_ctx.hashsize_b>=100)
173 NESSIE_PUTC('0' + (nessie_hash_ctx.hashsize_b/100)%10);
174 if(nessie_hash_ctx.hashsize_b>=10)
175 NESSIE_PUTC('0' + (nessie_hash_ctx.hashsize_b/10)%10);
176 NESSIE_PUTC('0' + nessie_hash_ctx.hashsize_b%10);
178 NESSIE_PUTSTR_P(PSTR(" zero bits"));
179 memset(block, 0, 256/8);
181 nessie_hash_ctx.hash_init(ctx);
182 while(n>=nessie_hash_ctx.blocksize_B*8){
183 nessie_hash_ctx.hash_next(ctx, block);
184 n -= nessie_hash_ctx.blocksize_B*8;
186 nessie_hash_ctx.hash_last(ctx, block, n);
187 nessie_hash_ctx.hash_conv(hash, ctx);
188 nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
189 if(nessie_hash_quick)
191 for(i=1; i<100000L; ++i){ /* this assumes BLOCKSIZE >= HASHSIZE */
192 nessie_hash_ctx.hash_init(ctx);
193 nessie_hash_ctx.hash_last(ctx, hash, nessie_hash_ctx.hashsize_b);
194 nessie_hash_ctx.hash_conv(hash, ctx);
195 NESSIE_SEND_ALIVE_A(i);
197 nessie_print_item("iterated 100000 times", hash, (nessie_hash_ctx.hashsize_b+7)/8);
204 message="message digest"
205 message="abcdefghijklmnopqrstuvwxyz"
206 message="abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
207 message="A...Za...z0...9"
208 message=8 times "1234567890"
212 void nessie_hash_run(void){
216 nessie_print_header(nessie_hash_ctx.name, 0, 0, nessie_hash_ctx.hashsize_b, 0, 0);
218 char* challange_dbz= PSTR(
220 "\"\" (empty string)\0"
226 "\"message digest\"\0"
227 "abcdefghijklmnopqrstuvwxyz\0"
228 "\"abcdefghijklmnopqrstuvwxyz\"\0"
229 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\0"
230 "\"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\"\0"
231 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
232 "abcdefghijklmnopqrstuvwxyz"
234 "\"A...Za...z0...9\"\0"
235 "1234567890123456789012345678901234567890"
236 "1234567890123456789012345678901234567890\0"
237 "8 times \"1234567890\"\0"
241 nessie_print_setheader(set);
242 dbz_splitup_P(challange_dbz, challange);
244 nessie_print_set_vector(set, i);
245 ascii_hash_P(challange[2*i], challange[2*i+1]);
247 nessie_print_set_vector(set, i);
248 if(!nessie_hash_quick)
252 nessie_print_setheader(set);
253 for(i=0; i<1024; ++i){
254 nessie_print_set_vector(set, i);
259 nessie_print_setheader(set);
260 for(i=0; i<512; ++i){
261 nessie_print_set_vector(set, i);
266 nessie_print_setheader(set);
267 nessie_print_set_vector(set, 0);
270 nessie_print_footer();