d7bc46135f99a72e1f952f56b3ff933f3fbda705
[oweals/gnunet.git] / src / include / gnunet_secretsharing_service.h
1 /*
2       This file is part of GNUnet
3       (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 verified 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  * Session that will eventually establish a shared secred between
47  * the involved peers and allow encryption and cooperative decryption.
48  */
49 struct GNUNET_SECRETSHARING_Session;
50
51
52 /**
53  * Handle to cancel a cooperative decryption operation.
54  */
55 struct GNUNET_SECRETSHARING_DecryptionHandle;
56
57
58 /**
59  * Parameters of the crypto system.
60  */
61 struct GNUNET_SECRETSHARING_Parameters
62 {
63   /**
64    * Prime with p = 2q+1.
65    */
66   gcry_mpi_t p;
67   /**
68    * Prime.
69    */
70   gcry_mpi_t q;
71   /**
72    * Generator of G_q.
73    */
74   gcry_mpi_t g;
75 };
76
77
78 /**
79  * Encrypted field element.
80  */
81 struct GNUNET_SECRETSHARING_Ciphertext 
82 {
83   /**
84    * First component.
85    */
86   gcry_mpi_t c1;
87   /**
88    * Second component.
89    */
90   gcry_mpi_t c2;
91 };
92
93
94 /**
95  * Called once the secret has been established with all peers, or the deadline is due.
96  *
97  * Note that the number of peers can be smaller that 'k' (this threshold parameter), which
98  * makes the threshold crypto system useledd.  However, in this case one can still determine which peers
99  * were able to participate in the secret sharing successfully.
100  *
101  * @param cls closure
102  * @param public_key public key of the session
103  * @param num_ready_peers number of peers in @ready_peers
104  * @parem ready_peers peers that successfuly participated in establishing
105  *                    the shared secret
106  */
107 typedef void (*GNUNET_SECRETSHARING_SecretReadyCallback) (void *cls,
108                                                           gcry_mpi_t public_key,
109                                                           unsigned int num_ready_peers,
110                                                           const struct GNUNET_PeerIdentity *ready_peers);
111
112
113 /**
114  * Called when a decryption has succeeded.
115  *
116  * @param cls closure
117  * @param result decrypted value
118  */
119 typedef void (*GNUNET_SECRETSHARING_DecryptCallback) (void *cls,
120                                                       gcry_mpi_t result);
121
122
123 /**
124  * Create a session that will eventually establish a shared secret
125  * with the other peers.
126  *
127  * @param cfg configuration to use
128  * @param num_peers number of peers in @peers
129  * @param session_id unique session id
130  * @param deadline point in time where the session must be established; taken as hint
131  *                 by underlying consensus sessions
132  * @param parameters parameters for the crypto system
133  * @param threshold minimum number of peers that must cooperate to decrypt a value
134  * @param cb called when the secret has been established
135  * @param cls closure for cb
136  */
137 struct GNUNET_SECRETSHARING_Session *
138 GNUNET_SECRETSHARING_create_session (const struct GNUNET_CONFIGURATION_Handle *cfg,
139                                      unsigned int num_peers,
140                                      const struct GNUNET_PeerIdentity *peers,
141                                      const struct GNUNET_HashCode *session_id,
142                                      struct GNUNET_TIME_Absolute deadline,
143                                      struct GNUNET_SECRETSHARING_Parameters *parameters,
144                                      unsigned int threshold,
145                                      GNUNET_SECRETSHARING_SecretReadyCallback *cb,
146                                      void *cls);
147
148
149 /**
150  * Destroy a secret sharing session.
151  *
152  * @param session session to destroy
153  */
154 void
155 GNUNET_SECRETSHARING_destroy_session (struct GNUNET_SECRETSHARING_Session *session);
156
157
158 /**
159  * Encrypt a value.  This operation is executed locally, no communication is
160  * necessary.
161  *
162  * This is a helper function, encryption can be done soley with a session's public key
163  * and the crypto system parameters.
164  *
165  * @param session session to take the key for encryption from,
166  *                the session's ready callback must have been already called
167  * @param message message to encrypt
168  * @param result_cyphertext pointer to store the resulting ciphertext
169  * @return GNUNET_YES on succes, GNUNET_SYSERR if the message is invalid (invalid range)
170  */
171 int 
172 GNUNET_SECRETSHARING_encrypt (const struct GNUNET_SECRETSHARING_Session *session,
173                               gcry_mpi_t message,
174                               struct GNUNET_SECRETSHARING_Ciphertext *result_ciphertext);
175
176
177 /**
178  * Publish the given ciphertext for decryption.  Once a sufficient (>=k) number of peers has
179  * published the same value, it will be decrypted.
180  *
181  * When the operation is canceled, the decrypt_cb is not called anymore, but the calling
182  * peer may already have irrevocably contributed his share for the decryption of the value.
183  *
184  * @param session session to use for the decryption
185  * @param ciphertext ciphertext to publish in order to decrypt it (if enough peers agree)
186  * @param decrypt_cb callback called once the decryption succeeded
187  * @param cls closure for decrypt_cb
188  * @return handle to cancel the operation
189  */
190 struct GNUNET_SECRETSHARING_DecryptionHandle *
191 GNUNET_SECRETSHARING_publish_decrypt (struct GNUNET_SECRETSHARING_Session *session,
192                                       struct GNUNET_SECRETSHARING_Ciphertext *ciphertext,
193                                       GNUNET_SECRETSHARING_DecryptCallback decrypt_cb,
194                                       void *cls);
195
196
197 /**
198  * Cancel a decryption.
199  *
200  * The decrypt_cb is not called anymore, but the calling
201  * peer may already have irrevocably contributed his share for the decryption of the value.
202  *
203  * @param decryption_handle decryption to cancel
204  */
205 void
206 GNUNET_SECRETSHARING_cancel_decrypt (struct GNUNET_SECRETSHARING_DecryptionHandle *decryption_handle);
207
208
209 #if 0                           /* keep Emacsens' auto-indent happy */
210 {
211 #endif
212 #ifdef __cplusplus
213 }
214 #endif
215
216 #endif