-skeletons for transport-ng
[oweals/gnunet.git] / src / cadet / gnunet-service-cadet_tunnel.c
index f32f209d0ee4681e3676fbeda72e476f612eba1d..b9f0e1fa25f9ff13855c9592f02081b2503436c9 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     Copyright (C) 2013 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2013 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
@@ -14,8 +14,8 @@
 
      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
 
 #include "platform.h"
@@ -43,6 +43,8 @@
 #define DUMP_KEYS_TO_STDERR GNUNET_NO
 #endif
 
+#define MIN_TUNNEL_BUFFER       8
+#define MAX_TUNNEL_BUFFER       64
 #define MAX_SKIPPED_KEYS        64
 #define MAX_KEY_GAP             256
 #define AX_HEADER_SIZE (sizeof (uint32_t) * 2\
@@ -186,10 +188,16 @@ struct CadetTunnelSkippedKey
    * Message key.
    */
   struct GNUNET_CRYPTO_SymmetricSessionKey MK;
+
+  /**
+   * Key number for a given HK.
+   */
+  unsigned int Kn;
 };
 
+
 /**
- * Axolotl data, according to @url https://github.com/trevp/axolotl/wiki .
+ * Axolotl data, according to https://github.com/trevp/axolotl/wiki .
  */
 struct CadetTunnelAxolotl
 {
@@ -250,11 +258,6 @@ struct CadetTunnelAxolotl
    */
   struct GNUNET_CRYPTO_EcdhePrivateKey *kx_0;
 
-  /**
-   * ECDH Identity key (recv).
-   */
-  struct GNUNET_CRYPTO_EcdhePublicKey DHIr;
-
   /**
    * ECDH Ratchet key (send).
    */
@@ -466,28 +469,6 @@ struct CadetTunnelQueue
 };
 
 
-/**
- * Cached Axolotl key with signature.
- */
-struct CadetAxolotlSignedKey
-{
-  /**
-   * Information about what is being signed (@a permanent_key).
-   */
-  struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
-
-  /**
-   * Permanent public ECDH key.
-   */
-  struct GNUNET_CRYPTO_EcdhePublicKey permanent_key;
-
-  /**
-   * An EdDSA signature of the permanent ECDH key with the Peer's ID key.
-   */
-  struct GNUNET_CRYPTO_EddsaSignature signature;
-} GNUNET_PACKED;
-
-
 /******************************************************************************/
 /*******************************   GLOBALS  ***********************************/
 /******************************************************************************/
@@ -533,13 +514,6 @@ const static struct GNUNET_CRYPTO_EddsaPrivateKey *id_key;
 
 /********************************  AXOLOTL ************************************/
 
-static struct GNUNET_CRYPTO_EcdhePrivateKey *ax_key;
-
-/**
- * Own Axolotl permanent public key (cache).
- */
-static struct CadetAxolotlSignedKey ax_identity;
-
 /**
  * How many messages are needed to trigger a ratchet advance.
  */
@@ -655,11 +629,15 @@ static int
 is_ready (struct CadetTunnel *t)
 {
   int ready;
-
-  GCT_debug (t, GNUNET_ERROR_TYPE_DEBUG);
-  ready = CADET_TUNNEL_READY == t->cstate
-          && (CADET_TUNNEL_KEY_OK == t->estate
-              || CADET_TUNNEL_KEY_REKEY == t->estate);
+  int conn_ok;
+  int enc_ok;
+
+  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);
+  ready = conn_ok && enc_ok;
   ready = ready || GCT_is_loopback (t);
   return ready;
 }
@@ -703,19 +681,6 @@ ephemeral_purpose_size (void)
 }
 
 
-/**
- * Ephemeral key message purpose size.
- *
- * @return Size of the part of the ephemeral key message that must be signed.
- */
-static size_t
-ax_purpose_size (void)
-{
-  return sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
-         sizeof (struct GNUNET_CRYPTO_EcdhePublicKey);
-}
-
-
 /**
  * Size of the encrypted part of a ping message.
  *
@@ -810,7 +775,7 @@ get_connection_allowed (const struct CadetTConnection *tc)
  * @param t Tunnel on which the message came.
  * @param msg The ephemeral key message.
  *
- * @return GNUNET_OK if message is fine, GNUNET_SYSERR otherwise.
+ * @return #GNUNET_OK if message is fine, #GNUNET_SYSERR otherwise.
  */
 int
 check_ephemeral (struct CadetTunnel *t,
@@ -818,17 +783,35 @@ check_ephemeral (struct CadetTunnel *t,
 {
   /* Check message size */
   if (ntohs (msg->header.size) != sizeof (struct GNUNET_CADET_KX_Ephemeral))
+  {
+    /* This is probably an old "MESH" version. */
+    LOG (GNUNET_ERROR_TYPE_INFO,
+         "Expected ephemeral of size %u, got %u\n",
+         sizeof (struct GNUNET_CADET_KX_Ephemeral),
+         ntohs (msg->header.size));
     return GNUNET_SYSERR;
+  }
 
   /* Check signature size */
   if (ntohl (msg->purpose.size) != ephemeral_purpose_size ())
+  {
+    LOG (GNUNET_ERROR_TYPE_WARNING,
+         "Expected signature purpose of size %u, got %u\n",
+         ephemeral_purpose_size (),
+         ntohs (msg->purpose.size));
     return GNUNET_SYSERR;
+  }
 
   /* Check origin */
   if (0 != memcmp (&msg->origin_identity,
                    GCP_get_id (t->peer),
                    sizeof (struct GNUNET_PeerIdentity)))
+  {
+    LOG (GNUNET_ERROR_TYPE_WARNING,
+         "Unexpected origin, got %s\n",
+         GNUNET_i2s (&msg->origin_identity));
     return GNUNET_SYSERR;
+  }
 
   /* Check signature */
   if (GNUNET_OK !=
@@ -836,7 +819,10 @@ check_ephemeral (struct CadetTunnel *t,
                                   &msg->purpose,
                                   &msg->signature,
                                   &msg->origin_identity.public_key))
+  {
+    LOG (GNUNET_ERROR_TYPE_WARNING, "Signature invalid\n");
     return GNUNET_SYSERR;
+  }
 
   return GNUNET_OK;
 }
@@ -887,6 +873,27 @@ select_key (const struct CadetTunnel *t)
 }
 
 
+/**
+ * Create a new Axolotl ephemeral (ratchet) key.
+ *
+ * @param t Tunnel.
+ */
+static void
+new_ephemeral (struct CadetTunnel *t)
+{
+  GNUNET_free_non_null (t->ax->DHRs);
+  t->ax->DHRs = GNUNET_CRYPTO_ecdhe_key_create();
+  #if DUMP_KEYS_TO_STDERR
+  {
+    struct GNUNET_CRYPTO_EcdhePublicKey pub;
+    GNUNET_CRYPTO_ecdhe_key_get_public (t->ax->DHRs, &pub);
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "  new DHRs generated: pub  %s\n",
+        GNUNET_i2s ((struct GNUNET_PeerIdentity *) &pub));
+  }
+  #endif
+}
+
+
 /**
  * Calculate HMAC.
  *
@@ -907,7 +914,7 @@ t_hmac (const void *plaintext, size_t size,
 
 #if DUMP_KEYS_TO_STDERR
   LOG (GNUNET_ERROR_TYPE_INFO, "  HMAC %u bytes with key %s\n", size,
-       GNUNET_h2s ((struct GNUNET_HashCode *) key));
+       GNUNET_i2s ((struct GNUNET_PeerIdentity *) key));
 #endif
   GNUNET_CRYPTO_hmac_derive_key (&auth_key, key,
                                  &iv, sizeof (iv),
@@ -946,7 +953,7 @@ t_encrypt (struct CadetTunnel *t, void *dst, const void *src,
   key = GNUNET_YES == force_newest_key ? &t->e_key : select_key (t);
   #if DUMP_KEYS_TO_STDERR
   LOG (GNUNET_ERROR_TYPE_INFO, "  ENC with key %s\n",
-       GNUNET_h2s ((struct GNUNET_HashCode *) key));
+       GNUNET_i2s ((struct GNUNET_PeerIdentity *) key));
   #endif
   GNUNET_CRYPTO_symmetric_derive_iv (&siv, key, &iv, sizeof (iv), NULL);
   LOG (GNUNET_ERROR_TYPE_DEBUG, "  t_encrypt IV derived\n");
@@ -1040,7 +1047,7 @@ t_ax_encrypt (struct CadetTunnel *t, void *dst, const void *src, size_t size)
     struct GNUNET_HashCode hmac;
     static const char ctx[] = "axolotl ratchet";
 
-    ax->DHRs = GNUNET_CRYPTO_ecdhe_key_create ();
+    new_ephemeral (t);
     ax->HKs = ax->NHKs;
 
     /* RK, NHKs, CKs = KDF( HMAC-HASH(RK, DH(DHRs, DHRr)) ) */
