- more partially replacing of pailier-encryption functionality in scalarproduct with...
[oweals/gnunet.git] / src / scalarproduct / gnunet-service-scalarproduct.c
index f856678741c9603531dcedcd3f7598ea7a6876ca..0633656110f692bd6adc3583edd48822b3865b9c 100644 (file)
 //                     Service Structure Definitions
 ///////////////////////////////////////////////////////////////////////////////
 
+
 /**
  * state a session can be in
  */
 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
 };
 
+
 /**
  * role a peer in a session can assume
  */
 enum PeerRole
 {
- ALICE,
- BOB
 ALICE,
 BOB
 };
 
+
 /**
  * A scalarproduct session which tracks:
  *
@@ -73,148 +76,174 @@ 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 total;
-
- /**
-  * how many elements actually are used after applying the mask
-  */
- uint32_t used;
-
- /**
-  * already transferred elements (sent/received) for multipart messages, less or equal than used_element_count for
-  */
- uint32_t transferred;
-
- /**
-  * index of the last transferred element for multipart messages
-  */
- uint32_t last_processed;
-
- /**
-  * 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;
-
- /**
-  * Handle to a task that sends a msg to the our client
-  */
- GNUNET_SCHEDULER_TaskIdentifier client_notification_task;
-
- /**
-  * Handle to a task that sends a msg to the our peer
-  */
- 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 total;
+
 /**
+   * how many elements actually are used after applying the mask
+   */
 uint32_t used;
+
 /**
+   * already transferred elements (sent/received) for multipart messages, less or equal than used_element_count for
+   */
 uint32_t transferred;
+
 /**
+   * index of the last transferred element for multipart messages
+   */
 uint32_t last_processed;
+
 /**
+   * 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;
+
 /**
+   * channel-handle associated with our mesh handle
+   */
 struct GNUNET_MESH_Channel * channel;
+
 /**
+   * Handle to a task that sends a msg to the our client
+   */
 GNUNET_SCHEDULER_TaskIdentifier client_notification_task;
+
 /**
+   * Handle to a task that sends a msg to the our peer
+   */
 GNUNET_SCHEDULER_TaskIdentifier service_request_task;
 };
 
+///////////////////////////////////////////////////////////////////////////////
+//                      Forward Delcarations
+///////////////////////////////////////////////////////////////////////////////
+
+/**
+ * Send a multi part chunk of a service request from alice to bob.
+ * This element only contains a part of the elements-vector (session->a[]),
+ * mask and public key set have to be contained within the first message
+ *
+ * This allows a ~32kbit key length while using 32000 elements or 62000 elements per request.
+ *
+ * @param cls the associated service session
+ */
+static void
+prepare_service_request_multipart (void *cls);
+
+/**
+ * Send a multi part chunk of a service response from bob to alice.
+ * This element only contains the two permutations of R, R'.
+ *
+ * @param cls the associated service session
+ */
+static void
+prepare_service_response_multipart (void *cls);
+
+
 ///////////////////////////////////////////////////////////////////////////////
 //                      Global Variables
 ///////////////////////////////////////////////////////////////////////////////
@@ -306,6 +335,7 @@ static int do_shutdown;
 //                      Helper Functions
 ///////////////////////////////////////////////////////////////////////////////
 
+
 /**
  * Generates an Paillier private/public keyset and extracts the values using libgrcypt only
  */
@@ -354,7 +384,7 @@ generate_keyset ()
   my_lambda = gcry_mpi_new (0);
 
   // calculate lambda
-  // lambda = \frac{(p-1)*(q-1)}{gcd(p-1,q-1)}
+  // lambda = frac{(p-1)*(q-1)}{gcd(p-1,q-1)}
   gcry_mpi_sub_ui (tmp1, p, 1);
   gcry_mpi_sub_ui (tmp2, q, 1);
   gcry_mpi_gcd (gcd, tmp1, tmp2);
@@ -420,6 +450,7 @@ 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
@@ -438,33 +469,6 @@ adjust (unsigned char *buf, size_t size, size_t target)
   }
 }
 
