-style fix
[oweals/gnunet.git] / src / core / gnunet-service-core_kx.c
index b8b58bc83b774745d94cc6fac7d10d804e83c6ee..d1990f8f4f5203edb4fd6aa1fa066c895ae3dad5 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2009, 2010, 2011, 2012 Christian Grothoff (and other contributing authors)
+     (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
@@ -90,7 +90,7 @@ struct EphemeralKeyMessage
    * An ECC signature of the 'origin' asserting the validity of
    * the given ephemeral key.
    */
-  struct GNUNET_CRYPTO_EccSignature signature;
+  struct GNUNET_CRYPTO_EddsaSignature signature;
 
   /**
    * Information about what is being signed.
@@ -101,7 +101,7 @@ struct EphemeralKeyMessage
    * 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).
    */
@@ -111,12 +111,12 @@ struct EphemeralKeyMessage
    * 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_EccPublicKeyBinaryEncoded ephemeral_key;  
+  struct GNUNET_CRYPTO_EcdhePublicKey ephemeral_key;
 
   /**
    * Public key of the signing peer (persistent version, not the ephemeral public key).
    */
-  struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded origin_public_key;
+  struct GNUNET_PeerIdentity origin_identity;
 
 };
 
@@ -270,8 +270,11 @@ enum KxStateMachine
   KX_STATE_UP,
 
   /**
-   * We're rekeying, so we have sent the other peer our new ephemeral
-   * key, but we did not get a matching PONG yet.
+   * 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
 
@@ -308,13 +311,13 @@ struct GSC_KeyExchangeInfo
    * Key we use to encrypt our messages for the other peer
    * (initialized by us when we do the handshake).
    */
-  struct GNUNET_CRYPTO_AesSessionKey encrypt_key;
+  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_AesSessionKey decrypt_key;
+  struct GNUNET_CRYPTO_SymmetricSessionKey decrypt_key;
 
   /**
    * At what time did the other peer generate the decryption key?
@@ -374,23 +377,18 @@ struct GSC_KeyExchangeInfo
 /**
  * Our private key.
  */
-static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
+static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
 
 /**
  * Our ephemeral private key.
  */
-static struct GNUNET_CRYPTO_EccPrivateKey *my_ephemeral_key;
+static struct GNUNET_CRYPTO_EcdhePrivateKey *my_ephemeral_key;
 
 /**
  * Current message we send for a key exchange.
  */
 static struct EphemeralKeyMessage current_ekm;
 
-/**
- * Our public key.
- */
-static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
-
 /**
  * Our message stream tokenizer (for encrypted payload).
  */
@@ -409,7 +407,7 @@ 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;
 
 
@@ -422,13 +420,15 @@ static GNUNET_SCHEDULER_TaskIdentifier rekey_task;
  */
 static void
 derive_auth_key (struct GNUNET_CRYPTO_AuthKey *akey,
-                 const struct GNUNET_CRYPTO_AesSessionKey *skey, uint32_t seed)
+                 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey, uint32_t seed)
 {
   static const char ctx[] = "authentication key";
 
-  GNUNET_CRYPTO_hmac_derive_key (akey, skey, &seed, sizeof (seed), &skey->key,
-                                 sizeof (skey->key), ctx,
-                                 sizeof (ctx), NULL);
+  GNUNET_CRYPTO_hmac_derive_key (akey, skey,
+                                 &seed, sizeof (seed),
+                                 skey, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
+                                 ctx, sizeof (ctx),
+                                 NULL);
 }
 
 
