Added keymat_v1_t.get_hash() to calculate authentication hashes
authorMartin Willi <martin@revosec.ch>
Mon, 21 Nov 2011 15:41:48 +0000 (16:41 +0100)
committerMartin Willi <martin@revosec.ch>
Tue, 20 Mar 2012 16:30:46 +0000 (17:30 +0100)
src/libcharon/sa/keymat_v1.c
src/libcharon/sa/keymat_v1.h

index 891f6a2..c8846fa 100644 (file)
@@ -466,6 +466,43 @@ METHOD(keymat_v1_t, derive_ike_keys, bool,
        return TRUE;
 }
 
        return TRUE;
 }
 
+METHOD(keymat_v1_t, get_hash, chunk_t,
+       private_keymat_v1_t *this, bool initiator, chunk_t dh, chunk_t dh_other,
+       ike_sa_id_t *ike_sa_id, chunk_t sa_i, identification_t *id)
+{
+       chunk_t hash, data;
+       u_int64_t spi, spi_other;
+       /* TODO-IKEv1: get real bytes from ID header? */
+       u_int8_t id_header[4] = { id->get_type(id), 0, 0, 0 };
+
+       /* HASH_I = prf(SKEYID, g^xi | g^xr | CKY-I | CKY-R | SAi_b | IDii_b )
+        * HASH_R = prf(SKEYID, g^xr | g^xi | CKY-R | CKY-I | SAi_b | IDir_b )
+        */
+       if (initiator)
+       {
+               spi = ike_sa_id->get_initiator_spi(ike_sa_id);
+               spi_other = ike_sa_id->get_responder_spi(ike_sa_id);
+       }
+       else
+       {
+               spi_other = ike_sa_id->get_initiator_spi(ike_sa_id);
+               spi = ike_sa_id->get_responder_spi(ike_sa_id);
+       }
+       data = chunk_cat("ccccccc", dh, dh_other,
+                                        chunk_from_thing(spi), chunk_from_thing(spi_other),
+                                        sa_i, chunk_from_thing(id_header), id->get_encoding(id));
+
+       DBG3(DBG_IKE, "HASH_%c data %B", initiator ? 'I' : 'R', &data);
+
+       this->prf->set_key(this->prf, this->skeyid);
+       this->prf->allocate_bytes(this->prf, data, &hash);
+
+       DBG3(DBG_IKE, "HASH_%c %B", initiator ? 'I' : 'R', &hash);
+
+       free(data.ptr);
+       return hash;
+}
+
 /**
  * Generate an IV
  */
 /**
  * Generate an IV
  */
@@ -602,6 +639,7 @@ keymat_v1_t *keymat_v1_create(bool initiator)
                                .destroy = _destroy,
                        },
                        .derive_ike_keys = _derive_ike_keys,
                                .destroy = _destroy,
                        },
                        .derive_ike_keys = _derive_ike_keys,
+                       .get_hash = _get_hash,
                        .get_iv = _get_iv,
                        .update_iv = _update_iv,
                        .confirm_iv = _confirm_iv,
                        .get_iv = _get_iv,
                        .update_iv = _update_iv,
                        .confirm_iv = _confirm_iv,
index 54d4d43..142c835 100644 (file)
@@ -57,6 +57,21 @@ struct keymat_v1_t {
                                                        auth_class_t auth, shared_key_t *shared_key);
 
        /**
                                                        auth_class_t auth, shared_key_t *shared_key);
 
        /**
+        * Get HASH data for authentication.
+        *
+        * @param initiatior    TRUE to create HASH_I, FALSE for HASH_R
+        * @param dh                    public DH value of peer to create HASH for
+        * @param dh_other              others public DH value
+        * @param ike_sa_id             IKE_SA identifier
+        * @param sa_i                  encoded SA payload of initiator
+        * @param id                    ID of peer to create hash for
+        * @return                              allocated HASH data
+        */
+       chunk_t (*get_hash)(keymat_v1_t *this, bool initiator,
+                                               chunk_t dh, chunk_t dh_other, ike_sa_id_t *ike_sa_id,
+                                               chunk_t sa_i, identification_t *id);
+
+       /**
         * Returns the IV for a message with the given message ID.
         *
         * @param mid                   message ID
         * Returns the IV for a message with the given message ID.
         *
         * @param mid                   message ID