gentleknife / rpms / libgcrypt

Forked from rpms/libgcrypt 4 years ago
Clone
66e42d
/* t-mpi-point.c  - Tests for mpi point functions
66e42d
 * Copyright (C) 2013 g10 Code GmbH
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, see <http://www.gnu.org/licenses/>.
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 <assert.h>
66e42d
#include <stdarg.h>
66e42d
66e42d
#define PGM "t-mpi-point"
66e42d
#include "t-common.h"
66e42d
66e42d
static struct
66e42d
{
66e42d
  const char *desc;           /* Description of the curve.  */
66e42d
  const char *p;              /* Order of the prime field.  */
66e42d
  const char *a, *b;          /* The coefficients. */
66e42d
  const char *n;              /* The order of the base point.  */
66e42d
  const char *g_x, *g_y;      /* Base point.  */
66e42d
  const char *h;              /* Cofactor.  */
66e42d
} test_curve[] =
66e42d
  {
66e42d
    {
66e42d
      "NIST P-224",
66e42d
      "0xffffffffffffffffffffffffffffffff000000000000000000000001",
66e42d
      "0xfffffffffffffffffffffffffffffffefffffffffffffffffffffffe",
66e42d
      "0xb4050a850c04b3abf54132565044b0b7d7bfd8ba270b39432355ffb4",
66e42d
      "0xffffffffffffffffffffffffffff16a2e0b8f03e13dd29455c5c2a3d" ,
66e42d
66e42d
      "0xb70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21",
66e42d
      "0xbd376388b5f723fb4c22dfe6cd4375a05a07476444d5819985007e34",
66e42d
      "0x01"
66e42d
    },
66e42d
    {
66e42d
      "NIST P-256",
66e42d
      "0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff",
66e42d
      "0xffffffff00000001000000000000000000000000fffffffffffffffffffffffc",
66e42d
      "0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b",
66e42d
      "0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551",
66e42d
66e42d
      "0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296",
66e42d
      "0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5",
66e42d
      "0x01"
66e42d
    },
66e42d
    {
66e42d
      "NIST P-384",
66e42d
      "0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"
66e42d
      "ffffffff0000000000000000ffffffff",
66e42d
      "0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"
66e42d
      "ffffffff0000000000000000fffffffc",
66e42d
      "0xb3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875a"
66e42d
      "c656398d8a2ed19d2a85c8edd3ec2aef",
66e42d
      "0xffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf"
66e42d
      "581a0db248b0a77aecec196accc52973",
66e42d
66e42d
      "0xaa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a38"
66e42d
      "5502f25dbf55296c3a545e3872760ab7",
66e42d
      "0x3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c0"
66e42d
      "0a60b1ce1d7e819d7a431d7c90ea0e5f",
66e42d
      "0x01"
66e42d
    },
66e42d
    {
66e42d
      "NIST P-521",
66e42d
      "0x01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
66e42d
      "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
66e42d
      "0x01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
66e42d
      "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc",
66e42d
      "0x051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef10"
66e42d
      "9e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00",
66e42d
      "0x1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
66e42d
      "ffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409",
66e42d
66e42d
      "0xc6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3d"
66e42d
      "baa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66",
66e42d
      "0x11839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e6"
66e42d
      "62c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650",
66e42d
      "0x01"
66e42d
    },
66e42d
    {
66e42d
      "Ed25519",
66e42d
      "0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFED",
66e42d
      "0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEC",
66e42d
      "0x52036CEE2B6FFE738CC740797779E89800700A4D4141D8AB75EB4DCA135978A3",
66e42d
      "0x1000000000000000000000000000000014DEF9DEA2F79CD65812631A5CF5D3ED",
66e42d
      "0x216936D3CD6E53FEC0A4E231FDD6DC5C692CC7609525A7B2C9562D608F25D51A",
66e42d
      "0x6666666666666666666666666666666666666666666666666666666666666658",
66e42d
      "0x08"
66e42d
    },
66e42d
    { NULL, NULL, NULL, NULL, NULL, NULL }
66e42d
  };
66e42d
66e42d
/* A sample public key for NIST P-256.  */
66e42d
static const char sample_p256_q[] =
66e42d
  "04"
66e42d
  "42B927242237639A36CE9221B340DB1A9AB76DF2FE3E171277F6A4023DED146E"
66e42d
  "E86525E38CCECFF3FB8D152CC6334F70D23A525175C1BCBDDE6E023B2228770E";
66e42d
static const char sample_p256_q_x[] =
66e42d
  "42B927242237639A36CE9221B340DB1A9AB76DF2FE3E171277F6A4023DED146E";
66e42d
static const char sample_p256_q_y[] =
66e42d
  "00E86525E38CCECFF3FB8D152CC6334F70D23A525175C1BCBDDE6E023B2228770E";
66e42d
66e42d
66e42d
/* A sample public key for Ed25519.  */
66e42d
static const char sample_ed25519_q[] =
66e42d
  "04"
66e42d
  "55d0e09a2b9d34292297e08d60d0f620c513d47253187c24b12786bd777645ce"
66e42d
  "1a5107f7681a02af2523a6daf372e10e3a0764c9d3fe4bd5b70ab18201985ad7";
66e42d
static const char sample_ed25519_q_x[] =
66e42d
  "55d0e09a2b9d34292297e08d60d0f620c513d47253187c24b12786bd777645ce";
66e42d
static const char sample_ed25519_q_y[] =
66e42d
  "1a5107f7681a02af2523a6daf372e10e3a0764c9d3fe4bd5b70ab18201985ad7";
66e42d
static const char sample_ed25519_q_eddsa[] =
66e42d
  "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a";
66e42d
static const char sample_ed25519_d[] =
66e42d
  "9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60";
66e42d
66e42d
66e42d
static void
66e42d
print_mpi_2 (const char *text, const char *text2, gcry_mpi_t a)
66e42d
{
66e42d
  gcry_error_t err;
66e42d
  char *buf;
66e42d
  void *bufaddr = &buf;
66e42d
66e42d
  err = gcry_mpi_aprint (GCRYMPI_FMT_HEX, bufaddr, NULL, a);
66e42d
  if (err)
66e42d
    fprintf (stderr, "%s%s: [error printing number: %s]\n",
66e42d
             text, text2? text2:"", gpg_strerror (err));
66e42d
  else
66e42d
    {
66e42d
      fprintf (stderr, "%s%s: %s\n", text, text2? text2:"", buf);
66e42d
      gcry_free (buf);
66e42d
    }
66e42d
}
66e42d
66e42d
66e42d
static void
66e42d
print_mpi (const char *text, gcry_mpi_t a)
66e42d
{
66e42d
  print_mpi_2 (text, NULL, a);
66e42d
}
66e42d
66e42d
66e42d
static void
66e42d
print_point (const char *text, gcry_mpi_point_t a)
66e42d
{
66e42d
  gcry_mpi_t x, y, z;
66e42d
66e42d
  x = gcry_mpi_new (0);
66e42d
  y = gcry_mpi_new (0);
66e42d
  z = gcry_mpi_new (0);
66e42d
  gcry_mpi_point_get (x, y, z, a);
66e42d
  print_mpi_2 (text, ".x", x);
66e42d
  print_mpi_2 (text, ".y", y);
66e42d
  print_mpi_2 (text, ".z", z);
66e42d
  gcry_mpi_release (x);
66e42d
  gcry_mpi_release (y);
66e42d
  gcry_mpi_release (z);
66e42d
}
66e42d
66e42d
66e42d
static void
66e42d
print_sexp (const char *prefix, gcry_sexp_t a)
66e42d
{
66e42d
  char *buf;
66e42d
  size_t size;
66e42d
66e42d
  if (prefix)
66e42d
    fputs (prefix, stderr);
66e42d
  size = gcry_sexp_sprint (a, GCRYSEXP_FMT_ADVANCED, NULL, 0);
66e42d
  buf = gcry_xmalloc (size);
66e42d
66e42d
  gcry_sexp_sprint (a, GCRYSEXP_FMT_ADVANCED, buf, size);
66e42d
  fprintf (stderr, "%.*s", (int)size, buf);
66e42d
  gcry_free (buf);
66e42d
}
66e42d
66e42d
66e42d
static gcry_mpi_t
66e42d
hex2mpi (const char *string)
66e42d
{
66e42d
  gpg_error_t err;
66e42d
  gcry_mpi_t val;
66e42d
66e42d
  err = gcry_mpi_scan (&val, GCRYMPI_FMT_HEX, string, 0, NULL);
66e42d
  if (err)
66e42d
    die ("hex2mpi '%s' failed: %s\n", string, gpg_strerror (err));
66e42d
  return val;
66e42d
}
66e42d
66e42d
66e42d
/* Convert STRING consisting of hex characters into its binary
66e42d
   representation and return it as an allocated buffer. The valid
66e42d
   length of the buffer is returned at R_LENGTH.  The string is
66e42d
   delimited by end of string.  The function returns NULL on
66e42d
   error.  */
