1/*
2 *  havaltest.c:  specifies a test program for the HAVAL hashing library.
3 *
4 *  Arguments for the test program:
5 *
6 *      (none)    - hash input from stdin
7 *      ? or -?   - show help menu
8 *      -c        - hash certification data
9 *      -e        - test whether your machine is little-endian
10 *      -mstring  - hash message (string of chars)
11 *      -s        - test speed
12 *      file_name - hash file
13 *
14 *  Makefile for the testing program:
15 *
16 *         CC=acc
17 *         CFLAGS=-fast
18 *
19 *         haval: haval.o havaltest.o
20 *                ${CC} ${CFLAGS} haval.o havaltest.o -o $@
21 *         haval.o havaltest.o: havalapp.h
22 *
23 *         clean:
24 *                /usr/bin/rm -f *.o haval
25 *
26 *  Author:     Yuliang Zheng
27 *              Department of Computer Science
28 *              University of Wollongong
29 *              Wollongong, NSW 2522, Australia
30 *              Email: yuliang@cs.uow.edu.au
31 *              Voice: +61 42 21 4331 (office)
32 *
33 *  Date:       June 1993
34 *
35 *      Copyright (C) 1993 by C^3SR. All rights reserved.
36 *      This program may not be sold or used as inducement to
37 *      buy a product without the written permission of C^3SR.
38 */
39
40#include <stdio.h>
41#include <time.h>
42#include <string.h>
43#include "havalapp.h"
44#include "haval.h"
45
46#define NUMBER_OF_BLOCKS 5000               /* number of test blocks */
47#define BLOCK_SIZE       1000               /* number of bytes in a block */
48
49static void haval_speed (void);             /* test the speed of HAVAL */
50static void haval_cert (void);              /* hash test data set */
51static void haval_print (unsigned char *);  /* print a fingerprint */
52static int  little_endian (void);           /* test endianity */
53
54int main (argc, argv)
55int  argc;
56char *argv[];
57{
58  int           i;
59  unsigned char fingerprint[FPTLEN >> 3];
60
61  if (argc <= 1) {
62    haval_stdin ();                                /* filter */
63  }
64  for (i = 1; i < argc; i++) {
65    if ((argv[i][0] == '?') ||                      /* show help info */
66        (argv[i][0] == '-' && argv[i][1] == '?')) {
67      printf (" (none)     hash input from stdin\n");
68      printf (" ? or -?    show help menu\n");
69      printf (" -c         hash certification data\n");
70      printf (" -e         test endianity\n");
71      printf (" -mstring   hash message\n");
72      printf (" -s         test speed\n");
73      printf (" file_name  hash file\n");
74    } else if (argv[i][0] == '-' && argv[i][1] == 'm') {  /* hash string */
75      haval_string (argv[i]+2, fingerprint);
76      printf ("HAVAL(\"%s\") = ", argv[i]+2);
77      haval_print (fingerprint);
78      printf ("\n");
79    } else if (strcmp (argv[i], "-c") == 0) {      /* hash test set */
80      haval_cert ();
81    } else if (strcmp (argv[i], "-s") == 0) {      /* test speed */
82      haval_speed ();
83    } else if (strcmp (argv[i], "-e") == 0) {      /* test endianity */
84      if (little_endian()) {
85        printf ("Your machine is little-endian.\n");
86        printf ("You may define LITTLE_ENDIAN to speed up processing.\n");
87      } else {
88        printf ("Your machine is NOT little-endian.\n");
89        printf ("You must NOT define LITTLE_ENDIAN.\n");
90      }
91    } else {                                       /* hash file */
92      if (haval_file (argv[i], fingerprint)) {
93        printf ("%s can not be opened !\n= ", argv[i]);
94      } else {
95        printf ("HAVAL(File %s) = ", argv[i]);
96        haval_print (fingerprint);
97        printf ("\n");
98      }
99    }
100  }
101  return (0);
102}
103
104/* test the speed of HAVAL */
105static void haval_speed (void)
106{
107  haval_state   state;
108  unsigned char buff[BLOCK_SIZE];
109  unsigned char fingerprint[FPTLEN >> 3];
110  time_t        start_time, end_time;
111  double        elapsed_time;
112  unsigned int  i;
113
114  printf ("Test the speed of HAVAL (PASS = %d, FPTLEN = %d bits).\n", PASS, FPTLEN);
115  printf ("Hashing %d %d-byte blocks ...\n", NUMBER_OF_BLOCKS, BLOCK_SIZE);
116
117  /* initialize test block */
118  for (i = 0; i < BLOCK_SIZE; i++) {
119    buff[i] = ~0;
120  }
121
122  /* get start time */
123  time (&start_time);
124
125  /* hash */
126  haval_start (&state);
127  for (i = 0; i < NUMBER_OF_BLOCKS; i++) {
128    haval_hash (&state, buff, BLOCK_SIZE);
129  }
130  haval_end (&state, fingerprint);
131
132  /* get end time */
133  time (&end_time);
134
135  /* get elapsed time */
136  elapsed_time = difftime(end_time, start_time);
137
138  if (elapsed_time > 0.0) {
139    printf ("Elapsed Time = %3.1f seconds\n", elapsed_time);
140    printf ("       Speed = %4.2f MBPS (megabits/second)\n",
141    (NUMBER_OF_BLOCKS * BLOCK_SIZE * 8)/(1.0E6 * elapsed_time));
142  } else {
143    printf ("not enough blocks !\n");
144  }
145}
146
147/* hash a set of certification data and print the results.  */
148static void haval_cert (void)
149{
150  unsigned int  i;
151  char          *str;
152  unsigned char fingerprint[FPTLEN >> 3];
153
154  printf ("\n");
155  printf ("HAVAL certification data (PASS=%d, FPTLEN=%d):", PASS, FPTLEN);
156  printf ("\n");
157
158  str = "";
159  haval_string (str, fingerprint);
160  printf ("HAVAL(\"%s\") = ", str);
161  haval_print (fingerprint);
162  printf ("\n");
163
164  str = "a";
165  haval_string (str, fingerprint);
166  printf ("HAVAL(\"%s\") = ", str);
167  haval_print (fingerprint);
168  printf ("\n");
169
170  str = "HAVAL";
171  haval_string (str, fingerprint);
172  printf ("HAVAL(\"%s\") = ", str);
173  haval_print (fingerprint);
174  printf ("\n");
175
176  str = "0123456789";
177  haval_string (str, fingerprint);
178  printf ("HAVAL(\"%s\") = ", str);
179  haval_print (fingerprint);
180  printf ("\n");
181
182  str = "abcdefghijklmnopqrstuvwxyz";
183  haval_string (str, fingerprint);
184  printf ("HAVAL(\"%s\") = ", str);
185  haval_print (fingerprint);
186  printf ("\n");
187
188  str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
189  haval_string (str, fingerprint);
190  printf ("HAVAL(\"%s\")\n      = ", str);
191  haval_print (fingerprint);
192  printf ("\n");
193
194  str = "pi.frac";
195  if (haval_file (str, fingerprint)) {
196    printf ("%s can not be opened !\n", str);
197  } else {
198    printf ("HAVAL(File %s) = ", str);
199    haval_print (fingerprint);
200    printf ("\n");
201  }
202}
203
204/* test endianity */
205static int little_endian(void)
206{
207  unsigned long *wp;
208  unsigned char str[4] = {'A', 'B', 'C', 'D'};
209
210  wp = (unsigned long *)str;
211  if (str[0] == (unsigned char)( *wp & 0xFF)) {
212    return (1);                       /* little endian */
213  } else {
214    return (0);                       /* big endian */
215  }
216}
217
218/* print a fingerprint in hexadecimal */
219static void haval_print (unsigned char fingerprint[FPTLEN >> 3])
220{
221  int i;
222
223  for (i = 0; i < FPTLEN >> 3; i++) {
224    printf ("%02X", fingerprint[i]);
225  }
226}
227
228
229
230
231