-remove async ecc key generation, not needed
[oweals/gnunet.git] / src / util / test_crypto_rsa.c
index a321e0cc52f6c46be4f687d23b86f2e7d125f42b..6574cc970cbcfe6d0539ed84162f80e561a66a20 100644 (file)
@@ -4,7 +4,7 @@
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 2, or (at your
+     by the Free Software Foundation; either version 3, or (at your
      option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
@@ -25,9 +25,8 @@
  */
 #include "platform.h"
 #include "gnunet_common.h"
-#include "gnunet_crypto_lib.h"
+#include "gnunet_util_lib.h"
 #include "gnunet_signatures.h"
-#include "gnunet_time_lib.h"
 
 #define TESTSTRING "Hello World\0"
 #define MAX_TESTVAL sizeof(struct GNUNET_CRYPTO_AesSessionKey)
 
 #define PERF GNUNET_YES
 
+static struct GNUNET_CRYPTO_RsaPrivateKey *key;
+
+
 static int
 testEncryptDecrypt ()
 {
-  struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
   struct GNUNET_CRYPTO_RsaEncryptedData target;
   char result[MAX_TESTVAL];
@@ -47,85 +48,77 @@ testEncryptDecrypt ()
   struct GNUNET_TIME_Absolute start;
   int ok;
 
-  fprintf (stderr, "W");
-  hostkey = GNUNET_CRYPTO_rsa_key_create ();
-  GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
-
+  FPRINTF (stderr, "%s",  "W");
+  GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
   ok = 0;
   start = GNUNET_TIME_absolute_get ();
   for (i = 0; i < ITER; i++)
+  {
+    FPRINTF (stderr, "%s",  ".");
+    if (GNUNET_SYSERR ==
+        GNUNET_CRYPTO_rsa_encrypt (TESTSTRING, strlen (TESTSTRING) + 1, &pkey,
+                                   &target))
     {
-      fprintf (stderr, ".");
-      if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_encrypt (TESTSTRING,
-                                                      strlen (TESTSTRING) + 1,
-                                                      &pkey, &target))
-        {
-          fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
-          ok++;
-          continue;
-        }
-      if (-1 == GNUNET_CRYPTO_rsa_decrypt (hostkey,
-                                           &target, result,
-                                           strlen (TESTSTRING) + 1))
-        {
-          fprintf (stderr, "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n");
-          ok++;
-          continue;
-
-        }
-      if (strncmp (TESTSTRING, result, strlen (TESTSTRING)) != 0)
-        {
-          printf ("%s != %.*s - testEncryptDecrypt failed!\n",
-                  TESTSTRING, (int) MAX_TESTVAL, result);
-          ok++;
-          continue;
-        }
+      FPRINTF (stderr, "%s",  "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
+      ok++;
+      continue;
     }
-  printf ("%d RSA encrypt/decrypt operations %llums (%d failures)\n",
-          ITER,
-          (unsigned long long)
-          GNUNET_TIME_absolute_get_duration (start).value, ok);
-  GNUNET_CRYPTO_rsa_key_free (hostkey);
+    if (-1 ==
+        GNUNET_CRYPTO_rsa_decrypt (key, &target, result,
+                                   strlen (TESTSTRING) + 1))
+    {
+      FPRINTF (stderr, "%s",  "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n");
+      ok++;
+      continue;
+
+    }
+    if (strncmp (TESTSTRING, result, strlen (TESTSTRING)) != 0)
+    {
+      printf ("%s != %.*s - testEncryptDecrypt failed!\n", TESTSTRING,
+              (int) MAX_TESTVAL, result);
+      ok++;
+      continue;
+    }
+  }
+  printf ("%d RSA encrypt/decrypt operations %s (%d failures)\n", 
+         ITER,
+          GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES), 
+         ok);
   if (ok == 0)
     return GNUNET_OK;
-  else
-    return GNUNET_SYSERR;
+  return GNUNET_SYSERR;
 }
 
+
 #if PERF
 static int
 testEncryptPerformance ()
 {
-  struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
   struct GNUNET_CRYPTO_RsaEncryptedData target;
   int i;
   struct GNUNET_TIME_Absolute start;
   int ok;
 
-  fprintf (stderr, "W");
-  hostkey = GNUNET_CRYPTO_rsa_key_create ();
-  GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
-
+  FPRINTF (stderr, "%s",  "W");
+  GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
   ok = 0;
   start = GNUNET_TIME_absolute_get ();
   for (i = 0; i < ITER; i++)
+  {
+    FPRINTF (stderr, "%s",  ".");
+    if (GNUNET_SYSERR ==
+        GNUNET_CRYPTO_rsa_encrypt (TESTSTRING, strlen (TESTSTRING) + 1, &pkey,
+                                   &target))
     {
-      fprintf (stderr, ".");
-      if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_encrypt (TESTSTRING,
-                                                      strlen (TESTSTRING) + 1,
-                                                      &pkey, &target))
-        {
-          fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
-          ok++;
-          continue;
-        }
+      FPRINTF (stderr, "%s",  "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
+      ok++;
+      continue;
     }
-  printf ("%d RSA encrypt operations %llu ms (%d failures)\n",
-          ITER,
+  }
+  printf ("%d RSA encrypt operations %llu ms (%d failures)\n", ITER,
           (unsigned long long)
-          GNUNET_TIME_absolute_get_duration (start).value, ok);
-  GNUNET_CRYPTO_rsa_key_free (hostkey);
+          GNUNET_TIME_absolute_get_duration (start).rel_value, ok);
   if (ok != 0)
     return GNUNET_SYSERR;
   return GNUNET_OK;
@@ -135,7 +128,6 @@ testEncryptPerformance ()
 static int
 testEncryptDecryptSK ()
 {
-  struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
   struct GNUNET_CRYPTO_RsaEncryptedData target;
   struct GNUNET_CRYPTO_AesSessionKey insk;
@@ -144,47 +136,43 @@ testEncryptDecryptSK ()
   struct GNUNET_TIME_Absolute start;
   int ok;
 
-  fprintf (stderr, "W");
-  hostkey = GNUNET_CRYPTO_rsa_key_create ();
-  GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
-
+  FPRINTF (stderr, "%s",  "W");
+  GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
   ok = 0;
   start = GNUNET_TIME_absolute_get ();
   for (i = 0; i < ITER; i++)
+  {
+    FPRINTF (stderr, "%s",  ".");
+    GNUNET_CRYPTO_aes_create_session_key (&insk);
+    if (GNUNET_SYSERR ==
+        GNUNET_CRYPTO_rsa_encrypt (&insk,
+                                   sizeof (struct GNUNET_CRYPTO_AesSessionKey),
+                                   &pkey, &target))
     {
-      fprintf (stderr, ".");
-      GNUNET_CRYPTO_aes_create_session_key (&insk);
-      if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_encrypt (&insk,
-                                                      sizeof (struct
-                                                              GNUNET_CRYPTO_AesSessionKey),
-                                                      &pkey, &target))
-        {
-          fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
-          ok++;
-          continue;
-        }
-      if (-1 == GNUNET_CRYPTO_rsa_decrypt (hostkey,
-                                           &target, &outsk,
-                                           sizeof (struct
-                                                   GNUNET_CRYPTO_AesSessionKey)))
-        {
-          fprintf (stderr, "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n");
-          ok++;
-          continue;
-        }
-      if (0 !=
-          memcmp (&insk, &outsk, sizeof (struct GNUNET_CRYPTO_AesSessionKey)))
-        {
-          printf ("testEncryptDecryptSK failed!\n");
-          ok++;
-          continue;
-        }
+      FPRINTF (stderr, "%s",  "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
+      ok++;
+      continue;
     }
-  printf ("%d RSA encrypt/decrypt SK operations %llus (%d failures)\n",
-          ITER,
-          (unsigned long long)
-          GNUNET_TIME_absolute_get_duration (start).value, ok);
-  GNUNET_CRYPTO_rsa_key_free (hostkey);
+    if (-1 ==
+        GNUNET_CRYPTO_rsa_decrypt (key, &target, &outsk,
+                                   sizeof (struct GNUNET_CRYPTO_AesSessionKey)))
+    {
+      FPRINTF (stderr, "%s",  "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n");
+      ok++;
+      continue;
+    }
+    if (0 !=
+        memcmp (&insk, &outsk, sizeof (struct GNUNET_CRYPTO_AesSessionKey)))
+    {
+      printf ("testEncryptDecryptSK failed!\n");
+      ok++;
+      continue;
+    }
+  }
+  printf ("%d RSA encrypt/decrypt SK operations %s (%d failures)\n", 
+         ITER,
+          GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES), 
+         ok);
   if (ok != 0)
     return GNUNET_SYSERR;
   return GNUNET_OK;
@@ -194,7 +182,6 @@ testEncryptDecryptSK ()
 static int
 testSignVerify ()
 {
-  struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
   struct GNUNET_CRYPTO_RsaSignature sig;
   struct GNUNET_CRYPTO_RsaSignaturePurpose purp;
   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
@@ -202,44 +189,40 @@ testSignVerify ()
   struct GNUNET_TIME_Absolute start;
   int ok = GNUNET_OK;
 
-  fprintf (stderr, "W");
-  hostkey = GNUNET_CRYPTO_rsa_key_create ();
-  GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
+  FPRINTF (stderr, "%s",  "W");
+  GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
   start = GNUNET_TIME_absolute_get ();
   purp.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose));
   purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
 
   for (i = 0; i < ITER; i++)
+  {
+    FPRINTF (stderr, "%s",  ".");
+    if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (key, &purp, &sig))
     {
-      fprintf (stderr, ".");
-      if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig))
-        {
-          fprintf (stderr, "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
-          ok = GNUNET_SYSERR;
-          continue;
-        }
-      if (GNUNET_SYSERR ==
-          GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST,
-                                    &purp, &sig, &pkey))
-        {
-          printf ("GNUNET_CRYPTO_rsa_verify failed!\n");
-          ok = GNUNET_SYSERR;
-          continue;
-        }
-      if (GNUNET_SYSERR !=
-          GNUNET_CRYPTO_rsa_verify
-          (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_TCP_PING, &purp, &sig, &pkey))
-        {
-          printf ("GNUNET_CRYPTO_rsa_verify failed to fail!\n");
-          ok = GNUNET_SYSERR;
-          continue;
-        }
+      FPRINTF (stderr, "%s",  "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
+      ok = GNUNET_SYSERR;
+      continue;
     }
