summaryrefslogtreecommitdiff
path: root/src/secretsharing/gnunet-secretsharing-profiler.c
blob: e66019dc6996b9855c6e5687a546bf34770321a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
/*
      This file is part of GNUnet
      Copyright (C) 2014 GNUnet e.V.

      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
      by the Free Software Foundation; either version 3, or (at your
      option) any later version.

      GNUnet is distributed in the hope that it will be useful, but
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      General Public License for more details.

      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
      Boston, MA 02110-1301, USA.
 */

/**
 * @file secretsharing/gnunet-secretsharing-profiler.c
 * @brief profiling tool for distributed key generation and decryption
 * @author Florian Dold
 */
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_secretsharing_service.h"
#include "gnunet_testbed_service.h"

/**
 * How many peers should participate in the key generation?
 */
static unsigned int num_peers = 3;

/**
 * What should the threshold for then key be?
 */
static unsigned int threshold = 2;

/**
 * Should we try to decrypt a value after the key generation?
 */
static int decrypt = GNUNET_NO;

/**
 * When would we like to see the operation finished?
 */
static struct GNUNET_TIME_Relative timeout;

/**
 * When should dkg communication start?
 */
static struct GNUNET_TIME_Relative delay;

/**
 * Handles for secretsharing sessions.
 */
static struct GNUNET_SECRETSHARING_Session **session_handles;

static struct GNUNET_SECRETSHARING_DecryptionHandle **decrypt_handles;

/**
 * Shares we got from the distributed key generation.
 */
static struct GNUNET_SECRETSHARING_Share **shares;

static struct GNUNET_SECRETSHARING_PublicKey common_pubkey;


static unsigned int num_connected_sessions;

static unsigned int num_connected_decrypt;

/**
 * Handles to the running peers.
 * When peers[i] is NULL, the i-th peer has stopped.
 */
static struct GNUNET_TESTBED_Peer **peers;

static struct GNUNET_PeerIdentity *peer_ids;

static unsigned int num_retrieved_peer_ids;

static unsigned int num_generated;

static unsigned int num_decrypted;

static struct GNUNET_HashCode session_id;

static unsigned int verbose;

static struct GNUNET_SECRETSHARING_Plaintext reference_plaintext;

static struct GNUNET_SECRETSHARING_Ciphertext ciphertext;

static struct GNUNET_TIME_Absolute dkg_start;

static struct GNUNET_TIME_Absolute dkg_deadline;


static struct GNUNET_TIME_Absolute decrypt_start;

static struct GNUNET_TIME_Absolute decrypt_deadline;

/**
 * Connect operations, one for every peer.
 */
static struct GNUNET_TESTBED_Operation **connect_ops;

/**
 * Are we performing a shutdown right now?
 */
static int in_shutdown;


/**
 * Signature of the event handler function called by the
 * respective event controller.
 *
 * @param cls closure
 * @param event information about the event
 */
static void
controller_cb (void *cls,
               const struct GNUNET_TESTBED_EventInformation *event)
{
  GNUNET_assert (0);
}


/**
 * Callback to be called when a service connect operation is completed
 *
 * @param cls the callback closure from functions generating an operation
 * @param op the operation that has been finished
 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
 * @param emsg error message in case the operation has failed; will be NULL if
 *          operation has executed successfully.
 */
static void
session_connect_complete (void *cls,
                          struct GNUNET_TESTBED_Operation *op,
                          void *ca_result,
                          const char *emsg)
{

  if (NULL != emsg)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "testbed connect emsg: %s\n",
                emsg);
    GNUNET_assert (0);
  }

  num_connected_sessions++;

  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "dkg: session connect complete\n");

  if (num_connected_sessions == num_peers)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                "dkg: all peers connected\n");
  }
}


/**
 * Callback to be called when a service connect operation is completed
 *
 * @param cls the callback closure from functions generating an operation
 * @param op the operation that has been finished
 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
 * @param emsg error message in case the operation has failed; will be NULL if
 *          operation has executed successfully.
 */
static void
decrypt_connect_complete (void *cls,
                          struct GNUNET_TESTBED_Operation *op,
                          void *ca_result,
                          const char *emsg)
{

  if (NULL != emsg)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "testbed connect emsg: %s\n",
                emsg);
    GNUNET_assert (0);
  }

  num_connected_decrypt++;

  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "decrypt: session connect complete\n");

  if (num_connected_decrypt == num_peers)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                "decrypt: all peers connected\n");
  }
}


