gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r34176 - in gnunet/src: include peerstore sensor


From: gnunet
Subject: [GNUnet-SVN] r34176 - in gnunet/src: include peerstore sensor
Date: Tue, 19 Aug 2014 22:32:27 +0200

Author: otarabai
Date: 2014-08-19 22:32:27 +0200 (Tue, 19 Aug 2014)
New Revision: 34176

Added:
   gnunet/src/sensor/test_pow_sign.c
   gnunet/src/sensor/test_pow_sign.conf
Modified:
   gnunet/src/include/gnunet_sensor_util_lib.h
   gnunet/src/peerstore/gnunet-service-peerstore.c
   gnunet/src/sensor/Makefile.am
   gnunet/src/sensor/sensor_util_lib_crypto.c
   gnunet/src/sensor/test_gnunet-service-sensor_reporting.c
Log:
sensor: test case for proof-of-work + fixes


Modified: gnunet/src/include/gnunet_sensor_util_lib.h
===================================================================
--- gnunet/src/include/gnunet_sensor_util_lib.h 2014-08-19 20:22:08 UTC (rev 
34175)
+++ gnunet/src/include/gnunet_sensor_util_lib.h 2014-08-19 20:32:27 UTC (rev 
34176)
@@ -415,10 +415,26 @@
   struct GNUNET_CRYPTO_EddsaSignature signature;
 
   /**
-   * Purpose of signing, data is allocated after this.
+   * Size of the msg component (allocated after this struct)
    */
+  size_t msg_size;
+
+  /**
+   * Purpose of signing.
+   * Data is allocated after this (timestamp, public_key, msg).
+   */
   struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
 
+  /**
+   * First part of data - timestamp
+   */
+  struct GNUNET_TIME_Absolute timestamp;
+
+  /**
+   * Second part of data - Public key
+   */
+  struct GNUNET_CRYPTO_EddsaPublicKey public_key;
+
 };
 
 
@@ -468,6 +484,29 @@
                                GNUNET_SENSOR_UTIL_pow_callback callback,
                                void *callback_cls);
 
+
+/**
+ * Verify that proof-of-work and signature in the given block are valid.
+ * If all valid, a pointer to the payload within the block is set and the size
+ * of the payload is returned.
+ *
+ * **VERY IMPORTANT** : You will still need to verify the timestamp yourself.
+ *
+ * @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
+ */
+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);
+
+
 #if 0                           /* keep Emacsens' auto-indent happy */
 {
 #endif

Modified: gnunet/src/peerstore/gnunet-service-peerstore.c
===================================================================
--- gnunet/src/peerstore/gnunet-service-peerstore.c     2014-08-19 20:22:08 UTC 
(rev 34175)
+++ gnunet/src/peerstore/gnunet-service-peerstore.c     2014-08-19 20:32:27 UTC 
(rev 34176)
@@ -93,12 +93,12 @@
 /**
  * Are we in the process of shutting down the service? #GNUNET_YES / #GNUNET_NO
  */
-int in_shutdown;
+static int in_shutdown;
 
 /**
  * Perform the actual shutdown operations
  */
-void
+static void
 do_shutdown ()
 {
   if (NULL != db_lib_name)

Modified: gnunet/src/sensor/Makefile.am
===================================================================
--- gnunet/src/sensor/Makefile.am       2014-08-19 20:22:08 UTC (rev 34175)
+++ gnunet/src/sensor/Makefile.am       2014-08-19 20:32:27 UTC (rev 34176)
@@ -85,7 +85,8 @@
 
 check_PROGRAMS = \
  test_sensor_api \
- test_gnunet-service-sensor_reporting
+ test_gnunet-service-sensor_reporting \
+ test_pow_sign
 
 if ENABLE_TEST_RUN
 AM_TESTS_ENVIRONMENT=export 
GNUNET_PREFIX=$${GNUNET_PREFIX:address@hidden@};export 
PATH=$${GNUNET_PREFIX:address@hidden@}/bin:$$PATH;
@@ -106,6 +107,13 @@
   $(top_builddir)/src/testbed/libgnunettestbed.la \
   $(top_builddir)/src/peerstore/libgnunetpeerstore.la
 
+test_pow_sign_SOURCES = \
+  test_pow_sign.c
+test_pow_sign_LDADD = \
+  $(top_builddir)/src/util/libgnunetutil.la \
+  $(top_builddir)/src/testbed/libgnunettestbed.la \
+  libgnunetsensorutil.la
+
 pkgsensordir = sensors
 
 install-data-local:

Modified: gnunet/src/sensor/sensor_util_lib_crypto.c
===================================================================
--- gnunet/src/sensor/sensor_util_lib_crypto.c  2014-08-19 20:22:08 UTC (rev 
34175)
+++ gnunet/src/sensor/sensor_util_lib_crypto.c  2014-08-19 20:32:27 UTC (rev 
34176)
@@ -23,7 +23,7 @@
  * @brief senor utilities - crpyto related functions
  * @author Omar Tarabai
  */
-
+#include <inttypes.h>
 #include "platform.h"
 #include "gnunet_util_lib.h"
 #include "gnunet_sensor_util_lib.h"
@@ -38,18 +38,8 @@
 {
 
   /**
-   * Buffer of the complete message to calculate the pow for
+   * Proof-of-work value
    */
-  void *buf;
-
-  /**
-   * Size of buf
-   */
-  size_t buf_size;
-
-  /**
-   * Proof-of-work number
-   */
   uint64_t pow;
 
   /**
@@ -77,6 +67,21 @@
    */
   GNUNET_SCHEDULER_TaskIdentifier calculate_pow_task;
 
+  /**
+   * Size of msg (allocated after this struct)
+   */
+  size_t msg_size;
+
+  /**
+   * Timestamp of the message
+   */
+  struct GNUNET_TIME_Absolute timestamp;
+
+  /**
+   * Public key of the peer sending this message
+   */
+  struct GNUNET_CRYPTO_EddsaPublicKey public_key;
+
 };
 
 
@@ -123,6 +128,7 @@
   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);
@@ -144,17 +150,26 @@
   int sign_result;
 
   if (GNUNET_YES ==
-      check_pow (cx->buf, cx->buf_size, cx->pow, cx->matching_bits))
+      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;
     result_block =
         GNUNET_malloc (sizeof (struct GNUNET_SENSOR_crypto_pow_block) +
-                       cx->buf_size);
+                       cx->msg_size);
+    result_block->msg_size = cx->msg_size;
+    result_block->pow = cx->pow;
+    result_block->timestamp = cx->timestamp;
+    result_block->public_key = cx->public_key;
     result_block->purpose.purpose =
