-fix time assertion introduce in last patch
[oweals/gnunet.git] / src / util / gnunet-ecc.c
index 092dc7b38b711c7ad1d71e7faa18ea9cbb07dc42..66d398aedfb2c866356b31b02276dca607291869 100644 (file)
@@ -113,7 +113,20 @@ create_keys (const char *fn)
 
 
 static void
-print_examples_ecdh (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;
@@ -132,14 +145,18 @@ print_examples_ecdh (void)
   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));
@@ -156,7 +173,7 @@ print_examples_ecdh (void)
  * Print some random example operations to stdout.
  */
 static void
-print_examples (void)
+print_examples ()
 {
   print_examples_ecdh ();
   // print_examples_ecdsa ();
@@ -178,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;
   }
 
@@ -187,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,
@@ -204,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);
@@ -247,9 +271,6 @@ 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 ();
@@ -272,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;
-
-    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);
+    struct GNUNET_DISK_FileHandle *keyfile;
+    struct GNUNET_CRYPTO_EddsaPrivateKey pk;
+    struct GNUNET_CRYPTO_EddsaPublicKey pub;
+
+    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);
+
 }