]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/nessie_hash_test.c
new hash function abstraction layer + shavs + dump util + ...
[avr-crypto-lib.git] / test_src / nessie_hash_test.c
1 /* nessie_hash_test.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2008  Daniel Otte (daniel.otte@rub.de)
5
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.
10
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.
15
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/>.
18 */
19 /**
20  * 
21  * author: Daniel Otte
22  * email:  daniel.otte@rub.de
23  * license: GPLv3
24  * 
25  * a suit for running the nessie-tests for hashes
26  * 
27  * */
28 #include <stdint.h>
29 #include <string.h>
30 #include "nessie_hash_test.h"
31 #include "nessie_common.h"
32 #include "uart.h"
33
34 nessie_hash_ctx_t nessie_hash_ctx;
35
36 static
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];
40         uint16_t sl;
41         
42         NESSIE_PUTSTR_P(PSTR("\r\n                       message="));
43         NESSIE_PUTSTR(desc);
44         nessie_hash_ctx.hash_init(ctx);
45         sl = strlen(data);
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;
50         }
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);
54 }
55
56 // message=1 million times "a"
57
58 static
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];
63         uint32_t n=1000000LL;
64         uint16_t i=0;
65         
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++);
74         }
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);
78 }
79
80
81 static
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];
86         
87         NESSIE_PUTSTR_P(PSTR("\r\n                       message="));
88         if(n>=10000)
89                 NESSIE_PUTC('0'+n/10000);
90         if(n>=1000)
91                 NESSIE_PUTC('0'+(n/1000)%10);
92         if(n>=100)
93                 NESSIE_PUTC('0'+(n/100)%10);
94         if(n>=10)
95                 NESSIE_PUTC('0'+(n/10)%10);
96         NESSIE_PUTC('0'+n%10);
97         NESSIE_PUTSTR_P(PSTR(" zero bits"));
98         
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;
104         }
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);
108 }
109
110 static
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];
115         uint16_t n=512;
116         char* tab[8]={"80", "40", "20", "10", 
117                       "08", "04", "02", "01" };
118
119         pos&=511;
120         NESSIE_PUTSTR_P(PSTR("\r\n                       message="));
121         NESSIE_PUTSTR_P(PSTR("512-bit string: "));
122         if((pos/8) >=10){
123                 NESSIE_PUTC('0'+(pos/8/10)%10);
124         } else {
125                 NESSIE_PUTC(' ');
126         }
127         NESSIE_PUTC('0'+(pos/8)%10);
128         NESSIE_PUTSTR_P(PSTR("*00,"));
129         NESSIE_PUTSTR(tab[pos&7]);
130         NESSIE_PUTC(',');
131         if(63-(pos/8) >=10){
132                 NESSIE_PUTC('0'+((63-pos/8)/10)%10);
133         } else {
134                 NESSIE_PUTC(' ');
135         }
136         NESSIE_PUTC('0'+(63-pos/8)%10);
137         NESSIE_PUTSTR_P(PSTR("*00"));
138         
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;
146         }
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);
150 }
151
152 static
153 void tv4_hash(void){
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;
158         uint32_t i;
159         
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);
170
171         NESSIE_PUTSTR_P(PSTR(" zero bits"));
172         memset(block, 0, 256/8);
173         
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;
178         }
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);
187         }
188         nessie_print_item("iterated 100000 times", hash, (nessie_hash_ctx.hashsize_b+7)/8);
189 }
190
191 /*
192    "" (empty string)
193    message="a"
194    message="abc"
195    message="message digest"
196    message="abcdefghijklmnopqrstuvwxyz"
197    message="abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
198    message="A...Za...z0...9"
199    message=8 times "1234567890"
200 */
201
202
203 void nessie_hash_run(void){
204         uint16_t i;
205         uint8_t set;
206         
207         nessie_print_header(nessie_hash_ctx.name, 0, 0, nessie_hash_ctx.hashsize_b, 0, 0);
208         /* test set 1 */
209         char* challange[8][2]= {
210                 {"", "\"\" (empty string)"},
211                 {"a", "\"a\""},
212                 {"abc", "\"abc\""},
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\""}      
223         };
224         set=1;
225         nessie_print_setheader(set);
226         for(i=0; i<8; ++i){
227                 nessie_print_set_vector(set, i);
228                 ascii_hash(challange[i][0], challange[i][1]);
229         }
230         nessie_print_set_vector(set, i);
231         amillion_hash();
232         /* test set 2 */
233         set=2;
234         nessie_print_setheader(set);
235         for(i=0; i<1024; ++i){
236                 nessie_print_set_vector(set, i);
237                 zero_hash(i);
238         }
239         /* test set 3 */
240         set=3;
241         nessie_print_setheader(set);
242         for(i=0; i<512; ++i){
243                 nessie_print_set_vector(set, i);
244                 one_in512_hash(i);
245         }
246         /* test set 4 */
247         set=4;
248         nessie_print_setheader(set);
249         nessie_print_set_vector(set, 0);
250         tv4_hash();
251
252         nessie_print_footer();
253 }