247b211
/* t-mpi-point.c  - Tests for mpi point functions
247b211
 * Copyright (C) 2013 g10 Code GmbH
247b211
 *
247b211
 * This file is part of Libgcrypt.
247b211
 *
247b211
 * Libgcrypt is free software; you can redistribute it and/or modify
247b211
 * it under the terms of the GNU Lesser General Public License as
247b211
 * published by the Free Software Foundation; either version 2.1 of
247b211
 * the License, or (at your option) any later version.
247b211
 *
247b211
 * Libgcrypt is distributed in the hope that it will be useful,
247b211
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
247b211
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
247b211
 * GNU Lesser General Public License for more details.
247b211
 *
247b211
 * You should have received a copy of the GNU Lesser General Public
247b211
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
247b211
 */
247b211
247b211
#ifdef HAVE_CONFIG_H
247b211
# include <config.h>
247b211
#endif
247b211
#include <stdio.h>
247b211
#include <stdlib.h>
247b211
#include <string.h>
247b211
#include <assert.h>
247b211
#include <stdarg.h>
247b211
247b211
#define PGM "t-mpi-point"
0c8865f
#include "t-common.h"
247b211
247b211
static struct
247b211
{
247b211
  const char *desc;           /* Description of the curve.  */
247b211
  const char *p;              /* Order of the prime field.  */
247b211
  const char *a, *b;          /* The coefficients. */
247b211
  const char *n;              /* The order of the base point.  */
247b211
  const char *g_x, *g_y;      /* Base point.  */
2c8c022
  const char *h;              /* Cofactor.  */
247b211
} test_curve[] =
247b211
  {
247b211
    {
2c8c022
      "NIST P-224",
2c8c022
      "0xffffffffffffffffffffffffffffffff000000000000000000000001",
2c8c022
      "0xfffffffffffffffffffffffffffffffefffffffffffffffffffffffe",
2c8c022
      "0xb4050a850c04b3abf54132565044b0b7d7bfd8ba270b39432355ffb4",
2c8c022
      "0xffffffffffffffffffffffffffff16a2e0b8f03e13dd29455c5c2a3d" ,
2c8c022
2c8c022
      "0xb70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21",
2c8c022
      "0xbd376388b5f723fb4c22dfe6cd4375a05a07476444d5819985007e34",
2c8c022
      "0x01"
2c8c022
    },
2c8c022
    {
247b211
      "NIST P-256",
247b211
      "0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff",
247b211
      "0xffffffff00000001000000000000000000000000fffffffffffffffffffffffc",
247b211
      "0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b",
247b211
      "0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551",
247b211
247b211
      "0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296",
2c8c022
      "0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5",
2c8c022
      "0x01"
247b211
    },
247b211
    {
247b211
      "NIST P-384",
247b211
      "0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"
247b211
      "ffffffff0000000000000000ffffffff",
247b211
      "0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"
247b211
      "ffffffff0000000000000000fffffffc",
247b211
      "0xb3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875a"
247b211
      "c656398d8a2ed19d2a85c8edd3ec2aef",
247b211
      "0xffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf"
247b211
      "581a0db248b0a77aecec196accc52973",
247b211
247b211
      "0xaa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a38"
247b211
      "5502f25dbf55296c3a545e3872760ab7",
247b211
      "0x3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c0"
2c8c022
      "0a60b1ce1d7e819d7a431d7c90ea0e5f",
2c8c022
      "0x01"
247b211
    },
247b211
    {
247b211
      "NIST P-521",
247b211
      "0x01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
247b211
      "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
247b211
      "0x01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
247b211
      "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc",
247b211
      "0x051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef10"
247b211
      "9e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00",
247b211
      "0x1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
247b211
      "ffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409",
247b211
247b211
      "0xc6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3d"
247b211
      "baa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66",
247b211
      "0x11839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e6"
2c8c022
      "62c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650",
2c8c022
      "0x01"
247b211
    },
247b211
    {
247b211
      "Ed25519",
247b211
      "0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFED",
2c8c022
      "0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEC",
2c8c022
      "0x52036CEE2B6FFE738CC740797779E89800700A4D4141D8AB75EB4DCA135978A3",
247b211
      "0x1000000000000000000000000000000014DEF9DEA2F79CD65812631A5CF5D3ED",
247b211
      "0x216936D3CD6E53FEC0A4E231FDD6DC5C692CC7609525A7B2C9562D608F25D51A",
2c8c022
      "0x6666666666666666666666666666666666666666666666666666666666666658",
2c8c022
      "0x08"
247b211
    },
2c8c022
    { NULL, NULL, NULL, NULL, NULL, NULL }
247b211
  };
247b211
247b211
/* A sample public key for NIST P-256.  */
247b211
static const char sample_p256_q[] =
247b211
  "04"
247b211
  "42B927242237639A36CE9221B340DB1A9AB76DF2FE3E171277F6A4023DED146E"
247b211
  "E86525E38CCECFF3FB8D152CC6334F70D23A525175C1BCBDDE6E023B2228770E";
247b211
static const char sample_p256_q_x[] =
247b211
  "42B927242237639A36CE9221B340DB1A9AB76DF2FE3E171277F6A4023DED146E";
247b211
static const char sample_p256_q_y[] =
247b211
  "00E86525E38CCECFF3FB8D152CC6334F70D23A525175C1BCBDDE6E023B2228770E";
247b211
247b211
247b211
/* A sample public key for Ed25519.  */
247b211
static const char sample_ed25519_q[] =
247b211
  "04"
247b211
  "55d0e09a2b9d34292297e08d60d0f620c513d47253187c24b12786bd777645ce"
247b211
  "1a5107f7681a02af2523a6daf372e10e3a0764c9d3fe4bd5b70ab18201985ad7";
