Adapting verify successor code to use trail id
[oweals/gnunet.git] / src / cadet / gnunet-service-cadet_tunnel.c
index 971b186c992411aa038eaa89e4417c55cab4260c..6c4d949e3cf9e6f7c2f0661328cd91174daa2b55 100644 (file)
@@ -86,6 +86,12 @@ struct CadetTConnection
  */
 struct CadetTunnelKXCtx
 {
+  /**
+   * Encryption ("our") old key, for encrypting traffic sent by us
+   * end before the key exchange is finished or times out.
+   */
+  struct GNUNET_CRYPTO_SymmetricSessionKey e_key_old;
+
   /**
    * Decryption ("their") old key, for decrypting traffic sent by the
    * other end before the key exchange started.
@@ -96,12 +102,23 @@ struct CadetTunnelKXCtx
    * Challenge to send in a ping and expect in the pong.
    */
   uint32_t challenge;
+
+  /**
+   * When the rekey started. One minute after this the new key will be used.
+   */
+  struct GNUNET_TIME_Absolute rekey_start_time;
+
+  /**
+   * Task for delayed destruction of the Key eXchange context, to allow delayed
+   * messages with the old key to be decrypted successfully.
+   */
+  GNUNET_SCHEDULER_TaskIdentifier finish_task;
 };
 
 /**
  * Struct containing all information regarding a tunnel to a peer.
  */
