LCOV - code coverage report
Current view: top level - tests - keygrip.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 26 66 39.4 %
Date: 2016-11-29 14:56:30 Functions: 2 5 40.0 %

          Line data    Source code
       1             : /* keygrip.c - verifies that keygrips are calculated as expected
       2             :  *      Copyright (C) 2005 Free Software Foundation, Inc.
       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, write to the Free Software
      18             :  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
      19             :  */
      20             : 
      21             : #ifdef HAVE_CONFIG_H
      22             : #include <config.h>
      23             : #endif
      24             : 
      25             : #include <stdio.h>
      26             : #include <stdlib.h>
      27             : #include <string.h>
      28             : #include <stdarg.h>
      29             : #include <assert.h>
      30             : 
      31             : #include "../src/gcrypt-int.h"
      32             : 
      33             : static int verbose;
      34             : static int repetitions;
      35             : 
      36             : 
      37             : 
      38             : static void
      39           0 : die (const char *format, ...)
      40             : {
      41             :   va_list arg_ptr;
      42             : 
      43           0 :   va_start (arg_ptr, format);
      44           0 :   vfprintf (stderr, format, arg_ptr);
      45           0 :   va_end (arg_ptr);
      46           0 :   exit (1);
      47             : }
      48             : 
      49             : static void
      50           0 : print_hex (const char *text, const void *buf, size_t n)
      51             : {
      52           0 :   const unsigned char *p = buf;
      53             : 
      54           0 :   fputs (text, stdout);
      55           0 :   for (; n; n--, p++)
      56           0 :     printf ("%02X", *p);
      57           0 :   putchar ('\n');
      58           0 : }
      59             : 
      60             : 
      61             : 
      62             : 
      63             : static struct
      64             : {
      65             :   int algo;
      66             :   const char *key;
      67             :   const unsigned char grip[20];
      68             : } key_grips[] =
      69             :   {
      70             :     {
      71             :       GCRY_PK_RSA,
      72             :       "(private-key"
      73             :       " (rsa"
      74             :       "  (n #00B6B509596A9ECABC939212F891E656A626BA07DA8521A9CAD4C08E640C04052FBB87F424EF1A0275A48A9299AC9DB69ABE3D0124E6C756B1F7DFB9B842D6251AEA6EE85390495CADA73D671537FCE5850A932F32BAB60AB1AC1F852C1F83C625E7A7D70CDA9EF16D5C8E47739D77DF59261ABE8454807FF441E143FBD37F8545#)"
      75             :       "  (e #010001#)"
      76             :       "  (d #077AD3DE284245F4806A1B82B79E616FBDE821C82D691A65665E57B5FAD3F34E67F401E7BD2E28699E89D9C496CF821945AE83AC7A1231176A196BA6027E77D85789055D50404A7A2A95B1512F91F190BBAEF730ED550D227D512F89C0CDB31AC06FA9A19503DDF6B66D0B42B9691BFD6140EC1720FFC48AE00C34796DC899E5#)"
      77             :       "  (p #00D586C78E5F1B4BF2E7CD7A04CA091911706F19788B93E44EE20AAF462E8363E98A72253ED845CCBF2481BB351E8557C85BCFFF0DABDBFF8E26A79A0938096F27#)"
      78             :       "  (q #00DB0CDF60F26F2A296C88D6BF9F8E5BE45C0DDD713C96CC73EBCB48B061740943F21D2A93D6E42A7211E7F02A95DCED6C390A67AD21ECF739AE8A0CA46FF2EBB3#)"
      79             :       "  (u #33149195F16912DB20A48D020DBC3B9E3881B39D722BF79378F6340F43148A6E9FC5F53E2853B7387BA4443BA53A52FCA8173DE6E85B42F9783D4A7817D0680B#)))",
      80             :       "\x32\xCF\xFA\x85\xB1\x79\x1F\xBB\x26\x14\xE9\x1A\xFD\xF3\xAF\xE3\x32\x08\x2E\x25"
      81             :     },
      82             :     {
      83             :       GCRY_PK_DSA,
      84             :       " (public-key"
      85             :       " (dsa"
      86             :       "  (p #0084E4C626E16005770BD9509ABF7354492E85B8C0060EFAAAEC617F725B592FAA59DF5460575F41022776A9718CE62EDD542AB73C7720869EBDBC834D174ADCD7136827DF51E2613545A25CA573BC502A61B809000B6E35F5EB7FD6F18C35678C23EA1C3638FB9CFDBA2800EE1B62F41A4479DE824F2834666FBF8DC5B53C2617#)"
      87             :       "  (q #00B0E6F710051002A9F425D98A677B18E0E5B038AB#)"
      88             :       "  (g #44370CEE0FE8609994183DBFEBA7EEA97D466838BCF65EFF506E35616DA93FA4E572A2F08886B74977BC00CA8CD3DBEA7AEB7DB8CBB180E6975E0D2CA76E023E6DE9F8CCD8826EBA2F72B8516532F6001DEFFAE76AA5E59E0FA33DBA3999B4E92D1703098CDEDCC416CF008801964084CDE1980132B2B78CB4CE9C15A559528B#)"
      89             :       "  (y #3D5DD14AFA2BF24A791E285B90232213D0E3BA74AB1109E768AED19639A322F84BB7D959E2BA92EF73DE4C7F381AA9F4053CFA3CD4527EF9043E304E5B95ED0A3A5A9D590AA641C13DB2B6E32B9B964A6A2C730DD3EA7C8E13F7A140AFF1A91CE375E9B9B960384779DC4EA180FA1F827C52288F366C0770A220F50D6D8FD6F6#)))",
      90             :       "\x04\xA3\x4F\xA0\x2B\x03\x94\xD7\x32\xAD\xD5\x9B\x50\xAF\xDB\x5D\x57\x22\xA6\x10"
      91             : 
      92             :     },
      93             :     {
      94             :       GCRY_PK_DSA,
      95             :       "(private-key"
      96             :       " (dsa"
      97             :       "  (p #0084E4C626E16005770BD9509ABF7354492E85B8C0060EFAAAEC617F725B592FAA59DF5460575F41022776A9718CE62EDD542AB73C7720869EBDBC834D174ADCD7136827DF51E2613545A25CA573BC502A61B809000B6E35F5EB7FD6F18C35678C23EA1C3638FB9CFDBA2800EE1B62F41A4479DE824F2834666FBF8DC5B53C2617#)"
      98             :       "  (q #00B0E6F710051002A9F425D98A677B18E0E5B038AB#)"
      99             :       "  (g #44370CEE0FE8609994183DBFEBA7EEA97D466838BCF65EFF506E35616DA93FA4E572A2F08886B74977BC00CA8CD3DBEA7AEB7DB8CBB180E6975E0D2CA76E023E6DE9F8CCD8826EBA2F72B8516532F6001DEFFAE76AA5E59E0FA33DBA3999B4E92D1703098CDEDCC416CF008801964084CDE1980132B2B78CB4CE9C15A559528B#)"
     100             :       "  (y #3D5DD14AFA2BF24A791E285B90232213D0E3BA74AB1109E768AED19639A322F84BB7D959E2BA92EF73DE4C7F381AA9F4053CFA3CD4527EF9043E304E5B95ED0A3A5A9D590AA641C13DB2B6E32B9B964A6A2C730DD3EA7C8E13F7A140AFF1A91CE375E9B9B960384779DC4EA180FA1F827C52288F366C0770A220F50D6D8FD6F6#)"
     101             :       "  (x #0087F9E91BFBCC1163DE71ED86D557708E32F8ADDE#)))",
     102             :       "\x04\xA3\x4F\xA0\x2B\x03\x94\xD7\x32\xAD\xD5\x9B\x50\xAF\xDB\x5D\x57\x22\xA6\x10"
     103             :     },
     104             :     {
     105             :       GCRY_PK_ECDSA,
     106             :       "(public-key"
     107             :       " (ecdsa(flags param)"
     108             :       " (p #00FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF#)"
     109             :       " (a #00FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC#)"
     110             :       " (b #5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B#)"
     111             :       " (g #046B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C2964FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5#)"
     112             :       " (n #00FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551#)"
     113             :       " (h #000000000000000000000000000000000000000000000000000000000000000001#)"
     114             :       " (q #04C8A4CEC2E9A9BC8E173531A67B0840DF345C32E261ADD780E6D83D56EFADFD5DE872F8B854819B59543CE0B7F822330464FBC4E6324DADDCD9D059554F63B344#)))",
     115             :       "\xE6\xDF\x94\x2D\xBD\x8C\x77\x05\xA3\xDD\x41\x6E\xFC\x04\x01\xDB\x31\x0E\x99\xB6"
     116             :     },
     117             :     {
     118             :       GCRY_PK_ECDSA,
     119             :       "(public-key"
     120             :       " (ecdsa(flags param)"
     121             :       " (p #00FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF#)"
     122             :       " (curve \"NIST P-256\")"
     123             :       " (b #5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B#)"
     124             :       " (g #046B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C2964FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5#)"
     125             :       " (n #00FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551#)"
     126             :       " (h #000000000000000000000000000000000000000000000000000000000000000001#)"
     127             :       " (q #04C8A4CEC2E9A9BC8E173531A67B0840DF345C32E261ADD780E6D83D56EFADFD5DE872F8B854819B59543CE0B7F822330464FBC4E6324DADDCD9D059554F63B344#)))",
     128             :       "\xE6\xDF\x94\x2D\xBD\x8C\x77\x05\xA3\xDD\x41\x6E\xFC\x04\x01\xDB\x31\x0E\x99\xB6"
     129             :     },
     130             :     {
     131             :       GCRY_PK_ECDSA,
     132             :       "(public-key"
     133             :       " (ecdsa"
     134             :       " (p #00FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF#)"
     135             :       " (curve \"NIST P-256\")"
     136             :       " (b #5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B#)"
     137             :       " (g #046B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C2964FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5#)"
     138             :       " (n #00FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551#)"
     139             :       " (h #000000000000000000000000000000000000000000000000000000000000000001#)"
     140             :       " (q #04C8A4CEC2E9A9BC8E173531A67B0840DF345C32E261ADD780E6D83D56EFADFD5DE872F8B854819B59543CE0B7F822330464FBC4E6324DADDCD9D059554F63B344#)))",
     141             :       "\xE6\xDF\x94\x2D\xBD\x8C\x77\x05\xA3\xDD\x41\x6E\xFC\x04\x01\xDB\x31\x0E\x99\xB6"
     142             :     },
     143             :     {
     144             :       GCRY_PK_ECDSA,
     145             :       "(public-key"
     146             :       " (ecdsa"
     147             :       " (curve secp256r1)"
     148             :       " (q #04C8A4CEC2E9A9BC8E173531A67B0840DF345C32E261ADD780E6D83D56EFADFD5DE872F8B854819B59543CE0B7F822330464FBC4E6324DADDCD9D059554F63B344#)))",
     149             :       "\xE6\xDF\x94\x2D\xBD\x8C\x77\x05\xA3\xDD\x41\x6E\xFC\x04\x01\xDB\x31\x0E\x99\xB6"
     150             :     },
     151             :     {
     152             :       GCRY_PK_ECC,
     153             :       "(public-key"
     154             :       " (ecc"
     155             :       " (curve secp256r1)"
     156             :       " (q #04C8A4CEC2E9A9BC8E173531A67B0840DF345C32E261ADD780E6D83D56EFADFD5DE872F8B854819B59543CE0B7F822330464FBC4E6324DADDCD9D059554F63B344#)))",
     157             :       "\xE6\xDF\x94\x2D\xBD\x8C\x77\x05\xA3\xDD\x41\x6E\xFC\x04\x01\xDB\x31\x0E\x99\xB6"
     158             :     },
     159             :     { /* Ed25519 standard */
     160             :       GCRY_PK_ECC,
     161             :       "(public-key"
     162             :       " (ecc"
     163             :       " (curve Ed25519)"
     164             :       " (q #04"
     165             :       "     1CC662926E7EFF4982B7FB8B928E61CD74CCDD85277CC57196C3AD20B611085F"
     166             :       "     47BD24842905C049257673B3F5249524E0A41FAA17B25B818D0F97E625F1A1D0#)"
     167             :       "     ))",
     168             :       "\x0C\xCA\xB2\xFD\x48\x9A\x33\x40\x2C\xE8"
     169             :       "\xE0\x4A\x1F\xB2\x45\xEA\x80\x3D\x0A\xF1"
     170             :     },
     171             :     { /* Ed25519+EdDSA */
     172             :       GCRY_PK_ECC,
     173             :       "(public-key"
     174             :       " (ecc"
     175             :       " (curve Ed25519)(flags eddsa)"
     176             :       " (q #773E72848C1FD5F9652B29E2E7AF79571A04990E96F2016BF4E0EC1890C2B7DB#)"
     177             :       " ))",
     178             :       "\x9D\xB6\xC6\x4A\x38\x83\x0F\x49\x60\x70"
     179             :       "\x17\x89\x47\x55\x20\xBE\x8C\x82\x1F\x47"
     180             :     },
     181             :     { /* Ed25519+EdDSA (with compression prefix) */
     182             :       GCRY_PK_ECC,
     183             :       "(public-key"
     184             :       " (ecc"
     185             :       " (curve Ed25519)(flags eddsa)"
     186             :       " (q #40"
     187             :       "     773E72848C1FD5F9652B29E2E7AF79571A04990E96F2016BF4E0EC1890C2B7DB#)"
     188             :       " ))",
     189             :       "\x9D\xB6\xC6\x4A\x38\x83\x0F\x49\x60\x70"
     190             :       "\x17\x89\x47\x55\x20\xBE\x8C\x82\x1F\x47"
     191             :     },
     192             :     { /* Ed25519+EdDSA  (same but uncompressed)*/
     193             :       GCRY_PK_ECC,
     194             :       "(public-key"
     195             :       " (ecc"
     196             :       " (curve Ed25519)(flags eddsa)"
     197             :       " (q #04"
     198             :       "     629ad237d1ed04dcd4abe1711dd699a1cf51b1584c4de7a4ef8b8a640180b26f"
     199             :       "     5bb7c29018ece0f46b01f2960e99041a5779afe7e2292b65f9d51f8c84723e77#)"
     200             :       " ))",
     201             :       "\x9D\xB6\xC6\x4A\x38\x83\x0F\x49\x60\x70"
     202             :       "\x17\x89\x47\x55\x20\xBE\x8C\x82\x1F\x47"
     203             :     }
     204             :   };
     205             : 
     206             : 
     207             : static void
     208           1 : check (void)
     209             : {
     210             :   unsigned char buf[20];
     211             :   unsigned char *ret;
     212             :   gcry_error_t err;
     213             :   gcry_sexp_t sexp;
     214             :   unsigned int i;
     215             :   int repn;
     216             : 
     217          13 :   for (i = 0; i < (sizeof (key_grips) / sizeof (*key_grips)); i++)
     218             :     {
     219          12 :       if (gcry_pk_test_algo (key_grips[i].algo))
     220             :         {
     221           0 :           if (verbose)
     222           0 :             fprintf (stderr, "algo %d not available; test skipped\n",
     223             :                      key_grips[i].algo);
     224           0 :           continue;
     225             :         }
     226          12 :       err = gcry_sexp_sscan (&sexp, NULL, key_grips[i].key,
     227             :                              strlen (key_grips[i].key));
     228          12 :       if (err)
     229           0 :         die ("scanning data %d failed: %s\n", i, gpg_strerror (err));
     230             : 
     231          24 :       for (repn=0; repn < repetitions; repn++)
     232             :         {
     233          12 :           ret = gcry_pk_get_keygrip (sexp, buf);
     234          12 :           if (!ret)
     235           0 :             die ("gcry_pk_get_keygrip failed for %d\n", i);
     236             : 
     237          12 :           if ( memcmp (key_grips[i].grip, buf, sizeof (buf)) )
     238             :             {
     239           0 :               print_hex ("keygrip: ", buf, sizeof buf);
     240           0 :               die ("keygrip for %d does not match\n", i);
     241             :             }
     242             :         }
     243             : 
     244          12 :       gcry_sexp_release (sexp);
     245             :     }
     246           1 : }
     247             : 
     248             : 
     249             : 
     250             : static void
     251           0 : progress_handler (void *cb_data, const char *what, int printchar,
     252             :                   int current, int total)
     253             : {
     254             :   (void)cb_data;
     255             :   (void)what;
     256             :   (void)current;
     257             :   (void)total;
     258             : 
     259           0 :   putchar (printchar);
     260           0 : }
     261             : 
     262             : int
     263           1 : main (int argc, char **argv)
     264             : {
     265           1 :   int last_argc = -1;
     266           1 :   int debug = 0;
     267             : 
     268           1 :   if (argc)
     269           1 :     { argc--; argv++; }
     270             : 
     271           2 :   while (argc && last_argc != argc )
     272             :     {
     273           0 :       last_argc = argc;
     274           0 :       if (!strcmp (*argv, "--"))
     275             :         {
     276           0 :           argc--; argv++;
     277           0 :           break;
     278             :         }
     279           0 :       else if (!strcmp (*argv, "--verbose"))
     280             :         {
     281           0 :           verbose = 1;
     282           0 :           argc--; argv++;
     283             :         }
     284           0 :       else if (!strcmp (*argv, "--debug"))
     285             :         {
     286           0 :           verbose = 1;
     287           0 :           debug = 1;
     288           0 :           argc--; argv++;
     289             :         }
     290           0 :       else if (!strcmp (*argv, "--repetitions"))
     291             :         {
     292           0 :           argc--; argv++;
     293           0 :           if (argc)
     294             :             {
     295           0 :               repetitions = atoi(*argv);
     296           0 :               argc--; argv++;
     297             :             }
     298             :         }
     299             :     }
     300             : 
     301           1 :   if (repetitions < 1)
     302           1 :     repetitions = 1;
     303             : 
     304           1 :   if (!gcry_check_version (GCRYPT_VERSION))
     305           0 :     die ("version mismatch\n");
     306             : 
     307           1 :   gcry_set_progress_handler (progress_handler, NULL);
     308             : 
     309           1 :   gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
     310           1 :   gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
     311           1 :   if (debug)
     312           0 :     gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
     313             : 
     314           1 :   check ();
     315             : 
     316           1 :   return 0;
     317             : }

Generated by: LCOV version 1.11