-fix time assertion introduce in last patch
[oweals/gnunet.git] / src / util / gnunet-ecc.c
index f2d2756fb6967b7473ebf58f66fe090c719db6f2..66d398aedfb2c866356b31b02276dca607291869 100644 (file)
@@ -44,6 +44,11 @@ static int list_keys_count;
  */
 static int print_public_key;
 
+/**
+ * Flag for printing the output of random example operations.
+ */
+static int print_examples_flag;
+
 /**
  * Flag for printing hash of public key.
  */
@@ -107,6 +112,75 @@ create_keys (const char *fn)
 }
 
 
+static void
+print_hex (char *msg, void *buf, size_t size)
+{
+  size_t i;
+  printf ("%s: ", msg);
+  for (i = 0; i < size; i++)
+  {
+    printf ("%02hhx", ((char *)buf)[i]);
+  }
+  printf ("\n");
+}
+
+
+static void
+print_examples_ecdh ()
+{
+  struct GNUNET_CRYPTO_EcdhePrivateKey *dh_priv1;
+  struct GNUNET_CRYPTO_EcdhePublicKey *dh_pub1;
+  struct GNUNET_CRYPTO_EcdhePrivateKey *dh_priv2;
+  struct GNUNET_CRYPTO_EcdhePublicKey *dh_pub2;
+  struct GNUNET_HashCode hash;
+  char buf[128];
+
+  dh_pub1 = GNUNET_new (struct GNUNET_CRYPTO_EcdhePublicKey);
+  dh_priv1 = GNUNET_CRYPTO_ecdhe_key_create ();
+  dh_pub2 = GNUNET_new (struct GNUNET_CRYPTO_EcdhePublicKey);
+  dh_priv2 = GNUNET_CRYPTO_ecdhe_key_create ();
+  GNUNET_CRYPTO_ecdhe_key_get_public (dh_priv1, dh_pub1);
+  GNUNET_CRYPTO_ecdhe_key_get_public (dh_priv2, dh_pub2);
+
+  GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_priv1, 32, buf, 128));
+  printf ("ECDHE key 1:\n");
+  printf ("private: %s\n", buf);
+  print_hex ("private(hex)", dh_priv1, sizeof *dh_priv1);
+  GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_pub1, 32, buf, 128));
+  printf ("public: %s\n", buf);
+  print_hex ("public(hex)", dh_pub1, sizeof *dh_pub1);
+
+  GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_priv2, 32, buf, 128));
+  printf ("ECDHE key 2:\n");
+  printf ("private: %s\n", buf);
+  print_hex ("private(hex)", dh_priv2, sizeof *dh_priv2);
+  GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_pub2, 32, buf, 128));
+  printf ("public: %s\n", buf);
+  print_hex ("public(hex)", dh_pub2, sizeof *dh_pub2);
+
+  GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_ecc_ecdh (dh_priv1, dh_pub2, &hash));
+  GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (&hash, 64, buf, 128));
+  printf ("ECDH shared secret: %s\n", buf);
+
+  GNUNET_free (dh_priv1);
+  GNUNET_free (dh_priv2);
+  GNUNET_free (dh_pub1);
+  GNUNET_free (dh_pub2);
+}
+
+
+/**
+ * Print some random example operations to stdout.
+ */
+static void
+print_examples ()
+{
+  print_examples_ecdh ();
+  // print_examples_ecdsa ();
+  // print_examples_eddsa ();
+}
+
+
 static void
 print_key (const char *filename)
 {
@@ -121,7 +195,9 @@ print_key (const char *filename)
 
   if (GNUNET_YES != GNUNET_DISK_file_test (filename))
   {
-    fprintf (stderr, _("Hostkeys file not found: %s\n"), filename);
+    fprintf (stderr,
+             _("Hostkeys file `%s' not found\n"),
+             filename);
     return;
   }
 
@@ -130,13 +206,16 @@ print_key (const char *filename)
     fs = 0;
   if (0 == fs)
   {
-    fprintf (stderr, _("Hostkeys file is empty: %s\n"), filename);
+    fprintf (stderr,
+             _("Hostkeys file `%s' is empty\n"),
+             filename);
     return;       /* File is empty */
   }
   if (0 != (fs % GNUNET_TESTING_HOSTKEYFILESIZE))
   {
     fprintf (stderr,
-         _("Incorrect hostkey file format: %s\n"), filename);
+             _("Incorrect hostkey file format: %s\n"),
+             filename);
     return;
   }
   fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