/**
 * Called when a decryption has succeeded.
 *
 * @param cls Plaintext
 * @param plaintext Plaintext
 */
static void decrypt_cb (void *cls,
                        const struct GNUNET_SECRETSHARING_Plaintext *plaintext)
{
  struct GNUNET_SECRETSHARING_DecryptionHandle **dhp = cls;
  unsigned int n = dhp - decrypt_handles;
  num_decrypted++;

  *dhp = NULL;

  // we should still be connected if this is called
  GNUNET_assert (NULL != connect_ops[n]);

  GNUNET_TESTBED_operation_done (connect_ops[n]);

  if (NULL == plaintext)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "decrypt failed for peer %u\n", n);
    return;
  }
  else if (0 == memcmp (&reference_plaintext, plaintext, sizeof (struct GNUNET_SECRETSHARING_Plaintext)))
    GNUNET_log (GNUNET_ERROR_TYPE_INFO, "decrypt got correct result for peer %u\n", n);
  else
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "decrypt got wrong result for peer %u\n", n);

  if (num_decrypted == num_peers)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_INFO, "every peer decrypted\n");
    GNUNET_SCHEDULER_shutdown ();
  }

  *dhp = NULL;
}



/**
 * Adapter function called to establish a connection to
 * a service.
 *
 * @param cls closure
 * @param cfg configuration of the peer to connect to; will be available until
 *          GNUNET_TESTBED_operation_done() is called on the operation returned
 *          from GNUNET_TESTBED_service_connect()
 * @return service handle to return in 'op_result', NULL on error
 */
static void *
decrypt_connect_adapter (void *cls,
                 const struct GNUNET_CONFIGURATION_Handle *cfg)
{
  struct GNUNET_SECRETSHARING_DecryptionHandle **hp = cls;
  unsigned int n = hp - decrypt_handles;

  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "decrypt connect adapter, %d peers\n",
              num_peers);
  *hp = GNUNET_SECRETSHARING_decrypt (cfg, shares[n], &ciphertext,
                                      decrypt_start, decrypt_deadline,
                                      decrypt_cb,
                                      hp);

  return *hp;
}


/**
 * Adapter function called to destroy a connection to
 * a service.
 *
 * @param cls closure
 * @param op_result service handle returned from the connect adapter
 */
static void
decrypt_disconnect_adapter(void *cls, void *op_result)
{
  struct GNUNET_SECRETSHARING_DecryptionHandle **dh = cls;
  unsigned int n = dh - decrypt_handles;

  GNUNET_assert (*dh == decrypt_handles[n]);

  if (NULL != *dh)
  {
    GNUNET_SECRETSHARING_decrypt_cancel (*dh);
    *dh = NULL;
  }

  GNUNET_assert (NULL != connect_ops[n]);
  connect_ops[n] = NULL;
}


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)
{
  struct GNUNET_SECRETSHARING_Session **sp = cls;
  unsigned int n = sp - session_handles;
  char pubkey_str[1024];
  char *ret;

  num_generated++;
  *sp = NULL;
  shares[n] = my_share;
  if (NULL == my_share)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_INFO, "key generation failed for peer #%u\n", n);
  }
  else
  {
    ret = GNUNET_STRINGS_data_to_string (public_key, sizeof *public_key, pubkey_str, 1024);
    GNUNET_assert (NULL != ret);
    *ret = '\0';
    GNUNET_log (GNUNET_ERROR_TYPE_INFO, "key generation successful for peer #%u, pubkey %s\n", n,
                pubkey_str);

    /* we're the first to get the key -> store it */
    if (num_generated == 1)
    {
      common_pubkey = *public_key;
    }
    else if (0 != memcmp (public_key, &common_pubkey, sizeof (struct GNUNET_SECRETSHARING_PublicKey)))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "generated public keys do not match\n");
      GNUNET_SCHEDULER_shutdown ();
      return;
    }
  }

  // we should still be connected
  GNUNET_assert (NULL != connect_ops[n]);

  // disconnect from the service, will call the disconnect callback
  GNUNET_TESTBED_operation_done (connect_ops[n]);

}