-struct CadetTunnel3
+struct CadetTunnel
 {
     /**
      * Endpoint of the tunnel.
@@ -111,18 +128,24 @@ struct CadetTunnel3
     /**
      * State of the tunnel connectivity.
      */
-  enum CadetTunnel3CState cstate;
+  enum CadetTunnelCState cstate;
 
   /**
    * State of the tunnel encryption.
    */
-  enum CadetTunnel3EState estate;
+  enum CadetTunnelEState estate;
 
   /**
    * Key eXchange context.
    */
   struct CadetTunnelKXCtx *kx_ctx;
 
+  /**
+   * Peer's ephemeral key, to recreate @c e_key and @c d_key when own ephemeral
+   * key changes.
+   */
+  struct GNUNET_CRYPTO_EcdhePublicKey peers_ephemeral_key;
+
   /**
    * Encryption ("our") key.
    */
@@ -187,12 +210,12 @@ struct CadetTunnelDelayed
   /**
    * Tunnel.
    */
-  struct CadetTunnel3 *t;
+  struct CadetTunnel *t;
 
   /**
    * Tunnel queue given to the channel to cancel request. Update on send_queued.
    */
-  struct CadetTunnel3Queue *tq;
+  struct CadetTunnelQueue *tq;
 
   /**
    * Message to send.
@@ -204,7 +227,7 @@ struct CadetTunnelDelayed
 /**
  * Handle for messages queued but not yet sent.
  */
-struct CadetTunnel3Queue
+struct CadetTunnelQueue
 {
   /**
    * Connection queue handle, to cancel if necessary.
@@ -302,7 +325,7 @@ static struct GNUNET_TIME_Relative rekey_period;
  * @return String representation.
  */
 static const char *
-cstate2s (enum CadetTunnel3CState cs)
+cstate2s (enum CadetTunnelCState cs)
 {
   static char buf[128];
 
@@ -333,7 +356,7 @@ cstate2s (enum CadetTunnel3CState cs)
  * @return String representation.
  */
 static const char *
-estate2s (enum CadetTunnel3EState es)
+estate2s (enum CadetTunnelEState es)
 {
   static char buf[128];
 
@@ -366,7 +389,7 @@ estate2s (enum CadetTunnel3EState es)
  * @return #GNUNET_YES if ready, #GNUNET_NO otherwise
  */
 static int
-is_ready (struct CadetTunnel3 *t)
+is_ready (struct CadetTunnel *t)
 {
   int ready;
 
@@ -490,7 +513,7 @@ get_connection_allowed (const struct CadetTConnection *tc)
  * @return GNUNET_OK if message is fine, GNUNET_SYSERR otherwise.
  */
 int
-check_ephemeral (struct CadetTunnel3 *t,
+check_ephemeral (struct CadetTunnel *t,
                  const struct GNUNET_CADET_KX_Ephemeral *msg)
 {
   /* Check message size */
@@ -519,6 +542,36 @@ check_ephemeral (struct CadetTunnel3 *t,
 }
 
 
+/**
+ * Calculate HMAC.
+ *
+ * @param t Tunnel to get keys from.
+ * @param plaintext Content to HMAC.
+ * @param size Size of @c plaintext.
+ * @param iv Initialization vector for the message.
+ * @param outgoing Is this an outgoing message that we encrypted?
+ * @param hmac Destination to store the HMAC.
+ */
+static void
+t_hmac (struct CadetTunnel *t, const void *plaintext, size_t size, uint32_t iv,
+        int outgoing, struct GNUNET_CADET_Hash *hmac)
+{
+  struct GNUNET_CRYPTO_AuthKey auth_key;
+  static const char ctx[] = "cadet authentication key";
+  struct GNUNET_CRYPTO_SymmetricSessionKey *key;
+  struct GNUNET_HashCode hash;
+
+  key = outgoing ? &t->e_key : &t->d_key;
+  GNUNET_CRYPTO_hmac_derive_key (&auth_key, key,
+                                 &iv, sizeof (iv),
+                                 key, sizeof (*key),
+                                 ctx, sizeof (ctx),
+                                 NULL);
+  GNUNET_CRYPTO_hmac (&auth_key, plaintext, size, &hash);
+  memcpy (hmac, &hash, sizeof (*hmac));
+}
+
+
 /**
  * Encrypt data with the tunnel key.
  *
@@ -527,19 +580,48 @@ check_ephemeral (struct CadetTunnel3 *t,
  * @param src Source of the plaintext. Can overlap with @c dst.
  * @param size Size of the plaintext.
  * @param iv Initialization Vector to use.
+ * @param force_newest_key Force the use of the newest key, otherwise
+ *                         CADET will use the old key when allowed.
+ *                         This can happen in the case when a KX is going on
+ *                         and the old one hasn't expired.
  */
 static int
-t_encrypt (struct CadetTunnel3 *t,
-           void *dst, const void *src,
-           size_t size, uint32_t iv)
+t_encrypt (struct CadetTunnel *t, void *dst, const void *src,
+           size_t size, uint32_t iv, int force_newest_key)
 {
   struct GNUNET_CRYPTO_SymmetricInitializationVector siv;
+  struct GNUNET_CRYPTO_SymmetricSessionKey *e_key;
   size_t out_size;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG, "  t_encrypt start\n");
-  GNUNET_CRYPTO_symmetric_derive_iv (&siv, &t->e_key, &iv, sizeof (iv), NULL);
+  if (GNUNET_NO == force_newest_key
+      && NULL != t->kx_ctx
+      && GNUNET_SCHEDULER_NO_TASK == t->kx_ctx->finish_task)
+  {
+    struct GNUNET_TIME_Relative age;
+
+    age = GNUNET_TIME_absolute_get_duration (t->kx_ctx->rekey_start_time);
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "  key exchange in progress, started %s ago\n",
+         GNUNET_STRINGS_relative_time_to_string (age, GNUNET_YES));
+    if (age.rel_value_us < GNUNET_TIME_UNIT_MINUTES.rel_value_us)
+    {
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "  using old key\n");
+      e_key = &t->kx_ctx->e_key_old;
+    }
+    else
+    {
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "  using new key\n");
+      e_key = &t->e_key;
+    }
+  }
+  else
+  {
+    e_key = &t->e_key;
+  }
+  GNUNET_CRYPTO_symmetric_derive_iv (&siv, e_key, &iv, sizeof (iv), NULL);
   LOG (GNUNET_ERROR_TYPE_DEBUG, "  t_encrypt IV derived\n");
-  out_size = GNUNET_CRYPTO_symmetric_encrypt (src, size, &t->e_key, &siv, dst);
+  out_size = GNUNET_CRYPTO_symmetric_encrypt (src, size, e_key, &siv, dst);
   LOG (GNUNET_ERROR_TYPE_DEBUG, "  t_encrypt end\n");
 
   return out_size;
@@ -547,20 +629,49 @@ t_encrypt (struct CadetTunnel3 *t,
 
 
 /**
- * Decrypt data with the tunnel key.
+ * Decrypt and verify data with the appropriate tunnel key.
+ *
+ * @param key Key to use.
+ * @param dst Destination for the plaintext.
+ * @param src Source of the encrypted data. Can overlap with @c dst.
+ * @param size Size of the encrypted data.
+ * @param iv Initialization Vector to use.
+ *
+ * @return Size of the decrypted data, -1 if an error was encountered.
+ */
+static int
+decrypt (const struct GNUNET_CRYPTO_SymmetricSessionKey *key,
+         void *dst, const void *src, size_t size, uint32_t iv)
+{
+  struct GNUNET_CRYPTO_SymmetricInitializationVector siv;
+  size_t out_size;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "  decrypt start\n");
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "  decrypt iv\n");
+  GNUNET_CRYPTO_symmetric_derive_iv (&siv, key, &iv, sizeof (iv), NULL);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "  decrypt iv done\n");
+  out_size = GNUNET_CRYPTO_symmetric_decrypt (src, size, key, &siv, dst);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "  decrypt end\n");
+
+  return out_size;
+}
+
+
+/**
+ * Decrypt and verify data with the most recent tunnel key.
  *
  * @param t Tunnel whose key to use.
  * @param dst Destination for the plaintext.
  * @param src Source of the encrypted data. Can overlap with @c dst.
  * @param size Size of the encrypted data.
  * @param iv Initialization Vector to use.
+ *
+ * @return Size of the decrypted data, -1 if an error was encountered.
  */
 static int
-t_decrypt (struct CadetTunnel3 *t,
-           void *dst, const void *src,
+t_decrypt (struct CadetTunnel *t, void *dst, const void *src,
            size_t size, uint32_t iv)
 {
-  struct GNUNET_CRYPTO_SymmetricInitializationVector siv;
   struct GNUNET_CRYPTO_SymmetricSessionKey *key;
   size_t out_size;
 
@@ -569,30 +680,77 @@ t_decrypt (struct CadetTunnel3 *t,
   {
     key = &t->d_key;
   }
-  else if (NULL != t->kx_ctx)
-  {
-    key = &t->kx_ctx->d_key_old;
-  }
   else
   {
     GNUNET_STATISTICS_update (stats, "# non decryptable data", 1, GNUNET_NO);
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "WARNING got data on %s without a valid key\n",
+    LOG (GNUNET_ERROR_TYPE_WARNING,
+         "got data on %s without a valid key\n",
          GCT_2s (t));
     GCT_debug (t);
-    return 0;
+    return -1;
   }
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  t_decrypt iv\n");
-  GNUNET_CRYPTO_symmetric_derive_iv (&siv, key, &iv, sizeof (iv), NULL);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  t_decrypt iv done\n");
-  out_size = GNUNET_CRYPTO_symmetric_decrypt (src, size, key, &siv, dst);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  t_decrypt end\n");
+  out_size = decrypt (key, dst, src, size, iv);
 
   return out_size;
 }
 
 
+/**
+ * Decrypt and verify data with the appropriate tunnel key and verify that the
+ * data has not been altered since it was sent by the remote peer.
+ *
+ * @param t Tunnel whose key to use.
+ * @param dst Destination for the plaintext.
+ * @param src Source of the encrypted data. Can overlap with @c dst.
+ * @param size Size of the encrypted data.
+ * @param iv Initialization Vector to use.
+ * @param msg_hmac HMAC of the message, cannot be NULL.
+ *
+ * @return Size of the decrypted data, -1 if an error was encountered.
+ */
+static int
+t_decrypt_and_validate (struct CadetTunnel *t,
+                        void *dst, const void *src,
+                        size_t size, uint32_t iv,
+                        const struct GNUNET_CADET_Hash *msg_hmac)
+{
+  struct GNUNET_CRYPTO_SymmetricSessionKey *key;
+  struct GNUNET_CADET_Hash hmac;
+  int decrypted_size;
+
+  /* Try primary (newest) key */
+  key = &t->d_key;
+  decrypted_size = decrypt (key, dst, src, size, iv);
+  t_hmac (t, src, size, iv, GNUNET_NO, &hmac);
+  if (0 == memcmp (msg_hmac, &hmac, sizeof (hmac)))
+    return decrypted_size;
+
+  /* If no key exchange is going on, we just failed */
+  if (NULL == t->kx_ctx)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                "Failed checksum validation on tunnel %s with no KX\n",
+                GCT_2s (t));
+    GNUNET_STATISTICS_update (stats, "# wrong HMAC", 1, GNUNET_NO);
+    return -1;
+  }
+
+  /* Try secondary (from previous KX period) key */
+  key = &t->kx_ctx->d_key_old;
+  decrypted_size = decrypt (key, dst, src, size, iv);
+  t_hmac (t, src, size, iv, GNUNET_NO, &hmac);
+  if (0 == memcmp (msg_hmac, &hmac, sizeof (hmac)))
+    return decrypted_size;
+
+  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+              "Failed checksum validation on tunnel %s with KX\n",
+              GCT_2s (t));
+  GNUNET_STATISTICS_update (stats, "# wrong HMAC", 1, GNUNET_NO);
+  return -1;
+}
+
+
 /**
  * Create key material by doing ECDH on the local and remote ephemeral keys.
  *
@@ -612,6 +770,7 @@ derive_key_material (struct GNUNET_HashCode *key_material,
   }
 }
 
+
 /**
  * Create a symmetic key from the identities of both ends and the key material
  * from ECDH.
@@ -637,6 +796,23 @@ derive_symmertic (struct GNUNET_CRYPTO_SymmetricSessionKey *key,
                      NULL);
 }
 
+
+/**
+ * Derive the tunnel's keys using our own and the peer's ephemeral keys.
+ *
+ * @param t Tunnel for which to create the keys.
+ */
+static void
+create_keys (struct CadetTunnel *t)
+{
+  struct GNUNET_HashCode km;
+
+  derive_key_material (&km, &t->peers_ephemeral_key);
+  derive_symmertic (&t->e_key, &my_full_id, GCP_get_id (t->peer), &km);
+  derive_symmertic (&t->d_key, GCP_get_id (t->peer), &my_full_id, &km);
+}
+
+
 /**
  * Pick a connection on which send the next data message.
  *
@@ -645,7 +821,7 @@ derive_symmertic (struct GNUNET_CRYPTO_SymmetricSessionKey *key,
  * @return The connection on which to send the next message.
  */
 static struct CadetConnection *
