ftbfs
[oweals/gnunet.git] / src / secretsharing / gnunet-service-secretsharing.c
index 1f565cfebc4edbeac5f9bbb350250017eb67a8b1..d136c5a6804026f9d72ef38012727bd3e118260a 100644 (file)
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      Affero General Public License for more details.
-    
+
      You should have received a copy of the GNU Affero General Public License
      along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+     SPDX-License-Identifier: AGPL3.0-or-later
 */
 
 /**
@@ -328,7 +330,7 @@ get_keygen_peer_info (const struct KeygenSession *ks,
 {
   unsigned int i;
   for (i = 0; i < ks->num_peers; i++)
-    if (0 == memcmp (peer, &ks->info[i].peer, sizeof (struct GNUNET_PeerIdentity)))
+    if (0 == GNUNET_memcmp (peer, &ks->info[i].peer))
       return &ks->info[i];
   return NULL;
 }
@@ -347,7 +349,7 @@ get_decrypt_peer_info (const struct DecryptSession *ds,
 {
   unsigned int i;
   for (i = 0; i < ds->share->num_peers; i++)
-    if (0 == memcmp (peer, &ds->info[i].peer, sizeof (struct GNUNET_PeerIdentity)))
+    if (0 == GNUNET_memcmp (peer, &ds->info[i].peer))
       return &ds->info[i];
   return NULL;
 }
@@ -409,9 +411,8 @@ peer_find (const struct GNUNET_PeerIdentity *haystack, unsigned int n,
   unsigned int i;
 
   for (i = 0; i < n; i++)
-    if (0 == memcmp (&haystack[i],
-                     needle,
-                     sizeof (struct GNUNET_PeerIdentity)))
+    if (0 == GNUNET_memcmp (&haystack[i],
+                     needle))
       return i;
   return -1;
 }
@@ -825,7 +826,7 @@ keygen_round2_conclude (void *cls)
                                         GNUNET_SECRETSHARING_ELGAMAL_BITS / 8,
                                         ks->info[i].sigma);
       share->original_indices[i] = j;
-      if (0 == memcmp (&share->peers[i], &my_peer, sizeof (struct GNUNET_PeerIdentity)))
+      if (0 == GNUNET_memcmp (&share->peers[i], &my_peer))
         share->my_peer = j;
       j += 1;
     }
@@ -896,7 +897,9 @@ restore_fair (const struct GNUNET_CRYPTO_PaillierPublicKey *ppub,
   GNUNET_assert (NULL != (big_b = gcry_mpi_new (0)));
 
   // a = (N,0)^T
-  GNUNET_CRYPTO_mpi_scan_unsigned (&a_1, ppub, sizeof (struct GNUNET_CRYPTO_PaillierPublicKey));
+  GNUNET_CRYPTO_mpi_scan_unsigned (&a_1,
+                                   ppub,
+                                   sizeof (struct GNUNET_CRYPTO_PaillierPublicKey));
   GNUNET_assert (NULL != (a_2 = gcry_mpi_new (0)));
   gcry_mpi_set_ui (a_2, 0);
   // b = (x,1)^T
@@ -955,15 +958,9 @@ restore_fair (const struct GNUNET_CRYPTO_PaillierPublicKey *ppub,
     gcry_mpi_set (big_b, big_t);
   }
 
-  {
-    gcry_mpi_t paillier_n;
-
-    GNUNET_CRYPTO_mpi_scan_unsigned (&paillier_n, ppub, sizeof (struct GNUNET_CRYPTO_PaillierPublicKey));
-
-    gcry_mpi_set (xres, b_2);
-    gcry_mpi_invm (xres, xres, elgamal_q);
-    gcry_mpi_mulm (xres, xres, b_1, elgamal_q);
-  }
+  gcry_mpi_set (xres, b_2);
+  gcry_mpi_invm (xres, xres, elgamal_q);
+  gcry_mpi_mulm (xres, xres, b_1, elgamal_q);
 
   gcry_mpi_release (a_1);
   gcry_mpi_release (a_2);
@@ -982,7 +979,8 @@ restore_fair (const struct GNUNET_CRYPTO_PaillierPublicKey *ppub,
 
 
 static void
-get_fair_encryption_challenge (const struct GNUNET_SECRETSHARING_FairEncryption *fe, gcry_mpi_t e)
+get_fair_encryption_challenge (const struct GNUNET_SECRETSHARING_FairEncryption *fe,
+                               gcry_mpi_t *e)
 {
   struct {
     struct GNUNET_CRYPTO_PaillierCiphertext c;
@@ -992,18 +990,27 @@ get_fair_encryption_challenge (const struct GNUNET_SECRETSHARING_FairEncryption
   } hash_data;
   struct GNUNET_HashCode e_hash;
 
+  memset (&hash_data,
+          0,
+          sizeof (hash_data));
   GNUNET_memcpy (&hash_data.c, &fe->c, sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
   GNUNET_memcpy (&hash_data.h, &fe->h, GNUNET_SECRETSHARING_ELGAMAL_BITS / 8);
   GNUNET_memcpy (&hash_data.t1, &fe->t1, GNUNET_SECRETSHARING_ELGAMAL_BITS / 8);
   GNUNET_memcpy (&hash_data.t2, &fe->t2, GNUNET_CRYPTO_PAILLIER_BITS * 2 / 8);
-
-  GNUNET_CRYPTO_mpi_scan_unsigned (&e, &e_hash, sizeof (struct GNUNET_HashCode));
-  gcry_mpi_mod (e, e, elgamal_q);
+  GNUNET_CRYPTO_hash (&hash_data,
+                      sizeof (hash_data),
+                      &e_hash);
+  /* This allocates "e" */
+  GNUNET_CRYPTO_mpi_scan_unsigned (e,
+                                   &e_hash,
+                                   sizeof (struct GNUNET_HashCode));
+  gcry_mpi_mod (*e, *e, elgamal_q);
 }
 
 
 static int
