- fix
[oweals/gnunet.git] / src / util / test_crypto_ecc.c
index 4cb8f1dbc3a20d8269cbabd2a19b009f2a62d634..fb6d5968a6d7f712719b7eebde219503c565cc35 100644 (file)
@@ -27,6 +27,7 @@
 #include "gnunet_common.h"
 #include "gnunet_util_lib.h"
 #include "gnunet_signatures.h"
+#include <gcrypt.h>
 
 #define TESTSTRING "Hello World\0"
 #define MAX_TESTVAL sizeof(struct GNUNET_CRYPTO_AesSessionKey)
@@ -178,13 +179,64 @@ test_async_creation (void *cls,
 }
 
 
+static void
+test_ecdh ()
+{
+  struct GNUNET_CRYPTO_EccPrivateKey *priv1;
+  struct GNUNET_CRYPTO_EccPrivateKey *priv2;
+  struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pub1;
+  struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pub2;
+  struct GNUNET_HashCode ecdh1;
+  struct GNUNET_HashCode ecdh2;
+
+  priv1 = GNUNET_CRYPTO_ecc_key_create ();
+  priv2 = GNUNET_CRYPTO_ecc_key_create ();
+  GNUNET_CRYPTO_ecc_key_get_public (priv1, &pub1);
+  GNUNET_CRYPTO_ecc_key_get_public (priv2, &pub2);
+  GNUNET_CRYPTO_ecc_ecdh (priv1, &pub2, &ecdh1);
+  GNUNET_CRYPTO_ecc_ecdh (priv2, &pub1, &ecdh2);
+  GNUNET_CRYPTO_ecc_key_free (priv1);
+  GNUNET_CRYPTO_ecc_key_free (priv2);
+  GNUNET_assert (0 == memcmp (&ecdh1, &ecdh2,
+                             sizeof (struct GNUNET_HashCode)));
+}
+
+static void
+perf_keygen ()
+{
+  struct GNUNET_TIME_Absolute start;
+  struct GNUNET_CRYPTO_EccPrivateKey *pk;
+  int i;
+
+  start = GNUNET_TIME_absolute_get ();
+  for (i=0;i<10;i++)
+  {
+    fprintf (stderr, ".");
+    pk = GNUNET_CRYPTO_ecc_key_create ();
+    GNUNET_CRYPTO_ecc_key_free (pk);
+  }
+  fprintf (stderr, "\n");
+  printf ("Creating 10 ECC keys took %llu ms\n",
+          (unsigned long long)
+          GNUNET_TIME_absolute_get_duration (start).rel_value);
+}
+
+
+
 int
 main (int argc, char *argv[])
 {
   int failureCount = 0;
 
+  if (!gcry_check_version ("1.5.0"))
+  {
+    FPRINTF (stderr,
+             _
+             ("libgcrypt has not the expected version (version %s is required).\n"),
+             "1.5.0");
+    return 0;
+  }
   GNUNET_log_setup ("test-crypto-ecc", "WARNING", NULL);
-  GNUNET_CRYPTO_random_disable_entropy_gathering ();
   if (GNUNET_OK != testCreateFromFile ())
     failureCount++;
   GNUNET_SCHEDULER_run (&test_async_creation, NULL);
@@ -196,6 +248,8 @@ main (int argc, char *argv[])
     failureCount++;
   GNUNET_CRYPTO_ecc_key_free (key);
   GNUNET_assert (0 == UNLINK (KEYFILE));
+  test_ecdh ();
+  perf_keygen ();
 
   if (failureCount != 0)
   {
@@ -203,4 +257,6 @@ main (int argc, char *argv[])
     return -1;
   }
   return 0;
-}                               /* end of main */
+}
+
+/* end of test_crypto_ecc.c */