-style fix
[oweals/gnunet.git] / src / core / gnunet-service-core_kx.c
index ac5b08d791d595371384ddd79481f86976f729ef..d1990f8f4f5203edb4fd6aa1fa066c895ae3dad5 100644 (file)
@@ -1,3 +1,125 @@
+/*
+     This file is part of GNUnet.
+     (C) 2009-2013 Christian Grothoff (and other contributing authors)
+
+     GNUnet is free software; you can redistribute it and/or modify
+     it under the terms of the GNU General Public License as published
+     by the Free Software Foundation; either version 3, or (at your
+     option) any later version.
+
+     GNUnet is distributed in the hope that it will be useful, but
+     WITHOUT ANY WARRANTY; without even the implied warranty of
+     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+     General Public License for more details.
+
+     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.
+*/
+
+/**
+ * @file core/gnunet-service-core_kx.c
+ * @brief code for managing the key exchange (SET_KEY, PING, PONG) with other peers
+ * @author Christian Grothoff
+ */
+#include "platform.h"
+#include "gnunet-service-core_kx.h"
+#include "gnunet-service-core.h"
+#include "gnunet-service-core_clients.h"
+#include "gnunet-service-core_neighbours.h"
+#include "gnunet-service-core_sessions.h"
+#include "gnunet_statistics_service.h"
+#include "gnunet_constants.h"
+#include "gnunet_signatures.h"
+#include "gnunet_protocols.h"
+#include "core.h"
+
+
+/**
+ * How long do we wait for SET_KEY confirmation initially?
+ */
+#define INITIAL_SET_KEY_RETRY_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
+
+/**
+ * What is the minimum frequency for a PING message?
+ */
+#define MIN_PING_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
+
+/**
+ * How often do we rekey?
+ */
+#define REKEY_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 12)
+
+/**
+ * What time difference do we tolerate?
+ */
+#define REKEY_TOLERANCE GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
+
+/**
+ * What is the maximum age of a message for us to consider processing
+ * it?  Note that this looks at the timestamp used by the other peer,
+ * so clock skew between machines does come into play here.  So this
+ * should be picked high enough so that a little bit of clock skew
+ * does not prevent peers from connecting to us.
+ */
+#define MAX_MESSAGE_AGE GNUNET_TIME_UNIT_DAYS
+
+
+
+GNUNET_NETWORK_STRUCT_BEGIN
+
+/**
+ * Message transmitted with the signed ephemeral key of a peer.  The
+ * session key is then derived from the two ephemeral keys (ECDHE).
+ */
+struct EphemeralKeyMessage
+{
+
+  /**
+   * Message type is CORE_EPHEMERAL_KEY.
+   */
+  struct GNUNET_MessageHeader header;
+
+  /**
+   * Status of the sender (should be in "enum PeerStateMachine"), nbo.
+   */
+  int32_t sender_status GNUNET_PACKED;
+
+  /**
+   * An ECC signature of the 'origin' asserting the validity of
+   * the given ephemeral key.
+   */
+  struct GNUNET_CRYPTO_EddsaSignature signature;
+
+  /**
+   * Information about what is being signed.
+   */
+  struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
+
+  /**
+   * At what time was this key created (beginning of validity).
+   */
+  struct GNUNET_TIME_AbsoluteNBO creation_time;
+
+  /**
+   * When does the given ephemeral key expire (end of validity).
+   */
+  struct GNUNET_TIME_AbsoluteNBO expiration_time;
+
+  /**
+   * Ephemeral public ECC key (always for NIST P-521) encoded in a format suitable
+   * for network transmission as created using 'gcry_sexp_sprint'.
+   */
+  struct GNUNET_CRYPTO_EcdhePublicKey ephemeral_key;
+
+  /**
+   * Public key of the signing peer (persistent version, not the ephemeral public key).
+   */
+  struct GNUNET_PeerIdentity origin_identity;
+
+};
+
 
 /**
  * We're sending an (encrypted) PING to the other peer to check if he
@@ -29,10 +151,8 @@ struct PingMessage
 };
 
 
-
 /**
- * Response to a PING.  Includes data from the original PING
- * plus initial bandwidth quota information.
+ * Response to a PING.  Includes data from the original PING.
  */
 struct PongMessage
 {
@@ -53,10 +173,9 @@ struct PongMessage
   uint32_t challenge GNUNET_PACKED;
 
   /**
-   * Desired bandwidth (how much we should send to this
-   * peer / how much is the sender willing to receive).
+   * Reserved, always zero.
    */
-  struct GNUNET_BANDWIDTH_Value32NBO inbound_bw_limit;
+  uint32_t reserved;
 
   /**
    * Intended target of the PING, used primarily to check
@@ -67,892 +186,1414 @@ struct PongMessage
 
 
 /**
- * Message transmitted to set (or update) a session key.
+ * Encapsulation for encrypted messages exchanged between
+ * peers.  Followed by the actual encrypted data.
  */
-struct SetKeyMessage
+struct EncryptedMessage
 {
-
   /**
-   * Message type is either CORE_SET_KEY.
+   * Message type is either CORE_ENCRYPTED_MESSAGE.
    */
   struct GNUNET_MessageHeader header;
 
   /**
-   * Status of the sender (should be in "enum PeerStateMachine"), nbo.
+   * Random value used for IV generation.
    */
-  int32_t sender_status GNUNET_PACKED;
+  uint32_t iv_seed GNUNET_PACKED;
 
   /**
-   * Purpose of the signature, will be
-   * GNUNET_SIGNATURE_PURPOSE_SET_KEY.
+   * MAC of the encrypted message (starting at 'sequence_number'),
+   * used to verify message integrity. Everything after this value
+   * (excluding this value itself) will be encrypted and authenticated.
+   * ENCRYPTED_HEADER_SIZE must be set to the offset of the *next* field.
    */
-  struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
+  struct GNUNET_HashCode hmac;
 
   /**
-   * At what time was this key created?
+   * Sequence number, in network byte order.  This field
+   * must be the first encrypted/decrypted field
    */
-  struct GNUNET_TIME_AbsoluteNBO creation_time;
+  uint32_t sequence_number GNUNET_PACKED;
 
   /**
-   * The encrypted session key.
+   * Reserved, always zero.
    */
-  struct GNUNET_CRYPTO_RsaEncryptedData encrypted_key;
+  uint32_t reserved;
 
   /**
-   * Who is the intended recipient?
+   * Timestamp.  Used to prevent reply of ancient messages
+   * (recent messages are caught with the sequence number).
    */
-  struct GNUNET_PeerIdentity target;
+  struct GNUNET_TIME_AbsoluteNBO timestamp;
+
+};
+GNUNET_NETWORK_STRUCT_END
+
+
+/**
+ * Number of bytes (at the beginning) of "struct EncryptedMessage"
+ * that are NOT encrypted.
+ */
+#define ENCRYPTED_HEADER_SIZE (offsetof(struct EncryptedMessage, sequence_number))
+
+
+/**
+ * State machine for our P2P encryption handshake.  Everyone starts in
+ * "DOWN", if we receive the other peer's key (other peer initiated)
+ * we start in state RECEIVED (since we will immediately send our
+ * own); otherwise we start in SENT.  If we get back a PONG from
+ * within either state, we move up to CONFIRMED (the PONG will always
+ * be sent back encrypted with the key we sent to the other peer).
+ */
+enum KxStateMachine
+{
+  /**
+   * No handshake yet.
+   */
+  KX_STATE_DOWN,
+
+  /**
+   * We've sent our session key.
+   */
+  KX_STATE_KEY_SENT,
+
+  /**
+   * We've received the other peers session key.
+   */
+  KX_STATE_KEY_RECEIVED,
+
+  /**
+   * The other peer has confirmed our session key + PING with a PONG
+   * message encrypted with his session key (which we got).  Key
+   * exchange is done.
+   */
+  KX_STATE_UP,
+
+  /**
+   * We're rekeying (or had a timeout), so we have sent the other peer
+   * our new ephemeral key, but we did not get a matching PONG yet.
+   * This is equivalent to being 'KX_STATE_KEY_RECEIVED', except that
+   * the session is marked as 'up' with sessions (as we don't want to
+   * drop and re-establish P2P connections simply due to rekeying).
+   */
+  KX_STATE_REKEY_SENT
+
+};
+
+
+/**
+ * Information about the status of a key exchange with another peer.
+ */
+struct GSC_KeyExchangeInfo
+{
+
+  /**
+   * DLL.
+   */
+  struct GSC_KeyExchangeInfo *next;
+
+  /**
+   * DLL.
+   */
+  struct GSC_KeyExchangeInfo *prev;
+
+  /**
+   * Identity of the peer.
+   */
+  struct GNUNET_PeerIdentity peer;
+
+  /**
+   * PING message we transmit to the other peer.
+   */
+  struct PingMessage ping;
+
+  /**
+   * Key we use to encrypt our messages for the other peer
+   * (initialized by us when we do the handshake).
+   */
+  struct GNUNET_CRYPTO_SymmetricSessionKey encrypt_key;
+
+  /**
+   * Key we use to decrypt messages from the other peer
+   * (given to us by the other peer during the handshake).
+   */
+  struct GNUNET_CRYPTO_SymmetricSessionKey decrypt_key;
+
+  /**
+   * At what time did the other peer generate the decryption key?
+   */
+  struct GNUNET_TIME_Absolute foreign_key_expires;
+
+  /**
+   * When should the session time out (if there are no PONGs)?
+   */
+  struct GNUNET_TIME_Absolute timeout;
+
+  /**
+   * At what frequency are we currently re-trying SET_KEY messages?
+   */
+  struct GNUNET_TIME_Relative set_key_retry_frequency;
+
+  /**
+   * ID of task used for re-trying SET_KEY and PING message.
+   */
+  GNUNET_SCHEDULER_TaskIdentifier retry_set_key_task;
+
+  /**
+   * ID of task used for sending keep-alive pings.
+   */
+  GNUNET_SCHEDULER_TaskIdentifier keep_alive_task;
+
+  /**
+   * Bit map indicating which of the 32 sequence numbers before the last
+   * were received (good for accepting out-of-order packets and
+   * estimating reliability of the connection)
+   */
+  unsigned int last_packets_bitmap;
+
+  /**
+   * last sequence number received on this connection (highest)
+   */
+  uint32_t last_sequence_number_received;
+
+  /**
+   * last sequence number transmitted
+   */
+  uint32_t last_sequence_number_sent;
+
+  /**
+   * What was our PING challenge number (for this peer)?
+   */
+  uint32_t ping_challenge;
 
   /**
-   * Signature of the stuff above (starting at purpose).
+   * What is our connection status?
    */
-  struct GNUNET_CRYPTO_RsaSignature signature;
+  enum KxStateMachine status;
 
 };
 
 
 /**
- * Handle to peerinfo service.
+ * Our private key.
+ */
+static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
+
+/**
+ * Our ephemeral private key.
+ */
+static struct GNUNET_CRYPTO_EcdhePrivateKey *my_ephemeral_key;
+
+/**
+ * Current message we send for a key exchange.
  */
-static struct GNUNET_PEERINFO_Handle *peerinfo;
+static struct EphemeralKeyMessage current_ekm;
 
+/**
+ * Our message stream tokenizer (for encrypted payload).
+ */
+static struct GNUNET_SERVER_MessageStreamTokenizer *mst;
 
+/**
+ * DLL head.
+ */
+static struct GSC_KeyExchangeInfo *kx_head;
 
 /**
- * We received a PING message.  Validate and transmit
- * PONG.
+ * DLL tail.
+ */
+static struct GSC_KeyExchangeInfo *kx_tail;
+
+/**
+ * Task scheduled for periodic re-generation (and thus rekeying) of our
+ * ephemeral key.
+ */
+static GNUNET_SCHEDULER_TaskIdentifier rekey_task;
+
+
+/**
+ * Derive an authentication key from "set key" information
  *
- * @param n sender of the PING
- * @param m the encrypted PING message itself
- * @param ats performance data
- * @param ats_count number of entries in ats (excluding 0-termination)
+ * @param akey authentication key to derive
+ * @param skey session key to use
+ * @param seed seed to use
  */
 static void
-handle_ping (struct Neighbour *n, const struct PingMessage *m,
-             const struct GNUNET_TRANSPORT_ATS_Information *ats,
-             uint32_t ats_count)
+derive_auth_key (struct GNUNET_CRYPTO_AuthKey *akey,
+                 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey, uint32_t seed)
 {
-  struct PingMessage t;
-  struct PongMessage tx;
-  struct PongMessage *tp;
-  struct MessageEntry *me;
-  struct GNUNET_CRYPTO_AesInitializationVector iv;
-
-#if DEBUG_CORE
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Core service receives `%s' request from `%4s'.\n", "PING",
-              GNUNET_i2s (&n->peer));
-#endif
-  derive_iv (&iv, &n->decrypt_key, m->iv_seed, &my_identity);
-  if (GNUNET_OK !=
-      do_decrypt (n, &iv, &m->target, &t.target,
-                  sizeof (struct PingMessage) - ((void *) &m->target -
-                                                 (void *) m)))
-    return;
-#if DEBUG_HANDSHAKE
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Decrypted `%s' to `%4s' with challenge %u decrypted using key %u, IV %u (salt %u)\n",
-              "PING", GNUNET_i2s (&t.target), (unsigned int) t.challenge,
-              (unsigned int) n->decrypt_key.crc32, GNUNET_CRYPTO_crc32_n (&iv,
-                                                                          sizeof
-                                                                          (iv)),
-              m->iv_seed);
-#endif
-  GNUNET_STATISTICS_update (stats, gettext_noop ("# PING messages decrypted"),
-                            1, GNUNET_NO);
-  if (0 !=
-      memcmp (&t.target, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
-  {
-    char sender[9];
-    char peer[9];
+  static const char ctx[] = "authentication key";
 
-    GNUNET_snprintf (sender, sizeof (sender), "%8s", GNUNET_i2s (&n->peer));
-    GNUNET_snprintf (peer, sizeof (peer), "%8s", GNUNET_i2s (&t.target));
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _
-                ("Received PING from `%s' for different identity: I am `%s', PONG identity: `%s'\n"),
-                sender, GNUNET_i2s (&my_identity), peer);
-    GNUNET_break_op (0);
-    return;
-  }
-  update_neighbour_performance (n, ats, ats_count);
-  me = GNUNET_malloc (sizeof (struct MessageEntry) +
-                      sizeof (struct PongMessage));
-  GNUNET_CONTAINER_DLL_insert_after (n->encrypted_head, n->encrypted_tail,
-                                     n->encrypted_tail, me);
-  me->deadline = GNUNET_TIME_relative_to_absolute (MAX_PONG_DELAY);
-  me->priority = PONG_PRIORITY;
-  me->size = sizeof (struct PongMessage);
-  tx.inbound_bw_limit = n->bw_in;
-  tx.challenge = t.challenge;
-  tx.target = t.target;
-  tp = (struct PongMessage *) &me[1];
-  tp->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_PONG);
-  tp->header.size = htons (sizeof (struct PongMessage));
-  tp->iv_seed =
-      GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
-  derive_pong_iv (&iv, &n->encrypt_key, tp->iv_seed, t.challenge, &n->peer);
-  do_encrypt (n, &iv, &tx.challenge, &tp->challenge,
-              sizeof (struct PongMessage) - ((void *) &tp->challenge -
-                                             (void *) tp));
-  GNUNET_STATISTICS_update (stats, gettext_noop ("# PONG messages created"), 1,
-                            GNUNET_NO);
-#if DEBUG_HANDSHAKE
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Encrypting `%s' with challenge %u using key %u, IV %u (salt %u)\n",
-              "PONG", (unsigned int) t.challenge,
-              (unsigned int) n->encrypt_key.crc32, GNUNET_CRYPTO_crc32_n (&iv,
-                                                                          sizeof
-                                                                          (iv)),
-              tp->iv_seed);
-#endif
-  /* trigger queue processing */
-  process_encrypted_neighbour_queue (n);
+  GNUNET_CRYPTO_hmac_derive_key (akey, skey,
+                                 &seed, sizeof (seed),
+                                 skey, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
+                                 ctx, sizeof (ctx),
+                                 NULL);
 }
 
 
 /**
- * We received a PONG message.  Validate and update our status.
+ * Derive an IV from packet information
  *
- * @param n sender of the PONG
- * @param m the encrypted PONG message itself
- * @param ats performance data
- * @param ats_count number of entries in ats (excluding 0-termination)
+ * @param iv initialization vector to initialize
+ * @param skey session key to use
+ * @param seed seed to use
+ * @param identity identity of the other peer to use
  */
 static void
-handle_pong (struct Neighbour *n, const struct PongMessage *m,
-             const struct GNUNET_TRANSPORT_ATS_Information *ats,
-             uint32_t ats_count)
+derive_iv (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
+           const struct GNUNET_CRYPTO_SymmetricSessionKey *skey, uint32_t seed,
+           const struct GNUNET_PeerIdentity *identity)
 {
-  struct PongMessage t;
-  struct ConnectNotifyMessage *cnm;
-  struct GNUNET_CRYPTO_AesInitializationVector iv;
-  char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
-  struct GNUNET_TRANSPORT_ATS_Information *mats;
-  size_t size;
+  static const char ctx[] = "initialization vector";
 
-#if DEBUG_HANDSHAKE
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Core service receives `%s' response from `%4s'.\n", "PONG",
-              GNUNET_i2s (&n->peer));
-#endif
-  /* mark as garbage, just to be sure */
-  memset (&t, 255, sizeof (t));
-  derive_pong_iv (&iv, &n->decrypt_key, m->iv_seed, n->ping_challenge,
-                  &my_identity);
-  if (GNUNET_OK !=
-      do_decrypt (n, &iv, &m->challenge, &t.challenge,
-                  sizeof (struct PongMessage) - ((void *) &m->challenge -
-                                                 (void *) m)))
-  {
-    GNUNET_break_op (0);
-    return;
-  }
-  GNUNET_STATISTICS_update (stats, gettext_noop ("# PONG messages decrypted"),
-                            1, GNUNET_NO);
-#if DEBUG_HANDSHAKE
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Decrypted `%s' from `%4s' with challenge %u using key %u, IV %u (salt %u)\n",
-              "PONG", GNUNET_i2s (&t.target), (unsigned int) t.challenge,
-              (unsigned int) n->decrypt_key.crc32, GNUNET_CRYPTO_crc32_n (&iv,
-                                                                          sizeof
-                                                                          (iv)),
-              m->iv_seed);
-#endif
-  if ((0 != memcmp (&t.target, &n->peer, sizeof (struct GNUNET_PeerIdentity)))
-      || (n->ping_challenge != t.challenge))
-  {
-    /* PONG malformed */
-#if DEBUG_CORE
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Received malformed `%s' wanted sender `%4s' with challenge %u\n",
-                "PONG", GNUNET_i2s (&n->peer),
-                (unsigned int) n->ping_challenge);
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Received malformed `%s' received from `%4s' with challenge %u\n",
-                "PONG", GNUNET_i2s (&t.target), (unsigned int) t.challenge);
-#endif
-    GNUNET_break_op (n->ping_challenge != t.challenge);
-    return;
-  }
-  switch (n->status)
-  {
-  case PEER_STATE_DOWN:
-    GNUNET_break (0);           /* should be impossible */
-    return;
-  case PEER_STATE_KEY_SENT:
-    GNUNET_break (0);           /* should be impossible, how did we decrypt? */
-    return;
-  case PEER_STATE_KEY_RECEIVED:
-    GNUNET_STATISTICS_update (stats,
-                              gettext_noop
-                              ("# Session keys confirmed via PONG"), 1,
-                              GNUNET_NO);
-    n->status = PEER_STATE_KEY_CONFIRMED;
-    {
-      struct GNUNET_MessageHeader *hdr;
+  GNUNET_CRYPTO_symmetric_derive_iv (iv, skey, &seed, sizeof (seed),
+                                    identity,
+                                    sizeof (struct GNUNET_PeerIdentity), ctx,
+                                    sizeof (ctx), NULL);
+}
 
-      hdr = compute_type_map_message ();
-      send_type_map_to_neighbour (hdr, &n->peer.hashPubKey, n);
-      GNUNET_free (hdr);
-    }
-    if (n->bw_out_external_limit.value__ != t.inbound_bw_limit.value__)
-    {
-      n->bw_out_external_limit = t.inbound_bw_limit;
-      n->bw_out =
-          GNUNET_BANDWIDTH_value_min (n->bw_out_external_limit,
-                                      n->bw_out_internal_limit);
-      GNUNET_BANDWIDTH_tracker_update_quota (&n->available_send_window,
-                                             n->bw_out);
-      GNUNET_TRANSPORT_set_quota (transport, &n->peer, n->bw_in, n->bw_out);
-    }
-#if DEBUG_CORE
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Confirmed key via `%s' message for peer `%4s'\n", "PONG",
-                GNUNET_i2s (&n->peer));
-#endif
-    if (n->retry_set_key_task != GNUNET_SCHEDULER_NO_TASK)
-    {
-      GNUNET_SCHEDULER_cancel (n->retry_set_key_task);
-      n->retry_set_key_task = GNUNET_SCHEDULER_NO_TASK;
-    }
-    update_neighbour_performance (n, ats, ats_count);
-    size =
-        sizeof (struct ConnectNotifyMessage) +
-        (n->ats_count) * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
-    if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
-    {
-      GNUNET_break (0);
-      /* recovery strategy: throw away performance data */
-      GNUNET_array_grow (n->ats, n->ats_count, 0);
-      size =
-          sizeof (struct PeerStatusNotifyMessage) +
-          n->ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
-    }
-    cnm = (struct ConnectNotifyMessage *) buf;
-    cnm->header.size = htons (size);
-    cnm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT);
-    cnm->ats_count = htonl (n->ats_count);
-    cnm->peer = n->peer;
-    mats = &cnm->ats;
-    memcpy (mats, n->ats,
-            n->ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
-    mats[n->ats_count].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
-    mats[n->ats_count].value = htonl (0);
-    send_to_all_clients (&cnm->header, GNUNET_NO,
-                         GNUNET_CORE_OPTION_SEND_CONNECT);
-    process_encrypted_neighbour_queue (n);
-    /* fall-through! */
-  case PEER_STATE_KEY_CONFIRMED:
-    n->last_activity = GNUNET_TIME_absolute_get ();
-    if (n->keep_alive_task != GNUNET_SCHEDULER_NO_TASK)
-      GNUNET_SCHEDULER_cancel (n->keep_alive_task);
-    n->keep_alive_task =
-        GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_divide
-                                      (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
-                                       2), &send_keep_alive, n);
-    handle_peer_status_change (n);
-    break;
-  default:
-    GNUNET_break (0);
-    break;
-  }
+
+/**
+ * Derive an IV from pong packet information
+ *
+ * @param iv initialization vector to initialize
+ * @param skey session key to use
+ * @param seed seed to use
+ * @param challenge nonce to use
+ * @param identity identity of the other peer to use
+ */
+static void
+derive_pong_iv (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
+                const struct GNUNET_CRYPTO_SymmetricSessionKey *skey, uint32_t seed,
+                uint32_t challenge, const struct GNUNET_PeerIdentity *identity)
+{
+  static const char ctx[] = "pong initialization vector";
+
+  GNUNET_CRYPTO_symmetric_derive_iv (iv, skey, &seed, sizeof (seed),
+                                    identity,
+                                    sizeof (struct GNUNET_PeerIdentity),
+                                    &challenge, sizeof (challenge),
+                                    ctx, sizeof (ctx),
+                                    NULL);
 }
 
 
 /**
- * We received a SET_KEY message.  Validate and update
- * our key material and status.
+ * Derive an AES key from key material
  *
- * @param n the neighbour from which we received message m
- * @param m the set key message we received
- * @param ats performance data
- * @param ats_count number of entries in ats (excluding 0-termination)
+ * @param sender peer identity of the sender
+ * @param receiver peer identity of the sender
+ * @param key_material high entropy key material to use
+ * @param skey set to derived session key
  */
 static void
