-fix ftbfs
[oweals/gnunet.git] / src / sensor / sensor_util_lib_crypto.c
index 7cf5051867254c9e37cc823f2048f5ed31e9f4b0..9a7d77fdac891864e3728e11557969b8a1a0159b 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C)
+     Copyright (C)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -65,7 +65,7 @@ struct GNUNET_SENSOR_crypto_pow_context
   /**
    * Task that calculates the proof-of-work
    */
-  GNUNET_SCHEDULER_TaskIdentifier calculate_pow_task;
+  struct GNUNET_SCHEDULER_Task * calculate_pow_task;
 
   /**
    * Size of msg (allocated after this struct)
@@ -128,7 +128,6 @@ check_pow (void *msg, size_t msg_size, uint64_t pow, int matching_bits)
   char buf[msg_size + sizeof (pow)] GNUNET_ALIGN;
   struct GNUNET_HashCode result;
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Msg size: %" PRIu64 ".\n", msg_size);
   memcpy (buf, &pow, sizeof (pow));
   memcpy (&buf[sizeof (pow)], msg, msg_size);
   pow_hash (buf, sizeof (buf), &result);
@@ -149,13 +148,16 @@ calculate_pow (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   void *callback_cls;
   int sign_result;
 
+  if (0 == cx->pow % 1000)
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking pow %" PRIu64 ".\n", cx->pow);
   if (GNUNET_YES ==
       check_pow (&cx->timestamp,
                  sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
                  sizeof (struct GNUNET_TIME_Absolute) + cx->msg_size, cx->pow,
                  cx->matching_bits))
   {
-    cx->calculate_pow_task = GNUNET_SCHEDULER_NO_TASK;
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "Found pow %" PRIu64 ".\n", cx->pow);
+    cx->calculate_pow_task = NULL;
     result_block =
         GNUNET_malloc (sizeof (struct GNUNET_SENSOR_crypto_pow_block) +
                        cx->msg_size);
@@ -167,8 +169,8 @@ calculate_pow (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
         htonl (GNUNET_SIGNATURE_PURPOSE_SENSOR_ANOMALY_REPORT);
     result_block->purpose.size =
         htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
-        sizeof (struct GNUNET_TIME_Absolute) +
-        sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) + cx->msg_size);
+               sizeof (struct GNUNET_TIME_Absolute) +
+               sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) + cx->msg_size);
     memcpy (&result_block[1], &cx[1], cx->msg_size);
     sign_result =
         GNUNET_CRYPTO_eddsa_sign (&cx->private_key, &result_block->purpose,
@@ -178,6 +180,8 @@ calculate_pow (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     GNUNET_SENSOR_crypto_pow_sign_cancel (cx);
     if (NULL != callback)
       callback (callback_cls, (GNUNET_OK == sign_result) ? result_block : NULL);
+    GNUNET_free (result_block);
+    return;
   }
   cx->pow++;
   cx->calculate_pow_task = GNUNET_SCHEDULER_add_now (&calculate_pow, cx);
@@ -193,7 +197,13 @@ void
 GNUNET_SENSOR_crypto_pow_sign_cancel (struct GNUNET_SENSOR_crypto_pow_context
                                       *cx)
 {
+  if (NULL != cx->calculate_pow_task)
+  {
+    GNUNET_SCHEDULER_cancel (cx->calculate_pow_task);
+    cx->calculate_pow_task = NULL;
+  }
   GNUNET_free (cx);
+  cx = NULL;
 }
 
 
@@ -250,35 +260,38 @@ GNUNET_SENSOR_crypto_pow_sign (void *msg, size_t msg_size,
  * @param block The block received and needs to be verified
  * @param matching_bits Number of leading zeros in the hash used to verify pow
  * @param public_key Public key of the peer that sent this block
- * @param purpose Expected signing purpose
  * @param payload Where to store the pointer to the payload
- * @return Size of the payload
+ * @return Size of the payload, 0 on error
  */
 size_t
 GNUNET_SENSOR_crypto_verify_pow_sign (struct GNUNET_SENSOR_crypto_pow_block *
                                       block, int matching_bits,
                                       struct GNUNET_CRYPTO_EddsaPublicKey *
-                                      public_key, uint32_t purpose,
-                                      void **payload)
+                                      public_key, void **payload)
 {
   /* Check public key */
-  if (0 != memcmp (public_key, &block->public_key, sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)))
+  if (0 !=
+      memcmp (public_key, &block->public_key,
+              sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)))
   {
     LOG (GNUNET_ERROR_TYPE_WARNING, "Public key mismatch.\n");
     return 0;
   }
   /* Check signature */
   if (GNUNET_OK !=
-      GNUNET_CRYPTO_eddsa_verify (purpose, &block->purpose,
-                                  &block->signature, public_key))
+      GNUNET_CRYPTO_eddsa_verify
+      (GNUNET_SIGNATURE_PURPOSE_SENSOR_ANOMALY_REPORT, &block->purpose,
+       &block->signature, public_key))
   {
     LOG (GNUNET_ERROR_TYPE_WARNING, "Invalid signature.\n");
     return 0;
   }
   /* Check pow */
-  if (GNUNET_NO == check_pow (&block->timestamp,
-      sizeof (struct GNUNET_TIME_Absolute) +
-              sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) + block->msg_size, block->pow, matching_bits))
+  if (GNUNET_NO ==
+      check_pow (&block->timestamp,
+                 sizeof (struct GNUNET_TIME_Absolute) +
+                 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) + block->msg_size,
+                 block->pow, matching_bits))
   {
     LOG (GNUNET_ERROR_TYPE_WARNING, "Invalid proof-of-work.\n");
     return 0;