_haval.c

filter: Statementstmt 62.5% 217 / 347 Branchbran 15.0% 9 / 60 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 55.5% 226 / 407 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Digest/Haval256.pm ↑ Summary Haval256.xs »
1 /*
2 * haval.h: specifies the interface and routines in the HAVAL (V.1)
3 * hashing library.
4 *
5 * HAVAL is a one-way hashing algorithm with the following
6 * collision-resistant property:
7 * It is computationally infeasible to find two or more
8 * messages that are hashed into the same fingerprint.
9 *
10 * Reference:
11 * Y. Zheng, J. Pieprzyk and J. Seberry:
12 * ``HAVAL --- a one-way hashing algorithm with variable
13 * length of output'', Advances in Cryptology --- AUSCRYPT'92,
14 * Lecture Notes in Computer Science, Vol.718, pp.83-104,
15 * Springer-Verlag, 1993.
16 *
17 * Descriptions:
18 * - haval_string: hash a string
19 * - haval_file: hash a file
20 * - haval_stdin: filter -- hash input from the stdin device
21 * - haval_hash: hash a string of specified length
22 * (Haval_hash is used in conjunction with
23 * haval_start & haval_end.)
24 * - haval_hash_block: hash a 32-word block
25 * - haval_start: initialization
26 * - haval_end: finalization
27 *
28 * Arguments for the test program:
29 *
30 * (none) - hash input from stdin
31 * ?,-?,-h - show help menu
32 * -c - hash certification data
33 * -e - test whether your machine is little-endian
34 * -mstring - hash message (string of chars)
35 * -s - test speed
36 * file_name - hash file
37 *
38 * Makefile for the testing program:
39 *
40 * CC=acc
41 * CFLAGS=-fast
42 *
43 * haval: haval.o havaltest.o
44 * ${CC} ${CFLAGS} haval.o havaltest.o -o $@
45 * haval.o havaltest.o: havalapp.h
46 *
47 * clean:
48 * /usr/bin/rm -f *.o haval
49 *
50 *
51 * Author: Yuliang Zheng
52 * School of Computing and Information Technology
53 * Monash University
54 * McMahons Road, Frankston 3199, Australia
55 * Email: yzheng@fcit.monash.edu.au
56 * URL: http://www-pscit.fcit.monash.edu.au:/~yuliang/
57 * Voice: +61 3 9904 4196
58 * Fax : +61 3 9904 4124
59 *
60 * Date:
61 * First release: June 1993
62 *
63 * Revised: April 1996
64 * Major changes:
65 * 1. added #include <string.h>
66 * 2. deleted #include <memory.h>
67 * 3. appended 'L' to all 32-bit constants
68 *
69 * Many thanks to Ross Williams (ross@rocksoft.com)
70 * for his invaluable comments on the previous
71 * version of the code.
72 *
73 * Revised: April 1997
74 * Corrected the first byte in padding[128]
75 * (from 0x80 to 0x01).
76 *
77 * Thanks go to Paulo Barreto (pbarreto@uninet.com.br)
78 * who detected the padding error while implementing
79 * HAVAL independently.
80 *
81 * Copyright (C) 1997 by Yuliang Zheng. All rights reserved.
82 * This program may not be sold or used as inducement to sell
83 * a product without the written permission of the author.
84 */
85  
86 #include <stdio.h>
87 #include <time.h>
88 #include <string.h>
89  
90 /* #undef LITTLE_ENDIAN */
91  
92 #define PASS 5 /* 3, 4, or 5 */
93 #define FPTLEN 256 /* 128, 160, 192, 224 or 256 */
94 #define HVERSION 1 /* current version number */
95  
96 typedef unsigned long int haval_word; /* a HAVAL word = 32 bits */
97  
98 typedef struct haval_state {
99 haval_word count[2]; /* number of bits in a message */
100 haval_word fingerprint[8]; /* current state of fingerprint */
101 haval_word block[32]; /* buffer for a 32-word block */
102 unsigned char remainder[32*4]; /* unhashed chars (No.<128) */
103 } haval_state;
104  
105 void haval_string(char *, unsigned char *); /* hash a string */
106 int haval_file(char *, unsigned char *); /* hash a file */
107 void haval_stdin(void); /* hash input from stdin */
108 void haval_start(haval_state *); /* initialization */
109 void haval_hash(haval_state *,
110 unsigned char *, unsigned int); /* updating routine */
111 void haval_end(haval_state *, unsigned char *); /* finalization */
112 void haval_hash_block(haval_state *); /* hash a 32-word block */
113 static void haval_tailor(haval_state *); /* folding the last output */
114  
115 static unsigned char padding[128] = { /* constants for padding */
116 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
117 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
118 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
119 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
120 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
121 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
122 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
123 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
124 };
125  
126 #define f_1(X6, X5, X4, X3, X2, X1, X0) \
127 (((X1) & ((X4) ^ (X0))) ^ ((X2) & (X5)) ^ ((X3) & (X6)) ^ (X0))
128  
129 #define f_2(X6, X5, X4, X3, X2, X1, X0) \
130 (((X2) & (((X1) & (~(X3))) ^ ((X4) & (X5)) ^ (X6) ^ (X0))) ^ \
131 (((X4) & ((X1) ^ (X5))) ^ ((X3) & (X5)) ^ (X0)))
132  
133 #define f_3(X6, X5, X4, X3, X2, X1, X0) \
134 (((X3) & (((X1) & (X2)) ^ (X6) ^ (X0))) ^ ((X1) & (X4)) ^ \
135 ((X2) & (X5)) ^ (X0))
136  
137 #define f_4(X6, X5, X4, X3, X2, X1, X0) \
138 (((X4) & (((~(X2)) & (X5)) ^ ((X3) | (X6)) ^ (X1) ^ (X0))) ^ \
139 ((X3) & (((X1) & (X2)) ^ (X5) ^ (X6))) ^ ((X2) & (X6)) ^ (X0))
140  
141 #define f_5(X6, X5, X4, X3, X2, X1, X0) \
142 (((X1) & ((X4) ^ ((X0) & (X2) & (X3)))) ^ \
143 (((X2) ^ (X0)) & (X5)) ^ ((X3) & (X6)) ^ (X0))
144  
145  
146 /*
147 * Permutations phi_{i,j}, i=3,4,5, j=1,...,i.
148 *
149 * PASS = 3:
150 * 6 5 4 3 2 1 0
151 * | | | | | | | (replaced by)
152 * phi_{3,1}: 1 0 3 5 6 2 4
153 * phi_{3,2}: 4 2 1 0 5 3 6
154 * phi_{3,3}: 6 1 2 3 4 5 0
155 *
156 * PASS = 4:
157 * 6 5 4 3 2 1 0
158 * | | | | | | | (replaced by)
159 * phi_{4,1}: 2 6 1 4 5 3 0
160 * phi_{4,2}: 3 5 2 0 1 6 4
161 * phi_{4,3}: 1 4 3 6 0 2 5
162 * phi_{4,4}: 6 4 0 5 2 1 3
163 *
164 * PASS = 5:
165 * 6 5 4 3 2 1 0
166 * | | | | | | | (replaced by)
167 * phi_{5,1}: 3 4 1 0 5 2 6
168 * phi_{5,2}: 6 2 1 0 3 4 5
169 * phi_{5,3}: 2 6 0 4 3 1 5
170 * phi_{5,4}: 1 5 3 2 0 4 6
171 * phi_{5,5}: 2 5 0 6 4 3 1
172 */
173  
174 #if PASS == 3
175 #define Fphi_1(x6, x5, x4, x3, x2, x1, x0) \
176 f_1(x1, x0, x3, x5, x6, x2, x4)
177 #elif PASS == 4
178 #define Fphi_1(x6, x5, x4, x3, x2, x1, x0) \
179 f_1(x2, x6, x1, x4, x5, x3, x0)
180 #else
181 #define Fphi_1(x6, x5, x4, x3, x2, x1, x0) \
182 f_1(x3, x4, x1, x0, x5, x2, x6)
183 #endif
184  
185 #if PASS == 3
186 #define Fphi_2(x6, x5, x4, x3, x2, x1, x0) \
187 f_2(x4, x2, x1, x0, x5, x3, x6)
188 #elif PASS == 4
189 #define Fphi_2(x6, x5, x4, x3, x2, x1, x0) \
190 f_2(x3, x5, x2, x0, x1, x6, x4)
191 #else
192 #define Fphi_2(x6, x5, x4, x3, x2, x1, x0) \
193 f_2(x6, x2, x1, x0, x3, x4, x5)
194 #endif
195  
196 #if PASS == 3
197 #define Fphi_3(x6, x5, x4, x3, x2, x1, x0) \
198 f_3(x6, x1, x2, x3, x4, x5, x0)
199 #elif PASS == 4
200 #define Fphi_3(x6, x5, x4, x3, x2, x1, x0) \
201 f_3(x1, x4, x3, x6, x0, x2, x5)
202 #else
203 #define Fphi_3(x6, x5, x4, x3, x2, x1, x0) \
204 f_3(x2, x6, x0, x4, x3, x1, x5)
205 #endif
206  
207 #if PASS == 4
208 #define Fphi_4(x6, x5, x4, x3, x2, x1, x0) \
209 f_4(x6, x4, x0, x5, x2, x1, x3)
210 #else
211 #define Fphi_4(x6, x5, x4, x3, x2, x1, x0) \
212 f_4(x1, x5, x3, x2, x0, x4, x6)
213 #endif
214  
215 #define Fphi_5(x6, x5, x4, x3, x2, x1, x0) \
216 f_5(x2, x5, x0, x6, x4, x3, x1)
217  
218 #define rotate_right(x, n) (((x) >> (n)) | ((x) << (32-(n))))
219  
220 #define FF_1(x7, x6, x5, x4, x3, x2, x1, x0, w) { \
221 register haval_word temp = Fphi_1(x6, x5, x4, x3, x2, x1, x0); \
222 (x7) = rotate_right(temp, 7) + rotate_right((x7), 11) + (w); \
223 }
224  
225 #define FF_2(x7, x6, x5, x4, x3, x2, x1, x0, w, c) { \
226 register haval_word temp = Fphi_2(x6, x5, x4, x3, x2, x1, x0); \
227 (x7) = rotate_right(temp, 7) + rotate_right((x7), 11) + (w) + (c); \
228 }
229  
230 #define FF_3(x7, x6, x5, x4, x3, x2, x1, x0, w, c) { \
231 register haval_word temp = Fphi_3(x6, x5, x4, x3, x2, x1, x0); \
232 (x7) = rotate_right(temp, 7) + rotate_right((x7), 11) + (w) + (c); \
233 }
234  
235 #define FF_4(x7, x6, x5, x4, x3, x2, x1, x0, w, c) { \
236 register haval_word temp = Fphi_4(x6, x5, x4, x3, x2, x1, x0); \
237 (x7) = rotate_right(temp, 7) + rotate_right((x7), 11) + (w) + (c); \
238 }
239  
240 #define FF_5(x7, x6, x5, x4, x3, x2, x1, x0, w, c) { \
241 register haval_word temp = Fphi_5(x6, x5, x4, x3, x2, x1, x0); \
242 (x7) = rotate_right(temp, 7) + rotate_right((x7), 11) + (w) + (c); \
243 }
244  
245 /*
246 * translate every four characters into a word.
247 * assume the number of characters is a multiple of four.
248 */
249 #define ch2uint(string, word, slen) { \
250 unsigned char *sp = string; \
251 haval_word *wp = word; \
252 while (sp < (string) + (slen)) { \
253 *wp++ = (haval_word)*sp | \
254 ((haval_word)*(sp+1) << 8) | \
255 ((haval_word)*(sp+2) << 16) | \
256 ((haval_word)*(sp+3) << 24); \
257 sp += 4; \
258 } \
259 }
260  
261 /* translate each word into four characters */
262 #define uint2ch(word, string, wlen) { \
263 haval_word *wp = word; \
264 unsigned char *sp = string; \
265 while (wp < (word) + (wlen)) { \
266 *(sp++) = (unsigned char)( *wp & 0xFF); \
267 *(sp++) = (unsigned char)((*wp >> 8) & 0xFF); \
268 *(sp++) = (unsigned char)((*wp >> 16) & 0xFF); \
269 *(sp++) = (unsigned char)((*wp >> 24) & 0xFF); \
270 wp++; \
271 } \
272 }
273  
274 /* hash a string */
275 0 void haval_string(char *string, unsigned char *fingerprint)
276 {
277 haval_state state;
278 0 unsigned int len = strlen (string);
279  
280 0 haval_start(&state);
281 0 haval_hash(&state, (unsigned char *)string, len);
282 0 haval_end(&state, fingerprint);
283 0 }
284  
285 /* hash a file */
286 0 int haval_file(char *file_name, unsigned char *fingerprint)
287 {
288 FILE *file;
289 haval_state state;
290 int len;
291 unsigned char buffer[1024];
292  
293 0 ▶ if ((file = fopen(file_name, "rb")) == NULL){
Branch: 1 branch
BranchTrueFalseTotal
if ((file = fopen(file_name, "rb")) == NULL){ 0 0 0
294 0 return 1; /* fail */
295 } else {
296 0 haval_start(&state);
297 0 ▶ while ((len = fread(buffer, 1, 1024, file))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((len = fread(buffer, 1, 1024, file))) { 0 0 0
298 0 haval_hash(&state, buffer, len);
299 }
300 0 fclose(file);
301 0 haval_end(&state, fingerprint);
302 0 return 0; /* success */
303 }
304 }
305  
306 /* hash input from stdin */
307 0 void haval_stdin(void)
308 {
309 haval_state state;
310 int i, len;
311 unsigned char buffer[32],
312 fingerprint[FPTLEN >> 3];
313  
314 0 haval_start(&state);
315 0 ▶ while ((len = fread(buffer, 1, 32, stdin))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((len = fread(buffer, 1, 32, stdin))) { 0 0 0
316 0 haval_hash(&state, buffer, len);
317 }
318 0 haval_end(&state, fingerprint);
319  
320 0 ▶ for (i = 0; i < FPTLEN >> 3; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < FPTLEN >> 3; i++) { 0 0 0
321 0 putchar(fingerprint[i]);
322 }
323 0 }
324  
325 /* initialization */
326 12 void haval_start(haval_state *state)
327 {
328 12 state->count[0] = state->count[1] = 0; /* clear count */
329 12 state->fingerprint[0] = 0x243F6A88L; /* initial fingerprint */
330 12 state->fingerprint[1] = 0x85A308D3L;
331 12 state->fingerprint[2] = 0x13198A2EL;
332 12 state->fingerprint[3] = 0x03707344L;
333 12 state->fingerprint[4] = 0xA4093822L;
334 12 state->fingerprint[5] = 0x299F31D0L;
335 12 state->fingerprint[6] = 0x082EFA98L;
336 12 state->fingerprint[7] = 0xEC4E6C89L;
337 12 }
338  
339 /*
340 * hash a string of specified length.
341 * to be used in conjunction with haval_start and haval_end.
342 */
343 35 void haval_hash(haval_state *state,
344 unsigned char *str, unsigned int str_len)
345 {
346 unsigned int i,
347 rmd_len,
348 fill_len;
349  
350 /* calculate the number of bytes in the remainder */
351 35 rmd_len = (unsigned int)((state->count[0] >> 3) & 0x7F);
352 35 fill_len = 128 - rmd_len;
353  
354 /* update the number of bits */
355 35 if ((state->count[0] += (haval_word) str_len << 3)
356 35 ▶ < ((haval_word) str_len << 3)) {
Branch: 1 branch
BranchTrueFalseTotal
< ((haval_word) str_len << 3)) { 0 35 35
357 0 state->count[1]++;
358 }
359 35 state->count[1] += (haval_word) str_len >> 29;
360  
361 #ifdef LITTLE_ENDIAN
362  
363 /* hash as many blocks as possible */
364 35 ▶ if (rmd_len + str_len >= 128) {
Branch: 1 branch
BranchTrueFalseTotal
if (rmd_len + str_len >= 128) { 12 23 35
365 12 memcpy(((unsigned char *) state->block) + rmd_len, str, fill_len);
366 12 haval_hash_block (state);
367 12 ▶ for (i = fill_len; i + 127 < str_len; i += 128){
Branch: 1 branch
BranchTrueFalseTotal
for (i = fill_len; i + 127 < str_len; i += 128){ 0 12 12
368 0 memcpy((unsigned char *) state->block, str+i, 128);
369 0 haval_hash_block(state);
370 }
371 12 rmd_len = 0;
372 } else {
373 23 i = 0;
374 }
375 35 memcpy(((unsigned char *) state->block) + rmd_len, str + i, str_len - i);
376  
377 #else
378  
379 /* hash as many blocks as possible */
380 if (rmd_len + str_len >= 128) {
381 memcpy(&state->remainder[rmd_len], str, fill_len);
382 ch2uint(state->remainder, state->block, 128);
383 haval_hash_block (state);
384 for (i = fill_len; i + 127 < str_len; i += 128){
385 memcpy(state->remainder, str+i, 128);
386 ch2uint(state->remainder, state->block, 128);
387 haval_hash_block(state);
388 }
389 rmd_len = 0;
390 } else {
391 i = 0;
392 }
393 /* save the remaining input chars */
394 memcpy(&state->remainder[rmd_len], str + i, str_len - i);
395  
396 #endif
397 35 }
398  
399 /* finalization */
400 12 void haval_end(haval_state *state, unsigned char *final_fpt)
401 {
402 unsigned char tail[10];
403 unsigned int rmd_len, pad_len;
404  
405 /*
406 * save the version number, the number of passes, the fingerprint
407 * length and the number of bits in the unpadded message.
408 */
409 12 tail[0] = (unsigned char)(((FPTLEN & 0x3) << 6) |
410 ((PASS & 0x7) << 3) |
411 (HVERSION & 0x7));
412 12 tail[1] = (unsigned char)((FPTLEN >> 2) & 0xFF);
413 36 ▶ uint2ch (state->count, &tail[2], 2);
Branch: 1 branch
BranchTrueFalseTotal
uint2ch (state->count, &tail[2], 2); 24 12 36
414  
415 /* pad out to 118 mod 128 */
416 12 rmd_len = (unsigned int)((state->count[0] >> 3) & 0x7f);
417 12 ▶ pad_len = (rmd_len < 118) ? (118 - rmd_len) : (246 - rmd_len);
Branch: 1 branch
BranchTrueFalseTotal
pad_len = (rmd_len < 118) ? (118 - rmd_len) : (246 - rmd_len); 12 0 12
418 12 haval_hash(state, padding, pad_len);
419  
420 /*
421 * append the version number, the number of passes,
422 * the fingerprint length and the number of bits
423 */
424 12 haval_hash(state, tail, 10);
425  
426 /* tailor the last output */
427 12 haval_tailor(state);
428  
429 /* translate and save the final fingerprint */
430 108 ▶ uint2ch (state->fingerprint, final_fpt, FPTLEN >> 5);
Branch: 1 branch
BranchTrueFalseTotal
uint2ch (state->fingerprint, final_fpt, FPTLEN >> 5); 96 12 108
431  
432 /* clear the state information */
433 /* memset((unsigned char *)state, 0, sizeof (*state)); */
434 12 }
435  
436 /* hash a 32-word block */
437 12 void haval_hash_block(haval_state *state)
438 {
439 12 register haval_word t0 = state->fingerprint[0], /* make use of */
440 12 t1 = state->fingerprint[1], /* internal registers */
441 12 t2 = state->fingerprint[2],
442 12 t3 = state->fingerprint[3],
443 12 t4 = state->fingerprint[4],
444 12 t5 = state->fingerprint[5],
445 12 t6 = state->fingerprint[6],
446 12 t7 = state->fingerprint[7],
447 12 *w = state->block;
448  
449 /* Pass 1 */
450 12 FF_1(t7, t6, t5, t4, t3, t2, t1, t0, *(w ));
451 12 FF_1(t6, t5, t4, t3, t2, t1, t0, t7, *(w+ 1));
452 12 FF_1(t5, t4, t3, t2, t1, t0, t7, t6, *(w+ 2));
453 12 FF_1(t4, t3, t2, t1, t0, t7, t6, t5, *(w+ 3));
454 12 FF_1(t3, t2, t1, t0, t7, t6, t5, t4, *(w+ 4));
455 12 FF_1(t2, t1, t0, t7, t6, t5, t4, t3, *(w+ 5));
456 12 FF_1(t1, t0, t7, t6, t5, t4, t3, t2, *(w+ 6));
457 12 FF_1(t0, t7, t6, t5, t4, t3, t2, t1, *(w+ 7));
458  
459 12 FF_1(t7, t6, t5, t4, t3, t2, t1, t0, *(w+ 8));
460 12 FF_1(t6, t5, t4, t3, t2, t1, t0, t7, *(w+ 9));
461 12 FF_1(t5, t4, t3, t2, t1, t0, t7, t6, *(w+10));
462 12 FF_1(t4, t3, t2, t1, t0, t7, t6, t5, *(w+11));
463 12 FF_1(t3, t2, t1, t0, t7, t6, t5, t4, *(w+12));
464 12 FF_1(t2, t1, t0, t7, t6, t5, t4, t3, *(w+13));
465 12 FF_1(t1, t0, t7, t6, t5, t4, t3, t2, *(w+14));
466 12 FF_1(t0, t7, t6, t5, t4, t3, t2, t1, *(w+15));
467  
468 12 FF_1(t7, t6, t5, t4, t3, t2, t1, t0, *(w+16));
469 12 FF_1(t6, t5, t4, t3, t2, t1, t0, t7, *(w+17));
470 12 FF_1(t5, t4, t3, t2, t1, t0, t7, t6, *(w+18));
471 12 FF_1(t4, t3, t2, t1, t0, t7, t6, t5, *(w+19));
472 12 FF_1(t3, t2, t1, t0, t7, t6, t5, t4, *(w+20));
473 12 FF_1(t2, t1, t0, t7, t6, t5, t4, t3, *(w+21));
474 12 FF_1(t1, t0, t7, t6, t5, t4, t3, t2, *(w+22));
475 12 FF_1(t0, t7, t6, t5, t4, t3, t2, t1, *(w+23));
476  
477 12 FF_1(t7, t6, t5, t4, t3, t2, t1, t0, *(w+24));
478 12 FF_1(t6, t5, t4, t3, t2, t1, t0, t7, *(w+25));
479 12 FF_1(t5, t4, t3, t2, t1, t0, t7, t6, *(w+26));
480 12 FF_1(t4, t3, t2, t1, t0, t7, t6, t5, *(w+27));
481 12 FF_1(t3, t2, t1, t0, t7, t6, t5, t4, *(w+28));
482 12 FF_1(t2, t1, t0, t7, t6, t5, t4, t3, *(w+29));
483 12 FF_1(t1, t0, t7, t6, t5, t4, t3, t2, *(w+30));
484 12 FF_1(t0, t7, t6, t5, t4, t3, t2, t1, *(w+31));
485  
486 /* Pass 2 */
487 12 FF_2(t7, t6, t5, t4, t3, t2, t1, t0, *(w+ 5), 0x452821E6L);
488 12 FF_2(t6, t5, t4, t3, t2, t1, t0, t7, *(w+14), 0x38D01377L);
489 12 FF_2(t5, t4, t3, t2, t1, t0, t7, t6, *(w+26), 0xBE5466CFL);
490 12 FF_2(t4, t3, t2, t1, t0, t7, t6, t5, *(w+18), 0x34E90C6CL);
491 12 FF_2(t3, t2, t1, t0, t7, t6, t5, t4, *(w+11), 0xC0AC29B7L);
492 12 FF_2(t2, t1, t0, t7, t6, t5, t4, t3, *(w+28), 0xC97C50DDL);
493 12 FF_2(t1, t0, t7, t6, t5, t4, t3, t2, *(w+ 7), 0x3F84D5B5L);
494 12 FF_2(t0, t7, t6, t5, t4, t3, t2, t1, *(w+16), 0xB5470917L);
495  
496 12 FF_2(t7, t6, t5, t4, t3, t2, t1, t0, *(w ), 0x9216D5D9L);
497 12 FF_2(t6, t5, t4, t3, t2, t1, t0, t7, *(w+23), 0x8979FB1BL);
498 12 FF_2(t5, t4, t3, t2, t1, t0, t7, t6, *(w+20), 0xD1310BA6L);
499 12 FF_2(t4, t3, t2, t1, t0, t7, t6, t5, *(w+22), 0x98DFB5ACL);
500 12 FF_2(t3, t2, t1, t0, t7, t6, t5, t4, *(w+ 1), 0x2FFD72DBL);
501 12 FF_2(t2, t1, t0, t7, t6, t5, t4, t3, *(w+10), 0xD01ADFB7L);
502 12 FF_2(t1, t0, t7, t6, t5, t4, t3, t2, *(w+ 4), 0xB8E1AFEDL);
503 12 FF_2(t0, t7, t6, t5, t4, t3, t2, t1, *(w+ 8), 0x6A267E96L);
504  
505 12 FF_2(t7, t6, t5, t4, t3, t2, t1, t0, *(w+30), 0xBA7C9045L);
506 12 FF_2(t6, t5, t4, t3, t2, t1, t0, t7, *(w+ 3), 0xF12C7F99L);
507 12 FF_2(t5, t4, t3, t2, t1, t0, t7, t6, *(w+21), 0x24A19947L);
508 12 FF_2(t4, t3, t2, t1, t0, t7, t6, t5, *(w+ 9), 0xB3916CF7L);
509 12 FF_2(t3, t2, t1, t0, t7, t6, t5, t4, *(w+17), 0x0801F2E2L);
510 12 FF_2(t2, t1, t0, t7, t6, t5, t4, t3, *(w+24), 0x858EFC16L);
511 12 FF_2(t1, t0, t7, t6, t5, t4, t3, t2, *(w+29), 0x636920D8L);
512 12 FF_2(t0, t7, t6, t5, t4, t3, t2, t1, *(w+ 6), 0x71574E69L);
513  
514 12 FF_2(t7, t6, t5, t4, t3, t2, t1, t0, *(w+19), 0xA458FEA3L);
515 12 FF_2(t6, t5, t4, t3, t2, t1, t0, t7, *(w+12), 0xF4933D7EL);
516 12 FF_2(t5, t4, t3, t2, t1, t0, t7, t6, *(w+15), 0x0D95748FL);
517 12 FF_2(t4, t3, t2, t1, t0, t7, t6, t5, *(w+13), 0x728EB658L);
518 12 FF_2(t3, t2, t1, t0, t7, t6, t5, t4, *(w+ 2), 0x718BCD58L);
519 12 FF_2(t2, t1, t0, t7, t6, t5, t4, t3, *(w+25), 0x82154AEEL);
520 12 FF_2(t1, t0, t7, t6, t5, t4, t3, t2, *(w+31), 0x7B54A41DL);
521 12 FF_2(t0, t7, t6, t5, t4, t3, t2, t1, *(w+27), 0xC25A59B5L);
522  
523 /* Pass 3 */
524 12 FF_3(t7, t6, t5, t4, t3, t2, t1, t0, *(w+19), 0x9C30D539L);
525 12 FF_3(t6, t5, t4, t3, t2, t1, t0, t7, *(w+ 9), 0x2AF26013L);
526 12 FF_3(t5, t4, t3, t2, t1, t0, t7, t6, *(w+ 4), 0xC5D1B023L);
527 12 FF_3(t4, t3, t2, t1, t0, t7, t6, t5, *(w+20), 0x286085F0L);
528 12 FF_3(t3, t2, t1, t0, t7, t6, t5, t4, *(w+28), 0xCA417918L);
529 12 FF_3(t2, t1, t0, t7, t6, t5, t4, t3, *(w+17), 0xB8DB38EFL);
530 12 FF_3(t1, t0, t7, t6, t5, t4, t3, t2, *(w+ 8), 0x8E79DCB0L);
531 12 FF_3(t0, t7, t6, t5, t4, t3, t2, t1, *(w+22), 0x603A180EL);
532  
533 12 FF_3(t7, t6, t5, t4, t3, t2, t1, t0, *(w+29), 0x6C9E0E8BL);
534 12 FF_3(t6, t5, t4, t3, t2, t1, t0, t7, *(w+14), 0xB01E8A3EL);
535 12 FF_3(t5, t4, t3, t2, t1, t0, t7, t6, *(w+25), 0xD71577C1L);
536 12 FF_3(t4, t3, t2, t1, t0, t7, t6, t5, *(w+12), 0xBD314B27L);
537 12 FF_3(t3, t2, t1, t0, t7, t6, t5, t4, *(w+24), 0x78AF2FDAL);
538 12 FF_3(t2, t1, t0, t7, t6, t5, t4, t3, *(w+30), 0x55605C60L);
539 12 FF_3(t1, t0, t7, t6, t5, t4, t3, t2, *(w+16), 0xE65525F3L);
540 12 FF_3(t0, t7, t6, t5, t4, t3, t2, t1, *(w+26), 0xAA55AB94L);
541  
542 12 FF_3(t7, t6, t5, t4, t3, t2, t1, t0, *(w+31), 0x57489862L);
543 12 FF_3(t6, t5, t4, t3, t2, t1, t0, t7, *(w+15), 0x63E81440L);
544 12 FF_3(t5, t4, t3, t2, t1, t0, t7, t6, *(w+ 7), 0x55CA396AL);
545 12 FF_3(t4, t3, t2, t1, t0, t7, t6, t5, *(w+ 3), 0x2AAB10B6L);
546 12 FF_3(t3, t2, t1, t0, t7, t6, t5, t4, *(w+ 1), 0xB4CC5C34L);
547 12 FF_3(t2, t1, t0, t7, t6, t5, t4, t3, *(w ), 0x1141E8CEL);
548 12 FF_3(t1, t0, t7, t6, t5, t4, t3, t2, *(w+18), 0xA15486AFL);
549 12 FF_3(t0, t7, t6, t5, t4, t3, t2, t1, *(w+27), 0x7C72E993L);
550  
551 12 FF_3(t7, t6, t5, t4, t3, t2, t1, t0, *(w+13), 0xB3EE1411L);
552 12 FF_3(t6, t5, t4, t3, t2, t1, t0, t7, *(w+ 6), 0x636FBC2AL);
553 12 FF_3(t5, t4, t3, t2, t1, t0, t7, t6, *(w+21), 0x2BA9C55DL);
554 12 FF_3(t4, t3, t2, t1, t0, t7, t6, t5, *(w+10), 0x741831F6L);
555 12 FF_3(t3, t2, t1, t0, t7, t6, t5, t4, *(w+23), 0xCE5C3E16L);
556 12 FF_3(t2, t1, t0, t7, t6, t5, t4, t3, *(w+11), 0x9B87931EL);
557 12 FF_3(t1, t0, t7, t6, t5, t4, t3, t2, *(w+ 5), 0xAFD6BA33L);
558 12 FF_3(t0, t7, t6, t5, t4, t3, t2, t1, *(w+ 2), 0x6C24CF5CL);
559  
560 #if PASS >= 4
561 /* Pass 4. executed only when PASS =4 or 5 */
562 12 FF_4(t7, t6, t5, t4, t3, t2, t1, t0, *(w+24), 0x7A325381L);
563 12 FF_4(t6, t5, t4, t3, t2, t1, t0, t7, *(w+ 4), 0x28958677L);
564 12 FF_4(t5, t4, t3, t2, t1, t0, t7, t6, *(w ), 0x3B8F4898L);
565 12 FF_4(t4, t3, t2, t1, t0, t7, t6, t5, *(w+14), 0x6B4BB9AFL);
566 12 FF_4(t3, t2, t1, t0, t7, t6, t5, t4, *(w+ 2), 0xC4BFE81BL);
567 12 FF_4(t2, t1, t0, t7, t6, t5, t4, t3, *(w+ 7), 0x66282193L);
568 12 FF_4(t1, t0, t7, t6, t5, t4, t3, t2, *(w+28), 0x61D809CCL);
569 12 FF_4(t0, t7, t6, t5, t4, t3, t2, t1, *(w+23), 0xFB21A991L);
570  
571 12 FF_4(t7, t6, t5, t4, t3, t2, t1, t0, *(w+26), 0x487CAC60L);
572 12 FF_4(t6, t5, t4, t3, t2, t1, t0, t7, *(w+ 6), 0x5DEC8032L);
573 12 FF_4(t5, t4, t3, t2, t1, t0, t7, t6, *(w+30), 0xEF845D5DL);
574 12 FF_4(t4, t3, t2, t1, t0, t7, t6, t5, *(w+20), 0xE98575B1L);
575 12 FF_4(t3, t2, t1, t0, t7, t6, t5, t4, *(w+18), 0xDC262302L);
576 12 FF_4(t2, t1, t0, t7, t6, t5, t4, t3, *(w+25), 0xEB651B88L);
577 12 FF_4(t1, t0, t7, t6, t5, t4, t3, t2, *(w+19), 0x23893E81L);
578 12 FF_4(t0, t7, t6, t5, t4, t3, t2, t1, *(w+ 3), 0xD396ACC5L);
579  
580 12 FF_4(t7, t6, t5, t4, t3, t2, t1, t0, *(w+22), 0x0F6D6FF3L);
581 12 FF_4(t6, t5, t4, t3, t2, t1, t0, t7, *(w+11), 0x83F44239L);
582 12 FF_4(t5, t4, t3, t2, t1, t0, t7, t6, *(w+31), 0x2E0B4482L);
583 12 FF_4(t4, t3, t2, t1, t0, t7, t6, t5, *(w+21), 0xA4842004L);
584 12 FF_4(t3, t2, t1, t0, t7, t6, t5, t4, *(w+ 8), 0x69C8F04AL);
585 12 FF_4(t2, t1, t0, t7, t6, t5, t4, t3, *(w+27), 0x9E1F9B5EL);
586 12 FF_4(t1, t0, t7, t6, t5, t4, t3, t2, *(w+12), 0x21C66842L);
587 12 FF_4(t0, t7, t6, t5, t4, t3, t2, t1, *(w+ 9), 0xF6E96C9AL);
588  
589 12 FF_4(t7, t6, t5, t4, t3, t2, t1, t0, *(w+ 1), 0x670C9C61L);
590 12 FF_4(t6, t5, t4, t3, t2, t1, t0, t7, *(w+29), 0xABD388F0L);
591 12 FF_4(t5, t4, t3, t2, t1, t0, t7, t6, *(w+ 5), 0x6A51A0D2L);
592 12 FF_4(t4, t3, t2, t1, t0, t7, t6, t5, *(w+15), 0xD8542F68L);
593 12 FF_4(t3, t2, t1, t0, t7, t6, t5, t4, *(w+17), 0x960FA728L);
594 12 FF_4(t2, t1, t0, t7, t6, t5, t4, t3, *(w+10), 0xAB5133A3L);
595 12 FF_4(t1, t0, t7, t6, t5, t4, t3, t2, *(w+16), 0x6EEF0B6CL);
596 12 FF_4(t0, t7, t6, t5, t4, t3, t2, t1, *(w+13), 0x137A3BE4L);
597 #endif
598  
599 #if PASS == 5
600 /* Pass 5. executed only when PASS = 5 */
601 12 FF_5(t7, t6, t5, t4, t3, t2, t1, t0, *(w+27), 0xBA3BF050L);
602 12 FF_5(t6, t5, t4, t3, t2, t1, t0, t7, *(w+ 3), 0x7EFB2A98L);
603 12 FF_5(t5, t4, t3, t2, t1, t0, t7, t6, *(w+21), 0xA1F1651DL);
604 12 FF_5(t4, t3, t2, t1, t0, t7, t6, t5, *(w+26), 0x39AF0176L);
605 12 FF_5(t3, t2, t1, t0, t7, t6, t5, t4, *(w+17), 0x66CA593EL);
606 12 FF_5(t2, t1, t0, t7, t6, t5, t4, t3, *(w+11), 0x82430E88L);
607 12 FF_5(t1, t0, t7, t6, t5, t4, t3, t2, *(w+20), 0x8CEE8619L);
608 12 FF_5(t0, t7, t6, t5, t4, t3, t2, t1, *(w+29), 0x456F9FB4L);
609  
610 12 FF_5(t7, t6, t5, t4, t3, t2, t1, t0, *(w+19), 0x7D84A5C3L);
611 12 FF_5(t6, t5, t4, t3, t2, t1, t0, t7, *(w ), 0x3B8B5EBEL);
612 12 FF_5(t5, t4, t3, t2, t1, t0, t7, t6, *(w+12), 0xE06F75D8L);
613 12 FF_5(t4, t3, t2, t1, t0, t7, t6, t5, *(w+ 7), 0x85C12073L);
614 12 FF_5(t3, t2, t1, t0, t7, t6, t5, t4, *(w+13), 0x401A449FL);
615 12 FF_5(t2, t1, t0, t7, t6, t5, t4, t3, *(w+ 8), 0x56C16AA6L);
616 12 FF_5(t1, t0, t7, t6, t5, t4, t3, t2, *(w+31), 0x4ED3AA62L);
617 12 FF_5(t0, t7, t6, t5, t4, t3, t2, t1, *(w+10), 0x363F7706L);
618  
619 12 FF_5(t7, t6, t5, t4, t3, t2, t1, t0, *(w+ 5), 0x1BFEDF72L);
620 12 FF_5(t6, t5, t4, t3, t2, t1, t0, t7, *(w+ 9), 0x429B023DL);
621 12 FF_5(t5, t4, t3, t2, t1, t0, t7, t6, *(w+14), 0x37D0D724L);
622 12 FF_5(t4, t3, t2, t1, t0, t7, t6, t5, *(w+30), 0xD00A1248L);
623 12 FF_5(t3, t2, t1, t0, t7, t6, t5, t4, *(w+18), 0xDB0FEAD3L);
624 12 FF_5(t2, t1, t0, t7, t6, t5, t4, t3, *(w+ 6), 0x49F1C09BL);
625 12 FF_5(t1, t0, t7, t6, t5, t4, t3, t2, *(w+28), 0x075372C9L);
626 12 FF_5(t0, t7, t6, t5, t4, t3, t2, t1, *(w+24), 0x80991B7BL);
627  
628 12 FF_5(t7, t6, t5, t4, t3, t2, t1, t0, *(w+ 2), 0x25D479D8L);
629 12 FF_5(t6, t5, t4, t3, t2, t1, t0, t7, *(w+23), 0xF6E8DEF7L);
630 12 FF_5(t5, t4, t3, t2, t1, t0, t7, t6, *(w+16), 0xE3FE501AL);
631 12 FF_5(t4, t3, t2, t1, t0, t7, t6, t5, *(w+22), 0xB6794C3BL);
632 12 FF_5(t3, t2, t1, t0, t7, t6, t5, t4, *(w+ 4), 0x976CE0BDL);
633 12 FF_5(t2, t1, t0, t7, t6, t5, t4, t3, *(w+ 1), 0x04C006BAL);
634 12 FF_5(t1, t0, t7, t6, t5, t4, t3, t2, *(w+25), 0xC1A94FB6L);
635 12 FF_5(t0, t7, t6, t5, t4, t3, t2, t1, *(w+15), 0x409F60C4L);
636 #endif
637  
638 12 state->fingerprint[0] += t0;
639 12 state->fingerprint[1] += t1;
640 12 state->fingerprint[2] += t2;
641 12 state->fingerprint[3] += t3;
642 12 state->fingerprint[4] += t4;
643 12 state->fingerprint[5] += t5;
644 12 state->fingerprint[6] += t6;
645 12 state->fingerprint[7] += t7;
646 12 }
647  
648 /* tailor the last output */
649 12 static void haval_tailor(haval_state *state)
650 {
651 #if FPTLEN == 128
652 temp = (state->fingerprint[7] & 0x000000FFL) |
653 (state->fingerprint[6] & 0xFF000000L) |
654 (state->fingerprint[5] & 0x00FF0000L) |
655 (state->fingerprint[4] & 0x0000FF00L);
656 state->fingerprint[0] += rotate_right(temp, 8);
657  
658 temp = (state->fingerprint[7] & 0x0000FF00L) |
659 (state->fingerprint[6] & 0x000000FFL) |
660 (state->fingerprint[5] & 0xFF000000L) |
661 (state->fingerprint[4] & 0x00FF0000L);
662 state->fingerprint[1] += rotate_right(temp, 16);
663  
664 temp = (state->fingerprint[7] & 0x00FF0000L) |
665 (state->fingerprint[6] & 0x0000FF00L) |
666 (state->fingerprint[5] & 0x000000FFL) |
667 (state->fingerprint[4] & 0xFF000000L);
668 state->fingerprint[2] += rotate_right(temp, 24);
669  
670 temp = (state->fingerprint[7] & 0xFF000000L) |
671 (state->fingerprint[6] & 0x00FF0000L) |
672 (state->fingerprint[5] & 0x0000FF00L) |
673 (state->fingerprint[4] & 0x000000FFL);
674 state->fingerprint[3] += temp;
675  
676 #elif FPTLEN == 160
677 temp = (state->fingerprint[7] & (haval_word)0x3F) |
678 (state->fingerprint[6] & ((haval_word)0x7F << 25)) |
679 (state->fingerprint[5] & ((haval_word)0x3F << 19));
680 state->fingerprint[0] += rotate_right(temp, 19);
681  
682 temp = (state->fingerprint[7] & ((haval_word)0x3F << 6)) |
683 (state->fingerprint[6] & (haval_word)0x3F) |
684 (state->fingerprint[5] & ((haval_word)0x7F << 25));
685 state->fingerprint[1] += rotate_right(temp, 25);
686  
687 temp = (state->fingerprint[7] & ((haval_word)0x7F << 12)) |
688 (state->fingerprint[6] & ((haval_word)0x3F << 6)) |
689 (state->fingerprint[5] & (haval_word)0x3F);
690 state->fingerprint[2] += temp;
691  
692 temp = (state->fingerprint[7] & ((haval_word)0x3F << 19)) |
693 (state->fingerprint[6] & ((haval_word)0x7F << 12)) |
694 (state->fingerprint[5] & ((haval_word)0x3F << 6));
695 state->fingerprint[3] += temp >> 6;
696  
697 temp = (state->fingerprint[7] & ((haval_word)0x7F << 25)) |
698 (state->fingerprint[6] & ((haval_word)0x3F << 19)) |
699 (state->fingerprint[5] & ((haval_word)0x7F << 12));
700 state->fingerprint[4] += temp >> 12;
701  
702 #elif FPTLEN == 192
703 temp = (state->fingerprint[7] & (haval_word)0x1F) |
704 (state->fingerprint[6] & ((haval_word)0x3F << 26));
705 state->fingerprint[0] += rotate_right(temp, 26);
706  
707 temp = (state->fingerprint[7] & ((haval_word)0x1F << 5)) |
708 (state->fingerprint[6] & (haval_word)0x1F);
709 state->fingerprint[1] += temp;
710  
711 temp = (state->fingerprint[7] & ((haval_word)0x3F << 10)) |
712 (state->fingerprint[6] & ((haval_word)0x1F << 5));
713 state->fingerprint[2] += temp >> 5;
714  
715 temp = (state->fingerprint[7] & ((haval_word)0x1F << 16)) |
716 (state->fingerprint[6] & ((haval_word)0x3F << 10));
717 state->fingerprint[3] += temp >> 10;
718  
719 temp = (state->fingerprint[7] & ((haval_word)0x1F << 21)) |
720 (state->fingerprint[6] & ((haval_word)0x1F << 16));
721 state->fingerprint[4] += temp >> 16;
722  
723 temp = (state->fingerprint[7] & ((haval_word)0x3F << 26)) |
724 (state->fingerprint[6] & ((haval_word)0x1F << 21));
725 state->fingerprint[5] += temp >> 21;
726  
727 #elif FPTLEN == 224
728 state->fingerprint[0] += (state->fingerprint[7] >> 27) & 0x1F;
729 state->fingerprint[1] += (state->fingerprint[7] >> 22) & 0x1F;
730 state->fingerprint[2] += (state->fingerprint[7] >> 18) & 0x0F;
731 state->fingerprint[3] += (state->fingerprint[7] >> 13) & 0x1F;
732 state->fingerprint[4] += (state->fingerprint[7] >> 9) & 0x0F;
733 state->fingerprint[5] += (state->fingerprint[7] >> 4) & 0x1F;
734 state->fingerprint[6] += state->fingerprint[7] & 0x0F;
735 #endif
736 12 }
737  
738 /*#include "haval.h" */
739  
740 /* #define LITTLE_ENDIAN */
741 #define NUMBER_OF_BLOCKS 5000 /* number of test blocks */
742 #define BLOCK_SIZE 5000 /* number of bytes in a block */
743  
744 static void haval_speed (void); /* test the speed of HAVAL */
745 static void haval_cert (void); /* hash test data set */
746 static void haval_print (unsigned char *); /* print a fingerprint */
747 static int little_endian (void); /* test endianity */
748  
749 0 int main (int argc, char *argv[])
750 {
751 int i;
752 unsigned char fingerprint[FPTLEN >> 3];
753  
754 0 ▶ if (argc <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (argc <= 1) { 0 0 0
755 0 haval_stdin (); /* filter */
756 }
757 0 ▶ for (i = 1; i < argc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < argc; i++) { 0 0 0
758 0 ▶ if ((argv[i][0] == '?') || /* show help info */
Branch: 1 branch
BranchTrueFalseTotal
if ((argv[i][0] == '?') || /* show help info */ 0 0 0
759 0 ▶ (argv[i][0] == '-' && argv[i][1] == '?') ||
Branch: 2 branches
BranchTrueFalseTotal
(argv[i][0] == '-' && argv[i][1] == '?') || 0 0 0
(argv[i][0] == '-' && argv[i][1] == '?') || 0 0 0
760 0 ▶ (argv[i][0] == '-' && argv[i][1] == 'h')) {
Branch: 2 branches
BranchTrueFalseTotal
(argv[i][0] == '-' && argv[i][1] == 'h')) { 0 0 0
(argv[i][0] == '-' && argv[i][1] == 'h')) { 0 0 0
761 0 printf (" (none) hash input from stdin\n");
762 0 printf (" ?/-?/-h show help menu\n");
763 0 printf (" -c hash certification data\n");
764 0 printf (" -e test endianity\n");
765 0 printf (" -mstring hash message\n");
766 0 printf (" -s test speed\n");
767 0 printf (" file_name hash file\n");
768 0 ▶ } else if (argv[i][0] == '-' && argv[i][1] == 'm') { /* hash string */
Branch: 2 branches
BranchTrueFalseTotal
} else if (argv[i][0] == '-' && argv[i][1] == 'm') { /* hash string */ 0 0 0
} else if (argv[i][0] == '-' && argv[i][1] == 'm') { /* hash string */ 0 0 0
769 0 haval_string (argv[i]+2, fingerprint);
770 0 printf ("%s ", argv[i]+2);
771 0 haval_print (fingerprint);
772 0 printf ("\n");
773 0 ▶ } else if (strcmp (argv[i], "-c") == 0) { /* hash test set */
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp (argv[i], "-c") == 0) { /* hash test set */ 0 0 0
774 0 haval_cert ();
775 0 ▶ } else if (strcmp (argv[i], "-s") == 0) { /* test speed */
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp (argv[i], "-s") == 0) { /* test speed */ 0 0 0
776 0 haval_speed ();
777 0 ▶ } else if (strcmp (argv[i], "-e") == 0) { /* test endianity */
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp (argv[i], "-e") == 0) { /* test endianity */ 0 0 0
778 0 ▶ if (little_endian()) {
Branch: 1 branch
BranchTrueFalseTotal
if (little_endian()) { 0 0 0
779 0 printf ("Your machine is little-endian.\n");
780 0 printf ("You may define LITTLE_ENDIAN to speed up processing.\n");
781 } else {
782 0 printf ("Your machine is NOT little-endian.\n");
783 0 printf ("You must NOT define LITTLE_ENDIAN.\n");
784 }
785 } else { /* hash file */
786 0 ▶ if (haval_file (argv[i], fingerprint)) {
Branch: 1 branch
BranchTrueFalseTotal
if (haval_file (argv[i], fingerprint)) { 0 0 0
787 0 printf ("%s can not be opened !\n= ", argv[i]);
788 } else {
789 0 haval_print (fingerprint);
790 0 printf (" %s", argv[i]);
791 0 printf ("\n");
792 }
793 }
794 }
795 0 return (0);
796 }
797  
798 /* test the speed of HAVAL */
799 0 static void haval_speed (void)
800 {
801 haval_state state;
802 unsigned char buff[BLOCK_SIZE];
803 unsigned char fingerprint[FPTLEN >> 3];
804 clock_t clks;
805 double cpu_time;
806 unsigned int i;
807  
808 0 printf ("Test the speed of HAVAL (PASS = %d, FPTLEN = %d bits).\n", PASS, FPTLEN);
809 0 printf ("Hashing %d %d-byte blocks ...\n", NUMBER_OF_BLOCKS, BLOCK_SIZE);
810  
811 /* initialize test block */
812 0 ▶ for (i = 0; i < BLOCK_SIZE; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < BLOCK_SIZE; i++) { 0 0 0
813 0 buff[i] = ~0;
814 }
815  
816 /* reset the clock */
817 0 clock();
818  
819 /* hash */
820 0 haval_start (&state);
821 0 ▶ for (i = 0; i < NUMBER_OF_BLOCKS; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < NUMBER_OF_BLOCKS; i++) { 0 0 0
822 0 haval_hash (&state, buff, BLOCK_SIZE);
823 }
824 0 haval_end (&state, fingerprint);
825  
826 /* get the number of clocks */
827 0 clks = clock();
828 /* get cpu time */
829 0 cpu_time = (double)clks / (double)CLOCKS_PER_SEC;
830  
831 0 ▶ if (cpu_time > 0.0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cpu_time > 0.0) { 0 0 0
832 0 printf ("CPU Time = %3.1f seconds\n", cpu_time);
833 0 printf (" Speed = %4.2f MBPS (megabits/second)\n",
834 0 (NUMBER_OF_BLOCKS * BLOCK_SIZE * 8)/(1.0E6 * cpu_time));
835 } else {
836 0 printf ("not enough blocks !\n");
837 }
838 0 }
839  
840 /* hash a set of certification data and print the results. */
841 0 static void haval_cert (void)
842 {
843 char *str;
844 unsigned char fingerprint[FPTLEN >> 3];
845  
846 0 printf ("\n");
847 0 printf ("HAVAL certification data (PASS=%d, FPTLEN=%d):", PASS, FPTLEN);
848 0 printf ("\n");
849  
850 0 str = "";
851 0 haval_string (str, fingerprint);
852 0 printf ("%s ", str);
853 0 haval_print (fingerprint);
854 0 printf ("\n");
855  
856 0 str = "a";
857 0 haval_string (str, fingerprint);
858 0 printf ("%s ", str);
859 0 haval_print (fingerprint);
860 0 printf ("\n");
861  
862 0 str = "HAVAL";
863 0 haval_string (str, fingerprint);
864 0 printf ("%s ", str);
865 0 haval_print (fingerprint);
866 0 printf ("\n");
867  
868 0 str = "0123456789";
869 0 haval_string (str, fingerprint);
870 0 printf ("%s ", str);
871 0 haval_print (fingerprint);
872 0 printf ("\n");
873  
874 0 str = "abcdefghijklmnopqrstuvwxyz";
875 0 haval_string (str, fingerprint);
876 0 printf ("%s ", str);
877 0 haval_print (fingerprint);
878 0 printf ("\n");
879  
880 0 str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
881 0 haval_string (str, fingerprint);
882 0 printf ("%s ", str);
883 0 haval_print (fingerprint);
884 0 printf ("\n");
885  
886 0 str = "pi.frac";
887 0 ▶ if (haval_file (str, fingerprint)) {
Branch: 1 branch
BranchTrueFalseTotal
if (haval_file (str, fingerprint)) { 0 0 0
888 0 printf ("%s can not be opened !\n", str);
889 } else {
890 0 printf ("%s ", str);
891 0 haval_print (fingerprint);
892 0 printf ("\n");
893 }
894 0 }
895  
896 /* test endianity */
897 0 static int little_endian(void)
898 {
899 unsigned long *wp;
900 0 unsigned char str[4] = {'A', 'B', 'C', 'D'};
901  
902 0 wp = (unsigned long *)str;
903 0 ▶ if (str[0] == (unsigned char)( *wp & 0xFF)) {
Branch: 1 branch
BranchTrueFalseTotal
if (str[0] == (unsigned char)( *wp & 0xFF)) { 0 0 0
904 0 return (1); /* little endian */
905 } else {
906 0 return (0); /* big endian */
907 }
908 }
909  
910 /* print a fingerprint in hexadecimal */
911 0 static void haval_print (unsigned char fingerprint[FPTLEN >> 3])
912 {
913 int i;
914  
915 0 ▶ for (i = 0; i < FPTLEN >> 3; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < FPTLEN >> 3; i++) { 0 0 0
916 0 printf ("%02x", fingerprint[i]);
917 }
918 0 }
919  
« blib/lib/Digest/Haval256.pm ↑ Summary Haval256.xs »