-handle_set_key (struct Neighbour *n, const struct SetKeyMessage *m,
-                const struct GNUNET_TRANSPORT_ATS_Information *ats,
-                uint32_t ats_count)
+derive_aes_key (const struct GNUNET_PeerIdentity *sender,
+               const struct GNUNET_PeerIdentity *receiver,
+               const struct GNUNET_HashCode *key_material,
+               struct GNUNET_CRYPTO_SymmetricSessionKey *skey)
 {
-  struct SetKeyMessage *m_cpy;
-  struct GNUNET_TIME_Absolute t;
-  struct GNUNET_CRYPTO_AesSessionKey k;
-  struct PingMessage *ping;
-  struct PongMessage *pong;
-  enum PeerStateMachine sender_status;
+  static const char ctx[] = "aes key generation vector";
+
+  GNUNET_CRYPTO_kdf (skey, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
+                    ctx, sizeof (ctx),
+                    key_material, sizeof (struct GNUNET_HashCode),
+                    sender, sizeof (struct GNUNET_PeerIdentity),
+                    receiver, sizeof (struct GNUNET_PeerIdentity),
+                    NULL);
+}
+
 
-#if DEBUG_CORE
+/**
+ * Encrypt size bytes from in and write the result to out.  Use the
+ * key for outbound traffic of the given neighbour.
+ *
+ * @param kx key information context
+ * @param iv initialization vector to use
+ * @param in ciphertext
+ * @param out plaintext
+ * @param size size of in/out
+ * @return GNUNET_OK on success
+ */
+static int
+do_encrypt (struct GSC_KeyExchangeInfo *kx,
+            const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
+            const void *in, void *out, size_t size)
+{
+  if (size != (uint16_t) size)
+  {
+    GNUNET_break (0);
+    return GNUNET_NO;
+  }
+  GNUNET_assert (size ==
+                 GNUNET_CRYPTO_symmetric_encrypt (in, (uint16_t) size,
+                                            &kx->encrypt_key, iv, out));
+  GNUNET_STATISTICS_update (GSC_stats, gettext_noop ("# bytes encrypted"), size,
+                            GNUNET_NO);
+  /* the following is too sensitive to write to log files by accident,
+     so we require manual intervention to get this one... */
+#if 0
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Core service receives `%s' request from `%4s'.\n", "SET_KEY",
-              GNUNET_i2s (&n->peer));
+              "Encrypted %u bytes for `%4s' using key %u, IV %u\n",
+              (unsigned int) size, GNUNET_i2s (&kx->peer),
+              (unsigned int) kx->encrypt_key.crc32, GNUNET_CRYPTO_crc32_n (iv,
+                                                                           sizeof
+                                                                           (iv)));
 #endif
