aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDennis Brentjes <d.brentjes@gmail.com>2016-10-14 11:39:25 +0200
committerDennis Brentjes <d.brentjes@gmail.com>2016-10-14 11:39:25 +0200
commit20139a86eddc062fdbaacad0d7d6fdbd999f2e18 (patch)
tree494db5134029fc7e46ffdf0cc2c2d7b5daf33c70
parent366bae00016bfbfdd354ab010555c2927505b2b2 (diff)
downloadcmix-20139a86eddc062fdbaacad0d7d6fdbd999f2e18.tar.gz
cmix-20139a86eddc062fdbaacad0d7d6fdbd999f2e18.tar.bz2
cmix-20139a86eddc062fdbaacad0d7d6fdbd999f2e18.zip
Refactored some code to be more consistent.
-rw-r--r--libcmix/CMakeLists.txt1
-rw-r--r--libcmix/cmix.c10
-rw-r--r--libcmix/cmix.h7
-rw-r--r--node/node.cpp68
-rw-r--r--node/node.hpp5
5 files changed, 60 insertions, 31 deletions
diff --git a/libcmix/CMakeLists.txt b/libcmix/CMakeLists.txt
index fe65e7a..885a6c8 100644
--- a/libcmix/CMakeLists.txt
+++ b/libcmix/CMakeLists.txt
@@ -13,4 +13,5 @@ target_compile_options(cmix
target_link_libraries(cmix
PUBLIC cmix-crypto
+ PUBLIC cmix-bignum
)
diff --git a/libcmix/cmix.c b/libcmix/cmix.c
index 944ac9c..db385c6 100644
--- a/libcmix/cmix.c
+++ b/libcmix/cmix.c
@@ -25,4 +25,14 @@ enum cmix_error set_message(char const* message, struct CMixBuffer b, unsigned i
}
strncpy(b.buffer + index * b.message_length, message, b.message_length);
+
+ return no_error;
+}
+
+enum cmix_error calculate_shared_secret(struct Bignum* result, struct Bignum partial_shared, struct Bignum my_share, struct Bignum mod)
+{
+ if(multiply_mod(result, partial_shared, my_share, mod) != NoError) {
+ return cmix_bignum_error;
+ }
+ return no_error;
}
diff --git a/libcmix/cmix.h b/libcmix/cmix.h
index 4a4441b..96d578f 100644
--- a/libcmix/cmix.h
+++ b/libcmix/cmix.h
@@ -2,6 +2,8 @@
extern "C" {
#endif
+#include "bignum.h"
+
/*!
* \file
*/
@@ -22,7 +24,8 @@ struct CMixBuffer {
*/
enum cmix_error {
no_error = 0,
- index_out_of_range = 1000
+ index_out_of_range = 1000,
+ cmix_bignum_error = 2000,
};
/*!
@@ -50,6 +53,8 @@ enum cmix_error get_message(char* message, struct CMixBuffer b, unsigned int ind
*/
enum cmix_error set_message(char const* message, struct CMixBuffer b, unsigned int index);
+enum cmix_error calculate_shared_secret(struct Bignum* result, struct Bignum partial_shared, struct Bignum my_share, struct Bignum mod);
+
#ifdef __cplusplus
} // extern "C"
#endif
diff --git a/node/node.cpp b/node/node.cpp
index 5487299..73eb4b6 100644
--- a/node/node.cpp
+++ b/node/node.cpp
@@ -1,4 +1,4 @@
-#include "node.hpp"
+ #include "node.hpp"
#include "logging.hpp"
@@ -61,7 +61,7 @@ void Node::start_initialisation() {
next_node.async_send(init);
}
-void Node::handle_initialization(const cmix_proto::Initialization& init)
+void Node::handle_node_initialization(const cmix_proto::Initialization& init)
{
if(network_settings.is_first) {
std::string share = init.public_share();
@@ -71,26 +71,34 @@ void Node::handle_initialization(const cmix_proto::Initialization& init)
} else {
Bignum shared = allocate_bignum(init.public_share().size());
std::copy_n(init.public_share().data(), init.public_share().size(), shared.data);
- Bignum my_share{keypair.pub, keypair.pub_len};
+ Bignum my_share = allocate_bignum(keypair.pub_len);
+ std::copy_n(my_share.data, my_share.len, keypair.pub);
Bignum mod = allocate_bignum(keypair.pub_len);
+ get_curve25519_mod(&mod);
Bignum new_shared = allocate_bignum(keypair.pub_len);
- if(multiply_mod(&new_shared, shared, my_share, mod) != NoError) {
- BOOST_LOG_TRIVIAL(fatal) << "Group multiplication failed";
- throw std::runtime_error("Group multiplication failed");
- }
-
cmix_proto::Initialization init;
init.set_public_share(new_shared.data, new_shared.len);
next_node.async_send(init);
free_bignum(&shared);
+ free_bignum(&my_share);
free_bignum(&mod);
free_bignum(&new_shared);
}
}
+void Node::handle_node_bye(cmix_proto::Bye) {
+ prev_node.close();
+ if (!shutting_down) {
+ send_bye();
+ prev_node.async_receive([this](cmix_proto::CMixMessage message){
+ handle_node_message(message);
+ });
+ }
+}
+
void Node::send_bye()
{
cmix_proto::Bye bye;
@@ -102,17 +110,13 @@ void Node::handle_node_message(cmix_proto::CMixMessage message)
{
switch(message.contents_case()) {
case cmix_proto::CMixMessage::ContentsCase::kInitialization: {
- handle_initialization(message.initialization());
+ BOOST_LOG_TRIVIAL(trace) << "Handling initialization";
+ handle_node_initialization(message.initialization());
break;
}
case cmix_proto::CMixMessage::ContentsCase::kBye: {
- prev_node.close();
- if (!shutting_down) {
- send_bye();
- prev_node.async_receive([this](cmix_proto::CMixMessage message){
- handle_node_message(message);
- });
- }
+ BOOST_LOG_TRIVIAL(trace) << "Handling bye";
+ handle_node_bye(message.bye());
return;
}
default: {
@@ -124,32 +128,36 @@ void Node::handle_node_message(cmix_proto::CMixMessage message)
});
}
+void Node::handle_client_keyexchange(ClientMap::key_type handle, cmix_proto::KeyExchange ke) {
+ api.derive_shared_key(keypair, reinterpret_cast<uint8_t const*>(ke.public_key().c_str()), true);
+}
+
+void Node::handle_client_bye(ClientMap::key_type handle, cmix_proto::Bye)
+{
+ clients.at(handle).close();
+ clients.erase(handle);
+}
+
void Node::handle_client_message(ClientMap::key_type handle, cmix_proto::CMixMessage message)
{
switch(message.contents_case()) {
case cmix_proto::CMixMessage::ContentsCase::kKeyexchange: {
- BOOST_LOG_TRIVIAL(trace) << "Deriving shared key";
- api.derive_shared_key(keypair, reinterpret_cast<uint8_t const*>(message.keyexchange().public_key().c_str()), true);
-
- clients.at(handle).async_receive([this, handle](cmix_proto::CMixMessage message){
- handle_client_message(handle, message);
- });
- return;
+ BOOST_LOG_TRIVIAL(trace) << "Handling keyexchange";
+ handle_client_keyexchange(handle, message.keyexchange());
+ break;
}
case cmix_proto::CMixMessage::ContentsCase::kBye: {
BOOST_LOG_TRIVIAL(trace) << "Handling bye";
-
- clients.at(handle).close();
- clients.erase(handle);
-
+ handle_client_bye(handle, message.bye());
return;
}
default: {
BOOST_LOG_TRIVIAL(error) << "handle_client_message: CMixMessage contains unknown contents.";
}
}
- clients.at(handle).close();
- clients.erase(handle);
+ clients.at(handle).async_receive([this, handle](cmix_proto::CMixMessage message){
+ handle_client_message(handle, message);
+ });
}
void Node::handle_imanode(std::list<Receiver>::iterator handle) {
@@ -178,10 +186,12 @@ void Node::handle_message(std::list<Receiver>::iterator handle, cmix_proto::CMix
{
switch(message.contents_case()) {
case cmix_proto::CMixMessage::ContentsCase::kImanode: {
+ BOOST_LOG_TRIVIAL(trace) << "Handling imanode";
handle_imanode(handle);
return;
}
case cmix_proto::CMixMessage::ContentsCase::kImaclient: {
+ BOOST_LOG_TRIVIAL(trace) << "Handling imaclient";
handle_imaclient(handle, message.imaclient());
return;
}
diff --git a/node/node.hpp b/node/node.hpp
index 9423860..76f3c0d 100644
--- a/node/node.hpp
+++ b/node/node.hpp
@@ -56,9 +56,12 @@ class Node
void start_precomputation();
void start_initialisation();
- void handle_initialization(cmix_proto::Initialization const& init);
+ void handle_node_initialization(cmix_proto::Initialization const& init);
+ void handle_node_bye(cmix_proto::Bye);
void handle_node_message(cmix_proto::CMixMessage message);
+ void handle_client_keyexchange(ClientMap::key_type handle, cmix_proto::KeyExchange ke);
+ void handle_client_bye(ClientMap::key_type handle, cmix_proto::Bye);
void handle_client_message(ClientMap::key_type handle, cmix_proto::CMixMessage message);
void handle_imanode(std::list<Receiver>::iterator handle);