Returns now GNUNET_SYSERR
[oweals/gnunet.git] / src / util / test_crypto_ksk.c
index efce7cc050629d14df3443481f6cd5a17ff1ba20..74f88d5bf934fc4bc5d937904f7f9fb7d614e274 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
 #define ITER 25
 
 
+static int
+testCorrectKey ()
+{
+  const char *want = "010601000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b73c215f7a5e6b09bec55713c901786c09324a150980e014bdb0d04426934929c3b4971a9711af5455536cd6eeb8bfa004ee904972a737455f53c752987d8c82b755bc02882b44950c4acdc1672ba74c3b94d81a4c1ea3d74e7700ae5594c3a4f3c559e4bff2df6844fac302e4b66175e14dc8bad3ce44281d2fec1a1abef06301010000";
+  GNUNET_HashCode in;
+  struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
+  struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
+  int i;
+  char out[3];
+
+  fprintf (stderr, "Testing KBlock key correctness");
+  GNUNET_CRYPTO_hash ("X", strlen ("X"), &in);
+  hostkey = GNUNET_CRYPTO_rsa_key_create_from_hash (&in);
+  if (hostkey == NULL)
+    {
+      GNUNET_break (0);
+      return GNUNET_SYSERR;
+    }
+  GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
+  GNUNET_CRYPTO_rsa_key_free (hostkey);
+#if 0
+  for (i=0;i<sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);i++)
+    printf("%02x", ((unsigned char*) &pkey)[i]);
+  printf ("\n");
+#endif
+  for (i=0;i<sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);i++)
+    {
+      snprintf(out, sizeof (out), "%02x", ((unsigned char*) &pkey)[i]);
+      if (0 != strncmp (out, &want[i*2], 2))
+       {
+         fprintf (stderr,
+                  " Failed! Wanted %.2s but got %2s at %d\n",
+                  &want[i*2],
+                  out, i);
+         return GNUNET_SYSERR;
+       }
+    }
+  fprintf (stderr, " OK\n");
+  return GNUNET_OK;
+}
+
+
 static int
 testMultiKey (const char *word)
 {
@@ -129,7 +171,7 @@ testEncryptDecrypt (struct GNUNET_CRYPTO_RsaPrivateKey *hostkey)
   printf ("%d RSA encrypt/decrypt operations %llums (%d failures)\n",
           ITER,
           (unsigned long long)
-          GNUNET_TIME_absolute_get_duration (start).value, ok);
+          GNUNET_TIME_absolute_get_duration (start).rel_value, ok);
   if (ok == 0)
     return GNUNET_OK;
   else
@@ -169,8 +211,8 @@ testSignVerify (struct GNUNET_CRYPTO_RsaPrivateKey *hostkey)
           continue;
         }
       if (GNUNET_SYSERR !=
-          GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_HELLO,
-                                    &purp, &sig, &pkey))
+          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;
@@ -180,7 +222,7 @@ testSignVerify (struct GNUNET_CRYPTO_RsaPrivateKey *hostkey)
   printf ("%d RSA sign/verify operations %llums\n",
           ITER,
           (unsigned long long)
-          GNUNET_TIME_absolute_get_duration (start).value);
+          GNUNET_TIME_absolute_get_duration (start).rel_value);
   return ok;
 }
 
@@ -193,14 +235,15 @@ main (int argc, char *argv[])
   struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
 
   GNUNET_log_setup ("test-crypto-ksk", "WARNING", NULL);
-  GNUNET_CRYPTO_hash_create_random (&in);
+  if (GNUNET_OK != testCorrectKey ())
+    failureCount++;
+  GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &in);
   hostkey = GNUNET_CRYPTO_rsa_key_create_from_hash (&in);
   if (hostkey == NULL)
     {
       printf ("\nGNUNET_CRYPTO_rsa_key_create_from_hash failed!\n");
       return 1;
     }
-
   if (GNUNET_OK != testMultiKey ("foo"))
     failureCount++;
   if (GNUNET_OK != testMultiKey ("bar"))