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"
34 nessie_hash_ctx_t nessie_hash_ctx;
37 void ascii_hash(char* data, char* desc){
38 uint8_t ctx[nessie_hash_ctx.ctx_size_B];
39 uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8];
42 NESSIE_PUTSTR_P(PSTR("\r\n message="));
44 nessie_hash_ctx.hash_init(ctx);
46 while(sl>=nessie_hash_ctx.blocksize_B){
47 nessie_hash_ctx.hash_next(data, ctx);
48 data += nessie_hash_ctx.blocksize_B;
49 sl -= nessie_hash_ctx.blocksize_B;
51 nessie_hash_ctx.hash_last(data, sl*8, ctx);
52 nessie_hash_ctx.hash_conv(hash, ctx);
53 nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
56 // message=1 million times "a"
59 void amillion_hash(void){
60 uint8_t ctx[nessie_hash_ctx.ctx_size_B];
61 uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8];
62 uint8_t block[nessie_hash_ctx.blocksize_B];
66 NESSIE_PUTSTR_P(PSTR("\r\n message="));
67 NESSIE_PUTSTR_P(PSTR("1 million times \"a\""));
68 memset(block, 'a', nessie_hash_ctx.blocksize_B);
69 nessie_hash_ctx.hash_init(ctx);
70 while(n>=nessie_hash_ctx.blocksize_B){
71 nessie_hash_ctx.hash_next(block, ctx);
72 n -= nessie_hash_ctx.blocksize_B;
73 NESSIE_SEND_ALIVE_A(i++);
75 nessie_hash_ctx.hash_last(block, n*8, ctx);
76 nessie_hash_ctx.hash_conv(hash, ctx);
77 nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
82 void zero_hash(uint16_t n){
83 uint8_t ctx[nessie_hash_ctx.ctx_size_B];
84 uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8];
85 uint8_t block[nessie_hash_ctx.blocksize_B];
87 NESSIE_PUTSTR_P(PSTR("\r\n message="));
89 NESSIE_PUTC('0'+n/10000);
91 NESSIE_PUTC('0'+(n/1000)%10);
93 NESSIE_PUTC('0'+(n/100)%10);
95 NESSIE_PUTC('0'+(n/10)%10);
96 NESSIE_PUTC('0'+n%10);
97 NESSIE_PUTSTR_P(PSTR(" zero bits"));
99 memset(block, 0, nessie_hash_ctx.blocksize_B);
100 nessie_hash_ctx.hash_init(ctx);
101 while(n>=nessie_hash_ctx.blocksize_B*8){
102 nessie_hash_ctx.hash_next(block, ctx);
103 n -= nessie_hash_ctx.blocksize_B*8;
105 nessie_hash_ctx.hash_last(block, n, ctx);
106 nessie_hash_ctx.hash_conv(hash, ctx);
107 nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
111 void one_in512_hash(uint16_t pos){
112 uint8_t ctx[nessie_hash_ctx.ctx_size_B];
113 uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8];
114 uint8_t block[nessie_hash_ctx.blocksize_B];
116 char* tab[8]={"80", "40", "20", "10",
117 "08", "04", "02", "01" };
120 NESSIE_PUTSTR_P(PSTR("\r\n message="));
121 NESSIE_PUTSTR_P(PSTR("512-bit string: "));
123 NESSIE_PUTC('0'+(pos/8/10)%10);
127 NESSIE_PUTC('0'+(pos/8)%10);
128 NESSIE_PUTSTR_P(PSTR("*00,"));
129 NESSIE_PUTSTR(tab[pos&7]);
132 NESSIE_PUTC('0'+((63-pos/8)/10)%10);
136 NESSIE_PUTC('0'+(63-pos/8)%10);
137 NESSIE_PUTSTR_P(PSTR("*00"));
139 /* now the real stuff */
140 memset(block, 0, 512/8);
141 block[pos>>3] = 0x80>>(pos&0x7);
142 nessie_hash_ctx.hash_init(ctx);
143 while(n>=nessie_hash_ctx.blocksize_B*8){
144 nessie_hash_ctx.hash_next(block, ctx);
145 n -= nessie_hash_ctx.blocksize_B*8;
147 nessie_hash_ctx.hash_last(block, n, ctx);
148 nessie_hash_ctx.hash_conv(hash, ctx);
149 nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
154 uint8_t ctx[nessie_hash_ctx.ctx_size_B];
155 uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8];
156 uint8_t block[nessie_hash_ctx.hashsize_b/8];
157 uint16_t n=nessie_hash_ctx.hashsize_b;
160 NESSIE_PUTSTR_P(PSTR("\r\n message="));
161 if(nessie_hash_ctx.hashsize_b>=10000)
162 NESSIE_PUTC('0' + (nessie_hash_ctx.hashsize_b/10000)%10);
163 if(nessie_hash_ctx.hashsize_b>=1000)
164 NESSIE_PUTC('0' + (nessie_hash_ctx.hashsize_b/1000)%10);
165 if(nessie_hash_ctx.hashsize_b>=100)
166 NESSIE_PUTC('0' + (nessie_hash_ctx.hashsize_b/100)%10);
167 if(nessie_hash_ctx.hashsize_b>=10)
168 NESSIE_PUTC('0' + (nessie_hash_ctx.hashsize_b/10)%10);
169 NESSIE_PUTC('0' + nessie_hash_ctx.hashsize_b%10);
171 NESSIE_PUTSTR_P(PSTR(" zero bits"));
172 memset(block, 0, 256/8);
174 nessie_hash_ctx.hash_init(ctx);
175 while(n>=nessie_hash_ctx.blocksize_B*8){
176 nessie_hash_ctx.hash_next(block, ctx);
177 n -= nessie_hash_ctx.blocksize_B*8;
179 nessie_hash_ctx.hash_last(block, n, ctx);
180 nessie_hash_ctx.hash_conv(hash, ctx);
181 nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
182 for(i=1; i<100000L; ++i){ /* this assumes BLOCKSIZE >= HASHSIZE */
183 nessie_hash_ctx.hash_init(ctx);
184 nessie_hash_ctx.hash_last(hash, nessie_hash_ctx.hashsize_b, ctx);
185 nessie_hash_ctx.hash_conv(hash, ctx);
186 NESSIE_SEND_ALIVE_A(i);
188 nessie_print_item("iterated 100000 times", hash, (nessie_hash_ctx.hashsize_b+7)/8);
195 message="message digest"
196 message="abcdefghijklmnopqrstuvwxyz"
197 message="abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
198 message="A...Za...z0...9"
199 message=8 times "1234567890"
203 void nessie_hash_run(void){
207 nessie_print_header(nessie_hash_ctx.name, 0, 0, nessie_hash_ctx.hashsize_b, 0, 0);
209 char* challange[8][2]= {
210 {"", "\"\" (empty string)"},
213 {"message digest", "\"message digest\""},
214 {"abcdefghijklmnopqrstuvwxyz","\"abcdefghijklmnopqrstuvwxyz\""},
215 {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
216 "\"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\""},
217 {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
218 "abcdefghijklmnopqrstuvwxyz"
219 "0123456789" , "\"A...Za...z0...9\""},
220 {"1234567890" "1234567890" "1234567890" "1234567890"
221 "1234567890" "1234567890" "1234567890" "1234567890",
222 "8 times \"1234567890\""}
225 nessie_print_setheader(set);
227 nessie_print_set_vector(set, i);
228 ascii_hash(challange[i][0], challange[i][1]);
230 nessie_print_set_vector(set, i);
234 nessie_print_setheader(set);
235 for(i=0; i<1024; ++i){
236 nessie_print_set_vector(set, i);
241 nessie_print_setheader(set);
242 for(i=0; i<512; ++i){
243 nessie_print_set_vector(set, i);
248 nessie_print_setheader(set);
249 nessie_print_set_vector(set, 0);
252 nessie_print_footer();