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[nessie_hash_ctx.hashsize_b/8];
155 uint16_t n=nessie_hash_ctx.hashsize_b;
158 uart_putstr_P(PSTR("\r\n message="));
159 if(nessie_hash_ctx.hashsize_b>=10000)
160 uart_putc('0' + (nessie_hash_ctx.hashsize_b/10000)%10);
161 if(nessie_hash_ctx.hashsize_b>=1000)
162 uart_putc('0' + (nessie_hash_ctx.hashsize_b/1000)%10);
163 if(nessie_hash_ctx.hashsize_b>=100)
164 uart_putc('0' + (nessie_hash_ctx.hashsize_b/100)%10);
165 if(nessie_hash_ctx.hashsize_b>=10)
166 uart_putc('0' + (nessie_hash_ctx.hashsize_b/10)%10);
167 uart_putc('0' + nessie_hash_ctx.hashsize_b%10);
169 uart_putstr_P(PSTR(" zero bits"));
170 memset(block, 0, 256/8);
172 nessie_hash_ctx.hash_init(ctx);
173 while(n>=nessie_hash_ctx.blocksize_B*8){
174 nessie_hash_ctx.hash_next(block, ctx);
175 n -= nessie_hash_ctx.blocksize_B*8;
177 nessie_hash_ctx.hash_last(block, n, ctx);
178 nessie_hash_ctx.hash_conv(hash, ctx);
179 nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
180 for(i=1; i<100000L; ++i){ /* this assumes BLOCKSIZE >= HASHSIZE */
181 nessie_hash_ctx.hash_init(ctx);
182 nessie_hash_ctx.hash_last(hash, nessie_hash_ctx.hashsize_b, ctx);
183 nessie_hash_ctx.hash_conv(hash, ctx);
185 nessie_print_item("iterated 100000 times", hash, (nessie_hash_ctx.hashsize_b+7)/8);
192 message="message digest"
193 message="abcdefghijklmnopqrstuvwxyz"
194 message="abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
195 message="A...Za...z0...9"
196 message=8 times "1234567890"
200 void nessie_hash_run(void){
204 nessie_print_header(nessie_hash_ctx.name, 0, 0, nessie_hash_ctx.hashsize_b, 0, 0);
206 char* challange[8][2]= {
207 {"", "\"\" (empty string)"},
210 {"message digest", "\"message digest\""},
211 {"abcdefghijklmnopqrstuvwxyz","\"abcdefghijklmnopqrstuvwxyz\""},
212 {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
213 "\"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\""},
214 {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
215 "abcdefghijklmnopqrstuvwxyz"
216 "0123456789" , "\"A...Za...z0...9\""},
217 {"1234567890" "1234567890" "1234567890" "1234567890"
218 "1234567890" "1234567890" "1234567890" "1234567890",
219 "8 times \"1234567890\""}
222 nessie_print_setheader(set);
224 nessie_print_set_vector(set, i);
225 ascii_hash(challange[i][0], challange[i][1]);
227 nessie_print_set_vector(set, i);
231 nessie_print_setheader(set);
232 for(i=0; i<1024; ++i){
233 nessie_print_set_vector(set, i);
238 nessie_print_setheader(set);
239 for(i=0; i<512; ++i){
240 nessie_print_set_vector(set, i);
245 nessie_print_setheader(set);
246 nessie_print_set_vector(set, 0);
249 nessie_print_footer();