/**
 * Adapter function called to establish a connection to
 * a service.
 *
 * @param cls closure
 * @param cfg configuration of the peer to connect to; will be available until
 *          GNUNET_TESTBED_operation_done() is called on the operation returned
 *          from GNUNET_TESTBED_service_connect()
 * @return service handle to return in 'op_result', NULL on error
 */
static void *
session_connect_adapter (void *cls,
                         const struct GNUNET_CONFIGURATION_Handle *cfg)
{
  struct GNUNET_SECRETSHARING_Session **sp = cls;

  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "connect adapter, %d peers\n",
              num_peers);
  *sp = GNUNET_SECRETSHARING_create_session (cfg,
                                             num_peers,
                                             peer_ids,
                                             &session_id,
                                             dkg_start,
                                             dkg_deadline,
                                             threshold,
                                             &secret_ready_cb, sp);
  return *sp;
}



/**
 * Adapter function called to destroy a connection to
 * a service.
 *
 * @param cls closure
 * @param op_result service handle returned from the connect adapter
 */
static void
session_disconnect_adapter (void *cls, void *op_result)
{
  struct GNUNET_SECRETSHARING_Session **sp = cls;
  unsigned int n = (sp - session_handles);

  GNUNET_assert (*sp == session_handles[n]);

  if (NULL != *sp)
  {
    GNUNET_SECRETSHARING_session_destroy (*sp);
    *sp = NULL;
  }

  GNUNET_assert (NULL != connect_ops[n]);
  connect_ops[n] = NULL;

  if (GNUNET_YES == in_shutdown)
    return;

  // all peers received their secret
  if (num_generated == num_peers)
  {
    int i;

    // only do decryption if requested by the user
    if (GNUNET_NO == decrypt)
    {
      GNUNET_SCHEDULER_shutdown ();
      return;
    }

    decrypt_start = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
    decrypt_deadline = GNUNET_TIME_absolute_add (decrypt_start, timeout);

    // compute g^42 as the plaintext which we will decrypt and then
    // cooperatively decrypt
    GNUNET_SECRETSHARING_plaintext_generate_i (&reference_plaintext, 42);
    GNUNET_SECRETSHARING_encrypt (&common_pubkey, &reference_plaintext, &ciphertext);

    for (i = 0; i < num_peers; i++)
      connect_ops[i] =
          GNUNET_TESTBED_service_connect (NULL, peers[i], "secretsharing", &decrypt_connect_complete, NULL,
                                          &decrypt_connect_adapter, &decrypt_disconnect_adapter, &decrypt_handles[i]);
  }
}


/**
 * Callback to be called when the requested peer information is available
 *
 * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
 * @param op the operation this callback corresponds to
 * @param pinfo the result; will be NULL if the operation has failed
 * @param emsg error message if the operation has failed; will be NULL if the
 *          operation is successfull
 */
static void
peer_info_cb (void *cb_cls,
              struct GNUNET_TESTBED_Operation *op,
              const struct GNUNET_TESTBED_PeerInformation *pinfo,
              const char *emsg)
{
  struct GNUNET_PeerIdentity *p;
  int i;

  GNUNET_assert (NULL == emsg);

  p = (struct GNUNET_PeerIdentity *) cb_cls;

  if (pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY)
  {
    *p = *pinfo->result.id;
    num_retrieved_peer_ids++;
    if (num_retrieved_peer_ids == num_peers)
      for (i = 0; i < num_peers; i++)
        connect_ops[i] =
            GNUNET_TESTBED_service_connect (NULL, peers[i], "secretsharing", session_connect_complete, NULL,
                                            session_connect_adapter, session_disconnect_adapter, &session_handles[i]);
  }
  else
  {
    GNUNET_assert (0);
  }

  GNUNET_TESTBED_operation_done (op);
}


/**
 * Signature of the main function of a task.
 *
 * @param cls closure
 */
static void
handle_shutdown (void *cls)
{
  in_shutdown = GNUNET_YES;

  if (NULL != connect_ops)
  {
    unsigned int i;
    for (i = 0; i < num_peers; i++)
      if (NULL != connect_ops[i])
      {
        // the disconnect callback will set the op to NULL
        GNUNET_TESTBED_operation_done (connect_ops[i]);
      }
    GNUNET_free (connect_ops);
  }

  // killing the testbed operation will take care of remaining
  // service handles in the disconnect callback
}