@@ -1065,10 +1072,10 @@ t_ax_encrypt (struct CadetTunnel *t, void *dst, const void *src, size_t size)
   GNUNET_CRYPTO_symmetric_derive_iv (&iv, &MK, NULL, 0, NULL);
 
   #if DUMP_KEYS_TO_STDERR
-  LOG (GNUNET_ERROR_TYPE_INFO, "  CKs: %s\n",
-       GNUNET_h2s ((struct GNUNET_HashCode *) &ax->CKs));
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "  CKs: %s\n",
+       GNUNET_i2s ((struct GNUNET_PeerIdentity *) &ax->CKs));
   LOG (GNUNET_ERROR_TYPE_INFO, "  AX_ENC with key %u: %s\n", ax->Ns,
-       GNUNET_h2s ((struct GNUNET_HashCode *) &MK));
+       GNUNET_i2s ((struct GNUNET_PeerIdentity *) &MK));
   #endif
 
   out_size = GNUNET_CRYPTO_symmetric_encrypt (src, size, &MK, &iv, dst);
@@ -1107,10 +1114,10 @@ t_ax_decrypt (struct CadetTunnel *t, void *dst, const void *src, size_t size)
   GNUNET_CRYPTO_symmetric_derive_iv (&iv, &MK, NULL, 0, NULL);
 
   #if DUMP_KEYS_TO_STDERR
-  LOG (GNUNET_ERROR_TYPE_INFO, "  CKr: %s\n",
-       GNUNET_h2s ((struct GNUNET_HashCode *) &ax->CKr));
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "  CKr: %s\n",
+       GNUNET_i2s ((struct GNUNET_PeerIdentity *) &ax->CKr));
   LOG (GNUNET_ERROR_TYPE_INFO, "  AX_DEC with key %u: %s\n", ax->Nr,
-       GNUNET_h2s ((struct GNUNET_HashCode *) &MK));
+       GNUNET_i2s ((struct GNUNET_PeerIdentity *) &MK));
   #endif
 
   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
@@ -1145,7 +1152,7 @@ t_h_encrypt (struct CadetTunnel *t, struct GNUNET_CADET_AX *msg)
 
   #if DUMP_KEYS_TO_STDERR
   LOG (GNUNET_ERROR_TYPE_INFO, "  AX_ENC_H with key %s\n",
-       GNUNET_h2s ((struct GNUNET_HashCode *) &ax->HKs));
+       GNUNET_i2s ((struct GNUNET_PeerIdentity *) &ax->HKs));
   #endif
 
   out_size = GNUNET_CRYPTO_symmetric_encrypt (&msg->Ns, AX_HEADER_SIZE,
@@ -1179,7 +1186,7 @@ t_h_decrypt (struct CadetTunnel *t, const struct GNUNET_CADET_AX *src,
 
   #if DUMP_KEYS_TO_STDERR
   LOG (GNUNET_ERROR_TYPE_INFO, "  AX_DEC_H with key %s\n",
-       GNUNET_h2s ((struct GNUNET_HashCode *) &ax->HKr));
+       GNUNET_i2s ((struct GNUNET_PeerIdentity *) &ax->HKr));
   #endif
 
   out_size = GNUNET_CRYPTO_symmetric_decrypt (&src->Ns, AX_HEADER_SIZE,
@@ -1187,7 +1194,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_ax_decrypt end\n");
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "  t_h_decrypt end\n");
 }
 
 
@@ -1239,7 +1246,7 @@ t_decrypt (struct CadetTunnel *t, void *dst, const void *src,
 
 #if DUMP_KEYS_TO_STDERR
   LOG (GNUNET_ERROR_TYPE_DEBUG, "  t_decrypt with %s\n",
-       GNUNET_h2s ((struct GNUNET_HashCode *) &t->d_key));
+       GNUNET_i2s ((struct GNUNET_PeerIdentity *) &t->d_key));
 #endif
   if (CADET_TUNNEL_KEY_UNINITIALIZED == t->estate)
   {
@@ -1331,43 +1338,126 @@ t_decrypt_and_validate (struct CadetTunnel *t,
  * @return Size of the decrypted data, -1 if an error was encountered.
  */
 static int
-try_old_ax_keys (struct CadetTunnel *t, struct GNUNET_CADET_AX *dst,
+try_old_ax_keys (struct CadetTunnel *t, void *dst,
                  const struct GNUNET_CADET_AX *src, size_t size)
 {
   struct CadetTunnelSkippedKey *key;
-  struct GNUNET_CADET_Hash hmac;
+  struct GNUNET_CADET_Hash *hmac;
   struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
+  struct GNUNET_CADET_AX plaintext_header;
+  struct GNUNET_CRYPTO_SymmetricSessionKey *valid_HK;
+  size_t esize;
   size_t res;
   size_t len;
+  unsigned int N;
 
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Trying old keys\n");
+  hmac = &plaintext_header.hmac;
+  esize = size - sizeof (struct GNUNET_CADET_AX);
 
+  /* Find a correct Header Key */
   for (key = t->ax->skipped_head; NULL != key; key = key->next)
   {
-    t_hmac (&src->Ns, AX_HEADER_SIZE, 0, &key->HK, &hmac);
-    if (0 != memcmp (&hmac, &src->hmac, sizeof (hmac)))
+    #if DUMP_KEYS_TO_STDERR
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "  Trying hmac with key %s\n",
+         GNUNET_i2s ((struct GNUNET_PeerIdentity *) &key->HK));
+    #endif
+    t_hmac (&src->Ns, AX_HEADER_SIZE + esize, 0, &key->HK, hmac);
+    if (0 == memcmp (hmac, &src->hmac, sizeof (*hmac)))
+    {
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "  hmac correct\n");
+      valid_HK = &key->HK;
       break;
+    }
   }
   if (NULL == key)
     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 (len >= sizeof (struct GNUNET_MessageHeader));
+
+  /* Decrypt header */
+  GNUNET_CRYPTO_symmetric_derive_iv (&iv, &key->HK, NULL, 0, NULL);
+  res = GNUNET_CRYPTO_symmetric_decrypt (&src->Ns, AX_HEADER_SIZE,
+                                         &key->HK, &iv, &plaintext_header.Ns);
+  GNUNET_assert (AX_HEADER_SIZE == res);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "  Message %u, previous: %u\n",
+       ntohl (plaintext_header.Ns), ntohl (plaintext_header.PNs));
+
+  /* Find the correct Message Key */
+  N = ntohl (plaintext_header.Ns);
+  while (NULL != key && N != key->Kn)
+    key = key->next;
+  if (NULL == key || 0 != memcmp (&key->HK, valid_HK, sizeof (*valid_HK)))
+    return -1;
+
   #if DUMP_KEYS_TO_STDERR
-  LOG (GNUNET_ERROR_TYPE_INFO, "  AX_DEC with skipped key %s\n",
-       GNUNET_h2s ((struct GNUNET_HashCode *) &key->MK));
+  LOG (GNUNET_ERROR_TYPE_INFO, "  AX_DEC_H with skipped key %s\n",
+       GNUNET_i2s ((struct GNUNET_PeerIdentity *) &key->HK));
+  LOG (GNUNET_ERROR_TYPE_INFO, "  AX_DEC with skipped key %u: %s\n",
+       key->Kn, GNUNET_i2s ((struct GNUNET_PeerIdentity *) &key->MK));
   #endif
 
-  GNUNET_assert (size > sizeof (struct GNUNET_CADET_AX));
-  len = size - sizeof (struct GNUNET_CADET_AX);
+  /* Decrypt payload */
   GNUNET_CRYPTO_symmetric_derive_iv (&iv, &key->MK, NULL, 0, NULL);
-  res = GNUNET_CRYPTO_symmetric_decrypt (&src[1], len, &key->MK, &iv, &dst[1]);
+  res = GNUNET_CRYPTO_symmetric_decrypt (&src[1], len, &key->MK, &iv, dst);
 
+  /* Remove key */
   GNUNET_CONTAINER_DLL_remove (t->ax->skipped_head, t->ax->skipped_tail, key);
   t->ax->skipped--;
-  GNUNET_free (key);
+  GNUNET_free (key); /* GNUNET_free overwrites memory with 0xbaadf00d */
 
   return res;
 }
 
 
