#pragma once #ifdef __cplusplus extern "C" { #endif #include "api.h" #include "keypair.h" #include "groupelement.h" #include "bignum.h" /*! * \file */ /** * \struct * \brief The CMixBuffer struct is the temporary storage of messages in each Node. * On this buffer, operations like decrypt and permute are performed. */ struct CMixBuffer { char* buffer; ///< The actual buffer unsigned int nr_messages; ///< The number of messages in the buffer unsigned int message_length; ///< The length of each message in the buffer }; /*! * \brief The cmix_error enum describes the output state of a each of the cmix functions */ enum cmix_error { no_error = 0, index_out_of_range = 1000, cmix_bignum_error = 2000, out_of_memory = 4000, }; /*! * \brief permutate mixes the messages before sending the messages. * \param[in,out] b A buffer of \p nr_messages messages, each message of length *message_len* * \return no_error */ enum cmix_error permute(struct CMixBuffer b); /*! * \brief get_message takes the \p index message of the buffer \p b copies it into \p message * \param[out] message is the output buffer that has to hold at least \p b.message_length bytes. * \param[in] b is the buffer from which to copy the message. * \param[in] index is the index of the message to copy. * \return index_out_of_range if \p index is greater than or equal to \p b.nr_messages , otherwise no_error. */ enum cmix_error get_message(char* message, struct CMixBuffer b, unsigned int index); /*! * \brief set_message set the \p index message of the buffer to a copy of \p message. * \param[in] message The message to set the part of the buffer to. * \param[in,out] b The buffer * \param[in] index The index into the buffer * \return index_out_of_range if \p index is greater than or equal to \p b.nr_messages , otherwise no_error. */ enum cmix_error set_message(char const* message, struct CMixBuffer b, unsigned int index); /*! * \brief calculate_shared_key_part Calculates (partly) the shared key which is needed by all the nodes. * \param result Storage for the result * \param partial_shared The shared key so far. * \param my_share My part to add to the key. * \param mod The modulus of the group we are in. * \return An error_code. */ enum cmix_error calculate_shared_key_part(struct Bignum* result, struct Bignum partial_shared, struct Bignum my_share, struct Bignum mod); struct CMixContext { struct Api api; struct KeyPair keypair; GroupElement network_key; size_t nr_participants; GroupElement* r; GroupElement* s; unsigned int* permutation; GroupElement* pirs; }; #ifndef NDEBUG void element_to_buffer(struct CMixContext const* ctx, char* buffer, GroupElement const element); #endif struct CMixContext initialize_cmix_context(struct Api api); void deinitialize(struct CMixContext* ctx); enum cmix_error initialize_keypair(struct CMixContext* ctx); enum cmix_error get_public_key(struct CMixContext const* ctx, char* buffer); enum cmix_error add_public_share(struct CMixContext const* ctx, char* buffer, char const* share); enum cmix_error get_pub_key_hash(struct CMixContext const* ctx, char** buffer); enum cmix_error start_mix(struct CMixContext* ctx, size_t nr_participants); enum cmix_error initialize_mix_randomness(struct CMixContext* ctx); enum cmix_error generate_random_message(struct CMixContext* ctx, char* buffer); size_t get_group_element_array_size(struct CMixContext const* ctx); enum cmix_error set_network_key(struct CMixContext* ctx, char const* buffer, size_t len); enum cmix_error encrypt_r(struct CMixContext const* ctx, char* random_buffer, char* message_buffer, size_t index); enum cmix_error encrypt_r_and_multiply(struct CMixContext const* ctx, char* random_buffer, char* message_buffer, char const* random_element, char const* message_element, size_t index); enum cmix_error permute_and_multiply_encrypted_s(struct CMixContext const* ctx, char** random_buffer, char** message_buffer, char const** random_element, char const** message_element, size_t nr_elements); enum cmix_error multiply_s(struct CMixContext const* ctx, char* out_buffer, char const* message, size_t index); enum cmix_error get_key_exchange_value(struct CMixContext const* ctx, char* buffer, GroupElement priv_element); enum cmix_error key_exchange_init(struct CMixContext const* ctx, char* pubkey_buffer, char* value_buffer, GroupElement* secret_value); 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 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); enum cmix_error enqueue_message(struct CMixContext* ctx, char const* message, size_t index); enum cmix_error enqueue_random_message(struct CMixContext* ctx, size_t index); enum cmix_error swap_k_for_r(struct CMixContext const* ctx, char* out_buffer, char const* message, GroupElement const key, size_t index); enum cmix_error remove_r_and_s(struct CMixContext const* ctx, char* out_buffer, char const* message, size_t index); #ifdef __cplusplus } // extern "C" #endif