LCOV - code coverage report
Current view: top level - tests - hashtest.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 32 182 17.6 %
Date: 2016-12-15 12:59:22 Functions: 2 7 28.6 %

          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             : #include "t-common.h"
      36             : 
      37             : static int missing_test_vectors;
      38             : 
      39             : static struct {
      40             :   int algo;
      41             :   int gigs;
      42             :   int bytes;
      43             :   const char *hex;
      44             : } testvectors[] = {
      45             :   { GCRY_MD_SHA1, 256, -64, "92fc51850c7b750e6e774b75f294f6979d4059f0" },
      46             :   { GCRY_MD_SHA1, 256,  -1, "4bddeeb4c08683f02d4944d93dbcb02ebab50134" },
      47             :   { GCRY_MD_SHA1, 256,  -0, "71b923afde1c8c040884c723a2e3335b333e64c6" },
      48             :   { GCRY_MD_SHA1, 256,   1, "2d99f9b5b86e9c9c937104f4242bd6b8bc0927ef" },
      49             :   { GCRY_MD_SHA1, 256,  64, "a60dabe8d749f798b7ec3a684cc3eab487451482" },
      50             : 
      51             :   { GCRY_MD_SHA224, 256, -64,
      52             :     "b5672b54d2480a5688a2dc727a1ad4db7a81ef31ce8999e0bbaeffdc" },
      53             :   { GCRY_MD_SHA224, 256,  -1,
      54             :     "814ea7159473e6ffc1c64b90026a542e13ac6980f7f3ca3c4582a9b8" },
      55             :   { GCRY_MD_SHA224, 256,   0,
      56             :     "9ec0e1829455db8650ec7a8b06912196f97a7358bc3a73c79911cd4e" },
      57             :   { GCRY_MD_SHA224, 256,   1,
      58             :     "e578d5d523320876565bbbc892511a485427caee6dd754d57e3e58c2" },
      59             :   { GCRY_MD_SHA224, 256,  64,
      60             :     "ff0464df248cd298b63765bc4f87f21e25c93c657fdf3656d3c878e5" },
      61             : 
      62             :   { GCRY_MD_SHA256, 256, -64,
      63             :     "87a9828d3de78d55d252341db2a622908c4e0ceaee9961ecf9768700fc799ec8" },
      64             :   { GCRY_MD_SHA256, 256,  -1,
      65             :     "823bf95f64ef04a4a77579c38760b1d401b56bf3a8e664bdf56ca15afb468a03" },
      66             :   { GCRY_MD_SHA256, 256,   0,
      67             :     "2d0723878cb2c3d5c59dfad910cdb857f4430a6ba2a7d687938d7a20e63dde47" },
      68             :   { GCRY_MD_SHA256, 256,   1,
      69             :     "5a2e21b1e79cd866acf53a2a18ca76bd4e02c4b01bf4627354171824c812d95f" },
      70             :   { GCRY_MD_SHA256, 256,  64,
      71             :     "34444808af8e9d995e67f9e155ed94bf55f195a51dc1d8a989e6bcf95511c8a2" },
      72             : 
      73             :   { GCRY_MD_SHA512, 256, -64,
      74             :     "e01bf8140874bf240e8426cb2bcbc377cbed2e6037334116637149e1cd8cd462"
      75             :     "96828b71f32b9f002771d4cb51172ce578b73b7939221e4df655ecd08601e655" },
      76             :   { GCRY_MD_SHA512, 256,  -1,
      77             :     "4917ff94514b1757705c289fdc3e7d6ffcce5771b20ae237ebc03d2ec9eb435f"
      78             :     "b7ce9f0e27272be8cced77a5edae1a01a0ad62b0a44169d88bbee45474a17734" },
      79             :   { GCRY_MD_SHA512, 256,   0,
      80             :     "1e28e8b3c79f2f47da11f3c0b7da4e7981e7d932db6d17d528a31e191922edda"
      81             :     "8fc4bb2df10ea876232db5a1c606bc41886e8b2c570a3e721221f60c8c7dc4ab" },
      82             :   { GCRY_MD_SHA512, 256,   1,
      83             :     "027d3324dd1cf127770ceb53681f4c70937c9bca4e3acd5fd76cb266c7d4527d"
      84             :     "58140290a1822e8d60c4d3ae9725fb923183230d6dfd2d7d73c0d74a4757f34a" },
      85             :   { GCRY_MD_SHA512, 256,  64,
      86             :     "49920704ea9d6ee19f0742d6c868110fa3eda8ac09f026e9ef22cc731af53020"
      87             :     "de40eedef66cb1afd94c61e285fa9327e01336e804903740a9145ab1f065c2d5" },
      88             : 
      89             :   { GCRY_MD_SHA3_512, 256, -64,
      90             :     "c6e082b3db996dbe5f2c5709818a7f325ef4febd883d7e9c545c06bfa7225198"
      91             :     "1ecf40103788913cd5a5bdf13246b952ded6651043684b24197eb23544882a97" },
      92             :   { GCRY_MD_SHA3_512, 256,  -1,
      93             :     "d7bf28e8216bf7d3d0d3969e34078e94b98598e17b6f21f256379389e4eba8ee"
      94             :     "74eb288774797263fec00bdfd357d132cea9e408be36b982f5a60ab56ad01613" },
      95             :   { GCRY_MD_SHA3_512, 256,  +0,
      96             :     "c1270852ba7b1e1a3eaa777969b8a65be28c3894537c61eb8cd22b1df6af703d"
      97             :     "b59939f6adadeb64317faece8167d4817e73daf73e28a5ccd26bebee0a35c322" },
      98             :   { GCRY_MD_SHA3_512, 256,  +1,
      99             :     "8bdfeb3a1a9a1cdcef21172cbc5bb3b87c0d8f7111df0aaf7f1bc03ad4775bd6"
     100             :     "a03e0a875c4e7d02d2230c213562c6a57be28d92eaf6e4bea4bc24690454c8ef" },
     101             :   { GCRY_MD_SHA3_512, 256, +64,
     102             :     "0c91b91665ceaf7af5102e0ed31aa4f050668ab3c57b1f4763946d567efe66b3"
     103             :     "ab9a2016cf238dee5b44eae9f0cdfbf7b7a6eb1e759986273243dc35894706b6" },
     104             : 
     105             :   { 0 }
     106             : };
     107             : 
     108             : 
     109             : static void
     110           0 : showhex (const void *buffer, size_t buflen, const char *format, ...)
     111             : {
     112             :   va_list arg_ptr;
     113             :   const unsigned char *s;
     114             : 
     115           0 :   fprintf (stderr, "%s: ", PGM);
     116           0 :   va_start (arg_ptr, format);
     117           0 :   vfprintf (stderr, format, arg_ptr);
     118           0 :   va_end (arg_ptr);
     119             : 
     120           0 :   for (s=buffer; buflen; buflen--, s++)
     121           0 :     fprintf (stderr, "%02x", *s);
     122           0 :   putc ('\n', stderr);
     123           0 : }
     124             : 
     125             : 
     126             : static void
     127           0 : show_note (const char *format, ...)
     128             : {
     129             :   va_list arg_ptr;
     130             : 
     131           0 :   if (!verbose && getenv ("srcdir"))
     132           0 :     fputs ("      ", stderr);  /* To align above "PASS: ".  */
     133             :   else
     134           0 :     fprintf (stderr, "%s: ", PGM);
     135           0 :   va_start (arg_ptr, format);
     136           0 :   vfprintf (stderr, format, arg_ptr);
     137           0 :   if (*format && format[strlen(format)-1] != '\n')
     138           0 :     putc ('\n', stderr);
     139           0 :   va_end (arg_ptr);
     140           0 : }
     141             : 
     142             : /* Convert STRING consisting of hex characters into its binary
     143             :    representation and return it as an allocated buffer. The valid
     144             :    length of the buffer is returned at R_LENGTH.  The string is
     145             :    delimited by end of string.  The function returns NULL on
     146             :    error.  */
     147             : static void *
     148           0 : hex2buffer (const char *string, size_t *r_length)
     149             : {
     150             :   const char *s;
     151             :   unsigned char *buffer;
     152             :   size_t length;
     153             : 
     154           0 :   buffer = xmalloc (strlen(string)/2+1);
     155           0 :   length = 0;
     156           0 :   for (s=string; *s; s +=2 )
     157             :     {
     158           0 :       if (!hexdigitp (s) || !hexdigitp (s+1))
     159           0 :         return NULL;           /* Invalid hex digits. */
     160           0 :       ((unsigned char*)buffer)[length++] = xtoi_2 (s);
     161             :     }
     162           0 :   *r_length = length;
     163           0 :   return buffer;
     164             : }
     165             : 
     166             : 
     167             : static void
     168          25 : run_selftest (int algo)
     169             : {
     170             :   gpg_error_t err;
     171             :   size_t n;
     172             : 
     173          25 :   n = 1;
     174          25 :   err = gcry_md_algo_info (algo, GCRYCTL_SELFTEST, NULL, &n);
     175          25 :   if (err && gpg_err_code (err) != GPG_ERR_NOT_IMPLEMENTED)
     176           0 :     fail ("extended selftest for %s (%d) failed: %s",
     177             :           gcry_md_algo_name (algo), algo, gpg_strerror (err));
     178          25 :   else if (err && verbose)
     179           0 :     info ("extended selftest for %s (%d) not implemented",
     180             :           gcry_md_algo_name (algo), algo);
     181          25 :   else if (verbose)
     182           0 :     info ("extended selftest for %s (%d) passed",
     183             :           gcry_md_algo_name (algo), algo);
     184          25 : }
     185             : 
     186             : /* Compare DIGEST of length DIGESTLEN generated using ALGO and GIGS
     187             :    plus BYTES with the test vector and print an error message if the
     188             :    don't match.  Return 0 on match.  */
     189             : static int
     190           0 : cmp_digest (const unsigned char *digest, size_t digestlen,
     191             :             int algo, int gigs, int bytes)
     192             : {
     193             :   int idx;
     194             :   unsigned char *tv_digest;
     195           0 :   size_t tv_digestlen = 0;
     196             : 
     197           0 :   for (idx=0; testvectors[idx].algo; idx++)
     198             :     {
     199           0 :       if (testvectors[idx].algo == algo
     200           0 :           && testvectors[idx].gigs == gigs
     201           0 :           && testvectors[idx].bytes == bytes)
     202           0 :         break;
     203             :     }
     204           0 :   if (!testvectors[idx].algo)
     205             :     {
     206           0 :       info ("%d GiB %+3d %-10s warning: %s",
     207             :             gigs, bytes, gcry_md_algo_name (algo), "no test vector");
     208           0 :       missing_test_vectors++;
     209           0 :       return 1;
     210             :     }
     211             : 
     212           0 :   tv_digest = hex2buffer (testvectors[idx].hex, &tv_digestlen);
     213           0 :   if (tv_digestlen != digestlen) /* Ooops.  */
     214             :     {
     215           0 :       fail ("%d GiB %+3d %-10s error: %s",
     216             :             gigs, bytes, gcry_md_algo_name (algo), "digest length mismatch");
     217           0 :       xfree (tv_digest);
     218           0 :       return 1;
     219             :     }
     220           0 :   if (memcmp (tv_digest, digest, tv_digestlen))
     221             :     {
     222           0 :       fail ("%d GiB %+3d %-10s error: %s",
     223             :             gigs, bytes, gcry_md_algo_name (algo), "mismatch");
     224           0 :       xfree (tv_digest);
     225           0 :       return 1;
     226             :     }
     227           0 :   xfree (tv_digest);
     228             : 
     229           0 :   return 0;
     230             : }
     231             : 
     232             : 
     233             : static void
     234           0 : run_longtest (int algo, int gigs)
     235             : {
     236             :   gpg_error_t err;
     237             :   gcry_md_hd_t hd;
     238           0 :   gcry_md_hd_t hd_pre = NULL;
     239           0 :   gcry_md_hd_t hd_pre2 = NULL;
     240           0 :   gcry_md_hd_t hd_post = NULL;
     241           0 :   gcry_md_hd_t hd_post2 = NULL;
     242             :   char pattern[1024];
     243             :   int i, g;
     244             :   const unsigned char *digest;
     245             :   unsigned int digestlen;
     246             : 
     247           0 :   memset (pattern, 'a', sizeof pattern);
     248             : 
     249           0 :   err = gcry_md_open (&hd, algo, 0);
     250           0 :   if (err)
     251             :     {
     252           0 :       fail ("gcry_md_open failed for %s (%d): %s",
     253             :             gcry_md_algo_name (algo), algo, gpg_strerror (err));
     254           0 :       return;
     255             :     }
     256             : 
     257           0 :   digestlen = gcry_md_get_algo_dlen (algo);
     258             : 
     259             : 
     260           0 :   for (g=0; g < gigs; g++)
     261             :     {
     262           0 :       if (g == gigs - 1)
     263             :         {
     264           0 :           for (i = 0; i < 1024*1023; i++)
     265           0 :             gcry_md_write (hd, pattern, sizeof pattern);
     266           0 :           for (i = 0; i < 1023; i++)
     267           0 :             gcry_md_write (hd, pattern, sizeof pattern);
     268           0 :           err = gcry_md_copy (&hd_pre, hd);
     269           0 :           if (!err)
     270           0 :             err = gcry_md_copy (&hd_pre2, hd);
     271           0 :           if (err)
     272           0 :             die ("gcry_md_copy failed for %s (%d): %s",
     273             :                  gcry_md_algo_name (algo), algo, gpg_strerror (err));
     274           0 :           gcry_md_write (hd, pattern, sizeof pattern);
     275             :         }
     276             :       else
     277             :         {
     278           0 :           for (i = 0; i < 1024*1024; i++)
     279           0 :             gcry_md_write (hd, pattern, sizeof pattern);
     280             :         }
     281           0 :       if (g && !(g % 16))
     282           0 :         show_note ("%d GiB so far hashed with %s", g, gcry_md_algo_name (algo));
     283             :     }
     284           0 :   if (g >= 16)
     285           0 :     show_note ("%d GiB hashed with %s", g, gcry_md_algo_name (algo));
     286             : 
     287           0 :   err = gcry_md_copy (&hd_post, hd);
     288           0 :   if (err)
     289           0 :     die ("gcry_md_copy failed for %s (%d): %s",
     290             :          gcry_md_algo_name (algo), algo, gpg_strerror (err));
     291           0 :   err = gcry_md_copy (&hd_post2, hd);
     292           0 :   if (err)
     293           0 :     die ("gcry_md_copy failed for %s (%d): %s",
     294             :          gcry_md_algo_name (algo), algo, gpg_strerror (err));
     295             : 
     296           0 :   gcry_md_write (hd_pre2, pattern, sizeof pattern - 64);
     297           0 :   gcry_md_write (hd_pre, pattern, sizeof pattern - 1);
     298           0 :   gcry_md_write (hd_post, pattern, 1);
     299           0 :   gcry_md_write (hd_post2, pattern, 64);
     300             : 
     301           0 :   digest = gcry_md_read (hd_pre2, algo);
     302           0 :   if (cmp_digest (digest, digestlen, algo, gigs, -64) || verbose)
     303           0 :     showhex (digest, digestlen, "%d GiB %+3d %-10s ",
     304             :              gigs, -64, gcry_md_algo_name (algo));
     305           0 :   digest = gcry_md_read (hd_pre, algo);
     306           0 :   if (cmp_digest (digest, digestlen, algo, gigs, -1) || verbose)
     307           0 :     showhex (digest, digestlen, "%d GiB %+3d %-10s ",
     308             :              gigs, -1, gcry_md_algo_name (algo));
     309           0 :   digest = gcry_md_read (hd, algo);
     310           0 :   if (cmp_digest (digest, digestlen, algo, gigs, 0) || verbose)
     311           0 :     showhex (digest, digestlen, "%d GiB %+3d %-10s ",
     312             :              gigs, 0, gcry_md_algo_name (algo));
     313           0 :   digest = gcry_md_read (hd_post, algo);
     314           0 :   if (cmp_digest (digest, digestlen, algo, gigs, 1) || verbose)
     315           0 :     showhex (digest, digestlen, "%d GiB %+3d %-10s ",
     316             :              gigs, 1, gcry_md_algo_name (algo));
     317           0 :   digest = gcry_md_read (hd_post2, algo);
     318           0 :   if (cmp_digest (digest, digestlen, algo, gigs, 64) || verbose)
     319           0 :     showhex (digest, digestlen, "%d GiB %+3d %-10s ",
     320             :              gigs, 64, gcry_md_algo_name (algo));
     321             : 
     322           0 :   gcry_md_close (hd);
     323           0 :   gcry_md_close (hd_pre);
     324           0 :   gcry_md_close (hd_pre2);
     325           0 :   gcry_md_close (hd_post);
     326           0 :   gcry_md_close (hd_post2);
     327             : }
     328             : 
     329             : 
     330             : int
     331           1 : main (int argc, char **argv)
     332             : {
     333           1 :   int last_argc = -1;
     334           1 :   int gigs = 0;
     335           1 :   int algo = 0;
     336             :   int idx;
     337             : 
     338           1 :   if (argc)
     339           1 :     { argc--; argv++; }
     340             : 
     341           2 :   while (argc && last_argc != argc )
     342             :     {
     343           0 :       last_argc = argc;
     344           0 :       if (!strcmp (*argv, "--"))
     345             :         {
     346           0 :           argc--; argv++;
     347           0 :           break;
     348             :         }
     349           0 :       else if (!strcmp (*argv, "--help"))
     350             :         {
     351           0 :           fputs ("usage: " PGM " [options] [algos]\n"
     352             :                  "Options:\n"
     353             :                  "  --verbose       print timings etc.\n"
     354             :                  "  --debug         flyswatter\n"
     355             :                  "  --gigs N        Run a test on N GiB\n",
     356             :                  stdout);
     357           0 :           exit (0);
     358             :         }
     359           0 :       else if (!strcmp (*argv, "--verbose"))
     360             :         {
     361           0 :           verbose++;
     362           0 :           argc--; argv++;
     363             :         }
     364           0 :       else if (!strcmp (*argv, "--debug"))
     365             :         {
     366           0 :           verbose += 2;
     367           0 :           debug++;
     368           0 :           argc--; argv++;
     369             :         }
     370           0 :       else if (!strcmp (*argv, "--gigs"))
     371             :         {
     372           0 :           argc--; argv++;
     373           0 :           if (argc)
     374             :             {
     375           0 :               gigs = atoi (*argv);
     376           0 :               argc--; argv++;
     377             :             }
     378             :         }
     379           0 :       else if (!strncmp (*argv, "--", 2))
     380           0 :         die ("unknown option '%s'", *argv);
     381             :     }
     382             : 
     383           1 :   if (gigs < 0 || gigs > 1024*1024)
     384           0 :     die ("value for --gigs must be in the range 0 to %d", 1024*1024);
     385             : 
     386           1 :   xgcry_control (GCRYCTL_DISABLE_SECMEM, 0);
     387           1 :   if (!gcry_check_version (GCRYPT_VERSION))
     388           0 :     die ("version mismatch\n");
     389           1 :   if (debug)
     390           0 :     xgcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u , 0);
     391           1 :   xgcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
     392           1 :   xgcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
     393             : 
     394             :   /* A quick check that all given algorithms are valid.  */
     395           1 :   for (idx=0; idx < argc; idx++)
     396             :     {
     397           0 :       algo = gcry_md_map_name (argv[idx]);
     398           0 :       if (!algo)
     399           0 :         fail ("invalid algorithm '%s'", argv[idx]);
     400             :     }
     401           1 :   if (error_count)
     402           0 :     exit (1);
     403             : 
     404             :   /* Start checking.  */
     405           1 :   start_timer ();
     406           1 :   if (!argc)
     407             :     {
     408         400 :       for (algo=1; algo < 400; algo++)
     409         399 :         if (!gcry_md_test_algo (algo))
     410             :           {
     411          25 :             if (!gigs)
     412          25 :               run_selftest (algo);
     413             :             else
     414           0 :               run_longtest (algo, gigs);
     415             :           }
     416             :      }
     417             :   else
     418             :     {
     419           0 :       for (idx=0; idx < argc; idx++)
     420             :         {
     421           0 :           algo = gcry_md_map_name (argv[idx]);
     422           0 :           if (!algo)
     423           0 :             die ("invalid algorithm '%s'", argv[idx]);
     424             : 
     425           0 :           if (!gigs)
     426           0 :             run_selftest (algo);
     427             :           else
     428           0 :             run_longtest (algo, gigs);
     429             :         }
     430             :     }
     431           1 :   stop_timer ();
     432             : 
     433           1 :   if (missing_test_vectors)
     434           0 :     fail ("Some test vectors are missing");
     435             : 
     436           1 :   if (verbose)
     437           0 :     info ("All tests completed in %s.  Errors: %d\n",
     438             :           elapsed_time (1), error_count);
     439           1 :   return !!error_count;
     440             : }

Generated by: LCOV version 1.12