66e42d
static void *
66e42d
hex2buffer (const char *string, size_t *r_length)
66e42d
{
66e42d
  const char *s;
66e42d
  unsigned char *buffer;
66e42d
  size_t length;
66e42d
66e42d
  buffer = xmalloc (strlen(string)/2+1);
66e42d
  length = 0;
66e42d
  for (s=string; *s; s +=2 )
66e42d
    {
66e42d
      if (!hexdigitp (s) || !hexdigitp (s+1))
66e42d
        return NULL;           /* Invalid hex digits. */
66e42d
      ((unsigned char*)buffer)[length++] = xtoi_2 (s);
66e42d
    }
66e42d
  *r_length = length;
66e42d
  return buffer;
66e42d
}
66e42d
66e42d
66e42d
static gcry_mpi_t
66e42d
hex2mpiopa (const char *string)
66e42d
{
66e42d
  char *buffer;
66e42d
  size_t buflen;
66e42d
  gcry_mpi_t val;
66e42d
66e42d
  buffer = hex2buffer (string, &buflen);
66e42d
  if (!buffer)
66e42d
    die ("hex2mpiopa '%s' failed: parser error\n", string);
66e42d
  val = gcry_mpi_set_opaque (NULL, buffer, buflen*8);
66e42d
  if (!buffer)
66e42d
    die ("hex2mpiopa '%s' failed: set_opaque error\n", string);
66e42d
  return val;
66e42d
}
66e42d
66e42d
66e42d
/* Compare A to B, where B is given as a hex string.  */
66e42d
static int
66e42d
cmp_mpihex (gcry_mpi_t a, const char *b)
66e42d
{
66e42d
  gcry_mpi_t bval;
66e42d
  int res;
66e42d
66e42d
  if (gcry_mpi_get_flag (a, GCRYMPI_FLAG_OPAQUE))
66e42d
    bval = hex2mpiopa (b);
66e42d
  else
66e42d
    bval = hex2mpi (b);
66e42d
  res = gcry_mpi_cmp (a, bval);
66e42d
  gcry_mpi_release (bval);
66e42d
  return res;
66e42d
}
66e42d
66e42d
66e42d
/* Wrapper to emulate the libgcrypt internal EC context allocation
66e42d
   function.  */
66e42d
static gpg_error_t
66e42d
ec_p_new (gcry_ctx_t *r_ctx, gcry_mpi_t p, gcry_mpi_t a)
66e42d
{
66e42d
  gpg_error_t err;
66e42d
  gcry_sexp_t sexp;
66e42d
66e42d
  if (p && a)
66e42d
    err = gcry_sexp_build (&sexp, NULL, "(ecdsa (p %m)(a %m))", p, a);
66e42d
  else if (p)
66e42d
    err = gcry_sexp_build (&sexp, NULL, "(ecdsa (p %m))", p);
66e42d
  else if (a)
66e42d
    err = gcry_sexp_build (&sexp, NULL, "(ecdsa (a %m))", a);
66e42d
  else
66e42d
    err = gcry_sexp_build (&sexp, NULL, "(ecdsa)");
66e42d
  if (err)
66e42d
    return err;
66e42d
  err = gcry_mpi_ec_new (r_ctx, sexp, NULL);
66e42d
  gcry_sexp_release (sexp);
66e42d
  return err;
66e42d
}
66e42d
66e42d
66e42d

66e42d
static void
66e42d
set_get_point (void)
66e42d
{
66e42d
  gcry_mpi_point_t point, point2;
66e42d
  gcry_mpi_t x, y, z;
66e42d
66e42d
  wherestr = "set_get_point";
66e42d
  info ("checking point setting functions\n");
66e42d
66e42d
  point = gcry_mpi_point_new (0);
66e42d
  x = gcry_mpi_set_ui (NULL, 17);
66e42d
  y = gcry_mpi_set_ui (NULL, 42);
66e42d
  z = gcry_mpi_set_ui (NULL, 11371);
66e42d
  gcry_mpi_point_get (x, y, z, point);
66e42d
  if (gcry_mpi_cmp_ui (x, 0)
66e42d
      || gcry_mpi_cmp_ui (y, 0) || gcry_mpi_cmp_ui (z, 0))
66e42d
    fail ("new point not initialized to (0,0,0)\n");
66e42d
  gcry_mpi_point_snatch_get (x, y, z, point);
66e42d
  point = NULL;
66e42d
  if (gcry_mpi_cmp_ui (x, 0)
66e42d
      || gcry_mpi_cmp_ui (y, 0) || gcry_mpi_cmp_ui (z, 0))
66e42d
    fail ("snatch_get failed\n");
66e42d
  gcry_mpi_release (x);
66e42d
  gcry_mpi_release (y);
66e42d
  gcry_mpi_release (z);
66e42d
66e42d
  point = gcry_mpi_point_new (0);
66e42d
  x = gcry_mpi_set_ui (NULL, 17);
66e42d
  y = gcry_mpi_set_ui (NULL, 42);
66e42d
  z = gcry_mpi_set_ui (NULL, 11371);
66e42d
  gcry_mpi_point_set (point, x, y, z);
66e42d
  gcry_mpi_set_ui (x, 23);
66e42d
  gcry_mpi_set_ui (y, 24);
66e42d
  gcry_mpi_set_ui (z, 25);
66e42d
  gcry_mpi_point_get (x, y, z, point);
66e42d
  if (gcry_mpi_cmp_ui (x, 17)
66e42d
      || gcry_mpi_cmp_ui (y, 42) || gcry_mpi_cmp_ui (z, 11371))
66e42d
    fail ("point_set/point_get failed\n");
66e42d
  gcry_mpi_point_snatch_set (point, x, y, z);
66e42d
  x = gcry_mpi_new (0);
66e42d
  y = gcry_mpi_new (0);
66e42d
  z = gcry_mpi_new (0);
66e42d
  gcry_mpi_point_get (x, y, z, point);
66e42d
  if (gcry_mpi_cmp_ui (x, 17)
66e42d
      || gcry_mpi_cmp_ui (y, 42) || gcry_mpi_cmp_ui (z, 11371))
66e42d
    fail ("point_snatch_set/point_get failed\n");
66e42d
66e42d
  point2 = gcry_mpi_point_copy (point);
66e42d
66e42d
  gcry_mpi_point_get (x, y, z, point2);
66e42d
  if (gcry_mpi_cmp_ui (x, 17)
66e42d
      || gcry_mpi_cmp_ui (y, 42) || gcry_mpi_cmp_ui (z, 11371))
66e42d
    fail ("point_copy failed (1)\n");
66e42d
66e42d
  gcry_mpi_point_release (point);
66e42d
66e42d
  gcry_mpi_point_get (x, y, z, point2);
66e42d
  if (gcry_mpi_cmp_ui (x, 17)
66e42d
      || gcry_mpi_cmp_ui (y, 42) || gcry_mpi_cmp_ui (z, 11371))
66e42d
    fail ("point_copy failed (2)\n");
66e42d
66e42d
  gcry_mpi_point_release (point2);
66e42d
66e42d
  gcry_mpi_release (x);
66e42d
  gcry_mpi_release (y);
66e42d
  gcry_mpi_release (z);
66e42d
}
66e42d
66e42d
66e42d
static void
66e42d
context_alloc (void)
66e42d
{
66e42d
  gpg_error_t err;
66e42d
  gcry_ctx_t ctx;
66e42d
  gcry_mpi_t p, a;
66e42d
66e42d
  wherestr = "context_alloc";
66e42d
  info ("checking context functions\n");
66e42d
66e42d
  p = gcry_mpi_set_ui (NULL, 1);
66e42d
  a = gcry_mpi_set_ui (NULL, 1);
66e42d
  err = ec_p_new (&ctx, p, a);
66e42d
  if (err)
66e42d
    die ("ec_p_new returned an error: %s\n", gpg_strerror (err));
66e42d
  gcry_mpi_release (p);
66e42d
  gcry_mpi_release (a);
66e42d
  gcry_ctx_release (ctx);
66e42d
66e42d
  p = NULL;
66e42d
  a = gcry_mpi_set_ui (NULL, 0);
66e42d
66e42d
  err = ec_p_new (&ctx, p, a);
66e42d
  if (!err || gpg_err_code (err) != GPG_ERR_EINVAL)
66e42d
    fail ("ec_p_new: bad parameter detection failed (1)\n");
66e42d
66e42d
  gcry_mpi_release (a);
66e42d
  a = NULL;
66e42d
  err = ec_p_new (&ctx, p, a);
66e42d
  if (!err || gpg_err_code (err) != GPG_ERR_EINVAL)
66e42d
    fail ("ec_p_new: bad parameter detection failed (2)\n");
66e42d
66e42d
}
66e42d
66e42d
66e42d
static int
66e42d
get_and_cmp_mpi (const char *name, const char *mpistring, const char *desc,
66e42d
                 gcry_ctx_t ctx)
