diff options
-rw-r--r-- | brandt.c | 82 | ||||
-rw-r--r-- | brandt.h | 99 | ||||
-rw-r--r-- | internals.h | 81 |
3 files changed, 188 insertions, 74 deletions
@@ -22,8 +22,62 @@ | |||
22 | #include <gcrypt.h> | 22 | #include <gcrypt.h> |
23 | 23 | ||
24 | #include "crypto.h" | 24 | #include "crypto.h" |
25 | #include "internals.h" | ||
25 | #include "util.h" | 26 | #include "util.h" |
26 | 27 | ||
28 | |||
29 | typedef int | ||
30 | (*msg_recv)(struct BRANDT_Auction *ad, | ||
31 | const unsigned char *buf, | ||
32 | size_t buflen, | ||
33 | uint16_t sender); | ||
34 | |||
35 | |||
36 | /** | ||
37 | * stores the function pointers to receive functions for each state. | ||
38 | * | ||
39 | * The first index denotes if a first price auction or a M+1st price auction is | ||
40 | * used. If it is 0, it is a first price auction, if it is 1, it is a M+1st | ||
41 | * price auction. | ||
42 | * | ||
43 | * The second index denotes if the outcome should be public or private. A value | ||
44 | * of 0 means a private outcome, while a value of 1 means public outcome. | ||
45 | */ | ||
46 | static msg_recv handler_in[2][2][msg_last] = | ||
47 | { | ||
48 | [0] = | ||
49 | { | ||
50 | [0] = | ||
51 | { | ||
52 | [msg_init] = smc_recv_keyshare, | ||
53 | [msg_bid] = smc_recv_encrypted_bid, | ||
54 | [msg_outcome] = fp_priv_recv_outcome, | ||
55 | [msg_decrypt] = fp_priv_recv_decryption, | ||
56 | }, | ||
57 | [1] = | ||
58 | { | ||
59 | [msg_init] = smc_recv_keyshare, | ||
60 | [msg_bid] = smc_recv_encrypted_bid, | ||
61 | [msg_outcome] = fp_pub_recv_outcome, | ||
62 | [msg_decrypt] = fp_pub_recv_decryption, | ||
63 | } | ||
64 | }, | ||
65 | [1] = | ||
66 | { | ||
67 | [0] = | ||
68 | { | ||
69 | [msg_init] = smc_recv_keyshare, | ||
70 | [msg_bid] = smc_recv_encrypted_bid, | ||
71 | }, | ||
72 | [1] = | ||
73 | { | ||
74 | [msg_init] = smc_recv_keyshare, | ||
75 | [msg_bid] = smc_recv_encrypted_bid, | ||
76 | } | ||
77 | } | ||
78 | }; | ||
79 | |||
80 | |||
27 | void | 81 | void |
28 | BRANDT_init () | 82 | BRANDT_init () |
29 | { | 83 | { |
@@ -37,7 +91,7 @@ BRANDT_init () | |||
37 | weprintf ("failed to set libgcrypt option DISABLE_SECMEM: %s", | 91 | weprintf ("failed to set libgcrypt option DISABLE_SECMEM: %s", |
38 | gcry_strerror (err)); | 92 | gcry_strerror (err)); |
39 | 93 | ||
40 | /* ecc is slow otherwise. */ | 94 | /* ecc is slow otherwise and we don't create long term keys anyway. */ |
41 | if ((err = gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0))) | 95 | if ((err = gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0))) |
42 | weprintf ("failed to set libgcrypt option ENABLE_QUICK_RANDOM: %s", | 96 | weprintf ("failed to set libgcrypt option ENABLE_QUICK_RANDOM: %s", |
43 | gcry_strerror (err)); | 97 | gcry_strerror (err)); |
@@ -45,3 +99,29 @@ BRANDT_init () | |||
45 | gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0); | 99 | gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0); |
46 | brandt_crypto_init (); | 100 | brandt_crypto_init (); |
47 | } | 101 | } |
102 | |||
103 | |||
104 | void | ||
105 | BRANDT_got_message (struct BRANDT_Auction *auction, | ||
106 | uint16_t sender, | ||
107 | const unsigned char *msg, | ||
108 | size_t msg_len) | ||
109 | { | ||
110 | uint16_t type = *(uint16_t *)msg; | ||
111 | int m = !!auction->desc->m; | ||
112 | int pub = !!auction->desc->outcome_public; | ||
113 | enum rounds round = auction->cur_round; | ||
114 | |||
115 | /** todo: cache out of order messages */ | ||
116 | |||
117 | if (!handler_in[m][pub][round] || | ||
118 | !handler_in[m][pub][round](auction, | ||
119 | msg + sizeof (type), | ||
120 | msg_len - sizeof (type), | ||
121 | sender)) | ||
122 | { | ||
123 | /** \todo */ | ||
124 | weprintf ("wow fail"); | ||
125 | } | ||
126 | msg + sizeof (type); | ||
127 | } | ||
@@ -23,19 +23,16 @@ | |||
23 | #ifndef _BRANDT_BRANDT_H | 23 | #ifndef _BRANDT_BRANDT_H |
24 | #define _BRANDT_BRANDT_H | 24 | #define _BRANDT_BRANDT_H |
25 | 25 | ||
26 | #include <unistd.h> | ||
27 | #include <stdint.h> | 26 | #include <stdint.h> |
27 | #include <unistd.h> | ||
28 | 28 | ||
29 | /** | 29 | /** defined in internals.h */ |
30 | * \todo. | ||
31 | */ | ||
32 | struct BRANDT_Auction; | 30 | struct BRANDT_Auction; |
33 | 31 | ||
34 | /** | 32 | /** |
35 | * Functions of this type are called by libbrandt to broadcast messages to the | 33 | * Functions of this type are called by libbrandt to broadcast messages to the |
36 | * blackboard of a specific auction. | 34 | * blackboard of a specific auction. They have to be sent using authenticated |
37 | * | 35 | * encryption. |
38 | * \todo: how must the message be handled? (encryption, auth, reliability, …) | ||
39 | * | 36 | * |
40 | * @param[in] auction_closure Closure pointer representing the respective | 37 | * @param[in] auction_closure Closure pointer representing the respective |
41 | * auction. This is the Pointer given to BRANDT_join(). | 38 | * auction. This is the Pointer given to BRANDT_join(). |
@@ -45,16 +42,15 @@ struct BRANDT_Auction; | |||
45 | * @return 0 on success, -1 on failure. | 42 | * @return 0 on success, -1 on failure. |
46 | */ | 43 | */ |
47 | typedef int | 44 | typedef int |
48 | (*BRANDT_BroadcastCallback)(void * auction_closure, | 45 | (*BRANDT_CbBroadcast)(void *auction_closure, |
49 | const void *msg, | 46 | const void *msg, |
50 | size_t msg_len); | 47 | size_t msg_len); |
51 | 48 | ||
52 | 49 | ||
53 | /** | 50 | /** |
54 | * Functions of this type are called by libbrandt to unicast messages to the | 51 | * Functions of this type are called by libbrandt to unicast messages to the |
55 | * seller of a specific auction. | 52 | * seller of a specific auction. They have to be sent using authenticated |
56 | * | 53 | * encryption. |
57 | * \todo: how must the message be handled? (encryption, auth, reliability, …) | ||
58 | * | 54 | * |
59 | * @param[in] auction_closure Closure pointer representing the respective | 55 | * @param[in] auction_closure Closure pointer representing the respective |
60 | * auction. This is the Pointer given to BRANDT_join(). | 56 | * auction. This is the Pointer given to BRANDT_join(). |
@@ -63,16 +59,15 @@ typedef int | |||
63 | * @return 0 on success, -1 on failure. | 59 | * @return 0 on success, -1 on failure. |
64 | */ | 60 | */ |
65 | typedef int | 61 | typedef int |
66 | (*BRANDT_UnicastSellerCallback)(void * auction_closure, | 62 | (*BRANDT_CbUnicast)(void *auction_closure, |
67 | const void *msg, | 63 | const void *msg, |
68 | size_t msg_len); | 64 | size_t msg_len); |
69 | 65 | ||
70 | 66 | ||
71 | /** | 67 | /** |
72 | * Functions of this type are called by libbrandt to report the auction outcome | 68 | * Functions of this type are called by libbrandt to report the auction outcome |
73 | * or malicious/erroneous participants. | 69 | * or malicious/erroneous participants. |
74 | * | 70 | * |
75 | * \todo: update price type. | ||
76 | * \todo: export proof of erroneous behaviour. | 71 | * \todo: export proof of erroneous behaviour. |
77 | * | 72 | * |
78 | * @param[in] auction_closure Closure pointer representing the respective | 73 | * @param[in] auction_closure Closure pointer representing the respective |
@@ -84,10 +79,10 @@ typedef int | |||
84 | * is private and the user did not win. | 79 | * is private and the user did not win. |
85 | */ | 80 | */ |
86 | typedef void | 81 | typedef void |
87 | (*BRANDT_ReportResultCallback)(void * auction_closure, | 82 | (*BRANDT_CbResult)(void *auction_closure, |
88 | unsigned int bidder_id, | 83 | unsigned int bidder_id, |
89 | int status, | 84 | int status, |
90 | uint16_t price); | 85 | uint16_t price); |
91 | 86 | ||
92 | 87 | ||
93 | void | 88 | void |
@@ -98,7 +93,7 @@ BRANDT_init (); | |||
98 | * | 93 | * |
99 | * @param[in] broadcast Pointer to the broadcast callback function | 94 | * @param[in] broadcast Pointer to the broadcast callback function |
100 | * @param[in] unicast Pointer to the unicast callback function | 95 | * @param[in] unicast Pointer to the unicast callback function |
101 | * @param[in] report Pointer to the report callback function | 96 | * @param[in] result Pointer to the result callback function |
102 | * @param[in] auction_closure Closure pointer representing the auction. This | 97 | * @param[in] auction_closure Closure pointer representing the auction. This |
103 | * will not be touched by libbrandt. It is only passed to the callbacks. | 98 | * will not be touched by libbrandt. It is only passed to the callbacks. |
104 | * @param[in] auction_data The auction information data a an opaque data | 99 | * @param[in] auction_data The auction information data a an opaque data |
@@ -110,12 +105,12 @@ BRANDT_init (); | |||
110 | * black-box pointer, do NOT access/change it or the data it points to! | 105 | * black-box pointer, do NOT access/change it or the data it points to! |
111 | */ | 106 | */ |
112 | struct BRANDT_Auction * | 107 | struct BRANDT_Auction * |
113 | BRANDT_join (BRANDT_BroadcastCallback broadcast, | 108 | BRANDT_join (BRANDT_CbBroadcast broadcast, |
114 | BRANDT_UnicastSellerCallback unicast, | 109 | BRANDT_CbUnicast unicast, |
115 | BRANDT_ReportResultCallback report, | 110 | BRANDT_CbResult result, |
116 | const void * auction_closure, | 111 | const void *auction_closure, |
117 | const void * auction_data, | 112 | const void *auction_data, |
118 | size_t auction_data_len); | 113 | size_t auction_data_len); |
119 | /* \todo: where do I specify my bid? */ | 114 | /* \todo: where do I specify my bid? */ |
120 | 115 | ||
121 | 116 | ||
@@ -132,7 +127,7 @@ BRANDT_join (BRANDT_BroadcastCallback broadcast, | |||
132 | * Create a new auction described by the @a auction_data parameter. | 127 | * Create a new auction described by the @a auction_data parameter. |
133 | * | 128 | * |
134 | * @param[in] broadcast Pointer to the broadcast callback function | 129 | * @param[in] broadcast Pointer to the broadcast callback function |
135 | * @param[in] report Pointer to the report callback function | 130 | * @param[in] result Pointer to the result callback function |
136 | * @param[in] auction_closure Closure pointer representing the auction. This | 131 | * @param[in] auction_closure Closure pointer representing the auction. This |
137 | * will not be touched by libbrandt. It is only passed to the callbacks. | 132 | * will not be touched by libbrandt. It is only passed to the callbacks. |
138 | * @param[out] auction_data The auction information data a an opaque data | 133 | * @param[out] auction_data The auction information data a an opaque data |
@@ -155,45 +150,35 @@ BRANDT_join (BRANDT_BroadcastCallback broadcast, | |||
155 | * black-box pointer, do NOT access/change it or the data it points to! | 150 | * black-box pointer, do NOT access/change it or the data it points to! |
156 | */ | 151 | */ |
157 | struct BRANDT_Auction * | 152 | struct BRANDT_Auction * |
158 | BRANDT_new (BRANDT_BroadcastCallback broadcast, | 153 | BRANDT_new (BRANDT_CbBroadcast broadcast, |
159 | BRANDT_ReportResultCallback report, | 154 | BRANDT_CbResult result, |
160 | const void * auction_closure, | 155 | void *auction_closure, |
161 | const void ** auction_data, | 156 | void **auction_data, |
162 | size_t * auction_data_len, | 157 | size_t *auction_data_len, |
163 | uint16_t num_prices, | 158 | uint16_t num_prices, |
164 | uint16_t m, | 159 | uint16_t m, |
165 | int outcome_public); | 160 | int outcome_public); |
166 | 161 | ||
167 | 162 | ||
168 | /** | 163 | /** \todo */ |
169 | * Receive a broadcast message related to a specific auction. | ||
170 | * | ||
171 | * @param[in] auction The pointer returned by BRANDT_join() or BRANDT_new() from | ||
172 | * which message @a msg was received. | ||
173 | * @param[in] msg The message that was received. | ||
174 | * @param[in] msg_len The length in bytes of @a msg. | ||
175 | */ | ||
176 | void | 164 | void |
177 | BRANDT_got_broadcast (struct BRANDT_Auction *auction, | 165 | BRANDT_free (); |
178 | void * msg, | ||
179 | size_t msg_len); | ||
180 | 166 | ||
181 | 167 | ||
182 | /** | 168 | /** |
183 | * Receive a unicast message from a bidder related to a specific auction. | 169 | * Receive a message related to a specific auction. |
184 | * | 170 | * |
185 | * @param[in] auction The pointer returned by BRANDT_new() from which message | 171 | * @param[in] auction The pointer returned by BRANDT_join() or BRANDT_new() from |
186 | * @a msg was received. | 172 | * which message @a msg was received. |
173 | * @param[in] sender The id of the sender. | ||
187 | * @param[in] msg The message that was received. | 174 | * @param[in] msg The message that was received. |
188 | * @param[in] msg_len The length in bytes of @a msg. | 175 | * @param[in] msg_len The length in bytes of @a msg. |
189 | * \todo: how to link message to sender id within auction? | ||
190 | * ANSWER: on start, know that we have 'n' participants, here give | ||
191 | * participant number (1..n) | ||
192 | */ | 176 | */ |
193 | void | 177 | void |
194 | BRANDT_got_unicast (struct BRANDT_Auction *auction, | 178 | BRANDT_got_message (struct BRANDT_Auction *auction, |
195 | void * msg, | 179 | uint16_t sender, |
196 | size_t msg_len); | 180 | const unsigned char *msg, |
181 | size_t msg_len); | ||
197 | 182 | ||
198 | 183 | ||
199 | /**\todo: Error handling functions? */ | 184 | /**\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 @@ | |||
25 | 25 | ||
26 | #include <gcrypt.h> | 26 | #include <gcrypt.h> |
27 | 27 | ||
28 | struct AuctionData { | 28 | #include "brandt.h" |
29 | uint16_t n; /** The amount of bidders/agents */ | 29 | |
30 | uint16_t k; /** The amount of possible prices */ | 30 | |
31 | uint16_t i; /** Own agents index, only used when bidding */ | 31 | enum rounds { |
32 | uint16_t b; /** Own bid */ | 32 | msg_init, |
33 | 33 | msg_bid, | |
34 | gcry_mpi_t x; /** Own private additive key share */ | 34 | msg_outcome, |
35 | gcry_mpi_point_t *y; /** public multiplicative key shares, size: n */ | 35 | msg_decrypt, |
36 | gcry_mpi_point_t Y; /** Shared public key */ | 36 | msg_last |
37 | 37 | }; | |
38 | gcry_mpi_point_t **alpha; /** alphas, size: n*k */ | 38 | |
39 | gcry_mpi_point_t **beta; /** betas, size: n*k */ | 39 | |
40 | 40 | /** | |
41 | gcry_mpi_point_t ***gamma; /** gamma, size: n*n*k */ | 41 | * This struct describes an auction and has to be followed by #description_len |
42 | gcry_mpi_point_t ***delta; /** delta, size: n*n*k */ | 42 | * bytes of arbitrary data where the description of the item to be sold is |
43 | gcry_mpi_point_t ***phi; /** phi, size: n*n*k */ | 43 | * stored. */ |
44 | struct AuctionDescr { | ||
45 | /** The length of the description in bytes directly following this struct */ | ||
46 | uint32_t description_len; | ||
47 | |||
48 | /** Auction type. 0 means first price Auction, >= 0 means M+1st price | ||
49 | * auction with an amount of m items being sold. */ | ||
50 | uint16_t m; | ||
51 | |||
52 | /** Outcome type. 0 means private outcome, everything else means public | ||
53 | * outcome. */ | ||
54 | uint16_t outcome_public; | ||
55 | |||
56 | /** The amount of possible prices */ | ||
57 | uint16_t price_range; | ||
58 | }; | ||
59 | |||
60 | |||
61 | struct BRANDT_Auction { | ||
62 | struct AuctionDescr *desc; /** pointer to the auction information */ | ||
63 | |||
64 | BRANDT_CbBroadcast bcast; /** broadcast callback */ | ||
65 | BRANDT_CbUnicast ucast; /** unicast callback */ | ||
66 | BRANDT_CbResult result; /** result reporting callback */ | ||
67 | |||
68 | int seller_mode; /** If 0 we are bidding, selling otherwise */ | ||
69 | enum rounds cur_round; /** The round we expect messages from */ | ||
70 | gcry_mpi_t round_progress; /** Stores which round messages were received */ | ||
71 | |||
72 | uint16_t n; /** The amount of bidders/agents */ | ||
73 | uint16_t k; /** The amount of possible prices */ | ||
74 | uint16_t i; /** Own agents index, only used when bidding */ | ||
75 | uint16_t b; /** Own bid */ | ||
76 | |||
77 | gcry_mpi_t x; /** Own private additive key share */ | ||
78 | gcry_mpi_point_t *y; /** public multiplicative key shares, size: n */ | ||
79 | gcry_mpi_point_t Y; /** Shared public key */ | ||
80 | |||
81 | gcry_mpi_point_t **alpha; /** alphas, size: n*k */ | ||
82 | gcry_mpi_point_t **beta; /** betas, size: n*k */ | ||
83 | |||
84 | gcry_mpi_point_t **gamma2; /** gamma2, for public outcome, size: n*k */ | ||
85 | gcry_mpi_point_t ***gamma3; /** gamma3, for private outcome, size: n*n*k */ | ||
86 | gcry_mpi_point_t **delta2; /** delta2, for public outcome, size: n*k */ | ||
87 | gcry_mpi_point_t ***delta3; /** delta3, for private outcome, size: n*n*k */ | ||
88 | gcry_mpi_point_t **phi2; /** phi2, for public outcome, size: n*k */ | ||
89 | gcry_mpi_point_t ***phi3; /** phi3, for private outcome, size: n*n*k */ | ||
90 | |||
91 | gcry_mpi_point_t *tmpa1; /** used for temporary storage, size: k */ | ||
92 | gcry_mpi_point_t *tmpb1; /** used for temporary storage, size: k */ | ||
44 | }; | 93 | }; |
45 | 94 | ||
46 | #endif /* ifndef _BRANDT_INTERNALS_H */ | 95 | #endif /* ifndef _BRANDT_INTERNALS_H */ |