-tunnel_get_connection (struct CadetTunnel3 *t)
+tunnel_get_connection (struct CadetTunnel *t)
 {
   struct CadetTConnection *iter;
   struct CadetConnection *best;
@@ -693,8 +869,8 @@ tun_message_sent (void *cls,
               struct CadetConnectionQueue *q,
               uint16_t type, int fwd, size_t size)
 {
-  struct CadetTunnel3Queue *qt = cls;
-  struct CadetTunnel3 *t;
+  struct CadetTunnelQueue *qt = cls;
+  struct CadetTunnel *t;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG, "tun_message_sent\n");
 
@@ -726,7 +902,7 @@ unqueue_data (struct CadetTunnelDelayed *tqd)
  * @param msg Message itself (copy will be made).
  */
 static struct CadetTunnelDelayed *
-queue_data (struct CadetTunnel3 *t, const struct GNUNET_MessageHeader *msg)
+queue_data (struct CadetTunnel *t, const struct GNUNET_MessageHeader *msg)
 {
   struct CadetTunnelDelayed *tqd;
   uint16_t size = ntohs (msg->size);
@@ -748,36 +924,6 @@ queue_data (struct CadetTunnel3 *t, const struct GNUNET_MessageHeader *msg)
 }
 
 
-/**
- * Calculate HMAC.
- *
- * @param t Tunnel to get keys from.
- * @param plaintext Content to HMAC.
- * @param size Size of @c plaintext.
- * @param iv Initialization vector for the message.
- * @param outgoing Is this an outgoing message that we encrypted?
- * @param hmac Destination to store the HMAC.
- */
-static void
-t_hmac (struct CadetTunnel3 *t, const void *plaintext, size_t size, uint32_t iv,
-        int outgoing, struct GNUNET_CADET_Hash *hmac)
-{
-  struct GNUNET_CRYPTO_AuthKey auth_key;
-  static const char ctx[] = "cadet authentication key";
-  struct GNUNET_CRYPTO_SymmetricSessionKey *key;
-  struct GNUNET_HashCode hash;
-
-  key = outgoing ? &t->e_key : &t->d_key;
-  GNUNET_CRYPTO_hmac_derive_key (&auth_key, key,
-                                 &iv, sizeof (iv),
-                                 key, sizeof (*key),
-                                 ctx, sizeof (ctx),
-                                 NULL);
-  GNUNET_CRYPTO_hmac (&auth_key, plaintext, size, &hash);
-  memcpy (hmac, &hash, sizeof (*hmac));
-}
-
-
 /**
  * Sends an already built message on a tunnel, encrypting it and
  * choosing the best connection.
@@ -794,13 +940,13 @@ t_hmac (struct CadetTunnel3 *t, const void *plaintext, size_t size, uint32_t iv,
  *
  * @return Handle to cancel message. NULL if @c cont is NULL.
  */
-static struct CadetTunnel3Queue *
+static struct CadetTunnelQueue *
 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
-                       struct CadetTunnel3 *t, struct CadetConnection *c,
+                       struct CadetTunnel *t, struct CadetConnection *c,
                        int force, GCT_sent cont, void *cont_cls,
-                       struct CadetTunnel3Queue *existing_q)
+                       struct CadetTunnelQueue *existing_q)
 {
-  struct CadetTunnel3Queue *tq;
+  struct CadetTunnelQueue *tq;
   struct GNUNET_CADET_Encrypted *msg;
   size_t size = ntohs (message->size);
   char cbuf[sizeof (struct GNUNET_CADET_Encrypted) + size];
@@ -821,7 +967,7 @@ send_prebuilt_message (const struct GNUNET_MessageHeader *message,
     tqd = queue_data (t, message);
     if (NULL == cont)
       return NULL;
-    tq = GNUNET_new (struct CadetTunnel3Queue);
+    tq = GNUNET_new (struct CadetTunnelQueue);
     tq->tqd = tqd;
     tqd->tq = tq;
     tq->cont = cont;
@@ -835,7 +981,7 @@ send_prebuilt_message (const struct GNUNET_MessageHeader *message,
   msg = (struct GNUNET_CADET_Encrypted *) cbuf;
   msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED);
   msg->iv = iv;
-  GNUNET_assert (t_encrypt (t, &msg[1], message, size, iv) == size);
+  GNUNET_assert (t_encrypt (t, &msg[1], message, size, iv, GNUNET_NO) == size);
   t_hmac (t, &msg[1], size, iv, GNUNET_YES, &msg->hmac);
   msg->header.size = htons (sizeof (struct GNUNET_CADET_Encrypted) + size);
 
@@ -887,7 +1033,7 @@ send_prebuilt_message (const struct GNUNET_MessageHeader *message,
   }
   if (NULL == existing_q)
   {
-    tq = GNUNET_new (struct CadetTunnel3Queue); /* FIXME valgrind: leak*/
+    tq = GNUNET_new (struct CadetTunnelQueue); /* FIXME valgrind: leak*/
   }
   else
   {
@@ -909,7 +1055,7 @@ send_prebuilt_message (const struct GNUNET_MessageHeader *message,
  * @param t Tunnel that holds the messages. Cannot be loopback.
  */
 static void
-send_queued_data (struct CadetTunnel3 *t)
+send_queued_data (struct CadetTunnel *t)
 {
   struct CadetTunnelDelayed *tqd;
   struct CadetTunnelDelayed *next;
@@ -959,7 +1105,7 @@ send_queued_data (struct CadetTunnel3 *t)
  * @param message Message to send. Function modifies it.
  */
 static void
-send_kx (struct CadetTunnel3 *t,
+send_kx (struct CadetTunnel *t,
          const struct GNUNET_MessageHeader *message)
 {
   struct CadetConnection *c;
@@ -1030,9 +1176,9 @@ send_kx (struct CadetTunnel3 *t,
  * @param t Tunnel on which to send the key.
  */
 static void
-send_ephemeral (struct CadetTunnel3 *t)
+send_ephemeral (struct CadetTunnel *t)
 {
-  LOG (GNUNET_ERROR_TYPE_INFO, "=> EPHM for %s\n", GCT_2s (t));
+  LOG (GNUNET_ERROR_TYPE_INFO, "===> EPHM for %s\n", GCT_2s (t));
 
   kx_msg.sender_status = htonl (t->estate);
   send_kx (t, &kx_msg.header);
@@ -1044,11 +1190,11 @@ send_ephemeral (struct CadetTunnel3 *t)
  * @param t Tunnel on which to send the ping.
  */
 static void
-send_ping (struct CadetTunnel3 *t)
+send_ping (struct CadetTunnel *t)
 {
   struct GNUNET_CADET_KX_Ping msg;
 
-  LOG (GNUNET_ERROR_TYPE_INFO, "=> PING for %s\n", GCT_2s (t));
+  LOG (GNUNET_ERROR_TYPE_INFO, "===> PING for %s\n", GCT_2s (t));
   msg.header.size = htons (sizeof (msg));
   msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_KX_PING);
   msg.iv = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
@@ -1057,7 +1203,8 @@ send_ping (struct CadetTunnel3 *t)
 
   LOG (GNUNET_ERROR_TYPE_DEBUG, "  sending %u\n", msg.nonce);
   LOG (GNUNET_ERROR_TYPE_DEBUG, "  towards %s\n", GNUNET_i2s (&msg.target));
-  t_encrypt (t, &msg.target, &msg.target, ping_encryption_size(), msg.iv);
+  t_encrypt (t, &msg.target, &msg.target,
+             ping_encryption_size(), msg.iv, GNUNET_YES);
   LOG (GNUNET_ERROR_TYPE_DEBUG, "  e sending %u\n", msg.nonce);
   LOG (GNUNET_ERROR_TYPE_DEBUG, "  e towards %s\n", GNUNET_i2s (&msg.target));
 
@@ -1067,22 +1214,23 @@ send_ping (struct CadetTunnel3 *t)
 
 /**
  * Send a pong message on a tunnel.
- *
+ *d_
  * @param t Tunnel on which to send the pong.
  * @param challenge Value sent in the ping that we have to send back.
  */
 static void
-send_pong (struct CadetTunnel3 *t, uint32_t challenge)
+send_pong (struct CadetTunnel *t, uint32_t challenge)
 {
   struct GNUNET_CADET_KX_Pong msg;
 
-  LOG (GNUNET_ERROR_TYPE_INFO, "=> PONG for %s\n", GCT_2s (t));
+  LOG (GNUNET_ERROR_TYPE_INFO, "===> PONG for %s\n", GCT_2s (t));
   msg.header.size = htons (sizeof (msg));
   msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_KX_PONG);
   msg.iv = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
   msg.nonce = challenge;
   LOG (GNUNET_ERROR_TYPE_DEBUG, "  sending %u\n", msg.nonce);
-  t_encrypt (t, &msg.nonce, &msg.nonce, sizeof (msg.nonce), msg.iv);
+  t_encrypt (t, &msg.nonce, &msg.nonce,
+             sizeof (msg.nonce), msg.iv, GNUNET_YES);
   LOG (GNUNET_ERROR_TYPE_DEBUG, "  e sending %u\n", msg.nonce);
 
   send_kx (t, &msg.header);
@@ -1098,7 +1246,7 @@ send_pong (struct CadetTunnel3 *t, uint32_t challenge)
 static void
 rekey_tunnel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  struct CadetTunnel3 *t = cls;
+  struct CadetTunnel *t = cls;
 
   t->rekey_task = GNUNET_SCHEDULER_NO_TASK;
 
@@ -1113,10 +1261,15 @@ rekey_tunnel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     t->kx_ctx->challenge =
         GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
     t->kx_ctx->d_key_old = t->d_key;
+    t->kx_ctx->e_key_old = t->e_key;
+    create_keys (t);
+    t->kx_ctx->rekey_start_time = GNUNET_TIME_absolute_get ();
     LOG (GNUNET_ERROR_TYPE_DEBUG, "  new challenge for %s: %u\n",
          GCT_2s (t), t->kx_ctx->challenge);
   }
+
   send_ephemeral (t);
+
   switch (t->estate)
   {
     case CADET_TUNNEL3_KEY_UNINITIALIZED:
@@ -1133,6 +1286,7 @@ rekey_tunnel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
       LOG (GNUNET_ERROR_TYPE_DEBUG, "Unexpected state %u\n", t->estate);
   }
 
+  // FIXME exponential backoff
   LOG (GNUNET_ERROR_TYPE_DEBUG, "  next call in %s\n",
        GNUNET_STRINGS_relative_time_to_string (REKEY_WAIT, GNUNET_YES));
   t->rekey_task = GNUNET_SCHEDULER_add_delayed (REKEY_WAIT, &rekey_tunnel, t);
@@ -1140,7 +1294,11 @@ rekey_tunnel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
 
 /**
- * Out ephemeral key has changed, create new session key on all tunnels.
+ * Our ephemeral key has changed, create new session key on all tunnels.
+ *
+ * Each tunnel will start the Key Exchange with a random delay between
+ * 0 and number_of_tunnels*100 milliseconds, so there are 10 key exchanges
+ * per second, on average.
  *
  * @param cls Closure (size of the hashmap).
  * @param key Current public key.
@@ -1153,7 +1311,7 @@ rekey_iterator (void *cls,
                 const struct GNUNET_PeerIdentity *key,
                 void *value)
 {
-  struct CadetTunnel3 *t = value;
+  struct CadetTunnel *t = value;
   struct GNUNET_TIME_Relative delay;
   long n = (long) cls;
   uint32_t r;
@@ -1225,7 +1383,7 @@ destroy_iterator (void *cls,
                 const struct GNUNET_PeerIdentity *key,
                 void *value)
 {
-  struct CadetTunnel3 *t = value;
+  struct CadetTunnel *t = value;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG, "GCT_shutdown destroying tunnel at %p\n", t);
   GCT_destroy (t);
@@ -1241,7 +1399,7 @@ destroy_iterator (void *cls,
  * @param gid ID of the channel.
  */
 static void
-send_channel_destroy (struct CadetTunnel3 *t, unsigned int gid)
+send_channel_destroy (struct CadetTunnel *t, unsigned int gid)
 {
   struct GNUNET_CADET_ChannelManage msg;
 
@@ -1267,7 +1425,7 @@ send_channel_destroy (struct CadetTunnel3 *t, unsigned int gid)
  *            #GNUNET_SYSERR if message on a one-ended channel (remote)
  */
 static void
-handle_data (struct CadetTunnel3 *t,
+handle_data (struct CadetTunnel *t,
              const struct GNUNET_CADET_Data *msg,
              int fwd)
 {
@@ -1313,7 +1471,7 @@ handle_data (struct CadetTunnel3 *t,
  *            #GNUNET_SYSERR if message on a one-ended channel (remote)
  */
 static void
-handle_data_ack (struct CadetTunnel3 *t,
+handle_data_ack (struct CadetTunnel *t,
                  const struct GNUNET_CADET_DataACK *msg,
                  int fwd)
 {
@@ -1350,7 +1508,7 @@ handle_data_ack (struct CadetTunnel3 *t,
  * @param msg Data message.
  */
 static void
-handle_ch_create (struct CadetTunnel3 *t,
+handle_ch_create (struct CadetTunnel *t,
                   const struct GNUNET_CADET_ChannelCreate *msg)
 {
   struct CadetChannel *ch;
@@ -1385,7 +1543,7 @@ handle_ch_create (struct CadetTunnel3 *t,
  * @param msg NACK message.
  */
 static void
-handle_ch_nack (struct CadetTunnel3 *t,
+handle_ch_nack (struct CadetTunnel *t,
                 const struct GNUNET_CADET_ChannelManage *msg)
 {
   struct CadetChannel *ch;
@@ -1425,7 +1583,7 @@ handle_ch_nack (struct CadetTunnel3 *t,
  *            #GNUNET_SYSERR if message on a one-ended channel (remote)
  */
 static void
-handle_ch_ack (struct CadetTunnel3 *t,
+handle_ch_ack (struct CadetTunnel *t,
                const struct GNUNET_CADET_ChannelManage *msg,
                int fwd)
 {
@@ -1455,7 +1613,6 @@ handle_ch_ack (struct CadetTunnel3 *t,
 }
 
 
-
 /**
  * Handle a channel destruction message.
  *
@@ -1467,7 +1624,7 @@ handle_ch_ack (struct CadetTunnel3 *t,
  *            #GNUNET_SYSERR if message on a one-ended channel (remote)
  */
 static void
-handle_ch_destroy (struct CadetTunnel3 *t,
+handle_ch_destroy (struct CadetTunnel *t,
                    const struct GNUNET_CADET_ChannelManage *msg,
                    int fwd)
 {
@@ -1501,10 +1658,9 @@ handle_ch_destroy (struct CadetTunnel3 *t,
  * @param msg Key eXchange message.
  */
 static void
-handle_ephemeral (struct CadetTunnel3 *t,
+handle_ephemeral (struct CadetTunnel *t,
                   const struct GNUNET_CADET_KX_Ephemeral *msg)
 {
-  struct GNUNET_HashCode km;
   LOG (GNUNET_ERROR_TYPE_INFO, "<=== EPHM for %s\n", GCT_2s (t));
 
   if (GNUNET_OK != check_ephemeral (t, msg))
@@ -1512,13 +1668,11 @@ handle_ephemeral (struct CadetTunnel3 *t,
     GNUNET_break_op (0);
     return;
   }
-  derive_key_material (&km, &msg->ephemeral_key);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  km is %s\n", GNUNET_h2s (&km));
-  derive_symmertic (&t->e_key, &my_full_id, GCP_get_id (t->peer), &km);
-  derive_symmertic (&t->d_key, GCP_get_id (t->peer), &my_full_id, &km);
+  t->peers_ephemeral_key = msg->ephemeral_key;
+  create_keys (t);
   if (CADET_TUNNEL3_KEY_SENT == t->estate)
   {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "  our key was sent, send ping\n");
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "  our key was sent, sending ping\n");
     send_ping (t);
     t->estate = CADET_TUNNEL3_KEY_PING;
   }
@@ -1533,7 +1687,7 @@ handle_ephemeral (struct CadetTunnel3 *t,
  * @param msg Key eXchange Ping message.
  */
 static void
-handle_ping (struct CadetTunnel3 *t,
+handle_ping (struct CadetTunnel *t,
              const struct GNUNET_CADET_KX_Ping *msg)
 {
   struct GNUNET_CADET_KX_Ping res;
@@ -1561,6 +1715,23 @@ handle_ping (struct CadetTunnel3 *t,
 
   send_pong (t, res.nonce);
 }
+/**
+ * @brief Finish the Key eXchange and destory the old keys.
+ *
+ * @param cls Closure (Tunnel for which to finish the KX).
+ * @param tc Task context.
+ */
+static void
+finish_kx (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  struct CadetTunnel *t = cls;
+
+  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
+    return;
+
+  GNUNET_free (t->kx_ctx);
+  t->kx_ctx = NULL;
+}
 
 
 /**
@@ -1572,7 +1743,7 @@ handle_ping (struct CadetTunnel3 *t,
  * @param msg Key eXchange Pong message.
  */
 static void
-handle_pong (struct CadetTunnel3 *t,
+handle_pong (struct CadetTunnel *t,
              const struct GNUNET_CADET_KX_Pong *msg)
 {
   uint32_t challenge;
@@ -1596,8 +1767,17 @@ handle_pong (struct CadetTunnel3 *t,
   }
   GNUNET_SCHEDULER_cancel (t->rekey_task);
   t->rekey_task = GNUNET_SCHEDULER_NO_TASK;
-  GNUNET_free (t->kx_ctx);
-  t->kx_ctx = NULL;
+
+  /* Don't free the old keys right away, but after a delay.
+   * Rationale: the KX could have happened over a very fast connection,
+   * with payload traffic still signed with the old key stuck in a slower
+   * connection.
+   */
+  if (GNUNET_SCHEDULER_NO_TASK == t->kx_ctx->finish_task)
+  {
+    t->kx_ctx->finish_task =
+      GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_MINUTES, finish_kx, t);
+  }
   GCT_change_estate (t, CADET_TUNNEL3_KEY_OK);
 }
 
@@ -1614,7 +1794,7 @@ handle_pong (struct CadetTunnel3 *t,
  *            #GNUNET_SYSERR if message on a one-ended channel (remote)
  */
 static void
-handle_decrypted (struct CadetTunnel3 *t,
+handle_decrypted (struct CadetTunnel *t,
                   const struct GNUNET_MessageHeader *msgh,
                   int fwd)
 {
@@ -1682,28 +1862,19 @@ handle_decrypted (struct CadetTunnel3 *t,
  * @param msg Encrypted message.
  */
 void
-GCT_handle_encrypted (struct CadetTunnel3 *t,
+GCT_handle_encrypted (struct CadetTunnel *t,
                       const struct GNUNET_CADET_Encrypted *msg)
 {
   size_t size = ntohs (msg->header.size);
   size_t payload_size = size - sizeof (struct GNUNET_CADET_Encrypted);
-  size_t decrypted_size;
+  int decrypted_size;
   char cbuf [payload_size];
   struct GNUNET_MessageHeader *msgh;
   unsigned int off;
-  struct GNUNET_CADET_Hash hmac;
 
-  decrypted_size = t_decrypt (t, cbuf, &msg[1], payload_size, msg->iv);
-  t_hmac (t, &msg[1], payload_size, msg->iv, GNUNET_NO, &hmac);
-  if (0 != memcmp (&hmac, &msg->hmac, sizeof (hmac)))
-  {
-    /* checksum failed */
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Failed checksum validation for a message on tunnel `%s'\n",
-                GCT_2s (t));
-    GNUNET_STATISTICS_update (stats, "# wrong HMAC", 1, GNUNET_NO);
-    return;
-  }
+  decrypted_size = t_decrypt_and_validate (t, cbuf, &msg[1], payload_size,
+                                           msg->iv, &msg->hmac);
+
   off = 0;
   while (off < decrypted_size)
   {
@@ -1721,7 +1892,7 @@ GCT_handle_encrypted (struct CadetTunnel3 *t,
  * @param message Payload of KX message.
  */
 void
-GCT_handle_kx (struct CadetTunnel3 *t,
+GCT_handle_kx (struct CadetTunnel *t,
                const struct GNUNET_MessageHeader *message)
 {
   uint16_t type;
@@ -1808,12 +1979,12 @@ GCT_shutdown (void)
  *
  * @param destination Peer this tunnel is towards.
  */
-struct CadetTunnel3 *
+struct CadetTunnel *
 GCT_new (struct CadetPeer *destination)
 {
-  struct CadetTunnel3 *t;
+  struct CadetTunnel *t;
 
-  t = GNUNET_new (struct CadetTunnel3);
+  t = GNUNET_new (struct CadetTunnel);
   t->next_chid = 0;
   t->peer = destination;
 
@@ -1836,7 +2007,7 @@ GCT_new (struct CadetPeer *destination)
  * @param cstate New connection state.
  */
 void
-GCT_change_cstate (struct CadetTunnel3* t, enum CadetTunnel3CState cstate)
+GCT_change_cstate (struct CadetTunnel* t, enum CadetTunnelCState cstate)
 {
   if (NULL == t)
     return;
@@ -1875,7 +2046,7 @@ GCT_change_cstate (struct CadetTunnel3* t, enum CadetTunnel3CState cstate)
  * @param state New encryption state.
  */
 void
-GCT_change_estate (struct CadetTunnel3* t, enum CadetTunnel3EState state)
+GCT_change_estate (struct CadetTunnel* t, enum CadetTunnelEState state)
 {
   if (NULL == t)
     return;
@@ -1909,7 +2080,7 @@ GCT_change_estate (struct CadetTunnel3* t, enum CadetTunnel3EState state)
 static void
 trim_connections (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  struct CadetTunnel3 *t = cls;
+  struct CadetTunnel *t = cls;
 
   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
     return;
@@ -1950,7 +2121,7 @@ trim_connections (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * @param c Connection.
  */
 void
-GCT_add_connection (struct CadetTunnel3 *t, struct CadetConnection *c)
+GCT_add_connection (struct CadetTunnel *t, struct CadetConnection *c)
 {
   struct CadetTConnection *aux;
 
@@ -1979,7 +2150,7 @@ GCT_add_connection (struct CadetTunnel3 *t, struct CadetConnection *c)
  * @param path Invalid path to remove. Is destroyed after removal.
  */
 void
-GCT_remove_path (struct CadetTunnel3 *t, struct CadetPeerPath *path)
+GCT_remove_path (struct CadetTunnel *t, struct CadetPeerPath *path)
 {
   GCP_remove_path (t->peer, path);
 }
@@ -1992,7 +2163,7 @@ GCT_remove_path (struct CadetTunnel3 *t, struct CadetPeerPath *path)
  * @param c Connection.
  */
 void
-GCT_remove_connection (struct CadetTunnel3 *t,
+GCT_remove_connection (struct CadetTunnel *t,
                        struct CadetConnection *c)
 {
   struct CadetTConnection *aux;
@@ -2042,7 +2213,7 @@ GCT_remove_connection (struct CadetTunnel3 *t,
  * @param ch Channel.
  */
 void
-GCT_add_channel (struct CadetTunnel3 *t, struct CadetChannel *ch)
+GCT_add_channel (struct CadetTunnel *t, struct CadetChannel *ch)
 {
   struct CadetTChannel *aux;
 
@@ -2078,7 +2249,7 @@ GCT_add_channel (struct CadetTunnel3 *t, struct CadetChannel *ch)
  * @param ch Channel.
  */
 void
-GCT_remove_channel (struct CadetTunnel3 *t, struct CadetChannel *ch)
+GCT_remove_channel (struct CadetTunnel *t, struct CadetChannel *ch)
 {
   struct CadetTChannel *aux;
 
@@ -2105,7 +2276,7 @@ GCT_remove_channel (struct CadetTunnel3 *t, struct CadetChannel *ch)
  * @return channel handler, NULL if doesn't exist
  */
 struct CadetChannel *
-GCT_get_channel (struct CadetTunnel3 *t, CADET_ChannelNumber chid)
+GCT_get_channel (struct CadetTunnel *t, CADET_ChannelNumber chid)
 {
   struct CadetTChannel *iter;
 
@@ -2135,7 +2306,7 @@ GCT_get_channel (struct CadetTunnel3 *t, CADET_ChannelNumber chid)
 static void
 delayed_destroy (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  struct CadetTunnel3 *t = cls;
+  struct CadetTunnel *t = cls;
   struct CadetTConnection *iter;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG, "delayed destroying tunnel %p\n", t);
@@ -2165,7 +2336,7 @@ delayed_destroy (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * @param t Tunnel to destroy.
  */
 void
-GCT_destroy_empty (struct CadetTunnel3 *t)
+GCT_destroy_empty (struct CadetTunnel *t)
 {
   if (GNUNET_YES == shutting_down)
     return; /* Will be destroyed immediately anyway */
@@ -2199,7 +2370,7 @@ GCT_destroy_empty (struct CadetTunnel3 *t)
  * @param t Tunnel to destroy if empty.
  */
 void
-GCT_destroy_if_empty (struct CadetTunnel3 *t)
+GCT_destroy_if_empty (struct CadetTunnel *t)
 {
   LOG (GNUNET_ERROR_TYPE_DEBUG, "Tunnel %s destroy if empty\n", GCT_2s (t));
   if (1 < GCT_count_channels (t))
@@ -2221,7 +2392,7 @@ GCT_destroy_if_empty (struct CadetTunnel3 *t)
  * @param t The tunnel to destroy.
  */
 void
-GCT_destroy (struct CadetTunnel3 *t)
+GCT_destroy (struct CadetTunnel *t)
 {
   struct CadetTConnection *iter_c;
   struct CadetTConnection *next_c;
@@ -2263,12 +2434,13 @@ GCT_destroy (struct CadetTunnel3 *t)
   {
     GNUNET_SCHEDULER_cancel (t->rekey_task);
     t->rekey_task = GNUNET_SCHEDULER_NO_TASK;
-    if (NULL != t->kx_ctx)
-      GNUNET_free (t->kx_ctx);
-    else
-      GNUNET_break (0);
   }
-
+  if (NULL != t->kx_ctx)
+  {
+    if (GNUNET_SCHEDULER_NO_TASK != t->kx_ctx->finish_task)
+      GNUNET_SCHEDULER_cancel (t->kx_ctx->finish_task);
+    GNUNET_free (t->kx_ctx);
+  }
   GNUNET_free (t);
 }
 
@@ -2284,7 +2456,7 @@ GCT_destroy (struct CadetTunnel3 *t)
  * @return Connection created.
  */
 struct CadetConnection *
-GCT_use_path (struct CadetTunnel3 *t, struct CadetPeerPath *p)
+GCT_use_path (struct CadetTunnel *t, struct CadetPeerPath *p)
 {
   struct CadetConnection *c;
   struct GNUNET_CADET_Hash cid;
@@ -2333,7 +2505,7 @@ GCT_use_path (struct CadetTunnel3 *t, struct CadetPeerPath *p)
  * @return Number of connections.
  */
 unsigned int
-GCT_count_connections (struct CadetTunnel3 *t)
+GCT_count_connections (struct CadetTunnel *t)
 {
   struct CadetTConnection *iter;
   unsigned int count;
@@ -2356,7 +2528,7 @@ GCT_count_connections (struct CadetTunnel3 *t)
  * @return Number of channels.
  */
 unsigned int
-GCT_count_channels (struct CadetTunnel3 *t)
+GCT_count_channels (struct CadetTunnel *t)
 {
   struct CadetTChannel *iter;
   unsigned int count;
@@ -2376,13 +2548,13 @@ GCT_count_channels (struct CadetTunnel3 *t)
  *
  * @return Tunnel's connectivity state.
  */
-enum CadetTunnel3CState
-GCT_get_cstate (struct CadetTunnel3 *t)
+enum CadetTunnelCState
+GCT_get_cstate (struct CadetTunnel *t)
 {
   if (NULL == t)
   {
     GNUNET_assert (0);
-    return (enum CadetTunnel3CState) -1;
+    return (enum CadetTunnelCState) -1;
   }
   return t->cstate;
 }
@@ -2395,13 +2567,13 @@ GCT_get_cstate (struct CadetTunnel3 *t)
  *
  * @return Tunnel's encryption state.
  */
-enum CadetTunnel3EState
-GCT_get_estate (struct CadetTunnel3 *t)
+enum CadetTunnelEState
+GCT_get_estate (struct CadetTunnel *t)
 {
   if (NULL == t)
   {
     GNUNET_assert (0);
-    return (enum CadetTunnel3EState) -1;
+    return (enum CadetTunnelEState) -1;
   }
   return t->estate;
 }
@@ -2414,7 +2586,7 @@ GCT_get_estate (struct CadetTunnel3 *t)
  * @return Biggest buffer space offered by any channel in the tunnel.
  */
 unsigned int
-GCT_get_channels_buffer (struct CadetTunnel3 *t)
+GCT_get_channels_buffer (struct CadetTunnel *t)
 {
   struct CadetTChannel *iter;
   unsigned int buffer;
@@ -2445,7 +2617,7 @@ GCT_get_channels_buffer (struct CadetTunnel3 *t)
  * @return Buffer space offered by all connections in the tunnel.
  */
 unsigned int
-GCT_get_connections_buffer (struct CadetTunnel3 *t)
+GCT_get_connections_buffer (struct CadetTunnel *t)
 {
   struct CadetTConnection *iter;
   unsigned int buffer;
@@ -2472,7 +2644,7 @@ GCT_get_connections_buffer (struct CadetTunnel3 *t)
  * @return ID of the destination peer.
  */
 const struct GNUNET_PeerIdentity *
-GCT_get_destination (struct CadetTunnel3 *t)
+GCT_get_destination (struct CadetTunnel *t)
 {
   return GCP_get_id (t->peer);
 }
@@ -2486,7 +2658,7 @@ GCT_get_destination (struct CadetTunnel3 *t)
  * @return GID of a channel free to use.
  */
 CADET_ChannelNumber
-GCT_get_next_chid (struct CadetTunnel3 *t)
+GCT_get_next_chid (struct CadetTunnel *t)
 {
   CADET_ChannelNumber chid;
   CADET_ChannelNumber mask;
@@ -2523,7 +2695,7 @@ GCT_get_next_chid (struct CadetTunnel3 *t)
  * @param t Channel which has some free buffer space.
  */
 void
-GCT_unchoke_channels (struct CadetTunnel3 *t)
+GCT_unchoke_channels (struct CadetTunnel *t)
 {
   struct CadetTChannel *iter;
   unsigned int buffer;
@@ -2574,7 +2746,7 @@ GCT_unchoke_channels (struct CadetTunnel3 *t)
  * @param t Tunnel.
  */
 void
-GCT_send_connection_acks (struct CadetTunnel3 *t)
+GCT_send_connection_acks (struct CadetTunnel *t)
 {
   struct CadetTConnection *iter;
   uint32_t allowed;
@@ -2641,7 +2813,7 @@ GCT_send_connection_acks (struct CadetTunnel3 *t)
  * @param q Handle to the queue.
  */
 void
-GCT_cancel (struct CadetTunnel3Queue *q)
+GCT_cancel (struct CadetTunnelQueue *q)
 {
   if (NULL != q->cq)
   {
@@ -2676,9 +2848,9 @@ GCT_cancel (struct CadetTunnel3Queue *q)
  *
  * @return Handle to cancel message. NULL if @c cont is NULL.
  */
-struct CadetTunnel3Queue *
+struct CadetTunnelQueue *
 GCT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
-                           struct CadetTunnel3 *t, struct CadetConnection *c,
+                           struct CadetTunnel *t, struct CadetConnection *c,
                            int force, GCT_sent cont, void *cont_cls)
 {
   return send_prebuilt_message (message, t, c, force, cont, cont_cls, NULL);
@@ -2693,7 +2865,7 @@ GCT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
  * @return #GNUNET_YES if it is loopback.
  */
 int
-GCT_is_loopback (const struct CadetTunnel3 *t)
+GCT_is_loopback (const struct CadetTunnel *t)
 {
   return (myid == GCP_get_short_id (t->peer));
 }
@@ -2708,7 +2880,7 @@ GCT_is_loopback (const struct CadetTunnel3 *t)
  * @return #GNUNET_YES a connection uses this path.
  */
 int
-GCT_is_path_used (const struct CadetTunnel3 *t, const struct CadetPeerPath *p)
+GCT_is_path_used (const struct CadetTunnel *t, const struct CadetPeerPath *p)
 {
   struct CadetTConnection *iter;
 
@@ -2729,7 +2901,7 @@ GCT_is_path_used (const struct CadetTunnel3 *t, const struct CadetPeerPath *p)
  * @return Cost of the path (path length + number of overlapping nodes)
  */
 unsigned int
-GCT_get_path_cost (const struct CadetTunnel3 *t,
+GCT_get_path_cost (const struct CadetTunnel *t,
                    const struct CadetPeerPath *path)
 {
   struct CadetTConnection *iter;
@@ -2774,7 +2946,7 @@ GCT_get_path_cost (const struct CadetTunnel3 *t,
  * @return Static string the destination peer's ID.
  */
 const char *
-GCT_2s (const struct CadetTunnel3 *t)
+GCT_2s (const struct CadetTunnel *t)
 {
   if (NULL == t)
     return "(NULL)";
@@ -2794,7 +2966,7 @@ GCT_2s (const struct CadetTunnel3 *t)
  * @param t Tunnel to debug.
  */
 void
-GCT_debug (const struct CadetTunnel3 *t)
+GCT_debug (const struct CadetTunnel *t)
 {
   struct CadetTChannel *iterch;
   struct CadetTConnection *iterc;
@@ -2862,7 +3034,7 @@ GCT_count_all (void)
  * @param cls Closure for @c iter.
  */
 void
-GCT_iterate_connections (struct CadetTunnel3 *t, GCT_conn_iter iter, void *cls)
+GCT_iterate_connections (struct CadetTunnel *t, GCT_conn_iter iter, void *cls)
 {
   struct CadetTConnection *ct;
 
@@ -2879,7 +3051,7 @@ GCT_iterate_connections (struct CadetTunnel3 *t, GCT_conn_iter iter, void *cls)
  * @param cls Closure for @c iter.
  */
 void
-GCT_iterate_channels (struct CadetTunnel3 *t, GCT_chan_iter iter, void *cls)
+GCT_iterate_channels (struct CadetTunnel *t, GCT_chan_iter iter, void *cls)
 {
   struct CadetTChannel *cht;