LCOV - code coverage report
Current view: top level - tests - hashtest.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 32 211 15.2 %
Date: 2015-11-05 17:08:00 Functions: 2 10 20.0 %

          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             :   { 0 }
     106             : };
     107             : 
     108             : 
     109             : 
     110             : static void
     111           0 : die (const char *format, ...)
     112             : {
     113             :   va_list arg_ptr ;
     114             : 
     115           0 :   fflush (stdout);
     116           0 :   fprintf (stderr, "%s: ", PGM);
     117           0 :   va_start( arg_ptr, format ) ;
     118           0 :   vfprintf (stderr, format, arg_ptr );
     119           0 :   va_end(arg_ptr);
     120           0 :   if (*format && format[strlen(format)-1] != '\n')
     121           0 :     putc ('\n', stderr);
     122           0 :   exit (1);
     123             : }
     124             : 
     125             : static void
     126           0 : fail (const char *format, ...)
     127             : {
     128             :   va_list arg_ptr;
     129             : 
     130           0 :   fflush (stdout);
     131           0 :   fprintf (stderr, "%s: ", PGM);
     132             :   /* if (wherestr) */
     133             :   /*   fprintf (stderr, "%s: ", wherestr); */
     134           0 :   va_start (arg_ptr, format);
     135           0 :   vfprintf (stderr, format, arg_ptr);
     136           0 :   va_end (arg_ptr);
     137           0 :   if (*format && format[strlen(format)-1] != '\n')
     138           0 :     putc ('\n', stderr);
     139           0 :   error_count++;
     140           0 :   if (error_count >= 50)
     141           0 :     die ("stopped after 50 errors.");
     142           0 : }
     143             : 
     144             : static void
     145           0 : show (const char *format, ...)
     146             : {
     147             :   va_list arg_ptr;
     148             : 
     149           0 :   fprintf (stderr, "%s: ", PGM);
     150           0 :   va_start (arg_ptr, format);
     151           0 :   vfprintf (stderr, format, arg_ptr);
     152           0 :   if (*format && format[strlen(format)-1] != '\n')
     153           0 :     putc ('\n', stderr);
     154           0 :   va_end (arg_ptr);
     155           0 : }
     156             : 
     157             : 
     158             : static void
     159           0 : showhex (const void *buffer, size_t buflen, const char *format, ...)
     160             : {
     161             :   va_list arg_ptr;
     162             :   const unsigned char *s;
     163             : 
     164           0 :   fprintf (stderr, "%s: ", PGM);
     165           0 :   va_start (arg_ptr, format);
     166           0 :   vfprintf (stderr, format, arg_ptr);
     167           0 :   va_end (arg_ptr);
     168             : 
     169           0 :   for (s=buffer; buflen; buflen--, s++)
     170           0 :     fprintf (stderr, "%02x", *s);
     171           0 :   putc ('\n', stderr);
     172           0 : }
     173             : 
     174             : 
     175             : static void
     176           0 : show_note (const char *format, ...)
     177             : {
     178             :   va_list arg_ptr;
     179             : 
     180           0 :   if (!verbose && getenv ("srcdir"))
     181           0 :     fputs ("      ", stderr);  /* To align above "PASS: ".  */
     182             :   else
     183           0 :     fprintf (stderr, "%s: ", PGM);
     184           0 :   va_start (arg_ptr, format);
     185           0 :   vfprintf (stderr, format, arg_ptr);
     186           0 :   if (*format && format[strlen(format)-1] != '\n')
     187           0 :     putc ('\n', stderr);
     188           0 :   va_end (arg_ptr);
     189           0 : }
     190             : 
     191             : /* Convert STRING consisting of hex characters into its binary
     192             :    representation and return it as an allocated buffer. The valid
     193             :    length of the buffer is returned at R_LENGTH.  The string is
     194             :    delimited by end of string.  The function returns NULL on
     195             :    error.  */
     196             : static void *
     197           0 : hex2buffer (const char *string, size_t *r_length)
     198             : {
     199             :   const char *s;
     200             :   unsigned char *buffer;
     201             :   size_t length;
     202             : 
     203           0 :   buffer = xmalloc (strlen(string)/2+1);
     204           0 :   length = 0;
     205           0 :   for (s=string; *s; s +=2 )
     206             :     {
     207           0 :       if (!hexdigitp (s) || !hexdigitp (s+1))
     208           0 :         return NULL;           /* Invalid hex digits. */
     209           0 :       ((unsigned char*)buffer)[length++] = xtoi_2 (s);
     210             :     }
     211           0 :   *r_length = length;
     212           0 :   return buffer;
     213             : }
     214             : 
     215             : 
     216             : static void
     217          25 : run_selftest (int algo)
     218             : {
     219             :   gpg_error_t err;
     220             :   size_t n;
     221             : 
     222          25 :   n = 1;
     223          25 :   err = gcry_md_algo_info (algo, GCRYCTL_SELFTEST, NULL, &n);
     224          25 :   if (err && gpg_err_code (err) != GPG_ERR_NOT_IMPLEMENTED)
     225           0 :     fail ("extended selftest for %s (%d) failed: %s",
     226             :           gcry_md_algo_name (algo), algo, gpg_strerror (err));
     227          25 :   else if (err && verbose)
     228           0 :     show ("extended selftest for %s (%d) not implemented",
     229             :           gcry_md_algo_name (algo), algo);
     230          25 :   else if (verbose)
     231           0 :     show ("extended selftest for %s (%d) passed",
     232             :           gcry_md_algo_name (algo), algo);
     233          25 : }
     234             : 
     235             : /* Compare DIGEST of length DIGESTLEN generated using ALGO and GIGS
     236             :    plus BYTES with the test vector and print an error message if the
     237             :    don't match.  Return 0 on match.  */
     238             : static int
     239           0 : cmp_digest (const unsigned char *digest, size_t digestlen,
     240             :             int algo, int gigs, int bytes)
     241             : {
     242             :   int idx;
     243             :   unsigned char *tv_digest;
     244           0 :   size_t tv_digestlen = 0;
     245             : 
     246           0 :   for (idx=0; testvectors[idx].algo; idx++)
     247             :     {
     248           0 :       if (testvectors[idx].algo == algo
     249           0 :           && testvectors[idx].gigs == gigs
     250           0 :           && testvectors[idx].bytes == bytes)
     251           0 :         break;
     252             :     }
     253           0 :   if (!testvectors[idx].algo)
     254             :     {
     255           0 :       show ("%d GiB %+3d %-10s warning: %s",
     256             :             gigs, bytes, gcry_md_algo_name (algo), "no test vector");
     257           0 :       missing_test_vectors++;
     258           0 :       return 1;
     259             :     }
     260             : 
     261           0 :   tv_digest = hex2buffer (testvectors[idx].hex, &tv_digestlen);
     262           0 :   if (tv_digestlen != digestlen) /* Ooops.  */
     263             :     {
     264           0 :       fail ("%d GiB %+3d %-10s error: %s",
     265             :             gigs, bytes, gcry_md_algo_name (algo), "digest length mismatch");
     266           0 :       xfree (tv_digest);
     267           0 :       return 1;
     268             :     }
     269           0 :   if (memcmp (tv_digest, digest, tv_digestlen))
     270             :     {
     271           0 :       fail ("%d GiB %+3d %-10s error: %s",
     272             :             gigs, bytes, gcry_md_algo_name (algo), "mismatch");
     273           0 :       xfree (tv_digest);
     274           0 :       return 1;
     275             :     }
     276           0 :   xfree (tv_digest);
     277             : 
     278           0 :   return 0;
     279             : }
     280             : 
     281             : 
     282             : static void
     283           0 : run_longtest (int algo, int gigs)
     284             : {
     285             :   gpg_error_t err;
     286             :   gcry_md_hd_t hd;
     287           0 :   gcry_md_hd_t hd_pre = NULL;
     288           0 :   gcry_md_hd_t hd_pre2 = NULL;
     289           0 :   gcry_md_hd_t hd_post = NULL;
     290           0 :   gcry_md_hd_t hd_post2 = NULL;
     291             :   char pattern[1024];
     292             :   int i, g;
     293             :   const unsigned char *digest;
     294             :   unsigned int digestlen;
     295             : 
     296           0 :   memset (pattern, 'a', sizeof pattern);
     297             : 
     298           0 :   err = gcry_md_open (&hd, algo, 0);
     299           0 :   if (err)
     300             :     {
     301           0 :       fail ("gcry_md_open failed for %s (%d): %s",
     302             :             gcry_md_algo_name (algo), algo, gpg_strerror (err));
     303           0 :       return;
     304             :     }
     305             : 
     306           0 :   digestlen = gcry_md_get_algo_dlen (algo);
     307             : 
     308             : 
     309           0 :   for (g=0; g < gigs; g++)
     310             :     {
     311           0 :       if (g == gigs - 1)
     312             :         {
     313           0 :           for (i = 0; i < 1024*1023; i++)
     314           0 :             gcry_md_write (hd, pattern, sizeof pattern);
     315           0 :           for (i = 0; i < 1023; i++)
     316           0 :             gcry_md_write (hd, pattern, sizeof pattern);
     317           0 :           err = gcry_md_copy (&hd_pre, hd);
     318           0 :           if (!err)
     319           0 :             err = gcry_md_copy (&hd_pre2, hd);
     320           0 :           if (err)
     321           0 :             die ("gcry_md_copy failed for %s (%d): %s",
     322             :                  gcry_md_algo_name (algo), algo, gpg_strerror (err));
     323           0 :           gcry_md_write (hd, pattern, sizeof pattern);
     324             :         }
     325             :       else
     326             :         {
     327           0 :           for (i = 0; i < 1024*1024; i++)
     328           0 :             gcry_md_write (hd, pattern, sizeof pattern);
     329             :         }
     330           0 :       if (g && !(g % 16))
     331           0 :         show_note ("%d GiB so far hashed with %s", g, gcry_md_algo_name (algo));
     332             :     }
     333           0 :   if (g >= 16)
     334           0 :     show_note ("%d GiB hashed with %s", g, gcry_md_algo_name (algo));
     335             : 
     336           0 :   err = gcry_md_copy (&hd_post, hd);
     337           0 :   if (err)
     338           0 :     die ("gcry_md_copy failed for %s (%d): %s",
     339             :          gcry_md_algo_name (algo), algo, gpg_strerror (err));
     340           0 :   err = gcry_md_copy (&hd_post2, hd);
     341           0 :   if (err)
     342           0 :     die ("gcry_md_copy failed for %s (%d): %s",
     343             :          gcry_md_algo_name (algo), algo, gpg_strerror (err));
     344             : 
     345           0 :   gcry_md_write (hd_pre2, pattern, sizeof pattern - 64);
     346           0 :   gcry_md_write (hd_pre, pattern, sizeof pattern - 1);
     347           0 :   gcry_md_write (hd_post, pattern, 1);
     348           0 :   gcry_md_write (hd_post2, pattern, 64);
     349             : 
     350           0 :   digest = gcry_md_read (hd_pre2, algo);
     351           0 :   if (cmp_digest (digest, digestlen, algo, gigs, -64) || verbose)
     352           0 :     showhex (digest, digestlen, "%d GiB %+3d %-10s ",
     353             :              gigs, -64, gcry_md_algo_name (algo));
     354           0 :   digest = gcry_md_read (hd_pre, algo);
     355           0 :   if (cmp_digest (digest, digestlen, algo, gigs, -1) || verbose)
     356           0 :     showhex (digest, digestlen, "%d GiB %+3d %-10s ",
     357             :              gigs, -1, gcry_md_algo_name (algo));
     358           0 :   digest = gcry_md_read (hd, algo);
     359           0 :   if (cmp_digest (digest, digestlen, algo, gigs, 0) || verbose)
     360           0 :     showhex (digest, digestlen, "%d GiB %+3d %-10s ",
     361             :              gigs, 0, gcry_md_algo_name (algo));
     362           0 :   digest = gcry_md_read (hd_post, algo);
     363           0 :   if (cmp_digest (digest, digestlen, algo, gigs, 1) || verbose)
     364           0 :     showhex (digest, digestlen, "%d GiB %+3d %-10s ",
     365             :              gigs, 1, gcry_md_algo_name (algo));
     366           0 :   digest = gcry_md_read (hd_post2, 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             : 
     371           0 :   gcry_md_close (hd);
     372           0 :   gcry_md_close (hd_pre);
     373           0 :   gcry_md_close (hd_pre2);
     374           0 :   gcry_md_close (hd_post);
     375           0 :   gcry_md_close (hd_post2);
     376             : }
     377             : 
     378             : 
     379             : int
     380           1 : main (int argc, char **argv)
     381             : {
     382           1 :   int last_argc = -1;
     383           1 :   int gigs = 0;
     384           1 :   int algo = 0;
     385             :   int idx;
     386             : 
     387           1 :   if (argc)
     388           1 :     { argc--; argv++; }
     389             : 
     390           2 :   while (argc && last_argc != argc )
     391             :     {
     392           0 :       last_argc = argc;
     393           0 :       if (!strcmp (*argv, "--"))
     394             :         {
     395           0 :           argc--; argv++;
     396           0 :           break;
     397             :         }
     398           0 :       else if (!strcmp (*argv, "--help"))
     399             :         {
     400           0 :           fputs ("usage: " PGM " [options] [algos]\n"
     401             :                  "Options:\n"
     402             :                  "  --verbose       print timings etc.\n"
     403             :                  "  --debug         flyswatter\n"
     404             :                  "  --gigs N        Run a test on N GiB\n",
     405             :                  stdout);
     406           0 :           exit (0);
     407             :         }
     408           0 :       else if (!strcmp (*argv, "--verbose"))
     409             :         {
     410           0 :           verbose++;
     411           0 :           argc--; argv++;
     412             :         }
     413           0 :       else if (!strcmp (*argv, "--debug"))
     414             :         {
     415           0 :           verbose += 2;
     416           0 :           debug++;
     417           0 :           argc--; argv++;
     418             :         }
     419           0 :       else if (!strcmp (*argv, "--gigs"))
     420             :         {
     421           0 :           argc--; argv++;
     422           0 :           if (argc)
     423             :             {
     424           0 :               gigs = atoi (*argv);
     425           0 :               argc--; argv++;
     426             :             }
     427             :         }
     428           0 :       else if (!strncmp (*argv, "--", 2))
     429           0 :         die ("unknown option '%s'", *argv);
     430             :     }
     431             : 
     432           1 :   if (gigs < 0 || gigs > 1024*1024)
     433           0 :     die ("value for --gigs must be in the range 0 to %d", 1024*1024);
     434             : 
     435           1 :   gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
     436           1 :   if (!gcry_check_version (GCRYPT_VERSION))
     437           0 :     die ("version mismatch\n");
     438           1 :   if (debug)
     439           0 :     gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u , 0);
     440           1 :   gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
     441           1 :   gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
     442             : 
     443             :   /* A quick check that all given algorithms are valid.  */
     444           1 :   for (idx=0; idx < argc; idx++)
     445             :     {
     446           0 :       algo = gcry_md_map_name (argv[idx]);
     447           0 :       if (!algo)
     448           0 :         fail ("invalid algorithm '%s'", argv[idx]);
     449             :     }
     450           1 :   if (error_count)
     451           0 :     exit (1);
     452             : 
     453             :   /* Start checking.  */
     454           1 :   start_timer ();
     455           1 :   if (!argc)
     456             :     {
     457         400 :       for (algo=1; algo < 400; algo++)
     458         399 :         if (!gcry_md_test_algo (algo))
     459             :           {
     460          25 :             if (!gigs)
     461          25 :               run_selftest (algo);
     462             :             else
     463           0 :               run_longtest (algo, gigs);
     464             :           }
     465             :      }
     466             :   else
     467             :     {
     468           0 :       for (idx=0; idx < argc; idx++)
     469             :         {
     470           0 :           algo = gcry_md_map_name (argv[idx]);
     471           0 :           if (!algo)
     472           0 :             die ("invalid algorithm '%s'", argv[idx]);
     473             : 
     474           0 :           if (!gigs)
     475           0 :             run_selftest (algo);
     476             :           else
     477           0 :             run_longtest (algo, gigs);
     478             :         }
     479             :     }
     480           1 :   stop_timer ();
     481             : 
     482           1 :   if (missing_test_vectors)
     483           0 :     fail ("Some test vectors are missing");
     484             : 
     485           1 :   if (verbose)
     486           0 :     show ("All tests completed in %s.  Errors: %d\n",
     487             :           elapsed_time (1), error_count);
     488           1 :   return !!error_count;
     489             : }

Generated by: LCOV version 1.11