-  if (n->public_key == NULL)
+  return GNUNET_OK;
+}
+
+
+/**
+ * Decrypt size bytes from in and write the result to out.  Use the
+ * key for inbound traffic of the given neighbour.  This function does
+ * NOT do any integrity-checks on the result.
+ *
+ * @param kx key information context
+ * @param iv initialization vector to use
+ * @param in ciphertext
+ * @param out plaintext
+ * @param size size of @a in / @a out
+ * @return #GNUNET_OK on success
+ */
+static int
+do_decrypt (struct GSC_KeyExchangeInfo *kx,
+            const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
+            const void *in, void *out, size_t size)
+{
+  if (size != (uint16_t) size)
   {
-    if (n->pitr != NULL)
-    {
-#if DEBUG_CORE
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "Ignoring `%s' message due to lack of public key for peer (still trying to obtain one).\n",
-                  "SET_KEY");
-#endif
-      return;
-    }
-#if DEBUG_CORE
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Lacking public key for peer, trying to obtain one (handle_set_key).\n");
+    GNUNET_break (0);
+    return GNUNET_NO;
+  }
+  if ( (kx->status != KX_STATE_KEY_RECEIVED) && (kx->status != KX_STATE_UP) &&
+       (kx->status != KX_STATE_REKEY_SENT) )
+  {
+    GNUNET_break_op (0);
+    return GNUNET_SYSERR;
+  }
+  if (size !=
+      GNUNET_CRYPTO_symmetric_decrypt (in, (uint16_t) size, &kx->decrypt_key, iv,
+                                 out))
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+  GNUNET_STATISTICS_update (GSC_stats, gettext_noop ("# bytes decrypted"), size,
+                            GNUNET_NO);
+  /* the following is too sensitive to write to log files by accident,
+     so we require manual intervention to get this one... */
+#if 0
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Decrypted %u bytes from `%4s' using key %u, IV %u\n",
+              (unsigned int) size, GNUNET_i2s (&kx->peer),
+              (unsigned int) kx->decrypt_key.crc32, GNUNET_CRYPTO_crc32_n (iv,
+                                                                           sizeof
+                                                                           (*iv)));
 #endif
-    m_cpy = GNUNET_malloc (sizeof (struct SetKeyMessage));
-    memcpy (m_cpy, m, sizeof (struct SetKeyMessage));
-    /* lookup n's public key, then try again */
-    GNUNET_assert (n->skm == NULL);
-    n->skm = m_cpy;
-    n->pitr =
-        GNUNET_PEERINFO_iterate (peerinfo, &n->peer, GNUNET_TIME_UNIT_MINUTES,
-                                 &process_hello_retry_handle_set_key, n);
-    GNUNET_STATISTICS_update (stats,
-                              gettext_noop
-                              ("# SET_KEY messages deferred (need public key)"),
-                              1, GNUNET_NO);
+  return GNUNET_OK;
+}
+
+
+/**
+ * Send our key (and encrypted PING) to the other peer.
+ *
+ * @param kx key exchange context
+ */
+static void
+send_key (struct GSC_KeyExchangeInfo *kx);
+
+
+/**
+ * Task that will retry "send_key" if our previous attempt failed.
+ *
+ * @param cls our 'struct GSC_KeyExchangeInfo'
+ * @param tc scheduler context
+ */
+static void
+set_key_retry_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  struct GSC_KeyExchangeInfo *kx = cls;
+
+  kx->retry_set_key_task = GNUNET_SCHEDULER_NO_TASK;
+  kx->set_key_retry_frequency = GNUNET_TIME_STD_BACKOFF (kx->set_key_retry_frequency);
+  GNUNET_assert (KX_STATE_DOWN != kx->status);
+  send_key (kx);
+}
+
+
+/**
+ * Create a fresh PING message for transmission to the other peer.
+ *
+ * @param kx key exchange context to create PING for
+ */
+static void
+setup_fresh_ping (struct GSC_KeyExchangeInfo *kx)
+{
+  struct PingMessage pp;
+  struct PingMessage *pm;
+  struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
+
+  pm = &kx->ping;
+  pm->header.size = htons (sizeof (struct PingMessage));
+  pm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_PING);
+  pm->iv_seed =
+      GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
+  derive_iv (&iv, &kx->encrypt_key, pm->iv_seed, &kx->peer);
+  pp.challenge = kx->ping_challenge;
+  pp.target = kx->peer;
+  do_encrypt (kx, &iv, &pp.target, &pm->target,
+              sizeof (struct PingMessage) - ((void *) &pm->target -
+                                             (void *) pm));
+}
+
+
+/**
+ * Start the key exchange with the given peer.
+ *
+ * @param pid identity of the peer to do a key exchange with
+ * @return key exchange information context
+ */
+struct GSC_KeyExchangeInfo *
+GSC_KX_start (const struct GNUNET_PeerIdentity *pid)
+{
+  struct GSC_KeyExchangeInfo *kx;
+  struct GNUNET_HashCode h1;
+  struct GNUNET_HashCode h2;
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Initiating key exchange with `%s'\n",
+              GNUNET_i2s (pid));
+  GNUNET_STATISTICS_update (GSC_stats,
+                            gettext_noop ("# key exchanges initiated"), 1,
+                            GNUNET_NO);
+  kx = GNUNET_new (struct GSC_KeyExchangeInfo);
+  kx->peer = *pid;
+  kx->set_key_retry_frequency = INITIAL_SET_KEY_RETRY_FREQUENCY;
+  GNUNET_CONTAINER_DLL_insert (kx_head,
+                              kx_tail,
+                              kx);
+  GNUNET_CRYPTO_hash (pid, sizeof (struct GNUNET_PeerIdentity), &h1);
+  GNUNET_CRYPTO_hash (&GSC_my_identity, sizeof (struct GNUNET_PeerIdentity), &h2);
+
+  kx->status = KX_STATE_KEY_SENT;
+  if (0 < GNUNET_CRYPTO_hash_cmp (&h1,
+                                 &h2))
+  {
+    /* peer with "lower" identity starts KX, otherwise we typically end up
+       with both peers starting the exchange and transmit the 'set key'
+       message twice */
+    send_key (kx);
+  }
+  else
+  {
+    /* peer with "higher" identity starts a delayed  KX, if the "lower" peer
+     * does not start a KX since he sees no reasons to do so  */
+    kx->retry_set_key_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
+          &set_key_retry_task, kx);
+  }
+  return kx;
+}
+
+
+/**
+ * Stop key exchange with the given peer.  Clean up key material.
+ *
+ * @param kx key exchange to stop
+ */
+void
+GSC_KX_stop (struct GSC_KeyExchangeInfo *kx)
+{
+  GSC_SESSIONS_end (&kx->peer);
+  GNUNET_STATISTICS_update (GSC_stats, gettext_noop ("# key exchanges stopped"),
+                            1, GNUNET_NO);
+  if (kx->retry_set_key_task != GNUNET_SCHEDULER_NO_TASK)
+  {
+    GNUNET_SCHEDULER_cancel (kx->retry_set_key_task);
+    kx->retry_set_key_task = GNUNET_SCHEDULER_NO_TASK;
+  }
+  if (kx->keep_alive_task != GNUNET_SCHEDULER_NO_TASK)
+  {
+    GNUNET_SCHEDULER_cancel (kx->keep_alive_task);
+    kx->keep_alive_task = GNUNET_SCHEDULER_NO_TASK;
+  }
+  GNUNET_CONTAINER_DLL_remove (kx_head,
+                              kx_tail,
+                              kx);
+  GNUNET_free (kx);
+}
+
+
+/**
+ * Send our PING to the other peer.
+ *
+ * @param kx key exchange context
+ */
+static void
+send_ping (struct GSC_KeyExchangeInfo *kx)
+{
+  GSC_NEIGHBOURS_transmit (&kx->peer, &kx->ping.header,
+                           MIN_PING_FREQUENCY);
+}
+
+/**
+ * We received a SET_KEY message.  Validate and update
+ * our key material and status.
+ *
+ * @param kx key exchange status for the corresponding peer
+ * @param msg the set key message we received
+ */
+void
+GSC_KX_handle_ephemeral_key (struct GSC_KeyExchangeInfo *kx,
+                            const struct GNUNET_MessageHeader *msg)
+{
+  const struct EphemeralKeyMessage *m;
+  struct GNUNET_TIME_Absolute start_t;
+  struct GNUNET_TIME_Absolute end_t;
+  struct GNUNET_TIME_Absolute now;
+  enum KxStateMachine sender_status;
+  uint16_t size;
+  struct GNUNET_HashCode key_material;
+
+  size = ntohs (msg->size);
+  if (sizeof (struct EphemeralKeyMessage) != size)
+  {
+    GNUNET_break_op (0);
+    return;
+  }
+  m = (const struct EphemeralKeyMessage *) msg;
+  end_t = GNUNET_TIME_absolute_ntoh (m->expiration_time);
+  if ( ( (KX_STATE_KEY_RECEIVED == kx->status) ||
+        (KX_STATE_UP == kx->status) ||
+        (KX_STATE_REKEY_SENT == kx->status) ) &&
+       (end_t.abs_value_us <= kx->foreign_key_expires.abs_value_us) )
+  {
+    GNUNET_STATISTICS_update (GSC_stats, gettext_noop ("# old ephemeral keys ignored"),
+                             1, GNUNET_NO);
     return;
   }
+  start_t = GNUNET_TIME_absolute_ntoh (m->creation_time);
+
+  GNUNET_STATISTICS_update (GSC_stats, gettext_noop ("# ephemeral keys received"),
+                            1, GNUNET_NO);
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Core service receives `%s' request from `%4s'.\n", "EPHEMERAL_KEY",
+              GNUNET_i2s (&kx->peer));
   if (0 !=
-      memcmp (&m->target, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
+      memcmp (&m->origin_identity,
+             &kx->peer.public_key,
+              sizeof (struct GNUNET_PeerIdentity)))
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                _
-                ("Received `%s' message that was for `%s', not for me.  Ignoring.\n"),
-                "SET_KEY", GNUNET_i2s (&m->target));
+    GNUNET_break_op (0);
     return;
   }
   if ((ntohl (m->purpose.size) !=
-       sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
+       sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
        sizeof (struct GNUNET_TIME_AbsoluteNBO) +
-       sizeof (struct GNUNET_CRYPTO_RsaEncryptedData) +
-       sizeof (struct GNUNET_PeerIdentity)) ||
+       sizeof (struct GNUNET_TIME_AbsoluteNBO) +
+       sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
+       sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)) ||
       (GNUNET_OK !=
-       GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_SET_KEY, &m->purpose,
-                                 &m->signature, n->public_key)))
+       GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_SET_ECC_KEY,
+                                &m->purpose,
+                                 &m->signature, &m->origin_identity.public_key)))
   {
     /* invalid signature */
     GNUNET_break_op (0);
     return;
   }