66e42d
{
66e42d
  gcry_mpi_t mpi;
66e42d
66e42d
  mpi = gcry_mpi_ec_get_mpi (name, ctx, 1);
66e42d
  if (!mpi)
66e42d
    {
66e42d
      fail ("error getting parameter '%s' of curve '%s'\n", name, desc);
66e42d
      return 1;
66e42d
    }
66e42d
  if (debug)
66e42d
    print_mpi (name, mpi);
66e42d
  if (cmp_mpihex (mpi, mpistring))
66e42d
    {
66e42d
      fail ("parameter '%s' of curve '%s' does not match\n", name, desc);
66e42d
      gcry_mpi_release (mpi);
66e42d
      return 1;
66e42d
    }
66e42d
  gcry_mpi_release (mpi);
66e42d
  return 0;
66e42d
}
66e42d
66e42d
66e42d
static int
66e42d
get_and_cmp_point (const char *name,
66e42d
                   const char *mpi_x_string, const char *mpi_y_string,
66e42d
                   const char *desc, gcry_ctx_t ctx)
66e42d
{
66e42d
  gcry_mpi_point_t point;
66e42d
  gcry_mpi_t x, y, z;
66e42d
  int result = 0;
66e42d
66e42d
  point = gcry_mpi_ec_get_point (name, ctx, 1);
66e42d
  if (!point)
66e42d
    {
66e42d
      fail ("error getting point parameter '%s' of curve '%s'\n", name, desc);
66e42d
      return 1;
66e42d
    }
66e42d
  if (debug)
66e42d
    print_point (name, point);
66e42d
66e42d
  x = gcry_mpi_new (0);
66e42d
  y = gcry_mpi_new (0);
66e42d
  z = gcry_mpi_new (0);
66e42d
  gcry_mpi_point_snatch_get (x, y, z, point);
66e42d
  if (cmp_mpihex (x, mpi_x_string))
66e42d
    {
66e42d
      fail ("x coordinate of '%s' of curve '%s' does not match\n", name, desc);
66e42d
      result = 1;
66e42d
    }
66e42d
  if (cmp_mpihex (y, mpi_y_string))
66e42d
    {
66e42d
      fail ("y coordinate of '%s' of curve '%s' does not match\n", name, desc);
66e42d
      result = 1;
66e42d
    }
66e42d
  if (cmp_mpihex (z, "01"))
66e42d
    {
66e42d
      fail ("z coordinate of '%s' of curve '%s' is not 1\n", name, desc);
66e42d
      result = 1;
66e42d
    }
66e42d
  gcry_mpi_release (x);
66e42d
  gcry_mpi_release (y);
66e42d
  gcry_mpi_release (z);
66e42d
  return result;
66e42d
}
66e42d
66e42d
66e42d
static void
66e42d
context_param (void)
66e42d
{
66e42d
  gpg_error_t err;
66e42d
  int idx;
66e42d
  gcry_ctx_t ctx = NULL;
66e42d
  gcry_mpi_t q, d;
66e42d
  gcry_sexp_t keyparam;
66e42d
66e42d
  wherestr = "context_param";
66e42d
66e42d
  info ("checking standard curves\n");
66e42d
  for (idx=0; test_curve[idx].desc; idx++)
66e42d
    {
66e42d
      /* P-192 and Ed25519 are not supported in fips mode */
66e42d
      if (gcry_fips_mode_active())
66e42d
        {
66e42d
          if (!strcmp(test_curve[idx].desc, "NIST P-192")
66e42d
              || !strcmp(test_curve[idx].desc, "Ed25519"))
66e42d
            {
66e42d
	      info ("skipping %s in fips mode\n", test_curve[idx].desc );
66e42d
              continue;
66e42d
            }
66e42d
        }
66e42d
66e42d
      gcry_ctx_release (ctx);
66e42d
      err = gcry_mpi_ec_new (&ctx, NULL, test_curve[idx].desc);
66e42d
      if (err)
66e42d
        {
66e42d
          fail ("can't create context for curve '%s': %s\n",
66e42d
                test_curve[idx].desc, gpg_strerror (err));
66e42d
          continue;
66e42d
        }
66e42d
      if (get_and_cmp_mpi ("p", test_curve[idx].p, test_curve[idx].desc, ctx))
66e42d
        continue;
66e42d
      if (get_and_cmp_mpi ("a", test_curve[idx].a, test_curve[idx].desc, ctx))
66e42d
        continue;
66e42d
      if (get_and_cmp_mpi ("b", test_curve[idx].b, test_curve[idx].desc, ctx))
66e42d
        continue;
66e42d
      if (get_and_cmp_mpi ("g.x",test_curve[idx].g_x, test_curve[idx].desc,ctx))
66e42d
        continue;
66e42d
      if (get_and_cmp_mpi ("g.y",test_curve[idx].g_y, test_curve[idx].desc,ctx))
66e42d
        continue;
66e42d
      if (get_and_cmp_mpi ("n", test_curve[idx].n, test_curve[idx].desc, ctx))
66e42d
        continue;
66e42d
      if (get_and_cmp_point ("g", test_curve[idx].g_x, test_curve[idx].g_y,
66e42d
                             test_curve[idx].desc, ctx))
66e42d
        continue;
66e42d
      if (get_and_cmp_mpi ("h", test_curve[idx].h, test_curve[idx].desc, ctx))
66e42d
        continue;
66e42d
66e42d
    }
66e42d
66e42d
  info ("checking sample public key (nistp256)\n");
66e42d
  q = hex2mpi (sample_p256_q);
66e42d
  err = gcry_sexp_build (&keyparam, NULL,
66e42d
                        "(public-key(ecc(curve %s)(q %m)))",
66e42d
                        "NIST P-256", q);
66e42d
  if (err)
66e42d
    die ("gcry_sexp_build failed: %s\n", gpg_strerror (err));
66e42d
  gcry_mpi_release (q);
66e42d
66e42d
  /* We can't call gcry_pk_testkey because it is only implemented for
66e42d
     private keys.  */
66e42d
  /* err = gcry_pk_testkey (keyparam); */
66e42d
  /* if (err) */
66e42d
  /*   fail ("gcry_pk_testkey failed for sample public key: %s\n", */
66e42d
  /*         gpg_strerror (err)); */
66e42d
66e42d
  gcry_ctx_release (ctx);
66e42d
  err = gcry_mpi_ec_new (&ctx, keyparam, NULL);
66e42d
  if (err)
66e42d
    fail ("gcry_mpi_ec_new failed for sample public key (nistp256): %s\n",
66e42d
          gpg_strerror (err));
66e42d
  else
66e42d
    {
66e42d
      gcry_sexp_t sexp;
66e42d
66e42d
      get_and_cmp_mpi ("q", sample_p256_q, "nistp256", ctx);
66e42d
      get_and_cmp_point ("q", sample_p256_q_x, sample_p256_q_y, "nistp256",
66e42d
                         ctx);
66e42d
66e42d
      /* Delete Q.  */
66e42d
      err = gcry_mpi_ec_set_mpi ("q", NULL, ctx);
66e42d
      if (err)
66e42d
        fail ("clearing Q for nistp256 failed: %s\n", gpg_strerror (err));
66e42d
      if (gcry_mpi_ec_get_mpi ("q", ctx, 0))
66e42d
        fail ("clearing Q for nistp256 did not work\n");
66e42d
66e42d
      /* Set Q again.  */
66e42d
      q = hex2mpi (sample_p256_q);
66e42d
      err = gcry_mpi_ec_set_mpi ("q", q, ctx);
66e42d
      if (err)
66e42d
        fail ("setting Q for nistp256 failed: %s\n", gpg_strerror (err));
66e42d
      get_and_cmp_mpi ("q", sample_p256_q, "nistp256(2)", ctx);
66e42d
      gcry_mpi_release (q);
66e42d
66e42d
      /* Get as s-expression.  */
66e42d
      err = gcry_pubkey_get_sexp (&sexp, 0, ctx);
66e42d
      if (err)
66e42d
        fail ("gcry_pubkey_get_sexp(0) failed: %s\n", gpg_strerror (err));
66e42d
      else if (debug)
66e42d
        print_sexp ("Result of gcry_pubkey_get_sexp (0):\n", sexp);
66e42d
      gcry_sexp_release (sexp);
66e42d
66e42d
      err = gcry_pubkey_get_sexp (&sexp, GCRY_PK_GET_PUBKEY, ctx);
66e42d
      if (err)
66e42d
        fail ("gcry_pubkey_get_sexp(GET_PUBKEY) failed: %s\n",
66e42d
              gpg_strerror (err));
66e42d
      else if (debug)
66e42d
        print_sexp ("Result of gcry_pubkey_get_sexp (GET_PUBKEY):\n", sexp);
66e42d
      gcry_sexp_release (sexp);
66e42d
66e42d
      err = gcry_pubkey_get_sexp (&sexp, GCRY_PK_GET_SECKEY, ctx);
66e42d
      if (gpg_err_code (err) != GPG_ERR_NO_SECKEY)
66e42d
        fail ("gcry_pubkey_get_sexp(GET_SECKEY) returned wrong error: %s\n",
66e42d
              gpg_strerror (err));
66e42d
      gcry_sexp_release (sexp);
66e42d
    }
66e42d
66e42d
  /* Skipping Ed25519 if in FIPS mode (it isn't supported) */
66e42d
  if (gcry_fips_mode_active())
66e42d
    goto cleanup;
66e42d
66e42d
  info ("checking sample public key (Ed25519)\n");
66e42d
  q = hex2mpi (sample_ed25519_q);
66e42d
  gcry_sexp_release (keyparam);
66e42d
  err = gcry_sexp_build (&keyparam, NULL,
66e42d
                        "(public-key(ecc(curve %s)(flags eddsa)(q %m)))",
66e42d
                        "Ed25519", q);
66e42d
  if (err)
66e42d
    die ("gcry_sexp_build failed: %s\n", gpg_strerror (err));
66e42d
  gcry_mpi_release (q);
66e42d
66e42d
  /* We can't call gcry_pk_testkey because it is only implemented for
66e42d
     private keys.  */
66e42d
  /* err = gcry_pk_testkey (keyparam); */
66e42d
  /* if (err) */
66e42d
  /*   fail ("gcry_pk_testkey failed for sample public key: %s\n", */
66e42d
  /*         gpg_strerror (err)); */
66e42d
66e42d
  gcry_ctx_release (ctx);
66e42d
  err = gcry_mpi_ec_new (&ctx, keyparam, NULL);
66e42d
  if (err)
66e42d
    fail ("gcry_mpi_ec_new failed for sample public key: %s\n",
66e42d
          gpg_strerror (err));
66e42d
  else
66e42d
    {
66e42d
      gcry_sexp_t sexp;
66e42d
66e42d
      get_and_cmp_mpi ("q", sample_ed25519_q, "Ed25519", ctx);
66e42d
      get_and_cmp_point ("q", sample_ed25519_q_x, sample_ed25519_q_y,
66e42d
                         "Ed25519", ctx);
66e42d
      get_and_cmp_mpi ("q@eddsa", sample_ed25519_q_eddsa, "Ed25519", ctx);
66e42d
66e42d
      /* Set d to see whether Q is correctly re-computed.  */
66e42d
      d = hex2mpi (sample_ed25519_d);
66e42d
      err = gcry_mpi_ec_set_mpi ("d", d, ctx);
66e42d
      if (err)
66e42d
        fail ("setting d for Ed25519 failed: %s\n", gpg_strerror (err));
66e42d
      gcry_mpi_release (d);
66e42d
      get_and_cmp_mpi ("q", sample_ed25519_q, "Ed25519(recompute Q)", ctx);
66e42d
66e42d
      /* Delete Q by setting d and then clearing d.  The clearing is
66e42d
         required so that we can check whether Q has been cleared and
66e42d
         because further tests only expect a public key.  */
66e42d
      d = hex2mpi (sample_ed25519_d);
66e42d
      err = gcry_mpi_ec_set_mpi ("d", d, ctx);
66e42d
      if (err)
66e42d
        fail ("setting d for Ed25519 failed: %s\n", gpg_strerror (err));
66e42d
      gcry_mpi_release (d);
66e42d
      err = gcry_mpi_ec_set_mpi ("d", NULL, ctx);
66e42d
      if (err)
66e42d
        fail ("setting d for Ed25519 failed(2): %s\n", gpg_strerror (err));
66e42d
      if (gcry_mpi_ec_get_mpi ("q", ctx, 0))
66e42d
        fail ("setting d for Ed25519 did not reset Q\n");
66e42d
66e42d
      /* Set Q again.  We need to use an opaque MPI here because
66e42d
         sample_ed25519_q is in uncompressed format which can only be
66e42d
         auto-detected if passed opaque.  */
66e42d
      q = hex2mpiopa (sample_ed25519_q);
66e42d
      err = gcry_mpi_ec_set_mpi ("q", q, ctx);
66e42d
      if (err)
66e42d
        fail ("setting Q for Ed25519 failed: %s\n", gpg_strerror (err));
66e42d
      gcry_mpi_release (q);
66e42d
      get_and_cmp_mpi ("q", sample_ed25519_q, "Ed25519(2)", ctx);
66e42d
66e42d
      /* Get as s-expression.  */
66e42d
      err = gcry_pubkey_get_sexp (&sexp, 0, ctx);
66e42d
      if (err)
66e42d
        fail ("gcry_pubkey_get_sexp(0) failed: %s\n", gpg_strerror (err));
66e42d
      else if (debug)
66e42d
        print_sexp ("Result of gcry_pubkey_get_sexp (0):\n", sexp);
66e42d
      gcry_sexp_release (sexp);
66e42d
66e42d
      err = gcry_pubkey_get_sexp (&sexp, GCRY_PK_GET_PUBKEY, ctx);
66e42d
      if (err)
66e42d
        fail ("gcry_pubkey_get_sexp(GET_PUBKEY) failed: %s\n",
66e42d
              gpg_strerror (err));
66e42d
      else if (debug)
66e42d
        print_sexp ("Result of gcry_pubkey_get_sexp (GET_PUBKEY):\n", sexp);
66e42d
      gcry_sexp_release (sexp);
66e42d
66e42d
      err = gcry_pubkey_get_sexp (&sexp, GCRY_PK_GET_SECKEY, ctx);
66e42d
      if (gpg_err_code (err) != GPG_ERR_NO_SECKEY)
66e42d
        fail ("gcry_pubkey_get_sexp(GET_SECKEY) returned wrong error: %s\n",
66e42d
              gpg_strerror (err));
66e42d
      gcry_sexp_release (sexp);
66e42d
66e42d
    }
66e42d
66e42d
 cleanup:
66e42d
  gcry_ctx_release (ctx);
66e42d
  gcry_sexp_release (keyparam);
66e42d
}
66e42d
66e42d
66e42d
66e42d
66e42d
/* Create a new point from (X,Y,Z) given as hex strings.  */
66e42d
gcry_mpi_point_t
66e42d
make_point (const char *x, const char *y, const char *z)
66e42d
{
66e42d
  gcry_mpi_point_t point;
66e42d
66e42d
  point = gcry_mpi_point_new (0);
66e42d
  gcry_mpi_point_snatch_set (point, hex2mpi (x), hex2mpi (y), hex2mpi (z));
66e42d
66e42d
  return point;
66e42d
}
66e42d
66e42d
66e42d
/* This tests checks that the low-level EC API yields the same result
66e42d
   as using the high level API.  The values have been taken from a
66e42d
   test run using the high level API.  */
