Blame SOURCES/curves.c

66e42d
/* curves.c -  ECC curves regression tests
66e42d
 *	Copyright (C) 2011 Free Software Foundation, Inc.
66e42d
 *
66e42d
 * This file is part of Libgcrypt.
66e42d
 *
66e42d
 * Libgcrypt is free software; you can redistribute it and/or modify
66e42d
 * it under the terms of the GNU Lesser General Public License as
66e42d
 * published by the Free Software Foundation; either version 2.1 of
66e42d
 * the License, or (at your option) any later version.
66e42d
 *
66e42d
 * Libgcrypt is distributed in the hope that it will be useful,
66e42d
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
66e42d
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
66e42d
 * GNU Lesser General Public License for more details.
66e42d
 *
66e42d
 * You should have received a copy of the GNU Lesser General Public
66e42d
 * License along with this program; if not, write to the Free Software
66e42d
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
66e42d
 */
66e42d
66e42d
#ifdef HAVE_CONFIG_H
66e42d
#include <config.h>
66e42d
#endif
66e42d
#include <stdio.h>
66e42d
#include <stdlib.h>
66e42d
#include <string.h>
66e42d
#include <stdarg.h>
66e42d
66e42d
#include "../src/gcrypt-int.h"
66e42d
66e42d
66e42d
#define PGM "curves"
66e42d
#include "t-common.h"
66e42d
66e42d
/* Number of curves defined in ../cipger/ecc.c */
66e42d
#define N_CURVES 14
66e42d
66e42d
/* A real world sample public key.  */
66e42d
static char const sample_key_1[] =
66e42d
"(public-key\n"
66e42d
" (ecdsa\n"
66e42d
"  (p #00FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF#)\n"
66e42d
"  (a #00FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC#)\n"
66e42d
"  (b #5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B#)\n"
66e42d
"  (g #046B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"
66e42d
        "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5#)\n"
66e42d
"  (n #00FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551#)\n"
66e42d
"  (h #000000000000000000000000000000000000000000000000000000000000000001#)\n"
66e42d
"  (q #0442B927242237639A36CE9221B340DB1A9AB76DF2FE3E171277F6A4023DED146EE"
66e42d
      "86525E38CCECFF3FB8D152CC6334F70D23A525175C1BCBDDE6E023B2228770E#)\n"
66e42d
"  ))";
66e42d
static char const sample_key_1_curve[] = "NIST P-256";
66e42d
static unsigned int sample_key_1_nbits = 256;
66e42d
66e42d
66e42d
66e42d
static void
66e42d
list_curves (void)
66e42d
{
66e42d
  int idx;
66e42d
  const char *name;
66e42d
  unsigned int nbits;
66e42d
66e42d
  for (idx=0; (name = gcry_pk_get_curve (NULL, idx, &nbits)); idx++)
66e42d
    {
66e42d
      if (verbose)
66e42d
        printf ("%s - %u bits\n", name, nbits);
66e42d
    }
66e42d
  if (idx != N_CURVES)
66e42d
    fail ("expected %d curves but got %d\n", N_CURVES, idx);
66e42d
  if (gcry_pk_get_curve (NULL, -1, NULL))
66e42d
    fail ("curve iteration failed\n");
66e42d
}
66e42d
66e42d
66e42d
static void
66e42d
check_matching (void)
66e42d
{
66e42d
  gpg_error_t err;
66e42d
  gcry_sexp_t key;
66e42d
  const char *name;
66e42d
  unsigned int nbits;
66e42d
66e42d
  err = gcry_sexp_new (&key, sample_key_1, 0, 1);
66e42d
  if (err)
66e42d
    die ("parsing s-expression string failed: %s\n", gpg_strerror (err));
66e42d
  name = gcry_pk_get_curve (key, 0, &nbits);
66e42d
  if (!name)
66e42d
    fail ("curve name not found for sample_key_1\n");
66e42d
  else if (strcmp (name, sample_key_1_curve))
66e42d
    fail ("expected curve name %s but got %s for sample_key_1\n",
66e42d
          sample_key_1_curve, name);
66e42d
  else if (nbits != sample_key_1_nbits)
66e42d
    fail ("expected curve size %u but got %u for sample_key_1\n",
66e42d
          sample_key_1_nbits, nbits);
66e42d
66e42d
  gcry_sexp_release (key);
66e42d
66e42d
}
66e42d
66e42d
66e42d
static void
66e42d
check_get_params (void)
66e42d
{
66e42d
  gcry_sexp_t param;
66e42d
  const char *name;
66e42d
66e42d
  param = gcry_pk_get_param (GCRY_PK_ECDSA, sample_key_1_curve);
66e42d
  if (!param)
66e42d
    fail ("error gerring parameters for `%s'\n", sample_key_1_curve);
66e42d
66e42d
  name = gcry_pk_get_curve (param, 0, NULL);
66e42d
  if (!name)
66e42d
    fail ("get_param: curve name not found for sample_key_1\n");
66e42d
  else if (strcmp (name, sample_key_1_curve))
66e42d
    fail ("get_param: expected curve name %s but got %s for sample_key_1\n",
66e42d
          sample_key_1_curve, name);
66e42d
66e42d
  gcry_sexp_release (param);
66e42d
66e42d
}
66e42d
66e42d
66e42d
int
66e42d
main (int argc, char **argv)
66e42d
{
66e42d
  if (argc > 1 && !strcmp (argv[1], "--verbose"))
66e42d
    verbose = 1;
66e42d
  else if (argc > 1 && !strcmp (argv[1], "--debug"))
66e42d
    verbose = debug = 1;
66e42d
66e42d
  if (!gcry_check_version (GCRYPT_VERSION))
66e42d
    die ("version mismatch\n");
66e42d
66e42d
  xgcry_control (GCRYCTL_DISABLE_SECMEM, 0);
66e42d
  xgcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
66e42d
  if (debug)
66e42d
    xgcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
66e42d
  list_curves ();
66e42d
  check_matching ();
66e42d
  check_get_params ();
66e42d
66e42d
  return error_count ? 1 : 0;
66e42d
}