]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
botan: Simplify DH/ECDH key derivation
authorTobias Brunner <tobias@strongswan.org>
Tue, 11 Sep 2018 08:58:42 +0000 (10:58 +0200)
committerTobias Brunner <tobias@strongswan.org>
Wed, 12 Sep 2018 14:25:00 +0000 (16:25 +0200)
src/libstrongswan/plugins/botan/botan_diffie_hellman.c
src/libstrongswan/plugins/botan/botan_ec_diffie_hellman.c
src/libstrongswan/plugins/botan/botan_util.c
src/libstrongswan/plugins/botan/botan_util.h

index 008e15fbde6c06c4caec74ce65b87e2ac7974c13..a55711d1b5ad3e24c7389979f9c0ff3f805e575f 100644 (file)
@@ -97,37 +97,14 @@ bool load_private_key(private_botan_diffie_hellman_t *this, chunk_t value)
 METHOD(diffie_hellman_t, set_other_public_value, bool,
        private_botan_diffie_hellman_t *this, chunk_t value)
 {
-       botan_pk_op_ka_t op;
-
        if (!diffie_hellman_verify_value(this->group, value))
        {
                return FALSE;
        }
 
-       if (botan_pk_op_key_agreement_create(&op, this->dh_key, "Raw", 0))
-       {
-               return FALSE;
-       }
-
        chunk_clear(&this->shared_secret);
 
-       if (botan_pk_op_key_agreement_size(op, &this->shared_secret.len))
-       {
-               botan_pk_op_key_agreement_destroy(op);
-               return FALSE;
-       }
-
-       this->shared_secret = chunk_alloc(this->shared_secret.len);
-       if (botan_pk_op_key_agreement(op, this->shared_secret.ptr,
-                                                                 &this->shared_secret.len, value.ptr,
-                                                                 value.len, NULL, 0))
-       {
-               chunk_clear(&this->shared_secret);
-               botan_pk_op_key_agreement_destroy(op);
-               return FALSE;
-       }
-       botan_pk_op_key_agreement_destroy(op);
-       return TRUE;
+       return botan_dh_key_derivation(this->dh_key, value, &this->shared_secret);
 }
 
 METHOD(diffie_hellman_t, get_my_public_value, bool,
index a482bc0289540fdd9112883abbd46cbaa391d397..ed28b46390a5d03541d53b843d6de88dfe3fe5d1 100644 (file)
@@ -69,40 +69,17 @@ struct private_botan_ec_diffie_hellman_t {
 METHOD(diffie_hellman_t, set_other_public_value, bool,
        private_botan_ec_diffie_hellman_t *this, chunk_t value)
 {
-       botan_pk_op_ka_t ka;
-
        if (!diffie_hellman_verify_value(this->group, value))
        {
                return FALSE;
        }
 
-       if (botan_pk_op_key_agreement_create(&ka, this->key, "Raw", 0))
-       {
-               return FALSE;
-       }
-
        chunk_clear(&this->shared_secret);
 
-       if (botan_pk_op_key_agreement_size(ka, &this->shared_secret.len))
-       {
-               botan_pk_op_key_agreement_destroy(ka);
-               return FALSE;
-       }
-
        /* prepend 0x04 to indicate uncompressed point format */
        value = chunk_cata("cc", chunk_from_chars(0x04), value);
 
-       this->shared_secret = chunk_alloc(this->shared_secret.len);
-       if (botan_pk_op_key_agreement(ka, this->shared_secret.ptr,
-                                                                 &this->shared_secret.len, value.ptr,
-                                                                 value.len, NULL, 0))
-       {
-               chunk_clear(&this->shared_secret);
-               botan_pk_op_key_agreement_destroy(ka);
-               return FALSE;
-       }
-       botan_pk_op_key_agreement_destroy(ka);
-       return TRUE;
+       return botan_dh_key_derivation(this->key, value, &this->shared_secret);
 }
 
 METHOD(diffie_hellman_t, get_my_public_value, bool,
index 860d376c3bc17e9c0d25048527c2c4e3e8518d3d..a1d352950445e18ea3933287d49f93dcf0d35a93 100644 (file)
@@ -259,3 +259,33 @@ bool botan_get_signature(botan_privkey_t key, const char *scheme,
        botan_pk_op_sign_destroy(sign_op);
        return TRUE;
 }
+
+/*
+ * Described in header
+ */
+bool botan_dh_key_derivation(botan_privkey_t key, chunk_t pub, chunk_t *secret)
+{
+       botan_pk_op_ka_t ka;
+
+       if (botan_pk_op_key_agreement_create(&ka, key, "Raw", 0))
+       {
+               return FALSE;
+       }
+
+       if (botan_pk_op_key_agreement_size(ka, &secret->len))
+       {
+               botan_pk_op_key_agreement_destroy(ka);
+               return FALSE;
+       }
+
+       *secret = chunk_alloc(secret->len);
+       if (botan_pk_op_key_agreement(ka, secret->ptr, &secret->len, pub.ptr,
+                                                                 pub.len, NULL, 0))
+       {
+               chunk_clear(secret);
+               botan_pk_op_key_agreement_destroy(ka);
+               return FALSE;
+       }
+       botan_pk_op_key_agreement_destroy(ka);
+       return TRUE;
+}
index 2c6b1f816fe3e292d407bf2908ca701eefbf1b19..08830356ed5c9260c1dcf730e0a10ca69dc31c76 100644 (file)
@@ -100,4 +100,17 @@ bool botan_get_fingerprint(botan_pubkey_t pubkey, void *cache,
 bool botan_get_signature(botan_privkey_t key, const char *scheme,
                                                 chunk_t data, chunk_t *signature);
 
+/**
+ * Do the Diffie-Hellman key derivation using the given private key and public
+ * value.
+ *
+ * Note that the public value is not verified in this function.
+ *
+ * @param key          DH private key
+ * @param pub          other's public value
+ * @param secret       the derived secret (allocated on success)
+ * @return                     TRUE if derivation was successful
+ */
+bool botan_dh_key_derivation(botan_privkey_t key, chunk_t pub, chunk_t *secret);
+
 #endif /** BOTAN_UTIL_H_ @}*/