-  printf ("%d RSA sign/verify operations %llums\n",
-          ITER,
-          (unsigned long long)
-          GNUNET_TIME_absolute_get_duration (start).value);
-  GNUNET_CRYPTO_rsa_key_free (hostkey);
+    if (GNUNET_SYSERR ==
+        GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST, &purp, &sig,
+                                  &pkey))
+    {
+      printf ("GNUNET_CRYPTO_rsa_verify failed!\n");
+      ok = GNUNET_SYSERR;
+      continue;
+    }
+    if (GNUNET_SYSERR !=
+        GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
+                                  &purp, &sig, &pkey))
+    {
+      printf ("GNUNET_CRYPTO_rsa_verify failed to fail!\n");
+      ok = GNUNET_SYSERR;
+      continue;
+    }
+  }
+  printf ("%d RSA sign/verify operations %s\n", ITER,
+          GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
   return ok;
 }
 
@@ -248,7 +231,6 @@ testSignVerify ()
 static int
 testSignPerformance ()
 {
-  struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
   struct GNUNET_CRYPTO_RsaSignaturePurpose purp;
   struct GNUNET_CRYPTO_RsaSignature sig;
   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
@@ -258,24 +240,22 @@ testSignPerformance ()
 
   purp.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose));
   purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
