]> git.cryptolib.org Git - avr-crypto-lib.git/blob - nessie_hash_test.c
insereated GPLv3 stub
[avr-crypto-lib.git] / nessie_hash_test.c
1 /* nessie_hash_test.c */
2 /*
3     This file is part of the Crypto-avr-lib/microcrypt-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[256/8];
155         uint16_t n=256;
156         uint32_t i;
157         
158         uart_putstr_P(PSTR("\r\n                       message="));
159         uart_putstr(PSTR("256 zero bits"));
160         memset(block, 0, 256/8);
161         
162         nessie_hash_ctx.hash_init(ctx);
163         while(n>=nessie_hash_ctx.blocksize_B*8){
164                 nessie_hash_ctx.hash_next(block, ctx);
165                 n    -= nessie_hash_ctx.blocksize_B*8;
166         }
167         nessie_hash_ctx.hash_last(block, n, ctx);
168         nessie_hash_ctx.hash_conv(hash, ctx);
169         nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
170         for(i=1; i<100000L; ++i){ /* this assumes BLOCKSIZE >= HASHSIZE */
171                 nessie_hash_ctx.hash_init(ctx);
172                 nessie_hash_ctx.hash_last(hash, nessie_hash_ctx.hashsize_b, ctx);
173                 nessie_hash_ctx.hash_conv(hash, ctx);
174         }
175         nessie_print_item("iterated 100000 times", hash, (nessie_hash_ctx.hashsize_b+7)/8);
176 }
177
178 /*
179    "" (empty string)
180    message="a"
181    message="abc"
182    message="message digest"
183    message="abcdefghijklmnopqrstuvwxyz"
184    message="abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
185    message="A...Za...z0...9"
186    message=8 times "1234567890"
187 */
188
189
190 void nessie_hash_run(void){
191         uint16_t i;
192         uint8_t set;
193         
194         nessie_print_header(nessie_hash_ctx.name, 0, 0, nessie_hash_ctx.hashsize_b, 0, 0);
195         /* test set 1 */
196         char* challange[8][2]= {
197                 {"", "\"\" (empty string)"},
198                 {"a", "\"a\""},
199                 {"abc", "\"abc\""},
200                 {"message digest", "\"message digest\""},
201                 {"abcdefghijklmnopqrstuvwxyz","\"abcdefghijklmnopqrstuvwxyz\""},
202                 {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
203                         "\"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\""},
204                 {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
205                  "abcdefghijklmnopqrstuvwxyz"
206                  "0123456789"   , "\"A...Za...z0...9\""},
207                 {"1234567890" "1234567890" "1234567890" "1234567890" 
208                  "1234567890" "1234567890" "1234567890" "1234567890",
209                  "8 times \"1234567890\""}      
210         };
211         set=1;
212         nessie_print_setheader(set);
213         for(i=0; i<8; ++i){
214                 nessie_print_set_vector(set, i);
215                 ascii_hash(challange[i][0], challange[i][1]);
216         }
217         nessie_print_set_vector(set, i);
218         amillion_hash();
219         /* test set 2 */
220         set=2;
221         nessie_print_setheader(set);
222         for(i=0; i<1024; ++i){
223                 nessie_print_set_vector(set, i);
224                 zero_hash(i);
225         }
226         /* test set 3 */
227         set=3;
228         nessie_print_setheader(set);
229         for(i=0; i<512; ++i){
230                 nessie_print_set_vector(set, i);
231                 one_in512_hash(i);
232         }
233         /* test set 4 */
234         set=4;
235         nessie_print_setheader(set);
236         nessie_print_set_vector(set, 0);
237         tv4_hash();
238
239         nessie_print_footer();
240 }