Line data Source code
1 : /* hashtest.c - Check the hash fucntions
2 : * Copyright (C) 2013 g10 Code GmbH
3 : *
4 : * This file is part of Libgcrypt.
5 : *
6 : * Libgcrypt is free software; you can redistribute it and/or modify
7 : * it under the terms of the GNU Lesser General Public License as
8 : * published by the Free Software Foundation; either version 2.1 of
9 : * the License, or (at your option) any later version.
10 : *
11 : * Libgcrypt 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 Lesser General Public License for more details.
15 : *
16 : * You should have received a copy of the GNU Lesser General Public
17 : * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18 : */
19 :
20 : #ifdef HAVE_CONFIG_H
21 : #include <config.h>
22 : #endif
23 : #include <stdarg.h>
24 : #include <stdio.h>
25 : #include <ctype.h>
26 : #include <stdlib.h>
27 : #include <string.h>
28 : #include <errno.h>
29 :
30 : #include "../src/gcrypt-int.h"
31 :
32 : #include "stopwatch.h"
33 :
34 : #define PGM "hashtest"
35 :
36 : #define my_isascii(c) (!((c) & 0x80))
37 : #define digitp(p) (*(p) >= '0' && *(p) <= '9')
38 : #define hexdigitp(a) (digitp (a) \
39 : || (*(a) >= 'A' && *(a) <= 'F') \
40 : || (*(a) >= 'a' && *(a) <= 'f'))
41 : #define xtoi_1(p) (*(p) <= '9'? (*(p)- '0'): \
42 : *(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10))
43 : #define xtoi_2(p) ((xtoi_1(p) * 16) + xtoi_1((p)+1))
44 : #define xmalloc(a) gcry_xmalloc ((a))
45 : #define xcalloc(a,b) gcry_xcalloc ((a),(b))
46 : #define xstrdup(a) gcry_xstrdup ((a))
47 : #define xfree(a) gcry_free ((a))
48 : #define pass() do { ; } while (0)
49 :
50 : static int verbose;
51 : static int debug;
52 : static int error_count;
53 : static int missing_test_vectors;
54 :
55 : static struct {
56 : int algo;
57 : int gigs;
58 : int bytes;
59 : const char *hex;
60 : } testvectors[] = {
61 : { GCRY_MD_SHA1, 256, -64, "92fc51850c7b750e6e774b75f294f6979d4059f0" },
62 : { GCRY_MD_SHA1, 256, -1, "4bddeeb4c08683f02d4944d93dbcb02ebab50134" },
63 : { GCRY_MD_SHA1, 256, -0, "71b923afde1c8c040884c723a2e3335b333e64c6" },
64 : { GCRY_MD_SHA1, 256, 1, "2d99f9b5b86e9c9c937104f4242bd6b8bc0927ef" },
65 : { GCRY_MD_SHA1, 256, 64, "a60dabe8d749f798b7ec3a684cc3eab487451482" },
66 :
67 : { GCRY_MD_SHA224, 256, -64,
68 : "b5672b54d2480a5688a2dc727a1ad4db7a81ef31ce8999e0bbaeffdc" },
69 : { GCRY_MD_SHA224, 256, -1,
70 : "814ea7159473e6ffc1c64b90026a542e13ac6980f7f3ca3c4582a9b8" },
71 : { GCRY_MD_SHA224, 256, 0,
72 : "9ec0e1829455db8650ec7a8b06912196f97a7358bc3a73c79911cd4e" },
73 : { GCRY_MD_SHA224, 256, 1,
74 : "e578d5d523320876565bbbc892511a485427caee6dd754d57e3e58c2" },
75 : { GCRY_MD_SHA224, 256, 64,
76 : "ff0464df248cd298b63765bc4f87f21e25c93c657fdf3656d3c878e5" },
77 :
78 : { GCRY_MD_SHA256, 256, -64,
79 : "87a9828d3de78d55d252341db2a622908c4e0ceaee9961ecf9768700fc799ec8" },
80 : { GCRY_MD_SHA256, 256, -1,
81 : "823bf95f64ef04a4a77579c38760b1d401b56bf3a8e664bdf56ca15afb468a03" },
82 : { GCRY_MD_SHA256, 256, 0,
83 : "2d0723878cb2c3d5c59dfad910cdb857f4430a6ba2a7d687938d7a20e63dde47" },
84 : { GCRY_MD_SHA256, 256, 1,
85 : "5a2e21b1e79cd866acf53a2a18ca76bd4e02c4b01bf4627354171824c812d95f" },
86 : { GCRY_MD_SHA256, 256, 64,
87 : "34444808af8e9d995e67f9e155ed94bf55f195a51dc1d8a989e6bcf95511c8a2" },
88 :
89 : { GCRY_MD_SHA512, 256, -64,
90 : "e01bf8140874bf240e8426cb2bcbc377cbed2e6037334116637149e1cd8cd462"
91 : "96828b71f32b9f002771d4cb51172ce578b73b7939221e4df655ecd08601e655" },
92 : { GCRY_MD_SHA512, 256, -1,
93 : "4917ff94514b1757705c289fdc3e7d6ffcce5771b20ae237ebc03d2ec9eb435f"
94 : "b7ce9f0e27272be8cced77a5edae1a01a0ad62b0a44169d88bbee45474a17734" },
95 : { GCRY_MD_SHA512, 256, 0,
96 : "1e28e8b3c79f2f47da11f3c0b7da4e7981e7d932db6d17d528a31e191922edda"
97 : "8fc4bb2df10ea876232db5a1c606bc41886e8b2c570a3e721221f60c8c7dc4ab" },
98 : { GCRY_MD_SHA512, 256, 1,
99 : "027d3324dd1cf127770ceb53681f4c70937c9bca4e3acd5fd76cb266c7d4527d"
100 : "58140290a1822e8d60c4d3ae9725fb923183230d6dfd2d7d73c0d74a4757f34a" },
101 : { GCRY_MD_SHA512, 256, 64,
102 : "49920704ea9d6ee19f0742d6c868110fa3eda8ac09f026e9ef22cc731af53020"
103 : "de40eedef66cb1afd94c61e285fa9327e01336e804903740a9145ab1f065c2d5" },
104 :
105 : { GCRY_MD_SHA3_512, 256, -64,
106 : "c6e082b3db996dbe5f2c5709818a7f325ef4febd883d7e9c545c06bfa7225198"
107 : "1ecf40103788913cd5a5bdf13246b952ded6651043684b24197eb23544882a97" },
108 : { GCRY_MD_SHA3_512, 256, -1,
109 : "d7bf28e8216bf7d3d0d3969e34078e94b98598e17b6f21f256379389e4eba8ee"
110 : "74eb288774797263fec00bdfd357d132cea9e408be36b982f5a60ab56ad01613" },
111 : { GCRY_MD_SHA3_512, 256, +0,
112 : "c1270852ba7b1e1a3eaa777969b8a65be28c3894537c61eb8cd22b1df6af703d"
113 : "b59939f6adadeb64317faece8167d4817e73daf73e28a5ccd26bebee0a35c322" },
114 : { GCRY_MD_SHA3_512, 256, +1,
115 : "8bdfeb3a1a9a1cdcef21172cbc5bb3b87c0d8f7111df0aaf7f1bc03ad4775bd6"
116 : "a03e0a875c4e7d02d2230c213562c6a57be28d92eaf6e4bea4bc24690454c8ef" },
117 : { GCRY_MD_SHA3_512, 256, +64,
118 : "0c91b91665ceaf7af5102e0ed31aa4f050668ab3c57b1f4763946d567efe66b3"
119 : "ab9a2016cf238dee5b44eae9f0cdfbf7b7a6eb1e759986273243dc35894706b6" },
120 :
121 : { 0 }
122 : };
123 :
124 :
125 :
126 : static void
127 0 : die (const char *format, ...)
128 : {
129 : va_list arg_ptr ;
130 :
131 0 : fflush (stdout);
132 0 : fprintf (stderr, "%s: ", PGM);
133 0 : va_start( arg_ptr, format ) ;
134 0 : vfprintf (stderr, format, arg_ptr );
135 0 : va_end(arg_ptr);
136 0 : if (*format && format[strlen(format)-1] != '\n')
137 0 : putc ('\n', stderr);
138 0 : exit (1);
139 : }
140 :
141 : static void
142 0 : fail (const char *format, ...)
143 : {
144 : va_list arg_ptr;
145 :
146 0 : fflush (stdout);
147 0 : fprintf (stderr, "%s: ", PGM);
148 : /* if (wherestr) */
149 : /* fprintf (stderr, "%s: ", wherestr); */
150 0 : va_start (arg_ptr, format);
151 0 : vfprintf (stderr, format, arg_ptr);
152 0 : va_end (arg_ptr);
153 0 : if (*format && format[strlen(format)-1] != '\n')
154 0 : putc ('\n', stderr);
155 0 : error_count++;
156 0 : if (error_count >= 50)
157 0 : die ("stopped after 50 errors.");
158 0 : }
159 :
160 : static void
161 0 : show (const char *format, ...)
162 : {
163 : va_list arg_ptr;
164 :
165 0 : fprintf (stderr, "%s: ", PGM);
166 0 : va_start (arg_ptr, format);
167 0 : vfprintf (stderr, format, arg_ptr);
168 0 : if (*format && format[strlen(format)-1] != '\n')
169 0 : putc ('\n', stderr);
170 0 : va_end (arg_ptr);
171 0 : }
172 :
173 :
174 : static void
175 0 : showhex (const void *buffer, size_t buflen, const char *format, ...)
176 : {
177 : va_list arg_ptr;
178 : const unsigned char *s;
179 :
180 0 : fprintf (stderr, "%s: ", PGM);
181 0 : va_start (arg_ptr, format);
182 0 : vfprintf (stderr, format, arg_ptr);
183 0 : va_end (arg_ptr);
184 :
185 0 : for (s=buffer; buflen; buflen--, s++)
186 0 : fprintf (stderr, "%02x", *s);
187 0 : putc ('\n', stderr);
188 0 : }
189 :
190 :
191 : static void
192 0 : show_note (const char *format, ...)
193 : {
194 : va_list arg_ptr;
195 :
196 0 : if (!verbose && getenv ("srcdir"))
197 0 : fputs (" ", stderr); /* To align above "PASS: ". */
198 : else
199 0 : fprintf (stderr, "%s: ", PGM);
200 0 : va_start (arg_ptr, format);
201 0 : vfprintf (stderr, format, arg_ptr);
202 0 : if (*format && format[strlen(format)-1] != '\n')
203 0 : putc ('\n', stderr);
204 0 : va_end (arg_ptr);
205 0 : }
206 :
207 : /* Convert STRING consisting of hex characters into its binary
208 : representation and return it as an allocated buffer. The valid
209 : length of the buffer is returned at R_LENGTH. The string is
210 : delimited by end of string. The function returns NULL on
211 : error. */
212 : static void *
213 0 : hex2buffer (const char *string, size_t *r_length)
214 : {
215 : const char *s;
216 : unsigned char *buffer;
217 : size_t length;
218 :
219 0 : buffer = xmalloc (strlen(string)/2+1);
220 0 : length = 0;
221 0 : for (s=string; *s; s +=2 )
222 : {
223 0 : if (!hexdigitp (s) || !hexdigitp (s+1))
224 0 : return NULL; /* Invalid hex digits. */
225 0 : ((unsigned char*)buffer)[length++] = xtoi_2 (s);
226 : }
227 0 : *r_length = length;
228 0 : return buffer;
229 : }
230 :
231 :
232 : static void
233 25 : run_selftest (int algo)
234 : {
235 : gpg_error_t err;
236 : size_t n;
237 :
238 25 : n = 1;
239 25 : err = gcry_md_algo_info (algo, GCRYCTL_SELFTEST, NULL, &n);
240 25 : if (err && gpg_err_code (err) != GPG_ERR_NOT_IMPLEMENTED)
241 0 : fail ("extended selftest for %s (%d) failed: %s",
242 : gcry_md_algo_name (algo), algo, gpg_strerror (err));
243 25 : else if (err && verbose)
244 0 : show ("extended selftest for %s (%d) not implemented",
245 : gcry_md_algo_name (algo), algo);
246 25 : else if (verbose)
247 0 : show ("extended selftest for %s (%d) passed",
248 : gcry_md_algo_name (algo), algo);
249 25 : }
250 :
251 : /* Compare DIGEST of length DIGESTLEN generated using ALGO and GIGS
252 : plus BYTES with the test vector and print an error message if the
253 : don't match. Return 0 on match. */
254 : static int
255 0 : cmp_digest (const unsigned char *digest, size_t digestlen,
256 : int algo, int gigs, int bytes)
257 : {
258 : int idx;
259 : unsigned char *tv_digest;
260 0 : size_t tv_digestlen = 0;
261 :
262 0 : for (idx=0; testvectors[idx].algo; idx++)
263 : {
264 0 : if (testvectors[idx].algo == algo
265 0 : && testvectors[idx].gigs == gigs
266 0 : && testvectors[idx].bytes == bytes)
267 0 : break;
268 : }
269 0 : if (!testvectors[idx].algo)
270 : {
271 0 : show ("%d GiB %+3d %-10s warning: %s",
272 : gigs, bytes, gcry_md_algo_name (algo), "no test vector");
273 0 : missing_test_vectors++;
274 0 : return 1;
275 : }
276 :
277 0 : tv_digest = hex2buffer (testvectors[idx].hex, &tv_digestlen);
278 0 : if (tv_digestlen != digestlen) /* Ooops. */
279 : {
280 0 : fail ("%d GiB %+3d %-10s error: %s",
281 : gigs, bytes, gcry_md_algo_name (algo), "digest length mismatch");
282 0 : xfree (tv_digest);
283 0 : return 1;
284 : }
285 0 : if (memcmp (tv_digest, digest, tv_digestlen))
286 : {
287 0 : fail ("%d GiB %+3d %-10s error: %s",
288 : gigs, bytes, gcry_md_algo_name (algo), "mismatch");
289 0 : xfree (tv_digest);
290 0 : return 1;
291 : }
292 0 : xfree (tv_digest);
293 :
294 0 : return 0;
295 : }
296 :
297 :
298 : static void
299 0 : run_longtest (int algo, int gigs)
300 : {
301 : gpg_error_t err;
302 : gcry_md_hd_t hd;
303 0 : gcry_md_hd_t hd_pre = NULL;
304 0 : gcry_md_hd_t hd_pre2 = NULL;
305 0 : gcry_md_hd_t hd_post = NULL;
306 0 : gcry_md_hd_t hd_post2 = NULL;
307 : char pattern[1024];
308 : int i, g;
309 : const unsigned char *digest;
310 : unsigned int digestlen;
311 :
312 0 : memset (pattern, 'a', sizeof pattern);
313 :
314 0 : err = gcry_md_open (&hd, algo, 0);
315 0 : if (err)
316 : {
317 0 : fail ("gcry_md_open failed for %s (%d): %s",
318 : gcry_md_algo_name (algo), algo, gpg_strerror (err));
319 0 : return;
320 : }
321 :
322 0 : digestlen = gcry_md_get_algo_dlen (algo);
323 :
324 :
325 0 : for (g=0; g < gigs; g++)
326 : {
327 0 : if (g == gigs - 1)
328 : {
329 0 : for (i = 0; i < 1024*1023; i++)
330 0 : gcry_md_write (hd, pattern, sizeof pattern);
331 0 : for (i = 0; i < 1023; i++)
332 0 : gcry_md_write (hd, pattern, sizeof pattern);
333 0 : err = gcry_md_copy (&hd_pre, hd);
334 0 : if (!err)
335 0 : err = gcry_md_copy (&hd_pre2, hd);
336 0 : if (err)
337 0 : die ("gcry_md_copy failed for %s (%d): %s",
338 : gcry_md_algo_name (algo), algo, gpg_strerror (err));
339 0 : gcry_md_write (hd, pattern, sizeof pattern);
340 : }
341 : else
342 : {
343 0 : for (i = 0; i < 1024*1024; i++)
344 0 : gcry_md_write (hd, pattern, sizeof pattern);
345 : }
346 0 : if (g && !(g % 16))
347 0 : show_note ("%d GiB so far hashed with %s", g, gcry_md_algo_name (algo));
348 : }
349 0 : if (g >= 16)
350 0 : show_note ("%d GiB hashed with %s", g, gcry_md_algo_name (algo));
351 :
352 0 : err = gcry_md_copy (&hd_post, hd);
353 0 : if (err)
354 0 : die ("gcry_md_copy failed for %s (%d): %s",
355 : gcry_md_algo_name (algo), algo, gpg_strerror (err));
356 0 : err = gcry_md_copy (&hd_post2, hd);
357 0 : if (err)
358 0 : die ("gcry_md_copy failed for %s (%d): %s",
359 : gcry_md_algo_name (algo), algo, gpg_strerror (err));
360 :
361 0 : gcry_md_write (hd_pre2, pattern, sizeof pattern - 64);
362 0 : gcry_md_write (hd_pre, pattern, sizeof pattern - 1);
363 0 : gcry_md_write (hd_post, pattern, 1);
364 0 : gcry_md_write (hd_post2, pattern, 64);
365 :
366 0 : digest = gcry_md_read (hd_pre2, algo);
367 0 : if (cmp_digest (digest, digestlen, algo, gigs, -64) || verbose)
368 0 : showhex (digest, digestlen, "%d GiB %+3d %-10s ",
369 : gigs, -64, gcry_md_algo_name (algo));
370 0 : digest = gcry_md_read (hd_pre, algo);
371 0 : if (cmp_digest (digest, digestlen, algo, gigs, -1) || verbose)
372 0 : showhex (digest, digestlen, "%d GiB %+3d %-10s ",
373 : gigs, -1, gcry_md_algo_name (algo));
374 0 : digest = gcry_md_read (hd, algo);
375 0 : if (cmp_digest (digest, digestlen, algo, gigs, 0) || verbose)
376 0 : showhex (digest, digestlen, "%d GiB %+3d %-10s ",
377 : gigs, 0, gcry_md_algo_name (algo));
378 0 : digest = gcry_md_read (hd_post, algo);
379 0 : if (cmp_digest (digest, digestlen, algo, gigs, 1) || verbose)
380 0 : showhex (digest, digestlen, "%d GiB %+3d %-10s ",
381 : gigs, 1, gcry_md_algo_name (algo));
382 0 : digest = gcry_md_read (hd_post2, algo);
383 0 : if (cmp_digest (digest, digestlen, algo, gigs, 64) || verbose)
384 0 : showhex (digest, digestlen, "%d GiB %+3d %-10s ",
385 : gigs, 64, gcry_md_algo_name (algo));
386 :
387 0 : gcry_md_close (hd);
388 0 : gcry_md_close (hd_pre);
389 0 : gcry_md_close (hd_pre2);
390 0 : gcry_md_close (hd_post);
391 0 : gcry_md_close (hd_post2);
392 : }
393 :
394 :
395 : int
396 1 : main (int argc, char **argv)
397 : {
398 1 : int last_argc = -1;
399 1 : int gigs = 0;
400 1 : int algo = 0;
401 : int idx;
402 :
403 1 : if (argc)
404 1 : { argc--; argv++; }
405 :
406 2 : while (argc && last_argc != argc )
407 : {
408 0 : last_argc = argc;
409 0 : if (!strcmp (*argv, "--"))
410 : {
411 0 : argc--; argv++;
412 0 : break;
413 : }
414 0 : else if (!strcmp (*argv, "--help"))
415 : {
416 0 : fputs ("usage: " PGM " [options] [algos]\n"
417 : "Options:\n"
418 : " --verbose print timings etc.\n"
419 : " --debug flyswatter\n"
420 : " --gigs N Run a test on N GiB\n",
421 : stdout);
422 0 : exit (0);
423 : }
424 0 : else if (!strcmp (*argv, "--verbose"))
425 : {
426 0 : verbose++;
427 0 : argc--; argv++;
428 : }
429 0 : else if (!strcmp (*argv, "--debug"))
430 : {
431 0 : verbose += 2;
432 0 : debug++;
433 0 : argc--; argv++;
434 : }
435 0 : else if (!strcmp (*argv, "--gigs"))
436 : {
437 0 : argc--; argv++;
438 0 : if (argc)
439 : {
440 0 : gigs = atoi (*argv);
441 0 : argc--; argv++;
442 : }
443 : }
444 0 : else if (!strncmp (*argv, "--", 2))
445 0 : die ("unknown option '%s'", *argv);
446 : }
447 :
448 1 : if (gigs < 0 || gigs > 1024*1024)
449 0 : die ("value for --gigs must be in the range 0 to %d", 1024*1024);
450 :
451 1 : gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
452 1 : if (!gcry_check_version (GCRYPT_VERSION))
453 0 : die ("version mismatch\n");
454 1 : if (debug)
455 0 : gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u , 0);
456 1 : gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
457 1 : gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
458 :
459 : /* A quick check that all given algorithms are valid. */
460 1 : for (idx=0; idx < argc; idx++)
461 : {
462 0 : algo = gcry_md_map_name (argv[idx]);
463 0 : if (!algo)
464 0 : fail ("invalid algorithm '%s'", argv[idx]);
465 : }
466 1 : if (error_count)
467 0 : exit (1);
468 :
469 : /* Start checking. */
470 1 : start_timer ();
471 1 : if (!argc)
472 : {
473 400 : for (algo=1; algo < 400; algo++)
474 399 : if (!gcry_md_test_algo (algo))
475 : {
476 25 : if (!gigs)
477 25 : run_selftest (algo);
478 : else
479 0 : run_longtest (algo, gigs);
480 : }
481 : }
482 : else
483 : {
484 0 : for (idx=0; idx < argc; idx++)
485 : {
486 0 : algo = gcry_md_map_name (argv[idx]);
487 0 : if (!algo)
488 0 : die ("invalid algorithm '%s'", argv[idx]);
489 :
490 0 : if (!gigs)
491 0 : run_selftest (algo);
492 : else
493 0 : run_longtest (algo, gigs);
494 : }
495 : }
496 1 : stop_timer ();
497 :
498 1 : if (missing_test_vectors)
499 0 : fail ("Some test vectors are missing");
500 :
501 1 : if (verbose)
502 0 : show ("All tests completed in %s. Errors: %d\n",
503 : elapsed_time (1), error_count);
504 1 : return !!error_count;
505 : }
|