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);
474 gcry_mpi_set_ui (ret, ai_bi);
478 gcry_mpi_set_ui (ret, - ai_bi);
479 gcry_mpi_neg (ret, ret);
486 * Handle a response we got from another service we wanted to
487 * calculate a scalarproduct with.
489 * @param cls closure (set from #GNUNET_CADET_connect)
490 * @param channel connection to the other end
491 * @param channel_ctx place to store local state associated with the channel
492 * @param message the actual message
493 * @return #GNUNET_OK to keep the connection open,
494 * #GNUNET_SYSERR to close it (we are done)
497 handle_bobs_cryptodata_message (void *cls,
498 struct GNUNET_CADET_Channel *channel,
500 const struct GNUNET_MessageHeader *message)
502 struct AliceServiceSession *s = *channel_ctx;
503 const struct EccBobCryptodataMessage *msg;
506 gcry_mpi_point_t prod_g_i_b_i;
507 gcry_mpi_point_t prod_h_i_b_i;
512 return GNUNET_SYSERR;
514 msg_size = ntohs (message->size);
515 if (sizeof (struct EccBobCryptodataMessage) > msg_size)
518 return GNUNET_SYSERR;
520 msg = (const struct EccBobCryptodataMessage *) message;
521 contained = ntohl (msg->contained_element_count);
525 return GNUNET_SYSERR;
527 if (NULL == s->sorted_elements)
529 /* we're not ready yet, how can Bob be? */
531 return GNUNET_SYSERR;
533 if (s->total != s->client_received_element_count)
535 /* we're not ready yet, how can Bob be? */
537 return GNUNET_SYSERR;
539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
540 "Received %u crypto values from Bob\n",
541 (unsigned int) contained);
542 GNUNET_CADET_receive_done (s->channel);
543 prod_g_i_b_i = GNUNET_CRYPTO_ecc_bin_to_point (edc,
545 prod_h_i_b_i = GNUNET_CRYPTO_ecc_bin_to_point (edc,
547 s->product = compute_scalar_product (s,
550 gcry_mpi_point_release (prod_g_i_b_i);
551 gcry_mpi_point_release (prod_h_i_b_i);
552 transmit_client_response (s);
558 * Iterator to copy over messages from the hash map
559 * into an array for sorting.
561 * @param cls the `struct AliceServiceSession *`
562 * @param key the key (unused)
563 * @param value the `struct GNUNET_SCALARPRODUCT_Element *`
566 copy_element_cb (void *cls,
567 const struct GNUNET_HashCode *key,
570 struct AliceServiceSession *s = cls;
571 struct GNUNET_SCALARPRODUCT_Element *e = value;
575 mval = gcry_mpi_new (0);
576 val = (int64_t) GNUNET_ntohll (e->value);
578 gcry_mpi_sub_ui (mval, mval, -val);
580 gcry_mpi_add_ui (mval, mval, val);
581 s->sorted_elements [s->used_element_count].value = mval;
582 s->sorted_elements [s->used_element_count].key = &e->key;
583 s->used_element_count++;
589 * Compare two `struct MpiValue`s by key for sorting.
591 * @param a pointer to first `struct MpiValue *`
592 * @param b pointer to first `struct MpiValue *`
593 * @return -1 for a < b, 0 for a=b, 1 for a > b.
596 element_cmp (const void *a,
599 const struct MpiElement *ma = a;
600 const struct MpiElement *mb = b;
602 return GNUNET_CRYPTO_hash_cmp (ma->key,
608 * Maximum number of elements we can put into a single cryptodata
611 #define ELEMENT_CAPACITY ((GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE - 1 - sizeof (struct EccAliceCryptodataMessage)) / sizeof (struct GNUNET_CRYPTO_EccPoint))
615 * Send the cryptographic data from Alice to Bob.
616 * Does nothing if we already transferred all elements.
618 * @param s the associated service session
621 send_alices_cryptodata_message (struct AliceServiceSession *s)
623 struct EccAliceCryptodataMessage *msg;
624 struct GNUNET_MQ_Envelope *e;
625 struct GNUNET_CRYPTO_EccPoint *payload;
630 unsigned int todo_count;
633 = GNUNET_malloc (GNUNET_CONTAINER_multihashmap_size (s->intersected_elements) *
634 sizeof (struct MpiElement));
635 s->used_element_count = 0;
636 GNUNET_CONTAINER_multihashmap_iterate (s->intersected_elements,
639 LOG (GNUNET_ERROR_TYPE_DEBUG,
640 "Finished intersection, %d items remain\n",
641 s->used_element_count);
642 qsort (s->sorted_elements,
643 s->used_element_count,
644 sizeof (struct MpiElement),
647 while (off < s->used_element_count)
649 todo_count = s->used_element_count - off;
650 if (todo_count > ELEMENT_CAPACITY)
651 todo_count = ELEMENT_CAPACITY;
652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
653 "Sending %u/%u crypto values to Bob\n",
654 (unsigned int) todo_count,
655 (unsigned int) s->used_element_count);
657 e = GNUNET_MQ_msg_extra (msg,
658 todo_count * 2 * sizeof (struct GNUNET_CRYPTO_EccPoint),
659 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_ALICE_CRYPTODATA);
660 msg->contained_element_count = htonl (todo_count);
661 payload = (struct GNUNET_CRYPTO_EccPoint *) &msg[1];
662 r_ia = gcry_mpi_new (0);
663 r_ia_ai = gcry_mpi_new (0);
664 for (i = off; i < off + todo_count; i++)
667 gcry_mpi_point_t g_i;
668 gcry_mpi_point_t h_i;
670 r_i = GNUNET_CRYPTO_ecc_random_mod_n (edc);
671 g_i = GNUNET_CRYPTO_ecc_dexp_mpi (edc,
677 gcry_mpi_release (r_i);
678 /* r_ia_ai = r_ia + a_i */
679 gcry_mpi_add (r_ia_ai,
681 s->sorted_elements[i].value);
682 h_i = GNUNET_CRYPTO_ecc_dexp_mpi (edc,
684 GNUNET_CRYPTO_ecc_point_to_bin (edc,
686 &payload[(i - off) * 2]);
687 GNUNET_CRYPTO_ecc_point_to_bin (edc,
689 &payload[(i - off) * 2 + 1]);
690 gcry_mpi_point_release (g_i);
691 gcry_mpi_point_release (h_i);
693 gcry_mpi_release (r_ia);
694 gcry_mpi_release (r_ia_ai);
696 GNUNET_MQ_send (s->cadet_mq,
703 * Callback for set operation results. Called for each element
704 * that should be removed from the result set, and then once
705 * to indicate that the set intersection operation is done.
707 * @param cls closure with the `struct AliceServiceSession`
708 * @param element a result element, only valid if status is #GNUNET_SET_STATUS_OK
709 * @param status what has happened with the set intersection?
712 cb_intersection_element_removed (void *cls,
713 const struct GNUNET_SET_Element *element,
714 enum GNUNET_SET_Status status)
716 struct AliceServiceSession *s = cls;
717 struct GNUNET_SCALARPRODUCT_Element *se;
721 case GNUNET_SET_STATUS_OK:
722 /* this element has been removed from the set */
723 se = GNUNET_CONTAINER_multihashmap_get (s->intersected_elements,
725 GNUNET_assert (NULL != se);
726 LOG (GNUNET_ERROR_TYPE_DEBUG,
727 "Intersection removed element with key %s and value %lld\n",
728 GNUNET_h2s (&se->key),
729 (long long) GNUNET_ntohll (se->value));
730 GNUNET_assert (GNUNET_YES ==
731 GNUNET_CONTAINER_multihashmap_remove (s->intersected_elements,
736 case GNUNET_SET_STATUS_DONE:
737 s->intersection_op = NULL;
738 if (NULL != s->intersection_set)
740 GNUNET_SET_destroy (s->intersection_set);
741 s->intersection_set = NULL;
743 send_alices_cryptodata_message (s);
745 case GNUNET_SET_STATUS_HALF_DONE:
746 /* unexpected for intersection */
749 case GNUNET_SET_STATUS_FAILURE:
750 /* unhandled status code */
751 LOG (GNUNET_ERROR_TYPE_DEBUG,
752 "Set intersection failed!\n");
753 if (NULL != s->intersection_listen)
755 GNUNET_SET_listen_cancel (s->intersection_listen);
756 s->intersection_listen = NULL;
758 s->intersection_op = NULL;
759 if (NULL != s->intersection_set)
761 GNUNET_SET_destroy (s->intersection_set);
762 s->intersection_set = NULL;
764 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
765 prepare_client_end_notification (s);
775 * Called when another peer wants to do a set operation with the
776 * local peer. If a listen error occurs, the @a request is NULL.
778 * @param cls closure with the `struct AliceServiceSession *`
779 * @param other_peer the other peer
780 * @param context_msg message with application specific information from
782 * @param request request from the other peer (never NULL), use GNUNET_SET_accept()
783 * to accept it, otherwise the request will be refused
784 * Note that we can't just return value from the listen callback,
785 * as it is also necessary to specify the set we want to do the
786 * operation with, whith sometimes can be derived from the context
787 * message. It's necessary to specify the timeout.
790 cb_intersection_request_alice (void *cls,
791 const struct GNUNET_PeerIdentity *other_peer,
792 const struct GNUNET_MessageHeader *context_msg,
793 struct GNUNET_SET_Request *request)
795 struct AliceServiceSession *s = cls;
797 if (0 != memcmp (other_peer,
799 sizeof (struct GNUNET_PeerIdentity)))
805 = GNUNET_SET_accept (request,
806 GNUNET_SET_RESULT_REMOVED,
807 &cb_intersection_element_removed,
809 if (NULL == s->intersection_op)
812 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
813 prepare_client_end_notification (s);
817 GNUNET_SET_commit (s->intersection_op,
818 s->intersection_set))
821 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
822 prepare_client_end_notification (s);
825 GNUNET_SET_destroy (s->intersection_set);
826 s->intersection_set = NULL;
827 GNUNET_SET_listen_cancel (s->intersection_listen);
828 s->intersection_listen = NULL;
833 * Our client has finished sending us its multipart message.
835 * @param session the service session context
838 client_request_complete_alice (struct AliceServiceSession *s)
840 struct EccServiceRequestMessage *msg;
841 struct GNUNET_MQ_Envelope *e;
843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
844 "Creating new channel for session with key %s.\n",
845 GNUNET_h2s (&s->session_id));
847 = GNUNET_CADET_channel_create (my_cadet,
850 GNUNET_APPLICATION_TYPE_SCALARPRODUCT_ECC,
851 GNUNET_CADET_OPTION_RELIABLE);
852 if (NULL == s->channel)
854 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
855 prepare_client_end_notification (s);
858 s->cadet_mq = GNUNET_CADET_mq_create (s->channel);
859 s->intersection_listen
860 = GNUNET_SET_listen (cfg,
861 GNUNET_SET_OPERATION_INTERSECTION,
863 &cb_intersection_request_alice,
865 if (NULL == s->intersection_listen)
867 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
868 GNUNET_CADET_channel_destroy (s->channel);
870 prepare_client_end_notification (s);
874 e = GNUNET_MQ_msg (msg,
875 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_SESSION_INITIALIZATION);
876 msg->session_id = s->session_id;
877 GNUNET_MQ_send (s->cadet_mq,
883 * We're receiving additional set data. Add it to our
884 * set and if we are done, initiate the transaction.
887 * @param client identification of the client
888 * @param message the actual message
891 GSS_handle_alice_client_message_multipart (void *cls,
892 struct GNUNET_SERVER_Client *client,
893 const struct GNUNET_MessageHeader *message)
895 const struct ComputationBobCryptodataMultipartMessage * msg;
896 struct AliceServiceSession *s;
897 uint32_t contained_count;
898 const struct GNUNET_SCALARPRODUCT_Element *elements;
901 struct GNUNET_SET_Element set_elem;
902 struct GNUNET_SCALARPRODUCT_Element *elem;
904 s = GNUNET_SERVER_client_get_user_context (client,
905 struct AliceServiceSession);
908 /* session needs to already exist */
910 GNUNET_SERVER_receive_done (client,
914 msize = ntohs (message->size);
915 if (msize < sizeof (struct ComputationBobCryptodataMultipartMessage))
918 GNUNET_SERVER_receive_done (client,
922 msg = (const struct ComputationBobCryptodataMultipartMessage *) message;
923 contained_count = ntohl (msg->element_count_contained);
925 if ( (msize != (sizeof (struct ComputationBobCryptodataMultipartMessage) +
926 contained_count * sizeof (struct GNUNET_SCALARPRODUCT_Element))) ||
927 (0 == contained_count) ||
928 (s->total == s->client_received_element_count) ||
929 (s->total < s->client_received_element_count + contained_count) )
932 GNUNET_SERVER_receive_done (client,
936 s->client_received_element_count += contained_count;
937 elements = (const struct GNUNET_SCALARPRODUCT_Element *) &msg[1];
938 for (i = 0; i < contained_count; i++)
940 elem = GNUNET_new (struct GNUNET_SCALARPRODUCT_Element);
943 sizeof (struct GNUNET_SCALARPRODUCT_Element));
945 GNUNET_CONTAINER_multihashmap_put (s->intersected_elements,
948 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
954 set_elem.data = &elem->key;
955 set_elem.size = sizeof (elem->key);
956 set_elem.element_type = 0;
957 GNUNET_SET_add_element (s->intersection_set,
960 s->used_element_count++;
962 GNUNET_SERVER_receive_done (client,
964 if (s->total != s->client_received_element_count)
969 client_request_complete_alice (s);
974 * Handler for Alice's client request message.
975 * We are doing request-initiation to compute a scalar product with a peer.
978 * @param client identification of the client
979 * @param message the actual message
982 GSS_handle_alice_client_message (void *cls,
983 struct GNUNET_SERVER_Client *client,
984 const struct GNUNET_MessageHeader *message)
986 const struct AliceComputationMessage *msg;
987 struct AliceServiceSession *s;
988 uint32_t contained_count;
989 uint32_t total_count;
990 const struct GNUNET_SCALARPRODUCT_Element *elements;
993 struct GNUNET_SET_Element set_elem;
994 struct GNUNET_SCALARPRODUCT_Element *elem;
996 s = GNUNET_SERVER_client_get_user_context (client,
997 struct AliceServiceSession);
1000 /* only one concurrent session per client connection allowed,
1001 simplifies logic a lot... */
1003 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1006 msize = ntohs (message->size);
1007 if (msize < sizeof (struct AliceComputationMessage))
1010 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1013 msg = (const struct AliceComputationMessage *) message;
1014 total_count = ntohl (msg->element_count_total);
1015 contained_count = ntohl (msg->element_count_contained);
1016 if ( (0 == total_count) ||
1017 (0 == contained_count) ||
1018 (msize != (sizeof (struct AliceComputationMessage) +
1019 contained_count * sizeof (struct GNUNET_SCALARPRODUCT_Element))) )
1021 GNUNET_break_op (0);
1022 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1026 s = GNUNET_new (struct AliceServiceSession);
1027 s->peer = msg->peer;
1028 s->status = GNUNET_SCALARPRODUCT_STATUS_ACTIVE;
1030 s->client_mq = GNUNET_MQ_queue_for_server_client (client);
1031 s->total = total_count;
1032 s->client_received_element_count = contained_count;
1033 s->session_id = msg->session_key;
1034 elements = (const struct GNUNET_SCALARPRODUCT_Element *) &msg[1];
1035 s->intersected_elements = GNUNET_CONTAINER_multihashmap_create (s->total,
1037 s->intersection_set = GNUNET_SET_create (cfg,
1038 GNUNET_SET_OPERATION_INTERSECTION);
1039 for (i = 0; i < contained_count; i++)
1041 if (0 == GNUNET_ntohll (elements[i].value))
1043 elem = GNUNET_new (struct GNUNET_SCALARPRODUCT_Element);
1046 sizeof (struct GNUNET_SCALARPRODUCT_Element));
1047 if (GNUNET_SYSERR ==
1048 GNUNET_CONTAINER_multihashmap_put (s->intersected_elements,
1051 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1053 /* element with same key encountered twice! */
1058 set_elem.data = &elem->key;
1059 set_elem.size = sizeof (elem->key);
1060 set_elem.element_type = 0;
1061 GNUNET_SET_add_element (s->intersection_set,
1064 s->used_element_count++;
1066 GNUNET_SERVER_client_set_user_context (client,
1068 GNUNET_SERVER_receive_done (client,
1070 if (s->total != s->client_received_element_count)
1072 /* wait for multipart msg */
1075 client_request_complete_alice (s);
1080 * Task run during shutdown.
1086 shutdown_task (void *cls,
1087 const struct GNUNET_SCHEDULER_TaskContext *tc)
1089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1090 "Shutting down, initiating cleanup.\n");
1091 // FIXME: we have to cut our connections to CADET first!
1092 if (NULL != my_cadet)
1094 GNUNET_CADET_disconnect (my_cadet);
1099 GNUNET_CRYPTO_ecc_dlog_release (edc);
1106 * A client disconnected.
1108 * Remove the associated session(s), release data structures
1109 * and cancel pending outgoing transmissions to the client.
1111 * @param cls closure, NULL
1112 * @param client identification of the client
1115 handle_client_disconnect (void *cls,
1116 struct GNUNET_SERVER_Client *client)
1118 struct AliceServiceSession *s;
1122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1123 "Client %p disconnected from us.\n",
1125 s = GNUNET_SERVER_client_get_user_context (client,
1126 struct AliceServiceSession);
1130 GNUNET_SERVER_client_set_user_context (client,
1132 destroy_service_session (s);
1137 * Initialization of the program and message handlers
1139 * @param cls closure
1140 * @param server the initialized server
1141 * @param c configuration to use
1145 struct GNUNET_SERVER_Handle *server,
1146 const struct GNUNET_CONFIGURATION_Handle *c)
1148 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1149 { &handle_bobs_cryptodata_message,
1150 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_BOB_CRYPTODATA,
1154 static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
1155 { &GSS_handle_alice_client_message, NULL,
1156 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE,
1158 { &GSS_handle_alice_client_message_multipart, NULL,
1159 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MUTLIPART_ALICE,
1165 edc = GNUNET_CRYPTO_ecc_dlog_prepare (MAX_RESULT /* max value */,
1167 /* Select a random 'a' value for Alice */
1168 GNUNET_CRYPTO_ecc_rnd_mpi (edc,
1171 GNUNET_SERVER_add_handlers (server,
1173 GNUNET_SERVER_disconnect_notify (server,
1174 &handle_client_disconnect,
1176 my_cadet = GNUNET_CADET_connect (cfg, NULL,
1177 NULL /* no incoming supported */,
1178 &cb_channel_destruction,
1181 if (NULL == my_cadet)
1183 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1184 _("Connect to CADET failed\n"));
1185 GNUNET_SCHEDULER_shutdown ();
1188 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1196 * The main function for the scalarproduct service.
1198 * @param argc number of arguments from the command line
1199 * @param argv command line arguments
1200 * @return 0 ok, 1 on error
1206 return (GNUNET_OK ==
1207 GNUNET_SERVICE_run (argc, argv,
1208 "scalarproduct-alice",
1209 GNUNET_SERVICE_OPTION_NONE,
1210 &run, NULL)) ? 0 : 1;
1213 /* end of gnunet-service-scalarproduct-ecc_alice.c */