-        GNUNET_SIGNATURE_PURPOSE_SENSOR_ANOMALY_REPORT;
+        htonl (GNUNET_SIGNATURE_PURPOSE_SENSOR_ANOMALY_REPORT);
     result_block->purpose.size =
-        sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) + cx->buf_size;
-    memcpy (&result_block[1], cx->buf, cx->buf_size);
+        htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
+        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,
                                   &result_block->signature);
@@ -178,11 +193,6 @@
 GNUNET_SENSOR_crypto_pow_sign_cancel (struct GNUNET_SENSOR_crypto_pow_context
                                       *cx)
 {
-  if (NULL != cx->buf)
-  {
-    GNUNET_free (cx->buf);
-    cx->buf = NULL;
-  }
   GNUNET_free (cx);
 }
 
@@ -212,15 +222,14 @@
                                void *callback_cls)
 {
   struct GNUNET_SENSOR_crypto_pow_context *cx;
-  void *buf;
-  size_t buf_size;
 
-  buf_size = msg_size + sizeof (*timestamp) + sizeof (*public_key);
-  buf = GNUNET_malloc (buf_size);
-  cx = GNUNET_new (struct GNUNET_SENSOR_crypto_pow_context);
+  cx = GNUNET_malloc (sizeof (struct GNUNET_SENSOR_crypto_pow_context) +
+                      msg_size);
 
-  cx->buf = buf;
-  cx->buf_size = buf_size;
+  cx->timestamp = *timestamp;
+  cx->public_key = *public_key;
+  cx->msg_size = msg_size;
+  memcpy (&cx[1], msg, msg_size);
   cx->pow = 0;
   cx->private_key = *private_key;
   cx->matching_bits = matching_bits;
@@ -236,9 +245,12 @@
  * If all valid, a pointer to the payload within the block is set and the size
  * of the payload is returned.
  *
+ * **VERY IMPORTANT** : You will still need to verify the timestamp yourself.
+ *
  * @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
  */
@@ -246,28 +258,31 @@
 GNUNET_SENSOR_crypto_verify_pow_sign (struct GNUNET_SENSOR_crypto_pow_block *
                                       block, int matching_bits,
                                       struct GNUNET_CRYPTO_EddsaPublicKey *
-                                      public_key, void **payload)
+                                      public_key, uint32_t purpose,
+                                      void **payload)
 {
-  void *msg;
-  size_t msg_size;
-
+  /* Check public key */
+  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 (block->purpose.purpose, &block->purpose,
+      GNUNET_CRYPTO_eddsa_verify (purpose, &block->purpose,
                                   &block->signature, public_key))
   {
     LOG (GNUNET_ERROR_TYPE_WARNING, "Invalid signature.\n");
     return 0;
   }
   /* Check pow */
-  msg = &block[1];
-  msg_size =
-      block->purpose.size - sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose);
-  if (GNUNET_NO == check_pow (msg, 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;
   }
-  *payload = msg;
-  return msg_size;
+  *payload = &block[1];
+  return block->msg_size;
 }

