pass only CadetTunnelAxolotl if it suffices, preparation for having ambiguous KX...
[oweals/gnunet.git] / src / cadet / gnunet-service-cadet_tunnel.c
index 05cd5f066c91fa41912caba4a4a987db3d9690d3..3b21f4107fb0d0e2c84f5a87202487a39026c38b 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     Copyright (C) 2013 GNUnet e.V.
+     Copyright (C) 2013, 2017 GNUnet e.V.
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
      Boston, MA 02110-1301, USA.
 */
-
+/**
+ * @file cadet/gnunet-service-cadet_tunnel.c
+ * @brief logical links between CADET clients
+ * @author Bartlomiej Polot
+ */
 #include "platform.h"
 #include "gnunet_util_lib.h"
-
 #include "gnunet_signatures.h"
 #include "gnunet_statistics_service.h"
-
 #include "cadet_protocol.h"
 #include "cadet_path.h"
-
 #include "gnunet-service-cadet_tunnel.h"
 #include "gnunet-service-cadet_connection.h"
 #include "gnunet-service-cadet_channel.h"
@@ -38,9 +39,9 @@
 #define REKEY_WAIT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5)
 
 #if !defined(GNUNET_CULL_LOGGING)
-#define DUMP_KEYS_TO_STDERR GNUNET_YES
+  #define DUMP_KEYS_TO_STDERR GNUNET_YES
 #else
-#define DUMP_KEYS_TO_STDERR GNUNET_YES
+  #define DUMP_KEYS_TO_STDERR GNUNET_NO
 #endif
 
 #define MIN_TUNNEL_BUFFER       8
@@ -50,7 +51,6 @@
 #define AX_HEADER_SIZE (sizeof (uint32_t) * 2\
                         + sizeof (struct GNUNET_CRYPTO_EcdhePublicKey))
 
-
 /******************************************************************************/
 /********************************   STRUCTS  **********************************/
 /******************************************************************************/
@@ -64,7 +64,7 @@ struct CadetTChannel
 
 
 /**
- * Connection list and metadata.
+ * Entry in list of connections used by tunnel, with metadata.
  */
 struct CadetTConnection
 {
@@ -95,18 +95,6 @@ struct CadetTConnection
 };
 
 
-/**
- * Encryption systems possible.
- */
-enum CadetTunnelEncryption
-{
-  /**
-   * Default Axolotl system.
-   */
-  CADET_Axolotl
-
-};
-
 /**
  * Struct to old keys for skipped messages while advancing the Axolotl ratchet.
  */
@@ -256,6 +244,7 @@ struct CadetTunnelAxolotl
   struct GNUNET_TIME_Absolute ratchet_expiration;
 };
 
+
 /**
  * Struct containing all information regarding a tunnel to a peer.
  */
@@ -266,11 +255,6 @@ struct CadetTunnel
    */
   struct CadetPeer *peer;
 
-  /**
-   * Type of encryption used in the tunnel.
-   */
-  enum CadetTunnelEncryption enc_type;
-
   /**
    * Axolotl info.
    */
@@ -327,7 +311,7 @@ struct CadetTunnel
   /**
    * Channel ID for the next created channel.
    */
-  CADET_ChannelNumber next_chid;
+  struct GNUNET_CADET_ChannelTunnelNumber next_ctn;
 
   /**
    * Destroy flag: if true, destroy on last message.
@@ -550,8 +534,7 @@ is_ready (struct CadetTunnel *t)
   conn_ok = CADET_TUNNEL_READY == t->cstate;
   enc_ok = CADET_TUNNEL_KEY_OK == t->estate
            || CADET_TUNNEL_KEY_REKEY == t->estate
-           || (CADET_TUNNEL_KEY_PING == t->estate
-               && CADET_Axolotl == t->enc_type);
+           || CADET_TUNNEL_KEY_PING == t->estate;
   ready = conn_ok && enc_ok;
   ready = ready || GCT_is_loopback (t);
   return ready;
@@ -667,7 +650,7 @@ new_ephemeral (struct CadetTunnel *t)
 static void
 t_hmac (const void *plaintext, size_t size,
         uint32_t iv, const struct GNUNET_CRYPTO_SymmetricSessionKey *key,
-        struct GNUNET_CADET_Hash *hmac)
+        struct GNUNET_ShortHashCode *hmac)
 {
   static const char ctx[] = "cadet authentication key";
   struct GNUNET_CRYPTO_AuthKey auth_key;
@@ -751,10 +734,9 @@ t_ax_encrypt (struct CadetTunnel *t, void *dst, const void *src, size_t size)
   struct CadetTunnelAxolotl *ax;
   size_t out_size;
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  t_ax_encrypt start\n");
+  CADET_TIMING_START;
 
   ax = t->ax;
-
   ax->ratchet_counter++;
   if (GNUNET_YES == ax->ratchet_allowed
       && (ratchet_messages <= ax->ratchet_counter
@@ -803,10 +785,9 @@ t_ax_encrypt (struct CadetTunnel *t, void *dst, const void *src, size_t size)
   #endif
 
   out_size = GNUNET_CRYPTO_symmetric_encrypt (src, size, &MK, &iv, dst);
-
   t_hmac_derive_key (&ax->CKs, &ax->CKs, "1", 1);
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  t_ax_encrypt end\n");
+  CADET_TIMING_END;
 
   return out_size;
 }
@@ -830,7 +811,7 @@ t_ax_decrypt (struct CadetTunnel *t, void *dst, const void *src, size_t size)
   struct CadetTunnelAxolotl *ax;
   size_t out_size;
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  t_ax_decrypt start\n");
+  CADET_TIMING_START;
 
   ax = t->ax;
 
@@ -850,7 +831,7 @@ t_ax_decrypt (struct CadetTunnel *t, void *dst, const void *src, size_t size)
 
   t_hmac_derive_key (&ax->CKr, &ax->CKr, "1", 1);
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  t_ax_decrypt end\n");
+  CADET_TIMING_END;
 
   return out_size;
 }
@@ -863,14 +844,13 @@ t_ax_decrypt (struct CadetTunnel *t, void *dst, const void *src, size_t size)
  * @param msg Message whose header to encrypt.
  */
 static void
-t_h_encrypt (struct CadetTunnel *t, struct GNUNET_CADET_AX *msg)
+t_h_encrypt (struct CadetTunnel *t, struct GNUNET_CADET_TunnelEncryptedMessage *msg)
 {
   struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
   struct CadetTunnelAxolotl *ax;
   size_t out_size;
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  t_h_encrypt start\n");
-
+  CADET_TIMING_START;
   ax = t->ax;
   GNUNET_CRYPTO_symmetric_derive_iv (&iv, &ax->HKs, NULL, 0, NULL);
 
@@ -883,8 +863,7 @@ t_h_encrypt (struct CadetTunnel *t, struct GNUNET_CADET_AX *msg)
                                               &ax->HKs, &iv, &msg->Ns);
 
   GNUNET_assert (AX_HEADER_SIZE == out_size);
