made the service more resilient against out of order and simply incorrect messages
[oweals/gnunet.git] / src / scalarproduct / gnunet-service-scalarproduct.c
index bdec7ebd8c55e2ec90b9af648cec57195bf69440..46567abed17a8cc03b4c62eeae07147febb28f8d 100644 (file)
  */
 enum SessionState
 {
 CLIENT_REQUEST_RECEIVED,
 WAITING_FOR_BOBS_CONNECT,
 CLIENT_RESPONSE_RECEIVED,
 WAITING_FOR_SERVICE_REQUEST,
 WAITING_FOR_MULTIPART_TRANSMISSION,
 WAITING_FOR_SERVICE_RESPONSE,
 SERVICE_REQUEST_RECEIVED,
 SERVICE_RESPONSE_RECEIVED,
 FINALIZED
+ CLIENT_REQUEST_RECEIVED,
+ WAITING_FOR_BOBS_CONNECT,
+ CLIENT_RESPONSE_RECEIVED,
+ WAITING_FOR_SERVICE_REQUEST,
+ WAITING_FOR_MULTIPART_TRANSMISSION,
+ WAITING_FOR_SERVICE_RESPONSE,
+ SERVICE_REQUEST_RECEIVED,
+ SERVICE_RESPONSE_RECEIVED,
+ FINALIZED
 };
 
 /**
@@ -60,8 +60,8 @@ enum SessionState
  */
 enum PeerRole
 {
 ALICE,
 BOB
+ ALICE,
+ BOB
 };
 
 /**
@@ -73,130 +73,140 @@ enum PeerRole
  */
 struct ServiceSession
 {
-  /**
-   * the role this peer has
-   */
-  enum PeerRole role;
-
-  /**
-   * session information is kept in a DLL
-   */
-  struct ServiceSession *next;
-
-  /**
-   * session information is kept in a DLL
-   */
-  struct ServiceSession *prev;
-
-  /**
-   * (hopefully) unique transaction ID
-   */
-  struct GNUNET_HashCode key;
-
-  /**
-   * state of the session
-   */
-  enum SessionState state;
-
-  /**
-   * Alice or Bob's peerID
-   */
-  struct GNUNET_PeerIdentity peer;
-
-  /**
-   * the client this request is related to
-   */
-  struct GNUNET_SERVER_Client * client;
-
-  /**
-   * The message to send
-   */
-  struct GNUNET_MessageHeader * msg;
-
-  /**
-   * how many elements we were supplied with from the client
-   */
-  uint32_t element_count;
-
-  /**
-   * how many elements actually are used after applying the mask
-   */
-  uint32_t used_element_count;
-
-  /**
-   * already transferred elements (sent/received) for multipart messages, less or equal than used_element_count for
-   */
-  uint32_t transferred_element_count;
-  
-  /**
-   * index of the last transferred element for multipart messages
-   */
-  uint32_t last_processed_element;
-
-  /**
-   * how many bytes the mask is long.
-   * just for convenience so we don't have to re-re-re calculate it each time
-   */
-  uint32_t mask_length;
-
-  /**
-   * all the vector elements we received
-   */
-  int32_t * vector;
-
-  /**
-   * mask of which elements to check
-   */
-  unsigned char * mask;
-
-  /**
-   * Public key of the remote service, only used by bob
-   */
-  gcry_sexp_t remote_pubkey;
-
-  /**
-   * E(ai)(Bob) or ai(Alice) after applying the mask
-   */
-  gcry_mpi_t * a;
-
-  /**
-   * Bob's permutation p of R
-   */
-  gcry_mpi_t * r;
-
-  /**
-   * Bob's permutation q of R
-   */
-  gcry_mpi_t * r_prime;
-  
-  /**
-   * Bobs matching response session from the client
-   */
-  struct ServiceSession * response;
-
-  /**
-   * The computed scalar
-   */
-  gcry_mpi_t product;
-
-  /**
-   * My transmit handle for the current message to a alice/bob
-   */
-  struct GNUNET_MESH_TransmitHandle * service_transmit_handle;
-
-  /**
-   * My transmit handle for the current message to the client
-   */
-  struct GNUNET_SERVER_TransmitHandle * client_transmit_handle;
-
-  /**
-   * tunnel-handle associated with our mesh handle
-   */
-  struct GNUNET_MESH_Tunnel * tunnel;
-
-  GNUNET_SCHEDULER_TaskIdentifier client_notification_task;
-
-  GNUNET_SCHEDULER_TaskIdentifier service_request_task;
+ /**
+  * the role this peer has
+  */
+ enum PeerRole role;
+
+ /**
+  * session information is kept in a DLL
+  */
+ struct ServiceSession *next;
+
+ /**
+  * session information is kept in a DLL
+  */
+ struct ServiceSession *prev;
+
+ /**
+  * (hopefully) unique transaction ID
+  */
+ struct GNUNET_HashCode key;
+
+ /**
+  * state of the session
+  */
+ enum SessionState state;
+
+ /**
+  * Alice or Bob's peerID
+  */
+ struct GNUNET_PeerIdentity peer;
+
+ /**
+  * the client this request is related to
+  */
+ struct GNUNET_SERVER_Client * client;
+
+ /**
+  * The message to send
+  */
+ struct GNUNET_MessageHeader * msg;
+
+ /**
+  * how many elements we were supplied with from the client
+  */
+ uint32_t element_count;
+
+ /**
+  * how many elements actually are used after applying the mask
+  */
+ uint32_t used_element_count;
+
+ /**
+  * already transferred elements (sent/received) for multipart messages, less or equal than used_element_count for
+  */
+ uint32_t transferred_element_count;
+
+ /**
+  * index of the last transferred element for multipart messages
+  */
+ uint32_t last_processed_element;
+
+ /**
+  * how many bytes the mask is long.
+  * just for convenience so we don't have to re-re-re calculate it each time
+  */
+ uint32_t mask_length;
+
+ /**
+  * all the vector elements we received
+  */
+ int32_t * vector;
+
+ /**
+  * mask of which elements to check
+  */
+ unsigned char * mask;
+
+ /**
+  * Public key of the remote service, only used by bob
+  */
+ gcry_sexp_t remote_pubkey;
+
+ /**
+  * E(ai)(Bob) or ai(Alice) after applying the mask
+  */
+ gcry_mpi_t * a;
+
+ /**
+  * Bob's permutation p of R
+  */
+ gcry_mpi_t * r;
+
+ /**
+  * Bob's permutation q of R
+  */
+ gcry_mpi_t * r_prime;
+ /**
+  * Bob's s
+  */
+ gcry_mpi_t s;
+ /**
+  * Bob's s'
+  */
+ gcry_mpi_t s_prime;
+
+ /**
+  * Bobs matching response session from the client
+  */
+ struct ServiceSession * response;
+
+ /**
+  * The computed scalar
+  */
+ gcry_mpi_t product;
+
+ /**
+  * My transmit handle for the current message to a alice/bob
+  */
+ struct GNUNET_MESH_TransmitHandle * service_transmit_handle;
+
+ /**
+  * My transmit handle for the current message to the client
+  */
+ struct GNUNET_SERVER_TransmitHandle * client_transmit_handle;
+
+ /**
+  * tunnel-handle associated with our mesh handle
+  */
+ struct GNUNET_MESH_Tunnel * tunnel;
+
+ GNUNET_SCHEDULER_TaskIdentifier client_notification_task;
+
+ GNUNET_SCHEDULER_TaskIdentifier service_request_task;
 };
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -290,7 +300,6 @@ static int do_shutdown;
 //                      Helper Functions
 ///////////////////////////////////////////////////////////////////////////////
 
-
 /**
  * Generates an Paillier private/public keyset and extracts the values using libgrcypt only
  */
@@ -349,11 +358,9 @@ generate_keyset ()
 
   // generate a g
   gcry_mpi_mul (my_nsquare, my_n, my_n);
