curl_lib: fix building with gnurl/curl.h
[oweals/gnunet.git] / src / include / gnunet_secretsharing_service.h
1 /*
2       This file is part of GNUnet
3       Copyright (C) 2013 GNUnet e.V.
4
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.
9
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.
14
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., 51 Franklin Street, Fifth Floor,
18       Boston, MA 02110-1301, USA.
19  */
20
21 /**
22  * @author Florian Dold
23  *
24  * @file
25  * Verifiable additive secret sharing and cooperative decryption
26  *
27  * @defgroup secretsharing  Secret Sharing service
28  * Verifiable additive secret sharing and cooperative decryption.
29  * @{
30  */
31
32 #ifndef GNUNET_SECRETSHARING_SERVICE_H
33 #define GNUNET_SECRETSHARING_SERVICE_H
34
35 #ifdef __cplusplus
36 extern "C"
37 {
38 #if 0                           /* keep Emacsens' auto-indent happy */
39 }
40 #endif
41 #endif
42
43 #include "platform.h"
44 #include "gnunet_common.h"
45 #include "gnunet_time_lib.h"
46 #include "gnunet_configuration_lib.h"
47 #include <gcrypt.h>
48
49
50 /**
51  * Number of bits for secretsharing elements.
52  * Must be smaller than the Pallier key size used internally
53  * by the secretsharing service.
54  * When changing this value, other internal parameters must also
55  * be adjusted.
56  */
57 #define GNUNET_SECRETSHARING_ELGAMAL_BITS 1024
58
59
60 /**
61  * The q-parameter for ElGamal encryption, a 1024-bit safe prime.
62  */
63 #define GNUNET_SECRETSHARING_ELGAMAL_P_HEX  \
64       "0x08a347d3d69e8b2dd7d1b12a08dfbccbebf4ca" \
65       "6f4269a0814e158a34312964d946b3ef22882317" \
66       "2bcf30fc08f772774cb404f9bc002a6f66b09a79" \
67       "d810d67c4f8cb3bedc6060e3c8ef874b1b64df71" \
68       "6c7d2b002da880e269438d5a776e6b5f253c8df5" \
69       "6a16b1c7ce58def07c03db48238aadfc52a354a2" \
70       "7ed285b0c1675cad3f3"
71
72 /**
73  * The q-parameter for ElGamal encryption,
74  * a 1023-bit Sophie Germain prime, q = (p-1)/2
75  */
76 #define GNUNET_SECRETSHARING_ELGAMAL_Q_HEX  \
77       "0x0451a3e9eb4f4596ebe8d895046fde65f5fa65" \
78       "37a134d040a70ac51a1894b26ca359f79144118b" \
79       "95e7987e047bb93ba65a027cde001537b3584d3c" \
80       "ec086b3e27c659df6e303071e477c3a58db26fb8" \
81       "b63e958016d4407134a1c6ad3bb735af929e46fa" \
82       "b50b58e3e72c6f783e01eda411c556fe2951aa51" \
83       "3f6942d860b3ae569f9"
84
85 /**
86  * The g-parameter for ElGamal encryption,
87  * a generator of the unique size q subgroup of Z_p^*
88  */
89 #define GNUNET_SECRETSHARING_ELGAMAL_G_HEX  \
90       "0x05c00c36d2e822950087ef09d8252994adc4e4" \
91       "8fe3ec70269f035b46063aff0c99b633fd64df43" \
92       "02442e1914c829a41505a275438871f365e91c12" \
93       "3d5303ef9e90f4b8cb89bf86cc9b513e74a72634" \
94       "9cfd9f953674fab5d511e1c078fc72d72b34086f" \
95       "c82b4b951989eb85325cb203ff98df76bc366bba" \
96       "1d7024c3650f60d0da"
97
98
99
100 /**
101  * Session that will eventually establish a shared secred between
102  * the involved peers and allow encryption and cooperative decryption.
103  */
104 struct GNUNET_SECRETSHARING_Session;
105
106 /**
107  * Share of a secret shared with a group of peers.
108  * Contains the secret share itself, the public key, the list of peers, and the
109  * exponential commitments to the secret shares of the other peers.
110  */
111 struct GNUNET_SECRETSHARING_Share;
112
113
114 /**
115  * Handle to cancel a cooperative decryption operation.
116  */
117 struct GNUNET_SECRETSHARING_DecryptionHandle;
118
119
120 /**
121  * Public key of a group sharing a secret.
122  */
123 struct GNUNET_SECRETSHARING_PublicKey
124 {
125   uint32_t bits[GNUNET_SECRETSHARING_ELGAMAL_BITS / 8 / sizeof (uint32_t)];
126 };
127
128
129 /**
130  * Encrypted field element.
131  */
132 struct GNUNET_SECRETSHARING_Ciphertext
133 {
134   uint32_t c1_bits[GNUNET_SECRETSHARING_ELGAMAL_BITS / 8 / sizeof (uint32_t)];
135   uint32_t c2_bits[GNUNET_SECRETSHARING_ELGAMAL_BITS / 8 / sizeof (uint32_t)];
136 };
137
138
139 /**
140  * Plain, unencrypted message that can be encrypted with
141  * a group public key.
142  * Note that we are not operating in GF(2^n), thus not every
143  * bit pattern is a valid plain text.
144  */
145 struct GNUNET_SECRETSHARING_Plaintext
146 {
147   /**
148    * Value of the message.
149    */
150   uint32_t bits[GNUNET_SECRETSHARING_ELGAMAL_BITS / 8 / sizeof (uint32_t)];
151 };
152
153
154 /**
155  * Called once the secret has been established with all peers, or the deadline is due.
156  *
157  * Note that the number of peers can be smaller than 'k' (this threshold parameter), which
158  * makes the threshold crypto system useless.  However, in this case one can still determine which peers
159  * were able to participate in the secret sharing successfully.
160  *
161  * If the secret sharing failed, num_ready_peers is 0 and my_share and public_key is NULL.
162  *
163  * After this callback has been called, the secretsharing session will be invalid.
164  *
165  * @param cls closure
166  * @param my_share the share of this peer
167  * @param public_key public key of the session
168  * @param num_ready_peers number of peers in @a ready_peers
169  * @param ready_peers peers that successfuly participated in establishing
170  *                    the shared secret
171  */
172 typedef void (*GNUNET_SECRETSHARING_SecretReadyCallback) (void *cls,
173                                                           struct GNUNET_SECRETSHARING_Share *my_share,
174                                                           struct GNUNET_SECRETSHARING_PublicKey *public_key,
175                                                           unsigned int num_ready_peers,
176                                                           struct GNUNET_PeerIdentity *ready_peers);
177
178
179 /**
180  * Called when a decryption has succeeded.
181  *
182  * @param cls closure
183  * @param data decrypted value
184  * @param data_size number of bytes in @a data
185  */
186 typedef void (*GNUNET_SECRETSHARING_DecryptCallback) (void *cls,
187                                                       const struct GNUNET_SECRETSHARING_Plaintext *plaintext);
188
189
190 /**
191  * Create a session that will eventually establish a shared secret
192  * with the other peers.
193  *
194  * @param cfg configuration to use
195  * @param num_peers number of peers in @a peers
196  * @param peers array of peers that we will share secrets with, can optionally contain the local peer
197  * @param session_id unique session id
198  * @param start When should all peers be available for sharing the secret?
199  *              Random number generation can take place before the start time.
200  * @param deadline point in time where the session must be established; taken as hint
201  *                 by underlying consensus sessions
202  * @param threshold minimum number of peers that must cooperate to decrypt a value
203  * @param cb called when the secret has been established
204  * @param cls closure for @a cb
205  */
206 struct GNUNET_SECRETSHARING_Session *
207 GNUNET_SECRETSHARING_create_session (const struct GNUNET_CONFIGURATION_Handle *cfg,
208                                      unsigned int num_peers,
209                                      const struct GNUNET_PeerIdentity *peers,
210                                      const struct GNUNET_HashCode *session_id,
211                                      struct GNUNET_TIME_Absolute start,
212                                      struct GNUNET_TIME_Absolute deadline,
213                                      unsigned int threshold,
214                                      GNUNET_SECRETSHARING_SecretReadyCallback cb,
215                                      void *cls);
216
217
218 /**
219  * Destroy a secret sharing session.
220  * The secret ready callback will not be called.
221  *
222  * @param s session to destroy
223  */
224 void
225 GNUNET_SECRETSHARING_session_destroy (struct GNUNET_SECRETSHARING_Session *s);
226
227
228 /**
229  * Encrypt a value.  This operation is executed locally, no communication is
230  * necessary.
231  *
232  * This is a helper function, encryption can be done soley with a session's public key
233  * and the crypto system parameters.
234  *
235  * @param public_key public key to use for decryption
236  * @param message message to encrypt
237  * @param message_size number of bytes in @a message
238  * @param result_ciphertext pointer to store the resulting ciphertext
239  * @return #GNUNET_YES on succes, #GNUNET_SYSERR if the message is invalid (invalid range)
240  */
241 int
242 GNUNET_SECRETSHARING_encrypt (const struct GNUNET_SECRETSHARING_PublicKey *public_key,
243                               const struct GNUNET_SECRETSHARING_Plaintext *plaintext,
244                               struct GNUNET_SECRETSHARING_Ciphertext *result_ciphertext);
245
246
247 /**
248  * Publish the given ciphertext for decryption.  Once a sufficient (>=k) number of peers has
249  * published the same value, it will be decrypted.
250  *
251  * When the operation is canceled, the decrypt_cb is not called anymore, but the calling
252  * peer may already have irrevocably contributed his share for the decryption of the value.
253  *
254  * @param cfg configuration to use
255  * @param share our secret share to use for decryption
256  * @param ciphertext ciphertext to publish in order to decrypt it (if enough peers agree)
257  * @param decrypt_cb callback called once the decryption succeeded
258  * @param start By when should the cooperation for decryption start?
259  * @param deadline By when should the decryption be finished?
260  * @param decrypt_cb_cls closure for @a decrypt_cb
261  * @return handle to cancel the operation
262  */
263 struct GNUNET_SECRETSHARING_DecryptionHandle *
264 GNUNET_SECRETSHARING_decrypt (const struct GNUNET_CONFIGURATION_Handle *cfg,
265                               struct GNUNET_SECRETSHARING_Share *share,
266                               const struct GNUNET_SECRETSHARING_Ciphertext *ciphertext,
267                               struct GNUNET_TIME_Absolute start,
268                               struct GNUNET_TIME_Absolute deadline,
269                               GNUNET_SECRETSHARING_DecryptCallback decrypt_cb,
270                               void *decrypt_cb_cls);
271
272
273 /**
274  * Cancel a decryption.
275  *
276  * The decrypt_cb is not called anymore, but the calling
277  * peer may already have irrevocably contributed his share for the decryption of the value.
278  *
279  * @param dh to cancel
280  */
281 void
282 GNUNET_SECRETSHARING_decrypt_cancel (struct GNUNET_SECRETSHARING_DecryptionHandle *dh);
283
284
285 /**
286  * Read a share from its binary representation.
287  *
288  * @param data Binary representation of the share.
289  * @param len Length of @a data.
290  * @param[out] readlen Number of bytes read,
291  *             ignored if NULL.
292  * @return The share, or NULL on error.
293  */
294 struct GNUNET_SECRETSHARING_Share *
295 GNUNET_SECRETSHARING_share_read (const void *data, size_t len, size_t *readlen);
296
297
298 /**
299  * Convert a share to its binary representation.
300  * Can be called with a NULL @a buf to get the size of the share.
301  *
302  * @param share Share to write.
303  * @param buf Buffer to write to.
304  * @param buflen Number of writable bytes in @a buf.
305  * @param[out] writelen Pointer to store number of bytes written,
306  *             ignored if NULL.
307  * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure.
308  */
309 int
310 GNUNET_SECRETSHARING_share_write (const struct GNUNET_SECRETSHARING_Share *share,
311                                   void *buf, size_t buflen, size_t *writelen);
312
313
314 void
315 GNUNET_SECRETSHARING_share_destroy (struct GNUNET_SECRETSHARING_Share *share);
316
317
318 int
319 GNUNET_SECRETSHARING_plaintext_generate (struct GNUNET_SECRETSHARING_Plaintext *plaintext,
320                                          gcry_mpi_t exponent);
321
322 int
323 GNUNET_SECRETSHARING_plaintext_generate_i (struct GNUNET_SECRETSHARING_Plaintext *plaintext,
324                                            int64_t exponent);
325
326
327 #if 0                           /* keep Emacsens' auto-indent happy */
328 {
329 #endif
330 #ifdef __cplusplus
331 }
332 #endif
333
334 #endif
335
336 /** @} */  /* end of group */