From ed83ebb3147ed2e261a709799e12d0eb43200bf3 Mon Sep 17 00:00:00 2001 From: Dennis Brentjes Date: Thu, 20 Oct 2016 12:43:29 +0200 Subject: Shared secret key is now distributed to the other nodes. --- CMakeLists.txt | 8 ++++++++ libcmix-common/cmixprotofunctor.cpp | 1 + libcmix-common/cmixprotofunctor.hpp | 5 +++++ libcmix-common/receiver.hpp | 1 - libcmix-protobuf/cmix.proto | 13 +++++++++---- node/node.cpp | 33 ++++++++++++++++++++++++++++----- node/node.hpp | 3 ++- 7 files changed, 53 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f30c66e..f241abd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,14 @@ if(DOXYGEN_FOUND) ) endif(DOXYGEN_FOUND) +option(use_lto "Enable link time optimalisation" OFF) +if(use_lto) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -flto") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -flto -fwhole-program") + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -flto") +endif(use_lto) + add_subdirectory(libcmix-bignum) add_subdirectory(libcmix-crypto) add_subdirectory(libcmix) diff --git a/libcmix-common/cmixprotofunctor.cpp b/libcmix-common/cmixprotofunctor.cpp index a026628..46e56a0 100644 --- a/libcmix-common/cmixprotofunctor.cpp +++ b/libcmix-common/cmixprotofunctor.cpp @@ -12,5 +12,6 @@ MESSAGE_SETTER_DEF(ImANode, imanode) MESSAGE_SETTER_DEF(ImAClient, imaclient) MESSAGE_SETTER_DEF(Bye, bye) MESSAGE_SETTER_DEF(KeyExchange, keyexchange) +MESSAGE_SETTER_DEF(SecretKey, secretkey) #undef MESSAGE_SETTER_DEF \ No newline at end of file diff --git a/libcmix-common/cmixprotofunctor.hpp b/libcmix-common/cmixprotofunctor.hpp index c3f6fe9..86caa75 100644 --- a/libcmix-common/cmixprotofunctor.hpp +++ b/libcmix-common/cmixprotofunctor.hpp @@ -49,6 +49,11 @@ struct CMixProtoFunctor { * #MESSAGE_SETTER_DECL(KeyExchange) */ MESSAGE_SETTER_DECL(KeyExchange); + + /*! + * #MESSAGE_SETTER_DECL(SecretKey) + */ + MESSAGE_SETTER_DECL(SecretKey); #undef MESSAGE_SETTER }; diff --git a/libcmix-common/receiver.hpp b/libcmix-common/receiver.hpp index 2b05790..8a73e9c 100644 --- a/libcmix-common/receiver.hpp +++ b/libcmix-common/receiver.hpp @@ -7,7 +7,6 @@ /*! * \file */ - struct SenderReceiver; /*! diff --git a/libcmix-protobuf/cmix.proto b/libcmix-protobuf/cmix.proto index 1074613..b57c86f 100644 --- a/libcmix-protobuf/cmix.proto +++ b/libcmix-protobuf/cmix.proto @@ -4,6 +4,10 @@ message Initialization { required bytes public_share = 1; } +message SecretKey { + required bytes secret_key = 1; +} + message ImANode { } @@ -23,9 +27,10 @@ message KeyExchange { message CMixMessage { oneof contents { Initialization initialization = 1; - ImANode imanode = 2; - ImAClient imaclient = 3; - Bye bye = 4; - KeyExchange keyexchange = 5; + SecretKey secretkey = 2; + ImANode imanode = 3; + ImAClient imaclient = 4; + Bye bye = 5; + KeyExchange keyexchange = 6; } } diff --git a/node/node.cpp b/node/node.cpp index 2cbcb19..280453d 100644 --- a/node/node.cpp +++ b/node/node.cpp @@ -19,7 +19,7 @@ Node::Node(ListenSettings const& listen_settings, NodeNetworkSettings network_se , next_node(Sender(tcp::socket(io_service))) , api(get_implementation()) , keypair(api.create_key_pair()) -, network_pub_key() +, network_key() , shutting_down(false) { GOOGLE_PROTOBUF_VERIFY_VERSION; @@ -65,12 +65,20 @@ void Node::start_initialisation() { void Node::handle_node_initialization(const cmix_proto::Initialization& init) { + if(init.public_share().size() != keypair.pub_len) { + prev_node.close(); + cmix_proto::Bye bye; + next_node.async_send(bye, [this]{stop();}); + BOOST_LOG_TRIVIAL(fatal) << "Previous node dit not send proper initialization message."; + return; + } + if(network_settings.is_first) { - std::string share = init.public_share(); - - network_pub_key = std::vector(share.begin(), share.end()); - start_precomputation(); + cmix_proto::SecretKey sec; + sec.set_secret_key(network_key.data(), network_key.size()); + next_node.async_send(sec); } 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 = allocate_bignum(keypair.pub_len); @@ -105,6 +113,16 @@ void Node::handle_node_bye(cmix_proto::Bye) { } } +void Node::handle_node_secretkey(cmix_proto::SecretKey const& secret) +{ + std::string share = secret.secret_key(); + network_key = std::vector(share.begin(), share.end()); + + if(network_settings.is_first) { + start_precomputation(); + } +} + void Node::send_bye() { cmix_proto::Bye bye; @@ -125,6 +143,11 @@ void Node::handle_node_message(cmix_proto::CMixMessage message) handle_node_bye(message.bye()); return; } + case cmix_proto::CMixMessage::ContentsCase::kSecretkey: { + BOOST_LOG_TRIVIAL(trace) << "Handling SecretKey"; + handle_node_secretkey(message.secretkey()); + break; + } default: { BOOST_LOG_TRIVIAL(error) << "handle_node_message: CMixMessage contains unknown contents."; } diff --git a/node/node.hpp b/node/node.hpp index 9dfc343..5e3292b 100644 --- a/node/node.hpp +++ b/node/node.hpp @@ -53,7 +53,7 @@ class Node Api api; KeyPair keypair; - std::vector network_pub_key; + std::vector network_key; bool shutting_down; @@ -66,6 +66,7 @@ class Node void handle_node_initialization(cmix_proto::Initialization const& init); void handle_node_bye(cmix_proto::Bye); + void handle_node_secretkey(cmix_proto::SecretKey const& secret); void handle_node_message(cmix_proto::CMixMessage message); void handle_client_keyexchange(ClientConnections::key_type handle, cmix_proto::KeyExchange ke); -- cgit v1.2.3-70-g09d2