247b211
static const char sample_ed25519_q_x[] =
247b211
  "55d0e09a2b9d34292297e08d60d0f620c513d47253187c24b12786bd777645ce";
247b211
static const char sample_ed25519_q_y[] =
247b211
  "1a5107f7681a02af2523a6daf372e10e3a0764c9d3fe4bd5b70ab18201985ad7";
247b211
static const char sample_ed25519_q_eddsa[] =
247b211
  "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a";
247b211
static const char sample_ed25519_d[] =
247b211
  "9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60";
247b211
247b211
247b211
static void
247b211
print_mpi_2 (const char *text, const char *text2, gcry_mpi_t a)
247b211
{
247b211
  gcry_error_t err;
247b211
  char *buf;
247b211
  void *bufaddr = &buf;
247b211
247b211
  err = gcry_mpi_aprint (GCRYMPI_FMT_HEX, bufaddr, NULL, a);
247b211
  if (err)
247b211
    fprintf (stderr, "%s%s: [error printing number: %s]\n",
247b211
             text, text2? text2:"", gpg_strerror (err));
247b211
  else
247b211
    {
247b211
      fprintf (stderr, "%s%s: %s\n", text, text2? text2:"", buf);
247b211
      gcry_free (buf);
247b211
    }
247b211
}
247b211
247b211
247b211
static void
247b211
print_mpi (const char *text, gcry_mpi_t a)
247b211
{
247b211
  print_mpi_2 (text, NULL, a);
247b211
}
247b211
247b211
247b211
static void
247b211
print_point (const char *text, gcry_mpi_point_t a)
247b211
{
247b211
  gcry_mpi_t x, y, z;
247b211
247b211
  x = gcry_mpi_new (0);
247b211
  y = gcry_mpi_new (0);
247b211
  z = gcry_mpi_new (0);
247b211
  gcry_mpi_point_get (x, y, z, a);
247b211
  print_mpi_2 (text, ".x", x);
247b211
  print_mpi_2 (text, ".y", y);
247b211
  print_mpi_2 (text, ".z", z);
247b211
  gcry_mpi_release (x);
247b211
  gcry_mpi_release (y);
247b211
  gcry_mpi_release (z);
247b211
}
247b211
247b211
247b211
static void
247b211
print_sexp (const char *prefix, gcry_sexp_t a)
247b211
{
247b211
  char *buf;
247b211
  size_t size;
247b211
247b211
  if (prefix)
247b211
    fputs (prefix, stderr);
247b211
  size = gcry_sexp_sprint (a, GCRYSEXP_FMT_ADVANCED, NULL, 0);
247b211
  buf = gcry_xmalloc (size);
247b211
247b211
  gcry_sexp_sprint (a, GCRYSEXP_FMT_ADVANCED, buf, size);
247b211
  fprintf (stderr, "%.*s", (int)size, buf);
247b211
  gcry_free (buf);
247b211
}
247b211
247b211
247b211
static gcry_mpi_t
247b211
hex2mpi (const char *string)
247b211
{
247b211
  gpg_error_t err;
247b211
  gcry_mpi_t val;
247b211
247b211
  err = gcry_mpi_scan (&val, GCRYMPI_FMT_HEX, string, 0, NULL);
247b211
  if (err)
247b211
    die ("hex2mpi '%s' failed: %s\n", string, gpg_strerror (err));
247b211
  return val;
247b211
}
247b211
247b211
247b211
/* Convert STRING consisting of hex characters into its binary
247b211
   representation and return it as an allocated buffer. The valid
247b211
   length of the buffer is returned at R_LENGTH.  The string is
247b211
   delimited by end of string.  The function returns NULL on
247b211
   error.  */
247b211
static void *
247b211
hex2buffer (const char *string, size_t *r_length)
247b211
{
247b211
  const char *s;
247b211
  unsigned char *buffer;
247b211
  size_t length;
247b211
247b211
  buffer = xmalloc (strlen(string)/2+1);
247b211
  length = 0;
247b211
  for (s=string; *s; s +=2 )
247b211
    {
247b211
      if (!hexdigitp (s) || !hexdigitp (s+1))
247b211
        return NULL;           /* Invalid hex digits. */
247b211
      ((unsigned char*)buffer)[length++] = xtoi_2 (s);
247b211
    }
247b211
  *r_length = length;
247b211
  return buffer;
247b211
}
247b211
247b211
247b211
static gcry_mpi_t
247b211
hex2mpiopa (const char *string)
247b211
{
247b211
  char *buffer;
247b211
  size_t buflen;
247b211
  gcry_mpi_t val;
247b211
247b211
  buffer = hex2buffer (string, &buflen);
247b211
  if (!buffer)
247b211
    die ("hex2mpiopa '%s' failed: parser error\n", string);
247b211
  val = gcry_mpi_set_opaque (NULL, buffer, buflen*8);
247b211
  if (!buffer)
0c8865f
    die ("hex2mpiopa '%s' failed: set_opaque error\n", string);
247b211
  return val;
247b211
}
247b211
247b211
247b211
/* Compare A to B, where B is given as a hex string.  */
247b211
static int
247b211
cmp_mpihex (gcry_mpi_t a, const char *b)
247b211
{
247b211
  gcry_mpi_t bval;
247b211
  int res;
247b211
247b211
  if (gcry_mpi_get_flag (a, GCRYMPI_FLAG_OPAQUE))
247b211
    bval = hex2mpiopa (b);
247b211
  else
247b211
    bval = hex2mpi (b);
247b211
  res = gcry_mpi_cmp (a, bval);
247b211
  gcry_mpi_release (bval);
247b211
  return res;
247b211
}
247b211
247b211
247b211
/* Wrapper to emulate the libgcrypt internal EC context allocation
247b211
   function.  */
