diffie-hellman: Add a bool return value to set_other_public_value()

This commit is contained in:
Martin Willi
2015-03-23 17:54:03 +01:00
parent 42431690e0
commit a777155ffe
19 changed files with 123 additions and 55 deletions
+3 -2
View File
@@ -73,7 +73,7 @@ struct private_gcrypt_dh_t {
size_t p_len;
};
METHOD(diffie_hellman_t, set_other_public_value, void,
METHOD(diffie_hellman_t, set_other_public_value, bool,
private_gcrypt_dh_t *this, chunk_t value)
{
gcry_mpi_t p_min_1;
@@ -88,7 +88,7 @@ METHOD(diffie_hellman_t, set_other_public_value, void,
if (err)
{
DBG1(DBG_LIB, "importing mpi yb failed: %s", gpg_strerror(err));
return;
return FALSE;
}
p_min_1 = gcry_mpi_new(this->p_len * 8);
@@ -112,6 +112,7 @@ METHOD(diffie_hellman_t, set_other_public_value, void,
" y < 2 || y > p - 1 ");
}
gcry_mpi_release(p_min_1);
return this->zz != NULL;
}
/**
@@ -85,7 +85,7 @@ struct private_gmp_diffie_hellman_t {
bool computed;
};
METHOD(diffie_hellman_t, set_other_public_value, void,
METHOD(diffie_hellman_t, set_other_public_value, bool,
private_gmp_diffie_hellman_t *this, chunk_t value)
{
mpz_t p_min_1;
@@ -142,6 +142,7 @@ METHOD(diffie_hellman_t, set_other_public_value, void,
" y < 2 || y > p - 1 ");
}
mpz_clear(p_min_1);
return this->computed;
}
METHOD(diffie_hellman_t, get_my_public_value, bool,
+12 -13
View File
@@ -106,7 +106,7 @@ struct private_ntru_ke_t {
/**
* Deterministic Random Bit Generator
*/
ntru_drbg_t *drbg;
ntru_drbg_t *drbg;
};
METHOD(diffie_hellman_t, get_my_public_value, bool,
@@ -153,8 +153,7 @@ METHOD(diffie_hellman_t, get_shared_secret, bool,
return TRUE;
}
METHOD(diffie_hellman_t, set_other_public_value, void,
METHOD(diffie_hellman_t, set_other_public_value, bool,
private_ntru_ke_t *this, chunk_t value)
{
if (this->privkey)
@@ -163,15 +162,15 @@ METHOD(diffie_hellman_t, set_other_public_value, void,
if (value.len == 0)
{
DBG1(DBG_LIB, "empty NTRU ciphertext");
return;
return FALSE;
}
DBG3(DBG_LIB, "NTRU ciphertext: %B", &value);
/* decrypt the shared secret */
if (!this->privkey->decrypt(this->privkey, value, &this->shared_secret))
if (!this->privkey->decrypt(this->privkey, value, &this->shared_secret))
{
DBG1(DBG_LIB, "NTRU decryption of shared secret failed");
return;
return FALSE;
}
this->computed = TRUE;
}
@@ -186,13 +185,13 @@ METHOD(diffie_hellman_t, set_other_public_value, void,
pubkey = ntru_public_key_create_from_data(this->drbg, value);
if (!pubkey)
{
return;
return FALSE;
}
if (pubkey->get_id(pubkey) != this->param_set->id)
{
DBG1(DBG_LIB, "received NTRU public key with wrong OUI");
pubkey->destroy(pubkey);
return;
return FALSE;
}
this->pubkey = pubkey;
@@ -205,7 +204,7 @@ METHOD(diffie_hellman_t, set_other_public_value, void,
{
DBG1(DBG_LIB, "generation of shared secret failed");
chunk_free(&this->shared_secret);
return;
return FALSE;
}
this->computed = TRUE;
@@ -213,10 +212,11 @@ METHOD(diffie_hellman_t, set_other_public_value, void,
if (!pubkey->encrypt(pubkey, this->shared_secret, &this->ciphertext))
{
DBG1(DBG_LIB, "NTRU encryption of shared secret failed");
return;
return FALSE;
}
DBG3(DBG_LIB, "NTRU ciphertext: %B", &this->ciphertext);
}
return this->computed;
}
METHOD(diffie_hellman_t, get_dh_group, diffie_hellman_group_t,
@@ -302,10 +302,10 @@ ntru_ke_t *ntru_ke_create(diffie_hellman_group_t group, chunk_t g, chunk_t p)
drbg = ntru_drbg_create(strength, chunk_from_str("IKE NTRU-KE"), entropy);
if (!drbg)
{
{
DBG1(DBG_LIB, "could not instantiate DRBG at %u bit security", strength);
entropy->destroy(entropy);
return NULL;
return NULL;
}
INIT(this,
@@ -327,4 +327,3 @@ ntru_ke_t *ntru_ke_create(diffie_hellman_group_t group, chunk_t g, chunk_t p)
return &this->public;
}
@@ -87,7 +87,7 @@ METHOD(diffie_hellman_t, get_shared_secret, bool,
}
METHOD(diffie_hellman_t, set_other_public_value, void,
METHOD(diffie_hellman_t, set_other_public_value, bool,
private_openssl_diffie_hellman_t *this, chunk_t value)
{
int len;
@@ -100,10 +100,11 @@ METHOD(diffie_hellman_t, set_other_public_value, void,
if (len < 0)
{
DBG1(DBG_LIB, "DH shared secret computation failed");
return;
return FALSE;
}
this->shared_secret.len = len;
this->computed = TRUE;
return TRUE;
}
METHOD(diffie_hellman_t, get_dh_group, diffie_hellman_group_t,
@@ -216,23 +216,24 @@ error:
return ret;
}
METHOD(diffie_hellman_t, set_other_public_value, void,
METHOD(diffie_hellman_t, set_other_public_value, bool,
private_openssl_ec_diffie_hellman_t *this, chunk_t value)
{
if (!chunk2ecp(this->ec_group, value, this->pub_key))
{
DBG1(DBG_LIB, "ECDH public value is malformed");
return;
return FALSE;
}
chunk_clear(&this->shared_secret);
if (!compute_shared_key(this, &this->shared_secret)) {
DBG1(DBG_LIB, "ECDH shared secret computation failed");
return;
return FALSE;
}
this->computed = TRUE;
return TRUE;
}
METHOD(diffie_hellman_t, get_my_public_value, bool,
+7 -7
View File
@@ -81,7 +81,7 @@ struct private_pkcs11_dh_t {
*
* If this succeeds the shared secret is stored in this->secret.
*/
static void derive_secret(private_pkcs11_dh_t *this, chunk_t other)
static bool derive_secret(private_pkcs11_dh_t *this, chunk_t other)
{
CK_OBJECT_CLASS klass = CKO_SECRET_KEY;
CK_KEY_TYPE type = CKK_GENERIC_SECRET;
@@ -102,17 +102,18 @@ static void derive_secret(private_pkcs11_dh_t *this, chunk_t other)
if (rv != CKR_OK)
{
DBG1(DBG_CFG, "C_DeriveKey() error: %N", ck_rv_names, rv);
return;
return FALSE;
}
if (!this->lib->get_ck_attribute(this->lib, this->session, secret,
CKA_VALUE, &this->secret))
{
chunk_free(&this->secret);
return;
return FALSE;
}
return TRUE;
}
METHOD(diffie_hellman_t, set_other_public_value, void,
METHOD(diffie_hellman_t, set_other_public_value, bool,
private_pkcs11_dh_t *this, chunk_t value)
{
switch (this->group)
@@ -137,7 +138,7 @@ METHOD(diffie_hellman_t, set_other_public_value, void,
if (!lib->settings->get_bool(lib->settings,
"%s.ecp_x_coordinate_only", TRUE, lib->ns))
{ /* we only get the x coordinate back */
return;
return FALSE;
}
value = chunk_from_thing(params);
break;
@@ -145,7 +146,7 @@ METHOD(diffie_hellman_t, set_other_public_value, void,
default:
break;
}
derive_secret(this, value);
return derive_secret(this, value);
}
METHOD(diffie_hellman_t, get_my_public_value, bool,
@@ -444,4 +445,3 @@ pkcs11_dh_t *pkcs11_dh_create(diffie_hellman_group_t group,
}
return NULL;
}