diff --git a/brandt.c b/brandt.c index c5d5a61..d56a607 100644 --- a/brandt.c +++ b/brandt.c @@ -22,8 +22,62 @@ #include #include "crypto.h" +#include "internals.h" #include "util.h" + +typedef int +(*msg_recv)(struct BRANDT_Auction *ad, + const unsigned char *buf, + size_t buflen, + uint16_t sender); + + +/** + * stores the function pointers to receive functions for each state. + * + * The first index denotes if a first price auction or a M+1st price auction is + * used. If it is 0, it is a first price auction, if it is 1, it is a M+1st + * price auction. + * + * The second index denotes if the outcome should be public or private. A value + * of 0 means a private outcome, while a value of 1 means public outcome. + */ +static msg_recv handler_in[2][2][msg_last] = +{ + [0] = + { + [0] = + { + [msg_init] = smc_recv_keyshare, + [msg_bid] = smc_recv_encrypted_bid, + [msg_outcome] = fp_priv_recv_outcome, + [msg_decrypt] = fp_priv_recv_decryption, + }, + [1] = + { + [msg_init] = smc_recv_keyshare, + [msg_bid] = smc_recv_encrypted_bid, + [msg_outcome] = fp_pub_recv_outcome, + [msg_decrypt] = fp_pub_recv_decryption, + } + }, + [1] = + { + [0] = + { + [msg_init] = smc_recv_keyshare, + [msg_bid] = smc_recv_encrypted_bid, + }, + [1] = + { + [msg_init] = smc_recv_keyshare, + [msg_bid] = smc_recv_encrypted_bid, + } + } +}; + + void BRANDT_init () { @@ -37,7 +91,7 @@ BRANDT_init () weprintf ("failed to set libgcrypt option DISABLE_SECMEM: %s", gcry_strerror (err)); - /* ecc is slow otherwise. */ + /* ecc is slow otherwise and we don't create long term keys anyway. */ if ((err = gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0))) weprintf ("failed to set libgcrypt option ENABLE_QUICK_RANDOM: %s", gcry_strerror (err)); @@ -45,3 +99,29 @@ BRANDT_init () gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0); brandt_crypto_init (); } + + +void +BRANDT_got_message (struct BRANDT_Auction *auction, + uint16_t sender, + const unsigned char *msg, + size_t msg_len) +{ + uint16_t type = *(uint16_t *)msg; + int m = !!auction->desc->m; + int pub = !!auction->desc->outcome_public; + enum rounds round = auction->cur_round; + + /** todo: cache out of order messages */ + + if (!handler_in[m][pub][round] || + !handler_in[m][pub][round](auction, + msg + sizeof (type), + msg_len - sizeof (type), + sender)) + { + /** \todo */ + weprintf ("wow fail"); + } + msg + sizeof (type); +} diff --git a/brandt.h b/brandt.h index e66866d..dd1c8a0 100644 --- a/brandt.h +++ b/brandt.h @@ -23,19 +23,16 @@ #ifndef _BRANDT_BRANDT_H #define _BRANDT_BRANDT_H -#include #include +#include -/** - * \todo. - */ +/** defined in internals.h */ struct BRANDT_Auction; /** * Functions of this type are called by libbrandt to broadcast messages to the - * blackboard of a specific auction. - * - * \todo: how must the message be handled? (encryption, auth, reliability, …) + * blackboard of a specific auction. They have to be sent using authenticated + * encryption. * * @param[in] auction_closure Closure pointer representing the respective * auction. This is the Pointer given to BRANDT_join(). @@ -45,16 +42,15 @@ struct BRANDT_Auction; * @return 0 on success, -1 on failure. */ typedef int -(*BRANDT_BroadcastCallback)(void * auction_closure, - const void *msg, - size_t msg_len); +(*BRANDT_CbBroadcast)(void *auction_closure, + const void *msg, + size_t msg_len); /** * Functions of this type are called by libbrandt to unicast messages to the - * seller of a specific auction. - * - * \todo: how must the message be handled? (encryption, auth, reliability, …) + * seller of a specific auction. They have to be sent using authenticated + * encryption. * * @param[in] auction_closure Closure pointer representing the respective * auction. This is the Pointer given to BRANDT_join(). @@ -63,16 +59,15 @@ typedef int * @return 0 on success, -1 on failure. */ typedef int -(*BRANDT_UnicastSellerCallback)(void * auction_closure, - const void *msg, - size_t msg_len); +(*BRANDT_CbUnicast)(void *auction_closure, + const void *msg, + size_t msg_len); /** * Functions of this type are called by libbrandt to report the auction outcome * or malicious/erroneous participants. * - * \todo: update price type. * \todo: export proof of erroneous behaviour. * * @param[in] auction_closure Closure pointer representing the respective @@ -84,10 +79,10 @@ typedef int * is private and the user did not win. */ typedef void -(*BRANDT_ReportResultCallback)(void * auction_closure, - unsigned int bidder_id, - int status, - uint16_t price); +(*BRANDT_CbResult)(void *auction_closure, + unsigned int bidder_id, + int status, + uint16_t price); void @@ -98,7 +93,7 @@ BRANDT_init (); * * @param[in] broadcast Pointer to the broadcast callback function * @param[in] unicast Pointer to the unicast callback function - * @param[in] report Pointer to the report callback function + * @param[in] result Pointer to the result callback function * @param[in] auction_closure Closure pointer representing the auction. This * will not be touched by libbrandt. It is only passed to the callbacks. * @param[in] auction_data The auction information data a an opaque data @@ -110,12 +105,12 @@ BRANDT_init (); * black-box pointer, do NOT access/change it or the data it points to! */ struct BRANDT_Auction * -BRANDT_join (BRANDT_BroadcastCallback broadcast, - BRANDT_UnicastSellerCallback unicast, - BRANDT_ReportResultCallback report, - const void * auction_closure, - const void * auction_data, - size_t auction_data_len); +BRANDT_join (BRANDT_CbBroadcast broadcast, + BRANDT_CbUnicast unicast, + BRANDT_CbResult result, + const void *auction_closure, + const void *auction_data, + size_t auction_data_len); /* \todo: where do I specify my bid? */ @@ -132,7 +127,7 @@ BRANDT_join (BRANDT_BroadcastCallback broadcast, * Create a new auction described by the @a auction_data parameter. * * @param[in] broadcast Pointer to the broadcast callback function - * @param[in] report Pointer to the report callback function + * @param[in] result Pointer to the result callback function * @param[in] auction_closure Closure pointer representing the auction. This * will not be touched by libbrandt. It is only passed to the callbacks. * @param[out] auction_data The auction information data a an opaque data @@ -155,45 +150,35 @@ BRANDT_join (BRANDT_BroadcastCallback broadcast, * black-box pointer, do NOT access/change it or the data it points to! */ struct BRANDT_Auction * -BRANDT_new (BRANDT_BroadcastCallback broadcast, - BRANDT_ReportResultCallback report, - const void * auction_closure, - const void ** auction_data, - size_t * auction_data_len, - uint16_t num_prices, - uint16_t m, - int outcome_public); +BRANDT_new (BRANDT_CbBroadcast broadcast, + BRANDT_CbResult result, + void *auction_closure, + void **auction_data, + size_t *auction_data_len, + uint16_t num_prices, + uint16_t m, + int outcome_public); + + +/** \todo */ +void +BRANDT_free (); /** - * Receive a broadcast message related to a specific auction. + * Receive a message related to a specific auction. * * @param[in] auction The pointer returned by BRANDT_join() or BRANDT_new() from * which message @a msg was received. + * @param[in] sender The id of the sender. * @param[in] msg The message that was received. * @param[in] msg_len The length in bytes of @a msg. */ void -BRANDT_got_broadcast (struct BRANDT_Auction *auction, - void * msg, - size_t msg_len); - - -/** - * Receive a unicast message from a bidder related to a specific auction. - * - * @param[in] auction The pointer returned by BRANDT_new() from which message - * @a msg was received. - * @param[in] msg The message that was received. - * @param[in] msg_len The length in bytes of @a msg. - * \todo: how to link message to sender id within auction? - * ANSWER: on start, know that we have 'n' participants, here give - * participant number (1..n) - */ -void -BRANDT_got_unicast (struct BRANDT_Auction *auction, - void * msg, - size_t msg_len); +BRANDT_got_message (struct BRANDT_Auction *auction, + uint16_t sender, + const unsigned char *msg, + size_t msg_len); /**\todo: Error handling functions? */ diff --git a/internals.h b/internals.h index 1e6eb18..7dab0d6 100644 --- a/internals.h +++ b/internals.h @@ -25,22 +25,71 @@ #include -struct AuctionData { - uint16_t n; /** The amount of bidders/agents */ - uint16_t k; /** The amount of possible prices */ - uint16_t i; /** Own agents index, only used when bidding */ - uint16_t b; /** Own bid */ +#include "brandt.h" - gcry_mpi_t x; /** Own private additive key share */ - gcry_mpi_point_t *y; /** public multiplicative key shares, size: n */ - gcry_mpi_point_t Y; /** Shared public key */ - gcry_mpi_point_t **alpha; /** alphas, size: n*k */ - gcry_mpi_point_t **beta; /** betas, size: n*k */ +enum rounds { + msg_init, + msg_bid, + msg_outcome, + msg_decrypt, + msg_last +}; - gcry_mpi_point_t ***gamma; /** gamma, size: n*n*k */ - gcry_mpi_point_t ***delta; /** delta, size: n*n*k */ - gcry_mpi_point_t ***phi; /** phi, size: n*n*k */ + +/** + * This struct describes an auction and has to be followed by #description_len + * bytes of arbitrary data where the description of the item to be sold is + * stored. */ +struct AuctionDescr { + /** The length of the description in bytes directly following this struct */ + uint32_t description_len; + + /** Auction type. 0 means first price Auction, >= 0 means M+1st price + * auction with an amount of m items being sold. */ + uint16_t m; + + /** Outcome type. 0 means private outcome, everything else means public + * outcome. */ + uint16_t outcome_public; + + /** The amount of possible prices */ + uint16_t price_range; +}; + + +struct BRANDT_Auction { + struct AuctionDescr *desc; /** pointer to the auction information */ + + BRANDT_CbBroadcast bcast; /** broadcast callback */ + BRANDT_CbUnicast ucast; /** unicast callback */ + BRANDT_CbResult result; /** result reporting callback */ + + int seller_mode; /** If 0 we are bidding, selling otherwise */ + enum rounds cur_round; /** The round we expect messages from */ + gcry_mpi_t round_progress; /** Stores which round messages were received */ + + uint16_t n; /** The amount of bidders/agents */ + uint16_t k; /** The amount of possible prices */ + uint16_t i; /** Own agents index, only used when bidding */ + uint16_t b; /** Own bid */ + + gcry_mpi_t x; /** Own private additive key share */ + gcry_mpi_point_t *y; /** public multiplicative key shares, size: n */ + gcry_mpi_point_t Y; /** Shared public key */ + + gcry_mpi_point_t **alpha; /** alphas, size: n*k */ + gcry_mpi_point_t **beta; /** betas, size: n*k */ + + gcry_mpi_point_t **gamma2; /** gamma2, for public outcome, size: n*k */ + gcry_mpi_point_t ***gamma3; /** gamma3, for private outcome, size: n*n*k */ + gcry_mpi_point_t **delta2; /** delta2, for public outcome, size: n*k */ + gcry_mpi_point_t ***delta3; /** delta3, for private outcome, size: n*n*k */ + gcry_mpi_point_t **phi2; /** phi2, for public outcome, size: n*k */ + gcry_mpi_point_t ***phi3; /** phi3, for private outcome, size: n*n*k */ + + gcry_mpi_point_t *tmpa1; /** used for temporary storage, size: k */ + gcry_mpi_point_t *tmpb1; /** used for temporary storage, size: k */ }; #endif /* ifndef _BRANDT_INTERNALS_H */