]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
botan: Move shared secret calculation to get_shared_secret()
authorTobias Brunner <tobias@strongswan.org>
Fri, 23 Jul 2021 12:53:15 +0000 (14:53 +0200)
committerTobias Brunner <tobias@strongswan.org>
Wed, 29 Jun 2022 08:28:50 +0000 (10:28 +0200)
src/libstrongswan/plugins/botan/botan_diffie_hellman.c
src/libstrongswan/plugins/botan/botan_ec_diffie_hellman.c
src/libstrongswan/plugins/botan/botan_x25519.c

index 4b541758589fd3f0d379389952c539846c9d659b..cacc220d650ce6b397c6267216b7cf47d6872b18 100644 (file)
@@ -54,7 +54,12 @@ struct private_botan_diffie_hellman_t {
        /**
         * Private key
         */
-       botan_privkey_t dh_key;
+       botan_privkey_t key;
+
+       /**
+        * Public key value provided by peer
+        */
+       chunk_t pubkey;
 
        /**
         * Diffie hellman shared secret
@@ -84,8 +89,8 @@ bool load_private_key(private_botan_diffie_hellman_t *this, chunk_t value)
                return FALSE;
        }
 
-       if (botan_privkey_destroy(this->dh_key) ||
-               botan_privkey_load_dh(&this->dh_key, this->p, this->g, xa))
+       if (botan_privkey_destroy(this->key) ||
+               botan_privkey_load_dh(&this->key, this->p, this->g, xa))
        {
                botan_mp_destroy(xa);
                return FALSE;
@@ -102,9 +107,9 @@ METHOD(key_exchange_t, set_public_key, bool,
                return FALSE;
        }
 
-       chunk_clear(&this->shared_secret);
-
-       return botan_dh_key_derivation(this->dh_key, value, &this->shared_secret);
+       chunk_clear(&this->pubkey);
+       this->pubkey = chunk_clone(value);
+       return TRUE;
 }
 
 METHOD(key_exchange_t, get_public_key, bool,
@@ -113,14 +118,14 @@ METHOD(key_exchange_t, get_public_key, bool,
        *value = chunk_empty;
 
        /* get key size of public key first */
-       if (botan_pk_op_key_agreement_export_public(this->dh_key, NULL, &value->len)
+       if (botan_pk_op_key_agreement_export_public(this->key, NULL, &value->len)
                != BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE)
        {
                return FALSE;
        }
 
        *value = chunk_alloc(value->len);
-       if (botan_pk_op_key_agreement_export_public(this->dh_key, value->ptr,
+       if (botan_pk_op_key_agreement_export_public(this->key, value->ptr,
                                                                                                &value->len))
        {
                chunk_clear(value);
@@ -139,7 +144,8 @@ METHOD(key_exchange_t, set_private_key, bool,
 METHOD(key_exchange_t, get_shared_secret, bool,
        private_botan_diffie_hellman_t *this, chunk_t *secret)
 {
-       if (!this->shared_secret.len)
+       if (!this->shared_secret.len &&
+               !botan_dh_key_derivation(this->key, this->pubkey, &this->shared_secret))
        {
                return FALSE;
        }
@@ -158,8 +164,9 @@ METHOD(key_exchange_t, destroy, void,
 {
        botan_mp_destroy(this->p);
        botan_mp_destroy(this->g);
-       botan_privkey_destroy(this->dh_key);
+       botan_privkey_destroy(this->key);
        chunk_clear(&this->shared_secret);
+       chunk_clear(&this->pubkey);
        free(this);
 }
 
index 871e58c565ddb566d770672906755ca03400e457..5ebaca4c44580241dbe87038fc31afe7e5223c60 100644 (file)
@@ -60,6 +60,11 @@ struct private_botan_ec_diffie_hellman_t {
         */
        botan_privkey_t key;
 
+       /**
+        * Public key value provided by peer
+        */
+       chunk_t pubkey;
+
        /**
         * Shared secret
         */
@@ -74,12 +79,10 @@ METHOD(key_exchange_t, set_public_key, bool,
                return FALSE;
        }
 
-       chunk_clear(&this->shared_secret);
-
+       chunk_clear(&this->pubkey);
        /* prepend 0x04 to indicate uncompressed point format */
-       value = chunk_cata("cc", chunk_from_chars(0x04), value);
-
-       return botan_dh_key_derivation(this->key, value, &this->shared_secret);
+       this->pubkey = chunk_cat("cc", chunk_from_chars(0x04), value);
+       return TRUE;
 }
 
 METHOD(key_exchange_t, get_public_key, bool,
@@ -135,7 +138,8 @@ METHOD(key_exchange_t, set_private_key, bool,
 METHOD(key_exchange_t, get_shared_secret, bool,
        private_botan_ec_diffie_hellman_t *this, chunk_t *secret)
 {
-       if (!this->shared_secret.len)
+       if (!this->shared_secret.len &&
+               !botan_dh_key_derivation(this->key, this->pubkey, &this->shared_secret))
        {
                return FALSE;
        }
@@ -154,6 +158,7 @@ METHOD(key_exchange_t, destroy, void,
 {
        botan_privkey_destroy(this->key);
        chunk_clear(&this->shared_secret);
+       chunk_clear(&this->pubkey);
        free(this);
 }
 
index 184ce2e8da8f612f627e259402be1298028e407a..8323e555dc51c4cbda097981e5990c1aaea59cc7 100644 (file)
@@ -49,6 +49,11 @@ struct private_diffie_hellman_t {
         */
        botan_privkey_t key;
 
+       /**
+        * Public key value provided by peer
+        */
+       chunk_t pubkey;
+
        /**
         * Shared secret
         */
@@ -63,9 +68,9 @@ METHOD(key_exchange_t, set_public_key, bool,
                return FALSE;
        }
 
-       chunk_clear(&this->shared_secret);
-
-       return botan_dh_key_derivation(this->key, value, &this->shared_secret);
+       chunk_clear(&this->pubkey);
+       this->pubkey = chunk_clone(value);
+       return TRUE;
 }
 
 METHOD(key_exchange_t, get_public_key, bool,
@@ -113,7 +118,8 @@ METHOD(key_exchange_t, set_private_key, bool,
 METHOD(key_exchange_t, get_shared_secret, bool,
        private_diffie_hellman_t *this, chunk_t *secret)
 {
-       if (!this->shared_secret.len)
+       if (!this->shared_secret.len &&
+               !botan_dh_key_derivation(this->key, this->pubkey, &this->shared_secret))
        {
                return FALSE;
        }
@@ -132,6 +138,7 @@ METHOD(key_exchange_t, destroy, void,
 {
        botan_privkey_destroy(this->key);
        chunk_clear(&this->shared_secret);
+       chunk_clear(&this->pubkey);
        free(this);
 }