247b211
static gpg_error_t
247b211
ec_p_new (gcry_ctx_t *r_ctx, gcry_mpi_t p, gcry_mpi_t a)
247b211
{
247b211
  gpg_error_t err;
247b211
  gcry_sexp_t sexp;
247b211
247b211
  if (p && a)
247b211
    err = gcry_sexp_build (&sexp, NULL, "(ecdsa (p %m)(a %m))", p, a);
247b211
  else if (p)
247b211
    err = gcry_sexp_build (&sexp, NULL, "(ecdsa (p %m))", p);
247b211
  else if (a)
247b211
    err = gcry_sexp_build (&sexp, NULL, "(ecdsa (a %m))", a);
247b211
  else
247b211
    err = gcry_sexp_build (&sexp, NULL, "(ecdsa)");
247b211
  if (err)
247b211
    return err;
247b211
  err = gcry_mpi_ec_new (r_ctx, sexp, NULL);
247b211
  gcry_sexp_release (sexp);
247b211
  return err;
247b211
}
247b211
247b211
247b211

247b211
static void
247b211
set_get_point (void)
247b211
{
0c8865f
  gcry_mpi_point_t point, point2;
247b211
  gcry_mpi_t x, y, z;
247b211
247b211
  wherestr = "set_get_point";
0c8865f
  info ("checking point setting functions\n");
247b211
247b211
  point = gcry_mpi_point_new (0);
247b211
  x = gcry_mpi_set_ui (NULL, 17);
247b211
  y = gcry_mpi_set_ui (NULL, 42);
247b211
  z = gcry_mpi_set_ui (NULL, 11371);
247b211
  gcry_mpi_point_get (x, y, z, point);
247b211
  if (gcry_mpi_cmp_ui (x, 0)
247b211
      || gcry_mpi_cmp_ui (y, 0) || gcry_mpi_cmp_ui (z, 0))
247b211
    fail ("new point not initialized to (0,0,0)\n");
247b211
  gcry_mpi_point_snatch_get (x, y, z, point);
247b211
  point = NULL;
247b211
  if (gcry_mpi_cmp_ui (x, 0)
247b211
      || gcry_mpi_cmp_ui (y, 0) || gcry_mpi_cmp_ui (z, 0))
247b211
    fail ("snatch_get failed\n");
247b211
  gcry_mpi_release (x);
247b211
  gcry_mpi_release (y);
247b211
  gcry_mpi_release (z);
247b211
247b211
  point = gcry_mpi_point_new (0);
247b211
  x = gcry_mpi_set_ui (NULL, 17);
247b211
  y = gcry_mpi_set_ui (NULL, 42);
247b211
  z = gcry_mpi_set_ui (NULL, 11371);
247b211
  gcry_mpi_point_set (point, x, y, z);
247b211
  gcry_mpi_set_ui (x, 23);
247b211
  gcry_mpi_set_ui (y, 24);
247b211
  gcry_mpi_set_ui (z, 25);
247b211
  gcry_mpi_point_get (x, y, z, point);
247b211
  if (gcry_mpi_cmp_ui (x, 17)
247b211
      || gcry_mpi_cmp_ui (y, 42) || gcry_mpi_cmp_ui (z, 11371))
247b211
    fail ("point_set/point_get failed\n");
247b211
  gcry_mpi_point_snatch_set (point, x, y, z);
247b211
  x = gcry_mpi_new (0);
247b211
  y = gcry_mpi_new (0);
247b211
  z = gcry_mpi_new (0);
247b211
  gcry_mpi_point_get (x, y, z, point);
247b211
  if (gcry_mpi_cmp_ui (x, 17)
247b211
      || gcry_mpi_cmp_ui (y, 42) || gcry_mpi_cmp_ui (z, 11371))
247b211
    fail ("point_snatch_set/point_get failed\n");
247b211
0c8865f
  point2 = gcry_mpi_point_copy (point);
0c8865f
0c8865f
  gcry_mpi_point_get (x, y, z, point2);
0c8865f
  if (gcry_mpi_cmp_ui (x, 17)
0c8865f
      || gcry_mpi_cmp_ui (y, 42) || gcry_mpi_cmp_ui (z, 11371))
0c8865f
    fail ("point_copy failed (1)\n");
0c8865f
247b211
  gcry_mpi_point_release (point);
0c8865f
0c8865f
  gcry_mpi_point_get (x, y, z, point2);
0c8865f
  if (gcry_mpi_cmp_ui (x, 17)
0c8865f
      || gcry_mpi_cmp_ui (y, 42) || gcry_mpi_cmp_ui (z, 11371))
0c8865f
    fail ("point_copy failed (2)\n");
0c8865f
0c8865f
  gcry_mpi_point_release (point2);
0c8865f
247b211
  gcry_mpi_release (x);
247b211
  gcry_mpi_release (y);
247b211
  gcry_mpi_release (z);
247b211
}
247b211
247b211
247b211
static void
247b211
context_alloc (void)
247b211
{
247b211
  gpg_error_t err;
247b211
  gcry_ctx_t ctx;
247b211
  gcry_mpi_t p, a;
247b211
247b211
  wherestr = "context_alloc";
0c8865f
  info ("checking context functions\n");
247b211
247b211
  p = gcry_mpi_set_ui (NULL, 1);
247b211
  a = gcry_mpi_set_ui (NULL, 1);
247b211
  err = ec_p_new (&ctx, p, a);
247b211
  if (err)
247b211
    die ("ec_p_new returned an error: %s\n", gpg_strerror (err));
247b211
  gcry_mpi_release (p);
247b211
  gcry_mpi_release (a);
247b211
  gcry_ctx_release (ctx);
247b211
2c8c022
  p = NULL;
247b211
  a = gcry_mpi_set_ui (NULL, 0);
247b211
247b211
  err = ec_p_new (&ctx, p, a);
247b211
  if (!err || gpg_err_code (err) != GPG_ERR_EINVAL)
2c8c022
    fail ("ec_p_new: bad parameter detection failed (1)\n");
247b211
247b211
  gcry_mpi_release (a);
247b211
  a = NULL;
247b211
  err = ec_p_new (&ctx, p, a);
247b211
  if (!err || gpg_err_code (err) != GPG_ERR_EINVAL)
2c8c022
    fail ("ec_p_new: bad parameter detection failed (2)\n");
247b211
247b211
}
247b211
247b211
247b211
static int
247b211
get_and_cmp_mpi (const char *name, const char *mpistring, const char *desc,
247b211
                 gcry_ctx_t ctx)
