openssl: Move shared secret calculation to get_shared_secret()

This is a change from the multi-KE branch.
This commit is contained in:
Tobias Brunner
2022-04-14 19:05:44 +02:00
parent 94a3563c68
commit 1c1213f4b6
4 changed files with 51 additions and 73 deletions
@@ -63,11 +63,6 @@ struct private_openssl_diffie_hellman_t {
* Shared secret
*/
chunk_t shared_secret;
/**
* True if shared secret is computed
*/
bool computed;
};
METHOD(diffie_hellman_t, get_my_public_value, bool,
@@ -85,15 +80,24 @@ METHOD(diffie_hellman_t, get_my_public_value, bool,
METHOD(diffie_hellman_t, get_shared_secret, bool,
private_openssl_diffie_hellman_t *this, chunk_t *secret)
{
if (!this->computed)
int len;
if (!this->shared_secret.len)
{
return FALSE;
this->shared_secret = chunk_alloc(DH_size(this->dh));
memset(this->shared_secret.ptr, 0xFF, this->shared_secret.len);
len = DH_compute_key(this->shared_secret.ptr, this->pub_key, this->dh);
if (len < 0)
{
DBG1(DBG_LIB, "DH shared secret computation failed");
chunk_clear(&this->shared_secret);
return FALSE;
}
this->shared_secret.len = len;
}
/* shared secret should requires a len according the DH group */
*secret = chunk_alloc(DH_size(this->dh));
memset(secret->ptr, 0, secret->len);
memcpy(secret->ptr + secret->len - this->shared_secret.len,
this->shared_secret.ptr, this->shared_secret.len);
/* shared secret requires a length according to the DH group */
*secret = chunk_copy_pad(chunk_alloc(DH_size(this->dh)),
this->shared_secret, 0);
return TRUE;
}
@@ -101,25 +105,16 @@ METHOD(diffie_hellman_t, get_shared_secret, bool,
METHOD(diffie_hellman_t, set_other_public_value, bool,
private_openssl_diffie_hellman_t *this, chunk_t value)
{
int len;
if (!diffie_hellman_verify_value(this->group, value))
{
return FALSE;
}
BN_bin2bn(value.ptr, value.len, this->pub_key);
chunk_clear(&this->shared_secret);
this->shared_secret.ptr = malloc(DH_size(this->dh));
memset(this->shared_secret.ptr, 0xFF, this->shared_secret.len);
len = DH_compute_key(this->shared_secret.ptr, this->pub_key, this->dh);
if (len < 0)
if (!BN_bin2bn(value.ptr, value.len, this->pub_key))
{
DBG1(DBG_LIB, "DH shared secret computation failed");
return FALSE;
}
this->shared_secret.len = len;
this->computed = TRUE;
chunk_clear(&this->shared_secret);
return TRUE;
}
@@ -136,7 +131,6 @@ METHOD(diffie_hellman_t, set_private_value, bool,
return FALSE;
}
chunk_clear(&this->shared_secret);
this->computed = FALSE;
return DH_generate_key(this->dh);
}
return FALSE;
@@ -220,9 +214,7 @@ openssl_diffie_hellman_t *openssl_diffie_hellman_create(
}
this->group = group;
this->computed = FALSE;
this->pub_key = BN_new();
this->shared_secret = chunk_empty;
if (group == MODP_CUSTOM)
{