-  fprintf (stderr, "W");
-  hostkey = GNUNET_CRYPTO_rsa_key_create ();
-  GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
+  FPRINTF (stderr, "%s",  "W");
+  GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
   start = GNUNET_TIME_absolute_get ();
   for (i = 0; i < ITER; i++)
+  {
+    FPRINTF (stderr, "%s",  ".");
+    if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (key, &purp, &sig))
     {
-      fprintf (stderr, ".");
-      if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig))
-        {
-          fprintf (stderr, "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
-          ok = GNUNET_SYSERR;
-          continue;
-        }
+      FPRINTF (stderr, "%s",  "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
+      ok = GNUNET_SYSERR;
+      continue;
     }
+  }
   printf ("%d RSA sign operations %llu ms\n", ITER,
           (unsigned long long)
-          GNUNET_TIME_absolute_get_duration (start).value);
-  GNUNET_CRYPTO_rsa_key_free (hostkey);
+          GNUNET_TIME_absolute_get_duration (start).rel_value);
   return ok;
 }
 #endif
@@ -284,36 +264,70 @@ testSignPerformance ()
 static int
 testCreateFromFile ()
 {
-  struct GNUNET_CRYPTO_RsaPrivateKey *key;
   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded p1;
   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded p2;
 
   key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE);