-  t = GNUNET_TIME_absolute_ntoh (m->creation_time);
-  if (((n->status == PEER_STATE_KEY_RECEIVED) ||
-       (n->status == PEER_STATE_KEY_CONFIRMED)) &&
-      (t.abs_value < n->decrypt_key_created.abs_value))
+  now = GNUNET_TIME_absolute_get ();
+  if ( (end_t.abs_value_us < GNUNET_TIME_absolute_subtract (now, REKEY_TOLERANCE).abs_value_us) ||
+       (start_t.abs_value_us > GNUNET_TIME_absolute_add (now, REKEY_TOLERANCE).abs_value_us) )
   {
-    /* this could rarely happen due to massive re-ordering of
-     * messages on the network level, but is most likely either
-     * a bug or some adversary messing with us.  Report. */
-    GNUNET_break_op (0);
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+               _("Ephemeral key message from peer `%s' rejected as its validity range does not match our system time (%llu not in [%llu,%llu]).\n"),
+               GNUNET_i2s (&kx->peer),
+               now.abs_value_us,
+               start_t.abs_value_us,
+               end_t.abs_value_us);
     return;
   }
-#if DEBUG_CORE
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Decrypting key material.\n");
-#endif
-  if ((GNUNET_CRYPTO_rsa_decrypt
-       (my_private_key, &m->encrypted_key, &k,
-        sizeof (struct GNUNET_CRYPTO_AesSessionKey)) !=
-       sizeof (struct GNUNET_CRYPTO_AesSessionKey)) ||
-      (GNUNET_OK != GNUNET_CRYPTO_aes_check_session_key (&k)))
+  if (GNUNET_OK !=
+      GNUNET_CRYPTO_ecc_ecdh (my_ephemeral_key,
+                             &m->ephemeral_key,
+                             &key_material))
   {
-    /* failed to decrypt !? */
-    GNUNET_break_op (0);
+    GNUNET_break (0);
     return;
   }
-  GNUNET_STATISTICS_update (stats,
-                            gettext_noop ("# SET_KEY messages decrypted"), 1,
+  GNUNET_STATISTICS_update (GSC_stats,
+                            gettext_noop ("# EPHEMERAL_KEY messages decrypted"), 1,
                             GNUNET_NO);
-  n->decrypt_key = k;
-  if (n->decrypt_key_created.abs_value != t.abs_value)
+  derive_aes_key (&GSC_my_identity,
+                 &kx->peer,
+                 &key_material,
+                 &kx->encrypt_key);
+  derive_aes_key (&kx->peer,
+                 &GSC_my_identity,
+                 &key_material,
+                 &kx->decrypt_key);
+  /* fresh key, reset sequence numbers */
+  kx->last_sequence_number_received = 0;
+  kx->last_packets_bitmap = 0;
+  kx->foreign_key_expires = end_t;
+  setup_fresh_ping (kx);
+
+  /* check if we still need to send the sender our key */
+  sender_status = (enum KxStateMachine) ntohl (m->sender_status);
+  switch (sender_status)
   {
-    /* fresh key, reset sequence numbers */
-    n->last_sequence_number_received = 0;
-    n->last_packets_bitmap = 0;
-    n->decrypt_key_created = t;
-  }
-  update_neighbour_performance (n, ats, ats_count);
-  sender_status = (enum PeerStateMachine) ntohl (m->sender_status);
-  switch (n->status)
-  {
-  case PEER_STATE_DOWN:
-    n->status = PEER_STATE_KEY_RECEIVED;
-#if DEBUG_CORE
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Responding to `%s' with my own key.\n", "SET_KEY");
-#endif
-    send_key (n);
+  case KX_STATE_DOWN:
+    GNUNET_break_op (0);
     break;
-  case PEER_STATE_KEY_SENT:
-  case PEER_STATE_KEY_RECEIVED:
-    n->status = PEER_STATE_KEY_RECEIVED;
-    if ((sender_status != PEER_STATE_KEY_RECEIVED) &&
-        (sender_status != PEER_STATE_KEY_CONFIRMED))
-    {
-#if DEBUG_CORE
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "Responding to `%s' with my own key (other peer has status %u).\n",
-                  "SET_KEY", (unsigned int) sender_status);
-#endif
-      send_key (n);
-    }
+  case KX_STATE_KEY_SENT:
+    /* fine, need to send our key after updating our status, see below */
     break;