-verify_fair (const struct GNUNET_CRYPTO_PaillierPublicKey *ppub, const struct GNUNET_SECRETSHARING_FairEncryption *fe)
+verify_fair (const struct GNUNET_CRYPTO_PaillierPublicKey *ppub,
+             const struct GNUNET_SECRETSHARING_FairEncryption *fe)
 {
   gcry_mpi_t n;
   gcry_mpi_t n_sq;
@@ -1021,11 +1028,13 @@ verify_fair (const struct GNUNET_CRYPTO_PaillierPublicKey *ppub, const struct GN
   GNUNET_assert (NULL != (n_sq = gcry_mpi_new (0)));
   GNUNET_assert (NULL != (tmp1 = gcry_mpi_new (0)));
   GNUNET_assert (NULL != (tmp2 = gcry_mpi_new (0)));
-  GNUNET_assert (NULL != (e = gcry_mpi_new (0)));
 
-  get_fair_encryption_challenge (fe, e);
+  get_fair_encryption_challenge (fe,
+                                 &e /* this allocates e */);
 
-  GNUNET_CRYPTO_mpi_scan_unsigned (&n, ppub, sizeof (struct GNUNET_CRYPTO_PaillierPublicKey));
+  GNUNET_CRYPTO_mpi_scan_unsigned (&n,
+                                   ppub,
+                                   sizeof (struct GNUNET_CRYPTO_PaillierPublicKey));
   GNUNET_CRYPTO_mpi_scan_unsigned (&t1, fe->t1, GNUNET_CRYPTO_PAILLIER_BITS / 8);
   GNUNET_CRYPTO_mpi_scan_unsigned (&z, fe->z, GNUNET_SECRETSHARING_ELGAMAL_BITS / 8);
   GNUNET_CRYPTO_mpi_scan_unsigned (&y, fe->h, GNUNET_SECRETSHARING_ELGAMAL_BITS / 8);
@@ -1094,7 +1103,9 @@ cleanup:
  * @param[out] fe the fair encryption
  */
 static void
-encrypt_fair (gcry_mpi_t v, const struct GNUNET_CRYPTO_PaillierPublicKey *ppub, struct GNUNET_SECRETSHARING_FairEncryption *fe)
+encrypt_fair (gcry_mpi_t v,
+              const struct GNUNET_CRYPTO_PaillierPublicKey *ppub,
+              struct GNUNET_SECRETSHARING_FairEncryption *fe)
 {
   gcry_mpi_t r;
   gcry_mpi_t s;
@@ -1109,6 +1120,7 @@ encrypt_fair (gcry_mpi_t v, const struct GNUNET_CRYPTO_PaillierPublicKey *ppub,
   gcry_mpi_t Y;
   gcry_mpi_t G;
   gcry_mpi_t h;
+
   GNUNET_assert (NULL != (r = gcry_mpi_new (0)));
   GNUNET_assert (NULL != (s = gcry_mpi_new (0)));
   GNUNET_assert (NULL != (t1 = gcry_mpi_new (0)));
@@ -1116,13 +1128,14 @@ encrypt_fair (gcry_mpi_t v, const struct GNUNET_CRYPTO_PaillierPublicKey *ppub,
   GNUNET_assert (NULL != (z = gcry_mpi_new (0)));
   GNUNET_assert (NULL != (w = gcry_mpi_new (0)));
   GNUNET_assert (NULL != (n_sq = gcry_mpi_new (0)));
-  GNUNET_assert (NULL != (e = gcry_mpi_new (0)));
   GNUNET_assert (NULL != (u = gcry_mpi_new (0)));
   GNUNET_assert (NULL != (Y = gcry_mpi_new (0)));
   GNUNET_assert (NULL != (G = gcry_mpi_new (0)));
   GNUNET_assert (NULL != (h = gcry_mpi_new (0)));
 
-  GNUNET_CRYPTO_mpi_scan_unsigned (&n, ppub, sizeof (struct GNUNET_CRYPTO_PaillierPublicKey));
+  GNUNET_CRYPTO_mpi_scan_unsigned (&n,
+                                   ppub,
+                                   sizeof (struct GNUNET_CRYPTO_PaillierPublicKey));
   gcry_mpi_mul (n_sq, n, n);
   gcry_mpi_add_ui (G, n, 1);
 
@@ -1168,8 +1181,8 @@ encrypt_fair (gcry_mpi_t v, const struct GNUNET_CRYPTO_PaillierPublicKey *ppub,
                                     GNUNET_CRYPTO_PAILLIER_BITS * 2 / 8,
                                     t2);
 
-
-  get_fair_encryption_challenge (fe, e);
+  get_fair_encryption_challenge (fe,
+                                 &e /* This allocates "e" */);
 
   // compute z
   gcry_mpi_mul (z, e, v);
@@ -1883,7 +1896,7 @@ decrypt_new_element (void *cls,
     return;
   }
 
-  if (0 != memcmp (&d->ciphertext, &session->ciphertext, sizeof (struct GNUNET_SECRETSHARING_Ciphertext)))
+  if (0 != GNUNET_memcmp (&d->ciphertext, &session->ciphertext))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "P%u: got decrypt element with non-matching ciphertext from P%u\n",
                 (unsigned int) session->share->my_peer, (unsigned int) (info - session->info));