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:
@@ -23,7 +23,7 @@
|
||||
|
||||
#include <ntt_fft.h>
|
||||
#include <ntt_fft_reduce.h>
|
||||
#include <crypto/diffie_hellman.h>
|
||||
#include <crypto/key_exchange.h>
|
||||
#include <utils/debug.h>
|
||||
|
||||
static const int seed_len = 32; /* 256 bits */
|
||||
@@ -278,7 +278,7 @@ static uint8_t* unpack_rec(private_newhope_ke_t *this, uint8_t *x)
|
||||
return r;
|
||||
}
|
||||
|
||||
METHOD(diffie_hellman_t, get_my_public_value, bool,
|
||||
METHOD(key_exchange_t, get_public_key, bool,
|
||||
private_newhope_ke_t *this, chunk_t *value)
|
||||
{
|
||||
uint16_t n, q;
|
||||
@@ -397,7 +397,7 @@ METHOD(diffie_hellman_t, get_my_public_value, bool,
|
||||
}
|
||||
}
|
||||
|
||||
METHOD(diffie_hellman_t, get_shared_secret, bool,
|
||||
METHOD(key_exchange_t, get_shared_secret, bool,
|
||||
private_newhope_ke_t *this, chunk_t *secret)
|
||||
{
|
||||
if (this->shared_secret.len == 0)
|
||||
@@ -410,7 +410,7 @@ METHOD(diffie_hellman_t, get_shared_secret, bool,
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
METHOD(diffie_hellman_t, set_other_public_value, bool,
|
||||
METHOD(key_exchange_t, set_public_key, bool,
|
||||
private_newhope_ke_t *this, chunk_t value)
|
||||
{
|
||||
newhope_reconciliation_t * rec;
|
||||
@@ -436,7 +436,7 @@ METHOD(diffie_hellman_t, set_other_public_value, bool,
|
||||
if (value.len != poly_len + seed_len)
|
||||
{
|
||||
DBG1(DBG_LIB, "received %N KE payload of incorrect size",
|
||||
diffie_hellman_group_names, NH_128_BIT);
|
||||
key_exchange_method_names, NH_128_BIT);
|
||||
return FALSE;
|
||||
}
|
||||
a_seed = chunk_create(value.ptr + poly_len, seed_len);
|
||||
@@ -545,7 +545,7 @@ METHOD(diffie_hellman_t, set_other_public_value, bool,
|
||||
if (value.len != poly_len + rec_len)
|
||||
{
|
||||
DBG1(DBG_LIB, "received %N KE payload of incorrect size",
|
||||
diffie_hellman_group_names, NH_128_BIT);
|
||||
key_exchange_method_names, NH_128_BIT);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
@@ -581,13 +581,13 @@ METHOD(diffie_hellman_t, set_other_public_value, bool,
|
||||
}
|
||||
}
|
||||
|
||||
METHOD(diffie_hellman_t, get_dh_group, diffie_hellman_group_t,
|
||||
METHOD(key_exchange_t, get_method, key_exchange_method_t,
|
||||
private_newhope_ke_t *this)
|
||||
{
|
||||
return NH_128_BIT;
|
||||
}
|
||||
|
||||
METHOD(diffie_hellman_t, destroy, void,
|
||||
METHOD(key_exchange_t, destroy, void,
|
||||
private_newhope_ke_t *this)
|
||||
{
|
||||
chunk_clear(&this->shared_secret);
|
||||
@@ -601,17 +601,17 @@ METHOD(diffie_hellman_t, destroy, void,
|
||||
/*
|
||||
* Described in header.
|
||||
*/
|
||||
newhope_ke_t *newhope_ke_create(diffie_hellman_group_t group, chunk_t g, chunk_t p)
|
||||
newhope_ke_t *newhope_ke_create(key_exchange_method_t ke, chunk_t g, chunk_t p)
|
||||
{
|
||||
private_newhope_ke_t *this;
|
||||
|
||||
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,
|
||||
.get_dh_group = _get_dh_group,
|
||||
.set_public_key = _set_public_key,
|
||||
.get_public_key = _get_public_key,
|
||||
.get_method = _get_method,
|
||||
.destroy = _destroy,
|
||||
},
|
||||
},
|
||||
|
||||
@@ -32,20 +32,20 @@ typedef struct newhope_ke_t newhope_ke_t;
|
||||
struct newhope_ke_t {
|
||||
|
||||
/**
|
||||
* Implements diffie_hellman_t interface.
|
||||
* Implements key_exchange_t interface.
|
||||
*/
|
||||
diffie_hellman_t dh;
|
||||
key_exchange_t ke;
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a new newhope_ke_t object.
|
||||
*
|
||||
* @param group New Hope DH group number
|
||||
* @param ke New Hope key exchange number
|
||||
* @param g not used
|
||||
* @param p not used
|
||||
* @return newhope_ke_t object, NULL if not supported
|
||||
*/
|
||||
newhope_ke_t *newhope_ke_create(diffie_hellman_group_t group, chunk_t g, chunk_t p);
|
||||
newhope_ke_t *newhope_ke_create(key_exchange_method_t ke, chunk_t g, chunk_t p);
|
||||
|
||||
#endif /** NEWHOPE_KE_H_ @}*/
|
||||
|
||||
|
||||
@@ -42,8 +42,8 @@ METHOD(plugin_t, get_features, int,
|
||||
private_newhope_plugin_t *this, plugin_feature_t *features[])
|
||||
{
|
||||
static plugin_feature_t f[] = {
|
||||
PLUGIN_REGISTER(DH, newhope_ke_create),
|
||||
PLUGIN_PROVIDE(DH, NH_128_BIT),
|
||||
PLUGIN_REGISTER(KE, newhope_ke_create),
|
||||
PLUGIN_PROVIDE(KE, NH_128_BIT),
|
||||
PLUGIN_DEPENDS(XOF, XOF_SHAKE_128),
|
||||
PLUGIN_DEPENDS(XOF, XOF_CHACHA20),
|
||||
};
|
||||
|
||||
@@ -27,7 +27,7 @@ const int count = 1000;
|
||||
START_TEST(test_newhope_ke_good)
|
||||
{
|
||||
chunk_t i_msg, r_msg, i_shared_secret, r_shared_secret;
|
||||
diffie_hellman_t *i_nh, *r_nh;
|
||||
key_exchange_t *i_nh, *r_nh;
|
||||
struct timespec start, stop;
|
||||
int i;
|
||||
|
||||
@@ -35,24 +35,24 @@ START_TEST(test_newhope_ke_good)
|
||||
|
||||
for (i = 0; i < count; i++)
|
||||
{
|
||||
i_nh = lib->crypto->create_dh(lib->crypto, NH_128_BIT);
|
||||
i_nh = lib->crypto->create_ke(lib->crypto, NH_128_BIT);
|
||||
ck_assert(i_nh != NULL);
|
||||
ck_assert(i_nh->get_dh_group(i_nh) == NH_128_BIT);
|
||||
ck_assert(i_nh->get_method(i_nh) == NH_128_BIT);
|
||||
|
||||
ck_assert(i_nh->get_my_public_value(i_nh, &i_msg));
|
||||
ck_assert(i_nh->get_public_key(i_nh, &i_msg));
|
||||
ck_assert(i_msg.len = 1824);
|
||||
|
||||
r_nh = lib->crypto->create_dh(lib->crypto, NH_128_BIT);
|
||||
r_nh = lib->crypto->create_ke(lib->crypto, NH_128_BIT);
|
||||
ck_assert(r_nh != NULL);
|
||||
|
||||
ck_assert(r_nh->set_other_public_value(r_nh, i_msg));
|
||||
ck_assert(r_nh->get_my_public_value(r_nh, &r_msg));
|
||||
ck_assert(r_nh->set_public_key(r_nh, i_msg));
|
||||
ck_assert(r_nh->get_public_key(r_nh, &r_msg));
|
||||
ck_assert(r_msg.len == 2048);
|
||||
|
||||
ck_assert(r_nh->get_shared_secret(r_nh, &r_shared_secret));
|
||||
ck_assert(r_shared_secret.len == 32);
|
||||
|
||||
ck_assert(i_nh->set_other_public_value(i_nh, r_msg));
|
||||
ck_assert(i_nh->set_public_key(i_nh, r_msg));
|
||||
ck_assert(i_nh->get_shared_secret(i_nh, &i_shared_secret));
|
||||
ck_assert(i_shared_secret.len == 32);
|
||||
ck_assert(chunk_equals(i_shared_secret, r_shared_secret));
|
||||
@@ -77,26 +77,26 @@ END_TEST
|
||||
START_TEST(test_newhope_ke_wrong)
|
||||
{
|
||||
chunk_t i_msg, r_msg, i_shared_secret, r_shared_secret;
|
||||
diffie_hellman_t *i_nh, *r_nh;
|
||||
key_exchange_t *i_nh, *r_nh;
|
||||
|
||||
i_nh = lib->crypto->create_dh(lib->crypto, NH_128_BIT);
|
||||
i_nh = lib->crypto->create_ke(lib->crypto, NH_128_BIT);
|
||||
ck_assert(i_nh != NULL);
|
||||
ck_assert(i_nh->get_my_public_value(i_nh, &i_msg));
|
||||
ck_assert(i_nh->get_public_key(i_nh, &i_msg));
|
||||
|
||||
r_nh = lib->crypto->create_dh(lib->crypto, NH_128_BIT);
|
||||
r_nh = lib->crypto->create_ke(lib->crypto, NH_128_BIT);
|
||||
ck_assert(r_nh != NULL);
|
||||
ck_assert(r_nh->set_other_public_value(r_nh, i_msg));
|
||||
ck_assert(r_nh->get_my_public_value(r_nh, &r_msg));
|
||||
ck_assert(r_nh->set_public_key(r_nh, i_msg));
|
||||
ck_assert(r_nh->get_public_key(r_nh, &r_msg));
|
||||
|
||||
/* destroy 1st instance of i_nh */
|
||||
i_nh->destroy(i_nh);
|
||||
chunk_free(&i_msg);
|
||||
|
||||
/* create 2nd instance of i_nh */
|
||||
i_nh = lib->crypto->create_dh(lib->crypto, NH_128_BIT);
|
||||
i_nh = lib->crypto->create_ke(lib->crypto, NH_128_BIT);
|
||||
ck_assert(i_nh != NULL);
|
||||
ck_assert(i_nh->get_my_public_value(i_nh, &i_msg));
|
||||
ck_assert(i_nh->set_other_public_value(i_nh, r_msg));
|
||||
ck_assert(i_nh->get_public_key(i_nh, &i_msg));
|
||||
ck_assert(i_nh->set_public_key(i_nh, r_msg));
|
||||
|
||||
ck_assert(r_nh->get_shared_secret(r_nh, &r_shared_secret));
|
||||
ck_assert(i_nh->get_shared_secret(i_nh, &i_shared_secret));
|
||||
@@ -114,7 +114,7 @@ END_TEST
|
||||
|
||||
START_TEST(test_newhope_ke_fail_i)
|
||||
{
|
||||
diffie_hellman_t *i_nh;
|
||||
key_exchange_t *i_nh;
|
||||
char buf_ff[2048];
|
||||
int i;
|
||||
|
||||
@@ -131,10 +131,10 @@ START_TEST(test_newhope_ke_fail_i)
|
||||
|
||||
for (i = 0; i < countof(r_msg); i++)
|
||||
{
|
||||
i_nh = lib->crypto->create_dh(lib->crypto, NH_128_BIT);
|
||||
i_nh = lib->crypto->create_ke(lib->crypto, NH_128_BIT);
|
||||
ck_assert(i_nh != NULL);
|
||||
ck_assert(i_nh->get_my_public_value(i_nh, &i_msg));
|
||||
ck_assert(!i_nh->set_other_public_value(i_nh, r_msg[i]));
|
||||
ck_assert(i_nh->get_public_key(i_nh, &i_msg));
|
||||
ck_assert(!i_nh->set_public_key(i_nh, r_msg[i]));
|
||||
chunk_free(&i_msg);
|
||||
i_nh->destroy(i_nh);
|
||||
}
|
||||
@@ -143,7 +143,7 @@ END_TEST
|
||||
|
||||
START_TEST(test_newhope_ke_fail_r)
|
||||
{
|
||||
diffie_hellman_t *r_nh;
|
||||
key_exchange_t *r_nh;
|
||||
char buf_ff[1824];
|
||||
int i;
|
||||
|
||||
@@ -158,9 +158,9 @@ START_TEST(test_newhope_ke_fail_r)
|
||||
|
||||
for (i = 0; i < countof(i_msg); i++)
|
||||
{
|
||||
r_nh = lib->crypto->create_dh(lib->crypto, NH_128_BIT);
|
||||
r_nh = lib->crypto->create_ke(lib->crypto, NH_128_BIT);
|
||||
ck_assert(r_nh != NULL);
|
||||
ck_assert(!r_nh->set_other_public_value(r_nh, i_msg[i]));
|
||||
ck_assert(!r_nh->set_public_key(r_nh, i_msg[i]));
|
||||
r_nh->destroy(r_nh);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user