LCOV - code coverage report
Current view: top level - tests - curves.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 49 81 60.5 %
Date: 2016-09-12 12:56:58 Functions: 4 6 66.7 %

          Line data    Source code
       1             : /* curves.c -  ECC curves regression tests
       2             :  *      Copyright (C) 2011 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             : #include <stdio.h>
      25             : #include <stdlib.h>
      26             : #include <string.h>
      27             : #include <stdarg.h>
      28             : 
      29             : #include "../src/gcrypt-int.h"
      30             : 
      31             : /* Number of curves defined in ../cipger/ecc.c */
      32             : #define N_CURVES 22
      33             : 
      34             : /* A real world sample public key.  */
      35             : static char const sample_key_1[] =
      36             : "(public-key\n"
      37             : " (ecdsa\n"
      38             : "  (p #00FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF#)\n"
      39             : "  (a #00FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC#)\n"
      40             : "  (b #5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B#)\n"
      41             : "  (g #046B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"
      42             :         "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5#)\n"
      43             : "  (n #00FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551#)\n"
      44             : "  (h #000000000000000000000000000000000000000000000000000000000000000001#)\n"
      45             : "  (q #0442B927242237639A36CE9221B340DB1A9AB76DF2FE3E171277F6A4023DED146EE"
      46             :       "86525E38CCECFF3FB8D152CC6334F70D23A525175C1BCBDDE6E023B2228770E#)\n"
      47             : "  ))";
      48             : static char const sample_key_1_curve[] = "NIST P-256";
      49             : static unsigned int sample_key_1_nbits = 256;
      50             : 
      51             : /* A made up sample public key.  */
      52             : static char const sample_key_2[] =
      53             : "(public-key\n"
      54             : " (ecdh\n"
      55             : "  (p #00e95e4a5f737059dc60dfc7ad95b3d8139515620f#)\n"
      56             : "  (a #340e7be2a280eb74e2be61bada745d97e8f7c300#)\n"
      57             : "  (b #1e589a8595423412134faa2dbdec95c8d8675e58#)\n"
      58             : "  (g #04bed5af16ea3f6a4f62938c4631eb5af7bdbcdbc3"
      59             :         "1667cb477a1a8ec338f94741669c976316da6321#)\n"
      60             : "  (n #00e95e4a5f737059dc60df5991d45029409e60fc09#)\n"
      61             : "  (h #000000000000000000000000000000000000000000000000000000000000000001#)\n"
      62             : "  (q #041111111111111111111111111111111111111111"
      63             :         "2222222222222222222222222222222222222222#)\n"
      64             : "  ))";
      65             : static char const sample_key_2_curve[] = "brainpoolP160r1";
      66             : static unsigned int sample_key_2_nbits = 160;
      67             : 
      68             : 
      69             : /* Program option flags.  */
      70             : static int verbose;
      71             : static int error_count;
      72             : 
      73             : static void
      74           0 : fail (const char *format, ...)
      75             : {
      76             :   va_list arg_ptr;
      77             : 
      78           0 :   va_start (arg_ptr, format);
      79           0 :   vfprintf (stderr, format, arg_ptr);
      80           0 :   va_end (arg_ptr);
      81           0 :   error_count++;
      82           0 : }
      83             : 
      84             : static void
      85           0 : die (const char *format, ...)
      86             : {
      87             :   va_list arg_ptr;
      88             : 
      89           0 :   va_start (arg_ptr, format);
      90           0 :   vfprintf (stderr, format, arg_ptr);
      91           0 :   va_end (arg_ptr);
      92           0 :   exit (1);
      93             : }
      94             : 
      95             : 
      96             : static void
      97           1 : list_curves (void)
      98             : {
      99             :   int idx;
     100             :   const char *name;
     101             :   unsigned int nbits;
     102             : 
     103          23 :   for (idx=0; (name = gcry_pk_get_curve (NULL, idx, &nbits)); idx++)
     104             :     {
     105          22 :       if (verbose)
     106           0 :         printf ("%s - %u bits\n", name, nbits);
     107             :     }
     108           1 :   if (idx != N_CURVES)
     109           0 :     fail ("expected %d curves but got %d\n", N_CURVES, idx);
     110           1 :   if (gcry_pk_get_curve (NULL, -1, NULL))
     111           0 :     fail ("curve iteration failed\n");
     112           1 : }
     113             : 
     114             : 
     115             : static void
     116           1 : check_matching (void)
     117             : {
     118             :   gpg_error_t err;
     119             :   gcry_sexp_t key;
     120             :   const char *name;
     121             :   unsigned int nbits;
     122             : 
     123           1 :   err = gcry_sexp_new (&key, sample_key_1, 0, 1);
     124           1 :   if (err)
     125           0 :     die ("parsing s-expression string failed: %s\n", gpg_strerror (err));
     126           1 :   name = gcry_pk_get_curve (key, 0, &nbits);
     127           1 :   if (!name)
     128           0 :     fail ("curve name not found for sample_key_1\n");
     129           1 :   else if (strcmp (name, sample_key_1_curve))
     130           0 :     fail ("expected curve name %s but got %s for sample_key_1\n",
     131             :           sample_key_1_curve, name);
     132           1 :   else if (nbits != sample_key_1_nbits)
     133           0 :     fail ("expected curve size %u but got %u for sample_key_1\n",
     134             :           sample_key_1_nbits, nbits);
     135             : 
     136           1 :   gcry_sexp_release (key);
     137             : 
     138           1 :   err = gcry_sexp_new (&key, sample_key_2, 0, 1);
     139           1 :   if (err)
     140           0 :     die ("parsing s-expression string failed: %s\n", gpg_strerror (err));
     141           1 :   name = gcry_pk_get_curve (key, 0, &nbits);
     142           1 :   if (!name)
     143           0 :     fail ("curve name not found for sample_key_2\n");
     144           1 :   else if (strcmp (name, sample_key_2_curve))
     145           0 :     fail ("expected curve name %s but got %s for sample_key_2\n",
     146             :           sample_key_2_curve, name);
     147           1 :   else if (nbits != sample_key_2_nbits)
     148           0 :     fail ("expected curve size %u but got %u for sample_key_2\n",
     149             :           sample_key_2_nbits, nbits);
     150             : 
     151           1 :   gcry_sexp_release (key);
     152           1 : }
     153             : 
     154             : 
     155             : static void
     156           1 : check_get_params (void)
     157             : {
     158             :   gcry_sexp_t param;
     159             :   const char *name;
     160             : 
     161           1 :   param = gcry_pk_get_param (GCRY_PK_ECDSA, sample_key_1_curve);
     162           1 :   if (!param)
     163           0 :     fail ("error gerring parameters for `%s'\n", sample_key_1_curve);
     164             : 
     165           1 :   name = gcry_pk_get_curve (param, 0, NULL);
     166           1 :   if (!name)
     167           0 :     fail ("get_param: curve name not found for sample_key_1\n");
     168           1 :   else if (strcmp (name, sample_key_1_curve))
     169           0 :     fail ("get_param: expected curve name %s but got %s for sample_key_1\n",
     170             :           sample_key_1_curve, name);
     171             : 
     172           1 :   gcry_sexp_release (param);
     173             : 
     174             :   /* Brainpool curves are not supported in fips mode */
     175           1 :   if (gcry_fips_mode_active())
     176           1 :     return;
     177             : 
     178           1 :   param = gcry_pk_get_param (GCRY_PK_ECDSA, sample_key_2_curve);
     179           1 :   if (!param)
     180           0 :     fail ("error gerring parameters for `%s'\n", sample_key_2_curve);
     181             : 
     182           1 :   name = gcry_pk_get_curve (param, 0, NULL);
     183           1 :   if (!name)
     184           0 :     fail ("get_param: curve name not found for sample_key_2\n");
     185           1 :   else if (strcmp (name, sample_key_2_curve))
     186           0 :     fail ("get_param: expected curve name %s but got %s for sample_key_2\n",
     187             :           sample_key_2_curve, name);
     188             : 
     189           1 :   gcry_sexp_release (param);
     190             : }
     191             : 
     192             : 
     193             : int
     194           1 : main (int argc, char **argv)
     195             : {
     196           1 :   int debug = 0;
     197             : 
     198           1 :   if (argc > 1 && !strcmp (argv[1], "--verbose"))
     199           0 :     verbose = 1;
     200           1 :   else if (argc > 1 && !strcmp (argv[1], "--debug"))
     201           0 :     verbose = debug = 1;
     202             : 
     203           1 :   if (!gcry_check_version (GCRYPT_VERSION))
     204           0 :     die ("version mismatch\n");
     205             : 
     206           1 :   gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
     207           1 :   gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
     208           1 :   if (debug)
     209           0 :     gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
     210           1 :   list_curves ();
     211           1 :   check_matching ();
     212           1 :   check_get_params ();
     213             : 
     214           1 :   return error_count ? 1 : 0;
     215             : }

Generated by: LCOV version 1.11