-  case PEER_STATE_KEY_CONFIRMED:
-    if ((sender_status != PEER_STATE_KEY_RECEIVED) &&
-        (sender_status != PEER_STATE_KEY_CONFIRMED))
-    {
-#if DEBUG_CORE
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "Responding to `%s' with my own key (other peer has status %u), I was already fully up.\n",
-                  "SET_KEY", (unsigned int) sender_status);
-#endif
-      send_key (n);
-    }
+  case KX_STATE_KEY_RECEIVED:
+  case KX_STATE_UP:
+  case KX_STATE_REKEY_SENT:
+    /* other peer already got our key */
     break;
   default:
     GNUNET_break (0);
     break;
   }
-  if (n->pending_ping != NULL)
+  /* check if we need to confirm everything is fine via PING + PONG */
+  switch (kx->status)
   {
-    ping = n->pending_ping;
-    n->pending_ping = NULL;
-    handle_ping (n, ping, NULL, 0);
-    GNUNET_free (ping);
-  }
-  if (n->pending_pong != NULL)
-  {
-    pong = n->pending_pong;
-    n->pending_pong = NULL;
-    handle_pong (n, pong, NULL, 0);
-    GNUNET_free (pong);
+  case KX_STATE_DOWN:
+    GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == kx->keep_alive_task);
+    kx->status = KX_STATE_KEY_RECEIVED;
+    if (KX_STATE_KEY_SENT == sender_status)
+      send_key (kx);
+    send_ping (kx);
+    break;
+  case KX_STATE_KEY_SENT:
+    GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == kx->keep_alive_task);
+    kx->status = KX_STATE_KEY_RECEIVED;
+    if (KX_STATE_KEY_SENT == sender_status)
+      send_key (kx);
+    send_ping (kx);
+    break;
+  case KX_STATE_KEY_RECEIVED:
+    GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == kx->keep_alive_task);
+    if (KX_STATE_KEY_SENT == sender_status)
+      send_key (kx);
+    send_ping (kx);
+    break;
+  case KX_STATE_UP:
+    kx->status = KX_STATE_REKEY_SENT;
+    if (KX_STATE_KEY_SENT == sender_status)
+      send_key (kx);
+    /* we got a new key, need to reconfirm! */
+    send_ping (kx);
+    break;
+  case KX_STATE_REKEY_SENT:
+    if (KX_STATE_KEY_SENT == sender_status)
+      send_key (kx);
+    /* we got a new key, need to reconfirm! */
+    send_ping (kx);
+    break;
+  default:
+    GNUNET_break (0);
+    break;
   }
 }
 
 
-
 /**
- * PEERINFO is giving us a HELLO for a peer.  Add the public key to
- * the neighbour's struct and retry send_key.  Or, if we did not get a
- * HELLO, just do nothing.
+ * We received a PING message.  Validate and transmit
+ * a PONG message.
  *
- * @param cls the 'struct Neighbour' to retry sending the key for
- * @param peer the peer for which this is the HELLO
- * @param hello HELLO message of that peer
- * @param err_msg NULL if successful, otherwise contains error message
+ * @param kx key exchange status for the corresponding peer
+ * @param msg the encrypted PING message itself
  */
-static void
-process_hello_retry_send_key (void *cls, const struct GNUNET_PeerIdentity *peer,
-                              const struct GNUNET_HELLO_Message *hello,
-                              const char *err_msg)
+void
+GSC_KX_handle_ping (struct GSC_KeyExchangeInfo *kx,
+                    const struct GNUNET_MessageHeader *msg)
 {
-  struct Neighbour *n = cls;
+  const struct PingMessage *m;
+  struct PingMessage t;
+  struct PongMessage tx;
+  struct PongMessage tp;
+  struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
+  uint16_t msize;
 
-  if (err_msg != NULL)
+  msize = ntohs (msg->size);
+  if (msize != sizeof (struct PingMessage))
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                _("Error in communication with PEERINFO service\n"));
-    /* return; */
+    GNUNET_break_op (0);
+    return;
   }
-
-  if (peer == NULL)
+  GNUNET_STATISTICS_update (GSC_stats,
+                            gettext_noop ("# PING messages received"), 1,
+                            GNUNET_NO);
+  if ( (kx->status != KX_STATE_KEY_RECEIVED) &&
+       (kx->status != KX_STATE_UP) &&
+       (kx->status != KX_STATE_REKEY_SENT))
   {
-#if DEBUG_CORE
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Entered `%s' and `%s' is NULL!\n",
-                "process_hello_retry_send_key", "peer");
-#endif
-    n->pitr = NULL;
-    if (n->public_key != NULL)
-    {
-      if (n->retry_set_key_task != GNUNET_SCHEDULER_NO_TASK)
-      {
-        GNUNET_SCHEDULER_cancel (n->retry_set_key_task);
-        n->retry_set_key_task = GNUNET_SCHEDULER_NO_TASK;
-      }
-      GNUNET_STATISTICS_update (stats,
-                                gettext_noop
-                                ("# SET_KEY messages deferred (need public key)"),
-                                -1, GNUNET_NO);
-      send_key (n);
-    }
-    else
-    {
-#if DEBUG_CORE
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "Failed to obtain public key for peer `%4s', delaying processing of SET_KEY\n",
-                  GNUNET_i2s (&n->peer));
-#endif
-      GNUNET_STATISTICS_update (stats,
-                                gettext_noop
-                                ("# Delayed connecting due to lack of public key"),
-                                1, GNUNET_NO);
-      if (GNUNET_SCHEDULER_NO_TASK == n->retry_set_key_task)
-        n->retry_set_key_task =
-            GNUNET_SCHEDULER_add_delayed (n->set_key_retry_frequency,
-                                          &set_key_retry_task, n);
-    }
+    /* ignore */
+    GNUNET_STATISTICS_update (GSC_stats,
+                             gettext_noop ("# PING messages dropped (out of order)"), 1,
+                             GNUNET_NO);
     return;
   }
-
-#if DEBUG_CORE
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Entered `%s' for peer `%4s'\n",
-              "process_hello_retry_send_key", GNUNET_i2s (peer));
-#endif
-  if (n->public_key != NULL)
+  m = (const struct PingMessage *) msg;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Core service receives `%s' request from `%4s'.\n", "PING",
+              GNUNET_i2s (&kx->peer));
+  derive_iv (&iv, &kx->decrypt_key, m->iv_seed, &GSC_my_identity);
+  if (GNUNET_OK !=
+      do_decrypt (kx, &iv, &m->target, &t.target,
+                  sizeof (struct PingMessage) - ((void *) &m->target -
+                                                 (void *) m)))
   {
-    /* already have public key, why are we here? */
-    GNUNET_break (0);
+    GNUNET_break_op (0);
     return;
   }
-
-#if DEBUG_CORE
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Received new `%s' message for `%4s', initiating key exchange.\n",
-              "HELLO", GNUNET_i2s (peer));
-#endif
-  n->public_key =
-      GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
-  if (GNUNET_OK != GNUNET_HELLO_get_key (hello, n->public_key))
+  if (0 !=
+      memcmp (&t.target, &GSC_my_identity, sizeof (struct GNUNET_PeerIdentity)))
   {
-    GNUNET_STATISTICS_update (stats,
-                              gettext_noop
-                              ("# Error extracting public key from HELLO"), 1,
-                              GNUNET_NO);
-    GNUNET_free (n->public_key);
-    n->public_key = NULL;
-#if DEBUG_CORE
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "GNUNET_HELLO_get_key returned awfully\n");
-#endif
+    char sender[9];
+    char peer[9];
+
+    GNUNET_snprintf (sender, sizeof (sender), "%8s", GNUNET_i2s (&kx->peer));
+    GNUNET_snprintf (peer, sizeof (peer), "%8s", GNUNET_i2s (&t.target));
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                _
+                ("Received PING from `%s' for different identity: I am `%s', PONG identity: `%s'\n"),
+                sender, GNUNET_i2s (&GSC_my_identity), peer);
+    GNUNET_break_op (0);
     return;
   }
+  /* construct PONG */
+  tx.reserved = 0;
+  tx.challenge = t.challenge;
+  tx.target = t.target;
+  tp.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_PONG);
+  tp.header.size = htons (sizeof (struct PongMessage));
+  tp.iv_seed =
+      GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
+  derive_pong_iv (&iv, &kx->encrypt_key, tp.iv_seed, t.challenge, &kx->peer);
+  do_encrypt (kx, &iv, &tx.challenge, &tp.challenge,
+              sizeof (struct PongMessage) - ((void *) &tp.challenge -
+                                             (void *) &tp));
+  GNUNET_STATISTICS_update (GSC_stats, gettext_noop ("# PONG messages created"),
+                            1, GNUNET_NO);
+  GSC_NEIGHBOURS_transmit (&kx->peer, &tp.header,
+                           GNUNET_TIME_UNIT_FOREVER_REL /* FIXME: timeout */ );
 }
 
 
 /**
- * Send our key (and encrypted PING) to the other peer.
+ * Task triggered when a neighbour entry is about to time out
+ * (and we should prevent this by sending a PING).
  *
- * @param n the other peer
+ * @param cls the 'struct GSC_KeyExchangeInfo'
+ * @param tc scheduler context (not used)
  */
 static void
-send_key (struct Neighbour *n)
+send_keep_alive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  struct MessageEntry *pos;
-  struct SetKeyMessage *sm;
-  struct MessageEntry *me;
-  struct PingMessage pp;
-  struct PingMessage *pm;
-  struct GNUNET_CRYPTO_AesInitializationVector iv;
+  struct GSC_KeyExchangeInfo *kx = cls;
+  struct GNUNET_TIME_Relative retry;
+  struct GNUNET_TIME_Relative left;
 
-  if (n->retry_set_key_task != GNUNET_SCHEDULER_NO_TASK)
+  kx->keep_alive_task = GNUNET_SCHEDULER_NO_TASK;
+  left = GNUNET_TIME_absolute_get_remaining (kx->timeout);
+  if (0 == left.rel_value_us)
   {
-    GNUNET_SCHEDULER_cancel (n->retry_set_key_task);
-    n->retry_set_key_task = GNUNET_SCHEDULER_NO_TASK;
+    GNUNET_STATISTICS_update (GSC_stats,
+                              gettext_noop ("# sessions terminated by timeout"),
+                              1, GNUNET_NO);
+    GSC_SESSIONS_end (&kx->peer);
+    kx->status = KX_STATE_KEY_SENT;
+    send_key (kx);
+    return;
   }
