diff options
| -rw-r--r-- | libcmix-common/cmixprotofunctor.hpp | 23 | ||||
| -rw-r--r-- | libcmix-crypto/api.h | 6 | ||||
| -rw-r--r-- | libcmix-crypto/curve25519/curve25519.c | 33 | ||||
| -rw-r--r-- | libcmix-crypto/curve25519/curve25519.h | 70 | ||||
| -rw-r--r-- | libcmix-crypto/curve25519/null/null_curve25519.c | 40 | ||||
| -rw-r--r-- | libcmix-crypto/curve25519/sodium/libsodium_curve25519.c | 35 | ||||
| -rw-r--r-- | libcmix-crypto/elgamal/elgamal.c | 1 | ||||
| -rw-r--r-- | libcmix-crypto/elgamal/elgamal.h | 2 | ||||
| -rw-r--r-- | libcmix-crypto/elgamal/gcrypt/gcrypt_elgamal.c | 11 | ||||
| -rw-r--r-- | libcmix-crypto/elgamal/null/null_elgamal.c | 42 | ||||
| -rw-r--r-- | libcmix-protobuf/cmix.proto | 4 | ||||
| -rw-r--r-- | libcmix/cmix.c | 16 | ||||
| -rw-r--r-- | libcmix/cmix.h | 2 | ||||
| -rw-r--r-- | node/node.cpp | 76 | ||||
| -rw-r--r-- | node/node.hpp | 1 |
15 files changed, 235 insertions, 127 deletions
diff --git a/libcmix-common/cmixprotofunctor.hpp b/libcmix-common/cmixprotofunctor.hpp index 5fd736b..755894e 100644 --- a/libcmix-common/cmixprotofunctor.hpp +++ b/libcmix-common/cmixprotofunctor.hpp @@ -30,49 +30,54 @@ struct CMixProtoFunctor { } \ /*! - * #MESSAGE_SETTER_DECL(Initialization, initialization) + * #MESSAGE_SETTER_DEF(Initialization, initialization) */ MESSAGE_SETTER_DEF(Initialization, initialization) /*! - * #MESSAGE_SETTER_DECL(ImANode, imanode) + * #MESSAGE_SETTER_DEF(ImANode, imanode) */ MESSAGE_SETTER_DEF(ImANode, imanode) /*! - * #MESSAGE_SETTER_DECL(ImAClient, imaclient) + * #MESSAGE_SETTER_DEF(ImAClient, imaclient) */ MESSAGE_SETTER_DEF(ImAClient, imaclient) /*! - * #MESSAGE_SETTER_DECL(Bye, bye) + * #MESSAGE_SETTER_DEF(Bye, bye) */ MESSAGE_SETTER_DEF(Bye, bye) /*! - * #MESSAGE_SETTER_DECL(KeyExchange, keyexchange) + * #MESSAGE_SETTER_DEF(KeyExchange, keyexchange) */ MESSAGE_SETTER_DEF(KeyExchange, keyexchange) /*! - * #MESSAGE_SETTER_DECL(SecretKey, secretkey) + * #MESSAGE_SETTER_DEF(SecretKey, secretkey) */ MESSAGE_SETTER_DEF(SecretKey, secretkey) /*! - * #MESSAGE_SETTER_DECL(NodeReady, nodeready) + * #MESSAGE_SETTER_DEF(NodeReady, nodeready) */ MESSAGE_SETTER_DEF(NodeReady, nodeready) /*! - * #MESSAGE_SETTER_DECL(PrePre, prepre) + * #MESSAGE_SETTER_DEF(PrePre, prepre) */ MESSAGE_SETTER_DEF(PrePre, prepre) /*! - * #MESSAGE_SETTER_DECL(PreMix, premix) + * #MESSAGE_SETTER_DEF(PreMix, premix) */ MESSAGE_SETTER_DEF(PreMix, premix) + + /*! + * #MESSAGE_SETTER_DEF(PrePost, prepost) + */ + MESSAGE_SETTER_DEF(PrePost, prepost) #undef MESSAGE_SETTER_DEF }; diff --git a/libcmix-crypto/api.h b/libcmix-crypto/api.h index 6d701d7..addf9e3 100644 --- a/libcmix-crypto/api.h +++ b/libcmix-crypto/api.h @@ -96,6 +96,11 @@ typedef void(*GroupElementDeleter)(GroupElement); typedef void (*Encrypter)(GroupElement*, GroupElement*, GroupElement, GroupElement); /*! + * + */ +typedef GroupElement (*DecryptionShareGetter)(GroupElement, GroupElement); + +/*! * \brief The Api struct stores pointers to functions of a specific implementation. Like a Curve25519 specific one. */ struct Api { @@ -110,6 +115,7 @@ struct Api { GroupElementDeleter free_group_element; ///< frees a base type of the cryptolibrary. KeyExchangeValueGetter get_key_exchange_value; ///< get generator *op* group element. GroupElementMultiplier multiply; ///< Multiplies two groupelements modulo group. + DecryptionShareGetter get_decryption_share; ///< calculates the first argument to the power of Inverse second argument; PublicShareAdder add_public_share; ///< Adds the public key stored in void* to the existing share. SharedKeyDeriver derive_shared_key; ///< Pointer to shared key derivation function SharedKeyDeleter free_shared_key; ///< Pointer to shared key deleter function diff --git a/libcmix-crypto/curve25519/curve25519.c b/libcmix-crypto/curve25519/curve25519.c index db77d9a..a963840 100644 --- a/libcmix-crypto/curve25519/curve25519.c +++ b/libcmix-crypto/curve25519/curve25519.c @@ -5,22 +5,23 @@ struct Api get_curve25519_implementation() { curve25519_initialize(); return (struct Api) { - &curve25519_initialize, - &curve25519_create_keypair, - &curve25519_keypair_deleter, - &curve25519_key_to_array, - NULL, - NULL, - NULL, - NULL, - NULL, - NULL, - NULL, - &curve25519_add_public_share, - &curve25519_derive_shared_key, - &curve25519_shared_key_deleter, - NULL, - &curve25519_deinitialize + .initialize = curve25519_initialize, + .create_keypair = curve25519_create_keypair, + .free_keypair = curve25519_delete_keypair, + .element_to_array = curve25519_element_to_array, + .free_buffer = curve25519_free_buffer, + .array_to_element = curve25519_array_to_element, + .get_group_element = curve25519_get_group_element, + .get_group_element_array_size = curve25519_get_group_element_array_size, + .free_group_element = curve25519_delete_group_element, + .get_key_exchange_value = curve25519_get_key_exchange_value, + .multiply = curve25519_multiply, + .get_decryption_share = curve25519_get_decryption_share, + .add_public_share = curve25519_add_public_share, + .derive_shared_key = curve25519_derive_shared_key, + .free_shared_key = curve25519_delete_shared_key, + .encrypt = curve25519_encrypt, + .deinitialize = curve25519_deinitialize }; } diff --git a/libcmix-crypto/curve25519/curve25519.h b/libcmix-crypto/curve25519/curve25519.h index 762e8db..5f30915 100644 --- a/libcmix-crypto/curve25519/curve25519.h +++ b/libcmix-crypto/curve25519/curve25519.h @@ -10,55 +10,39 @@ extern "C" { * \file */ -/*! - * \brief curve25519_initialize initilalize curve25519 library - */ -extern void curve25519_initialize(void); +extern Initializer curve25519_initialize; -/*! - * \brief curve25519_create_keypair - * \return A curve25519 keypair. - */ -extern struct KeyPair curve25519_create_keypair(void); +extern KeyPairCreator curve25519_create_keypair; -/*! - * \brief curve25519_keypair_deleter - * \param p The keypair to free. - */ -extern void curve25519_keypair_deleter(struct KeyPair* p); +extern KeyPairDeleter curve25519_delete_keypair; -/*! - * \brief curve25519_get_pubkey_array - * \param pubkey - * \param buffer - * \param len - */ -extern void curve25519_key_to_array(unsigned char** buffer, size_t* len, void* pubkey); +extern ElementToArray curve25519_element_to_array; -/*! - * \brief curve25519_add_public_share - * \param buffer - * \param out_len - * \param share - * \param pubkey - */ -extern void curve25519_add_public_share(char** buffer, size_t* out_len, char const* share, size_t in_len, void* pubkey); +extern BufferDeleter curve25519_free_buffer; -/*! - * \brief curve25519_derive_shared_key - * \param pair Our keypair. - * \param pub_key The public key of the other party. - * \param swap_pub_order Should we swap the order in which we feed the public keys to the hash function. - * \return A Shared key - */ -extern GroupElement curve25519_derive_shared_key(struct KeyPair pair, unsigned char const* other_pub, size_t pub_len, unsigned char const* value, size_t value_len, void* priv_value, bool swap_pub_order); -/*! - * \brief curve25519_shared_key_deleter - * \param s the Shared key to free. - */ -extern void curve25519_shared_key_deleter(struct SharedKey* s); +extern ArrayToElement curve25519_array_to_element; + +extern GroupElementGetter curve25519_get_group_element; + +extern GroupElementDeleter curve25519_delete_group_element; + +extern KeyExchangeValueGetter curve25519_get_key_exchange_value; + +extern GroupElementMultiplier curve25519_multiply; + +extern DecryptionShareGetter curve25519_get_decryption_share; + +extern GroupElementArraySizeGetter curve25519_get_group_element_array_size; + +extern PublicShareAdder curve25519_add_public_share; + +extern SharedKeyDeriver curve25519_derive_shared_key; + +extern SharedKeyDeleter curve25519_delete_shared_key; + +extern Encrypter curve25519_encrypt; -extern void curve25519_deinitialize(void); +extern Deinitializer curve25519_deinitialize; /*! * \brief get_curve25519_implementation diff --git a/libcmix-crypto/curve25519/null/null_curve25519.c b/libcmix-crypto/curve25519/null/null_curve25519.c index 33fb963..d261791 100644 --- a/libcmix-crypto/curve25519/null/null_curve25519.c +++ b/libcmix-crypto/curve25519/null/null_curve25519.c @@ -1,31 +1,47 @@ -#include "api.h" +#include "curve25519.h" #include <stddef.h> #include <stdbool.h> -void curve25519_initialize(void){} +void null_curve25519_initialize(void){} -struct KeyPair curve25519_create_keypair(void) { +struct KeyPair null_curve25519_create_keypair(void) { return (struct KeyPair){ NULL, NULL, }; } -void curve25519_keypair_deleter(struct KeyPair* p) {} +void null_curve25519_delete_keypair(struct KeyPair* p) {} -void curve25519_get_key_array(char** buffer, size_t* len, void* key) {} +void null_curve25519_get_key_array(char** buffer, size_t* len, void* key) {} -void curve25519_add_public_share(char** buffer, size_t* len_out, char const* share, size_t in_len, void* pubkey) {} +void null_curve25519_add_public_share(char** buffer, size_t* len_out, char const* share, size_t in_len, void* pubkey) {} -void curve25519_shared_key_deleter(struct SharedKey* s) {} +void null_curve25519_shared_key_deleter(struct SharedKey* s) {} -struct SharedKey curve25519_derive_shared_key(struct KeyPair pair, unsigned char const* pub_key, bool swap_pub_order) { - return (struct SharedKey){ - NULL, - }; +GroupElement null_curve25519_derive_shared_key(struct KeyPair keypair, unsigned char const* other_pub, size_t pub_len, unsigned char const* value, size_t value_len, void* priv_value, bool swap) { + return NULL; } -void curve25519_deinitialize(void){}
\ No newline at end of file +void null_curve25519_deinitialize(void){} + +Initializer curve25519_initialize = &null_curve25519_initialize; +KeyPairCreator curve25519_create_keypair = &null_curve25519_create_keypair; +KeyPairDeleter curve25519_delete_keypair = &null_curve25519_delete_keypair; +ElementToArray curve25519_element_to_array = NULL;//&null_curve25519_element_to_array; +BufferDeleter curve25519_free_buffer = NULL;//&null_curve25519_free_buffer; +ArrayToElement curve25519_array_to_element = NULL;//&null_curve25519_array_to_element; +GroupElementGetter curve25519_get_group_element = NULL;//&null_curve25519_get_group_element; +GroupElementDeleter curve25519_delete_group_element = NULL;//&null_curve25519_delete_group_element; +KeyExchangeValueGetter curve25519_get_key_exchange_value = NULL;//&null_curve25519_get_key_exchange_value; +GroupElementMultiplier curve25519_multiply = NULL;//&null_curve25519_multiply; +DecryptionShareGetter curve25519_get_decryption_share = NULL;//&null_curve25519_get_decryption_share; +GroupElementArraySizeGetter curve25519_get_group_element_array_size = NULL;//&null_curve25519_get_group_element_array_size; +PublicShareAdder curve25519_add_public_share = NULL;//&null_curve25519_add_public_share; +SharedKeyDeriver curve25519_derive_shared_key = &null_curve25519_derive_shared_key; +SharedKeyDeleter curve25519_delete_shared_key = NULL;//&null_curve25519_delete_shared_key; +Encrypter curve25519_encrypt = NULL;//&null_curve25519_encrypt; +Deinitializer curve25519_deinitialize = &null_curve25519_deinitialize; diff --git a/libcmix-crypto/curve25519/sodium/libsodium_curve25519.c b/libcmix-crypto/curve25519/sodium/libsodium_curve25519.c index 2405442..b4ae963 100644 --- a/libcmix-crypto/curve25519/sodium/libsodium_curve25519.c +++ b/libcmix-crypto/curve25519/sodium/libsodium_curve25519.c @@ -6,13 +6,13 @@ #include <stddef.h> #include <stdlib.h> -void curve25519_initialize(void) { +void sodium_curve25519_initialize(void) { if(sodium_init() == -1) { exit(-1); } } -void curve25519_keypair_deleter(struct KeyPair* p) { +void sodium_curve25519_delete_keypair(struct KeyPair* p) { sodium_free(p->sec); free(p->pub); @@ -20,7 +20,7 @@ void curve25519_keypair_deleter(struct KeyPair* p) { p->pub = NULL; } -struct KeyPair curve25519_create_keypair(void) { +struct KeyPair sodium_curve25519_create_keypair(void) { unsigned char* sec = (unsigned char*) sodium_malloc(crypto_box_SECRETKEYBYTES); unsigned char* pub = (unsigned char*) malloc(crypto_box_PUBLICKEYBYTES); @@ -33,21 +33,21 @@ struct KeyPair curve25519_create_keypair(void) { }; } -void curve25519_shared_key_deleter(struct SharedKey* s) { +void sodium_curve25519_shared_key_deleter(struct SharedKey* s) { sodium_free(s->shared); s->shared = NULL; } -void curve25519_key_to_array(unsigned char** buffer, size_t* len, void* key) { +void sodium_curve25519_key_to_array(unsigned char** buffer, size_t* len, void* key) { } -void curve25519_add_public_share(char** buffer, size_t* out_len, char const* share, size_t in_len, void* pubkey) { +void sodium_curve25519_add_public_share(char** buffer, size_t* out_len, char const* share, size_t in_len, void* pubkey) { } -GroupElement curve25519_derive_shared_key(struct KeyPair pair, unsigned char const* other_pub, size_t pub_len, unsigned char const* value, size_t value_len, void* priv_value, bool swap_pub_order) { +GroupElement sodium_curve25519_derive_shared_key(struct KeyPair pair, unsigned char const* other_pub, size_t pub_len, unsigned char const* value, size_t value_len, void* priv_value, bool swap_pub_order) { unsigned char* shared = (unsigned char*) sodium_malloc(crypto_generichash_BYTES); crypto_generichash_state h; @@ -74,5 +74,22 @@ GroupElement curve25519_derive_shared_key(struct KeyPair pair, unsigned char con return shared; } -void curve25519_deinitialize(void) {} - +void sodium_curve25519_deinitialize(void) {} + +Initializer curve25519_initialize = &sodium_curve25519_initialize; +KeyPairCreator curve25519_create_keypair = &sodium_curve25519_create_keypair; +KeyPairDeleter curve25519_delete_keypair = &sodium_curve25519_delete_keypair; +ElementToArray curve25519_element_to_array = NULL;//&sodium_curve25519_element_to_array; +BufferDeleter curve25519_free_buffer = NULL;//&sodium_curve25519_free_buffer; +ArrayToElement curve25519_array_to_element = NULL;//&sodium_curve25519_array_to_element; +GroupElementGetter curve25519_get_group_element = NULL;//&sodium_curve25519_get_group_element; +GroupElementDeleter curve25519_delete_group_element = NULL;//&sodium_curve25519_delete_group_element; +KeyExchangeValueGetter curve25519_get_key_exchange_value = NULL;//&sodium_curve25519_get_key_exchange_value; +GroupElementMultiplier curve25519_multiply = NULL;//&sodium_curve25519_multiply; +DecryptionShareGetter curve25519_get_decryption_share = NULL;//&sodium_curve25519_get_decryption_share; +GroupElementArraySizeGetter curve25519_get_group_element_array_size = NULL;//&sodium_curve25519_get_group_element_array_size; +PublicShareAdder curve25519_add_public_share = NULL;//&sodium_curve25519_add_public_share; +SharedKeyDeriver curve25519_derive_shared_key = &sodium_curve25519_derive_shared_key; +SharedKeyDeleter curve25519_delete_shared_key = NULL;//&sodium_curve25519_delete_shared_key; +Encrypter curve25519_encrypt = NULL;//&sodium_curve25519_encrypt; +Deinitializer curve25519_deinitialize = &sodium_curve25519_deinitialize; diff --git a/libcmix-crypto/elgamal/elgamal.c b/libcmix-crypto/elgamal/elgamal.c index e54f76d..dbfccb9 100644 --- a/libcmix-crypto/elgamal/elgamal.c +++ b/libcmix-crypto/elgamal/elgamal.c @@ -15,6 +15,7 @@ struct Api get_elgamal_implementation() .free_group_element = elgamal_delete_group_element, .get_key_exchange_value = elgamal_get_key_exchange_value, .multiply = elgamal_multiply, + .get_decryption_share = elgamal_get_decryption_share, .add_public_share = elgamal_add_public_share, .derive_shared_key = elgamal_derive_shared_key, .free_shared_key = elgamal_delete_shared_key, diff --git a/libcmix-crypto/elgamal/elgamal.h b/libcmix-crypto/elgamal/elgamal.h index 43ddca0..21bf58a 100644 --- a/libcmix-crypto/elgamal/elgamal.h +++ b/libcmix-crypto/elgamal/elgamal.h @@ -30,6 +30,8 @@ extern KeyExchangeValueGetter elgamal_get_key_exchange_value; extern GroupElementMultiplier elgamal_multiply; +extern DecryptionShareGetter elgamal_get_decryption_share; + extern GroupElementArraySizeGetter elgamal_get_group_element_array_size; extern PublicShareAdder elgamal_add_public_share; diff --git a/libcmix-crypto/elgamal/gcrypt/gcrypt_elgamal.c b/libcmix-crypto/elgamal/gcrypt/gcrypt_elgamal.c index c610556..1824ccc 100644 --- a/libcmix-crypto/elgamal/gcrypt/gcrypt_elgamal.c +++ b/libcmix-crypto/elgamal/gcrypt/gcrypt_elgamal.c @@ -152,6 +152,16 @@ GroupElement gcrypt_elgamal_multiply(GroupElement lh, GroupElement rh, bool secu return ret; } +GroupElement gcrypt_elgamal_get_decryption_share(GroupElement r, GroupElement e) { + gcry_mpi_t inve = gcry_mpi_snew(0); + gcry_mpi_invm(inve, (gcry_mpi_t)e, p); + + gcry_mpi_t d = gcry_mpi_snew(0); + gcry_mpi_powm(d, (gcry_mpi_t)r, inve, p); + gcry_mpi_release(inve); + return d; +} + size_t gcrypt_elgamal_get_group_element_array_size() { return 256u; } @@ -241,6 +251,7 @@ GroupElementGetter elgamal_get_group_element = &gcrypt_elgamal_get_group_element GroupElementDeleter elgamal_delete_group_element = &gcrypt_elgamal_delete_group_element; KeyExchangeValueGetter elgamal_get_key_exchange_value = &gcrypt_elgamal_get_key_exchange_value; GroupElementMultiplier elgamal_multiply = &gcrypt_elgamal_multiply; +DecryptionShareGetter elgamal_get_decryption_share = &gcrypt_elgamal_get_decryption_share; GroupElementArraySizeGetter elgamal_get_group_element_array_size = &gcrypt_elgamal_get_group_element_array_size; PublicShareAdder elgamal_add_public_share = &gcrypt_elgamal_add_public_share; SharedKeyDeriver elgamal_derive_shared_key = &gcrypt_elgamal_derive_shared_key; diff --git a/libcmix-crypto/elgamal/null/null_elgamal.c b/libcmix-crypto/elgamal/null/null_elgamal.c index 17478e1..2b5c43b 100644 --- a/libcmix-crypto/elgamal/null/null_elgamal.c +++ b/libcmix-crypto/elgamal/null/null_elgamal.c @@ -1,30 +1,46 @@ -#include "api.h" +#include "elgamal.h" #include <stddef.h> #include <stdbool.h> -void elgamal_initialize(void) {} +void null_elgamal_initialize(void) {} -void elgamal_keypair_deleter(struct KeyPair p) {} +void null_elgamal_keypair_deleter(struct KeyPair p) {} -struct KeyPair elgamal_create_keypair() { +struct KeyPair null_elgamal_create_keypair() { return (struct KeyPair){ NULL, NULL, }; } -void elgamal_get_key_array(char** buffer, size_t* len, void* pubkey) {} +void null_elgamal_get_key_array(char** buffer, size_t* len, void* pubkey) {} -void elgamal_add_public_share(char** buffer, size_t* out_len, char const* share, size_t in_len, void* pubkey) {} +void null_elgamal_add_public_share(char** buffer, size_t* out_len, char const* share, size_t in_len, void* pubkey) {} -struct SharedKey elgamal_derive_shared_key(struct KeyPair pair, void const* pub_key, bool swap_pub_order) { - return (struct SharedKey){ - NULL, - }; +GroupElement null_elgamal_derive_shared_key(struct KeyPair keypair, unsigned char const* other_pub, size_t pub_len, unsigned char const* value, size_t value_len, void* priv_value, bool swap) { + return NULL; } -void elgamal_shared_key_deleter(struct SharedKey* s) {} - -void elgamal_deinitialize(void) {} +void null_elgamal_shared_key_deleter(struct SharedKey* s) {} + +void null_elgamal_deinitialize(void) {} + +Initializer elgamal_initialize = &null_elgamal_initialize; +KeyPairCreator elgamal_create_keypair = &null_elgamal_create_keypair; +KeyPairDeleter elgamal_delete_keypair = NULL;//&null_elgamal_delete_keypair; +ElementToArray elgamal_element_to_array = NULL;//&null_elgamal_element_to_array; +BufferDeleter elgamal_free_buffer = NULL;//&null_elgamal_free_buffer; +ArrayToElement elgamal_array_to_element = NULL;//&null_elgamal_array_to_element; +GroupElementGetter elgamal_get_group_element = NULL;//&null_elgamal_get_group_element; +GroupElementDeleter elgamal_delete_group_element = NULL;//&null_elgamal_delete_group_element; +KeyExchangeValueGetter elgamal_get_key_exchange_value = NULL;//&null_elgamal_get_key_exchange_value; +GroupElementMultiplier elgamal_multiply = NULL;//&null_elgamal_multiply; +DecryptionShareGetter elgamal_get_decryption_share = NULL;//&null_elgamal_get_decryption_share; +GroupElementArraySizeGetter elgamal_get_group_element_array_size = NULL;//&null_elgamal_get_group_element_array_size; +PublicShareAdder elgamal_add_public_share = NULL;//&null_elgamal_add_public_share; +SharedKeyDeriver elgamal_derive_shared_key = &null_elgamal_derive_shared_key; +SharedKeyDeleter elgamal_delete_shared_key = NULL;//&null_elgamal_delete_shared_key; +Encrypter elgamal_encrypt = NULL;//&null_elgamal_encrypt; +Deinitializer elgamal_deinitialize = &null_elgamal_deinitialize;
\ No newline at end of file diff --git a/libcmix-protobuf/cmix.proto b/libcmix-protobuf/cmix.proto index 00ab377..e048fef 100644 --- a/libcmix-protobuf/cmix.proto +++ b/libcmix-protobuf/cmix.proto @@ -40,7 +40,8 @@ message PreMix { } message PrePost { - repeated bytes PiRS = 1; + repeated bytes r_EPiRS = 1; + repeated bytes m_EPiRS = 2; } message CMixMessage { @@ -54,5 +55,6 @@ message CMixMessage { NodeReady nodeready = 7; PrePre prepre = 8; PreMix premix = 9; + PrePost prepost = 10; } } diff --git a/libcmix/cmix.c b/libcmix/cmix.c index 4893c6c..28f56db 100644 --- a/libcmix/cmix.c +++ b/libcmix/cmix.c @@ -216,3 +216,19 @@ enum cmix_error key_exchange(struct CMixContext const* ctx, GroupElement* shared return no_error; } + +enum cmix_error post_process(struct CMixContext* ctx, char const* r_epirs, char const* m_epirs, size_t index) { + GroupElement x = ctx->api.array_to_element(r_epirs, get_group_element_array_size(ctx), true); + + GroupElement D = ctx->api.get_decryption_share(x, ctx->keypair.sec); + + GroupElement msg = ctx->api.array_to_element(m_epirs, get_group_element_array_size(ctx), true); + GroupElement pirs = ctx->api.multiply(D, msg, true); + + ctx->pirs[index] = pirs; + ctx->api.free_group_element(x); + ctx->api.free_group_element(D); + ctx->api.free_group_element(msg); + + return no_error; +} diff --git a/libcmix/cmix.h b/libcmix/cmix.h index 3877382..433c3bc 100644 --- a/libcmix/cmix.h +++ b/libcmix/cmix.h @@ -106,6 +106,8 @@ enum cmix_error multiply_s(struct CMixContext const* ctx, char* r_out_buffer, ch enum cmix_error key_exchange(struct CMixContext const* ctx, GroupElement* shared_key, char* public_key_buffer, char* exhange_value_buffer, char const* pubkey, char const* value); +enum cmix_error post_process(struct CMixContext* ctx, char const* r_epirs, char const* m_epirs, size_t index); + #ifdef __cplusplus } // extern "C" #endif diff --git a/node/node.cpp b/node/node.cpp index 98b5873..a781755 100644 --- a/node/node.cpp +++ b/node/node.cpp @@ -115,28 +115,6 @@ void Node::handle_node_secretkey(cmix_proto::SecretKey const& secret) } template <typename T> -cmix_proto::PreMix fill_precomputation_mix_message(CMixContext const& ctx, T const& rs, T const& ms) { - cmix_proto::PreMix premix; - for(size_t i = 0; i < ctx.nr_participants; ++i) { - auto new_pos = ctx.permutation[i]; - size_t el_len = get_group_element_array_size(&ctx); - - premix.mutable_r_epirs(new_pos)->resize(el_len); - premix.mutable_m_epirs(new_pos)->resize(el_len); - - multiply_s( - &ctx, - &(*premix.mutable_r_epirs(new_pos))[0], - &(*premix.mutable_m_epirs(new_pos))[0], - rs.Get(i).data(), - ms.Get(i).data(), - i - ); - } - return premix; -} - -template <typename T> cmix_proto::PrePre fill_precomputation_pre_message(CMixContext& ctx, T const& rs, T const& ms) { if(start_mix(&ctx, rs.size()) != no_error) { exit(-1); @@ -148,7 +126,7 @@ cmix_proto::PrePre fill_precomputation_pre_message(CMixContext& ctx, T const& rs cmix_proto::PrePre prepre; - for(int i = 0; i < rs.size(); ++i) { + for(size_t i = 0; i < ctx.nr_participants; ++i) { size_t len = get_group_element_array_size(&ctx); prepre.mutable_m_er(i)->resize(len); @@ -168,6 +146,41 @@ cmix_proto::PrePre fill_precomputation_pre_message(CMixContext& ctx, T const& rs return prepre; } +template <typename T> +cmix_proto::PreMix fill_precomputation_mix_message(CMixContext const& ctx, T const& rs, T const& ms) { + cmix_proto::PreMix premix; + for(size_t i = 0; i < ctx.nr_participants; ++i) { + auto new_pos = ctx.permutation[i]; + size_t el_len = get_group_element_array_size(&ctx); + + premix.mutable_r_epirs(new_pos)->resize(el_len); + premix.mutable_m_epirs(new_pos)->resize(el_len); + + multiply_s( + &ctx, + &(*premix.mutable_r_epirs(new_pos))[0], + &(*premix.mutable_m_epirs(new_pos))[0], + rs.Get(i).data(), + ms.Get(i).data(), + i + ); + } + return premix; +} + +template <typename T> +cmix_proto::PrePost fill_precomputation_post_message(CMixContext& ctx, T const& rs, T const& ms) { + cmix_proto::PrePost prepost; + + for(size_t i = 0; i < ctx.nr_participants; ++i) { + post_process(&ctx, rs.Get(i).data(), ms.Get(i).data(), i); + *prepost.mutable_r_epirs(i) = rs.Get(i); + *prepost.mutable_m_epirs(i) = ms.Get(i); + } + + return prepost; +} + void Node::handle_node_prepre(cmix_proto::PrePre const& pre) { if(network_settings.is_first) { cmix_proto::PreMix premix = fill_precomputation_mix_message(cmix_ctx, pre.r_er(), pre.m_er()); @@ -180,13 +193,23 @@ void Node::handle_node_prepre(cmix_proto::PrePre const& pre) { void Node::handle_node_premix(cmix_proto::PreMix const& premix) { if(network_settings.is_first) { - + cmix_proto::PrePost prepost = fill_precomputation_post_message(cmix_ctx, premix.r_epirs(), premix.m_epirs()); + next_node.async_send(prepost); } else { cmix_proto::PreMix n_premix = fill_precomputation_mix_message(cmix_ctx, premix.r_epirs(), premix.m_epirs()); next_node.async_send(n_premix); } } +void Node::handle_node_prepost(cmix_proto::PrePost const& prepost) { + if(network_settings.is_first) { + + } else { + cmix_proto::PrePost n_prepost = fill_precomputation_post_message(cmix_ctx, prepost.r_epirs(), prepost.m_epirs()); + next_node.async_send(n_prepost); + } +} + void Node::handle_node_message(cmix_proto::CMixMessage message) { switch(message.contents_case()) { @@ -215,6 +238,11 @@ void Node::handle_node_message(cmix_proto::CMixMessage message) handle_node_premix(message.premix()); break; } + case cmix_proto::CMixMessage::ContentsCase::kPrepost: { + BOOST_LOG_TRIVIAL(trace) << "Handling PrePost"; + handle_node_prepost(message.prepost()); + break; + } default: { BOOST_LOG_TRIVIAL(error) << "handle_node_message: CMixMessage contains unknown contents."; } diff --git a/node/node.hpp b/node/node.hpp index 6587556..a143359 100644 --- a/node/node.hpp +++ b/node/node.hpp @@ -83,6 +83,7 @@ class Node void handle_node_secretkey(cmix_proto::SecretKey const& secret); void handle_node_prepre(cmix_proto::PrePre const& prepre); void handle_node_premix(cmix_proto::PreMix const& premix); + void handle_node_prepost(cmix_proto::PrePost const& prepost); void handle_node_message(cmix_proto::CMixMessage message); void handle_client_keyexchange(ClientConnections::key_type handle, cmix_proto::KeyExchange ke); |