-
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  t_ax_encrypt end\n");
+  CADET_TIMING_END;
 }
 
 
@@ -896,14 +875,14 @@ t_h_encrypt (struct CadetTunnel *t, struct GNUNET_CADET_AX *msg)
  * @param dst Where to decrypt header to.
  */
 static void
-t_h_decrypt (struct CadetTunnel *t, const struct GNUNET_CADET_AX *src,
-             struct GNUNET_CADET_AX *dst)
+t_h_decrypt (struct CadetTunnel *t, const struct GNUNET_CADET_TunnelEncryptedMessage *src,
+             struct GNUNET_CADET_TunnelEncryptedMessage *dst)
 {
   struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
   struct CadetTunnelAxolotl *ax;
   size_t out_size;
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  t_h_decrypt start\n");
+  CADET_TIMING_START;
 
   ax = t->ax;
   GNUNET_CRYPTO_symmetric_derive_iv (&iv, &ax->HKr, NULL, 0, NULL);
@@ -918,7 +897,7 @@ t_h_decrypt (struct CadetTunnel *t, const struct GNUNET_CADET_AX *src,
 
   GNUNET_assert (AX_HEADER_SIZE == out_size);
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  t_h_decrypt end\n");
+  CADET_TIMING_END;
 }
 
 
@@ -935,12 +914,12 @@ t_h_decrypt (struct CadetTunnel *t, const struct GNUNET_CADET_AX *src,
  */
 static int
 try_old_ax_keys (struct CadetTunnel *t, void *dst,
-                 const struct GNUNET_CADET_AX *src, size_t size)
+                 const struct GNUNET_CADET_TunnelEncryptedMessage *src, size_t size)
 {
   struct CadetTunnelSkippedKey *key;
-  struct GNUNET_CADET_Hash *hmac;
+  struct GNUNET_ShortHashCode *hmac;
   struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
-  struct GNUNET_CADET_AX plaintext_header;
+  struct GNUNET_CADET_TunnelEncryptedMessage plaintext_header;
   struct GNUNET_CRYPTO_SymmetricSessionKey *valid_HK;
   size_t esize;
   size_t res;
@@ -949,7 +928,7 @@ try_old_ax_keys (struct CadetTunnel *t, void *dst,
 
   LOG (GNUNET_ERROR_TYPE_DEBUG, "Trying old keys\n");
   hmac = &plaintext_header.hmac;
-  esize = size - sizeof (struct GNUNET_CADET_AX);
+  esize = size - sizeof (struct GNUNET_CADET_TunnelEncryptedMessage);
 
   /* Find a correct Header Key */
   for (key = t->ax->skipped_head; NULL != key; key = key->next)
@@ -970,8 +949,8 @@ try_old_ax_keys (struct CadetTunnel *t, void *dst,
     return -1;
 
   /* Should've been checked in -cadet_connection.c handle_cadet_encrypted. */
-  GNUNET_assert (size > sizeof (struct GNUNET_CADET_AX));
-  len = size - sizeof (struct GNUNET_CADET_AX);
+  GNUNET_assert (size > sizeof (struct GNUNET_CADET_TunnelEncryptedMessage));
+  len = size - sizeof (struct GNUNET_CADET_TunnelEncryptedMessage);
   GNUNET_assert (len >= sizeof (struct GNUNET_MessageHeader));
 
   /* Decrypt header */
@@ -1114,20 +1093,20 @@ store_ax_keys (struct CadetTunnel *t,
  */
 static int
 t_ax_decrypt_and_validate (struct CadetTunnel *t, void *dst,
-                           const struct GNUNET_CADET_AX *src, size_t size)
+                           const struct GNUNET_CADET_TunnelEncryptedMessage *src,
+                           size_t size)
 {
   struct CadetTunnelAxolotl *ax;
-  struct GNUNET_CADET_Hash msg_hmac;
+  struct GNUNET_ShortHashCode msg_hmac;
   struct GNUNET_HashCode hmac;
-  struct GNUNET_CADET_AX plaintext_header;
+  struct GNUNET_CADET_TunnelEncryptedMessage plaintext_header;
   uint32_t Np;
   uint32_t PNp;
-  size_t esize;
-  size_t osize;
+  size_t esize; /* Size of encryped payload */
+  size_t osize; /* Size of output (decrypted payload) */
 
+  esize = size - sizeof (struct GNUNET_CADET_TunnelEncryptedMessage);
   ax = t->ax;
-  esize = size - sizeof (struct GNUNET_CADET_AX);
-
   if (NULL == ax)
     return -1;
 
@@ -1344,12 +1323,11 @@ send_prebuilt_message (const struct GNUNET_MessageHeader *message,
                        struct CadetTunnelQueue *existing_q)
 {
   struct GNUNET_MessageHeader *msg;
-  struct GNUNET_CADET_AX *ax_msg;
+  struct GNUNET_CADET_TunnelEncryptedMessage *ax_msg;
   struct CadetTunnelQueue *tq;
   size_t size = ntohs (message->size);
-  char cbuf[sizeof (struct GNUNET_CADET_AX) + size] GNUNET_ALIGN;
+  char cbuf[sizeof (struct GNUNET_CADET_TunnelEncryptedMessage) + size] GNUNET_ALIGN;
   size_t esize;
-  uint32_t mid;
   uint16_t type;
   int fwd;
 
@@ -1375,11 +1353,10 @@ send_prebuilt_message (const struct GNUNET_MessageHeader *message,
 
   GNUNET_assert (GNUNET_NO == GCT_is_loopback (t));
 
-  GNUNET_assert (CADET_Axolotl == t->enc_type);
-  ax_msg = (struct GNUNET_CADET_AX *) cbuf;
+  ax_msg = (struct GNUNET_CADET_TunnelEncryptedMessage *) cbuf;
   msg = &ax_msg->header;
-  msg->size = htons (sizeof (struct GNUNET_CADET_AX) + size);
-  msg->type = htons (GNUNET_MESSAGE_TYPE_CADET_AX);
+  msg->size = htons (sizeof (struct GNUNET_CADET_TunnelEncryptedMessage) + size);
+  msg->type = htons (GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED);
   esize = t_ax_encrypt (t, &ax_msg[1], message, size);
   ax_msg->Ns = htonl (t->ax->Ns++);
   ax_msg->PNs = htonl (t->ax->PNs);
@@ -1400,36 +1377,25 @@ send_prebuilt_message (const struct GNUNET_MessageHeader *message,
     }
     return NULL; /* Drop... */
   }
-
-  mid = 0;
-  type = ntohs (message->type);
-  switch (type)
-  {
-    case GNUNET_MESSAGE_TYPE_CADET_DATA:
-    case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK:
-      if (GNUNET_MESSAGE_TYPE_CADET_DATA == type)
-        mid = ntohl (((struct GNUNET_CADET_Data *) message)->mid);
-      else
-        mid = ntohl (((struct GNUNET_CADET_DataACK *) message)->mid);
-      /* Fall thru */
-    case GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE:
-    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
-    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
-    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK:
-    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK:
-      break;
-    default:
-      GNUNET_break (0);
-      LOG (GNUNET_ERROR_TYPE_ERROR, "type %s not valid\n", GC_m2s (type));
-  }
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "type %s\n", GC_m2s (type));
-
   fwd = GCC_is_origin (c, GNUNET_YES);
+  ax_msg->cid = *GCC_get_id (c);
+  ax_msg->cemi = GCC_get_pid (c, fwd);
+
+  type = htons (message->type);
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Sending message of type %s with CEMI %u and CID %s\n",
+       GC_m2s (type),
+       htonl (ax_msg->cemi.pid),
+       GNUNET_sh2s (&ax_msg->cid.connection_of_tunnel));
 
   if (NULL == cont)
   {
-    GNUNET_break (NULL == GCC_send_prebuilt_message (msg, type, mid, c, fwd,
-                                                     force, NULL, NULL));
+    (void) GCC_send_prebuilt_message (msg,
+                                      type,
+                                      ax_msg->cemi,
+                                      c,
+                                      fwd,
+                                      force, NULL, NULL);
     return NULL;
   }
   if (NULL == existing_q)
@@ -1441,11 +1407,16 @@ send_prebuilt_message (const struct GNUNET_MessageHeader *message,
     tq = existing_q;
     tq->tqd = NULL;
   }
-  tq->cq = GCC_send_prebuilt_message (msg, type, mid, c, fwd, force,
-                                      &tun_message_sent, tq);
-  GNUNET_assert (NULL != tq->cq);
   tq->cont = cont;
   tq->cont_cls = cont_cls;
+  tq->cq = GCC_send_prebuilt_message (msg,
+                                      type,
+                                      ax_msg->cemi,
+                                      c,
+                                      fwd,
+                                      force,
+                                      &tun_message_sent, tq);
+  GNUNET_assert (NULL != tq->cq);
 
   return tq;
 }
@@ -1498,12 +1469,12 @@ send_queued_data (struct CadetTunnel *t)
 
 
 /**
- * @brief Resend the AX KX until we complete the handshake.
+ * @brief Resend the KX until we complete the handshake.
  *
  * @param cls Closure (tunnel).
  */
 static void
-ax_kx_resend (void *cls)
+kx_resend (void *cls)
 {
   struct CadetTunnel *t = cls;
 
@@ -1515,7 +1486,7 @@ ax_kx_resend (void *cls)
     return;
   }
 
-  GCT_send_ax_kx (t, CADET_TUNNEL_KEY_SENT >= t->estate);
+  GCT_send_kx (t, CADET_TUNNEL_KEY_SENT >= t->estate);
 }
 
 
@@ -1542,96 +1513,15 @@ ephm_sent (void *cls,
   if (CADET_TUNNEL_KEY_OK == t->estate)
     return;
 
-  if (CADET_Axolotl == t->enc_type)
-  {
-    if (NULL != t->rekey_task)
-    {
-      GNUNET_break (0);
-      GCT_debug (t, GNUNET_ERROR_TYPE_WARNING);
-      GNUNET_SCHEDULER_cancel (t->rekey_task);
-    }
-    t->rekey_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
-                                                  &ax_kx_resend, t);
-  }
-}
-
-
-/**
- * Sends key exchange message on a tunnel, choosing the best connection.
- * Should not be called on loopback tunnels.
- *
- * @param t Tunnel on which this message is transmitted.
- * @param message Message to send. Function modifies it.
- *
- * @return Handle to the message in the connection queue.
- */
-static struct CadetConnectionQueue *
-send_kx (struct CadetTunnel *t,
-         const struct GNUNET_MessageHeader *message)
-{
-  struct CadetConnection *c;
-  struct GNUNET_CADET_KX *msg;
-  size_t size = ntohs (message->size);
-  char cbuf[sizeof (struct GNUNET_CADET_KX) + size];
-  uint16_t type;
-  int fwd;
-
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT KX on Tunnel %s\n", GCT_2s (t));
-
-  /* Avoid loopback. */
-  if (GCT_is_loopback (t))
+  if (NULL != t->rekey_task)
   {
     GNUNET_break (0);
-    return NULL;
-  }
-  type = ntohs (message->type);
-
-  /* Even if tunnel is "being destroyed", send anyway.
-   * Could be a response to a rekey initiated by remote peer,
-   * who is trying to create a new channel!
-   */
-
-  /* Must have a connection, or be looking for one. */
-  if (NULL == t->connection_head)
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "%s with no connection\n", GC_m2s (type));
-    if (CADET_TUNNEL_SEARCHING != t->cstate)
-    {
-      GNUNET_break (0);
-      GCT_debug (t, GNUNET_ERROR_TYPE_ERROR);
-    }
-    return NULL;
-  }
-
-  msg = (struct GNUNET_CADET_KX *) cbuf;
-  msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_KX);
-  msg->header.size = htons (sizeof (struct GNUNET_CADET_KX) + size);
-  c = tunnel_get_connection (t);
-  if (NULL == c)
-  {
-    if (NULL == t->destroy_task && CADET_TUNNEL_READY == t->cstate)
-    {
-      GNUNET_break (0);
-      GCT_debug (t, GNUNET_ERROR_TYPE_ERROR);
-    }
-    return NULL;
-  }
-  switch (type)
-  {
-    case GNUNET_MESSAGE_TYPE_CADET_AX_KX:
-      GNUNET_assert (NULL == t->ephm_h);
-      break;
-    default:
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n", GC_m2s (type));
-      GNUNET_assert (0);
+    GCT_debug (t, GNUNET_ERROR_TYPE_WARNING);
+    GNUNET_SCHEDULER_cancel (t->rekey_task);
   }
