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