2 This file is part of GNUnet.
3 (C) 2012 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/secretsharing_api.c
24 * @author Florian Dold
27 #include "gnunet_util_lib.h"
28 #include "gnunet_secretsharing_service.h"
29 #include "secretsharing.h"
33 #define LOG(kind,...) GNUNET_log_from (kind, "secretsharing-api",__VA_ARGS__)
36 * Session that will eventually establish a shared secred between
37 * the involved peers and allow encryption and cooperative decryption.
39 struct GNUNET_SECRETSHARING_Session
42 * Client connected to the secretsharing service.
44 struct GNUNET_CLIENT_Connection *client;
47 * Message queue for 'client'.
49 struct GNUNET_MQ_Handle *mq;
52 * Called when the secret sharing is done.
54 GNUNET_SECRETSHARING_SecretReadyCallback secret_ready_cb;
57 * Closure for 'secret_ready_cb'.
59 void *secret_ready_cls;
63 struct GNUNET_SECRETSHARING_DecryptionHandle
66 * Client connected to the secretsharing service.
68 struct GNUNET_CLIENT_Connection *client;
71 * Message queue for 'client'.
73 struct GNUNET_MQ_Handle *mq;
76 * Called when the secret sharing is done.
78 GNUNET_SECRETSHARING_DecryptCallback decrypt_cb;
81 * Closure for 'decrypt_cb'.
88 * The ElGamal prime field order as libgcrypt mpi.
89 * Initialized in #init_crypto_constants.
91 static gcry_mpi_t elgamal_q;
94 * Modulus of the prime field used for ElGamal.
95 * Initialized in #init_crypto_constants.
97 static gcry_mpi_t elgamal_p;
100 * Generator for prime field of order 'elgamal_q'.
101 * Initialized in #init_crypto_constants.
103 static gcry_mpi_t elgamal_g;
107 ensure_elgamal_initialized (void)
109 if (NULL != elgamal_q)
110 return; /* looks like crypto is already initialized */
112 GNUNET_assert (0 == gcry_mpi_scan (&elgamal_q, GCRYMPI_FMT_HEX,
113 GNUNET_SECRETSHARING_ELGAMAL_Q_HEX, 0, NULL));
114 GNUNET_assert (0 == gcry_mpi_scan (&elgamal_p, GCRYMPI_FMT_HEX,
115 GNUNET_SECRETSHARING_ELGAMAL_P_HEX, 0, NULL));
116 GNUNET_assert (0 == gcry_mpi_scan (&elgamal_g, GCRYMPI_FMT_HEX,
117 GNUNET_SECRETSHARING_ELGAMAL_G_HEX, 0, NULL));
122 handle_session_client_error (void *cls, enum GNUNET_MQ_Error error)
124 struct GNUNET_SECRETSHARING_Session *s = cls;
126 s->secret_ready_cb (s->secret_ready_cls, NULL, NULL, 0, NULL);
131 handle_decrypt_client_error (void *cls, enum GNUNET_MQ_Error error)
133 struct GNUNET_SECRETSHARING_DecryptionHandle *dh = cls;
135 dh->decrypt_cb (dh->decrypt_cls, NULL);
140 handle_secret_ready (void *cls, const struct GNUNET_MessageHeader *msg)
142 struct GNUNET_SECRETSHARING_Session *session = cls;
143 struct GNUNET_SECRETSHARING_Share *share;
144 const struct GNUNET_SECRETSHARING_SecretReadyMessage *m = (const void *) msg;
147 share_size = ntohs (m->header.size) - sizeof *m;
149 share = GNUNET_SECRETSHARING_share_read (&m[1], share_size, NULL);
151 session->secret_ready_cb (session->secret_ready_cls,
155 (struct GNUNET_PeerIdentity *) &m[1]);
157 GNUNET_SECRETSHARING_session_destroy (session);
162 GNUNET_SECRETSHARING_session_destroy (struct GNUNET_SECRETSHARING_Session *session)
164 GNUNET_MQ_destroy (session->mq);
166 GNUNET_CLIENT_disconnect (session->client);
167 session->client = NULL;
168 GNUNET_free (session);
172 struct GNUNET_SECRETSHARING_Session *
173 GNUNET_SECRETSHARING_create_session (const struct GNUNET_CONFIGURATION_Handle *cfg,
174 unsigned int num_peers,
175 const struct GNUNET_PeerIdentity *peers,
176 const struct GNUNET_HashCode *session_id,
177 struct GNUNET_TIME_Absolute deadline,
178 unsigned int threshold,
179 GNUNET_SECRETSHARING_SecretReadyCallback cb,
182 struct GNUNET_SECRETSHARING_Session *s;
183 struct GNUNET_MQ_Envelope *ev;
184 struct GNUNET_SECRETSHARING_CreateMessage *msg;
185 static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
186 {handle_secret_ready, GNUNET_MESSAGE_TYPE_SECRETSHARING_CLIENT_SECRET_READY, 0},
187 GNUNET_MQ_HANDLERS_END
191 s = GNUNET_new (struct GNUNET_SECRETSHARING_Session);
192 s->client = GNUNET_CLIENT_connect ("secretsharing", cfg);
193 s->secret_ready_cb = cb;
194 s->secret_ready_cls = cls;
195 GNUNET_assert (NULL != s->client);
197 s->mq = GNUNET_MQ_queue_for_connection_client (s->client, mq_handlers,
198 handle_session_client_error, s);
199 GNUNET_assert (NULL != s->mq);
201 ev = GNUNET_MQ_msg_extra (msg,
202 num_peers * sizeof (struct GNUNET_PeerIdentity),
203 GNUNET_MESSAGE_TYPE_SECRETSHARING_CLIENT_GENERATE);
205 msg->threshold = htons (threshold);
206 msg->num_peers = htons (num_peers);
207 msg->session_id = *session_id;
208 msg->deadline = GNUNET_TIME_absolute_hton (deadline);
209 memcpy (&msg[1], peers, num_peers * sizeof (struct GNUNET_PeerIdentity));
211 GNUNET_MQ_send (s->mq, ev);
213 LOG (GNUNET_ERROR_TYPE_DEBUG, "secretsharing session created with %u peers\n",
220 handle_decrypt_done (void *cls, const struct GNUNET_MessageHeader *msg)
222 struct GNUNET_SECRETSHARING_DecryptionHandle *dh = cls;
223 const struct GNUNET_SECRETSHARING_DecryptResponseMessage *m =
226 const struct GNUNET_SECRETSHARING_Plaintext *plaintext;
231 plaintext = (void *) &m->plaintext;
233 dh->decrypt_cb (dh->decrypt_cls, plaintext);
235 GNUNET_SECRETSHARING_decrypt_cancel (dh);
240 * Publish the given ciphertext for decryption. Once a sufficient (>=k) number of peers has
241 * published the same value, it will be decrypted.
243 * When the operation is canceled, the decrypt_cb is not called anymore, but the calling
244 * peer may already have irrevocably contributed his share for the decryption of the value.
246 * @param share our secret share to use for decryption
247 * @param ciphertext ciphertext to publish in order to decrypt it (if enough peers agree)
248 * @param decrypt_cb callback called once the decryption succeeded
249 * @param decrypt_cb_cls closure for @a decrypt_cb
250 * @return handle to cancel the operation
252 struct GNUNET_SECRETSHARING_DecryptionHandle *
253 GNUNET_SECRETSHARING_decrypt (const struct GNUNET_CONFIGURATION_Handle *cfg,
254 struct GNUNET_SECRETSHARING_Share *share,
255 const struct GNUNET_SECRETSHARING_Ciphertext *ciphertext,
256 struct GNUNET_TIME_Absolute deadline,
257 GNUNET_SECRETSHARING_DecryptCallback decrypt_cb,
258 void *decrypt_cb_cls)
260 struct GNUNET_SECRETSHARING_DecryptionHandle *s;
261 struct GNUNET_MQ_Envelope *ev;
262 struct GNUNET_SECRETSHARING_DecryptRequestMessage *msg;
263 static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
264 {handle_decrypt_done, GNUNET_MESSAGE_TYPE_SECRETSHARING_CLIENT_DECRYPT_DONE, 0},
265 GNUNET_MQ_HANDLERS_END
270 s = GNUNET_new (struct GNUNET_SECRETSHARING_DecryptionHandle);
271 s->client = GNUNET_CLIENT_connect ("secretsharing", cfg);
272 s->decrypt_cb = decrypt_cb;
273 s->decrypt_cls = decrypt_cb_cls;
274 GNUNET_assert (NULL != s->client);
276 s->mq = GNUNET_MQ_queue_for_connection_client (s->client, mq_handlers,
277 handle_decrypt_client_error, s);
278 GNUNET_assert (NULL != s->mq);
280 GNUNET_assert (GNUNET_OK == GNUNET_SECRETSHARING_share_write (share, NULL, 0, &share_size));
282 ev = GNUNET_MQ_msg_extra (msg,
284 GNUNET_MESSAGE_TYPE_SECRETSHARING_CLIENT_DECRYPT);
286 GNUNET_assert (GNUNET_OK == GNUNET_SECRETSHARING_share_write (share, &msg[1], share_size, NULL));
288 msg->deadline = GNUNET_TIME_absolute_hton (deadline);
289 msg->ciphertext = *ciphertext;
291 GNUNET_MQ_send (s->mq, ev);
293 LOG (GNUNET_ERROR_TYPE_DEBUG, "decrypt session created\n");
299 GNUNET_SECRETSHARING_plaintext_generate_i (struct GNUNET_SECRETSHARING_Plaintext *plaintext,
305 ensure_elgamal_initialized ();
307 GNUNET_assert (NULL != (x = gcry_mpi_new (0)));
309 negative = GNUNET_NO;
312 negative = GNUNET_YES;
313 exponent = -exponent;
316 gcry_mpi_set_ui (x, exponent);
318 gcry_mpi_powm (x, elgamal_g, x, elgamal_p);
320 if (GNUNET_YES == negative)
323 res = gcry_mpi_invm (x, x, elgamal_p);
325 return GNUNET_SYSERR;
328 GNUNET_CRYPTO_mpi_print_unsigned (plaintext, sizeof (struct GNUNET_SECRETSHARING_Plaintext), x);
335 * Encrypt a value. This operation is executed locally, no communication is
338 * This is a helper function, encryption can be done soley with a session's public key
339 * and the crypto system parameters.
341 * @param public_key public key to use for decryption
342 * @param message message to encrypt
343 * @param message_size number of bytes in @a message
344 * @param result_ciphertext pointer to store the resulting ciphertext
345 * @return #GNUNET_YES on succes, #GNUNET_SYSERR if the message is invalid (invalid range)
348 GNUNET_SECRETSHARING_encrypt (const struct GNUNET_SECRETSHARING_PublicKey *public_key,
349 const struct GNUNET_SECRETSHARING_Plaintext *plaintext,
350 struct GNUNET_SECRETSHARING_Ciphertext *result_ciphertext)
356 /* plaintext message */
361 ensure_elgamal_initialized ();
363 GNUNET_assert (NULL != (h = gcry_mpi_new (0)));
364 GNUNET_assert (NULL != (y = gcry_mpi_new (0)));
365 GNUNET_assert (NULL != (tmp = gcry_mpi_new (0)));
367 GNUNET_CRYPTO_mpi_scan_unsigned (&h, public_key, sizeof *public_key);
368 GNUNET_CRYPTO_mpi_scan_unsigned (&m, plaintext, sizeof *plaintext);
370 // Randomize y such that 0 < y < elgamal_q.
371 // The '- 1' is necessary as bitlength(q) = bitlength(p) - 1.
374 gcry_mpi_randomize (y, GNUNET_SECRETSHARING_ELGAMAL_BITS - 1, GCRY_WEAK_RANDOM);
375 } while ((gcry_mpi_cmp_ui (y, 0) == 0) || (gcry_mpi_cmp (y, elgamal_q) >= 0));
378 gcry_mpi_powm (tmp, elgamal_g, y, elgamal_p);
380 GNUNET_CRYPTO_mpi_print_unsigned (&result_ciphertext->c1_bits,
381 GNUNET_SECRETSHARING_ELGAMAL_BITS / 8, tmp);
384 gcry_mpi_powm (tmp, h, y, elgamal_p);
386 gcry_mpi_mulm (tmp, tmp, m, elgamal_p);
388 GNUNET_CRYPTO_mpi_print_unsigned (&result_ciphertext->c2_bits,
389 GNUNET_SECRETSHARING_ELGAMAL_BITS / 8, tmp);
396 GNUNET_SECRETSHARING_decrypt_cancel (struct GNUNET_SECRETSHARING_DecryptionHandle *h)
398 GNUNET_MQ_destroy (h->mq);
400 GNUNET_CLIENT_disconnect (h->client);