-/**
- * encrypts an element using the paillier crypto system
- *
- * @param c ciphertext (output)
- * @param m plaintext
- * @param g the public base
- * @param n the module from which which r is chosen (Z*_n)
- * @param n_square the module for encryption, for performance reasons.
- */
-static void
-encrypt_element (gcry_mpi_t c, gcry_mpi_t m, gcry_mpi_t g, gcry_mpi_t n, gcry_mpi_t n_square)
-{
-  gcry_mpi_t tmp;
-
-  GNUNET_assert (tmp = gcry_mpi_new (0));
-
-  while (0 >= gcry_mpi_cmp_ui (tmp, 1)) {
-    gcry_mpi_randomize (tmp, KEYBITS / 3, GCRY_WEAK_RANDOM);
-    // r must be 1 < r < n
-  }
-
-  gcry_mpi_powm (c, g, m, n_square);
-  gcry_mpi_powm (tmp, tmp, n, n_square);
-  gcry_mpi_mulm (c, tmp, c, n_square);
-
-  gcry_mpi_release (tmp);
-}
 
 /**
  * decrypts an element using the paillier crypto system
@@ -485,6 +489,7 @@ 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.
  *
@@ -513,14 +518,6 @@ compute_square_sum (gcry_mpi_t * vector, uint32_t length)
 }
 
 
-static void
-prepare_service_request_multipart (void *cls,
-                                   const struct GNUNET_SCHEDULER_TaskContext *tc);
-static void
-prepare_service_response_multipart (void *cls,
-                                    const struct GNUNET_SCHEDULER_TaskContext *tc);
-
-
 /**
  * Primitive callback for copying over a message, as they
  * usually are too complex to be handled in the callback itself.
@@ -539,12 +536,11 @@ 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) {
     GNUNET_break (0);
     return 0;
   }
-  
+
   type = ntohs (session->msg->type);
   memcpy (buf, session->msg, size);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -559,29 +555,29 @@ do_send_message (void *cls, size_t size, void *buf)
     session->state = FINALIZED;
     session->client_transmit_handle = NULL;
     break;
+
   case GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_TO_BOB:
   case GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_TO_BOB_MULTIPART:
-    //else
     session->service_transmit_handle = NULL;
-    // reset flags for sending
-    if ((session->state != WAITING_FOR_MULTIPART_TRANSMISSION) && (session->used != session->transferred))
-      prepare_service_request_multipart (session, NULL);
-    //TODO we have sent a message and now need to trigger trigger the next multipart message sending
+    if (session->state == WAITING_FOR_MULTIPART_TRANSMISSION)
+      prepare_service_request_multipart (session);
     break;
+
   case GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_TO_ALICE:
   case GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_TO_ALICE_MULTIPART:
-    //else
     session->service_transmit_handle = NULL;
-    if ((session->state != WAITING_FOR_MULTIPART_TRANSMISSION) && (session->used != session->transferred))
-      prepare_service_response_multipart (session, NULL);
+    if (session->state == WAITING_FOR_MULTIPART_TRANSMISSION)
+      prepare_service_response_multipart (session);
     break;
+
   default:
-    session->service_transmit_handle = NULL;
+    GNUNET_assert (0);
   }
 
   return size;
 }
 
+
 /**
  * initializes a new vector with fresh MPI values (=0) of a given length
  *
@@ -599,6 +595,7 @@ initialize_mpi_vector (uint32_t length)
   return output;
 }
 
+
 /**
  * permutes an MPI vector according to the given permutation vector
  *
@@ -627,43 +624,17 @@ permute_vector (gcry_mpi_t * vector,
   return vector;
 }
 
-/**
- * Populate a vector with random integer values and convert them to
- *
- * @param length the length of the vector we must generate
- * @return an array of MPI values with random values
- */
-static gcry_mpi_t *
-generate_random_vector (uint32_t length)
-{
-  gcry_mpi_t * random_vector;
-  int32_t value;
-  uint32_t i;
-
-  random_vector = initialize_mpi_vector (length);
-  for (i = 0; i < length; i++) {
-    value = (int32_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
-
-    // long to gcry_mpi_t
-    if (value < 0)
-      gcry_mpi_sub_ui (random_vector[i],
-                       random_vector[i],
-                       -value);
-    else
-      random_vector[i] = gcry_mpi_set_ui (random_vector[i], value);
-  }
-
-  return random_vector;
-}
 
 /**
  * Finds a not terminated client/service session in the
  * given DLL based on session key, element count and state.
  *
  * @param tail - the tail of the DLL
- * @param my - the session to compare it to
- * @return a pointer to a matching session,
- *         else NULL
+ * @param key - the key we want to search for
+ * @param element_count - the total element count of the dataset (session->total)
+ * @param state - a pointer to the state the session should be in, NULL to ignore
+ * @param peerid - a pointer to the peer ID of the associated peer, NULL to ignore
+ * @return a pointer to a matching session, or NULL
  */
 static struct ServiceSession *
 find_matching_session (struct ServiceSession * tail,
@@ -692,47 +663,72 @@ find_matching_session (struct ServiceSession * tail,
   return NULL;
 }
 
+/**
+ * Safely frees ALL memory areas referenced by a session.
+ *
+ * @param session - the session to free elements from
+ */
 static void
 free_session_variables (struct ServiceSession * session)
 {
   unsigned int i;
 
-  if (session->a){
+  if (session->a) {
     for (i = 0; i < session->used; i++)
       if (session->a[i]) gcry_mpi_release (session->a[i]);
     GNUNET_free (session->a);
+    session->a = NULL;
+  }
+  if (session->mask) {
+    GNUNET_free (session->mask);
+    session->mask = NULL;
   }
-  GNUNET_free_non_null (session->mask);
-  if (session->r){
+  if (session->r) {
     for (i = 0; i < session->used; i++)
       if (session->r[i]) gcry_mpi_release (session->r[i]);
-    GNUNET_free(session->r);
+    GNUNET_free (session->r);
+    session->r = NULL;
   }
-  if (session->r_prime){
+  if (session->r_prime) {
     for (i = 0; i < session->used; i++)
       if (session->r_prime[i]) gcry_mpi_release (session->r_prime[i]);
-    GNUNET_free(session->r_prime);
+    GNUNET_free (session->r_prime);
+    session->r_prime = NULL;
   }
-  if (session->s)
+  if (session->s) {
     gcry_mpi_release (session->s);
-  if (session->s_prime)
+    session->s = NULL;
+  }
+
+  if (session->s_prime) {
     gcry_mpi_release (session->s_prime);
-  if (session->product)
+    session->s_prime = NULL;
+  }
+
+  if (session->product) {
     gcry_mpi_release (session->product);
+    session->product = NULL;
+  }
 
-  if (session->remote_pubkey)
+  if (session->remote_pubkey) {
     gcry_sexp_release (session->remote_pubkey);
+    session->remote_pubkey = NULL;
+  }
 
-  GNUNET_free_non_null (session->vector);
+  if (session->vector) {
+    GNUNET_free_non_null (session->vector);
+    session->s = NULL;
+  }
 }
 ///////////////////////////////////////////////////////////////////////////////
 //                      Event and Message Handlers
 ///////////////////////////////////////////////////////////////////////////////
 
+
 /**
  * A client disconnected.
  *
- * Remove the associated session(s), release datastructures
+ * Remove the associated session(s), release data structures
  * and cancel pending outgoing transmissions to the client.
  * if the session has not yet completed, we also cancel Alice's request to Bob.
  *
@@ -745,21 +741,23 @@ handle_client_disconnect (void *cls,
 {
   struct ServiceSession *session;
 
-  if (client == NULL)
+  if (NULL != client)
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              _ ("Client (%p) disconnected from us.\n"), client);
+  else
     return;
+
   session = GNUNET_SERVER_client_get_user_context (client, struct ServiceSession);
   if (NULL == session)
     return;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              _ ("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)) {
     //we MUST terminate any client message underway
-    if (session->service_transmit_handle && session->tunnel)
+    if (session->service_transmit_handle && session->channel)
       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 (session->channel && session->state == WAITING_FOR_SERVICE_RESPONSE)
+      GNUNET_MESH_channel_destroy (session->channel);
   }
   if (GNUNET_SCHEDULER_NO_TASK != session->client_notification_task) {
     GNUNET_SCHEDULER_cancel (session->client_notification_task);
@@ -777,15 +775,15 @@ handle_client_disconnect (void *cls,
   GNUNET_free (session);
 }
 
+
 /**
  * Notify the client that the session has succeeded or failed completely.
  * This message gets sent to
  * * alice's client if bob disconnected or to
  * * bob's client if the operation completed or alice disconnected
  *
- * @param client_session the associated client session
- * @return GNUNET_NO, if we could not notify the client
- *         GNUNET_YES if we notified it.
+ * @param cls the associated client session
+ * @param tc the task context handed to us by the scheduler, unused
  */
 static void
 prepare_client_end_notification (void * cls,
@@ -825,11 +823,113 @@ prepare_client_end_notification (void * cls,
   else
     GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Sending session-end notification to client (%p) for session %s\n"), &session->client, GNUNET_h2s (&session->key));
 
+  free_session_variables (session);
 }
 
+
+/**
+ * prepare the response we will send to alice or bobs' clients.
+ * in Bobs case the product will be NULL.
+ *
+ * @param cls the session associated with our client.
+ * @param tc the task context handed to us by the scheduler, unused
+ */
 static void
-prepare_service_response_multipart (void *cls,
-                                    const struct GNUNET_SCHEDULER_TaskContext *tc)
+prepare_client_response (void *cls,
+                         const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  struct ServiceSession * session = cls;
+  struct GNUNET_SCALARPRODUCT_client_response * msg;
+  unsigned char * product_exported = NULL;
+  size_t product_length = 0;
+  uint32_t msg_length = 0;
+  int8_t range = -1;
+  gcry_error_t rc;
+  int sign;
+
+  session->client_notification_task = GNUNET_SCHEDULER_NO_TASK;
+
+  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) {
+      gcry_mpi_sub (value, value, session->product);
+    }
+    else if (0 < sign) {
+      range = 1;
+      gcry_mpi_add (value, value, session->product);
+    }
+    else
+      range = 0;
+
+    gcry_mpi_release (session->product);
+    session->product = NULL;
+
+    // get representation as string
+    if (range
+        && (0 != (rc = gcry_mpi_aprint (GCRYMPI_FMT_STD,
+                                        &product_exported,
+                                        &product_length,
+                                        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
+    }
+    gcry_mpi_release (value);
+  }
+
+  msg_length = sizeof (struct GNUNET_SCALARPRODUCT_client_response) +product_length;
+  msg = GNUNET_malloc (msg_length);
+  msg->key = session->key;
+  msg->peer = session->peer;
+  if (product_exported != NULL)
+  {
+    memcpy (&msg[1], product_exported, product_length);
+    GNUNET_free (product_exported);
+  }
+  msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_SERVICE_TO_CLIENT);
+  msg->header.size = htons (msg_length);
+  msg->range = range;
+  msg->product_length = htonl (product_length);
+
+  session->msg = (struct GNUNET_MessageHeader *) msg;
+  //transmit this message to our client
+  session->client_transmit_handle =
+          GNUNET_SERVER_notify_transmit_ready (session->client,
+                                               msg_length,
+                                               GNUNET_TIME_UNIT_FOREVER_REL,
+                                               &do_send_message,
+                                               session);
+  if (NULL == session->client_transmit_handle) {
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                _ ("Could not send message to client (%p)!\n"),
+                session->client);
+    session->client = NULL;
+    // callback was not called!
+    GNUNET_free (msg);
+    session->msg = NULL;
+  }
+  else
+    // gracefully sent message, just terminate session structure
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                _ ("Sent result to client (%p), this session (%s) has ended!\n"),
+                session->client,
+                GNUNET_h2s (&session->key));
+  free_session_variables (session);
+}
+
+
+/**
+ * Send a multipart chunk of a service response from bob to alice.
+ * This element only contains the two permutations of R, R'.
+ *
+ * @param cls the associated service session
+ */
+static void
+prepare_service_response_multipart (void *cls)
 {
   struct ServiceSession * session = cls;
   unsigned char * current;
@@ -879,12 +979,14 @@ prepare_service_response_multipart (void *cls,
   GNUNET_free (element_exported);
   for (i = session->transferred; i < session->transferred; i++) {
     gcry_mpi_release (session->r_prime[i]);
+    session->r_prime[i] = NULL;
     gcry_mpi_release (session->r[i]);
+    session->r[i] = NULL;
   }
   session->transferred += todo_count;
   session->msg = (struct GNUNET_MessageHeader *) msg;
   session->service_transmit_handle =
-          GNUNET_MESH_notify_transmit_ready (session->tunnel,
+          GNUNET_MESH_notify_transmit_ready (session->channel,
                                              GNUNET_YES,
                                              GNUNET_TIME_UNIT_FOREVER_REL,
                                              msg_length,
@@ -901,13 +1003,19 @@ prepare_service_response_multipart (void *cls,
     return;
   }
   if (session->transferred != session->used)
-    // multipart
+    // more multiparts
     session->state = WAITING_FOR_MULTIPART_TRANSMISSION;
-  else
-    //singlepart
+  else{
+    // final part
     session->state = FINALIZED;
+    GNUNET_free(session->r);
+    GNUNET_free(session->r_prime);
+    session->r_prime = NULL;
+    session->r = NULL;
+  }
 }
 
+
 /**
  * Bob executes:
  * generates the response message to be sent to alice after computing
@@ -920,8 +1028,8 @@ prepare_service_response_multipart (void *cls,
  * @param s         S: $S := E_A(sum (r_i + b_i)^2)$
  * @param s_prime    S': $S' := E_A(sum r_i^2)$
  * @param session  the associated requesting session with alice
- * @return GNUNET_NO if we could not send our message
- *         GNUNET_OK if the operation succeeded
+ * @return #GNUNET_NO if we could not send our message
+ *         #GNUNET_OK if the operation succeeded
  */
 static int
 prepare_service_response (gcry_mpi_t s,
@@ -951,7 +1059,7 @@ prepare_service_response (gcry_mpi_t s,
   msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_TO_ALICE);
   msg->header.size = htons (msg_length);
   msg->total_element_count = htonl (session->total);
-  msg->contained_element_count = htonl (session->used);
+  msg->used_element_count = htonl (session->used);
   msg->contained_element_count = htonl (session->transferred);
   memcpy (&msg->key, &session->key, sizeof (struct GNUNET_HashCode));
   current = (unsigned char *) &msg[1];
@@ -1005,14 +1113,18 @@ prepare_service_response (gcry_mpi_t s,
   GNUNET_free (element_exported);
   for (i = 0; i < session->transferred; i++) {
     gcry_mpi_release (session->r_prime[i]);
+    session->r_prime[i] = NULL;
     gcry_mpi_release (session->r[i]);
+    session->r[i] = NULL;
   }
   gcry_mpi_release (s);
+  session->s = NULL;
   gcry_mpi_release (s_prime);
+  session->s_prime = NULL;
 
   session->msg = (struct GNUNET_MessageHeader *) msg;
   session->service_transmit_handle =
-          GNUNET_MESH_notify_transmit_ready (session->tunnel,
+          GNUNET_MESH_notify_transmit_ready (session->channel,
                                              GNUNET_YES,
                                              GNUNET_TIME_UNIT_FOREVER_REL,
                                              msg_length,
@@ -1031,20 +1143,26 @@ prepare_service_response (gcry_mpi_t s,
   if (session->transferred != session->used)
     // multipart
     session->state = WAITING_FOR_MULTIPART_TRANSMISSION;
-  else
+  else{
     //singlepart
     session->state = FINALIZED;
+    GNUNET_free(session->r);
+    GNUNET_free(session->r_prime);
+    session->r_prime = NULL;
+    session->r = NULL;
+  }
 
   return GNUNET_OK;
 }
 
+
 /**
  * executed by bob:
  * compute the values
- *  (1)[]: $E_A(a_{\pi(i)}) \otimes E_A(- r_{\pi(i)} - b_{\pi(i)}) &= E_A(a_{\pi(i)} - r_{\pi(i)} - b_{\pi(i)})$
- *  (2)[]: $E_A(a_{\pi'(i)}) \otimes E_A(- r_{\pi'(i)}) &= E_A(a_{\pi'(i)} - r_{\pi'(i)})$
- *      S: $S := E_A(\sum (r_i + b_i)^2)$
- *     S': $S' := E_A(\sum r_i^2)$
+ *  (1)[]: $E_A(a_{pi(i)}) otimes E_A(- r_{pi(i)} - b_{pi(i)}) &= E_A(a_{pi(i)} - r_{pi(i)} - b_{pi(i)})$
+ *  (2)[]: $E_A(a_{pi'(i)}) otimes E_A(- r_{pi'(i)}) &= E_A(a_{pi'(i)} - r_{pi'(i)})$
+ *      S: $S := E_A(sum (r_i + b_i)^2)$
+ *     S': $S' := E_A(sum r_i^2)$
  *
  * @param request the requesting session + bob's requesting peer
  * @param response the responding session + bob's client handle
@@ -1063,7 +1181,9 @@ compute_service_response (struct ServiceSession * request,
   uint32_t count;
   gcry_mpi_t * rand = NULL;
   gcry_mpi_t * r = NULL;
+  struct GNUNET_CRYPTO_PaillierCiphertext * R;
   gcry_mpi_t * r_prime = NULL;
+  struct GNUNET_CRYPTO_PaillierCiphertext * R_prime;
   gcry_mpi_t * b;
   gcry_mpi_t * a_pi;
   gcry_mpi_t * a_pi_prime;
@@ -1071,11 +1191,10 @@ compute_service_response (struct ServiceSession * request,
   gcry_mpi_t * rand_pi;
   gcry_mpi_t * rand_pi_prime;
   gcry_mpi_t s = NULL;
+  struct GNUNET_CRYPTO_PaillierCiphertext * S;
   gcry_mpi_t s_prime = NULL;
-  gcry_mpi_t remote_n = NULL;
-  gcry_mpi_t remote_nsquare;
-  gcry_mpi_t remote_g = NULL;
-  gcry_sexp_t tmp_exp;
+  struct GNUNET_CRYPTO_PaillierCiphertext * S_prime;
+  
   uint32_t value;
 
   count = request->used;
@@ -1088,11 +1207,14 @@ 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->total && j < count; i++) {
-    if (request->mask[i / 8] & (1 << (i % 8))) {
+  for (i = 0, j = 0; i < response->total && 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);
       }
@@ -1104,42 +1226,25 @@ compute_service_response (struct ServiceSession * request,
   }
   GNUNET_free (response->vector);
   response->vector = NULL;
-
-  tmp_exp = gcry_sexp_find_token (request->remote_pubkey, "n", 0);
-  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) {
-    GNUNET_break (0);
-    gcry_sexp_release (tmp_exp);
-    goto except;
-  }
-  remote_nsquare = gcry_mpi_new (KEYBITS + 1);
-  gcry_mpi_mul (remote_nsquare, remote_n, remote_n);
-  gcry_sexp_release (tmp_exp);
-  tmp_exp = gcry_sexp_find_token (request->remote_pubkey, "g", 0);
-  gcry_sexp_release (request->remote_pubkey);
-  request->remote_pubkey = NULL;
-  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) {
-    GNUNET_break (0);
-    gcry_mpi_release (remote_n);
-    gcry_sexp_release (tmp_exp);
-    goto except;
-  }
-  gcry_sexp_release (tmp_exp);
+  q = NULL;
+  p = NULL;
 
   // generate r, p and q
-  rand = generate_random_vector (count);
+  rand = initialize_mpi_vector (count);
+  for (i = 0; i < count; i++)
+  {
+    int32_t svalue;
+
+    svalue = (int32_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
+
+    // long to gcry_mpi_t
+    if (svalue < 0)
+      gcry_mpi_sub_ui (rand[i],
+                       rand[i],
+                       -svalue);
+    else
+      rand[i] = gcry_mpi_set_ui (rand[i], svalue);
+  }
   p = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, count);
   q = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, count);
   //initialize the result vectors
@@ -1153,8 +1258,11 @@ compute_service_response (struct ServiceSession * request,
   memcpy (b_pi, b, sizeof (gcry_mpi_t) * count);
   memcpy (rand_pi, rand, sizeof (gcry_mpi_t) * count);
   memcpy (rand_pi_prime, rand, sizeof (gcry_mpi_t) * count);
+  
+  //todo get API-cryptoblocks, instead of MPI values
 
   // generate p and q permutations for a, b and r
+  // TODO: APIify
   GNUNET_assert (permute_vector (a_pi, p, count));
   GNUNET_assert (permute_vector (b_pi, p, count));
   GNUNET_assert (permute_vector (rand_pi, p, count));
@@ -1166,27 +1274,39 @@ 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]);
-    encrypt_element (r[i], r[i], remote_g, remote_n, remote_nsquare);
-
+    GNUNET_CRYPTO_paillier_encrypt (&request->remote_pubkey, 
+                                    r[i], 
+                                    &R[i]);
+    
     // E(S - r_pi - b_pi) * E(S + a_pi) ==  E(2*S + a - r - b)
-    gcry_mpi_mulm (r[i], r[i], a_pi[i], remote_nsquare);
+    GNUNET_CRYPTO_paillier_hom_add (&request->remote_pubkey, 
+                                    &R[i], 
+                                    &A_pi[i], 
+                                    &R[i]);
   }
   GNUNET_free (a_pi);
   GNUNET_free (b_pi);
   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);
+    GNUNET_CRYPTO_paillier_encrypt (&request->remote_pubkey, 
+                                    r_prime[i], 
+                                    &R_prime[i]);
 
     // E(S - r_qi) * E(S + a_qi) == E(2*S + a_qi - r_qi)
-    gcry_mpi_mulm (r_prime[i], r_prime[i], a_pi_prime[i], remote_nsquare);
+    GNUNET_CRYPTO_paillier_hom_add (&request->remote_pubkey, 
+                                    &R_prime[i], 
+                                    &A_pi_prime[i], 
+                                    &R_prime[i]);
   }
   GNUNET_free (a_pi_prime);
   GNUNET_free (rand_pi_prime);
@@ -1197,17 +1317,18 @@ compute_service_response (struct ServiceSession * request,
 
   // Calculate S' =  E(SUM( r_i^2 ))
   s_prime = compute_square_sum (rand, count);
-  encrypt_element (s_prime, s_prime, remote_g, remote_n, remote_nsquare);
+  GNUNET_CRYPTO_paillier_encrypt (&request->remote_pubkey, 
+                                  s_prime, 
+                                  &S_prime);
 
   // Calculate S = E(SUM( (r_i + b_i)^2 ))
   for (i = 0; i < count; i++) {
     gcry_mpi_add (rand[i], rand[i], b[i]);
   }
   s = compute_square_sum (rand, count);
-  encrypt_element (s, s, remote_g, remote_n, remote_nsquare);
-  gcry_mpi_release (remote_n);
-  gcry_mpi_release (remote_g);
-  gcry_mpi_release (remote_nsquare);
+  GNUNET_CRYPTO_paillier_encrypt (&request->remote_pubkey, 
+                                  s[i], 
+                                  &S);
 
   // release r and tmp
   for (i = 0; i < count; i++)
@@ -1215,14 +1336,15 @@ compute_service_response (struct ServiceSession * request,
     gcry_mpi_release (rand[i]);
 
   // copy the r[], r_prime[], S and Stick into a new message, prepare_service_response frees these
-  if (GNUNET_YES != prepare_service_response (s, s_prime, request))
+  if (GNUNET_YES != prepare_service_response (S, S_prime, request))
     GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Failed to communicate with `%s', scalar product calculation aborted.\n"),
                 GNUNET_i2s (&request->peer));
   else
     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]);
   }
@@ -1230,13 +1352,24 @@ except:
   GNUNET_free (b);
   GNUNET_free (request->a);
   request->a = NULL;
-
+  GNUNET_free_non_null (p);
+  GNUNET_free_non_null (q);
+  GNUNET_free (rand);
   return ret;
 }
 
+
+/**
+ * Send a multi part chunk of a service request from alice to bob.
+ * This element only contains a part of the elements-vector (session->a[]),
+ * mask and public key set have to be contained within the first message
+ *
+ * This allows a ~32kbit key length while using 32000 elements or 62000 elements per request.
+ *
+ * @param cls the associated service session
+ */
 static void
-prepare_service_request_multipart (void *cls,
-                                   const struct GNUNET_SCHEDULER_TaskContext *tc)
+prepare_service_request_multipart (void *cls)
 {
   struct ServiceSession * session = cls;
   unsigned char * current;
@@ -1267,7 +1400,8 @@ prepare_service_request_multipart (void *cls,
   a = gcry_mpi_new (KEYBITS * 2);
   current = (unsigned char *) &msg[1];
   // encrypt our vector and generate string representations
-  for (i = session->last_processed, j = 0; i < session->total; i++) {
+  for (i = session->last_processed, j = 0; i < session->total; i++)
+  {
     // is this a used element?
     if (session->mask[i / 8] & 1 << (i % 8)) {
       if (todo_count <= j)
@@ -1310,13 +1444,13 @@ prepare_service_request_multipart (void *cls,
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Transmitting service request.\n"));
 
   //transmit via mesh messaging
-  session->service_transmit_handle = GNUNET_MESH_notify_transmit_ready (session->tunnel, GNUNET_YES,
+  session->service_transmit_handle = GNUNET_MESH_notify_transmit_ready (session->channel, GNUNET_YES,
                                                                         GNUNET_TIME_UNIT_FOREVER_REL,
                                                                         msg_length,
                                                                         &do_send_message,
                                                                         session);
   if (!session->service_transmit_handle) {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Could not send service-request multipart message to tunnel!\n"));
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Could not send service-request multipart message to channel!\n"));
     GNUNET_free (msg);
     session->msg = NULL;
     session->client_notification_task =
@@ -1332,13 +1466,12 @@ prepare_service_request_multipart (void *cls,
     session->state = WAITING_FOR_SERVICE_RESPONSE;
 }
 
+
 /**
  * Executed by Alice, fills in a service-request message and sends it to the given peer
  *
- * @param session the session associated with this request, then also holds the CORE-handle
- * @return #GNUNET_SYSERR if we could not send the message
- *         #GNUNET_NO if the message was too large
- *         #GNUNET_OK if we sent it
+ * @param cls the session associated with this request
+ * @param tc task context handed over by scheduler, unsued
  */
 static void
 prepare_service_request (void *cls,
@@ -1357,7 +1490,7 @@ prepare_service_request (void *cls,
 
   session->service_request_task = GNUNET_SCHEDULER_NO_TASK;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _ ("Successfully created new tunnel to peer (%s)!\n"), GNUNET_i2s (&session->peer));
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _ ("Successfully created new channel to peer (%s)!\n"), GNUNET_i2s (&session->peer));
 
   msg_length = sizeof (struct GNUNET_SCALARPRODUCT_service_request)
           +session->mask_length
@@ -1438,13 +1571,13 @@ prepare_service_request (void *cls,
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Transmitting service request.\n"));
 
   //transmit via mesh messaging
-  session->service_transmit_handle = GNUNET_MESH_notify_transmit_ready (session->tunnel, GNUNET_YES,
+  session->service_transmit_handle = GNUNET_MESH_notify_transmit_ready (session->channel, GNUNET_YES,
                                                                         GNUNET_TIME_UNIT_FOREVER_REL,
                                                                         msg_length,
                                                                         &do_send_message,
                                                                         session);
   if (!session->service_transmit_handle) {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Could not send message to tunnel!\n"));
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Could not send message to channel!\n"));
     GNUNET_free (msg);
     session->msg = NULL;
     session->client_notification_task =
@@ -1461,6 +1594,7 @@ prepare_service_request (void *cls,
     session->state = WAITING_FOR_SERVICE_RESPONSE;
 }
 
+
 /**
  * Handler for a client request message.
  * Can either be type A or B
@@ -1513,7 +1647,7 @@ handle_client_request (void *cls,
   if ((ntohs (msg->header.size) != (sizeof (struct GNUNET_SCALARPRODUCT_client_request) +element_count * sizeof (int32_t) + mask_length))
       || (0 == element_count)) {
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                _ ("Invalid message received from client, session information incorrect!\n"));
+                _("Invalid message received from client, session information incorrect!\n"));
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
     return;
   }
@@ -1524,7 +1658,7 @@ handle_client_request (void *cls,
                                      element_count,
                                      NULL, NULL)) {
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                _ ("Duplicate session information received, cannot create new session with key `%s'\n"),
+                _("Duplicate session information received, cannot create new session with key `%s'\n"),
                 GNUNET_h2s (&msg->key));
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
     return;
@@ -1542,9 +1676,10 @@ 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"),
+                _("Got client-request-session with key %s, preparing channel to remote service.\n"),
                 GNUNET_h2s (&session->key));
 
     session->role = ALICE;
@@ -1554,7 +1689,8 @@ handle_client_request (void *cls,
 
     // copy over the elements
     session->used = 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));
@@ -1562,7 +1698,8 @@ handle_client_request (void *cls,
         session->used++;
     }
 
-    if (0 == session->used) {
+    if (0 == session->used)
+    {
       GNUNET_break_op (0);
       GNUNET_free (session->vector);
       GNUNET_free (session);
@@ -1570,7 +1707,8 @@ 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);
@@ -1580,15 +1718,14 @@ handle_client_request (void *cls,
     // get our peer ID
     memcpy (&session->peer, &msg->peer, sizeof (struct GNUNET_PeerIdentity));
     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-                _ ("Creating new tunnel to for session with key %s.\n"),
+                _ ("Creating new channel for session with key %s.\n"),
                 GNUNET_h2s (&session->key));
-    session->tunnel = GNUNET_MESH_tunnel_create (my_mesh, session,
+    session->channel = GNUNET_MESH_channel_create (my_mesh, session,
                                                  &session->peer,
                                                  GNUNET_APPLICATION_TYPE_SCALARPRODUCT,
-                                                 GNUNET_NO,
-                                                 GNUNET_YES);
-    //prepare_service_request, tunnel_peer_disconnect_handler,
-    if (!session->tunnel) {
+                                                 GNUNET_MESH_OPTION_RELIABLE);
+    //prepare_service_request, channel_peer_disconnect_handler,
+    if (!session->channel) {
       GNUNET_break (0);
       GNUNET_free (session->vector);
       GNUNET_free (session);
@@ -1604,7 +1741,8 @@ handle_client_request (void *cls,
                                       session);
 
   }
-  else {
+  else
+  {
     struct ServiceSession * requesting_session;
     enum SessionState needed_state = SERVICE_REQUEST_RECEIVED;
 
@@ -1624,16 +1762,22 @@ handle_client_request (void *cls,
                                                 &session->key,
                                                 session->total,
                                                 &needed_state, NULL);
-    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 (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 =
               GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
                                         session);
 
     }
-    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));
+    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()
     }
@@ -1641,48 +1785,55 @@ handle_client_request (void *cls,
   GNUNET_SERVER_receive_done (client, GNUNET_YES);
 }
 
+
 /**
- * Function called for inbound tunnels.
+ * Function called for inbound channels.
  *
  * @param cls closure
- * @param tunnel new handle to the tunnel
- * @param initiator peer that started the tunnel
- * @param atsi performance information for the tunnel
- * @return initial tunnel context for the tunnel
- *         (can be NULL -- that's not an error)
+ * @param channel new handle to the channel
+ * @param initiator peer that started the channel
+ * @param port unused
+ * @param options unused
+ *
+ * @return session associated with the channel
  */
 static void *
-tunnel_incoming_handler (void *cls,
-                         struct GNUNET_MESH_Tunnel *tunnel,
+channel_incoming_handler (void *cls,
+                         struct GNUNET_MESH_Channel *channel,
                          const struct GNUNET_PeerIdentity *initiator,
-                         uint32_t port)
+                         uint32_t port, enum GNUNET_MESH_ChannelOption options)
 {
   struct ServiceSession * c = GNUNET_new (struct ServiceSession);
 
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              _("New incoming channel from peer %s.\n"),
+              GNUNET_i2s (initiator));
+
   c->peer = *initiator;
-  c->tunnel = tunnel;
+  c->channel = channel;
   c->role = BOB;
   c->state = WAITING_FOR_SERVICE_REQUEST;
   return c;
 }
 
+
 /**
- * Function called whenever a tunnel is destroyed.  Should clean up
+ * Function called whenever a channel is destroyed.  Should clean up
  * any associated state.
  *
- * It must NOT call GNUNET_MESH_tunnel_destroy on the tunnel.
+ * It must NOT call GNUNET_MESH_channel_destroy on the channel.
  *
  * @param cls closure (set from GNUNET_MESH_connect)
- * @param tunnel connection to the other end (henceforth invalid)
- * @param tunnel_ctx place where local state associated
- *                   with the tunnel is stored
+ * @param channel connection to the other end (henceforth invalid)
+ * @param channel_ctx place where local state associated
+ *                   with the channel is stored
  */
 static void
-tunnel_destruction_handler (void *cls,
-                            const struct GNUNET_MESH_Tunnel *tunnel,
-                            void *tunnel_ctx)
+channel_destruction_handler (void *cls,
+                            const struct GNUNET_MESH_Channel *channel,
+                            void *channel_ctx)
 {
-  struct ServiceSession * session = tunnel_ctx;
+  struct ServiceSession * session = channel_ctx;
   struct ServiceSession * client_session;
   struct ServiceSession * curr;
 
@@ -1694,7 +1845,7 @@ tunnel_destruction_handler (void *cls,
     // as we have only one peer connected in each session, just remove the session
 
     if ((SERVICE_RESPONSE_RECEIVED > session->state) && (!do_shutdown)) {
-      session->tunnel = NULL;
+      session->channel = NULL;
       // if this happened before we received the answer, we must terminate the session
       session->client_notification_task =
               GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
@@ -1730,18 +1881,11 @@ tunnel_destruction_handler (void *cls,
   }
 }
 
+
 /**
  * Compute our scalar product, done by Alice
  *
  * @param session - the session associated with this computation
- * @param kp - (1) from the protocol definition:
- *             $E_A(a_{\pi(i)}) \otimes E_A(- r_{\pi(i)} - b_{\pi(i)}) &= E_A(a_{\pi(i)} - r_{\pi(i)} - b_{\pi(i)})$
- * @param kq - (2) from the protocol definition:
- *             $E_A(a_{\pi'(i)}) \otimes E_A(- r_{\pi'(i)}) &= E_A(a_{\pi'(i)} - r_{\pi'(i)})$
- * @param s - S from the protocol definition:
- *            $S := E_A(\sum (r_i + b_i)^2)$
- * @param stick - S' from the protocol definition:
- *                $S' := E_A(\sum r_i^2)$
  * @return product as MPI, never NULL
  */
 static gcry_mpi_t
@@ -1750,18 +1894,18 @@ compute_scalar_product (struct ServiceSession * session)
   uint32_t count;
   gcry_mpi_t t;
   gcry_mpi_t u;
-  gcry_mpi_t utick;
+  gcry_mpi_t u_prime;
   gcry_mpi_t p;
-  gcry_mpi_t ptick;
+  gcry_mpi_t p_prime;
   gcry_mpi_t tmp;
   unsigned int i;
 
   count = session->used;
-  tmp = gcry_mpi_new (KEYBITS);
   // 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++) {
+  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);
@@ -1780,12 +1924,12 @@ compute_scalar_product (struct ServiceSession * session)
   gcry_mpi_release (tmp);
 
   //calculate U'
-  utick = gcry_mpi_new (0);
+  u_prime = gcry_mpi_new (0);
   tmp = compute_square_sum (session->r_prime, count);
-  gcry_mpi_sub (utick, utick, tmp);
+  gcry_mpi_sub (u_prime, u_prime, tmp);
 
   GNUNET_assert (p = gcry_mpi_new (0));
-  GNUNET_assert (ptick = gcry_mpi_new (0));
+  GNUNET_assert (p_prime = gcry_mpi_new (0));
 
   // compute P
   decrypt_element (session->s, session->s, my_mu, my_lambda, my_n, my_nsquare);
@@ -1796,16 +1940,16 @@ compute_scalar_product (struct ServiceSession * session)
   gcry_mpi_add (p, p, u);
 
   // compute P'
-  gcry_mpi_add (ptick, session->s_prime, t);
-  gcry_mpi_add (ptick, ptick, utick);
+  gcry_mpi_add (p_prime, session->s_prime, t);
+  gcry_mpi_add (p_prime, p_prime, u_prime);
 
   gcry_mpi_release (t);
   gcry_mpi_release (u);
-  gcry_mpi_release (utick);
+  gcry_mpi_release (u_prime);
 
   // compute product
-  gcry_mpi_sub (p, p, ptick);
-  gcry_mpi_release (ptick);
+  gcry_mpi_sub (p, p, p_prime);
+  gcry_mpi_release (p_prime);
   tmp = gcry_mpi_set_ui (tmp, 2);
   gcry_mpi_div (p, NULL, p, tmp, 0);
 
@@ -1818,126 +1962,34 @@ 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.
- *
- * @param session  the session associated with our client.
- */
-static void
-prepare_client_response (void *cls,
-                         const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  struct ServiceSession * session = cls;
-  struct GNUNET_SCALARPRODUCT_client_response * msg;
-  unsigned char * product_exported = NULL;
-  size_t product_length = 0;
-  uint32_t msg_length = 0;
-  int8_t range = -1;
-  gcry_error_t rc;
-  int sign;
-
-  session->client_notification_task = GNUNET_SCHEDULER_NO_TASK;
-
-  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) {
-      gcry_mpi_sub (value, value, session->product);
-    }
-    else if (0 < sign) {
-      range = 1;
-      gcry_mpi_add (value, value, session->product);
-    }
-    else
-      range = 0;
-
-    gcry_mpi_release (session->product);
-    session->product = NULL;
-
-    // get representation as string
-    if (range
-        && (0 != (rc = gcry_mpi_aprint (GCRYMPI_FMT_STD,
-                                        &product_exported,
-                                        &product_length,
-                                        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
-    }
-    gcry_mpi_release (value);
-  }
-
-  msg_length = sizeof (struct GNUNET_SCALARPRODUCT_client_response) +product_length;
-  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) {
-    memcpy (&msg[1], product_exported, product_length);
-    GNUNET_free (product_exported);
-  }
-  msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_SERVICE_TO_CLIENT);
-  msg->header.size = htons (msg_length);
-  msg->range = range;
-  msg->product_length = htonl (product_length);
-
-  session->msg = (struct GNUNET_MessageHeader *) msg;
-  //transmit this message to our client
-  session->client_transmit_handle =
-          GNUNET_SERVER_notify_transmit_ready (session->client,
-                                               msg_length,
-                                               GNUNET_TIME_UNIT_FOREVER_REL,
-                                               &do_send_message,
-                                               session);
-  if (NULL == session->client_transmit_handle) {
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                _ ("Could not send message to client (%p)!\n"),
-                session->client);
-    session->client = NULL;
-    // callback was not called!
-    GNUNET_free (msg);
-    session->msg = NULL;
-  }
-  else
-    // gracefully sent message, just terminate session structure
-    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-                _ ("Sent result to client (%p), this session (%s) has ended!\n"),
-                session->client,
-                GNUNET_h2s (&session->key));
-}
 
 /**
  * Handle a multipart-chunk of a request from another service to calculate a scalarproduct with us.
  *
  * @param cls closure (set from #GNUNET_MESH_connect)
- * @param tunnel connection to the other end
- * @param tunnel_ctx place to store local state associated with the tunnel
- * @param sender who sent the message
+ * @param channel connection to the other end
+ * @param channel_ctx place to store local state associated with the channel
  * @param message the actual message
- * @param atsi performance data for the connection
  * @return #GNUNET_OK to keep the connection open,
  *         #GNUNET_SYSERR to close it (signal serious error)
  */
 static int
 handle_service_request_multipart (void *cls,
-                                  struct GNUNET_MESH_Tunnel * tunnel,
-                                  void **tunnel_ctx,
+                                  struct GNUNET_MESH_Channel * channel,
+                                  void **channel_ctx,
                                   const struct GNUNET_MessageHeader * message)
 {
   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 contained_elements = 0;
   uint32_t msg_length;
   unsigned char * current;
   gcry_error_t rc;
   int32_t i = -1;
+
   // are we in the correct state?
-  session = (struct ServiceSession *) * tunnel_ctx;
+  session = (struct ServiceSession *) * channel_ctx;
   if ((BOB != session->role) || (WAITING_FOR_MULTIPART_TRANSMISSION != session->state)) {
     goto except;
   }
@@ -1948,46 +2000,53 @@ handle_service_request_multipart (void *cls,
   used_elements = session->used;
   contained_elements = ntohl (msg->multipart_element_count);
   msg_length = sizeof (struct GNUNET_SCALARPRODUCT_multipart_message)
-          + contained_elements * PAILLIER_ELEMENT_LENGTH;
+          +contained_elements * PAILLIER_ELEMENT_LENGTH;
   //sanity check
-  if (( ntohs (msg->header.size) != msg_length) 
-       || (used_elements < contained_elements + session->transferred)) {
+  if ((ntohs (msg->header.size) != msg_length)
+      || (used_elements < contained_elements + session->transferred)) {
     goto except;
   }
   current = (unsigned char *) &msg[1];
   if (contained_elements != 0) {
     // Convert each vector element to MPI_value
-    for (i = session->transferred; i < session->transferred+contained_elements; i++) {
+    for (i = session->transferred; i < session->transferred + contained_elements; i++) {
       size_t read = 0;
       if (0 != (rc = gcry_mpi_scan (&session->a[i],
-                           GCRYMPI_FMT_USG,
-                           &current[i * PAILLIER_ELEMENT_LENGTH],
-                           PAILLIER_ELEMENT_LENGTH,
-                           &read))) {
+                                    GCRYMPI_FMT_USG,
+                                    &current[i * PAILLIER_ELEMENT_LENGTH],
+                                    PAILLIER_ELEMENT_LENGTH,
+                                    &read))) {
         LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc);
         goto except;
       }
     }
-    session->transferred+=contained_elements;
-    
-    if (session->transferred == used_elements) {
+    session->transferred += contained_elements;
+
+    if (session->transferred == 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 (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...
           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));
+        GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                    _("Got session with key %s without a matching element set, queueing.\n"),
+                    GNUNET_h2s (&session->key));
     }
-    else{
+    else
+    {
       // multipart message
     }
   }
-  
+
   return GNUNET_OK;
 except:
   // and notify our client-session that we could not complete the session
@@ -2002,22 +2061,21 @@ except:
   return GNUNET_SYSERR;
 }
 
+
 /**
  * Handle a request from another service to calculate a scalarproduct with us.
  *
  * @param cls closure (set from #GNUNET_MESH_connect)
- * @param tunnel connection to the other end
- * @param tunnel_ctx place to store local state associated with the tunnel
- * @param sender who sent the message
+ * @param channel connection to the other end
+ * @param channel_ctx place to store local state associated with the channel
  * @param message the actual message
- * @param atsi performance data for the connection
  * @return #GNUNET_OK to keep the connection open,
  *         #GNUNET_SYSERR to close it (signal serious error)
  */
 static int
 handle_service_request (void *cls,
-                        struct GNUNET_MESH_Tunnel * tunnel,
-                        void **tunnel_ctx,
+                        struct GNUNET_MESH_Channel * channel,
+                        void **channel_ctx,
                         const struct GNUNET_MessageHeader * message)
 {
   struct ServiceSession * session;
@@ -2033,7 +2091,7 @@ handle_service_request (void *cls,
   int32_t i = -1;
   enum SessionState needed_state;
 
-  session = (struct ServiceSession *) * tunnel_ctx;
+  session = (struct ServiceSession *) * channel_ctx;
   if (WAITING_FOR_SERVICE_REQUEST != session->state) {
     goto invalid_msg;
   }
@@ -2058,9 +2116,12 @@ handle_service_request (void *cls,
           +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) || (used_elements < contained_elements)
-      || (used_elements == 0) || (mask_length != (element_count / 8 + (element_count % 8 ? 1 : 0)))
-      ) {
+  if ( (ntohs (msg->header.size) != msg_length) ||
+       (element_count < used_elements) ||
+       (used_elements < contained_elements) ||
+       (0 == used_elements) ||
+       (mask_length != (element_count / 8 + ((element_count % 8) ? 1 : 0))) )
+  {
     GNUNET_free (session);
     GNUNET_break_op (0);
     return GNUNET_SYSERR;
@@ -2069,17 +2130,19 @@ handle_service_request (void *cls,
                              &msg->key,
                              element_count,
                              NULL,
-                             NULL)) {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Got message with duplicate session key (`%s'), ignoring service request.\n"), (const char *) &(msg->key));
+                             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->total = element_count;
   session->used = used_elements;
   session->transferred = contained_elements;
-  session->tunnel = tunnel;
+  session->channel = channel;
 
   // session key
   memcpy (&session->key, &msg->key, sizeof (struct GNUNET_HashCode));
@@ -2106,17 +2169,17 @@ handle_service_request (void *cls,
                                              &needed_state, NULL);
 
   session->a = GNUNET_malloc (sizeof (gcry_mpi_t) * used_elements);
-  session->state = WAITING_FOR_MULTIPART_TRANSMISSION; 
+  session->state = WAITING_FOR_MULTIPART_TRANSMISSION;
   GNUNET_CONTAINER_DLL_insert (from_service_head, from_service_tail, session);
   if (contained_elements != 0) {
     // Convert each vector element to MPI_value
     for (i = 0; i < contained_elements; i++) {
       size_t read = 0;
       if (0 != (rc = gcry_mpi_scan (&session->a[i],
-                           GCRYMPI_FMT_USG,
-                           &current[i * PAILLIER_ELEMENT_LENGTH],
-                           PAILLIER_ELEMENT_LENGTH,
-                           &read))) {
+                                    GCRYMPI_FMT_USG,
+                                    &current[i * PAILLIER_ELEMENT_LENGTH],
+                                    PAILLIER_ELEMENT_LENGTH,
+                                    &read))) {
         LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc);
         goto invalid_msg;
       }
@@ -2134,7 +2197,7 @@ handle_service_request (void *cls,
       else
         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Got session with key %s without a matching element set, queueing.\n"), GNUNET_h2s (&session->key));
     }
-    else{
+    else {
       // multipart message
     }
   }
@@ -2153,22 +2216,21 @@ invalid_msg:
   return GNUNET_SYSERR;
 }
 
+
 /**
  * Handle a multipart chunk of a response we got from another service we wanted to calculate a scalarproduct with.
  *
  * @param cls closure (set from #GNUNET_MESH_connect)
- * @param tunnel connection to the other end
- * @param tunnel_ctx place to store local state associated with the tunnel
- * @param sender who sent the message
+ * @param channel connection to the other end
+ * @param channel_ctx place to store local state associated with the channel
  * @param message the actual message
- * @param atsi performance data for the connection
  * @return #GNUNET_OK to keep the connection open,
  *         #GNUNET_SYSERR to close it (signal serious error)
  */
 static int
 handle_service_response_multipart (void *cls,
-                                   struct GNUNET_MESH_Tunnel * tunnel,
-                                   void **tunnel_ctx,
+                                   struct GNUNET_MESH_Channel * channel,
+                                   void **channel_ctx,
                                    const struct GNUNET_MessageHeader * message)
 {
   struct ServiceSession * session;
@@ -2176,25 +2238,28 @@ handle_service_response_multipart (void *cls,
   unsigned char * current;
   size_t read;
   size_t i;
-  uint32_t contained=0;
+  uint32_t contained = 0;
   size_t msg_size;
+  size_t required_size;
   int rc;
 
   GNUNET_assert (NULL != message);
   // are we in the correct state?
-  session = (struct ServiceSession *) * tunnel_ctx;
+  session = (struct ServiceSession *) * channel_ctx;
   if ((ALICE != session->role) || (WAITING_FOR_MULTIPART_TRANSMISSION != session->state)) {
     goto invalid_msg;
   }
+  msg_size = ntohs (msg->header.size);
+  required_size = sizeof (struct GNUNET_SCALARPRODUCT_multipart_message) + 2 * PAILLIER_ELEMENT_LENGTH;
   // shorter than minimum?
-  if (ntohs (msg->header.size) <= sizeof (struct GNUNET_SCALARPRODUCT_multipart_message)) {
+  if (required_size > msg_size) {
     goto invalid_msg;
   }
   contained = ntohl (msg->multipart_element_count);
-  msg_size = sizeof (struct GNUNET_SCALARPRODUCT_multipart_message)
+  required_size = sizeof (struct GNUNET_SCALARPRODUCT_multipart_message)
           + 2 * contained * 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 < contained)) {
+  if ((required_size != msg_size) || (session->used < session->transferred + contained)) {
     goto invalid_msg;
   }
   current = (unsigned char *) &msg[1];
@@ -2217,41 +2282,40 @@ handle_service_response_multipart (void *cls,
   if (session->transferred != session->used)
     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!
-  
+  session->product = compute_scalar_product (session); //never NULL
+
 invalid_msg:
-  GNUNET_break_op (0);
-  free_session_variables (session);
-  session->state = FINALIZED;
-  session->tunnel = NULL;
+  GNUNET_break_op (NULL != session->product);
+
   // send message with product to client
-  if (ALICE == session->role)
-        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
+  if (ALICE == session->role){
+    session->state = FINALIZED;
+    session->channel = NULL;
+    session->client_notification_task =
+          GNUNET_SCHEDULER_add_now (&prepare_client_response,
+                                    session);
+  }
+  // the channel has done its job, terminate our connection and the channel
+  // the peer will be notified that the channel was destroyed via channel_destruction_handler
   // just close the connection, as recommended by Christian
   return GNUNET_SYSERR;
 }
 
+
 /**
  * Handle a response we got from another service we wanted to calculate a scalarproduct with.
  *
  * @param cls closure (set from #GNUNET_MESH_connect)
- * @param tunnel connection to the other end
- * @param tunnel_ctx place to store local state associated with the tunnel
- * @param sender who sent the message
+ * @param channel connection to the other end
+ * @param channel_ctx place to store local state associated with the channel
  * @param message the actual message
- * @param atsi performance data for the connection
  * @return #GNUNET_OK to keep the connection open,
  *         #GNUNET_SYSERR to close it (we are done)
  */
 static int
 handle_service_response (void *cls,
-                         struct GNUNET_MESH_Tunnel * tunnel,
-                         void **tunnel_ctx,
+                         struct GNUNET_MESH_Channel * channel,
+                         void **channel_ctx,
                          const struct GNUNET_MessageHeader * message)
 {
   struct ServiceSession * session;
@@ -2259,26 +2323,30 @@ handle_service_response (void *cls,
   unsigned char * current;
   size_t read;
   size_t i;
-  uint32_t contained=0;
+  uint32_t contained = 0;
   size_t msg_size;
+  size_t required_size;
   int rc;
 
   GNUNET_assert (NULL != message);
-  session = (struct ServiceSession *) * tunnel_ctx;
+  session = (struct ServiceSession *) * channel_ctx;
   // are we in the correct state?
-  if (session->state != WAITING_FOR_SERVICE_REQUEST) {
+  if (WAITING_FOR_SERVICE_RESPONSE != session->state) {
     goto invalid_msg;
   }
   //we need at least a full message without elements attached
-  if (sizeof (struct GNUNET_SCALARPRODUCT_service_response) + 2 * PAILLIER_ELEMENT_LENGTH > ntohs (msg->header.size)) {
+  msg_size = ntohs (msg->header.size);
+  required_size = sizeof (struct GNUNET_SCALARPRODUCT_service_response) + 2 * PAILLIER_ELEMENT_LENGTH;
+
+  if (required_size > msg_size) {
     goto invalid_msg;
   }
   contained = ntohl (msg->contained_element_count);
-  msg_size = sizeof (struct GNUNET_SCALARPRODUCT_service_response)
+  required_size = sizeof (struct GNUNET_SCALARPRODUCT_service_response)
           + 2 * contained * 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) || (session->used < contained)) {
+  if ((msg_size != required_size) || (session->used < contained)) {
     goto invalid_msg;
   }
   session->state = WAITING_FOR_MULTIPART_TRANSMISSION;
@@ -2317,27 +2385,27 @@ handle_service_response (void *cls,
   }
   if (session->transferred != session->used)
     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!
+  session->product = compute_scalar_product (session); //never NULL
 
 invalid_msg:
-  GNUNET_break_op (0);
-  free_session_variables (session);
-  session->state = FINALIZED;
-  session->tunnel = NULL;
+  GNUNET_break_op (NULL != session->product);
   // send message with product to client
-  if (ALICE == session->role)
+  if (ALICE == session->role){
+    session->state = FINALIZED;
+    session->channel = NULL;
     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
+  }
+  // the channel has done its job, terminate our connection and the channel
+  // the peer will be notified that the channel was destroyed via channel_destruction_handler
   // just close the connection, as recommended by Christian
   return GNUNET_SYSERR;
 }
 
+
 /**
  * Task run during shutdown.
  *
@@ -2353,11 +2421,11 @@ shutdown_task (void *cls,
 
   do_shutdown = GNUNET_YES;
 
-  // terminate all owned open tunnels.
+  // terminate all owned open channels.
   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 ((FINALIZED != session->state) && (NULL != session->channel)) {
+      GNUNET_MESH_channel_destroy (session->channel);
+      session->channel = NULL;
     }
     if (GNUNET_SCHEDULER_NO_TASK != session->client_notification_task) {
       GNUNET_SCHEDULER_cancel (session->client_notification_task);
@@ -2373,9 +2441,9 @@ shutdown_task (void *cls,
     }
   }
   for (session = from_service_head; NULL != session; session = session->next)
-    if (NULL != session->tunnel) {
-      GNUNET_MESH_tunnel_destroy (session->tunnel);
-      session->tunnel = NULL;
+    if (NULL != session->channel) {
+      GNUNET_MESH_channel_destroy (session->channel);
+      session->channel = NULL;
     }
 
   if (my_mesh) {
@@ -2384,6 +2452,7 @@ shutdown_task (void *cls,
   }
 }
 
+
 /**
  * Initialization of the program and message handlers
  *
@@ -2424,8 +2493,8 @@ run (void *cls,
                 GNUNET_CRYPTO_get_peer_identity (c,
                                                  &me));
   my_mesh = GNUNET_MESH_connect (c, NULL,
-                                 &tunnel_incoming_handler,
-                                 &tunnel_destruction_handler,
+                                 &channel_incoming_handler,
+                                 &channel_destruction_handler,
                                  mesh_handlers, ports);
   if (!my_mesh) {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Connect to MESH failed\n"));
@@ -2438,6 +2507,7 @@ run (void *cls,
                                 NULL);
 }
 
+
 /**
  * The main function for the scalarproduct service.
  *
@@ -2455,4 +2525,4 @@ main (int argc, char *const *argv)
                               &run, NULL)) ? 0 : 1;
 }
 
-/* end of gnunet-service-ext.c */
+/* end of gnunet-service-scalarproduct.c */