aboutsummaryrefslogtreecommitdiff
path: root/src/scalarproduct
diff options
context:
space:
mode:
authorChristian Fuchs <christian.fuchs@cfuchs.net>2013-10-08 08:32:47 +0000
committerChristian Fuchs <christian.fuchs@cfuchs.net>2013-10-08 08:32:47 +0000
commit31cc9c3182ef5ac01edcdd4c4e4dc9c312fc8c9c (patch)
tree979af902cba17476c64d724e9ddd750bf80d46c8 /src/scalarproduct
parent315d98a19702f2f29c1e18706909dd40917fe1f7 (diff)
downloadgnunet-31cc9c3182ef5ac01edcdd4c4e4dc9c312fc8c9c.tar.gz
gnunet-31cc9c3182ef5ac01edcdd4c4e4dc9c312fc8c9c.zip
work on multipart request receiving
Diffstat (limited to 'src/scalarproduct')
-rw-r--r--src/scalarproduct/gnunet-service-scalarproduct.c687
1 files changed, 287 insertions, 400 deletions
diff --git a/src/scalarproduct/gnunet-service-scalarproduct.c b/src/scalarproduct/gnunet-service-scalarproduct.c
index bdec7ebd8..518ff5f3d 100644
--- a/src/scalarproduct/gnunet-service-scalarproduct.c
+++ b/src/scalarproduct/gnunet-service-scalarproduct.c
@@ -44,15 +44,15 @@
44 */ 44 */
45enum SessionState 45enum SessionState
46{ 46{
47 CLIENT_REQUEST_RECEIVED, 47 CLIENT_REQUEST_RECEIVED,
48 WAITING_FOR_BOBS_CONNECT, 48 WAITING_FOR_BOBS_CONNECT,
49 CLIENT_RESPONSE_RECEIVED, 49 CLIENT_RESPONSE_RECEIVED,
50 WAITING_FOR_SERVICE_REQUEST, 50 WAITING_FOR_SERVICE_REQUEST,
51 WAITING_FOR_MULTIPART_TRANSMISSION, 51 WAITING_FOR_MULTIPART_TRANSMISSION,
52 WAITING_FOR_SERVICE_RESPONSE, 52 WAITING_FOR_SERVICE_RESPONSE,
53 SERVICE_REQUEST_RECEIVED, 53 SERVICE_REQUEST_RECEIVED,
54 SERVICE_RESPONSE_RECEIVED, 54 SERVICE_RESPONSE_RECEIVED,
55 FINALIZED 55 FINALIZED
56}; 56};
57 57
58/** 58/**
@@ -60,8 +60,8 @@ enum SessionState
60 */ 60 */
61enum PeerRole 61enum PeerRole
62{ 62{
63 ALICE, 63 ALICE,
64 BOB 64 BOB
65}; 65};
66 66
67/** 67/**
@@ -73,130 +73,130 @@ enum PeerRole
73 */ 73 */
74struct ServiceSession 74struct ServiceSession
75{ 75{
76 /** 76 /**
77 * the role this peer has 77 * the role this peer has
78 */ 78 */
79 enum PeerRole role; 79 enum PeerRole role;
80 80
81 /** 81 /**
82 * session information is kept in a DLL 82 * session information is kept in a DLL
83 */ 83 */
84 struct ServiceSession *next; 84 struct ServiceSession *next;
85 85
86 /** 86 /**
87 * session information is kept in a DLL 87 * session information is kept in a DLL
88 */ 88 */
89 struct ServiceSession *prev; 89 struct ServiceSession *prev;
90 90
91 /** 91 /**
92 * (hopefully) unique transaction ID 92 * (hopefully) unique transaction ID
93 */ 93 */
94 struct GNUNET_HashCode key; 94 struct GNUNET_HashCode key;
95 95
96 /** 96 /**
97 * state of the session 97 * state of the session
98 */ 98 */
99 enum SessionState state; 99 enum SessionState state;
100 100
101 /** 101 /**
102 * Alice or Bob's peerID 102 * Alice or Bob's peerID
103 */ 103 */
104 struct GNUNET_PeerIdentity peer; 104 struct GNUNET_PeerIdentity peer;
105 105
106 /** 106 /**
107 * the client this request is related to 107 * the client this request is related to
108 */ 108 */
109 struct GNUNET_SERVER_Client * client; 109 struct GNUNET_SERVER_Client * client;
110 110
111 /** 111 /**
112 * The message to send 112 * The message to send
113 */ 113 */
114 struct GNUNET_MessageHeader * msg; 114 struct GNUNET_MessageHeader * msg;
115 115
116 /** 116 /**
117 * how many elements we were supplied with from the client 117 * how many elements we were supplied with from the client
118 */ 118 */
119 uint32_t element_count; 119 uint32_t element_count;
120 120
121 /** 121 /**
122 * how many elements actually are used after applying the mask 122 * how many elements actually are used after applying the mask
123 */ 123 */
124 uint32_t used_element_count; 124 uint32_t used_element_count;
125 125
126 /** 126 /**
127 * already transferred elements (sent/received) for multipart messages, less or equal than used_element_count for 127 * already transferred elements (sent/received) for multipart messages, less or equal than used_element_count for
128 */ 128 */
129 uint32_t transferred_element_count; 129 uint32_t transferred_element_count;
130 130
131 /** 131 /**
132 * index of the last transferred element for multipart messages 132 * index of the last transferred element for multipart messages
133 */ 133 */
134 uint32_t last_processed_element; 134 uint32_t last_processed_element;
135 135
136 /** 136 /**
137 * how many bytes the mask is long. 137 * how many bytes the mask is long.
138 * just for convenience so we don't have to re-re-re calculate it each time 138 * just for convenience so we don't have to re-re-re calculate it each time
139 */ 139 */
140 uint32_t mask_length; 140 uint32_t mask_length;
141 141
142 /** 142 /**
143 * all the vector elements we received 143 * all the vector elements we received
144 */ 144 */
145 int32_t * vector; 145 int32_t * vector;
146 146
147 /** 147 /**
148 * mask of which elements to check 148 * mask of which elements to check
149 */ 149 */
150 unsigned char * mask; 150 unsigned char * mask;
151 151
152 /** 152 /**
153 * Public key of the remote service, only used by bob 153 * Public key of the remote service, only used by bob
154 */ 154 */
155 gcry_sexp_t remote_pubkey; 155 gcry_sexp_t remote_pubkey;
156 156
157 /** 157 /**
158 * E(ai)(Bob) or ai(Alice) after applying the mask 158 * E(ai)(Bob) or ai(Alice) after applying the mask
159 */ 159 */
160 gcry_mpi_t * a; 160 gcry_mpi_t * a;
161 161
162 /** 162 /**
163 * Bob's permutation p of R 163 * Bob's permutation p of R
164 */ 164 */
165 gcry_mpi_t * r; 165 gcry_mpi_t * r;
166 166
167 /** 167 /**
168 * Bob's permutation q of R 168 * Bob's permutation q of R
169 */ 169 */
170 gcry_mpi_t * r_prime; 170 gcry_mpi_t * r_prime;
171 171
172 /** 172 /**
173 * Bobs matching response session from the client 173 * Bobs matching response session from the client
174 */ 174 */
175 struct ServiceSession * response; 175 struct ServiceSession * response;
176 176
177 /** 177 /**
178 * The computed scalar 178 * The computed scalar
179 */ 179 */
180 gcry_mpi_t product; 180 gcry_mpi_t product;
181 181
182 /** 182 /**
183 * My transmit handle for the current message to a alice/bob 183 * My transmit handle for the current message to a alice/bob
184 */ 184 */
185 struct GNUNET_MESH_TransmitHandle * service_transmit_handle; 185 struct GNUNET_MESH_TransmitHandle * service_transmit_handle;
186 186
187 /** 187 /**
188 * My transmit handle for the current message to the client 188 * My transmit handle for the current message to the client
189 */ 189 */
190 struct GNUNET_SERVER_TransmitHandle * client_transmit_handle; 190 struct GNUNET_SERVER_TransmitHandle * client_transmit_handle;
191 191
192 /** 192 /**
193 * tunnel-handle associated with our mesh handle 193 * tunnel-handle associated with our mesh handle
194 */ 194 */
195 struct GNUNET_MESH_Tunnel * tunnel; 195 struct GNUNET_MESH_Tunnel * tunnel;
196 196
197 GNUNET_SCHEDULER_TaskIdentifier client_notification_task; 197 GNUNET_SCHEDULER_TaskIdentifier client_notification_task;
198 198
199 GNUNET_SCHEDULER_TaskIdentifier service_request_task; 199 GNUNET_SCHEDULER_TaskIdentifier service_request_task;
200}; 200};
201 201
202/////////////////////////////////////////////////////////////////////////////// 202///////////////////////////////////////////////////////////////////////////////
@@ -290,7 +290,6 @@ static int do_shutdown;
290// Helper Functions 290// Helper Functions
291/////////////////////////////////////////////////////////////////////////////// 291///////////////////////////////////////////////////////////////////////////////
292 292
293
294/** 293/**
295 * Generates an Paillier private/public keyset and extracts the values using libgrcypt only 294 * Generates an Paillier private/public keyset and extracts the values using libgrcypt only
296 */ 295 */
@@ -349,11 +348,9 @@ generate_keyset ()
349 348
350 // generate a g 349 // generate a g
351 gcry_mpi_mul (my_nsquare, my_n, my_n); 350 gcry_mpi_mul (my_nsquare, my_n, my_n);
352 do 351 do {
353 {
354 // find a matching g 352 // find a matching g
355 do 353 do {
356 {
357 gcry_mpi_randomize (my_g, KEYBITS * 2, GCRY_WEAK_RANDOM); 354 gcry_mpi_randomize (my_g, KEYBITS * 2, GCRY_WEAK_RANDOM);
358 // g must be smaller than n^2 355 // g must be smaller than n^2
359 if (0 >= gcry_mpi_cmp (my_g, my_nsquare)) 356 if (0 >= gcry_mpi_cmp (my_g, my_nsquare))
@@ -407,7 +404,6 @@ generate_keyset ()
407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _ ("Generated key set with key length %d bits.\n"), KEYBITS); 404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _ ("Generated key set with key length %d bits.\n"), KEYBITS);
408} 405}
409 406
410
411/** 407/**
412 * If target != size, move target bytes to the 408 * If target != size, move target bytes to the
413 * end of the size-sized buffer and zero out the 409 * end of the size-sized buffer and zero out the
@@ -420,14 +416,12 @@ generate_keyset ()
420static void 416static void
421adjust (unsigned char *buf, size_t size, size_t target) 417adjust (unsigned char *buf, size_t size, size_t target)
422{ 418{
423 if (size < target) 419 if (size < target) {
424 {
425 memmove (&buf[target - size], buf, size); 420 memmove (&buf[target - size], buf, size);
426 memset (buf, 0, target - size); 421 memset (buf, 0, target - size);
427 } 422 }
428} 423}
429 424
430
431/** 425/**
432 * encrypts an element using the paillier crypto system 426 * encrypts an element using the paillier crypto system
433 * 427 *
@@ -444,8 +438,7 @@ encrypt_element (gcry_mpi_t c, gcry_mpi_t m, gcry_mpi_t g, gcry_mpi_t n, gcry_mp
444 438
445 GNUNET_assert (tmp = gcry_mpi_new (0)); 439 GNUNET_assert (tmp = gcry_mpi_new (0));
446 440
447 while (0 >= gcry_mpi_cmp_ui (tmp, 1)) 441 while (0 >= gcry_mpi_cmp_ui (tmp, 1)) {
448 {
449 gcry_mpi_randomize (tmp, KEYBITS / 3, GCRY_WEAK_RANDOM); 442 gcry_mpi_randomize (tmp, KEYBITS / 3, GCRY_WEAK_RANDOM);
450 // r must be 1 < r < n 443 // r must be 1 < r < n
451 } 444 }
@@ -457,7 +450,6 @@ encrypt_element (gcry_mpi_t c, gcry_mpi_t m, gcry_mpi_t g, gcry_mpi_t n, gcry_mp
457 gcry_mpi_release (tmp); 450 gcry_mpi_release (tmp);
458} 451}
459 452
460
461/** 453/**
462 * decrypts an element using the paillier crypto system 454 * decrypts an element using the paillier crypto system
463 * 455 *
@@ -477,7 +469,6 @@ decrypt_element (gcry_mpi_t m, gcry_mpi_t c, gcry_mpi_t mu, gcry_mpi_t lambda, g
477 gcry_mpi_mulm (m, m, mu, n); 469 gcry_mpi_mulm (m, m, mu, n);
478} 470}
479 471
480
481/** 472/**
482 * computes the square sum over a vector of a given length. 473 * computes the square sum over a vector of a given length.
483 * 474 *
@@ -496,8 +487,7 @@ compute_square_sum (gcry_mpi_t * vector, uint32_t length)
496 GNUNET_assert (elem = gcry_mpi_new (0)); 487 GNUNET_assert (elem = gcry_mpi_new (0));
497 488
498 // calculare E(sum (ai ^ 2), publickey) 489 // calculare E(sum (ai ^ 2), publickey)
499 for (i = 0; i < length; i++) 490 for (i = 0; i < length; i++) {
500 {
501 gcry_mpi_mul (elem, vector[i], vector[i]); 491 gcry_mpi_mul (elem, vector[i], vector[i]);
502 gcry_mpi_add (sum, sum, elem); 492 gcry_mpi_add (sum, sum, elem);
503 } 493 }
@@ -509,10 +499,10 @@ compute_square_sum (gcry_mpi_t * vector, uint32_t length)
509 499
510static void 500static void
511prepare_service_request_multipart (void *cls, 501prepare_service_request_multipart (void *cls,
512 const struct GNUNET_SCHEDULER_TaskContext *tc); 502 const struct GNUNET_SCHEDULER_TaskContext *tc);
513static void 503static void
514prepare_service_response_multipart (void *cls, 504prepare_service_response_multipart (void *cls,
515 const struct GNUNET_SCHEDULER_TaskContext *tc); 505 const struct GNUNET_SCHEDULER_TaskContext *tc);
516 506
517/** 507/**
518 * Primitive callback for copying over a message, as they 508 * Primitive callback for copying over a message, as they
@@ -532,8 +522,7 @@ do_send_message (void *cls, size_t size, void *buf)
532 522
533 GNUNET_assert (buf); 523 GNUNET_assert (buf);
534 524
535 if (ntohs (session->msg->size) == size) 525 if (ntohs (session->msg->size) == size) {
536 {
537 memcpy (buf, session->msg, size); 526 memcpy (buf, session->msg, size);
538 written = size; 527 written = size;
539 } 528 }
@@ -550,7 +539,7 @@ do_send_message (void *cls, size_t size, void *buf)
550 session->service_transmit_handle = NULL; 539 session->service_transmit_handle = NULL;
551 // reset flags for sending 540 // reset flags for sending
552 if ((session->state != WAITING_FOR_MULTIPART_TRANSMISSION) && (session->used_element_count != session->transferred_element_count)) 541 if ((session->state != WAITING_FOR_MULTIPART_TRANSMISSION) && (session->used_element_count != session->transferred_element_count))
553 prepare_service_request_multipart(session, NULL); 542 prepare_service_request_multipart (session, NULL);
554 //TODO we have sent a message and now need to trigger trigger the next multipart message sending 543 //TODO we have sent a message and now need to trigger trigger the next multipart message sending
555 break; 544 break;
556 case GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_TO_ALICE: 545 case GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_TO_ALICE:
@@ -558,7 +547,7 @@ do_send_message (void *cls, size_t size, void *buf)
558 //else 547 //else
559 session->service_transmit_handle = NULL; 548 session->service_transmit_handle = NULL;
560 if ((session->state != WAITING_FOR_MULTIPART_TRANSMISSION) && (session->used_element_count != session->transferred_element_count)) 549 if ((session->state != WAITING_FOR_MULTIPART_TRANSMISSION) && (session->used_element_count != session->transferred_element_count))
561 prepare_service_response_multipart(session, NULL); 550 prepare_service_response_multipart (session, NULL);
562 break; 551 break;
563 default: 552 default:
564 session->service_transmit_handle = NULL; 553 session->service_transmit_handle = NULL;
@@ -573,7 +562,6 @@ do_send_message (void *cls, size_t size, void *buf)
573 return written; 562 return written;
574} 563}
575 564
576
577/** 565/**
578 * initializes a new vector with fresh MPI values (=0) of a given length 566 * initializes a new vector with fresh MPI values (=0) of a given length
579 * 567 *
@@ -591,7 +579,6 @@ initialize_mpi_vector (uint32_t length)
591 return output; 579 return output;
592} 580}
593 581
594
595/** 582/**
596 * permutes an MPI vector according to the given permutation vector 583 * permutes an MPI vector according to the given permutation vector
597 * 584 *
@@ -620,7 +607,6 @@ permute_vector (gcry_mpi_t * vector,
620 return vector; 607 return vector;
621} 608}
622 609
623
624/** 610/**
625 * Populate a vector with random integer values and convert them to 611 * Populate a vector with random integer values and convert them to
626 * 612 *
@@ -635,8 +621,7 @@ generate_random_vector (uint32_t length)
635 uint32_t i; 621 uint32_t i;
636 622
637 random_vector = initialize_mpi_vector (length); 623 random_vector = initialize_mpi_vector (length);
638 for (i = 0; i < length; i++) 624 for (i = 0; i < length; i++) {
639 {
640 value = (int32_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX); 625 value = (int32_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
641 626
642 // long to gcry_mpi_t 627 // long to gcry_mpi_t
@@ -651,7 +636,6 @@ generate_random_vector (uint32_t length)
651 return random_vector; 636 return random_vector;
652} 637}
653 638
654
655/** 639/**
656 * Finds a not terminated client/service session in the 640 * Finds a not terminated client/service session in the
657 * given DLL based on session key, element count and state. 641 * given DLL based on session key, element count and state.
@@ -670,15 +654,12 @@ find_matching_session (struct ServiceSession * tail,
670{ 654{
671 struct ServiceSession * curr; 655 struct ServiceSession * curr;
672 656
673 for (curr = tail; NULL != curr; curr = curr->prev) 657 for (curr = tail; NULL != curr; curr = curr->prev) {
674 {
675 // if the key matches, and the element_count is same 658 // if the key matches, and the element_count is same
676 if ((!memcmp (&curr->key, key, sizeof (struct GNUNET_HashCode))) 659 if ((!memcmp (&curr->key, key, sizeof (struct GNUNET_HashCode)))
677 && (curr->element_count == element_count)) 660 && (curr->element_count == element_count)) {
678 {
679 // if incoming state is NULL OR is same as state of the queued request 661 // if incoming state is NULL OR is same as state of the queued request
680 if ((NULL == state) || (curr->state == *state)) 662 if ((NULL == state) || (curr->state == *state)) {
681 {
682 // if peerid is NULL OR same as the peer Id in the queued request 663 // if peerid is NULL OR same as the peer Id in the queued request
683 if ((NULL == peerid) 664 if ((NULL == peerid)
684 || (!memcmp (&curr->peer, peerid, sizeof (struct GNUNET_PeerIdentity)))) 665 || (!memcmp (&curr->peer, peerid, sizeof (struct GNUNET_PeerIdentity))))
@@ -691,14 +672,12 @@ find_matching_session (struct ServiceSession * tail,
691 return NULL; 672 return NULL;
692} 673}
693 674
694
695static void 675static void
696free_session (struct ServiceSession * session) 676free_session (struct ServiceSession * session)
697{ 677{
698 unsigned int i; 678 unsigned int i;
699 679
700 if (session->a) 680 if (session->a) {
701 {
702 for (i = 0; i < session->used_element_count; i++) 681 for (i = 0; i < session->used_element_count; i++)
703 gcry_mpi_release (session->a[i]); 682 gcry_mpi_release (session->a[i]);
704 683
@@ -717,7 +696,6 @@ free_session (struct ServiceSession * session)
717// Event and Message Handlers 696// Event and Message Handlers
718/////////////////////////////////////////////////////////////////////////////// 697///////////////////////////////////////////////////////////////////////////////
719 698
720
721/** 699/**
722 * A client disconnected. 700 * A client disconnected.
723 * 701 *
@@ -743,33 +721,28 @@ handle_client_disconnect (void *cls,
743 _ ("Client (%p) disconnected from us.\n"), client); 721 _ ("Client (%p) disconnected from us.\n"), client);
744 GNUNET_CONTAINER_DLL_remove (from_client_head, from_client_tail, session); 722 GNUNET_CONTAINER_DLL_remove (from_client_head, from_client_tail, session);
745 723
746 if (!(session->role == BOB && session->state == FINALIZED)) 724 if (!(session->role == BOB && session->state == FINALIZED)) {
747 {
748 //we MUST terminate any client message underway 725 //we MUST terminate any client message underway
749 if (session->service_transmit_handle && session->tunnel) 726 if (session->service_transmit_handle && session->tunnel)
750 GNUNET_MESH_notify_transmit_ready_cancel (session->service_transmit_handle); 727 GNUNET_MESH_notify_transmit_ready_cancel (session->service_transmit_handle);
751 if (session->tunnel && session->state == WAITING_FOR_SERVICE_RESPONSE) 728 if (session->tunnel && session->state == WAITING_FOR_SERVICE_RESPONSE)
752 GNUNET_MESH_tunnel_destroy (session->tunnel); 729 GNUNET_MESH_tunnel_destroy (session->tunnel);
753 } 730 }
754 if (GNUNET_SCHEDULER_NO_TASK != session->client_notification_task) 731 if (GNUNET_SCHEDULER_NO_TASK != session->client_notification_task) {
755 {
756 GNUNET_SCHEDULER_cancel (session->client_notification_task); 732 GNUNET_SCHEDULER_cancel (session->client_notification_task);
757 session->client_notification_task = GNUNET_SCHEDULER_NO_TASK; 733 session->client_notification_task = GNUNET_SCHEDULER_NO_TASK;
758 } 734 }
759 if (GNUNET_SCHEDULER_NO_TASK != session->service_request_task) 735 if (GNUNET_SCHEDULER_NO_TASK != session->service_request_task) {
760 {
761 GNUNET_SCHEDULER_cancel (session->service_request_task); 736 GNUNET_SCHEDULER_cancel (session->service_request_task);
762 session->service_request_task = GNUNET_SCHEDULER_NO_TASK; 737 session->service_request_task = GNUNET_SCHEDULER_NO_TASK;
763 } 738 }
764 if (NULL != session->client_transmit_handle) 739 if (NULL != session->client_transmit_handle) {
765 {
766 GNUNET_SERVER_notify_transmit_ready_cancel (session->client_transmit_handle); 740 GNUNET_SERVER_notify_transmit_ready_cancel (session->client_transmit_handle);
767 session->client_transmit_handle = NULL; 741 session->client_transmit_handle = NULL;
768 } 742 }
769 free_session (session); 743 free_session (session);
770} 744}
771 745
772
773/** 746/**
774 * Notify the client that the session has succeeded or failed completely. 747 * Notify the client that the session has succeeded or failed completely.
775 * This message gets sent to 748 * This message gets sent to
@@ -809,8 +782,7 @@ prepare_client_end_notification (void * cls,
809 session); 782 session);
810 783
811 // if we could not even queue our request, something is wrong 784 // if we could not even queue our request, something is wrong
812 if (NULL == session->client_transmit_handle) 785 if (NULL == session->client_transmit_handle) {
813 {
814 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Could not send message to client (%p)!\n"), session->client); 786 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Could not send message to client (%p)!\n"), session->client);
815 // usually gets freed by do_send_message 787 // usually gets freed by do_send_message
816 session->msg = NULL; 788 session->msg = NULL;
@@ -823,7 +795,7 @@ prepare_client_end_notification (void * cls,
823 795
824static void 796static void
825prepare_service_response_multipart (void *cls, 797prepare_service_response_multipart (void *cls,
826 const struct GNUNET_SCHEDULER_TaskContext *tc) 798 const struct GNUNET_SCHEDULER_TaskContext *tc)
827{ 799{
828 struct ServiceSession * session = cls; 800 struct ServiceSession * session = cls;
829 unsigned char * current; 801 unsigned char * current;
@@ -833,25 +805,24 @@ prepare_service_response_multipart (void *cls,
833 uint32_t msg_length; 805 uint32_t msg_length;
834 uint32_t todo_count; 806 uint32_t todo_count;
835 size_t element_length = 0; // initialized by gcry_mpi_print, but the compiler doesn't know that 807 size_t element_length = 0; // initialized by gcry_mpi_print, but the compiler doesn't know that
836 808
837 msg_length = sizeof (struct GNUNET_SCALARPRODUCT_multipart_message); 809 msg_length = sizeof (struct GNUNET_SCALARPRODUCT_multipart_message);
838 todo_count = session->used_element_count - session->transferred_element_count; 810 todo_count = session->used_element_count - session->transferred_element_count;
839 811
840 if (todo_count > MULTIPART_ELEMENT_CAPACITY/2) 812 if (todo_count > MULTIPART_ELEMENT_CAPACITY / 2)
841 // send the currently possible maximum chunk, we always transfer both permutations 813 // send the currently possible maximum chunk, we always transfer both permutations
842 todo_count = MULTIPART_ELEMENT_CAPACITY/2; 814 todo_count = MULTIPART_ELEMENT_CAPACITY / 2;
843 815
844 msg_length += todo_count * PAILLIER_ELEMENT_LENGTH * 2; 816 msg_length += todo_count * PAILLIER_ELEMENT_LENGTH * 2;
845 msg = GNUNET_malloc (msg_length); 817 msg = GNUNET_malloc (msg_length);
846 msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_TO_BOB_MULTIPART); 818 msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_TO_BOB_MULTIPART);
847 msg->header.size = htons (msg_length); 819 msg->header.size = htons (msg_length);
848 msg->multipart_element_count = htonl (todo_count); 820 msg->multipart_element_count = htonl (todo_count);
849 821
850 element_exported = GNUNET_malloc (PAILLIER_ELEMENT_LENGTH); 822 element_exported = GNUNET_malloc (PAILLIER_ELEMENT_LENGTH);
851 current = (unsigned char *) &msg[1]; 823 current = (unsigned char *) &msg[1];
852 // convert k[][] 824 // convert k[][]
853 for (i = session->transferred_element_count; i < session->transferred_element_count + todo_count; i++) 825 for (i = session->transferred_element_count; i < session->transferred_element_count + todo_count; i++) {
854 {
855 //k[i][p] 826 //k[i][p]
856 memset (element_exported, 0, PAILLIER_ELEMENT_LENGTH); 827 memset (element_exported, 0, PAILLIER_ELEMENT_LENGTH);
857 GNUNET_assert (0 == gcry_mpi_print (GCRYMPI_FMT_USG, 828 GNUNET_assert (0 == gcry_mpi_print (GCRYMPI_FMT_USG,
@@ -872,12 +843,11 @@ prepare_service_response_multipart (void *cls,
872 current += PAILLIER_ELEMENT_LENGTH; 843 current += PAILLIER_ELEMENT_LENGTH;
873 } 844 }
874 GNUNET_free (element_exported); 845 GNUNET_free (element_exported);
875 for (i = session->transferred_element_count; i < session->transferred_element_count; i++) 846 for (i = session->transferred_element_count; i < session->transferred_element_count; i++) {
876 {
877 gcry_mpi_release (session->r_prime[i]); 847 gcry_mpi_release (session->r_prime[i]);
878 gcry_mpi_release (session->r[i]); 848 gcry_mpi_release (session->r[i]);
879 } 849 }
880 session->transferred_element_count+=todo_count; 850 session->transferred_element_count += todo_count;
881 session->msg = (struct GNUNET_MessageHeader *) msg; 851 session->msg = (struct GNUNET_MessageHeader *) msg;
882 session->service_transmit_handle = 852 session->service_transmit_handle =
883 GNUNET_MESH_notify_transmit_ready (session->tunnel, 853 GNUNET_MESH_notify_transmit_ready (session->tunnel,
@@ -887,8 +857,7 @@ prepare_service_response_multipart (void *cls,
887 &do_send_message, 857 &do_send_message,
888 session); 858 session);
889 //disconnect our client 859 //disconnect our client
890 if (NULL == session->service_transmit_handle) 860 if (NULL == session->service_transmit_handle) {
891 {
892 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Could not send service-response message via mesh!)\n")); 861 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Could not send service-response message via mesh!)\n"));
893 session->state = FINALIZED; 862 session->state = FINALIZED;
894 863
@@ -935,8 +904,8 @@ prepare_service_response (gcry_mpi_t s,
935 msg_length = sizeof (struct GNUNET_SCALARPRODUCT_service_response) 904 msg_length = sizeof (struct GNUNET_SCALARPRODUCT_service_response)
936 + 2 * PAILLIER_ELEMENT_LENGTH; // s, stick 905 + 2 * PAILLIER_ELEMENT_LENGTH; // s, stick
937 906
938 if (GNUNET_SERVER_MAX_MESSAGE_SIZE > msg_length + 2 * session->used_element_count * PAILLIER_ELEMENT_LENGTH){ //kp, kq 907 if (GNUNET_SERVER_MAX_MESSAGE_SIZE > msg_length + 2 * session->used_element_count * PAILLIER_ELEMENT_LENGTH) { //kp, kq
939 msg_length += + 2 * session->used_element_count * PAILLIER_ELEMENT_LENGTH; 908 msg_length += +2 * session->used_element_count * PAILLIER_ELEMENT_LENGTH;
940 session->transferred_element_count = session->used_element_count; 909 session->transferred_element_count = session->used_element_count;
941 } 910 }
942 else { 911 else {
@@ -978,8 +947,7 @@ prepare_service_response (gcry_mpi_t s,
978 current += PAILLIER_ELEMENT_LENGTH; 947 current += PAILLIER_ELEMENT_LENGTH;
979 948
980 // convert k[][] 949 // convert k[][]
981 for (i = 0; i < session->transferred_element_count; i++) 950 for (i = 0; i < session->transferred_element_count; i++) {
982 {
983 //k[i][p] 951 //k[i][p]
984 memset (element_exported, 0, PAILLIER_ELEMENT_LENGTH); 952 memset (element_exported, 0, PAILLIER_ELEMENT_LENGTH);
985 GNUNET_assert (0 == gcry_mpi_print (GCRYMPI_FMT_USG, 953 GNUNET_assert (0 == gcry_mpi_print (GCRYMPI_FMT_USG,
@@ -1001,8 +969,7 @@ prepare_service_response (gcry_mpi_t s,
1001 } 969 }
1002 970
1003 GNUNET_free (element_exported); 971 GNUNET_free (element_exported);
1004 for (i = 0; i < session->transferred_element_count; i++) 972 for (i = 0; i < session->transferred_element_count; i++) {
1005 {
1006 gcry_mpi_release (session->r_prime[i]); 973 gcry_mpi_release (session->r_prime[i]);
1007 gcry_mpi_release (session->r[i]); 974 gcry_mpi_release (session->r[i]);
1008 } 975 }
@@ -1018,8 +985,7 @@ prepare_service_response (gcry_mpi_t s,
1018 &do_send_message, 985 &do_send_message,
1019 session); 986 session);
1020 //disconnect our client 987 //disconnect our client
1021 if (NULL == session->service_transmit_handle) 988 if (NULL == session->service_transmit_handle) {
1022 {
1023 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Could not send service-response message via mesh!)\n")); 989 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Could not send service-response message via mesh!)\n"));
1024 session->state = FINALIZED; 990 session->state = FINALIZED;
1025 991
@@ -1038,7 +1004,6 @@ prepare_service_response (gcry_mpi_t s,
1038 return GNUNET_OK; 1004 return GNUNET_OK;
1039} 1005}
1040 1006
1041
1042/** 1007/**
1043 * executed by bob: 1008 * executed by bob:
1044 * compute the values 1009 * compute the values
@@ -1089,19 +1054,15 @@ compute_service_response (struct ServiceSession * request,
1089 rand_pi_prime = GNUNET_malloc (sizeof (gcry_mpi_t) * count); 1054 rand_pi_prime = GNUNET_malloc (sizeof (gcry_mpi_t) * count);
1090 1055
1091 // convert responder session to from long to mpi 1056 // convert responder session to from long to mpi
1092 for (i = 0, j = 0; i < response->element_count && j < count; i++) 1057 for (i = 0, j = 0; i < response->element_count && j < count; i++) {
1093 { 1058 if (request->mask[i / 8] & (1 << (i % 8))) {
1094 if (request->mask[i / 8] & (1 << (i % 8)))
1095 {
1096 value = response->vector[i] >= 0 ? response->vector[i] : -response->vector[i]; 1059 value = response->vector[i] >= 0 ? response->vector[i] : -response->vector[i];
1097 // long to gcry_mpi_t 1060 // long to gcry_mpi_t
1098 if (0 > response->vector[i]) 1061 if (0 > response->vector[i]) {
1099 {
1100 b[j] = gcry_mpi_new (0); 1062 b[j] = gcry_mpi_new (0);
1101 gcry_mpi_sub_ui (b[j], b[j], value); 1063 gcry_mpi_sub_ui (b[j], b[j], value);
1102 } 1064 }
1103 else 1065 else {
1104 {
1105 b[j] = gcry_mpi_set_ui (NULL, value); 1066 b[j] = gcry_mpi_set_ui (NULL, value);
1106 } 1067 }
1107 j++; 1068 j++;
@@ -1111,16 +1072,14 @@ compute_service_response (struct ServiceSession * request,
1111 response->vector = NULL; 1072 response->vector = NULL;
1112 1073
1113 tmp_exp = gcry_sexp_find_token (request->remote_pubkey, "n", 0); 1074 tmp_exp = gcry_sexp_find_token (request->remote_pubkey, "n", 0);
1114 if (!tmp_exp) 1075 if (!tmp_exp) {
1115 {
1116 GNUNET_break_op (0); 1076 GNUNET_break_op (0);
1117 gcry_sexp_release (request->remote_pubkey); 1077 gcry_sexp_release (request->remote_pubkey);
1118 request->remote_pubkey = NULL; 1078 request->remote_pubkey = NULL;
1119 goto except; 1079 goto except;
1120 } 1080 }
1121 remote_n = gcry_sexp_nth_mpi (tmp_exp, 1, GCRYMPI_FMT_USG); 1081 remote_n = gcry_sexp_nth_mpi (tmp_exp, 1, GCRYMPI_FMT_USG);
1122 if (!remote_n) 1082 if (!remote_n) {
1123 {
1124 GNUNET_break (0); 1083 GNUNET_break (0);
1125 gcry_sexp_release (tmp_exp); 1084 gcry_sexp_release (tmp_exp);
1126 goto except; 1085 goto except;
@@ -1131,15 +1090,13 @@ compute_service_response (struct ServiceSession * request,
1131 tmp_exp = gcry_sexp_find_token (request->remote_pubkey, "g", 0); 1090 tmp_exp = gcry_sexp_find_token (request->remote_pubkey, "g", 0);
1132 gcry_sexp_release (request->remote_pubkey); 1091 gcry_sexp_release (request->remote_pubkey);
1133 request->remote_pubkey = NULL; 1092 request->remote_pubkey = NULL;
1134 if (!tmp_exp) 1093 if (!tmp_exp) {
1135 {
1136 GNUNET_break_op (0); 1094 GNUNET_break_op (0);
1137 gcry_mpi_release (remote_n); 1095 gcry_mpi_release (remote_n);
1138 goto except; 1096 goto except;
1139 } 1097 }
1140 remote_g = gcry_sexp_nth_mpi (tmp_exp, 1, GCRYMPI_FMT_USG); 1098 remote_g = gcry_sexp_nth_mpi (tmp_exp, 1, GCRYMPI_FMT_USG);
1141 if (!remote_g) 1099 if (!remote_g) {
1142 {
1143 GNUNET_break (0); 1100 GNUNET_break (0);
1144 gcry_mpi_release (remote_n); 1101 gcry_mpi_release (remote_n);
1145 gcry_sexp_release (tmp_exp); 1102 gcry_sexp_release (tmp_exp);
@@ -1175,8 +1132,7 @@ compute_service_response (struct ServiceSession * request,
1175 // vectors, which get rid of all the lookups in p/q. 1132 // vectors, which get rid of all the lookups in p/q.
1176 // however, ap/aq are not absolutely necessary but are just abstraction 1133 // however, ap/aq are not absolutely necessary but are just abstraction
1177 // Calculate Kp = E(S + a_pi) (+) E(S - r_pi - b_pi) 1134 // Calculate Kp = E(S + a_pi) (+) E(S - r_pi - b_pi)
1178 for (i = 0; i < count; i++) 1135 for (i = 0; i < count; i++) {
1179 {
1180 // E(S - r_pi - b_pi) 1136 // E(S - r_pi - b_pi)
1181 gcry_mpi_sub (r[i], my_offset, rand_pi[i]); 1137 gcry_mpi_sub (r[i], my_offset, rand_pi[i]);
1182 gcry_mpi_sub (r[i], r[i], b_pi[i]); 1138 gcry_mpi_sub (r[i], r[i], b_pi[i]);
@@ -1190,8 +1146,7 @@ compute_service_response (struct ServiceSession * request,
1190 GNUNET_free (rand_pi); 1146 GNUNET_free (rand_pi);
1191 1147
1192 // Calculate Kq = E(S + a_qi) (+) E(S - r_qi) 1148 // Calculate Kq = E(S + a_qi) (+) E(S - r_qi)
1193 for (i = 0; i < count; i++) 1149 for (i = 0; i < count; i++) {
1194 {
1195 // E(S - r_qi) 1150 // E(S - r_qi)
1196 gcry_mpi_sub (r_prime[i], my_offset, rand_pi_prime[i]); 1151 gcry_mpi_sub (r_prime[i], my_offset, rand_pi_prime[i]);
1197 encrypt_element (r_prime[i], r_prime[i], remote_g, remote_n, remote_nsquare); 1152 encrypt_element (r_prime[i], r_prime[i], remote_g, remote_n, remote_nsquare);
@@ -1211,8 +1166,7 @@ compute_service_response (struct ServiceSession * request,
1211 encrypt_element (s_prime, s_prime, remote_g, remote_n, remote_nsquare); 1166 encrypt_element (s_prime, s_prime, remote_g, remote_n, remote_nsquare);
1212 1167
1213 // Calculate S = E(SUM( (r_i + b_i)^2 )) 1168 // Calculate S = E(SUM( (r_i + b_i)^2 ))
1214 for (i = 0; i < count; i++) 1169 for (i = 0; i < count; i++) {
1215 {
1216 gcry_mpi_add (rand[i], rand[i], b[i]); 1170 gcry_mpi_add (rand[i], rand[i], b[i]);
1217 } 1171 }
1218 s = compute_square_sum (rand, count); 1172 s = compute_square_sum (rand, count);
@@ -1234,8 +1188,7 @@ compute_service_response (struct ServiceSession * request,
1234 ret = GNUNET_OK; 1188 ret = GNUNET_OK;
1235 1189
1236except: 1190except:
1237 for (i = 0; i < count; i++) 1191 for (i = 0; i < count; i++) {
1238 {
1239 gcry_mpi_release (b[i]); 1192 gcry_mpi_release (b[i]);
1240 gcry_mpi_release (request->a[i]); 1193 gcry_mpi_release (request->a[i]);
1241 } 1194 }
@@ -1249,7 +1202,7 @@ except:
1249 1202
1250static void 1203static void
1251prepare_service_request_multipart (void *cls, 1204prepare_service_request_multipart (void *cls,
1252 const struct GNUNET_SCHEDULER_TaskContext *tc) 1205 const struct GNUNET_SCHEDULER_TaskContext *tc)
1253{ 1206{
1254 struct ServiceSession * session = cls; 1207 struct ServiceSession * session = cls;
1255 unsigned char * current; 1208 unsigned char * current;
@@ -1262,33 +1215,31 @@ prepare_service_request_multipart (void *cls,
1262 size_t element_length = 0; // initialized by gcry_mpi_print, but the compiler doesn't know that 1215 size_t element_length = 0; // initialized by gcry_mpi_print, but the compiler doesn't know that
1263 gcry_mpi_t a; 1216 gcry_mpi_t a;
1264 uint32_t value; 1217 uint32_t value;
1265 1218
1266 msg_length = sizeof (struct GNUNET_SCALARPRODUCT_multipart_message); 1219 msg_length = sizeof (struct GNUNET_SCALARPRODUCT_multipart_message);
1267 todo_count = session->used_element_count - session->transferred_element_count; 1220 todo_count = session->used_element_count - session->transferred_element_count;
1268 1221
1269 if (todo_count > MULTIPART_ELEMENT_CAPACITY) 1222 if (todo_count > MULTIPART_ELEMENT_CAPACITY)
1270 // send the currently possible maximum chunk 1223 // send the currently possible maximum chunk
1271 todo_count = MULTIPART_ELEMENT_CAPACITY; 1224 todo_count = MULTIPART_ELEMENT_CAPACITY;
1272 1225
1273 msg_length += todo_count * PAILLIER_ELEMENT_LENGTH; 1226 msg_length += todo_count * PAILLIER_ELEMENT_LENGTH;
1274 msg = GNUNET_malloc (msg_length); 1227 msg = GNUNET_malloc (msg_length);
1275 msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_TO_BOB_MULTIPART); 1228 msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_TO_BOB_MULTIPART);
1276 msg->header.size = htons (msg_length); 1229 msg->header.size = htons (msg_length);
1277 msg->multipart_element_count = htonl (todo_count); 1230 msg->multipart_element_count = htonl (todo_count);
1278 1231
1279 element_exported = GNUNET_malloc (PAILLIER_ELEMENT_LENGTH); 1232 element_exported = GNUNET_malloc (PAILLIER_ELEMENT_LENGTH);
1280 a = gcry_mpi_new (KEYBITS * 2); 1233 a = gcry_mpi_new (KEYBITS * 2);
1281 current = (unsigned char *) &msg[1]; 1234 current = (unsigned char *) &msg[1];
1282 // encrypt our vector and generate string representations 1235 // encrypt our vector and generate string representations
1283 for (i = session->last_processed_element, j = 0; i < session->element_count; i++) 1236 for (i = session->last_processed_element, j = 0; i < session->element_count; i++) {
1284 {
1285 // is this a used element? 1237 // is this a used element?
1286 if (session->mask[i / 8] & 1 << (i % 8)) 1238 if (session->mask[i / 8] & 1 << (i % 8)) {
1287 {
1288 if (todo_count <= j) 1239 if (todo_count <= j)
1289 break; //reached end of this message, can't include more 1240 break; //reached end of this message, can't include more
1290 1241
1291 memset(element_exported, 0, PAILLIER_ELEMENT_LENGTH); 1242 memset (element_exported, 0, PAILLIER_ELEMENT_LENGTH);
1292 value = session->vector[i] >= 0 ? session->vector[i] : -session->vector[i]; 1243 value = session->vector[i] >= 0 ? session->vector[i] : -session->vector[i];
1293 1244
1294 a = gcry_mpi_set_ui (a, 0); 1245 a = gcry_mpi_set_ui (a, 0);
@@ -1318,9 +1269,9 @@ prepare_service_request_multipart (void *cls,
1318 } 1269 }
1319 } 1270 }
1320 gcry_mpi_release (a); 1271 gcry_mpi_release (a);
1321 GNUNET_free(element_exported); 1272 GNUNET_free (element_exported);
1322 session->transferred_element_count+=todo_count; 1273 session->transferred_element_count += todo_count;
1323 1274
1324 session->msg = (struct GNUNET_MessageHeader *) msg; 1275 session->msg = (struct GNUNET_MessageHeader *) msg;
1325 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Transmitting service request.\n")); 1276 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Transmitting service request.\n"));
1326 1277
@@ -1330,8 +1281,7 @@ prepare_service_request_multipart (void *cls,
1330 msg_length, 1281 msg_length,
1331 &do_send_message, 1282 &do_send_message,
1332 session); 1283 session);
1333 if (!session->service_transmit_handle) 1284 if (!session->service_transmit_handle) {
1334 {
1335 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Could not send service-request multipart message to tunnel!\n")); 1285 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Could not send service-request multipart message to tunnel!\n"));
1336 GNUNET_free (msg); 1286 GNUNET_free (msg);
1337 session->msg = NULL; 1287 session->msg = NULL;
@@ -1340,7 +1290,7 @@ prepare_service_request_multipart (void *cls,
1340 session); 1290 session);
1341 return; 1291 return;
1342 } 1292 }
1343 if (session->transferred_element_count != session->used_element_count){ 1293 if (session->transferred_element_count != session->used_element_count) {
1344 session->last_processed_element = i; 1294 session->last_processed_element = i;
1345 } 1295 }
1346 else 1296 else
@@ -1376,10 +1326,10 @@ prepare_service_request (void *cls,
1376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _ ("Successfully created new tunnel to peer (%s)!\n"), GNUNET_i2s (&session->peer)); 1326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _ ("Successfully created new tunnel to peer (%s)!\n"), GNUNET_i2s (&session->peer));
1377 1327
1378 msg_length = sizeof (struct GNUNET_SCALARPRODUCT_service_request) 1328 msg_length = sizeof (struct GNUNET_SCALARPRODUCT_service_request)
1379 + session->mask_length 1329 +session->mask_length
1380 + my_pubkey_external_length; 1330 + my_pubkey_external_length;
1381 1331
1382 if (GNUNET_SERVER_MAX_MESSAGE_SIZE > msg_length + session->used_element_count * PAILLIER_ELEMENT_LENGTH){ 1332 if (GNUNET_SERVER_MAX_MESSAGE_SIZE > msg_length + session->used_element_count * PAILLIER_ELEMENT_LENGTH) {
1383 msg_length += session->used_element_count * PAILLIER_ELEMENT_LENGTH; 1333 msg_length += session->used_element_count * PAILLIER_ELEMENT_LENGTH;
1384 session->transferred_element_count = session->used_element_count; 1334 session->transferred_element_count = session->used_element_count;
1385 } 1335 }
@@ -1390,7 +1340,7 @@ prepare_service_request (void *cls,
1390 1340
1391 msg = GNUNET_malloc (msg_length); 1341 msg = GNUNET_malloc (msg_length);
1392 msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_TO_BOB); 1342 msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_TO_BOB);
1393 msg->total_element_count = htonl(session->used_element_count); 1343 msg->total_element_count = htonl (session->used_element_count);
1394 msg->contained_element_count = htonl (session->transferred_element_count); 1344 msg->contained_element_count = htonl (session->transferred_element_count);
1395 memcpy (&msg->key, &session->key, sizeof (struct GNUNET_HashCode)); 1345 memcpy (&msg->key, &session->key, sizeof (struct GNUNET_HashCode));
1396 msg->mask_length = htonl (session->mask_length); 1346 msg->mask_length = htonl (session->mask_length);
@@ -1412,15 +1362,13 @@ prepare_service_request (void *cls,
1412 session->a = GNUNET_malloc (sizeof (gcry_mpi_t) * session->used_element_count); 1362 session->a = GNUNET_malloc (sizeof (gcry_mpi_t) * session->used_element_count);
1413 a = gcry_mpi_new (KEYBITS * 2); 1363 a = gcry_mpi_new (KEYBITS * 2);
1414 // encrypt our vector and generate string representations 1364 // encrypt our vector and generate string representations
1415 for (i = 0, j = 0; i < session->element_count; i++) 1365 for (i = 0, j = 0; i < session->element_count; i++) {
1416 {
1417 // if this is a used element... 1366 // if this is a used element...
1418 if (session->mask[i / 8] & 1 << (i % 8)) 1367 if (session->mask[i / 8] & 1 << (i % 8)) {
1419 {
1420 if (session->transferred_element_count <= j) 1368 if (session->transferred_element_count <= j)
1421 break; //reached end of this message, can't include more 1369 break; //reached end of this message, can't include more
1422 1370
1423 memset(element_exported, 0, PAILLIER_ELEMENT_LENGTH); 1371 memset (element_exported, 0, PAILLIER_ELEMENT_LENGTH);
1424 value = session->vector[i] >= 0 ? session->vector[i] : -session->vector[i]; 1372 value = session->vector[i] >= 0 ? session->vector[i] : -session->vector[i];
1425 1373
1426 a = gcry_mpi_set_ui (a, 0); 1374 a = gcry_mpi_set_ui (a, 0);
@@ -1450,7 +1398,7 @@ prepare_service_request (void *cls,
1450 } 1398 }
1451 } 1399 }
1452 gcry_mpi_release (a); 1400 gcry_mpi_release (a);
1453 GNUNET_free(element_exported); 1401 GNUNET_free (element_exported);
1454 1402
1455 session->msg = (struct GNUNET_MessageHeader *) msg; 1403 session->msg = (struct GNUNET_MessageHeader *) msg;
1456 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Transmitting service request.\n")); 1404 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Transmitting service request.\n"));
@@ -1461,8 +1409,7 @@ prepare_service_request (void *cls,
1461 msg_length, 1409 msg_length,
1462 &do_send_message, 1410 &do_send_message,
1463 session); 1411 session);
1464 if (!session->service_transmit_handle) 1412 if (!session->service_transmit_handle) {
1465 {
1466 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Could not send message to tunnel!\n")); 1413 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Could not send message to tunnel!\n"));
1467 GNUNET_free (msg); 1414 GNUNET_free (msg);
1468 session->msg = NULL; 1415 session->msg = NULL;
@@ -1471,7 +1418,7 @@ prepare_service_request (void *cls,
1471 session); 1418 session);
1472 return; 1419 return;
1473 } 1420 }
1474 if (session->transferred_element_count != session->used_element_count){ 1421 if (session->transferred_element_count != session->used_element_count) {
1475 session->state = WAITING_FOR_MULTIPART_TRANSMISSION; 1422 session->state = WAITING_FOR_MULTIPART_TRANSMISSION;
1476 session->last_processed_element = i; 1423 session->last_processed_element = i;
1477 } 1424 }
@@ -1480,7 +1427,6 @@ prepare_service_request (void *cls,
1480 session->state = WAITING_FOR_SERVICE_RESPONSE; 1427 session->state = WAITING_FOR_SERVICE_RESPONSE;
1481} 1428}
1482 1429
1483
1484/** 1430/**
1485 * Handler for a client request message. 1431 * Handler for a client request message.
1486 * Can either be type A or B 1432 * Can either be type A or B
@@ -1506,21 +1452,18 @@ handle_client_request (void *cls,
1506 1452
1507 // only one concurrent session per client connection allowed, simplifies logics a lot... 1453 // only one concurrent session per client connection allowed, simplifies logics a lot...
1508 session = GNUNET_SERVER_client_get_user_context (client, struct ServiceSession); 1454 session = GNUNET_SERVER_client_get_user_context (client, struct ServiceSession);
1509 if ((NULL != session) && (session->state != FINALIZED)) 1455 if ((NULL != session) && (session->state != FINALIZED)) {
1510 {
1511 GNUNET_SERVER_receive_done (client, GNUNET_OK); 1456 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1512 return; 1457 return;
1513 } 1458 }
1514 else if (NULL != session) 1459 else if (NULL != session) {
1515 {
1516 // old session is already completed, clean it up 1460 // old session is already completed, clean it up
1517 GNUNET_CONTAINER_DLL_remove (from_client_head, from_client_tail, session); 1461 GNUNET_CONTAINER_DLL_remove (from_client_head, from_client_tail, session);
1518 free_session (session); 1462 free_session (session);
1519 } 1463 }
1520 1464
1521 //we need at least a peer and one message id to compare 1465 //we need at least a peer and one message id to compare
1522 if (sizeof (struct GNUNET_SCALARPRODUCT_client_request) > ntohs (msg->header.size)) 1466 if (sizeof (struct GNUNET_SCALARPRODUCT_client_request) > ntohs (msg->header.size)) {
1523 {
1524 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1467 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1525 _ ("Too short message received from client!\n")); 1468 _ ("Too short message received from client!\n"));
1526 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 1469 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
@@ -1533,8 +1476,7 @@ handle_client_request (void *cls,
1533 1476
1534 //sanity check: is the message as long as the message_count fields suggests? 1477 //sanity check: is the message as long as the message_count fields suggests?
1535 if ((ntohs (msg->header.size) != (sizeof (struct GNUNET_SCALARPRODUCT_client_request) +element_count * sizeof (int32_t) + mask_length)) 1478 if ((ntohs (msg->header.size) != (sizeof (struct GNUNET_SCALARPRODUCT_client_request) +element_count * sizeof (int32_t) + mask_length))
1536 || (0 == element_count)) 1479 || (0 == element_count)) {
1537 {
1538 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1480 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1539 _ ("Invalid message received from client, session information incorrect!\n")); 1481 _ ("Invalid message received from client, session information incorrect!\n"));
1540 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 1482 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
@@ -1545,8 +1487,7 @@ handle_client_request (void *cls,
1545 if (NULL != find_matching_session (from_client_tail, 1487 if (NULL != find_matching_session (from_client_tail,
1546 &msg->key, 1488 &msg->key,
1547 element_count, 1489 element_count,
1548 NULL, NULL)) 1490 NULL, NULL)) {
1549 {
1550 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1491 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1551 _ ("Duplicate session information received, cannot create new session with key `%s'\n"), 1492 _ ("Duplicate session information received, cannot create new session with key `%s'\n"),
1552 GNUNET_h2s (&msg->key)); 1493 GNUNET_h2s (&msg->key));
@@ -1566,8 +1507,7 @@ handle_client_request (void *cls,
1566 session->vector = GNUNET_malloc (sizeof (int32_t) * element_count); 1507 session->vector = GNUNET_malloc (sizeof (int32_t) * element_count);
1567 vector = (int32_t *) & msg[1]; 1508 vector = (int32_t *) & msg[1];
1568 1509
1569 if (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE == msg_type) 1510 if (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE == msg_type) {
1570 {
1571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1572 _ ("Got client-request-session with key %s, preparing tunnel to remote service.\n"), 1512 _ ("Got client-request-session with key %s, preparing tunnel to remote service.\n"),
1573 GNUNET_h2s (&session->key)); 1513 GNUNET_h2s (&session->key));
@@ -1579,8 +1519,7 @@ handle_client_request (void *cls,
1579 1519
1580 // copy over the elements 1520 // copy over the elements
1581 session->used_element_count = 0; 1521 session->used_element_count = 0;
1582 for (i = 0; i < element_count; i++) 1522 for (i = 0; i < element_count; i++) {
1583 {
1584 session->vector[i] = ntohl (vector[i]); 1523 session->vector[i] = ntohl (vector[i]);
1585 if (session->vector[i] == 0) 1524 if (session->vector[i] == 0)
1586 session->mask[i / 8] &= ~(1 << (i % 8)); 1525 session->mask[i / 8] &= ~(1 << (i % 8));
@@ -1588,8 +1527,7 @@ handle_client_request (void *cls,
1588 session->used_element_count++; 1527 session->used_element_count++;
1589 } 1528 }
1590 1529
1591 if (0 == session->used_element_count) 1530 if (0 == session->used_element_count) {
1592 {
1593 GNUNET_break_op (0); 1531 GNUNET_break_op (0);
1594 GNUNET_free (session->vector); 1532 GNUNET_free (session->vector);
1595 GNUNET_free (session); 1533 GNUNET_free (session);
@@ -1597,8 +1535,7 @@ handle_client_request (void *cls,
1597 return; 1535 return;
1598 } 1536 }
1599 //session with ourself makes no sense! 1537 //session with ourself makes no sense!
1600 if (!memcmp (&msg->peer, &me, sizeof (struct GNUNET_PeerIdentity))) 1538 if (!memcmp (&msg->peer, &me, sizeof (struct GNUNET_PeerIdentity))) {
1601 {
1602 GNUNET_break (0); 1539 GNUNET_break (0);
1603 GNUNET_free (session->vector); 1540 GNUNET_free (session->vector);
1604 GNUNET_free (session); 1541 GNUNET_free (session);
@@ -1616,8 +1553,7 @@ handle_client_request (void *cls,
1616 GNUNET_NO, 1553 GNUNET_NO,
1617 GNUNET_YES); 1554 GNUNET_YES);
1618 //prepare_service_request, tunnel_peer_disconnect_handler, 1555 //prepare_service_request, tunnel_peer_disconnect_handler,
1619 if (!session->tunnel) 1556 if (!session->tunnel) {
1620 {
1621 GNUNET_break (0); 1557 GNUNET_break (0);
1622 GNUNET_free (session->vector); 1558 GNUNET_free (session->vector);
1623 GNUNET_free (session); 1559 GNUNET_free (session);
@@ -1633,8 +1569,7 @@ handle_client_request (void *cls,
1633 session); 1569 session);
1634 1570
1635 } 1571 }
1636 else 1572 else {
1637 {
1638 struct ServiceSession * requesting_session; 1573 struct ServiceSession * requesting_session;
1639 enum SessionState needed_state = SERVICE_REQUEST_RECEIVED; 1574 enum SessionState needed_state = SERVICE_REQUEST_RECEIVED;
1640 1575
@@ -1654,8 +1589,7 @@ handle_client_request (void *cls,
1654 &session->key, 1589 &session->key,
1655 session->element_count, 1590 session->element_count,
1656 &needed_state, NULL); 1591 &needed_state, NULL);
1657 if (NULL != requesting_session) 1592 if (NULL != requesting_session) {
1658 {
1659 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Got client-responder-session with key %s and a matching service-request-session set, processing.\n"), GNUNET_h2s (&session->key)); 1593 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Got client-responder-session with key %s and a matching service-request-session set, processing.\n"), GNUNET_h2s (&session->key));
1660 if (GNUNET_OK != compute_service_response (requesting_session, session)) 1594 if (GNUNET_OK != compute_service_response (requesting_session, session))
1661 session->client_notification_task = 1595 session->client_notification_task =
@@ -1663,8 +1597,7 @@ handle_client_request (void *cls,
1663 session); 1597 session);
1664 1598
1665 } 1599 }
1666 else 1600 else {
1667 {
1668 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Got client-responder-session with key %s but NO matching service-request-session set, queuing element for later use.\n"), GNUNET_h2s (&session->key)); 1601 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Got client-responder-session with key %s but NO matching service-request-session set, queuing element for later use.\n"), GNUNET_h2s (&session->key));
1669 // no matching session exists yet, store the response 1602 // no matching session exists yet, store the response
1670 // for later processing by handle_service_request() 1603 // for later processing by handle_service_request()
@@ -1673,7 +1606,6 @@ handle_client_request (void *cls,
1673 GNUNET_SERVER_receive_done (client, GNUNET_YES); 1606 GNUNET_SERVER_receive_done (client, GNUNET_YES);
1674} 1607}
1675 1608
1676
1677/** 1609/**
1678 * Function called for inbound tunnels. 1610 * Function called for inbound tunnels.
1679 * 1611 *
@@ -1699,7 +1631,6 @@ tunnel_incoming_handler (void *cls,
1699 return c; 1631 return c;
1700} 1632}
1701 1633
1702
1703/** 1634/**
1704 * Function called whenever a tunnel is destroyed. Should clean up 1635 * Function called whenever a tunnel is destroyed. Should clean up
1705 * any associated state. 1636 * any associated state.
@@ -1724,12 +1655,10 @@ tunnel_destruction_handler (void *cls,
1724 _ ("Peer disconnected, terminating session %s with peer (%s)\n"), 1655 _ ("Peer disconnected, terminating session %s with peer (%s)\n"),
1725 GNUNET_h2s (&session->key), 1656 GNUNET_h2s (&session->key),
1726 GNUNET_i2s (&session->peer)); 1657 GNUNET_i2s (&session->peer));
1727 if (ALICE == session->role) 1658 if (ALICE == session->role) {
1728 {
1729 // as we have only one peer connected in each session, just remove the session 1659 // as we have only one peer connected in each session, just remove the session
1730 1660
1731 if ((SERVICE_RESPONSE_RECEIVED > session->state) && (!do_shutdown)) 1661 if ((SERVICE_RESPONSE_RECEIVED > session->state) && (!do_shutdown)) {
1732 {
1733 session->tunnel = NULL; 1662 session->tunnel = NULL;
1734 // if this happened before we received the answer, we must terminate the session 1663 // if this happened before we received the answer, we must terminate the session
1735 session->client_notification_task = 1664 session->client_notification_task =
@@ -1737,14 +1666,12 @@ tunnel_destruction_handler (void *cls,
1737 session); 1666 session);
1738 } 1667 }
1739 } 1668 }
1740 else 1669 else { //(BOB == session->role) service session
1741 { //(BOB == session->role) service session
1742 // remove the session, unless it has already been dequeued, but somehow still active 1670 // remove the session, unless it has already been dequeued, but somehow still active
1743 // this could bug without the IF in case the queue is empty and the service session was the only one know to the service 1671 // this could bug without the IF in case the queue is empty and the service session was the only one know to the service
1744 // scenario: disconnect before alice can send her message to bob. 1672 // scenario: disconnect before alice can send her message to bob.
1745 for (curr = from_service_head; NULL != curr; curr = curr->next) 1673 for (curr = from_service_head; NULL != curr; curr = curr->next)
1746 if (curr == session) 1674 if (curr == session) {
1747 {
1748 GNUNET_CONTAINER_DLL_remove (from_service_head, from_service_tail, curr); 1675 GNUNET_CONTAINER_DLL_remove (from_service_head, from_service_tail, curr);
1749 break; 1676 break;
1750 } 1677 }
@@ -1758,8 +1685,7 @@ tunnel_destruction_handler (void *cls,
1758 1685
1759 // the client has to check if it was waiting for a result 1686 // the client has to check if it was waiting for a result
1760 // or if it was a responder, no point in adding more statefulness 1687 // or if it was a responder, no point in adding more statefulness
1761 if (client_session && (!do_shutdown)) 1688 if (client_session && (!do_shutdown)) {
1762 {
1763 client_session->state = FINALIZED; 1689 client_session->state = FINALIZED;
1764 client_session->client_notification_task = 1690 client_session->client_notification_task =
1765 GNUNET_SCHEDULER_add_now (&prepare_client_end_notification, 1691 GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
@@ -1768,7 +1694,6 @@ tunnel_destruction_handler (void *cls,
1768 } 1694 }
1769} 1695}
1770 1696
1771
1772/** 1697/**
1773 * Compute our scalar product, done by Alice 1698 * Compute our scalar product, done by Alice
1774 * 1699 *
@@ -1801,8 +1726,7 @@ compute_scalar_product (struct ServiceSession * session,
1801 // due to the introduced static offset S, we now also have to remove this 1726 // due to the introduced static offset S, we now also have to remove this
1802 // from the E(a_pi)(+)E(-b_pi-r_pi) and E(a_qi)(+)E(-r_qi) twice each, 1727 // from the E(a_pi)(+)E(-b_pi-r_pi) and E(a_qi)(+)E(-r_qi) twice each,
1803 // the result is E((S + a_pi) + (S -b_pi-r_pi)) and E(S + a_qi + S - r_qi) 1728 // the result is E((S + a_pi) + (S -b_pi-r_pi)) and E(S + a_qi + S - r_qi)
1804 for (i = 0; i < count; i++) 1729 for (i = 0; i < count; i++) {
1805 {
1806 decrypt_element (r[i], r[i], my_mu, my_lambda, my_n, my_nsquare); 1730 decrypt_element (r[i], r[i], my_mu, my_lambda, my_n, my_nsquare);
1807 gcry_mpi_sub (r[i], r[i], my_offset); 1731 gcry_mpi_sub (r[i], r[i], my_offset);
1808 gcry_mpi_sub (r[i], r[i], my_offset); 1732 gcry_mpi_sub (r[i], r[i], my_offset);
@@ -1859,7 +1783,6 @@ compute_scalar_product (struct ServiceSession * session,
1859 return p; 1783 return p;
1860} 1784}
1861 1785
1862
1863/** 1786/**
1864 * prepare the response we will send to alice or bobs' clients. 1787 * prepare the response we will send to alice or bobs' clients.
1865 * in Bobs case the product will be NULL. 1788 * in Bobs case the product will be NULL.
@@ -1881,19 +1804,16 @@ prepare_client_response (void *cls,
1881 1804
1882 session->client_notification_task = GNUNET_SCHEDULER_NO_TASK; 1805 session->client_notification_task = GNUNET_SCHEDULER_NO_TASK;
1883 1806
1884 if (session->product) 1807 if (session->product) {
1885 {
1886 gcry_mpi_t value = gcry_mpi_new (0); 1808 gcry_mpi_t value = gcry_mpi_new (0);
1887 1809
1888 sign = gcry_mpi_cmp_ui (session->product, 0); 1810 sign = gcry_mpi_cmp_ui (session->product, 0);
1889 // libgcrypt can not handle a print of a negative number 1811 // libgcrypt can not handle a print of a negative number
1890 // if (a->sign) return gcry_error (GPG_ERR_INTERNAL); /* Can't handle it yet. */ 1812 // if (a->sign) return gcry_error (GPG_ERR_INTERNAL); /* Can't handle it yet. */
1891 if (0 > sign) 1813 if (0 > sign) {
1892 {
1893 gcry_mpi_sub (value, value, session->product); 1814 gcry_mpi_sub (value, value, session->product);
1894 } 1815 }
1895 else if (0 < sign) 1816 else if (0 < sign) {
1896 {
1897 range = 1; 1817 range = 1;
1898 gcry_mpi_add (value, value, session->product); 1818 gcry_mpi_add (value, value, session->product);
1899 } 1819 }
@@ -1908,8 +1828,7 @@ prepare_client_response (void *cls,
1908 && (0 != (rc = gcry_mpi_aprint (GCRYMPI_FMT_STD, 1828 && (0 != (rc = gcry_mpi_aprint (GCRYMPI_FMT_STD,
1909 &product_exported, 1829 &product_exported,
1910 &product_length, 1830 &product_length,
1911 value)))) 1831 value)))) {
1912 {
1913 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); 1832 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
1914 product_length = 0; 1833 product_length = 0;
1915 range = -1; // signal error with product-length = 0 and range = -1 1834 range = -1; // signal error with product-length = 0 and range = -1
@@ -1921,8 +1840,7 @@ prepare_client_response (void *cls,
1921 msg = GNUNET_malloc (msg_length); 1840 msg = GNUNET_malloc (msg_length);
1922 memcpy (&msg->key, &session->key, sizeof (struct GNUNET_HashCode)); 1841 memcpy (&msg->key, &session->key, sizeof (struct GNUNET_HashCode));
1923 memcpy (&msg->peer, &session->peer, sizeof ( struct GNUNET_PeerIdentity)); 1842 memcpy (&msg->peer, &session->peer, sizeof ( struct GNUNET_PeerIdentity));
1924 if (product_exported != NULL) 1843 if (product_exported != NULL) {
1925 {
1926 memcpy (&msg[1], product_exported, product_length); 1844 memcpy (&msg[1], product_exported, product_length);
1927 GNUNET_free (product_exported); 1845 GNUNET_free (product_exported);
1928 } 1846 }
@@ -1939,8 +1857,7 @@ prepare_client_response (void *cls,
1939 GNUNET_TIME_UNIT_FOREVER_REL, 1857 GNUNET_TIME_UNIT_FOREVER_REL,
1940 &do_send_message, 1858 &do_send_message,
1941 session); 1859 session);
1942 if (NULL == session->client_transmit_handle) 1860 if (NULL == session->client_transmit_handle) {
1943 {
1944 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1861 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1945 _ ("Could not send message to client (%p)!\n"), 1862 _ ("Could not send message to client (%p)!\n"),
1946 session->client); 1863 session->client);
@@ -1971,11 +1888,11 @@ prepare_client_response (void *cls,
1971 */ 1888 */
1972static int 1889static int
1973handle_service_request_multipart (void *cls, 1890handle_service_request_multipart (void *cls,
1974 struct GNUNET_MESH_Tunnel * tunnel, 1891 struct GNUNET_MESH_Tunnel * tunnel,
1975 void **tunnel_ctx, 1892 void **tunnel_ctx,
1976 const struct GNUNET_MessageHeader * message) 1893 const struct GNUNET_MessageHeader * message)
1977{ 1894{
1978 return GNUNET_SYSERR; 1895 return GNUNET_SYSERR;
1979} 1896}
1980 1897
1981/** 1898/**
@@ -2001,6 +1918,7 @@ handle_service_request (void *cls,
2001 uint32_t mask_length; 1918 uint32_t mask_length;
2002 uint32_t pk_length; 1919 uint32_t pk_length;
2003 uint32_t used_elements; 1920 uint32_t used_elements;
1921 uint32_t contained_elements;
2004 uint32_t element_count; 1922 uint32_t element_count;
2005 uint32_t msg_length; 1923 uint32_t msg_length;
2006 unsigned char * current; 1924 unsigned char * current;
@@ -2009,44 +1927,39 @@ handle_service_request (void *cls,
2009 enum SessionState needed_state; 1927 enum SessionState needed_state;
2010 1928
2011 session = (struct ServiceSession *) * tunnel_ctx; 1929 session = (struct ServiceSession *) * tunnel_ctx;
2012 if (BOB != session->role) 1930 if (BOB != session->role) {
2013 {
2014 GNUNET_break_op (0); 1931 GNUNET_break_op (0);
2015 return GNUNET_SYSERR; 1932 return GNUNET_SYSERR;
2016 } 1933 }
2017 // is this tunnel already in use? 1934 // is this tunnel already in use?
2018 if ((session->next) || (from_service_head == session)) 1935 if ((session->next) || (from_service_head == session)) {
2019 {
2020 GNUNET_break_op (0); 1936 GNUNET_break_op (0);
2021 return GNUNET_SYSERR; 1937 return GNUNET_SYSERR;
2022 } 1938 }
2023 // Check if message was sent by me, which would be bad! 1939 // Check if message was sent by me, which would be bad!
2024 if (!memcmp (&session->peer, &me, sizeof (struct GNUNET_PeerIdentity))) 1940 if (!memcmp (&session->peer, &me, sizeof (struct GNUNET_PeerIdentity))) {
2025 {
2026 GNUNET_free (session); 1941 GNUNET_free (session);
2027 GNUNET_break (0); 1942 GNUNET_break (0);
2028 return GNUNET_SYSERR; 1943 return GNUNET_SYSERR;
2029 } 1944 }
2030 1945 // shorter than expected?
2031 //we need at least a peer and one message id to compare 1946 if (ntohs (msg->header.size) < sizeof (struct GNUNET_SCALARPRODUCT_service_request)) {
2032 if (ntohs (msg->header.size) < sizeof (struct GNUNET_SCALARPRODUCT_service_request))
2033 {
2034 GNUNET_free (session); 1947 GNUNET_free (session);
2035 GNUNET_break_op (0); 1948 GNUNET_break_op (0);
2036 return GNUNET_SYSERR; 1949 return GNUNET_SYSERR;
2037 } 1950 }
2038 mask_length = ntohl (msg->mask_length); 1951 mask_length = ntohl (msg->mask_length);
2039 pk_length = ntohl (msg->pk_length); 1952 pk_length = ntohl (msg->pk_length);
2040 used_elements = ntohl (msg->contained_element_count); 1953 used_elements = ntohl (msg->total_element_count);
1954 contained_elements = ntohl (msg->contained_element_count);
2041 element_count = ntohl (msg->element_count); 1955 element_count = ntohl (msg->element_count);
2042 msg_length = sizeof (struct GNUNET_SCALARPRODUCT_service_request) 1956 msg_length = sizeof (struct GNUNET_SCALARPRODUCT_service_request)
2043 +mask_length + pk_length + used_elements * PAILLIER_ELEMENT_LENGTH; 1957 +mask_length + pk_length + contained_elements * PAILLIER_ELEMENT_LENGTH;
2044 1958
2045 //sanity check: is the message as long as the message_count fields suggests? 1959 //sanity check: is the message as long as the message_count fields suggests?
2046 if ((ntohs (msg->header.size) != msg_length) || (element_count < used_elements) 1960 if ((ntohs (msg->header.size) != msg_length) || (element_count < used_elements) || (used_elements < contained_elements)
2047 || (used_elements == 0) || (mask_length != (element_count / 8 + (element_count % 8 ? 1 : 0))) 1961 || (used_elements == 0) || (mask_length != (element_count / 8 + (element_count % 8 ? 1 : 0)))
2048 ) 1962 ) {
2049 {
2050 GNUNET_free (session); 1963 GNUNET_free (session);
2051 GNUNET_break_op (0); 1964 GNUNET_break_op (0);
2052 return GNUNET_SYSERR; 1965 return GNUNET_SYSERR;
@@ -2055,8 +1968,7 @@ handle_service_request (void *cls,
2055 &msg->key, 1968 &msg->key,
2056 element_count, 1969 element_count,
2057 NULL, 1970 NULL,
2058 NULL)) 1971 NULL)) {
2059 {
2060 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Got message with duplicate session key (`%s'), ignoring service request.\n"), (const char *) &(msg->key)); 1972 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Got message with duplicate session key (`%s'), ignoring service request.\n"), (const char *) &(msg->key));
2061 GNUNET_free (session); 1973 GNUNET_free (session);
2062 return GNUNET_SYSERR; 1974 return GNUNET_SYSERR;
@@ -2066,6 +1978,7 @@ handle_service_request (void *cls,
2066 session->state = SERVICE_REQUEST_RECEIVED; 1978 session->state = SERVICE_REQUEST_RECEIVED;
2067 session->element_count = ntohl (msg->element_count); 1979 session->element_count = ntohl (msg->element_count);
2068 session->used_element_count = used_elements; 1980 session->used_element_count = used_elements;
1981 session->transferred_element_count = contained_elements;
2069 session->tunnel = tunnel; 1982 session->tunnel = tunnel;
2070 1983
2071 // session key 1984 // session key
@@ -2078,8 +1991,7 @@ handle_service_request (void *cls,
2078 current += mask_length; 1991 current += mask_length;
2079 1992
2080 //convert the publickey to sexp 1993 //convert the publickey to sexp
2081 if (gcry_sexp_new (&session->remote_pubkey, current, pk_length, 1)) 1994 if (gcry_sexp_new (&session->remote_pubkey, current, pk_length, 1)) {
2082 {
2083 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Could not translate remote public key to sexpression!\n")); 1995 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Could not translate remote public key to sexpression!\n"));
2084 GNUNET_free (session->mask); 1996 GNUNET_free (session->mask);
2085 GNUNET_free (session); 1997 GNUNET_free (session);
@@ -2097,16 +2009,11 @@ handle_service_request (void *cls,
2097 2009
2098 session->a = GNUNET_malloc (sizeof (gcry_mpi_t) * used_elements); 2010 session->a = GNUNET_malloc (sizeof (gcry_mpi_t) * used_elements);
2099 2011
2100 if (GNUNET_SERVER_MAX_MESSAGE_SIZE >= sizeof (struct GNUNET_SCALARPRODUCT_service_request) 2012 if (contained_elements != 0) {
2101 +pk_length
2102 + mask_length
2103 + used_elements * PAILLIER_ELEMENT_LENGTH)
2104 {
2105 gcry_error_t ret = 0; 2013 gcry_error_t ret = 0;
2106 session->a = GNUNET_malloc (sizeof (gcry_mpi_t) * used_elements); 2014 session->a = GNUNET_malloc (sizeof (gcry_mpi_t) * used_elements);
2107 // Convert each vector element to MPI_value 2015 // Convert each vector element to MPI_value
2108 for (i = 0; i < used_elements; i++) 2016 for (i = 0; i < used_elements; i++) {
2109 {
2110 size_t read = 0; 2017 size_t read = 0;
2111 2018
2112 ret = gcry_mpi_scan (&session->a[i], 2019 ret = gcry_mpi_scan (&session->a[i],
@@ -2114,35 +2021,34 @@ handle_service_request (void *cls,
2114 &current[i * PAILLIER_ELEMENT_LENGTH], 2021 &current[i * PAILLIER_ELEMENT_LENGTH],
2115 PAILLIER_ELEMENT_LENGTH, 2022 PAILLIER_ELEMENT_LENGTH,
2116 &read); 2023 &read);
2117 if (ret) 2024 if (ret) {
2118 {
2119 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Could not translate E[a%d] to MPI!\n%s/%s\n"), 2025 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Could not translate E[a%d] to MPI!\n%s/%s\n"),
2120 i, gcry_strsource (ret), gcry_strerror (ret)); 2026 i, gcry_strsource (ret), gcry_strerror (ret));
2121 goto except; 2027 goto except;
2122 } 2028 }
2123 } 2029 }
2124 GNUNET_CONTAINER_DLL_insert (from_service_head, from_service_tail, session); 2030 GNUNET_CONTAINER_DLL_insert (from_service_head, from_service_tail, session);
2125 if (responder_session) 2031
2126 { 2032 if (contained_elements == used_elements) {
2127 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Got session with key %s and a matching element set, processing.\n"), GNUNET_h2s (&session->key)); 2033 // single part finished
2128 if (GNUNET_OK != compute_service_response (session, responder_session)) 2034 if (responder_session) {
2129 { 2035 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Got session with key %s and a matching element set, processing.\n"), GNUNET_h2s (&session->key));
2130 //something went wrong, remove it again... 2036 if (GNUNET_OK != compute_service_response (session, responder_session)) {
2131 GNUNET_CONTAINER_DLL_remove (from_service_head, from_service_tail, session); 2037 //something went wrong, remove it again...
2132 goto except; 2038 GNUNET_CONTAINER_DLL_remove (from_service_head, from_service_tail, session);
2039 goto except;
2040 }
2133 } 2041 }
2042 else
2043 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Got session with key %s without a matching element set, queueing.\n"), GNUNET_h2s (&session->key));
2044 }
2045 else{
2046 // multipart message
2047
2134 } 2048 }
2135 else
2136 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Got session with key %s without a matching element set, queueing.\n"), GNUNET_h2s (&session->key));
2137 2049
2138 return GNUNET_OK; 2050 return GNUNET_OK;
2139 } 2051 }
2140 else
2141 {
2142 // TODO FEATURE: fallback to fragmentation, in case the message is too long
2143 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Message too large, fragmentation is currently not supported!\n"));
2144 goto except;
2145 }
2146except: 2052except:
2147 for (i = 0; i < used_elements; i++) 2053 for (i = 0; i < used_elements; i++)
2148 if (session->a[i]) 2054 if (session->a[i])
@@ -2161,7 +2067,6 @@ except:
2161 return GNUNET_SYSERR; 2067 return GNUNET_SYSERR;
2162} 2068}
2163 2069
2164
2165/** 2070/**
2166 * Handle a multipart chunk of a response we got from another service we wanted to calculate a scalarproduct with. 2071 * Handle a multipart chunk of a response we got from another service we wanted to calculate a scalarproduct with.
2167 * 2072 *
@@ -2176,11 +2081,11 @@ except:
2176 */ 2081 */
2177static int 2082static int
2178handle_service_response_multipart (void *cls, 2083handle_service_response_multipart (void *cls,
2179 struct GNUNET_MESH_Tunnel * tunnel, 2084 struct GNUNET_MESH_Tunnel * tunnel,
2180 void **tunnel_ctx, 2085 void **tunnel_ctx,
2181 const struct GNUNET_MessageHeader * message) 2086 const struct GNUNET_MessageHeader * message)
2182{ 2087{
2183 return GNUNET_SYSERR; 2088 return GNUNET_SYSERR;
2184} 2089}
2185 2090
2186/** 2091/**
@@ -2217,8 +2122,7 @@ handle_service_response (void *cls,
2217 2122
2218 GNUNET_assert (NULL != message); 2123 GNUNET_assert (NULL != message);
2219 session = (struct ServiceSession *) * tunnel_ctx; 2124 session = (struct ServiceSession *) * tunnel_ctx;
2220 if (ALICE != session->role) 2125 if (ALICE != session->role) {
2221 {
2222 GNUNET_break_op (0); 2126 GNUNET_break_op (0);
2223 return GNUNET_SYSERR; 2127 return GNUNET_SYSERR;
2224 } 2128 }
@@ -2228,8 +2132,7 @@ handle_service_response (void *cls,
2228 session->state = SERVICE_RESPONSE_RECEIVED; 2132 session->state = SERVICE_RESPONSE_RECEIVED;
2229 2133
2230 //we need at least a peer and one message id to compare 2134 //we need at least a peer and one message id to compare
2231 if (sizeof (struct GNUNET_SCALARPRODUCT_service_response) > ntohs (msg->header.size)) 2135 if (sizeof (struct GNUNET_SCALARPRODUCT_service_response) > ntohs (msg->header.size)) {
2232 {
2233 GNUNET_break_op (0); 2136 GNUNET_break_op (0);
2234 goto invalid_msg; 2137 goto invalid_msg;
2235 } 2138 }
@@ -2238,8 +2141,7 @@ handle_service_response (void *cls,
2238 + 2 * contained_element_count * PAILLIER_ELEMENT_LENGTH 2141 + 2 * contained_element_count * PAILLIER_ELEMENT_LENGTH
2239 + 2 * PAILLIER_ELEMENT_LENGTH; 2142 + 2 * PAILLIER_ELEMENT_LENGTH;
2240 //sanity check: is the message as long as the message_count fields suggests? 2143 //sanity check: is the message as long as the message_count fields suggests?
2241 if ((ntohs (msg->header.size) != msg_size) || (count != contained_element_count)) 2144 if ((ntohs (msg->header.size) != msg_size) || (count != contained_element_count)) {
2242 {
2243 GNUNET_break_op (0); 2145 GNUNET_break_op (0);
2244 goto invalid_msg; 2146 goto invalid_msg;
2245 } 2147 }
@@ -2247,8 +2149,7 @@ handle_service_response (void *cls,
2247 //convert s 2149 //convert s
2248 current = (unsigned char *) &msg[1]; 2150 current = (unsigned char *) &msg[1];
2249 if (0 != (rc = gcry_mpi_scan (&s, GCRYMPI_FMT_USG, current, 2151 if (0 != (rc = gcry_mpi_scan (&s, GCRYMPI_FMT_USG, current,
2250 PAILLIER_ELEMENT_LENGTH, &read))) 2152 PAILLIER_ELEMENT_LENGTH, &read))) {
2251 {
2252 LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc); 2153 LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc);
2253 GNUNET_break_op (0); 2154 GNUNET_break_op (0);
2254 goto invalid_msg; 2155 goto invalid_msg;
@@ -2256,8 +2157,7 @@ handle_service_response (void *cls,
2256 current += PAILLIER_ELEMENT_LENGTH; 2157 current += PAILLIER_ELEMENT_LENGTH;
2257 //convert stick 2158 //convert stick
2258 if (0 != (rc = gcry_mpi_scan (&s_prime, GCRYMPI_FMT_USG, current, 2159 if (0 != (rc = gcry_mpi_scan (&s_prime, GCRYMPI_FMT_USG, current,
2259 PAILLIER_ELEMENT_LENGTH, &read))) 2160 PAILLIER_ELEMENT_LENGTH, &read))) {
2260 {
2261 LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc); 2161 LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc);
2262 GNUNET_break_op (0); 2162 GNUNET_break_op (0);
2263 goto invalid_msg; 2163 goto invalid_msg;
@@ -2266,11 +2166,9 @@ handle_service_response (void *cls,
2266 2166
2267 r = GNUNET_malloc (sizeof (gcry_mpi_t) * count); 2167 r = GNUNET_malloc (sizeof (gcry_mpi_t) * count);
2268 // Convert each kp[] to its MPI_value 2168 // Convert each kp[] to its MPI_value
2269 for (i = 0; i < count; i++) 2169 for (i = 0; i < count; i++) {
2270 {
2271 if (0 != (rc = gcry_mpi_scan (&r[i], GCRYMPI_FMT_USG, current, 2170 if (0 != (rc = gcry_mpi_scan (&r[i], GCRYMPI_FMT_USG, current,
2272 PAILLIER_ELEMENT_LENGTH, &read))) 2171 PAILLIER_ELEMENT_LENGTH, &read))) {
2273 {
2274 LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc); 2172 LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc);
2275 GNUNET_break_op (0); 2173 GNUNET_break_op (0);
2276 goto invalid_msg; 2174 goto invalid_msg;
@@ -2281,11 +2179,9 @@ handle_service_response (void *cls,
2281 2179
2282 r_prime = GNUNET_malloc (sizeof (gcry_mpi_t) * count); 2180 r_prime = GNUNET_malloc (sizeof (gcry_mpi_t) * count);
2283 // Convert each kq[] to its MPI_value 2181 // Convert each kq[] to its MPI_value
2284 for (i = 0; i < count; i++) 2182 for (i = 0; i < count; i++) {
2285 {
2286 if (0 != (rc = gcry_mpi_scan (&r_prime[i], GCRYMPI_FMT_USG, current, 2183 if (0 != (rc = gcry_mpi_scan (&r_prime[i], GCRYMPI_FMT_USG, current,
2287 PAILLIER_ELEMENT_LENGTH, &read))) 2184 PAILLIER_ELEMENT_LENGTH, &read))) {
2288 {
2289 LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc); 2185 LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc);
2290 GNUNET_break_op (0); 2186 GNUNET_break_op (0);
2291 goto invalid_msg; 2187 goto invalid_msg;
@@ -2317,7 +2213,6 @@ invalid_msg:
2317 return GNUNET_SYSERR; 2213 return GNUNET_SYSERR;
2318} 2214}
2319 2215
2320
2321/** 2216/**
2322 * Task run during shutdown. 2217 * Task run during shutdown.
2323 * 2218 *
@@ -2334,42 +2229,36 @@ shutdown_task (void *cls,
2334 do_shutdown = GNUNET_YES; 2229 do_shutdown = GNUNET_YES;
2335 2230
2336 // terminate all owned open tunnels. 2231 // terminate all owned open tunnels.
2337 for (session = from_client_head; NULL != session; session = session->next) 2232 for (session = from_client_head; NULL != session; session = session->next) {
2338 { 2233 if ((FINALIZED != session->state) && (NULL != session->tunnel)) {
2339 if ((FINALIZED != session->state) && (NULL != session->tunnel)){
2340 GNUNET_MESH_tunnel_destroy (session->tunnel); 2234 GNUNET_MESH_tunnel_destroy (session->tunnel);
2341 session->tunnel = NULL; 2235 session->tunnel = NULL;
2342 } 2236 }
2343 if (GNUNET_SCHEDULER_NO_TASK != session->client_notification_task) 2237 if (GNUNET_SCHEDULER_NO_TASK != session->client_notification_task) {
2344 {
2345 GNUNET_SCHEDULER_cancel (session->client_notification_task); 2238 GNUNET_SCHEDULER_cancel (session->client_notification_task);
2346 session->client_notification_task = GNUNET_SCHEDULER_NO_TASK; 2239 session->client_notification_task = GNUNET_SCHEDULER_NO_TASK;
2347 } 2240 }
2348 if (GNUNET_SCHEDULER_NO_TASK != session->service_request_task) 2241 if (GNUNET_SCHEDULER_NO_TASK != session->service_request_task) {
2349 {
2350 GNUNET_SCHEDULER_cancel (session->service_request_task); 2242 GNUNET_SCHEDULER_cancel (session->service_request_task);
2351 session->service_request_task = GNUNET_SCHEDULER_NO_TASK; 2243 session->service_request_task = GNUNET_SCHEDULER_NO_TASK;
2352 } 2244 }
2353 if (NULL != session->client) 2245 if (NULL != session->client) {
2354 {
2355 GNUNET_SERVER_client_disconnect (session->client); 2246 GNUNET_SERVER_client_disconnect (session->client);
2356 session->client = NULL; 2247 session->client = NULL;
2357 } 2248 }
2358 } 2249 }
2359 for (session = from_service_head; NULL != session; session = session->next) 2250 for (session = from_service_head; NULL != session; session = session->next)
2360 if (NULL != session->tunnel){ 2251 if (NULL != session->tunnel) {
2361 GNUNET_MESH_tunnel_destroy (session->tunnel); 2252 GNUNET_MESH_tunnel_destroy (session->tunnel);
2362 session->tunnel = NULL; 2253 session->tunnel = NULL;
2363 } 2254 }
2364 2255
2365 if (my_mesh) 2256 if (my_mesh) {
2366 {
2367 GNUNET_MESH_disconnect (my_mesh); 2257 GNUNET_MESH_disconnect (my_mesh);
2368 my_mesh = NULL; 2258 my_mesh = NULL;
2369 } 2259 }
2370} 2260}
2371 2261
2372
2373/** 2262/**
2374 * Initialization of the program and message handlers 2263 * Initialization of the program and message handlers
2375 * 2264 *
@@ -2413,8 +2302,7 @@ run (void *cls,
2413 &tunnel_incoming_handler, 2302 &tunnel_incoming_handler,
2414 &tunnel_destruction_handler, 2303 &tunnel_destruction_handler,
2415 mesh_handlers, ports); 2304 mesh_handlers, ports);
2416 if (!my_mesh) 2305 if (!my_mesh) {
2417 {
2418 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Connect to MESH failed\n")); 2306 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Connect to MESH failed\n"));
2419 GNUNET_SCHEDULER_shutdown (); 2307 GNUNET_SCHEDULER_shutdown ();
2420 return; 2308 return;
@@ -2425,7 +2313,6 @@ run (void *cls,
2425 NULL); 2313 NULL);
2426} 2314}
2427 2315
2428
2429/** 2316/**
2430 * The main function for the scalarproduct service. 2317 * The main function for the scalarproduct service.
2431 * 2318 *