-  if (n->pitr != NULL)
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending KEEPALIVE to `%s'\n",
+              GNUNET_i2s (&kx->peer));
+  GNUNET_STATISTICS_update (GSC_stats,
+                            gettext_noop ("# keepalive messages sent"), 1,
+                            GNUNET_NO);
+  setup_fresh_ping (kx);
+  GSC_NEIGHBOURS_transmit (&kx->peer, &kx->ping.header,
+                           kx->set_key_retry_frequency);
+  retry =
+      GNUNET_TIME_relative_max (GNUNET_TIME_relative_divide (left, 2),
+                                MIN_PING_FREQUENCY);
+  kx->keep_alive_task =
+      GNUNET_SCHEDULER_add_delayed (retry, &send_keep_alive, kx);
+}
+
+
+/**
+ * We've seen a valid message from the other peer.
+ * Update the time when the session would time out
+ * and delay sending our keep alive message further.
+ *
+ * @param kx key exchange where we saw activity
+ */
+static void
+update_timeout (struct GSC_KeyExchangeInfo *kx)
+{
+  kx->timeout =
+      GNUNET_TIME_relative_to_absolute
+      (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
+  if (kx->keep_alive_task != GNUNET_SCHEDULER_NO_TASK)
+    GNUNET_SCHEDULER_cancel (kx->keep_alive_task);
+  kx->keep_alive_task =
+      GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_divide
+                                    (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
+                                     2), &send_keep_alive, kx);
+}
+
+
+/**
+ * We received a PONG message.  Validate and update our status.
+ *
+ * @param kx key exchange context for the the PONG
+ * @param msg the encrypted PONG message itself
+ */
+void
+GSC_KX_handle_pong (struct GSC_KeyExchangeInfo *kx,
+                    const struct GNUNET_MessageHeader *msg)
+{
+  const struct PongMessage *m;
+  struct PongMessage t;
+  struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
+  uint16_t msize;
+
+  msize = ntohs (msg->size);
+  if (sizeof (struct PongMessage) != msize)
   {
-#if DEBUG_CORE
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Key exchange in progress with `%4s'.\n",
-                GNUNET_i2s (&n->peer));
-#endif
-    return;                     /* already in progress */
+    GNUNET_break_op (0);
+    return;
   }
-  if (GNUNET_YES != n->is_connected)
+  GNUNET_STATISTICS_update (GSC_stats,
+                            gettext_noop ("# PONG messages received"), 1,
+                            GNUNET_NO);
+  switch (kx->status)
   {
-    GNUNET_STATISTICS_update (stats,
-                              gettext_noop
-                              ("# Asking transport to connect (for SET_KEY)"),
-                              1, GNUNET_NO);
-    GNUNET_TRANSPORT_try_connect (transport, &n->peer);
+  case KX_STATE_DOWN:
+    GNUNET_STATISTICS_update (GSC_stats,
+                             gettext_noop ("# PONG messages dropped (connection down)"), 1,
+                             GNUNET_NO);
+    return;
+  case KX_STATE_KEY_SENT:
+    GNUNET_STATISTICS_update (GSC_stats,
+                             gettext_noop ("# PONG messages dropped (out of order)"), 1,
+                             GNUNET_NO);
+    return;
+  case KX_STATE_KEY_RECEIVED:
+    break;
+  case KX_STATE_UP:
+    break;
+  case KX_STATE_REKEY_SENT:
+    break;
+  default:
+    GNUNET_break (0);
     return;
   }
-#if DEBUG_CORE
+  m = (const struct PongMessage *) msg;
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Asked to perform key exchange with `%4s'.\n",
-              GNUNET_i2s (&n->peer));
-#endif
-  if (n->public_key == NULL)
+              "Core service receives `%s' response from `%4s'.\n", "PONG",
+              GNUNET_i2s (&kx->peer));
+  /* mark as garbage, just to be sure */
+  memset (&t, 255, sizeof (t));
+  derive_pong_iv (&iv, &kx->decrypt_key, m->iv_seed, kx->ping_challenge,
+                  &GSC_my_identity);
+  if (GNUNET_OK !=
+      do_decrypt (kx, &iv, &m->challenge, &t.challenge,
+                  sizeof (struct PongMessage) - ((void *) &m->challenge -
+                                                 (void *) m)))
+  {
+    GNUNET_break_op (0);
+    return;
+  }
+  GNUNET_STATISTICS_update (GSC_stats,
+                            gettext_noop ("# PONG messages decrypted"), 1,
+                            GNUNET_NO);
+  if ((0 != memcmp (&t.target, &kx->peer, sizeof (struct GNUNET_PeerIdentity)))
+      || (kx->ping_challenge != t.challenge))
   {
-    /* lookup n's public key, then try again */
-#if DEBUG_CORE
+    /* PONG malformed */
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Lacking public key for `%4s', trying to obtain one (send_key).\n",
-                GNUNET_i2s (&n->peer));
-#endif
-    GNUNET_assert (n->pitr == NULL);
-    n->pitr =
-        GNUNET_PEERINFO_iterate (peerinfo, &n->peer,
-                                 GNUNET_TIME_relative_multiply
-                                 (GNUNET_TIME_UNIT_SECONDS, 20),
-                                 &process_hello_retry_send_key, n);
+                "Received malformed `%s' wanted sender `%4s' with challenge %u\n",
+                "PONG", GNUNET_i2s (&kx->peer),
+                (unsigned int) kx->ping_challenge);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Received malformed `%s' received from `%4s' with challenge %u\n",
+                "PONG", GNUNET_i2s (&t.target), (unsigned int) t.challenge);
     return;
   }
-  pos = n->encrypted_head;
-  while (pos != NULL)
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received PONG from `%s'\n",
+              GNUNET_i2s (&kx->peer));
+  /* no need to resend key any longer */
+  if (GNUNET_SCHEDULER_NO_TASK != kx->retry_set_key_task)
   {
-    if (GNUNET_YES == pos->is_setkey)
-    {
-      if (pos->sender_status == n->status)
-      {
-#if DEBUG_CORE
-        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                    "`%s' message for `%4s' queued already\n", "SET_KEY",
-                    GNUNET_i2s (&n->peer));
-#endif
-        goto trigger_processing;
-      }
-      GNUNET_CONTAINER_DLL_remove (n->encrypted_head, n->encrypted_tail, pos);
-      GNUNET_free (pos);
-#if DEBUG_CORE
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "Removing queued `%s' message for `%4s', will create a new one\n",
-                  "SET_KEY", GNUNET_i2s (&n->peer));
-#endif
-      break;
-    }
-    pos = pos->next;
+    GNUNET_SCHEDULER_cancel (kx->retry_set_key_task);
+    kx->retry_set_key_task = GNUNET_SCHEDULER_NO_TASK;
   }
-
-  /* update status */
-  switch (n->status)
+  switch (kx->status)
   {
-  case PEER_STATE_DOWN:
-    n->status = PEER_STATE_KEY_SENT;
-    break;
-  case PEER_STATE_KEY_SENT:
+  case KX_STATE_DOWN:
+    GNUNET_assert (0);           /* should be impossible */
+    return;
+  case KX_STATE_KEY_SENT:
+    GNUNET_assert (0);           /* should be impossible */
+    return;
+  case KX_STATE_KEY_RECEIVED:
+    GNUNET_STATISTICS_update (GSC_stats,
+                              gettext_noop
+                              ("# session keys confirmed via PONG"), 1,
+                              GNUNET_NO);
+    kx->status = KX_STATE_UP;
+    GSC_SESSIONS_create (&kx->peer, kx);
+    GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == kx->keep_alive_task);
+    update_timeout (kx);
     break;
-  case PEER_STATE_KEY_RECEIVED:
+  case KX_STATE_UP:
+    GNUNET_STATISTICS_update (GSC_stats,
+                              gettext_noop
+                              ("# timeouts prevented via PONG"), 1,
+                              GNUNET_NO);
+    update_timeout (kx);
     break;
-  case PEER_STATE_KEY_CONFIRMED:
+  case KX_STATE_REKEY_SENT:
+    GNUNET_STATISTICS_update (GSC_stats,
+                              gettext_noop
+                              ("# rekey operations confirmed via PONG"), 1,
+                              GNUNET_NO);
+    kx->status = KX_STATE_UP;
+    update_timeout (kx);
     break;
   default:
     GNUNET_break (0);
     break;
   }