+/**
+ * Delete a key from the list of skipped keys.
+ *
+ * @param t Tunnel to delete from.
+ * @param HKr Header Key to use.
+ */
+static void
+store_skipped_key (struct CadetTunnel *t,
+                   const struct GNUNET_CRYPTO_SymmetricSessionKey *HKr)
+{
+  struct CadetTunnelSkippedKey *key;
+
+  key = GNUNET_new (struct CadetTunnelSkippedKey);
+  key->timestamp = GNUNET_TIME_absolute_get ();
+  key->Kn = t->ax->Nr;
+  key->HK = t->ax->HKr;
+  t_hmac_derive_key (&t->ax->CKr, &key->MK, "0", 1);
+  #if DUMP_KEYS_TO_STDERR
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "    storing MK for Nr %u: %s\n",
+       key->Kn, GNUNET_i2s ((struct GNUNET_PeerIdentity *) &key->MK));
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "    for CKr: %s\n",
+       GNUNET_i2s ((struct GNUNET_PeerIdentity *) &t->ax->CKr));
+  #endif
+  t_hmac_derive_key (&t->ax->CKr, &t->ax->CKr, "1", 1);
+  GNUNET_CONTAINER_DLL_insert (t->ax->skipped_head, t->ax->skipped_tail, key);
+  t->ax->Nr++;
+  t->ax->skipped++;
+}
+
+
+/**
+ * Delete a key from the list of skipped keys.
+ *
+ * @param t Tunnel to delete from.
+ * @param key Key to delete.
+ */
+static void
+delete_skipped_key (struct CadetTunnel *t, struct CadetTunnelSkippedKey *key)
+{
+  GNUNET_CONTAINER_DLL_remove (t->ax->skipped_head, t->ax->skipped_tail, key);
+  GNUNET_free (key);
+  t->ax->skipped--;
+}
+
+
 /**
  * Stage skipped AX keys and calculate the message key.
  *
@@ -1375,44 +1465,43 @@ try_old_ax_keys (struct CadetTunnel *t, struct GNUNET_CADET_AX *dst,
  *
  * @param t Tunnel where to stage the keys.
  * @param HKr Header key.
- * @param Nr Current message number.
  * @param Np Received meesage number.
- * @param CKr[in/out] Chain key, gets ratcheted forward to the new state.
+ *
+ * @return GNUNET_OK if keys were stored.
+ *         GNUNET_SYSERR if an error ocurred (Np not expected).
  */
-static void
+static int
 store_ax_keys (struct CadetTunnel *t,
                const struct GNUNET_CRYPTO_SymmetricSessionKey *HKr,
-               uint32_t Nr, uint32_t Np,
-               struct GNUNET_CRYPTO_SymmetricSessionKey *CKr)
+               uint32_t Np)
 {
-  struct CadetTunnelSkippedKey *key;
-  unsigned int i;
   int gap;
 
-  gap = Np - Nr;
-  if (MAX_KEY_GAP < gap || 0 > gap)
+
+  gap = Np - t->ax->Nr;
+  LOG (GNUNET_ERROR_TYPE_INFO, "Storing keys [%u, %u)\n", t->ax->Nr, Np);
+  if (MAX_KEY_GAP < gap)
   {
     /* Avoid DoS (forcing peer to do 2*33 chain HMAC operations) */
     /* TODO: start new key exchange on return */
     GNUNET_break_op (0);
-    return;
+    LOG (GNUNET_ERROR_TYPE_WARNING, "Got message %u, expected %u+\n",
+         Np, t->ax->Nr);
+    return GNUNET_SYSERR;
   }
-
-  for (i = Nr; i < Np; i++)
+  if (0 > gap)
   {
-    key = GNUNET_new (struct CadetTunnelSkippedKey);
-    key->timestamp = GNUNET_TIME_absolute_get ();
-    t_hmac_derive_key (CKr, &key->MK, "0", 1);
-    #if DUMP_KEYS_TO_STDERR
-    LOG (GNUNET_ERROR_TYPE_INFO, "    storing MK for Nr %u: %s\n",
-         i, GNUNET_h2s ((struct GNUNET_HashCode *) &key->MK));
-    LOG (GNUNET_ERROR_TYPE_INFO, "    for CKr: %s\n",
-         GNUNET_h2s ((struct GNUNET_HashCode *) &t->ax->CKr));
-    #endif
-    t_hmac_derive_key (CKr, CKr, "1", 1);
-    GNUNET_CONTAINER_DLL_insert (t->ax->skipped_head, t->ax->skipped_tail, key);
-    t->ax->skipped++;
+    /* Delayed message: don't store keys, flag to try old keys. */
+    return GNUNET_SYSERR;
   }
+
+  while (t->ax->Nr < Np)
+    store_skipped_key (t, HKr);
+
+  while (t->ax->skipped > MAX_SKIPPED_KEYS)
+    delete_skipped_key (t, t->ax->skipped_tail);
+
+  return GNUNET_OK;
 }
 
 
@@ -1434,14 +1523,13 @@ t_ax_decrypt_and_validate (struct CadetTunnel *t, void *dst,
   struct CadetTunnelAxolotl *ax;
   struct GNUNET_CADET_Hash msg_hmac;
   struct GNUNET_HashCode hmac;
-  struct GNUNET_CADET_AX *dstmsg;
+  struct GNUNET_CADET_AX plaintext_header;
   uint32_t Np;
   uint32_t PNp;
   size_t esize;
   size_t osize;
 
   ax = t->ax;
-  dstmsg = dst;
   esize = size - sizeof (struct GNUNET_CADET_AX);
 
   if (NULL == ax)
@@ -1458,21 +1546,22 @@ t_ax_decrypt_and_validate (struct CadetTunnel *t, void *dst,
     struct GNUNET_CRYPTO_EcdhePublicKey *DHRp;
 
     /* Try Next HK */
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "  trying next HK\n");
     t_hmac (&src->Ns, AX_HEADER_SIZE + esize, 0, &ax->NHKr, &msg_hmac);
     if (0 != memcmp (&msg_hmac, &src->hmac, sizeof (msg_hmac)))
     {
       /* Try the skipped keys, if that fails, we're out of luck. */
       return try_old_ax_keys (t, dst, src, size);
     }
-    LOG (GNUNET_ERROR_TYPE_INFO, "next HK\n");
+    LOG (GNUNET_ERROR_TYPE_INFO, "next HK worked\n");
 
     HK = ax->HKr;
     ax->HKr = ax->NHKr;
-    t_h_decrypt (t, src, dstmsg);
-    Np = ntohl (dstmsg->Ns);
-    PNp = ntohl (dstmsg->PNs);
-    DHRp = &dstmsg->DHRs;
-    store_ax_keys (t, &HK, ax->Nr, PNp, &ax->CKr);
+    t_h_decrypt (t, src, &plaintext_header);
+    Np = ntohl (plaintext_header.Ns);
+    PNp = ntohl (plaintext_header.PNs);
+    DHRp = &plaintext_header.DHRs;
+    store_ax_keys (t, &HK, PNp);
 
     /* RKp, NHKp, CKp = KDF (HMAC-HASH (RK, DH (DHRp, DHRs))) */
     GNUNET_CRYPTO_ecc_ecdh (ax->DHRs, DHRp, &dh);
@@ -1491,17 +1580,19 @@ t_ax_decrypt_and_validate (struct CadetTunnel *t, void *dst,
   else
   {
     LOG (GNUNET_ERROR_TYPE_DEBUG, "current HK\n");
-    t_h_decrypt (t, src, dstmsg);
-    Np = ntohl (dstmsg->Ns);
-    PNp = ntohl (dstmsg->PNs);
+    t_h_decrypt (t, src, &plaintext_header);
+    Np = ntohl (plaintext_header.Ns);
+    PNp = ntohl (plaintext_header.PNs);
   }
+  LOG (GNUNET_ERROR_TYPE_INFO, "  got AX Nr %u\n", Np);
+  if (Np != ax->Nr)
+    if (GNUNET_OK != store_ax_keys (t, &ax->HKr, Np))
+      /* Try the skipped keys, if that fails, we're out of luck. */
+      return try_old_ax_keys (t, dst, src, size);
 
-  if (Np > ax->Nr)
-    store_ax_keys (t, &ax->HKr, ax->Nr, Np, &ax->CKr);
-
+  osize = t_ax_decrypt (t, dst, &src[1], esize);
   ax->Nr = Np + 1;
 
-  osize = t_ax_decrypt (t, dst, &src[1], esize);
   if (osize != esize)
   {
     GNUNET_break_op (0);
@@ -1578,14 +1669,14 @@ create_otr_keys (struct CadetTunnel *t)
   derive_symmertic (&t->d_key, GCP_get_id (t->peer), &my_full_id, &km);
   #if DUMP_KEYS_TO_STDERR
   LOG (GNUNET_ERROR_TYPE_INFO, "ME: %s\n",
-       GNUNET_h2s ((struct GNUNET_HashCode *) &otr_kx_msg.ephemeral_key));
+       GNUNET_i2s ((struct GNUNET_PeerIdentity *) &otr_kx_msg.ephemeral_key));
   LOG (GNUNET_ERROR_TYPE_INFO, "PE: %s\n",
-       GNUNET_h2s ((struct GNUNET_HashCode *) &t->peers_ephemeral_key));
+       GNUNET_i2s ((struct GNUNET_PeerIdentity *) &t->peers_ephemeral_key));
   LOG (GNUNET_ERROR_TYPE_INFO, "KM: %s\n", GNUNET_h2s (&km));
   LOG (GNUNET_ERROR_TYPE_INFO, "EK: %s\n",
-       GNUNET_h2s ((struct GNUNET_HashCode *) &t->e_key));
+       GNUNET_i2s ((struct GNUNET_PeerIdentity *) &t->e_key));
   LOG (GNUNET_ERROR_TYPE_INFO, "DK: %s\n",
-       GNUNET_h2s ((struct GNUNET_HashCode *) &t->d_key));
+       GNUNET_i2s ((struct GNUNET_PeerIdentity *) &t->d_key));
   #endif
   return GNUNET_OK;
 }