66e42d
static void
66e42d
basic_ec_math (void)
66e42d
{
66e42d
  gpg_error_t err;
66e42d
  gcry_ctx_t ctx;
66e42d
  gcry_mpi_t P, A;
66e42d
  gcry_mpi_point_t G, Q;
66e42d
  gcry_mpi_t d;
66e42d
  gcry_mpi_t x, y, z;
66e42d
66e42d
  wherestr = "basic_ec_math";
66e42d
  info ("checking basic math functions for EC\n");
66e42d
66e42d
  P = hex2mpi ("0xfffffffffffffffffffffffffffffffeffffffffffffffff");
66e42d
  A = hex2mpi ("0xfffffffffffffffffffffffffffffffefffffffffffffffc");
66e42d
  G = make_point ("188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012",
66e42d
                  "7192B95FFC8DA78631011ED6B24CDD573F977A11E794811",
66e42d
                  "1");
66e42d
  d = hex2mpi ("D4EF27E32F8AD8E2A1C6DDEBB1D235A69E3CEF9BCE90273D");
66e42d
  Q = gcry_mpi_point_new (0);
66e42d
66e42d
  err = ec_p_new (&ctx, P, A);
66e42d
  if (err)
66e42d
    die ("ec_p_new failed: %s\n", gpg_strerror (err));
66e42d
66e42d
  x = gcry_mpi_new (0);
66e42d
  y = gcry_mpi_new (0);
66e42d
  z = gcry_mpi_new (0);
66e42d
66e42d
  {
66e42d
    /* A quick check that multiply by zero works.  */
66e42d
    gcry_mpi_t tmp;
66e42d
66e42d
    tmp = gcry_mpi_new (0);
66e42d
    gcry_mpi_ec_mul (Q, tmp, G, ctx);
66e42d
    gcry_mpi_release (tmp);
66e42d
    gcry_mpi_point_get (x, y, z, Q);
66e42d
    if (gcry_mpi_cmp_ui (x, 0) || gcry_mpi_cmp_ui (y, 0)
66e42d
        || gcry_mpi_cmp_ui (z, 0))
66e42d
      fail ("multiply a point by zero failed\n");
66e42d
  }
66e42d
66e42d
  gcry_mpi_ec_mul (Q, d, G, ctx);
66e42d
  gcry_mpi_point_get (x, y, z, Q);
66e42d
  if (cmp_mpihex (x, "222D9EC717C89D047E0898C9185B033CD11C0A981EE6DC66")
66e42d
      || cmp_mpihex (y, "605DE0A82D70D3E0F84A127D0739ED33D657DF0D054BFDE8")
66e42d
      || cmp_mpihex (z, "00B06B519071BC536999AC8F2D3934B3C1FC9EACCD0A31F88F"))
66e42d
    fail ("computed public key does not match\n");
66e42d
  if (debug)
66e42d
    {
66e42d
      print_mpi ("Q.x", x);
66e42d
      print_mpi ("Q.y", y);
66e42d
      print_mpi ("Q.z", z);
66e42d
    }
66e42d
66e42d
  if (gcry_mpi_ec_get_affine (x, y, Q, ctx))
66e42d
    fail ("failed to get affine coordinates\n");
66e42d
  if (cmp_mpihex (x, "008532093BA023F4D55C0424FA3AF9367E05F309DC34CDC3FE")
66e42d
      || cmp_mpihex (y, "00C13CA9E617C6C8487BFF6A726E3C4F277913D97117939966"))
66e42d
    fail ("computed affine coordinates of public key do not match\n");
66e42d
  if (debug)
66e42d
    {
66e42d
      print_mpi ("q.x", x);
66e42d
      print_mpi ("q.y", y);
66e42d
    }
66e42d
66e42d
  gcry_mpi_release (z);
66e42d
  gcry_mpi_release (y);
66e42d
  gcry_mpi_release (x);
66e42d
  gcry_mpi_point_release (Q);
66e42d
  gcry_mpi_release (d);
66e42d
  gcry_mpi_point_release (G);
66e42d
  gcry_mpi_release (A);
66e42d
  gcry_mpi_release (P);
66e42d
  gcry_ctx_release (ctx);
66e42d
}
66e42d
66e42d
66e42d
/* This is the same as basic_ec_math but uses more advanced
66e42d
   features.  */
