]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/nessie_hash_test.c
ef5d968ed871185b250f61d01d273221bc741526
[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
36 #define HASHSIZE_B ((nessie_hash_ctx.hashsize_b+7)/8)
37 #define BLOCKSIZE_B (nessie_hash_ctx.blocksize_B)
38
39 static
40 void ascii_hash_P(PGM_P data, PGM_P desc){
41         uint8_t ctx[nessie_hash_ctx.ctx_size_B];
42         uint8_t hash[HASHSIZE_B];
43         uint16_t sl;
44         uint8_t buffer[BLOCKSIZE_B];
45         
46         NESSIE_PUTSTR_P(PSTR("\r\n                       message="));
47         NESSIE_PUTSTR_P(desc);
48         nessie_hash_ctx.hash_init(ctx);
49         sl = strlen_P(data);
50         while(sl>=BLOCKSIZE_B){
51                 memcpy_P(buffer, data, BLOCKSIZE_B);
52                 nessie_hash_ctx.hash_next(buffer, ctx);
53                 data += BLOCKSIZE_B;
54                 sl   -= BLOCKSIZE_B;
55         }
56         memcpy_P(buffer, data, sl);
57         nessie_hash_ctx.hash_last(buffer, sl*8, ctx);
58         nessie_hash_ctx.hash_conv(hash, ctx);
59         nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
60 }
61
62 // message=1 million times "a"
63
64 static
65 void amillion_hash(void){
66         uint8_t ctx[nessie_hash_ctx.ctx_size_B];
67         uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8];
68         uint8_t block[nessie_hash_ctx.blocksize_B];
69         uint32_t n=1000000LL;
70         uint16_t i=0;
71         
72         NESSIE_PUTSTR_P(PSTR("\r\n                       message="));
73         NESSIE_PUTSTR_P(PSTR("1 million times \"a\""));
74         memset(block, 'a', nessie_hash_ctx.blocksize_B);
75         nessie_hash_ctx.hash_init(ctx);
76         while(n>=nessie_hash_ctx.blocksize_B){
77                 nessie_hash_ctx.hash_next(block, ctx);
78                 n    -= nessie_hash_ctx.blocksize_B;
79                 NESSIE_SEND_ALIVE_A(i++);
80         }
81         nessie_hash_ctx.hash_last(block, n*8, ctx);
82         nessie_hash_ctx.hash_conv(hash, ctx);
83         nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
84 }
85
86
87 static
88 void zero_hash(uint16_t n){
89         uint8_t ctx[nessie_hash_ctx.ctx_size_B];
90         uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8];
91         uint8_t block[nessie_hash_ctx.blocksize_B];
92         
93         NESSIE_PUTSTR_P(PSTR("\r\n                       message="));
94         if(n>=10000)
95                 NESSIE_PUTC('0'+n/10000);
96         if(n>=1000)
97                 NESSIE_PUTC('0'+(n/1000)%10);
98         if(n>=100)
99                 NESSIE_PUTC('0'+(n/100)%10);
100         if(n>=10)
101                 NESSIE_PUTC('0'+(n/10)%10);
102         NESSIE_PUTC('0'+n%10);
103         NESSIE_PUTSTR_P(PSTR(" zero bits"));
104         
105         memset(block, 0, nessie_hash_ctx.blocksize_B); 
106         nessie_hash_ctx.hash_init(ctx);
107         while(n>=nessie_hash_ctx.blocksize_B*8){
108                 nessie_hash_ctx.hash_next(block, ctx);
109                 n   -= nessie_hash_ctx.blocksize_B*8;
110         }
111         nessie_hash_ctx.hash_last(block, n, ctx);
112         nessie_hash_ctx.hash_conv(hash, ctx);
113         nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
114 }
115
116 static
117 void one_in512_hash(uint16_t pos){
118         uint8_t ctx[nessie_hash_ctx.ctx_size_B];
119         uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8];
120         uint8_t block[nessie_hash_ctx.blocksize_B];
121         uint16_t n=512;
122         char* tab[8]={"80", "40", "20", "10", 
123                       "08", "04", "02", "01" };
124
125         pos&=511;
126         NESSIE_PUTSTR_P(PSTR("\r\n                       message="));
127         NESSIE_PUTSTR_P(PSTR("512-bit string: "));
128         if((pos/8) >=10){
129                 NESSIE_PUTC('0'+(pos/8/10)%10);
130         } else {
131                 NESSIE_PUTC(' ');
132         }
133         NESSIE_PUTC('0'+(pos/8)%10);
134         NESSIE_PUTSTR_P(PSTR("*00,"));
135         NESSIE_PUTSTR(tab[pos&7]);
136         NESSIE_PUTC(',');
137         if(63-(pos/8) >=10){
138                 NESSIE_PUTC('0'+((63-pos/8)/10)%10);
139         } else {
140                 NESSIE_PUTC(' ');
141         }
142         NESSIE_PUTC('0'+(63-pos/8)%10);
143         NESSIE_PUTSTR_P(PSTR("*00"));
144         
145         /* now the real stuff */
146         memset(block, 0, 512/8);
147         block[pos>>3] = 0x80>>(pos&0x7);
148         nessie_hash_ctx.hash_init(ctx);
149         while(n>=nessie_hash_ctx.blocksize_B*8){
150                 nessie_hash_ctx.hash_next(block, ctx);
151                 n   -= nessie_hash_ctx.blocksize_B*8;
152         }
153         nessie_hash_ctx.hash_last(block, n, ctx);
154         nessie_hash_ctx.hash_conv(hash, ctx);
155         nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
156 }
157
158 static
159 void tv4_hash(void){
160         uint8_t ctx[nessie_hash_ctx.ctx_size_B];
161         uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8];
162         uint8_t block[nessie_hash_ctx.hashsize_b/8];
163         uint16_t n=nessie_hash_ctx.hashsize_b;
164         uint32_t i;
165         
166         NESSIE_PUTSTR_P(PSTR("\r\n                       message="));
167         if(nessie_hash_ctx.hashsize_b>=10000)
168                 NESSIE_PUTC('0' + (nessie_hash_ctx.hashsize_b/10000)%10);
169         if(nessie_hash_ctx.hashsize_b>=1000)
170                 NESSIE_PUTC('0' + (nessie_hash_ctx.hashsize_b/1000)%10);
171         if(nessie_hash_ctx.hashsize_b>=100)
172                 NESSIE_PUTC('0' + (nessie_hash_ctx.hashsize_b/100)%10);
173         if(nessie_hash_ctx.hashsize_b>=10)
174                 NESSIE_PUTC('0' + (nessie_hash_ctx.hashsize_b/10)%10);
175         NESSIE_PUTC('0' + nessie_hash_ctx.hashsize_b%10);
176
177         NESSIE_PUTSTR_P(PSTR(" zero bits"));
178         memset(block, 0, 256/8);
179         
180         nessie_hash_ctx.hash_init(ctx);
181         while(n>=nessie_hash_ctx.blocksize_B*8){
182                 nessie_hash_ctx.hash_next(block, ctx);
183                 n    -= nessie_hash_ctx.blocksize_B*8;
184         }
185         nessie_hash_ctx.hash_last(block, n, ctx);
186         nessie_hash_ctx.hash_conv(hash, ctx);
187         nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
188         for(i=1; i<100000L; ++i){ /* this assumes BLOCKSIZE >= HASHSIZE */
189                 nessie_hash_ctx.hash_init(ctx);
190                 nessie_hash_ctx.hash_last(hash, nessie_hash_ctx.hashsize_b, ctx);
191                 nessie_hash_ctx.hash_conv(hash, ctx);
192                 NESSIE_SEND_ALIVE_A(i);
193         }
194         nessie_print_item("iterated 100000 times", hash, (nessie_hash_ctx.hashsize_b+7)/8);
195 }
196
197 /*
198    "" (empty string)
199    message="a"
200    message="abc"
201    message="message digest"
202    message="abcdefghijklmnopqrstuvwxyz"
203    message="abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
204    message="A...Za...z0...9"
205    message=8 times "1234567890"
206 */
207
208
209 void nessie_hash_run(void){
210         uint16_t i;
211         uint8_t set;
212         
213         nessie_print_header(nessie_hash_ctx.name, 0, 0, nessie_hash_ctx.hashsize_b, 0, 0);
214         /* test set 1 */
215         char* challange_dbz= PSTR(
216                   "\0"
217                 "\"\" (empty string)\0"
218                   "a\0"
219                 "\"a\"\0"
220                   "abc\0"
221                 "\"abc\"\0"
222                   "message digest\0"
223                 "\"message digest\"\0"
224                   "abcdefghijklmnopqrstuvwxyz\0"
225                 "\"abcdefghijklmnopqrstuvwxyz\"\0"
226                   "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\0"
227                 "\"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\"\0"
228                   "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
229                   "abcdefghijklmnopqrstuvwxyz"
230                   "0123456789\0"         
231                  "\"A...Za...z0...9\"\0"
232                  "1234567890123456789012345678901234567890" 
233                  "1234567890123456789012345678901234567890\0"
234                  "8 times \"1234567890\"\0"
235         );
236         PGM_P challange[16];
237         set=1;
238         nessie_print_setheader(set);
239         dbz_splitup_P(challange_dbz, challange);
240         for(i=0; i<8; ++i){
241                 nessie_print_set_vector(set, i);
242                 ascii_hash_P(challange[2*i], challange[2*i+1]);
243         }
244         nessie_print_set_vector(set, i);
245         amillion_hash();
246         /* test set 2 */
247         set=2;
248         nessie_print_setheader(set);
249         for(i=0; i<1024; ++i){
250                 nessie_print_set_vector(set, i);
251                 zero_hash(i);
252         }
253         /* test set 3 */
254         set=3;
255         nessie_print_setheader(set);
256         for(i=0; i<512; ++i){
257                 nessie_print_set_vector(set, i);
258                 one_in512_hash(i);
259         }
260         /* test set 4 */
261         set=4;
262         nessie_print_setheader(set);
263         nessie_print_set_vector(set, 0);
264         tv4_hash();
265
266         nessie_print_footer();
267 }