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