2 This file is part of GNUnet.
3 (C) 2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file secretsharing/gnunet-service-secretsharing.c
23 * @brief secret sharing service
24 * @author Florian Dold
27 #include "gnunet_util_lib.h"
28 #include "gnunet_time_lib.h"
29 #include "gnunet_signatures.h"
30 #include "gnunet_consensus_service.h"
31 #include "secretsharing.h"
32 #include "secretsharing_protocol.h"
36 #define EXTRA_CHECKS 1
40 * Info about a peer in a key generation session.
45 * Peer identity of the peer.
47 struct GNUNET_PeerIdentity peer;
50 * The peer's paillier public key.
51 * Freshly generated for each keygen session.
53 struct GNUNET_CRYPTO_PaillierPublicKey paillier_public_key;
56 * The peer's commitment to his presecret.
58 gcry_mpi_t presecret_commitment;
61 * Commitment to the preshare that is
62 * intended for our peer.
64 gcry_mpi_t preshare_commitment;
67 * Sigma (exponentiated share) for this peer.
72 * Did we successfully receive the round1 element
78 * Did we successfully receive the round2 element
86 * Information about a peer in a decrypt session.
88 struct DecryptPeerInfo
91 * Identity of the peer.
93 struct GNUNET_PeerIdentity peer;
96 * Original index in the key generation round.
97 * Necessary for computing the lagrange coefficients.
99 unsigned int original_index;
102 * Set to the partial decryption of
103 * this peer, or NULL if we did not
104 * receive a partial decryption from this
105 * peer or the zero knowledge proof failed.
107 gcry_mpi_t partial_decryption;
112 * Session to establish a threshold-shared secret.
117 * Keygen sessions are held in a linked list.
119 struct KeygenSession *next;
122 * Keygen sessions are held in a linked list.
124 struct KeygenSession *prev;
127 * Current consensus, used for both DKG rounds.
129 struct GNUNET_CONSENSUS_Handle *consensus;
132 * Client that is interested in the result
133 * of this key generation session.
135 struct GNUNET_SERVER_Client *client;
138 * Message queue for 'client'
140 struct GNUNET_MQ_Handle *client_mq;
143 * Randomly generated coefficients of the polynomial for sharing our
144 * pre-secret, where 'preshares[0]' is our pre-secret. Contains 'threshold'
145 * elements, thus represents a polynomial of degree 'threshold-1', which can
146 * be interpolated with 'threshold' data points.
148 * The pre-secret-shares 'i=1,...,num_peers' are given by evaluating this
149 * polyomial at 'i' for share i.
151 gcry_mpi_t *presecret_polynomial;
154 * Minimum number of shares required to restore the secret.
155 * Also the number of coefficients for the polynomial representing
156 * the sharing. Obviously, the polynomial then has degree threshold-1.
158 unsigned int threshold;
161 * Total number of peers.
163 unsigned int num_peers;
166 * Index of the local peer.
168 unsigned int local_peer;
171 * Information about all participating peers.
172 * Array of size 'num_peers'.
174 struct KeygenPeerInfo *info;
177 * List of all peers involved in the secret sharing session.
179 struct GNUNET_PeerIdentity *peers;
182 * Identifier for this session.
184 struct GNUNET_HashCode session_id;
187 * Paillier private key of our peer.
189 struct GNUNET_CRYPTO_PaillierPrivateKey paillier_private_key;
192 * When would we like the key to be established?
194 struct GNUNET_TIME_Absolute deadline;
197 * When does the DKG start? Necessary to compute fractions of the
198 * operation's desired time interval.
200 struct GNUNET_TIME_Absolute start_time;
203 * Index of the local peer in the ordered list
204 * of peers in the session.
206 unsigned int local_peer_idx;
209 * Share of our peer. Once preshares from other peers are received, they
210 * will be added to 'my'share.
215 * Public key, will be updated when a round2 element arrives.
217 gcry_mpi_t public_key;
222 * Session to cooperatively decrypt a value.
224 struct DecryptSession
227 * Decrypt sessions are stored in a linked list.
229 struct DecryptSession *next;
232 * Decrypt sessions are stored in a linked list.
234 struct DecryptSession *prev;
237 * Handle to the consensus over partial decryptions.
239 struct GNUNET_CONSENSUS_Handle *consensus;
242 * Client connected to us.
244 struct GNUNET_SERVER_Client *client;
247 * Message queue for 'client'.
249 struct GNUNET_MQ_Handle *client_mq;
252 * When should we start communicating for decryption?
254 struct GNUNET_TIME_Absolute start;
257 * When would we like the ciphertext to be
260 struct GNUNET_TIME_Absolute deadline;
263 * Ciphertext we want to decrypt.
265 struct GNUNET_SECRETSHARING_Ciphertext ciphertext;
268 * Share of the local peer.
269 * Containts other important information, such as
270 * the list of other peers.
272 struct GNUNET_SECRETSHARING_Share *share;
275 * State information about other peers.
277 struct DecryptPeerInfo *info;
282 * Decrypt sessions are held in a linked list.
284 static struct DecryptSession *decrypt_sessions_head;
287 * Decrypt sessions are held in a linked list.
289 static struct DecryptSession *decrypt_sessions_tail;
292 * Decrypt sessions are held in a linked list.
294 static struct KeygenSession *keygen_sessions_head;
297 * Decrypt sessions are held in a linked list.
299 static struct KeygenSession *keygen_sessions_tail;
302 * The ElGamal prime field order as libgcrypt mpi.
303 * Initialized in #init_crypto_constants.
305 static gcry_mpi_t elgamal_q;
308 * Modulus of the prime field used for ElGamal.
309 * Initialized in #init_crypto_constants.
311 static gcry_mpi_t elgamal_p;
314 * Generator for prime field of order 'elgamal_q'.
315 * Initialized in #init_crypto_constants.
317 static gcry_mpi_t elgamal_g;
320 * Peer that runs this service.
322 static struct GNUNET_PeerIdentity my_peer;
325 * Peer that runs this service.
327 static struct GNUNET_CRYPTO_EddsaPrivateKey *my_peer_private_key;
330 * Configuration of this service.
332 static const struct GNUNET_CONFIGURATION_Handle *cfg;
335 * Server for this service.
337 static struct GNUNET_SERVER_Handle *srv;
341 * Get the peer info belonging to a peer identity in a keygen session.
343 * @param ks The keygen session.
344 * @param peer The peer identity.
345 * @return The Keygen peer info, or NULL if the peer could not be found.
347 static struct KeygenPeerInfo *
348 get_keygen_peer_info (const struct KeygenSession *ks,
349 const struct GNUNET_PeerIdentity *peer)
352 for (i = 0; i < ks->num_peers; i++)
353 if (0 == memcmp (peer, &ks->info[i].peer, sizeof (struct GNUNET_PeerIdentity)))
360 * Get the peer info belonging to a peer identity in a decrypt session.
362 * @param ds The decrypt session.
363 * @param peer The peer identity.
364 * @return The decrypt peer info, or NULL if the peer could not be found.
366 static struct DecryptPeerInfo *
367 get_decrypt_peer_info (const struct DecryptSession *ds,
368 const struct GNUNET_PeerIdentity *peer)
371 for (i = 0; i < ds->share->num_peers; i++)
372 if (0 == memcmp (peer, &ds->info[i].peer, sizeof (struct GNUNET_PeerIdentity)))
379 * Interpolate between two points in time.
381 * @param start start time
382 * @param end end time
383 * @param num numerator of the scale factor
384 * @param denum denumerator of the scale factor
386 static struct GNUNET_TIME_Absolute
387 time_between (struct GNUNET_TIME_Absolute start,
388 struct GNUNET_TIME_Absolute end,
391 struct GNUNET_TIME_Absolute result;
394 GNUNET_assert (start.abs_value_us <= end.abs_value_us);
395 diff = end.abs_value_us - start.abs_value_us;
396 result.abs_value_us = start.abs_value_us + ((diff * num) / denum);
403 * Compare two peer identities. Indended to be used with qsort or bsearch.
405 * @param p1 Some peer identity.
406 * @param p2 Some peer identity.
407 * @return 1 if p1 > p2, -1 if p1 < p2 and 0 if p1 == p2.
410 peer_id_cmp (const void *p1, const void *p2)
412 return memcmp (p1, p2, sizeof (struct GNUNET_PeerIdentity));
417 * Get the index of a peer in an array of peers
419 * @param haystack Array of peers.
420 * @param n Size of @a haystack.
421 * @param needle Peer to find
422 * @return Index of @a needle in @a haystack, or -1 if peer
423 * is not in the list.
426 peer_find (const struct GNUNET_PeerIdentity *haystack, unsigned int n,
427 const struct GNUNET_PeerIdentity *needle)
430 for (i = 0; i < n; i++)
431 if (0 == memcmp (&haystack[i], needle, sizeof (struct GNUNET_PeerIdentity)))
438 * Normalize the given list of peers, by including the local peer
439 * (if it is missing) and sorting the peers by their identity.
441 * @param listed Peers in the unnormalized list.
442 * @param num_listed Peers in the un-normalized list.
443 * @param[out] num_normalized Number of peers in the normalized list.
444 * @param[out] my_peer_idx Index of the local peer in the normalized list.
445 * @return Normalized list, must be free'd by the caller.
447 static struct GNUNET_PeerIdentity *
448 normalize_peers (struct GNUNET_PeerIdentity *listed,
449 unsigned int num_listed,
450 unsigned int *num_normalized,
451 unsigned int *my_peer_idx)
453 unsigned int local_peer_in_list;
454 /* number of peers in the normalized list */
456 struct GNUNET_PeerIdentity *normalized;
458 local_peer_in_list = GNUNET_YES;
460 if (peer_find (listed, num_listed, &my_peer) < 0)
462 local_peer_in_list = GNUNET_NO;
466 normalized = GNUNET_new_array (n, struct GNUNET_PeerIdentity);
468 if (GNUNET_NO == local_peer_in_list)
469 normalized[n - 1] = my_peer;
471 memcpy (normalized, listed, num_listed * sizeof (struct GNUNET_PeerIdentity));
472 qsort (normalized, n, sizeof (struct GNUNET_PeerIdentity), &peer_id_cmp);
474 if (NULL != my_peer_idx)
475 *my_peer_idx = peer_find (normalized, n, &my_peer);
476 if (NULL != num_normalized)
484 * Get a the j-th lagrange coefficient for a set of indices.
486 * @param[out] coeff the lagrange coefficient
487 * @param j lagrange coefficient we want to compute
488 * @param indices indices
489 * @param num number of indices in @a indices
492 compute_lagrange_coefficient (gcry_mpi_t coeff, unsigned int j,
493 unsigned int *indices,
501 /* temp value for l-j */
504 GNUNET_assert (0 != coeff);
506 GNUNET_assert (0 != (n = gcry_mpi_new (0)));
507 GNUNET_assert (0 != (d = gcry_mpi_new (0)));
508 GNUNET_assert (0 != (tmp = gcry_mpi_new (0)));
510 gcry_mpi_set_ui (n, 1);
511 gcry_mpi_set_ui (d, 1);
513 for (i = 0; i < num; i++)
515 unsigned int l = indices[i];
518 gcry_mpi_mul_ui (n, n, l + 1);
520 gcry_mpi_set_ui (tmp, l + 1);
521 gcry_mpi_sub_ui (tmp, tmp, j + 1);
522 gcry_mpi_mul (d, d, tmp);
525 // gcry_mpi_invm does not like negative numbers ...
526 gcry_mpi_mod (d, d, elgamal_q);
528 GNUNET_assert (gcry_mpi_cmp_ui (d, 0) > 0);
530 // now we do the actual division, with everything mod q, as we
531 // are not operating on elements from <g>, but on exponents
532 GNUNET_assert (0 != gcry_mpi_invm (d, d, elgamal_q));
534 gcry_mpi_mulm (coeff, n, d, elgamal_q);
536 gcry_mpi_release (n);
537 gcry_mpi_release (d);
538 gcry_mpi_release (tmp);
543 decrypt_session_destroy (struct DecryptSession *ds)
545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying decrypt session\n");
547 GNUNET_CONTAINER_DLL_remove (decrypt_sessions_head, decrypt_sessions_tail, ds);
549 if (NULL != ds->client_mq)
551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying decrypt MQ\n");
552 GNUNET_MQ_destroy (ds->client_mq);
553 ds->client_mq = NULL;
556 if (NULL != ds->client)
558 GNUNET_SERVER_client_disconnect (ds->client);
567 keygen_session_destroy (struct KeygenSession *ks)
569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying keygen session\n");
571 GNUNET_CONTAINER_DLL_remove (keygen_sessions_head, keygen_sessions_tail, ks);
573 if (NULL != ks->client_mq)
575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying keygen MQ\n");
576 GNUNET_MQ_destroy (ks->client_mq);
577 ks->client_mq = NULL;
580 if (NULL != ks->client)
582 GNUNET_SERVER_client_disconnect (ks->client);
591 * Task run during shutdown.
597 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
599 while (NULL != decrypt_sessions_head)
600 decrypt_session_destroy (decrypt_sessions_head);
602 while (NULL != keygen_sessions_head)
603 keygen_session_destroy (keygen_sessions_head);
609 * Generate the random coefficients of our pre-secret polynomial
611 * @param ks the session
614 generate_presecret_polynomial (struct KeygenSession *ks)
619 GNUNET_assert (NULL == ks->presecret_polynomial);
620 ks->presecret_polynomial = GNUNET_new_array (ks->threshold, gcry_mpi_t);
621 for (i = 0; i < ks->threshold; i++)
623 v = ks->presecret_polynomial[i] = gcry_mpi_new (GNUNET_SECRETSHARING_ELGAMAL_BITS);
624 GNUNET_assert (NULL != v);
625 // Randomize v such that 0 < v < elgamal_q.
626 // The '- 1' is necessary as bitlength(q) = bitlength(p) - 1.
629 gcry_mpi_randomize (v, GNUNET_SECRETSHARING_ELGAMAL_BITS - 1, GCRY_WEAK_RANDOM);
630 } while ((gcry_mpi_cmp_ui (v, 0) == 0) || (gcry_mpi_cmp (v, elgamal_q) >= 0));
636 * Consensus element handler for round one.
637 * We should get one ephemeral key for each peer.
639 * @param cls Closure (keygen session).
640 * @param element The element from consensus, or
641 * NULL if consensus failed.
644 keygen_round1_new_element (void *cls,
645 const struct GNUNET_SET_Element *element)
647 const struct GNUNET_SECRETSHARING_KeygenCommitData *d;
648 struct KeygenSession *ks = cls;
649 struct KeygenPeerInfo *info;
653 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "round1 consensus failed\n");
657 /* elements have fixed size */
658 if (element->size != sizeof (struct GNUNET_SECRETSHARING_KeygenCommitData))
660 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
661 "keygen commit data with wrong size (%u) in consensus, "
663 element->size, sizeof (struct GNUNET_SECRETSHARING_KeygenCommitData));
667 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "got round1 element\n");
670 info = get_keygen_peer_info (ks, &d->peer);
674 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "keygen commit data with wrong peer identity (%s) in consensus\n",
675 GNUNET_i2s (&d->peer));
679 /* Check that the right amount of data has been signed. */
680 if (d->purpose.size !=
681 htonl (element->size - offsetof (struct GNUNET_SECRETSHARING_KeygenCommitData, purpose)))
683 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "keygen commit data with wrong signature purpose size in consensus\n");
687 if (GNUNET_OK != GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_SECRETSHARING_DKG1,
688 &d->purpose, &d->signature, &d->peer.public_key))
690 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "keygen commit data with invalid signature in consensus\n");
693 info->paillier_public_key = d->pubkey;
694 GNUNET_CRYPTO_mpi_scan_unsigned (&info->presecret_commitment, &d->commitment, 512 / 8);
695 info->round1_valid = GNUNET_YES;
700 * Evaluate the polynomial with coefficients @a coeff at @a x.
701 * The i-th element in @a coeff corresponds to the coefficient of x^i.
703 * @param[out] z result of the evaluation
704 * @param coeff array of coefficients
705 * @param num_coeff number of coefficients
706 * @param x where to evaluate the polynomial
707 * @param m what group are we operating in?
710 horner_eval (gcry_mpi_t z, gcry_mpi_t *coeff, unsigned int num_coeff, gcry_mpi_t x, gcry_mpi_t m)
714 gcry_mpi_set_ui (z, 0);
715 for (i = 0; i < num_coeff; i++)
718 gcry_mpi_mul (z, z, x);
719 gcry_mpi_addm (z, z, coeff[num_coeff - i - 1], m);
725 keygen_round2_conclude (void *cls)
727 struct KeygenSession *ks = cls;
728 struct GNUNET_SECRETSHARING_SecretReadyMessage *m;
729 struct GNUNET_MQ_Envelope *ev;
733 struct GNUNET_SECRETSHARING_Share *share;
735 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "round2 conclude\n");
737 share = GNUNET_new (struct GNUNET_SECRETSHARING_Share);
739 share->num_peers = 0;
741 for (i = 0; i < ks->num_peers; i++)
742 if (GNUNET_YES == ks->info[i].round2_valid)
745 share->peers = GNUNET_new_array (share->num_peers, struct GNUNET_PeerIdentity);
747 GNUNET_new_array (share->num_peers, struct GNUNET_SECRETSHARING_FieldElement);
748 share->original_indices = GNUNET_new_array (share->num_peers, uint16_t);
750 /* maybe we're not even in the list of peers? */
751 share->my_peer = share->num_peers;
753 j = 0; /* running index of valid peers */
754 for (i = 0; i < ks->num_peers; i++)
756 if (GNUNET_YES == ks->info[i].round2_valid)
758 share->peers[j] = ks->info[i].peer;
759 GNUNET_CRYPTO_mpi_print_unsigned (&share->sigmas[j],
760 GNUNET_SECRETSHARING_ELGAMAL_BITS / 8,
762 share->original_indices[i] = j;
763 if (0 == memcmp (&share->peers[i], &my_peer, sizeof (struct GNUNET_PeerIdentity)))
769 if (share->my_peer == share->num_peers)
771 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "P%u: peer identity not in share\n", ks->local_peer_idx);
774 GNUNET_CRYPTO_mpi_print_unsigned (&share->my_share, GNUNET_SECRETSHARING_ELGAMAL_BITS / 8,
776 GNUNET_CRYPTO_mpi_print_unsigned (&share->public_key, GNUNET_SECRETSHARING_ELGAMAL_BITS / 8,
779 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "keygen completed with %u peers\n", share->num_peers);
781 /* Write the share. If 0 peers completed the dkg, an empty
782 * share will be sent. */
784 m = GNUNET_malloc (sizeof (struct GNUNET_SECRETSHARING_SecretReadyMessage) +
785 ks->num_peers * sizeof (struct GNUNET_PeerIdentity));
787 GNUNET_assert (GNUNET_OK == GNUNET_SECRETSHARING_share_write (share, NULL, 0, &share_size));
789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "writing share of size %u\n",
790 (unsigned int) share_size);
792 ev = GNUNET_MQ_msg_extra (m, share_size,
793 GNUNET_MESSAGE_TYPE_SECRETSHARING_CLIENT_SECRET_READY);
795 GNUNET_assert (GNUNET_OK == GNUNET_SECRETSHARING_share_write (share, &m[1], share_size, NULL));
797 GNUNET_MQ_send (ks->client_mq, ev);
802 * Insert round 2 element in the consensus, consisting of
803 * (1) The exponentiated pre-share polynomial coefficients A_{i,l}=g^{a_{i,l}}
804 * (2) The exponentiated pre-shares y_{i,j}=g^{s_{i,j}}
805 * (3) The encrypted pre-shares Y_{i,j}
806 * (4) The zero knowledge proof for fairness of
809 * @param ks session to use
812 insert_round2_element (struct KeygenSession *ks)
814 struct GNUNET_SET_Element *element;
815 struct GNUNET_SECRETSHARING_KeygenRevealData *d;
817 unsigned char *last_pos;
824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "P%u: Inserting round2 element\n",
827 GNUNET_assert (NULL != (v = gcry_mpi_new (GNUNET_SECRETSHARING_ELGAMAL_BITS)));
828 GNUNET_assert (NULL != (idx = gcry_mpi_new (GNUNET_SECRETSHARING_ELGAMAL_BITS)));
829 GNUNET_assert (NULL != (c = gcry_mpi_new (GNUNET_SECRETSHARING_ELGAMAL_BITS)));
831 element_size = (sizeof (struct GNUNET_SECRETSHARING_KeygenRevealData) +
832 GNUNET_SECRETSHARING_ELGAMAL_BITS / 8 * ks->num_peers +
833 sizeof (struct GNUNET_CRYPTO_PaillierCiphertext) * ks->num_peers +
834 GNUNET_SECRETSHARING_ELGAMAL_BITS / 8 * ks->threshold);
836 element = GNUNET_malloc (sizeof (struct GNUNET_SET_Element) + element_size);
837 element->size = element_size;
838 element->data = (void *) &element[1];
840 d = (void *) element->data;
843 // start inserting vector elements
844 // after the fixed part of the element's data
845 pos = (void *) &d[1];
846 last_pos = pos + element_size;
848 // exponentiated pre-shares
849 for (i = 0; i < ks->num_peers; i++)
851 ptrdiff_t remaining = last_pos - pos;
852 GNUNET_assert (remaining > 0);
853 gcry_mpi_set_ui (idx, i + 1);
854 // evaluate the polynomial
855 horner_eval (v, ks->presecret_polynomial, ks->threshold, idx, elgamal_q);
856 // take g to the result
857 gcry_mpi_powm (v, elgamal_g, v, elgamal_p);
858 GNUNET_CRYPTO_mpi_print_unsigned (pos, GNUNET_SECRETSHARING_ELGAMAL_BITS / 8, v);
859 pos += GNUNET_SECRETSHARING_ELGAMAL_BITS / 8;
862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "P%u: computed exp preshares\n",
865 // encrypted pre-shares
866 for (i = 0; i < ks->num_peers; i++)
868 ptrdiff_t remaining = last_pos - pos;
869 struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext;
871 GNUNET_assert (remaining > 0);
872 ciphertext = (void *) pos;
873 memset (ciphertext, 0, sizeof *ciphertext);
874 if (GNUNET_YES == ks->info[i].round1_valid)
876 gcry_mpi_set_ui (idx, i + 1);
877 // evaluate the polynomial
878 horner_eval (v, ks->presecret_polynomial, ks->threshold, idx, elgamal_q);
879 // encrypt the result
880 GNUNET_CRYPTO_paillier_encrypt (&ks->info[i].paillier_public_key, v, 0, ciphertext);
882 pos += sizeof *ciphertext;
885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "P%u: computed enc preshares\n",
888 // exponentiated coefficients
889 for (i = 0; i < ks->threshold; i++)
891 ptrdiff_t remaining = last_pos - pos;
892 GNUNET_assert (remaining > 0);
893 gcry_mpi_powm (v, elgamal_g, ks->presecret_polynomial[i], elgamal_p);
894 GNUNET_CRYPTO_mpi_print_unsigned (pos, GNUNET_SECRETSHARING_ELGAMAL_BITS / 8, v);
895 pos += GNUNET_SECRETSHARING_ELGAMAL_BITS / 8;
898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "P%u: computed exp coefficients\n",
901 d->purpose.size = htonl (element_size - offsetof (struct GNUNET_SECRETSHARING_KeygenRevealData, purpose));
902 d->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_SECRETSHARING_DKG2);
903 GNUNET_CRYPTO_eddsa_sign (my_peer_private_key, &d->purpose, &d->signature);
905 GNUNET_CONSENSUS_insert (ks->consensus, element, NULL, NULL);
906 GNUNET_free (element); /* FIXME: maybe stack-allocate instead? */
908 gcry_mpi_release (v);
909 gcry_mpi_release (idx);
914 keygen_reveal_get_exp_preshare (struct KeygenSession *ks,
915 const struct GNUNET_SECRETSHARING_KeygenRevealData *d,
919 gcry_mpi_t exp_preshare;
921 GNUNET_assert (idx < ks->num_peers);
923 GNUNET_assert (NULL != (exp_preshare = gcry_mpi_new (0)));
925 pos = (void *) &d[1];
926 // skip exponentiated pre-shares we don't want
927 pos += GNUNET_SECRETSHARING_ELGAMAL_BITS / 8 * idx;
928 GNUNET_CRYPTO_mpi_scan_unsigned (&exp_preshare, pos, GNUNET_SECRETSHARING_ELGAMAL_BITS / 8);
933 keygen_reveal_get_exp_coeff (struct KeygenSession *ks,
934 const struct GNUNET_SECRETSHARING_KeygenRevealData *d,
938 gcry_mpi_t exp_coeff;
940 GNUNET_assert (idx < ks->threshold);
941 GNUNET_assert (NULL != (exp_coeff = gcry_mpi_new (0)));
943 pos = (void *) &d[1];
944 // skip exponentiated pre-shares
945 pos += GNUNET_SECRETSHARING_ELGAMAL_BITS / 8 * ks->num_peers;
946 // skip encrypted pre-shares
947 pos += sizeof (struct GNUNET_CRYPTO_PaillierCiphertext) * ks->num_peers;
948 // skip exp. coeffs we are not interested in
949 pos += GNUNET_SECRETSHARING_ELGAMAL_BITS / 8 * idx;
950 // the first exponentiated coefficient is the public key share
951 GNUNET_CRYPTO_mpi_scan_unsigned (&exp_coeff, pos, GNUNET_SECRETSHARING_ELGAMAL_BITS / 8);
956 static struct GNUNET_CRYPTO_PaillierCiphertext *
957 keygen_reveal_get_enc_preshare (struct KeygenSession *ks,
958 const struct GNUNET_SECRETSHARING_KeygenRevealData *d,
963 GNUNET_assert (idx < ks->num_peers);
965 pos = (void *) &d[1];
966 // skip exponentiated pre-shares
967 pos += GNUNET_SECRETSHARING_ELGAMAL_BITS / 8 * ks->num_peers;
968 // skip encrypted pre-shares we're not interested in
969 pos += sizeof (struct GNUNET_CRYPTO_PaillierCiphertext) * idx;
970 return (struct GNUNET_CRYPTO_PaillierCiphertext *) pos;
975 keygen_round2_new_element (void *cls,
976 const struct GNUNET_SET_Element *element)
978 struct KeygenSession *ks = cls;
979 const struct GNUNET_SECRETSHARING_KeygenRevealData *d;
980 struct KeygenPeerInfo *info;
981 size_t expected_element_size;
984 gcry_mpi_t public_key_share;
989 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "round2 consensus failed\n");
993 expected_element_size = (sizeof (struct GNUNET_SECRETSHARING_KeygenRevealData) +
994 GNUNET_SECRETSHARING_ELGAMAL_BITS / 8 * ks->num_peers +
995 sizeof (struct GNUNET_CRYPTO_PaillierCiphertext) * ks->num_peers +
996 GNUNET_SECRETSHARING_ELGAMAL_BITS / 8 * ks->threshold);
998 if (element->size != expected_element_size)
1000 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1001 "keygen round2 data with wrong size (%u) in consensus, "
1003 element->size, expected_element_size);
1007 d = (const void *) element->data;
1009 info = get_keygen_peer_info (ks, &d->peer);
1013 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "keygen commit data with wrong peer identity (%s) in consensus\n",
1014 GNUNET_i2s (&d->peer));
1018 if (GNUNET_NO == info->round1_valid)
1020 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1021 "ignoring round2 element from peer with invalid round1 element (%s)\n",
1022 GNUNET_i2s (&d->peer));
1026 if (GNUNET_YES == info->round2_valid)
1028 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1029 "ignoring duplicate round2 element (%s)\n",
1030 GNUNET_i2s (&d->peer));
1034 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "got round2 element\n");
1036 if (ntohl (d->purpose.size) !=
1037 element->size - offsetof (struct GNUNET_SECRETSHARING_KeygenRevealData, purpose))
1039 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "keygen reveal data with wrong signature purpose size in consensus\n");
1043 if (GNUNET_OK != GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_SECRETSHARING_DKG2,
1044 &d->purpose, &d->signature, &d->peer.public_key))
1046 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "keygen reveal data with invalid signature in consensus\n");
1050 public_key_share = keygen_reveal_get_exp_coeff (ks, d, 0);
1051 info->preshare_commitment = keygen_reveal_get_exp_preshare (ks, d, ks->local_peer_idx);
1053 if (NULL == ks->public_key)
1055 GNUNET_assert (NULL != (ks->public_key = gcry_mpi_new (0)));
1056 gcry_mpi_set_ui (ks->public_key, 1);
1058 gcry_mpi_mulm (ks->public_key, ks->public_key, public_key_share, elgamal_p);
1060 GNUNET_assert (NULL != (preshare = gcry_mpi_new (0)));
1061 GNUNET_CRYPTO_paillier_decrypt (&ks->paillier_private_key,
1062 &ks->info[ks->local_peer_idx].paillier_public_key,
1063 keygen_reveal_get_enc_preshare (ks, d, ks->local_peer_idx),
1066 GNUNET_assert (NULL != (tmp = gcry_mpi_new (0)));
1067 gcry_mpi_powm (tmp, elgamal_g, preshare, elgamal_p);
1069 // TODO: restore a valid secret from the decryption (the hard part, solving SVP with gauss)
1070 if (0 != gcry_mpi_cmp (tmp, info->preshare_commitment))
1072 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "P%u: Got invalid presecret from P%u\n",
1073 (unsigned int) ks->local_peer_idx, (unsigned int) (info - ks->info));
1077 if (NULL == ks->my_share)
1079 GNUNET_assert (NULL != (ks->my_share = gcry_mpi_new (0)));
1081 gcry_mpi_addm (ks->my_share, ks->my_share, preshare, elgamal_q);
1083 for (j = 0; j < ks->num_peers; j++)
1085 gcry_mpi_t presigma;
1086 if (NULL == ks->info[j].sigma)
1088 GNUNET_assert (NULL != (ks->info[j].sigma = gcry_mpi_new (0)));
1089 gcry_mpi_set_ui (ks->info[j].sigma, 1);
1091 presigma = keygen_reveal_get_exp_preshare (ks, d, j);
1092 gcry_mpi_mulm (ks->info[j].sigma, ks->info[j].sigma, presigma, elgamal_p);
1096 GNUNET_assert (NULL != (prod = gcry_mpi_new (0)));
1098 GNUNET_assert (NULL != (j_to_k = gcry_mpi_new (0)));
1099 // validate that the polynomial sharing matches the additive sharing
1100 for (j = 0; j < ks->num_peers; j++)
1104 gcry_mpi_t exp_preshare;
1105 gcry_mpi_set_ui (prod, 1);
1106 for (k = 0; k < ks->threshold; k++)
1108 // Using pow(double,double) is a bit sketchy.
1109 // We count players from 1, but shares from 0.
1110 gcry_mpi_set_ui (j_to_k, (unsigned int) pow(j+1, k));
1111 tmp = keygen_reveal_get_exp_coeff (ks, d, k);
1112 gcry_mpi_powm (tmp, tmp, j_to_k, elgamal_p);
1113 gcry_mpi_mulm (prod, prod, tmp, elgamal_p);
1115 exp_preshare = keygen_reveal_get_exp_preshare (ks, d, j);
1116 gcry_mpi_mod (exp_preshare, exp_preshare, elgamal_p);
1117 if (0 != gcry_mpi_cmp (prod, exp_preshare))
1119 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "P%u: reveal data from P%u incorrect\n",
1120 ks->local_peer_idx, j);
1121 /* no need for further verification, round2 stays invalid ... */
1126 // TODO: verify proof of fair encryption (once implemented)
1128 info->round2_valid = GNUNET_YES;
1133 * Called when the first consensus round has concluded.
1134 * Will initiate the second round.
1136 * @param cls closure
1139 keygen_round1_conclude (void *cls)
1141 struct KeygenSession *ks = cls;
1143 GNUNET_CONSENSUS_destroy (ks->consensus);
1145 ks->consensus = GNUNET_CONSENSUS_create (cfg, ks->num_peers, ks->peers, &ks->session_id,
1146 time_between (ks->start_time, ks->deadline, 1, 2),
1148 keygen_round2_new_element, ks);
1150 insert_round2_element (ks);
1152 GNUNET_CONSENSUS_conclude (ks->consensus,
1153 keygen_round2_conclude,
1159 * Insert the ephemeral key and the presecret commitment
1160 * of this peer in the consensus of the given session.
1162 * @param ks session to use
1165 insert_round1_element (struct KeygenSession *ks)
1167 struct GNUNET_SET_Element *element;
1168 struct GNUNET_SECRETSHARING_KeygenCommitData *d;
1171 // big-endian representation of 'v'
1172 unsigned char v_data[GNUNET_SECRETSHARING_ELGAMAL_BITS / 8];
1174 element = GNUNET_malloc (sizeof *element + sizeof *d);
1175 d = (void *) &element[1];
1177 element->size = sizeof *d;
1181 GNUNET_assert (0 != (v = gcry_mpi_new (GNUNET_SECRETSHARING_ELGAMAL_BITS)));
1183 gcry_mpi_powm (v, elgamal_g, ks->presecret_polynomial[0], elgamal_p);
1185 GNUNET_CRYPTO_mpi_print_unsigned (v_data, GNUNET_SECRETSHARING_ELGAMAL_BITS / 8, v);
1187 GNUNET_CRYPTO_hash (v_data, GNUNET_SECRETSHARING_ELGAMAL_BITS / 8, &d->commitment);
1189 d->pubkey = ks->info[ks->local_peer_idx].paillier_public_key;
1191 d->purpose.size = htonl ((sizeof *d) - offsetof (struct GNUNET_SECRETSHARING_KeygenCommitData, purpose));
1192 d->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_SECRETSHARING_DKG1);
1193 GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_eddsa_sign (my_peer_private_key, &d->purpose, &d->signature));
1195 GNUNET_CONSENSUS_insert (ks->consensus, element, NULL, NULL);
1197 gcry_mpi_release (v);
1198 GNUNET_free (element);
1203 * Functions with this signature are called whenever a message is
1206 * @param cls closure
1207 * @param client identification of the client
1208 * @param message the actual message
1210 static void handle_client_keygen (void *cls,
1211 struct GNUNET_SERVER_Client *client,
1212 const struct GNUNET_MessageHeader
1215 const struct GNUNET_SECRETSHARING_CreateMessage *msg =
1216 (const struct GNUNET_SECRETSHARING_CreateMessage *) message;
1217 struct KeygenSession *ks;
1220 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "client requested key generation\n");
1222 ks = GNUNET_new (struct KeygenSession);
1224 /* FIXME: check if client already has some session */
1226 GNUNET_CONTAINER_DLL_insert (keygen_sessions_head, keygen_sessions_tail, ks);
1228 ks->client = client;
1229 ks->client_mq = GNUNET_MQ_queue_for_server_client (client);
1231 ks->deadline = GNUNET_TIME_absolute_ntoh (msg->deadline);
1232 ks->threshold = ntohs (msg->threshold);
1233 ks->num_peers = ntohs (msg->num_peers);
1235 ks->peers = normalize_peers ((struct GNUNET_PeerIdentity *) &msg[1], ks->num_peers,
1236 &ks->num_peers, &ks->local_peer_idx);
1239 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "first round of consensus with %u peers\n", ks->num_peers);
1240 ks->consensus = GNUNET_CONSENSUS_create (cfg, ks->num_peers, ks->peers, &msg->session_id,
1241 GNUNET_TIME_absolute_ntoh (msg->start),
1242 GNUNET_TIME_absolute_ntoh (msg->deadline),
1243 keygen_round1_new_element, ks);
1245 ks->info = GNUNET_new_array (ks->num_peers, struct KeygenPeerInfo);
1247 for (i = 0; i < ks->num_peers; i++)
1248 ks->info[i].peer = ks->peers[i];
1250 GNUNET_CRYPTO_paillier_create (&ks->info[ks->local_peer_idx].paillier_public_key,
1251 &ks->paillier_private_key);
1253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "P%u: Generated paillier key pair\n", ks->local_peer_idx);
1255 generate_presecret_polynomial (ks);
1257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "P%u: Generated presecret polynomial\n", ks->local_peer_idx);
1259 insert_round1_element (ks);
1261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "P%u: Concluding for round 1\n", ks->local_peer_idx);
1263 GNUNET_CONSENSUS_conclude (ks->consensus,
1264 keygen_round1_conclude,
1267 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "P%u: Waiting for round 1 elements ...\n", ks->local_peer_idx);
1274 * Called when the partial decryption consensus concludes.
1277 decrypt_conclude (void *cls)
1279 struct DecryptSession *ds = cls;
1280 struct GNUNET_SECRETSHARING_DecryptResponseMessage *msg;
1281 struct GNUNET_MQ_Envelope *ev;
1282 gcry_mpi_t lagrange;
1287 unsigned int *indices;
1292 GNUNET_assert (0 != (lagrange = gcry_mpi_new (0)));
1293 GNUNET_assert (0 != (m = gcry_mpi_new (0)));
1294 GNUNET_assert (0 != (tmp = gcry_mpi_new (0)));
1295 GNUNET_assert (0 != (prod = gcry_mpi_new (0)));
1298 for (i = 0; i < ds->share->num_peers; i++)
1299 if (NULL != ds->info[i].partial_decryption)
1302 indices = GNUNET_malloc (num * sizeof (unsigned int));
1304 for (i = 0; i < ds->share->num_peers; i++)
1305 if (NULL != ds->info[i].partial_decryption)
1306 indices[j++] = ds->info[i].original_index;
1308 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "P%u: decrypt conclude, with %u peers\n",
1309 ds->share->my_peer, num);
1311 gcry_mpi_set_ui (prod, 1);
1312 for (i = 0; i < num; i++)
1315 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "P%u: index of %u: %u\n",
1316 ds->share->my_peer, i, indices[i]);
1317 compute_lagrange_coefficient (lagrange, indices[i], indices, num);
1319 gcry_mpi_powm (tmp, ds->info[indices[i]].partial_decryption, lagrange, elgamal_p);
1321 // product of all exponentiated partiel decryptions ...
1322 gcry_mpi_mulm (prod, prod, tmp, elgamal_p);
1325 GNUNET_CRYPTO_mpi_scan_unsigned (&c_2, ds->ciphertext.c2_bits, GNUNET_SECRETSHARING_ELGAMAL_BITS / 8);
1327 GNUNET_assert (0 != gcry_mpi_invm (prod, prod, elgamal_p));
1328 gcry_mpi_mulm (m, c_2, prod, elgamal_p);
1329 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SECRETSHARING_CLIENT_DECRYPT_DONE);
1330 GNUNET_CRYPTO_mpi_print_unsigned (&msg->plaintext, GNUNET_SECRETSHARING_ELGAMAL_BITS / 8, m);
1331 msg->success = htonl (1);
1332 GNUNET_MQ_send (ds->client_mq, ev);
1334 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "sent decrypt done to client\n");
1336 // FIXME: what if not enough peers participated?
1341 * Get a string representation of an MPI.
1342 * The caller must free the returned string.
1344 * @param mpi mpi to convert to a string
1345 * @return string representation of @a mpi, must be free'd by the caller
1348 mpi_to_str (gcry_mpi_t mpi)
1352 GNUNET_assert (0 == gcry_mpi_aprint (GCRYMPI_FMT_HEX, &buf, NULL, mpi));
1353 return (char *) buf;
1358 * Called when a new partial decryption arrives.
1361 decrypt_new_element (void *cls,
1362 const struct GNUNET_SET_Element *element)
1364 struct DecryptSession *session = cls;
1365 const struct GNUNET_SECRETSHARING_DecryptData *d;
1366 struct DecryptPeerInfo *info;
1367 struct GNUNET_HashCode challenge_hash;
1371 /* nizk challenge */
1372 gcry_mpi_t challenge;
1373 /* nizk commit1, g^\beta */
1375 /* nizk commit2, c_1^\beta */
1377 /* homomorphic commitment to the peer's share,
1378 * public key share */
1380 /* partial decryption we received */
1382 /* ciphertext component #1 */
1384 /* temporary variable (for comparision) #1 */
1386 /* temporary variable (for comparision) #2 */
1389 if (NULL == element)
1391 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "decryption failed\n");
1392 /* FIXME: destroy */
1396 if (element->size != sizeof *d)
1398 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "element of wrong size in decrypt consensus\n");
1404 info = get_decrypt_peer_info (session, &d->peer);
1408 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "decrypt element from invalid peer (%s)\n",
1409 GNUNET_i2s (&d->peer));
1413 if (NULL != info->partial_decryption)
1415 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "decrypt element duplicate\n",
1416 GNUNET_i2s (&d->peer));
1420 if (0 != memcmp (&d->ciphertext, &session->ciphertext, sizeof (struct GNUNET_SECRETSHARING_Ciphertext)))
1422 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "P%u: got decrypt element with non-matching ciphertext from P%u\n",
1423 (unsigned int) session->share->my_peer, (unsigned int) (info - session->info));
1429 GNUNET_CRYPTO_hash (offsetof (struct GNUNET_SECRETSHARING_DecryptData, ciphertext) + (char *) d,
1430 offsetof (struct GNUNET_SECRETSHARING_DecryptData, nizk_response) -
1431 offsetof (struct GNUNET_SECRETSHARING_DecryptData, ciphertext),
1434 GNUNET_CRYPTO_mpi_scan_unsigned (&challenge, &challenge_hash,
1435 sizeof (struct GNUNET_HashCode));
1437 GNUNET_CRYPTO_mpi_scan_unsigned (&sigma, &session->share->sigmas[info - session->info],
1438 sizeof (struct GNUNET_SECRETSHARING_FieldElement));
1440 GNUNET_CRYPTO_mpi_scan_unsigned (&c1, session->ciphertext.c1_bits,
1441 sizeof (struct GNUNET_SECRETSHARING_FieldElement));
1443 GNUNET_CRYPTO_mpi_scan_unsigned (&commit1, &d->nizk_commit1,
1444 sizeof (struct GNUNET_SECRETSHARING_FieldElement));
1446 GNUNET_CRYPTO_mpi_scan_unsigned (&commit2, &d->nizk_commit2,
1447 sizeof (struct GNUNET_SECRETSHARING_FieldElement));
1449 GNUNET_CRYPTO_mpi_scan_unsigned (&r, &d->nizk_response,
1450 sizeof (struct GNUNET_SECRETSHARING_FieldElement));
1452 GNUNET_CRYPTO_mpi_scan_unsigned (&w, &d->partial_decryption,
1453 sizeof (struct GNUNET_SECRETSHARING_FieldElement));
1455 GNUNET_assert (NULL != (tmp1 = gcry_mpi_new (0)));
1456 GNUNET_assert (NULL != (tmp2 = gcry_mpi_new (0)));
1459 gcry_mpi_powm (tmp1, elgamal_g, r, elgamal_p);
1461 // tmp2 = g^\beta * \sigma^challenge
1462 gcry_mpi_powm (tmp2, sigma, challenge, elgamal_p);
1463 gcry_mpi_mulm (tmp2, tmp2, commit1, elgamal_p);
1465 if (0 != gcry_mpi_cmp (tmp1, tmp2))
1469 tmp1_str = mpi_to_str (tmp1);
1470 tmp2_str = mpi_to_str (tmp2);
1471 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "P%u: Received invalid partial decryption from P%u (eqn 1), expected %s got %s\n",
1472 session->share->my_peer, info - session->info, tmp1_str, tmp2_str);
1473 GNUNET_free (tmp1_str);
1474 GNUNET_free (tmp2_str);
1479 gcry_mpi_powm (tmp1, c1, r, elgamal_p);
1481 gcry_mpi_powm (tmp2, w, challenge, elgamal_p);
1482 gcry_mpi_mulm (tmp2, tmp2, commit2, elgamal_p);
1485 if (0 != gcry_mpi_cmp (tmp1, tmp2))
1487 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "P%u: Received invalid partial decryption from P%u (eqn 2)\n",
1488 session->share->my_peer, info - session->info);
1493 GNUNET_CRYPTO_mpi_scan_unsigned (&info->partial_decryption, &d->partial_decryption,
1494 GNUNET_SECRETSHARING_ELGAMAL_BITS / 8);
1499 insert_decrypt_element (struct DecryptSession *ds)
1501 struct GNUNET_SECRETSHARING_DecryptData d;
1502 struct GNUNET_SET_Element element;
1505 /* partial decryption with our share */
1507 /* first component of the elgamal ciphertext */
1509 /* nonce for dlog zkp */
1512 gcry_mpi_t challenge;
1514 struct GNUNET_HashCode challenge_hash;
1516 /* make vagrind happy until we implement the real deal ... */
1517 memset (&d, 0, sizeof d);
1519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "P%u: Inserting decrypt element\n",
1520 ds->share->my_peer);
1522 GNUNET_assert (ds->share->my_peer < ds->share->num_peers);
1524 GNUNET_CRYPTO_mpi_scan_unsigned (&c1, &ds->ciphertext.c1_bits,
1525 GNUNET_SECRETSHARING_ELGAMAL_BITS / 8);
1526 GNUNET_CRYPTO_mpi_scan_unsigned (&s, &ds->share->my_share,
1527 GNUNET_SECRETSHARING_ELGAMAL_BITS / 8);
1528 GNUNET_CRYPTO_mpi_scan_unsigned (&sigma, &ds->share->sigmas[ds->share->my_peer],
1529 GNUNET_SECRETSHARING_ELGAMAL_BITS / 8);
1531 GNUNET_assert (NULL != (w = gcry_mpi_new (0)));
1532 GNUNET_assert (NULL != (beta = gcry_mpi_new (0)));
1533 GNUNET_assert (NULL != (tmp = gcry_mpi_new (0)));
1535 // FIXME: unnecessary, remove once crypto works
1536 gcry_mpi_powm (tmp, elgamal_g, s, elgamal_p);
1537 if (0 != gcry_mpi_cmp (tmp, sigma))
1539 char *sigma_str = mpi_to_str (sigma);
1540 char *tmp_str = mpi_to_str (tmp);
1541 char *s_str = mpi_to_str (s);
1542 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Share of P%u is invalid, ref sigma %s, "
1543 "computed sigma %s, s %s\n",
1545 sigma_str, tmp_str, s_str);
1546 GNUNET_free (sigma_str);
1547 GNUNET_free (tmp_str);
1548 GNUNET_free (s_str);
1551 gcry_mpi_powm (w, c1, s, elgamal_p);
1553 element.data = (void *) &d;
1554 element.size = sizeof (struct GNUNET_SECRETSHARING_DecryptData);
1557 d.ciphertext = ds->ciphertext;
1559 GNUNET_CRYPTO_mpi_print_unsigned (&d.partial_decryption, GNUNET_SECRETSHARING_ELGAMAL_BITS / 8, w);
1561 // create the zero knowledge proof
1562 // randomly choose beta such that 0 < beta < q
1565 gcry_mpi_randomize (beta, GNUNET_SECRETSHARING_ELGAMAL_BITS - 1, GCRY_WEAK_RANDOM);
1566 } while ((gcry_mpi_cmp_ui (beta, 0) == 0) || (gcry_mpi_cmp (beta, elgamal_q) >= 0));
1568 gcry_mpi_powm (tmp, elgamal_g, beta, elgamal_p);
1569 GNUNET_CRYPTO_mpi_print_unsigned (&d.nizk_commit1, GNUNET_SECRETSHARING_ELGAMAL_BITS / 8, tmp);
1571 gcry_mpi_powm (tmp, c1, beta, elgamal_p);
1572 GNUNET_CRYPTO_mpi_print_unsigned (&d.nizk_commit2, GNUNET_SECRETSHARING_ELGAMAL_BITS / 8, tmp);
1574 // the challenge is the hash of everything up to the response
1575 GNUNET_CRYPTO_hash (offsetof (struct GNUNET_SECRETSHARING_DecryptData, ciphertext) + (char *) &d,
1576 offsetof (struct GNUNET_SECRETSHARING_DecryptData, nizk_response) -
1577 offsetof (struct GNUNET_SECRETSHARING_DecryptData, ciphertext),
1580 GNUNET_CRYPTO_mpi_scan_unsigned (&challenge, &challenge_hash,
1581 sizeof (struct GNUNET_HashCode));
1583 // compute the response in tmp,
1584 // tmp = (c * s + beta) mod q
1585 gcry_mpi_mulm (tmp, challenge, s, elgamal_q);
1586 gcry_mpi_addm (tmp, tmp, beta, elgamal_q);
1588 GNUNET_CRYPTO_mpi_print_unsigned (&d.nizk_response, GNUNET_SECRETSHARING_ELGAMAL_BITS / 8, tmp);
1590 d.purpose.size = htonl (element.size - offsetof (struct GNUNET_SECRETSHARING_DecryptData, purpose));
1591 d.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_SECRETSHARING_DECRYPTION);
1593 GNUNET_CRYPTO_eddsa_sign (my_peer_private_key, &d.purpose, &d.signature);
1595 GNUNET_CONSENSUS_insert (ds->consensus, &element, NULL, NULL);
1596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "P%u: Inserting decrypt element done!\n",
1597 ds->share->my_peer);
1602 * Functions with this signature are called whenever a message is
1605 * @param cls closure
1606 * @param client identification of the client
1607 * @param message the actual message
1609 static void handle_client_decrypt (void *cls,
1610 struct GNUNET_SERVER_Client *client,
1611 const struct GNUNET_MessageHeader
1614 const struct GNUNET_SECRETSHARING_DecryptRequestMessage *msg =
1615 (const void *) message;
1616 struct DecryptSession *ds;
1617 struct GNUNET_HashCode session_id;
1620 ds = GNUNET_new (struct DecryptSession);
1621 // FIXME: check if session already exists
1622 GNUNET_CONTAINER_DLL_insert (decrypt_sessions_head, decrypt_sessions_tail, ds);
1623 ds->client = client;
1624 ds->client_mq = GNUNET_MQ_queue_for_server_client (client);
1625 ds->start = GNUNET_TIME_absolute_ntoh (msg->start);
1626 ds->deadline = GNUNET_TIME_absolute_ntoh (msg->deadline);
1627 ds->ciphertext = msg->ciphertext;
1629 ds->share = GNUNET_SECRETSHARING_share_read (&msg[1], ntohs (msg->header.size) - sizeof *msg, NULL);
1630 // FIXME: probably should be break rather than assert
1631 GNUNET_assert (NULL != ds->share);
1633 // FIXME: this is probably sufficient, but kdf/hash with all values would be nicer ...
1634 GNUNET_CRYPTO_hash (&msg->ciphertext, sizeof (struct GNUNET_SECRETSHARING_Ciphertext), &session_id);
1636 ds->consensus = GNUNET_CONSENSUS_create (cfg,
1637 ds->share->num_peers,
1642 &decrypt_new_element,
1646 ds->info = GNUNET_new_array (ds->share->num_peers, struct DecryptPeerInfo);
1647 for (i = 0; i < ds->share->num_peers; i++)
1649 ds->info[i].peer = ds->share->peers[i];
1650 ds->info[i].original_index = ds->share->original_indices[i];
1653 insert_decrypt_element (ds);
1655 GNUNET_CONSENSUS_conclude (ds->consensus, decrypt_conclude, ds);
1657 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1659 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "decrypting with %u peers\n",
1660 ds->share->num_peers);
1665 init_crypto_constants (void)
1667 GNUNET_assert (0 == gcry_mpi_scan (&elgamal_q, GCRYMPI_FMT_HEX,
1668 GNUNET_SECRETSHARING_ELGAMAL_Q_HEX, 0, NULL));
1669 GNUNET_assert (0 == gcry_mpi_scan (&elgamal_p, GCRYMPI_FMT_HEX,
1670 GNUNET_SECRETSHARING_ELGAMAL_P_HEX, 0, NULL));
1671 GNUNET_assert (0 == gcry_mpi_scan (&elgamal_g, GCRYMPI_FMT_HEX,
1672 GNUNET_SECRETSHARING_ELGAMAL_G_HEX, 0, NULL));
1676 static struct KeygenSession *
1677 keygen_session_get (struct GNUNET_SERVER_Client *client)
1679 struct KeygenSession *ks;
1680 for (ks = keygen_sessions_head; NULL != ks; ks = ks->next)
1681 if (ks->client == client)
1686 static struct DecryptSession *
1687 decrypt_session_get (struct GNUNET_SERVER_Client *client)
1689 struct DecryptSession *ds;
1690 for (ds = decrypt_sessions_head; NULL != ds; ds = ds->next)
1691 if (ds->client == client)
1698 * Clean up after a client has disconnected
1700 * @param cls closure, unused
1701 * @param client the client to clean up after
1704 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
1706 struct KeygenSession *ks;
1707 struct DecryptSession *ds;
1709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "handling client disconnect\n");
1711 ks = keygen_session_get (client);
1713 keygen_session_destroy (ks);
1715 ds = decrypt_session_get (client);
1717 decrypt_session_destroy (ds);
1722 * Process template requests.
1724 * @param cls closure
1725 * @param server the initialized server
1726 * @param c configuration to use
1729 run (void *cls, struct GNUNET_SERVER_Handle *server,
1730 const struct GNUNET_CONFIGURATION_Handle *c)
1732 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1733 {handle_client_keygen, NULL, GNUNET_MESSAGE_TYPE_SECRETSHARING_CLIENT_GENERATE, 0},
1734 {handle_client_decrypt, NULL, GNUNET_MESSAGE_TYPE_SECRETSHARING_CLIENT_DECRYPT, 0},
1739 my_peer_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (c);
1740 if (NULL == my_peer_private_key)
1742 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not access host private key\n");
1744 GNUNET_SCHEDULER_shutdown ();
1747 init_crypto_constants ();
1748 if (GNUNET_OK != GNUNET_CRYPTO_get_peer_identity (cfg, &my_peer))
1750 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not retrieve host identity\n");
1752 GNUNET_SCHEDULER_shutdown ();
1755 GNUNET_SERVER_add_handlers (server, handlers);
1756 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
1757 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
1763 * The main function for the template service.
1765 * @param argc number of arguments from the command line
1766 * @param argv command line arguments
1767 * @return 0 ok, 1 on error
1770 main (int argc, char *const *argv)
1772 return (GNUNET_OK ==
1773 GNUNET_SERVICE_run (argc, argv, "secretsharing",
1774 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;