]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/nessie_hash_test.c
renaming to AVR-Crypto-Lib
[avr-crypto-lib.git] / test_src / nessie_hash_test.c
1 /* nessie_hash_test.c */
2 /*
3     This file is part of the 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         uart_putstr_P(PSTR("\r\n                       message="));
43         uart_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         
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;
72         }
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);
76 }
77
78
79 static
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];
84         
85         uart_putstr_P(PSTR("\r\n                       message="));
86         if(n>=10000)
87                 uart_putc('0'+n/10000);
88         if(n>=1000)
89                 uart_putc('0'+(n/1000)%10);
90         if(n>=100)
91                 uart_putc('0'+(n/100)%10);
92         if(n>=10)
93                 uart_putc('0'+(n/10)%10);
94         uart_putc('0'+n%10);
95         uart_putstr_P(PSTR(" zero bits"));
96         
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;
102         }
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);
106 }
107
108 static
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];
113         uint16_t n=512;
114         char* tab[8]={"80", "40", "20", "10", 
115                       "08", "04", "02", "01" };
116
117         pos&=511;
118         uart_putstr_P(PSTR("\r\n                       message="));
119         uart_putstr_P(PSTR("512-bit string: "));
120         if((pos/8) >=10){
121                 uart_putc('0'+(pos/8/10)%10);
122         } else {
123                 uart_putc(' ');
124         }
125         uart_putc('0'+(pos/8)%10);
126         uart_putstr_P(PSTR("*00,"));
127         uart_putstr(tab[pos&7]);
128         uart_putc(',');
129         if(63-(pos/8) >=10){
130                 uart_putc('0'+((63-pos/8)/10)%10);
131         } else {
132                 uart_putc(' ');
133         }
134         uart_putc('0'+(63-pos/8)%10);
135         uart_putstr_P(PSTR("*00"));
136         
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;
144         }
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);
148 }
149
150 static
151 void tv4_hash(void){
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;
156         uint32_t i;
157         
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);
168
169         uart_putstr_P(PSTR(" zero bits"));
170         memset(block, 0, 256/8);
171         
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;
176         }
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);
184         }
185         nessie_print_item("iterated 100000 times", hash, (nessie_hash_ctx.hashsize_b+7)/8);
186 }
187
188 /*
189    "" (empty string)
190    message="a"
191    message="abc"
192    message="message digest"
193    message="abcdefghijklmnopqrstuvwxyz"
194    message="abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
195    message="A...Za...z0...9"
196    message=8 times "1234567890"
197 */
198
199
200 void nessie_hash_run(void){
201         uint16_t i;
202         uint8_t set;
203         
204         nessie_print_header(nessie_hash_ctx.name, 0, 0, nessie_hash_ctx.hashsize_b, 0, 0);
205         /* test set 1 */
206         char* challange[8][2]= {
207                 {"", "\"\" (empty string)"},
208                 {"a", "\"a\""},
209                 {"abc", "\"abc\""},
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\""}      
220         };
221         set=1;
222         nessie_print_setheader(set);
223         for(i=0; i<8; ++i){
224                 nessie_print_set_vector(set, i);
225                 ascii_hash(challange[i][0], challange[i][1]);
226         }
227         nessie_print_set_vector(set, i);
228         amillion_hash();
229         /* test set 2 */
230         set=2;
231         nessie_print_setheader(set);
232         for(i=0; i<1024; ++i){
233                 nessie_print_set_vector(set, i);
234                 zero_hash(i);
235         }
236         /* test set 3 */
237         set=3;
238         nessie_print_setheader(set);
239         for(i=0; i<512; ++i){
240                 nessie_print_set_vector(set, i);
241                 one_in512_hash(i);
242         }
243         /* test set 4 */
244         set=4;
245         nessie_print_setheader(set);
246         nessie_print_set_vector(set, 0);
247         tv4_hash();
248
249         nessie_print_footer();
250 }