/**
 * Signature of a main function for a testcase.
 *
 * @param cls closure
 * @param h the run handle
 * @param num_peers number of peers in 'peers'
 * @param started_peers handle to peers run in the testbed.  NULL upon timeout (see
 *          GNUNET_TESTBED_test_run()).
 * @param links_succeeded the number of overlay link connection attempts that
 *          succeeded
 * @param links_failed the number of overlay link connection attempts that
 *          failed
 */
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)
{
  int i;

  GNUNET_log_setup ("gnunet-secretsharing-profiler", "INFO", NULL);

  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test master\n");

  GNUNET_SCHEDULER_add_shutdown (&handle_shutdown, NULL);

  peers = started_peers;

  peer_ids = GNUNET_malloc (num_peers * sizeof (struct GNUNET_PeerIdentity));

  session_handles = GNUNET_new_array (num_peers, struct GNUNET_SECRETSHARING_Session *);
  decrypt_handles = GNUNET_new_array (num_peers, struct GNUNET_SECRETSHARING_DecryptionHandle *);
  connect_ops = GNUNET_new_array (num_peers, struct GNUNET_TESTBED_Operation *);
  shares = GNUNET_new_array (num_peers, struct GNUNET_SECRETSHARING_Share *);

  for (i = 0; i < num_peers; i++)
  {
    // we do not store the returned operation, as peer_info_cb
    // will receive it as a parameter and call GNUNET_TESTBED_operation_done.
    GNUNET_TESTBED_peer_get_information (peers[i],
                                         GNUNET_TESTBED_PIT_IDENTITY,
                                         peer_info_cb,
                                         &peer_ids[i]);
  }
}


static void
run (void *cls, char *const *args, const char *cfgfile,
     const struct GNUNET_CONFIGURATION_Handle *cfg)
{
  static char *session_str = "gnunet-secretsharing/test";
  char *topology;
  int topology_cmp_result;

  dkg_start = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
  dkg_deadline = GNUNET_TIME_absolute_add (dkg_start, timeout);

  if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "testbed", "OVERLAY_TOPOLOGY", &topology))
  {
    fprintf (stderr,
             "'OVERLAY_TOPOLOGY' not found in 'testbed' config section, "
             "seems like you passed the wrong configuration file\n");
    return;
  }

  topology_cmp_result = strcasecmp (topology, "NONE");
  GNUNET_free (topology);

  if (0 == topology_cmp_result)
  {
    fprintf (stderr,
             "'OVERLAY_TOPOLOGY' set to 'NONE', "
             "seems like you passed the wrong configuration file\n");
    return;
  }

  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "running gnunet-secretsharing-profiler\n");

  GNUNET_CRYPTO_hash (session_str, strlen (session_str), &session_id);

  (void) GNUNET_TESTBED_test_run ("gnunet-secretsharing-profiler",
                                  cfgfile,
                                  num_peers,
                                  0,
                                  controller_cb,
                                  NULL,
                                  test_master,
                                  NULL);
}


int
main (int argc, char **argv)
{
  struct GNUNET_GETOPT_CommandLineOption options[] = {

    GNUNET_GETOPT_OPTION_SET_UINT ('n',
                                   "num-peers",
                                   NULL,
                                   gettext_noop ("number of peers in consensus"),
                                   &num_peers),

    GNUNET_GETOPT_OPTION_SET_RELATIVE_TIME ('D',
                                            "delay",
                                            NULL,
                                            gettext_noop ("dkg start delay"),
                                            &delay),

    GNUNET_GETOPT_OPTION_SET_RELATIVE_TIME ('t',
                                            "timeout",
                                            NULL,
                                            gettext_noop ("dkg timeout"),
                                            &timeout),

    GNUNET_GETOPT_OPTION_SET_UINT ('k',
                                   "threshold",
                                   NULL,
                                   gettext_noop ("threshold"),
                                   &threshold),
    
    GNUNET_GETOPT_OPTION_SET_ONE ('d',
                                  "descrypt",
                                  gettext_noop ("also profile decryption"),
                                  &decrypt),


    GNUNET_GETOPT_OPTION_VERBOSE (&verbose),

    GNUNET_GETOPT_OPTION_END
  };
  delay = GNUNET_TIME_UNIT_ZERO;
  timeout = GNUNET_TIME_UNIT_MINUTES;
  GNUNET_PROGRAM_run2 (argc, argv, "gnunet-secretsharing-profiler",
		      "help",
		      options, &run, NULL, GNUNET_YES);
  return 0;
}