diff options
Diffstat (limited to 'src/scalarproduct/gnunet-service-scalarproduct-ecc_bob.c')
-rw-r--r-- | src/scalarproduct/gnunet-service-scalarproduct-ecc_bob.c | 131 |
1 files changed, 51 insertions, 80 deletions
diff --git a/src/scalarproduct/gnunet-service-scalarproduct-ecc_bob.c b/src/scalarproduct/gnunet-service-scalarproduct-ecc_bob.c index 4c835d52a..02a62c164 100644 --- a/src/scalarproduct/gnunet-service-scalarproduct-ecc_bob.c +++ b/src/scalarproduct/gnunet-service-scalarproduct-ecc_bob.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | Copyright (C) 2013-2017 GNUnet e.V. | 3 | Copyright (C) 2013-2017, 2021 GNUnet e.V. |
4 | 4 | ||
5 | GNUnet is free software: you can redistribute it and/or modify it | 5 | GNUnet is free software: you can redistribute it and/or modify it |
6 | under the terms of the GNU Affero General Public License as published | 6 | under the terms of the GNU Affero General Public License as published |
@@ -54,7 +54,7 @@ struct MpiElement | |||
54 | /** | 54 | /** |
55 | * Value represented (a). | 55 | * Value represented (a). |
56 | */ | 56 | */ |
57 | gcry_mpi_t value; | 57 | int64_t value; |
58 | }; | 58 | }; |
59 | 59 | ||
60 | 60 | ||
@@ -104,12 +104,12 @@ struct BobServiceSession | |||
104 | /** | 104 | /** |
105 | * Product of the g_i^{b_i} | 105 | * Product of the g_i^{b_i} |
106 | */ | 106 | */ |
107 | gcry_mpi_point_t prod_g_i_b_i; | 107 | struct GNUNET_CRYPTO_EccPoint prod_g_i_b_i; |
108 | 108 | ||
109 | /** | 109 | /** |
110 | * Product of the h_i^{b_i} | 110 | * Product of the h_i^{b_i} |
111 | */ | 111 | */ |
112 | gcry_mpi_point_t prod_h_i_b_i; | 112 | struct GNUNET_CRYPTO_EccPoint prod_h_i_b_i; |
113 | 113 | ||
114 | /** | 114 | /** |
115 | * How many elements will be supplied in total from the client. | 115 | * How many elements will be supplied in total from the client. |
@@ -213,8 +213,6 @@ free_element_cb (void *cls, | |||
213 | static void | 213 | static void |
214 | destroy_service_session (struct BobServiceSession *s) | 214 | destroy_service_session (struct BobServiceSession *s) |
215 | { | 215 | { |
216 | unsigned int i; | ||
217 | |||
218 | if (GNUNET_YES == s->in_destroy) | 216 | if (GNUNET_YES == s->in_destroy) |
219 | return; | 217 | return; |
220 | s->in_destroy = GNUNET_YES; | 218 | s->in_destroy = GNUNET_YES; |
@@ -245,21 +243,9 @@ destroy_service_session (struct BobServiceSession *s) | |||
245 | } | 243 | } |
246 | if (NULL != s->sorted_elements) | 244 | if (NULL != s->sorted_elements) |
247 | { | 245 | { |
248 | for (i = 0; i < s->used_element_count; i++) | ||
249 | gcry_mpi_release (s->sorted_elements[i].value); | ||
250 | GNUNET_free (s->sorted_elements); | 246 | GNUNET_free (s->sorted_elements); |
251 | s->sorted_elements = NULL; | 247 | s->sorted_elements = NULL; |
252 | } | 248 | } |
253 | if (NULL != s->prod_g_i_b_i) | ||
254 | { | ||
255 | gcry_mpi_point_release (s->prod_g_i_b_i); | ||
256 | s->prod_g_i_b_i = NULL; | ||
257 | } | ||
258 | if (NULL != s->prod_h_i_b_i) | ||
259 | { | ||
260 | gcry_mpi_point_release (s->prod_h_i_b_i); | ||
261 | s->prod_h_i_b_i = NULL; | ||
262 | } | ||
263 | if (NULL != s->port) | 249 | if (NULL != s->port) |
264 | { | 250 | { |
265 | GNUNET_CADET_close_port (s->port); | 251 | GNUNET_CADET_close_port (s->port); |
@@ -364,14 +350,8 @@ transmit_bobs_cryptodata_message (struct BobServiceSession *s) | |||
364 | e = GNUNET_MQ_msg (msg, | 350 | e = GNUNET_MQ_msg (msg, |
365 | GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_BOB_CRYPTODATA); | 351 | GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_BOB_CRYPTODATA); |
366 | msg->contained_element_count = htonl (2); | 352 | msg->contained_element_count = htonl (2); |
367 | if (NULL != s->prod_g_i_b_i) | 353 | msg->prod_g_i_b_i = s->prod_g_i_b_i; |
368 | GNUNET_CRYPTO_ecc_point_to_bin (edc, | 354 | msg->prod_h_i_b_i = s->prod_h_i_b_i; |
369 | s->prod_g_i_b_i, | ||
370 | &msg->prod_g_i_b_i); | ||
371 | if (NULL != s->prod_h_i_b_i) | ||
372 | GNUNET_CRYPTO_ecc_point_to_bin (edc, | ||
373 | s->prod_h_i_b_i, | ||
374 | &msg->prod_h_i_b_i); | ||
375 | GNUNET_MQ_notify_sent (e, | 355 | GNUNET_MQ_notify_sent (e, |
376 | &bob_cadet_done_cb, | 356 | &bob_cadet_done_cb, |
377 | s); | 357 | s); |
@@ -384,10 +364,9 @@ transmit_bobs_cryptodata_message (struct BobServiceSession *s) | |||
384 | * Iterator to copy over messages from the hash map | 364 | * Iterator to copy over messages from the hash map |
385 | * into an array for sorting. | 365 | * into an array for sorting. |
386 | * | 366 | * |
387 | * @param cls the `struct BobServiceSession *` | 367 | * @param cls the `struct AliceServiceSession *` |
388 | * @param key the key (unused) | 368 | * @param key the key (unused) |
389 | * @param value the `struct GNUNET_SCALARPRODUCT_Element *` | 369 | * @param value the `struct GNUNET_SCALARPRODUCT_Element *` |
390 | * TODO: code duplication with Alice! | ||
391 | */ | 370 | */ |
392 | static int | 371 | static int |
393 | copy_element_cb (void *cls, | 372 | copy_element_cb (void *cls, |
@@ -396,17 +375,10 @@ copy_element_cb (void *cls, | |||
396 | { | 375 | { |
397 | struct BobServiceSession *s = cls; | 376 | struct BobServiceSession *s = cls; |
398 | struct GNUNET_SCALARPRODUCT_Element *e = value; | 377 | struct GNUNET_SCALARPRODUCT_Element *e = value; |
399 | gcry_mpi_t mval; | 378 | |
400 | int64_t val; | 379 | s->sorted_elements[s->used_element_count].value = (int64_t) GNUNET_ntohll ( |
401 | 380 | e->value); | |
402 | mval = gcry_mpi_new (0); | 381 | s->sorted_elements[s->used_element_count].key = &e->key; |
403 | val = (int64_t) GNUNET_ntohll (e->value); | ||
404 | if (0 > val) | ||
405 | gcry_mpi_sub_ui (mval, mval, -val); | ||
406 | else | ||
407 | gcry_mpi_add_ui (mval, mval, val); | ||
408 | s->sorted_elements [s->used_element_count].value = mval; | ||
409 | s->sorted_elements [s->used_element_count].key = &e->key; | ||
410 | s->used_element_count++; | 382 | s->used_element_count++; |
411 | return GNUNET_OK; | 383 | return GNUNET_OK; |
412 | } | 384 | } |
@@ -490,13 +462,10 @@ handle_alices_cryptodata_message (void *cls, | |||
490 | const struct GNUNET_CRYPTO_EccPoint *payload; | 462 | const struct GNUNET_CRYPTO_EccPoint *payload; |
491 | uint32_t contained_elements; | 463 | uint32_t contained_elements; |
492 | unsigned int max; | 464 | unsigned int max; |
493 | unsigned int i; | 465 | const struct GNUNET_CRYPTO_EccPoint *g_i; |
494 | const struct MpiElement *b_i; | 466 | const struct GNUNET_CRYPTO_EccPoint *h_i; |
495 | gcry_mpi_point_t tmp; | 467 | struct GNUNET_CRYPTO_EccPoint g_i_b_i; |
496 | gcry_mpi_point_t g_i; | 468 | struct GNUNET_CRYPTO_EccPoint h_i_b_i; |
497 | gcry_mpi_point_t h_i; | ||
498 | gcry_mpi_point_t g_i_b_i; | ||
499 | gcry_mpi_point_t h_i_b_i; | ||
500 | 469 | ||
501 | contained_elements = ntohl (msg->contained_element_count); | 470 | contained_elements = ntohl (msg->contained_element_count); |
502 | max = GNUNET_CONTAINER_multihashmap_size (s->intersected_elements); | 471 | max = GNUNET_CONTAINER_multihashmap_size (s->intersected_elements); |
@@ -522,21 +491,29 @@ handle_alices_cryptodata_message (void *cls, | |||
522 | (unsigned int) contained_elements); | 491 | (unsigned int) contained_elements); |
523 | payload = (const struct GNUNET_CRYPTO_EccPoint *) &msg[1]; | 492 | payload = (const struct GNUNET_CRYPTO_EccPoint *) &msg[1]; |
524 | 493 | ||
525 | for (i = 0; i < contained_elements; i++) | 494 | for (unsigned int i = 0; i < contained_elements; i++) |
526 | { | 495 | { |
527 | b_i = &s->sorted_elements[i + s->cadet_received_element_count]; | 496 | int64_t val = s->sorted_elements[i + s->cadet_received_element_count].value; |
528 | g_i = GNUNET_CRYPTO_ecc_bin_to_point (edc, | 497 | struct GNUNET_CRYPTO_EccScalar vali; |
529 | &payload[i * 2]); | 498 | |
530 | g_i_b_i = GNUNET_CRYPTO_ecc_pmul_mpi (edc, | 499 | GNUNET_assert (INT64_MIN != val); |
531 | g_i, | 500 | GNUNET_CRYPTO_ecc_scalar_from_int (val > 0 ? val : -val, |
532 | b_i->value); | 501 | &vali); |
533 | gcry_mpi_point_release (g_i); | 502 | if (val < 0) |
534 | h_i = GNUNET_CRYPTO_ecc_bin_to_point (edc, | 503 | crypto_core_ed25519_scalar_negate (vali.v, |
535 | &payload[i * 2 + 1]); | 504 | vali.v); |
536 | h_i_b_i = GNUNET_CRYPTO_ecc_pmul_mpi (edc, | 505 | g_i = &payload[i * 2]; |
537 | h_i, | 506 | /* g_i_b_i = g_i^vali */ |
538 | b_i->value); | 507 | GNUNET_assert (GNUNET_OK == |
539 | gcry_mpi_point_release (h_i); | 508 | GNUNET_CRYPTO_ecc_pmul_mpi (g_i, |
509 | &vali, | ||
510 | &g_i_b_i)); | ||
511 | h_i = &payload[i * 2 + 1]; | ||
512 | /* h_i_b_i = h_i^vali */ | ||
513 | GNUNET_assert (GNUNET_OK == | ||
514 | GNUNET_CRYPTO_ecc_pmul_mpi (h_i, | ||
515 | &vali, | ||
516 | &h_i_b_i)); | ||
540 | if (0 == i + s->cadet_received_element_count) | 517 | if (0 == i + s->cadet_received_element_count) |
541 | { | 518 | { |
542 | /* first iteration, nothing to add */ | 519 | /* first iteration, nothing to add */ |
@@ -546,18 +523,14 @@ handle_alices_cryptodata_message (void *cls, | |||
546 | else | 523 | else |
547 | { | 524 | { |
548 | /* further iterations, cummulate resulting value */ | 525 | /* further iterations, cummulate resulting value */ |
549 | tmp = GNUNET_CRYPTO_ecc_add (edc, | 526 | GNUNET_assert (GNUNET_OK == |
550 | s->prod_g_i_b_i, | 527 | GNUNET_CRYPTO_ecc_add (&s->prod_g_i_b_i, |
551 | g_i_b_i); | 528 | &g_i_b_i, |
552 | gcry_mpi_point_release (s->prod_g_i_b_i); | 529 | &s->prod_g_i_b_i)); |
553 | gcry_mpi_point_release (g_i_b_i); | 530 | GNUNET_assert (GNUNET_OK == |
554 | s->prod_g_i_b_i = tmp; | 531 | GNUNET_CRYPTO_ecc_add (&s->prod_h_i_b_i, |
555 | tmp = GNUNET_CRYPTO_ecc_add (edc, | 532 | &h_i_b_i, |
556 | s->prod_h_i_b_i, | 533 | &s->prod_h_i_b_i)); |
557 | h_i_b_i); | ||
558 | gcry_mpi_point_release (s->prod_h_i_b_i); | ||
559 | gcry_mpi_point_release (h_i_b_i); | ||
560 | s->prod_h_i_b_i = tmp; | ||
561 | } | 534 | } |
562 | } | 535 | } |
563 | s->cadet_received_element_count += contained_elements; | 536 | s->cadet_received_element_count += contained_elements; |
@@ -747,10 +720,9 @@ cb_channel_incoming (void *cls, | |||
747 | * @return #GNUNET_OK if @a msg is well-formed | 720 | * @return #GNUNET_OK if @a msg is well-formed |
748 | */ | 721 | */ |
749 | static int | 722 | static int |
750 | check_bob_client_message_multipart (void *cls, | 723 | check_bob_client_message_multipart ( |
751 | const struct | 724 | void *cls, |
752 | ComputationBobCryptodataMultipartMessage * | 725 | const struct ComputationBobCryptodataMultipartMessage *msg) |
753 | msg) | ||
754 | { | 726 | { |
755 | struct BobServiceSession *s = cls; | 727 | struct BobServiceSession *s = cls; |
756 | uint32_t contained_count; | 728 | uint32_t contained_count; |
@@ -781,10 +753,9 @@ check_bob_client_message_multipart (void *cls, | |||
781 | * @param msg the actual message | 753 | * @param msg the actual message |
782 | */ | 754 | */ |
783 | static void | 755 | static void |
784 | handle_bob_client_message_multipart (void *cls, | 756 | handle_bob_client_message_multipart ( |
785 | const struct | 757 | void *cls, |
786 | ComputationBobCryptodataMultipartMessage * | 758 | const struct ComputationBobCryptodataMultipartMessage *msg) |
787 | msg) | ||
788 | { | 759 | { |
789 | struct BobServiceSession *s = cls; | 760 | struct BobServiceSession *s = cls; |
790 | uint32_t contained_count; | 761 | uint32_t contained_count; |