247b211
{
247b211
  gcry_mpi_t mpi;
247b211
247b211
  mpi = gcry_mpi_ec_get_mpi (name, ctx, 1);
247b211
  if (!mpi)
247b211
    {
247b211
      fail ("error getting parameter '%s' of curve '%s'\n", name, desc);
247b211
      return 1;
247b211
    }
247b211
  if (debug)
247b211
    print_mpi (name, mpi);
247b211
  if (cmp_mpihex (mpi, mpistring))
247b211
    {
247b211
      fail ("parameter '%s' of curve '%s' does not match\n", name, desc);
247b211
      gcry_mpi_release (mpi);
247b211
      return 1;
247b211
    }
247b211
  gcry_mpi_release (mpi);
247b211
  return 0;
247b211
}
247b211
247b211
247b211
static int
247b211
get_and_cmp_point (const char *name,
247b211
                   const char *mpi_x_string, const char *mpi_y_string,
247b211
                   const char *desc, gcry_ctx_t ctx)
247b211
{
247b211
  gcry_mpi_point_t point;
247b211
  gcry_mpi_t x, y, z;
247b211
  int result = 0;
247b211
247b211
  point = gcry_mpi_ec_get_point (name, ctx, 1);
247b211
  if (!point)
247b211
    {
247b211
      fail ("error getting point parameter '%s' of curve '%s'\n", name, desc);
247b211
      return 1;
247b211
    }
247b211
  if (debug)
247b211
    print_point (name, point);
247b211
247b211
  x = gcry_mpi_new (0);
247b211
  y = gcry_mpi_new (0);
247b211
  z = gcry_mpi_new (0);
247b211
  gcry_mpi_point_snatch_get (x, y, z, point);
247b211
  if (cmp_mpihex (x, mpi_x_string))
247b211
    {
247b211
      fail ("x coordinate of '%s' of curve '%s' does not match\n", name, desc);
247b211
      result = 1;
247b211
    }
247b211
  if (cmp_mpihex (y, mpi_y_string))
247b211
    {
247b211
      fail ("y coordinate of '%s' of curve '%s' does not match\n", name, desc);
247b211
      result = 1;
247b211
    }
247b211
  if (cmp_mpihex (z, "01"))
247b211
    {
247b211
      fail ("z coordinate of '%s' of curve '%s' is not 1\n", name, desc);
247b211
      result = 1;
247b211
    }
247b211
  gcry_mpi_release (x);
247b211
  gcry_mpi_release (y);
247b211
  gcry_mpi_release (z);
247b211
  return result;
247b211
}
247b211
247b211
247b211
static void
247b211
context_param (void)
247b211
{
247b211
  gpg_error_t err;
247b211
  int idx;
247b211
  gcry_ctx_t ctx = NULL;
247b211
  gcry_mpi_t q, d;
247b211
  gcry_sexp_t keyparam;
247b211
247b211
  wherestr = "context_param";
247b211
0c8865f
  info ("checking standard curves\n");
247b211
  for (idx=0; test_curve[idx].desc; idx++)
247b211
    {
2c8c022
      /* P-192 and Ed25519 are not supported in fips mode */
2c8c022
      if (gcry_fips_mode_active())
2c8c022
        {
2c8c022
          if (!strcmp(test_curve[idx].desc, "NIST P-192")
2c8c022
              || !strcmp(test_curve[idx].desc, "Ed25519"))
2c8c022
            {
0c8865f
	      info ("skipping %s in fips mode\n", test_curve[idx].desc );
2c8c022
              continue;
2c8c022
            }
2c8c022
        }
2c8c022
247b211
      gcry_ctx_release (ctx);
247b211
      err = gcry_mpi_ec_new (&ctx, NULL, test_curve[idx].desc);
247b211
      if (err)
247b211
        {
247b211
          fail ("can't create context for curve '%s': %s\n",
247b211
                test_curve[idx].desc, gpg_strerror (err));
247b211
          continue;
247b211
        }
247b211
      if (get_and_cmp_mpi ("p", test_curve[idx].p, test_curve[idx].desc, ctx))
247b211
        continue;
247b211
      if (get_and_cmp_mpi ("a", test_curve[idx].a, test_curve[idx].desc, ctx))
247b211
        continue;
247b211
      if (get_and_cmp_mpi ("b", test_curve[idx].b, test_curve[idx].desc, ctx))
247b211
        continue;
247b211
      if (get_and_cmp_mpi ("g.x",test_curve[idx].g_x, test_curve[idx].desc,ctx))
247b211
        continue;
247b211
      if (get_and_cmp_mpi ("g.y",test_curve[idx].g_y, test_curve[idx].desc,ctx))
247b211
        continue;
247b211
      if (get_and_cmp_mpi ("n", test_curve[idx].n, test_curve[idx].desc, ctx))
247b211
        continue;
247b211
      if (get_and_cmp_point ("g", test_curve[idx].g_x, test_curve[idx].g_y,
247b211
                             test_curve[idx].desc, ctx))
247b211
        continue;
2c8c022
      if (get_and_cmp_mpi ("h", test_curve[idx].h, test_curve[idx].desc, ctx))
2c8c022
        continue;
247b211
247b211
    }
247b211
0c8865f
  info ("checking sample public key (nistp256)\n");
247b211
  q = hex2mpi (sample_p256_q);
247b211
  err = gcry_sexp_build (&keyparam, NULL,
247b211
                        "(public-key(ecc(curve %s)(q %m)))",
247b211
                        "NIST P-256", q);
247b211
  if (err)
247b211
    die ("gcry_sexp_build failed: %s\n", gpg_strerror (err));
247b211
  gcry_mpi_release (q);
247b211
247b211
  /* We can't call gcry_pk_testkey because it is only implemented for
247b211
     private keys.  */
247b211
  /* err = gcry_pk_testkey (keyparam); */
247b211
  /* if (err) */
247b211
  /*   fail ("gcry_pk_testkey failed for sample public key: %s\n", */
247b211
  /*         gpg_strerror (err)); */
247b211
247b211
  gcry_ctx_release (ctx);
247b211
  err = gcry_mpi_ec_new (&ctx, keyparam, NULL);
247b211
  if (err)
247b211
    fail ("gcry_mpi_ec_new failed for sample public key (nistp256): %s\n",
247b211
          gpg_strerror (err));
247b211
  else
247b211
    {
247b211
      gcry_sexp_t sexp;
247b211
247b211
      get_and_cmp_mpi ("q", sample_p256_q, "nistp256", ctx);
247b211
      get_and_cmp_point ("q", sample_p256_q_x, sample_p256_q_y, "nistp256",
247b211
                         ctx);
247b211
247b211
      /* Delete Q.  */
247b211
      err = gcry_mpi_ec_set_mpi ("q", NULL, ctx);
247b211
      if (err)
247b211
        fail ("clearing Q for nistp256 failed: %s\n", gpg_strerror (err));
247b211
      if (gcry_mpi_ec_get_mpi ("q", ctx, 0))
247b211
        fail ("clearing Q for nistp256 did not work\n");
247b211
247b211
      /* Set Q again.  */
247b211
      q = hex2mpi (sample_p256_q);
247b211
      err = gcry_mpi_ec_set_mpi ("q", q, ctx);
247b211
      if (err)
247b211
        fail ("setting Q for nistp256 failed: %s\n", gpg_strerror (err));
247b211
      get_and_cmp_mpi ("q", sample_p256_q, "nistp256(2)", ctx);
247b211
      gcry_mpi_release (q);
247b211
247b211
      /* Get as s-expression.  */
247b211
      err = gcry_pubkey_get_sexp (&sexp, 0, ctx);
247b211
      if (err)
247b211
        fail ("gcry_pubkey_get_sexp(0) failed: %s\n", gpg_strerror (err));
247b211
      else if (debug)
247b211
        print_sexp ("Result of gcry_pubkey_get_sexp (0):\n", sexp);
247b211
      gcry_sexp_release (sexp);
247b211
247b211
      err = gcry_pubkey_get_sexp (&sexp, GCRY_PK_GET_PUBKEY, ctx);
247b211
      if (err)
247b211
        fail ("gcry_pubkey_get_sexp(GET_PUBKEY) failed: %s\n",
247b211
              gpg_strerror (err));
247b211
      else if (debug)
247b211
        print_sexp ("Result of gcry_pubkey_get_sexp (GET_PUBKEY):\n", sexp);
247b211
      gcry_sexp_release (sexp);
247b211
247b211
      err = gcry_pubkey_get_sexp (&sexp, GCRY_PK_GET_SECKEY, ctx);
247b211
      if (gpg_err_code (err) != GPG_ERR_NO_SECKEY)
247b211
        fail ("gcry_pubkey_get_sexp(GET_SECKEY) returned wrong error: %s\n",
247b211
              gpg_strerror (err));
247b211
      gcry_sexp_release (sexp);
247b211
    }
247b211
2c8c022
  /* Skipping Ed25519 if in FIPS mode (it isn't supported) */
2c8c022
  if (gcry_fips_mode_active())
2c8c022
    goto cleanup;
2c8c022
0c8865f
  info ("checking sample public key (Ed25519)\n");
247b211
  q = hex2mpi (sample_ed25519_q);
247b211
  gcry_sexp_release (keyparam);
247b211
  err = gcry_sexp_build (&keyparam, NULL,
247b211
                        "(public-key(ecc(curve %s)(flags eddsa)(q %m)))",
247b211
                        "Ed25519", q);
247b211
  if (err)
247b211
    die ("gcry_sexp_build failed: %s\n", gpg_strerror (err));
247b211
  gcry_mpi_release (q);
247b211
247b211
  /* We can't call gcry_pk_testkey because it is only implemented for
247b211
     private keys.  */
247b211
  /* err = gcry_pk_testkey (keyparam); */
247b211
  /* if (err) */
247b211
  /*   fail ("gcry_pk_testkey failed for sample public key: %s\n", */
247b211
  /*         gpg_strerror (err)); */
247b211
247b211
  gcry_ctx_release (ctx);
247b211
  err = gcry_mpi_ec_new (&ctx, keyparam, NULL);
247b211
  if (err)
247b211
    fail ("gcry_mpi_ec_new failed for sample public key: %s\n",
247b211
          gpg_strerror (err));
247b211
  else
247b211
    {
247b211
      gcry_sexp_t sexp;
247b211
247b211
      get_and_cmp_mpi ("q", sample_ed25519_q, "Ed25519", ctx);
247b211
      get_and_cmp_point ("q", sample_ed25519_q_x, sample_ed25519_q_y,
247b211
                         "Ed25519", ctx);
247b211
      get_and_cmp_mpi ("q@eddsa", sample_ed25519_q_eddsa, "Ed25519", ctx);
247b211
247b211
      /* Set d to see whether Q is correctly re-computed.  */
247b211
      d = hex2mpi (sample_ed25519_d);
247b211
      err = gcry_mpi_ec_set_mpi ("d", d, ctx);
247b211
      if (err)
247b211
        fail ("setting d for Ed25519 failed: %s\n", gpg_strerror (err));
247b211
      gcry_mpi_release (d);
247b211
      get_and_cmp_mpi ("q", sample_ed25519_q, "Ed25519(recompute Q)", ctx);
247b211
247b211
      /* Delete Q by setting d and then clearing d.  The clearing is
247b211
         required so that we can check whether Q has been cleared and
247b211
         because further tests only expect a public key.  */
247b211
      d = hex2mpi (sample_ed25519_d);
247b211
      err = gcry_mpi_ec_set_mpi ("d", d, ctx);
247b211
      if (err)
247b211
        fail ("setting d for Ed25519 failed: %s\n", gpg_strerror (err));
247b211
      gcry_mpi_release (d);
247b211
      err = gcry_mpi_ec_set_mpi ("d", NULL, ctx);
247b211
      if (err)
247b211
        fail ("setting d for Ed25519 failed(2): %s\n", gpg_strerror (err));
247b211
      if (gcry_mpi_ec_get_mpi ("q", ctx, 0))
247b211
        fail ("setting d for Ed25519 did not reset Q\n");
247b211
247b211
      /* Set Q again.  We need to use an opaque MPI here because
247b211
         sample_ed25519_q is in uncompressed format which can only be
247b211
         auto-detected if passed opaque.  */
247b211
      q = hex2mpiopa (sample_ed25519_q);
247b211
      err = gcry_mpi_ec_set_mpi ("q", q, ctx);
247b211
      if (err)
247b211
        fail ("setting Q for Ed25519 failed: %s\n", gpg_strerror (err));
247b211
      gcry_mpi_release (q);
247b211
      get_and_cmp_mpi ("q", sample_ed25519_q, "Ed25519(2)", ctx);
247b211
247b211
      /* Get as s-expression.  */
247b211
      err = gcry_pubkey_get_sexp (&sexp, 0, ctx);
247b211
      if (err)
247b211
        fail ("gcry_pubkey_get_sexp(0) failed: %s\n", gpg_strerror (err));
247b211
      else if (debug)
247b211
        print_sexp ("Result of gcry_pubkey_get_sexp (0):\n", sexp);
247b211
      gcry_sexp_release (sexp);
247b211
247b211
      err = gcry_pubkey_get_sexp (&sexp, GCRY_PK_GET_PUBKEY, ctx);
247b211
      if (err)
247b211
        fail ("gcry_pubkey_get_sexp(GET_PUBKEY) failed: %s\n",
247b211
              gpg_strerror (err));
247b211
      else if (debug)
247b211
        print_sexp ("Result of gcry_pubkey_get_sexp (GET_PUBKEY):\n", sexp);
247b211
      gcry_sexp_release (sexp);
247b211
247b211
      err = gcry_pubkey_get_sexp (&sexp, GCRY_PK_GET_SECKEY, ctx);
247b211
      if (gpg_err_code (err) != GPG_ERR_NO_SECKEY)
247b211
        fail ("gcry_pubkey_get_sexp(GET_SECKEY) returned wrong error: %s\n",
247b211
              gpg_strerror (err));
247b211
      gcry_sexp_release (sexp);
247b211
247b211
    }
247b211
2c8c022
 cleanup:
247b211
  gcry_ctx_release (ctx);
247b211
  gcry_sexp_release (keyparam);
247b211
}
247b211
247b211
247b211
247b211
247b211
/* Create a new point from (X,Y,Z) given as hex strings.  */
247b211
gcry_mpi_point_t
247b211
make_point (const char *x, const char *y, const char *z)
247b211
{
247b211
  gcry_mpi_point_t point;
247b211
247b211
  point = gcry_mpi_point_new (0);
247b211
  gcry_mpi_point_snatch_set (point, hex2mpi (x), hex2mpi (y), hex2mpi (z));
247b211
247b211
  return point;
247b211
}
247b211
247b211
247b211
/* This tests checks that the low-level EC API yields the same result
247b211
   as using the high level API.  The values have been taken from a
247b211
   test run using the high level API.  */
