diff options
author | Christian Grothoff <christian@grothoff.org> | 2016-06-18 21:19:27 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2016-06-18 21:19:27 +0000 |
commit | a4c1a9c44bc82c4e6863f20c63260282767036de (patch) | |
tree | 09576e2fbb5e915e97a4e25cd206c246d78d937d | |
parent | 8e3ecd2bc42af4f56562932563a3eef4cc6c33f2 (diff) | |
download | gnunet-a4c1a9c44bc82c4e6863f20c63260282767036de.tar.gz gnunet-a4c1a9c44bc82c4e6863f20c63260282767036de.zip |
update secretsharing to new MQ API
-rw-r--r-- | src/secretsharing/secretsharing.h | 4 | ||||
-rw-r--r-- | src/secretsharing/secretsharing_api.c | 96 | ||||
-rw-r--r-- | src/secretsharing/secretsharing_common.c | 17 | ||||
-rw-r--r-- | src/secretsharing/test_secretsharing_api.c | 11 |
4 files changed, 76 insertions, 52 deletions
diff --git a/src/secretsharing/secretsharing.h b/src/secretsharing/secretsharing.h index 4e725198a..5936b6ab9 100644 --- a/src/secretsharing/secretsharing.h +++ b/src/secretsharing/secretsharing.h | |||
@@ -157,7 +157,7 @@ struct GNUNET_SECRETSHARING_DecryptRequestMessage | |||
157 | struct GNUNET_SECRETSHARING_DecryptResponseMessage | 157 | struct GNUNET_SECRETSHARING_DecryptResponseMessage |
158 | { | 158 | { |
159 | /** | 159 | /** |
160 | * Type: GNUNET_MESSAGE_TYPE_SECRETSHARING_CLIENT_DECRYPT_DONE | 160 | * Type: #GNUNET_MESSAGE_TYPE_SECRETSHARING_CLIENT_DECRYPT_DONE |
161 | */ | 161 | */ |
162 | struct GNUNET_MessageHeader header; | 162 | struct GNUNET_MessageHeader header; |
163 | 163 | ||
@@ -165,7 +165,7 @@ struct GNUNET_SECRETSHARING_DecryptResponseMessage | |||
165 | * Zero if decryption failed, non-zero if decryption succeeded. | 165 | * Zero if decryption failed, non-zero if decryption succeeded. |
166 | * If the decryption failed, plaintext is also zero. | 166 | * If the decryption failed, plaintext is also zero. |
167 | */ | 167 | */ |
168 | uint32_t success; | 168 | uint32_t success GNUNET_PACKED; |
169 | 169 | ||
170 | /** | 170 | /** |
171 | * Decrypted plaintext. | 171 | * Decrypted plaintext. |
diff --git a/src/secretsharing/secretsharing_api.c b/src/secretsharing/secretsharing_api.c index 78c444810..441164850 100644 --- a/src/secretsharing/secretsharing_api.c +++ b/src/secretsharing/secretsharing_api.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | Copyright (C) 2012 GNUnet e.V. | 3 | Copyright (C) 2012, 2016 GNUnet e.V. |
4 | 4 | ||
5 | GNUnet is free software; you can redistribute it and/or modify | 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 | 6 | it under the terms of the GNU General Public License as published |
@@ -166,38 +166,51 @@ handle_decrypt_client_error (void *cls, | |||
166 | * result to the application. | 166 | * result to the application. |
167 | * | 167 | * |
168 | * @param cls the `struct GNUNET_SECRETSHARING_Session` | 168 | * @param cls the `struct GNUNET_SECRETSHARING_Session` |
169 | * @param msg message with the result | 169 | * @param m message with the result |
170 | */ | ||
171 | static int | ||
172 | check_secret_ready (void *cls, | ||
173 | const struct GNUNET_SECRETSHARING_SecretReadyMessage *m) | ||
174 | { | ||
175 | /* FIXME: actually check m is well-formed here! */ | ||
176 | return GNUNET_OK; | ||
177 | } | ||
178 | |||
179 | |||
180 | /** | ||
181 | * Handler invoked with the final result message from | ||
182 | * secret sharing. Decodes the message and passes the | ||
183 | * result to the application. | ||
184 | * | ||
185 | * @param cls the `struct GNUNET_SECRETSHARING_Session` | ||
186 | * @param m message with the result | ||
170 | */ | 187 | */ |
171 | static void | 188 | static void |
172 | handle_secret_ready (void *cls, | 189 | handle_secret_ready (void *cls, |
173 | const struct GNUNET_MessageHeader *msg) | 190 | const struct GNUNET_SECRETSHARING_SecretReadyMessage *m) |
174 | { | 191 | { |
175 | struct GNUNET_SECRETSHARING_Session *s = cls; | 192 | struct GNUNET_SECRETSHARING_Session *s = cls; |
176 | const struct GNUNET_SECRETSHARING_SecretReadyMessage *m; | ||
177 | struct GNUNET_SECRETSHARING_Share *share; | 193 | struct GNUNET_SECRETSHARING_Share *share; |
178 | size_t share_size; | 194 | size_t share_size; |
179 | 195 | ||
180 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 196 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
181 | "Got secret ready message of size %u\n", | 197 | "Got secret ready message of size %u\n", |
182 | ntohs (msg->size)); | 198 | ntohs (m->header.size)); |
183 | if (ntohs (msg->size) < sizeof (struct GNUNET_SECRETSHARING_SecretReadyMessage)) | ||
184 | { | ||
185 | GNUNET_break (0); | ||
186 | s->secret_ready_cb (s->secret_ready_cls, NULL, NULL, 0, NULL); | ||
187 | GNUNET_SECRETSHARING_session_destroy (s); | ||
188 | return; | ||
189 | } | ||
190 | m = (const struct GNUNET_SECRETSHARING_SecretReadyMessage *) msg; | ||
191 | share_size = ntohs (m->header.size) - sizeof (struct GNUNET_SECRETSHARING_SecretReadyMessage); | 199 | share_size = ntohs (m->header.size) - sizeof (struct GNUNET_SECRETSHARING_SecretReadyMessage); |
192 | 200 | ||
193 | share = GNUNET_SECRETSHARING_share_read (&m[1], | 201 | share = GNUNET_SECRETSHARING_share_read (&m[1], |
194 | share_size, | 202 | share_size, |
195 | NULL); | 203 | NULL); |
204 | GNUNET_assert (NULL != share); // FIXME: this can fail! | ||
205 | // should have been checked in #check_secret_ready! | ||
206 | // FIXME: below we never check &m[1] is valid! | ||
207 | // FIXME: do we leak 'share' here? | ||
196 | s->secret_ready_cb (s->secret_ready_cls, | 208 | s->secret_ready_cb (s->secret_ready_cls, |
197 | share, /* FIXME */ | 209 | share, /* FIXME */ |
198 | &share->public_key, | 210 | &share->public_key, |
199 | share->num_peers, | 211 | share->num_peers, |
200 | (struct GNUNET_PeerIdentity *) &m[1]); | 212 | (const struct GNUNET_PeerIdentity *) &m[1]); |
213 | |||
201 | GNUNET_SECRETSHARING_session_destroy (s); | 214 | GNUNET_SECRETSHARING_session_destroy (s); |
202 | } | 215 | } |
203 | 216 | ||
@@ -246,16 +259,18 @@ GNUNET_SECRETSHARING_create_session (const struct GNUNET_CONFIGURATION_Handle *c | |||
246 | GNUNET_SECRETSHARING_SecretReadyCallback cb, | 259 | GNUNET_SECRETSHARING_SecretReadyCallback cb, |
247 | void *cls) | 260 | void *cls) |
248 | { | 261 | { |
249 | struct GNUNET_SECRETSHARING_Session *s; | 262 | GNUNET_MQ_hd_var_size (secret_ready, |
263 | GNUNET_MESSAGE_TYPE_SECRETSHARING_CLIENT_SECRET_READY, | ||
264 | struct GNUNET_SECRETSHARING_SecretReadyMessage); | ||
265 | struct GNUNET_SECRETSHARING_Session *s | ||
266 | = GNUNET_new (struct GNUNET_SECRETSHARING_Session); | ||
267 | struct GNUNET_MQ_MessageHandler mq_handlers[] = { | ||
268 | make_secret_ready_handler (s), | ||
269 | GNUNET_MQ_handler_end () | ||
270 | }; | ||
250 | struct GNUNET_MQ_Envelope *ev; | 271 | struct GNUNET_MQ_Envelope *ev; |
251 | struct GNUNET_SECRETSHARING_CreateMessage *msg; | 272 | struct GNUNET_SECRETSHARING_CreateMessage *msg; |
252 | static const struct GNUNET_MQ_MessageHandler mq_handlers[] = { | ||
253 | { &handle_secret_ready, | ||
254 | GNUNET_MESSAGE_TYPE_SECRETSHARING_CLIENT_SECRET_READY, 0}, | ||
255 | GNUNET_MQ_HANDLERS_END | ||
256 | }; | ||
257 | 273 | ||
258 | s = GNUNET_new (struct GNUNET_SECRETSHARING_Session); | ||
259 | s->client = GNUNET_CLIENT_connect ("secretsharing", cfg); | 274 | s->client = GNUNET_CLIENT_connect ("secretsharing", cfg); |
260 | if (NULL == s->client) | 275 | if (NULL == s->client) |
261 | { | 276 | { |
@@ -293,20 +308,16 @@ GNUNET_SECRETSHARING_create_session (const struct GNUNET_CONFIGURATION_Handle *c | |||
293 | 308 | ||
294 | static void | 309 | static void |
295 | handle_decrypt_done (void *cls, | 310 | handle_decrypt_done (void *cls, |
296 | const struct GNUNET_MessageHeader *msg) | 311 | const struct GNUNET_SECRETSHARING_DecryptResponseMessage *m) |
297 | { | 312 | { |
298 | struct GNUNET_SECRETSHARING_DecryptionHandle *dh = cls; | 313 | struct GNUNET_SECRETSHARING_DecryptionHandle *dh = cls; |
299 | const struct GNUNET_SECRETSHARING_DecryptResponseMessage *m = | ||
300 | (const void *) msg; // FIXME: size check!? | ||
301 | const struct GNUNET_SECRETSHARING_Plaintext *plaintext; | 314 | const struct GNUNET_SECRETSHARING_Plaintext *plaintext; |
302 | 315 | ||
303 | if (m->success == 0) | 316 | if (m->success == 0) |
304 | plaintext = NULL; | 317 | plaintext = NULL; |
305 | else | 318 | else |
306 | plaintext = (void *) &m->plaintext; | 319 | plaintext = (void *) &m->plaintext; |
307 | |||
308 | dh->decrypt_cb (dh->decrypt_cls, plaintext); | 320 | dh->decrypt_cb (dh->decrypt_cls, plaintext); |
309 | |||
310 | GNUNET_SECRETSHARING_decrypt_cancel (dh); | 321 | GNUNET_SECRETSHARING_decrypt_cancel (dh); |
311 | } | 322 | } |
312 | 323 | ||
@@ -333,17 +344,19 @@ GNUNET_SECRETSHARING_decrypt (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
333 | GNUNET_SECRETSHARING_DecryptCallback decrypt_cb, | 344 | GNUNET_SECRETSHARING_DecryptCallback decrypt_cb, |
334 | void *decrypt_cb_cls) | 345 | void *decrypt_cb_cls) |
335 | { | 346 | { |
336 | struct GNUNET_SECRETSHARING_DecryptionHandle *s; | 347 | GNUNET_MQ_hd_fixed_size (decrypt_done, |
348 | GNUNET_MESSAGE_TYPE_SECRETSHARING_CLIENT_DECRYPT_DONE, | ||
349 | struct GNUNET_SECRETSHARING_DecryptResponseMessage); | ||
350 | struct GNUNET_SECRETSHARING_DecryptionHandle *s | ||
351 | = GNUNET_new (struct GNUNET_SECRETSHARING_DecryptionHandle); | ||
352 | struct GNUNET_MQ_MessageHandler mq_handlers[] = { | ||
353 | make_decrypt_done_handler (s), | ||
354 | GNUNET_MQ_handler_end () | ||
355 | }; | ||
337 | struct GNUNET_MQ_Envelope *ev; | 356 | struct GNUNET_MQ_Envelope *ev; |
338 | struct GNUNET_SECRETSHARING_DecryptRequestMessage *msg; | 357 | struct GNUNET_SECRETSHARING_DecryptRequestMessage *msg; |
339 | static const struct GNUNET_MQ_MessageHandler mq_handlers[] = { | ||
340 | {handle_decrypt_done, GNUNET_MESSAGE_TYPE_SECRETSHARING_CLIENT_DECRYPT_DONE, 0}, | ||
341 | GNUNET_MQ_HANDLERS_END | ||
342 | }; | ||
343 | size_t share_size; | 358 | size_t share_size; |
344 | 359 | ||
345 | |||
346 | s = GNUNET_new (struct GNUNET_SECRETSHARING_DecryptionHandle); | ||
347 | s->client = GNUNET_CLIENT_connect ("secretsharing", cfg); | 360 | s->client = GNUNET_CLIENT_connect ("secretsharing", cfg); |
348 | s->decrypt_cb = decrypt_cb; | 361 | s->decrypt_cb = decrypt_cb; |
349 | s->decrypt_cls = decrypt_cb_cls; | 362 | s->decrypt_cls = decrypt_cb_cls; |
@@ -354,13 +367,19 @@ GNUNET_SECRETSHARING_decrypt (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
354 | s); | 367 | s); |
355 | GNUNET_assert (NULL != s->mq); | 368 | GNUNET_assert (NULL != s->mq); |
356 | 369 | ||
357 | GNUNET_assert (GNUNET_OK == GNUNET_SECRETSHARING_share_write (share, NULL, 0, &share_size)); | 370 | GNUNET_assert (GNUNET_OK == |
371 | GNUNET_SECRETSHARING_share_write (share, NULL, 0, | ||
372 | &share_size)); | ||
358 | 373 | ||
359 | ev = GNUNET_MQ_msg_extra (msg, | 374 | ev = GNUNET_MQ_msg_extra (msg, |
360 | share_size, | 375 | share_size, |
361 | GNUNET_MESSAGE_TYPE_SECRETSHARING_CLIENT_DECRYPT); | 376 | GNUNET_MESSAGE_TYPE_SECRETSHARING_CLIENT_DECRYPT); |
362 | 377 | ||
363 | GNUNET_assert (GNUNET_OK == GNUNET_SECRETSHARING_share_write (share, &msg[1], share_size, NULL)); | 378 | GNUNET_assert (GNUNET_OK == |
379 | GNUNET_SECRETSHARING_share_write (share, | ||
380 | &msg[1], | ||
381 | share_size, | ||
382 | NULL)); | ||
364 | 383 | ||
365 | msg->start = GNUNET_TIME_absolute_hton (start); | 384 | msg->start = GNUNET_TIME_absolute_hton (start); |
366 | msg->deadline = GNUNET_TIME_absolute_hton (deadline); | 385 | msg->deadline = GNUNET_TIME_absolute_hton (deadline); |
@@ -368,7 +387,8 @@ GNUNET_SECRETSHARING_decrypt (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
368 | 387 | ||
369 | GNUNET_MQ_send (s->mq, ev); | 388 | GNUNET_MQ_send (s->mq, ev); |
370 | 389 | ||
371 | LOG (GNUNET_ERROR_TYPE_DEBUG, "decrypt session created\n"); | 390 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
391 | "decrypt session created\n"); | ||
372 | return s; | 392 | return s; |
373 | } | 393 | } |
374 | 394 | ||
@@ -403,7 +423,9 @@ GNUNET_SECRETSHARING_plaintext_generate_i (struct GNUNET_SECRETSHARING_Plaintext | |||
403 | return GNUNET_SYSERR; | 423 | return GNUNET_SYSERR; |
404 | } | 424 | } |
405 | 425 | ||
406 | GNUNET_CRYPTO_mpi_print_unsigned (plaintext, sizeof (struct GNUNET_SECRETSHARING_Plaintext), x); | 426 | GNUNET_CRYPTO_mpi_print_unsigned (plaintext, |
427 | sizeof (struct GNUNET_SECRETSHARING_Plaintext), | ||
428 | x); | ||
407 | 429 | ||
408 | return GNUNET_OK; | 430 | return GNUNET_OK; |
409 | } | 431 | } |
diff --git a/src/secretsharing/secretsharing_common.c b/src/secretsharing/secretsharing_common.c index 8fcba96d4..121e1975f 100644 --- a/src/secretsharing/secretsharing_common.c +++ b/src/secretsharing/secretsharing_common.c | |||
@@ -36,7 +36,7 @@ GNUNET_SECRETSHARING_share_read (const void *data, | |||
36 | { | 36 | { |
37 | struct GNUNET_SECRETSHARING_Share *share; | 37 | struct GNUNET_SECRETSHARING_Share *share; |
38 | const struct GNUNET_SECRETSHARING_ShareHeaderNBO *sh = data; | 38 | const struct GNUNET_SECRETSHARING_ShareHeaderNBO *sh = data; |
39 | char *p; | 39 | const char *p; |
40 | size_t n; | 40 | size_t n; |
41 | uint16_t payload_size; | 41 | uint16_t payload_size; |
42 | 42 | ||
@@ -47,7 +47,7 @@ GNUNET_SECRETSHARING_share_read (const void *data, | |||
47 | if (NULL != readlen) | 47 | if (NULL != readlen) |
48 | *readlen = payload_size + sizeof *sh; | 48 | *readlen = payload_size + sizeof *sh; |
49 | 49 | ||
50 | share = GNUNET_malloc (sizeof *share); | 50 | share = GNUNET_new (struct GNUNET_SECRETSHARING_Share); |
51 | 51 | ||
52 | share->threshold = ntohs (sh->threshold); | 52 | share->threshold = ntohs (sh->threshold); |
53 | share->num_peers = ntohs (sh->num_peers); | 53 | share->num_peers = ntohs (sh->num_peers); |
@@ -56,20 +56,23 @@ GNUNET_SECRETSHARING_share_read (const void *data, | |||
56 | share->my_share = sh->my_share; | 56 | share->my_share = sh->my_share; |
57 | share->public_key = sh->public_key; | 57 | share->public_key = sh->public_key; |
58 | 58 | ||
59 | p = (void *) &sh[1]; | 59 | p = (const char *) &sh[1]; |
60 | 60 | ||
61 | n = share->num_peers * sizeof (struct GNUNET_PeerIdentity); | 61 | n = share->num_peers * sizeof (struct GNUNET_PeerIdentity); |
62 | share->peers = GNUNET_malloc (n); | 62 | share->peers = GNUNET_new_array (share->num_peers, |
63 | struct GNUNET_PeerIdentity); | ||
63 | memcpy (share->peers, p, n); | 64 | memcpy (share->peers, p, n); |
64 | p += n; | 65 | p += n; |
65 | 66 | ||
66 | n = share->num_peers * sizeof (struct GNUNET_SECRETSHARING_FieldElement); | 67 | n = share->num_peers * sizeof (struct GNUNET_SECRETSHARING_FieldElement); |
67 | share->sigmas = GNUNET_malloc (n); | 68 | share->sigmas = GNUNET_new_array (share->num_peers, |
69 | struct GNUNET_SECRETSHARING_FieldElement); | ||
68 | memcpy (share->sigmas, p, n); | 70 | memcpy (share->sigmas, p, n); |
69 | p += n; | 71 | p += n; |
70 | 72 | ||
71 | n = share->num_peers * sizeof (uint16_t); | 73 | n = share->num_peers * sizeof (uint16_t); |
72 | share->original_indices = GNUNET_malloc (n); | 74 | share->original_indices = GNUNET_new_array (share->num_peers, |
75 | uint16_t); | ||
73 | memcpy (share->original_indices, p, n); | 76 | memcpy (share->original_indices, p, n); |
74 | 77 | ||
75 | return share; | 78 | return share; |
@@ -148,5 +151,3 @@ GNUNET_SECRETSHARING_share_destroy (struct GNUNET_SECRETSHARING_Share *share) | |||
148 | share->peers = NULL; | 151 | share->peers = NULL; |
149 | GNUNET_free (share); | 152 | GNUNET_free (share); |
150 | } | 153 | } |
151 | |||
152 | |||
diff --git a/src/secretsharing/test_secretsharing_api.c b/src/secretsharing/test_secretsharing_api.c index 341a33b84..c0d527f13 100644 --- a/src/secretsharing/test_secretsharing_api.c +++ b/src/secretsharing/test_secretsharing_api.c | |||
@@ -33,11 +33,12 @@ static int success; | |||
33 | static struct GNUNET_SECRETSHARING_Session *keygen; | 33 | static struct GNUNET_SECRETSHARING_Session *keygen; |
34 | 34 | ||
35 | 35 | ||
36 | static void secret_ready_cb (void *cls, | 36 | static void |
37 | struct GNUNET_SECRETSHARING_Share *my_share, | 37 | secret_ready_cb (void *cls, |
38 | struct GNUNET_SECRETSHARING_PublicKey *public_key, | 38 | struct GNUNET_SECRETSHARING_Share *my_share, |
39 | unsigned int num_ready_peers, | 39 | struct GNUNET_SECRETSHARING_PublicKey *public_key, |
40 | struct GNUNET_PeerIdentity *ready_peers) | 40 | unsigned int num_ready_peers, |
41 | const struct GNUNET_PeerIdentity *ready_peers) | ||
41 | { | 42 | { |
42 | keygen = NULL; | 43 | keygen = NULL; |
43 | if (num_ready_peers == 1) | 44 | if (num_ready_peers == 1) |