GNUnet  0.19.4
gnunet-secretsharing-profiler.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet
3  Copyright (C) 2014 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 
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
29 #include "gnunet_testbed_service.h"
30 
34 static unsigned int num_peers = 3;
35 
39 static unsigned int threshold = 2;
40 
44 static int decrypt = GNUNET_NO;
45 
49 static struct GNUNET_TIME_Relative timeout;
50 
54 static struct GNUNET_TIME_Relative delay;
55 
60 
62 
67 
69 
70 
71 static unsigned int num_connected_sessions;
72 
73 static unsigned int num_connected_decrypt;
74 
79 static struct GNUNET_TESTBED_Peer **peers;
80 
82 
83 static unsigned int num_retrieved_peer_ids;
84 
85 static unsigned int num_generated;
86 
87 static unsigned int num_decrypted;
88 
89 static struct GNUNET_HashCode session_id;
90 
91 static unsigned int verbose;
92 
94 
96 
97 static struct GNUNET_TIME_Absolute dkg_start;
98 
100 
101 
102 static struct GNUNET_TIME_Absolute decrypt_start;
103 
105 
110 
114 static int in_shutdown;
115 
116 
124 static void
125 controller_cb (void *cls,
126  const struct GNUNET_TESTBED_EventInformation *event)
127 {
128  GNUNET_assert (0);
129 }
130 
131 
141 static void
144  void *ca_result,
145  const char *emsg)
146 {
147  if (NULL != emsg)
148  {
150  "testbed connect emsg: %s\n",
151  emsg);
152  GNUNET_assert (0);
153  }
154 
156 
158  "dkg: session connect complete\n");
159 
161  {
163  "dkg: all peers connected\n");
164  }
165 }
166 
167 
177 static void
180  void *ca_result,
181  const char *emsg)
182 {
183  if (NULL != emsg)
184  {
186  "testbed connect emsg: %s\n",
187  emsg);
188  GNUNET_assert (0);
189  }
190 
192 
194  "decrypt: session connect complete\n");
195 
197  {
199  "decrypt: all peers connected\n");
200  }
201 }
202 
203 
210 static void
211 decrypt_cb (void *cls,
212  const struct GNUNET_SECRETSHARING_Plaintext *plaintext)
213 {
214  struct GNUNET_SECRETSHARING_DecryptionHandle **dhp = cls;
215  unsigned int n = dhp - decrypt_handles;
216 
217  num_decrypted++;
218 
219  *dhp = NULL;
220 
221  // we should still be connected if this is called
222  GNUNET_assert (NULL != connect_ops[n]);
223 
225 
226  if (NULL == plaintext)
227  {
228  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "decrypt failed for peer %u\n", n);
229  return;
230  }
231  else if (0 == GNUNET_memcmp (&reference_plaintext, plaintext))
233  "decrypt got correct result for peer %u\n", n);
234  else
236  "decrypt got wrong result for peer %u\n", n);
237 
238  if (num_decrypted == num_peers)
239  {
240  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "every peer decrypted\n");
242  }
243 
244  *dhp = NULL;
245 }
246 
247 
258 static void *
260  const struct GNUNET_CONFIGURATION_Handle *cfg)
261 {
262  struct GNUNET_SECRETSHARING_DecryptionHandle **hp = cls;
263  unsigned int n = hp - decrypt_handles;
264 
266  "decrypt connect adapter, %d peers\n",
267  num_peers);
270  decrypt_cb,
271  hp);
272 
273  return *hp;
274 }
275 
276 
284 static void
286 {
287  struct GNUNET_SECRETSHARING_DecryptionHandle **dh = cls;
288  unsigned int n = dh - decrypt_handles;
289 
290  GNUNET_assert (*dh == decrypt_handles[n]);
291 
292  if (NULL != *dh)
293  {
295  *dh = NULL;
296  }
297 
298  GNUNET_assert (NULL != connect_ops[n]);
299  connect_ops[n] = NULL;
300 }
301 
302 
303 static void
304 secret_ready_cb (void *cls,
305  struct GNUNET_SECRETSHARING_Share *my_share,
306  struct GNUNET_SECRETSHARING_PublicKey *public_key,
307  unsigned int num_ready_peers,
308  const struct GNUNET_PeerIdentity *ready_peers)
309 {
310  struct GNUNET_SECRETSHARING_Session **sp = cls;
311  unsigned int n = sp - session_handles;
312  char pubkey_str[1024];
313  char *ret;
314 
315  num_generated++;
316  *sp = NULL;
317  shares[n] = my_share;
318  if (NULL == my_share)
319  {
320  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "key generation failed for peer #%u\n",
321  n);
322  }
323  else
324  {
325  ret = GNUNET_STRINGS_data_to_string (public_key, sizeof *public_key,
326  pubkey_str, 1024);
327  GNUNET_assert (NULL != ret);
328  *ret = '\0';
330  "key generation successful for peer #%u, pubkey %s\n", n,
331  pubkey_str);
332 
333  /* we're the first to get the key -> store it */
334  if (num_generated == 1)
335  {
336  common_pubkey = *public_key;
337  }
338  else if (0 != GNUNET_memcmp (public_key, &common_pubkey))
339  {
341  "generated public keys do not match\n");
343  return;
344  }
345  }
346 
347  // we should still be connected
348  GNUNET_assert (NULL != connect_ops[n]);
349 
350  // disconnect from the service, will call the disconnect callback
352 }
353 
354 
365 static void *
367  const struct GNUNET_CONFIGURATION_Handle *cfg)
368 {
369  struct GNUNET_SECRETSHARING_Session **sp = cls;
370 
372  "connect adapter, %d peers\n",
373  num_peers);
375  num_peers,
376  peer_ids,
377  &session_id,
378  dkg_start,
379  dkg_deadline,
380  threshold,
381  &secret_ready_cb, sp);
382  return *sp;
383 }
384 
385 
393 static void
395 {
396  struct GNUNET_SECRETSHARING_Session **sp = cls;
397  unsigned int n = (sp - session_handles);
398 
399  GNUNET_assert (*sp == session_handles[n]);
400 
401  if (NULL != *sp)
402  {
404  *sp = NULL;
405  }
406 
407  GNUNET_assert (NULL != connect_ops[n]);
408  connect_ops[n] = NULL;
409 
410  if (GNUNET_YES == in_shutdown)
411  return;
412 
413  // all peers received their secret
414  if (num_generated == num_peers)
415  {
416  int i;
417 
418  // only do decryption if requested by the user
419  if (GNUNET_NO == decrypt)
420  {
422  return;
423  }
424 
426  delay);
428 
429  // compute g^42 as the plaintext which we will decrypt and then
430  // cooperatively decrypt
433  &ciphertext);
434 
435  for (i = 0; i < num_peers; i++)
436  connect_ops[i] =
437  GNUNET_TESTBED_service_connect (NULL, peers[i], "secretsharing",
441  &decrypt_handles[i]);
442  }
443 }
444 
445 
455 static void
456 peer_info_cb (void *cb_cls,
458  const struct GNUNET_TESTBED_PeerInformation *pinfo,
459  const char *emsg)
460 {
461  struct GNUNET_PeerIdentity *p;
462  int i;
463 
464  GNUNET_assert (NULL == emsg);
465 
466  p = (struct GNUNET_PeerIdentity *) cb_cls;
467 
468  if (pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY)
469  {
470  *p = *pinfo->result.id;
473  for (i = 0; i < num_peers; i++)
474  connect_ops[i] =
475  GNUNET_TESTBED_service_connect (NULL, peers[i], "secretsharing",
479  &session_handles[i]);
480  }
481  else
482  {
483  GNUNET_assert (0);
484  }
485 
487 }
488 
489 
495 static void
496 handle_shutdown (void *cls)
497 {
499 
500  if (NULL != connect_ops)
501  {
502  unsigned int i;
503  for (i = 0; i < num_peers; i++)
504  if (NULL != connect_ops[i])
505  {
506  // the disconnect callback will set the op to NULL
508  }
510  }
511 
512  // killing the testbed operation will take care of remaining
513  // service handles in the disconnect callback
514 }
515 
516 
530 static void
531 test_master (void *cls,
532  struct GNUNET_TESTBED_RunHandle *h,
533  unsigned int num_peers,
534  struct GNUNET_TESTBED_Peer **started_peers,
535  unsigned int links_succeeded,
536  unsigned int links_failed)
537 {
538  int i;
539 
540  GNUNET_log_setup ("gnunet-secretsharing-profiler", "INFO", NULL);
541 
542  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test master\n");
543 
545 
546  peers = started_peers;
547 
549 
556 
557  for (i = 0; i < num_peers; i++)
558  {
559  // we do not store the returned operation, as peer_info_cb
560  // will receive it as a parameter and call GNUNET_TESTBED_operation_done.
563  peer_info_cb,
564  &peer_ids[i]);
565  }
566 }
567 
568 
569 static void
570 run (void *cls, char *const *args, const char *cfgfile,
571  const struct GNUNET_CONFIGURATION_Handle *cfg)
572 {
573  static char *session_str = "gnunet-secretsharing/test";
574  char *topology;
575  int topology_cmp_result;
576 
579 
581  "OVERLAY_TOPOLOGY",
582  &topology))
583  {
584  fprintf (stderr,
585  "'OVERLAY_TOPOLOGY' not found in 'testbed' config section, "
586  "seems like you passed the wrong configuration file\n");
587  return;
588  }
589 
590  topology_cmp_result = strcasecmp (topology, "NONE");
592 
593  if (0 == topology_cmp_result)
594  {
595  fprintf (stderr,
596  "'OVERLAY_TOPOLOGY' set to 'NONE', "
597  "seems like you passed the wrong configuration file\n");
598  return;
599  }
600 
602  "running gnunet-secretsharing-profiler\n");
603 
604  GNUNET_CRYPTO_hash (session_str, strlen (session_str), &session_id);
605 
606  (void) GNUNET_TESTBED_test_run ("gnunet-secretsharing-profiler",
607  cfgfile,
608  num_peers,
609  0,
611  NULL,
612  test_master,
613  NULL);
614 }
615 
616 
617 int
618 main (int argc, char **argv)
619 {
622  "num-peers",
623  NULL,
624  gettext_noop ("number of peers in consensus"),
625  &num_peers),
626 
628  "delay",
629  NULL,
630  gettext_noop ("dkg start delay"),
631  &delay),
632 
634  "timeout",
635  NULL,
636  gettext_noop ("dkg timeout"),
637  &timeout),
638 
640  "threshold",
641  NULL,
642  gettext_noop ("threshold"),
643  &threshold),
644 
646  "descrypt",
647  gettext_noop ("also profile decryption"),
648  &decrypt),
649 
650 
652 
654  };
655 
658  GNUNET_PROGRAM_run2 (argc, argv, "gnunet-secretsharing-profiler",
659  "help",
660  options, &run, NULL, GNUNET_YES);
661  return 0;
662 }
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
enum GNUNET_TESTBED_TopologyOption topology
The topology to generate.
#define gettext_noop(String)
Definition: gettext.h:70
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static struct GNUNET_ARM_Operation * op
Current operation.
Definition: gnunet-arm.c:144
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:99
static unsigned int verbose
static unsigned int threshold
What should the threshold for then key be?
static void * session_connect_adapter(void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
Adapter function called to establish a connection to a service.
static unsigned int num_connected_decrypt
static void session_connect_complete(void *cls, struct GNUNET_TESTBED_Operation *op, void *ca_result, const char *emsg)
Callback to be called when a service connect operation is completed.
static struct GNUNET_TESTBED_Peer ** peers
Handles to the running peers.
static void secret_ready_cb(void *cls, struct GNUNET_SECRETSHARING_Share *my_share, struct GNUNET_SECRETSHARING_PublicKey *public_key, unsigned int num_ready_peers, const struct GNUNET_PeerIdentity *ready_peers)
static struct GNUNET_SECRETSHARING_Session ** session_handles
Handles for secretsharing sessions.
static struct GNUNET_SECRETSHARING_Share ** shares
Shares we got from the distributed key generation.
int main(int argc, char **argv)
static struct GNUNET_TIME_Absolute dkg_deadline
static struct GNUNET_SECRETSHARING_DecryptionHandle ** decrypt_handles
static struct GNUNET_HashCode session_id
static unsigned int num_decrypted
static int in_shutdown
Are we performing a shutdown right now?
static struct GNUNET_TIME_Absolute decrypt_start
static void peer_info_cb(void *cb_cls, struct GNUNET_TESTBED_Operation *op, const struct GNUNET_TESTBED_PeerInformation *pinfo, const char *emsg)
Callback to be called when the requested peer information is available.
static struct GNUNET_TIME_Absolute dkg_start
static unsigned int num_retrieved_peer_ids
static void test_master(void *cls, struct GNUNET_TESTBED_RunHandle *h, unsigned int num_peers, struct GNUNET_TESTBED_Peer **started_peers, unsigned int links_succeeded, unsigned int links_failed)
Signature of a main function for a testcase.
static struct GNUNET_TIME_Absolute decrypt_deadline
static void session_disconnect_adapter(void *cls, void *op_result)
Adapter function called to destroy a connection to a service.
static struct GNUNET_SECRETSHARING_PublicKey common_pubkey
static void decrypt_disconnect_adapter(void *cls, void *op_result)
Adapter function called to destroy a connection to a service.
static struct GNUNET_TIME_Relative timeout
When would we like to see the operation finished?
static struct GNUNET_SECRETSHARING_Plaintext reference_plaintext
static unsigned int num_generated
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
static struct GNUNET_SECRETSHARING_Ciphertext ciphertext
static struct GNUNET_TESTBED_Operation ** connect_ops
Connect operations, one for every peer.
static int decrypt
Should we try to decrypt a value after the key generation?
static struct GNUNET_PeerIdentity * peer_ids
static void * decrypt_connect_adapter(void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
Adapter function called to establish a connection to a service.
static void decrypt_connect_complete(void *cls, struct GNUNET_TESTBED_Operation *op, void *ca_result, const char *emsg)
Callback to be called when a service connect operation is completed.
static struct GNUNET_TIME_Relative delay
When should dkg communication start?
static void controller_cb(void *cls, const struct GNUNET_TESTBED_EventInformation *event)
Signature of the event handler function called by the respective event controller.
static unsigned int num_peers
How many peers should participate in the key generation?
static void decrypt_cb(void *cls, const struct GNUNET_SECRETSHARING_Plaintext *plaintext)
Called when a decryption has succeeded.
static unsigned int num_connected_sessions
static void handle_shutdown(void *cls)
Signature of the main function of a task.
static struct GNUNET_OS_Process * p
Helper process we started.
Definition: gnunet-uri.c:38
API for writing tests and creating large-scale emulation testbeds for GNUnet.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_string(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be a string.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_uint(char shortName, const char *name, const char *argumentHelp, const char *description, unsigned int *val)
Allow user to specify an unsigned int.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_relative_time(char shortName, const char *name, const char *argumentHelp, const char *description, struct GNUNET_TIME_Relative *val)
Allow user to specify a struct GNUNET_TIME_Relative (using human-readable "fancy" time).
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_verbose(unsigned int *level)
Define the '-V' verbosity option.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_flag(char shortName, const char *name, const char *description, int *val)
Allow user to specify a flag (which internally means setting an integer to 1/GNUNET_YES/GNUNET_OK.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:41
#define GNUNET_log(kind,...)
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
int GNUNET_log_setup(const char *comp, const char *loglevel, const char *logfile)
Setup logging.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_INFO
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
enum GNUNET_GenericReturnValue GNUNET_PROGRAM_run2(int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls, int run_without_scheduler)
Run a standard GNUnet command startup sequence (initialize loggers and configuration,...
Definition: program.c:132
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:562
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received,...
Definition: scheduler.c:1334
int GNUNET_SECRETSHARING_encrypt(const struct GNUNET_SECRETSHARING_PublicKey *public_key, const struct GNUNET_SECRETSHARING_Plaintext *plaintext, struct GNUNET_SECRETSHARING_Ciphertext *result_ciphertext)
Encrypt a value.
struct GNUNET_SECRETSHARING_DecryptionHandle * GNUNET_SECRETSHARING_decrypt(const struct GNUNET_CONFIGURATION_Handle *cfg, struct GNUNET_SECRETSHARING_Share *share, const struct GNUNET_SECRETSHARING_Ciphertext *ciphertext, struct GNUNET_TIME_Absolute start, struct GNUNET_TIME_Absolute deadline, GNUNET_SECRETSHARING_DecryptCallback decrypt_cb, void *decrypt_cb_cls)
Publish the given ciphertext for decryption.
struct GNUNET_SECRETSHARING_Session * GNUNET_SECRETSHARING_create_session(const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned int num_peers, const struct GNUNET_PeerIdentity *peers, const struct GNUNET_HashCode *session_id, struct GNUNET_TIME_Absolute start, struct GNUNET_TIME_Absolute deadline, unsigned int threshold, GNUNET_SECRETSHARING_SecretReadyCallback cb, void *cls)
Create a session that will eventually establish a shared secret with the other peers.
void GNUNET_SECRETSHARING_decrypt_cancel(struct GNUNET_SECRETSHARING_DecryptionHandle *dh)
Cancel a decryption.
int GNUNET_SECRETSHARING_plaintext_generate_i(struct GNUNET_SECRETSHARING_Plaintext *plaintext, int64_t exponent)
void GNUNET_SECRETSHARING_session_destroy(struct GNUNET_SECRETSHARING_Session *s)
Destroy a secret sharing session.
char * GNUNET_STRINGS_data_to_string(const void *data, size_t size, char *out, size_t out_size)
Convert binary data to ASCII encoding using CrockfordBase32.
Definition: strings.c:708
void GNUNET_TESTBED_operation_done(struct GNUNET_TESTBED_Operation *operation)
This function is used to signal that the event information (struct GNUNET_TESTBED_EventInformation) f...
Definition: testbed_api.c:2021
struct GNUNET_TESTBED_Operation * GNUNET_TESTBED_peer_get_information(struct GNUNET_TESTBED_Peer *peer, enum GNUNET_TESTBED_PeerInformationType pit, GNUNET_TESTBED_PeerInfoCallback cb, void *cb_cls)
Request information about a peer.
int GNUNET_TESTBED_test_run(const char *testname, const char *cfg_filename, unsigned int num_peers, uint64_t event_mask, GNUNET_TESTBED_ControllerCallback cc, void *cc_cls, GNUNET_TESTBED_TestMaster test_master, void *test_master_cls)
Convenience method for running a "simple" test on the local system with a single call from 'main'.
struct GNUNET_TESTBED_Operation * GNUNET_TESTBED_service_connect(void *op_cls, struct GNUNET_TESTBED_Peer *peer, const char *service_name, GNUNET_TESTBED_ServiceConnectCompletionCallback cb, void *cb_cls, GNUNET_TESTBED_ConnectAdapter ca, GNUNET_TESTBED_DisconnectAdapter da, void *cada_cls)
Connect to a service offered by the given peer.
@ GNUNET_TESTBED_PIT_IDENTITY
What is the identity of the peer? Returns a 'const struct GNUNET_PeerIdentity *'.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:111
#define GNUNET_TIME_UNIT_MINUTES
One minute.
#define GNUNET_TIME_UNIT_ZERO
Relative time zero.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_add(struct GNUNET_TIME_Absolute start, struct GNUNET_TIME_Relative duration)
Add a given relative duration to the given start time.
Definition: time.c:450
static int op_result(struct GNUNET_OP_Handle *h, uint64_t op_id, int64_t result_code, const void *data, uint16_t data_size, void **ctx, uint8_t cancel)
Remove an operation, and call its result callback (unless it was cancelled).
Definition: op.c:246
Definition of a command line option.
A 512-bit hashcode.
The identity of the host (wraps the signing key of the peer).
Handle to cancel a cooperative decryption operation.
Plain, unencrypted message that can be encrypted with a group public key.
Public key of a group sharing a secret.
Session that will eventually establish a shared secred between the involved peers and allow encryptio...
A share, with all values in in host byte order.
Argument to GNUNET_TESTBED_ControllerCallback with details about the event.
Opaque handle to an abstract operation to be executed by the testing framework.
Data returned from GNUNET_TESTBED_peer_get_information.
union GNUNET_TESTBED_PeerInformation::@49 result
The result of the get information operation; Choose according to the pit.
enum GNUNET_TESTBED_PeerInformationType pit
Peer information type; captures which of the types in the 'op_result' is actually in use.
struct GNUNET_PeerIdentity * id
The identity of the peer.
A peer controlled by the testing framework.
Time for absolute times used by GNUnet, in microseconds.
Time for relative time used by GNUnet, in microseconds.