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.
68 * A scalarproduct session which tracks
69 * a request form the client to our final response.
71 struct AliceServiceSession
75 * (hopefully) unique transaction ID
77 struct GNUNET_HashCode session_id;
80 * Alice or Bob's peerID
82 struct GNUNET_PeerIdentity peer;
85 * The client this request is related to.
87 struct GNUNET_SERVER_Client *client;
90 * The message queue for the client.
92 struct GNUNET_MQ_Handle *client_mq;
95 * The message queue for CADET.
97 struct GNUNET_MQ_Handle *cadet_mq;
100 * all non-0-value'd elements transmitted to us.
101 * Values are of type `struct GNUNET_SCALARPRODUCT_Element *`
103 struct GNUNET_CONTAINER_MultiHashMap *intersected_elements;
106 * Set of elements for which will conduction an intersection.
107 * the resulting elements are then used for computing the scalar product.
109 struct GNUNET_SET_Handle *intersection_set;
112 * Set of elements for which will conduction an intersection.
113 * the resulting elements are then used for computing the scalar product.
115 struct GNUNET_SET_OperationHandle *intersection_op;
118 * Handle to Alice's Intersection operation listening for Bob
120 struct GNUNET_SET_ListenHandle *intersection_listen;
123 * channel-handle associated with our cadet handle
125 struct GNUNET_CADET_Channel *channel;
128 * a(Alice), sorted array by key of length @e used_element_count.
130 struct MpiElement *sorted_elements;
133 * The computed scalar
138 * How many elements we were supplied with from the client (total
139 * count before intersection).
144 * How many elements actually are used for the scalar product.
145 * Size of the arrays in @e r and @e r_prime. Sometimes also
146 * reset to 0 and used as a counter!
148 uint32_t used_element_count;
151 * Already transferred elements from client to us.
152 * Less or equal than @e total.
154 uint32_t client_received_element_count;
157 * State of this session. In
158 * #GNUNET_SCALARPRODUCT_STATUS_ACTIVE while operation is
159 * ongoing, afterwards in #GNUNET_SCALARPRODUCT_STATUS_SUCCESS or
160 * #GNUNET_SCALARPRODUCT_STATUS_FAILURE.
162 enum GNUNET_SCALARPRODUCT_ResponseStatus status;
165 * Flag to prevent recursive calls to #destroy_service_session() from
174 * GNUnet configuration handle
176 static const struct GNUNET_CONFIGURATION_Handle *cfg;
179 * Context for DLOG operations on a curve.
181 static struct GNUNET_CRYPTO_EccDlogContext *edc;
184 * Alice's private key ('a').
186 static gcry_mpi_t my_privkey;
189 * Inverse of Alice's private key ('a_inv').
191 static gcry_mpi_t my_privkey_inv;
194 * Handle to the CADET service.
196 static struct GNUNET_CADET_Handle *my_cadet;
200 * Iterator called to free elements.
202 * @param cls the `struct AliceServiceSession *` (unused)
203 * @param key the key (unused)
204 * @param value value to free
205 * @return #GNUNET_OK (continue to iterate)
208 free_element_cb (void *cls,
209 const struct GNUNET_HashCode *key,
212 struct GNUNET_SCALARPRODUCT_Element *e = value;
220 * Destroy session state, we are done with it.
222 * @param s the session to free elements from
225 destroy_service_session (struct AliceServiceSession *s)
229 if (GNUNET_YES == s->in_destroy)
231 s->in_destroy = GNUNET_YES;
232 if (NULL != s->client_mq)
234 GNUNET_MQ_destroy (s->client_mq);
237 if (NULL != s->cadet_mq)
239 GNUNET_MQ_destroy (s->cadet_mq);
242 if (NULL != s->client)
244 GNUNET_SERVER_client_set_user_context (s->client,
246 GNUNET_SERVER_client_disconnect (s->client);
249 if (NULL != s->channel)
251 GNUNET_CADET_channel_destroy (s->channel);
254 if (NULL != s->intersected_elements)
256 GNUNET_CONTAINER_multihashmap_iterate (s->intersected_elements,
259 GNUNET_CONTAINER_multihashmap_destroy (s->intersected_elements);
260 s->intersected_elements = NULL;
262 if (NULL != s->intersection_listen)
264 GNUNET_SET_listen_cancel (s->intersection_listen);
265 s->intersection_listen = NULL;
267 if (NULL != s->intersection_op)
269 GNUNET_SET_operation_cancel (s->intersection_op);
270 s->intersection_op = NULL;
272 if (NULL != s->intersection_set)
274 GNUNET_SET_destroy (s->intersection_set);
275 s->intersection_set = NULL;
277 if (NULL != s->sorted_elements)
279 for (i=0;i<s->used_element_count;i++)
280 gcry_mpi_release (s->sorted_elements[i].value);
281 GNUNET_free (s->sorted_elements);
282 s->sorted_elements = NULL;
284 if (NULL != s->product)
286 gcry_mpi_release (s->product);
294 * Notify the client that the session has failed. A message gets sent
295 * to Alice's client if we encountered any error.
297 * @param session the associated client session to fail or succeed
300 prepare_client_end_notification (struct AliceServiceSession *session)
302 struct ClientResponseMessage *msg;
303 struct GNUNET_MQ_Envelope *e;
305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
306 "Sending session-end notification with status %d to client for session %s\n",
308 GNUNET_h2s (&session->session_id));
309 e = GNUNET_MQ_msg (msg,
310 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT);
311 msg->product_length = htonl (0);
312 msg->status = htonl (session->status);
313 GNUNET_MQ_send (session->client_mq,
319 * Prepare the final (positive) response we will send to Alice's
322 * @param s the session associated with our client.
325 transmit_client_response (struct AliceServiceSession *s)
327 struct ClientResponseMessage *msg;
328 struct GNUNET_MQ_Envelope *e;
329 unsigned char *product_exported = NULL;
330 size_t product_length = 0;
336 if (NULL == s->product)
339 prepare_client_end_notification (s);
342 value = gcry_mpi_new (0);
343 sign = gcry_mpi_cmp_ui (s->product, 0);
354 gcry_mpi_add (value, value, s->product);
358 /* result is exactly zero */
361 gcry_mpi_release (s->product);
365 (0 != (rc = gcry_mpi_aprint (GCRYMPI_FMT_STD,
370 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR,
373 prepare_client_end_notification (s);
376 gcry_mpi_release (value);
377 e = GNUNET_MQ_msg_extra (msg,
379 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT);
380 msg->status = htonl (GNUNET_SCALARPRODUCT_STATUS_SUCCESS);
381 msg->range = htonl (range);
382 msg->product_length = htonl (product_length);
383 if (NULL != product_exported)
388 GNUNET_free (product_exported);
390 GNUNET_MQ_send (s->client_mq,
392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
393 "Sent result to client, session %s has ended!\n",
394 GNUNET_h2s (&s->session_id));
400 * Function called whenever a channel is destroyed. Should clean up
401 * any associated state.
403 * It must NOT call #GNUNET_CADET_channel_destroy() on the channel.
405 * @param cls closure (set from #GNUNET_CADET_connect())
406 * @param channel connection to the other end (henceforth invalid)
407 * @param channel_ctx place where local state associated
408 * with the channel is stored
411 cb_channel_destruction (void *cls,
412 const struct GNUNET_CADET_Channel *channel,
415 struct AliceServiceSession *s = channel_ctx;
417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
418 "Peer disconnected, terminating session %s with peer %s\n",
419 GNUNET_h2s (&s->session_id),
420 GNUNET_i2s (&s->peer));
421 if (NULL != s->cadet_mq)
423 GNUNET_MQ_destroy (s->cadet_mq);
427 if (GNUNET_SCALARPRODUCT_STATUS_ACTIVE == s->status)
429 /* We didn't get an answer yet, fail with error */
430 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
431 prepare_client_end_notification (s);
437 * Compute our scalar product, done by Alice
439 * @param session the session associated with this computation
440 * @param prod_g_i_b_i value from Bob
441 * @param prod_h_i_b_i value from Bob
442 * @return product as MPI, never NULL
445 compute_scalar_product (struct AliceServiceSession *session,
446 gcry_mpi_point_t prod_g_i_b_i,
447 gcry_mpi_point_t prod_h_i_b_i)
449 gcry_mpi_point_t g_i_b_i_a_inv;
450 gcry_mpi_point_t g_ai_bi;
454 g_i_b_i_a_inv = GNUNET_CRYPTO_ecc_pmul_mpi (edc,
457 g_ai_bi = GNUNET_CRYPTO_ecc_add (edc,
460 gcry_mpi_point_release (g_i_b_i_a_inv);
461 ai_bi = GNUNET_CRYPTO_ecc_dlog (edc,
463 gcry_mpi_point_release (g_ai_bi);
464 if (MAX_RESULT == ai_bi)
467 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
468 "Scalar product result out of range\n");
471 ret = gcry_mpi_new (0);
472 gcry_mpi_set_ui (ret, ai_bi);
478 * Handle a response we got from another service we wanted to
479 * calculate a scalarproduct with.
481 * @param cls closure (set from #GNUNET_CADET_connect)
482 * @param channel connection to the other end
483 * @param channel_ctx place to store local state associated with the channel
484 * @param message the actual message
485 * @return #GNUNET_OK to keep the connection open,
486 * #GNUNET_SYSERR to close it (we are done)
489 handle_bobs_cryptodata_message (void *cls,
490 struct GNUNET_CADET_Channel *channel,
492 const struct GNUNET_MessageHeader *message)
494 struct AliceServiceSession *s = *channel_ctx;
495 const struct EccBobCryptodataMessage *msg;
498 gcry_mpi_point_t prod_g_i_b_i;
499 gcry_mpi_point_t prod_h_i_b_i;
504 return GNUNET_SYSERR;
506 msg_size = ntohs (message->size);
507 if (sizeof (struct EccBobCryptodataMessage) > msg_size)
510 return GNUNET_SYSERR;
512 msg = (const struct EccBobCryptodataMessage *) message;
513 contained = ntohl (msg->contained_element_count);
517 return GNUNET_SYSERR;
519 if (NULL == s->sorted_elements)
521 /* we're not ready yet, how can Bob be? */
523 return GNUNET_SYSERR;
525 if (s->total != s->client_received_element_count)
527 /* we're not ready yet, how can Bob be? */
529 return GNUNET_SYSERR;
531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
532 "Received %u crypto values from Bob\n",
533 (unsigned int) contained);
534 GNUNET_CADET_receive_done (s->channel);
535 prod_g_i_b_i = GNUNET_CRYPTO_ecc_bin_to_point (edc,
537 prod_h_i_b_i = GNUNET_CRYPTO_ecc_bin_to_point (edc,
539 s->product = compute_scalar_product (s,
542 gcry_mpi_point_release (prod_g_i_b_i);
543 gcry_mpi_point_release (prod_h_i_b_i);
544 transmit_client_response (s);
550 * Iterator to copy over messages from the hash map
551 * into an array for sorting.
553 * @param cls the `struct AliceServiceSession *`
554 * @param key the key (unused)
555 * @param value the `struct GNUNET_SCALARPRODUCT_Element *`
558 copy_element_cb (void *cls,
559 const struct GNUNET_HashCode *key,
562 struct AliceServiceSession *s = cls;
563 struct GNUNET_SCALARPRODUCT_Element *e = value;
567 mval = gcry_mpi_new (0);
568 val = (int64_t) GNUNET_ntohll (e->value);
570 gcry_mpi_sub_ui (mval, mval, -val);
572 gcry_mpi_add_ui (mval, mval, val);
573 s->sorted_elements [s->used_element_count].value = mval;
574 s->sorted_elements [s->used_element_count].key = &e->key;
575 s->used_element_count++;
581 * Compare two `struct MpiValue`s by key for sorting.
583 * @param a pointer to first `struct MpiValue *`
584 * @param b pointer to first `struct MpiValue *`
585 * @return -1 for a < b, 0 for a=b, 1 for a > b.
588 element_cmp (const void *a,
591 const struct MpiElement *ma = a;
592 const struct MpiElement *mb = b;
594 return GNUNET_CRYPTO_hash_cmp (ma->key,
600 * Maximum number of elements we can put into a single cryptodata
603 #define ELEMENT_CAPACITY ((GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE - 1 - sizeof (struct EccAliceCryptodataMessage)) / sizeof (struct GNUNET_CRYPTO_EccPoint))
607 * Send the cryptographic data from Alice to Bob.
608 * Does nothing if we already transferred all elements.
610 * @param s the associated service session
613 send_alices_cryptodata_message (struct AliceServiceSession *s)
615 struct EccAliceCryptodataMessage *msg;
616 struct GNUNET_MQ_Envelope *e;
617 struct GNUNET_CRYPTO_EccPoint *payload;
622 unsigned int todo_count;
625 = GNUNET_malloc (GNUNET_CONTAINER_multihashmap_size (s->intersected_elements) *
626 sizeof (struct MpiElement));
627 s->used_element_count = 0;
628 GNUNET_CONTAINER_multihashmap_iterate (s->intersected_elements,
631 LOG (GNUNET_ERROR_TYPE_DEBUG,
632 "Finished intersection, %d items remain\n",
633 s->used_element_count);
634 qsort (s->sorted_elements,
635 s->used_element_count,
636 sizeof (struct MpiElement),
639 while (off < s->used_element_count)
641 todo_count = s->used_element_count - off;
642 if (todo_count > ELEMENT_CAPACITY)
643 todo_count = ELEMENT_CAPACITY;
644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
645 "Sending %u/%u crypto values to Bob\n",
646 (unsigned int) todo_count,
647 (unsigned int) s->used_element_count);
649 e = GNUNET_MQ_msg_extra (msg,
650 todo_count * 2 * sizeof (struct GNUNET_CRYPTO_EccPoint),
651 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_ALICE_CRYPTODATA);
652 msg->contained_element_count = htonl (todo_count);
653 payload = (struct GNUNET_CRYPTO_EccPoint *) &msg[1];
654 r_ia = gcry_mpi_new (0);
655 r_ia_ai = gcry_mpi_new (0);
656 for (i = off; i < off + todo_count; i++)
659 gcry_mpi_point_t g_i;
660 gcry_mpi_point_t h_i;
662 r_i = GNUNET_CRYPTO_ecc_random_mod_n (edc);
663 g_i = GNUNET_CRYPTO_ecc_dexp_mpi (edc,
669 gcry_mpi_release (r_i);
670 /* r_ia_ai = r_ia + a_i */
671 gcry_mpi_add (r_ia_ai,
672 s->sorted_elements[i].value,
674 h_i = GNUNET_CRYPTO_ecc_dexp_mpi (edc,
676 GNUNET_CRYPTO_ecc_point_to_bin (edc,
678 &payload[(i - off) * 2]);
679 GNUNET_CRYPTO_ecc_point_to_bin (edc,
681 &payload[(i - off) * 2 + 1]);
682 gcry_mpi_point_release (g_i);
683 gcry_mpi_point_release (h_i);
685 gcry_mpi_release (r_ia);
686 gcry_mpi_release (r_ia_ai);
688 GNUNET_MQ_send (s->cadet_mq,
695 * Callback for set operation results. Called for each element
696 * that should be removed from the result set, and then once
697 * to indicate that the set intersection operation is done.
699 * @param cls closure with the `struct AliceServiceSession`
700 * @param element a result element, only valid if status is #GNUNET_SET_STATUS_OK
701 * @param status what has happened with the set intersection?
704 cb_intersection_element_removed (void *cls,
705 const struct GNUNET_SET_Element *element,
706 enum GNUNET_SET_Status status)
708 struct AliceServiceSession *s = cls;
709 struct GNUNET_SCALARPRODUCT_Element *se;
713 case GNUNET_SET_STATUS_OK:
714 /* this element has been removed from the set */
715 se = GNUNET_CONTAINER_multihashmap_get (s->intersected_elements,
717 GNUNET_assert (NULL != se);
718 LOG (GNUNET_ERROR_TYPE_DEBUG,
719 "Intersection removed element with key %s and value %lld\n",
720 GNUNET_h2s (&se->key),
721 (long long) GNUNET_ntohll (se->value));
722 GNUNET_assert (GNUNET_YES ==
723 GNUNET_CONTAINER_multihashmap_remove (s->intersected_elements,
728 case GNUNET_SET_STATUS_DONE:
729 s->intersection_op = NULL;
730 if (NULL != s->intersection_set)
732 GNUNET_SET_destroy (s->intersection_set);
733 s->intersection_set = NULL;
735 send_alices_cryptodata_message (s);
737 case GNUNET_SET_STATUS_HALF_DONE:
738 /* unexpected for intersection */
741 case GNUNET_SET_STATUS_FAILURE:
742 /* unhandled status code */
743 LOG (GNUNET_ERROR_TYPE_DEBUG,
744 "Set intersection failed!\n");
745 if (NULL != s->intersection_listen)
747 GNUNET_SET_listen_cancel (s->intersection_listen);
748 s->intersection_listen = NULL;
750 s->intersection_op = NULL;
751 if (NULL != s->intersection_set)
753 GNUNET_SET_destroy (s->intersection_set);
754 s->intersection_set = NULL;
756 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
757 prepare_client_end_notification (s);
767 * Called when another peer wants to do a set operation with the
768 * local peer. If a listen error occurs, the @a request is NULL.
770 * @param cls closure with the `struct AliceServiceSession *`
771 * @param other_peer the other peer
772 * @param context_msg message with application specific information from
774 * @param request request from the other peer (never NULL), use GNUNET_SET_accept()
775 * to accept it, otherwise the request will be refused
776 * Note that we can't just return value from the listen callback,
777 * as it is also necessary to specify the set we want to do the
778 * operation with, whith sometimes can be derived from the context
779 * message. It's necessary to specify the timeout.
782 cb_intersection_request_alice (void *cls,
783 const struct GNUNET_PeerIdentity *other_peer,
784 const struct GNUNET_MessageHeader *context_msg,
785 struct GNUNET_SET_Request *request)
787 struct AliceServiceSession *s = cls;
789 if (0 != memcmp (other_peer,
791 sizeof (struct GNUNET_PeerIdentity)))
797 = GNUNET_SET_accept (request,
798 GNUNET_SET_RESULT_REMOVED,
799 &cb_intersection_element_removed,
801 if (NULL == s->intersection_op)
804 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
805 prepare_client_end_notification (s);
809 GNUNET_SET_commit (s->intersection_op,
810 s->intersection_set))
813 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
814 prepare_client_end_notification (s);
817 GNUNET_SET_destroy (s->intersection_set);
818 s->intersection_set = NULL;
819 GNUNET_SET_listen_cancel (s->intersection_listen);
820 s->intersection_listen = NULL;
825 * Our client has finished sending us its multipart message.
827 * @param session the service session context
830 client_request_complete_alice (struct AliceServiceSession *s)
832 struct EccServiceRequestMessage *msg;
833 struct GNUNET_MQ_Envelope *e;
835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
836 "Creating new channel for session with key %s.\n",
837 GNUNET_h2s (&s->session_id));
839 = GNUNET_CADET_channel_create (my_cadet,
842 GNUNET_APPLICATION_TYPE_SCALARPRODUCT_ECC,
843 GNUNET_CADET_OPTION_RELIABLE);
844 if (NULL == s->channel)
846 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
847 prepare_client_end_notification (s);
850 s->cadet_mq = GNUNET_CADET_mq_create (s->channel);
851 s->intersection_listen
852 = GNUNET_SET_listen (cfg,
853 GNUNET_SET_OPERATION_INTERSECTION,
855 &cb_intersection_request_alice,
857 if (NULL == s->intersection_listen)
859 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
860 GNUNET_CADET_channel_destroy (s->channel);
862 prepare_client_end_notification (s);
866 e = GNUNET_MQ_msg (msg,
867 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_SESSION_INITIALIZATION);
868 msg->session_id = s->session_id;
869 GNUNET_MQ_send (s->cadet_mq,
875 * We're receiving additional set data. Add it to our
876 * set and if we are done, initiate the transaction.
879 * @param client identification of the client
880 * @param message the actual message
883 GSS_handle_alice_client_message_multipart (void *cls,
884 struct GNUNET_SERVER_Client *client,
885 const struct GNUNET_MessageHeader *message)
887 const struct ComputationBobCryptodataMultipartMessage * msg;
888 struct AliceServiceSession *s;
889 uint32_t contained_count;
890 const struct GNUNET_SCALARPRODUCT_Element *elements;
893 struct GNUNET_SET_Element set_elem;
894 struct GNUNET_SCALARPRODUCT_Element *elem;
896 s = GNUNET_SERVER_client_get_user_context (client,
897 struct AliceServiceSession);
900 /* session needs to already exist */
902 GNUNET_SERVER_receive_done (client,
906 msize = ntohs (message->size);
907 if (msize < sizeof (struct ComputationBobCryptodataMultipartMessage))
910 GNUNET_SERVER_receive_done (client,
914 msg = (const struct ComputationBobCryptodataMultipartMessage *) message;
915 contained_count = ntohl (msg->element_count_contained);
917 if ( (msize != (sizeof (struct ComputationBobCryptodataMultipartMessage) +
918 contained_count * sizeof (struct GNUNET_SCALARPRODUCT_Element))) ||
919 (0 == contained_count) ||
920 (s->total == s->client_received_element_count) ||
921 (s->total < s->client_received_element_count + contained_count) )
924 GNUNET_SERVER_receive_done (client,
928 s->client_received_element_count += contained_count;
929 elements = (const struct GNUNET_SCALARPRODUCT_Element *) &msg[1];
930 for (i = 0; i < contained_count; i++)
932 elem = GNUNET_new (struct GNUNET_SCALARPRODUCT_Element);
935 sizeof (struct GNUNET_SCALARPRODUCT_Element));
937 GNUNET_CONTAINER_multihashmap_put (s->intersected_elements,
940 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
946 set_elem.data = &elem->key;
947 set_elem.size = sizeof (elem->key);
948 set_elem.element_type = 0;
949 GNUNET_SET_add_element (s->intersection_set,
952 s->used_element_count++;
954 GNUNET_SERVER_receive_done (client,
956 if (s->total != s->client_received_element_count)
961 client_request_complete_alice (s);
966 * Handler for Alice's client request message.
967 * We are doing request-initiation to compute a scalar product with a peer.
970 * @param client identification of the client
971 * @param message the actual message
974 GSS_handle_alice_client_message (void *cls,
975 struct GNUNET_SERVER_Client *client,
976 const struct GNUNET_MessageHeader *message)
978 const struct AliceComputationMessage *msg;
979 struct AliceServiceSession *s;
980 uint32_t contained_count;
981 uint32_t total_count;
982 const struct GNUNET_SCALARPRODUCT_Element *elements;
985 struct GNUNET_SET_Element set_elem;
986 struct GNUNET_SCALARPRODUCT_Element *elem;
988 s = GNUNET_SERVER_client_get_user_context (client,
989 struct AliceServiceSession);
992 /* only one concurrent session per client connection allowed,
993 simplifies logic a lot... */
995 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
998 msize = ntohs (message->size);
999 if (msize < sizeof (struct AliceComputationMessage))
1002 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1005 msg = (const struct AliceComputationMessage *) message;
1006 total_count = ntohl (msg->element_count_total);
1007 contained_count = ntohl (msg->element_count_contained);
1008 if ( (0 == total_count) ||
1009 (0 == contained_count) ||
1010 (msize != (sizeof (struct AliceComputationMessage) +
1011 contained_count * sizeof (struct GNUNET_SCALARPRODUCT_Element))) )
1013 GNUNET_break_op (0);
1014 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1018 s = GNUNET_new (struct AliceServiceSession);
1019 s->peer = msg->peer;
1020 s->status = GNUNET_SCALARPRODUCT_STATUS_ACTIVE;
1022 s->client_mq = GNUNET_MQ_queue_for_server_client (client);
1023 s->total = total_count;
1024 s->client_received_element_count = contained_count;
1025 s->session_id = msg->session_key;
1026 elements = (const struct GNUNET_SCALARPRODUCT_Element *) &msg[1];
1027 s->intersected_elements = GNUNET_CONTAINER_multihashmap_create (s->total,
1029 s->intersection_set = GNUNET_SET_create (cfg,
1030 GNUNET_SET_OPERATION_INTERSECTION);
1031 for (i = 0; i < contained_count; i++)
1033 if (0 == GNUNET_ntohll (elements[i].value))
1035 elem = GNUNET_new (struct GNUNET_SCALARPRODUCT_Element);
1038 sizeof (struct GNUNET_SCALARPRODUCT_Element));
1039 if (GNUNET_SYSERR ==
1040 GNUNET_CONTAINER_multihashmap_put (s->intersected_elements,
1043 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1045 /* element with same key encountered twice! */
1050 set_elem.data = &elem->key;
1051 set_elem.size = sizeof (elem->key);
1052 set_elem.element_type = 0;
1053 GNUNET_SET_add_element (s->intersection_set,
1056 s->used_element_count++;
1058 GNUNET_SERVER_client_set_user_context (client,
1060 GNUNET_SERVER_receive_done (client,
1062 if (s->total != s->client_received_element_count)
1064 /* wait for multipart msg */
1067 client_request_complete_alice (s);
1072 * Task run during shutdown.
1078 shutdown_task (void *cls,
1079 const struct GNUNET_SCHEDULER_TaskContext *tc)
1081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1082 "Shutting down, initiating cleanup.\n");
1083 // FIXME: we have to cut our connections to CADET first!
1084 if (NULL != my_cadet)
1086 GNUNET_CADET_disconnect (my_cadet);
1091 GNUNET_CRYPTO_ecc_dlog_release (edc);
1098 * A client disconnected.
1100 * Remove the associated session(s), release data structures
1101 * and cancel pending outgoing transmissions to the client.
1103 * @param cls closure, NULL
1104 * @param client identification of the client
1107 handle_client_disconnect (void *cls,
1108 struct GNUNET_SERVER_Client *client)
1110 struct AliceServiceSession *s;
1114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1115 "Client %p disconnected from us.\n",
1117 s = GNUNET_SERVER_client_get_user_context (client,
1118 struct AliceServiceSession);
1122 GNUNET_SERVER_client_set_user_context (client,
1124 destroy_service_session (s);
1129 * Initialization of the program and message handlers
1131 * @param cls closure
1132 * @param server the initialized server
1133 * @param c configuration to use
1137 struct GNUNET_SERVER_Handle *server,
1138 const struct GNUNET_CONFIGURATION_Handle *c)
1140 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1141 { &handle_bobs_cryptodata_message,
1142 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_BOB_CRYPTODATA,
1146 static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
1147 { &GSS_handle_alice_client_message, NULL,
1148 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE,
1150 { &GSS_handle_alice_client_message_multipart, NULL,
1151 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MUTLIPART_ALICE,
1157 edc = GNUNET_CRYPTO_ecc_dlog_prepare (MAX_RESULT /* max value */,
1159 /* Select a random 'a' value for Alice */
1160 GNUNET_CRYPTO_ecc_rnd_mpi (edc,
1163 GNUNET_SERVER_add_handlers (server,
1165 GNUNET_SERVER_disconnect_notify (server,
1166 &handle_client_disconnect,
1168 my_cadet = GNUNET_CADET_connect (cfg, NULL,
1169 NULL /* no incoming supported */,
1170 &cb_channel_destruction,
1173 if (NULL == my_cadet)
1175 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1176 _("Connect to CADET failed\n"));
1177 GNUNET_SCHEDULER_shutdown ();
1180 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1188 * The main function for the scalarproduct service.
1190 * @param argc number of arguments from the command line
1191 * @param argv command line arguments
1192 * @return 0 ok, 1 on error
1198 return (GNUNET_OK ==
1199 GNUNET_SERVICE_run (argc, argv,
1200 "scalarproduct-alice",
1201 GNUNET_SERVICE_OPTION_NONE,
1202 &run, NULL)) ? 0 : 1;
1205 /* end of gnunet-service-scalarproduct-ecc_alice.c */