@@ -441,16 +441,16 @@ derive_auth_key (struct GNUNET_CRYPTO_AuthKey *akey,
  * @param identity identity of the other peer to use
  */
 static void
-derive_iv (struct GNUNET_CRYPTO_AesInitializationVector *iv,
-           const struct GNUNET_CRYPTO_AesSessionKey *skey, uint32_t seed,
+derive_iv (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
+           const struct GNUNET_CRYPTO_SymmetricSessionKey *skey, uint32_t seed,
            const struct GNUNET_PeerIdentity *identity)
 {
   static const char ctx[] = "initialization vector";
 
-  GNUNET_CRYPTO_aes_derive_iv (iv, skey, &seed, sizeof (seed),
-                               &identity->hashPubKey.bits,
-                               sizeof (identity->hashPubKey.bits), ctx,
-                               sizeof (ctx), NULL);
+  GNUNET_CRYPTO_symmetric_derive_iv (iv, skey, &seed, sizeof (seed),
+                                    identity,
+                                    sizeof (struct GNUNET_PeerIdentity), ctx,
+                                    sizeof (ctx), NULL);
 }
 
 
@@ -464,16 +464,18 @@ derive_iv (struct GNUNET_CRYPTO_AesInitializationVector *iv,
  * @param identity identity of the other peer to use
  */
 static void
-derive_pong_iv (struct GNUNET_CRYPTO_AesInitializationVector *iv,
-                const struct GNUNET_CRYPTO_AesSessionKey *skey, uint32_t seed,
+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_aes_derive_iv (iv, skey, &seed, sizeof (seed),
-                               &identity->hashPubKey.bits,
-                               sizeof (identity->hashPubKey.bits), &challenge,
-                               sizeof (challenge), ctx, sizeof (ctx), NULL);
+  GNUNET_CRYPTO_symmetric_derive_iv (iv, skey, &seed, sizeof (seed),
+                                    identity,
+                                    sizeof (struct GNUNET_PeerIdentity),
+                                    &challenge, sizeof (challenge),
+                                    ctx, sizeof (ctx),
+                                    NULL);
 }
 
 
@@ -483,19 +485,19 @@ derive_pong_iv (struct GNUNET_CRYPTO_AesInitializationVector *iv,
  * @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 
+ * @param skey set to derived session key
  */
 static void
 derive_aes_key (const struct GNUNET_PeerIdentity *sender,
                const struct GNUNET_PeerIdentity *receiver,
                const struct GNUNET_HashCode *key_material,
-               struct GNUNET_CRYPTO_AesSessionKey *skey)
+               struct GNUNET_CRYPTO_SymmetricSessionKey *skey)
 {
   static const char ctx[] = "aes key generation vector";
 
-  GNUNET_CRYPTO_kdf (skey, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
+  GNUNET_CRYPTO_kdf (skey, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
                     ctx, sizeof (ctx),
-                    skey, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
+                    key_material, sizeof (struct GNUNET_HashCode),
                     sender, sizeof (struct GNUNET_PeerIdentity),
                     receiver, sizeof (struct GNUNET_PeerIdentity),
                     NULL);
@@ -515,7 +517,7 @@ derive_aes_key (const struct GNUNET_PeerIdentity *sender,
  */
 static int
 do_encrypt (struct GSC_KeyExchangeInfo *kx,
-            const struct GNUNET_CRYPTO_AesInitializationVector *iv,
+            const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
             const void *in, void *out, size_t size)
 {
   if (size != (uint16_t) size)
@@ -524,7 +526,7 @@ do_encrypt (struct GSC_KeyExchangeInfo *kx,
     return GNUNET_NO;
   }
   GNUNET_assert (size ==
-                 GNUNET_CRYPTO_aes_encrypt (in, (uint16_t) 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);
@@ -551,12 +553,12 @@ do_encrypt (struct GSC_KeyExchangeInfo *kx,
  * @param iv initialization vector to use
  * @param in ciphertext
  * @param out plaintext
- * @param size size of in/out
- * @return GNUNET_OK on success
+ * @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_AesInitializationVector *iv,
+            const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
             const void *in, void *out, size_t size)
 {
   if (size != (uint16_t) size)
@@ -571,7 +573,7 @@ do_decrypt (struct GSC_KeyExchangeInfo *kx,
     return GNUNET_SYSERR;
   }
   if (size !=
-      GNUNET_CRYPTO_aes_decrypt (in, (uint16_t) size, &kx->decrypt_key, iv,
+      GNUNET_CRYPTO_symmetric_decrypt (in, (uint16_t) size, &kx->decrypt_key, iv,
                                  out))
   {
     GNUNET_break (0);
@@ -615,6 +617,7 @@ set_key_retry_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
   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);
 }
 
@@ -629,7 +632,7 @@ setup_fresh_ping (struct GSC_KeyExchangeInfo *kx)
 {
   struct PingMessage pp;
   struct PingMessage *pm;
-  struct GNUNET_CRYPTO_AesInitializationVector iv;
+  struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
 
   pm = &kx->ping;
   pm->header.size = htons (sizeof (struct PingMessage));
@@ -655,21 +658,40 @@ 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, 
+  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_malloc (sizeof (struct GSC_KeyExchangeInfo));
+  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;
-  send_key (kx);
+  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;
 }
 
@@ -682,6 +704,7 @@ GSC_KX_start (const struct GNUNET_PeerIdentity *pid)
 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)
@@ -728,11 +751,10 @@ GSC_KX_handle_ephemeral_key (struct GSC_KeyExchangeInfo *kx,
   struct GNUNET_TIME_Absolute start_t;
   struct GNUNET_TIME_Absolute end_t;
   struct GNUNET_TIME_Absolute now;
-  struct GNUNET_PeerIdentity signer_id;
-  enum KxStateMachine sender_status;  
+  enum KxStateMachine sender_status;
   uint16_t size;
   struct GNUNET_HashCode key_material;
-  
+
   size = ntohs (msg->size);
   if (sizeof (struct EphemeralKeyMessage) != size)
   {
@@ -740,19 +762,29 @@ GSC_KX_handle_ephemeral_key (struct GSC_KeyExchangeInfo *kx,
     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));
-  GNUNET_CRYPTO_hash (&m->origin_public_key,
-                     sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded),
-                     &signer_id.hashPubKey);
   if (0 !=
-      memcmp (&signer_id, &kx->peer,
+      memcmp (&m->origin_identity,
+             &kx->peer.public_key,
               sizeof (struct GNUNET_PeerIdentity)))
-  {    
+  {
     GNUNET_break_op (0);
     return;
   }
@@ -760,33 +792,32 @@ GSC_KX_handle_ephemeral_key (struct GSC_KeyExchangeInfo *kx,
        sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
        sizeof (struct GNUNET_TIME_AbsoluteNBO) +
        sizeof (struct GNUNET_TIME_AbsoluteNBO) +
-       sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded) +
-       sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded)) ||
+       sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
+       sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)) ||
       (GNUNET_OK !=
-       GNUNET_CRYPTO_ecc_verify (GNUNET_SIGNATURE_PURPOSE_SET_ECC_KEY,
+       GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_SET_ECC_KEY,
                                 &m->purpose,
-                                 &m->signature, &m->origin_public_key)))
+                                 &m->signature, &m->origin_identity.public_key)))
   {
     /* invalid signature */
     GNUNET_break_op (0);
     return;
   }
-  start_t = GNUNET_TIME_absolute_ntoh (m->creation_time);
-  end_t = GNUNET_TIME_absolute_ntoh (m->expiration_time);
   now = GNUNET_TIME_absolute_get ();
-  if ( (end_t.abs_value < GNUNET_TIME_absolute_subtract (now, REKEY_TOLERANCE).abs_value) ||
-       (start_t.abs_value > GNUNET_TIME_absolute_add (now, REKEY_TOLERANCE).abs_value) )
+  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) )
   {
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-               _("Ephemeral key message rejected as its validity range does not match our system time (%llu not in [%llu,%llu]).\n"),
-               now.abs_value,
-               start_t.abs_value,
-               end_t.abs_value);
+               _("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 (GNUNET_OK !=
       GNUNET_CRYPTO_ecc_ecdh (my_ephemeral_key,
-                             &m->ephemeral_key,                              
+                             &m->ephemeral_key,
                              &key_material))
   {
     GNUNET_break (0);
@@ -810,18 +841,17 @@ GSC_KX_handle_ephemeral_key (struct GSC_KeyExchangeInfo *kx,
   setup_fresh_ping (kx);
 
   /* check if we still need to send the sender our key */
-  sender_status = (enum KxStateMachine) ntohl (m->sender_status);  
+  sender_status = (enum KxStateMachine) ntohl (m->sender_status);
   switch (sender_status)
   {
   case KX_STATE_DOWN:
-    /* makes no sense, should be at least KX_STATE_KEY_SENT */
     GNUNET_break_op (0);
     break;
   case KX_STATE_KEY_SENT:
-    send_key (kx);
+    /* fine, need to send our key after updating our status, see below */
     break;
   case KX_STATE_KEY_RECEIVED:
-  case KX_STATE_UP: 
+  case KX_STATE_UP:
   case KX_STATE_REKEY_SENT:
     /* other peer already got our key */
     break;
@@ -833,23 +863,35 @@ GSC_KX_handle_ephemeral_key (struct GSC_KeyExchangeInfo *kx,
   switch (kx->status)
   {
   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: 
+  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:
-    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;
@@ -875,7 +917,7 @@ GSC_KX_handle_ping (struct GSC_KeyExchangeInfo *kx,
   struct PingMessage t;
   struct PongMessage tx;
   struct PongMessage tp;
-  struct GNUNET_CRYPTO_AesInitializationVector iv;
+  struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
   uint16_t msize;
 
   msize = ntohs (msg->size);
@@ -887,7 +929,7 @@ GSC_KX_handle_ping (struct GSC_KeyExchangeInfo *kx,
   GNUNET_STATISTICS_update (GSC_stats,
                             gettext_noop ("# PING messages received"), 1,
                             GNUNET_NO);
-  if ( (kx->status != KX_STATE_KEY_RECEIVED) && 
+  if ( (kx->status != KX_STATE_KEY_RECEIVED) &&
        (kx->status != KX_STATE_UP) &&
        (kx->status != KX_STATE_REKEY_SENT))
   {
@@ -960,7 +1002,7 @@ send_keep_alive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
   kx->keep_alive_task = GNUNET_SCHEDULER_NO_TASK;
   left = GNUNET_TIME_absolute_get_remaining (kx->timeout);
-  if (0 == left.rel_value)
+  if (0 == left.rel_value_us)
   {
     GNUNET_STATISTICS_update (GSC_stats,
                               gettext_noop ("# sessions terminated by timeout"),
@@ -1020,7 +1062,7 @@ GSC_KX_handle_pong (struct GSC_KeyExchangeInfo *kx,
 {
   const struct PongMessage *m;
   struct PongMessage t;
-  struct GNUNET_CRYPTO_AesInitializationVector iv;
+  struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
   uint16_t msize;
 
   msize = ntohs (msg->size);
@@ -1093,7 +1135,7 @@ GSC_KX_handle_pong (struct GSC_KeyExchangeInfo *kx,
   {
     GNUNET_SCHEDULER_cancel (kx->retry_set_key_task);
     kx->retry_set_key_task = GNUNET_SCHEDULER_NO_TASK;
-  }  
+  }
   switch (kx->status)
   {
   case KX_STATE_DOWN:
@@ -1142,17 +1184,18 @@ GSC_KX_handle_pong (struct GSC_KeyExchangeInfo *kx,
 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, 
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Sending key to `%s' (my status: %d)\n",
               GNUNET_i2s (&kx->peer),
              kx->status);
-  current_ekm.sender_status = htonl ((int32_t) (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 =
@@ -1177,7 +1220,7 @@ GSC_KX_encrypt_and_transmit (struct GSC_KeyExchangeInfo *kx,
   char cbuf[used];              /* ciphertext */
   struct EncryptedMessage *em;  /* encrypted message */
   struct EncryptedMessage *ph;  /* plaintext header */
-  struct GNUNET_CRYPTO_AesInitializationVector iv;
+  struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
   struct GNUNET_CRYPTO_AuthKey auth_key;
 
   ph = (struct EncryptedMessage *) pbuf;
@@ -1200,8 +1243,8 @@ GSC_KX_encrypt_and_transmit (struct GSC_KeyExchangeInfo *kx,
                              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, 
+  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);
@@ -1217,19 +1260,14 @@ struct DeliverMessageContext
 {
 
   /**
-   * Performance information for the connection.
+   * Key exchange context.
    */
-  const struct GNUNET_ATS_Information *atsi;
+  struct GSC_KeyExchangeInfo *kx;
 
   /**
    * Sender of the message.
    */
   const struct GNUNET_PeerIdentity *peer;
-
-  /**
-   * Number of entries in 'atsi' array.
-   */
-  uint32_t atsi_count;
 };
 
 
@@ -1239,21 +1277,17 @@ struct DeliverMessageContext
  *
  * @param kx key exchange context for encrypting the message
  * @param msg encrypted message
- * @param atsi performance data
- * @param atsi_count number of entries in ats (excluding 0-termination)
  */
 void
 GSC_KX_handle_encrypted_message (struct GSC_KeyExchangeInfo *kx,
-                                 const struct GNUNET_MessageHeader *msg,
-                                 const struct GNUNET_ATS_Information *atsi,
-                                 uint32_t atsi_count)
+                                 const struct GNUNET_MessageHeader *msg)
 {
   const struct EncryptedMessage *m;
   struct EncryptedMessage *pt;  /* plaintext */
   struct GNUNET_HashCode ph;
   uint32_t snum;
   struct GNUNET_TIME_Absolute t;
-  struct GNUNET_CRYPTO_AesInitializationVector iv;
+  struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
   struct GNUNET_CRYPTO_AuthKey auth_key;
   struct DeliverMessageContext dmc;
   uint16_t size = ntohs (msg->size);
@@ -1274,13 +1308,21 @@ GSC_KX_handle_encrypted_message (struct GSC_KeyExchangeInfo *kx,
                               1, GNUNET_NO);
     return;
   }
-  if (0 == GNUNET_TIME_absolute_get_remaining (kx->foreign_key_expires).rel_value)
+  if (0 == GNUNET_TIME_absolute_get_remaining (kx->foreign_key_expires).rel_value_us)
   {
-    kx->status = KX_STATE_KEY_SENT;
+    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
-                              ("# DATA message dropped (session key expired)"),
+                              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;
   }
@@ -1292,8 +1334,8 @@ GSC_KX_handle_encrypted_message (struct GSC_KeyExchangeInfo *kx,
   if (0 != memcmp (&ph, &m->hmac, sizeof (struct GNUNET_HashCode)))
   {
     /* checksum failed */
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
-               "Failed checksum validation for a message from `%s'\n", 
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+               "Failed checksum validation for a message from `%s'\n",
                GNUNET_i2s (&kx->peer));
     return;
   }
@@ -1360,8 +1402,8 @@ GSC_KX_handle_encrypted_message (struct GSC_KeyExchangeInfo *kx,
 
   /* check timestamp */
   t = GNUNET_TIME_absolute_ntoh (pt->timestamp);
-  if (GNUNET_TIME_absolute_get_duration (t).rel_value >
-      MAX_MESSAGE_AGE.rel_value)
+  if (GNUNET_TIME_absolute_get_duration (t).rel_value_us >
+      MAX_MESSAGE_AGE.rel_value_us)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "Message received far too old (%s). Content ignored.\n",
@@ -1378,8 +1420,7 @@ GSC_KX_handle_encrypted_message (struct GSC_KeyExchangeInfo *kx,
   GNUNET_STATISTICS_update (GSC_stats,
                             gettext_noop ("# bytes of payload decrypted"),
                             size - sizeof (struct EncryptedMessage), GNUNET_NO);
-  dmc.atsi = atsi;
-  dmc.atsi_count = atsi_count;
+  dmc.kx = kx;
   dmc.peer = &kx->peer;
   if (GNUNET_OK !=
       GNUNET_SERVER_mst_receive (mst, &dmc,
@@ -1404,6 +1445,14 @@ deliver_message (void *cls, void *client, const struct GNUNET_MessageHeader *m)
 {
   struct DeliverMessageContext *dmc = client;
 
+  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:
@@ -1411,10 +1460,10 @@ deliver_message (void *cls, void *client, const struct GNUNET_MessageHeader *m)
     GSC_SESSIONS_set_typemap (dmc->peer, m);
     return GNUNET_OK;
   default:
-    GSC_CLIENTS_deliver_message (dmc->peer, dmc->atsi, dmc->atsi_count, m,
+    GSC_CLIENTS_deliver_message (dmc->peer, m,
                                  ntohs (m->size),
                                  GNUNET_CORE_OPTION_SEND_FULL_INBOUND);
-    GSC_CLIENTS_deliver_message (dmc->peer, dmc->atsi, dmc->atsi_count, m,
+    GSC_CLIENTS_deliver_message (dmc->peer, m,
                                  sizeof (struct GNUNET_MessageHeader),
                                  GNUNET_CORE_OPTION_SEND_HDR_INBOUND);
   }
@@ -1436,16 +1485,26 @@ sign_ephemeral_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_EccPublicKeyBinaryEncoded) +
-                                   sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded));
+                                   sizeof (struct GNUNET_CRYPTO_EcdhePublicKey) +
+                                   sizeof (struct GNUNET_PeerIdentity));
   current_ekm.creation_time = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
-  current_ekm.expiration_time = GNUNET_TIME_absolute_hton (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_add (REKEY_FREQUENCY,
-                                                                                                                      REKEY_TOLERANCE)));
-  GNUNET_CRYPTO_ecc_key_get_public (my_ephemeral_key,
-                                   &current_ekm.ephemeral_key);
-  current_ekm.origin_public_key = my_public_key;
+  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_ecc_sign (my_private_key,
+                GNUNET_CRYPTO_eddsa_sign (my_private_key,
                                         &current_ekm.purpose,
                                         &current_ekm.signature));
 }
@@ -1467,8 +1526,8 @@ do_rekey (void *cls,
                                             &do_rekey,
                                             NULL);
   if (NULL != my_ephemeral_key)
-    GNUNET_CRYPTO_ecc_key_free (my_ephemeral_key);
-  my_ephemeral_key = GNUNET_CRYPTO_ecc_key_create ();
+    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)
@@ -1483,29 +1542,28 @@ do_rekey (void *cls,
  * Initialize KX subsystem.
  *
  * @param pk private key to use for the peer
- * @return GNUNET_OK on success, GNUNET_SYSERR on failure
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
  */
 int
-GSC_KX_init (struct GNUNET_CRYPTO_EccPrivateKey *pk)
+GSC_KX_init (struct GNUNET_CRYPTO_EddsaPrivateKey *pk)
 {
   GNUNET_assert (NULL != pk);
   my_private_key = pk;
-  GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
-  GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
-                      &GSC_my_identity.hashPubKey);
-  my_ephemeral_key = GNUNET_CRYPTO_ecc_key_create ();
+  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_break (0);
-    GNUNET_CRYPTO_ecc_key_free (my_private_key);
+    GNUNET_free (my_private_key);
     my_private_key = NULL;
     return GNUNET_SYSERR;
   }
   sign_ephemeral_key ();
-  mst = GNUNET_SERVER_mst_create (&deliver_message, NULL);
   rekey_task = GNUNET_SCHEDULER_add_delayed (REKEY_FREQUENCY,
-                                            &do_rekey,
-                                            NULL);
+                                             &do_rekey,
+                                             NULL);
+  mst = GNUNET_SERVER_mst_create (&deliver_message, NULL);
   return GNUNET_OK;
 }
 
@@ -1521,16 +1579,16 @@ GSC_KX_done ()
     GNUNET_SCHEDULER_cancel (rekey_task);
     rekey_task = GNUNET_SCHEDULER_NO_TASK;
   }
-  if (NULL != my_private_key)
-  {
-    GNUNET_CRYPTO_ecc_key_free (my_private_key);
-    my_private_key = NULL;
-  }
   if (NULL != my_ephemeral_key)
   {
-    GNUNET_CRYPTO_ecc_key_free (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);