]> git.cryptolib.org Git - arm-crypto-lib.git/blob - test_src/nessie_hash_test.c
switching to dedicated endian switching function
[arm-crypto-lib.git] / test_src / nessie_hash_test.c
1 /* nessie_hash_test.c */
2 /*
3     This file is part of the ARM-Crypto-Lib.
4     Copyright (C) 2006-2010  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(const char* data, const char* 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         cli_putstr("\r\n                       message=");
48         cli_putstr(desc);
49         nessie_hash_ctx.hash_init(ctx);
50         sl = strlen(data);
51         while(sl>=BLOCKSIZE_B){
52                 memcpy(buffer, data, BLOCKSIZE_B);
53                 nessie_hash_ctx.hash_next(ctx, buffer);
54                 data += BLOCKSIZE_B;
55                 sl   -= BLOCKSIZE_B;
56         }
57         memcpy(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         cli_putstr("\r\n                       message=");
74         cli_putstr("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         cli_putstr("\r\n                       message=");
95         if(n>=10000)
96                 cli_putc('0'+n/10000);
97         if(n>=1000)
98                 cli_putc('0'+(n/1000)%10);
99         if(n>=100)
100                 cli_putc('0'+(n/100)%10);
101         if(n>=10)
102                 cli_putc('0'+(n/10)%10);
103         cli_putc('0'+n%10);
104         cli_putstr(" 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         cli_putstr("\r\n                       message=");
128         cli_putstr("512-bit string: ");
129         if((pos/8) >=10){
130                 cli_putc('0'+(pos/8/10)%10);
131         } else {
132                 cli_putc(' ');
133         }
134         cli_putc('0'+(pos/8)%10);
135         cli_putstr("*00,");
136         cli_putstr(tab[pos&7]);
137         cli_putc(',');
138         if(63-(pos/8) >=10){
139                 cli_putc('0'+((63-pos/8)/10)%10);
140         } else {
141                 cli_putc(' ');
142         }
143         cli_putc('0'+(63-pos/8)%10);
144         cli_putstr("*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         cli_putstr("\r\n                       message=");
168         if(nessie_hash_ctx.hashsize_b>=10000)
169                 cli_putc('0' + (nessie_hash_ctx.hashsize_b/10000)%10);
170         if(nessie_hash_ctx.hashsize_b>=1000)
171                 cli_putc('0' + (nessie_hash_ctx.hashsize_b/1000)%10);
172         if(nessie_hash_ctx.hashsize_b>=100)
173                 cli_putc('0' + (nessie_hash_ctx.hashsize_b/100)%10);
174         if(nessie_hash_ctx.hashsize_b>=10)
175                 cli_putc('0' + (nessie_hash_ctx.hashsize_b/10)%10);
176         cli_putc('0' + nessie_hash_ctx.hashsize_b%10);
177
178         cli_putstr(" zero bits");
179         memset(block, 0, 256/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         char* challange_dbz=
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         const char* challange[16];
239         set=1;
240         nessie_print_setheader(set);
241         dbz_splitup(challange_dbz, challange);
242         for(i=0; i<8; ++i){
243                 nessie_print_set_vector(set, i);
244                 ascii_hash(challange[2*i], challange[2*i+1]);
245         }
246         nessie_print_set_vector(set, i);
247         if(!nessie_hash_quick)
248                 amillion_hash();
249         /* test set 2 */
250         set=2;
251         nessie_print_setheader(set);
252         for(i=0; i<1024; ++i){
253                 nessie_print_set_vector(set, i);
254                 zero_hash(i);
255         }
256         /* test set 3 */
257         set=3;
258         nessie_print_setheader(set);
259         for(i=0; i<512; ++i){
260                 nessie_print_set_vector(set, i);
261                 one_in512_hash(i);
262         }
263         /* test set 4 */
264         set=4;
265         nessie_print_setheader(set);
266         nessie_print_set_vector(set, 0);
267         tv4_hash();
268
269         nessie_print_footer();
270 }