2 This file is part of GNUnet.
3 (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_bob.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-bob", __VA_ARGS__)
43 * An encrypted element key-value pair.
48 * Key used to identify matching pairs of values to multiply.
49 * Points into an existing data structure, to avoid copying
50 * and doubling memory use.
52 const struct GNUNET_HashCode *key;
55 * Value represented (a).
62 * An incoming session from CADET.
64 struct CadetIncomingSession;
68 * A scalarproduct session which tracks an offer for a
69 * multiplication service by a local client.
71 struct BobServiceSession
75 * (hopefully) unique transaction ID
77 struct GNUNET_HashCode session_id;
80 * The client this request is related to.
82 struct GNUNET_SERVER_Client *client;
85 * Client message queue.
87 struct GNUNET_MQ_Handle *client_mq;
90 * All non-0-value'd elements transmitted to us.
92 struct GNUNET_CONTAINER_MultiHashMap *intersected_elements;
95 * Set of elements for which we will be conducting an intersection.
96 * The resulting elements are then used for computing the scalar product.
98 struct GNUNET_SET_Handle *intersection_set;
101 * Set of elements for which will conduction an intersection.
102 * the resulting elements are then used for computing the scalar product.
104 struct GNUNET_SET_OperationHandle *intersection_op;
109 struct MpiElement *sorted_elements;
112 * E(ai)(Bob) after applying the mask
114 struct GNUNET_CRYPTO_PaillierCiphertext *e_a;
117 * Bob's permutation p of R
119 struct GNUNET_CRYPTO_PaillierCiphertext *r;
122 * Bob's permutation q of R
124 struct GNUNET_CRYPTO_PaillierCiphertext *r_prime;
129 struct GNUNET_CRYPTO_PaillierCiphertext s;
134 struct GNUNET_CRYPTO_PaillierCiphertext s_prime;
137 * Handle for our associated incoming CADET session, or NULL
138 * if we have not gotten one yet.
140 struct CadetIncomingSession *cadet;
143 * The computed scalar
148 * How many elements will be supplied in total from the client.
153 * Already transferred elements (received) for multipart
154 * messages from client. Always less than @e total.
156 uint32_t client_received_element_count;
159 * How many elements actually are used for the scalar product.
160 * Size of the arrays in @e r and @e r_prime. Also sometimes
161 * used as an index into the arrays during construction.
163 uint32_t used_element_count;
166 * Counts the number of values received from Alice by us.
167 * Always less than @e used_element_count.
169 uint32_t cadet_received_element_count;
172 * Counts the number of values transmitted from us to Alice.
173 * Always less than @e used_element_count.
175 uint32_t cadet_transmitted_element_count;
178 * State of this session. In
179 * #GNUNET_SCALARPRODUCT_STATUS_ACTIVE while operation is
180 * ongoing, afterwards in #GNUNET_SCALARPRODUCT_STATUS_SUCCESS or
181 * #GNUNET_SCALARPRODUCT_STATUS_FAILURE.
183 enum GNUNET_SCALARPRODUCT_ResponseStatus status;
186 * Are we already in #destroy_service_session()?
194 * An incoming session from CADET.
196 struct CadetIncomingSession
200 * Associated client session, or NULL.
202 struct BobServiceSession *s;
207 struct GNUNET_CADET_Channel *channel;
210 * Originator's peer identity. (Only for diagnostics.)
212 struct GNUNET_PeerIdentity peer;
215 * (hopefully) unique transaction ID
217 struct GNUNET_HashCode session_id;
220 * Public key of the remote service.
222 struct GNUNET_CRYPTO_PaillierPublicKey remote_pubkey;
225 * The message queue for this channel.
227 struct GNUNET_MQ_Handle *cadet_mq;
230 * Has this CADET session been added to the map yet?
231 * #GNUNET_YES if so, in which case @e session_id is
237 * Are we already in #destroy_cadet_session()?
245 * GNUnet configuration handle
247 static const struct GNUNET_CONFIGURATION_Handle *cfg;
250 * Service's own public key
252 static struct GNUNET_CRYPTO_PaillierPublicKey my_pubkey;
255 * Service's own private key
257 static struct GNUNET_CRYPTO_PaillierPrivateKey my_privkey;
260 * Service's offset for values that could possibly be negative but are plaintext for encryption.
262 static gcry_mpi_t my_offset;
265 * Map of `struct BobServiceSession`, by session keys.
267 static struct GNUNET_CONTAINER_MultiHashMap *client_sessions;
270 * Map of `struct CadetIncomingSession`, by session keys.
272 static struct GNUNET_CONTAINER_MultiHashMap *cadet_sessions;
275 * Handle to the CADET service.
277 static struct GNUNET_CADET_Handle *my_cadet;
282 * Finds a not terminated client session in the respective map based on
285 * @param key the session key we want to search for
286 * @return the matching session, or NULL for none
288 static struct BobServiceSession *
289 find_matching_client_session (const struct GNUNET_HashCode *key)
291 return GNUNET_CONTAINER_multihashmap_get (client_sessions,
297 * Finds a CADET session in the respective map based on session key.
299 * @param key the session key we want to search for
300 * @return the matching session, or NULL for none
302 static struct CadetIncomingSession *
303 find_matching_cadet_session (const struct GNUNET_HashCode *key)
305 return GNUNET_CONTAINER_multihashmap_get (cadet_sessions,
311 * Callback used to free the elements in the map.
314 * @param key key of the element
315 * @param value the value to free
318 free_element_cb (void *cls,
319 const struct GNUNET_HashCode *key,
322 struct GNUNET_SCALARPRODUCT_Element *element = value;
324 GNUNET_free (element);
330 * Destroy session state, we are done with it.
332 * @param session the session to free elements from
335 destroy_cadet_session (struct CadetIncomingSession *s);
339 * Destroy session state, we are done with it.
341 * @param session the session to free elements from
344 destroy_service_session (struct BobServiceSession *s)
346 struct CadetIncomingSession *in;
349 if (GNUNET_YES == s->in_destroy)
351 s->in_destroy = GNUNET_YES;
352 if (NULL != (in = s->cadet))
355 destroy_cadet_session (in);
357 if (NULL != s->client_mq)
359 GNUNET_MQ_destroy (s->client_mq);
362 if (NULL != s->client)
364 GNUNET_SERVER_client_disconnect (s->client);
367 GNUNET_assert (GNUNET_YES ==
368 GNUNET_CONTAINER_multihashmap_remove (client_sessions,
371 if (NULL != s->intersected_elements)
373 GNUNET_CONTAINER_multihashmap_iterate (s->intersected_elements,
376 GNUNET_CONTAINER_multihashmap_destroy (s->intersected_elements);
377 s->intersected_elements = NULL;
379 if (NULL != s->intersection_op)
381 GNUNET_SET_operation_cancel (s->intersection_op);
382 s->intersection_op = NULL;
384 if (NULL != s->intersection_set)
386 GNUNET_SET_destroy (s->intersection_set);
387 s->intersection_set = NULL;
391 GNUNET_free (s->e_a);
394 if (NULL != s->sorted_elements)
396 for (i=0;i<s->used_element_count;i++)
397 gcry_mpi_release (s->sorted_elements[i].value);
398 GNUNET_free (s->sorted_elements);
399 s->sorted_elements = NULL;
406 if (NULL != s->r_prime)
408 GNUNET_free (s->r_prime);
411 if (NULL != s->product)
413 gcry_mpi_release (s->product);
421 * Destroy incoming CADET session state, we are done with it.
423 * @param in the session to free elements from
426 destroy_cadet_session (struct CadetIncomingSession *in)
428 struct BobServiceSession *s;
430 if (GNUNET_YES == in->in_destroy)
432 in->in_destroy = GNUNET_YES;
433 if (NULL != (s = in->s))
436 destroy_service_session (s);
438 if (GNUNET_YES == in->in_map)
440 GNUNET_assert (GNUNET_YES ==
441 GNUNET_CONTAINER_multihashmap_remove (cadet_sessions,
444 in->in_map = GNUNET_NO;
446 if (NULL != in->cadet_mq)
448 GNUNET_MQ_destroy (in->cadet_mq);
451 if (NULL != in->channel)
453 GNUNET_CADET_channel_destroy (in->channel);
461 * Notify the client that the session has succeeded or failed. This
462 * message gets sent to Bob's client if the operation completed or
463 * Alice disconnected.
465 * @param session the associated client session to fail or succeed
468 prepare_client_end_notification (struct BobServiceSession *session)
470 struct ClientResponseMessage *msg;
471 struct GNUNET_MQ_Envelope *e;
473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
474 "Sending session-end notification with status %d to client for session %s\n",
476 GNUNET_h2s (&session->session_id));
477 e = GNUNET_MQ_msg (msg,
478 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT);
480 msg->product_length = htonl (0);
481 msg->status = htonl (session->status);
482 GNUNET_MQ_send (session->client_mq,
488 * Function called whenever a channel is destroyed. Should clean up
489 * any associated state.
491 * It must NOT call #GNUNET_CADET_channel_destroy() on the channel.
493 * @param cls closure (set from #GNUNET_CADET_connect())
494 * @param channel connection to the other end (henceforth invalid)
495 * @param channel_ctx place where local state associated
496 * with the channel is stored
499 cb_channel_destruction (void *cls,
500 const struct GNUNET_CADET_Channel *channel,
503 struct CadetIncomingSession *in = channel_ctx;
504 struct BobServiceSession *s;
506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
507 "Peer disconnected, terminating session %s with peer %s\n",
508 GNUNET_h2s (&in->session_id),
509 GNUNET_i2s (&in->peer));
510 if (NULL != in->cadet_mq)
512 GNUNET_MQ_destroy (in->cadet_mq);
516 if (NULL != (s = in->s))
518 if (GNUNET_SCALARPRODUCT_STATUS_ACTIVE == s->status)
520 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
521 prepare_client_end_notification (s);
524 destroy_cadet_session (in);
529 * MQ finished giving our last message to CADET, now notify
530 * the client that we are finished.
533 bob_cadet_done_cb (void *cls)
535 struct BobServiceSession *session = cls;
537 session->status = GNUNET_SCALARPRODUCT_STATUS_SUCCESS;
538 prepare_client_end_notification (session);
543 * Maximum count of elements we can put into a multipart message
545 #define ELEMENT_CAPACITY ((GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE - 1 - sizeof (struct BobCryptodataMultipartMessage)) / sizeof (struct GNUNET_CRYPTO_PaillierCiphertext))
549 * Send a multipart chunk of a service response from Bob to Alice.
550 * This element only contains the two permutations of R, R'.
552 * @param s the associated service session
555 transmit_bobs_cryptodata_message_multipart (struct BobServiceSession *s)
557 struct GNUNET_CRYPTO_PaillierCiphertext *payload;
558 struct BobCryptodataMultipartMessage *msg;
559 struct GNUNET_MQ_Envelope *e;
564 while (s->cadet_transmitted_element_count != s->used_element_count)
566 todo_count = s->used_element_count - s->cadet_transmitted_element_count;
567 if (todo_count > ELEMENT_CAPACITY / 2)
568 todo_count = ELEMENT_CAPACITY / 2;
570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
571 "Sending %u additional crypto values to Alice\n",
572 (unsigned int) todo_count);
573 e = GNUNET_MQ_msg_extra (msg,
574 todo_count * sizeof (struct GNUNET_CRYPTO_PaillierCiphertext) * 2,
575 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_CRYPTODATA_MULTIPART);
576 msg->contained_element_count = htonl (todo_count);
577 payload = (struct GNUNET_CRYPTO_PaillierCiphertext *) &msg[1];
578 for (i = s->cadet_transmitted_element_count, j = 0; i < s->cadet_transmitted_element_count + todo_count; i++)
580 //r[i][p] and r[i][q]
581 memcpy (&payload[j++],
583 sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
584 memcpy (&payload[j++],
586 sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
588 s->cadet_transmitted_element_count += todo_count;
589 if (s->cadet_transmitted_element_count == s->used_element_count)
590 GNUNET_MQ_notify_sent (e,
593 GNUNET_MQ_send (s->cadet->cadet_mq,
596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
597 "All values queued for Alice, Bob is done\n");
602 * Bob generates the response message to be sent to Alice after
603 * computing the values (1), (2), S and S'.
605 * (1)[]: $E_A(a_{pi(i)}) times E_A(- r_{pi(i)} - b_{pi(i)}) &= E_A(a_{pi(i)} - r_{pi(i)} - b_{pi(i)})$
606 * (2)[]: $E_A(a_{pi'(i)}) times E_A(- r_{pi'(i)}) &= E_A(a_{pi'(i)} - r_{pi'(i)})$
607 * S: $S := E_A(sum (r_i + b_i)^2)$
608 * S': $S' := E_A(sum r_i^2)$
610 * @param s the associated requesting session with Alice
613 transmit_bobs_cryptodata_message (struct BobServiceSession *s)
615 struct BobCryptodataMessage *msg;
616 struct GNUNET_MQ_Envelope *e;
617 struct GNUNET_CRYPTO_PaillierCiphertext *payload;
620 s->cadet_transmitted_element_count
621 = ((GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE - 1 - sizeof (struct BobCryptodataMessage))
622 / sizeof (struct GNUNET_CRYPTO_PaillierCiphertext) / 2) - 1;
623 if (s->cadet_transmitted_element_count > s->used_element_count)
624 s->cadet_transmitted_element_count = s->used_element_count;
626 e = GNUNET_MQ_msg_extra (msg,
627 (2 + s->cadet_transmitted_element_count * 2)
628 * sizeof (struct GNUNET_CRYPTO_PaillierCiphertext),
629 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_CRYPTODATA);
630 msg->contained_element_count = htonl (s->cadet_transmitted_element_count);
632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
633 "Sending %u/%u crypto values to Alice\n",
634 (unsigned int) s->cadet_transmitted_element_count,
635 (unsigned int) s->used_element_count);
637 payload = (struct GNUNET_CRYPTO_PaillierCiphertext *) &msg[1];
640 sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
643 sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
645 payload = &payload[2];
647 for (i = 0; i < s->cadet_transmitted_element_count; i++)
649 //k[i][p] and k[i][q]
650 memcpy (&payload[i * 2],
652 sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
653 memcpy (&payload[i * 2 + 1],
655 sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
657 if (s->cadet_transmitted_element_count == s->used_element_count)
658 GNUNET_MQ_notify_sent (e,
661 GNUNET_MQ_send (s->cadet->cadet_mq,
663 transmit_bobs_cryptodata_message_multipart (s);
665 #undef ELEMENT_CAPACITY
669 * Computes the square sum over a vector of a given length.
671 * @param vector the vector to compute over
672 * @param length the length of the vector
673 * @return an MPI value containing the calculated sum, never NULL
674 * TODO: code duplication with Alice!
677 compute_square_sum (const gcry_mpi_t *vector,
684 GNUNET_assert (NULL != (sum = gcry_mpi_new (0)));
685 GNUNET_assert (NULL != (elem = gcry_mpi_new (0)));
686 for (i = 0; i < length; i++)
688 gcry_mpi_mul (elem, vector[i], vector[i]);
689 gcry_mpi_add (sum, sum, elem);
691 gcry_mpi_release (elem);
698 * (1)[]: $E_A(a_{pi(i)}) otimes E_A(- r_{pi(i)} - b_{pi(i)}) &= E_A(a_{pi(i)} - r_{pi(i)} - b_{pi(i)})$
699 * (2)[]: $E_A(a_{pi'(i)}) otimes E_A(- r_{pi'(i)}) &= E_A(a_{pi'(i)} - r_{pi'(i)})$
700 * S: $S := E_A(sum (r_i + b_i)^2)$
701 * S': $S' := E_A(sum r_i^2)$
703 * @param request the requesting session + bob's requesting peer
704 * @return #GNUNET_OK on success
707 compute_service_response (struct BobServiceSession *session)
715 const struct MpiElement *b;
716 struct GNUNET_CRYPTO_PaillierCiphertext *a;
717 struct GNUNET_CRYPTO_PaillierCiphertext *r;
718 struct GNUNET_CRYPTO_PaillierCiphertext *r_prime;
720 count = session->used_element_count;
722 b = session->sorted_elements;
723 q = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
725 p = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
727 rand = GNUNET_malloc (sizeof (gcry_mpi_t) * count);
728 for (i = 0; i < count; i++)
729 GNUNET_assert (NULL != (rand[i] = gcry_mpi_new (0)));
730 r = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_PaillierCiphertext) * count);
731 r_prime = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_PaillierCiphertext) * count);
733 for (i = 0; i < count; i++)
737 svalue = (int32_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
739 // long to gcry_mpi_t
741 gcry_mpi_sub_ui (rand[i],
745 rand[i] = gcry_mpi_set_ui (rand[i], svalue);
748 tmp = gcry_mpi_new (0);
749 // encrypt the element
750 // for the sake of readability I decided to have dedicated permutation
751 // vectors, which get rid of all the lookups in p/q.
752 // however, ap/aq are not absolutely necessary but are just abstraction
753 // Calculate Kp = E(S + a_pi) (+) E(S - r_pi - b_pi)
754 for (i = 0; i < count; i++)
756 // E(S - r_pi - b_pi)
757 gcry_mpi_sub (tmp, my_offset, rand[p[i]]);
758 gcry_mpi_sub (tmp, tmp, b[p[i]].value);
760 GNUNET_CRYPTO_paillier_encrypt (&session->cadet->remote_pubkey,
765 // E(S - r_pi - b_pi) * E(S + a_pi) == E(2*S + a - r - b)
767 GNUNET_CRYPTO_paillier_hom_add (&session->cadet->remote_pubkey,
777 // Calculate Kq = E(S + a_qi) (+) E(S - r_qi)
778 for (i = 0; i < count; i++)
781 gcry_mpi_sub (tmp, my_offset, rand[q[i]]);
783 GNUNET_CRYPTO_paillier_encrypt (&session->cadet->remote_pubkey,
788 // E(S - r_qi) * E(S + a_qi) == E(2*S + a_qi - r_qi)
790 GNUNET_CRYPTO_paillier_hom_add (&session->cadet->remote_pubkey,
799 gcry_mpi_release (tmp);
801 // Calculate S' = E(SUM( r_i^2 ))
802 tmp = compute_square_sum (rand, count);
804 GNUNET_CRYPTO_paillier_encrypt (&session->cadet->remote_pubkey,
808 gcry_mpi_release (tmp);
810 // Calculate S = E(SUM( (r_i + b_i)^2 ))
811 for (i = 0; i < count; i++)
812 gcry_mpi_add (rand[i], rand[i], b[i].value);
813 tmp = compute_square_sum (rand, count);
815 GNUNET_CRYPTO_paillier_encrypt (&session->cadet->remote_pubkey,
819 gcry_mpi_release (tmp);
822 session->r_prime = r_prime;
824 for (i = 0; i < count; i++)
825 gcry_mpi_release (rand[i]);
826 GNUNET_free (session->e_a);
835 GNUNET_free (r_prime);
836 gcry_mpi_release (tmp);
839 for (i = 0; i < count; i++)
840 gcry_mpi_release (rand[i]);
842 return GNUNET_SYSERR;
847 * Iterator to copy over messages from the hash map
848 * into an array for sorting.
850 * @param cls the `struct BobServiceSession *`
851 * @param key the key (unused)
852 * @param value the `struct GNUNET_SCALARPRODUCT_Element *`
853 * TODO: code duplication with Alice!
856 copy_element_cb (void *cls,
857 const struct GNUNET_HashCode *key,
860 struct BobServiceSession *s = cls;
861 struct GNUNET_SCALARPRODUCT_Element *e = value;
865 mval = gcry_mpi_new (0);
866 val = (int64_t) GNUNET_ntohll (e->value);
868 gcry_mpi_sub_ui (mval, mval, -val);
870 gcry_mpi_add_ui (mval, mval, val);
871 s->sorted_elements [s->used_element_count].value = mval;
872 s->sorted_elements [s->used_element_count].key = &e->key;
873 s->used_element_count++;
879 * Compare two `struct MpiValue`s by key for sorting.
881 * @param a pointer to first `struct MpiValue *`
882 * @param b pointer to first `struct MpiValue *`
883 * @return -1 for a < b, 0 for a=b, 1 for a > b.
884 * TODO: code duplication with Alice!
887 element_cmp (const void *a,
890 const struct MpiElement *ma = a;
891 const struct MpiElement *mb = b;
893 return GNUNET_CRYPTO_hash_cmp (ma->key,
899 * Intersection operation and receiving data via CADET from
900 * Alice are both done, compute and transmit our reply via
903 * @param s session to transmit reply for.
906 transmit_cryptographic_reply (struct BobServiceSession *s)
908 struct GNUNET_CADET_Channel *channel;
910 /* TODO: code duplication with Alice! */
911 LOG (GNUNET_ERROR_TYPE_DEBUG,
912 "Received everything, building reply for Alice\n");
914 = GNUNET_malloc (GNUNET_CONTAINER_multihashmap_size (s->intersected_elements) *
915 sizeof (struct MpiElement));
916 s->used_element_count = 0;
917 GNUNET_CONTAINER_multihashmap_iterate (s->intersected_elements,
920 qsort (s->sorted_elements,
921 s->used_element_count,
922 sizeof (struct MpiElement),
925 compute_service_response (s))
927 channel = s->cadet->channel;
928 s->cadet->channel = NULL;
929 GNUNET_CADET_channel_destroy (channel);
932 transmit_bobs_cryptodata_message (s);
937 * Handle a multipart-chunk of a request from another service to
938 * calculate a scalarproduct with us.
940 * @param cls closure (set from #GNUNET_CADET_connect)
941 * @param channel connection to the other end
942 * @param channel_ctx place to store local state associated with the @a channel
943 * @param message the actual message
944 * @return #GNUNET_OK to keep the connection open,
945 * #GNUNET_SYSERR to close it (signal serious error)
948 handle_alices_cryptodata_message (void *cls,
949 struct GNUNET_CADET_Channel *channel,
951 const struct GNUNET_MessageHeader *message)
953 struct CadetIncomingSession *in = *channel_ctx;
954 struct BobServiceSession *s;
955 const struct AliceCryptodataMessage *msg;
956 const struct GNUNET_CRYPTO_PaillierCiphertext *payload;
957 uint32_t contained_elements;
965 return GNUNET_SYSERR;
971 return GNUNET_SYSERR;
973 msize = ntohs (message->size);
974 if (msize <= sizeof (struct AliceCryptodataMessage))
977 return GNUNET_SYSERR;
979 msg = (const struct AliceCryptodataMessage *) message;
980 contained_elements = ntohl (msg->contained_element_count);
981 /* Our intersection may still be ongoing, but this is nevertheless
982 an upper bound on the required array size */
983 max = GNUNET_CONTAINER_multihashmap_size (s->intersected_elements);
984 msg_length = sizeof (struct AliceCryptodataMessage)
985 + contained_elements * sizeof (struct GNUNET_CRYPTO_PaillierCiphertext);
986 if ( (msize != msg_length) ||
987 (0 == contained_elements) ||
988 (contained_elements > UINT16_MAX) ||
989 (max < contained_elements + s->cadet_received_element_count) )
992 return GNUNET_SYSERR;
994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
995 "Received %u crypto values from Alice\n",
996 (unsigned int) contained_elements);
998 payload = (const struct GNUNET_CRYPTO_PaillierCiphertext *) &msg[1];
1000 s->e_a = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_PaillierCiphertext) *
1002 memcpy (&s->e_a[s->cadet_received_element_count],
1004 sizeof (struct GNUNET_CRYPTO_PaillierCiphertext) * contained_elements);
1005 s->cadet_received_element_count += contained_elements;
1007 if ( (s->cadet_received_element_count == max) &&
1008 (NULL == s->intersection_op) )
1010 /* intersection has finished also on our side, and
1011 we got the full set, so we can proceed with the
1012 CADET response(s) */
1013 transmit_cryptographic_reply (s);
1015 GNUNET_CADET_receive_done (s->cadet->channel);
1021 * Callback for set operation results. Called for each element
1022 * that needs to be removed from the result set.
1024 * @param cls closure with the `struct BobServiceSession`
1025 * @param element a result element, only valid if status is #GNUNET_SET_STATUS_OK
1026 * @param status what has happened with the set intersection?
1029 cb_intersection_element_removed (void *cls,
1030 const struct GNUNET_SET_Element *element,
1031 enum GNUNET_SET_Status status)
1033 struct BobServiceSession *s = cls;
1034 struct GNUNET_SCALARPRODUCT_Element *se;
1038 case GNUNET_SET_STATUS_OK:
1039 /* this element has been removed from the set */
1040 se = GNUNET_CONTAINER_multihashmap_get (s->intersected_elements,
1042 GNUNET_assert (NULL != se);
1043 LOG (GNUNET_ERROR_TYPE_DEBUG,
1044 "Removed element with key %s and value %lld\n",
1045 GNUNET_h2s (&se->key),
1046 (long long) GNUNET_ntohll (se->value));
1047 GNUNET_assert (GNUNET_YES ==
1048 GNUNET_CONTAINER_multihashmap_remove (s->intersected_elements,
1053 case GNUNET_SET_STATUS_DONE:
1054 s->intersection_op = NULL;
1055 GNUNET_break (NULL == s->intersection_set);
1056 GNUNET_CADET_receive_done (s->cadet->channel);
1057 LOG (GNUNET_ERROR_TYPE_DEBUG,
1058 "Finished intersection, %d items remain\n",
1059 GNUNET_CONTAINER_multihashmap_size (s->intersected_elements));
1060 if (s->client_received_element_count ==
1061 GNUNET_CONTAINER_multihashmap_size (s->intersected_elements))
1063 /* CADET transmission from Alice is also already done,
1064 start with our own reply */
1065 transmit_cryptographic_reply (s);
1068 case GNUNET_SET_STATUS_HALF_DONE:
1069 /* unexpected for intersection */
1072 case GNUNET_SET_STATUS_FAILURE:
1073 /* unhandled status code */
1074 LOG (GNUNET_ERROR_TYPE_DEBUG,
1075 "Set intersection failed!\n");
1076 s->intersection_op = NULL;
1077 if (NULL != s->intersection_set)
1079 GNUNET_SET_destroy (s->intersection_set);
1080 s->intersection_set = NULL;
1082 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
1083 prepare_client_end_notification (s);
1093 * We've paired up a client session with an incoming CADET request.
1094 * Initiate set intersection work.
1096 * @param s client session to start intersection for
1099 start_intersection (struct BobServiceSession *s)
1101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1102 "Got session with key %s and %u elements, starting intersection.\n",
1103 GNUNET_h2s (&s->session_id),
1104 (unsigned int) s->total);
1107 = GNUNET_SET_prepare (&s->cadet->peer,
1110 GNUNET_SET_RESULT_REMOVED,
1111 &cb_intersection_element_removed,
1114 GNUNET_SET_commit (s->intersection_op,
1115 s->intersection_set))
1118 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
1119 prepare_client_end_notification (s);
1122 GNUNET_SET_destroy (s->intersection_set);
1123 s->intersection_set = NULL;
1128 * Handle a request from Alice to calculate a scalarproduct with us (Bob).
1130 * @param cls closure (set from #GNUNET_CADET_connect)
1131 * @param channel connection to the other end
1132 * @param channel_ctx place to store the `struct CadetIncomingSession *`
1133 * @param message the actual message
1134 * @return #GNUNET_OK to keep the connection open,
1135 * #GNUNET_SYSERR to close it (signal serious error)
1138 handle_alices_computation_request (void *cls,
1139 struct GNUNET_CADET_Channel *channel,
1141 const struct GNUNET_MessageHeader *message)
1143 struct CadetIncomingSession *in = *channel_ctx;
1144 struct BobServiceSession *s;
1145 const struct ServiceRequestMessage *msg;
1147 if (ntohs (message->size) != sizeof (struct ServiceRequestMessage))
1149 GNUNET_break_op (0);
1150 return GNUNET_SYSERR;
1152 msg = (const struct ServiceRequestMessage *) message;
1153 if (GNUNET_YES == in->in_map)
1155 GNUNET_break_op (0);
1156 return GNUNET_SYSERR;
1158 if (NULL != find_matching_cadet_session (&msg->session_id))
1160 /* not unique, got one like this already */
1161 GNUNET_break_op (0);
1162 return GNUNET_SYSERR;
1164 in->session_id = msg->session_id;
1165 in->remote_pubkey = msg->public_key;
1166 GNUNET_assert (GNUNET_YES ==
1167 GNUNET_CONTAINER_multihashmap_put (cadet_sessions,
1170 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1171 s = find_matching_client_session (&in->session_id);
1174 /* no client waiting for this request, wait for client */
1177 GNUNET_assert (NULL == s->cadet);
1181 if (s->client_received_element_count == s->total)
1182 start_intersection (s);
1188 * Function called for inbound channels on Bob's end. Does some
1189 * preliminary initialization, more happens after we get Alice's first
1192 * @param cls closure
1193 * @param channel new handle to the channel
1194 * @param initiator peer that started the channel
1195 * @param port unused
1196 * @param options unused
1197 * @return session associated with the channel
1200 cb_channel_incoming (void *cls,
1201 struct GNUNET_CADET_Channel *channel,
1202 const struct GNUNET_PeerIdentity *initiator,
1204 enum GNUNET_CADET_ChannelOption options)
1206 struct CadetIncomingSession *in;
1208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1209 "New incoming channel from peer %s.\n",
1210 GNUNET_i2s (initiator));
1211 in = GNUNET_new (struct CadetIncomingSession);
1212 in->peer = *initiator;
1213 in->channel = channel;
1214 in->cadet_mq = GNUNET_CADET_mq_create (in->channel);
1220 * We're receiving additional set data. Add it to our
1221 * set and if we are done, initiate the transaction.
1223 * @param cls closure
1224 * @param client identification of the client
1225 * @param message the actual message
1228 GSS_handle_bob_client_message_multipart (void *cls,
1229 struct GNUNET_SERVER_Client *client,
1230 const struct GNUNET_MessageHeader *message)
1232 const struct ComputationBobCryptodataMultipartMessage * msg;
1233 struct BobServiceSession *s;
1234 uint32_t contained_count;
1235 const struct GNUNET_SCALARPRODUCT_Element *elements;
1238 struct GNUNET_SET_Element set_elem;
1239 struct GNUNET_SCALARPRODUCT_Element *elem;
1241 s = GNUNET_SERVER_client_get_user_context (client,
1242 struct BobServiceSession);
1245 /* session needs to already exist */
1247 GNUNET_SERVER_receive_done (client,
1251 msize = ntohs (message->size);
1252 if (msize < sizeof (struct ComputationBobCryptodataMultipartMessage))
1255 GNUNET_SERVER_receive_done (client,
1259 msg = (const struct ComputationBobCryptodataMultipartMessage *) message;
1260 contained_count = ntohl (msg->element_count_contained);
1262 if ( (msize != (sizeof (struct ComputationBobCryptodataMultipartMessage) +
1263 contained_count * sizeof (struct GNUNET_SCALARPRODUCT_Element))) ||
1264 (0 == contained_count) ||
1265 (UINT16_MAX < contained_count) ||
1266 (s->total == s->client_received_element_count) ||
1267 (s->total < s->client_received_element_count + contained_count) )
1269 GNUNET_break_op (0);
1270 GNUNET_SERVER_receive_done (client,
1274 elements = (const struct GNUNET_SCALARPRODUCT_Element *) &msg[1];
1275 for (i = 0; i < contained_count; i++)
1277 elem = GNUNET_new (struct GNUNET_SCALARPRODUCT_Element);
1280 sizeof (struct GNUNET_SCALARPRODUCT_Element));
1281 if (GNUNET_SYSERR ==
1282 GNUNET_CONTAINER_multihashmap_put (s->intersected_elements,
1285 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
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,
1298 s->client_received_element_count += contained_count;
1299 GNUNET_SERVER_receive_done (client,
1301 if (s->total != s->client_received_element_count)
1306 if (NULL == s->cadet)
1308 /* no Alice waiting for this request, wait for Alice */
1311 start_intersection (s);
1316 * Handler for Bob's a client request message. Bob is in the response
1317 * role, keep the values + session and waiting for a matching session
1318 * or process a waiting request from Alice.
1320 * @param cls closure
1321 * @param client identification of the client
1322 * @param message the actual message
1325 GSS_handle_bob_client_message (void *cls,
1326 struct GNUNET_SERVER_Client *client,
1327 const struct GNUNET_MessageHeader *message)
1329 const struct BobComputationMessage *msg;
1330 struct BobServiceSession *s;
1331 struct CadetIncomingSession *in;
1332 uint32_t contained_count;
1333 uint32_t total_count;
1334 const struct GNUNET_SCALARPRODUCT_Element *elements;
1336 struct GNUNET_SET_Element set_elem;
1337 struct GNUNET_SCALARPRODUCT_Element *elem;
1340 s = GNUNET_SERVER_client_get_user_context (client,
1341 struct BobServiceSession);
1344 /* only one concurrent session per client connection allowed,
1345 simplifies logic a lot... */
1347 GNUNET_SERVER_receive_done (client,
1351 msize = ntohs (message->size);
1352 if (msize < sizeof (struct BobComputationMessage))
1355 GNUNET_SERVER_receive_done (client,
1359 msg = (const struct BobComputationMessage *) message;
1360 total_count = ntohl (msg->element_count_total);
1361 contained_count = ntohl (msg->element_count_contained);
1362 if ( (0 == total_count) ||
1363 (0 == contained_count) ||
1364 (UINT16_MAX < contained_count) ||
1365 (msize != (sizeof (struct BobComputationMessage) +
1366 contained_count * sizeof (struct GNUNET_SCALARPRODUCT_Element))) )
1368 GNUNET_break_op (0);
1369 GNUNET_SERVER_receive_done (client,
1373 if (NULL != find_matching_client_session (&msg->session_key))
1376 GNUNET_SERVER_receive_done (client,
1381 s = GNUNET_new (struct BobServiceSession);
1382 s->status = GNUNET_SCALARPRODUCT_STATUS_ACTIVE;
1384 s->client_mq = GNUNET_MQ_queue_for_server_client (client);
1385 s->total = total_count;
1386 s->client_received_element_count = contained_count;
1387 s->session_id = msg->session_key;
1388 GNUNET_break (GNUNET_YES ==
1389 GNUNET_CONTAINER_multihashmap_put (client_sessions,
1392 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1393 elements = (const struct GNUNET_SCALARPRODUCT_Element *) &msg[1];
1394 s->intersected_elements = GNUNET_CONTAINER_multihashmap_create (s->total,
1396 s->intersection_set = GNUNET_SET_create (cfg,
1397 GNUNET_SET_OPERATION_INTERSECTION);
1398 for (i = 0; i < contained_count; i++)
1400 if (0 == GNUNET_ntohll (elements[i].value))
1402 elem = GNUNET_new (struct GNUNET_SCALARPRODUCT_Element);
1405 sizeof (struct GNUNET_SCALARPRODUCT_Element));
1406 if (GNUNET_SYSERR ==
1407 GNUNET_CONTAINER_multihashmap_put (s->intersected_elements,
1410 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1416 set_elem.data = &elem->key;
1417 set_elem.size = sizeof (elem->key);
1418 set_elem.element_type = 0;
1419 GNUNET_SET_add_element (s->intersection_set,
1422 s->used_element_count++;
1424 GNUNET_SERVER_client_set_user_context (client,
1426 GNUNET_SERVER_receive_done (client,
1428 if (s->total != s->client_received_element_count)
1433 in = find_matching_cadet_session (&s->session_id);
1436 /* nothing yet, wait for Alice */
1439 GNUNET_assert (NULL == in->s);
1443 start_intersection (s);
1448 * Task run during shutdown.
1454 shutdown_task (void *cls,
1455 const struct GNUNET_SCHEDULER_TaskContext *tc)
1457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1458 "Shutting down, initiating cleanup.\n");
1459 // FIXME: we have to cut our connections to CADET first!
1460 if (NULL != my_cadet)
1462 GNUNET_CADET_disconnect (my_cadet);
1465 GNUNET_CONTAINER_multihashmap_destroy (client_sessions);
1466 client_sessions = NULL;
1467 GNUNET_CONTAINER_multihashmap_destroy (cadet_sessions);
1468 cadet_sessions = NULL;
1473 * A client disconnected.
1475 * Remove the associated session(s), release data structures
1476 * and cancel pending outgoing transmissions to the client.
1478 * @param cls closure, NULL
1479 * @param client identification of the client
1482 handle_client_disconnect (void *cls,
1483 struct GNUNET_SERVER_Client *client)
1485 struct BobServiceSession *s;
1489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1490 "Client disconnected from us.\n",
1492 s = GNUNET_SERVER_client_get_user_context (client,
1493 struct BobServiceSession);
1497 destroy_service_session (s);
1502 * Initialization of the program and message handlers
1504 * @param cls closure
1505 * @param server the initialized server
1506 * @param c configuration to use
1510 struct GNUNET_SERVER_Handle *server,
1511 const struct GNUNET_CONFIGURATION_Handle *c)
1513 static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
1514 { &GSS_handle_bob_client_message, NULL,
1515 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_BOB,
1517 { &GSS_handle_bob_client_message_multipart, NULL,
1518 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MUTLIPART_BOB,
1522 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1523 { &handle_alices_computation_request,
1524 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_SESSION_INITIALIZATION,
1525 sizeof (struct ServiceRequestMessage) },
1526 { &handle_alices_cryptodata_message,
1527 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_CRYPTODATA,
1531 static const uint32_t ports[] = {
1532 GNUNET_APPLICATION_TYPE_SCALARPRODUCT,
1538 offset has to be sufficiently small to allow computation of:
1539 m1+m2 mod n == (S + a) + (S + b) mod n,
1540 if we have more complex operations, this factor needs to be lowered */
1541 my_offset = gcry_mpi_new (GNUNET_CRYPTO_PAILLIER_BITS / 3);
1542 gcry_mpi_set_bit (my_offset,
1543 GNUNET_CRYPTO_PAILLIER_BITS / 3);
1545 GNUNET_CRYPTO_paillier_create (&my_pubkey,
1547 GNUNET_SERVER_add_handlers (server,
1549 GNUNET_SERVER_disconnect_notify (server,
1550 &handle_client_disconnect,
1552 client_sessions = GNUNET_CONTAINER_multihashmap_create (128,
1554 cadet_sessions = GNUNET_CONTAINER_multihashmap_create (128,
1556 my_cadet = GNUNET_CADET_connect (cfg, NULL,
1557 &cb_channel_incoming,
1558 &cb_channel_destruction,
1561 if (NULL == my_cadet)
1563 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1564 _("Connect to CADET failed\n"));
1565 GNUNET_SCHEDULER_shutdown ();
1568 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1575 * The main function for the scalarproduct service.
1577 * @param argc number of arguments from the command line
1578 * @param argv command line arguments
1579 * @return 0 ok, 1 on error
1585 return (GNUNET_OK ==
1586 GNUNET_SERVICE_run (argc, argv,
1587 "scalarproduct-bob",
1588 GNUNET_SERVICE_OPTION_NONE,
1589 &run, NULL)) ? 0 : 1;
1592 /* end of gnunet-service-scalarproduct_bob.c */