gnunet-secretsharing-profiler.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2014 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file secretsharing/gnunet-secretsharing-profiler.c
  18. * @brief profiling tool for distributed key generation and decryption
  19. * @author Florian Dold
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_secretsharing_service.h"
  24. #include "gnunet_testbed_service.h"
  25. /**
  26. * How many peers should participate in the key generation?
  27. */
  28. static unsigned int num_peers = 3;
  29. /**
  30. * What should the threshold for then key be?
  31. */
  32. static unsigned int threshold = 2;
  33. /**
  34. * Should we try to decrypt a value after the key generation?
  35. */
  36. static int decrypt = GNUNET_NO;
  37. /**
  38. * When would we like to see the operation finished?
  39. */
  40. static struct GNUNET_TIME_Relative timeout;
  41. /**
  42. * When should dkg communication start?
  43. */
  44. static struct GNUNET_TIME_Relative delay;
  45. /**
  46. * Handles for secretsharing sessions.
  47. */
  48. static struct GNUNET_SECRETSHARING_Session **session_handles;
  49. static struct GNUNET_SECRETSHARING_DecryptionHandle **decrypt_handles;
  50. /**
  51. * Shares we got from the distributed key generation.
  52. */
  53. static struct GNUNET_SECRETSHARING_Share **shares;
  54. static struct GNUNET_SECRETSHARING_PublicKey common_pubkey;
  55. static unsigned int num_connected_sessions;
  56. static unsigned int num_connected_decrypt;
  57. /**
  58. * Handles to the running peers.
  59. * When peers[i] is NULL, the i-th peer has stopped.
  60. */
  61. static struct GNUNET_TESTBED_Peer **peers;
  62. static struct GNUNET_PeerIdentity *peer_ids;
  63. static unsigned int num_retrieved_peer_ids;
  64. static unsigned int num_generated;
  65. static unsigned int num_decrypted;
  66. static struct GNUNET_HashCode session_id;
  67. static unsigned int verbose;
  68. static struct GNUNET_SECRETSHARING_Plaintext reference_plaintext;
  69. static struct GNUNET_SECRETSHARING_Ciphertext ciphertext;
  70. static struct GNUNET_TIME_Absolute dkg_start;
  71. static struct GNUNET_TIME_Absolute dkg_deadline;
  72. static struct GNUNET_TIME_Absolute decrypt_start;
  73. static struct GNUNET_TIME_Absolute decrypt_deadline;
  74. /**
  75. * Connect operations, one for every peer.
  76. */
  77. static struct GNUNET_TESTBED_Operation **connect_ops;
  78. /**
  79. * Are we performing a shutdown right now?
  80. */
  81. static int in_shutdown;
  82. /**
  83. * Signature of the event handler function called by the
  84. * respective event controller.
  85. *
  86. * @param cls closure
  87. * @param event information about the event
  88. */
  89. static void
  90. controller_cb (void *cls,
  91. const struct GNUNET_TESTBED_EventInformation *event)
  92. {
  93. GNUNET_assert (0);
  94. }
  95. /**
  96. * Callback to be called when a service connect operation is completed
  97. *
  98. * @param cls the callback closure from functions generating an operation
  99. * @param op the operation that has been finished
  100. * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
  101. * @param emsg error message in case the operation has failed; will be NULL if
  102. * operation has executed successfully.
  103. */
  104. static void
  105. session_connect_complete (void *cls,
  106. struct GNUNET_TESTBED_Operation *op,
  107. void *ca_result,
  108. const char *emsg)
  109. {
  110. if (NULL != emsg)
  111. {
  112. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  113. "testbed connect emsg: %s\n",
  114. emsg);
  115. GNUNET_assert (0);
  116. }
  117. num_connected_sessions++;
  118. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  119. "dkg: session connect complete\n");
  120. if (num_connected_sessions == num_peers)
  121. {
  122. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  123. "dkg: all peers connected\n");
  124. }
  125. }
  126. /**
  127. * Callback to be called when a service connect operation is completed
  128. *
  129. * @param cls the callback closure from functions generating an operation
  130. * @param op the operation that has been finished
  131. * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
  132. * @param emsg error message in case the operation has failed; will be NULL if
  133. * operation has executed successfully.
  134. */
  135. static void
  136. decrypt_connect_complete (void *cls,
  137. struct GNUNET_TESTBED_Operation *op,
  138. void *ca_result,
  139. const char *emsg)
  140. {
  141. if (NULL != emsg)
  142. {
  143. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  144. "testbed connect emsg: %s\n",
  145. emsg);
  146. GNUNET_assert (0);
  147. }
  148. num_connected_decrypt++;
  149. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  150. "decrypt: session connect complete\n");
  151. if (num_connected_decrypt == num_peers)
  152. {
  153. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  154. "decrypt: all peers connected\n");
  155. }
  156. }
  157. /**
  158. * Called when a decryption has succeeded.
  159. *
  160. * @param cls Plaintext
  161. * @param plaintext Plaintext
  162. */
  163. static void
  164. decrypt_cb (void *cls,
  165. const struct GNUNET_SECRETSHARING_Plaintext *plaintext)
  166. {
  167. struct GNUNET_SECRETSHARING_DecryptionHandle **dhp = cls;
  168. unsigned int n = dhp - decrypt_handles;
  169. num_decrypted++;
  170. *dhp = NULL;
  171. // we should still be connected if this is called
  172. GNUNET_assert (NULL != connect_ops[n]);
  173. GNUNET_TESTBED_operation_done (connect_ops[n]);
  174. if (NULL == plaintext)
  175. {
  176. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "decrypt failed for peer %u\n", n);
  177. return;
  178. }
  179. else if (0 == GNUNET_memcmp (&reference_plaintext, plaintext))
  180. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  181. "decrypt got correct result for peer %u\n", n);
  182. else
  183. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  184. "decrypt got wrong result for peer %u\n", n);
  185. if (num_decrypted == num_peers)
  186. {
  187. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "every peer decrypted\n");
  188. GNUNET_SCHEDULER_shutdown ();
  189. }
  190. *dhp = NULL;
  191. }
  192. /**
  193. * Adapter function called to establish a connection to
  194. * a service.
  195. *
  196. * @param cls closure
  197. * @param cfg configuration of the peer to connect to; will be available until
  198. * GNUNET_TESTBED_operation_done() is called on the operation returned
  199. * from GNUNET_TESTBED_service_connect()
  200. * @return service handle to return in 'op_result', NULL on error
  201. */
  202. static void *
  203. decrypt_connect_adapter (void *cls,
  204. const struct GNUNET_CONFIGURATION_Handle *cfg)
  205. {
  206. struct GNUNET_SECRETSHARING_DecryptionHandle **hp = cls;
  207. unsigned int n = hp - decrypt_handles;
  208. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  209. "decrypt connect adapter, %d peers\n",
  210. num_peers);
  211. *hp = GNUNET_SECRETSHARING_decrypt (cfg, shares[n], &ciphertext,
  212. decrypt_start, decrypt_deadline,
  213. decrypt_cb,
  214. hp);
  215. return *hp;
  216. }
  217. /**
  218. * Adapter function called to destroy a connection to
  219. * a service.
  220. *
  221. * @param cls closure
  222. * @param op_result service handle returned from the connect adapter
  223. */
  224. static void
  225. decrypt_disconnect_adapter (void *cls, void *op_result)
  226. {
  227. struct GNUNET_SECRETSHARING_DecryptionHandle **dh = cls;
  228. unsigned int n = dh - decrypt_handles;
  229. GNUNET_assert (*dh == decrypt_handles[n]);
  230. if (NULL != *dh)
  231. {
  232. GNUNET_SECRETSHARING_decrypt_cancel (*dh);
  233. *dh = NULL;
  234. }
  235. GNUNET_assert (NULL != connect_ops[n]);
  236. connect_ops[n] = NULL;
  237. }
  238. static void
  239. secret_ready_cb (void *cls,
  240. struct GNUNET_SECRETSHARING_Share *my_share,
  241. struct GNUNET_SECRETSHARING_PublicKey *public_key,
  242. unsigned int num_ready_peers,
  243. const struct GNUNET_PeerIdentity *ready_peers)
  244. {
  245. struct GNUNET_SECRETSHARING_Session **sp = cls;
  246. unsigned int n = sp - session_handles;
  247. char pubkey_str[1024];
  248. char *ret;
  249. num_generated++;
  250. *sp = NULL;
  251. shares[n] = my_share;
  252. if (NULL == my_share)
  253. {
  254. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "key generation failed for peer #%u\n",
  255. n);
  256. }
  257. else
  258. {
  259. ret = GNUNET_STRINGS_data_to_string (public_key, sizeof *public_key,
  260. pubkey_str, 1024);
  261. GNUNET_assert (NULL != ret);
  262. *ret = '\0';
  263. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  264. "key generation successful for peer #%u, pubkey %s\n", n,
  265. pubkey_str);
  266. /* we're the first to get the key -> store it */
  267. if (num_generated == 1)
  268. {
  269. common_pubkey = *public_key;
  270. }
  271. else if (0 != GNUNET_memcmp (public_key, &common_pubkey))
  272. {
  273. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  274. "generated public keys do not match\n");
  275. GNUNET_SCHEDULER_shutdown ();
  276. return;
  277. }
  278. }
  279. // we should still be connected
  280. GNUNET_assert (NULL != connect_ops[n]);
  281. // disconnect from the service, will call the disconnect callback
  282. GNUNET_TESTBED_operation_done (connect_ops[n]);
  283. }
  284. /**
  285. * Adapter function called to establish a connection to
  286. * a service.
  287. *
  288. * @param cls closure
  289. * @param cfg configuration of the peer to connect to; will be available until
  290. * GNUNET_TESTBED_operation_done() is called on the operation returned
  291. * from GNUNET_TESTBED_service_connect()
  292. * @return service handle to return in 'op_result', NULL on error
  293. */
  294. static void *
  295. session_connect_adapter (void *cls,
  296. const struct GNUNET_CONFIGURATION_Handle *cfg)
  297. {
  298. struct GNUNET_SECRETSHARING_Session **sp = cls;
  299. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  300. "connect adapter, %d peers\n",
  301. num_peers);
  302. *sp = GNUNET_SECRETSHARING_create_session (cfg,
  303. num_peers,
  304. peer_ids,
  305. &session_id,
  306. dkg_start,
  307. dkg_deadline,
  308. threshold,
  309. &secret_ready_cb, sp);
  310. return *sp;
  311. }
  312. /**
  313. * Adapter function called to destroy a connection to
  314. * a service.
  315. *
  316. * @param cls closure
  317. * @param op_result service handle returned from the connect adapter
  318. */
  319. static void
  320. session_disconnect_adapter (void *cls, void *op_result)
  321. {
  322. struct GNUNET_SECRETSHARING_Session **sp = cls;
  323. unsigned int n = (sp - session_handles);
  324. GNUNET_assert (*sp == session_handles[n]);
  325. if (NULL != *sp)
  326. {
  327. GNUNET_SECRETSHARING_session_destroy (*sp);
  328. *sp = NULL;
  329. }
  330. GNUNET_assert (NULL != connect_ops[n]);
  331. connect_ops[n] = NULL;
  332. if (GNUNET_YES == in_shutdown)
  333. return;
  334. // all peers received their secret
  335. if (num_generated == num_peers)
  336. {
  337. int i;
  338. // only do decryption if requested by the user
  339. if (GNUNET_NO == decrypt)
  340. {
  341. GNUNET_SCHEDULER_shutdown ();
  342. return;
  343. }
  344. decrypt_start = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
  345. delay);
  346. decrypt_deadline = GNUNET_TIME_absolute_add (decrypt_start, timeout);
  347. // compute g^42 as the plaintext which we will decrypt and then
  348. // cooperatively decrypt
  349. GNUNET_SECRETSHARING_plaintext_generate_i (&reference_plaintext, 42);
  350. GNUNET_SECRETSHARING_encrypt (&common_pubkey, &reference_plaintext,
  351. &ciphertext);
  352. for (i = 0; i < num_peers; i++)
  353. connect_ops[i] =
  354. GNUNET_TESTBED_service_connect (NULL, peers[i], "secretsharing",
  355. &decrypt_connect_complete, NULL,
  356. &decrypt_connect_adapter,
  357. &decrypt_disconnect_adapter,
  358. &decrypt_handles[i]);
  359. }
  360. }
  361. /**
  362. * Callback to be called when the requested peer information is available
  363. *
  364. * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
  365. * @param op the operation this callback corresponds to
  366. * @param pinfo the result; will be NULL if the operation has failed
  367. * @param emsg error message if the operation has failed; will be NULL if the
  368. * operation is successful
  369. */
  370. static void
  371. peer_info_cb (void *cb_cls,
  372. struct GNUNET_TESTBED_Operation *op,
  373. const struct GNUNET_TESTBED_PeerInformation *pinfo,
  374. const char *emsg)
  375. {
  376. struct GNUNET_PeerIdentity *p;
  377. int i;
  378. GNUNET_assert (NULL == emsg);
  379. p = (struct GNUNET_PeerIdentity *) cb_cls;
  380. if (pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY)
  381. {
  382. *p = *pinfo->result.id;
  383. num_retrieved_peer_ids++;
  384. if (num_retrieved_peer_ids == num_peers)
  385. for (i = 0; i < num_peers; i++)
  386. connect_ops[i] =
  387. GNUNET_TESTBED_service_connect (NULL, peers[i], "secretsharing",
  388. session_connect_complete, NULL,
  389. session_connect_adapter,
  390. session_disconnect_adapter,
  391. &session_handles[i]);
  392. }
  393. else
  394. {
  395. GNUNET_assert (0);
  396. }
  397. GNUNET_TESTBED_operation_done (op);
  398. }
  399. /**
  400. * Signature of the main function of a task.
  401. *
  402. * @param cls closure
  403. */
  404. static void
  405. handle_shutdown (void *cls)
  406. {
  407. in_shutdown = GNUNET_YES;
  408. if (NULL != connect_ops)
  409. {
  410. unsigned int i;
  411. for (i = 0; i < num_peers; i++)
  412. if (NULL != connect_ops[i])
  413. {
  414. // the disconnect callback will set the op to NULL
  415. GNUNET_TESTBED_operation_done (connect_ops[i]);
  416. }
  417. GNUNET_free (connect_ops);
  418. }
  419. // killing the testbed operation will take care of remaining
  420. // service handles in the disconnect callback
  421. }
  422. /**
  423. * Signature of a main function for a testcase.
  424. *
  425. * @param cls closure
  426. * @param h the run handle
  427. * @param num_peers number of peers in 'peers'
  428. * @param started_peers handle to peers run in the testbed. NULL upon timeout (see
  429. * GNUNET_TESTBED_test_run()).
  430. * @param links_succeeded the number of overlay link connection attempts that
  431. * succeeded
  432. * @param links_failed the number of overlay link connection attempts that
  433. * failed
  434. */
  435. static void
  436. test_master (void *cls,
  437. struct GNUNET_TESTBED_RunHandle *h,
  438. unsigned int num_peers,
  439. struct GNUNET_TESTBED_Peer **started_peers,
  440. unsigned int links_succeeded,
  441. unsigned int links_failed)
  442. {
  443. int i;
  444. GNUNET_log_setup ("gnunet-secretsharing-profiler", "INFO", NULL);
  445. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test master\n");
  446. GNUNET_SCHEDULER_add_shutdown (&handle_shutdown, NULL);
  447. peers = started_peers;
  448. peer_ids = GNUNET_malloc (num_peers * sizeof(struct GNUNET_PeerIdentity));
  449. session_handles = GNUNET_new_array (num_peers, struct
  450. GNUNET_SECRETSHARING_Session *);
  451. decrypt_handles = GNUNET_new_array (num_peers, struct
  452. GNUNET_SECRETSHARING_DecryptionHandle *);
  453. connect_ops = GNUNET_new_array (num_peers, struct GNUNET_TESTBED_Operation *);
  454. shares = GNUNET_new_array (num_peers, struct GNUNET_SECRETSHARING_Share *);
  455. for (i = 0; i < num_peers; i++)
  456. {
  457. // we do not store the returned operation, as peer_info_cb
  458. // will receive it as a parameter and call GNUNET_TESTBED_operation_done.
  459. GNUNET_TESTBED_peer_get_information (peers[i],
  460. GNUNET_TESTBED_PIT_IDENTITY,
  461. peer_info_cb,
  462. &peer_ids[i]);
  463. }
  464. }
  465. static void
  466. run (void *cls, char *const *args, const char *cfgfile,
  467. const struct GNUNET_CONFIGURATION_Handle *cfg)
  468. {
  469. static char *session_str = "gnunet-secretsharing/test";
  470. char *topology;
  471. int topology_cmp_result;
  472. dkg_start = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
  473. dkg_deadline = GNUNET_TIME_absolute_add (dkg_start, timeout);
  474. if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "testbed",
  475. "OVERLAY_TOPOLOGY",
  476. &topology))
  477. {
  478. fprintf (stderr,
  479. "'OVERLAY_TOPOLOGY' not found in 'testbed' config section, "
  480. "seems like you passed the wrong configuration file\n");
  481. return;
  482. }
  483. topology_cmp_result = strcasecmp (topology, "NONE");
  484. GNUNET_free (topology);
  485. if (0 == topology_cmp_result)
  486. {
  487. fprintf (stderr,
  488. "'OVERLAY_TOPOLOGY' set to 'NONE', "
  489. "seems like you passed the wrong configuration file\n");
  490. return;
  491. }
  492. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  493. "running gnunet-secretsharing-profiler\n");
  494. GNUNET_CRYPTO_hash (session_str, strlen (session_str), &session_id);
  495. (void) GNUNET_TESTBED_test_run ("gnunet-secretsharing-profiler",
  496. cfgfile,
  497. num_peers,
  498. 0,
  499. controller_cb,
  500. NULL,
  501. test_master,
  502. NULL);
  503. }
  504. int
  505. main (int argc, char **argv)
  506. {
  507. struct GNUNET_GETOPT_CommandLineOption options[] = {
  508. GNUNET_GETOPT_option_uint ('n',
  509. "num-peers",
  510. NULL,
  511. gettext_noop ("number of peers in consensus"),
  512. &num_peers),
  513. GNUNET_GETOPT_option_relative_time ('D',
  514. "delay",
  515. NULL,
  516. gettext_noop ("dkg start delay"),
  517. &delay),
  518. GNUNET_GETOPT_option_relative_time ('t',
  519. "timeout",
  520. NULL,
  521. gettext_noop ("dkg timeout"),
  522. &timeout),
  523. GNUNET_GETOPT_option_uint ('k',
  524. "threshold",
  525. NULL,
  526. gettext_noop ("threshold"),
  527. &threshold),
  528. GNUNET_GETOPT_option_flag ('d',
  529. "descrypt",
  530. gettext_noop ("also profile decryption"),
  531. &decrypt),
  532. GNUNET_GETOPT_option_verbose (&verbose),
  533. GNUNET_GETOPT_OPTION_END
  534. };
  535. delay = GNUNET_TIME_UNIT_ZERO;
  536. timeout = GNUNET_TIME_UNIT_MINUTES;
  537. GNUNET_PROGRAM_run2 (argc, argv, "gnunet-secretsharing-profiler",
  538. "help",
  539. options, &run, NULL, GNUNET_YES);
  540. return 0;
  541. }