]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/nessie_hash_test.c
fixing E-Mail-Address & Copyright
[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) 2006-2015 Daniel Otte (bg@nerilex.org)
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:  bg@nerilex.org
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 <stdio.h>
31 #include "nessie_hash_test.h"
32 #include "nessie_common.h"
33 #include "dbz_strings.h"
34
35 nessie_hash_ctx_t nessie_hash_ctx;
36 uint8_t           nessie_hash_quick=0;
37
38 #define HASHSIZE_B ((nessie_hash_ctx.hashsize_b+7)/8)
39 #define BLOCKSIZE_B (nessie_hash_ctx.blocksize_B)
40
41 static
42 void ascii_hash_P(PGM_P data, PGM_P desc){
43         uint8_t ctx[nessie_hash_ctx.ctx_size_B];
44         uint8_t hash[HASHSIZE_B];
45         uint16_t sl;
46         uint8_t buffer[BLOCKSIZE_B];
47         
48         fputs_P(PSTR("\n                       message="), stdout);
49         fputs_P(desc, stdout);
50         nessie_hash_ctx.hash_init(ctx);
51         sl = strlen_P(data);
52         while(sl>=BLOCKSIZE_B){
53                 memcpy_P(buffer, data, BLOCKSIZE_B);
54                 nessie_hash_ctx.hash_next(ctx, buffer);
55                 data += BLOCKSIZE_B;
56                 sl   -= BLOCKSIZE_B;
57         }
58         memcpy_P(buffer, data, sl);
59         nessie_hash_ctx.hash_last(ctx, buffer, sl*8);
60         nessie_hash_ctx.hash_conv(hash, ctx);
61         nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
62 }
63
64 // message=1 million times "a"
65
66 static
67 void amillion_hash(void){
68         uint8_t ctx[nessie_hash_ctx.ctx_size_B];
69         uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8];
70         uint8_t block[nessie_hash_ctx.blocksize_B];
71         uint32_t n=1000000LL;
72         uint16_t i=0;
73         
74         fputs_P(PSTR("\n                       message="), stdout);
75         fputs_P(PSTR("1 million times \"a\""), stdout);
76         memset(block, 'a', nessie_hash_ctx.blocksize_B);
77         nessie_hash_ctx.hash_init(ctx);
78         while(n>=nessie_hash_ctx.blocksize_B){
79                 nessie_hash_ctx.hash_next(ctx, block);
80                 n    -= nessie_hash_ctx.blocksize_B;
81                 NESSIE_SEND_ALIVE_A(i++);
82         }
83         nessie_hash_ctx.hash_last(ctx, block, n*8);
84         nessie_hash_ctx.hash_conv(hash, ctx);
85         nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
86 }
87
88
89 static
90 void zero_hash(uint16_t n){
91         uint8_t ctx[nessie_hash_ctx.ctx_size_B];
92         uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8];
93         uint8_t block[nessie_hash_ctx.blocksize_B];
94         
95         fputs_P(PSTR("\n                       message="), stdout);
96         fprintf_P(stdout, PSTR("%"PRIu16" zero bits"));
97         
98         memset(block, 0, nessie_hash_ctx.blocksize_B); 
99         nessie_hash_ctx.hash_init(ctx);
100         while(n>=nessie_hash_ctx.blocksize_B*8){
101                 nessie_hash_ctx.hash_next(ctx, block);
102                 n   -= nessie_hash_ctx.blocksize_B*8;
103         }
104         nessie_hash_ctx.hash_last(ctx, block, n);
105         nessie_hash_ctx.hash_conv(hash, ctx);
106         nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
107 }
108
109 static
110 void one_in512_hash(uint16_t pos){
111         uint8_t ctx[nessie_hash_ctx.ctx_size_B];
112         uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8];
113         uint8_t block[nessie_hash_ctx.blocksize_B];
114         uint16_t n=512;
115         char *tab[8] = { "80", "40", "20", "10",
116                          "08", "04", "02", "01" };
117
118         pos&=511;
119         fputs_P(PSTR("\n                       message="), stdout);
120         fputs_P(PSTR("512-bit string: "), stdout);
121
122         fprintf_P(stdout, PSTR("%2"PRIu16"*00,%s,%2"PRIu16"*00"), pos / 8, tab[pos & 7], 63 - pos / 8);
123         
124         /* now the real stuff */
125         memset(block, 0, 512/8);
126         block[pos>>3] = 0x80>>(pos&0x7);
127         nessie_hash_ctx.hash_init(ctx);
128         while(n>=nessie_hash_ctx.blocksize_B*8){
129                 nessie_hash_ctx.hash_next(ctx, block);
130                 n   -= nessie_hash_ctx.blocksize_B*8;
131         }
132         nessie_hash_ctx.hash_last(ctx, block, n);
133         nessie_hash_ctx.hash_conv(hash, ctx);
134         nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
135 }
136
137 static
138 void tv4_hash(void){
139         uint8_t ctx[nessie_hash_ctx.ctx_size_B];
140         uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8];
141         uint8_t block[nessie_hash_ctx.hashsize_b/8];
142         uint16_t n=nessie_hash_ctx.hashsize_b;
143         uint32_t i;
144         
145         fputs_P(PSTR("\r\n                       message="), stdout);
146         fprintf_P(stdout, PSTR("%"PRIu16" zero bits"), nessie_hash_ctx.hashsize_b);
147
148         memset(block, 0, nessie_hash_ctx.hashsize_b/8);
149         
150         nessie_hash_ctx.hash_init(ctx);
151         while(n>=nessie_hash_ctx.blocksize_B*8){
152                 nessie_hash_ctx.hash_next(ctx, block);
153                 n    -= nessie_hash_ctx.blocksize_B*8;
154         }
155         nessie_hash_ctx.hash_last(ctx, block, n);
156         nessie_hash_ctx.hash_conv(hash, ctx);
157         nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
158         if(nessie_hash_quick)
159                 return;
160         for(i=1; i<100000L; ++i){ /* this assumes BLOCKSIZE >= HASHSIZE */
161                 nessie_hash_ctx.hash_init(ctx);
162                 nessie_hash_ctx.hash_last(ctx, hash, nessie_hash_ctx.hashsize_b);
163                 nessie_hash_ctx.hash_conv(hash, ctx);
164                 NESSIE_SEND_ALIVE_A(i);
165         }
166         nessie_print_item("iterated 100000 times", hash, (nessie_hash_ctx.hashsize_b+7)/8);
167 }
168
169 /*
170    "" (empty string)
171    message="a"
172    message="abc"
173    message="message digest"
174    message="abcdefghijklmnopqrstuvwxyz"
175    message="abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
176    message="A...Za...z0...9"
177    message=8 times "1234567890"
178 */
179
180
181 void nessie_hash_run(void){
182         uint16_t i;
183         uint8_t set;
184         
185         nessie_print_header(nessie_hash_ctx.name, 0, 0, nessie_hash_ctx.hashsize_b, 0, 0);
186         /* test set 1 */
187         const char *challange_dbz= PSTR(
188                   "\0"
189                 "\"\" (empty string)\0"
190                   "a\0"
191                 "\"a\"\0"
192                   "abc\0"
193                 "\"abc\"\0"
194                   "message digest\0"
195                 "\"message digest\"\0"
196                   "abcdefghijklmnopqrstuvwxyz\0"
197                 "\"abcdefghijklmnopqrstuvwxyz\"\0"
198                   "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\0"
199                 "\"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\"\0"
200                   "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
201                   "abcdefghijklmnopqrstuvwxyz"
202                   "0123456789\0"         
203                  "\"A...Za...z0...9\"\0"
204                  "1234567890123456789012345678901234567890" 
205                  "1234567890123456789012345678901234567890\0"
206                  "8 times \"1234567890\"\0"
207         );
208         PGM_P challange[16];
209         set=1;
210         nessie_print_setheader(set);
211         dbz_splitup_P(challange_dbz, challange);
212         for(i=0; i<8; ++i){
213                 nessie_print_set_vector(set, i);
214                 ascii_hash_P(challange[2*i], challange[2*i+1]);
215         }
216         nessie_print_set_vector(set, i);
217         if(!nessie_hash_quick)
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 }