66e42d
static void
66e42d
basic_ec_math_simplified (void)
66e42d
{
66e42d
}
66e42d
66e42d
66e42d
/* Check the math used with Twisted Edwards curves.  */
66e42d
static void
66e42d
twistededwards_math (void)
66e42d
{
66e42d
  gpg_error_t err;
66e42d
  gcry_ctx_t ctx;
66e42d
  gcry_mpi_point_t G, Q;
66e42d
  gcry_mpi_t k;
66e42d
  gcry_mpi_t w, a, x, y, z, p, n, b, I;
66e42d
66e42d
  wherestr = "twistededwards_math";
66e42d
  info ("checking basic Twisted Edwards math\n");
66e42d
66e42d
  err = gcry_mpi_ec_new (&ctx, NULL, "Ed25519");
66e42d
  if (err)
66e42d
    die ("gcry_mpi_ec_new failed: %s\n", gpg_strerror (err));
66e42d
66e42d
  k = hex2mpi
66e42d
    ("2D3501E723239632802454EE5DDC406EFB0BDF18486A5BDE9C0390A9C2984004"
66e42d
     "F47252B628C953625B8DEB5DBCB8DA97AA43A1892D11FA83596F42E0D89CB1B6");
66e42d
  G = gcry_mpi_ec_get_point ("g", ctx, 1);
66e42d
  if (!G)
66e42d
    die ("gcry_mpi_ec_get_point(G) failed\n");
66e42d
  Q = gcry_mpi_point_new (0);
66e42d
66e42d
66e42d
  w = gcry_mpi_new (0);
66e42d
  a = gcry_mpi_new (0);
66e42d
  x = gcry_mpi_new (0);
66e42d
  y = gcry_mpi_new (0);
66e42d
  z = gcry_mpi_new (0);
66e42d
  I = gcry_mpi_new (0);
66e42d
  p = gcry_mpi_ec_get_mpi ("p", ctx, 1);
66e42d
  n = gcry_mpi_ec_get_mpi ("n", ctx, 1);
66e42d
  b = gcry_mpi_ec_get_mpi ("b", ctx, 1);
66e42d
66e42d
  /* Check: 2^{p-1} mod p == 1 */
66e42d
  gcry_mpi_sub_ui (a, p, 1);
66e42d
  gcry_mpi_powm (w, GCRYMPI_CONST_TWO, a, p);
66e42d
  if (gcry_mpi_cmp_ui (w, 1))
66e42d
    fail ("failed assertion: 2^{p-1} mod p == 1\n");
66e42d
66e42d
  /* Check: p % 4 == 1 */
66e42d
  gcry_mpi_mod (w, p, GCRYMPI_CONST_FOUR);
66e42d
  if (gcry_mpi_cmp_ui (w, 1))
66e42d
    fail ("failed assertion: p %% 4 == 1\n");
66e42d
66e42d
  /* Check: 2^{n-1} mod n == 1 */
66e42d
  gcry_mpi_sub_ui (a, n, 1);
66e42d
  gcry_mpi_powm (w, GCRYMPI_CONST_TWO, a, n);
66e42d
  if (gcry_mpi_cmp_ui (w, 1))
66e42d
    fail ("failed assertion: 2^{n-1} mod n == 1\n");
66e42d
66e42d
  /* Check: b^{(p-1)/2} mod p == p-1 */
66e42d
  gcry_mpi_sub_ui (a, p, 1);
66e42d
  gcry_mpi_div (x, NULL, a, GCRYMPI_CONST_TWO, -1);
66e42d
  gcry_mpi_powm (w, b, x, p);
66e42d
  gcry_mpi_abs (w);
66e42d
  if (gcry_mpi_cmp (w, a))
66e42d
    fail ("failed assertion: b^{(p-1)/2} mod p == p-1\n");
66e42d
66e42d
  /* I := 2^{(p-1)/4} mod p */
66e42d
  gcry_mpi_sub_ui (a, p, 1);
66e42d
  gcry_mpi_div (x, NULL, a, GCRYMPI_CONST_FOUR, -1);
66e42d
  gcry_mpi_powm (I, GCRYMPI_CONST_TWO, x, p);
66e42d
66e42d
  /* Check: I^2 mod p == p-1 */
66e42d
  gcry_mpi_powm (w, I, GCRYMPI_CONST_TWO, p);
66e42d
  if (gcry_mpi_cmp (w, a))
66e42d
    fail ("failed assertion: I^2 mod p == p-1\n");
66e42d
66e42d
  /* Check: G is on the curve */
66e42d
  if (!gcry_mpi_ec_curve_point (G, ctx))
66e42d
    fail ("failed assertion: G is on the curve\n");
66e42d
66e42d
  /* Check: nG == (0,1) */
66e42d
  gcry_mpi_ec_mul (Q, n, G, ctx);
66e42d
  if (gcry_mpi_ec_get_affine (x, y, Q, ctx))
66e42d
    fail ("failed to get affine coordinates\n");
66e42d
  if (gcry_mpi_cmp_ui (x, 0) || gcry_mpi_cmp_ui (y, 1))
66e42d
    fail ("failed assertion: nG == (0,1)\n");
66e42d
66e42d
  /* Now two arbitrary point operations taken from the ed25519.py
66e42d
     sample data.  */
66e42d
  gcry_mpi_release (a);
66e42d
  a = hex2mpi
66e42d
    ("4f71d012df3c371af3ea4dc38385ca5bb7272f90cb1b008b3ed601c76de1d496"
66e42d
     "e30cbf625f0a756a678d8f256d5325595cccc83466f36db18f0178eb9925edd3");
66e42d
  gcry_mpi_ec_mul (Q, a, G, ctx);
66e42d
  if (gcry_mpi_ec_get_affine (x, y, Q, ctx))
66e42d
    fail ("failed to get affine coordinates\n");
66e42d
  if (cmp_mpihex (x, ("157f7361c577aad36f67ed33e38dc7be"
66e42d
                      "00014fecc2165ca5cee9eee19fe4d2c1"))
66e42d
      || cmp_mpihex (y, ("5a69dbeb232276b38f3f5016547bb2a2"
66e42d
                         "4025645f0b820e72b8cad4f0a909a092")))
66e42d
    {
66e42d
      fail ("sample point multiply failed:\n");
66e42d
      print_mpi ("r", a);
66e42d
      print_mpi ("Rx", x);
66e42d
      print_mpi ("Ry", y);
66e42d
    }
66e42d
66e42d
  gcry_mpi_release (a);
66e42d
  a = hex2mpi
66e42d
    ("2d3501e723239632802454ee5ddc406efb0bdf18486a5bde9c0390a9c2984004"
66e42d
     "f47252b628c953625b8deb5dbcb8da97aa43a1892d11fa83596f42e0d89cb1b6");
66e42d
  gcry_mpi_ec_mul (Q, a, G, ctx);
66e42d
  if (gcry_mpi_ec_get_affine (x, y, Q, ctx))
66e42d
    fail ("failed to get affine coordinates\n");
66e42d
  if (cmp_mpihex (x, ("6218e309d40065fcc338b3127f468371"
66e42d
                      "82324bd01ce6f3cf81ab44e62959c82a"))
66e42d
      || cmp_mpihex (y, ("5501492265e073d874d9e5b81e7f8784"
66e42d
                         "8a826e80cce2869072ac60c3004356e5")))
66e42d
    {
66e42d
      fail ("sample point multiply failed:\n");
66e42d
      print_mpi ("r", a);
66e42d
      print_mpi ("Rx", x);
66e42d
      print_mpi ("Ry", y);
66e42d
    }
66e42d
66e42d
66e42d
  gcry_mpi_release (I);
66e42d
  gcry_mpi_release (b);
66e42d
  gcry_mpi_release (n);
66e42d
  gcry_mpi_release (p);
66e42d
  gcry_mpi_release (w);
66e42d
  gcry_mpi_release (a);
66e42d
  gcry_mpi_release (x);
66e42d
  gcry_mpi_release (y);
66e42d
  gcry_mpi_release (z);
66e42d
  gcry_mpi_point_release (Q);
66e42d
  gcry_mpi_point_release (G);
66e42d
  gcry_mpi_release (k);
66e42d
  gcry_ctx_release (ctx);
66e42d
}
66e42d
66e42d
66e42d
/* Check the point on curve function.  */
66e42d
static void
66e42d
point_on_curve (void)
66e42d
{
66e42d
  static struct {
66e42d
    const char *curve;
66e42d
    int oncurve;      /* Point below is on the curve.  */
66e42d
    const char *qx;
66e42d
    const char *qy;
66e42d
  } t[] = {
66e42d
    {
66e42d
      "NIST P-256", 0,
66e42d
      "015B4F6775D68D4D2E2192C6B8027FC5A3D49957E453CB251155AA3FF5D3EC9974",
66e42d
      "4BC4C87B57A25E1056831208AB5B8F091142F891E9FF19F1E090B030DF1087B3"
66e42d
    }, {
66e42d
      "NIST P-256", 0,
66e42d
      "D22C316E7EBE7B293BD66808E000806F0754398A5D72A4F9BBC21C26EAC0A651",
66e42d
      "3C8DB80CC3CDE5E530D040536E6A58AAB41C33FA70B30896943513FF3690132D"
66e42d
    }, {
66e42d
      "NIST P-256", 0,
66e42d
      "0130F7E7BC52854CA493A0DE87DC4AB3B4343758F2B634F15B10D70DBC0A5A5291",
66e42d
      "86F9CA73C25CE86D54CB21C181AECBB52A5971334FF5040F76CAE9845ED46023"
66e42d
    }, {
66e42d
      "NIST P-256", 1,
66e42d
      "14957B602C7849F28858C7407696F014BC091D6D68C449560B7A38147D6E6A9B",
66e42d
      "A8E09EFEECFE00C797A0848F38B61992D30C61FAB13021E88C8BD3545B3A6C63"
66e42d
    }, {
66e42d
      "NIST P-256", 0,
66e42d
      "923DE4957241DD97780841C76294DB0D4F5DC04C3045081174764D2D32AD2D53",
66e42d
      "01B4B1A2027C02F0F520A3B01E4CE3C668BF481346A74499C5D1044A53E210B600"
66e42d
    }, {
66e42d
      "NIST P-256", 1,
66e42d
      "9021DFAB8B4DAEAADA634AAA26D6E5FFDF8C0476FF5CA31606C870A1B933FB36",
66e42d
      "9AFC65EEB24E46C7B75712EF29A981CB09FAC56E2B81D3ED024748CCAB1CB77E"
66e42d
    }, {
66e42d
      "NIST P-256", 0,
66e42d
      "011529F0B26DE5E0EB2DA4BFB6C149C802CB52EE479DD666553286928A4005E990",
66e42d
      "0EBC63DB2104884456DC0AA81A3F4E99D93B7AE2CD4B1489655EA9BE6289CF9E"
66e42d
    }, {
66e42d
      "NIST P-256", 1,
66e42d
      "216EC5DE8CA989199D31F0DFCD381DCC9270A0785365EC3E34CA347C070A87BE",
66e42d
      "87A88897BA763509ECC1DBE28D9D37F6F4E70E3B99B1CD3C0B934D4190968A6D"
66e42d
    }, {
66e42d
      "NIST P-256", 1,
66e42d
      "7ABAA44ACBC6016FDB52A6F45F6178E65CBFC35F9920D99149CA9999612CE945",
66e42d
      "88F7684BDCDA31EAFB6CAD859F8AB29B5D921D7DB2B34DF7E40CE36235F45B63"
66e42d
    }, {
66e42d
      "NIST P-256", 0,
66e42d
      "E765B4272D211DD0064189B55421FB76BB3A7756364A6CB1627FAED848157A84",
66e42d
      "C13171CFFB243E06B203F0996BBDD16F52292AD11F2DA81106E9C2FD87F4FA0F"
66e42d
    }, {
66e42d
      "NIST P-256", 0,
66e42d
      "EE4999DFC3A1871EE7A592BE26A09BEC9D9B561613EE9EFB6ED42F17985C9CDC",
66e42d
      "8399E967338A7A618336AF70DA67D9CAC1C19267809652F5C5183C8B129E0902"
66e42d
    }, {
66e42d
      "NIST P-256", 0,
66e42d
      "F755D0CF2642A2C7FBACCC8E9E442B8B047A99C6E052B2FA5AB0544B36B4D51C",
66e42d
      "AA080F17657B6565D9A4D94BD260B54D92FEE8DC4A78C4FC9C19209933AF39B0"
66e42d
    } , {
66e42d
      "NIST P-384", 0,
66e42d
      "CBFC7DBEBF15BEAD682549757F9BBA0E3F67669DF13FCE0EBE8024B725B38B00"
66e42d
      "83EC46A8F2FF3203C5C7F8C7E722A5EF",
66e42d
      "0548FE281BEAB18FD1AB86F59B0CA524479A4A81373C83B78AFFD801FAC75922"
66e42d
      "96470753DCF46173C9AA4A8A4C2FBE51"
66e42d
    }, {
66e42d
      "NIST P-384", 0,
66e42d
      "1DC8E054A883DB81EAEDE6C487B26816C927B8196780525A6CA8F675D2557752"
66e42d
      "02CE06CCBE705EA8A38AA2894D4BEEE6",
66e42d
      "010191050E867AFAA96A199FE9C591CF8B853D81486786DA889124881FB39D2F"
66e42d
      "8E0875F4C4BB1E3D0F8535C7A52306FB82"
66e42d
    }, {
66e42d
      "NIST P-384", 1,
66e42d
      "2539FC368CE1D5E464B6C0FBB12D557B712327DB086975255AD7D17F7E7E4F23"
66e42d
      "D719ED4116E2CC907AEB92CF22331A60",
66e42d
      "8843FDBA742CB64323E49CEBE8DD74908CFC9C3AA0015662DFBB7219E92CF32E"
66e42d
      "9FC63F61EF19DE9B3CEA98D163ABF254"
66e42d
    }, {
66e42d
      "NIST P-384", 0,
66e42d
      "0B786DACF400D43575394349EDD9F9CD145FC7EF737A3C5F69B253BE7639DB24"
66e42d
      "EC2F0CA62FF1F90B6515DE356EC2A404",
66e42d
      "225D6B2939CC7F7133F43353946A682C68DAC6BB75EE9CF6BD9A1609FA915692"
66e42d
      "72F4D3A87E88529754E109BB9B61B03B"
66e42d
    }, {
66e42d
      "NIST P-384", 0,
66e42d
      "76C660C9F58CF2051F9F8B06049694AB6FE418009DE6F0A0833BC690CEC06CC2"
66e42d
      "9A440AD51C94CF5BC28817C8C6E2D302",
66e42d
      "012974E5D9E55304ED294AB6C7A3C65B663E67ABC5E6F6C0F6498B519F2F6CA1"
66e42d
      "8306976291F3ADC0B5ABA42DED376EA9A5"
66e42d
    }, {
66e42d
      "NIST P-384", 0,
66e42d
      "23D758B1EDB8E12E9E707C53C131A19D9464B20EE05C99766F5ABDF9F906AD03"
66e42d
      "B958BF28B022E54E320672C4BAD4EEC0",
66e42d
      "01E9E72870C88F4C82A5AB3CC8A3398E8F006BF3EC05FFBB1EFF8AEE88020FEA"
66e42d
      "9E558E9F58ED1D324C9DCBCB4E8F2A5970"
66e42d
    }, {
66e42d
      "NIST P-384", 0,
66e42d
      "D062B96D5A10F715ACF361F99262ABF0F7693A8BB60ECB1DF459CF95750E4293"
66e42d
      "18BCB9FC60499D009F949298F3F9F47B",
66e42d
      "9089C6328E4B39A73D7EE6FAE1A77E48CE354B83BBCE432082C32C8FD6784B86"
66e42d
      "CFE9C552E2E720F5DA5806503D3784CD"
66e42d
    }, {
66e42d
      "NIST P-384", 0,
66e42d
      "2A951D4D6EB35C43D94866280D37365B82441BC84D62CBFF3365CAB1FD0A3E20"
66e42d
      "823CA8F84D2BBF4EA687885437DE7839",
66e42d
      "01CC7D762AFE613F7B5568BC516568A421159C40599E8D52DE10E8F9488931E1"
66e42d
      "69F3656C322DE45C4A70DC6DB9A661E599"
66e42d
    }, {
66e42d
      "NIST P-384", 1,
66e42d
      "A4BAEE6CDAF3AEB69032B3FBA811707C54F5753670DA5173D891547E8CBAEEF3"
66e42d
      "89B92C9A55573A596123415FBFA26991",
66e42d
      "3241EA716583C11C71BB30AF6C5E3A6637956F17ADBBE641BAB52E8539F9FC7B"
66e42d
      "F3B04F46DBFFE08151E0F0950CC70081"
66e42d
    }, {
66e42d
      "NIST P-384", 0,
66e42d
      "5C0E18B0DE3261BCBCFC7B702C2D75CF481336BFBADF420BADC616235C1966AB"
66e42d
      "4C0F876575DDEC1BDB3F3F04061C9AE4",
66e42d
      "E90C78550D1C922F1D8161D8C9C0576E29BD09CA665376FA887D13FA8DF48352"
66e42d
      "D7BBEEFB803F6CC8FC7895E47F348D33"
66e42d
    }, {
66e42d
      "NIST P-384", 1,
66e42d
      "2015864CD50F0A1A50E6401F44191665C19E4AD4B4903EA9EB464E95D1070E36"
66e42d
      "F1D8325E45734D5A0FDD103F4DF6F83E",
66e42d
      "5FB3E9A5C59DD5C5262A8176CB7032A00AE33AED08485884A3E5D68D9EEB990B"
66e42d
      "F26E8D87EC175577E782AD51A6A12C02"
66e42d
    }, {
66e42d
      "NIST P-384", 1,
66e42d
      "56EBF5310EEF5A5D8D001F570A18625383ECD4882B3FC738A69874E7C9D8F89C"
66e42d
      "187BECA23369DFD6C15CC0DA0629958F",
66e42d
      "C1230B349FB662CB762563DB8F9FCB32D5CCA16120681C474D67D279CCA6F6DB"
66e42d
      "73DE6AA96140B5C457B7486E06D318CE"
66e42d
    }, {
66e42d
      "NIST P-521", 0,
66e42d
      "01E4D82EE5CD6DA37080252295EFA273BBBA6952012D0120EAF131E73F1E5024"
66e42d
      "36E3324624471040030E1C345D65490ECEE9B64E03B15B6C7EB69A39C618BAFEED70",
66e42d
      "03EE3A3C88A6933B7B16016BE4CC4E3BF5EA0625CB3DB2604CDCBBD02CABBC90"
66e42d
      "8904D9DB42998F6C5101D4D4318ACFC9643C9CD641F636D1810ED86F1840EA74F3C0"
66e42d
    }, {
66e42d
      "NIST P-521", 0,
66e42d
      "01F3DFCB5433387B6B2E3F74177F4F3D7300F05E1AD49DE112630E27B1C8A437"
66e42d
      "1E742CB020E0039B5477FC897D17332034F9660B3066764EFF5FB440EB8856E782E3",
66e42d
      "02D337616C9D202DC5E290C486F5855CBD6A8470AE62CA96245834CF49257D8D"
66e42d
      "96D4041B15007650DEE668C00DDBF749054256C571F60980AC74D0DBCA7FB96C2F48"
66e42d
    }, {
66e42d
      "NIST P-521", 1,
66e42d
      "822A846606DC9E96452CAC373567A8B57D9ACA15B177F75DD7EF10C635F52CE4"
66e42d
      "EF6ABEEDB90D3F48F50A0C9015A95C955A25C45DE8413DE3BF899B6B1E62CF7CB8",
66e42d
      "0102771B5F3EC8C36838CEC04DCBC28AD1E38C37DAB0EA89B5EE92D21F7A35CE"
66e42d
      "ABC8B155EDC70154D6DFA2E77EC1D8C4A3406A6BD0ECF8F1EE2AC33A02464CB70C97"
66e42d
    }, {
66e42d
      "NIST P-521", 0,
66e42d
      "F733D48467912D1FFE46CF442F27FDD218D190E7B8A829D822DA3B6BAF9B987E"
66e42d
      "5B4BCCE34499248F59EEAF74F63ED15FF73F243C6FC3FD5E5842F6A3BA34C2022D",
66e42d
      "0281AAAD1B7EEBABEB6EC67932CB7E95717AFA3B4CF7A2DB151CD537C419C3A5"
66e42d
      "156ED9160758190B47696CDC15E81BBAD12975283907A571604DB23F702AEA4B38FF"
66e42d
    }, {
66e42d
      "NIST P-521", 0,
66e42d
      "03B1B274175AAEB5907152E5114CCAEADA28A7ADD4A2B1831C3D8302E8596489"
66e42d
      "E2C98B9B8D0CAE98C03BB11E28CE66D4736449758AF58BAFE40EF5A5FA22C9A43117",
66e42d
      "94C5951F81D544E959EDFC5DC1D5F42FE427871D4FB91A43A0B4A6BEA6B35B9E"
66e42d
      "BC5FB444C70BE4FD47B4ED16704F8C86EF019FC47C7FF2271F8B0DDEA9E2D3BCDD"
66e42d
    }, {
66e42d
      "NIST P-521", 1,
66e42d
      "F2248C318055DE37CD706D4FCAF7E7D96737A4A7B6B8067A66DCD58B6B8DFC55"
66e42d
      "90ECE67F6AA67F9C51B57E7B023075F2F42909BF47361CB6881C10F55FB7215B56",
66e42d
      "0162F735CE6A2ADA54CAF96A12D6888C02DE0A74638CF34CE39DABBACA4D651B"
66e42d
      "7E6ED1A65B551B36BAE7BE474BB6E6905ED0E33C7BA2021885027C7C6E40C5613004"
66e42d
    }, {
66e42d
      "NIST P-521", 0,
66e42d
      "9F08E97FEADCF0A391CA1EA4D97B5FE62D3B164593E12027EB967BD6E1FA841A"
66e42d
      "9831158DF164BCAD0BF3ADA96127745E25F349BDDD52EEA1654892B35960C9C023",
66e42d
      "AE2A25F5440F258AFACA6925C4C9F7AEAD3CB67153C4FACB31AC33F58B43A78C"
66e42d
      "B14F682FF726CEE2A6B6F6B481AEEB29A9B3150F02D1CFB764672BA8294C477291"
66e42d
    }, {
66e42d
      "NIST P-521", 0,
66e42d
      "01047B52014748C904980716953206A93F0D01B34CA94A997407FA93FE304F86"
66e42d
      "17BB6E402B2BB8B434C2671ECE953ABE7BADB75713CD9DF950943A33A9A19ACCDABE",
66e42d
      "7433533F098037DEA616337986887D01C5CC8DEC3DC1FDB9CDF7287EF27CC125"
66e42d
      "54FCF3A5E212DF9DAD9F8A3A7173B23FC6E15930704F3AEE1B074BDDB0ED6823E4"
66e42d
    }, {
66e42d
      "NIST P-521", 0,
66e42d
      "01C2A9EBF51592FE6589F618EAADA1697D9B2EC7CE5D48C9E80FC597642B23F1"
66e42d
      "F0EBE953449762BD3F094F57791D9850AFE98BBDA9872BE399B7BDD617860076BB03",
66e42d
      "0B822E27692F63DB8E12C59BB3CCA172B9BBF613CAE5F9D1474186E45E8B26FF"
66e42d
      "962084E1C6BE74821EDBB60941A3B75516F603719563433383812BFEA89EC14B89"
66e42d
    }, {
66e42d
      "NIST P-521", 0,
66e42d
      "99390F342C3F0D46E80C5B65C61E8AA8ACA0B6D4E1352404586364A05D8398E9"
66e42d
      "2BC71A644E8663F0A9B87D0B3ACAEE32F2AB9B321317AD23059D045EBAB91C5D93",
66e42d
      "82FCF93AE4467EB57766F2B150E736636727E7282500CD482DA70D153D195F2B"
66e42d
      "DF9B96D689A0DC1BB9137B41557A33F202F1B71840544CBEFF03072E77E4BB6F0B"
66e42d
    }, {
66e42d
      "NIST P-521", 1,
66e42d
      "018E48E80594FF5496D8CC7DF8A19D6AA18805A4EF4490038AED6A1E9AA18056"
66e42d
      "D0244A97DCF6D132C6804E3F4F369922119544B4C057D783C848FB798B48730A382C",
66e42d
      "01AF510B4F5E1C40BC9C110216D35E7C6D7A2BEE52914FC98258676288449901"
66e42d
      "F27A07EE91DF2D5D79259712906C3E18A990CBF35BCAC41A952820CE2BA8D0220080"
66e42d
    }, {
66e42d
      "NIST P-521", 1,
66e42d
      "ADCEF3539B4BC831DC0AFD173137A4426152058AFBAE06A17FCB89F4DB6E48B5"
66e42d
      "335CB88F8E4DB475A1E390E5656072F06605BFB84CBF9795B7992ECA04A8E10CA1",
66e42d
      "01BCB985AFD6404B9EDA49B6190AAA346BF7D5909CA440C0F7E505C62FAC8635"
66e42d
      "31D3EB7B2AC4DD4F4404E4B12E9D6D3C596179587F3724B1EFFF684CFDB4B21826B9"
66e42d
    }
66e42d
  };
66e42d
  gpg_error_t err;
66e42d
  int tidx;
66e42d
  const char *lastcurve = NULL;
66e42d
  gcry_ctx_t ctx = NULL;
66e42d
  gcry_mpi_t qx = NULL;
66e42d
  gcry_mpi_t qy = NULL;
66e42d
  gcry_mpi_point_t Q;
66e42d
  int oncurve;
66e42d
66e42d
  wherestr = "point_on_curve";
66e42d
  for (tidx=0; tidx < DIM (t); tidx++)
66e42d
    {
66e42d
      if (!t[tidx].curve)
66e42d
        {
66e42d
          if (!lastcurve || !ctx)
66e42d
            die ("invalid test vectors at idx %d\n", tidx);
66e42d
        }
66e42d
      else if (!ctx || !lastcurve || strcmp (t[tidx].curve, lastcurve))
66e42d
        {
66e42d
          lastcurve = t[tidx].curve;
66e42d
          gcry_ctx_release (ctx);
66e42d
          err = gcry_mpi_ec_new (&ctx, NULL, lastcurve);
66e42d
          if (err)
66e42d
            die ("error creating context for curve %s at idx %d: %s\n",
66e42d
                 lastcurve, tidx, gpg_strerror (err));
66e42d
66e42d
          info ("checking points on curve %s\n", lastcurve);
66e42d
        }
66e42d
66e42d
      gcry_mpi_release (qx);
66e42d
      gcry_mpi_release (qy);
66e42d
      qx = hex2mpi (t[tidx].qx);
66e42d
      qy = hex2mpi (t[tidx].qy);
66e42d
66e42d
      Q = gcry_mpi_point_set (NULL, qx, qy, GCRYMPI_CONST_ONE);
66e42d
      if (!Q)
66e42d
        die ("gcry_mpi_point_set(Q) failed at idx %d\n", tidx);
66e42d
66e42d
      oncurve = gcry_mpi_ec_curve_point (Q, ctx);
66e42d
66e42d
      if (t[tidx].oncurve && !oncurve)
66e42d
        {
66e42d
          fail ("point expected on curve but not identified as such (i=%d):\n",
66e42d
                tidx);
66e42d
          print_point ("  Q", Q);
66e42d
        }
66e42d
      else if (!t[tidx].oncurve && oncurve)
66e42d
        {
66e42d
          fail ("point not expected on curve but identified as such (i=%d):\n",
66e42d
                tidx);
66e42d
          print_point ("  Q", Q);
66e42d
        }
66e42d
      gcry_mpi_point_release (Q);
66e42d
    }
66e42d
66e42d
  gcry_mpi_release (qx);
66e42d
  gcry_mpi_release (qy);
66e42d
  gcry_ctx_release (ctx);
66e42d
}
66e42d
66e42d
66e42d
int
66e42d
main (int argc, char **argv)
66e42d
{
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_ENABLE_QUICK_RANDOM, 0);
66e42d
  if (debug)
66e42d
    xgcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
66e42d
  xgcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
66e42d
66e42d
  set_get_point ();
66e42d
  context_alloc ();
66e42d
  context_param ();
66e42d
  basic_ec_math ();
66e42d
  point_on_curve ();
66e42d
66e42d
  /* The tests are for P-192 and ed25519 which are not supported in
66e42d
     FIPS mode.  */
66e42d
  if (!gcry_fips_mode_active())
66e42d
    {
66e42d
      basic_ec_math_simplified ();
66e42d
      twistededwards_math ();
66e42d
    }
66e42d
66e42d
  info ("All tests completed. Errors: %d\n", error_count);
66e42d
  return error_count ? 1 : 0;
66e42d
}