1 /* nessie_hash_test.c */
3 This file is part of the ARM-Crypto-Lib.
4 Copyright (C) 2006-2010 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;
35 uint8_t nessie_hash_quick=0;
37 #define HASHSIZE_B ((nessie_hash_ctx.hashsize_b+7)/8)
38 #define BLOCKSIZE_B (nessie_hash_ctx.blocksize_B)
41 void ascii_hash(const char* data, const char* desc){
42 uint8_t ctx[nessie_hash_ctx.ctx_size_B];
43 uint8_t hash[HASHSIZE_B];
45 uint8_t buffer[BLOCKSIZE_B];
47 cli_putstr("\r\n message=");
49 nessie_hash_ctx.hash_init(ctx);
51 while(sl>=BLOCKSIZE_B){
52 memcpy(buffer, data, BLOCKSIZE_B);
53 nessie_hash_ctx.hash_next(ctx, buffer);
57 memcpy(buffer, data, sl);
58 nessie_hash_ctx.hash_last(ctx, buffer, sl*8);
59 nessie_hash_ctx.hash_conv(hash, ctx);
60 nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
63 // message=1 million times "a"
66 void amillion_hash(void){
67 uint8_t ctx[nessie_hash_ctx.ctx_size_B];
68 uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8];
69 uint8_t block[nessie_hash_ctx.blocksize_B];
73 cli_putstr("\r\n message=");
74 cli_putstr("1 million times \"a\"");
75 memset(block, 'a', nessie_hash_ctx.blocksize_B);
76 nessie_hash_ctx.hash_init(ctx);
77 while(n>=nessie_hash_ctx.blocksize_B){
78 nessie_hash_ctx.hash_next(ctx, block);
79 n -= nessie_hash_ctx.blocksize_B;
80 NESSIE_SEND_ALIVE_A(i++);
82 nessie_hash_ctx.hash_last(ctx, block, n*8);
83 nessie_hash_ctx.hash_conv(hash, ctx);
84 nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
89 void zero_hash(uint16_t n){
90 uint8_t ctx[nessie_hash_ctx.ctx_size_B];
91 uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8];
92 uint8_t block[nessie_hash_ctx.blocksize_B];
94 cli_putstr("\r\n message=");
96 cli_putc('0'+n/10000);
98 cli_putc('0'+(n/1000)%10);
100 cli_putc('0'+(n/100)%10);
102 cli_putc('0'+(n/10)%10);
104 cli_putstr(" zero bits");
106 memset(block, 0, nessie_hash_ctx.blocksize_B);
107 nessie_hash_ctx.hash_init(ctx);
108 while(n>=nessie_hash_ctx.blocksize_B*8){
109 nessie_hash_ctx.hash_next(ctx, block);
110 n -= nessie_hash_ctx.blocksize_B*8;
112 nessie_hash_ctx.hash_last(ctx, block, n);
113 nessie_hash_ctx.hash_conv(hash, ctx);
114 nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
118 void one_in512_hash(uint16_t pos){
119 uint8_t ctx[nessie_hash_ctx.ctx_size_B];
120 uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8];
121 uint8_t block[nessie_hash_ctx.blocksize_B];
123 char* tab[8]={"80", "40", "20", "10",
124 "08", "04", "02", "01" };
127 cli_putstr("\r\n message=");
128 cli_putstr("512-bit string: ");
130 cli_putc('0'+(pos/8/10)%10);
134 cli_putc('0'+(pos/8)%10);
136 cli_putstr(tab[pos&7]);
139 cli_putc('0'+((63-pos/8)/10)%10);
143 cli_putc('0'+(63-pos/8)%10);
146 /* now the real stuff */
147 memset(block, 0, 512/8);
148 block[pos>>3] = 0x80>>(pos&0x7);
149 nessie_hash_ctx.hash_init(ctx);
150 while(n>=nessie_hash_ctx.blocksize_B*8){
151 nessie_hash_ctx.hash_next(ctx, block);
152 n -= nessie_hash_ctx.blocksize_B*8;
154 nessie_hash_ctx.hash_last(ctx, block, n);
155 nessie_hash_ctx.hash_conv(hash, ctx);
156 nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
161 uint8_t ctx[nessie_hash_ctx.ctx_size_B];
162 uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8];
163 uint8_t block[nessie_hash_ctx.hashsize_b/8];
164 uint16_t n=nessie_hash_ctx.hashsize_b;
167 cli_putstr("\r\n message=");
168 if(nessie_hash_ctx.hashsize_b>=10000)
169 cli_putc('0' + (nessie_hash_ctx.hashsize_b/10000)%10);
170 if(nessie_hash_ctx.hashsize_b>=1000)
171 cli_putc('0' + (nessie_hash_ctx.hashsize_b/1000)%10);
172 if(nessie_hash_ctx.hashsize_b>=100)
173 cli_putc('0' + (nessie_hash_ctx.hashsize_b/100)%10);
174 if(nessie_hash_ctx.hashsize_b>=10)
175 cli_putc('0' + (nessie_hash_ctx.hashsize_b/10)%10);
176 cli_putc('0' + nessie_hash_ctx.hashsize_b%10);
178 cli_putstr(" zero bits");
179 memset(block, 0, 256/8);
181 nessie_hash_ctx.hash_init(ctx);
182 while(n>=nessie_hash_ctx.blocksize_B*8){
183 nessie_hash_ctx.hash_next(ctx, block);
184 n -= nessie_hash_ctx.blocksize_B*8;
186 nessie_hash_ctx.hash_last(ctx, block, n);
187 nessie_hash_ctx.hash_conv(hash, ctx);
188 nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
189 if(nessie_hash_quick)
191 for(i=1; i<100000L; ++i){ /* this assumes BLOCKSIZE >= HASHSIZE */
192 nessie_hash_ctx.hash_init(ctx);
193 nessie_hash_ctx.hash_last(ctx, hash, nessie_hash_ctx.hashsize_b);
194 nessie_hash_ctx.hash_conv(hash, ctx);
195 NESSIE_SEND_ALIVE_A(i);
197 nessie_print_item("iterated 100000 times", hash, (nessie_hash_ctx.hashsize_b+7)/8);
204 message="message digest"
205 message="abcdefghijklmnopqrstuvwxyz"
206 message="abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
207 message="A...Za...z0...9"
208 message=8 times "1234567890"
212 void nessie_hash_run(void){
216 nessie_print_header(nessie_hash_ctx.name, 0, 0, nessie_hash_ctx.hashsize_b, 0, 0);
220 "\"\" (empty string)\0"
226 "\"message digest\"\0"
227 "abcdefghijklmnopqrstuvwxyz\0"
228 "\"abcdefghijklmnopqrstuvwxyz\"\0"
229 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\0"
230 "\"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\"\0"
231 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
232 "abcdefghijklmnopqrstuvwxyz"
234 "\"A...Za...z0...9\"\0"
235 "1234567890123456789012345678901234567890"
236 "1234567890123456789012345678901234567890\0"
237 "8 times \"1234567890\"\0" ;
238 const char* challange[16];
240 nessie_print_setheader(set);
241 dbz_splitup(challange_dbz, challange);
243 nessie_print_set_vector(set, i);
244 ascii_hash(challange[2*i], challange[2*i+1]);
246 nessie_print_set_vector(set, i);
247 if(!nessie_hash_quick)
251 nessie_print_setheader(set);
252 for(i=0; i<1024; ++i){
253 nessie_print_set_vector(set, i);
258 nessie_print_setheader(set);
259 for(i=0; i<512; ++i){
260 nessie_print_set_vector(set, i);
265 nessie_print_setheader(set);
266 nessie_print_set_vector(set, 0);
269 nessie_print_footer();