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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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 * How many elements will be supplied in total from the client.
148 * Already transferred elements (received) for multipart
149 * messages from client. Always less than @e total.
151 uint32_t client_received_element_count;
154 * How many elements actually are used for the scalar product.
155 * Size of the arrays in @e r and @e r_prime. Also sometimes
156 * used as an index into the arrays during construction.
158 uint32_t used_element_count;
161 * Counts the number of values received from Alice by us.
162 * Always less than @e used_element_count.
164 uint32_t cadet_received_element_count;
167 * Counts the number of values transmitted from us to Alice.
168 * Always less than @e used_element_count.
170 uint32_t cadet_transmitted_element_count;
173 * State of this session. In
174 * #GNUNET_SCALARPRODUCT_STATUS_ACTIVE while operation is
175 * ongoing, afterwards in #GNUNET_SCALARPRODUCT_STATUS_SUCCESS or
176 * #GNUNET_SCALARPRODUCT_STATUS_FAILURE.
178 enum GNUNET_SCALARPRODUCT_ResponseStatus status;
181 * Are we already in #destroy_service_session()?
189 * An incoming session from CADET.
191 struct CadetIncomingSession
195 * Associated client session, or NULL.
197 struct BobServiceSession *s;
202 struct GNUNET_CADET_Channel *channel;
205 * Originator's peer identity. (Only for diagnostics.)
207 struct GNUNET_PeerIdentity peer;
210 * (hopefully) unique transaction ID
212 struct GNUNET_HashCode session_id;
215 * Public key of the remote service.
217 struct GNUNET_CRYPTO_PaillierPublicKey remote_pubkey;
220 * The message queue for this channel.
222 struct GNUNET_MQ_Handle *cadet_mq;
225 * Has this CADET session been added to the map yet?
226 * #GNUNET_YES if so, in which case @e session_id is
232 * Are we already in #destroy_cadet_session()?
240 * GNUnet configuration handle
242 static const struct GNUNET_CONFIGURATION_Handle *cfg;
245 * Service's own public key
247 static struct GNUNET_CRYPTO_PaillierPublicKey my_pubkey;
250 * Service's own private key
252 static struct GNUNET_CRYPTO_PaillierPrivateKey my_privkey;
255 * Service's offset for values that could possibly be negative but are plaintext for encryption.
257 static gcry_mpi_t my_offset;
260 * Map of `struct BobServiceSession`, by session keys.
262 static struct GNUNET_CONTAINER_MultiHashMap *client_sessions;
265 * Map of `struct CadetIncomingSession`, by session keys.
267 static struct GNUNET_CONTAINER_MultiHashMap *cadet_sessions;
270 * Handle to the CADET service.
272 static struct GNUNET_CADET_Handle *my_cadet;
277 * Finds a not terminated client session in the respective map based on
280 * @param key the session key we want to search for
281 * @return the matching session, or NULL for none
283 static struct BobServiceSession *
284 find_matching_client_session (const struct GNUNET_HashCode *key)
286 return GNUNET_CONTAINER_multihashmap_get (client_sessions,
292 * Finds a CADET session in the respective map based on session key.
294 * @param key the session key we want to search for
295 * @return the matching session, or NULL for none
297 static struct CadetIncomingSession *
298 find_matching_cadet_session (const struct GNUNET_HashCode *key)
300 return GNUNET_CONTAINER_multihashmap_get (cadet_sessions,
306 * Callback used to free the elements in the map.
309 * @param key key of the element
310 * @param value the value to free
313 free_element_cb (void *cls,
314 const struct GNUNET_HashCode *key,
317 struct GNUNET_SCALARPRODUCT_Element *element = value;
319 GNUNET_free (element);
325 * Destroy session state, we are done with it.
327 * @param session the session to free elements from
330 destroy_cadet_session (struct CadetIncomingSession *s);
334 * Destroy session state, we are done with it.
336 * @param session the session to free elements from
339 destroy_service_session (struct BobServiceSession *s)
341 struct CadetIncomingSession *in;
344 if (GNUNET_YES == s->in_destroy)
346 s->in_destroy = GNUNET_YES;
347 if (NULL != (in = s->cadet))
350 destroy_cadet_session (in);
352 if (NULL != s->client_mq)
354 GNUNET_MQ_destroy (s->client_mq);
357 if (NULL != s->client)
359 GNUNET_SERVER_client_disconnect (s->client);
362 GNUNET_assert (GNUNET_YES ==
363 GNUNET_CONTAINER_multihashmap_remove (client_sessions,
366 if (NULL != s->intersected_elements)
368 GNUNET_CONTAINER_multihashmap_iterate (s->intersected_elements,
371 GNUNET_CONTAINER_multihashmap_destroy (s->intersected_elements);
372 s->intersected_elements = NULL;
374 if (NULL != s->intersection_op)
376 GNUNET_SET_operation_cancel (s->intersection_op);
377 s->intersection_op = NULL;
379 if (NULL != s->intersection_set)
381 GNUNET_SET_destroy (s->intersection_set);
382 s->intersection_set = NULL;
386 GNUNET_free (s->e_a);
389 if (NULL != s->sorted_elements)
391 for (i=0;i<s->used_element_count;i++)
392 gcry_mpi_release (s->sorted_elements[i].value);
393 GNUNET_free (s->sorted_elements);
394 s->sorted_elements = NULL;
401 if (NULL != s->r_prime)
403 GNUNET_free (s->r_prime);
411 * Destroy incoming CADET session state, we are done with it.
413 * @param in the session to free elements from
416 destroy_cadet_session (struct CadetIncomingSession *in)
418 struct BobServiceSession *s;
420 if (GNUNET_YES == in->in_destroy)
422 in->in_destroy = GNUNET_YES;
423 if (NULL != (s = in->s))
426 destroy_service_session (s);
428 if (GNUNET_YES == in->in_map)
430 GNUNET_assert (GNUNET_YES ==
431 GNUNET_CONTAINER_multihashmap_remove (cadet_sessions,
434 in->in_map = GNUNET_NO;
436 if (NULL != in->cadet_mq)
438 GNUNET_MQ_destroy (in->cadet_mq);
441 if (NULL != in->channel)
443 GNUNET_CADET_channel_destroy (in->channel);
451 * Notify the client that the session has succeeded or failed. This
452 * message gets sent to Bob's client if the operation completed or
453 * Alice disconnected.
455 * @param session the associated client session to fail or succeed
458 prepare_client_end_notification (struct BobServiceSession *session)
460 struct ClientResponseMessage *msg;
461 struct GNUNET_MQ_Envelope *e;
463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
464 "Sending session-end notification with status %d to client for session %s\n",
466 GNUNET_h2s (&session->session_id));
467 e = GNUNET_MQ_msg (msg,
468 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT);
470 msg->product_length = htonl (0);
471 msg->status = htonl (session->status);
472 GNUNET_MQ_send (session->client_mq,
478 * Function called whenever a channel is destroyed. Should clean up
479 * any associated state.
481 * It must NOT call #GNUNET_CADET_channel_destroy() on the channel.
483 * @param cls closure (set from #GNUNET_CADET_connect())
484 * @param channel connection to the other end (henceforth invalid)
485 * @param channel_ctx place where local state associated
486 * with the channel is stored
489 cb_channel_destruction (void *cls,
490 const struct GNUNET_CADET_Channel *channel,
493 struct CadetIncomingSession *in = channel_ctx;
494 struct BobServiceSession *s;
496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
497 "Peer disconnected, terminating session %s with peer %s\n",
498 GNUNET_h2s (&in->session_id),
499 GNUNET_i2s (&in->peer));
500 if (NULL != in->cadet_mq)
502 GNUNET_MQ_destroy (in->cadet_mq);
506 if (NULL != (s = in->s))
508 if (GNUNET_SCALARPRODUCT_STATUS_ACTIVE == s->status)
510 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
511 prepare_client_end_notification (s);
514 destroy_cadet_session (in);
519 * MQ finished giving our last message to CADET, now notify
520 * the client that we are finished.
523 bob_cadet_done_cb (void *cls)
525 struct BobServiceSession *session = cls;
527 session->status = GNUNET_SCALARPRODUCT_STATUS_SUCCESS;
528 prepare_client_end_notification (session);
533 * Maximum count of elements we can put into a multipart message
535 #define ELEMENT_CAPACITY ((GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE - 1 - sizeof (struct BobCryptodataMultipartMessage)) / sizeof (struct GNUNET_CRYPTO_PaillierCiphertext))
539 * Send a multipart chunk of a service response from Bob to Alice.
540 * This element only contains the two permutations of R, R'.
542 * @param s the associated service session
545 transmit_bobs_cryptodata_message_multipart (struct BobServiceSession *s)
547 struct GNUNET_CRYPTO_PaillierCiphertext *payload;
548 struct BobCryptodataMultipartMessage *msg;
549 struct GNUNET_MQ_Envelope *e;
554 while (s->cadet_transmitted_element_count != s->used_element_count)
556 todo_count = s->used_element_count - s->cadet_transmitted_element_count;
557 if (todo_count > ELEMENT_CAPACITY / 2)
558 todo_count = ELEMENT_CAPACITY / 2;
560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
561 "Sending %u additional crypto values to Alice\n",
562 (unsigned int) todo_count);
563 e = GNUNET_MQ_msg_extra (msg,
564 todo_count * sizeof (struct GNUNET_CRYPTO_PaillierCiphertext) * 2,
565 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_CRYPTODATA_MULTIPART);
566 msg->contained_element_count = htonl (todo_count);
567 payload = (struct GNUNET_CRYPTO_PaillierCiphertext *) &msg[1];
568 for (i = s->cadet_transmitted_element_count, j = 0; i < s->cadet_transmitted_element_count + todo_count; i++)
570 //r[i][p] and r[i][q]
571 memcpy (&payload[j++],
573 sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
574 memcpy (&payload[j++],
576 sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
578 s->cadet_transmitted_element_count += todo_count;
579 if (s->cadet_transmitted_element_count == s->used_element_count)
580 GNUNET_MQ_notify_sent (e,
583 GNUNET_MQ_send (s->cadet->cadet_mq,
586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
587 "All values queued for Alice, Bob is done\n");
592 * Bob generates the response message to be sent to Alice after
593 * computing the values (1), (2), S and S'.
595 * (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)})$
596 * (2)[]: $E_A(a_{pi'(i)}) times E_A(- r_{pi'(i)}) &= E_A(a_{pi'(i)} - r_{pi'(i)})$
597 * S: $S := E_A(sum (r_i + b_i)^2)$
598 * S': $S' := E_A(sum r_i^2)$
600 * @param s the associated requesting session with Alice
603 transmit_bobs_cryptodata_message (struct BobServiceSession *s)
605 struct BobCryptodataMessage *msg;
606 struct GNUNET_MQ_Envelope *e;
607 struct GNUNET_CRYPTO_PaillierCiphertext *payload;
610 s->cadet_transmitted_element_count
611 = ((GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE - 1 - sizeof (struct BobCryptodataMessage))
612 / sizeof (struct GNUNET_CRYPTO_PaillierCiphertext) / 2) - 1;
613 if (s->cadet_transmitted_element_count > s->used_element_count)
614 s->cadet_transmitted_element_count = s->used_element_count;
616 e = GNUNET_MQ_msg_extra (msg,
617 (2 + s->cadet_transmitted_element_count * 2)
618 * sizeof (struct GNUNET_CRYPTO_PaillierCiphertext),
619 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_CRYPTODATA);
620 msg->contained_element_count = htonl (s->cadet_transmitted_element_count);
622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
623 "Sending %u/%u crypto values to Alice\n",
624 (unsigned int) s->cadet_transmitted_element_count,
625 (unsigned int) s->used_element_count);
627 payload = (struct GNUNET_CRYPTO_PaillierCiphertext *) &msg[1];
630 sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
633 sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
635 payload = &payload[2];
637 for (i = 0; i < s->cadet_transmitted_element_count; i++)
639 //k[i][p] and k[i][q]
640 memcpy (&payload[i * 2],
642 sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
643 memcpy (&payload[i * 2 + 1],
645 sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
647 if (s->cadet_transmitted_element_count == s->used_element_count)
648 GNUNET_MQ_notify_sent (e,
651 GNUNET_MQ_send (s->cadet->cadet_mq,
653 transmit_bobs_cryptodata_message_multipart (s);
655 #undef ELEMENT_CAPACITY
659 * Computes the square sum over a vector of a given length.
661 * @param vector the vector to compute over
662 * @param length the length of the vector
663 * @return an MPI value containing the calculated sum, never NULL
664 * TODO: code duplication with Alice!
667 compute_square_sum (const gcry_mpi_t *vector,
674 GNUNET_assert (NULL != (sum = gcry_mpi_new (0)));
675 GNUNET_assert (NULL != (elem = gcry_mpi_new (0)));
676 for (i = 0; i < length; i++)
678 gcry_mpi_mul (elem, vector[i], vector[i]);
679 gcry_mpi_add (sum, sum, elem);
681 gcry_mpi_release (elem);
688 * (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)})$
689 * (2)[]: $E_A(a_{pi'(i)}) otimes E_A(- r_{pi'(i)}) &= E_A(a_{pi'(i)} - r_{pi'(i)})$
690 * S: $S := E_A(sum (r_i + b_i)^2)$
691 * S': $S' := E_A(sum r_i^2)$
693 * @param request the requesting session + bob's requesting peer
694 * @return #GNUNET_OK on success
697 compute_service_response (struct BobServiceSession *session)
705 const struct MpiElement *b;
706 struct GNUNET_CRYPTO_PaillierCiphertext *a;
707 struct GNUNET_CRYPTO_PaillierCiphertext *r;
708 struct GNUNET_CRYPTO_PaillierCiphertext *r_prime;
710 count = session->used_element_count;
712 b = session->sorted_elements;
713 q = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
715 p = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
717 rand = GNUNET_malloc (sizeof (gcry_mpi_t) * count);
718 for (i = 0; i < count; i++)
719 GNUNET_assert (NULL != (rand[i] = gcry_mpi_new (0)));
720 r = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_PaillierCiphertext) * count);
721 r_prime = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_PaillierCiphertext) * count);
723 for (i = 0; i < count; i++)
727 svalue = (int32_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
729 // long to gcry_mpi_t
731 gcry_mpi_sub_ui (rand[i],
735 rand[i] = gcry_mpi_set_ui (rand[i], svalue);
738 tmp = gcry_mpi_new (0);
739 // encrypt the element
740 // for the sake of readability I decided to have dedicated permutation
741 // vectors, which get rid of all the lookups in p/q.
742 // however, ap/aq are not absolutely necessary but are just abstraction
743 // Calculate Kp = E(S + a_pi) (+) E(S - r_pi - b_pi)
744 for (i = 0; i < count; i++)
746 // E(S - r_pi - b_pi)
747 gcry_mpi_sub (tmp, my_offset, rand[p[i]]);
748 gcry_mpi_sub (tmp, tmp, b[p[i]].value);
750 GNUNET_CRYPTO_paillier_encrypt (&session->cadet->remote_pubkey,
755 // E(S - r_pi - b_pi) * E(S + a_pi) == E(2*S + a - r - b)
757 GNUNET_CRYPTO_paillier_hom_add (&session->cadet->remote_pubkey,
767 // Calculate Kq = E(S + a_qi) (+) E(S - r_qi)
768 for (i = 0; i < count; i++)
771 gcry_mpi_sub (tmp, my_offset, rand[q[i]]);
773 GNUNET_CRYPTO_paillier_encrypt (&session->cadet->remote_pubkey,
778 // E(S - r_qi) * E(S + a_qi) == E(2*S + a_qi - r_qi)
780 GNUNET_CRYPTO_paillier_hom_add (&session->cadet->remote_pubkey,
789 gcry_mpi_release (tmp);
791 // Calculate S' = E(SUM( r_i^2 ))
792 tmp = compute_square_sum (rand, count);
794 GNUNET_CRYPTO_paillier_encrypt (&session->cadet->remote_pubkey,
798 gcry_mpi_release (tmp);
800 // Calculate S = E(SUM( (r_i + b_i)^2 ))
801 for (i = 0; i < count; i++)
802 gcry_mpi_add (rand[i], rand[i], b[i].value);
803 tmp = compute_square_sum (rand, count);
805 GNUNET_CRYPTO_paillier_encrypt (&session->cadet->remote_pubkey,
809 gcry_mpi_release (tmp);
812 session->r_prime = r_prime;
814 for (i = 0; i < count; i++)
815 gcry_mpi_release (rand[i]);
816 GNUNET_free (session->e_a);
825 GNUNET_free (r_prime);
826 gcry_mpi_release (tmp);
829 for (i = 0; i < count; i++)
830 gcry_mpi_release (rand[i]);
832 return GNUNET_SYSERR;
837 * Iterator to copy over messages from the hash map
838 * into an array for sorting.
840 * @param cls the `struct BobServiceSession *`
841 * @param key the key (unused)
842 * @param value the `struct GNUNET_SCALARPRODUCT_Element *`
843 * TODO: code duplication with Alice!
846 copy_element_cb (void *cls,
847 const struct GNUNET_HashCode *key,
850 struct BobServiceSession *s = cls;
851 struct GNUNET_SCALARPRODUCT_Element *e = value;
855 mval = gcry_mpi_new (0);
856 val = (int64_t) GNUNET_ntohll (e->value);
858 gcry_mpi_sub_ui (mval, mval, -val);
860 gcry_mpi_add_ui (mval, mval, val);
861 s->sorted_elements [s->used_element_count].value = mval;
862 s->sorted_elements [s->used_element_count].key = &e->key;
863 s->used_element_count++;
869 * Compare two `struct MpiValue`s by key for sorting.
871 * @param a pointer to first `struct MpiValue *`
872 * @param b pointer to first `struct MpiValue *`
873 * @return -1 for a < b, 0 for a=b, 1 for a > b.
874 * TODO: code duplication with Alice!
877 element_cmp (const void *a,
880 const struct MpiElement *ma = a;
881 const struct MpiElement *mb = b;
883 return GNUNET_CRYPTO_hash_cmp (ma->key,
889 * Intersection operation and receiving data via CADET from
890 * Alice are both done, compute and transmit our reply via
893 * @param s session to transmit reply for.
896 transmit_cryptographic_reply (struct BobServiceSession *s)
898 struct GNUNET_CADET_Channel *channel;
900 /* TODO: code duplication with Alice! */
901 LOG (GNUNET_ERROR_TYPE_DEBUG,
902 "Received everything, building reply for Alice\n");
904 = GNUNET_malloc (GNUNET_CONTAINER_multihashmap_size (s->intersected_elements) *
905 sizeof (struct MpiElement));
906 s->used_element_count = 0;
907 GNUNET_CONTAINER_multihashmap_iterate (s->intersected_elements,
910 qsort (s->sorted_elements,
911 s->used_element_count,
912 sizeof (struct MpiElement),
915 compute_service_response (s))
917 channel = s->cadet->channel;
918 s->cadet->channel = NULL;
919 GNUNET_CADET_channel_destroy (channel);
922 transmit_bobs_cryptodata_message (s);
927 * Handle a multipart-chunk of a request from another service to
928 * calculate a scalarproduct with us.
930 * @param cls closure (set from #GNUNET_CADET_connect)
931 * @param channel connection to the other end
932 * @param channel_ctx place to store local state associated with the @a channel
933 * @param message the actual message
934 * @return #GNUNET_OK to keep the connection open,
935 * #GNUNET_SYSERR to close it (signal serious error)
938 handle_alices_cryptodata_message (void *cls,
939 struct GNUNET_CADET_Channel *channel,
941 const struct GNUNET_MessageHeader *message)
943 struct CadetIncomingSession *in = *channel_ctx;
944 struct BobServiceSession *s;
945 const struct AliceCryptodataMessage *msg;
946 const struct GNUNET_CRYPTO_PaillierCiphertext *payload;
947 uint32_t contained_elements;
955 return GNUNET_SYSERR;
961 return GNUNET_SYSERR;
963 msize = ntohs (message->size);
964 if (msize <= sizeof (struct AliceCryptodataMessage))
967 return GNUNET_SYSERR;
969 msg = (const struct AliceCryptodataMessage *) message;
970 contained_elements = ntohl (msg->contained_element_count);
971 /* Our intersection may still be ongoing, but this is nevertheless
972 an upper bound on the required array size */
973 max = GNUNET_CONTAINER_multihashmap_size (s->intersected_elements);
974 msg_length = sizeof (struct AliceCryptodataMessage)
975 + contained_elements * sizeof (struct GNUNET_CRYPTO_PaillierCiphertext);
976 if ( (msize != msg_length) ||
977 (0 == contained_elements) ||
978 (contained_elements > UINT16_MAX) ||
979 (max < contained_elements + s->cadet_received_element_count) )
982 return GNUNET_SYSERR;
984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
985 "Received %u crypto values from Alice\n",
986 (unsigned int) contained_elements);
988 payload = (const struct GNUNET_CRYPTO_PaillierCiphertext *) &msg[1];
990 s->e_a = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_PaillierCiphertext) *
992 memcpy (&s->e_a[s->cadet_received_element_count],
994 sizeof (struct GNUNET_CRYPTO_PaillierCiphertext) * contained_elements);
995 s->cadet_received_element_count += contained_elements;
997 if ( (s->cadet_received_element_count == max) &&
998 (NULL == s->intersection_op) )
1000 /* intersection has finished also on our side, and
1001 we got the full set, so we can proceed with the
1002 CADET response(s) */
1003 transmit_cryptographic_reply (s);
1005 GNUNET_CADET_receive_done (s->cadet->channel);
1011 * Callback for set operation results. Called for each element
1012 * that needs to be removed from the result set.
1014 * @param cls closure with the `struct BobServiceSession`
1015 * @param element a result element, only valid if status is #GNUNET_SET_STATUS_OK
1016 * @param status what has happened with the set intersection?
1019 cb_intersection_element_removed (void *cls,
1020 const struct GNUNET_SET_Element *element,
1021 enum GNUNET_SET_Status status)
1023 struct BobServiceSession *s = cls;
1024 struct GNUNET_SCALARPRODUCT_Element *se;
1028 case GNUNET_SET_STATUS_OK:
1029 /* this element has been removed from the set */
1030 se = GNUNET_CONTAINER_multihashmap_get (s->intersected_elements,
1032 GNUNET_assert (NULL != se);
1033 LOG (GNUNET_ERROR_TYPE_DEBUG,
1034 "Removed element with key %s and value %lld\n",
1035 GNUNET_h2s (&se->key),
1036 (long long) GNUNET_ntohll (se->value));
1037 GNUNET_assert (GNUNET_YES ==
1038 GNUNET_CONTAINER_multihashmap_remove (s->intersected_elements,
1043 case GNUNET_SET_STATUS_DONE:
1044 s->intersection_op = NULL;
1045 GNUNET_break (NULL == s->intersection_set);
1046 GNUNET_CADET_receive_done (s->cadet->channel);
1047 LOG (GNUNET_ERROR_TYPE_DEBUG,
1048 "Finished intersection, %d items remain\n",
1049 GNUNET_CONTAINER_multihashmap_size (s->intersected_elements));
1050 if (s->client_received_element_count ==
1051 GNUNET_CONTAINER_multihashmap_size (s->intersected_elements))
1053 /* CADET transmission from Alice is also already done,
1054 start with our own reply */
1055 transmit_cryptographic_reply (s);
1058 case GNUNET_SET_STATUS_HALF_DONE:
1059 /* unexpected for intersection */
1062 case GNUNET_SET_STATUS_FAILURE:
1063 /* unhandled status code */
1064 LOG (GNUNET_ERROR_TYPE_DEBUG,
1065 "Set intersection failed!\n");
1066 s->intersection_op = NULL;
1067 if (NULL != s->intersection_set)
1069 GNUNET_SET_destroy (s->intersection_set);
1070 s->intersection_set = NULL;
1072 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
1073 prepare_client_end_notification (s);
1083 * We've paired up a client session with an incoming CADET request.
1084 * Initiate set intersection work.
1086 * @param s client session to start intersection for
1089 start_intersection (struct BobServiceSession *s)
1091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1092 "Got session with key %s and %u elements, starting intersection.\n",
1093 GNUNET_h2s (&s->session_id),
1094 (unsigned int) s->total);
1097 = GNUNET_SET_prepare (&s->cadet->peer,
1100 GNUNET_SET_RESULT_REMOVED,
1101 &cb_intersection_element_removed,
1104 GNUNET_SET_commit (s->intersection_op,
1105 s->intersection_set))
1108 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
1109 prepare_client_end_notification (s);
1112 GNUNET_SET_destroy (s->intersection_set);
1113 s->intersection_set = NULL;
1118 * Handle a request from Alice to calculate a scalarproduct with us (Bob).
1120 * @param cls closure (set from #GNUNET_CADET_connect)
1121 * @param channel connection to the other end
1122 * @param channel_ctx place to store the `struct CadetIncomingSession *`
1123 * @param message the actual message
1124 * @return #GNUNET_OK to keep the connection open,
1125 * #GNUNET_SYSERR to close it (signal serious error)
1128 handle_alices_computation_request (void *cls,
1129 struct GNUNET_CADET_Channel *channel,
1131 const struct GNUNET_MessageHeader *message)
1133 struct CadetIncomingSession *in = *channel_ctx;
1134 struct BobServiceSession *s;
1135 const struct ServiceRequestMessage *msg;
1137 if (ntohs (message->size) != sizeof (struct ServiceRequestMessage))
1139 GNUNET_break_op (0);
1140 return GNUNET_SYSERR;
1142 msg = (const struct ServiceRequestMessage *) message;
1143 if (GNUNET_YES == in->in_map)
1145 GNUNET_break_op (0);
1146 return GNUNET_SYSERR;
1148 if (NULL != find_matching_cadet_session (&msg->session_id))
1150 /* not unique, got one like this already */
1151 GNUNET_break_op (0);
1152 return GNUNET_SYSERR;
1154 in->session_id = msg->session_id;
1155 in->remote_pubkey = msg->public_key;
1156 GNUNET_assert (GNUNET_YES ==
1157 GNUNET_CONTAINER_multihashmap_put (cadet_sessions,
1160 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1161 s = find_matching_client_session (&in->session_id);
1164 /* no client waiting for this request, wait for client */
1167 GNUNET_assert (NULL == s->cadet);
1171 if (s->client_received_element_count == s->total)
1172 start_intersection (s);
1178 * Function called for inbound channels on Bob's end. Does some
1179 * preliminary initialization, more happens after we get Alice's first
1182 * @param cls closure
1183 * @param channel new handle to the channel
1184 * @param initiator peer that started the channel
1185 * @param port unused
1186 * @param options unused
1187 * @return session associated with the channel
1190 cb_channel_incoming (void *cls,
1191 struct GNUNET_CADET_Channel *channel,
1192 const struct GNUNET_PeerIdentity *initiator,
1194 enum GNUNET_CADET_ChannelOption options)
1196 struct CadetIncomingSession *in;
1198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1199 "New incoming channel from peer %s.\n",
1200 GNUNET_i2s (initiator));
1201 in = GNUNET_new (struct CadetIncomingSession);
1202 in->peer = *initiator;
1203 in->channel = channel;
1204 in->cadet_mq = GNUNET_CADET_mq_create (in->channel);
1210 * We're receiving additional set data. Add it to our
1211 * set and if we are done, initiate the transaction.
1213 * @param cls closure
1214 * @param client identification of the client
1215 * @param message the actual message
1218 GSS_handle_bob_client_message_multipart (void *cls,
1219 struct GNUNET_SERVER_Client *client,
1220 const struct GNUNET_MessageHeader *message)
1222 const struct ComputationBobCryptodataMultipartMessage * msg;
1223 struct BobServiceSession *s;
1224 uint32_t contained_count;
1225 const struct GNUNET_SCALARPRODUCT_Element *elements;
1228 struct GNUNET_SET_Element set_elem;
1229 struct GNUNET_SCALARPRODUCT_Element *elem;
1231 s = GNUNET_SERVER_client_get_user_context (client,
1232 struct BobServiceSession);
1235 /* session needs to already exist */
1237 GNUNET_SERVER_receive_done (client,
1241 msize = ntohs (message->size);
1242 if (msize < sizeof (struct ComputationBobCryptodataMultipartMessage))
1245 GNUNET_SERVER_receive_done (client,
1249 msg = (const struct ComputationBobCryptodataMultipartMessage *) message;
1250 contained_count = ntohl (msg->element_count_contained);
1252 if ( (msize != (sizeof (struct ComputationBobCryptodataMultipartMessage) +
1253 contained_count * sizeof (struct GNUNET_SCALARPRODUCT_Element))) ||
1254 (0 == contained_count) ||
1255 (UINT16_MAX < contained_count) ||
1256 (s->total == s->client_received_element_count) ||
1257 (s->total < s->client_received_element_count + contained_count) )
1259 GNUNET_break_op (0);
1260 GNUNET_SERVER_receive_done (client,
1264 elements = (const struct GNUNET_SCALARPRODUCT_Element *) &msg[1];
1265 for (i = 0; i < contained_count; i++)
1267 elem = GNUNET_new (struct GNUNET_SCALARPRODUCT_Element);
1270 sizeof (struct GNUNET_SCALARPRODUCT_Element));
1271 if (GNUNET_SYSERR ==
1272 GNUNET_CONTAINER_multihashmap_put (s->intersected_elements,
1275 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1281 set_elem.data = &elem->key;
1282 set_elem.size = sizeof (elem->key);
1283 set_elem.element_type = 0;
1284 GNUNET_SET_add_element (s->intersection_set,
1288 s->client_received_element_count += contained_count;
1289 GNUNET_SERVER_receive_done (client,
1291 if (s->total != s->client_received_element_count)
1296 if (NULL == s->cadet)
1298 /* no Alice waiting for this request, wait for Alice */
1301 start_intersection (s);
1306 * Handler for Bob's a client request message. Bob is in the response
1307 * role, keep the values + session and waiting for a matching session
1308 * or process a waiting request from Alice.
1310 * @param cls closure
1311 * @param client identification of the client
1312 * @param message the actual message
1315 GSS_handle_bob_client_message (void *cls,
1316 struct GNUNET_SERVER_Client *client,
1317 const struct GNUNET_MessageHeader *message)
1319 const struct BobComputationMessage *msg;
1320 struct BobServiceSession *s;
1321 struct CadetIncomingSession *in;
1322 uint32_t contained_count;
1323 uint32_t total_count;
1324 const struct GNUNET_SCALARPRODUCT_Element *elements;
1326 struct GNUNET_SET_Element set_elem;
1327 struct GNUNET_SCALARPRODUCT_Element *elem;
1330 s = GNUNET_SERVER_client_get_user_context (client,
1331 struct BobServiceSession);
1334 /* only one concurrent session per client connection allowed,
1335 simplifies logic a lot... */
1337 GNUNET_SERVER_receive_done (client,
1341 msize = ntohs (message->size);
1342 if (msize < sizeof (struct BobComputationMessage))
1345 GNUNET_SERVER_receive_done (client,
1349 msg = (const struct BobComputationMessage *) message;
1350 total_count = ntohl (msg->element_count_total);
1351 contained_count = ntohl (msg->element_count_contained);
1352 if ( (0 == total_count) ||
1353 (0 == contained_count) ||
1354 (UINT16_MAX < contained_count) ||
1355 (msize != (sizeof (struct BobComputationMessage) +
1356 contained_count * sizeof (struct GNUNET_SCALARPRODUCT_Element))) )
1358 GNUNET_break_op (0);
1359 GNUNET_SERVER_receive_done (client,
1363 if (NULL != find_matching_client_session (&msg->session_key))
1366 GNUNET_SERVER_receive_done (client,
1371 s = GNUNET_new (struct BobServiceSession);
1372 s->status = GNUNET_SCALARPRODUCT_STATUS_ACTIVE;
1374 s->client_mq = GNUNET_MQ_queue_for_server_client (client);
1375 s->total = total_count;
1376 s->client_received_element_count = contained_count;
1377 s->session_id = msg->session_key;
1378 GNUNET_break (GNUNET_YES ==
1379 GNUNET_CONTAINER_multihashmap_put (client_sessions,
1382 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1383 elements = (const struct GNUNET_SCALARPRODUCT_Element *) &msg[1];
1384 s->intersected_elements = GNUNET_CONTAINER_multihashmap_create (s->total,
1386 s->intersection_set = GNUNET_SET_create (cfg,
1387 GNUNET_SET_OPERATION_INTERSECTION);
1388 for (i = 0; i < contained_count; i++)
1390 if (0 == GNUNET_ntohll (elements[i].value))
1392 elem = GNUNET_new (struct GNUNET_SCALARPRODUCT_Element);
1395 sizeof (struct GNUNET_SCALARPRODUCT_Element));
1396 if (GNUNET_SYSERR ==
1397 GNUNET_CONTAINER_multihashmap_put (s->intersected_elements,
1400 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1406 set_elem.data = &elem->key;
1407 set_elem.size = sizeof (elem->key);
1408 set_elem.element_type = 0;
1409 GNUNET_SET_add_element (s->intersection_set,
1412 s->used_element_count++;
1414 GNUNET_SERVER_client_set_user_context (client,
1416 GNUNET_SERVER_receive_done (client,
1418 if (s->total != s->client_received_element_count)
1423 in = find_matching_cadet_session (&s->session_id);
1426 /* nothing yet, wait for Alice */
1429 GNUNET_assert (NULL == in->s);
1433 start_intersection (s);
1438 * Task run during shutdown.
1444 shutdown_task (void *cls,
1445 const struct GNUNET_SCHEDULER_TaskContext *tc)
1447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1448 "Shutting down, initiating cleanup.\n");
1449 // FIXME: we have to cut our connections to CADET first!
1450 if (NULL != my_cadet)
1452 GNUNET_CADET_disconnect (my_cadet);
1455 GNUNET_CONTAINER_multihashmap_destroy (client_sessions);
1456 client_sessions = NULL;
1457 GNUNET_CONTAINER_multihashmap_destroy (cadet_sessions);
1458 cadet_sessions = NULL;
1463 * A client disconnected.
1465 * Remove the associated session(s), release data structures
1466 * and cancel pending outgoing transmissions to the client.
1468 * @param cls closure, NULL
1469 * @param client identification of the client
1472 handle_client_disconnect (void *cls,
1473 struct GNUNET_SERVER_Client *client)
1475 struct BobServiceSession *s;
1479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1480 "Client disconnected from us.\n",
1482 s = GNUNET_SERVER_client_get_user_context (client,
1483 struct BobServiceSession);
1487 destroy_service_session (s);
1492 * Initialization of the program and message handlers
1494 * @param cls closure
1495 * @param server the initialized server
1496 * @param c configuration to use
1500 struct GNUNET_SERVER_Handle *server,
1501 const struct GNUNET_CONFIGURATION_Handle *c)
1503 static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
1504 { &GSS_handle_bob_client_message, NULL,
1505 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_BOB,
1507 { &GSS_handle_bob_client_message_multipart, NULL,
1508 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MUTLIPART_BOB,
1512 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1513 { &handle_alices_computation_request,
1514 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_SESSION_INITIALIZATION,
1515 sizeof (struct ServiceRequestMessage) },
1516 { &handle_alices_cryptodata_message,
1517 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_CRYPTODATA,
1521 static const uint32_t ports[] = {
1522 GNUNET_APPLICATION_TYPE_SCALARPRODUCT,
1528 offset has to be sufficiently small to allow computation of:
1529 m1+m2 mod n == (S + a) + (S + b) mod n,
1530 if we have more complex operations, this factor needs to be lowered */
1531 my_offset = gcry_mpi_new (GNUNET_CRYPTO_PAILLIER_BITS / 3);
1532 gcry_mpi_set_bit (my_offset,
1533 GNUNET_CRYPTO_PAILLIER_BITS / 3);
1535 GNUNET_CRYPTO_paillier_create (&my_pubkey,
1537 GNUNET_SERVER_add_handlers (server,
1539 GNUNET_SERVER_disconnect_notify (server,
1540 &handle_client_disconnect,
1542 client_sessions = GNUNET_CONTAINER_multihashmap_create (128,
1544 cadet_sessions = GNUNET_CONTAINER_multihashmap_create (128,
1546 my_cadet = GNUNET_CADET_connect (cfg, NULL,
1547 &cb_channel_incoming,
1548 &cb_channel_destruction,
1551 if (NULL == my_cadet)
1553 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1554 _("Connect to CADET failed\n"));
1555 GNUNET_SCHEDULER_shutdown ();
1558 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1565 * The main function for the scalarproduct service.
1567 * @param argc number of arguments from the command line
1568 * @param argv command line arguments
1569 * @return 0 ok, 1 on error
1575 return (GNUNET_OK ==
1576 GNUNET_SERVICE_run (argc, argv,
1577 "scalarproduct-bob",
1578 GNUNET_SERVICE_OPTION_NONE,
1579 &run, NULL)) ? 0 : 1;
1582 /* end of gnunet-service-scalarproduct_bob.c */