aboutsummaryrefslogtreecommitdiff
path: root/src/scalarproduct/gnunet-service-scalarproduct_alice.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/scalarproduct/gnunet-service-scalarproduct_alice.c')
-rw-r--r--src/scalarproduct/gnunet-service-scalarproduct_alice.c213
1 files changed, 101 insertions, 112 deletions
diff --git a/src/scalarproduct/gnunet-service-scalarproduct_alice.c b/src/scalarproduct/gnunet-service-scalarproduct_alice.c
index 45d1f4e2c..779d84b60 100644
--- a/src/scalarproduct/gnunet-service-scalarproduct_alice.c
+++ b/src/scalarproduct/gnunet-service-scalarproduct_alice.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2013, 2014 GNUnet e.V. 3 Copyright (C) 2013, 2014, 2017 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
@@ -248,15 +248,10 @@ destroy_service_session (struct AliceServiceSession *s)
248 if (GNUNET_YES == s->in_destroy) 248 if (GNUNET_YES == s->in_destroy)
249 return; 249 return;
250 s->in_destroy = GNUNET_YES; 250 s->in_destroy = GNUNET_YES;
251 if (NULL != s->cadet_mq)
252 {
253 GNUNET_MQ_destroy (s->cadet_mq);
254 s->cadet_mq = NULL;
255 }
256 if (NULL != s->client) 251 if (NULL != s->client)
257 { 252 {
258 struct GNUNET_SERVICE_Client *c = s->client; 253 struct GNUNET_SERVICE_Client *c = s->client;
259 254
260 s->client = NULL; 255 s->client = NULL;
261 GNUNET_SERVICE_client_drop (c); 256 GNUNET_SERVICE_client_drop (c);
262 } 257 }
@@ -428,17 +423,14 @@ transmit_client_response (struct AliceServiceSession *s)
428 * 423 *
429 * It must NOT call #GNUNET_CADET_channel_destroy() on the channel. 424 * It must NOT call #GNUNET_CADET_channel_destroy() on the channel.
430 * 425 *
431 * @param cls closure (set from #GNUNET_CADET_connect()) 426 * @param cls our `struct AliceServiceSession`
432 * @param channel connection to the other end (henceforth invalid) 427 * @param channel connection to the other end (henceforth invalid)
433 * @param channel_ctx place where local state associated
434 * with the channel is stored
435 */ 428 */
436static void 429static void
437cb_channel_destruction (void *cls, 430cb_channel_destruction (void *cls,
438 const struct GNUNET_CADET_Channel *channel, 431 const struct GNUNET_CADET_Channel *channel)
439 void *channel_ctx)
440{ 432{
441 struct AliceServiceSession *s = channel_ctx; 433 struct AliceServiceSession *s = cls;
442 434
443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
444 "Peer disconnected, terminating session %s with peer %s\n", 436 "Peer disconnected, terminating session %s with peer %s\n",
@@ -450,11 +442,6 @@ cb_channel_destruction (void *cls,
450 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE; 442 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
451 prepare_client_end_notification (s); 443 prepare_client_end_notification (s);
452 } 444 }
453 if (NULL != s->cadet_mq)
454 {
455 GNUNET_MQ_destroy (s->cadet_mq);
456 s->cadet_mq = NULL;
457 }
458 s->channel = NULL; 445 s->channel = NULL;
459} 446}
460 447
@@ -633,42 +620,24 @@ compute_scalar_product (struct AliceServiceSession *session)
633 620
634 621
635/** 622/**
636 * Handle a multipart chunk of a response we got from another service 623 * Check a multipart chunk of a response we got from another service
637 * we wanted to calculate a scalarproduct with. 624 * we wanted to calculate a scalarproduct with.
638 * 625 *
639 * @param cls closure (set from #GNUNET_CADET_connect) 626 * @param cls the `struct AliceServiceSession`
640 * @param channel connection to the other end 627 * @param msg the actual message
641 * @param channel_ctx place to store local state associated with the @a channel
642 * @param message the actual message
643 * @return #GNUNET_OK to keep the connection open, 628 * @return #GNUNET_OK to keep the connection open,
644 * #GNUNET_SYSERR to close it (signal serious error) 629 * #GNUNET_SYSERR to close it (signal serious error)
645 */ 630 */
646static int 631static int
647handle_bobs_cryptodata_multipart (void *cls, 632check_bobs_cryptodata_multipart (void *cls,
648 struct GNUNET_CADET_Channel *channel, 633 const struct BobCryptodataMultipartMessage *msg)
649 void **channel_ctx,
650 const struct GNUNET_MessageHeader *message)
651{ 634{
652 struct AliceServiceSession *s = *channel_ctx; 635 struct AliceServiceSession *s = cls;
653 const struct BobCryptodataMultipartMessage *msg;
654 const struct GNUNET_CRYPTO_PaillierCiphertext *payload;
655 size_t i;
656 uint32_t contained; 636 uint32_t contained;
657 size_t msg_size; 637 size_t msg_size;
658 size_t required_size; 638 size_t required_size;
659 639
660 if (NULL == s) 640 msg_size = ntohs (msg->header.size);
661 {
662 GNUNET_break_op (0);
663 return GNUNET_SYSERR;
664 }
665 msg_size = ntohs (message->size);
666 if (sizeof (struct BobCryptodataMultipartMessage) > msg_size)
667 {
668 GNUNET_break_op (0);
669 return GNUNET_SYSERR;
670 }
671 msg = (const struct BobCryptodataMultipartMessage *) message;
672 contained = ntohl (msg->contained_element_count); 641 contained = ntohl (msg->contained_element_count);
673 required_size = sizeof (struct BobCryptodataMultipartMessage) 642 required_size = sizeof (struct BobCryptodataMultipartMessage)
674 + 2 * contained * sizeof (struct GNUNET_CRYPTO_PaillierCiphertext); 643 + 2 * contained * sizeof (struct GNUNET_CRYPTO_PaillierCiphertext);
@@ -678,7 +647,26 @@ handle_bobs_cryptodata_multipart (void *cls,
678 GNUNET_break (0); 647 GNUNET_break (0);
679 return GNUNET_SYSERR; 648 return GNUNET_SYSERR;
680 } 649 }
650 return GNUNET_OK;
651}
681 652
653/**
654 * Handle a multipart chunk of a response we got from another service
655 * we wanted to calculate a scalarproduct with.
656 *
657 * @param cls the `struct AliceServiceSession`
658 * @param msg the actual message
659 */
660static void
661handle_bobs_cryptodata_multipart (void *cls,
662 const struct BobCryptodataMultipartMessage *msg)
663{
664 struct AliceServiceSession *s = cls;
665 const struct GNUNET_CRYPTO_PaillierCiphertext *payload;
666 size_t i;
667 uint32_t contained;
668
669 contained = ntohl (msg->contained_element_count);
682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
683 "Received %u additional crypto values from Bob\n", 671 "Received %u additional crypto values from Bob\n",
684 (unsigned int) contained); 672 (unsigned int) contained);
@@ -688,60 +676,41 @@ handle_bobs_cryptodata_multipart (void *cls,
688 for (i = 0; i < contained; i++) 676 for (i = 0; i < contained; i++)
689 { 677 {
690 GNUNET_memcpy (&s->r[s->cadet_received_element_count + i], 678 GNUNET_memcpy (&s->r[s->cadet_received_element_count + i],
691 &payload[2 * i], 679 &payload[2 * i],
692 sizeof (struct GNUNET_CRYPTO_PaillierCiphertext)); 680 sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
693 GNUNET_memcpy (&s->r_prime[s->cadet_received_element_count + i], 681 GNUNET_memcpy (&s->r_prime[s->cadet_received_element_count + i],
694 &payload[2 * i], 682 &payload[2 * i],
695 sizeof (struct GNUNET_CRYPTO_PaillierCiphertext)); 683 sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
696 } 684 }
697 s->cadet_received_element_count += contained; 685 s->cadet_received_element_count += contained;
698 GNUNET_CADET_receive_done (s->channel); 686 GNUNET_CADET_receive_done (s->channel);
699 if (s->cadet_received_element_count != s->used_element_count) 687 if (s->cadet_received_element_count != s->used_element_count)
700 return GNUNET_OK; 688 return; /* more to come */
701 689
702 s->product = compute_scalar_product (s); 690 s->product = compute_scalar_product (s);
703 transmit_client_response (s); 691 transmit_client_response (s);
704 return GNUNET_OK;
705} 692}
706 693
707 694
708/** 695/**
709 * Handle a response we got from another service we wanted to 696 * Check a response we got from another service we wanted to
710 * calculate a scalarproduct with. 697 * calculate a scalarproduct with.
711 * 698 *
712 * @param cls closure (set from #GNUNET_CADET_connect) 699 * @param cls our `struct AliceServiceSession`
713 * @param channel connection to the other end
714 * @param channel_ctx place to store local state associated with the channel
715 * @param message the actual message 700 * @param message the actual message
716 * @return #GNUNET_OK to keep the connection open, 701 * @return #GNUNET_OK to keep the connection open,
717 * #GNUNET_SYSERR to close it (we are done) 702 * #GNUNET_SYSERR to close it (we are done)
718 */ 703 */
719static int 704static int
720handle_bobs_cryptodata_message (void *cls, 705check_bobs_cryptodata_message (void *cls,
721 struct GNUNET_CADET_Channel *channel, 706 const struct BobCryptodataMessage *msg)
722 void **channel_ctx,
723 const struct GNUNET_MessageHeader *message)
724{ 707{
725 struct AliceServiceSession *s = *channel_ctx; 708 struct AliceServiceSession *s = cls;
726 const struct BobCryptodataMessage *msg;
727 const struct GNUNET_CRYPTO_PaillierCiphertext *payload;
728 uint32_t i;
729 uint32_t contained; 709 uint32_t contained;
730 uint16_t msg_size; 710 uint16_t msg_size;
731 size_t required_size; 711 size_t required_size;
732 712
733 if (NULL == s) 713 msg_size = ntohs (msg->header.size);
734 {
735 GNUNET_break_op (0);
736 return GNUNET_SYSERR;
737 }
738 msg_size = ntohs (message->size);
739 if (sizeof (struct BobCryptodataMessage) > msg_size)
740 {
741 GNUNET_break_op (0);
742 return GNUNET_SYSERR;
743 }
744 msg = (const struct BobCryptodataMessage *) message;
745 contained = ntohl (msg->contained_element_count); 714 contained = ntohl (msg->contained_element_count);
746 required_size = sizeof (struct BobCryptodataMessage) 715 required_size = sizeof (struct BobCryptodataMessage)
747 + 2 * contained * sizeof (struct GNUNET_CRYPTO_PaillierCiphertext) 716 + 2 * contained * sizeof (struct GNUNET_CRYPTO_PaillierCiphertext)
@@ -765,29 +734,51 @@ handle_bobs_cryptodata_message (void *cls,
765 GNUNET_break_op (0); 734 GNUNET_break_op (0);
766 return GNUNET_SYSERR; 735 return GNUNET_SYSERR;
767 } 736 }
737 return GNUNET_OK;
738}
739
740
741/**
742 * Handle a response we got from another service we wanted to
743 * calculate a scalarproduct with.
744 *
745 * @param cls our `struct AliceServiceSession`
746 * @param msg the actual message
747 */
748static void
749handle_bobs_cryptodata_message (void *cls,
750 const struct BobCryptodataMessage *msg)
751{
752 struct AliceServiceSession *s = cls;
753 const struct GNUNET_CRYPTO_PaillierCiphertext *payload;
754 uint32_t i;
755 uint32_t contained;
756
757 contained = ntohl (msg->contained_element_count);
768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
769 "Received %u crypto values from Bob\n", 759 "Received %u crypto values from Bob\n",
770 (unsigned int) contained); 760 (unsigned int) contained);
771
772 payload = (const struct GNUNET_CRYPTO_PaillierCiphertext *) &msg[1]; 761 payload = (const struct GNUNET_CRYPTO_PaillierCiphertext *) &msg[1];
773 GNUNET_memcpy (&s->s, 762 GNUNET_memcpy (&s->s,
774 &payload[0], 763 &payload[0],
775 sizeof (struct GNUNET_CRYPTO_PaillierCiphertext)); 764 sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
776 GNUNET_memcpy (&s->s_prime, 765 GNUNET_memcpy (&s->s_prime,
777 &payload[1], 766 &payload[1],
778 sizeof (struct GNUNET_CRYPTO_PaillierCiphertext)); 767 sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
779 payload = &payload[2]; 768 payload = &payload[2];
780 769
781 s->r = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_PaillierCiphertext) * s->used_element_count); 770 s->r = GNUNET_new_array (s->used_element_count,
782 s->r_prime = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_PaillierCiphertext) * s->used_element_count); 771 struct GNUNET_CRYPTO_PaillierCiphertext);
772 s->r_prime = GNUNET_new_array (s->used_element_count,
773 struct GNUNET_CRYPTO_PaillierCiphertext);
783 for (i = 0; i < contained; i++) 774 for (i = 0; i < contained; i++)
784 { 775 {
785 GNUNET_memcpy (&s->r[i], 776 GNUNET_memcpy (&s->r[i],
786 &payload[2 * i], 777 &payload[2 * i],
787 sizeof (struct GNUNET_CRYPTO_PaillierCiphertext)); 778 sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
788 GNUNET_memcpy (&s->r_prime[i], 779 GNUNET_memcpy (&s->r_prime[i],
789 &payload[2 * i + 1], 780 &payload[2 * i + 1],
790 sizeof (struct GNUNET_CRYPTO_PaillierCiphertext)); 781 sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
791 } 782 }
792 s->cadet_received_element_count = contained; 783 s->cadet_received_element_count = contained;
793 GNUNET_CADET_receive_done (s->channel); 784 GNUNET_CADET_receive_done (s->channel);
@@ -795,12 +786,10 @@ handle_bobs_cryptodata_message (void *cls,
795 if (s->cadet_received_element_count != s->used_element_count) 786 if (s->cadet_received_element_count != s->used_element_count)
796 { 787 {
797 /* More to come */ 788 /* More to come */
798 return GNUNET_OK; 789 return;
799 } 790 }
800
801 s->product = compute_scalar_product (s); 791 s->product = compute_scalar_product (s);
802 transmit_client_response (s); 792 transmit_client_response (s);
803 return GNUNET_OK;
804} 793}
805 794
806 795
@@ -1066,6 +1055,17 @@ cb_intersection_request_alice (void *cls,
1066static void 1055static void
1067client_request_complete_alice (struct AliceServiceSession *s) 1056client_request_complete_alice (struct AliceServiceSession *s)
1068{ 1057{
1058 struct GNUNET_MQ_MessageHandler cadet_handlers[] = {
1059 GNUNET_MQ_hd_var_size (bobs_cryptodata_message,
1060 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_CRYPTODATA,
1061 struct BobCryptodataMessage,
1062 s),
1063 GNUNET_MQ_hd_var_size (bobs_cryptodata_multipart,
1064 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_CRYPTODATA_MULTIPART,
1065 struct BobCryptodataMultipartMessage,
1066 s),
1067 GNUNET_MQ_handler_end ()
1068 };
1069 struct ServiceRequestMessage *msg; 1069 struct ServiceRequestMessage *msg;
1070 struct GNUNET_MQ_Envelope *e; 1070 struct GNUNET_MQ_Envelope *e;
1071 1071
@@ -1073,18 +1073,21 @@ client_request_complete_alice (struct AliceServiceSession *s)
1073 "Creating new channel for session with key %s.\n", 1073 "Creating new channel for session with key %s.\n",
1074 GNUNET_h2s (&s->session_id)); 1074 GNUNET_h2s (&s->session_id));
1075 s->channel 1075 s->channel
1076 = GNUNET_CADET_channel_create (my_cadet, 1076 = GNUNET_CADET_channel_creatE (my_cadet,
1077 s, 1077 s,
1078 &s->peer, 1078 &s->peer,
1079 &s->session_id, 1079 &s->session_id,
1080 GNUNET_CADET_OPTION_RELIABLE); 1080 GNUNET_CADET_OPTION_RELIABLE,
1081 NULL,
1082 &cb_channel_destruction,
1083 cadet_handlers);
1081 if (NULL == s->channel) 1084 if (NULL == s->channel)
1082 { 1085 {
1083 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE; 1086 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
1084 prepare_client_end_notification (s); 1087 prepare_client_end_notification (s);
1085 return; 1088 return;
1086 } 1089 }
1087 s->cadet_mq = GNUNET_CADET_mq_create (s->channel); 1090 s->cadet_mq = GNUNET_CADET_get_mq (s->channel);
1088 s->intersection_listen 1091 s->intersection_listen
1089 = GNUNET_SET_listen (cfg, 1092 = GNUNET_SET_listen (cfg,
1090 GNUNET_SET_OPERATION_INTERSECTION, 1093 GNUNET_SET_OPERATION_INTERSECTION,
@@ -1110,7 +1113,7 @@ client_request_complete_alice (struct AliceServiceSession *s)
1110 1113
1111 1114
1112/** 1115/**
1113 * We're receiving additional set data. Check if 1116 * We're receiving additional set data. Check if
1114 * @a msg is well-formed. 1117 * @a msg is well-formed.
1115 * 1118 *
1116 * @param cls client identification of the client 1119 * @param cls client identification of the client
@@ -1118,7 +1121,7 @@ client_request_complete_alice (struct AliceServiceSession *s)
1118 * @return #GNUNET_OK if @a msg is well-formed 1121 * @return #GNUNET_OK if @a msg is well-formed
1119 */ 1122 */
1120static int 1123static int
1121check_alice_client_message_multipart (void *cls, 1124check_alice_client_message_multipart (void *cls,
1122 const struct ComputationBobCryptodataMultipartMessage *msg) 1125 const struct ComputationBobCryptodataMultipartMessage *msg)
1123{ 1126{
1124 struct AliceServiceSession *s = cls; 1127 struct AliceServiceSession *s = cls;
@@ -1148,7 +1151,7 @@ check_alice_client_message_multipart (void *cls,
1148 * @param msg the actual message 1151 * @param msg the actual message
1149 */ 1152 */
1150static void 1153static void
1151handle_alice_client_message_multipart (void *cls, 1154handle_alice_client_message_multipart (void *cls,
1152 const struct ComputationBobCryptodataMultipartMessage *msg) 1155 const struct ComputationBobCryptodataMultipartMessage *msg)
1153{ 1156{
1154 struct AliceServiceSession *s = cls; 1157 struct AliceServiceSession *s = cls;
@@ -1203,7 +1206,7 @@ handle_alice_client_message_multipart (void *cls,
1203 * @return #GNUNET_OK if @a msg is well-formed 1206 * @return #GNUNET_OK if @a msg is well-formed
1204 */ 1207 */
1205static int 1208static int
1206check_alice_client_message (void *cls, 1209check_alice_client_message (void *cls,
1207 const struct AliceComputationMessage *msg) 1210 const struct AliceComputationMessage *msg)
1208{ 1211{
1209 struct AliceServiceSession *s = cls; 1212 struct AliceServiceSession *s = cls;
@@ -1232,7 +1235,7 @@ check_alice_client_message (void *cls,
1232 return GNUNET_OK; 1235 return GNUNET_OK;
1233} 1236}
1234 1237
1235 1238
1236/** 1239/**
1237 * Handler for Alice's client request message. 1240 * Handler for Alice's client request message.
1238 * We are doing request-initiation to compute a scalar product with a peer. 1241 * We are doing request-initiation to compute a scalar product with a peer.
@@ -1241,7 +1244,7 @@ check_alice_client_message (void *cls,
1241 * @param msg the actual message 1244 * @param msg the actual message
1242 */ 1245 */
1243static void 1246static void
1244handle_alice_client_message (void *cls, 1247handle_alice_client_message (void *cls,
1245 const struct AliceComputationMessage *msg) 1248 const struct AliceComputationMessage *msg)
1246{ 1249{
1247 struct AliceServiceSession *s = cls; 1250 struct AliceServiceSession *s = cls;
@@ -1382,16 +1385,6 @@ run (void *cls,
1382 const struct GNUNET_CONFIGURATION_Handle *c, 1385 const struct GNUNET_CONFIGURATION_Handle *c,
1383 struct GNUNET_SERVICE_Handle *service) 1386 struct GNUNET_SERVICE_Handle *service)
1384{ 1387{
1385 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1386 { &handle_bobs_cryptodata_message,
1387 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_CRYPTODATA,
1388 0},
1389 { &handle_bobs_cryptodata_multipart,
1390 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_CRYPTODATA_MULTIPART,
1391 0},
1392 { NULL, 0, 0}
1393 };
1394
1395 cfg = c; 1388 cfg = c;
1396 /* 1389 /*
1397 offset has to be sufficiently small to allow computation of: 1390 offset has to be sufficiently small to allow computation of:
@@ -1400,13 +1393,9 @@ run (void *cls,
1400 my_offset = gcry_mpi_new (GNUNET_CRYPTO_PAILLIER_BITS / 3); 1393 my_offset = gcry_mpi_new (GNUNET_CRYPTO_PAILLIER_BITS / 3);
1401 gcry_mpi_set_bit (my_offset, 1394 gcry_mpi_set_bit (my_offset,
1402 GNUNET_CRYPTO_PAILLIER_BITS / 3); 1395 GNUNET_CRYPTO_PAILLIER_BITS / 3);
1403
1404 GNUNET_CRYPTO_paillier_create (&my_pubkey, 1396 GNUNET_CRYPTO_paillier_create (&my_pubkey,
1405 &my_privkey); 1397 &my_privkey);
1406 my_cadet = GNUNET_CADET_connect (cfg, 1398 my_cadet = GNUNET_CADET_connecT (cfg);
1407 NULL,
1408 &cb_channel_destruction,
1409 cadet_handlers);
1410 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, 1399 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1411 NULL); 1400 NULL);
1412 if (NULL == my_cadet) 1401 if (NULL == my_cadet)
@@ -1437,7 +1426,7 @@ GNUNET_SERVICE_MAIN
1437 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_ALICE, 1426 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_ALICE,
1438 struct ComputationBobCryptodataMultipartMessage, 1427 struct ComputationBobCryptodataMultipartMessage,
1439 NULL), 1428 NULL),
1440 GNUNET_MQ_handler_end ()); 1429 GNUNET_MQ_handler_end ());
1441 1430
1442 1431
1443/* end of gnunet-service-scalarproduct_alice.c */ 1432/* end of gnunet-service-scalarproduct_alice.c */