-  GNUNET_memcpy (&msg[1], message, size);
-
-  fwd = GCC_is_origin (c, GNUNET_YES);
+  t->rekey_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
+                                                &kx_resend, t);
 
-  return GCC_send_prebuilt_message (&msg->header, type, 0, c,
-                                    fwd, GNUNET_YES,
-                                    &ephm_sent, t);
 }
 
 
@@ -1666,17 +1556,19 @@ destroy_iterator (void *cls,
  * @param gid ID of the channel.
  */
 static void
-send_channel_destroy (struct CadetTunnel *t, unsigned int gid)
+send_channel_destroy (struct CadetTunnel *t,
+                      struct GNUNET_CADET_ChannelTunnelNumber gid)
 {
-  struct GNUNET_CADET_ChannelManage msg;
+  struct GNUNET_CADET_ChannelManageMessage msg;
 
   msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
   msg.header.size = htons (sizeof (msg));
-  msg.chid = htonl (gid);
+  msg.ctn = gid;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "WARNING destroying unknown channel %u on tunnel %s\n",
-       gid, GCT_2s (t));
+       ntohl (gid.cn),
+       GCT_2s (t));
   send_prebuilt_message (&msg.header, t, NULL, GNUNET_YES, NULL, NULL, NULL);
 }
 
