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;
36 #define HASHSIZE_B ((nessie_hash_ctx.hashsize_b+7)/8)
37 #define BLOCKSIZE_B (nessie_hash_ctx.blocksize_B)
40 void ascii_hash_P(PGM_P data, PGM_P desc){
41 uint8_t ctx[nessie_hash_ctx.ctx_size_B];
42 uint8_t hash[HASHSIZE_B];
44 uint8_t buffer[BLOCKSIZE_B];
46 NESSIE_PUTSTR_P(PSTR("\r\n message="));
47 NESSIE_PUTSTR_P(desc);
48 nessie_hash_ctx.hash_init(ctx);
50 while(sl>=BLOCKSIZE_B){
51 memcpy_P(buffer, data, BLOCKSIZE_B);
52 nessie_hash_ctx.hash_next(buffer, ctx);
56 memcpy_P(buffer, data, sl);
57 nessie_hash_ctx.hash_last(buffer, sl*8, ctx);
58 nessie_hash_ctx.hash_conv(hash, ctx);
59 nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
62 // message=1 million times "a"
65 void amillion_hash(void){
66 uint8_t ctx[nessie_hash_ctx.ctx_size_B];
67 uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8];
68 uint8_t block[nessie_hash_ctx.blocksize_B];
72 NESSIE_PUTSTR_P(PSTR("\r\n message="));
73 NESSIE_PUTSTR_P(PSTR("1 million times \"a\""));
74 memset(block, 'a', nessie_hash_ctx.blocksize_B);
75 nessie_hash_ctx.hash_init(ctx);
76 while(n>=nessie_hash_ctx.blocksize_B){
77 nessie_hash_ctx.hash_next(block, ctx);
78 n -= nessie_hash_ctx.blocksize_B;
79 NESSIE_SEND_ALIVE_A(i++);
81 nessie_hash_ctx.hash_last(block, n*8, ctx);
82 nessie_hash_ctx.hash_conv(hash, ctx);
83 nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
88 void zero_hash(uint16_t n){
89 uint8_t ctx[nessie_hash_ctx.ctx_size_B];
90 uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8];
91 uint8_t block[nessie_hash_ctx.blocksize_B];
93 NESSIE_PUTSTR_P(PSTR("\r\n message="));
95 NESSIE_PUTC('0'+n/10000);
97 NESSIE_PUTC('0'+(n/1000)%10);
99 NESSIE_PUTC('0'+(n/100)%10);
101 NESSIE_PUTC('0'+(n/10)%10);
102 NESSIE_PUTC('0'+n%10);
103 NESSIE_PUTSTR_P(PSTR(" zero bits"));
105 memset(block, 0, nessie_hash_ctx.blocksize_B);
106 nessie_hash_ctx.hash_init(ctx);
107 while(n>=nessie_hash_ctx.blocksize_B*8){
108 nessie_hash_ctx.hash_next(block, ctx);
109 n -= nessie_hash_ctx.blocksize_B*8;
111 nessie_hash_ctx.hash_last(block, n, ctx);
112 nessie_hash_ctx.hash_conv(hash, ctx);
113 nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
117 void one_in512_hash(uint16_t pos){
118 uint8_t ctx[nessie_hash_ctx.ctx_size_B];
119 uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8];
120 uint8_t block[nessie_hash_ctx.blocksize_B];
122 char* tab[8]={"80", "40", "20", "10",
123 "08", "04", "02", "01" };
126 NESSIE_PUTSTR_P(PSTR("\r\n message="));
127 NESSIE_PUTSTR_P(PSTR("512-bit string: "));
129 NESSIE_PUTC('0'+(pos/8/10)%10);
133 NESSIE_PUTC('0'+(pos/8)%10);
134 NESSIE_PUTSTR_P(PSTR("*00,"));
135 NESSIE_PUTSTR(tab[pos&7]);
138 NESSIE_PUTC('0'+((63-pos/8)/10)%10);
142 NESSIE_PUTC('0'+(63-pos/8)%10);
143 NESSIE_PUTSTR_P(PSTR("*00"));
145 /* now the real stuff */
146 memset(block, 0, 512/8);
147 block[pos>>3] = 0x80>>(pos&0x7);
148 nessie_hash_ctx.hash_init(ctx);
149 while(n>=nessie_hash_ctx.blocksize_B*8){
150 nessie_hash_ctx.hash_next(block, ctx);
151 n -= nessie_hash_ctx.blocksize_B*8;
153 nessie_hash_ctx.hash_last(block, n, ctx);
154 nessie_hash_ctx.hash_conv(hash, ctx);
155 nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
160 uint8_t ctx[nessie_hash_ctx.ctx_size_B];
161 uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8];
162 uint8_t block[nessie_hash_ctx.hashsize_b/8];
163 uint16_t n=nessie_hash_ctx.hashsize_b;
166 NESSIE_PUTSTR_P(PSTR("\r\n message="));
167 if(nessie_hash_ctx.hashsize_b>=10000)
168 NESSIE_PUTC('0' + (nessie_hash_ctx.hashsize_b/10000)%10);
169 if(nessie_hash_ctx.hashsize_b>=1000)
170 NESSIE_PUTC('0' + (nessie_hash_ctx.hashsize_b/1000)%10);
171 if(nessie_hash_ctx.hashsize_b>=100)
172 NESSIE_PUTC('0' + (nessie_hash_ctx.hashsize_b/100)%10);
173 if(nessie_hash_ctx.hashsize_b>=10)
174 NESSIE_PUTC('0' + (nessie_hash_ctx.hashsize_b/10)%10);
175 NESSIE_PUTC('0' + nessie_hash_ctx.hashsize_b%10);
177 NESSIE_PUTSTR_P(PSTR(" zero bits"));
178 memset(block, 0, 256/8);
180 nessie_hash_ctx.hash_init(ctx);
181 while(n>=nessie_hash_ctx.blocksize_B*8){
182 nessie_hash_ctx.hash_next(block, ctx);
183 n -= nessie_hash_ctx.blocksize_B*8;
185 nessie_hash_ctx.hash_last(block, n, ctx);
186 nessie_hash_ctx.hash_conv(hash, ctx);
187 nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
188 for(i=1; i<100000L; ++i){ /* this assumes BLOCKSIZE >= HASHSIZE */
189 nessie_hash_ctx.hash_init(ctx);
190 nessie_hash_ctx.hash_last(hash, nessie_hash_ctx.hashsize_b, ctx);
191 nessie_hash_ctx.hash_conv(hash, ctx);
192 NESSIE_SEND_ALIVE_A(i);
194 nessie_print_item("iterated 100000 times", hash, (nessie_hash_ctx.hashsize_b+7)/8);
201 message="message digest"
202 message="abcdefghijklmnopqrstuvwxyz"
203 message="abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
204 message="A...Za...z0...9"
205 message=8 times "1234567890"
209 void nessie_hash_run(void){
213 nessie_print_header(nessie_hash_ctx.name, 0, 0, nessie_hash_ctx.hashsize_b, 0, 0);
215 char* challange_dbz= PSTR(
217 "\"\" (empty string)\0"
223 "\"message digest\"\0"
224 "abcdefghijklmnopqrstuvwxyz\0"
225 "\"abcdefghijklmnopqrstuvwxyz\"\0"
226 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\0"
227 "\"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\"\0"
228 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
229 "abcdefghijklmnopqrstuvwxyz"
231 "\"A...Za...z0...9\"\0"
232 "1234567890123456789012345678901234567890"
233 "1234567890123456789012345678901234567890\0"
234 "8 times \"1234567890\"\0"
238 nessie_print_setheader(set);
239 dbz_splitup_P(challange_dbz, challange);
241 nessie_print_set_vector(set, i);
242 ascii_hash_P(challange[2*i], challange[2*i+1]);
244 nessie_print_set_vector(set, i);
248 nessie_print_setheader(set);
249 for(i=0; i<1024; ++i){
250 nessie_print_set_vector(set, i);
255 nessie_print_setheader(set);
256 for(i=0; i<512; ++i){
257 nessie_print_set_vector(set, i);
262 nessie_print_setheader(set);
263 nessie_print_set_vector(set, 0);
266 nessie_print_footer();