convert fs publish to MQ
[oweals/gnunet.git] / src / secretsharing / gnunet-service-secretsharing.c
index 9425a6c7b1d0fd0844ce3c035ae898f40efc2ea7..660db3b6c987fc4bc104fd67c536a41b06070f72 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2013 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2013 GNUnet e.V.
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -14,8 +14,8 @@
 
      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.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
 
 /**
@@ -409,7 +409,9 @@ time_between (struct GNUNET_TIME_Absolute start,
 static int
 peer_id_cmp (const void *p1, const void *p2)
 {
-  return memcmp (p1, p2, sizeof (struct GNUNET_PeerIdentity));
+  return memcmp (p1,
+                 p2,
+                 sizeof (struct GNUNET_PeerIdentity));
 }
 
 
@@ -427,8 +429,11 @@ peer_find (const struct GNUNET_PeerIdentity *haystack, unsigned int n,
            const struct GNUNET_PeerIdentity *needle)
 {
   unsigned int i;
+
   for (i = 0; i < n; i++)
-    if (0 == memcmp (&haystack[i], needle, sizeof (struct GNUNET_PeerIdentity)))
+    if (0 == memcmp (&haystack[i],
+                     needle,
+                     sizeof (struct GNUNET_PeerIdentity)))
       return i;
   return -1;
 }
@@ -468,8 +473,13 @@ normalize_peers (struct GNUNET_PeerIdentity *listed,
   if (GNUNET_NO == local_peer_in_list)
     normalized[n - 1] = my_peer;
 
-  memcpy (normalized, listed, num_listed * sizeof (struct GNUNET_PeerIdentity));
-  qsort (normalized, n, sizeof (struct GNUNET_PeerIdentity), &peer_id_cmp);
+  memcpy (normalized,
+          listed,
+          num_listed * sizeof (struct GNUNET_PeerIdentity));
+  qsort (normalized,
+         n,
+         sizeof (struct GNUNET_PeerIdentity),
+         &peer_id_cmp);
 
   if (NULL != my_peer_idx)
     *my_peer_idx = peer_find (normalized, n, &my_peer);
@@ -693,7 +703,7 @@ keygen_session_destroy (struct KeygenSession *ks)
  * @param tc unused
  */
 static void
-cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+cleanup_task (void *cls)
 {
   while (NULL != decrypt_sessions_head)
     decrypt_session_destroy (decrypt_sessions_head);
@@ -723,7 +733,7 @@ generate_presecret_polynomial (struct KeygenSession *ks)
     GNUNET_assert (NULL != v);
     // Randomize v such that 0 < v < elgamal_q.
     // The '- 1' is necessary as bitlength(q) = bitlength(p) - 1.
-    do 
+    do
     {
       gcry_mpi_randomize (v, GNUNET_SECRETSHARING_ELGAMAL_BITS - 1, GCRY_WEAK_RANDOM);
     } while ((gcry_mpi_cmp_ui (v, 0) == 0) || (gcry_mpi_cmp (v, elgamal_q) >= 0));
@@ -758,7 +768,7 @@ keygen_round1_new_element (void *cls,
   {
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                 "keygen commit data with wrong size (%u) in consensus, "
-                " %u expected\n",
+                " %lu expected\n",
                 element->size, sizeof (struct GNUNET_SECRETSHARING_KeygenCommitData));
     return;
   }
@@ -1001,7 +1011,19 @@ restore_fair (const struct GNUNET_CRYPTO_PaillierPublicKey *ppub,
     gcry_mpi_mulm (xres, xres, b_1, elgamal_q);
   }
 
-  // FIXME: cleanup!
+  gcry_mpi_release (a_1);
+  gcry_mpi_release (a_2);
+  gcry_mpi_release (b_1);
+  gcry_mpi_release (b_2);
+  gcry_mpi_release (big_a);
+  gcry_mpi_release (big_b);
+  gcry_mpi_release (big_t);
+  gcry_mpi_release (n);
+  gcry_mpi_release (t_1);
+  gcry_mpi_release (t_2);
+  gcry_mpi_release (t);
+  gcry_mpi_release (r);
+  gcry_mpi_release (v);
 }
 
 