@@ -1639,21 +1730,13 @@ create_kx_ctx (struct CadetTunnel *t)
  * @brief Finish the Key eXchange and destroy 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)
+finish_kx (void *cls)
 {
   struct CadetTunnel *t = cls;
 
   LOG (GNUNET_ERROR_TYPE_INFO, "finish KX for %s\n", GCT_2s (t));
-
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-  {
-    LOG (GNUNET_ERROR_TYPE_INFO, "  shutdown\n");
-    return;
-  }
-
   GNUNET_free (t->kx_ctx);
   t->kx_ctx = NULL;
 }
@@ -1676,14 +1759,15 @@ destroy_kx_ctx (struct CadetTunnel *t)
 
   if (is_key_null (&t->kx_ctx->e_key_old))
   {
-    t->kx_ctx->finish_task = GNUNET_SCHEDULER_add_now (finish_kx, t);
+    t->kx_ctx->finish_task = GNUNET_SCHEDULER_add_now (&finish_kx, t);
     return;
   }
 
   delay = GNUNET_TIME_relative_divide (rekey_period, 4);
   delay = GNUNET_TIME_relative_min (delay, GNUNET_TIME_UNIT_MINUTES);
 
-  t->kx_ctx->finish_task = GNUNET_SCHEDULER_add_delayed (delay, finish_kx, t);
+  t->kx_ctx->finish_task = GNUNET_SCHEDULER_add_delayed (delay,
+                                                        &finish_kx, t);
 }
 
 
@@ -1796,11 +1880,7 @@ queue_data (struct CadetTunnel *t, const struct GNUNET_MessageHeader *msg)
 
   LOG (GNUNET_ERROR_TYPE_DEBUG, "queue data on Tunnel %s\n", GCT_2s (t));
 
-  if (GNUNET_YES == is_ready (t))
-  {
-    GNUNET_break (0);
-    return NULL;
-  }
+  GNUNET_assert (GNUNET_NO == is_ready (t));
 
   tqd = GNUNET_malloc (sizeof (struct CadetTunnelDelayed) + size);
 
