]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/nessie_hash_test.c
a lot of fixes
[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 "dbz_strings.h"
33
34 nessie_hash_ctx_t nessie_hash_ctx;
35 uint8_t           nessie_hash_quick=0;
36
37 #define HASHSIZE_B ((nessie_hash_ctx.hashsize_b+7)/8)
38 #define BLOCKSIZE_B (nessie_hash_ctx.blocksize_B)
39
40 static
41 void ascii_hash_P(PGM_P data, PGM_P desc){
42         uint8_t ctx[nessie_hash_ctx.ctx_size_B];
43         uint8_t hash[HASHSIZE_B];
44         uint16_t sl;
45         uint8_t buffer[BLOCKSIZE_B];
46         
47         NESSIE_PUTSTR_P(PSTR("\r\n                       message="));
48         NESSIE_PUTSTR_P(desc);
49         nessie_hash_ctx.hash_init(ctx);
50         sl = strlen_P(data);
51         while(sl>=BLOCKSIZE_B){
52                 memcpy_P(buffer, data, BLOCKSIZE_B);
53                 nessie_hash_ctx.hash_next(ctx, buffer);
54                 data += BLOCKSIZE_B;
55                 sl   -= BLOCKSIZE_B;
56         }
57         memcpy_P(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);
61 }
62
63 // message=1 million times "a"
64
65 static
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];
70         uint32_t n=1000000LL;
71         uint16_t i=0;
72         
73         NESSIE_PUTSTR_P(PSTR("\r\n                       message="));
74         NESSIE_PUTSTR_P(PSTR("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++);
81         }
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);
85 }
86
87
88 static
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];
93         
94         NESSIE_PUTSTR_P(PSTR("\r\n                       message="));
95         if(n>=10000)
96                 NESSIE_PUTC('0'+n/10000);
97         if(n>=1000)
98                 NESSIE_PUTC('0'+(n/1000)%10);
99         if(n>=100)
100                 NESSIE_PUTC('0'+(n/100)%10);
101         if(n>=10)
102                 NESSIE_PUTC('0'+(n/10)%10);
103         NESSIE_PUTC('0'+n%10);
104         NESSIE_PUTSTR_P(PSTR(" zero bits"));
105         
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;
111         }
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);
115 }
116
117 static
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];
122         uint16_t n=512;
123         char* tab[8]={"80", "40", "20", "10", 
124                       "08", "04", "02", "01" };
125
126         pos&=511;
127         NESSIE_PUTSTR_P(PSTR("\r\n                       message="));
128         NESSIE_PUTSTR_P(PSTR("512-bit string: "));
129         if((pos/8) >=10){
130                 NESSIE_PUTC('0'+(pos/8/10)%10);
131         } else {
132                 NESSIE_PUTC(' ');
133         }
134         NESSIE_PUTC('0'+(pos/8)%10);
135         NESSIE_PUTSTR_P(PSTR("*00,"));
136         NESSIE_PUTSTR(tab[pos&7]);
137         NESSIE_PUTC(',');
138         if(63-(pos/8) >=10){
139                 NESSIE_PUTC('0'+((63-pos/8)/10)%10);
140         } else {
141                 NESSIE_PUTC(' ');
142         }
143         NESSIE_PUTC('0'+(63-pos/8)%10);
144         NESSIE_PUTSTR_P(PSTR("*00"));
145         
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;
153         }
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);
157 }
158
159 static
160 void tv4_hash(void){
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;
165         uint32_t i;
166         
167         NESSIE_PUTSTR_P(PSTR("\r\n                       message="));
168         if(nessie_hash_ctx.hashsize_b>=10000)
169                 NESSIE_PUTC('0' + (nessie_hash_ctx.hashsize_b/10000)%10);
170         if(nessie_hash_ctx.hashsize_b>=1000)
171                 NESSIE_PUTC('0' + (nessie_hash_ctx.hashsize_b/1000)%10);
172         if(nessie_hash_ctx.hashsize_b>=100)
173                 NESSIE_PUTC('0' + (nessie_hash_ctx.hashsize_b/100)%10);
174         if(nessie_hash_ctx.hashsize_b>=10)
175                 NESSIE_PUTC('0' + (nessie_hash_ctx.hashsize_b/10)%10);
176         NESSIE_PUTC('0' + nessie_hash_ctx.hashsize_b%10);
177
178         NESSIE_PUTSTR_P(PSTR(" zero bits"));
179         memset(block, 0, nessie_hash_ctx.hashsize_b/8);
180         
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;
185         }
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)
190                 return;
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);
196         }
197         nessie_print_item("iterated 100000 times", hash, (nessie_hash_ctx.hashsize_b+7)/8);
198 }
199
200 /*
201    "" (empty string)
202    message="a"
203    message="abc"
204    message="message digest"
205    message="abcdefghijklmnopqrstuvwxyz"
206    message="abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
207    message="A...Za...z0...9"
208    message=8 times "1234567890"
209 */
210
211
212 void nessie_hash_run(void){
213         uint16_t i;
214         uint8_t set;
215         
216         nessie_print_header(nessie_hash_ctx.name, 0, 0, nessie_hash_ctx.hashsize_b, 0, 0);
217         /* test set 1 */
218         const char* challange_dbz= PSTR(
219                   "\0"
220                 "\"\" (empty string)\0"
221                   "a\0"
222                 "\"a\"\0"
223                   "abc\0"
224                 "\"abc\"\0"
225                   "message digest\0"
226                 "\"message digest\"\0"
227                   "abcdefghijklmnopqrstuvwxyz\0"
228                 "\"abcdefghijklmnopqrstuvwxyz\"\0"
229                   "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\0"
230                 "\"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\"\0"
231                   "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
232                   "abcdefghijklmnopqrstuvwxyz"
233                   "0123456789\0"         
234                  "\"A...Za...z0...9\"\0"
235                  "1234567890123456789012345678901234567890" 
236                  "1234567890123456789012345678901234567890\0"
237                  "8 times \"1234567890\"\0"
238         );
239         PGM_P challange[16];
240         set=1;
241         nessie_print_setheader(set);
242         dbz_splitup_P(challange_dbz, challange);
243         for(i=0; i<8; ++i){
244                 nessie_print_set_vector(set, i);
245                 ascii_hash_P(challange[2*i], challange[2*i+1]);
246         }
247         nessie_print_set_vector(set, i);
248         if(!nessie_hash_quick)
249                 amillion_hash();
250         /* test set 2 */
251         set=2;
252         nessie_print_setheader(set);
253         for(i=0; i<1024; ++i){
254                 nessie_print_set_vector(set, i);
255                 zero_hash(i);
256         }
257         /* test set 3 */
258         set=3;
259         nessie_print_setheader(set);
260         for(i=0; i<512; ++i){
261                 nessie_print_set_vector(set, i);
262                 one_in512_hash(i);
263         }
264         /* test set 4 */
265         set=4;
266         nessie_print_setheader(set);
267         nessie_print_set_vector(set, 0);
268         tv4_hash();
269
270         nessie_print_footer();
271 }