1 /* nessie_hash_test.c */
3 This file is part of the Crypto-avr-lib/microcrypt-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 uart_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];
65 uart_putstr_P(PSTR("\r\n message="));
66 uart_putstr_P(PSTR("1 million times \"a\""));
67 memset(block, 'a', nessie_hash_ctx.blocksize_B);
68 nessie_hash_ctx.hash_init(ctx);
69 while(n>=nessie_hash_ctx.blocksize_B){
70 nessie_hash_ctx.hash_next(block, ctx);
71 n -= nessie_hash_ctx.blocksize_B;
73 nessie_hash_ctx.hash_last(block, n*8, ctx);
74 nessie_hash_ctx.hash_conv(hash, ctx);
75 nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
80 void zero_hash(uint16_t n){
81 uint8_t ctx[nessie_hash_ctx.ctx_size_B];
82 uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8];
83 uint8_t block[nessie_hash_ctx.blocksize_B];
85 uart_putstr_P(PSTR("\r\n message="));
87 uart_putc('0'+n/10000);
89 uart_putc('0'+(n/1000)%10);
91 uart_putc('0'+(n/100)%10);
93 uart_putc('0'+(n/10)%10);
95 uart_putstr_P(PSTR(" zero bits"));
97 memset(block, 0, nessie_hash_ctx.blocksize_B);
98 nessie_hash_ctx.hash_init(ctx);
99 while(n>=nessie_hash_ctx.blocksize_B*8){
100 nessie_hash_ctx.hash_next(block, ctx);
101 n -= nessie_hash_ctx.blocksize_B*8;
103 nessie_hash_ctx.hash_last(block, n, ctx);
104 nessie_hash_ctx.hash_conv(hash, ctx);
105 nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
109 void one_in512_hash(uint16_t pos){
110 uint8_t ctx[nessie_hash_ctx.ctx_size_B];
111 uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8];
112 uint8_t block[nessie_hash_ctx.blocksize_B];
114 char* tab[8]={"80", "40", "20", "10",
115 "08", "04", "02", "01" };
118 uart_putstr_P(PSTR("\r\n message="));
119 uart_putstr_P(PSTR("512-bit string: "));
121 uart_putc('0'+(pos/8/10)%10);
125 uart_putc('0'+(pos/8)%10);
126 uart_putstr_P(PSTR("*00,"));
127 uart_putstr(tab[pos&7]);
130 uart_putc('0'+((63-pos/8)/10)%10);
134 uart_putc('0'+(63-pos/8)%10);
135 uart_putstr_P(PSTR("*00"));
137 /* now the real stuff */
138 memset(block, 0, 512/8);
139 block[pos>>3] = 0x80>>(pos&0x7);
140 nessie_hash_ctx.hash_init(ctx);
141 while(n>=nessie_hash_ctx.blocksize_B*8){
142 nessie_hash_ctx.hash_next(block, ctx);
143 n -= nessie_hash_ctx.blocksize_B*8;
145 nessie_hash_ctx.hash_last(block, n, ctx);
146 nessie_hash_ctx.hash_conv(hash, ctx);
147 nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
152 uint8_t ctx[nessie_hash_ctx.ctx_size_B];
153 uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8];
154 uint8_t block[256/8];
158 uart_putstr_P(PSTR("\r\n message="));
159 uart_putstr(PSTR("256 zero bits"));
160 memset(block, 0, 256/8);
162 nessie_hash_ctx.hash_init(ctx);
163 while(n>=nessie_hash_ctx.blocksize_B*8){
164 nessie_hash_ctx.hash_next(block, ctx);
165 n -= nessie_hash_ctx.blocksize_B*8;
167 nessie_hash_ctx.hash_last(block, n, ctx);
168 nessie_hash_ctx.hash_conv(hash, ctx);
169 nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
170 for(i=1; i<100000L; ++i){ /* this assumes BLOCKSIZE >= HASHSIZE */
171 nessie_hash_ctx.hash_init(ctx);
172 nessie_hash_ctx.hash_last(hash, nessie_hash_ctx.hashsize_b, ctx);
173 nessie_hash_ctx.hash_conv(hash, ctx);
175 nessie_print_item("iterated 100000 times", hash, (nessie_hash_ctx.hashsize_b+7)/8);
182 message="message digest"
183 message="abcdefghijklmnopqrstuvwxyz"
184 message="abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
185 message="A...Za...z0...9"
186 message=8 times "1234567890"
190 void nessie_hash_run(void){
194 nessie_print_header(nessie_hash_ctx.name, 0, 0, nessie_hash_ctx.hashsize_b, 0, 0);
196 char* challange[8][2]= {
197 {"", "\"\" (empty string)"},
200 {"message digest", "\"message digest\""},
201 {"abcdefghijklmnopqrstuvwxyz","\"abcdefghijklmnopqrstuvwxyz\""},
202 {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
203 "\"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\""},
204 {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
205 "abcdefghijklmnopqrstuvwxyz"
206 "0123456789" , "\"A...Za...z0...9\""},
207 {"1234567890" "1234567890" "1234567890" "1234567890"
208 "1234567890" "1234567890" "1234567890" "1234567890",
209 "8 times \"1234567890\""}
212 nessie_print_setheader(set);
214 nessie_print_set_vector(set, i);
215 ascii_hash(challange[i][0], challange[i][1]);
217 nessie_print_set_vector(set, i);
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();