2 This file is part of GNUnet.
3 Copyright (C) 2013, 2014 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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file scalarproduct/gnunet-service-scalarproduct_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.h"
39 #define LOG(kind,...) GNUNET_log_from (kind, "scalarproduct-alice", __VA_ARGS__)
42 * An encrypted element key-value pair.
47 * Key used to identify matching pairs of values to multiply.
48 * Points into an existing data structure, to avoid copying
49 * and doubling memory use.
51 const struct GNUNET_HashCode *key;
54 * Value represented (a).
61 * A scalarproduct session which tracks
62 * a request form the client to our final response.
64 struct AliceServiceSession
68 * (hopefully) unique transaction ID
70 struct GNUNET_HashCode session_id;
73 * Alice or Bob's peerID
75 struct GNUNET_PeerIdentity peer;
78 * The client this request is related to.
80 struct GNUNET_SERVER_Client *client;
83 * The message queue for the client.
85 struct GNUNET_MQ_Handle *client_mq;
88 * The message queue for CADET.
90 struct GNUNET_MQ_Handle *cadet_mq;
93 * all non-0-value'd elements transmitted to us.
94 * Values are of type `struct GNUNET_SCALARPRODUCT_Element *`
96 struct GNUNET_CONTAINER_MultiHashMap *intersected_elements;
99 * Set of elements for which will conduction an intersection.
100 * the resulting elements are then used for computing the scalar product.
102 struct GNUNET_SET_Handle *intersection_set;
105 * Set of elements for which will conduction an intersection.
106 * the resulting elements are then used for computing the scalar product.
108 struct GNUNET_SET_OperationHandle *intersection_op;
111 * Handle to Alice's Intersection operation listening for Bob
113 struct GNUNET_SET_ListenHandle *intersection_listen;
116 * channel-handle associated with our cadet handle
118 struct GNUNET_CADET_Channel *channel;
121 * a(Alice), sorted array by key of length @e used_element_count.
123 struct MpiElement *sorted_elements;
126 * Bob's permutation p of R
128 struct GNUNET_CRYPTO_PaillierCiphertext *r;
131 * Bob's permutation q of R
133 struct GNUNET_CRYPTO_PaillierCiphertext *r_prime;
138 struct GNUNET_CRYPTO_PaillierCiphertext s;
143 struct GNUNET_CRYPTO_PaillierCiphertext s_prime;
146 * The computed scalar
151 * How many elements we were supplied with from the client (total
152 * count before intersection).
157 * How many elements actually are used for the scalar product.
158 * Size of the arrays in @e r and @e r_prime. Sometimes also
159 * reset to 0 and used as a counter!
161 uint32_t used_element_count;
164 * Already transferred elements from client to us.
165 * Less or equal than @e total.
167 uint32_t client_received_element_count;
170 * Already transferred elements from Bob to us.
171 * Less or equal than @e total.
173 uint32_t cadet_received_element_count;
176 * State of this session. In
177 * #GNUNET_SCALARPRODUCT_STATUS_ACTIVE while operation is
178 * ongoing, afterwards in #GNUNET_SCALARPRODUCT_STATUS_SUCCESS or
179 * #GNUNET_SCALARPRODUCT_STATUS_FAILURE.
181 enum GNUNET_SCALARPRODUCT_ResponseStatus status;
184 * Flag to prevent recursive calls to #destroy_service_session() from
193 * GNUnet configuration handle
195 static const struct GNUNET_CONFIGURATION_Handle *cfg;
198 * Service's own public key
200 static struct GNUNET_CRYPTO_PaillierPublicKey my_pubkey;
203 * Service's own private key
205 static struct GNUNET_CRYPTO_PaillierPrivateKey my_privkey;
208 * Service's offset for values that could possibly be negative but are plaintext for encryption.
210 static gcry_mpi_t my_offset;
213 * Handle to the CADET service.
215 static struct GNUNET_CADET_Handle *my_cadet;
219 * Iterator called to free elements.
221 * @param cls the `struct AliceServiceSession *` (unused)
222 * @param key the key (unused)
223 * @param value value to free
224 * @return #GNUNET_OK (continue to iterate)
227 free_element_cb (void *cls,
228 const struct GNUNET_HashCode *key,
231 struct GNUNET_SCALARPRODUCT_Element *e = value;
239 * Destroy session state, we are done with it.
241 * @param s the session to free elements from
244 destroy_service_session (struct AliceServiceSession *s)
248 if (GNUNET_YES == s->in_destroy)
250 s->in_destroy = GNUNET_YES;
251 if (NULL != s->client_mq)
253 GNUNET_MQ_destroy (s->client_mq);
256 if (NULL != s->cadet_mq)
258 GNUNET_MQ_destroy (s->cadet_mq);
261 if (NULL != s->client)
263 GNUNET_SERVER_client_set_user_context (s->client,
265 GNUNET_SERVER_client_disconnect (s->client);
268 if (NULL != s->channel)
270 GNUNET_CADET_channel_destroy (s->channel);
273 if (NULL != s->intersected_elements)
275 GNUNET_CONTAINER_multihashmap_iterate (s->intersected_elements,
278 GNUNET_CONTAINER_multihashmap_destroy (s->intersected_elements);
279 s->intersected_elements = NULL;
281 if (NULL != s->intersection_listen)
283 GNUNET_SET_listen_cancel (s->intersection_listen);
284 s->intersection_listen = NULL;
286 if (NULL != s->intersection_op)
288 GNUNET_SET_operation_cancel (s->intersection_op);
289 s->intersection_op = NULL;
291 if (NULL != s->intersection_set)
293 GNUNET_SET_destroy (s->intersection_set);
294 s->intersection_set = NULL;
296 if (NULL != s->sorted_elements)
298 for (i=0;i<s->used_element_count;i++)
299 gcry_mpi_release (s->sorted_elements[i].value);
300 GNUNET_free (s->sorted_elements);
301 s->sorted_elements = NULL;
308 if (NULL != s->r_prime)
310 GNUNET_free (s->r_prime);
313 if (NULL != s->product)
315 gcry_mpi_release (s->product);
323 * Notify the client that the session has failed. A message gets sent
324 * to Alice's client if we encountered any error.
326 * @param session the associated client session to fail or succeed
329 prepare_client_end_notification (struct AliceServiceSession *session)
331 struct ClientResponseMessage *msg;
332 struct GNUNET_MQ_Envelope *e;
334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335 "Sending session-end notification with status %d to client for session %s\n",
337 GNUNET_h2s (&session->session_id));
338 e = GNUNET_MQ_msg (msg,
339 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT);
340 msg->product_length = htonl (0);
341 msg->status = htonl (session->status);
342 GNUNET_MQ_send (session->client_mq,
348 * Prepare the final (positive) response we will send to Alice's
351 * @param s the session associated with our client.
354 transmit_client_response (struct AliceServiceSession *s)
356 struct ClientResponseMessage *msg;
357 struct GNUNET_MQ_Envelope *e;
358 unsigned char *product_exported = NULL;
359 size_t product_length = 0;
365 if (NULL == s->product)
368 prepare_client_end_notification (s);
371 value = gcry_mpi_new (0);
372 sign = gcry_mpi_cmp_ui (s->product, 0);
383 gcry_mpi_add (value, value, s->product);
387 /* result is exactly zero */
390 gcry_mpi_release (s->product);
394 (0 != (rc = gcry_mpi_aprint (GCRYMPI_FMT_STD,
399 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR,
402 prepare_client_end_notification (s);
405 gcry_mpi_release (value);
406 e = GNUNET_MQ_msg_extra (msg,
408 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT);
409 msg->status = htonl (GNUNET_SCALARPRODUCT_STATUS_SUCCESS);
410 msg->range = htonl (range);
411 msg->product_length = htonl (product_length);
412 if (NULL != product_exported)
417 GNUNET_free (product_exported);
419 GNUNET_MQ_send (s->client_mq,
421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
422 "Sent result to client, session %s has ended!\n",
423 GNUNET_h2s (&s->session_id));
429 * Function called whenever a channel is destroyed. Should clean up
430 * any associated state.
432 * It must NOT call #GNUNET_CADET_channel_destroy() on the channel.
434 * @param cls closure (set from #GNUNET_CADET_connect())
435 * @param channel connection to the other end (henceforth invalid)
436 * @param channel_ctx place where local state associated
437 * with the channel is stored
440 cb_channel_destruction (void *cls,
441 const struct GNUNET_CADET_Channel *channel,
444 struct AliceServiceSession *s = channel_ctx;
446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
447 "Peer disconnected, terminating session %s with peer %s\n",
448 GNUNET_h2s (&s->session_id),
449 GNUNET_i2s (&s->peer));
450 if (NULL != s->cadet_mq)
452 GNUNET_MQ_destroy (s->cadet_mq);
456 if (GNUNET_SCALARPRODUCT_STATUS_ACTIVE == s->status)
458 /* We didn't get an answer yet, fail with error */
459 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
460 prepare_client_end_notification (s);
466 * Computes the square sum over a vector of a given length.
468 * @param vector the vector to compute over
469 * @param length the length of the vector
470 * @return an MPI value containing the calculated sum, never NULL
473 compute_square_sum_mpi_elements (const struct MpiElement *vector,
480 GNUNET_assert (NULL != (sum = gcry_mpi_new (0)));
481 GNUNET_assert (NULL != (elem = gcry_mpi_new (0)));
482 for (i = 0; i < length; i++)
484 gcry_mpi_mul (elem, vector[i].value, vector[i].value);
485 gcry_mpi_add (sum, sum, elem);
487 gcry_mpi_release (elem);
493 * Computes the square sum over a vector of a given length.
495 * @param vector the vector to compute over
496 * @param length the length of the vector
497 * @return an MPI value containing the calculated sum, never NULL
500 compute_square_sum (const gcry_mpi_t *vector,
507 GNUNET_assert (NULL != (sum = gcry_mpi_new (0)));
508 GNUNET_assert (NULL != (elem = gcry_mpi_new (0)));
509 for (i = 0; i < length; i++)
511 gcry_mpi_mul (elem, vector[i], vector[i]);
512 gcry_mpi_add (sum, sum, elem);
514 gcry_mpi_release (elem);
520 * Compute our scalar product, done by Alice
522 * @param session the session associated with this computation
523 * @return product as MPI, never NULL
526 compute_scalar_product (struct AliceServiceSession *session)
535 gcry_mpi_t r[session->used_element_count];
536 gcry_mpi_t r_prime[session->used_element_count];
541 count = session->used_element_count;
542 // due to the introduced static offset S, we now also have to remove this
543 // from the E(a_pi)(+)E(-b_pi-r_pi) and E(a_qi)(+)E(-r_qi) twice each,
544 // the result is E((S + a_pi) + (S -b_pi-r_pi)) and E(S + a_qi + S - r_qi)
545 for (i = 0; i < count; i++)
547 r[i] = gcry_mpi_new (0);
548 GNUNET_CRYPTO_paillier_decrypt (&my_privkey,
558 r_prime[i] = gcry_mpi_new (0);
559 GNUNET_CRYPTO_paillier_decrypt (&my_privkey,
561 &session->r_prime[i],
563 gcry_mpi_sub (r_prime[i],
566 gcry_mpi_sub (r_prime[i],
571 // calculate t = sum(ai)
572 t = compute_square_sum_mpi_elements (session->sorted_elements,
575 u = gcry_mpi_new (0);
576 tmp = compute_square_sum (r, count);
577 gcry_mpi_sub (u, u, tmp);
578 gcry_mpi_release (tmp);
581 u_prime = gcry_mpi_new (0);
582 tmp = compute_square_sum (r_prime, count);
583 gcry_mpi_sub (u_prime, u_prime, tmp);
585 GNUNET_assert (p = gcry_mpi_new (0));
586 GNUNET_assert (p_prime = gcry_mpi_new (0));
587 GNUNET_assert (s = gcry_mpi_new (0));
588 GNUNET_assert (s_prime = gcry_mpi_new (0));
591 GNUNET_CRYPTO_paillier_decrypt (&my_privkey,
595 GNUNET_CRYPTO_paillier_decrypt (&my_privkey,
601 gcry_mpi_add (p, s, t);
602 gcry_mpi_add (p, p, u);
605 gcry_mpi_add (p_prime, s_prime, t);
606 gcry_mpi_add (p_prime, p_prime, u_prime);
608 gcry_mpi_release (t);
609 gcry_mpi_release (u);
610 gcry_mpi_release (u_prime);
611 gcry_mpi_release (s);
612 gcry_mpi_release (s_prime);
615 gcry_mpi_sub (p, p, p_prime);
616 gcry_mpi_release (p_prime);
617 tmp = gcry_mpi_set_ui (tmp, 2);
618 gcry_mpi_div (p, NULL, p, tmp, 0);
620 gcry_mpi_release (tmp);
621 for (i = 0; i < count; i++)
623 gcry_mpi_release (session->sorted_elements[i].value);
624 gcry_mpi_release (r[i]);
625 gcry_mpi_release (r_prime[i]);
627 GNUNET_free (session->sorted_elements);
628 session->sorted_elements = NULL;
629 GNUNET_free (session->r);
631 GNUNET_free (session->r_prime);
632 session->r_prime = NULL;
639 * Handle a multipart chunk of a response we got from another service
640 * we wanted to calculate a scalarproduct with.
642 * @param cls closure (set from #GNUNET_CADET_connect)
643 * @param channel connection to the other end
644 * @param channel_ctx place to store local state associated with the @a channel
645 * @param message the actual message
646 * @return #GNUNET_OK to keep the connection open,
647 * #GNUNET_SYSERR to close it (signal serious error)
650 handle_bobs_cryptodata_multipart (void *cls,
651 struct GNUNET_CADET_Channel *channel,
653 const struct GNUNET_MessageHeader *message)
655 struct AliceServiceSession *s = *channel_ctx;
656 const struct BobCryptodataMultipartMessage *msg;
657 const struct GNUNET_CRYPTO_PaillierCiphertext *payload;
661 size_t required_size;
666 return GNUNET_SYSERR;
668 msg_size = ntohs (message->size);
669 if (sizeof (struct BobCryptodataMultipartMessage) > msg_size)
672 return GNUNET_SYSERR;
674 msg = (const struct BobCryptodataMultipartMessage *) message;
675 contained = ntohl (msg->contained_element_count);
676 required_size = sizeof (struct BobCryptodataMultipartMessage)
677 + 2 * contained * sizeof (struct GNUNET_CRYPTO_PaillierCiphertext);
678 if ( (required_size != msg_size) ||
679 (s->cadet_received_element_count + contained > s->used_element_count) )
682 return GNUNET_SYSERR;
685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
686 "Received %u additional crypto values from Bob\n",
687 (unsigned int) contained);
689 payload = (const struct GNUNET_CRYPTO_PaillierCiphertext *) &msg[1];
690 /* Convert each k[][perm] to its MPI_value */
691 for (i = 0; i < contained; i++)
693 memcpy (&s->r[s->cadet_received_element_count + i],
695 sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
696 memcpy (&s->r_prime[s->cadet_received_element_count + i],
698 sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
700 s->cadet_received_element_count += contained;
701 GNUNET_CADET_receive_done (s->channel);
702 if (s->cadet_received_element_count != s->used_element_count)
705 s->product = compute_scalar_product (s);
706 transmit_client_response (s);
712 * Handle a response we got from another service we wanted to
713 * calculate a scalarproduct with.
715 * @param cls closure (set from #GNUNET_CADET_connect)
716 * @param channel connection to the other end
717 * @param channel_ctx place to store local state associated with the channel
718 * @param message the actual message
719 * @return #GNUNET_OK to keep the connection open,
720 * #GNUNET_SYSERR to close it (we are done)
723 handle_bobs_cryptodata_message (void *cls,
724 struct GNUNET_CADET_Channel *channel,
726 const struct GNUNET_MessageHeader *message)
728 struct AliceServiceSession *s = *channel_ctx;
729 const struct BobCryptodataMessage *msg;
730 const struct GNUNET_CRYPTO_PaillierCiphertext *payload;
734 size_t required_size;
739 return GNUNET_SYSERR;
741 msg_size = ntohs (message->size);
742 if (sizeof (struct BobCryptodataMessage) > msg_size)
745 return GNUNET_SYSERR;
747 msg = (const struct BobCryptodataMessage *) message;
748 contained = ntohl (msg->contained_element_count);
749 required_size = sizeof (struct BobCryptodataMessage)
750 + 2 * contained * sizeof (struct GNUNET_CRYPTO_PaillierCiphertext)
751 + 2 * sizeof (struct GNUNET_CRYPTO_PaillierCiphertext);
752 if ( (msg_size != required_size) ||
753 (contained > UINT16_MAX) ||
754 (s->used_element_count < contained) )
757 return GNUNET_SYSERR;
759 if (NULL == s->sorted_elements)
761 /* we're not ready yet, how can Bob be? */
763 return GNUNET_SYSERR;
765 if (s->total != s->client_received_element_count)
767 /* we're not ready yet, how can Bob be? */
769 return GNUNET_SYSERR;
771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
772 "Received %u crypto values from Bob\n",
773 (unsigned int) contained);
775 payload = (const struct GNUNET_CRYPTO_PaillierCiphertext *) &msg[1];
778 sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
781 sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
782 payload = &payload[2];
784 s->r = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_PaillierCiphertext) * s->used_element_count);
785 s->r_prime = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_PaillierCiphertext) * s->used_element_count);
786 for (i = 0; i < contained; i++)
790 sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
791 memcpy (&s->r_prime[i],
793 sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
795 s->cadet_received_element_count = contained;
796 GNUNET_CADET_receive_done (s->channel);
798 if (s->cadet_received_element_count != s->used_element_count)
804 s->product = compute_scalar_product (s);
805 transmit_client_response (s);
811 * Iterator to copy over messages from the hash map
812 * into an array for sorting.
814 * @param cls the `struct AliceServiceSession *`
815 * @param key the key (unused)
816 * @param value the `struct GNUNET_SCALARPRODUCT_Element *`
819 copy_element_cb (void *cls,
820 const struct GNUNET_HashCode *key,
823 struct AliceServiceSession *s = cls;
824 struct GNUNET_SCALARPRODUCT_Element *e = value;
828 mval = gcry_mpi_new (0);
829 val = (int64_t) GNUNET_ntohll (e->value);
831 gcry_mpi_sub_ui (mval, mval, -val);
833 gcry_mpi_add_ui (mval, mval, val);
834 s->sorted_elements [s->used_element_count].value = mval;
835 s->sorted_elements [s->used_element_count].key = &e->key;
836 s->used_element_count++;
842 * Compare two `struct MpiValue`s by key for sorting.
844 * @param a pointer to first `struct MpiValue *`
845 * @param b pointer to first `struct MpiValue *`
846 * @return -1 for a < b, 0 for a=b, 1 for a > b.
849 element_cmp (const void *a,
852 const struct MpiElement *ma = a;
853 const struct MpiElement *mb = b;
855 return GNUNET_CRYPTO_hash_cmp (ma->key,
861 * Maximum number of elements we can put into a single cryptodata
864 #define ELEMENT_CAPACITY ((GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE - 1 - sizeof (struct AliceCryptodataMessage)) / sizeof (struct GNUNET_CRYPTO_PaillierCiphertext))
868 * Send the cryptographic data from Alice to Bob.
869 * Does nothing if we already transferred all elements.
871 * @param s the associated service session
874 send_alices_cryptodata_message (struct AliceServiceSession *s)
876 struct AliceCryptodataMessage *msg;
877 struct GNUNET_MQ_Envelope *e;
878 struct GNUNET_CRYPTO_PaillierCiphertext *payload;
885 = GNUNET_malloc (GNUNET_CONTAINER_multihashmap_size (s->intersected_elements) *
886 sizeof (struct MpiElement));
887 s->used_element_count = 0;
888 GNUNET_CONTAINER_multihashmap_iterate (s->intersected_elements,
891 LOG (GNUNET_ERROR_TYPE_DEBUG,
892 "Finished intersection, %d items remain\n",
893 s->used_element_count);
894 qsort (s->sorted_elements,
895 s->used_element_count,
896 sizeof (struct MpiElement),
899 while (off < s->used_element_count)
901 todo_count = s->used_element_count - off;
902 if (todo_count > ELEMENT_CAPACITY)
903 todo_count = ELEMENT_CAPACITY;
904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
905 "Sending %u/%u crypto values to Bob\n",
906 (unsigned int) todo_count,
907 (unsigned int) s->used_element_count);
909 e = GNUNET_MQ_msg_extra (msg,
910 todo_count * sizeof (struct GNUNET_CRYPTO_PaillierCiphertext),
911 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_CRYPTODATA);
912 msg->contained_element_count = htonl (todo_count);
913 payload = (struct GNUNET_CRYPTO_PaillierCiphertext *) &msg[1];
914 a = gcry_mpi_new (0);
915 for (i = off; i < off + todo_count; i++)
918 s->sorted_elements[i].value,
921 GNUNET_CRYPTO_paillier_encrypt (&my_pubkey,
926 gcry_mpi_release (a);
928 GNUNET_MQ_send (s->cadet_mq,
935 * Callback for set operation results. Called for each element
936 * that should be removed from the result set, and then once
937 * to indicate that the set intersection operation is done.
939 * @param cls closure with the `struct AliceServiceSession`
940 * @param element a result element, only valid if status is #GNUNET_SET_STATUS_OK
941 * @param status what has happened with the set intersection?
944 cb_intersection_element_removed (void *cls,
945 const struct GNUNET_SET_Element *element,
946 enum GNUNET_SET_Status status)
948 struct AliceServiceSession *s = cls;
949 struct GNUNET_SCALARPRODUCT_Element *se;
953 case GNUNET_SET_STATUS_OK:
954 /* this element has been removed from the set */
955 se = GNUNET_CONTAINER_multihashmap_get (s->intersected_elements,
957 GNUNET_assert (NULL != se);
958 LOG (GNUNET_ERROR_TYPE_DEBUG,
959 "Intersection removed element with key %s and value %lld\n",
960 GNUNET_h2s (&se->key),
961 (long long) GNUNET_ntohll (se->value));
962 GNUNET_assert (GNUNET_YES ==
963 GNUNET_CONTAINER_multihashmap_remove (s->intersected_elements,
968 case GNUNET_SET_STATUS_DONE:
969 s->intersection_op = NULL;
970 if (NULL != s->intersection_set)
972 GNUNET_SET_destroy (s->intersection_set);
973 s->intersection_set = NULL;
975 send_alices_cryptodata_message (s);
977 case GNUNET_SET_STATUS_HALF_DONE:
978 /* unexpected for intersection */
981 case GNUNET_SET_STATUS_FAILURE:
982 /* unhandled status code */
983 LOG (GNUNET_ERROR_TYPE_DEBUG,
984 "Set intersection failed!\n");
985 if (NULL != s->intersection_listen)
987 GNUNET_SET_listen_cancel (s->intersection_listen);
988 s->intersection_listen = NULL;
990 s->intersection_op = NULL;
991 if (NULL != s->intersection_set)
993 GNUNET_SET_destroy (s->intersection_set);
994 s->intersection_set = NULL;
996 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
997 prepare_client_end_notification (s);
1007 * Called when another peer wants to do a set operation with the
1008 * local peer. If a listen error occurs, the @a request is NULL.
1010 * @param cls closure with the `struct AliceServiceSession *`
1011 * @param other_peer the other peer
1012 * @param context_msg message with application specific information from
1014 * @param request request from the other peer (never NULL), use GNUNET_SET_accept()
1015 * to accept it, otherwise the request will be refused
1016 * Note that we can't just return value from the listen callback,
1017 * as it is also necessary to specify the set we want to do the
1018 * operation with, whith sometimes can be derived from the context
1019 * message. It's necessary to specify the timeout.
1022 cb_intersection_request_alice (void *cls,
1023 const struct GNUNET_PeerIdentity *other_peer,
1024 const struct GNUNET_MessageHeader *context_msg,
1025 struct GNUNET_SET_Request *request)
1027 struct AliceServiceSession *s = cls;
1029 if (0 != memcmp (other_peer,
1031 sizeof (struct GNUNET_PeerIdentity)))
1033 GNUNET_break_op (0);
1037 = GNUNET_SET_accept (request,
1038 GNUNET_SET_RESULT_REMOVED,
1039 &cb_intersection_element_removed,
1041 if (NULL == s->intersection_op)
1044 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
1045 prepare_client_end_notification (s);
1049 GNUNET_SET_commit (s->intersection_op,
1050 s->intersection_set))
1053 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
1054 prepare_client_end_notification (s);
1057 GNUNET_SET_destroy (s->intersection_set);
1058 s->intersection_set = NULL;
1059 GNUNET_SET_listen_cancel (s->intersection_listen);
1060 s->intersection_listen = NULL;
1065 * Our client has finished sending us its multipart message.
1067 * @param session the service session context
1070 client_request_complete_alice (struct AliceServiceSession *s)
1072 struct ServiceRequestMessage *msg;
1073 struct GNUNET_MQ_Envelope *e;
1075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1076 "Creating new channel for session with key %s.\n",
1077 GNUNET_h2s (&s->session_id));
1079 = GNUNET_CADET_channel_create (my_cadet,
1082 GNUNET_APPLICATION_TYPE_SCALARPRODUCT,
1083 GNUNET_CADET_OPTION_RELIABLE);
1084 if (NULL == s->channel)
1086 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
1087 prepare_client_end_notification (s);
1090 s->cadet_mq = GNUNET_CADET_mq_create (s->channel);
1091 s->intersection_listen
1092 = GNUNET_SET_listen (cfg,
1093 GNUNET_SET_OPERATION_INTERSECTION,
1095 &cb_intersection_request_alice,
1097 if (NULL == s->intersection_listen)
1099 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
1100 GNUNET_CADET_channel_destroy (s->channel);
1102 prepare_client_end_notification (s);
1106 e = GNUNET_MQ_msg (msg,
1107 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_SESSION_INITIALIZATION);
1108 msg->session_id = s->session_id;
1109 msg->public_key = my_pubkey;
1110 GNUNET_MQ_send (s->cadet_mq,
1116 * We're receiving additional set data. Add it to our
1117 * set and if we are done, initiate the transaction.
1119 * @param cls closure
1120 * @param client identification of the client
1121 * @param message the actual message
1124 GSS_handle_alice_client_message_multipart (void *cls,
1125 struct GNUNET_SERVER_Client *client,
1126 const struct GNUNET_MessageHeader *message)
1128 const struct ComputationBobCryptodataMultipartMessage * msg;
1129 struct AliceServiceSession *s;
1130 uint32_t contained_count;
1131 const struct GNUNET_SCALARPRODUCT_Element *elements;
1134 struct GNUNET_SET_Element set_elem;
1135 struct GNUNET_SCALARPRODUCT_Element *elem;
1137 s = GNUNET_SERVER_client_get_user_context (client,
1138 struct AliceServiceSession);
1141 /* session needs to already exist */
1143 GNUNET_SERVER_receive_done (client,
1147 msize = ntohs (message->size);
1148 if (msize < sizeof (struct ComputationBobCryptodataMultipartMessage))
1151 GNUNET_SERVER_receive_done (client,
1155 msg = (const struct ComputationBobCryptodataMultipartMessage *) message;
1156 contained_count = ntohl (msg->element_count_contained);
1158 if ( (msize != (sizeof (struct ComputationBobCryptodataMultipartMessage) +
1159 contained_count * sizeof (struct GNUNET_SCALARPRODUCT_Element))) ||
1160 (0 == contained_count) ||
1161 (s->total == s->client_received_element_count) ||
1162 (s->total < s->client_received_element_count + contained_count) )
1164 GNUNET_break_op (0);
1165 GNUNET_SERVER_receive_done (client,
1169 s->client_received_element_count += contained_count;
1170 elements = (const struct GNUNET_SCALARPRODUCT_Element *) &msg[1];
1171 for (i = 0; i < contained_count; i++)
1173 elem = GNUNET_new (struct GNUNET_SCALARPRODUCT_Element);
1176 sizeof (struct GNUNET_SCALARPRODUCT_Element));
1177 if (GNUNET_SYSERR ==
1178 GNUNET_CONTAINER_multihashmap_put (s->intersected_elements,
1181 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1187 set_elem.data = &elem->key;
1188 set_elem.size = sizeof (elem->key);
1189 set_elem.element_type = 0;
1190 GNUNET_SET_add_element (s->intersection_set,
1193 s->used_element_count++;
1195 GNUNET_SERVER_receive_done (client,
1197 if (s->total != s->client_received_element_count)
1202 client_request_complete_alice (s);
1207 * Handler for Alice's client request message.
1208 * We are doing request-initiation to compute a scalar product with a peer.
1210 * @param cls closure
1211 * @param client identification of the client
1212 * @param message the actual message
1215 GSS_handle_alice_client_message (void *cls,
1216 struct GNUNET_SERVER_Client *client,
1217 const struct GNUNET_MessageHeader *message)
1219 const struct AliceComputationMessage *msg;
1220 struct AliceServiceSession *s;
1221 uint32_t contained_count;
1222 uint32_t total_count;
1223 const struct GNUNET_SCALARPRODUCT_Element *elements;
1226 struct GNUNET_SET_Element set_elem;
1227 struct GNUNET_SCALARPRODUCT_Element *elem;
1229 s = GNUNET_SERVER_client_get_user_context (client,
1230 struct AliceServiceSession);
1233 /* only one concurrent session per client connection allowed,
1234 simplifies logic a lot... */
1236 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1239 msize = ntohs (message->size);
1240 if (msize < sizeof (struct AliceComputationMessage))
1243 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1246 msg = (const struct AliceComputationMessage *) message;
1247 total_count = ntohl (msg->element_count_total);
1248 contained_count = ntohl (msg->element_count_contained);
1249 if ( (0 == total_count) ||
1250 (0 == contained_count) ||
1251 (msize != (sizeof (struct AliceComputationMessage) +
1252 contained_count * sizeof (struct GNUNET_SCALARPRODUCT_Element))) )
1254 GNUNET_break_op (0);
1255 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1259 s = GNUNET_new (struct AliceServiceSession);
1260 s->peer = msg->peer;
1261 s->status = GNUNET_SCALARPRODUCT_STATUS_ACTIVE;
1263 s->client_mq = GNUNET_MQ_queue_for_server_client (client);
1264 s->total = total_count;
1265 s->client_received_element_count = contained_count;
1266 s->session_id = msg->session_key;
1267 elements = (const struct GNUNET_SCALARPRODUCT_Element *) &msg[1];
1268 s->intersected_elements = GNUNET_CONTAINER_multihashmap_create (s->total,
1270 s->intersection_set = GNUNET_SET_create (cfg,
1271 GNUNET_SET_OPERATION_INTERSECTION);
1272 for (i = 0; i < contained_count; i++)
1274 if (0 == GNUNET_ntohll (elements[i].value))
1276 elem = GNUNET_new (struct GNUNET_SCALARPRODUCT_Element);
1279 sizeof (struct GNUNET_SCALARPRODUCT_Element));
1280 if (GNUNET_SYSERR ==
1281 GNUNET_CONTAINER_multihashmap_put (s->intersected_elements,
1284 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1286 /* element with same key encountered twice! */
1291 set_elem.data = &elem->key;
1292 set_elem.size = sizeof (elem->key);
1293 set_elem.element_type = 0;
1294 GNUNET_SET_add_element (s->intersection_set,
1297 s->used_element_count++;
1299 GNUNET_SERVER_client_set_user_context (client,
1301 GNUNET_SERVER_receive_done (client,
1303 if (s->total != s->client_received_element_count)
1305 /* wait for multipart msg */
1308 client_request_complete_alice (s);
1313 * Task run during shutdown.
1319 shutdown_task (void *cls,
1320 const struct GNUNET_SCHEDULER_TaskContext *tc)
1322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1323 "Shutting down, initiating cleanup.\n");
1324 // FIXME: we have to cut our connections to CADET first!
1325 if (NULL != my_cadet)
1327 GNUNET_CADET_disconnect (my_cadet);
1334 * A client disconnected.
1336 * Remove the associated session(s), release data structures
1337 * and cancel pending outgoing transmissions to the client.
1339 * @param cls closure, NULL
1340 * @param client identification of the client
1343 handle_client_disconnect (void *cls,
1344 struct GNUNET_SERVER_Client *client)
1346 struct AliceServiceSession *s;
1350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1351 "Client %p disconnected from us.\n",
1353 s = GNUNET_SERVER_client_get_user_context (client,
1354 struct AliceServiceSession);
1358 GNUNET_SERVER_client_set_user_context (client,
1360 destroy_service_session (s);
1365 * Initialization of the program and message handlers
1367 * @param cls closure
1368 * @param server the initialized server
1369 * @param c configuration to use
1373 struct GNUNET_SERVER_Handle *server,
1374 const struct GNUNET_CONFIGURATION_Handle *c)
1376 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1377 { &handle_bobs_cryptodata_message,
1378 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_CRYPTODATA,
1380 { &handle_bobs_cryptodata_multipart,
1381 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_CRYPTODATA_MULTIPART,
1385 static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
1386 { &GSS_handle_alice_client_message, NULL,
1387 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE,
1389 { &GSS_handle_alice_client_message_multipart, NULL,
1390 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MUTLIPART_ALICE,
1397 offset has to be sufficiently small to allow computation of:
1398 m1+m2 mod n == (S + a) + (S + b) mod n,
1399 if we have more complex operations, this factor needs to be lowered */
1400 my_offset = gcry_mpi_new (GNUNET_CRYPTO_PAILLIER_BITS / 3);
1401 gcry_mpi_set_bit (my_offset,
1402 GNUNET_CRYPTO_PAILLIER_BITS / 3);
1404 GNUNET_CRYPTO_paillier_create (&my_pubkey,
1406 GNUNET_SERVER_add_handlers (server,
1408 GNUNET_SERVER_disconnect_notify (server,
1409 &handle_client_disconnect,
1411 my_cadet = GNUNET_CADET_connect (cfg, NULL,
1412 NULL /* no incoming supported */,
1413 &cb_channel_destruction,
1416 if (NULL == my_cadet)
1418 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1419 _("Connect to CADET failed\n"));
1420 GNUNET_SCHEDULER_shutdown ();
1423 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1431 * The main function for the scalarproduct service.
1433 * @param argc number of arguments from the command line
1434 * @param argv command line arguments
1435 * @return 0 ok, 1 on error
1441 return (GNUNET_OK ==
1442 GNUNET_SERVICE_run (argc, argv,
1443 "scalarproduct-alice",
1444 GNUNET_SERVICE_OPTION_NONE,
1445 &run, NULL)) ? 0 : 1;
1448 /* end of gnunet-service-scalarproduct_alice.c */