2 This file is part of GNUnet.
3 Copyright (C) 2013-2015 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file scalarproduct/gnunet-service-scalarproduct-ecc_alice.c
22 * @brief scalarproduct service implementation
23 * @author Christian M. Fuchs
24 * @author Christian Grothoff
29 #include "gnunet_util_lib.h"
30 #include "gnunet_core_service.h"
31 #include "gnunet_cadet_service.h"
32 #include "gnunet_applications.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_scalarproduct_service.h"
35 #include "gnunet_set_service.h"
36 #include "scalarproduct.h"
37 #include "gnunet-service-scalarproduct-ecc.h"
39 #define LOG(kind,...) GNUNET_log_from (kind, "scalarproduct-alice", __VA_ARGS__)
42 * Maximum allowed result value for the scalarproduct computation.
43 * DLOG will fail if the result is bigger.
45 #define MAX_RESULT (1024 * 1024)
48 * An encrypted element key-value pair.
53 * Key used to identify matching pairs of values to multiply.
54 * Points into an existing data structure, to avoid copying
55 * and doubling memory use.
57 const struct GNUNET_HashCode *key;
60 * a_i value, not disclosed to Bob.
65 * r_i value, chosen at random, not disclosed to Bob.
73 * A scalarproduct session which tracks
74 * a request form the client to our final response.
76 struct AliceServiceSession
80 * (hopefully) unique transaction ID
82 struct GNUNET_HashCode session_id;
85 * Alice or Bob's peerID
87 struct GNUNET_PeerIdentity peer;
90 * The client this request is related to.
92 struct GNUNET_SERVER_Client *client;
95 * The message queue for the client.
97 struct GNUNET_MQ_Handle *client_mq;
100 * The message queue for CADET.
102 struct GNUNET_MQ_Handle *cadet_mq;
105 * all non-0-value'd elements transmitted to us.
106 * Values are of type `struct GNUNET_SCALARPRODUCT_Element *`
108 struct GNUNET_CONTAINER_MultiHashMap *intersected_elements;
111 * Set of elements for which will conduction an intersection.
112 * the resulting elements are then used for computing the scalar product.
114 struct GNUNET_SET_Handle *intersection_set;
117 * Set of elements for which will conduction an intersection.
118 * the resulting elements are then used for computing the scalar product.
120 struct GNUNET_SET_OperationHandle *intersection_op;
123 * Handle to Alice's Intersection operation listening for Bob
125 struct GNUNET_SET_ListenHandle *intersection_listen;
128 * channel-handle associated with our cadet handle
130 struct GNUNET_CADET_Channel *channel;
133 * a(Alice), sorted array by key of length @e used_element_count.
135 struct MpiElement *sorted_elements;
138 * The computed scalar
143 * How many elements we were supplied with from the client (total
144 * count before intersection).
149 * How many elements actually are used for the scalar product.
150 * Size of the arrays in @e r and @e r_prime. Sometimes also
151 * reset to 0 and used as a counter!
153 uint32_t used_element_count;
156 * Already transferred elements from client to us.
157 * Less or equal than @e total.
159 uint32_t client_received_element_count;
162 * State of this session. In
163 * #GNUNET_SCALARPRODUCT_STATUS_ACTIVE while operation is
164 * ongoing, afterwards in #GNUNET_SCALARPRODUCT_STATUS_SUCCESS or
165 * #GNUNET_SCALARPRODUCT_STATUS_FAILURE.
167 enum GNUNET_SCALARPRODUCT_ResponseStatus status;
170 * Flag to prevent recursive calls to #destroy_service_session() from
179 * GNUnet configuration handle
181 static const struct GNUNET_CONFIGURATION_Handle *cfg;
184 * Context for DLOG operations on a curve.
186 static struct GNUNET_CRYPTO_EccDlogContext *edc;
189 * Alice's private key ('a').
191 static gcry_mpi_t my_privkey;
194 * Inverse of Alice's private key ('a_inv').
196 static gcry_mpi_t my_privkey_inv;
199 * Handle to the CADET service.
201 static struct GNUNET_CADET_Handle *my_cadet;
205 * Iterator called to free elements.
207 * @param cls the `struct AliceServiceSession *` (unused)
208 * @param key the key (unused)
209 * @param value value to free
210 * @return #GNUNET_OK (continue to iterate)
213 free_element_cb (void *cls,
214 const struct GNUNET_HashCode *key,
217 struct GNUNET_SCALARPRODUCT_Element *e = value;
225 * Destroy session state, we are done with it.
227 * @param s the session to free elements from
230 destroy_service_session (struct AliceServiceSession *s)
234 if (GNUNET_YES == s->in_destroy)
236 s->in_destroy = GNUNET_YES;
237 if (NULL != s->client_mq)
239 GNUNET_MQ_destroy (s->client_mq);
242 if (NULL != s->cadet_mq)
244 GNUNET_MQ_destroy (s->cadet_mq);
247 if (NULL != s->client)
249 GNUNET_SERVER_client_set_user_context (s->client,
251 GNUNET_SERVER_client_disconnect (s->client);
254 if (NULL != s->channel)
256 GNUNET_CADET_channel_destroy (s->channel);
259 if (NULL != s->intersected_elements)
261 GNUNET_CONTAINER_multihashmap_iterate (s->intersected_elements,
264 GNUNET_CONTAINER_multihashmap_destroy (s->intersected_elements);
265 s->intersected_elements = NULL;
267 if (NULL != s->intersection_listen)
269 GNUNET_SET_listen_cancel (s->intersection_listen);
270 s->intersection_listen = NULL;
272 if (NULL != s->intersection_op)
274 GNUNET_SET_operation_cancel (s->intersection_op);
275 s->intersection_op = NULL;
277 if (NULL != s->intersection_set)
279 GNUNET_SET_destroy (s->intersection_set);
280 s->intersection_set = NULL;
282 if (NULL != s->sorted_elements)
284 for (i=0;i<s->used_element_count;i++)
285 gcry_mpi_release (s->sorted_elements[i].value);
286 GNUNET_free (s->sorted_elements);
287 s->sorted_elements = NULL;
289 if (NULL != s->product)
291 gcry_mpi_release (s->product);
299 * Notify the client that the session has failed. A message gets sent
300 * to Alice's client if we encountered any error.
302 * @param session the associated client session to fail or succeed
305 prepare_client_end_notification (struct AliceServiceSession *session)
307 struct ClientResponseMessage *msg;
308 struct GNUNET_MQ_Envelope *e;
310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
311 "Sending session-end notification with status %d to client for session %s\n",
313 GNUNET_h2s (&session->session_id));
314 e = GNUNET_MQ_msg (msg,
315 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT);
316 msg->product_length = htonl (0);
317 msg->status = htonl (session->status);
318 GNUNET_MQ_send (session->client_mq,
324 * Prepare the final (positive) response we will send to Alice's
327 * @param s the session associated with our client.
330 transmit_client_response (struct AliceServiceSession *s)
332 struct ClientResponseMessage *msg;
333 struct GNUNET_MQ_Envelope *e;
334 unsigned char *product_exported = NULL;
335 size_t product_length = 0;
341 if (NULL == s->product)
344 prepare_client_end_notification (s);
347 value = gcry_mpi_new (0);
348 sign = gcry_mpi_cmp_ui (s->product, 0);
359 gcry_mpi_add (value, value, s->product);
363 /* result is exactly zero */
366 gcry_mpi_release (s->product);
370 (0 != (rc = gcry_mpi_aprint (GCRYMPI_FMT_STD,
375 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR,
378 prepare_client_end_notification (s);
381 gcry_mpi_release (value);
382 e = GNUNET_MQ_msg_extra (msg,
384 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT);
385 msg->status = htonl (GNUNET_SCALARPRODUCT_STATUS_SUCCESS);
386 msg->range = htonl (range);
387 msg->product_length = htonl (product_length);
388 if (NULL != product_exported)
393 GNUNET_free (product_exported);
395 GNUNET_MQ_send (s->client_mq,
397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
398 "Sent result to client, session %s has ended!\n",
399 GNUNET_h2s (&s->session_id));
405 * Function called whenever a channel is destroyed. Should clean up
406 * any associated state.
408 * It must NOT call #GNUNET_CADET_channel_destroy() on the channel.
410 * @param cls closure (set from #GNUNET_CADET_connect())
411 * @param channel connection to the other end (henceforth invalid)
412 * @param channel_ctx place where local state associated
413 * with the channel is stored
416 cb_channel_destruction (void *cls,
417 const struct GNUNET_CADET_Channel *channel,
420 struct AliceServiceSession *s = channel_ctx;
422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
423 "Peer disconnected, terminating session %s with peer %s\n",
424 GNUNET_h2s (&s->session_id),
425 GNUNET_i2s (&s->peer));
426 if (NULL != s->cadet_mq)
428 GNUNET_MQ_destroy (s->cadet_mq);
432 if (GNUNET_SCALARPRODUCT_STATUS_ACTIVE == s->status)
434 /* We didn't get an answer yet, fail with error */
435 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
436 prepare_client_end_notification (s);
442 * Compute our scalar product, done by Alice
444 * @param session the session associated with this computation
445 * @param prod_g_i_b_i value from Bob
446 * @param prod_h_i_b_i value from Bob
447 * @return product as MPI, never NULL
450 compute_scalar_product (struct AliceServiceSession *session,
451 gcry_mpi_point_t prod_g_i_b_i,
452 gcry_mpi_point_t prod_h_i_b_i)
454 gcry_mpi_point_t g_i_b_i_a_inv;
455 gcry_mpi_point_t g_ai_bi;
459 g_i_b_i_a_inv = GNUNET_CRYPTO_ecc_pmul_mpi (edc,
462 g_ai_bi = GNUNET_CRYPTO_ecc_add (edc,
465 gcry_mpi_point_release (g_i_b_i_a_inv);
466 ai_bi = GNUNET_CRYPTO_ecc_dlog (edc,
468 gcry_mpi_point_release (g_ai_bi);
469 if (MAX_RESULT == ai_bi)
474 ret = gcry_mpi_new (0);
475 gcry_mpi_set_ui (ret, ai_bi);
481 * Handle a response we got from another service we wanted to
482 * calculate a scalarproduct with.
484 * @param cls closure (set from #GNUNET_CADET_connect)
485 * @param channel connection to the other end
486 * @param channel_ctx place to store local state associated with the channel
487 * @param message the actual message
488 * @return #GNUNET_OK to keep the connection open,
489 * #GNUNET_SYSERR to close it (we are done)
492 handle_bobs_cryptodata_message (void *cls,
493 struct GNUNET_CADET_Channel *channel,
495 const struct GNUNET_MessageHeader *message)
497 struct AliceServiceSession *s = *channel_ctx;
498 const struct EccBobCryptodataMessage *msg;
501 gcry_mpi_point_t prod_g_i_b_i;
502 gcry_mpi_point_t prod_h_i_b_i;
507 return GNUNET_SYSERR;
509 msg_size = ntohs (message->size);
510 if (sizeof (struct EccBobCryptodataMessage) > msg_size)
513 return GNUNET_SYSERR;
515 msg = (const struct EccBobCryptodataMessage *) message;
516 contained = ntohl (msg->contained_element_count);
520 return GNUNET_SYSERR;
522 if (NULL == s->sorted_elements)
524 /* we're not ready yet, how can Bob be? */
526 return GNUNET_SYSERR;
528 if (s->total != s->client_received_element_count)
530 /* we're not ready yet, how can Bob be? */
532 return GNUNET_SYSERR;
534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
535 "Received %u crypto values from Bob\n",
536 (unsigned int) contained);
537 GNUNET_CADET_receive_done (s->channel);
538 prod_g_i_b_i = GNUNET_CRYPTO_ecc_bin_to_point (edc,
540 prod_h_i_b_i = GNUNET_CRYPTO_ecc_bin_to_point (edc,
542 s->product = compute_scalar_product (s,
545 gcry_mpi_point_release (prod_g_i_b_i);
546 gcry_mpi_point_release (prod_h_i_b_i);
547 transmit_client_response (s);
553 * Iterator to copy over messages from the hash map
554 * into an array for sorting.
556 * @param cls the `struct AliceServiceSession *`
557 * @param key the key (unused)
558 * @param value the `struct GNUNET_SCALARPRODUCT_Element *`
561 copy_element_cb (void *cls,
562 const struct GNUNET_HashCode *key,
565 struct AliceServiceSession *s = cls;
566 struct GNUNET_SCALARPRODUCT_Element *e = value;
570 mval = gcry_mpi_new (0);
571 val = (int64_t) GNUNET_ntohll (e->value);
573 gcry_mpi_sub_ui (mval, mval, -val);
575 gcry_mpi_add_ui (mval, mval, val);
576 s->sorted_elements [s->used_element_count].value = mval;
577 s->sorted_elements [s->used_element_count].r_i
578 = GNUNET_CRYPTO_ecc_random_mod_n (edc);
579 s->sorted_elements [s->used_element_count].key = &e->key;
580 s->used_element_count++;
586 * Compare two `struct MpiValue`s by key for sorting.
588 * @param a pointer to first `struct MpiValue *`
589 * @param b pointer to first `struct MpiValue *`
590 * @return -1 for a < b, 0 for a=b, 1 for a > b.
593 element_cmp (const void *a,
596 const struct MpiElement *ma = a;
597 const struct MpiElement *mb = b;
599 return GNUNET_CRYPTO_hash_cmp (ma->key,
605 * Maximum number of elements we can put into a single cryptodata
608 #define ELEMENT_CAPACITY ((GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE - 1 - sizeof (struct EccAliceCryptodataMessage)) / sizeof (struct GNUNET_CRYPTO_EccPoint))
612 * Send the cryptographic data from Alice to Bob.
613 * Does nothing if we already transferred all elements.
615 * @param s the associated service session
618 send_alices_cryptodata_message (struct AliceServiceSession *s)
620 struct EccAliceCryptodataMessage *msg;
621 struct GNUNET_MQ_Envelope *e;
622 struct GNUNET_CRYPTO_EccPoint *payload;
623 gcry_mpi_point_t g_i;
624 gcry_mpi_point_t h_i;
629 unsigned int todo_count;
632 = GNUNET_malloc (GNUNET_CONTAINER_multihashmap_size (s->intersected_elements) *
633 sizeof (struct MpiElement));
634 s->used_element_count = 0;
635 GNUNET_CONTAINER_multihashmap_iterate (s->intersected_elements,
638 LOG (GNUNET_ERROR_TYPE_DEBUG,
639 "Finished intersection, %d items remain\n",
640 s->used_element_count);
641 qsort (s->sorted_elements,
642 s->used_element_count,
643 sizeof (struct MpiElement),
646 while (off < s->used_element_count)
648 todo_count = s->used_element_count - off;
649 if (todo_count > ELEMENT_CAPACITY)
650 todo_count = ELEMENT_CAPACITY;
651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
652 "Sending %u/%u crypto values to Bob\n",
653 (unsigned int) todo_count,
654 (unsigned int) s->used_element_count);
656 e = GNUNET_MQ_msg_extra (msg,
657 todo_count * 2 * sizeof (struct GNUNET_CRYPTO_EccPoint),
658 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_ALICE_CRYPTODATA);
659 msg->contained_element_count = htonl (todo_count);
660 payload = (struct GNUNET_CRYPTO_EccPoint *) &msg[1];
661 r_ia = gcry_mpi_new (0);
662 r_ia_ai = gcry_mpi_new (0);
663 for (i = off; i < off + todo_count; i++)
665 g_i = GNUNET_CRYPTO_ecc_dexp_mpi (edc,
666 s->sorted_elements [i].r_i);
668 gcry_mpi_mul (s->sorted_elements[i].r_i,
671 /* r_ia_ai = r_ia + a_i */
672 gcry_mpi_add (r_ia_ai,
673 s->sorted_elements[i].value,
675 h_i = GNUNET_CRYPTO_ecc_dexp_mpi (edc,
677 GNUNET_CRYPTO_ecc_point_to_bin (edc,
679 &payload[(i - off) * 2]);
680 GNUNET_CRYPTO_ecc_point_to_bin (edc,
682 &payload[(i - off) * 2 + 1]);
683 gcry_mpi_point_release (g_i);
684 gcry_mpi_point_release (h_i);
686 gcry_mpi_release (r_ia);
687 gcry_mpi_release (r_ia_ai);
689 GNUNET_MQ_send (s->cadet_mq,
696 * Callback for set operation results. Called for each element
697 * that should be removed from the result set, and then once
698 * to indicate that the set intersection operation is done.
700 * @param cls closure with the `struct AliceServiceSession`
701 * @param element a result element, only valid if status is #GNUNET_SET_STATUS_OK
702 * @param status what has happened with the set intersection?
705 cb_intersection_element_removed (void *cls,
706 const struct GNUNET_SET_Element *element,
707 enum GNUNET_SET_Status status)
709 struct AliceServiceSession *s = cls;
710 struct GNUNET_SCALARPRODUCT_Element *se;
714 case GNUNET_SET_STATUS_OK:
715 /* this element has been removed from the set */
716 se = GNUNET_CONTAINER_multihashmap_get (s->intersected_elements,
718 GNUNET_assert (NULL != se);
719 LOG (GNUNET_ERROR_TYPE_DEBUG,
720 "Intersection removed element with key %s and value %lld\n",
721 GNUNET_h2s (&se->key),
722 (long long) GNUNET_ntohll (se->value));
723 GNUNET_assert (GNUNET_YES ==
724 GNUNET_CONTAINER_multihashmap_remove (s->intersected_elements,
729 case GNUNET_SET_STATUS_DONE:
730 s->intersection_op = NULL;
731 if (NULL != s->intersection_set)
733 GNUNET_SET_destroy (s->intersection_set);
734 s->intersection_set = NULL;
736 send_alices_cryptodata_message (s);
738 case GNUNET_SET_STATUS_HALF_DONE:
739 /* unexpected for intersection */
742 case GNUNET_SET_STATUS_FAILURE:
743 /* unhandled status code */
744 LOG (GNUNET_ERROR_TYPE_DEBUG,
745 "Set intersection failed!\n");
746 if (NULL != s->intersection_listen)
748 GNUNET_SET_listen_cancel (s->intersection_listen);
749 s->intersection_listen = NULL;
751 s->intersection_op = NULL;
752 if (NULL != s->intersection_set)
754 GNUNET_SET_destroy (s->intersection_set);
755 s->intersection_set = NULL;
757 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
758 prepare_client_end_notification (s);
768 * Called when another peer wants to do a set operation with the
769 * local peer. If a listen error occurs, the @a request is NULL.
771 * @param cls closure with the `struct AliceServiceSession *`
772 * @param other_peer the other peer
773 * @param context_msg message with application specific information from
775 * @param request request from the other peer (never NULL), use GNUNET_SET_accept()
776 * to accept it, otherwise the request will be refused
777 * Note that we can't just return value from the listen callback,
778 * as it is also necessary to specify the set we want to do the
779 * operation with, whith sometimes can be derived from the context
780 * message. It's necessary to specify the timeout.
783 cb_intersection_request_alice (void *cls,
784 const struct GNUNET_PeerIdentity *other_peer,
785 const struct GNUNET_MessageHeader *context_msg,
786 struct GNUNET_SET_Request *request)
788 struct AliceServiceSession *s = cls;
790 if (0 != memcmp (other_peer,
792 sizeof (struct GNUNET_PeerIdentity)))
798 = GNUNET_SET_accept (request,
799 GNUNET_SET_RESULT_REMOVED,
800 &cb_intersection_element_removed,
802 if (NULL == s->intersection_op)
805 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
806 prepare_client_end_notification (s);
810 GNUNET_SET_commit (s->intersection_op,
811 s->intersection_set))
814 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
815 prepare_client_end_notification (s);
818 GNUNET_SET_destroy (s->intersection_set);
819 s->intersection_set = NULL;
820 GNUNET_SET_listen_cancel (s->intersection_listen);
821 s->intersection_listen = NULL;
826 * Our client has finished sending us its multipart message.
828 * @param session the service session context
831 client_request_complete_alice (struct AliceServiceSession *s)
833 struct EccServiceRequestMessage *msg;
834 struct GNUNET_MQ_Envelope *e;
836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
837 "Creating new channel for session with key %s.\n",
838 GNUNET_h2s (&s->session_id));
840 = GNUNET_CADET_channel_create (my_cadet,
843 GNUNET_APPLICATION_TYPE_SCALARPRODUCT_ECC,
844 GNUNET_CADET_OPTION_RELIABLE);
845 if (NULL == s->channel)
847 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
848 prepare_client_end_notification (s);
851 s->cadet_mq = GNUNET_CADET_mq_create (s->channel);
852 s->intersection_listen
853 = GNUNET_SET_listen (cfg,
854 GNUNET_SET_OPERATION_INTERSECTION,
856 &cb_intersection_request_alice,
858 if (NULL == s->intersection_listen)
860 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
861 GNUNET_CADET_channel_destroy (s->channel);
863 prepare_client_end_notification (s);
867 e = GNUNET_MQ_msg (msg,
868 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_SESSION_INITIALIZATION);
869 msg->session_id = s->session_id;
870 GNUNET_MQ_send (s->cadet_mq,
876 * We're receiving additional set data. Add it to our
877 * set and if we are done, initiate the transaction.
880 * @param client identification of the client
881 * @param message the actual message
884 GSS_handle_alice_client_message_multipart (void *cls,
885 struct GNUNET_SERVER_Client *client,
886 const struct GNUNET_MessageHeader *message)
888 const struct ComputationBobCryptodataMultipartMessage * msg;
889 struct AliceServiceSession *s;
890 uint32_t contained_count;
891 const struct GNUNET_SCALARPRODUCT_Element *elements;
894 struct GNUNET_SET_Element set_elem;
895 struct GNUNET_SCALARPRODUCT_Element *elem;
897 s = GNUNET_SERVER_client_get_user_context (client,
898 struct AliceServiceSession);
901 /* session needs to already exist */
903 GNUNET_SERVER_receive_done (client,
907 msize = ntohs (message->size);
908 if (msize < sizeof (struct ComputationBobCryptodataMultipartMessage))
911 GNUNET_SERVER_receive_done (client,
915 msg = (const struct ComputationBobCryptodataMultipartMessage *) message;
916 contained_count = ntohl (msg->element_count_contained);
918 if ( (msize != (sizeof (struct ComputationBobCryptodataMultipartMessage) +
919 contained_count * sizeof (struct GNUNET_SCALARPRODUCT_Element))) ||
920 (0 == contained_count) ||
921 (s->total == s->client_received_element_count) ||
922 (s->total < s->client_received_element_count + contained_count) )
925 GNUNET_SERVER_receive_done (client,
929 s->client_received_element_count += contained_count;
930 elements = (const struct GNUNET_SCALARPRODUCT_Element *) &msg[1];
931 for (i = 0; i < contained_count; i++)
933 elem = GNUNET_new (struct GNUNET_SCALARPRODUCT_Element);
936 sizeof (struct GNUNET_SCALARPRODUCT_Element));
938 GNUNET_CONTAINER_multihashmap_put (s->intersected_elements,
941 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
947 set_elem.data = &elem->key;
948 set_elem.size = sizeof (elem->key);
949 set_elem.element_type = 0;
950 GNUNET_SET_add_element (s->intersection_set,
953 s->used_element_count++;
955 GNUNET_SERVER_receive_done (client,
957 if (s->total != s->client_received_element_count)
962 client_request_complete_alice (s);
967 * Handler for Alice's client request message.
968 * We are doing request-initiation to compute a scalar product with a peer.
971 * @param client identification of the client
972 * @param message the actual message
975 GSS_handle_alice_client_message (void *cls,
976 struct GNUNET_SERVER_Client *client,
977 const struct GNUNET_MessageHeader *message)
979 const struct AliceComputationMessage *msg;
980 struct AliceServiceSession *s;
981 uint32_t contained_count;
982 uint32_t total_count;
983 const struct GNUNET_SCALARPRODUCT_Element *elements;
986 struct GNUNET_SET_Element set_elem;
987 struct GNUNET_SCALARPRODUCT_Element *elem;
989 s = GNUNET_SERVER_client_get_user_context (client,
990 struct AliceServiceSession);
993 /* only one concurrent session per client connection allowed,
994 simplifies logic a lot... */
996 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
999 msize = ntohs (message->size);
1000 if (msize < sizeof (struct AliceComputationMessage))
1003 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1006 msg = (const struct AliceComputationMessage *) message;
1007 total_count = ntohl (msg->element_count_total);
1008 contained_count = ntohl (msg->element_count_contained);
1009 if ( (0 == total_count) ||
1010 (0 == contained_count) ||
1011 (msize != (sizeof (struct AliceComputationMessage) +
1012 contained_count * sizeof (struct GNUNET_SCALARPRODUCT_Element))) )
1014 GNUNET_break_op (0);
1015 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1019 s = GNUNET_new (struct AliceServiceSession);
1020 s->peer = msg->peer;
1021 s->status = GNUNET_SCALARPRODUCT_STATUS_ACTIVE;
1023 s->client_mq = GNUNET_MQ_queue_for_server_client (client);
1024 s->total = total_count;
1025 s->client_received_element_count = contained_count;
1026 s->session_id = msg->session_key;
1027 elements = (const struct GNUNET_SCALARPRODUCT_Element *) &msg[1];
1028 s->intersected_elements = GNUNET_CONTAINER_multihashmap_create (s->total,
1030 s->intersection_set = GNUNET_SET_create (cfg,
1031 GNUNET_SET_OPERATION_INTERSECTION);
1032 for (i = 0; i < contained_count; i++)
1034 if (0 == GNUNET_ntohll (elements[i].value))
1036 elem = GNUNET_new (struct GNUNET_SCALARPRODUCT_Element);
1039 sizeof (struct GNUNET_SCALARPRODUCT_Element));
1040 if (GNUNET_SYSERR ==
1041 GNUNET_CONTAINER_multihashmap_put (s->intersected_elements,
1044 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1046 /* element with same key encountered twice! */
1051 set_elem.data = &elem->key;
1052 set_elem.size = sizeof (elem->key);
1053 set_elem.element_type = 0;
1054 GNUNET_SET_add_element (s->intersection_set,
1057 s->used_element_count++;
1059 GNUNET_SERVER_client_set_user_context (client,
1061 GNUNET_SERVER_receive_done (client,
1063 if (s->total != s->client_received_element_count)
1065 /* wait for multipart msg */
1068 client_request_complete_alice (s);
1073 * Task run during shutdown.
1079 shutdown_task (void *cls,
1080 const struct GNUNET_SCHEDULER_TaskContext *tc)
1082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1083 "Shutting down, initiating cleanup.\n");
1084 // FIXME: we have to cut our connections to CADET first!
1085 if (NULL != my_cadet)
1087 GNUNET_CADET_disconnect (my_cadet);
1092 GNUNET_CRYPTO_ecc_dlog_release (edc);
1099 * A client disconnected.
1101 * Remove the associated session(s), release data structures
1102 * and cancel pending outgoing transmissions to the client.
1104 * @param cls closure, NULL
1105 * @param client identification of the client
1108 handle_client_disconnect (void *cls,
1109 struct GNUNET_SERVER_Client *client)
1111 struct AliceServiceSession *s;
1115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1116 "Client %p disconnected from us.\n",
1118 s = GNUNET_SERVER_client_get_user_context (client,
1119 struct AliceServiceSession);
1123 GNUNET_SERVER_client_set_user_context (client,
1125 destroy_service_session (s);
1130 * Initialization of the program and message handlers
1132 * @param cls closure
1133 * @param server the initialized server
1134 * @param c configuration to use
1138 struct GNUNET_SERVER_Handle *server,
1139 const struct GNUNET_CONFIGURATION_Handle *c)
1141 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1142 { &handle_bobs_cryptodata_message,
1143 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_BOB_CRYPTODATA,
1147 static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
1148 { &GSS_handle_alice_client_message, NULL,
1149 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE,
1151 { &GSS_handle_alice_client_message_multipart, NULL,
1152 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MUTLIPART_ALICE,
1158 edc = GNUNET_CRYPTO_ecc_dlog_prepare (MAX_RESULT /* max value */,
1160 /* Select a random 'a' value for Alice */
1161 GNUNET_CRYPTO_ecc_rnd_mpi (edc,
1164 GNUNET_SERVER_add_handlers (server,
1166 GNUNET_SERVER_disconnect_notify (server,
1167 &handle_client_disconnect,
1169 my_cadet = GNUNET_CADET_connect (cfg, NULL,
1170 NULL /* no incoming supported */,
1171 &cb_channel_destruction,
1174 if (NULL == my_cadet)
1176 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1177 _("Connect to CADET failed\n"));
1178 GNUNET_SCHEDULER_shutdown ();
1181 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1189 * The main function for the scalarproduct service.
1191 * @param argc number of arguments from the command line
1192 * @param argv command line arguments
1193 * @return 0 ok, 1 on error
1199 return (GNUNET_OK ==
1200 GNUNET_SERVICE_run (argc, argv,
1201 "scalarproduct-alice",
1202 GNUNET_SERVICE_OPTION_NONE,
1203 &run, NULL)) ? 0 : 1;
1206 /* end of gnunet-service-scalarproduct-ecc_alice.c */