-style fix
[oweals/gnunet.git] / src / core / gnunet-service-core_kx.c
index a74b3ae7427379d3b0c808ca11585c40f749639b..d1990f8f4f5203edb4fd6aa1fa066c895ae3dad5 100644 (file)
@@ -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.
@@ -111,7 +111,7 @@ 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_EccPublicEncryptKey ephemeral_key;
+  struct GNUNET_CRYPTO_EcdhePublicKey ephemeral_key;
 
   /**
    * Public key of the signing peer (persistent version, not the ephemeral public key).
@@ -377,12 +377,12 @@ 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.
@@ -553,8 +553,8 @@ 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,
@@ -667,7 +667,7 @@ GSC_KX_start (const struct GNUNET_PeerIdentity *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,
@@ -675,16 +675,23 @@ GSC_KX_start (const struct GNUNET_PeerIdentity *pid)
                               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 */
-    kx->status = KX_STATE_KEY_SENT;
     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;
 }
 
@@ -785,10 +792,10 @@ 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_EccPublicSignKey) +
-       sizeof (struct GNUNET_CRYPTO_EccPublicSignKey)) ||
+       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_identity.public_key)))
   {
@@ -810,7 +817,7 @@ GSC_KX_handle_ephemeral_key (struct GSC_KeyExchangeInfo *kx,
   }
   if (GNUNET_OK !=
       GNUNET_CRYPTO_ecc_ecdh (my_ephemeral_key,
-                             &m->ephemeral_key,                        
+                             &m->ephemeral_key,
                              &key_material))
   {
     GNUNET_break (0);
@@ -1478,8 +1485,8 @@ 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_EccPublicSignKey) +
-                                   sizeof (struct GNUNET_CRYPTO_EccPublicSignKey));
+                                   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,
@@ -1493,11 +1500,11 @@ sign_ephemeral_key ()
   {
     current_ekm.expiration_time = GNUNET_TIME_absolute_hton (GNUNET_TIME_UNIT_FOREVER_ABS);
   }
-  GNUNET_CRYPTO_ecc_key_get_public_for_encryption (my_ephemeral_key,
-                                                  &current_ekm.ephemeral_key);
+  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));
 }
@@ -1520,7 +1527,7 @@ do_rekey (void *cls,
                                             NULL);
   if (NULL != my_ephemeral_key)
     GNUNET_free (my_ephemeral_key);
-  my_ephemeral_key = GNUNET_CRYPTO_ecc_key_create ();
+  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)
@@ -1535,38 +1542,27 @@ 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_for_signature (my_private_key,
+  GNUNET_CRYPTO_eddsa_key_get_public (my_private_key,
                                                  &GSC_my_identity.public_key);
-  if (GNUNET_YES ==
-      GNUNET_CONFIGURATION_get_value_yesno (GSC_cfg,
-                                           "core",
-                                           "USE_EPHEMERAL_KEYS"))
+  my_ephemeral_key = GNUNET_CRYPTO_ecdhe_key_create ();
+  if (NULL == my_ephemeral_key)
   {
-    my_ephemeral_key = GNUNET_CRYPTO_ecc_key_create ();
-    if (NULL == my_ephemeral_key)
-    {
-      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);
-  }
-  else
-  {
-    my_ephemeral_key = my_private_key;
-    sign_ephemeral_key ();
+    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;
 }
@@ -1583,8 +1579,7 @@ GSC_KX_done ()
     GNUNET_SCHEDULER_cancel (rekey_task);
     rekey_task = GNUNET_SCHEDULER_NO_TASK;
   }
-  if ( (NULL != my_ephemeral_key) &&
-       (my_ephemeral_key != my_private_key) )
+  if (NULL != my_ephemeral_key)
   {
     GNUNET_free (my_ephemeral_key);
     my_ephemeral_key = NULL;