@@ -1040,6 +1062,7 @@ verify_fair (const struct GNUNET_CRYPTO_PaillierPublicKey *ppub, const struct GN
   gcry_mpi_t tmp2;
   gcry_mpi_t y;
   gcry_mpi_t big_y;
+  int res;
 
   GNUNET_assert (NULL != (n_sq = gcry_mpi_new (0)));
   GNUNET_assert (NULL != (tmp1 = gcry_mpi_new (0)));
@@ -1068,7 +1091,8 @@ verify_fair (const struct GNUNET_CRYPTO_PaillierPublicKey *ppub, const struct GN
   if (0 == gcry_mpi_cmp (t1, tmp1))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "fair encryption invalid (t1)\n");
-    return GNUNET_NO;
+    res = GNUNET_NO;
+    goto cleanup;
   }
 
   gcry_mpi_powm (big_y, big_y, e, n_sq);
@@ -1086,10 +1110,26 @@ verify_fair (const struct GNUNET_CRYPTO_PaillierPublicKey *ppub, const struct GN
   if (0 == gcry_mpi_cmp (t2, tmp1))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "fair encryption invalid (t2)\n");
-    return GNUNET_NO;
+    res = GNUNET_NO;
+    goto cleanup;
   }
 
-  return GNUNET_YES;
+  res = GNUNET_YES;
+
+cleanup:
+
+  gcry_mpi_release (n);
+  gcry_mpi_release (n_sq);
+  gcry_mpi_release (z);
+  gcry_mpi_release (t1);
+  gcry_mpi_release (t2);
+  gcry_mpi_release (e);
+  gcry_mpi_release (w);
+  gcry_mpi_release (tmp1);
+  gcry_mpi_release (tmp2);
+  gcry_mpi_release (y);
+  gcry_mpi_release (big_y);
+  return res;
 }
 
 
@@ -1296,7 +1336,10 @@ insert_round2_element (struct KeygenSession *ks)
 
   d->purpose.size = htonl (element_size - offsetof (struct GNUNET_SECRETSHARING_KeygenRevealData, purpose));
   d->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_SECRETSHARING_DKG2);
-  GNUNET_CRYPTO_eddsa_sign (my_peer_private_key, &d->purpose, &d->signature);
+  GNUNET_assert (GNUNET_OK ==
+                 GNUNET_CRYPTO_eddsa_sign (my_peer_private_key,
+                                           &d->purpose,
+                                           &d->signature));
 
   GNUNET_CONSENSUS_insert (ks->consensus, element, NULL, NULL);
   GNUNET_free (element); /* FIXME: maybe stack-allocate instead? */
@@ -1348,20 +1391,16 @@ keygen_reveal_get_exp_preshare (struct KeygenSession *ks,
                                 const struct GNUNET_SECRETSHARING_KeygenRevealData *d,
                                 unsigned int idx)
 {
-  unsigned char *pos;
   gcry_mpi_t exp_preshare;
   struct GNUNET_SECRETSHARING_FairEncryption *fe;
 
   GNUNET_assert (idx < ks->num_peers);
-
   fe = keygen_reveal_get_enc_preshare (ks, d, idx);
-  pos += GNUNET_SECRETSHARING_ELGAMAL_BITS / 8 * idx;
   GNUNET_CRYPTO_mpi_scan_unsigned (&exp_preshare, fe->h, GNUNET_SECRETSHARING_ELGAMAL_BITS / 8);
   return exp_preshare;
 }
 
 
-
 static void
 keygen_round2_new_element (void *cls,
                            const struct GNUNET_SET_Element *element)
@@ -1390,7 +1429,7 @@ keygen_round2_new_element (void *cls,
   {
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                 "keygen round2 data with wrong size (%u) in consensus, "
-                " %u expected\n",
+                " %lu expected\n",
                 element->size, expected_element_size);
     return;
   }