@@ -147,11 +226,13 @@ print_key (const char *filename)
     return;
   }
   hostkeys_data = GNUNET_malloc (fs);
-  if (fs != GNUNET_DISK_file_read(fd, hostkeys_data, fs))
+  if (fs != GNUNET_DISK_file_read (fd, hostkeys_data, fs))
   {
     fprintf (stderr,
-         _("Could not readk hostkey file: %s\n"), filename);
+             _("Could not read hostkey file: %s\n"),
+             filename);
     GNUNET_free (hostkeys_data);
+    GNUNET_DISK_file_close (fd);
     return;
   }
   GNUNET_DISK_file_close (fd);
@@ -190,9 +271,11 @@ static void
 run (void *cls, char *const *args, const char *cfgfile,
      const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
-  struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
-  struct GNUNET_CRYPTO_EddsaPublicKey pub;
-
+  if (print_examples_flag)
+  {
+    print_examples ();
+    return;
+  }
   if (NULL == args[0])
   {
     FPRINTF (stderr,
@@ -202,7 +285,7 @@ run (void *cls, char *const *args, const char *cfgfile,
   }
   if (list_keys)
   {
-    print_key(args[0]);
+    print_key (args[0]);
     return;
   }
   if (make_keys > 0)
@@ -210,32 +293,44 @@ run (void *cls, char *const *args, const char *cfgfile,
     create_keys (args[0]);
     return;
   }
-  pk = GNUNET_CRYPTO_eddsa_key_create_from_file (args[0]);
-  if (NULL == pk)
-    return;
   if (print_public_key)
   {
+    struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
+    struct GNUNET_CRYPTO_EddsaPublicKey pub;
     char *s;
 
+    pk = GNUNET_CRYPTO_eddsa_key_create_from_file (args[0]);
+    if (NULL == pk)
+      return;
     GNUNET_CRYPTO_eddsa_key_get_public (pk, &pub);
     s = GNUNET_CRYPTO_eddsa_public_key_to_string (&pub);
     FPRINTF (stdout,
              "%s\n",
              s);
     GNUNET_free (s);
+    GNUNET_free (pk);
   }
   if (print_peer_identity)
   {
     char *str;
+    struct GNUNET_DISK_FileHandle *keyfile;
+    struct GNUNET_CRYPTO_EddsaPrivateKey pk;
+    struct GNUNET_CRYPTO_EddsaPublicKey pub;
 
-    GNUNET_CRYPTO_eddsa_key_get_public (pk, &pub);
-    str = GNUNET_CRYPTO_eddsa_public_key_to_string (&pub);
-    FPRINTF (stdout,
-             "%s\n",
-             str);
-    GNUNET_free (str);
+    keyfile = GNUNET_DISK_file_open (args[0], GNUNET_DISK_OPEN_READ,
+                                     GNUNET_DISK_PERM_NONE);
+    if (NULL == keyfile)
+      return;
+    while (sizeof (pk) == GNUNET_DISK_file_read (keyfile, &pk, sizeof (pk)))
+    {
+      GNUNET_CRYPTO_eddsa_key_get_public (&pk, &pub);
+      str = GNUNET_CRYPTO_eddsa_public_key_to_string (&pub);
+      FPRINTF (stdout, "%s\n", str);
+      GNUNET_free (str);
+    }
+    GNUNET_DISK_file_close (keyfile);
   }
-  GNUNET_free (pk);
+
 }
 
 
@@ -266,6 +361,9 @@ main (int argc, char *const *argv)
     { 'P', "print-peer-identity", NULL,
       gettext_noop ("print the hash of the public key in ASCII format"),
       0, &GNUNET_GETOPT_set_one, &print_peer_identity },
+    { 'E', "examples", NULL,
+      gettext_noop ("print examples of ECC operations (used for compatibility testing)"),
+      0, &GNUNET_GETOPT_set_one, &print_examples_flag },
     GNUNET_GETOPT_OPTION_END
   };
   int ret;