@@ -1938,8 +2018,8 @@ send_prebuilt_message (const struct GNUNET_MessageHeader *message,
 
   if (NULL == cont)
   {
-    GNUNET_break (NULL == GCC_send_prebuilt_message (msg, type,
-                                                     mid, c, fwd, force, NULL, NULL));
+    GNUNET_break (NULL == GCC_send_prebuilt_message (msg, type, mid, c, fwd,
+                                                     force, NULL, NULL));
     return NULL;
   }
   if (NULL == existing_q)
@@ -1983,7 +2063,7 @@ send_queued_data (struct CadetTunnel *t)
 
   if (GNUNET_NO == is_ready (t))
   {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "  not ready yet: %s/%s\n",
+    LOG (GNUNET_ERROR_TYPE_WARNING, "  not ready yet: %s/%s\n",
          estate2s (t->estate), cstate2s (t->cstate));
     return;
   }
@@ -2011,22 +2091,21 @@ send_queued_data (struct CadetTunnel *t)
  * @brief Resend the AX KX until we complete the handshake.
  *
  * @param cls Closure (tunnel).
- * @param tc Task context.
  */
 static void
-ax_kx_resend (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+ax_kx_resend (void *cls)
 {
   struct CadetTunnel *t = cls;
 
   t->rekey_task = NULL;
-
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-    return;
-
   if (CADET_TUNNEL_KEY_OK == t->estate)
+  {
+    /* Should have been canceled on estate change */
+    GNUNET_break (0);
     return;
+  }
 
-  GCT_send_ax_kx (t, GNUNET_YES);
+  GCT_send_ax_kx (t, CADET_TUNNEL_KEY_SENT >= t->estate);
 }
 
 
@@ -2053,11 +2132,12 @@ ephm_sent (void *cls,
   if (CADET_TUNNEL_KEY_OK == t->estate)
     return;
 
-  if (CADET_Axolotl == t->enc_type && CADET_TUNNEL_KEY_OK != t->estate)
+  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,
@@ -2184,7 +2264,7 @@ send_kx (struct CadetTunnel *t,
 static void
 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));
   if (NULL != t->ephm_h)
   {
     LOG (GNUNET_ERROR_TYPE_INFO, "     already queued\n");
@@ -2213,7 +2293,7 @@ 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));
   if (NULL != t->pong_h)
   {
     LOG (GNUNET_ERROR_TYPE_INFO, "     already queued\n");
@@ -2236,25 +2316,21 @@ send_pong (struct CadetTunnel *t, uint32_t challenge)
  * Initiate a rekey with the remote peer.
  *
  * @param cls Closure (tunnel).
- * @param tc TaskContext.
  */
 static void
-rekey_tunnel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+rekey_tunnel (void *cls)
 {
   struct CadetTunnel *t = cls;
 
   t->rekey_task = NULL;
-
   LOG (GNUNET_ERROR_TYPE_INFO, "Re-key Tunnel %s\n", GCT_2s (t));
-  if (NULL != tc && 0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
-    return;
-
   GNUNET_assert (NULL != t->kx_ctx);
   struct GNUNET_TIME_Relative duration;
 
   duration = GNUNET_TIME_absolute_get_duration (t->kx_ctx->rekey_start_time);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, " kx started %s ago\n",
-        GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       " kx started %s ago\n",
+       GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
 
   // FIXME make duration of old keys configurable
   if (duration.rel_value_us >= GNUNET_TIME_UNIT_MINUTES.rel_value_us)
@@ -2354,19 +2430,14 @@ rekey_iterator (void *cls,
  * Create a new ephemeral key and key message, schedule next rekeying.
  *
  * @param cls Closure (unused).
- * @param tc TaskContext.
  */
 static void
-global_otr_rekey (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+global_otr_rekey (void *cls)
 {
   struct GNUNET_TIME_Absolute time;
   long n;
 
   rekey_task = NULL;
-
-  if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
-    return;
-
   GNUNET_free_non_null (otr_ephemeral_key);
   otr_ephemeral_key = GNUNET_CRYPTO_ecdhe_key_create ();
 
@@ -2377,7 +2448,7 @@ global_otr_rekey (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   otr_kx_msg.expiration_time = GNUNET_TIME_absolute_hton (time);
   GNUNET_CRYPTO_ecdhe_key_get_public (otr_ephemeral_key, &otr_kx_msg.ephemeral_key);
   LOG (GNUNET_ERROR_TYPE_INFO, "GLOBAL OTR RE-KEY, NEW EPHM: %s\n",
-       GNUNET_h2s ((struct GNUNET_HashCode *) &otr_kx_msg.ephemeral_key));
+       GNUNET_i2s ((struct GNUNET_PeerIdentity *) &otr_kx_msg.ephemeral_key));
 
   GNUNET_assert (GNUNET_OK ==
                  GNUNET_CRYPTO_eddsa_sign (id_key,
@@ -2408,7 +2479,8 @@ destroy_iterator (void *cls,
 {
   struct CadetTunnel *t = value;
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "GCT_shutdown destroying tunnel at %p\n", t);
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "GCT_shutdown destroying tunnel at %p\n", t);
   GCT_destroy (t);
   return GNUNET_YES;
 }
@@ -2453,7 +2525,9 @@ handle_data (struct CadetTunnel *t,
              int fwd)
 {
   struct CadetChannel *ch;
+  char buf[128];
   size_t size;
+  uint16_t type;
 
   /* Check size */
   size = ntohs (msg->header.size);
@@ -2464,8 +2538,11 @@ handle_data (struct CadetTunnel *t,
     GNUNET_break (0);
     return;
   }
-  LOG (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
-              GC_m2s (ntohs (msg[1].header.type)));
+  type = ntohs (msg[1].header.type);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n", GC_m2s (type));
+  SPRINTF (buf, "# received payload of type %hu", type);
+  GNUNET_STATISTICS_update (stats, buf, 1, GNUNET_NO);
+
 
   /* Check channel */
   ch = GCT_get_channel (t, ntohl (msg->chid));
@@ -2473,8 +2550,7 @@ handle_data (struct CadetTunnel *t,
   {
     GNUNET_STATISTICS_update (stats, "# data on unknown channel",
                               1, GNUNET_NO);
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel 0x%X unknown\n",
-         ntohl (msg->chid));
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "channel 0x%X unknown\n", ntohl (msg->chid));
     send_channel_destroy (t, ntohl (msg->chid));
     return;
   }
@@ -2527,8 +2603,8 @@ handle_data_ack (struct CadetTunnel *t,
 /**
  * Handle channel create.
  *
- * @param t Tunnel on which the data came.
- * @param msg Data message.
+ * @param t Tunnel on which the message came.
+ * @param msg ChannelCreate message.
  */
 static void
 handle_ch_create (struct CadetTunnel *t,
@@ -2541,7 +2617,7 @@ handle_ch_create (struct CadetTunnel *t,
   size = ntohs (msg->header.size);
   if (size != sizeof (struct GNUNET_CADET_ChannelCreate))
   {
-    GNUNET_break (0);
+    GNUNET_break_op (0);
     return;
   }
 
@@ -2674,19 +2750,6 @@ handle_ch_destroy (struct CadetTunnel *t,
 }
 
 
-/**
- * Create a new Axolotl ephemeral (ratchet) key.
- *
- * @param t Tunnel.
- */
-static void
-new_ephemeral (struct CadetTunnel *t)
-{
-  GNUNET_free_non_null (t->ax->DHRs);
-  t->ax->DHRs = GNUNET_CRYPTO_ecdhe_key_create();
-}
-
-
 /**
  * Free Axolotl data.
  *
@@ -2700,6 +2763,9 @@ destroy_ax (struct CadetTunnel *t)
 
   GNUNET_free_non_null (t->ax->DHRs);
   GNUNET_free_non_null (t->ax->kx_0);
+  while (NULL != t->ax->skipped_head)
+    delete_skipped_key (t, t->ax->skipped_head);
+  GNUNET_assert (0 == t->ax->skipped);
 
   GNUNET_free (t->ax);
   t->ax = NULL;
@@ -2717,7 +2783,6 @@ destroy_ax (struct CadetTunnel *t)
 }
 
 
-
 /**
  * The peer's ephemeral key has changed: update the symmetrical keys.
  *
@@ -2728,13 +2793,11 @@ static void
 handle_ephemeral (struct CadetTunnel *t,
                   const struct GNUNET_CADET_KX_Ephemeral *msg)
 {
-  LOG (GNUNET_ERROR_TYPE_INFO, "<=== EPHM for %s\n", GCT_2s (t));
+  LOG (GNUNET_ERROR_TYPE_INFO, "<== EPHM for %s\n", GCT_2s (t));
 
+  /* Some old versions are still around, don't log as error. */
   if (GNUNET_OK != check_ephemeral (t, msg))
-  {
-    GNUNET_break_op (0);
     return;
-  }
 
   /* If we get a proper OTR-style ephemeral, fallback to old crypto. */
   if (NULL != t->ax)
@@ -2749,7 +2812,7 @@ handle_ephemeral (struct CadetTunnel *t,
       GNUNET_break (0);
       return;
     }
-    rekey_tunnel (t, NULL);
+    rekey_tunnel (t);
     GNUNET_STATISTICS_update (stats, "# otr-downgrades", -1, GNUNET_NO);
   }
 
@@ -2762,9 +2825,9 @@ handle_ephemeral (struct CadetTunnel *t,
   {
     #if DUMP_KEYS_TO_STDERR
     LOG (GNUNET_ERROR_TYPE_INFO, "OLD: %s\n",
-         GNUNET_h2s ((struct GNUNET_HashCode *) &t->peers_ephemeral_key));
+         GNUNET_i2s ((struct GNUNET_PeerIdentity *) &t->peers_ephemeral_key));
     LOG (GNUNET_ERROR_TYPE_INFO, "NEW: %s\n",
-         GNUNET_h2s ((struct GNUNET_HashCode *) &msg->ephemeral_key));
+         GNUNET_i2s ((struct GNUNET_PeerIdentity *) &msg->ephemeral_key));
     #endif
     t->peers_ephemeral_key = msg->ephemeral_key;
 
@@ -2781,7 +2844,7 @@ handle_ephemeral (struct CadetTunnel *t,
     }
     if (NULL != t->rekey_task)
       GNUNET_SCHEDULER_cancel (t->rekey_task);
-    t->rekey_task = GNUNET_SCHEDULER_add_now (rekey_tunnel, t);
+    t->rekey_task = GNUNET_SCHEDULER_add_now (&rekey_tunnel, t);
   }
   if (CADET_TUNNEL_KEY_SENT == t->estate)
   {
@@ -2811,11 +2874,12 @@ handle_ephemeral (struct CadetTunnel *t,
  * @param msg Key eXchange Pong message.
  */
 static void
-handle_pong (struct CadetTunnel *t, const struct GNUNET_CADET_KX_Pong *msg)
+handle_pong (struct CadetTunnel *t,
+             const struct GNUNET_CADET_KX_Pong *msg)
 {
   uint32_t challenge;
 
-  LOG (GNUNET_ERROR_TYPE_INFO, "<=== PONG for %s\n", GCT_2s (t));
+  LOG (GNUNET_ERROR_TYPE_INFO, "<== PONG for %s\n", GCT_2s (t));
   if (NULL == t->rekey_task)
   {
     GNUNET_STATISTICS_update (stats, "# duplicate PONG messages", 1, GNUNET_NO);
@@ -2863,13 +2927,11 @@ handle_kx_ax (struct CadetTunnel *t, const struct GNUNET_CADET_AX_KX *msg)
   struct CadetTunnelAxolotl *ax;
   struct GNUNET_HashCode key_material[3];
   struct GNUNET_CRYPTO_SymmetricSessionKey keys[5];
-  const struct GNUNET_CRYPTO_EcdhePublicKey *pub;
-  const struct GNUNET_CRYPTO_EcdhePrivateKey *priv;
   const char salt[] = "CADET Axolotl salt";
   const struct GNUNET_PeerIdentity *pid;
   int am_I_alice;
 
-  LOG (GNUNET_ERROR_TYPE_INFO, "<=== AX_KX on %s\n", GCT_2s (t));
+  LOG (GNUNET_ERROR_TYPE_INFO, "<== {     AX_KX} on %s\n", GCT_2s (t));
 
   if (NULL == t->ax)
   {
@@ -2878,13 +2940,7 @@ handle_kx_ax (struct CadetTunnel *t, const struct GNUNET_CADET_AX_KX *msg)
     GNUNET_break (CADET_Axolotl == t->enc_type);
     return;
   }
-
-  if (GNUNET_OK != GCP_check_key (t->peer, &msg->permanent_key,
-                                  &msg->purpose, &msg->signature))
-  {
-    GNUNET_break_op (0);
-    return;
-  }
+  ax = t->ax;
 
   pid = GCT_get_destination (t);
   if (0 > GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, pid))
@@ -2897,48 +2953,62 @@ handle_kx_ax (struct CadetTunnel *t, const struct GNUNET_CADET_AX_KX *msg)
     return;
   }
 
-  if (GNUNET_YES == ntohl (msg->force_reply))
+  if (0 != (GNUNET_CADET_AX_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);
+  }
 
-  if (CADET_TUNNEL_KEY_OK == t->estate)
+  if (0 == memcmp (&ax->DHRr, &msg->ratchet_key, sizeof(msg->ratchet_key)))
+  {
+    LOG (GNUNET_ERROR_TYPE_INFO, " known ratchet key, exit\n");
     return;
+  }
 
   LOG (GNUNET_ERROR_TYPE_INFO, " is Alice? %s\n", am_I_alice ? "YES" : "NO");
 
-  ax = t->ax;
   ax->DHRr = msg->ratchet_key;
-  ax->DHIr = msg->permanent_key;
 
   /* ECDH A B0 */
   if (GNUNET_YES == am_I_alice)
   {
-    priv = ax_key;                                              /* A */
-    pub = &msg->ephemeral_key;                                  /* B0 */
+    GNUNET_CRYPTO_eddsa_ecdh (id_key,              /* A */
+                              &msg->ephemeral_key, /* B0 */
+                              &key_material[0]);
   }
   else
   {
-    priv = ax->kx_0;                                            /* B0 */
-    pub = &ax->DHIr;                                            /* A */
+    GNUNET_CRYPTO_ecdh_eddsa (ax->kx_0,            /* B0 */
+                              &pid->public_key,    /* A */
+                              &key_material[0]);
   }
-  GNUNET_CRYPTO_ecc_ecdh (priv, pub, &key_material[0]);
 
   /* ECDH A0 B */
   if (GNUNET_YES == am_I_alice)
   {
-    priv = ax->kx_0;                                            /* A0 */
-    pub = &ax->DHIr;                                            /* B */
+    GNUNET_CRYPTO_ecdh_eddsa (ax->kx_0,            /* A0 */
+                              &pid->public_key,    /* B */
+                              &key_material[1]);
   }
   else
   {
-    priv = ax_key;                                              /* B */
-    pub = &msg->ephemeral_key;                                  /* A0 */
+    GNUNET_CRYPTO_eddsa_ecdh (id_key,              /* A */
+                              &msg->ephemeral_key, /* B0 */
+                              &key_material[1]);
+
+
   }
-  GNUNET_CRYPTO_ecc_ecdh (priv, pub, &key_material[1]);
 
-  /* ECDH A0 B0*/
-  priv = ax->kx_0;                                              /* A0 or B0 */
-  pub = &msg->ephemeral_key;                                    /* B0 or A0 */
-  GNUNET_CRYPTO_ecc_ecdh (priv, pub, &key_material[2]);
+  /* ECDH A0 B0 */
+  /* (This is the triple-DH, we could probably safely skip this,
+     as A0/B0 are already in the key material.) */
+  GNUNET_CRYPTO_ecc_ecdh (ax->kx_0,             /* A0 or B0 */
+                          &msg->ephemeral_key,  /* B0 or A0 */
+                          &key_material[2]);
 
   #if DUMP_KEYS_TO_STDERR
   {
@@ -2954,6 +3024,11 @@ handle_kx_ax (struct CadetTunnel *t, const struct GNUNET_CADET_AX_KX *msg)
                      salt, sizeof (salt),
                      &key_material, sizeof (key_material), NULL);
 
+  if (0 == memcmp (&ax->RK, &keys[0], sizeof(ax->RK)))
+  {
+    LOG (GNUNET_ERROR_TYPE_INFO, " known handshake key, exit\n");
+    return;
+  }
   ax->RK = keys[0];
   if (GNUNET_YES == am_I_alice)
   {
@@ -2975,7 +3050,11 @@ handle_kx_ax (struct CadetTunnel *t, const struct GNUNET_CADET_AX_KX *msg)
     ax->ratchet_expiration =
       GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(), ratchet_time);
   }
-  GCT_change_estate (t, CADET_TUNNEL_KEY_OK);
+  ax->PNs = 0;
+  ax->Nr = 0;
+  ax->Ns = 0;
+  GCT_change_estate (t, CADET_TUNNEL_KEY_PING);
+  send_queued_data (t);
 }
 
 
@@ -2996,9 +3075,12 @@ handle_decrypted (struct CadetTunnel *t,
                   int fwd)
 {
   uint16_t type;
+  char buf[256];
 
   type = ntohs (msgh->type);
-  LOG (GNUNET_ERROR_TYPE_INFO, "<=== %s on %s\n", GC_m2s (type), GCT_2s (t));
+  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)
   {
@@ -3041,6 +3123,7 @@ handle_decrypted (struct CadetTunnel *t,
   }
 }
 
+
 /******************************************************************************/
 /********************************    API    ***********************************/
 /******************************************************************************/
@@ -3057,48 +3140,70 @@ GCT_handle_encrypted (struct CadetTunnel *t,
 {
   uint16_t size = ntohs (msg->size);
   char cbuf [size];
-  size_t payload_size;
   int decrypted_size;
   uint16_t type;
-  struct GNUNET_MessageHeader *msgh;
+  const struct GNUNET_MessageHeader *msgh;
   unsigned int off;
 
   type = ntohs (msg->type);
-  if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type)
-  {
-    const struct GNUNET_CADET_Encrypted *emsg;
-
-    emsg = (struct GNUNET_CADET_Encrypted *) msg;
-    payload_size = size - sizeof (struct GNUNET_CADET_Encrypted);
-    decrypted_size = t_decrypt_and_validate (t, cbuf, &emsg[1], payload_size,
-                                             emsg->iv, &emsg->hmac);
-  }
-  else if (GNUNET_MESSAGE_TYPE_CADET_AX == type)
+  switch (type)
   {
-    const struct GNUNET_CADET_AX *emsg;
+  case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
+    {
+      const struct GNUNET_CADET_Encrypted *emsg;
+      size_t payload_size;
+
+      GNUNET_STATISTICS_update (stats, "# received OTR", 1, GNUNET_NO);
+      emsg = (const struct GNUNET_CADET_Encrypted *) msg;
+      payload_size = size - sizeof (struct GNUNET_CADET_Encrypted);
+      decrypted_size = t_decrypt_and_validate (t, cbuf, &emsg[1], payload_size,
+                                               emsg->iv, &emsg->hmac);
+    }
+    break;
+  case GNUNET_MESSAGE_TYPE_CADET_AX:
+    {
+      const struct GNUNET_CADET_AX *emsg;
 
-    emsg = (struct GNUNET_CADET_AX *) msg;
-    decrypted_size = t_ax_decrypt_and_validate (t, cbuf, emsg, size);
+      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_break_op (0);
+    GNUNET_STATISTICS_update (stats, "# unable to decrypt", 1, GNUNET_NO);
+    LOG (GNUNET_ERROR_TYPE_WARNING, "Wrong crypto on 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 < decrypted_size)
+  while (off + sizeof (struct GNUNET_MessageHeader) <= decrypted_size)
   {
     uint16_t msize;
 
-    msgh = (struct GNUNET_MessageHeader *) &cbuf[off];
+    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;
   }
@@ -3116,9 +3221,12 @@ 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_KX_EPHEMERAL:
@@ -3199,16 +3307,6 @@ GCT_init (const struct GNUNET_CONFIGURATION_Handle *c,
   otr_kx_msg.purpose.size = htonl (ephemeral_purpose_size ());
   otr_kx_msg.origin_identity = my_full_id;
   rekey_task = GNUNET_SCHEDULER_add_now (&global_otr_rekey, NULL);
-
-  ax_key = GNUNET_CRYPTO_ecdhe_key_create ();
-  GNUNET_CRYPTO_ecdhe_key_get_public (ax_key, &ax_identity.permanent_key);
-  ax_identity.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_CADET_AXKX);
-  ax_identity.purpose.size = htonl (ax_purpose_size ());
-  GNUNET_assert (GNUNET_OK ==
-                 GNUNET_CRYPTO_eddsa_sign (id_key,
-                                           &ax_identity.purpose,
-                                           &ax_identity.signature));
-
   tunnels = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_YES);
 }
 
@@ -3219,6 +3317,7 @@ GCT_init (const struct GNUNET_CONFIGURATION_Handle *c,
 void
 GCT_shutdown (void)
 {
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down tunnels\n");
   if (NULL != rekey_task)
   {
     GNUNET_SCHEDULER_cancel (rekey_task);
@@ -3226,7 +3325,6 @@ GCT_shutdown (void)
   }
   GNUNET_CONTAINER_multipeermap_iterate (tunnels, &destroy_iterator, NULL);
   GNUNET_CONTAINER_multipeermap_destroy (tunnels);
-  GNUNET_free (ax_key);
 }
 
 
@@ -3306,6 +3404,8 @@ GCT_change_cstate (struct CadetTunnel* t, enum CadetTunnelCState cstate)
 /**
  * Change the tunnel encryption state.
  *
+ * If the encryption state changes to OK, stop the rekey task.
+ *
  * @param t Tunnel whose encryption state to change, or NULL.
  * @param state New encryption state.
  */
@@ -3324,11 +3424,16 @@ GCT_change_estate (struct CadetTunnel* t, enum CadetTunnelEState state)
   LOG (GNUNET_ERROR_TYPE_DEBUG, "Tunnel %s estate is now %s\n",
        GCP_2s (t->peer), estate2s (t->estate));
 
-  /* Send queued data if enc state changes to OK */
-  if (myid != GCP_get_short_id (t->peer) &&
-      CADET_TUNNEL_KEY_OK != old && CADET_TUNNEL_KEY_OK == t->estate)
+  if (CADET_TUNNEL_KEY_OK != old && CADET_TUNNEL_KEY_OK == t->estate)
   {
-    send_queued_data (t);
+    if (NULL != t->rekey_task)
+    {
+      GNUNET_SCHEDULER_cancel (t->rekey_task);
+      t->rekey_task = NULL;
+    }
+    /* Send queued data if tunnel is not loopback */
+    if (myid != GCP_get_short_id (t->peer))
+      send_queued_data (t);
   }
 }
 
@@ -3341,18 +3446,13 @@ GCT_change_estate (struct CadetTunnel* t, enum CadetTunnelEState state)
  * of being created/processed.
  *
  * @param cls Closure (Tunnel to check).
- * @param tc Task context.
  */
 static void
-trim_connections (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+trim_connections (void *cls)
 {
   struct CadetTunnel *t = cls;
 
   t->trim_connections_task = NULL;
-
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-    return;
-
   if (GCT_count_connections (t) > 2 * CONNECTIONS_PER_TUNNEL)
   {
     struct CadetTConnection *iter;
@@ -3455,7 +3555,6 @@ GCT_remove_connection (struct CadetTunnel *t,
 
   /* Start new connections if needed */
   if (CONNECTIONS_PER_TUNNEL > conns
-      && NULL == t->destroy_task
       && CADET_TUNNEL_SHUTDOWN != t->cstate
       && GNUNET_NO == shutting_down)
   {
@@ -3499,8 +3598,11 @@ GCT_add_channel (struct CadetTunnel *t, struct CadetChannel *ch)
 
   aux = GNUNET_new (struct CadetTChannel);
   aux->ch = ch;
-  LOG (GNUNET_ERROR_TYPE_DEBUG, " adding %p to %p\n", aux, t->channel_head);
-  GNUNET_CONTAINER_DLL_insert_tail (t->channel_head, t->channel_tail, aux);
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       " adding %p to %p\n", aux, t->channel_head);
+  GNUNET_CONTAINER_DLL_insert_tail (t->channel_head,
+                                   t->channel_tail,
+                                   aux);
 
   if (NULL != t->destroy_task)
   {
@@ -3528,7 +3630,9 @@ GCT_remove_channel (struct CadetTunnel *t, struct CadetChannel *ch)
     if (aux->ch == ch)
     {
       LOG (GNUNET_ERROR_TYPE_DEBUG, " found! %s\n", GCCH_2s (ch));
-      GNUNET_CONTAINER_DLL_remove (t->channel_head, t->channel_tail, aux);
+      GNUNET_CONTAINER_DLL_remove (t->channel_head,
+                                  t->channel_tail,
+                                  aux);
       GNUNET_free (aux);
       return;
     }
@@ -3570,25 +3674,18 @@ GCT_get_channel (struct CadetTunnel *t, CADET_ChannelNumber chid)
  * the tunnel. This way we avoid a new public key handshake.
  *
  * @param cls Closure (tunnel to destroy).
- * @param tc Task context.
  */
 static void
-delayed_destroy (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+delayed_destroy (void *cls)
 {
   struct CadetTunnel *t = cls;
   struct CadetTConnection *iter;
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "delayed destroying tunnel %p\n", t);
-  if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
-  {
-    LOG (GNUNET_ERROR_TYPE_WARNING,
-         "Not destroying tunnel, due to shutdown. "
-         "Tunnel at %p should have been freed by GCT_shutdown\n", t);
-    return;
-  }
   t->destroy_task = NULL;
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "delayed destroying tunnel %p\n",
+       t);
   t->cstate = CADET_TUNNEL_SHUTDOWN;
-
   for (iter = t->connection_head; NULL != iter; iter = iter->next)
   {
     GCC_send_destroy (iter->c);
@@ -3630,7 +3727,7 @@ GCT_destroy_empty (struct CadetTunnel *t)
   // FIXME make delay a config option
   t->destroy_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
                                                   &delayed_destroy, t);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled destroy of %p as %llu\n",
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled destroy of %p as %p\n",
        t, t->destroy_task);
 }
 
@@ -3669,12 +3766,14 @@ GCT_destroy (struct CadetTunnel *t)
   struct CadetTConnection *next_c;
   struct CadetTChannel *iter_ch;
   struct CadetTChannel *next_ch;
+  unsigned int keepalives_queued;
 
   if (NULL == t)
     return;
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n", GCP_2s (t->peer));
-
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "destroying tunnel %s\n",
+       GCP_2s (t->peer));
   GNUNET_break (GNUNET_YES ==
                 GNUNET_CONTAINER_multipeermap_remove (tunnels,
                                                       GCP_get_id (t->peer), t));
@@ -3690,17 +3789,49 @@ GCT_destroy (struct CadetTunnel *t)
     GCCH_destroy (iter_ch->ch);
     /* Should only happen on shutdown, but it's ok. */
   }
+  keepalives_queued = 0;
+  while (NULL != t->tq_head)
+  {
+    /* Should have been cleaned by destuction of channel. */
+    struct GNUNET_MessageHeader *mh;
+    uint16_t type;
+
+    mh = (struct GNUNET_MessageHeader *) &t->tq_head[1];
+    type = ntohs (mh->type);
+    if (0 == keepalives_queued && GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE == type)
+    {
+      keepalives_queued = 1;
+      LOG (GNUNET_ERROR_TYPE_DEBUG,
+           "one keepalive left behind on tunnel shutdown\n");
+    }
+    else if (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY == type)
+    {
+      LOG (GNUNET_ERROR_TYPE_WARNING,
+           "tunnel destroyed before a CHANNEL_DESTROY was sent to peer\n");
+    }
+    else
+    {
+      GNUNET_break (0);
+      LOG (GNUNET_ERROR_TYPE_ERROR,
+           "message left behind on tunnel shutdown: %s\n",
+           GC_m2s (type));
+    }
+    unqueue_data (t->tq_head);
+  }
+
 
   if (NULL != t->destroy_task)
   {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "cancelling dest: %llX\n", t->destroy_task);
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+        "cancelling dest: %p\n",
+        t->destroy_task);
     GNUNET_SCHEDULER_cancel (t->destroy_task);
     t->destroy_task = NULL;
   }
 
   if (NULL != t->trim_connections_task)
   {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "cancelling trim: %llX\n",
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "cancelling trim: %p\n",
          t->trim_connections_task);
     GNUNET_SCHEDULER_cancel (t->trim_connections_task);
     t->trim_connections_task = NULL;
@@ -3739,13 +3870,13 @@ GCT_destroy (struct CadetTunnel *t)
  * @return Connection created.
  */
 struct CadetConnection *
-GCT_use_path (struct CadetTunnel *t, struct CadetPeerPath *p)
+GCT_use_path (struct CadetTunnel *t, struct CadetPeerPath *path)
 {
   struct CadetConnection *c;
   struct GNUNET_CADET_Hash cid;
   unsigned int own_pos;
 
-  if (NULL == t || NULL == p)
+  if (NULL == t || NULL == path)
   {
     GNUNET_break (0);
     return NULL;
@@ -3757,19 +3888,19 @@ GCT_use_path (struct CadetTunnel *t, struct CadetPeerPath *p)
     return NULL;
   }
 
-  for (own_pos = 0; own_pos < p->length; own_pos++)
+  for (own_pos = 0; own_pos < path->length; own_pos++)
   {
-    if (p->peers[own_pos] == myid)
+    if (path->peers[own_pos] == myid)
       break;
   }
-  if (own_pos >= p->length)
+  if (own_pos >= path->length)
   {
     GNUNET_break_op (0);
     return NULL;
   }
 
   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_NONCE, &cid, sizeof (cid));
-  c = GCC_new (&cid, t, p, own_pos);
+  c = GCC_new (&cid, t, path, own_pos);
   if (NULL == c)
   {
     /* Path was flawed */
@@ -3903,7 +4034,7 @@ GCT_get_channels_buffer (struct CadetTunnel *t)
   {
     /* Probably getting buffer for a channel create/handshake. */
     LOG (GNUNET_ERROR_TYPE_DEBUG, "  no channels, allow max\n");
-    return 64;
+    return MIN_TUNNEL_BUFFER;
   }
 
   buffer = 0;
@@ -3913,6 +4044,14 @@ GCT_get_channels_buffer (struct CadetTunnel *t)
     if (ch_buf > buffer)
       buffer = ch_buf;
   }
+  if (MIN_TUNNEL_BUFFER > buffer)
+    return MIN_TUNNEL_BUFFER;
+
+  if (MAX_TUNNEL_BUFFER < buffer)
+  {
+    GNUNET_break (0);
+    return MAX_TUNNEL_BUFFER;
+  }
   return buffer;
 }
 
@@ -3932,7 +4071,7 @@ GCT_get_connections_buffer (struct CadetTunnel *t)
 
   if (GNUNET_NO == is_ready (t))
   {
-    if (count_queued_data (t) > 3)
+    if (count_queued_data (t) >= 3)
       return 0;
     else
       return 1;
@@ -4024,6 +4163,9 @@ GCT_unchoke_channels (struct CadetTunnel *t)
   if (NULL != t->channel_head)
     LOG (GNUNET_ERROR_TYPE_DEBUG, " head ch: %p\n", t->channel_head->ch);
 
+  if (NULL != t->tq_head)
+    send_queued_data (t);
+
   /* Get buffer space */
   buffer = GCT_get_connections_buffer (t);
   if (0 == buffer)
@@ -4139,6 +4281,7 @@ GCT_cancel (struct CadetTunnelQueue *q)
 {
   if (NULL != q->cq)
   {
+    GNUNET_assert (NULL == q->tqd);
     GCC_cancel (q->cq);
     /* tun_message_sent() will be called and free q */
   }
@@ -4157,6 +4300,21 @@ GCT_cancel (struct CadetTunnelQueue *q)
 }
 
 
+/**
+ * Check if the tunnel has queued traffic.
+ *
+ * @param t Tunnel to check.
+ *
+ * @return #GNUNET_YES if there is queued traffic
+ *         #GNUNET_NO otherwise
+ */
+int
+GCT_has_queued_traffic (struct CadetTunnel *t)
+{
+  return (NULL != t->tq_head) ? GNUNET_YES : GNUNET_NO;
+}
+
+
 /**
  * Sends an already built message on a tunnel, encrypting it and
  * choosing the best connection if not provided.
@@ -4189,8 +4347,9 @@ void
 GCT_send_ax_kx (struct CadetTunnel *t, int force_reply)
 {
   struct GNUNET_CADET_AX_KX msg;
+  enum GNUNET_CADET_AX_KX_Flags flags;
 
-  LOG (GNUNET_ERROR_TYPE_INFO, "===> AX_KX for %s\n", GCT_2s (t));
+  LOG (GNUNET_ERROR_TYPE_INFO, "==> {     AX_KX} on %s\n", GCT_2s (t));
   if (NULL != t->ephm_h)
   {
     LOG (GNUNET_ERROR_TYPE_INFO, "     already queued\n");
@@ -4199,15 +4358,15 @@ GCT_send_ax_kx (struct CadetTunnel *t, int force_reply)
 
   msg.header.size = htons (sizeof (msg));
   msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_AX_KX);
-  msg.force_reply = htonl (force_reply);
-  msg.permanent_key = ax_identity.permanent_key;
-  msg.purpose = ax_identity.purpose;
-  msg.signature = ax_identity.signature;
+  flags = GNUNET_CADET_AX_KX_FLAG_NONE;
+  if (force_reply)
+    flags |= GNUNET_CADET_AX_KX_FLAG_FORCE_REPLY;
+  msg.flags = htonl (flags);
   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);
-  if (CADET_TUNNEL_KEY_OK != t->estate)
+  if (CADET_TUNNEL_KEY_UNINITIALIZED == t->estate)
     GCT_change_estate (t, CADET_TUNNEL_KEY_SENT);
 }
 
@@ -4234,7 +4393,8 @@ GCT_resend_message (const struct GNUNET_MessageHeader *message,
     return;
   }
   fwd = GCC_is_origin (c, GNUNET_YES);
-  GNUNET_break (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
+  GNUNET_break (NULL == GCC_send_prebuilt_message (message, UINT16_MAX, 0,
+                                                   c, fwd,
                                                    GNUNET_YES, NULL, NULL));
 }
 
@@ -4347,34 +4507,28 @@ ax_debug (const struct CadetTunnelAxolotl *ax, enum GNUNET_ErrorType level)
   struct GNUNET_CRYPTO_EcdhePublicKey pub;
   struct CadetTunnelSkippedKey *iter;
 
+  LOG2 (level, "TTT  RK  \t %s\n",
+        GNUNET_i2s ((struct GNUNET_PeerIdentity *) &ax->RK));
 
-  LOG2 (level, "TTT  RK\t %s\n",
-        GNUNET_h2s ((struct GNUNET_HashCode *) &ax->RK));
-
-  LOG2 (level, "TTT  HKs\t %s\n",
-        GNUNET_h2s ((struct GNUNET_HashCode *) &ax->HKs));
-  LOG2 (level, "TTT  HKr\t %s\n",
-        GNUNET_h2s ((struct GNUNET_HashCode *) &ax->HKr));
+  LOG2 (level, "TTT  HKs \t %s\n",
+        GNUNET_i2s ((struct GNUNET_PeerIdentity *) &ax->HKs));
+  LOG2 (level, "TTT  HKr \t %s\n",
+        GNUNET_i2s ((struct GNUNET_PeerIdentity *) &ax->HKr));
   LOG2 (level, "TTT  NHKs\t %s\n",
-        GNUNET_h2s ((struct GNUNET_HashCode *) &ax->NHKs));
+        GNUNET_i2s ((struct GNUNET_PeerIdentity *) &ax->NHKs));
   LOG2 (level, "TTT  NHKr\t %s\n",
-        GNUNET_h2s ((struct GNUNET_HashCode *) &ax->NHKr));
-
-  LOG2 (level, "TTT  CKs\t %s\n",
-        GNUNET_h2s ((struct GNUNET_HashCode *) &ax->CKs));
-  LOG2 (level, "TTT  CKr\t %s\n",
-        GNUNET_h2s ((struct GNUNET_HashCode *) &ax->CKr));
-
-  GNUNET_CRYPTO_ecdhe_key_get_public (ax_key, &pub);
-  LOG2 (level, "TTT  DHIs\t %s\n",
-        GNUNET_h2s ((struct GNUNET_HashCode *) &pub));
-  LOG2 (level, "TTT  DHIr\t %s\n",
-        GNUNET_h2s ((struct GNUNET_HashCode *) &ax->DHIr));
+        GNUNET_i2s ((struct GNUNET_PeerIdentity *) &ax->NHKr));
+
+  LOG2 (level, "TTT  CKs \t %s\n",
+        GNUNET_i2s ((struct GNUNET_PeerIdentity *) &ax->CKs));
+  LOG2 (level, "TTT  CKr \t %s\n",
+        GNUNET_i2s ((struct GNUNET_PeerIdentity *) &ax->CKr));
+
   GNUNET_CRYPTO_ecdhe_key_get_public (ax->DHRs, &pub);
   LOG2 (level, "TTT  DHRs\t %s\n",
-        GNUNET_h2s ((struct GNUNET_HashCode *) &pub));
+        GNUNET_i2s ((struct GNUNET_PeerIdentity *) &pub));
   LOG2 (level, "TTT  DHRr\t %s\n",
-        GNUNET_h2s ((struct GNUNET_HashCode *) &ax->DHRr));
+        GNUNET_i2s ((struct GNUNET_PeerIdentity *) &ax->DHRr));
 
   LOG2 (level, "TTT  Nr\t %u\tNs\t%u\n", ax->Nr, ax->Ns);
   LOG2 (level, "TTT  PNs\t %u\tSkipped\t%u\n", ax->PNs, ax->skipped);
@@ -4383,9 +4537,9 @@ ax_debug (const struct CadetTunnelAxolotl *ax, enum GNUNET_ErrorType level)
   for (iter = ax->skipped_head; NULL != iter; iter = iter->next)
   {
     LOG2 (level, "TTT    HK\t %s\n",
-          GNUNET_h2s ((struct GNUNET_HashCode *) &iter->HK));
+          GNUNET_i2s ((struct GNUNET_PeerIdentity *) &iter->HK));
     LOG2 (level, "TTT    MK\t %s\n",
-          GNUNET_h2s ((struct GNUNET_HashCode *) &iter->MK));
+          GNUNET_i2s ((struct GNUNET_PeerIdentity *) &iter->MK));
   }
 }
 
@@ -4421,29 +4575,29 @@ GCT_debug (const struct CadetTunnel *t, enum GNUNET_ErrorType level)
   else
   {
     LOG2 (level, "TTT  my EPHM\t %s\n",
-          GNUNET_h2s ((struct GNUNET_HashCode *) &otr_kx_msg.ephemeral_key));
+          GNUNET_i2s ((struct GNUNET_PeerIdentity *) &otr_kx_msg.ephemeral_key));
     LOG2 (level, "TTT  peers EPHM:\t %s\n",
-          GNUNET_h2s ((struct GNUNET_HashCode *) &t->peers_ephemeral_key));
+          GNUNET_i2s ((struct GNUNET_PeerIdentity *) &t->peers_ephemeral_key));
     LOG2 (level, "TTT  ENC key:\t %s\n",
-          GNUNET_h2s ((struct GNUNET_HashCode *) &t->e_key));
+          GNUNET_i2s ((struct GNUNET_PeerIdentity *) &t->e_key));
     LOG2 (level, "TTT  DEC key:\t %s\n",
-          GNUNET_h2s ((struct GNUNET_HashCode *) &t->d_key));
+          GNUNET_i2s ((struct GNUNET_PeerIdentity *) &t->d_key));
     if (t->kx_ctx)
     {
       LOG2 (level, "TTT  OLD ENC key:\t %s\n",
-            GNUNET_h2s ((struct GNUNET_HashCode *) &t->kx_ctx->e_key_old));
+            GNUNET_i2s ((struct GNUNET_PeerIdentity *) &t->kx_ctx->e_key_old));
       LOG2 (level, "TTT  OLD DEC key:\t %s\n",
-            GNUNET_h2s ((struct GNUNET_HashCode *) &t->kx_ctx->d_key_old));
+            GNUNET_i2s ((struct GNUNET_PeerIdentity *) &t->kx_ctx->d_key_old));
     }
   }
 #endif
   LOG2 (level, "TTT  tq_head %p, tq_tail %p\n", t->tq_head, t->tq_tail);
-  LOG2 (level, "TTT  destroy %u\n", t->destroy_task);
+  LOG2 (level, "TTT  destroy %p\n", t->destroy_task);
 
   LOG2 (level, "TTT  channels:\n");
   for (iterch = t->channel_head; NULL != iterch; iterch = iterch->next)
   {
-    LOG2 (level, "TTT  - %s\n", GCCH_2s (iterch->ch));
+    GCCH_debug (iterch->ch, level);
   }
 
   LOG2 (level, "TTT  connections:\n");