diff options
| author | Dennis Brentjes <d.brentjes@gmail.com> | 2016-11-07 16:45:34 +0100 |
|---|---|---|
| committer | Dennis Brentjes <d.brentjes@gmail.com> | 2016-11-07 16:45:34 +0100 |
| commit | 8ff9babe2da4a2efc8529e800a6093fbd0327286 (patch) | |
| tree | 83f2d15f8dae8c7a5e216f9ec0a8001bd40673a3 | |
| parent | 46f22621759d388f7cef4bf0d2ac03667a5d611e (diff) | |
| download | cmix-8ff9babe2da4a2efc8529e800a6093fbd0327286.tar.gz cmix-8ff9babe2da4a2efc8529e800a6093fbd0327286.tar.bz2 cmix-8ff9babe2da4a2efc8529e800a6093fbd0327286.zip | |
The client sends his first message.
Changed some of the boilerplate so it's more conveniant to add types.
| -rw-r--r-- | client/cmixclient.cpp | 29 | ||||
| -rw-r--r-- | client/cmixclient.hpp | 3 | ||||
| -rw-r--r-- | libcmix-common/cmixprotofunctor.hpp | 81 | ||||
| -rw-r--r-- | libcmix-protobuf/cmix.proto | 3 | ||||
| -rw-r--r-- | libcmix/cmix.c | 25 | ||||
| -rw-r--r-- | libcmix/cmix.h | 4 |
6 files changed, 79 insertions, 66 deletions
diff --git a/client/cmixclient.cpp b/client/cmixclient.cpp index ba2f1da..f8ecf7d 100644 --- a/client/cmixclient.cpp +++ b/client/cmixclient.cpp @@ -22,7 +22,7 @@ void CMixClient::key_exchange(size_t node_id) { ke.mutable_public_key()->resize(get_group_element_array_size(&cmix_ctx)); ke.mutable_value()->resize(get_group_element_array_size(&cmix_ctx)); - key_exchange_init(&cmix_ctx, &(*ke.mutable_public_key())[0], &(*ke.mutable_value())[0], &data.at(node_id).secret_value); + key_exchange_init(&cmix_ctx, &(*ke.mutable_public_key())[0], &(*ke.mutable_value())[0], &secret_values.at(node_id)); network_connections.at(node_id).async_send(ke); network_connections.at(node_id).async_receive([node_id, this](cmix_proto::CMixMessage message) { @@ -33,7 +33,9 @@ void CMixClient::key_exchange(size_t node_id) { void CMixClient::initialize_connections() { size_t nr_nodes = network_details.node_details.size(); network_connections.reserve(nr_nodes); - data.resize(nr_nodes); + + secret_values.resize(nr_nodes, nullptr); + shared_values.resize(nr_nodes, nullptr); for(size_t i = 0; i < nr_nodes; ++i) { auto handler = [this, i]() { @@ -55,20 +57,28 @@ void CMixClient::handle_key_exchange(size_t node_id, cmix_proto::KeyExchange con { key_exchange_initiator( &cmix_ctx, - &data.at(node_id).shared_value, + &shared_values.at(node_id), ke.public_key().data(), ke.value().data(), - data.at(node_id).secret_value + &secret_values.at(node_id) ); size_t len = get_group_element_array_size(&cmix_ctx); + std::vector<char> vec(len, '\0'); std::string s = "abcdefghijklmnopqrstuvwxyz"; - s.resize(len, '0'); - + std::copy_n(s.begin(), s.size(), vec.begin() + 1); + if(std::all_of(shared_values.begin(), shared_values.end(), [](auto const& value){return value != nullptr;})) { + cmix_proto::UserMessage message; + message.mutable_m()->resize(len); - cmix_proto::Bye bye; - network_connections.at(node_id).async_send(bye); + blind_message(&cmix_ctx, &(*message.mutable_m())[0], vec.data(), shared_values.data(), shared_values.size()); + + network_connections.at(0).async_send(message); + + cmix_proto::Bye bye; + network_connections.at(node_id).async_send(bye); + } } void CMixClient::handle_message(size_t node_id, cmix_proto::CMixMessage message) @@ -107,7 +117,8 @@ CMixClient::CMixClient(NetworkDetails details) , cmix_ctx(initialize_cmix_context(get_implementation())) , network_details(details) , network_connections() -, data() +, secret_values() +, shared_values() { initialize_keypair(&cmix_ctx); diff --git a/client/cmixclient.hpp b/client/cmixclient.hpp index 59aeba6..30c096d 100644 --- a/client/cmixclient.hpp +++ b/client/cmixclient.hpp @@ -54,7 +54,8 @@ class CMixClient { CMixContext cmix_ctx; NetworkDetails network_details; std::vector<SSLSenderReceiver> network_connections; - std::vector<NodeData> data; + std::vector<GroupElement> secret_values; + std::vector<GroupElement> shared_values; void key_exchange(size_t node_id); diff --git a/libcmix-common/cmixprotofunctor.hpp b/libcmix-common/cmixprotofunctor.hpp index 755894e..effa8bd 100644 --- a/libcmix-common/cmixprotofunctor.hpp +++ b/libcmix-common/cmixprotofunctor.hpp @@ -2,6 +2,14 @@ #include "cmix.pb.h" +#include <boost/preprocessor/repeat.hpp> +#include <boost/preprocessor/seq/elem.hpp> +#include <boost/preprocessor/variadic/to_seq.hpp> +#include <boost/preprocessor/variadic/size.hpp> +#include <boost/preprocessor/arithmetic/mul.hpp> +#include <boost/preprocessor/arithmetic/add.hpp> +#include <boost/preprocessor/arithmetic/div.hpp> + /*! * \file */ @@ -25,59 +33,30 @@ struct CMixProtoFunctor { #define MESSAGE_SETTER_DEF(TYPE, NAME) \ proto_type operator()(cmix_proto::TYPE const& v) { \ proto_type m; \ - *m.mutable_##NAME() = v; \ + BOOST_PP_CAT(*m.mutable_, NAME)() = v; \ return m; \ } \ - - /*! - * #MESSAGE_SETTER_DEF(Initialization, initialization) - */ - MESSAGE_SETTER_DEF(Initialization, initialization) - - /*! - * #MESSAGE_SETTER_DEF(ImANode, imanode) - */ - MESSAGE_SETTER_DEF(ImANode, imanode) - - /*! - * #MESSAGE_SETTER_DEF(ImAClient, imaclient) - */ - MESSAGE_SETTER_DEF(ImAClient, imaclient) - - /*! - * #MESSAGE_SETTER_DEF(Bye, bye) - */ - MESSAGE_SETTER_DEF(Bye, bye) - - /*! - * #MESSAGE_SETTER_DEF(KeyExchange, keyexchange) - */ - MESSAGE_SETTER_DEF(KeyExchange, keyexchange) - /*! - * #MESSAGE_SETTER_DEF(SecretKey, secretkey) - */ - MESSAGE_SETTER_DEF(SecretKey, secretkey) - - /*! - * #MESSAGE_SETTER_DEF(NodeReady, nodeready) - */ - MESSAGE_SETTER_DEF(NodeReady, nodeready) - - /*! - * #MESSAGE_SETTER_DEF(PrePre, prepre) - */ - MESSAGE_SETTER_DEF(PrePre, prepre) - - /*! - * #MESSAGE_SETTER_DEF(PreMix, premix) - */ - MESSAGE_SETTER_DEF(PreMix, premix) - - /*! - * #MESSAGE_SETTER_DEF(PrePost, prepost) - */ - MESSAGE_SETTER_DEF(PrePost, prepost) - + #define MESSAGE_SETTER_DEF_ITERATION(Z, N, DATA) \ + MESSAGE_SETTER_DEF(BOOST_PP_SEQ_ELEM(BOOST_PP_MUL(N, 2), DATA), BOOST_PP_SEQ_ELEM(BOOST_PP_ADD(BOOST_PP_MUL(N, 2), 1), DATA)) + + #define MESSAGE_SETTER_DEFS(...) \ + BOOST_PP_REPEAT(BOOST_PP_DIV(BOOST_PP_VARIADIC_SIZE(__VA_ARGS__), 2), MESSAGE_SETTER_DEF_ITERATION, BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__)) + + MESSAGE_SETTER_DEFS( + Initialization, initialization, + ImANode, imanode, + ImAClient, imaclient, + Bye, bye, + KeyExchange, keyexchange, + SecretKey, secretkey, + NodeReady, nodeready, + UserMessage, usermessage, + PrePre, prepre, + PreMix, premix, + PrePost, prepost + ) + #undef MESSAGE_SETTER_DEFS + #undef MESSAGE_SETTER_DEF_ITERATION #undef MESSAGE_SETTER_DEF }; diff --git a/libcmix-protobuf/cmix.proto b/libcmix-protobuf/cmix.proto index 9ff5bb2..ce1f2c2 100644 --- a/libcmix-protobuf/cmix.proto +++ b/libcmix-protobuf/cmix.proto @@ -30,8 +30,7 @@ message NodeReady { } message UserMessage { - required bytes r_M = 1; - required bytes m_M = 2; + required bytes M = 1; } message PrePre { diff --git a/libcmix/cmix.c b/libcmix/cmix.c index 582fe70..a47a9fa 100644 --- a/libcmix/cmix.c +++ b/libcmix/cmix.c @@ -216,6 +216,8 @@ enum cmix_error key_exchange_init(struct CMixContext const* ctx, char* pubkey_bu get_public_key(ctx, pubkey_buffer); element_to_buffer(ctx, value_buffer, value); + + ctx->api.free_group_element(value); return no_error; } @@ -235,10 +237,13 @@ enum cmix_error key_exchange_responder(struct CMixContext const* ctx, GroupEleme return no_error; } -enum cmix_error key_exchange_initiator(struct CMixContext const* ctx, GroupElement* shared_key, char const* pubkey, char const* value, GroupElement priv_el) { +enum cmix_error key_exchange_initiator(struct CMixContext const* ctx, GroupElement* shared_key, char const* pubkey, char const* value, GroupElement* priv_el) { size_t el_len = get_group_element_array_size(ctx); *shared_key = ctx->api.derive_shared_key(ctx->keypair, (unsigned char*)pubkey, el_len, (unsigned char*)value, el_len, priv_el, false); + ctx->api.free_group_element(*priv_el); + *priv_el = NULL; + return no_error; } @@ -251,7 +256,7 @@ enum cmix_error post_process(struct CMixContext* ctx, char* r_out, char* m_out, GroupElement pirs = ctx->api.multiply(D, msg, true); element_to_buffer(ctx, m_out, pirs); - ctx->pirs = pirs; // this is not always usable as only the last node will be able to use this effectively, but we store it anyways. + ctx->pirs[index] = pirs; // this is not always usable as only the last node will be able to use this effectively, but we store it anyways. ctx->api.free_group_element(x); ctx->api.free_group_element(D); @@ -259,3 +264,19 @@ enum cmix_error post_process(struct CMixContext* ctx, char* r_out, char* m_out, return no_error; } + +enum cmix_error blind_message(struct CMixContext const* ctx, char* m_out, char const* message, GroupElement const* keys, size_t const nr_nodes) { + size_t len = get_group_element_array_size(ctx); + + GroupElement mes = ctx->api.array_to_element(message, len, true); + + for(size_t i = 0; i < nr_nodes; ++i) { + ctx->api.multiply(mes, mes, keys[i]); + } + + element_to_buffer(ctx, m_out, mes); + + ctx->api.free_group_element(mes); + + return no_error; +} diff --git a/libcmix/cmix.h b/libcmix/cmix.h index 7442369..33b99d2 100644 --- a/libcmix/cmix.h +++ b/libcmix/cmix.h @@ -113,10 +113,12 @@ enum cmix_error key_exchange_init(struct CMixContext const* ctx, char* pubkey_bu enum cmix_error key_exchange_responder(struct CMixContext const* ctx, GroupElement* shared_key, char* public_key_buffer, char* exhange_value_buffer, char const* pubkey, char const* value); -enum cmix_error key_exchange_initiator(struct CMixContext const* ctx, GroupElement* shared_key, char const* pubkey, char const* value, GroupElement priv_el); +enum cmix_error key_exchange_initiator(struct CMixContext const* ctx, GroupElement* shared_key, char const* pubkey, char const* value, GroupElement* priv_el); enum cmix_error post_process(struct CMixContext* ctx, char* r_out, char* m_out, char const* r_epirs, char const* m_epirs, size_t index); +enum cmix_error blind_message(struct CMixContext const* ctx, char* m_out, char const* message, GroupElement const* keys, size_t const nr_nodes); + #ifdef __cplusplus } // extern "C" #endif |
