LCOV - code coverage report
Current view: top level - tests - curves.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 47 69 68.1 %
Date: 2016-12-15 12:59:22 Functions: 4 4 100.0 %

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

Generated by: LCOV version 1.12