+}
 
 
-  /* first, set key message */
-  me = GNUNET_malloc (sizeof (struct MessageEntry) +
-                      sizeof (struct SetKeyMessage) +
-                      sizeof (struct PingMessage));
-  me->deadline = GNUNET_TIME_relative_to_absolute (MAX_SET_KEY_DELAY);
-  me->priority = SET_KEY_PRIORITY;
-  me->size = sizeof (struct SetKeyMessage) + sizeof (struct PingMessage);
-  me->is_setkey = GNUNET_YES;
-  me->got_slack = GNUNET_YES;   /* do not defer this one! */
-  me->sender_status = n->status;
-  GNUNET_CONTAINER_DLL_insert_after (n->encrypted_head, n->encrypted_tail,
-                                     n->encrypted_tail, me);
-  sm = (struct SetKeyMessage *) &me[1];
-  sm->header.size = htons (sizeof (struct SetKeyMessage));
-  sm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SET_KEY);
-  sm->sender_status =
-      htonl ((int32_t)
-             ((n->status ==
-               PEER_STATE_DOWN) ? PEER_STATE_KEY_SENT : n->status));
-  sm->purpose.size =
-      htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
-             sizeof (struct GNUNET_TIME_AbsoluteNBO) +
-             sizeof (struct GNUNET_CRYPTO_RsaEncryptedData) +
-             sizeof (struct GNUNET_PeerIdentity));
-  sm->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_SET_KEY);
-  sm->creation_time = GNUNET_TIME_absolute_hton (n->encrypt_key_created);
-  sm->target = n->peer;
-  GNUNET_assert (GNUNET_OK ==
-                 GNUNET_CRYPTO_rsa_encrypt (&n->encrypt_key,
-                                            sizeof (struct
-                                                    GNUNET_CRYPTO_AesSessionKey),
-                                            n->public_key, &sm->encrypted_key));
-  GNUNET_assert (GNUNET_OK ==
-                 GNUNET_CRYPTO_rsa_sign (my_private_key, &sm->purpose,
-                                         &sm->signature));
-  pm = (struct PingMessage *) &sm[1];
-  pm->header.size = htons (sizeof (struct PingMessage));
-  pm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_PING);
-  pm->iv_seed =
-      GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
-  derive_iv (&iv, &n->encrypt_key, pm->iv_seed, &n->peer);
-  pp.challenge = n->ping_challenge;
-  pp.target = n->peer;
-#if DEBUG_HANDSHAKE
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Encrypting `%s' and `%s' messages with challenge %u for `%4s' using key %u, IV %u (salt %u).\n",
-              "SET_KEY", "PING", (unsigned int) n->ping_challenge,
-              GNUNET_i2s (&n->peer), (unsigned int) n->encrypt_key.crc32,
-              GNUNET_CRYPTO_crc32_n (&iv, sizeof (iv)), pm->iv_seed);
-#endif
-  do_encrypt (n, &iv, &pp.target, &pm->target,
-              sizeof (struct PingMessage) - ((void *) &pm->target -
-                                             (void *) pm));
-  GNUNET_STATISTICS_update (stats,
-                            gettext_noop
-                            ("# SET_KEY and PING messages created"), 1,
-                            GNUNET_NO);
-#if DEBUG_CORE
+/**
+ * Send our key to the other peer.
+ *
+ * @param kx key exchange context
+ */
+static void
+send_key (struct GSC_KeyExchangeInfo *kx)
+{
+  GNUNET_assert (KX_STATE_DOWN != kx->status);
+  if (GNUNET_SCHEDULER_NO_TASK != kx->retry_set_key_task)
+  {
+     GNUNET_SCHEDULER_cancel (kx->retry_set_key_task);
+     kx->retry_set_key_task = GNUNET_SCHEDULER_NO_TASK;
+  }
+  /* always update sender status in SET KEY message */
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Have %llu ms left for `%s' transmission.\n",
-              (unsigned long long)
-              GNUNET_TIME_absolute_get_remaining (me->deadline).rel_value,
-              "SET_KEY");
-#endif
-trigger_processing:
-  /* trigger queue processing */
-  process_encrypted_neighbour_queue (n);
-  if ((n->status != PEER_STATE_KEY_CONFIRMED) &&
-      (GNUNET_SCHEDULER_NO_TASK == n->retry_set_key_task))
-    n->retry_set_key_task =
-        GNUNET_SCHEDULER_add_delayed (n->set_key_retry_frequency,
-                                      &set_key_retry_task, n);
+             "Sending key to `%s' (my status: %d)\n",
+              GNUNET_i2s (&kx->peer),
+             kx->status);
+  current_ekm.sender_status = htonl ((int32_t) (kx->status));
+  GSC_NEIGHBOURS_transmit (&kx->peer, &current_ekm.header,
+                           kx->set_key_retry_frequency);
+  kx->retry_set_key_task =
+      GNUNET_SCHEDULER_add_delayed (kx->set_key_retry_frequency,
+                                    &set_key_retry_task, kx);
 }
 
 
 /**
- * We received a SET_KEY message.  Validate and update
- * our key material and status.
+ * Encrypt and transmit a message with the given payload.
  *
- * @param n the neighbour from which we received message m
- * @param m the set key message we received
- * @param ats performance data
- * @param ats_count number of entries in ats (excluding 0-termination)
+ * @param kx key exchange context
+ * @param payload payload of the message
+ * @param payload_size number of bytes in 'payload'
  */
-static void
-handle_set_key (struct Neighbour *n, const struct SetKeyMessage *m,
-                const struct GNUNET_TRANSPORT_ATS_Information *ats,
-                uint32_t ats_count);
+void
+GSC_KX_encrypt_and_transmit (struct GSC_KeyExchangeInfo *kx,
+                             const void *payload, size_t payload_size)
+{
+  size_t used = payload_size + sizeof (struct EncryptedMessage);
+  char pbuf[used];              /* plaintext */
+  char cbuf[used];              /* ciphertext */
+  struct EncryptedMessage *em;  /* encrypted message */
+  struct EncryptedMessage *ph;  /* plaintext header */
+  struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
+  struct GNUNET_CRYPTO_AuthKey auth_key;
+
+  ph = (struct EncryptedMessage *) pbuf;
+  ph->iv_seed =
+      htonl (GNUNET_CRYPTO_random_u32
+             (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX));
+  ph->sequence_number = htonl (++kx->last_sequence_number_sent);
+  ph->reserved = 0;
+  ph->timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
+  memcpy (&ph[1], payload, payload_size);
+
+  em = (struct EncryptedMessage *) cbuf;
+  em->header.size = htons (used);
+  em->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE);
+  em->iv_seed = ph->iv_seed;
+  derive_iv (&iv, &kx->encrypt_key, ph->iv_seed, &kx->peer);
+  GNUNET_assert (GNUNET_OK ==
+                 do_encrypt (kx, &iv, &ph->sequence_number,
+                             &em->sequence_number,
+                             used - ENCRYPTED_HEADER_SIZE));
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Encrypted %u bytes for %s\n",
+              used - ENCRYPTED_HEADER_SIZE, GNUNET_i2s (&kx->peer));
+  derive_auth_key (&auth_key,
+                  &kx->encrypt_key,
+                  ph->iv_seed);
+  GNUNET_CRYPTO_hmac (&auth_key, &em->sequence_number,
+                      used - ENCRYPTED_HEADER_SIZE, &em->hmac);
+  GSC_NEIGHBOURS_transmit (&kx->peer, &em->header,
+                           GNUNET_TIME_UNIT_FOREVER_REL);
+}
+
 
+/**
+ * Closure for 'deliver_message'
+ */
+struct DeliverMessageContext
+{
+
+  /**
+   * Key exchange context.
+   */
+  struct GSC_KeyExchangeInfo *kx;
+
+  /**
+   * Sender of the message.
+   */
+  const struct GNUNET_PeerIdentity *peer;
+};
 
 
 /**
- * PEERINFO is giving us a HELLO for a peer.  Add the public key to
- * the neighbour's struct and retry handling the set_key message.  Or,
- * if we did not get a HELLO, just free the set key message.
+ * We received an encrypted message.  Decrypt, validate and
+ * pass on to the appropriate clients.
  *
- * @param cls pointer to the set key message
- * @param peer the peer for which this is the HELLO
- * @param hello HELLO message of that peer
- * @param err_msg NULL if successful, otherwise contains error message
+ * @param kx key exchange context for encrypting the message
+ * @param msg encrypted message
  */
-static void
-process_hello_retry_handle_set_key (void *cls,
-                                    const struct GNUNET_PeerIdentity *peer,
-                                    const struct GNUNET_HELLO_Message *hello,
-                                    const char *err_msg)
+void
+GSC_KX_handle_encrypted_message (struct GSC_KeyExchangeInfo *kx,
+                                 const struct GNUNET_MessageHeader *msg)
 {
-  struct Neighbour *n = cls;
-  struct SetKeyMessage *sm = n->skm;
+  const struct EncryptedMessage *m;
+  struct EncryptedMessage *pt;  /* plaintext */
+  struct GNUNET_HashCode ph;
+  uint32_t snum;
+  struct GNUNET_TIME_Absolute t;
+  struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
+  struct GNUNET_CRYPTO_AuthKey auth_key;
+  struct DeliverMessageContext dmc;
+  uint16_t size = ntohs (msg->size);
+  char buf[size] GNUNET_ALIGN;
+
+  if (size <
+      sizeof (struct EncryptedMessage) + sizeof (struct GNUNET_MessageHeader))
+  {
+    GNUNET_break_op (0);
+    return;
+  }
+  m = (const struct EncryptedMessage *) msg;
+  if (kx->status != KX_STATE_UP)
+  {
+    GNUNET_STATISTICS_update (GSC_stats,
+                              gettext_noop
+                              ("# DATA message dropped (out of order)"),
+                              1, GNUNET_NO);
+    return;
+  }
+  if (0 == GNUNET_TIME_absolute_get_remaining (kx->foreign_key_expires).rel_value_us)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+               _("Session to peer `%s' went down due to key expiration (should not happen)\n"),
+               GNUNET_i2s (&kx->peer));
+    GNUNET_STATISTICS_update (GSC_stats,
+                              gettext_noop ("# sessions terminated by key expiration"),
+                              1, GNUNET_NO);
+    GSC_SESSIONS_end (&kx->peer);
+    if (GNUNET_SCHEDULER_NO_TASK != kx->keep_alive_task)
+    {
+      GNUNET_SCHEDULER_cancel (kx->keep_alive_task);
+      kx->keep_alive_task = GNUNET_SCHEDULER_NO_TASK;
+    }
+    kx->status = KX_STATE_KEY_SENT;
+    send_key (kx);
+    return;
+  }
 
-  if (err_msg != NULL)
+  /* validate hash */
+  derive_auth_key (&auth_key, &kx->decrypt_key, m->iv_seed);
+  GNUNET_CRYPTO_hmac (&auth_key, &m->sequence_number,
+                      size - ENCRYPTED_HEADER_SIZE, &ph);
+  if (0 != memcmp (&ph, &m->hmac, sizeof (struct GNUNET_HashCode)))
   {
+    /* checksum failed */
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                _("Error in communication with PEERINFO service\n"));
-    /* return; */
+               "Failed checksum validation for a message from `%s'\n",
+               GNUNET_i2s (&kx->peer));
+    return;
   }
+  derive_iv (&iv, &kx->decrypt_key, m->iv_seed, &GSC_my_identity);
+  /* decrypt */
+  if (GNUNET_OK !=
+      do_decrypt (kx, &iv, &m->sequence_number, &buf[ENCRYPTED_HEADER_SIZE],
+                  size - ENCRYPTED_HEADER_SIZE))
+    return;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Decrypted %u bytes from %s\n",
+              size - ENCRYPTED_HEADER_SIZE, GNUNET_i2s (&kx->peer));
+  pt = (struct EncryptedMessage *) buf;
 
-  if (peer == NULL)
+  /* validate sequence number */
+  snum = ntohl (pt->sequence_number);
+  if (kx->last_sequence_number_received == snum)
   {
-    n->skm = NULL;
-    n->pitr = NULL;
-    if (n->public_key != NULL)
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Received duplicate message, ignoring.\n");
+    /* duplicate, ignore */
+    GNUNET_STATISTICS_update (GSC_stats,
+                              gettext_noop ("# bytes dropped (duplicates)"),
+                              size, GNUNET_NO);
+    return;
+  }
+  if ((kx->last_sequence_number_received > snum) &&
+      (kx->last_sequence_number_received - snum > 32))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Received ancient out of sequence message, ignoring.\n");
+    /* ancient out of sequence, ignore */
+    GNUNET_STATISTICS_update (GSC_stats,
+                              gettext_noop
+                              ("# bytes dropped (out of sequence)"), size,
+                              GNUNET_NO);
+    return;
+  }
+  if (kx->last_sequence_number_received > snum)
+  {
+    unsigned int rotbit = 1 << (kx->last_sequence_number_received - snum - 1);
+
+    if ((kx->last_packets_bitmap & rotbit) != 0)
     {
-#if DEBUG_CORE
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "Received `%s' for `%4s', continuing processing of `%s' message.\n",
-                  "HELLO", GNUNET_i2s (&n->peer), "SET_KEY");
-#endif
-      handle_set_key (n, sm, NULL, 0);
+                  "Received duplicate message, ignoring.\n");
+      GNUNET_STATISTICS_update (GSC_stats,
+                                gettext_noop ("# bytes dropped (duplicates)"),
+                                size, GNUNET_NO);
+      /* duplicate, ignore */
+      return;
     }
