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