-  do
-  {
+  do {
     // find a matching g
-    do
-    {
+    do {
       gcry_mpi_randomize (my_g, KEYBITS * 2, GCRY_WEAK_RANDOM);
       // g must be smaller than n^2
       if (0 >= gcry_mpi_cmp (my_g, my_nsquare))
@@ -407,7 +414,6 @@ generate_keyset ()
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _ ("Generated key set with key length %d bits.\n"), KEYBITS);
 }
 
-
 /**
  * If target != size, move target bytes to the
  * end of the size-sized buffer and zero out the
@@ -420,14 +426,12 @@ generate_keyset ()
 static void
 adjust (unsigned char *buf, size_t size, size_t target)
 {
-  if (size < target)
-  {
+  if (size < target) {
     memmove (&buf[target - size], buf, size);
     memset (buf, 0, target - size);
   }
 }
 
-
 /**
  * encrypts an element using the paillier crypto system
  *
@@ -444,8 +448,7 @@ encrypt_element (gcry_mpi_t c, gcry_mpi_t m, gcry_mpi_t g, gcry_mpi_t n, gcry_mp
 
   GNUNET_assert (tmp = gcry_mpi_new (0));
 
-  while (0 >= gcry_mpi_cmp_ui (tmp, 1))
-  {
+  while (0 >= gcry_mpi_cmp_ui (tmp, 1)) {
     gcry_mpi_randomize (tmp, KEYBITS / 3, GCRY_WEAK_RANDOM);
     // r must be 1 < r < n
   }
@@ -457,7 +460,6 @@ encrypt_element (gcry_mpi_t c, gcry_mpi_t m, gcry_mpi_t g, gcry_mpi_t n, gcry_mp
   gcry_mpi_release (tmp);
 }
 
-
 /**
  * decrypts an element using the paillier crypto system
  *
@@ -477,7 +479,6 @@ decrypt_element (gcry_mpi_t m, gcry_mpi_t c, gcry_mpi_t mu, gcry_mpi_t lambda, g
   gcry_mpi_mulm (m, m, mu, n);
 }
 
-
 /**
  * computes the square sum over a vector of a given length.
  *
@@ -496,8 +497,7 @@ compute_square_sum (gcry_mpi_t * vector, uint32_t length)
   GNUNET_assert (elem = gcry_mpi_new (0));
 
   // calculare E(sum (ai ^ 2), publickey)
-  for (i = 0; i < length; i++)
-  {
+  for (i = 0; i < length; i++) {
     gcry_mpi_mul (elem, vector[i], vector[i]);
     gcry_mpi_add (sum, sum, elem);
   }
@@ -509,10 +509,10 @@ compute_square_sum (gcry_mpi_t * vector, uint32_t length)
 
 static void
 prepare_service_request_multipart (void *cls,
-                         const struct GNUNET_SCHEDULER_TaskContext *tc);
+                                   const struct GNUNET_SCHEDULER_TaskContext *tc);
 static void
 prepare_service_response_multipart (void *cls,
-                         const struct GNUNET_SCHEDULER_TaskContext *tc);
+                                    const struct GNUNET_SCHEDULER_TaskContext *tc);
 
 /**
  * Primitive callback for copying over a message, as they
@@ -532,8 +532,7 @@ do_send_message (void *cls, size_t size, void *buf)
 
   GNUNET_assert (buf);
 
-  if (ntohs (session->msg->size) == size)
-  {
+  if (ntohs (session->msg->size) == size) {
     memcpy (buf, session->msg, size);
     written = size;
   }
@@ -550,7 +549,7 @@ do_send_message (void *cls, size_t size, void *buf)
     session->service_transmit_handle = NULL;
     // reset flags for sending
     if ((session->state != WAITING_FOR_MULTIPART_TRANSMISSION) && (session->used_element_count != session->transferred_element_count))
-      prepare_service_request_multipart(session, NULL);
+      prepare_service_request_multipart (session, NULL);
     //TODO we have sent a message and now need to trigger trigger the next multipart message sending
     break;
   case GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_TO_ALICE:
@@ -558,7 +557,7 @@ do_send_message (void *cls, size_t size, void *buf)
     //else
     session->service_transmit_handle = NULL;
     if ((session->state != WAITING_FOR_MULTIPART_TRANSMISSION) && (session->used_element_count != session->transferred_element_count))
-      prepare_service_response_multipart(session, NULL);
+      prepare_service_response_multipart (session, NULL);
     break;
   default:
     session->service_transmit_handle = NULL;
@@ -573,7 +572,6 @@ do_send_message (void *cls, size_t size, void *buf)
   return written;
 }
 
-
 /**
  * initializes a new vector with fresh MPI values (=0) of a given length
  *
@@ -591,7 +589,6 @@ initialize_mpi_vector (uint32_t length)
   return output;
 }
 
-
 /**
  * permutes an MPI vector according to the given permutation vector
  *
@@ -620,7 +617,6 @@ permute_vector (gcry_mpi_t * vector,
   return vector;
 }
 
-
 /**
  * Populate a vector with random integer values and convert them to
  *
@@ -635,8 +631,7 @@ generate_random_vector (uint32_t length)
   uint32_t i;
 
   random_vector = initialize_mpi_vector (length);
-  for (i = 0; i < length; i++)
-  {
+  for (i = 0; i < length; i++) {
     value = (int32_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
 
     // long to gcry_mpi_t
@@ -651,7 +646,6 @@ generate_random_vector (uint32_t length)
   return random_vector;
 }
 
-
 /**
  * Finds a not terminated client/service session in the
  * given DLL based on session key, element count and state.
@@ -670,15 +664,12 @@ find_matching_session (struct ServiceSession * tail,
 {
   struct ServiceSession * curr;
 
-  for (curr = tail; NULL != curr; curr = curr->prev)
-  {
+  for (curr = tail; NULL != curr; curr = curr->prev) {
     // if the key matches, and the element_count is same
     if ((!memcmp (&curr->key, key, sizeof (struct GNUNET_HashCode)))
-        && (curr->element_count == element_count))
-    {
+        && (curr->element_count == element_count)) {
       // if incoming state is NULL OR is same as state of the queued request
-      if ((NULL == state) || (curr->state == *state))
-      {
+      if ((NULL == state) || (curr->state == *state)) {
         // if peerid is NULL OR same as the peer Id in the queued request
         if ((NULL == peerid)
             || (!memcmp (&curr->peer, peerid, sizeof (struct GNUNET_PeerIdentity))))
@@ -691,14 +682,12 @@ find_matching_session (struct ServiceSession * tail,
   return NULL;
 }
 
-
 static void
 free_session (struct ServiceSession * session)
 {
   unsigned int i;
 
-  if (session->a)
-  {
+  if (session->a) {
     for (i = 0; i < session->used_element_count; i++)
       gcry_mpi_release (session->a[i]);
 
@@ -717,7 +706,6 @@ free_session (struct ServiceSession * session)
 //                      Event and Message Handlers
 ///////////////////////////////////////////////////////////////////////////////
 
-
 /**
  * A client disconnected.
  *
@@ -743,33 +731,28 @@ handle_client_disconnect (void *cls,
               _ ("Client (%p) disconnected from us.\n"), client);
   GNUNET_CONTAINER_DLL_remove (from_client_head, from_client_tail, session);
 
-  if (!(session->role == BOB && session->state == FINALIZED))
-  {
+  if (!(session->role == BOB && session->state == FINALIZED)) {
     //we MUST terminate any client message underway
     if (session->service_transmit_handle && session->tunnel)
       GNUNET_MESH_notify_transmit_ready_cancel (session->service_transmit_handle);
     if (session->tunnel && session->state == WAITING_FOR_SERVICE_RESPONSE)
       GNUNET_MESH_tunnel_destroy (session->tunnel);
   }
-  if (GNUNET_SCHEDULER_NO_TASK != session->client_notification_task)
-  {
+  if (GNUNET_SCHEDULER_NO_TASK != session->client_notification_task) {
     GNUNET_SCHEDULER_cancel (session->client_notification_task);
     session->client_notification_task = GNUNET_SCHEDULER_NO_TASK;
   }
-  if (GNUNET_SCHEDULER_NO_TASK != session->service_request_task)
-  {
+  if (GNUNET_SCHEDULER_NO_TASK != session->service_request_task) {
     GNUNET_SCHEDULER_cancel (session->service_request_task);
     session->service_request_task = GNUNET_SCHEDULER_NO_TASK;
   }
-  if (NULL != session->client_transmit_handle)
-  {
+  if (NULL != session->client_transmit_handle) {
     GNUNET_SERVER_notify_transmit_ready_cancel (session->client_transmit_handle);
     session->client_transmit_handle = NULL;
   }
   free_session (session);
 }
 
-
 /**
  * Notify the client that the session has succeeded or failed completely.
  * This message gets sent to
@@ -809,8 +792,7 @@ prepare_client_end_notification (void * cls,
                                                session);
 
   // if we could not even queue our request, something is wrong
-  if (NULL == session->client_transmit_handle)
-  {
+  if (NULL == session->client_transmit_handle) {
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Could not send message to client (%p)!\n"), session->client);
     // usually gets freed by do_send_message
     session->msg = NULL;
@@ -823,7 +805,7 @@ prepare_client_end_notification (void * cls,
 
 static void
 prepare_service_response_multipart (void *cls,
-                         const struct GNUNET_SCHEDULER_TaskContext *tc)
+                                    const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct ServiceSession * session = cls;
   unsigned char * current;
@@ -833,25 +815,24 @@ prepare_service_response_multipart (void *cls,
   uint32_t msg_length;
   uint32_t todo_count;
   size_t element_length = 0; // initialized by gcry_mpi_print, but the compiler doesn't know that
-  
+
   msg_length = sizeof (struct GNUNET_SCALARPRODUCT_multipart_message);
   todo_count = session->used_element_count - session->transferred_element_count;
 
-  if (todo_count > MULTIPART_ELEMENT_CAPACITY/2)
+  if (todo_count > MULTIPART_ELEMENT_CAPACITY / 2)
     // send the currently possible maximum chunk, we always transfer both permutations
-    todo_count = MULTIPART_ELEMENT_CAPACITY/2;
-  
+    todo_count = MULTIPART_ELEMENT_CAPACITY / 2;
+
   msg_length += todo_count * PAILLIER_ELEMENT_LENGTH * 2;
   msg = GNUNET_malloc (msg_length);
   msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_TO_BOB_MULTIPART);
   msg->header.size = htons (msg_length);
   msg->multipart_element_count = htonl (todo_count);
-  
+
   element_exported = GNUNET_malloc (PAILLIER_ELEMENT_LENGTH);
   current = (unsigned char *) &msg[1];
   // convert k[][]
-  for (i = session->transferred_element_count; i < session->transferred_element_count + todo_count; i++)
-  {
+  for (i = session->transferred_element_count; i < session->transferred_element_count + todo_count; i++) {
     //k[i][p]
     memset (element_exported, 0, PAILLIER_ELEMENT_LENGTH);
     GNUNET_assert (0 == gcry_mpi_print (GCRYMPI_FMT_USG,
@@ -872,12 +853,11 @@ prepare_service_response_multipart (void *cls,
     current += PAILLIER_ELEMENT_LENGTH;
   }
   GNUNET_free (element_exported);
-  for (i = session->transferred_element_count; i < session->transferred_element_count; i++)
-  {
+  for (i = session->transferred_element_count; i < session->transferred_element_count; i++) {
     gcry_mpi_release (session->r_prime[i]);
     gcry_mpi_release (session->r[i]);
   }
-  session->transferred_element_count+=todo_count;
+  session->transferred_element_count += todo_count;
   session->msg = (struct GNUNET_MessageHeader *) msg;
   session->service_transmit_handle =
           GNUNET_MESH_notify_transmit_ready (session->tunnel,
@@ -887,8 +867,7 @@ prepare_service_response_multipart (void *cls,
                                              &do_send_message,
                                              session);
   //disconnect our client
-  if (NULL == session->service_transmit_handle)
-  {
+  if (NULL == session->service_transmit_handle) {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Could not send service-response message via mesh!)\n"));
     session->state = FINALIZED;
 
@@ -935,8 +914,8 @@ prepare_service_response (gcry_mpi_t s,
   msg_length = sizeof (struct GNUNET_SCALARPRODUCT_service_response)
           + 2 * PAILLIER_ELEMENT_LENGTH; // s, stick
 
-  if (GNUNET_SERVER_MAX_MESSAGE_SIZE > msg_length + 2 * session->used_element_count * PAILLIER_ELEMENT_LENGTH){ //kp, kq
-    msg_length +=  + 2 * session->used_element_count * PAILLIER_ELEMENT_LENGTH;
+  if (GNUNET_SERVER_MAX_MESSAGE_SIZE > msg_length + 2 * session->used_element_count * PAILLIER_ELEMENT_LENGTH) { //kp, kq
+    msg_length += +2 * session->used_element_count * PAILLIER_ELEMENT_LENGTH;
     session->transferred_element_count = session->used_element_count;
   }
   else {
@@ -978,8 +957,7 @@ prepare_service_response (gcry_mpi_t s,
   current += PAILLIER_ELEMENT_LENGTH;
 
   // convert k[][]
-  for (i = 0; i < session->transferred_element_count; i++)
-  {
+  for (i = 0; i < session->transferred_element_count; i++) {
     //k[i][p]
     memset (element_exported, 0, PAILLIER_ELEMENT_LENGTH);
     GNUNET_assert (0 == gcry_mpi_print (GCRYMPI_FMT_USG,
@@ -1001,8 +979,7 @@ prepare_service_response (gcry_mpi_t s,
   }
 
   GNUNET_free (element_exported);
-  for (i = 0; i < session->transferred_element_count; i++)
-  {
+  for (i = 0; i < session->transferred_element_count; i++) {
     gcry_mpi_release (session->r_prime[i]);
     gcry_mpi_release (session->r[i]);
   }
@@ -1018,8 +995,7 @@ prepare_service_response (gcry_mpi_t s,
                                              &do_send_message,
                                              session);
   //disconnect our client
-  if (NULL == session->service_transmit_handle)
-  {
+  if (NULL == session->service_transmit_handle) {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Could not send service-response message via mesh!)\n"));
     session->state = FINALIZED;
 
@@ -1038,7 +1014,6 @@ prepare_service_response (gcry_mpi_t s,
   return GNUNET_OK;
 }
 
-
 /**
  * executed by bob:
  * compute the values
@@ -1089,19 +1064,15 @@ compute_service_response (struct ServiceSession * request,
   rand_pi_prime = GNUNET_malloc (sizeof (gcry_mpi_t) * count);
 
   // convert responder session to from long to mpi
-  for (i = 0, j = 0; i < response->element_count && j < count; i++)
-  {
-    if (request->mask[i / 8] & (1 << (i % 8)))
-    {
+  for (i = 0, j = 0; i < response->element_count && j < count; i++) {
+    if (request->mask[i / 8] & (1 << (i % 8))) {
       value = response->vector[i] >= 0 ? response->vector[i] : -response->vector[i];
       // long to gcry_mpi_t
-      if (0 > response->vector[i])
-      {
+      if (0 > response->vector[i]) {
         b[j] = gcry_mpi_new (0);
         gcry_mpi_sub_ui (b[j], b[j], value);
       }
-      else
-      {
+      else {
         b[j] = gcry_mpi_set_ui (NULL, value);
       }
       j++;
@@ -1111,16 +1082,14 @@ compute_service_response (struct ServiceSession * request,
   response->vector = NULL;
 
   tmp_exp = gcry_sexp_find_token (request->remote_pubkey, "n", 0);
-  if (!tmp_exp)
-  {
+  if (!tmp_exp) {
     GNUNET_break_op (0);
     gcry_sexp_release (request->remote_pubkey);
     request->remote_pubkey = NULL;
     goto except;
   }
   remote_n = gcry_sexp_nth_mpi (tmp_exp, 1, GCRYMPI_FMT_USG);
-  if (!remote_n)
-  {
+  if (!remote_n) {
     GNUNET_break (0);
     gcry_sexp_release (tmp_exp);
     goto except;
@@ -1131,15 +1100,13 @@ compute_service_response (struct ServiceSession * request,
   tmp_exp = gcry_sexp_find_token (request->remote_pubkey, "g", 0);
   gcry_sexp_release (request->remote_pubkey);
   request->remote_pubkey = NULL;
-  if (!tmp_exp)
-  {
+  if (!tmp_exp) {
     GNUNET_break_op (0);
     gcry_mpi_release (remote_n);
     goto except;
   }
   remote_g = gcry_sexp_nth_mpi (tmp_exp, 1, GCRYMPI_FMT_USG);
-  if (!remote_g)
-  {
+  if (!remote_g) {
     GNUNET_break (0);
     gcry_mpi_release (remote_n);
     gcry_sexp_release (tmp_exp);
@@ -1175,8 +1142,7 @@ compute_service_response (struct ServiceSession * request,
   // vectors, which get rid of all the lookups in p/q.
   // however, ap/aq are not absolutely necessary but are just abstraction
   // Calculate Kp = E(S + a_pi) (+) E(S - r_pi - b_pi)
-  for (i = 0; i < count; i++)
-  {
+  for (i = 0; i < count; i++) {
     // E(S - r_pi - b_pi)
     gcry_mpi_sub (r[i], my_offset, rand_pi[i]);
     gcry_mpi_sub (r[i], r[i], b_pi[i]);
@@ -1190,8 +1156,7 @@ compute_service_response (struct ServiceSession * request,
   GNUNET_free (rand_pi);
 
   // Calculate Kq = E(S + a_qi) (+) E(S - r_qi)
-  for (i = 0; i < count; i++)
-  {
+  for (i = 0; i < count; i++) {
     // E(S - r_qi)
     gcry_mpi_sub (r_prime[i], my_offset, rand_pi_prime[i]);
     encrypt_element (r_prime[i], r_prime[i], remote_g, remote_n, remote_nsquare);
@@ -1211,8 +1176,7 @@ compute_service_response (struct ServiceSession * request,
   encrypt_element (s_prime, s_prime, remote_g, remote_n, remote_nsquare);
 
   // Calculate S = E(SUM( (r_i + b_i)^2 ))
-  for (i = 0; i < count; i++)
-  {
+  for (i = 0; i < count; i++) {
     gcry_mpi_add (rand[i], rand[i], b[i]);
   }
   s = compute_square_sum (rand, count);
@@ -1234,8 +1198,7 @@ compute_service_response (struct ServiceSession * request,
     ret = GNUNET_OK;
 
 except:
-  for (i = 0; i < count; i++)
-  {
+  for (i = 0; i < count; i++) {
     gcry_mpi_release (b[i]);
     gcry_mpi_release (request->a[i]);
   }
@@ -1249,7 +1212,7 @@ except:
 
 static void
 prepare_service_request_multipart (void *cls,
-                         const struct GNUNET_SCHEDULER_TaskContext *tc)
+                                   const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct ServiceSession * session = cls;
   unsigned char * current;
@@ -1262,33 +1225,31 @@ prepare_service_request_multipart (void *cls,
   size_t element_length = 0; // initialized by gcry_mpi_print, but the compiler doesn't know that
   gcry_mpi_t a;
   uint32_t value;
-  
+
   msg_length = sizeof (struct GNUNET_SCALARPRODUCT_multipart_message);
   todo_count = session->used_element_count - session->transferred_element_count;
 
   if (todo_count > MULTIPART_ELEMENT_CAPACITY)
     // send the currently possible maximum chunk
     todo_count = MULTIPART_ELEMENT_CAPACITY;
-  
+
   msg_length += todo_count * PAILLIER_ELEMENT_LENGTH;
   msg = GNUNET_malloc (msg_length);
   msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_TO_BOB_MULTIPART);
   msg->header.size = htons (msg_length);
   msg->multipart_element_count = htonl (todo_count);
-  
+
   element_exported = GNUNET_malloc (PAILLIER_ELEMENT_LENGTH);
   a = gcry_mpi_new (KEYBITS * 2);
   current = (unsigned char *) &msg[1];
   // encrypt our vector and generate string representations
-  for (i = session->last_processed_element, j = 0; i < session->element_count; i++)
-  {
+  for (i = session->last_processed_element, j = 0; i < session->element_count; i++) {
     // is this a used element?
-    if (session->mask[i / 8] & 1 << (i % 8))
-    {
+    if (session->mask[i / 8] & 1 << (i % 8)) {
       if (todo_count <= j)
         break; //reached end of this message, can't include more
 
-      memset(element_exported, 0, PAILLIER_ELEMENT_LENGTH);
+      memset (element_exported, 0, PAILLIER_ELEMENT_LENGTH);
       value = session->vector[i] >= 0 ? session->vector[i] : -session->vector[i];
 
       a = gcry_mpi_set_ui (a, 0);
@@ -1318,9 +1279,9 @@ prepare_service_request_multipart (void *cls,
     }
   }
   gcry_mpi_release (a);
-  GNUNET_free(element_exported);
-  session->transferred_element_count+=todo_count;
-  
+  GNUNET_free (element_exported);
+  session->transferred_element_count += todo_count;
+
   session->msg = (struct GNUNET_MessageHeader *) msg;
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Transmitting service request.\n"));
 
@@ -1330,8 +1291,7 @@ prepare_service_request_multipart (void *cls,
                                                                         msg_length,
                                                                         &do_send_message,
                                                                         session);
-  if (!session->service_transmit_handle)
-  {
+  if (!session->service_transmit_handle) {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Could not send service-request multipart message to tunnel!\n"));
     GNUNET_free (msg);
     session->msg = NULL;
@@ -1340,7 +1300,7 @@ prepare_service_request_multipart (void *cls,
                                       session);
     return;
   }
-  if (session->transferred_element_count != session->used_element_count){
+  if (session->transferred_element_count != session->used_element_count) {
     session->last_processed_element = i;
   }
   else
@@ -1376,10 +1336,10 @@ prepare_service_request (void *cls,
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _ ("Successfully created new tunnel to peer (%s)!\n"), GNUNET_i2s (&session->peer));
 
   msg_length = sizeof (struct GNUNET_SCALARPRODUCT_service_request)
-          + session->mask_length
+          +session->mask_length
           + my_pubkey_external_length;
 
-  if (GNUNET_SERVER_MAX_MESSAGE_SIZE > msg_length + session->used_element_count * PAILLIER_ELEMENT_LENGTH){
+  if (GNUNET_SERVER_MAX_MESSAGE_SIZE > msg_length + session->used_element_count * PAILLIER_ELEMENT_LENGTH) {
     msg_length += session->used_element_count * PAILLIER_ELEMENT_LENGTH;
     session->transferred_element_count = session->used_element_count;
   }
@@ -1390,7 +1350,7 @@ prepare_service_request (void *cls,
 
   msg = GNUNET_malloc (msg_length);
   msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_TO_BOB);
-  msg->total_element_count = htonl(session->used_element_count);
+  msg->total_element_count = htonl (session->used_element_count);
   msg->contained_element_count = htonl (session->transferred_element_count);
   memcpy (&msg->key, &session->key, sizeof (struct GNUNET_HashCode));
   msg->mask_length = htonl (session->mask_length);
@@ -1412,15 +1372,13 @@ prepare_service_request (void *cls,
   session->a = GNUNET_malloc (sizeof (gcry_mpi_t) * session->used_element_count);
   a = gcry_mpi_new (KEYBITS * 2);
   // encrypt our vector and generate string representations
-  for (i = 0, j = 0; i < session->element_count; i++)
-  {
+  for (i = 0, j = 0; i < session->element_count; i++) {
     // if this is a used element...
-    if (session->mask[i / 8] & 1 << (i % 8))
-    {
+    if (session->mask[i / 8] & 1 << (i % 8)) {
       if (session->transferred_element_count <= j)
         break; //reached end of this message, can't include more
 
-      memset(element_exported, 0, PAILLIER_ELEMENT_LENGTH);
+      memset (element_exported, 0, PAILLIER_ELEMENT_LENGTH);
       value = session->vector[i] >= 0 ? session->vector[i] : -session->vector[i];
 
       a = gcry_mpi_set_ui (a, 0);
@@ -1450,7 +1408,7 @@ prepare_service_request (void *cls,
     }
   }
   gcry_mpi_release (a);
-  GNUNET_free(element_exported);
+  GNUNET_free (element_exported);
 
   session->msg = (struct GNUNET_MessageHeader *) msg;
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Transmitting service request.\n"));
@@ -1461,8 +1419,7 @@ prepare_service_request (void *cls,
                                                                         msg_length,
                                                                         &do_send_message,
                                                                         session);
-  if (!session->service_transmit_handle)
-  {
+  if (!session->service_transmit_handle) {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Could not send message to tunnel!\n"));
     GNUNET_free (msg);
     session->msg = NULL;
@@ -1471,7 +1428,7 @@ prepare_service_request (void *cls,
                                       session);
     return;
   }
-  if (session->transferred_element_count != session->used_element_count){
+  if (session->transferred_element_count != session->used_element_count) {
     session->state = WAITING_FOR_MULTIPART_TRANSMISSION;
     session->last_processed_element = i;
   }
@@ -1480,7 +1437,6 @@ prepare_service_request (void *cls,
     session->state = WAITING_FOR_SERVICE_RESPONSE;
 }
 
-
 /**
  * Handler for a client request message.
  * Can either be type A or B
@@ -1506,21 +1462,18 @@ handle_client_request (void *cls,
 
   // only one concurrent session per client connection allowed, simplifies logics a lot...
   session = GNUNET_SERVER_client_get_user_context (client, struct ServiceSession);
-  if ((NULL != session) && (session->state != FINALIZED))
-  {
+  if ((NULL != session) && (session->state != FINALIZED)) {
     GNUNET_SERVER_receive_done (client, GNUNET_OK);
     return;
   }
-  else if (NULL != session)
-  {
+  else if (NULL != session) {
     // old session is already completed, clean it up
     GNUNET_CONTAINER_DLL_remove (from_client_head, from_client_tail, session);
     free_session (session);
   }
 
   //we need at least a peer and one message id to compare
-  if (sizeof (struct GNUNET_SCALARPRODUCT_client_request) > ntohs (msg->header.size))
-  {
+  if (sizeof (struct GNUNET_SCALARPRODUCT_client_request) > ntohs (msg->header.size)) {
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                 _ ("Too short message received from client!\n"));
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
@@ -1533,8 +1486,7 @@ handle_client_request (void *cls,
 
   //sanity check: is the message as long as the message_count fields suggests?
   if ((ntohs (msg->header.size) != (sizeof (struct GNUNET_SCALARPRODUCT_client_request) +element_count * sizeof (int32_t) + mask_length))
-      || (0 == element_count))
-  {
+      || (0 == element_count)) {
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                 _ ("Invalid message received from client, session information incorrect!\n"));
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
@@ -1545,8 +1497,7 @@ handle_client_request (void *cls,
   if (NULL != find_matching_session (from_client_tail,
                                      &msg->key,
                                      element_count,
-                                     NULL, NULL))
-  {
+                                     NULL, NULL)) {
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                 _ ("Duplicate session information received, cannot create new session with key `%s'\n"),
                 GNUNET_h2s (&msg->key));
@@ -1566,8 +1517,7 @@ handle_client_request (void *cls,
   session->vector = GNUNET_malloc (sizeof (int32_t) * element_count);
   vector = (int32_t *) & msg[1];
 
-  if (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE == msg_type)
-  {
+  if (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE == msg_type) {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 _ ("Got client-request-session with key %s, preparing tunnel to remote service.\n"),
                 GNUNET_h2s (&session->key));
@@ -1579,8 +1529,7 @@ handle_client_request (void *cls,
 
     // copy over the elements
     session->used_element_count = 0;
-    for (i = 0; i < element_count; i++)
-    {
+    for (i = 0; i < element_count; i++) {
       session->vector[i] = ntohl (vector[i]);
       if (session->vector[i] == 0)
         session->mask[i / 8] &= ~(1 << (i % 8));
@@ -1588,8 +1537,7 @@ handle_client_request (void *cls,
         session->used_element_count++;
     }
 
-    if (0 == session->used_element_count)
-    {
+    if (0 == session->used_element_count) {
       GNUNET_break_op (0);
       GNUNET_free (session->vector);
       GNUNET_free (session);
@@ -1597,8 +1545,7 @@ handle_client_request (void *cls,
       return;
     }
     //session with ourself makes no sense!
-    if (!memcmp (&msg->peer, &me, sizeof (struct GNUNET_PeerIdentity)))
-    {
+    if (!memcmp (&msg->peer, &me, sizeof (struct GNUNET_PeerIdentity))) {
       GNUNET_break (0);
       GNUNET_free (session->vector);
       GNUNET_free (session);
@@ -1616,8 +1563,7 @@ handle_client_request (void *cls,
                                                  GNUNET_NO,
                                                  GNUNET_YES);
     //prepare_service_request, tunnel_peer_disconnect_handler,
-    if (!session->tunnel)
-    {
+    if (!session->tunnel) {
       GNUNET_break (0);
       GNUNET_free (session->vector);
       GNUNET_free (session);
@@ -1633,8 +1579,7 @@ handle_client_request (void *cls,
                                       session);
 
   }
-  else
-  {
+  else {
     struct ServiceSession * requesting_session;
     enum SessionState needed_state = SERVICE_REQUEST_RECEIVED;
 
@@ -1654,8 +1599,7 @@ handle_client_request (void *cls,
                                                 &session->key,
                                                 session->element_count,
                                                 &needed_state, NULL);
-    if (NULL != requesting_session)
-    {
+    if (NULL != requesting_session) {
       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));
       if (GNUNET_OK != compute_service_response (requesting_session, session))
         session->client_notification_task =
@@ -1663,8 +1607,7 @@ handle_client_request (void *cls,
                                         session);
 
     }
-    else
-    {
+    else {
       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));
       // no matching session exists yet, store the response
       // for later processing by handle_service_request()
@@ -1673,7 +1616,6 @@ handle_client_request (void *cls,
   GNUNET_SERVER_receive_done (client, GNUNET_YES);
 }
 
-
 /**
  * Function called for inbound tunnels.
  *
@@ -1699,7 +1641,6 @@ tunnel_incoming_handler (void *cls,
   return c;
 }
 
-
 /**
  * Function called whenever a tunnel is destroyed.  Should clean up
  * any associated state.
@@ -1724,12 +1665,10 @@ tunnel_destruction_handler (void *cls,
               _ ("Peer disconnected, terminating session %s with peer (%s)\n"),
               GNUNET_h2s (&session->key),
               GNUNET_i2s (&session->peer));
-  if (ALICE == session->role)
-  {
+  if (ALICE == session->role) {
     // as we have only one peer connected in each session, just remove the session
 
-    if ((SERVICE_RESPONSE_RECEIVED > session->state) && (!do_shutdown))
-    {
+    if ((SERVICE_RESPONSE_RECEIVED > session->state) && (!do_shutdown)) {
       session->tunnel = NULL;
       // if this happened before we received the answer, we must terminate the session
       session->client_notification_task =
@@ -1737,14 +1676,12 @@ tunnel_destruction_handler (void *cls,
                                         session);
     }
   }
-  else
-  { //(BOB == session->role) service session
+  else { //(BOB == session->role) service session
     // remove the session, unless it has already been dequeued, but somehow still active
     // this could bug without the IF in case the queue is empty and the service session was the only one know to the service
     // scenario: disconnect before alice can send her message to bob.
     for (curr = from_service_head; NULL != curr; curr = curr->next)
-      if (curr == session)
-      {
+      if (curr == session) {
         GNUNET_CONTAINER_DLL_remove (from_service_head, from_service_tail, curr);
         break;
       }
@@ -1758,8 +1695,7 @@ tunnel_destruction_handler (void *cls,
 
     // the client has to check if it was waiting for a result
     // or if it was a responder, no point in adding more statefulness
-    if (client_session && (!do_shutdown))
-    {
+    if (client_session && (!do_shutdown)) {
       client_session->state = FINALIZED;
       client_session->client_notification_task =
               GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
@@ -1768,7 +1704,6 @@ tunnel_destruction_handler (void *cls,
   }
 }
 
-
 /**
  * Compute our scalar product, done by Alice
  *
@@ -1784,8 +1719,7 @@ tunnel_destruction_handler (void *cls,
  * @return product as MPI, never NULL
  */
 static gcry_mpi_t
-compute_scalar_product (struct ServiceSession * session,
-                        gcry_mpi_t * r, gcry_mpi_t * r_prime, gcry_mpi_t s, gcry_mpi_t s_prime)
+compute_scalar_product (struct ServiceSession * session)
 {
   uint32_t count;
   gcry_mpi_t t;
@@ -1801,14 +1735,13 @@ compute_scalar_product (struct ServiceSession * session,
   // due to the introduced static offset S, we now also have to remove this
   // from the E(a_pi)(+)E(-b_pi-r_pi) and E(a_qi)(+)E(-r_qi) twice each,
   // the result is E((S + a_pi) + (S -b_pi-r_pi)) and E(S + a_qi + S - r_qi)
-  for (i = 0; i < count; i++)
-  {
-    decrypt_element (r[i], r[i], my_mu, my_lambda, my_n, my_nsquare);
-    gcry_mpi_sub (r[i], r[i], my_offset);
-    gcry_mpi_sub (r[i], r[i], my_offset);
-    decrypt_element (r_prime[i], r_prime[i], my_mu, my_lambda, my_n, my_nsquare);
-    gcry_mpi_sub (r_prime[i], r_prime[i], my_offset);
-    gcry_mpi_sub (r_prime[i], r_prime[i], my_offset);
+  for (i = 0; i < count; i++) {
+    decrypt_element (session->r[i], session->r[i], my_mu, my_lambda, my_n, my_nsquare);
+    gcry_mpi_sub (session->r[i], session->r[i], my_offset);
+    gcry_mpi_sub (session->r[i], session->r[i], my_offset);
+    decrypt_element (session->r_prime[i], session->r_prime[i], my_mu, my_lambda, my_n, my_nsquare);
+    gcry_mpi_sub (session->r_prime[i], session->r_prime[i], my_offset);
+    gcry_mpi_sub (session->r_prime[i], session->r_prime[i], my_offset);
   }
 
   // calculate t = sum(ai)
@@ -1816,28 +1749,28 @@ compute_scalar_product (struct ServiceSession * session,
 
   // calculate U
   u = gcry_mpi_new (0);
-  tmp = compute_square_sum (r, count);
+  tmp = compute_square_sum (session->r, count);
   gcry_mpi_sub (u, u, tmp);
   gcry_mpi_release (tmp);
 
   //calculate U'
   utick = gcry_mpi_new (0);
-  tmp = compute_square_sum (r_prime, count);
+  tmp = compute_square_sum (session->r_prime, count);
   gcry_mpi_sub (utick, utick, tmp);
 
   GNUNET_assert (p = gcry_mpi_new (0));
   GNUNET_assert (ptick = gcry_mpi_new (0));
 
   // compute P
-  decrypt_element (ss, my_mu, my_lambda, my_n, my_nsquare);
-  decrypt_element (s_prime, s_prime, my_mu, my_lambda, my_n, my_nsquare);
+  decrypt_element (session->s, session->s, my_mu, my_lambda, my_n, my_nsquare);
+  decrypt_element (session->s_prime, session->s_prime, my_mu, my_lambda, my_n, my_nsquare);
 
   // compute P
-  gcry_mpi_add (p, s, t);
+  gcry_mpi_add (p, session->s, t);
   gcry_mpi_add (p, p, u);
 
   // compute P'
-  gcry_mpi_add (ptick, s_prime, t);
+  gcry_mpi_add (ptick, session->s_prime, t);
   gcry_mpi_add (ptick, ptick, utick);
 
   gcry_mpi_release (t);
@@ -1859,7 +1792,6 @@ compute_scalar_product (struct ServiceSession * session,
   return p;
 }
 
-
 /**
  * prepare the response we will send to alice or bobs' clients.
  * in Bobs case the product will be NULL.
@@ -1881,19 +1813,16 @@ prepare_client_response (void *cls,
 
   session->client_notification_task = GNUNET_SCHEDULER_NO_TASK;
 
-  if (session->product)
-  {
+  if (session->product) {
     gcry_mpi_t value = gcry_mpi_new (0);
 
     sign = gcry_mpi_cmp_ui (session->product, 0);
     // libgcrypt can not handle a print of a negative number
     // if (a->sign) return gcry_error (GPG_ERR_INTERNAL); /* Can't handle it yet. */
-    if (0 > sign)
-    {
+    if (0 > sign) {
       gcry_mpi_sub (value, value, session->product);
     }
-    else if (0 < sign)
-    {
+    else if (0 < sign) {
       range = 1;
       gcry_mpi_add (value, value, session->product);
     }
@@ -1908,8 +1837,7 @@ prepare_client_response (void *cls,
         && (0 != (rc = gcry_mpi_aprint (GCRYMPI_FMT_STD,
                                         &product_exported,
                                         &product_length,
-                                        value))))
-    {
+                                        value)))) {
       LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
       product_length = 0;
       range = -1; // signal error with product-length = 0 and range = -1
@@ -1921,8 +1849,7 @@ prepare_client_response (void *cls,
   msg = GNUNET_malloc (msg_length);
   memcpy (&msg->key, &session->key, sizeof (struct GNUNET_HashCode));
   memcpy (&msg->peer, &session->peer, sizeof ( struct GNUNET_PeerIdentity));
-  if (product_exported != NULL)
-  {
+  if (product_exported != NULL) {
     memcpy (&msg[1], product_exported, product_length);
     GNUNET_free (product_exported);
   }
@@ -1939,8 +1866,7 @@ prepare_client_response (void *cls,
                                                GNUNET_TIME_UNIT_FOREVER_REL,
                                                &do_send_message,
                                                session);
-  if (NULL == session->client_transmit_handle)
-  {
+  if (NULL == session->client_transmit_handle) {
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                 _ ("Could not send message to client (%p)!\n"),
                 session->client);
@@ -1971,11 +1897,93 @@ prepare_client_response (void *cls,
  */
 static int
 handle_service_request_multipart (void *cls,
-                        struct GNUNET_MESH_Tunnel * tunnel,
-                        void **tunnel_ctx,
-                        const struct GNUNET_MessageHeader * message)
+                                  struct GNUNET_MESH_Tunnel * tunnel,
+                                  void **tunnel_ctx,
+                                  const struct GNUNET_MessageHeader * message)
 {
-    return GNUNET_SYSERR;
+  struct ServiceSession * session;
+  const struct GNUNET_SCALARPRODUCT_multipart_message * msg = (const struct GNUNET_SCALARPRODUCT_multipart_message *) message;
+  uint32_t used_elements;
+  uint32_t contained_elements=0;
+  uint32_t msg_length;
+  unsigned char * current;
+  int32_t i = -1;
+  // are we in the correct state?
+  session = (struct ServiceSession *) * tunnel_ctx;
+  if ((BOB != session->role) || (WAITING_FOR_MULTIPART_TRANSMISSION != session->state)) {
+    GNUNET_break_op (0);
+    return GNUNET_OK;
+  }
+  // shorter than minimum?
+  if (ntohs (msg->header.size) <= sizeof (struct GNUNET_SCALARPRODUCT_multipart_message)) {
+    goto except;
+  }
+  used_elements = session->used_element_count;
+  contained_elements = ntohl (msg->multipart_element_count);
+  msg_length = sizeof (struct GNUNET_SCALARPRODUCT_multipart_message)
+          + contained_elements * PAILLIER_ELEMENT_LENGTH;
+  //sanity check
+  if (( ntohs (msg->header.size) != msg_length) 
+       || (used_elements < contained_elements + session->transferred_element_count)) {
+    goto except;
+  }
+  current = (unsigned char *) &msg[1];
+  if (contained_elements != 0) {
+    gcry_error_t ret = 0;
+    // Convert each vector element to MPI_value
+    for (i = session->transferred_element_count; i < session->transferred_element_count+contained_elements; i++) {
+      size_t read = 0;
+
+      ret = gcry_mpi_scan (&session->a[i],
+                           GCRYMPI_FMT_USG,
+                           &current[i * PAILLIER_ELEMENT_LENGTH],
+                           PAILLIER_ELEMENT_LENGTH,
+                           &read);
+      if (ret) {
+        GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Could not translate E[a%d] to MPI!\n%s/%s\n"),
+                    i, gcry_strsource (ret), gcry_strerror (ret));
+        goto except;
+      }
+    }
+    session->transferred_element_count+=contained_elements;
+    
+    if (session->transferred_element_count == used_elements) {
+      // single part finished
+      session->state = SERVICE_REQUEST_RECEIVED;
+      if (session->response) {
+        GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Got session with key %s and a matching element set, processing.\n"), GNUNET_h2s (&session->key));
+        if (GNUNET_OK != compute_service_response (session, session->response)) {
+          //something went wrong, remove it again...
+          GNUNET_CONTAINER_DLL_remove (from_service_head, from_service_tail, session);
+          goto except;
+        }
+      }
+      else
+        GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Got session with key %s without a matching element set, queueing.\n"), GNUNET_h2s (&session->key));
+    }
+    else{
+      // multipart message
+    }
+  }
+  
+  return GNUNET_OK;
+except:
+  for (i = 0; i < session->transferred_element_count + contained_elements; i++)
+    if (session->a[i])
+      gcry_mpi_release (session->a[i]);
+  gcry_sexp_release (session->remote_pubkey);
+  session->remote_pubkey = NULL;
+  GNUNET_free (session->a);
+  session->a = NULL;
+  free_session (session);
+  // and notify our client-session that we could not complete the session
+  if (session->response)
+    // we just found the responder session in this queue
+    session->response->client_notification_task =
+          GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
+                                    session->response);
+  return GNUNET_SYSERR;
 }
 
 /**
@@ -2001,52 +2009,42 @@ handle_service_request (void *cls,
   uint32_t mask_length;
   uint32_t pk_length;
   uint32_t used_elements;
+  uint32_t contained_elements;
   uint32_t element_count;
   uint32_t msg_length;
   unsigned char * current;
-  struct ServiceSession * responder_session;
   int32_t i = -1;
   enum SessionState needed_state;
 
   session = (struct ServiceSession *) * tunnel_ctx;
-  if (BOB != session->role)
-  {
-    GNUNET_break_op (0);
-    return GNUNET_SYSERR;
-  }
-  // is this tunnel already in use?
-  if ((session->next) || (from_service_head == session))
-  {
+  if (WAITING_FOR_SERVICE_REQUEST != session->state) {
     GNUNET_break_op (0);
-    return GNUNET_SYSERR;
+    return GNUNET_OK;
   }
   // Check if message was sent by me, which would be bad!
-  if (!memcmp (&session->peer, &me, sizeof (struct GNUNET_PeerIdentity)))
-  {
+  if (!memcmp (&session->peer, &me, sizeof (struct GNUNET_PeerIdentity))) {
     GNUNET_free (session);
     GNUNET_break (0);
     return GNUNET_SYSERR;
   }
-
-  //we need at least a peer and one message id to compare
-  if (ntohs (msg->header.size) < sizeof (struct GNUNET_SCALARPRODUCT_service_request))
-  {
+  // shorter than expected?
+  if (ntohs (msg->header.size) < sizeof (struct GNUNET_SCALARPRODUCT_service_request)) {
     GNUNET_free (session);
     GNUNET_break_op (0);
     return GNUNET_SYSERR;
   }
   mask_length = ntohl (msg->mask_length);
   pk_length = ntohl (msg->pk_length);
-  used_elements = ntohl (msg->contained_element_count);
+  used_elements = ntohl (msg->total_element_count);
+  contained_elements = ntohl (msg->contained_element_count);
   element_count = ntohl (msg->element_count);
   msg_length = sizeof (struct GNUNET_SCALARPRODUCT_service_request)
-          +mask_length + pk_length + used_elements * PAILLIER_ELEMENT_LENGTH;
+          +mask_length + pk_length + contained_elements * PAILLIER_ELEMENT_LENGTH;
 
   //sanity check: is the message as long as the message_count fields suggests?
-  if ((ntohs (msg->header.size) != msg_length) || (element_count < used_elements)
+  if ((ntohs (msg->header.size) != msg_length) || (element_count < used_elements) || (used_elements < contained_elements)
       || (used_elements == 0) || (mask_length != (element_count / 8 + (element_count % 8 ? 1 : 0)))
-      )
-  {
+      ) {
     GNUNET_free (session);
     GNUNET_break_op (0);
     return GNUNET_SYSERR;
@@ -2055,17 +2053,16 @@ handle_service_request (void *cls,
                              &msg->key,
                              element_count,
                              NULL,
-                             NULL))
-  {
+                             NULL)) {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Got message with duplicate session key (`%s'), ignoring service request.\n"), (const char *) &(msg->key));
     GNUNET_free (session);
     return GNUNET_SYSERR;
   }
 
   memcpy (&session->peer, &session->peer, sizeof (struct GNUNET_PeerIdentity));
-  session->state = SERVICE_REQUEST_RECEIVED;
-  session->element_count = ntohl (msg->element_count);
+  session->element_count = element_count;
   session->used_element_count = used_elements;
+  session->transferred_element_count = contained_elements;
   session->tunnel = tunnel;
 
   // session key
@@ -2078,8 +2075,7 @@ handle_service_request (void *cls,
   current += mask_length;
 
   //convert the publickey to sexp
-  if (gcry_sexp_new (&session->remote_pubkey, current, pk_length, 1))
-  {
+  if (gcry_sexp_new (&session->remote_pubkey, current, pk_length, 1)) {
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Could not translate remote public key to sexpression!\n"));
     GNUNET_free (session->mask);
     GNUNET_free (session);
@@ -2090,23 +2086,17 @@ handle_service_request (void *cls,
 
   //check if service queue contains a matching request
   needed_state = CLIENT_RESPONSE_RECEIVED;
-  responder_session = find_matching_session (from_client_tail,
+  session->response = find_matching_session (from_client_tail,
                                              &session->key,
                                              session->element_count,
                                              &needed_state, NULL);
 
   session->a = GNUNET_malloc (sizeof (gcry_mpi_t) * used_elements);
-
-  if (GNUNET_SERVER_MAX_MESSAGE_SIZE >= sizeof (struct GNUNET_SCALARPRODUCT_service_request)
-      +pk_length
-      + mask_length
-      + used_elements * PAILLIER_ELEMENT_LENGTH)
-  {
+  session->state = WAITING_FOR_MULTIPART_TRANSMISSION; 
+  if (contained_elements != 0) {
     gcry_error_t ret = 0;
-    session->a = GNUNET_malloc (sizeof (gcry_mpi_t) * used_elements);
     // Convert each vector element to MPI_value
-    for (i = 0; i < used_elements; i++)
-    {
+    for (i = 0; i < contained_elements; i++) {
       size_t read = 0;
 
       ret = gcry_mpi_scan (&session->a[i],
@@ -2114,54 +2104,51 @@ handle_service_request (void *cls,
                            &current[i * PAILLIER_ELEMENT_LENGTH],
                            PAILLIER_ELEMENT_LENGTH,
                            &read);
-      if (ret)
-      {
+      if (ret) {
         GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Could not translate E[a%d] to MPI!\n%s/%s\n"),
                     i, gcry_strsource (ret), gcry_strerror (ret));
         goto except;
       }
     }
     GNUNET_CONTAINER_DLL_insert (from_service_head, from_service_tail, session);
-    if (responder_session)
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Got session with key %s and a matching element set, processing.\n"), GNUNET_h2s (&session->key));
-      if (GNUNET_OK != compute_service_response (session, responder_session))
-      {
-        //something went wrong, remove it again...
-        GNUNET_CONTAINER_DLL_remove (from_service_head, from_service_tail, session);
-        goto except;
+    
+    if (contained_elements == used_elements) {
+      // single part finished
+      session->state = SERVICE_REQUEST_RECEIVED;
+      if (session->response) {
+        GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Got session with key %s and a matching element set, processing.\n"), GNUNET_h2s (&session->key));
+        if (GNUNET_OK != compute_service_response (session, session->response)) {
+          //something went wrong, remove it again...
+          GNUNET_CONTAINER_DLL_remove (from_service_head, from_service_tail, session);
+          goto except;
+        }
       }
+      else
+        GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Got session with key %s without a matching element set, queueing.\n"), GNUNET_h2s (&session->key));
+    }
+    else{
+      // multipart message
     }
-    else
-      GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Got session with key %s without a matching element set, queueing.\n"), GNUNET_h2s (&session->key));
-
-    return GNUNET_OK;
-  }
-  else
-  {
-    // TODO FEATURE: fallback to fragmentation, in case the message is too long
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Message too large, fragmentation is currently not supported!\n"));
-    goto except;
   }
+  return GNUNET_OK;
 except:
-  for (i = 0; i < used_elements; i++)
+  for (i = 0; i < contained_elements; i++)
     if (session->a[i])
       gcry_mpi_release (session->a[i]);
   gcry_sexp_release (session->remote_pubkey);
   session->remote_pubkey = NULL;
-  GNUNET_free_non_null (session->a);
+  GNUNET_free (session->a);
   session->a = NULL;
   free_session (session);
   // and notify our client-session that we could not complete the session
-  if (responder_session)
+  if (session->response)
     // we just found the responder session in this queue
-    responder_session->client_notification_task =
+    session->response->client_notification_task =
           GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
-                                    responder_session);
+                                    session->response);
   return GNUNET_SYSERR;
 }
 
-
 /**
  * Handle a multipart chunk of a response we got from another service we wanted to calculate a scalarproduct with.
  *
@@ -2176,11 +2163,83 @@ except:
  */
 static int
 handle_service_response_multipart (void *cls,
-                        struct GNUNET_MESH_Tunnel * tunnel,
-                        void **tunnel_ctx,
-                        const struct GNUNET_MessageHeader * message)
+                                   struct GNUNET_MESH_Tunnel * tunnel,
+                                   void **tunnel_ctx,
+                                   const struct GNUNET_MessageHeader * message)
 {
-    return GNUNET_SYSERR;
+  struct ServiceSession * session;
+  const struct GNUNET_SCALARPRODUCT_multipart_message * msg = (const struct GNUNET_SCALARPRODUCT_multipart_message *) message;
+  unsigned char * current;
+  size_t read;
+  size_t i;
+  uint32_t contained_element_count=0;
+  size_t msg_size;
+  int rc;
+
+  GNUNET_assert (NULL != message);
+  // are we in the correct state?
+  session = (struct ServiceSession *) * tunnel_ctx;
+  if ((ALICE != session->role) || (WAITING_FOR_MULTIPART_TRANSMISSION != session->state)) {
+    GNUNET_break_op (0);
+    return GNUNET_OK;
+  }
+  // shorter than minimum?
+  if (ntohs (msg->header.size) <= sizeof (struct GNUNET_SCALARPRODUCT_multipart_message)) {
+    goto except;
+  }
+  contained_element_count = ntohl (msg->multipart_element_count);
+  msg_size = sizeof (struct GNUNET_SCALARPRODUCT_multipart_message)
+          + 2 * contained_element_count * PAILLIER_ELEMENT_LENGTH;
+  //sanity check: is the message as long as the message_count fields suggests?
+  if ((ntohs (msg->header.size) != msg_size) || (session->used_element_count < contained_element_count)) {
+    goto except;
+  }
+  current = (unsigned char *) &msg[1];
+  // Convert each k[][perm] to its MPI_value
+  for (i = 0; i < contained_element_count; i++) {
+    if (0 != (rc = gcry_mpi_scan (&session->r[i], GCRYMPI_FMT_USG, current,
+                                  PAILLIER_ELEMENT_LENGTH, &read))) {
+      LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc);
+      GNUNET_break_op (0);
+      goto except;
+    }
+    current += PAILLIER_ELEMENT_LENGTH;
+    if (0 != (rc = gcry_mpi_scan (&session->r_prime[i], GCRYMPI_FMT_USG, current,
+                                  PAILLIER_ELEMENT_LENGTH, &read))) {
+      LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc);
+      GNUNET_break_op (0);
+      goto except;
+    }
+    current += PAILLIER_ELEMENT_LENGTH;
+  }
+  session->transferred_element_count += contained_element_count;
+  if (session->transferred_element_count != session->used_element_count)
+    return GNUNET_OK;
+  session->state = SERVICE_RESPONSE_RECEIVED;
+  session->product = compute_scalar_product (session);
+  return GNUNET_SYSERR; // terminate the tunnel right away, we are done here!
+except:
+  GNUNET_break_op (0);
+  if (session->s)
+    gcry_mpi_release (session->s);
+  if (session->s_prime)
+    gcry_mpi_release (session->s_prime);
+  for (i = 0; session->r && i < session->transferred_element_count; i++)
+    if (session->r[i]) gcry_mpi_release (session->r[i]);
+  for (i = 0; session->r_prime && i < session->transferred_element_count; i++)
+    if (session->r_prime[i]) gcry_mpi_release (session->r_prime[i]);
+  GNUNET_free_non_null (session->r);
+  GNUNET_free_non_null (session->r_prime);
+
+  session->tunnel = NULL;
+  // send message with product to client
+  session->client_notification_task =
+          GNUNET_SCHEDULER_add_now (&prepare_client_response,
+                                    session);
+  // the tunnel has done its job, terminate our connection and the tunnel
+  // the peer will be notified that the tunnel was destroyed via tunnel_destruction_handler
+  // just close the connection, as recommended by Christian
+  return GNUNET_SYSERR;
 }
 
 /**
@@ -2204,32 +2263,20 @@ handle_service_response (void *cls,
   struct ServiceSession * session;
   const struct GNUNET_SCALARPRODUCT_service_response * msg = (const struct GNUNET_SCALARPRODUCT_service_response *) message;
   unsigned char * current;
-  uint32_t count;
-  gcry_mpi_t s = NULL;
-  gcry_mpi_t s_prime = NULL;
   size_t read;
   size_t i;
-  uint32_t contained_element_count;
+  uint32_t contained_element_count=0;
   size_t msg_size;
-  gcry_mpi_t * r = NULL;
-  gcry_mpi_t * r_prime = NULL;
   int rc;
 
   GNUNET_assert (NULL != message);
   session = (struct ServiceSession *) * tunnel_ctx;
-  if (ALICE != session->role)
-  {
+  if (session->state != WAITING_FOR_SERVICE_REQUEST) {
     GNUNET_break_op (0);
-    return GNUNET_SYSERR;
+    return GNUNET_OK;
   }
-
-  count = session->used_element_count;
-  session->product = NULL;
-  session->state = SERVICE_RESPONSE_RECEIVED;
-
-  //we need at least a peer and one message id to compare
-  if (sizeof (struct GNUNET_SCALARPRODUCT_service_response) > ntohs (msg->header.size))
-  {
+  //we need at least a full message
+  if (sizeof (struct GNUNET_SCALARPRODUCT_service_response) > ntohs (msg->header.size)) {
     GNUNET_break_op (0);
     goto invalid_msg;
   }
@@ -2238,73 +2285,66 @@ handle_service_response (void *cls,
           + 2 * contained_element_count * PAILLIER_ELEMENT_LENGTH
           + 2 * PAILLIER_ELEMENT_LENGTH;
   //sanity check: is the message as long as the message_count fields suggests?
-  if ((ntohs (msg->header.size) != msg_size) || (count != contained_element_count))
-  {
+  if ((ntohs (msg->header.size) != msg_size) || (session->used_element_count < contained_element_count)) {
     GNUNET_break_op (0);
     goto invalid_msg;
   }
-
+  session->state = WAITING_FOR_MULTIPART_TRANSMISSION;
+  session->transferred_element_count = contained_element_count;
   //convert s
   current = (unsigned char *) &msg[1];
-  if (0 != (rc = gcry_mpi_scan (&s, GCRYMPI_FMT_USG, current,
-                                PAILLIER_ELEMENT_LENGTH, &read)))
-  {
+  if (0 != (rc = gcry_mpi_scan (&session->s, GCRYMPI_FMT_USG, current,
+                                PAILLIER_ELEMENT_LENGTH, &read))) {
     LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc);
     GNUNET_break_op (0);
     goto invalid_msg;
   }
   current += PAILLIER_ELEMENT_LENGTH;
   //convert stick
-  if (0 != (rc = gcry_mpi_scan (&s_prime, GCRYMPI_FMT_USG, current,
-                                PAILLIER_ELEMENT_LENGTH, &read)))
-  {
+  if (0 != (rc = gcry_mpi_scan (&session->s_prime, GCRYMPI_FMT_USG, current,
+                                PAILLIER_ELEMENT_LENGTH, &read))) {
     LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc);
     GNUNET_break_op (0);
     goto invalid_msg;
   }
   current += PAILLIER_ELEMENT_LENGTH;
-
-  r = GNUNET_malloc (sizeof (gcry_mpi_t) * count);
-  // Convert each kp[] to its MPI_value
-  for (i = 0; i < count; i++)
-  {
-    if (0 != (rc = gcry_mpi_scan (&r[i], GCRYMPI_FMT_USG, current,
-                                  PAILLIER_ELEMENT_LENGTH, &read)))
-    {
+  session->r = GNUNET_malloc (sizeof (gcry_mpi_t) * session->used_element_count);
+  session->r_prime = GNUNET_malloc (sizeof (gcry_mpi_t) * session->used_element_count);
+  // Convert each k[][perm] to its MPI_value
+  for (i = 0; i < contained_element_count; i++) {
+    if (0 != (rc = gcry_mpi_scan (&session->r[i], GCRYMPI_FMT_USG, current,
+                                  PAILLIER_ELEMENT_LENGTH, &read))) {
       LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc);
       GNUNET_break_op (0);
       goto invalid_msg;
     }
     current += PAILLIER_ELEMENT_LENGTH;
-  }
-
-
-  r_prime = GNUNET_malloc (sizeof (gcry_mpi_t) * count);
-  // Convert each kq[] to its MPI_value
-  for (i = 0; i < count; i++)
-  {
-    if (0 != (rc = gcry_mpi_scan (&r_prime[i], GCRYMPI_FMT_USG, current,
-                                  PAILLIER_ELEMENT_LENGTH, &read)))
-    {
+    if (0 != (rc = gcry_mpi_scan (&session->r_prime[i], GCRYMPI_FMT_USG, current,
+                                  PAILLIER_ELEMENT_LENGTH, &read))) {
       LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc);
       GNUNET_break_op (0);
       goto invalid_msg;
     }
     current += PAILLIER_ELEMENT_LENGTH;
   }
-  session->product = compute_scalar_product (session, r, r_prime, s, s_prime);
+  if (session->transferred_element_count != session->used_element_count)
+    return GNUNET_OK; //wait for the other multipart chunks
+  
+  session->state = SERVICE_RESPONSE_RECEIVED;
+  session->product = compute_scalar_product (session);
+  return GNUNET_SYSERR; // terminate the tunnel right away, we are done here!
 
 invalid_msg:
-  if (s)
-    gcry_mpi_release (s);
-  if (s_prime)
-    gcry_mpi_release (s_prime);
-  for (i = 0; r && i < count; i++)
-    if (r[i]) gcry_mpi_release (r[i]);
-  for (i = 0; r_prime && i < count; i++)
-    if (r_prime[i]) gcry_mpi_release (r_prime[i]);
-  GNUNET_free_non_null (r);
-  GNUNET_free_non_null (r_prime);
+  if (session->s)
+    gcry_mpi_release (session->s);
+  if (session->s_prime)
+    gcry_mpi_release (session->s_prime);
+  for (i = 0; session->r && i < contained_element_count; i++)
+    if (session->r[i]) gcry_mpi_release (session->r[i]);
+  for (i = 0; session->r_prime && i < contained_element_count; i++)
+    if (session->r_prime[i]) gcry_mpi_release (session->r_prime[i]);
+  GNUNET_free_non_null (session->r);
+  GNUNET_free_non_null (session->r_prime);
 
   session->tunnel = NULL;
   // send message with product to client
@@ -2317,7 +2357,6 @@ invalid_msg:
   return GNUNET_SYSERR;
 }
 
-
 /**
  * Task run during shutdown.
  *
@@ -2334,42 +2373,36 @@ shutdown_task (void *cls,
   do_shutdown = GNUNET_YES;
 
   // terminate all owned open tunnels.
-  for (session = from_client_head; NULL != session; session = session->next)
-  {
-    if ((FINALIZED != session->state) && (NULL != session->tunnel)){
+  for (session = from_client_head; NULL != session; session = session->next) {
+    if ((FINALIZED != session->state) && (NULL != session->tunnel)) {
       GNUNET_MESH_tunnel_destroy (session->tunnel);
       session->tunnel = NULL;
     }
-    if (GNUNET_SCHEDULER_NO_TASK != session->client_notification_task)
-    {
+    if (GNUNET_SCHEDULER_NO_TASK != session->client_notification_task) {
       GNUNET_SCHEDULER_cancel (session->client_notification_task);
       session->client_notification_task = GNUNET_SCHEDULER_NO_TASK;
     }
-    if (GNUNET_SCHEDULER_NO_TASK != session->service_request_task)
-    {
+    if (GNUNET_SCHEDULER_NO_TASK != session->service_request_task) {
       GNUNET_SCHEDULER_cancel (session->service_request_task);
       session->service_request_task = GNUNET_SCHEDULER_NO_TASK;
     }
-    if (NULL != session->client)
-    {
+    if (NULL != session->client) {
       GNUNET_SERVER_client_disconnect (session->client);
       session->client = NULL;
     }
   }
   for (session = from_service_head; NULL != session; session = session->next)
-    if (NULL != session->tunnel){
+    if (NULL != session->tunnel) {
       GNUNET_MESH_tunnel_destroy (session->tunnel);
-    session->tunnel = NULL;
+      session->tunnel = NULL;
     }
 
-  if (my_mesh)
-  {
+  if (my_mesh) {
     GNUNET_MESH_disconnect (my_mesh);
     my_mesh = NULL;
   }
 }
 
-
 /**
  * Initialization of the program and message handlers
  *
@@ -2413,8 +2446,7 @@ run (void *cls,
                                  &tunnel_incoming_handler,
                                  &tunnel_destruction_handler,
                                  mesh_handlers, ports);
-  if (!my_mesh)
-  {
+  if (!my_mesh) {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Connect to MESH failed\n"));
     GNUNET_SCHEDULER_shutdown ();
     return;
@@ -2425,7 +2457,6 @@ run (void *cls,
                                 NULL);
 }
 
-
 /**
  * The main function for the scalarproduct service.
  *