@@ -1447,30 +1486,23 @@ keygen_round2_new_element (void *cls,
     gcry_mpi_set_ui (ks->public_key, 1);
   }
   gcry_mpi_mulm (ks->public_key, ks->public_key, public_key_share, elgamal_p);
-  
+
   gcry_mpi_release (public_key_share);
   public_key_share = NULL;
 
   {
     struct GNUNET_SECRETSHARING_FairEncryption *fe = keygen_reveal_get_enc_preshare (ks, d, ks->local_peer_idx);
-    gcry_mpi_t resx;
     GNUNET_assert (NULL != (preshare = gcry_mpi_new (0)));
     GNUNET_CRYPTO_paillier_decrypt (&ks->paillier_private_key,
                                     &ks->info[ks->local_peer_idx].paillier_public_key,
                                     &fe->c,
                                     preshare);
 
-    GNUNET_assert (NULL != (resx = gcry_mpi_new (0)));
-
     // FIXME: not doing the restoration is less expensive
-
-    restore_fair (&ks->info[ks->local_peer_idx].paillier_public_key, fe, preshare, preshare);
-
-    //if (gcry_mpi_cmp (resx, preshare) != 0)
-    //{
-    //  GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "fair encryption restore failed\n");
-    //  return;
-    //}
+    restore_fair (&ks->info[ks->local_peer_idx].paillier_public_key,
+                  fe,
+                  preshare,
+                  preshare);
   }
 
   GNUNET_assert (NULL != (tmp = gcry_mpi_new (0)));
@@ -1521,7 +1553,7 @@ keygen_round2_new_element (void *cls,
       // Using pow(double,double) is a bit sketchy.
       // We count players from 1, but shares from 0.
       gcry_mpi_t tmp;
-      gcry_mpi_set_ui (j_to_k, (unsigned int) pow(j+1, k)); 
+      gcry_mpi_set_ui (j_to_k, (unsigned int) pow(j+1, k));
       tmp = keygen_reveal_get_exp_coeff (ks, d, k);
       gcry_mpi_powm (tmp, tmp, j_to_k, elgamal_p);
       gcry_mpi_mulm (prod, prod, tmp, elgamal_p);
@@ -1553,7 +1585,7 @@ keygen_round2_new_element (void *cls,
     }
 
   }
-  
+
   info->round2_valid = GNUNET_YES;
 
   gcry_mpi_release (preshare);
@@ -1623,7 +1655,10 @@ insert_round1_element (struct KeygenSession *ks)
 
   d->purpose.size = htonl ((sizeof *d) - offsetof (struct GNUNET_SECRETSHARING_KeygenCommitData, purpose));
   d->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_SECRETSHARING_DKG1);
-  GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_eddsa_sign (my_peer_private_key, &d->purpose, &d->signature));
+  GNUNET_assert (GNUNET_OK ==
+                 GNUNET_CRYPTO_eddsa_sign (my_peer_private_key,
+                                           &d->purpose,
+                                           &d->signature));
 
   GNUNET_CONSENSUS_insert (ks->consensus, element, NULL, NULL);
 
@@ -1846,7 +1881,7 @@ decrypt_new_element (void *cls,
   d = element->data;
 
   info = get_decrypt_peer_info (session, &d->peer);
-  
+
   if (NULL == info)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "decrypt element from invalid peer (%s)\n",
@@ -1856,8 +1891,7 @@ decrypt_new_element (void *cls,
 
   if (NULL != info->partial_decryption)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "decrypt element duplicate\n",
-                GNUNET_i2s (&d->peer));
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "decrypt element duplicate\n");
     return;
   }
 
