2 This file is part of GNUnet.
3 Copyright (C) 2013-2015 GNUnet e.V.
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. At 1 million, the
44 * precomputation takes about 2s on a fast machine.
46 #define MAX_RESULT (1024 * 1024)
49 * How many values should DLOG store in memory (determines baseline
50 * RAM consumption, roughly 100 bytes times the value given here).
51 * Should be about SQRT (MAX_RESULT), larger values will make the
52 * online computation faster.
54 #define MAX_RAM (1024)
57 * An encrypted element key-value pair.
62 * Key used to identify matching pairs of values to multiply.
63 * Points into an existing data structure, to avoid copying
64 * and doubling memory use.
66 const struct GNUNET_HashCode *key;
69 * a_i value, not disclosed to Bob.
77 * A scalarproduct session which tracks
78 * a request form the client to our final response.
80 struct AliceServiceSession
84 * (hopefully) unique transaction ID
86 struct GNUNET_HashCode session_id;
89 * Alice or Bob's peerID
91 struct GNUNET_PeerIdentity peer;
94 * The client this request is related to.
96 struct GNUNET_SERVER_Client *client;
99 * The message queue for the client.
101 struct GNUNET_MQ_Handle *client_mq;
104 * The message queue for CADET.
106 struct GNUNET_MQ_Handle *cadet_mq;
109 * all non-0-value'd elements transmitted to us.
110 * Values are of type `struct GNUNET_SCALARPRODUCT_Element *`
112 struct GNUNET_CONTAINER_MultiHashMap *intersected_elements;
115 * Set of elements for which will conduction an intersection.
116 * the resulting elements are then used for computing the scalar product.
118 struct GNUNET_SET_Handle *intersection_set;
121 * Set of elements for which will conduction an intersection.
122 * the resulting elements are then used for computing the scalar product.
124 struct GNUNET_SET_OperationHandle *intersection_op;
127 * Handle to Alice's Intersection operation listening for Bob
129 struct GNUNET_SET_ListenHandle *intersection_listen;
132 * channel-handle associated with our cadet handle
134 struct GNUNET_CADET_Channel *channel;
137 * a(Alice), sorted array by key of length @e used_element_count.
139 struct MpiElement *sorted_elements;
142 * The computed scalar
147 * How many elements we were supplied with from the client (total
148 * count before intersection).
153 * How many elements actually are used for the scalar product.
154 * Size of the arrays in @e r and @e r_prime. Sometimes also
155 * reset to 0 and used as a counter!
157 uint32_t used_element_count;
160 * Already transferred elements from client to us.
161 * Less or equal than @e total.
163 uint32_t client_received_element_count;
166 * State of this session. In
167 * #GNUNET_SCALARPRODUCT_STATUS_ACTIVE while operation is
168 * ongoing, afterwards in #GNUNET_SCALARPRODUCT_STATUS_SUCCESS or
169 * #GNUNET_SCALARPRODUCT_STATUS_FAILURE.
171 enum GNUNET_SCALARPRODUCT_ResponseStatus status;
174 * Flag to prevent recursive calls to #destroy_service_session() from
183 * GNUnet configuration handle
185 static const struct GNUNET_CONFIGURATION_Handle *cfg;
188 * Context for DLOG operations on a curve.
190 static struct GNUNET_CRYPTO_EccDlogContext *edc;
193 * Alice's private key ('a').
195 static gcry_mpi_t my_privkey;
198 * Inverse of Alice's private key ('a_inv').
200 static gcry_mpi_t my_privkey_inv;
203 * Handle to the CADET service.
205 static struct GNUNET_CADET_Handle *my_cadet;
209 * Iterator called to free elements.
211 * @param cls the `struct AliceServiceSession *` (unused)
212 * @param key the key (unused)
213 * @param value value to free
214 * @return #GNUNET_OK (continue to iterate)
217 free_element_cb (void *cls,
218 const struct GNUNET_HashCode *key,
221 struct GNUNET_SCALARPRODUCT_Element *e = value;
229 * Destroy session state, we are done with it.
231 * @param s the session to free elements from
234 destroy_service_session (struct AliceServiceSession *s)
238 if (GNUNET_YES == s->in_destroy)
240 s->in_destroy = GNUNET_YES;
241 if (NULL != s->client_mq)
243 GNUNET_MQ_destroy (s->client_mq);
246 if (NULL != s->cadet_mq)
248 GNUNET_MQ_destroy (s->cadet_mq);
251 if (NULL != s->client)
253 GNUNET_SERVER_client_set_user_context (s->client,
255 GNUNET_SERVER_client_disconnect (s->client);
258 if (NULL != s->channel)
260 GNUNET_CADET_channel_destroy (s->channel);
263 if (NULL != s->intersected_elements)
265 GNUNET_CONTAINER_multihashmap_iterate (s->intersected_elements,
268 GNUNET_CONTAINER_multihashmap_destroy (s->intersected_elements);
269 s->intersected_elements = NULL;
271 if (NULL != s->intersection_listen)
273 GNUNET_SET_listen_cancel (s->intersection_listen);
274 s->intersection_listen = NULL;
276 if (NULL != s->intersection_op)
278 GNUNET_SET_operation_cancel (s->intersection_op);
279 s->intersection_op = NULL;
281 if (NULL != s->intersection_set)
283 GNUNET_SET_destroy (s->intersection_set);
284 s->intersection_set = NULL;
286 if (NULL != s->sorted_elements)
288 for (i=0;i<s->used_element_count;i++)
289 gcry_mpi_release (s->sorted_elements[i].value);
290 GNUNET_free (s->sorted_elements);
291 s->sorted_elements = NULL;
293 if (NULL != s->product)
295 gcry_mpi_release (s->product);
303 * Notify the client that the session has failed. A message gets sent
304 * to Alice's client if we encountered any error.
306 * @param session the associated client session to fail or succeed
309 prepare_client_end_notification (struct AliceServiceSession *session)
311 struct ClientResponseMessage *msg;
312 struct GNUNET_MQ_Envelope *e;
314 if (NULL == session->client_mq)
315 return; /* no client left to be notified */
316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317 "Sending session-end notification with status %d to client for session %s\n",
319 GNUNET_h2s (&session->session_id));
320 e = GNUNET_MQ_msg (msg,
321 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT);
322 msg->product_length = htonl (0);
323 msg->status = htonl (session->status);
324 GNUNET_MQ_send (session->client_mq,
330 * Prepare the final (positive) response we will send to Alice's
333 * @param s the session associated with our client.
336 transmit_client_response (struct AliceServiceSession *s)
338 struct ClientResponseMessage *msg;
339 struct GNUNET_MQ_Envelope *e;
340 unsigned char *product_exported = NULL;
341 size_t product_length = 0;
347 if (NULL == s->product)
350 prepare_client_end_notification (s);
353 value = gcry_mpi_new (0);
354 sign = gcry_mpi_cmp_ui (s->product, 0);
365 gcry_mpi_add (value, value, s->product);
369 /* result is exactly zero */
372 gcry_mpi_release (s->product);
376 (0 != (rc = gcry_mpi_aprint (GCRYMPI_FMT_STD,
381 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR,
384 prepare_client_end_notification (s);
387 gcry_mpi_release (value);
388 e = GNUNET_MQ_msg_extra (msg,
390 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT);
391 msg->status = htonl (GNUNET_SCALARPRODUCT_STATUS_SUCCESS);
392 msg->range = htonl (range);
393 msg->product_length = htonl (product_length);
394 if (NULL != product_exported)
396 GNUNET_memcpy (&msg[1],
399 GNUNET_free (product_exported);
401 GNUNET_MQ_send (s->client_mq,
403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
404 "Sent result to client, session %s has ended!\n",
405 GNUNET_h2s (&s->session_id));
411 * Function called whenever a channel is destroyed. Should clean up
412 * any associated state.
414 * It must NOT call #GNUNET_CADET_channel_destroy() on the channel.
416 * @param cls closure (set from #GNUNET_CADET_connect())
417 * @param channel connection to the other end (henceforth invalid)
418 * @param channel_ctx place where local state associated
419 * with the channel is stored
422 cb_channel_destruction (void *cls,
423 const struct GNUNET_CADET_Channel *channel,
426 struct AliceServiceSession *s = channel_ctx;
428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
429 "Peer disconnected, terminating session %s with peer %s\n",
430 GNUNET_h2s (&s->session_id),
431 GNUNET_i2s (&s->peer));
432 if (NULL != s->cadet_mq)
434 GNUNET_MQ_destroy (s->cadet_mq);
438 if (GNUNET_SCALARPRODUCT_STATUS_ACTIVE == s->status)
440 /* We didn't get an answer yet, fail with error */
441 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
442 prepare_client_end_notification (s);
448 * Compute our scalar product, done by Alice
450 * @param session the session associated with this computation
451 * @param prod_g_i_b_i value from Bob
452 * @param prod_h_i_b_i value from Bob
453 * @return product as MPI, never NULL
456 compute_scalar_product (struct AliceServiceSession *session,
457 gcry_mpi_point_t prod_g_i_b_i,
458 gcry_mpi_point_t prod_h_i_b_i)
460 gcry_mpi_point_t g_i_b_i_a_inv;
461 gcry_mpi_point_t g_ai_bi;
465 g_i_b_i_a_inv = GNUNET_CRYPTO_ecc_pmul_mpi (edc,
468 g_ai_bi = GNUNET_CRYPTO_ecc_add (edc,
471 gcry_mpi_point_release (g_i_b_i_a_inv);
472 ai_bi = GNUNET_CRYPTO_ecc_dlog (edc,
474 gcry_mpi_point_release (g_ai_bi);
475 if (INT_MAX == ai_bi)
478 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
479 "Scalar product result out of range\n");
482 ret = gcry_mpi_new (0);
485 gcry_mpi_set_ui (ret, ai_bi);
489 gcry_mpi_set_ui (ret, - ai_bi);
490 gcry_mpi_neg (ret, ret);
497 * Handle a response we got from another service we wanted to
498 * calculate a scalarproduct with.
500 * @param cls closure (set from #GNUNET_CADET_connect)
501 * @param channel connection to the other end
502 * @param channel_ctx place to store local state associated with the channel
503 * @param message the actual message
504 * @return #GNUNET_OK to keep the connection open,
505 * #GNUNET_SYSERR to close it (we are done)
508 handle_bobs_cryptodata_message (void *cls,
509 struct GNUNET_CADET_Channel *channel,
511 const struct GNUNET_MessageHeader *message)
513 struct AliceServiceSession *s = *channel_ctx;
514 const struct EccBobCryptodataMessage *msg;
517 gcry_mpi_point_t prod_g_i_b_i;
518 gcry_mpi_point_t prod_h_i_b_i;
523 return GNUNET_SYSERR;
525 msg_size = ntohs (message->size);
526 if (sizeof (struct EccBobCryptodataMessage) > msg_size)
529 return GNUNET_SYSERR;
531 msg = (const struct EccBobCryptodataMessage *) message;
532 contained = ntohl (msg->contained_element_count);
536 return GNUNET_SYSERR;
538 if (NULL == s->sorted_elements)
540 /* we're not ready yet, how can Bob be? */
542 return GNUNET_SYSERR;
544 if (s->total != s->client_received_element_count)
546 /* we're not ready yet, how can Bob be? */
548 return GNUNET_SYSERR;
550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
551 "Received %u crypto values from Bob\n",
552 (unsigned int) contained);
553 GNUNET_CADET_receive_done (s->channel);
554 prod_g_i_b_i = GNUNET_CRYPTO_ecc_bin_to_point (edc,
556 prod_h_i_b_i = GNUNET_CRYPTO_ecc_bin_to_point (edc,
558 s->product = compute_scalar_product (s,
561 gcry_mpi_point_release (prod_g_i_b_i);
562 gcry_mpi_point_release (prod_h_i_b_i);
563 transmit_client_response (s);
569 * Iterator to copy over messages from the hash map
570 * into an array for sorting.
572 * @param cls the `struct AliceServiceSession *`
573 * @param key the key (unused)
574 * @param value the `struct GNUNET_SCALARPRODUCT_Element *`
577 copy_element_cb (void *cls,
578 const struct GNUNET_HashCode *key,
581 struct AliceServiceSession *s = cls;
582 struct GNUNET_SCALARPRODUCT_Element *e = value;
586 mval = gcry_mpi_new (0);
587 val = (int64_t) GNUNET_ntohll (e->value);
589 gcry_mpi_sub_ui (mval, mval, -val);
591 gcry_mpi_add_ui (mval, mval, val);
592 s->sorted_elements [s->used_element_count].value = mval;
593 s->sorted_elements [s->used_element_count].key = &e->key;
594 s->used_element_count++;
600 * Compare two `struct MpiValue`s by key for sorting.
602 * @param a pointer to first `struct MpiValue *`
603 * @param b pointer to first `struct MpiValue *`
604 * @return -1 for a < b, 0 for a=b, 1 for a > b.
607 element_cmp (const void *a,
610 const struct MpiElement *ma = a;
611 const struct MpiElement *mb = b;
613 return GNUNET_CRYPTO_hash_cmp (ma->key,
619 * Maximum number of elements we can put into a single cryptodata
622 #define ELEMENT_CAPACITY ((GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE - 1 - sizeof (struct EccAliceCryptodataMessage)) / sizeof (struct GNUNET_CRYPTO_EccPoint))
626 * Send the cryptographic data from Alice to Bob.
627 * Does nothing if we already transferred all elements.
629 * @param s the associated service session
632 send_alices_cryptodata_message (struct AliceServiceSession *s)
634 struct EccAliceCryptodataMessage *msg;
635 struct GNUNET_MQ_Envelope *e;
636 struct GNUNET_CRYPTO_EccPoint *payload;
641 unsigned int todo_count;
644 = GNUNET_malloc (GNUNET_CONTAINER_multihashmap_size (s->intersected_elements) *
645 sizeof (struct MpiElement));
646 s->used_element_count = 0;
647 GNUNET_CONTAINER_multihashmap_iterate (s->intersected_elements,
650 LOG (GNUNET_ERROR_TYPE_DEBUG,
651 "Finished intersection, %d items remain\n",
652 s->used_element_count);
653 qsort (s->sorted_elements,
654 s->used_element_count,
655 sizeof (struct MpiElement),
658 while (off < s->used_element_count)
660 todo_count = s->used_element_count - off;
661 if (todo_count > ELEMENT_CAPACITY)
662 todo_count = ELEMENT_CAPACITY;
663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
664 "Sending %u/%u crypto values to Bob\n",
665 (unsigned int) todo_count,
666 (unsigned int) s->used_element_count);
668 e = GNUNET_MQ_msg_extra (msg,
669 todo_count * 2 * sizeof (struct GNUNET_CRYPTO_EccPoint),
670 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_ALICE_CRYPTODATA);
671 msg->contained_element_count = htonl (todo_count);
672 payload = (struct GNUNET_CRYPTO_EccPoint *) &msg[1];
673 r_ia = gcry_mpi_new (0);
674 r_ia_ai = gcry_mpi_new (0);
675 for (i = off; i < off + todo_count; i++)
678 gcry_mpi_point_t g_i;
679 gcry_mpi_point_t h_i;
681 r_i = GNUNET_CRYPTO_ecc_random_mod_n (edc);
682 g_i = GNUNET_CRYPTO_ecc_dexp_mpi (edc,
688 gcry_mpi_release (r_i);
689 /* r_ia_ai = r_ia + a_i */
690 gcry_mpi_add (r_ia_ai,
692 s->sorted_elements[i].value);
693 h_i = GNUNET_CRYPTO_ecc_dexp_mpi (edc,
695 GNUNET_CRYPTO_ecc_point_to_bin (edc,
697 &payload[(i - off) * 2]);
698 GNUNET_CRYPTO_ecc_point_to_bin (edc,
700 &payload[(i - off) * 2 + 1]);
701 gcry_mpi_point_release (g_i);
702 gcry_mpi_point_release (h_i);
704 gcry_mpi_release (r_ia);
705 gcry_mpi_release (r_ia_ai);
707 GNUNET_MQ_send (s->cadet_mq,
714 * Callback for set operation results. Called for each element
715 * that should be removed from the result set, and then once
716 * to indicate that the set intersection operation is done.
718 * @param cls closure with the `struct AliceServiceSession`
719 * @param element a result element, only valid if status is #GNUNET_SET_STATUS_OK
720 * @param status what has happened with the set intersection?
723 cb_intersection_element_removed (void *cls,
724 const struct GNUNET_SET_Element *element,
725 enum GNUNET_SET_Status status)
727 struct AliceServiceSession *s = cls;
728 struct GNUNET_SCALARPRODUCT_Element *se;
732 case GNUNET_SET_STATUS_OK:
733 /* this element has been removed from the set */
734 se = GNUNET_CONTAINER_multihashmap_get (s->intersected_elements,
736 GNUNET_assert (NULL != se);
737 LOG (GNUNET_ERROR_TYPE_DEBUG,
738 "Intersection removed element with key %s and value %lld\n",
739 GNUNET_h2s (&se->key),
740 (long long) GNUNET_ntohll (se->value));
741 GNUNET_assert (GNUNET_YES ==
742 GNUNET_CONTAINER_multihashmap_remove (s->intersected_elements,
747 case GNUNET_SET_STATUS_DONE:
748 s->intersection_op = NULL;
749 if (NULL != s->intersection_set)
751 GNUNET_SET_destroy (s->intersection_set);
752 s->intersection_set = NULL;
754 send_alices_cryptodata_message (s);
756 case GNUNET_SET_STATUS_HALF_DONE:
757 /* unexpected for intersection */
760 case GNUNET_SET_STATUS_FAILURE:
761 /* unhandled status code */
762 LOG (GNUNET_ERROR_TYPE_DEBUG,
763 "Set intersection failed!\n");
764 if (NULL != s->intersection_listen)
766 GNUNET_SET_listen_cancel (s->intersection_listen);
767 s->intersection_listen = NULL;
769 s->intersection_op = NULL;
770 if (NULL != s->intersection_set)
772 GNUNET_SET_destroy (s->intersection_set);
773 s->intersection_set = NULL;
775 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
776 prepare_client_end_notification (s);
786 * Called when another peer wants to do a set operation with the
787 * local peer. If a listen error occurs, the @a request is NULL.
789 * @param cls closure with the `struct AliceServiceSession *`
790 * @param other_peer the other peer
791 * @param context_msg message with application specific information from
793 * @param request request from the other peer (never NULL), use GNUNET_SET_accept()
794 * to accept it, otherwise the request will be refused
795 * Note that we can't just return value from the listen callback,
796 * as it is also necessary to specify the set we want to do the
797 * operation with, whith sometimes can be derived from the context
798 * message. It's necessary to specify the timeout.
801 cb_intersection_request_alice (void *cls,
802 const struct GNUNET_PeerIdentity *other_peer,
803 const struct GNUNET_MessageHeader *context_msg,
804 struct GNUNET_SET_Request *request)
806 struct AliceServiceSession *s = cls;
808 if (0 != memcmp (other_peer,
810 sizeof (struct GNUNET_PeerIdentity)))
816 = GNUNET_SET_accept (request,
817 GNUNET_SET_RESULT_REMOVED,
818 &cb_intersection_element_removed,
820 if (NULL == s->intersection_op)
823 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
824 prepare_client_end_notification (s);
828 GNUNET_SET_commit (s->intersection_op,
829 s->intersection_set))
832 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
833 prepare_client_end_notification (s);
836 GNUNET_SET_destroy (s->intersection_set);
837 s->intersection_set = NULL;
838 GNUNET_SET_listen_cancel (s->intersection_listen);
839 s->intersection_listen = NULL;
844 * Our client has finished sending us its multipart message.
846 * @param session the service session context
849 client_request_complete_alice (struct AliceServiceSession *s)
851 struct EccServiceRequestMessage *msg;
852 struct GNUNET_MQ_Envelope *e;
853 struct GNUNET_HashCode set_sid;
855 GNUNET_CRYPTO_hash (&s->session_id,
856 sizeof (struct GNUNET_HashCode),
858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
859 "Creating new channel for session with key %s.\n",
860 GNUNET_h2s (&s->session_id));
862 = GNUNET_CADET_channel_create (my_cadet,
866 GNUNET_CADET_OPTION_RELIABLE);
867 if (NULL == s->channel)
869 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
870 prepare_client_end_notification (s);
873 s->cadet_mq = GNUNET_CADET_mq_create (s->channel);
874 s->intersection_listen
875 = GNUNET_SET_listen (cfg,
876 GNUNET_SET_OPERATION_INTERSECTION,
878 &cb_intersection_request_alice,
880 if (NULL == s->intersection_listen)
882 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
883 GNUNET_CADET_channel_destroy (s->channel);
885 prepare_client_end_notification (s);
889 e = GNUNET_MQ_msg (msg,
890 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_SESSION_INITIALIZATION);
891 msg->session_id = s->session_id;
892 GNUNET_MQ_send (s->cadet_mq,
898 * We're receiving additional set data. Add it to our
899 * set and if we are done, initiate the transaction.
902 * @param client identification of the client
903 * @param message the actual message
906 GSS_handle_alice_client_message_multipart (void *cls,
907 struct GNUNET_SERVER_Client *client,
908 const struct GNUNET_MessageHeader *message)
910 const struct ComputationBobCryptodataMultipartMessage * msg;
911 struct AliceServiceSession *s;
912 uint32_t contained_count;
913 const struct GNUNET_SCALARPRODUCT_Element *elements;
916 struct GNUNET_SET_Element set_elem;
917 struct GNUNET_SCALARPRODUCT_Element *elem;
919 s = GNUNET_SERVER_client_get_user_context (client,
920 struct AliceServiceSession);
923 /* session needs to already exist */
925 GNUNET_SERVER_receive_done (client,
929 msize = ntohs (message->size);
930 if (msize < sizeof (struct ComputationBobCryptodataMultipartMessage))
933 GNUNET_SERVER_receive_done (client,
937 msg = (const struct ComputationBobCryptodataMultipartMessage *) message;
938 contained_count = ntohl (msg->element_count_contained);
940 if ( (msize != (sizeof (struct ComputationBobCryptodataMultipartMessage) +
941 contained_count * sizeof (struct GNUNET_SCALARPRODUCT_Element))) ||
942 (0 == contained_count) ||
943 (s->total == s->client_received_element_count) ||
944 (s->total < s->client_received_element_count + contained_count) )
947 GNUNET_SERVER_receive_done (client,
951 s->client_received_element_count += contained_count;
952 elements = (const struct GNUNET_SCALARPRODUCT_Element *) &msg[1];
953 for (i = 0; i < contained_count; i++)
955 elem = GNUNET_new (struct GNUNET_SCALARPRODUCT_Element);
958 sizeof (struct GNUNET_SCALARPRODUCT_Element));
960 GNUNET_CONTAINER_multihashmap_put (s->intersected_elements,
963 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
969 set_elem.data = &elem->key;
970 set_elem.size = sizeof (elem->key);
971 set_elem.element_type = 0;
972 GNUNET_SET_add_element (s->intersection_set,
975 s->used_element_count++;
977 GNUNET_SERVER_receive_done (client,
979 if (s->total != s->client_received_element_count)
984 client_request_complete_alice (s);
989 * Handler for Alice's client request message.
990 * We are doing request-initiation to compute a scalar product with a peer.
993 * @param client identification of the client
994 * @param message the actual message
997 GSS_handle_alice_client_message (void *cls,
998 struct GNUNET_SERVER_Client *client,
999 const struct GNUNET_MessageHeader *message)
1001 const struct AliceComputationMessage *msg;
1002 struct AliceServiceSession *s;
1003 uint32_t contained_count;
1004 uint32_t total_count;
1005 const struct GNUNET_SCALARPRODUCT_Element *elements;
1008 struct GNUNET_SET_Element set_elem;
1009 struct GNUNET_SCALARPRODUCT_Element *elem;
1011 s = GNUNET_SERVER_client_get_user_context (client,
1012 struct AliceServiceSession);
1015 /* only one concurrent session per client connection allowed,
1016 simplifies logic a lot... */
1018 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1021 msize = ntohs (message->size);
1022 if (msize < sizeof (struct AliceComputationMessage))
1025 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1028 msg = (const struct AliceComputationMessage *) message;
1029 total_count = ntohl (msg->element_count_total);
1030 contained_count = ntohl (msg->element_count_contained);
1031 if ( (0 == total_count) ||
1032 (0 == contained_count) ||
1033 (msize != (sizeof (struct AliceComputationMessage) +
1034 contained_count * sizeof (struct GNUNET_SCALARPRODUCT_Element))) )
1036 GNUNET_break_op (0);
1037 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1041 s = GNUNET_new (struct AliceServiceSession);
1042 s->peer = msg->peer;
1043 s->status = GNUNET_SCALARPRODUCT_STATUS_ACTIVE;
1045 s->client_mq = GNUNET_MQ_queue_for_server_client (client);
1046 s->total = total_count;
1047 s->client_received_element_count = contained_count;
1048 s->session_id = msg->session_key;
1049 elements = (const struct GNUNET_SCALARPRODUCT_Element *) &msg[1];
1050 s->intersected_elements = GNUNET_CONTAINER_multihashmap_create (s->total,
1052 s->intersection_set = GNUNET_SET_create (cfg,
1053 GNUNET_SET_OPERATION_INTERSECTION);
1054 for (i = 0; i < contained_count; i++)
1056 if (0 == GNUNET_ntohll (elements[i].value))
1058 elem = GNUNET_new (struct GNUNET_SCALARPRODUCT_Element);
1059 GNUNET_memcpy (elem,
1061 sizeof (struct GNUNET_SCALARPRODUCT_Element));
1062 if (GNUNET_SYSERR ==
1063 GNUNET_CONTAINER_multihashmap_put (s->intersected_elements,
1066 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1068 /* element with same key encountered twice! */
1073 set_elem.data = &elem->key;
1074 set_elem.size = sizeof (elem->key);
1075 set_elem.element_type = 0;
1076 GNUNET_SET_add_element (s->intersection_set,
1079 s->used_element_count++;
1081 GNUNET_SERVER_client_set_user_context (client,
1083 GNUNET_SERVER_receive_done (client,
1085 if (s->total != s->client_received_element_count)
1087 /* wait for multipart msg */
1090 client_request_complete_alice (s);
1095 * Task run during shutdown.
1101 shutdown_task (void *cls)
1103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1104 "Shutting down, initiating cleanup.\n");
1105 // FIXME: we have to cut our connections to CADET first!
1106 if (NULL != my_cadet)
1108 GNUNET_CADET_disconnect (my_cadet);
1113 GNUNET_CRYPTO_ecc_dlog_release (edc);
1120 * A client disconnected.
1122 * Remove the associated session(s), release data structures
1123 * and cancel pending outgoing transmissions to the client.
1125 * @param cls closure, NULL
1126 * @param client identification of the client
1129 handle_client_disconnect (void *cls,
1130 struct GNUNET_SERVER_Client *client)
1132 struct AliceServiceSession *s;
1136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1137 "Client %p disconnected from us.\n",
1139 s = GNUNET_SERVER_client_get_user_context (client,
1140 struct AliceServiceSession);
1144 GNUNET_SERVER_client_set_user_context (client,
1146 destroy_service_session (s);
1151 * Initialization of the program and message handlers
1153 * @param cls closure
1154 * @param server the initialized server
1155 * @param c configuration to use
1159 struct GNUNET_SERVER_Handle *server,
1160 const struct GNUNET_CONFIGURATION_Handle *c)
1162 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1163 { &handle_bobs_cryptodata_message,
1164 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_BOB_CRYPTODATA,
1168 static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
1169 { &GSS_handle_alice_client_message, NULL,
1170 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE,
1172 { &GSS_handle_alice_client_message_multipart, NULL,
1173 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MUTLIPART_ALICE,
1179 edc = GNUNET_CRYPTO_ecc_dlog_prepare (MAX_RESULT,
1181 /* Select a random 'a' value for Alice */
1182 GNUNET_CRYPTO_ecc_rnd_mpi (edc,
1185 GNUNET_SERVER_add_handlers (server,
1187 GNUNET_SERVER_disconnect_notify (server,
1188 &handle_client_disconnect,
1190 my_cadet = GNUNET_CADET_connect (cfg, NULL,
1191 &cb_channel_destruction,
1193 if (NULL == my_cadet)
1195 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1196 _("Connect to CADET failed\n"));
1197 GNUNET_SCHEDULER_shutdown ();
1200 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1207 * The main function for the scalarproduct service.
1209 * @param argc number of arguments from the command line
1210 * @param argv command line arguments
1211 * @return 0 ok, 1 on error
1217 return (GNUNET_OK ==
1218 GNUNET_SERVICE_run (argc, argv,
1219 "scalarproduct-alice",
1220 GNUNET_SERVICE_OPTION_NONE,
1221 &run, NULL)) ? 0 : 1;
1224 /* end of gnunet-service-scalarproduct-ecc_alice.c */