diff options
Diffstat (limited to 'src/secretsharing/secretsharing_api.c')
-rw-r--r-- | src/secretsharing/secretsharing_api.c | 350 |
1 files changed, 184 insertions, 166 deletions
diff --git a/src/secretsharing/secretsharing_api.c b/src/secretsharing/secretsharing_api.c index da9e14d1e..4e35bf87a 100644 --- a/src/secretsharing/secretsharing_api.c +++ b/src/secretsharing/secretsharing_api.c | |||
@@ -30,13 +30,14 @@ | |||
30 | #include <gcrypt.h> | 30 | #include <gcrypt.h> |
31 | 31 | ||
32 | 32 | ||
33 | #define LOG(kind, ...) GNUNET_log_from(kind, "secretsharing-api", __VA_ARGS__) | 33 | #define LOG(kind, ...) GNUNET_log_from (kind, "secretsharing-api", __VA_ARGS__) |
34 | 34 | ||
35 | /** | 35 | /** |
36 | * Session that will eventually establish a shared secred between | 36 | * Session that will eventually establish a shared secred between |
37 | * the involved peers and allow encryption and cooperative decryption. | 37 | * the involved peers and allow encryption and cooperative decryption. |
38 | */ | 38 | */ |
39 | struct GNUNET_SECRETSHARING_Session { | 39 | struct GNUNET_SECRETSHARING_Session |
40 | { | ||
40 | /** | 41 | /** |
41 | * Message queue for @e client. | 42 | * Message queue for @e client. |
42 | */ | 43 | */ |
@@ -57,7 +58,8 @@ struct GNUNET_SECRETSHARING_Session { | |||
57 | /** | 58 | /** |
58 | * Handle to cancel a cooperative decryption operation. | 59 | * Handle to cancel a cooperative decryption operation. |
59 | */ | 60 | */ |
60 | struct GNUNET_SECRETSHARING_DecryptionHandle { | 61 | struct GNUNET_SECRETSHARING_DecryptionHandle |
62 | { | ||
61 | /** | 63 | /** |
62 | * Message queue for @e client. | 64 | * Message queue for @e client. |
63 | */ | 65 | */ |
@@ -98,17 +100,20 @@ static gcry_mpi_t elgamal_g; | |||
98 | * Function to initialize #elgamal_q, #elgamal_p and #elgamal_g. | 100 | * Function to initialize #elgamal_q, #elgamal_p and #elgamal_g. |
99 | */ | 101 | */ |
100 | static void | 102 | static void |
101 | ensure_elgamal_initialized(void) | 103 | ensure_elgamal_initialized (void) |
102 | { | 104 | { |
103 | if (NULL != elgamal_q) | 105 | if (NULL != elgamal_q) |
104 | return; /* looks like crypto is already initialized */ | 106 | return; /* looks like crypto is already initialized */ |
105 | 107 | ||
106 | GNUNET_assert(0 == gcry_mpi_scan(&elgamal_q, GCRYMPI_FMT_HEX, | 108 | GNUNET_assert (0 == gcry_mpi_scan (&elgamal_q, GCRYMPI_FMT_HEX, |
107 | GNUNET_SECRETSHARING_ELGAMAL_Q_HEX, 0, NULL)); | 109 | GNUNET_SECRETSHARING_ELGAMAL_Q_HEX, 0, |
108 | GNUNET_assert(0 == gcry_mpi_scan(&elgamal_p, GCRYMPI_FMT_HEX, | 110 | NULL)); |
109 | GNUNET_SECRETSHARING_ELGAMAL_P_HEX, 0, NULL)); | 111 | GNUNET_assert (0 == gcry_mpi_scan (&elgamal_p, GCRYMPI_FMT_HEX, |
110 | GNUNET_assert(0 == gcry_mpi_scan(&elgamal_g, GCRYMPI_FMT_HEX, | 112 | GNUNET_SECRETSHARING_ELGAMAL_P_HEX, 0, |
111 | GNUNET_SECRETSHARING_ELGAMAL_G_HEX, 0, NULL)); | 113 | NULL)); |
114 | GNUNET_assert (0 == gcry_mpi_scan (&elgamal_g, GCRYMPI_FMT_HEX, | ||
115 | GNUNET_SECRETSHARING_ELGAMAL_G_HEX, 0, | ||
116 | NULL)); | ||
112 | } | 117 | } |
113 | 118 | ||
114 | 119 | ||
@@ -120,13 +125,13 @@ ensure_elgamal_initialized(void) | |||
120 | * @param error error code | 125 | * @param error error code |
121 | */ | 126 | */ |
122 | static void | 127 | static void |
123 | handle_session_client_error(void *cls, | 128 | handle_session_client_error (void *cls, |
124 | enum GNUNET_MQ_Error error) | 129 | enum GNUNET_MQ_Error error) |
125 | { | 130 | { |
126 | struct GNUNET_SECRETSHARING_Session *s = cls; | 131 | struct GNUNET_SECRETSHARING_Session *s = cls; |
127 | 132 | ||
128 | s->secret_ready_cb(s->secret_ready_cls, NULL, NULL, 0, NULL); | 133 | s->secret_ready_cb (s->secret_ready_cls, NULL, NULL, 0, NULL); |
129 | GNUNET_SECRETSHARING_session_destroy(s); | 134 | GNUNET_SECRETSHARING_session_destroy (s); |
130 | } | 135 | } |
131 | 136 | ||
132 | 137 | ||
@@ -138,13 +143,13 @@ handle_session_client_error(void *cls, | |||
138 | * @param error error code | 143 | * @param error error code |
139 | */ | 144 | */ |
140 | static void | 145 | static void |
141 | handle_decrypt_client_error(void *cls, | 146 | handle_decrypt_client_error (void *cls, |
142 | enum GNUNET_MQ_Error error) | 147 | enum GNUNET_MQ_Error error) |
143 | { | 148 | { |
144 | struct GNUNET_SECRETSHARING_DecryptionHandle *dh = cls; | 149 | struct GNUNET_SECRETSHARING_DecryptionHandle *dh = cls; |
145 | 150 | ||
146 | dh->decrypt_cb(dh->decrypt_cls, NULL); | 151 | dh->decrypt_cb (dh->decrypt_cls, NULL); |
147 | GNUNET_SECRETSHARING_decrypt_cancel(dh); | 152 | GNUNET_SECRETSHARING_decrypt_cancel (dh); |
148 | } | 153 | } |
149 | 154 | ||
150 | 155 | ||
@@ -157,8 +162,8 @@ handle_decrypt_client_error(void *cls, | |||
157 | * @param m message with the result | 162 | * @param m message with the result |
158 | */ | 163 | */ |
159 | static int | 164 | static int |
160 | check_secret_ready(void *cls, | 165 | check_secret_ready (void *cls, |
161 | const struct GNUNET_SECRETSHARING_SecretReadyMessage *m) | 166 | const struct GNUNET_SECRETSHARING_SecretReadyMessage *m) |
162 | { | 167 | { |
163 | /* FIXME: actually check m is well-formed here! */ | 168 | /* FIXME: actually check m is well-formed here! */ |
164 | return GNUNET_OK; | 169 | return GNUNET_OK; |
@@ -174,32 +179,33 @@ check_secret_ready(void *cls, | |||
174 | * @param m message with the result | 179 | * @param m message with the result |
175 | */ | 180 | */ |
176 | static void | 181 | static void |
177 | handle_secret_ready(void *cls, | 182 | handle_secret_ready (void *cls, |
178 | const struct GNUNET_SECRETSHARING_SecretReadyMessage *m) | 183 | const struct GNUNET_SECRETSHARING_SecretReadyMessage *m) |
179 | { | 184 | { |
180 | struct GNUNET_SECRETSHARING_Session *s = cls; | 185 | struct GNUNET_SECRETSHARING_Session *s = cls; |
181 | struct GNUNET_SECRETSHARING_Share *share; | 186 | struct GNUNET_SECRETSHARING_Share *share; |
182 | size_t share_size; | 187 | size_t share_size; |
183 | 188 | ||
184 | LOG(GNUNET_ERROR_TYPE_DEBUG, | 189 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
185 | "Got secret ready message of size %u\n", | 190 | "Got secret ready message of size %u\n", |
186 | ntohs(m->header.size)); | 191 | ntohs (m->header.size)); |
187 | share_size = ntohs(m->header.size) - sizeof(struct GNUNET_SECRETSHARING_SecretReadyMessage); | 192 | share_size = ntohs (m->header.size) - sizeof(struct |
193 | GNUNET_SECRETSHARING_SecretReadyMessage); | ||
188 | 194 | ||
189 | share = GNUNET_SECRETSHARING_share_read(&m[1], | 195 | share = GNUNET_SECRETSHARING_share_read (&m[1], |
190 | share_size, | 196 | share_size, |
191 | NULL); | 197 | NULL); |
192 | GNUNET_assert(NULL != share); // FIXME: this can fail! | 198 | GNUNET_assert (NULL != share); // FIXME: this can fail! |
193 | // should have been checked in #check_secret_ready! | 199 | // should have been checked in #check_secret_ready! |
194 | // FIXME: below we never check &m[1] is valid! | 200 | // FIXME: below we never check &m[1] is valid! |
195 | // FIXME: do we leak 'share' here? | 201 | // FIXME: do we leak 'share' here? |
196 | s->secret_ready_cb(s->secret_ready_cls, | 202 | s->secret_ready_cb (s->secret_ready_cls, |
197 | share, /* FIXME */ | 203 | share, /* FIXME */ |
198 | &share->public_key, | 204 | &share->public_key, |
199 | share->num_peers, | 205 | share->num_peers, |
200 | (const struct GNUNET_PeerIdentity *)&m[1]); | 206 | (const struct GNUNET_PeerIdentity *) &m[1]); |
201 | 207 | ||
202 | GNUNET_SECRETSHARING_session_destroy(s); | 208 | GNUNET_SECRETSHARING_session_destroy (s); |
203 | } | 209 | } |
204 | 210 | ||
205 | 211 | ||
@@ -210,11 +216,11 @@ handle_secret_ready(void *cls, | |||
210 | * @param s session to destroy | 216 | * @param s session to destroy |
211 | */ | 217 | */ |
212 | void | 218 | void |
213 | GNUNET_SECRETSHARING_session_destroy(struct GNUNET_SECRETSHARING_Session *s) | 219 | GNUNET_SECRETSHARING_session_destroy (struct GNUNET_SECRETSHARING_Session *s) |
214 | { | 220 | { |
215 | GNUNET_MQ_destroy(s->mq); | 221 | GNUNET_MQ_destroy (s->mq); |
216 | s->mq = NULL; | 222 | s->mq = NULL; |
217 | GNUNET_free(s); | 223 | GNUNET_free (s); |
218 | } | 224 | } |
219 | 225 | ||
220 | 226 | ||
@@ -235,65 +241,68 @@ GNUNET_SECRETSHARING_session_destroy(struct GNUNET_SECRETSHARING_Session *s) | |||
235 | * @param cls closure for @a cb | 241 | * @param cls closure for @a cb |
236 | */ | 242 | */ |
237 | struct GNUNET_SECRETSHARING_Session * | 243 | struct GNUNET_SECRETSHARING_Session * |
238 | GNUNET_SECRETSHARING_create_session(const struct GNUNET_CONFIGURATION_Handle *cfg, | 244 | GNUNET_SECRETSHARING_create_session (const struct |
239 | unsigned int num_peers, | 245 | GNUNET_CONFIGURATION_Handle *cfg, |
240 | const struct GNUNET_PeerIdentity *peers, | 246 | unsigned int num_peers, |
241 | const struct GNUNET_HashCode *session_id, | 247 | const struct GNUNET_PeerIdentity *peers, |
242 | struct GNUNET_TIME_Absolute start, | 248 | const struct GNUNET_HashCode *session_id, |
243 | struct GNUNET_TIME_Absolute deadline, | 249 | struct GNUNET_TIME_Absolute start, |
244 | unsigned int threshold, | 250 | struct GNUNET_TIME_Absolute deadline, |
245 | GNUNET_SECRETSHARING_SecretReadyCallback cb, | 251 | unsigned int threshold, |
246 | void *cls) | 252 | GNUNET_SECRETSHARING_SecretReadyCallback cb, |
253 | void *cls) | ||
247 | { | 254 | { |
248 | struct GNUNET_SECRETSHARING_Session *s | 255 | struct GNUNET_SECRETSHARING_Session *s |
249 | = GNUNET_new(struct GNUNET_SECRETSHARING_Session); | 256 | = GNUNET_new (struct GNUNET_SECRETSHARING_Session); |
250 | struct GNUNET_MQ_MessageHandler mq_handlers[] = { | 257 | struct GNUNET_MQ_MessageHandler mq_handlers[] = { |
251 | GNUNET_MQ_hd_var_size(secret_ready, | 258 | GNUNET_MQ_hd_var_size (secret_ready, |
252 | GNUNET_MESSAGE_TYPE_SECRETSHARING_CLIENT_SECRET_READY, | 259 | GNUNET_MESSAGE_TYPE_SECRETSHARING_CLIENT_SECRET_READY, |
253 | struct GNUNET_SECRETSHARING_SecretReadyMessage, | 260 | struct GNUNET_SECRETSHARING_SecretReadyMessage, |
254 | s), | 261 | s), |
255 | GNUNET_MQ_handler_end() | 262 | GNUNET_MQ_handler_end () |
256 | }; | 263 | }; |
257 | struct GNUNET_MQ_Envelope *ev; | 264 | struct GNUNET_MQ_Envelope *ev; |
258 | struct GNUNET_SECRETSHARING_CreateMessage *msg; | 265 | struct GNUNET_SECRETSHARING_CreateMessage *msg; |
259 | 266 | ||
260 | s->mq = GNUNET_CLIENT_connect(cfg, | 267 | s->mq = GNUNET_CLIENT_connect (cfg, |
261 | "secretsharing", | 268 | "secretsharing", |
262 | mq_handlers, | 269 | mq_handlers, |
263 | &handle_session_client_error, | 270 | &handle_session_client_error, |
264 | s); | 271 | s); |
265 | if (NULL == s->mq) | 272 | if (NULL == s->mq) |
266 | { | 273 | { |
267 | /* secretsharing not configured correctly */ | 274 | /* secretsharing not configured correctly */ |
268 | GNUNET_break(0); | 275 | GNUNET_break (0); |
269 | GNUNET_free(s); | 276 | GNUNET_free (s); |
270 | return NULL; | 277 | return NULL; |
271 | } | 278 | } |
272 | s->secret_ready_cb = cb; | 279 | s->secret_ready_cb = cb; |
273 | s->secret_ready_cls = cls; | 280 | s->secret_ready_cls = cls; |
274 | ev = GNUNET_MQ_msg_extra(msg, | 281 | ev = GNUNET_MQ_msg_extra (msg, |
275 | num_peers * sizeof(struct GNUNET_PeerIdentity), | 282 | num_peers * sizeof(struct GNUNET_PeerIdentity), |
276 | GNUNET_MESSAGE_TYPE_SECRETSHARING_CLIENT_GENERATE); | 283 | GNUNET_MESSAGE_TYPE_SECRETSHARING_CLIENT_GENERATE); |
277 | 284 | ||
278 | msg->threshold = htons(threshold); | 285 | msg->threshold = htons (threshold); |
279 | msg->num_peers = htons(num_peers); | 286 | msg->num_peers = htons (num_peers); |
280 | msg->session_id = *session_id; | 287 | msg->session_id = *session_id; |
281 | msg->start = GNUNET_TIME_absolute_hton(start); | 288 | msg->start = GNUNET_TIME_absolute_hton (start); |
282 | msg->deadline = GNUNET_TIME_absolute_hton(deadline); | 289 | msg->deadline = GNUNET_TIME_absolute_hton (deadline); |
283 | GNUNET_memcpy(&msg[1], peers, num_peers * sizeof(struct GNUNET_PeerIdentity)); | 290 | GNUNET_memcpy (&msg[1], peers, num_peers * sizeof(struct |
291 | GNUNET_PeerIdentity)); | ||
284 | 292 | ||
285 | GNUNET_MQ_send(s->mq, ev); | 293 | GNUNET_MQ_send (s->mq, ev); |
286 | 294 | ||
287 | LOG(GNUNET_ERROR_TYPE_DEBUG, | 295 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
288 | "Secretsharing session created with %u peers\n", | 296 | "Secretsharing session created with %u peers\n", |
289 | num_peers); | 297 | num_peers); |
290 | return s; | 298 | return s; |
291 | } | 299 | } |
292 | 300 | ||
293 | 301 | ||
294 | static void | 302 | static void |
295 | handle_decrypt_done(void *cls, | 303 | handle_decrypt_done (void *cls, |
296 | const struct GNUNET_SECRETSHARING_DecryptResponseMessage *m) | 304 | const struct |
305 | GNUNET_SECRETSHARING_DecryptResponseMessage *m) | ||
297 | { | 306 | { |
298 | struct GNUNET_SECRETSHARING_DecryptionHandle *dh = cls; | 307 | struct GNUNET_SECRETSHARING_DecryptionHandle *dh = cls; |
299 | const struct GNUNET_SECRETSHARING_Plaintext *plaintext; | 308 | const struct GNUNET_SECRETSHARING_Plaintext *plaintext; |
@@ -301,9 +310,9 @@ handle_decrypt_done(void *cls, | |||
301 | if (m->success == 0) | 310 | if (m->success == 0) |
302 | plaintext = NULL; | 311 | plaintext = NULL; |
303 | else | 312 | else |
304 | plaintext = (void *)&m->plaintext; | 313 | plaintext = (void *) &m->plaintext; |
305 | dh->decrypt_cb(dh->decrypt_cls, plaintext); | 314 | dh->decrypt_cb (dh->decrypt_cls, plaintext); |
306 | GNUNET_SECRETSHARING_decrypt_cancel(dh); | 315 | GNUNET_SECRETSHARING_decrypt_cancel (dh); |
307 | } | 316 | } |
308 | 317 | ||
309 | 318 | ||
@@ -321,22 +330,23 @@ handle_decrypt_done(void *cls, | |||
321 | * @return handle to cancel the operation | 330 | * @return handle to cancel the operation |
322 | */ | 331 | */ |
323 | struct GNUNET_SECRETSHARING_DecryptionHandle * | 332 | struct GNUNET_SECRETSHARING_DecryptionHandle * |
324 | GNUNET_SECRETSHARING_decrypt(const struct GNUNET_CONFIGURATION_Handle *cfg, | 333 | GNUNET_SECRETSHARING_decrypt (const struct GNUNET_CONFIGURATION_Handle *cfg, |
325 | struct GNUNET_SECRETSHARING_Share *share, | 334 | struct GNUNET_SECRETSHARING_Share *share, |
326 | const struct GNUNET_SECRETSHARING_Ciphertext *ciphertext, | 335 | const struct |
327 | struct GNUNET_TIME_Absolute start, | 336 | GNUNET_SECRETSHARING_Ciphertext *ciphertext, |
328 | struct GNUNET_TIME_Absolute deadline, | 337 | struct GNUNET_TIME_Absolute start, |
329 | GNUNET_SECRETSHARING_DecryptCallback decrypt_cb, | 338 | struct GNUNET_TIME_Absolute deadline, |
330 | void *decrypt_cb_cls) | 339 | GNUNET_SECRETSHARING_DecryptCallback decrypt_cb, |
340 | void *decrypt_cb_cls) | ||
331 | { | 341 | { |
332 | struct GNUNET_SECRETSHARING_DecryptionHandle *s | 342 | struct GNUNET_SECRETSHARING_DecryptionHandle *s |
333 | = GNUNET_new(struct GNUNET_SECRETSHARING_DecryptionHandle); | 343 | = GNUNET_new (struct GNUNET_SECRETSHARING_DecryptionHandle); |
334 | struct GNUNET_MQ_MessageHandler mq_handlers[] = { | 344 | struct GNUNET_MQ_MessageHandler mq_handlers[] = { |
335 | GNUNET_MQ_hd_fixed_size(decrypt_done, | 345 | GNUNET_MQ_hd_fixed_size (decrypt_done, |
336 | GNUNET_MESSAGE_TYPE_SECRETSHARING_CLIENT_DECRYPT_DONE, | 346 | GNUNET_MESSAGE_TYPE_SECRETSHARING_CLIENT_DECRYPT_DONE, |
337 | struct GNUNET_SECRETSHARING_DecryptResponseMessage, | 347 | struct GNUNET_SECRETSHARING_DecryptResponseMessage, |
338 | s), | 348 | s), |
339 | GNUNET_MQ_handler_end() | 349 | GNUNET_MQ_handler_end () |
340 | }; | 350 | }; |
341 | struct GNUNET_MQ_Envelope *ev; | 351 | struct GNUNET_MQ_Envelope *ev; |
342 | struct GNUNET_SECRETSHARING_DecryptRequestMessage *msg; | 352 | struct GNUNET_SECRETSHARING_DecryptRequestMessage *msg; |
@@ -344,75 +354,78 @@ GNUNET_SECRETSHARING_decrypt(const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
344 | 354 | ||
345 | s->decrypt_cb = decrypt_cb; | 355 | s->decrypt_cb = decrypt_cb; |
346 | s->decrypt_cls = decrypt_cb_cls; | 356 | s->decrypt_cls = decrypt_cb_cls; |
347 | s->mq = GNUNET_CLIENT_connect(cfg, | 357 | s->mq = GNUNET_CLIENT_connect (cfg, |
348 | "secretsharing", | 358 | "secretsharing", |
349 | mq_handlers, | 359 | mq_handlers, |
350 | &handle_decrypt_client_error, | 360 | &handle_decrypt_client_error, |
351 | s); | 361 | s); |
352 | if (NULL == s->mq) | 362 | if (NULL == s->mq) |
353 | { | 363 | { |
354 | GNUNET_free(s); | 364 | GNUNET_free (s); |
355 | return NULL; | 365 | return NULL; |
356 | } | 366 | } |
357 | GNUNET_assert(GNUNET_OK == | 367 | GNUNET_assert (GNUNET_OK == |
358 | GNUNET_SECRETSHARING_share_write(share, NULL, 0, | 368 | GNUNET_SECRETSHARING_share_write (share, NULL, 0, |
359 | &share_size)); | 369 | &share_size)); |
360 | 370 | ||
361 | ev = GNUNET_MQ_msg_extra(msg, | 371 | ev = GNUNET_MQ_msg_extra (msg, |
362 | share_size, | 372 | share_size, |
363 | GNUNET_MESSAGE_TYPE_SECRETSHARING_CLIENT_DECRYPT); | 373 | GNUNET_MESSAGE_TYPE_SECRETSHARING_CLIENT_DECRYPT); |
364 | 374 | ||
365 | GNUNET_assert(GNUNET_OK == | 375 | GNUNET_assert (GNUNET_OK == |
366 | GNUNET_SECRETSHARING_share_write(share, | 376 | GNUNET_SECRETSHARING_share_write (share, |
367 | &msg[1], | 377 | &msg[1], |
368 | share_size, | 378 | share_size, |
369 | NULL)); | 379 | NULL)); |
370 | 380 | ||
371 | msg->start = GNUNET_TIME_absolute_hton(start); | 381 | msg->start = GNUNET_TIME_absolute_hton (start); |
372 | msg->deadline = GNUNET_TIME_absolute_hton(deadline); | 382 | msg->deadline = GNUNET_TIME_absolute_hton (deadline); |
373 | msg->ciphertext = *ciphertext; | 383 | msg->ciphertext = *ciphertext; |
374 | 384 | ||
375 | GNUNET_MQ_send(s->mq, ev); | 385 | GNUNET_MQ_send (s->mq, ev); |
376 | 386 | ||
377 | LOG(GNUNET_ERROR_TYPE_DEBUG, | 387 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
378 | "decrypt session created\n"); | 388 | "decrypt session created\n"); |
379 | return s; | 389 | return s; |
380 | } | 390 | } |
381 | 391 | ||
382 | 392 | ||
383 | int | 393 | int |
384 | GNUNET_SECRETSHARING_plaintext_generate_i(struct GNUNET_SECRETSHARING_Plaintext *plaintext, | 394 | GNUNET_SECRETSHARING_plaintext_generate_i (struct |
385 | int64_t exponent) | 395 | GNUNET_SECRETSHARING_Plaintext * |
396 | plaintext, | ||
397 | int64_t exponent) | ||
386 | { | 398 | { |
387 | int negative; | 399 | int negative; |
388 | gcry_mpi_t x; | 400 | gcry_mpi_t x; |
389 | 401 | ||
390 | ensure_elgamal_initialized(); | 402 | ensure_elgamal_initialized (); |
391 | 403 | ||
392 | GNUNET_assert(NULL != (x = gcry_mpi_new(0))); | 404 | GNUNET_assert (NULL != (x = gcry_mpi_new (0))); |
393 | 405 | ||
394 | negative = GNUNET_NO; | 406 | negative = GNUNET_NO; |
395 | if (exponent < 0) | 407 | if (exponent < 0) |
396 | { | 408 | { |
397 | negative = GNUNET_YES; | 409 | negative = GNUNET_YES; |
398 | exponent = -exponent; | 410 | exponent = -exponent; |
399 | } | 411 | } |
400 | 412 | ||
401 | gcry_mpi_set_ui(x, exponent); | 413 | gcry_mpi_set_ui (x, exponent); |
402 | 414 | ||
403 | gcry_mpi_powm(x, elgamal_g, x, elgamal_p); | 415 | gcry_mpi_powm (x, elgamal_g, x, elgamal_p); |
404 | 416 | ||
405 | if (GNUNET_YES == negative) | 417 | if (GNUNET_YES == negative) |
406 | { | 418 | { |
407 | int res; | 419 | int res; |
408 | res = gcry_mpi_invm(x, x, elgamal_p); | 420 | res = gcry_mpi_invm (x, x, elgamal_p); |
409 | if (0 == res) | 421 | if (0 == res) |
410 | return GNUNET_SYSERR; | 422 | return GNUNET_SYSERR; |
411 | } | 423 | } |
412 | 424 | ||
413 | GNUNET_CRYPTO_mpi_print_unsigned(plaintext, | 425 | GNUNET_CRYPTO_mpi_print_unsigned (plaintext, |
414 | sizeof(struct GNUNET_SECRETSHARING_Plaintext), | 426 | sizeof(struct |
415 | x); | 427 | GNUNET_SECRETSHARING_Plaintext), |
428 | x); | ||
416 | 429 | ||
417 | return GNUNET_OK; | 430 | return GNUNET_OK; |
418 | } | 431 | } |
@@ -432,9 +445,12 @@ GNUNET_SECRETSHARING_plaintext_generate_i(struct GNUNET_SECRETSHARING_Plaintext | |||
432 | * @return #GNUNET_YES on succes, #GNUNET_SYSERR if the message is invalid (invalid range) | 445 | * @return #GNUNET_YES on succes, #GNUNET_SYSERR if the message is invalid (invalid range) |
433 | */ | 446 | */ |
434 | int | 447 | int |
435 | GNUNET_SECRETSHARING_encrypt(const struct GNUNET_SECRETSHARING_PublicKey *public_key, | 448 | GNUNET_SECRETSHARING_encrypt (const struct |
436 | const struct GNUNET_SECRETSHARING_Plaintext *plaintext, | 449 | GNUNET_SECRETSHARING_PublicKey *public_key, |
437 | struct GNUNET_SECRETSHARING_Ciphertext *result_ciphertext) | 450 | const struct |
451 | GNUNET_SECRETSHARING_Plaintext *plaintext, | ||
452 | struct GNUNET_SECRETSHARING_Ciphertext * | ||
453 | result_ciphertext) | ||
438 | { | 454 | { |
439 | /* pubkey */ | 455 | /* pubkey */ |
440 | gcry_mpi_t h; | 456 | gcry_mpi_t h; |
@@ -445,36 +461,37 @@ GNUNET_SECRETSHARING_encrypt(const struct GNUNET_SECRETSHARING_PublicKey *public | |||
445 | /* temp value */ | 461 | /* temp value */ |
446 | gcry_mpi_t tmp; | 462 | gcry_mpi_t tmp; |
447 | 463 | ||
448 | ensure_elgamal_initialized(); | 464 | ensure_elgamal_initialized (); |
449 | 465 | ||
450 | GNUNET_assert(NULL != (h = gcry_mpi_new(0))); | 466 | GNUNET_assert (NULL != (h = gcry_mpi_new (0))); |
451 | GNUNET_assert(NULL != (y = gcry_mpi_new(0))); | 467 | GNUNET_assert (NULL != (y = gcry_mpi_new (0))); |
452 | GNUNET_assert(NULL != (tmp = gcry_mpi_new(0))); | 468 | GNUNET_assert (NULL != (tmp = gcry_mpi_new (0))); |
453 | 469 | ||
454 | GNUNET_CRYPTO_mpi_scan_unsigned(&h, public_key, sizeof *public_key); | 470 | GNUNET_CRYPTO_mpi_scan_unsigned (&h, public_key, sizeof *public_key); |
455 | GNUNET_CRYPTO_mpi_scan_unsigned(&m, plaintext, sizeof *plaintext); | 471 | GNUNET_CRYPTO_mpi_scan_unsigned (&m, plaintext, sizeof *plaintext); |
456 | 472 | ||
457 | // Randomize y such that 0 < y < elgamal_q. | 473 | // Randomize y such that 0 < y < elgamal_q. |
458 | // The '- 1' is necessary as bitlength(q) = bitlength(p) - 1. | 474 | // The '- 1' is necessary as bitlength(q) = bitlength(p) - 1. |
459 | do | 475 | do |
460 | { | 476 | { |
461 | gcry_mpi_randomize(y, GNUNET_SECRETSHARING_ELGAMAL_BITS - 1, GCRY_WEAK_RANDOM); | 477 | gcry_mpi_randomize (y, GNUNET_SECRETSHARING_ELGAMAL_BITS - 1, |
462 | } | 478 | GCRY_WEAK_RANDOM); |
463 | while ((gcry_mpi_cmp_ui(y, 0) == 0) || (gcry_mpi_cmp(y, elgamal_q) >= 0)); | 479 | } |
480 | while ((gcry_mpi_cmp_ui (y, 0) == 0) || (gcry_mpi_cmp (y, elgamal_q) >= 0)); | ||
464 | 481 | ||
465 | // tmp <- g^y | 482 | // tmp <- g^y |
466 | gcry_mpi_powm(tmp, elgamal_g, y, elgamal_p); | 483 | gcry_mpi_powm (tmp, elgamal_g, y, elgamal_p); |
467 | // write tmp to c1 | 484 | // write tmp to c1 |
468 | GNUNET_CRYPTO_mpi_print_unsigned(&result_ciphertext->c1_bits, | 485 | GNUNET_CRYPTO_mpi_print_unsigned (&result_ciphertext->c1_bits, |
469 | GNUNET_SECRETSHARING_ELGAMAL_BITS / 8, tmp); | 486 | GNUNET_SECRETSHARING_ELGAMAL_BITS / 8, tmp); |
470 | 487 | ||
471 | // tmp <- h^y | 488 | // tmp <- h^y |
472 | gcry_mpi_powm(tmp, h, y, elgamal_p); | 489 | gcry_mpi_powm (tmp, h, y, elgamal_p); |
473 | // tmp <- tmp * m | 490 | // tmp <- tmp * m |
474 | gcry_mpi_mulm(tmp, tmp, m, elgamal_p); | 491 | gcry_mpi_mulm (tmp, tmp, m, elgamal_p); |
475 | // write tmp to c2 | 492 | // write tmp to c2 |
476 | GNUNET_CRYPTO_mpi_print_unsigned(&result_ciphertext->c2_bits, | 493 | GNUNET_CRYPTO_mpi_print_unsigned (&result_ciphertext->c2_bits, |
477 | GNUNET_SECRETSHARING_ELGAMAL_BITS / 8, tmp); | 494 | GNUNET_SECRETSHARING_ELGAMAL_BITS / 8, tmp); |
478 | 495 | ||
479 | return GNUNET_OK; | 496 | return GNUNET_OK; |
480 | } | 497 | } |
@@ -489,11 +506,12 @@ GNUNET_SECRETSHARING_encrypt(const struct GNUNET_SECRETSHARING_PublicKey *public | |||
489 | * @param dh to cancel | 506 | * @param dh to cancel |
490 | */ | 507 | */ |
491 | void | 508 | void |
492 | GNUNET_SECRETSHARING_decrypt_cancel(struct GNUNET_SECRETSHARING_DecryptionHandle *dh) | 509 | GNUNET_SECRETSHARING_decrypt_cancel (struct |
510 | GNUNET_SECRETSHARING_DecryptionHandle *dh) | ||
493 | { | 511 | { |
494 | GNUNET_MQ_destroy(dh->mq); | 512 | GNUNET_MQ_destroy (dh->mq); |
495 | dh->mq = NULL; | 513 | dh->mq = NULL; |
496 | GNUNET_free(dh); | 514 | GNUNET_free (dh); |
497 | } | 515 | } |
498 | 516 | ||
499 | /* end of secretsharing_api.c */ | 517 | /* end of secretsharing_api.c */ |