Modified: gnunet/src/sensor/test_gnunet-service-sensor_reporting.c
===================================================================
--- gnunet/src/sensor/test_gnunet-service-sensor_reporting.c    2014-08-19 
20:22:08 UTC (rev 34175)
+++ gnunet/src/sensor/test_gnunet-service-sensor_reporting.c    2014-08-19 
20:32:27 UTC (rev 34176)
@@ -96,27 +96,27 @@
 /**
  * Test name
  */
-const static char *testname = "test_gnunet-service-sensor_reporting";
+static const char *testname = "test_gnunet-service-sensor_reporting";
 
 /**
  * Name of GNUNET config file used in this test
  */
-const static char *cfg_filename = "test_gnunet-service-sensor_reporting.conf";
+static const char *cfg_filename = "test_gnunet-service-sensor_reporting.conf";
 
 /**
  * Test sensor name
  */
-const static char *sensor_name = "test-sensor-statistics";
+static const char *sensor_name = "test-sensor-statistics";
 
 /**
  * Path to read test sensor from
  */
-const static char *sensor_path_src = "test_sensors/test-sensor-statistics";
+static const char *sensor_path_src = "test_sensors/test-sensor-statistics";
 
 /**
  * Path to write new test sensor to
  */
-const static char *sensor_path_dest =
+static const char *sensor_path_dest =
     "/tmp/test-gnunet-service-sensor-reporting/test-sensor-statistics";
 
 /**

Added: gnunet/src/sensor/test_pow_sign.c
===================================================================
--- gnunet/src/sensor/test_pow_sign.c                           (rev 0)
+++ gnunet/src/sensor/test_pow_sign.c   2014-08-19 20:32:27 UTC (rev 34176)
@@ -0,0 +1,199 @@
+  /*
+   * This file is part of GNUnet.
+   * (C)
+   *
+   * 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 3, or (at your
+   * option) any later version.
+   *
+   * GNUnet is distributed in the hope that it will be useful, but
+   * WITHOUT ANY WARRANTY; without even the implied warranty of
+   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   * General Public License for more details.
+   *
+   * You should have received a copy of the GNU General Public License
+   * along with GNUnet; see the file COPYING.  If not, write to the
+   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   * Boston, MA 02111-1307, USA.
+   */
+/**
+ * @file sensor/test_pow_sign.c
+ * @brief testcase for proof-of-work and signature library functions
+ */
+#include <inttypes.h>
+#include "platform.h"
+#include "gnunet_util_lib.h"
+#include "gnunet_sensor_util_lib.h"
+#include "gnunet_testbed_service.h"
+#include "gnunet_signatures.h"
+
+/**
+ * Number of peers to start for the test
+ */
+#define NUM_PEERS 1
+
+/**
+ * Size of the message exchanged
+ */
+#define MSG_SIZE 1024
+
+/**
+ * Number of matching bits to use for generating proof-of-work
+ */
+#define MATCHING_BITS 2
+
+/**
+ * Test timeout
+ */
+#define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 
1)
+
+/**
+ * Test name
+ */
+static const char *testname = "test_pow_sign";
+
+/**
+ * Name of GNUNET config file used in this test
+ */
+static const char *cfg_filename = "test_pow_sign.conf";
+
+/**
+ * Status of the test to be returned by main()
+ */
+static int ok = 1;
+
+/**
+ * Task used to shutdown / expire the test
+ */
+static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
+
+/**
+ * Message to be exchanged
+ */
+static char msg[MSG_SIZE];
+
+/**
+ * Private key of sending peer
+ */
+struct GNUNET_CRYPTO_EddsaPrivateKey *private_key;
+
+/**
+ * Public key of sending peer
+ */
+struct GNUNET_CRYPTO_EddsaPublicKey *public_key;
+
+
+/**
+ * Shutdown task
+ *
+ * @param cls Closure (unused)
+ * @param tc Task context (unused)
+ */
+static void
+do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  if (NULL != private_key)
+  {
+    GNUNET_free (private_key);
+    private_key = NULL;
+  }
+  if (NULL != public_key)
+  {
+    GNUNET_free (public_key);
+    public_key = NULL;
+  }
+  GNUNET_SCHEDULER_shutdown ();
+}
+
+
+static void pow_cb (void *cls, struct GNUNET_SENSOR_crypto_pow_block *block)
+{
+  void *response;
+
+  printf ("Received block:\n"
+      "pow: %" PRIu64 ".\n", block->pow);
+  GNUNET_assert (MSG_SIZE ==
+  GNUNET_SENSOR_crypto_verify_pow_sign (block, MATCHING_BITS,
+      public_key, GNUNET_SIGNATURE_PURPOSE_SENSOR_ANOMALY_REPORT, &response));
+  GNUNET_assert (0 == memcmp(msg, response, MSG_SIZE));
+  ok = 0;
+  GNUNET_SCHEDULER_cancel(shutdown_task);
+  GNUNET_SCHEDULER_add_now (do_shutdown, NULL);
+}
+
+
+/**
+ * Callback to be called when the requested peer information is available
+ *
+ * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
+ * @param op the operation this callback corresponds to
+ * @param pinfo the result; will be NULL if the operation has failed
+ * @param emsg error message if the operation has failed; will be NULL if the
+ *          operation is successfull
+ */
+static void
+peer_info_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
+              const struct GNUNET_TESTBED_PeerInformation *pinfo,
+              const char *emsg)
+{
+  struct GNUNET_TIME_Absolute timestamp;
+
+  /* generate random data block */
+  GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_WEAK, msg, MSG_SIZE);
+  /* get private and public keys */
+  private_key =
+      GNUNET_CRYPTO_eddsa_key_create_from_configuration (pinfo->result.cfg);
+  GNUNET_assert (NULL != private_key);
+  public_key = GNUNET_new (struct GNUNET_CRYPTO_EddsaPublicKey);
+  GNUNET_CRYPTO_eddsa_key_get_public (private_key, public_key);
+  /* create pow and sign */
+  timestamp = GNUNET_TIME_absolute_get();
+  GNUNET_SENSOR_crypto_pow_sign (msg, MSG_SIZE, &timestamp,
+      public_key, private_key, MATCHING_BITS, &pow_cb, NULL);
+}
+
+
+/**
+ * Signature of a main function for a testcase.
+ *
+ * @param cls closure
+ * @param h the run handle
+ * @param num_peers number of peers in 'peers'
+ * @param peers handle to peers run in the testbed.  NULL upon timeout (see
+ *          GNUNET_TESTBED_test_run()).
+ * @param links_succeeded the number of overlay link connection attempts that
+ *          succeeded
+ * @param links_failed the number of overlay link connection attempts that
+ *          failed
+ * @see GNUNET_TESTBED_test_run()
+ */
+static void
+test_master (void *cls, struct GNUNET_TESTBED_RunHandle *h,
+             unsigned int num_peers, struct GNUNET_TESTBED_Peer **peers,
+             unsigned int links_succeeded, unsigned int links_failed)
+{
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "%d peers started. %d links succeeded. %d links failed.\n",
+              num_peers, links_succeeded, links_failed);
+  GNUNET_assert (NUM_PEERS == num_peers);
+  GNUNET_assert (0 == links_failed);
+  /* Schedule test timeout */
+  shutdown_task =
+      GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, &do_shutdown, NULL);
+  GNUNET_TESTBED_peer_get_information (peers[0],
+                                       GNUNET_TESTBED_PIT_CONFIGURATION,
+                                       &peer_info_cb, peers[0]);
+}
+
+
+int
+main (int argc, char *argv[])
+{
+  GNUNET_log_setup (testname, "INFO", NULL);
+  if (GNUNET_OK ==
+      GNUNET_TESTBED_test_run (testname, cfg_filename, NUM_PEERS, 0, NULL, 
NULL,
+                               &test_master, NULL))
+    return ok;
+  return 1;
+}

Added: gnunet/src/sensor/test_pow_sign.conf
===================================================================
--- gnunet/src/sensor/test_pow_sign.conf                                (rev 0)
+++ gnunet/src/sensor/test_pow_sign.conf        2014-08-19 20:32:27 UTC (rev 
34176)
@@ -0,0 +1,2 @@
+[arm]
+DEFAULTSERVICES = core
\ No newline at end of file




reply via email to

[Prev in Thread] Current Thread [Next in Thread]