+  GNUNET_assert (NULL != key);
   GNUNET_CRYPTO_rsa_key_get_public (key, &p1);
   GNUNET_CRYPTO_rsa_key_free (key);
   key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE);
+  GNUNET_assert (NULL != key);
   GNUNET_CRYPTO_rsa_key_get_public (key, &p2);
   GNUNET_assert (0 == memcmp (&p1, &p2, sizeof (p1)));
   GNUNET_CRYPTO_rsa_key_free (key);
   GNUNET_assert (0 == UNLINK (KEYFILE));
   key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE);
+  GNUNET_assert (NULL != key);
   GNUNET_CRYPTO_rsa_key_get_public (key, &p2);
   GNUNET_assert (0 != memcmp (&p1, &p2, sizeof (p1)));
-  GNUNET_CRYPTO_rsa_key_free (key);
-  GNUNET_assert (0 == UNLINK (KEYFILE));
   return GNUNET_OK;
 }
 
 
+static void
+key_cont (void *cls,
+         struct GNUNET_CRYPTO_RsaPrivateKey *pk,
+         const char *emsg)
+{
+  const char *txt = cls;
+  struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub1;
+  struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub2;
+
+  GNUNET_assert (0 == strcmp ("ok", txt));
+  GNUNET_CRYPTO_rsa_key_get_public (pk, &pub1);
+  GNUNET_CRYPTO_rsa_key_get_public (key, &pub2);
+  GNUNET_assert (0 == memcmp (&pub1, &pub2, 
+                             sizeof (pub1)));
+  GNUNET_CRYPTO_rsa_key_free (pk);
+}
+
+
+static void
+test_async_creation (void *cls,
+                    const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  struct GNUNET_CRYPTO_RsaKeyGenerationContext *gc;
+
+  gc = GNUNET_CRYPTO_rsa_key_create_start (KEYFILE,
+                                          &key_cont, 
+                                          (void*) "bug");
+  GNUNET_CRYPTO_rsa_key_create_stop (gc);
+  gc = GNUNET_CRYPTO_rsa_key_create_start (KEYFILE,
+                                          &key_cont, 
+                                          (void*) "ok");
+}
+
+
 int
 main (int argc, char *argv[])
 {
   int failureCount = 0;
 
   GNUNET_log_setup ("test-crypto-rsa", "WARNING", NULL);
-  GNUNET_CRYPTO_random_disable_entropy_gathering ();
   if (GNUNET_OK != testCreateFromFile ())
     failureCount++;
+  GNUNET_SCHEDULER_run (&test_async_creation, NULL);
 #if PERF
   if (GNUNET_OK != testEncryptPerformance ())
     failureCount++;
@@ -326,11 +340,13 @@ main (int argc, char *argv[])
     failureCount++;
   if (GNUNET_OK != testSignVerify ())
     failureCount++;
+  GNUNET_CRYPTO_rsa_key_free (key);
+  GNUNET_assert (0 == UNLINK (KEYFILE));
 
   if (failureCount != 0)
-    {
-      printf ("\n\n%d TESTS FAILED!\n\n", failureCount);
-      return -1;
-    }
+  {
+    printf ("\n\n%d TESTS FAILED!\n\n", failureCount);
+    return -1;
+  }
   return 0;
 }                               /* end of main */