aboutsummaryrefslogtreecommitdiff
path: root/src/secretsharing/gnunet-secretsharing-profiler.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/secretsharing/gnunet-secretsharing-profiler.c')
-rwxr-xr-xsrc/secretsharing/gnunet-secretsharing-profiler.c265
1 files changed, 241 insertions, 24 deletions
diff --git a/src/secretsharing/gnunet-secretsharing-profiler.c b/src/secretsharing/gnunet-secretsharing-profiler.c
index 01eb49a2d..b718e8a27 100755
--- a/src/secretsharing/gnunet-secretsharing-profiler.c
+++ b/src/secretsharing/gnunet-secretsharing-profiler.c
@@ -28,17 +28,48 @@
28#include "gnunet_secretsharing_service.h" 28#include "gnunet_secretsharing_service.h"
29#include "gnunet_testbed_service.h" 29#include "gnunet_testbed_service.h"
30 30
31/**
32 * How many peers should participate in the key generation?
33 */
31static unsigned int num_peers = 3; 34static unsigned int num_peers = 3;
32 35
36/**
37 * What should the threshold for then key be?
38 */
33static unsigned int threshold = 2; 39static unsigned int threshold = 2;
34 40
41/**
42 * Should we try to decrypt a value after the key generation?
43 */
44static unsigned int decrypt = GNUNET_NO;
45
46/**
47 * When would we like to see the operation finished?
48 */
35static struct GNUNET_TIME_Relative timeout; 49static struct GNUNET_TIME_Relative timeout;
36 50
51/**
52 * Handles for secretsharing sessions.
53 */
37static struct GNUNET_SECRETSHARING_Session **session_handles; 54static struct GNUNET_SECRETSHARING_Session **session_handles;
38 55
56static struct GNUNET_SECRETSHARING_DecryptionHandle **decrypt_handles;
57
58/**
59 * Shares we got from the distributed key generation.
60 */
61static struct GNUNET_SECRETSHARING_Share **shares;
62
63static struct GNUNET_SECRETSHARING_PublicKey common_pubkey;
64
65/**
66 * ???
67 */
39static struct GNUNET_TESTBED_Operation **testbed_operations; 68static struct GNUNET_TESTBED_Operation **testbed_operations;
40 69
41static unsigned int num_connected_handles; 70static unsigned int num_connected_sessions;
71
72static unsigned int num_connected_decrypt;
42 73
43static struct GNUNET_TESTBED_Peer **peers; 74static struct GNUNET_TESTBED_Peer **peers;
44 75
@@ -46,10 +77,18 @@ static struct GNUNET_PeerIdentity *peer_ids;
46 77
47static unsigned int num_retrieved_peer_ids; 78static unsigned int num_retrieved_peer_ids;
48 79
80static unsigned int num_generated;
81
82static unsigned int num_decrypted;
83
49static struct GNUNET_HashCode session_id; 84static struct GNUNET_HashCode session_id;
50 85
51static int verbose; 86static int verbose;
52 87
88struct GNUNET_SECRETSHARING_Plaintext reference_plaintext;
89
90struct GNUNET_SECRETSHARING_Ciphertext ciphertext;
91
53 92
54/** 93/**
55 * Signature of the event handler function called by the 94 * Signature of the event handler function called by the
@@ -76,10 +115,47 @@ controller_cb (void *cls,
76 * operation has executed successfully. 115 * operation has executed successfully.
77 */ 116 */
78static void 117static void
79connect_complete (void *cls, 118session_connect_complete (void *cls,
80 struct GNUNET_TESTBED_Operation *op, 119 struct GNUNET_TESTBED_Operation *op,
81 void *ca_result, 120 void *ca_result,
82 const char *emsg) 121 const char *emsg)
122{
123
124 if (NULL != emsg)
125 {
126 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
127 "testbed connect emsg: %s\n",
128 emsg);
129 GNUNET_assert (0);
130 }
131
132 num_connected_sessions++;
133
134 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
135 "dkg: session connect complete\n");
136
137 if (num_connected_sessions == num_peers)
138 {
139 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
140 "dkg: all peers connected\n");
141 }
142}
143
144
145/**
146 * Callback to be called when a service connect operation is completed
147 *
148 * @param cls the callback closure from functions generating an operation
149 * @param op the operation that has been finished
150 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
151 * @param emsg error message in case the operation has failed; will be NULL if
152 * operation has executed successfully.
153 */
154static void
155decrypt_connect_complete (void *cls,
156 struct GNUNET_TESTBED_Operation *op,
157 void *ca_result,
158 const char *emsg)
83{ 159{
84 160
85 if (NULL != emsg) 161 if (NULL != emsg)
@@ -90,35 +166,164 @@ connect_complete (void *cls,
90 GNUNET_assert (0); 166 GNUNET_assert (0);
91 } 167 }
92 168
93 num_connected_handles++; 169 num_connected_decrypt++;
94 170
95 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 171 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
96 "connect complete\n"); 172 "decrypt: session connect complete\n");
97 173
98 if (num_connected_handles == num_peers) 174 if (num_connected_decrypt == num_peers)
99 { 175 {
100 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 176 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
101 "all peers connected\n"); 177 "decrypt: all peers connected\n");
178 }
179}
180
181
182/**
183 * Called when a decryption has succeeded.
184 *
185 * @param cls Plaintext
186 * @param plaintext Plaintext
187 */
188static void decrypt_cb (void *cls,
189 const struct GNUNET_SECRETSHARING_Plaintext *plaintext)
190{
191 struct GNUNET_SECRETSHARING_DecryptionHandle **dhp = cls;
192 unsigned int n = dhp - decrypt_handles;
193 num_decrypted++;
194
195 *dhp = NULL;
196
197 if (NULL == plaintext)
198 {
199 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "decrypt failed for peer %u\n", n);
200 return;
201 }
202 else if (0 == memcmp (&reference_plaintext, plaintext, sizeof (struct GNUNET_SECRETSHARING_Plaintext)))
203 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "decrypt got correct result for peer %u\n", n);
204 else
205 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "decrypt got wrong result for peer %u\n", n);
206
207 if (num_decrypted == num_peers)
208 {
209 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "every peer decrypted\n");
210 GNUNET_SCHEDULER_shutdown ();
211 }
212
213 *dhp = NULL;
214}
215
216
217
218/**
219 * Adapter function called to establish a connection to
220 * a service.
221 *
222 * @param cls closure
223 * @param cfg configuration of the peer to connect to; will be available until
224 * GNUNET_TESTBED_operation_done() is called on the operation returned
225 * from GNUNET_TESTBED_service_connect()
226 * @return service handle to return in 'op_result', NULL on error
227 */
228static void *
229decrypt_connect_adapter (void *cls,
230 const struct GNUNET_CONFIGURATION_Handle *cfg)
231{
232 struct GNUNET_SECRETSHARING_DecryptionHandle **hp = cls;
233 unsigned int n = hp - decrypt_handles;
234
235 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
236 "decrypt connect adapter, %d peers\n",
237 num_peers);
238 *hp = GNUNET_SECRETSHARING_decrypt (cfg, shares[n], &ciphertext,
239 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES),
240 decrypt_cb,
241 hp);
242
243 return *hp;
244}
245
246
247/**
248 * Adapter function called to destroy a connection to
249 * a service.
250 *
251 * @param cls closure
252 * @param op_result service handle returned from the connect adapter
253 */
254static void
255decrypt_disconnect_adapter(void *cls, void *op_result)
256{
257 struct GNUNET_SECRETSHARING_DecryptionHandle **dh = cls;
258 if (NULL != *dh)
259 {
260 GNUNET_SECRETSHARING_decrypt_cancel (*dh);
261 *dh = NULL;
102 } 262 }
103} 263}
104 264
105 265
106static void 266static void
107secret_ready_cb (void *cls, 267secret_ready_cb (void *cls,
108 const struct GNUNET_SECRETSHARING_Share *my_share, 268 struct GNUNET_SECRETSHARING_Share *my_share,
109 const struct GNUNET_SECRETSHARING_PublicKey *public_key, 269 struct GNUNET_SECRETSHARING_PublicKey *public_key,
110 unsigned int num_ready_peers, 270 unsigned int num_ready_peers,
111 const struct GNUNET_PeerIdentity *ready_peers) 271 struct GNUNET_PeerIdentity *ready_peers)
112{ 272{
113 struct GNUNET_SECRETSHARING_Session **sp = cls; 273 struct GNUNET_SECRETSHARING_Session **sp = cls;
114 unsigned int n = sp - session_handles; 274 unsigned int n = sp - session_handles;
115 if (NULL == my_share || NULL == public_key) 275 char pubkey_str[1024];
276 char *ret;
277
278 num_generated++;
279 *sp = NULL;
280 shares[n] = my_share;
281 if (NULL == my_share)
116 { 282 {
117 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "key generation failed for peer #%u\n", n); 283 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "key generation failed for peer #%u\n", n);
118 return;
119 } 284 }
120 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "secret ready for peer #%u\n", n); 285 else
121 // FIXME: end profiler or try decryption if all secrets are ready 286 {
287 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "secret ready for peer #%u\n", n);
288 /* we're the first to get the key -> store it */
289 if (num_generated == 1)
290 {
291 common_pubkey = *public_key;
292 }
293 else if (0 != memcmp (public_key, &common_pubkey, sizeof (struct GNUNET_SECRETSHARING_PublicKey)))
294 {
295 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "generated public keys do not match\n");
296 GNUNET_SCHEDULER_shutdown ();
297 return;
298 }
299 }
300
301 ret = GNUNET_STRINGS_data_to_string (public_key, sizeof *public_key, pubkey_str, 1024);
302 GNUNET_assert (NULL != ret);
303 *ret = '\0';
304 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "key generation successful for peer #%u, pubkey %s\n", n,
305 pubkey_str);
306
307 // FIXME: destroy testbed operation
308
309 if (num_generated == num_peers)
310 {
311 int i;
312 if (GNUNET_NO == decrypt)
313 {
314 GNUNET_SCHEDULER_shutdown ();
315 return;
316 }
317
318 // compute g^42
319 GNUNET_SECRETSHARING_plaintext_generate_i (&reference_plaintext, 42);
320 GNUNET_SECRETSHARING_encrypt (&common_pubkey, &reference_plaintext, &ciphertext);
321
322 // FIXME: store the ops somewhere!
323 for (i = 0; i < num_peers; i++)
324 GNUNET_TESTBED_service_connect (NULL, peers[i], "secretsharing", &decrypt_connect_complete, NULL,
325 &decrypt_connect_adapter, &decrypt_disconnect_adapter, &decrypt_handles[i]);
326 }
122} 327}
123 328
124 329
@@ -133,8 +338,8 @@ secret_ready_cb (void *cls,
133 * @return service handle to return in 'op_result', NULL on error 338 * @return service handle to return in 'op_result', NULL on error
134 */ 339 */
135static void * 340static void *
136connect_adapter (void *cls, 341session_connect_adapter (void *cls,
137 const struct GNUNET_CONFIGURATION_Handle *cfg) 342 const struct GNUNET_CONFIGURATION_Handle *cfg)
138{ 343{
139 struct GNUNET_SECRETSHARING_Session **sp = cls; 344 struct GNUNET_SECRETSHARING_Session **sp = cls;
140 345
@@ -152,6 +357,7 @@ connect_adapter (void *cls,
152} 357}
153 358
154 359
360
155/** 361/**
156 * Adapter function called to destroy a connection to 362 * Adapter function called to destroy a connection to
157 * a service. 363 * a service.
@@ -160,9 +366,14 @@ connect_adapter (void *cls,
160 * @param op_result service handle returned from the connect adapter 366 * @param op_result service handle returned from the connect adapter
161 */ 367 */
162static void 368static void
163disconnect_adapter(void *cls, void *op_result) 369session_disconnect_adapter (void *cls, void *op_result)
164{ 370{
165 /* FIXME: what to do here? */ 371 struct GNUNET_SECRETSHARING_Session **sp = cls;
372 if (NULL != *sp)
373 {
374 GNUNET_SECRETSHARING_session_destroy (*sp);
375 *sp = NULL;
376 }
166} 377}
167 378
168 379
@@ -195,8 +406,8 @@ peer_info_cb (void *cb_cls,
195 if (num_retrieved_peer_ids == num_peers) 406 if (num_retrieved_peer_ids == num_peers)
196 for (i = 0; i < num_peers; i++) 407 for (i = 0; i < num_peers; i++)
197 testbed_operations[i] = 408 testbed_operations[i] =
198 GNUNET_TESTBED_service_connect (NULL, peers[i], "secretsharing", connect_complete, NULL, 409 GNUNET_TESTBED_service_connect (NULL, peers[i], "secretsharing", session_connect_complete, NULL,
199 connect_adapter, disconnect_adapter, &session_handles[i]); 410 session_connect_adapter, session_disconnect_adapter, &session_handles[i]);
200 } 411 }
201 else 412 else
202 { 413 {
@@ -238,8 +449,11 @@ test_master (void *cls,
238 449
239 peer_ids = GNUNET_malloc (num_peers * sizeof (struct GNUNET_PeerIdentity)); 450 peer_ids = GNUNET_malloc (num_peers * sizeof (struct GNUNET_PeerIdentity));
240 451
241 session_handles = GNUNET_malloc (num_peers * sizeof (struct GNUNET_SECRETSHARING_Session *)); 452 session_handles = GNUNET_new_array (num_peers, struct GNUNET_SECRETSHARING_Session *);
242 testbed_operations = GNUNET_malloc (num_peers * sizeof (struct GNUNET_TESTBED_Operation *)); 453 decrypt_handles = GNUNET_new_array (num_peers, struct GNUNET_SECRETSHARING_DecryptionHandle *);
454 testbed_operations = GNUNET_new_array (num_peers, struct GNUNET_TESTBED_Operation *);
455 shares = GNUNET_new_array (num_peers, struct GNUNET_SECRETSHARING_Share *);
456
243 457
244 for (i = 0; i < num_peers; i++) 458 for (i = 0; i < num_peers; i++)
245 GNUNET_TESTBED_peer_get_information (peers[i], 459 GNUNET_TESTBED_peer_get_information (peers[i],
@@ -305,6 +519,9 @@ main (int argc, char **argv)
305 { 'k', "threshold", NULL, 519 { 'k', "threshold", NULL,
306 gettext_noop ("threshold"), 520 gettext_noop ("threshold"),
307 GNUNET_YES, &GNUNET_GETOPT_set_uint, &threshold }, 521 GNUNET_YES, &GNUNET_GETOPT_set_uint, &threshold },
522 { 'd', "decrypt", NULL,
523 gettext_noop ("also profile decryption"),
524 GNUNET_NO, &GNUNET_GETOPT_set_one, &decrypt },
308 { 'V', "verbose", NULL, 525 { 'V', "verbose", NULL,
309 gettext_noop ("be more verbose (print received values)"), 526 gettext_noop ("be more verbose (print received values)"),
310 GNUNET_NO, &GNUNET_GETOPT_set_one, &verbose }, 527 GNUNET_NO, &GNUNET_GETOPT_set_one, &verbose },