247b211
static void
247b211
basic_ec_math (void)
247b211
{
247b211
  gpg_error_t err;
247b211
  gcry_ctx_t ctx;
247b211
  gcry_mpi_t P, A;
247b211
  gcry_mpi_point_t G, Q;
247b211
  gcry_mpi_t d;
247b211
  gcry_mpi_t x, y, z;
247b211
247b211
  wherestr = "basic_ec_math";
0c8865f
  info ("checking basic math functions for EC\n");
247b211
247b211
  P = hex2mpi ("0xfffffffffffffffffffffffffffffffeffffffffffffffff");
247b211
  A = hex2mpi ("0xfffffffffffffffffffffffffffffffefffffffffffffffc");
247b211
  G = make_point ("188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012",
247b211
                  "7192B95FFC8DA78631011ED6B24CDD573F977A11E794811",
247b211
                  "1");
247b211
  d = hex2mpi ("D4EF27E32F8AD8E2A1C6DDEBB1D235A69E3CEF9BCE90273D");
247b211
  Q = gcry_mpi_point_new (0);
247b211
247b211
  err = ec_p_new (&ctx, P, A);
247b211
  if (err)
247b211
    die ("ec_p_new failed: %s\n", gpg_strerror (err));
247b211
247b211
  x = gcry_mpi_new (0);
247b211
  y = gcry_mpi_new (0);
247b211
  z = gcry_mpi_new (0);
247b211
247b211
  {
247b211
    /* A quick check that multiply by zero works.  */
247b211
    gcry_mpi_t tmp;
247b211
247b211
    tmp = gcry_mpi_new (0);
247b211
    gcry_mpi_ec_mul (Q, tmp, G, ctx);
247b211
    gcry_mpi_release (tmp);
247b211
    gcry_mpi_point_get (x, y, z, Q);
247b211
    if (gcry_mpi_cmp_ui (x, 0) || gcry_mpi_cmp_ui (y, 0)
247b211
        || gcry_mpi_cmp_ui (z, 0))
247b211
      fail ("multiply a point by zero failed\n");
247b211
  }
247b211
247b211
  gcry_mpi_ec_mul (Q, d, G, ctx);
247b211
  gcry_mpi_point_get (x, y, z, Q);
247b211
  if (cmp_mpihex (x, "222D9EC717C89D047E0898C9185B033CD11C0A981EE6DC66")
247b211
      || cmp_mpihex (y, "605DE0A82D70D3E0F84A127D0739ED33D657DF0D054BFDE8")
247b211
      || cmp_mpihex (z, "00B06B519071BC536999AC8F2D3934B3C1FC9EACCD0A31F88F"))
247b211
    fail ("computed public key does not match\n");
247b211
  if (debug)
247b211
    {
247b211
      print_mpi ("Q.x", x);
247b211
      print_mpi ("Q.y", y);
247b211
      print_mpi ("Q.z", z);
247b211
    }
247b211
247b211
  if (gcry_mpi_ec_get_affine (x, y, Q, ctx))
247b211
    fail ("failed to get affine coordinates\n");
247b211
  if (cmp_mpihex (x, "008532093BA023F4D55C0424FA3AF9367E05F309DC34CDC3FE")
247b211
      || cmp_mpihex (y, "00C13CA9E617C6C8487BFF6A726E3C4F277913D97117939966"))
247b211
    fail ("computed affine coordinates of public key do not match\n");
247b211
  if (debug)
247b211
    {
247b211
      print_mpi ("q.x", x);
247b211
      print_mpi ("q.y", y);
247b211
    }
247b211
247b211
  gcry_mpi_release (z);
247b211
  gcry_mpi_release (y);
247b211
  gcry_mpi_release (x);
247b211
  gcry_mpi_point_release (Q);
247b211
  gcry_mpi_release (d);
247b211
  gcry_mpi_point_release (G);
247b211
  gcry_mpi_release (A);
247b211
  gcry_mpi_release (P);
247b211
  gcry_ctx_release (ctx);
247b211
}
247b211
247b211
2c8c022
/* This is the same as basic_ec_math but uses more advanced
2c8c022
   features.  */
