Rename diffie_hellman_t to key_exchange_t and change the interface etc.

This makes it more generic so we can use it for QSKE methods.
This commit is contained in:
Tobias Brunner
2022-06-29 10:28:50 +02:00
parent ec95fd9b93
commit 3af7c6db87
130 changed files with 1379 additions and 1384 deletions
@@ -54,7 +54,7 @@ struct private_openssl_diffie_hellman_t {
/**
* Diffie Hellman group number.
*/
diffie_hellman_group_t group;
key_exchange_method_t group;
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
/**
@@ -84,13 +84,13 @@ struct private_openssl_diffie_hellman_t {
chunk_t shared_secret;
};
METHOD(diffie_hellman_t, get_dh_group, diffie_hellman_group_t,
METHOD(key_exchange_t, get_method, key_exchange_method_t,
private_openssl_diffie_hellman_t *this)
{
return this->group;
}
METHOD(diffie_hellman_t, get_my_public_value, bool,
METHOD(key_exchange_t, get_public_key, bool,
private_openssl_diffie_hellman_t *this, chunk_t *value)
{
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
@@ -115,7 +115,7 @@ METHOD(diffie_hellman_t, get_my_public_value, bool,
#endif
}
METHOD(diffie_hellman_t, get_shared_secret, bool,
METHOD(key_exchange_t, get_shared_secret, bool,
private_openssl_diffie_hellman_t *this, chunk_t *secret)
{
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
@@ -151,10 +151,10 @@ METHOD(diffie_hellman_t, get_shared_secret, bool,
}
METHOD(diffie_hellman_t, set_other_public_value, bool,
METHOD(key_exchange_t, set_public_key, bool,
private_openssl_diffie_hellman_t *this, chunk_t value)
{
if (!diffie_hellman_verify_value(this->group, value))
if (!key_exchange_verify_pubkey(this->group, value))
{
return FALSE;
}
@@ -204,7 +204,7 @@ static BIGNUM *calculate_public_key(BIGNUM *priv, const BIGNUM *g,
return pub;
}
METHOD(diffie_hellman_t, set_private_value, bool,
METHOD(key_exchange_t, set_private_key, bool,
private_openssl_diffie_hellman_t *this, chunk_t value)
{
BIGNUM *priv, *g = NULL, *p = NULL, *pub = NULL;
@@ -254,7 +254,7 @@ error:
#else /* OPENSSL_VERSION_NUMBER */
METHOD(diffie_hellman_t, set_private_value, bool,
METHOD(key_exchange_t, set_private_key, bool,
private_openssl_diffie_hellman_t *this, chunk_t value)
{
BIGNUM *privkey;
@@ -274,7 +274,7 @@ METHOD(diffie_hellman_t, set_private_value, bool,
#endif /* OPENSSL_VERSION_NUMBER */
METHOD(diffie_hellman_t, destroy, void,
METHOD(key_exchange_t, destroy, void,
private_openssl_diffie_hellman_t *this)
{
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
@@ -292,7 +292,7 @@ METHOD(diffie_hellman_t, destroy, void,
* Described in header.
*/
openssl_diffie_hellman_t *openssl_diffie_hellman_create(
diffie_hellman_group_t group, ...)
key_exchange_method_t group, ...)
{
private_openssl_diffie_hellman_t *this;
BIGNUM *g, *p;
@@ -300,12 +300,12 @@ openssl_diffie_hellman_t *openssl_diffie_hellman_create(
INIT(this,
.public = {
.dh = {
.ke = {
.get_shared_secret = _get_shared_secret,
.set_other_public_value = _set_other_public_value,
.get_my_public_value = _get_my_public_value,
.set_private_value = _set_private_value,
.get_dh_group = _get_dh_group,
.set_public_key = _set_public_key,
.get_public_key = _get_public_key,
.set_private_key = _set_private_key,
.get_method = _get_method,
.destroy = _destroy,
},
},
@@ -32,20 +32,20 @@ typedef struct openssl_diffie_hellman_t openssl_diffie_hellman_t;
struct openssl_diffie_hellman_t {
/**
* Implements diffie_hellman_t interface.
* Implements key_exchange_t interface.
*/
diffie_hellman_t dh;
key_exchange_t ke;
};
/**
* Creates a new openssl_diffie_hellman_t object.
*
* @param group Diffie Hellman group number to use
* @param group Diffie-Hellman group number to use
* @param ... expects generator and prime as chunk_t if MODP_CUSTOM
* @return openssl_diffie_hellman_t object, NULL if not supported
*/
openssl_diffie_hellman_t *openssl_diffie_hellman_create(
diffie_hellman_group_t group, ...);
key_exchange_method_t group, ...);
#endif /** OPENSSL_DIFFIE_HELLMAN_H_ @}*/
@@ -54,7 +54,7 @@ struct private_openssl_ec_diffie_hellman_t {
/**
* Diffie Hellman group number.
*/
diffie_hellman_group_t group;
key_exchange_method_t group;
/**
* EC private (public) key
@@ -198,10 +198,10 @@ error:
}
#endif /* OPENSSL_VERSION_NUMBER < ... */
METHOD(diffie_hellman_t, set_other_public_value, bool,
METHOD(key_exchange_t, set_public_key, bool,
private_openssl_ec_diffie_hellman_t *this, chunk_t value)
{
if (!diffie_hellman_verify_value(this->group, value))
if (!key_exchange_verify_pubkey(this->group, value))
{
return FALSE;
}
@@ -232,7 +232,7 @@ METHOD(diffie_hellman_t, set_other_public_value, bool,
return TRUE;
}
METHOD(diffie_hellman_t, get_my_public_value, bool,
METHOD(key_exchange_t, get_public_key, bool,
private_openssl_ec_diffie_hellman_t *this, chunk_t *value)
{
#if OPENSSL_VERSION_NUMBER < 0x1010000fL
@@ -253,7 +253,7 @@ METHOD(diffie_hellman_t, get_my_public_value, bool,
#endif
}
METHOD(diffie_hellman_t, get_shared_secret, bool,
METHOD(key_exchange_t, get_shared_secret, bool,
private_openssl_ec_diffie_hellman_t *this, chunk_t *secret)
{
if (!this->shared_secret.len &&
@@ -266,7 +266,7 @@ METHOD(diffie_hellman_t, get_shared_secret, bool,
return TRUE;
}
METHOD(diffie_hellman_t, get_dh_group, diffie_hellman_group_t,
METHOD(key_exchange_t, get_method, key_exchange_method_t,
private_openssl_ec_diffie_hellman_t *this)
{
return this->group;
@@ -275,7 +275,7 @@ METHOD(diffie_hellman_t, get_dh_group, diffie_hellman_group_t,
/*
* Described in header
*/
int openssl_ecdh_group_to_nid(diffie_hellman_group_t group)
int openssl_ecdh_group_to_nid(key_exchange_method_t group)
{
switch (group)
{
@@ -337,7 +337,7 @@ static bool ecp2chunk(EC_GROUP *group, EC_POINT *point, chunk_t *chunk)
return chunk->len;
}
METHOD(diffie_hellman_t, set_private_value, bool,
METHOD(key_exchange_t, set_private_key, bool,
private_openssl_ec_diffie_hellman_t *this, chunk_t value)
{
BIGNUM *priv = NULL;
@@ -390,7 +390,7 @@ METHOD(diffie_hellman_t, set_private_value, bool,
#else /* OPENSSL_VERSION_NUMBER */
METHOD(diffie_hellman_t, set_private_value, bool,
METHOD(key_exchange_t, set_private_key, bool,
private_openssl_ec_diffie_hellman_t *this, chunk_t value)
{
EC_KEY *key = NULL;
@@ -430,7 +430,7 @@ error:
#endif /* OPENSSL_VERSION_NUMBER */
METHOD(diffie_hellman_t, destroy, void,
METHOD(key_exchange_t, destroy, void,
private_openssl_ec_diffie_hellman_t *this)
{
EC_GROUP_free(this->ec_group);
@@ -443,7 +443,7 @@ METHOD(diffie_hellman_t, destroy, void,
/*
* Described in header
*/
openssl_ec_diffie_hellman_t *openssl_ec_diffie_hellman_create(diffie_hellman_group_t group)
openssl_ec_diffie_hellman_t *openssl_ec_diffie_hellman_create(key_exchange_method_t group)
{
private_openssl_ec_diffie_hellman_t *this;
int curve;
@@ -456,12 +456,12 @@ openssl_ec_diffie_hellman_t *openssl_ec_diffie_hellman_create(diffie_hellman_gro
INIT(this,
.public = {
.dh = {
.ke = {
.get_shared_secret = _get_shared_secret,
.set_other_public_value = _set_other_public_value,
.get_my_public_value = _get_my_public_value,
.set_private_value = _set_private_value,
.get_dh_group = _get_dh_group,
.set_public_key = _set_public_key,
.get_public_key = _get_public_key,
.set_private_key = _set_private_key,
.get_method = _get_method,
.destroy = _destroy,
},
},
@@ -32,18 +32,19 @@ typedef struct openssl_ec_diffie_hellman_t openssl_ec_diffie_hellman_t;
struct openssl_ec_diffie_hellman_t {
/**
* Implements diffie_hellman_t interface.
* Implements key_exchange_t interface.
*/
diffie_hellman_t dh;
key_exchange_t ke;
};
/**
* Creates a new openssl_ec_diffie_hellman_t object.
*
* @param group EC Diffie Hellman group number to use
* @return openssl_ec_diffie_hellman_t object, NULL if not supported
* @param group EC Diffie-Hellman group number to use
* @return openssl_ec_diffie_hellman_t object, NULL if unsupported
*/
openssl_ec_diffie_hellman_t *openssl_ec_diffie_hellman_create(diffie_hellman_group_t group);
openssl_ec_diffie_hellman_t *openssl_ec_diffie_hellman_create(
key_exchange_method_t group);
/**
* Map ECDH groups to OpenSSL NIDs for the ECC curve.
@@ -51,6 +52,6 @@ openssl_ec_diffie_hellman_t *openssl_ec_diffie_hellman_create(diffie_hellman_gro
* @param group ECDH group
* @return NID for the curve
*/
int openssl_ecdh_group_to_nid(diffie_hellman_group_t group);
int openssl_ecdh_group_to_nid(key_exchange_method_t group);
#endif /** OPENSSL_EC_DIFFIE_HELLMAN_H_ @}*/
@@ -326,7 +326,7 @@ METHOD(plugin_t, get_name, char*,
* Check if the given DH group is in the list of supported curves.
*/
static bool ecdh_group_supported(EC_builtin_curve *curves, size_t num_curves,
diffie_hellman_group_t group)
key_exchange_method_t group)
{
int j;
@@ -360,7 +360,7 @@ static void add_ecdh_features(plugin_feature_t *features,
for (i = 0; i < count; i++)
{
if (to_add[i].kind != FEATURE_PROVIDE ||
ecdh_group_supported(curves, num_curves, to_add[i].arg.dh_group))
ecdh_group_supported(curves, num_curves, to_add[i].arg.ke))
{
features[(*pos)++] = to_add[i];
}
@@ -528,19 +528,19 @@ METHOD(plugin_t, get_features, int,
#endif /* OPENSSL_VERSION_NUMBER */
#ifndef OPENSSL_NO_DH
/* MODP DH groups */
PLUGIN_REGISTER(DH, openssl_diffie_hellman_create),
PLUGIN_PROVIDE(DH, MODP_3072_BIT),
PLUGIN_PROVIDE(DH, MODP_4096_BIT),
PLUGIN_PROVIDE(DH, MODP_6144_BIT),
PLUGIN_PROVIDE(DH, MODP_8192_BIT),
PLUGIN_PROVIDE(DH, MODP_2048_BIT),
PLUGIN_PROVIDE(DH, MODP_2048_224),
PLUGIN_PROVIDE(DH, MODP_2048_256),
PLUGIN_PROVIDE(DH, MODP_1536_BIT),
PLUGIN_PROVIDE(DH, MODP_1024_BIT),
PLUGIN_PROVIDE(DH, MODP_1024_160),
PLUGIN_PROVIDE(DH, MODP_768_BIT),
PLUGIN_PROVIDE(DH, MODP_CUSTOM),
PLUGIN_REGISTER(KE, openssl_diffie_hellman_create),
PLUGIN_PROVIDE(KE, MODP_3072_BIT),
PLUGIN_PROVIDE(KE, MODP_4096_BIT),
PLUGIN_PROVIDE(KE, MODP_6144_BIT),
PLUGIN_PROVIDE(KE, MODP_8192_BIT),
PLUGIN_PROVIDE(KE, MODP_2048_BIT),
PLUGIN_PROVIDE(KE, MODP_2048_224),
PLUGIN_PROVIDE(KE, MODP_2048_256),
PLUGIN_PROVIDE(KE, MODP_1536_BIT),
PLUGIN_PROVIDE(KE, MODP_1024_BIT),
PLUGIN_PROVIDE(KE, MODP_1024_160),
PLUGIN_PROVIDE(KE, MODP_768_BIT),
PLUGIN_PROVIDE(KE, MODP_CUSTOM),
#endif
#ifndef OPENSSL_NO_RSA
/* RSA private/public key loading */
@@ -649,11 +649,11 @@ METHOD(plugin_t, get_features, int,
#endif
#endif /* OPENSSL_NO_ECDSA */
#if OPENSSL_VERSION_NUMBER >= 0x1010100fL && !defined(OPENSSL_NO_ECDH)
PLUGIN_REGISTER(DH, openssl_x_diffie_hellman_create),
PLUGIN_REGISTER(KE, openssl_x_diffie_hellman_create),
/* available since 1.1.0a, but we require 1.1.1 features */
PLUGIN_PROVIDE(DH, CURVE_25519),
PLUGIN_PROVIDE(KE, CURVE_25519),
/* available since 1.1.1 */
PLUGIN_PROVIDE(DH, CURVE_448),
PLUGIN_PROVIDE(KE, CURVE_448),
#endif /* OPENSSL_VERSION_NUMBER && !OPENSSL_NO_ECDH */
#if OPENSSL_VERSION_NUMBER >= 0x1010100fL && !defined(OPENSSL_NO_EC)
/* EdDSA private/public key loading */
@@ -686,17 +686,17 @@ METHOD(plugin_t, get_features, int,
static plugin_feature_t f_ecdh[] = {
#ifndef OPENSSL_NO_ECDH
/* EC DH groups */
PLUGIN_REGISTER(DH, openssl_ec_diffie_hellman_create),
PLUGIN_PROVIDE(DH, ECP_256_BIT),
PLUGIN_PROVIDE(DH, ECP_384_BIT),
PLUGIN_PROVIDE(DH, ECP_521_BIT),
PLUGIN_PROVIDE(DH, ECP_224_BIT),
PLUGIN_PROVIDE(DH, ECP_192_BIT),
PLUGIN_REGISTER(KE, openssl_ec_diffie_hellman_create),
PLUGIN_PROVIDE(KE, ECP_256_BIT),
PLUGIN_PROVIDE(KE, ECP_384_BIT),
PLUGIN_PROVIDE(KE, ECP_521_BIT),
PLUGIN_PROVIDE(KE, ECP_224_BIT),
PLUGIN_PROVIDE(KE, ECP_192_BIT),
#if OPENSSL_VERSION_NUMBER >= 0x10002000L
PLUGIN_PROVIDE(DH, ECP_256_BP),
PLUGIN_PROVIDE(DH, ECP_384_BP),
PLUGIN_PROVIDE(DH, ECP_512_BP),
PLUGIN_PROVIDE(DH, ECP_224_BP),
PLUGIN_PROVIDE(KE, ECP_256_BP),
PLUGIN_PROVIDE(KE, ECP_384_BP),
PLUGIN_PROVIDE(KE, ECP_512_BP),
PLUGIN_PROVIDE(KE, ECP_224_BP),
#endif /* OPENSSL_VERSION_NUMBER */
#endif /* OPENSSL_NO_ECDH */
};
@@ -25,21 +25,21 @@
#include <utils/debug.h>
typedef struct private_diffie_hellman_t private_diffie_hellman_t;
typedef struct private_key_exchange_t private_key_exchange_t;
/**
* Private data
*/
struct private_diffie_hellman_t {
struct private_key_exchange_t {
/**
* Public interface.
*/
diffie_hellman_t public;
key_exchange_t public;
/**
* Diffie Hellman group number.
* Key exchange method.
*/
diffie_hellman_group_t group;
key_exchange_method_t ke;
/**
* Private (public) key
@@ -58,11 +58,11 @@ struct private_diffie_hellman_t {
};
/**
* Map a DH group to a key type
* Map a key exchange method to a key type
*/
static int map_key_type(diffie_hellman_group_t group)
static int map_key_type(key_exchange_method_t ke)
{
switch (group)
switch (ke)
{
case CURVE_25519:
return EVP_PKEY_X25519;
@@ -73,29 +73,29 @@ static int map_key_type(diffie_hellman_group_t group)
}
}
METHOD(diffie_hellman_t, set_other_public_value, bool,
private_diffie_hellman_t *this, chunk_t value)
METHOD(key_exchange_t, set_public_key, bool,
private_key_exchange_t *this, chunk_t value)
{
if (!diffie_hellman_verify_value(this->group, value))
if (!key_exchange_verify_pubkey(this->ke, value))
{
return FALSE;
}
EVP_PKEY_free(this->pub);
this->pub = EVP_PKEY_new_raw_public_key(map_key_type(this->group), NULL,
this->pub = EVP_PKEY_new_raw_public_key(map_key_type(this->ke), NULL,
value.ptr, value.len);
if (!this->pub)
{
DBG1(DBG_LIB, "%N public value is malformed",
diffie_hellman_group_names, this->group);
key_exchange_method_names, this->ke);
return FALSE;
}
chunk_clear(&this->shared_secret);
return TRUE;
}
METHOD(diffie_hellman_t, get_my_public_value, bool,
private_diffie_hellman_t *this, chunk_t *value)
METHOD(key_exchange_t, get_public_key, bool,
private_key_exchange_t *this, chunk_t *value)
{
size_t len;
@@ -114,11 +114,11 @@ METHOD(diffie_hellman_t, get_my_public_value, bool,
return TRUE;
}
METHOD(diffie_hellman_t, set_private_value, bool,
private_diffie_hellman_t *this, chunk_t value)
METHOD(key_exchange_t, set_private_key, bool,
private_key_exchange_t *this, chunk_t value)
{
EVP_PKEY_free(this->key);
this->key = EVP_PKEY_new_raw_private_key(map_key_type(this->group), NULL,
this->key = EVP_PKEY_new_raw_private_key(map_key_type(this->ke), NULL,
value.ptr, value.len);
if (!this->key)
{
@@ -127,28 +127,28 @@ METHOD(diffie_hellman_t, set_private_value, bool,
return TRUE;
}
METHOD(diffie_hellman_t, get_shared_secret, bool,
private_diffie_hellman_t *this, chunk_t *secret)
METHOD(key_exchange_t, get_shared_secret, bool,
private_key_exchange_t *this, chunk_t *secret)
{
if (!this->shared_secret.len &&
!openssl_compute_shared_key(this->key, this->pub, &this->shared_secret))
{
DBG1(DBG_LIB, "%N shared secret computation failed",
diffie_hellman_group_names, this->group);
key_exchange_method_names, this->ke);
return FALSE;
}
*secret = chunk_clone(this->shared_secret);
return TRUE;
}
METHOD(diffie_hellman_t, get_dh_group, diffie_hellman_group_t,
private_diffie_hellman_t *this)
METHOD(key_exchange_t, get_method, key_exchange_method_t,
private_key_exchange_t *this)
{
return this->group;
return this->ke;
}
METHOD(diffie_hellman_t, destroy, void,
private_diffie_hellman_t *this)
METHOD(key_exchange_t, destroy, void,
private_key_exchange_t *this)
{
EVP_PKEY_free(this->key);
EVP_PKEY_free(this->pub);
@@ -159,13 +159,13 @@ METHOD(diffie_hellman_t, destroy, void,
/*
* Described in header
*/
diffie_hellman_t *openssl_x_diffie_hellman_create(diffie_hellman_group_t group)
key_exchange_t *openssl_x_diffie_hellman_create(key_exchange_method_t ke)
{
private_diffie_hellman_t *this;
private_key_exchange_t *this;
EVP_PKEY_CTX *ctx = NULL;
EVP_PKEY *key = NULL;
switch (group)
switch (ke)
{
case CURVE_25519:
ctx = EVP_PKEY_CTX_new_id(NID_X25519, NULL);
@@ -182,7 +182,7 @@ diffie_hellman_t *openssl_x_diffie_hellman_create(diffie_hellman_group_t group)
EVP_PKEY_keygen(ctx, &key) <= 0)
{
DBG1(DBG_LIB, "generating key for %N failed",
diffie_hellman_group_names, group);
key_exchange_method_names, ke);
EVP_PKEY_CTX_free(ctx);
return NULL;
}
@@ -191,13 +191,13 @@ diffie_hellman_t *openssl_x_diffie_hellman_create(diffie_hellman_group_t group)
INIT(this,
.public = {
.get_shared_secret = _get_shared_secret,
.set_other_public_value = _set_other_public_value,
.get_my_public_value = _get_my_public_value,
.set_private_value = _set_private_value,
.get_dh_group = _get_dh_group,
.set_public_key = _set_public_key,
.get_public_key = _get_public_key,
.set_private_key = _set_private_key,
.get_method = _get_method,
.destroy = _destroy,
},
.group = group,
.ke = ke,
.key = key,
);
return &this->public;
@@ -27,12 +27,12 @@
#include <library.h>
/**
* Creates a new diffie_hellman_t object.
* Creates a new key_exchange_t object.
*
* @param group Diffie Hellman group number to use
* @param ke key exchange method to use
* @return object, NULL if not supported
*/
diffie_hellman_t *openssl_x_diffie_hellman_create(diffie_hellman_group_t group);
key_exchange_t *openssl_x_diffie_hellman_create(key_exchange_method_t ke);
#endif /** OPENSSL_X_DIFFIE_HELLMAN_H_ @}*/