@@ -1871,7 +1905,7 @@ decrypt_new_element (void *cls,
 
 
   GNUNET_CRYPTO_hash (offsetof (struct GNUNET_SECRETSHARING_DecryptData, ciphertext) + (char *) d,
-                      offsetof (struct GNUNET_SECRETSHARING_DecryptData, nizk_response) - 
+                      offsetof (struct GNUNET_SECRETSHARING_DecryptData, nizk_response) -
                           offsetof (struct GNUNET_SECRETSHARING_DecryptData, ciphertext),
                       &challenge_hash);
 
@@ -1912,7 +1946,7 @@ decrypt_new_element (void *cls,
     char *tmp2_str;
     tmp1_str = mpi_to_str (tmp1);
     tmp2_str = mpi_to_str (tmp2);
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "P%u: Received invalid partial decryption from P%u (eqn 1), expected %s got %s\n",
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "P%u: Received invalid partial decryption from P%ld (eqn 1), expected %s got %s\n",
                 session->share->my_peer, info - session->info, tmp1_str, tmp2_str);
     GNUNET_free (tmp1_str);
     GNUNET_free (tmp2_str);
@@ -1928,7 +1962,7 @@ decrypt_new_element (void *cls,
 
   if (0 != gcry_mpi_cmp (tmp1, tmp2))
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "P%u: Received invalid partial decryption from P%u (eqn 2)\n",
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "P%u: Received invalid partial decryption from P%ld (eqn 2)\n",
                 session->share->my_peer, info - session->info);
     goto cleanup;
   }
@@ -2006,7 +2040,7 @@ insert_decrypt_element (struct DecryptSession *ds)
 
   element.data = (void *) &d;
   element.size = sizeof (struct GNUNET_SECRETSHARING_DecryptData);
-  element.type = 0;
+  element.element_type = 0;
 
   d.ciphertext = ds->ciphertext;
   d.peer = my_peer;
@@ -2014,7 +2048,7 @@ insert_decrypt_element (struct DecryptSession *ds)
 
   // create the zero knowledge proof
   // randomly choose beta such that 0 < beta < q
-  do 
+  do
   {
     gcry_mpi_randomize (beta, GNUNET_SECRETSHARING_ELGAMAL_BITS - 1, GCRY_WEAK_RANDOM);
   } while ((gcry_mpi_cmp_ui (beta, 0) == 0) || (gcry_mpi_cmp (beta, elgamal_q) >= 0));
@@ -2027,7 +2061,7 @@ insert_decrypt_element (struct DecryptSession *ds)
 
   // the challenge is the hash of everything up to the response
   GNUNET_CRYPTO_hash (offsetof (struct GNUNET_SECRETSHARING_DecryptData, ciphertext) + (char *) &d,
-                      offsetof (struct GNUNET_SECRETSHARING_DecryptData, nizk_response) - 
+                      offsetof (struct GNUNET_SECRETSHARING_DecryptData, nizk_response) -
                           offsetof (struct GNUNET_SECRETSHARING_DecryptData, ciphertext),
                       &challenge_hash);
 
@@ -2043,11 +2077,15 @@ insert_decrypt_element (struct DecryptSession *ds)
 
   d.purpose.size = htonl (element.size - offsetof (struct GNUNET_SECRETSHARING_DecryptData, purpose));
   d.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_SECRETSHARING_DECRYPTION);
-  
-  GNUNET_CRYPTO_eddsa_sign (my_peer_private_key, &d.purpose, &d.signature);
+
+  GNUNET_assert (GNUNET_OK ==
+                 GNUNET_CRYPTO_eddsa_sign (my_peer_private_key,
+                                           &d.purpose,
+                                           &d.signature));
 
   GNUNET_CONSENSUS_insert (ds->consensus, &element, NULL, NULL);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "P%u: Inserting decrypt element done!\n",
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "P%u: Inserting decrypt element done!\n",
               ds->share->my_peer);
 
   gcry_mpi_release (s);
@@ -2216,8 +2254,8 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
   }
   GNUNET_SERVER_add_handlers (server, handlers);
   GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
-                                NULL);
+  GNUNET_SCHEDULER_add_shutdown (&cleanup_task,
+                                NULL);
 }
 
 
@@ -2235,4 +2273,3 @@ main (int argc, char *const *argv)
           GNUNET_SERVICE_run (argc, argv, "secretsharing",
                               GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
 }
-