2c8c022
static void
2c8c022
basic_ec_math_simplified (void)
2c8c022
{
2c8c022
}
2c8c022
2c8c022
247b211
/* Check the math used with Twisted Edwards curves.  */
247b211
static void
247b211
twistededwards_math (void)
247b211
{
247b211
  gpg_error_t err;
247b211
  gcry_ctx_t ctx;
247b211
  gcry_mpi_point_t G, Q;
247b211
  gcry_mpi_t k;
247b211
  gcry_mpi_t w, a, x, y, z, p, n, b, I;
247b211
247b211
  wherestr = "twistededwards_math";
0c8865f
  info ("checking basic Twisted Edwards math\n");
247b211
247b211
  err = gcry_mpi_ec_new (&ctx, NULL, "Ed25519");
247b211
  if (err)
247b211
    die ("gcry_mpi_ec_new failed: %s\n", gpg_strerror (err));
247b211
247b211
  k = hex2mpi
247b211
    ("2D3501E723239632802454EE5DDC406EFB0BDF18486A5BDE9C0390A9C2984004"
247b211
     "F47252B628C953625B8DEB5DBCB8DA97AA43A1892D11FA83596F42E0D89CB1B6");
247b211
  G = gcry_mpi_ec_get_point ("g", ctx, 1);
247b211
  if (!G)
247b211
    die ("gcry_mpi_ec_get_point(G) failed\n");
247b211
  Q = gcry_mpi_point_new (0);
247b211
247b211
247b211
  w = gcry_mpi_new (0);
247b211
  a = gcry_mpi_new (0);
247b211
  x = gcry_mpi_new (0);
247b211
  y = gcry_mpi_new (0);
247b211
  z = gcry_mpi_new (0);
247b211
  I = gcry_mpi_new (0);
247b211
  p = gcry_mpi_ec_get_mpi ("p", ctx, 1);
247b211
  n = gcry_mpi_ec_get_mpi ("n", ctx, 1);
247b211
  b = gcry_mpi_ec_get_mpi ("b", ctx, 1);
247b211
247b211
  /* Check: 2^{p-1} mod p == 1 */
247b211
  gcry_mpi_sub_ui (a, p, 1);
247b211
  gcry_mpi_powm (w, GCRYMPI_CONST_TWO, a, p);
247b211
  if (gcry_mpi_cmp_ui (w, 1))
247b211
    fail ("failed assertion: 2^{p-1} mod p == 1\n");
247b211
247b211
  /* Check: p % 4 == 1 */
247b211
  gcry_mpi_mod (w, p, GCRYMPI_CONST_FOUR);
247b211
  if (gcry_mpi_cmp_ui (w, 1))
0c8865f
    fail ("failed assertion: p %% 4 == 1\n");
247b211
247b211
  /* Check: 2^{n-1} mod n == 1 */
247b211
  gcry_mpi_sub_ui (a, n, 1);
247b211
  gcry_mpi_powm (w, GCRYMPI_CONST_TWO, a, n);
247b211
  if (gcry_mpi_cmp_ui (w, 1))
247b211
    fail ("failed assertion: 2^{n-1} mod n == 1\n");
247b211
247b211
  /* Check: b^{(p-1)/2} mod p == p-1 */
247b211
  gcry_mpi_sub_ui (a, p, 1);
247b211
  gcry_mpi_div (x, NULL, a, GCRYMPI_CONST_TWO, -1);
247b211
  gcry_mpi_powm (w, b, x, p);
247b211
  gcry_mpi_abs (w);
247b211
  if (gcry_mpi_cmp (w, a))
247b211
    fail ("failed assertion: b^{(p-1)/2} mod p == p-1\n");
247b211
247b211
  /* I := 2^{(p-1)/4} mod p */
247b211
  gcry_mpi_sub_ui (a, p, 1);
247b211
  gcry_mpi_div (x, NULL, a, GCRYMPI_CONST_FOUR, -1);
247b211
  gcry_mpi_powm (I, GCRYMPI_CONST_TWO, x, p);
247b211
247b211
  /* Check: I^2 mod p == p-1 */
247b211
  gcry_mpi_powm (w, I, GCRYMPI_CONST_TWO, p);
247b211
  if (gcry_mpi_cmp (w, a))
247b211
    fail ("failed assertion: I^2 mod p == p-1\n");
247b211
247b211
  /* Check: G is on the curve */
247b211
  if (!gcry_mpi_ec_curve_point (G, ctx))
247b211
    fail ("failed assertion: G is on the curve\n");
247b211
247b211
  /* Check: nG == (0,1) */
247b211
  gcry_mpi_ec_mul (Q, n, G, ctx);
247b211
  if (gcry_mpi_ec_get_affine (x, y, Q, ctx))
247b211
    fail ("failed to get affine coordinates\n");
247b211
  if (gcry_mpi_cmp_ui (x, 0) || gcry_mpi_cmp_ui (y, 1))
247b211
    fail ("failed assertion: nG == (0,1)\n");
247b211
247b211
  /* Now two arbitrary point operations taken from the ed25519.py
247b211
     sample data.  */
247b211
  gcry_mpi_release (a);
247b211
  a = hex2mpi
247b211
    ("4f71d012df3c371af3ea4dc38385ca5bb7272f90cb1b008b3ed601c76de1d496"
247b211
     "e30cbf625f0a756a678d8f256d5325595cccc83466f36db18f0178eb9925edd3");
247b211
  gcry_mpi_ec_mul (Q, a, G, ctx);
247b211
  if (gcry_mpi_ec_get_affine (x, y, Q, ctx))
247b211
    fail ("failed to get affine coordinates\n");
247b211
  if (cmp_mpihex (x, ("157f7361c577aad36f67ed33e38dc7be"
247b211
                      "00014fecc2165ca5cee9eee19fe4d2c1"))
247b211
      || cmp_mpihex (y, ("5a69dbeb232276b38f3f5016547bb2a2"
247b211
                         "4025645f0b820e72b8cad4f0a909a092")))
247b211
    {
247b211
      fail ("sample point multiply failed:\n");
247b211
      print_mpi ("r", a);
247b211
      print_mpi ("Rx", x);
247b211
      print_mpi ("Ry", y);
247b211
    }
247b211
247b211
  gcry_mpi_release (a);
247b211
  a = hex2mpi
247b211
    ("2d3501e723239632802454ee5ddc406efb0bdf18486a5bde9c0390a9c2984004"
247b211
     "f47252b628c953625b8deb5dbcb8da97aa43a1892d11fa83596f42e0d89cb1b6");
247b211
  gcry_mpi_ec_mul (Q, a, G, ctx);
247b211
  if (gcry_mpi_ec_get_affine (x, y, Q, ctx))
247b211
    fail ("failed to get affine coordinates\n");
247b211
  if (cmp_mpihex (x, ("6218e309d40065fcc338b3127f468371"
247b211
                      "82324bd01ce6f3cf81ab44e62959c82a"))
247b211
      || cmp_mpihex (y, ("5501492265e073d874d9e5b81e7f8784"
247b211
                         "8a826e80cce2869072ac60c3004356e5")))
247b211
    {
247b211
      fail ("sample point multiply failed:\n");
247b211
      print_mpi ("r", a);
247b211
      print_mpi ("Rx", x);
247b211
      print_mpi ("Ry", y);
247b211
    }
247b211
247b211
247b211
  gcry_mpi_release (I);
247b211
  gcry_mpi_release (b);
247b211
  gcry_mpi_release (n);
247b211
  gcry_mpi_release (p);
247b211
  gcry_mpi_release (w);
247b211
  gcry_mpi_release (a);
247b211
  gcry_mpi_release (x);
247b211
  gcry_mpi_release (y);
247b211
  gcry_mpi_release (z);
247b211
  gcry_mpi_point_release (Q);
247b211
  gcry_mpi_point_release (G);
247b211
  gcry_mpi_release (k);
247b211
  gcry_ctx_release (ctx);
247b211
}
247b211
247b211
247b211
int
247b211
main (int argc, char **argv)
247b211
{
247b211
247b211
  if (argc > 1 && !strcmp (argv[1], "--verbose"))
247b211
    verbose = 1;
247b211
  else if (argc > 1 && !strcmp (argv[1], "--debug"))
247b211
    verbose = debug = 1;
247b211
247b211
  if (!gcry_check_version (GCRYPT_VERSION))
247b211
    die ("version mismatch\n");
247b211
0c8865f
  xgcry_control (GCRYCTL_DISABLE_SECMEM, 0);
0c8865f
  xgcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
247b211
  if (debug)
0c8865f
    xgcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
0c8865f
  xgcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
247b211
247b211
  set_get_point ();
247b211
  context_alloc ();
247b211
  context_param ();
247b211
  basic_ec_math ();
2c8c022
2c8c022
  /* The tests are for P-192 and ed25519 which are not supported in
2c8c022
     FIPS mode.  */
2c8c022
  if (!gcry_fips_mode_active())
2c8c022
    {
2c8c022
      basic_ec_math_simplified ();
2c8c022
      twistededwards_math ();
2c8c022
    }
247b211
0c8865f
  info ("All tests completed. Errors: %d\n", error_count);
247b211
  return error_count ? 1 : 0;
247b211
}