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
31 #include "nessie_hash_test.h"
32 #include "nessie_common.h"
33 #include "dbz_strings.h"
35 nessie_hash_ctx_t nessie_hash_ctx;
36 uint8_t nessie_hash_quick=0;
38 #define HASHSIZE_B ((nessie_hash_ctx.hashsize_b+7)/8)
39 #define BLOCKSIZE_B (nessie_hash_ctx.blocksize_B)
42 void ascii_hash_P(PGM_P data, PGM_P desc){
43 uint8_t ctx[nessie_hash_ctx.ctx_size_B];
44 uint8_t hash[HASHSIZE_B];
46 uint8_t buffer[BLOCKSIZE_B];
48 fputs_P(PSTR("\n message="), stdout);
49 fputs_P(desc, stdout);
50 nessie_hash_ctx.hash_init(ctx);
52 while(sl>=BLOCKSIZE_B){
53 memcpy_P(buffer, data, BLOCKSIZE_B);
54 nessie_hash_ctx.hash_next(ctx, buffer);
58 memcpy_P(buffer, data, sl);
59 nessie_hash_ctx.hash_last(ctx, buffer, sl*8);
60 nessie_hash_ctx.hash_conv(hash, ctx);
61 nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
64 // message=1 million times "a"
67 void amillion_hash(void){
68 uint8_t ctx[nessie_hash_ctx.ctx_size_B];
69 uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8];
70 uint8_t block[nessie_hash_ctx.blocksize_B];
74 fputs_P(PSTR("\n message="), stdout);
75 fputs_P(PSTR("1 million times \"a\""), stdout);
76 memset(block, 'a', nessie_hash_ctx.blocksize_B);
77 nessie_hash_ctx.hash_init(ctx);
78 while(n>=nessie_hash_ctx.blocksize_B){
79 nessie_hash_ctx.hash_next(ctx, block);
80 n -= nessie_hash_ctx.blocksize_B;
81 NESSIE_SEND_ALIVE_A(i++);
83 nessie_hash_ctx.hash_last(ctx, block, n*8);
84 nessie_hash_ctx.hash_conv(hash, ctx);
85 nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
90 void zero_hash(uint16_t n){
91 uint8_t ctx[nessie_hash_ctx.ctx_size_B];
92 uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8];
93 uint8_t block[nessie_hash_ctx.blocksize_B];
95 fputs_P(PSTR("\n message="), stdout);
96 fprintf_P(stdout, PSTR("%"PRIu16" zero bits"));
98 memset(block, 0, nessie_hash_ctx.blocksize_B);
99 nessie_hash_ctx.hash_init(ctx);
100 while(n>=nessie_hash_ctx.blocksize_B*8){
101 nessie_hash_ctx.hash_next(ctx, block);
102 n -= nessie_hash_ctx.blocksize_B*8;
104 nessie_hash_ctx.hash_last(ctx, block, n);
105 nessie_hash_ctx.hash_conv(hash, ctx);
106 nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
110 void one_in512_hash(uint16_t pos){
111 uint8_t ctx[nessie_hash_ctx.ctx_size_B];
112 uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8];
113 uint8_t block[nessie_hash_ctx.blocksize_B];
115 char* tab[8] = { "80", "40", "20", "10",
116 "08", "04", "02", "01" };
119 fputs_P(PSTR("\n message="), stdout);
120 fputs_P(PSTR("512-bit string: "), stdout);
122 fprintf_P(stdout, PSTR("%2"PRIu16"*00,%s,%2"PRIu16"*00"), pos / 8, tab[pos & 7], 63 - pos / 8);
124 /* now the real stuff */
125 memset(block, 0, 512/8);
126 block[pos>>3] = 0x80>>(pos&0x7);
127 nessie_hash_ctx.hash_init(ctx);
128 while(n>=nessie_hash_ctx.blocksize_B*8){
129 nessie_hash_ctx.hash_next(ctx, block);
130 n -= nessie_hash_ctx.blocksize_B*8;
132 nessie_hash_ctx.hash_last(ctx, block, n);
133 nessie_hash_ctx.hash_conv(hash, ctx);
134 nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
139 uint8_t ctx[nessie_hash_ctx.ctx_size_B];
140 uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8];
141 uint8_t block[nessie_hash_ctx.hashsize_b/8];
142 uint16_t n=nessie_hash_ctx.hashsize_b;
145 fputs_P(PSTR("\r\n message="), stdout);
146 fprintf_P(stdout, PSTR("%"PRIu16" zero bits"), nessie_hash_ctx.hashsize_b);
148 memset(block, 0, nessie_hash_ctx.hashsize_b/8);
150 nessie_hash_ctx.hash_init(ctx);
151 while(n>=nessie_hash_ctx.blocksize_B*8){
152 nessie_hash_ctx.hash_next(ctx, block);
153 n -= nessie_hash_ctx.blocksize_B*8;
155 nessie_hash_ctx.hash_last(ctx, block, n);
156 nessie_hash_ctx.hash_conv(hash, ctx);
157 nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
158 if(nessie_hash_quick)
160 for(i=1; i<100000L; ++i){ /* this assumes BLOCKSIZE >= HASHSIZE */
161 nessie_hash_ctx.hash_init(ctx);
162 nessie_hash_ctx.hash_last(ctx, hash, nessie_hash_ctx.hashsize_b);
163 nessie_hash_ctx.hash_conv(hash, ctx);
164 NESSIE_SEND_ALIVE_A(i);
166 nessie_print_item("iterated 100000 times", hash, (nessie_hash_ctx.hashsize_b+7)/8);
173 message="message digest"
174 message="abcdefghijklmnopqrstuvwxyz"
175 message="abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
176 message="A...Za...z0...9"
177 message=8 times "1234567890"
181 void nessie_hash_run(void){
185 nessie_print_header(nessie_hash_ctx.name, 0, 0, nessie_hash_ctx.hashsize_b, 0, 0);
187 const char* challange_dbz= PSTR(
189 "\"\" (empty string)\0"
195 "\"message digest\"\0"
196 "abcdefghijklmnopqrstuvwxyz\0"
197 "\"abcdefghijklmnopqrstuvwxyz\"\0"
198 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\0"
199 "\"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\"\0"
200 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
201 "abcdefghijklmnopqrstuvwxyz"
203 "\"A...Za...z0...9\"\0"
204 "1234567890123456789012345678901234567890"
205 "1234567890123456789012345678901234567890\0"
206 "8 times \"1234567890\"\0"
210 nessie_print_setheader(set);
211 dbz_splitup_P(challange_dbz, challange);
213 nessie_print_set_vector(set, i);
214 ascii_hash_P(challange[2*i], challange[2*i+1]);
216 nessie_print_set_vector(set, i);
217 if(!nessie_hash_quick)
221 nessie_print_setheader(set);
222 for(i=0; i<1024; ++i){
223 nessie_print_set_vector(set, i);
228 nessie_print_setheader(set);
229 for(i=0; i<512; ++i){
230 nessie_print_set_vector(set, i);
235 nessie_print_setheader(set);
236 nessie_print_set_vector(set, 0);
239 nessie_print_footer();