+    kx->last_packets_bitmap |= rotbit;
+  }
+  if (kx->last_sequence_number_received < snum)
+  {
+    unsigned int shift = (snum - kx->last_sequence_number_received);
+
+    if (shift >= 8 * sizeof (kx->last_packets_bitmap))
+      kx->last_packets_bitmap = 0;
     else
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  _
-                  ("Ignoring `%s' message due to lack of public key for peer `%4s' (failed to obtain one).\n"),
-                  "SET_KEY", GNUNET_i2s (&n->peer));
-    }
-    GNUNET_free (sm);
-    return;
+      kx->last_packets_bitmap <<= shift;
+    kx->last_sequence_number_received = snum;
   }
-  if (n->public_key != NULL)
-    return;                     /* multiple HELLOs match!? */
-  n->public_key =
-      GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
-  if (GNUNET_OK != GNUNET_HELLO_get_key (hello, n->public_key))
+
+  /* check timestamp */
+  t = GNUNET_TIME_absolute_ntoh (pt->timestamp);
+  if (GNUNET_TIME_absolute_get_duration (t).rel_value_us >
+      MAX_MESSAGE_AGE.rel_value_us)
   {
-    GNUNET_break_op (0);
-    GNUNET_free (n->public_key);
-    n->public_key = NULL;
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Message received far too old (%s). Content ignored.\n",
+                GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (t), GNUNET_YES));
+    GNUNET_STATISTICS_update (GSC_stats,
+                              gettext_noop
+                              ("# bytes dropped (ancient message)"), size,
+                              GNUNET_NO);
+    return;
   }
-}
 
+  /* process decrypted message(s) */
+  update_timeout (kx);
+  GNUNET_STATISTICS_update (GSC_stats,
+                            gettext_noop ("# bytes of payload decrypted"),
+                            size - sizeof (struct EncryptedMessage), GNUNET_NO);
+  dmc.kx = kx;
+  dmc.peer = &kx->peer;
+  if (GNUNET_OK !=
+      GNUNET_SERVER_mst_receive (mst, &dmc,
+                                 &buf[sizeof (struct EncryptedMessage)],
+                                 size - sizeof (struct EncryptedMessage),
+                                 GNUNET_YES, GNUNET_NO))
+    GNUNET_break_op (0);
+}
 
 
 /**
- * Task that will retry "send_key" if our previous attempt failed
- * to yield a PONG.
+ * Deliver P2P message to interested clients.
+ * Invokes send twice, once for clients that want the full message, and once
+ * for clients that only want the header
+ *
+ * @param cls always NULL
+ * @param client who sent us the message (struct GSC_KeyExchangeInfo)
+ * @param m the message
  */
-static void
-set_key_retry_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+static int
+deliver_message (void *cls, void *client, const struct GNUNET_MessageHeader *m)
 {
-  struct Neighbour *n = cls;
+  struct DeliverMessageContext *dmc = client;
 
-#if DEBUG_CORE
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Retrying key transmission to `%4s'\n",
-              GNUNET_i2s (&n->peer));
-#endif
-  n->retry_set_key_task = GNUNET_SCHEDULER_NO_TASK;
-  n->set_key_retry_frequency =
-      GNUNET_TIME_relative_multiply (n->set_key_retry_frequency, 2);
-  send_key (n);
+  if (KX_STATE_UP != dmc->kx->status)
+  {
+    GNUNET_STATISTICS_update (GSC_stats,
+                              gettext_noop
+                              ("# PAYLOAD dropped (out of order)"),
+                              1, GNUNET_NO);
+    return GNUNET_OK;
+  }
+  switch (ntohs (m->type))
+  {
+  case GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP:
+  case GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP:
+    GSC_SESSIONS_set_typemap (dmc->peer, m);
+    return GNUNET_OK;
+  default:
+    GSC_CLIENTS_deliver_message (dmc->peer, m,
+                                 ntohs (m->size),
+                                 GNUNET_CORE_OPTION_SEND_FULL_INBOUND);
+    GSC_CLIENTS_deliver_message (dmc->peer, m,
+                                 sizeof (struct GNUNET_MessageHeader),
+                                 GNUNET_CORE_OPTION_SEND_HDR_INBOUND);
+  }
+  return GNUNET_OK;
 }
 
 
-struct GSC_KeyExchangeInfo *
-GSC_KX_start (const struct GNUNET_PeerIdentity *pid)
+/**
+ * Setup the message that links the ephemeral key to our persistent
+ * public key and generate the appropriate signature.
+ */
+static void
+sign_ephemeral_key ()
 {
-  struct GSC_KeyExchangeInfo *kx;
-
-  kx = NULL;
-  return kx;
+  current_ekm.header.size = htons (sizeof (struct EphemeralKeyMessage));
+  current_ekm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_EPHEMERAL_KEY);
+  current_ekm.sender_status = 0; /* to be set later */
+  current_ekm.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_SET_ECC_KEY);
+  current_ekm.purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
+                                   sizeof (struct GNUNET_TIME_AbsoluteNBO) +
+                                   sizeof (struct GNUNET_TIME_AbsoluteNBO) +
+                                   sizeof (struct GNUNET_CRYPTO_EcdhePublicKey) +
+                                   sizeof (struct GNUNET_PeerIdentity));
+  current_ekm.creation_time = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
+  if (GNUNET_YES ==
+      GNUNET_CONFIGURATION_get_value_yesno (GSC_cfg,
+                                           "core",
+                                           "USE_EPHEMERAL_KEYS"))
+  {
+    current_ekm.expiration_time = GNUNET_TIME_absolute_hton (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_add (REKEY_FREQUENCY,
+                                                                                                                        REKEY_TOLERANCE)));
+  }
+  else
+  {
+    current_ekm.expiration_time = GNUNET_TIME_absolute_hton (GNUNET_TIME_UNIT_FOREVER_ABS);
+  }
+  GNUNET_CRYPTO_ecdhe_key_get_public (my_ephemeral_key,
+                                      &current_ekm.ephemeral_key);
+  current_ekm.origin_identity = GSC_my_identity;
+  GNUNET_assert (GNUNET_OK ==
+                GNUNET_CRYPTO_eddsa_sign (my_private_key,
+                                        &current_ekm.purpose,
+                                        &current_ekm.signature));
 }
 
 
-void
-GSC_KX_stop (struct GSC_KeyExchangeInfo *kx)
+/**
+ * Task run to trigger rekeying.
+ *
+ * @param cls closure, NULL
+ * @param tc scheduler context
+ */
+static void
+do_rekey (void *cls,
+         const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  if (kx->pitr != NULL)
+  struct GSC_KeyExchangeInfo *pos;
+
+  rekey_task = GNUNET_SCHEDULER_add_delayed (REKEY_FREQUENCY,
+                                            &do_rekey,
+                                            NULL);
+  if (NULL != my_ephemeral_key)
+    GNUNET_free (my_ephemeral_key);
+  my_ephemeral_key = GNUNET_CRYPTO_ecdhe_key_create ();
+  GNUNET_assert (NULL != my_ephemeral_key);
+  sign_ephemeral_key ();
+  for (pos = kx_head; NULL != pos; pos = pos->next)
   {
-    GNUNET_PEERINFO_iterate_cancel (kx->pitr);
-    kx->pitr = NULL;
+    pos->status = KX_STATE_REKEY_SENT;
+    send_key (pos);
   }
-  if (kx->retry_set_key_task != GNUNET_SCHEDULER_NO_TASK)
-    GNUNET_SCHEDULER_cancel (kx->retry_set_key_task);
-  GNUNET_free_non_null (kx->public_key);
-  GNUNET_free (kx);
 }
 
 
-int 
-GSC_KX_init ()
+/**
+ * Initialize KX subsystem.
+ *
+ * @param pk private key to use for the peer
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
+ */
+int
+GSC_KX_init (struct GNUNET_CRYPTO_EddsaPrivateKey *pk)
 {
-  peerinfo = GNUNET_PEERINFO_connect (cfg);
-  if (NULL == peerinfo)
+  GNUNET_assert (NULL != pk);
+  my_private_key = pk;
+  GNUNET_CRYPTO_eddsa_key_get_public (my_private_key,
+                                                 &GSC_my_identity.public_key);
+  my_ephemeral_key = GNUNET_CRYPTO_ecdhe_key_create ();
+  if (NULL == my_ephemeral_key)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _("Could not access PEERINFO service.  Exiting.\n"));
+    GNUNET_break (0);
+    GNUNET_free (my_private_key);
+    my_private_key = NULL;
     return GNUNET_SYSERR;
   }
+  sign_ephemeral_key ();
+  rekey_task = GNUNET_SCHEDULER_add_delayed (REKEY_FREQUENCY,
+                                             &do_rekey,
+                                             NULL);
+  mst = GNUNET_SERVER_mst_create (&deliver_message, NULL);
   return GNUNET_OK;
 }
 
 
-void 
+/**
+ * Shutdown KX subsystem.
+ */
+void
 GSC_KX_done ()
 {
-  if (peerinfo != NULL)
-    {
-      GNUNET_PEERINFO_disconnect (peerinfo);
-      peerinfo = NULL;
-    }
-
+  if (GNUNET_SCHEDULER_NO_TASK != rekey_task)
+  {
+    GNUNET_SCHEDULER_cancel (rekey_task);
+    rekey_task = GNUNET_SCHEDULER_NO_TASK;
+  }
+  if (NULL != my_ephemeral_key)
+  {
+    GNUNET_free (my_ephemeral_key);
+    my_ephemeral_key = NULL;
+  }
+  if (NULL != my_private_key)
+  {
+    GNUNET_free (my_private_key);
+    my_private_key = NULL;
+  }
+  if (NULL != mst)
+  {
+    GNUNET_SERVER_mst_destroy (mst);
+    mst = NULL;
+  }
 }
+
+/* end of gnunet-service-core_kx.c */