@@ -1693,7 +1585,7 @@ send_channel_destroy (struct CadetTunnel *t, unsigned int gid)
  */
 static void
 handle_data (struct CadetTunnel *t,
-             const struct GNUNET_CADET_Data *msg,
+             const struct GNUNET_CADET_ChannelAppDataMessage *msg,
              int fwd)
 {
   struct CadetChannel *ch;
@@ -1704,7 +1596,7 @@ handle_data (struct CadetTunnel *t,
   /* Check size */
   size = ntohs (msg->header.size);
   if (size <
-      sizeof (struct GNUNET_CADET_Data) +
+      sizeof (struct GNUNET_CADET_ChannelAppDataMessage) +
       sizeof (struct GNUNET_MessageHeader))
   {
     GNUNET_break (0);
@@ -1717,13 +1609,17 @@ handle_data (struct CadetTunnel *t,
 
 
   /* Check channel */
-  ch = GCT_get_channel (t, ntohl (msg->chid));
+  ch = GCT_get_channel (t, msg->ctn);
   if (NULL == ch)
   {
-    GNUNET_STATISTICS_update (stats, "# data on unknown channel",
-                              1, GNUNET_NO);
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "channel 0x%X unknown\n", ntohl (msg->chid));
-    send_channel_destroy (t, ntohl (msg->chid));
+    GNUNET_STATISTICS_update (stats,
+                              "# data on unknown channel",
+                              1,
+                              GNUNET_NO);
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "channel 0x%X unknown\n",
+         ntohl (msg->ctn.cn));
+    send_channel_destroy (t, msg->ctn);
     return;
   }
 
@@ -1743,7 +1639,7 @@ handle_data (struct CadetTunnel *t,
  */
 static void
 handle_data_ack (struct CadetTunnel *t,
-                 const struct GNUNET_CADET_DataACK *msg,
+                 const struct GNUNET_CADET_ChannelDataAckMessage *msg,
                  int fwd)
 {
   struct CadetChannel *ch;
@@ -1751,20 +1647,20 @@ handle_data_ack (struct CadetTunnel *t,
 
   /* Check size */
   size = ntohs (msg->header.size);
-  if (size != sizeof (struct GNUNET_CADET_DataACK))
+  if (size != sizeof (struct GNUNET_CADET_ChannelDataAckMessage))
   {
     GNUNET_break (0);
     return;
   }
 
   /* Check channel */
-  ch = GCT_get_channel (t, ntohl (msg->chid));
+  ch = GCT_get_channel (t, msg->ctn);
   if (NULL == ch)
   {
     GNUNET_STATISTICS_update (stats, "# data ack on unknown channel",
                               1, GNUNET_NO);
     LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
-         ntohl (msg->chid));
+         ntohl (msg->ctn.cn));
     return;
   }
 
@@ -1780,21 +1676,21 @@ handle_data_ack (struct CadetTunnel *t,
  */
 static void
 handle_ch_create (struct CadetTunnel *t,
-                  const struct GNUNET_CADET_ChannelCreate *msg)
+                  const struct GNUNET_CADET_ChannelOpenMessage *msg)
 {
   struct CadetChannel *ch;
   size_t size;
 
   /* Check size */
   size = ntohs (msg->header.size);
-  if (size != sizeof (struct GNUNET_CADET_ChannelCreate))
+  if (size != sizeof (struct GNUNET_CADET_ChannelOpenMessage))
   {
     GNUNET_break_op (0);
     return;
   }
 
   /* Check channel */
-  ch = GCT_get_channel (t, ntohl (msg->chid));
+  ch = GCT_get_channel (t, msg->ctn);
   if (NULL != ch && ! GCT_is_loopback (t))
   {
     /* Probably a retransmission, safe to ignore */
@@ -1815,27 +1711,28 @@ handle_ch_create (struct CadetTunnel *t,
  */
 static void
 handle_ch_nack (struct CadetTunnel *t,
-                const struct GNUNET_CADET_ChannelManage *msg)
+                const struct GNUNET_CADET_ChannelManageMessage *msg)
 {
   struct CadetChannel *ch;
   size_t size;
 
   /* Check size */
   size = ntohs (msg->header.size);
-  if (size != sizeof (struct GNUNET_CADET_ChannelManage))
+  if (size != sizeof (struct GNUNET_CADET_ChannelManageMessage))
   {
     GNUNET_break (0);
     return;
   }
 
   /* Check channel */
-  ch = GCT_get_channel (t, ntohl (msg->chid));
+  ch = GCT_get_channel (t, msg->ctn);
   if (NULL == ch)
   {
     GNUNET_STATISTICS_update (stats, "# channel NACK on unknown channel",
                               1, GNUNET_NO);
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
-         ntohl (msg->chid));
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "WARNING channel %u unknown\n",
+         ntohl (msg->ctn.cn));
     return;
   }
 
@@ -1855,7 +1752,7 @@ handle_ch_nack (struct CadetTunnel *t,
  */
 static void
 handle_ch_ack (struct CadetTunnel *t,
-               const struct GNUNET_CADET_ChannelManage *msg,
+               const struct GNUNET_CADET_ChannelManageMessage *msg,
                int fwd)
 {
   struct CadetChannel *ch;
@@ -1863,20 +1760,23 @@ handle_ch_ack (struct CadetTunnel *t,
 
   /* Check size */
   size = ntohs (msg->header.size);
-  if (size != sizeof (struct GNUNET_CADET_ChannelManage))
+  if (size != sizeof (struct GNUNET_CADET_ChannelManageMessage))
   {
     GNUNET_break (0);
     return;
   }
 
   /* Check channel */
-  ch = GCT_get_channel (t, ntohl (msg->chid));
+  ch = GCT_get_channel (t, msg->ctn);
   if (NULL == ch)
   {
-    GNUNET_STATISTICS_update (stats, "# channel ack on unknown channel",
-                              1, GNUNET_NO);
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
-         ntohl (msg->chid));
+    GNUNET_STATISTICS_update (stats,
+                              "# channel ack on unknown channel",
+                              1,
+                              GNUNET_NO);
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "WARNING channel %u unknown\n",
+         ntohl (msg->ctn.cn));
     return;
   }
 
@@ -1896,7 +1796,7 @@ handle_ch_ack (struct CadetTunnel *t,
  */
 static void
 handle_ch_destroy (struct CadetTunnel *t,
-                   const struct GNUNET_CADET_ChannelManage *msg,
+                   const struct GNUNET_CADET_ChannelManageMessage *msg,
                    int fwd)
 {
   struct CadetChannel *ch;
@@ -1904,14 +1804,14 @@ handle_ch_destroy (struct CadetTunnel *t,
 
   /* Check size */
   size = ntohs (msg->header.size);
-  if (size != sizeof (struct GNUNET_CADET_ChannelManage))
+  if (size != sizeof (struct GNUNET_CADET_ChannelManageMessage))
   {
     GNUNET_break (0);
     return;
   }
 
   /* Check channel */
-  ch = GCT_get_channel (t, ntohl (msg->chid));
+  ch = GCT_get_channel (t, msg->ctn);
   if (NULL == ch)
   {
     /* Probably a retransmission, safe to ignore */
@@ -1956,13 +1856,145 @@ destroy_ax (struct CadetTunnel *t)
 
 
 /**
- * Handle Axolotl handshake.
+ * Demultiplex by message type and call appropriate handler for a message
+ * towards a channel of a local tunnel.
  *
  * @param t Tunnel this message came on.
- * @param msg Key eXchange Pong message.
+ * @param msgh Message header.
+ * @param fwd Is this message fwd? This only is meaningful in loopback channels.
+ *            #GNUNET_YES if message is FWD on the respective channel (loopback)
+ *            #GNUNET_NO if message is BCK on the respective channel (loopback)
+ *            #GNUNET_SYSERR if message on a one-ended channel (remote)
  */
 static void
-handle_kx_ax (struct CadetTunnel *t, const struct GNUNET_CADET_AX_KX *msg)
+handle_decrypted (struct CadetTunnel *t,
+                  const struct GNUNET_MessageHeader *msgh,
+                  int fwd)
+{
+  uint16_t type;
+  char buf[256];
+
+  type = ntohs (msgh->type);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "<-- %s on %s\n", GC_m2s (type), GCT_2s (t));
+  SPRINTF (buf, "# received encrypted of type %hu (%s)", type, GC_m2s (type));
+  GNUNET_STATISTICS_update (stats, buf, 1, GNUNET_NO);
+
+  switch (type)
+  {
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_KEEPALIVE:
+      /* Do nothing, connection aleady got updated. */
+      GNUNET_STATISTICS_update (stats, "# keepalives received", 1, GNUNET_NO);
+      break;
+
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA:
+      /* Don't send hop ACK, wait for client to ACK */
+      handle_data (t, (struct GNUNET_CADET_ChannelAppDataMessage *) msgh, fwd);
+      break;
+
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK:
+      handle_data_ack (t, (struct GNUNET_CADET_ChannelDataAckMessage *) msgh, fwd);
+      break;
+
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN:
+      handle_ch_create (t, (struct GNUNET_CADET_ChannelOpenMessage *) msgh);
+      break;
+
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_NACK_DEPRECATED:
+      handle_ch_nack (t, (struct GNUNET_CADET_ChannelManageMessage *) msgh);
+      break;
+
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK:
+      handle_ch_ack (t, (struct GNUNET_CADET_ChannelManageMessage *) msgh, fwd);
+      break;
+
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
+      handle_ch_destroy (t, (struct GNUNET_CADET_ChannelManageMessage *) msgh, fwd);
+      break;
+
+    default:
+      GNUNET_break_op (0);
+      LOG (GNUNET_ERROR_TYPE_WARNING,
+           "end-to-end message not known (%u)\n",
+           ntohs (msgh->type));
+      GCT_debug (t, GNUNET_ERROR_TYPE_WARNING);
+  }
+}
+
+
+/******************************************************************************/
+/********************************    API    ***********************************/
+/******************************************************************************/
+
+/**
+ * Decrypt and process an encrypted message.
+ *
+ * Calls the appropriate handler for a message in a channel of a local tunnel.
+ *
+ * @param t Tunnel this message came on.
+ * @param msg Message header.
+ */
+void
+GCT_handle_encrypted (struct CadetTunnel *t,
+                      const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
+{
+  uint16_t size = ntohs (msg->header.size);
+  char cbuf [size];
+  int decrypted_size;
+  const struct GNUNET_MessageHeader *msgh;
+  unsigned int off;
+
+  GNUNET_STATISTICS_update (stats, "# received encrypted", 1, GNUNET_NO);
+
+  decrypted_size = t_ax_decrypt_and_validate (t, cbuf, msg, size);
+
+  if (-1 == decrypted_size)
+  {
+    GNUNET_STATISTICS_update (stats, "# unable to decrypt", 1, GNUNET_NO);
+    if (CADET_TUNNEL_KEY_PING <= t->estate)
+    {
+      GNUNET_break_op (0);
+      LOG (GNUNET_ERROR_TYPE_WARNING, "Wrong crypto, tunnel %s\n", GCT_2s (t));
+      GCT_debug (t, GNUNET_ERROR_TYPE_WARNING);
+    }
+    return;
+  }
+  GCT_change_estate (t, CADET_TUNNEL_KEY_OK);
+
+  /* FIXME: this is bad, as the structs returned from
+     this loop may be unaligned, see util's MST for
+     how to do this right. */
+  off = 0;
+  while (off + sizeof (struct GNUNET_MessageHeader) <= decrypted_size)
+  {
+    uint16_t msize;
+
+    msgh = (const struct GNUNET_MessageHeader *) &cbuf[off];
+    msize = ntohs (msgh->size);
+    if (msize < sizeof (struct GNUNET_MessageHeader))
+    {
+      GNUNET_break_op (0);
+      return;
+    }
+    if (off + msize < decrypted_size)
+    {
+      GNUNET_break_op (0);
+      return;
+    }
+    handle_decrypted (t, msgh, GNUNET_SYSERR);
+    off += msize;
+  }
+}
+
+
+/**
+ * Handle a Key eXchange message.
+ *
+ * @param t Tunnel on which the message came.
+ * @param msg KX message itself.
+ */
+void
+GCT_handle_kx (struct CadetTunnel *t,
+               const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg)
 {
   struct CadetTunnelAxolotl *ax;
   struct GNUNET_HashCode key_material[3];
@@ -1971,12 +2003,13 @@ handle_kx_ax (struct CadetTunnel *t, const struct GNUNET_CADET_AX_KX *msg)
   const struct GNUNET_PeerIdentity *pid;
   int am_I_alice;
 
-  LOG (GNUNET_ERROR_TYPE_INFO, "<== {     AX_KX} on %s\n", GCT_2s (t));
+  CADET_TIMING_START;
+
+  LOG (GNUNET_ERROR_TYPE_INFO, "<== {        KX} on %s\n", GCT_2s (t));
 
   if (NULL == t->ax)
   {
     /* Something is wrong if ax is NULL. Whose fault it is? */
-    GNUNET_break (CADET_Axolotl == t->enc_type);
     return;
   }
   ax = t->ax;
@@ -1992,14 +2025,14 @@ handle_kx_ax (struct CadetTunnel *t, const struct GNUNET_CADET_AX_KX *msg)
     return;
   }
 
-  if (0 != (GNUNET_CADET_AX_KX_FLAG_FORCE_REPLY & ntohl (msg->flags)))
+  if (0 != (GNUNET_CADET_KX_FLAG_FORCE_REPLY & ntohl (msg->flags)))
   {
     if (NULL != t->rekey_task)
     {
       GNUNET_SCHEDULER_cancel (t->rekey_task);
       t->rekey_task = NULL;
     }
-    GCT_send_ax_kx (t, GNUNET_NO);
+    GCT_send_kx (t, GNUNET_NO);
   }
 
   if (0 == memcmp (&ax->DHRr, &msg->ratchet_key, sizeof(msg->ratchet_key)))
@@ -2092,184 +2125,11 @@ handle_kx_ax (struct CadetTunnel *t, const struct GNUNET_CADET_AX_KX *msg)
   ax->PNs = 0;
   ax->Nr = 0;
   ax->Ns = 0;
+
   GCT_change_estate (t, CADET_TUNNEL_KEY_PING);
   send_queued_data (t);
-}
-
-
-/**
- * Demultiplex by message type and call appropriate handler for a message
- * towards a channel of a local tunnel.
- *
- * @param t Tunnel this message came on.
- * @param msgh Message header.
- * @param fwd Is this message fwd? This only is meaningful in loopback channels.
- *            #GNUNET_YES if message is FWD on the respective channel (loopback)
- *            #GNUNET_NO if message is BCK on the respective channel (loopback)
- *            #GNUNET_SYSERR if message on a one-ended channel (remote)
- */
-static void
-handle_decrypted (struct CadetTunnel *t,
-                  const struct GNUNET_MessageHeader *msgh,
-                  int fwd)
-{
-  uint16_t type;
-  char buf[256];
-
-  type = ntohs (msgh->type);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "<-- %s on %s\n", GC_m2s (type), GCT_2s (t));
-  SPRINTF (buf, "# received encrypted of type %hu (%s)", type, GC_m2s (type));
-  GNUNET_STATISTICS_update (stats, buf, 1, GNUNET_NO);
-
-  switch (type)
-  {
-    case GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE:
-      /* Do nothing, connection aleady got updated. */
-      GNUNET_STATISTICS_update (stats, "# keepalives received", 1, GNUNET_NO);
-      break;
-
-    case GNUNET_MESSAGE_TYPE_CADET_DATA:
-      /* Don't send hop ACK, wait for client to ACK */
-      handle_data (t, (struct GNUNET_CADET_Data *) msgh, fwd);
-      break;
-
-    case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK:
-      handle_data_ack (t, (struct GNUNET_CADET_DataACK *) msgh, fwd);
-      break;
-
-    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
-      handle_ch_create (t, (struct GNUNET_CADET_ChannelCreate *) msgh);
-      break;
-
-    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK:
-      handle_ch_nack (t, (struct GNUNET_CADET_ChannelManage *) msgh);
-      break;
-
-    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK:
-      handle_ch_ack (t, (struct GNUNET_CADET_ChannelManage *) msgh, fwd);
-      break;
 
-    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
-      handle_ch_destroy (t, (struct GNUNET_CADET_ChannelManage *) msgh, fwd);
-      break;
-
-    default:
-      GNUNET_break_op (0);
-      LOG (GNUNET_ERROR_TYPE_WARNING,
-           "end-to-end message not known (%u)\n",
-           ntohs (msgh->type));
-      GCT_debug (t, GNUNET_ERROR_TYPE_WARNING);
-  }
-}
-
-
-/******************************************************************************/
-/********************************    API    ***********************************/
-/******************************************************************************/
-/**
- * Decrypt old format and demultiplex by message type. Call appropriate handler
- * for a message towards a channel of a local tunnel.
- *
- * @param t Tunnel this message came on.
- * @param msg Message header.
- */
-void
-GCT_handle_encrypted (struct CadetTunnel *t,
-                      const struct GNUNET_MessageHeader *msg)
-{
-  uint16_t size = ntohs (msg->size);
-  char cbuf [size];
-  int decrypted_size;
-  uint16_t type;
-  const struct GNUNET_MessageHeader *msgh;
-  unsigned int off;
-
-  type = ntohs (msg->type);
-  switch (type)
-  {
-  case GNUNET_MESSAGE_TYPE_CADET_AX:
-    {
-      const struct GNUNET_CADET_AX *emsg;
-
-      GNUNET_STATISTICS_update (stats, "# received Axolotl", 1, GNUNET_NO);
-      emsg = (const struct GNUNET_CADET_AX *) msg;
-      decrypted_size = t_ax_decrypt_and_validate (t, cbuf, emsg, size);
-    }
-    break;
-  default:
-    GNUNET_break_op (0);
-    return;
-  }
-
-  if (-1 == decrypted_size)
-  {
-    GNUNET_STATISTICS_update (stats, "# unable to decrypt", 1, GNUNET_NO);
-    if (CADET_TUNNEL_KEY_PING <= t->estate)
-    {
-      GNUNET_break_op (0);
-      LOG (GNUNET_ERROR_TYPE_WARNING, "Wrong crypto, tunnel %s\n", GCT_2s (t));
-      GCT_debug (t, GNUNET_ERROR_TYPE_WARNING);
-    }
-    return;
-  }
-  GCT_change_estate (t, CADET_TUNNEL_KEY_OK);
-
-  /* FIXME: this is bad, as the structs returned from
-     this loop may be unaligned, see util's MST for
-     how to do this right. */
-  off = 0;
-  while (off + sizeof (struct GNUNET_MessageHeader) <= decrypted_size)
-  {
-    uint16_t msize;
-
-    msgh = (const struct GNUNET_MessageHeader *) &cbuf[off];
-    msize = ntohs (msgh->size);
-    if (msize < sizeof (struct GNUNET_MessageHeader))
-    {
-      GNUNET_break_op (0);
-      return;
-    }
-    if (off + msize < decrypted_size)
-    {
-      GNUNET_break_op (0);
-      return;
-    }
-    handle_decrypted (t, msgh, GNUNET_SYSERR);
-    off += msize;
-  }
-}
-
-
-/**
- * Demultiplex an encapsulated KX message by message type.
- *
- * @param t Tunnel on which the message came.
- * @param message Payload of KX message.
- *
- * FIXME: not needed anymore
- *  - substitute with call to kx_ax
- *  - eliminate encapsulation
- */
-void
-GCT_handle_kx (struct CadetTunnel *t,
-               const struct GNUNET_MessageHeader *message)
-{
-  uint16_t type;
-  char buf[256];
-
-  type = ntohs (message->type);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "kx message received: %s\n", GC_m2s (type));
-  sprintf (buf, "# received KX of type %hu (%s)", type, GC_m2s (type));
-  GNUNET_STATISTICS_update (stats, buf, 1, GNUNET_NO);
-  switch (type)
-  {
-    case GNUNET_MESSAGE_TYPE_CADET_AX_KX:
-      handle_kx_ax (t, (const struct GNUNET_CADET_AX_KX *) message);
-      break;
-    default:
-      GNUNET_break_op (0);
-      LOG (GNUNET_ERROR_TYPE_WARNING, "kx message %s unknown\n", GC_m2s (type));
-  }
+  CADET_TIMING_END;
 }
 
 /**
@@ -2287,9 +2147,9 @@ GCT_init (const struct GNUNET_CONFIGURATION_Handle *c,
   LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
 
   expected_overhead = 0;
-  expected_overhead += sizeof (struct GNUNET_CADET_AX);
-  expected_overhead += sizeof (struct GNUNET_CADET_Data);
-  expected_overhead += sizeof (struct GNUNET_CADET_ACK);
+  expected_overhead += sizeof (struct GNUNET_CADET_TunnelEncryptedMessage);
+  expected_overhead += sizeof (struct GNUNET_CADET_ChannelAppDataMessage);
+  expected_overhead += sizeof (struct GNUNET_CADET_ConnectionEncryptedAckMessage);
   GNUNET_assert (GNUNET_CONSTANTS_CADET_P2P_OVERHEAD == expected_overhead);
 
   if (GNUNET_OK !=
@@ -2344,7 +2204,7 @@ GCT_new (struct CadetPeer *destination)
   struct CadetTunnel *t;
 
   t = GNUNET_new (struct CadetTunnel);
-  t->next_chid = 0;
+  t->next_ctn.cn = 0;
   t->peer = destination;
 
   if (GNUNET_OK !=
@@ -2387,8 +2247,8 @@ GCT_change_cstate (struct CadetTunnel* t, enum CadetTunnelCState cstate)
     }
     else if (CADET_TUNNEL_KEY_UNINITIALIZED == t->estate)
     {
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "  cstate triggered kx\n");
-      GCT_send_ax_kx (t, GNUNET_NO);
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "  cstate triggered KX\n");
+      GCT_send_kx (t, GNUNET_NO);
     }
     else
     {
@@ -2586,7 +2446,8 @@ GCT_remove_connection (struct CadetTunnel *t,
  * @param ch Channel.
  */
 void
-GCT_add_channel (struct CadetTunnel *t, struct CadetChannel *ch)
+GCT_add_channel (struct CadetTunnel *t,
+                 struct CadetChannel *ch)
 {
   struct CadetTChannel *aux;
 
@@ -2649,12 +2510,13 @@ GCT_remove_channel (struct CadetTunnel *t, struct CadetChannel *ch)
  * Search for a channel by global ID.
  *
  * @param t Tunnel containing the channel.
- * @param chid Public channel number.
+ * @param ctn Public channel number.
  *
  * @return channel handler, NULL if doesn't exist
  */
 struct CadetChannel *
-GCT_get_channel (struct CadetTunnel *t, CADET_ChannelNumber chid)
+GCT_get_channel (struct CadetTunnel *t,
+                 struct GNUNET_CADET_ChannelTunnelNumber ctn)
 {
   struct CadetTChannel *iter;
 
@@ -2663,7 +2525,7 @@ GCT_get_channel (struct CadetTunnel *t, CADET_ChannelNumber chid)
 
   for (iter = t->channel_head; NULL != iter; iter = iter->next)
   {
-    if (GCCH_get_id (iter->ch) == chid)
+    if (GCCH_get_id (iter->ch).cn == ctn.cn)
       break;
   }
 
@@ -2803,7 +2665,7 @@ GCT_destroy (struct CadetTunnel *t)
 
     mh = (struct GNUNET_MessageHeader *) &t->tq_head[1];
     type = ntohs (mh->type);
-    if (0 == keepalives_queued && GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE == type)
+    if (0 == keepalives_queued && GNUNET_MESSAGE_TYPE_CADET_CHANNEL_KEEPALIVE == type)
     {
       keepalives_queued = 1;
       LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -2871,7 +2733,7 @@ struct CadetConnection *
 GCT_use_path (struct CadetTunnel *t, struct CadetPeerPath *path)
 {
   struct CadetConnection *c;
-  struct GNUNET_CADET_Hash cid;
+  struct GNUNET_CADET_ConnectionTunnelIdentifier cid;
   unsigned int own_pos;
 
   if (NULL == t || NULL == path)
@@ -3110,11 +2972,11 @@ GCT_get_destination (struct CadetTunnel *t)
  *
  * @return GID of a channel free to use.
  */
-CADET_ChannelNumber
-GCT_get_next_chid (struct CadetTunnel *t)
+struct GNUNET_CADET_ChannelTunnelNumber
+GCT_get_next_ctn (struct CadetTunnel *t)
 {
-  CADET_ChannelNumber chid;
-  CADET_ChannelNumber mask;
+  struct GNUNET_CADET_ChannelTunnelNumber ctn;
+  struct GNUNET_CADET_ChannelTunnelNumber mask;
   int result;
 
   /* Set bit 30 depending on the ID relationship. Bit 31 is always 0 for GID.
@@ -3123,22 +2985,24 @@ GCT_get_next_chid (struct CadetTunnel *t)
    */
   result = GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, GCP_get_id (t->peer));
   if (0 > result)
-    mask = 0x40000000;
+    mask.cn = htonl (0x40000000);
   else
-    mask = 0x0;
-  t->next_chid |= mask;
+    mask.cn = 0x0;
+  t->next_ctn.cn |= mask.cn;
 
-  while (NULL != GCT_get_channel (t, t->next_chid))
+  while (NULL != GCT_get_channel (t, t->next_ctn))
   {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", t->next_chid);
-    t->next_chid = (t->next_chid + 1) & ~GNUNET_CADET_LOCAL_CHANNEL_ID_CLI;
-    t->next_chid |= mask;
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Channel %u exists...\n",
+         t->next_ctn.cn);
+    t->next_ctn.cn = htonl ((ntohl (t->next_ctn.cn) + 1) & ~GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
+    t->next_ctn.cn |= mask.cn;
   }
-  chid = t->next_chid;
-  t->next_chid = (t->next_chid + 1) & ~GNUNET_CADET_LOCAL_CHANNEL_ID_CLI;
-  t->next_chid |= mask;
+  ctn = t->next_ctn;
+  t->next_ctn.cn = (t->next_ctn.cn + 1) & ~GNUNET_CADET_LOCAL_CHANNEL_ID_CLI;
+  t->next_ctn.cn |= mask.cn;
 
-  return chid;
+  return ctn;
 }
 
 
@@ -3329,7 +3193,8 @@ GCT_has_queued_traffic (struct CadetTunnel *t)
  */
 struct CadetTunnelQueue *
 GCT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
-                           struct CadetTunnel *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);
@@ -3337,34 +3202,54 @@ GCT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
 
 
 /**
- * Send an Axolotl KX message.
+ * Send a KX message.
  *
  * @param t Tunnel on which to send it.
  * @param force_reply Force the other peer to reply with a KX message.
  */
 void
-GCT_send_ax_kx (struct CadetTunnel *t, int force_reply)
+GCT_send_kx (struct CadetTunnel *t, int force_reply)
 {
-  struct GNUNET_CADET_AX_KX msg;
-  enum GNUNET_CADET_AX_KX_Flags flags;
+  static struct CadetEncryptedMessageIdentifier zero;
+  struct CadetConnection *c;
+  struct GNUNET_CADET_TunnelKeyExchangeMessage msg;
+  enum GNUNET_CADET_KX_Flags flags;
 
-  LOG (GNUNET_ERROR_TYPE_INFO, "==> {     AX_KX} on %s\n", GCT_2s (t));
+  LOG (GNUNET_ERROR_TYPE_INFO, "==> {        KX} on %s\n", GCT_2s (t));
   if (NULL != t->ephm_h)
   {
-    LOG (GNUNET_ERROR_TYPE_INFO, "     already queued\n");
+    LOG (GNUNET_ERROR_TYPE_INFO, "     already queued, nop\n");
+    return;
+  }
+  GNUNET_assert (GNUNET_NO == GCT_is_loopback (t));
+
+  c = tunnel_get_connection (t);
+  if (NULL == c)
+  {
+    if (NULL == t->destroy_task && CADET_TUNNEL_READY == t->cstate)
+    {
+      GNUNET_break (0);
+      GCT_debug (t, GNUNET_ERROR_TYPE_ERROR);
+    }
     return;
   }
 
   msg.header.size = htons (sizeof (msg));
-  msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_AX_KX);
-  flags = GNUNET_CADET_AX_KX_FLAG_NONE;
-  if (force_reply)
-    flags |= GNUNET_CADET_AX_KX_FLAG_FORCE_REPLY;
+  msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX);
+  flags = GNUNET_CADET_KX_FLAG_NONE;
+  if (GNUNET_YES == force_reply)
+    flags |= GNUNET_CADET_KX_FLAG_FORCE_REPLY;
   msg.flags = htonl (flags);
+  msg.cid = *GCC_get_id (c);
   GNUNET_CRYPTO_ecdhe_key_get_public (t->ax->kx_0, &msg.ephemeral_key);
   GNUNET_CRYPTO_ecdhe_key_get_public (t->ax->DHRs, &msg.ratchet_key);
 
-  t->ephm_h = send_kx (t, &msg.header);
+  t->ephm_h = GCC_send_prebuilt_message (&msg.header,
+                                         UINT16_MAX,
+                                         zero,
+                                         c,
+                                         GCC_is_origin (c, GNUNET_YES),
+                                         GNUNET_YES, &ephm_sent, t);
   if (CADET_TUNNEL_KEY_UNINITIALIZED == t->estate)
     GCT_change_estate (t, CADET_TUNNEL_KEY_SENT);
 }
@@ -3523,8 +3408,8 @@ ax_debug (const struct CadetTunnelAxolotl *ax, enum GNUNET_ErrorType level)
 void
 GCT_debug (const struct CadetTunnel *t, enum GNUNET_ErrorType level)
 {
-  struct CadetTChannel *iterch;
-  struct CadetTConnection *iterc;
+  struct CadetTChannel *iter_ch;
+  struct CadetTConnection *iter_c;
   int do_log;
 
   do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
@@ -3537,33 +3422,20 @@ GCT_debug (const struct CadetTunnel *t, enum GNUNET_ErrorType level)
   LOG2 (level, "TTT  cstate %s, estate %s\n",
        cstate2s (t->cstate), estate2s (t->estate));
 #if DUMP_KEYS_TO_STDERR
-  if (CADET_Axolotl == t->enc_type)
-  {
-    ax_debug (t->ax, level);
-  }
-  else
-  {
-    LOG2 (level, "TTT  peers EPHM:\t %s\n",
-          GNUNET_i2s ((struct GNUNET_PeerIdentity *) &t->peers_ephemeral_key));
-    LOG2 (level, "TTT  ENC key:\t %s\n",
-          GNUNET_i2s ((struct GNUNET_PeerIdentity *) &t->e_key));
-    LOG2 (level, "TTT  DEC key:\t %s\n",
-          GNUNET_i2s ((struct GNUNET_PeerIdentity *) &t->d_key));
-  }
+  ax_debug (t->ax, level);
 #endif
   LOG2 (level, "TTT  tq_head %p, tq_tail %p\n", t->tq_head, t->tq_tail);
   LOG2 (level, "TTT  destroy %p\n", t->destroy_task);
-
   LOG2 (level, "TTT  channels:\n");
-  for (iterch = t->channel_head; NULL != iterch; iterch = iterch->next)
+  for (iter_ch = t->channel_head; NULL != iter_ch; iter_ch = iter_ch->next)
   {
-    GCCH_debug (iterch->ch, level);
+    GCCH_debug (iter_ch->ch, level);
   }
 
   LOG2 (level, "TTT  connections:\n");
-  for (iterc = t->connection_head; NULL != iterc; iterc = iterc->next)
+  for (iter_c = t->connection_head; NULL != iter_c; iter_c = iter_c->next)
   {
-    GCC_debug (iterc->c, level);
+    GCC_debug (iter_c->c, level);
   }
 
   LOG2 (level, "TTT DEBUG TUNNEL END\n");