2 This file is part of GNUnet.
3 (C) 2013 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.
22 * @file scalarproduct/gnunet-service-scalarproduct.c
23 * @brief scalarproduct service implementation
24 * @author Christian M. Fuchs
28 #include "gnunet_util_lib.h"
29 #include "gnunet_core_service.h"
30 #include "gnunet_mesh_service.h"
31 #include "gnunet_applications.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_scalarproduct_service.h"
34 #include "scalarproduct.h"
36 #define LOG(kind,...) GNUNET_log_from (kind, "scalarproduct", __VA_ARGS__)
38 ///////////////////////////////////////////////////////////////////////////////
39 // Service Structure Definitions
40 ///////////////////////////////////////////////////////////////////////////////
43 * state a session can be in
47 CLIENT_REQUEST_RECEIVED,
48 WAITING_FOR_BOBS_CONNECT,
49 CLIENT_RESPONSE_RECEIVED,
50 WAITING_FOR_SERVICE_REQUEST,
51 WAITING_FOR_MULTIPART_TRANSMISSION,
52 WAITING_FOR_SERVICE_RESPONSE,
53 SERVICE_REQUEST_RECEIVED,
54 SERVICE_RESPONSE_RECEIVED,
59 * role a peer in a session can assume
68 * A scalarproduct session which tracks:
70 * a request form the client to our final response.
72 * a request from a service to us(service).
77 * the role this peer has
82 * session information is kept in a DLL
84 struct ServiceSession *next;
87 * session information is kept in a DLL
89 struct ServiceSession *prev;
92 * (hopefully) unique transaction ID
94 struct GNUNET_HashCode key;
97 * state of the session
99 enum SessionState state;
102 * Alice or Bob's peerID
104 struct GNUNET_PeerIdentity peer;
107 * the client this request is related to
109 struct GNUNET_SERVER_Client * client;
112 * The message to send
114 struct GNUNET_MessageHeader * msg;
117 * how many elements we were supplied with from the client
119 uint32_t element_count;
122 * how many elements actually are used after applying the mask
124 uint32_t used_element_count;
127 * already transferred elements (sent/received) for multipart messages, less or equal than used_element_count for
129 uint32_t transferred_element_count;
132 * how many bytes the mask is long.
133 * just for convenience so we don't have to re-re-re calculate it each time
135 uint32_t mask_length;
138 * all the vector elements we received
143 * mask of which elements to check
145 unsigned char * mask;
148 * Public key of the remote service, only used by bob
150 gcry_sexp_t remote_pubkey;
153 * E(ai)(Bob) or ai(Alice) after applying the mask
158 * Bob's permutation p of R
163 * Bob's permutation q of R
165 gcry_mpi_t * r_prime;
168 * The computed scalar
173 * My transmit handle for the current message to a alice/bob
175 struct GNUNET_MESH_TransmitHandle * service_transmit_handle;
178 * My transmit handle for the current message to the client
180 struct GNUNET_SERVER_TransmitHandle * client_transmit_handle;
183 * tunnel-handle associated with our mesh handle
185 struct GNUNET_MESH_Tunnel * tunnel;
187 GNUNET_SCHEDULER_TaskIdentifier client_notification_task;
189 GNUNET_SCHEDULER_TaskIdentifier service_request_task;
192 ///////////////////////////////////////////////////////////////////////////////
194 ///////////////////////////////////////////////////////////////////////////////
198 * Handle to the core service (NULL until we've connected to it).
200 static struct GNUNET_MESH_Handle *my_mesh;
203 * The identity of this host.
205 static struct GNUNET_PeerIdentity me;
208 * Service's own public key represented as string
210 static unsigned char * my_pubkey_external;
213 * Service's own public key represented as string
215 static uint32_t my_pubkey_external_length = 0;
220 static gcry_mpi_t my_n;
223 * Service's own n^2 (kept for performance)
225 static gcry_mpi_t my_nsquare;
228 * Service's own public exponent
230 static gcry_mpi_t my_g;
233 * Service's own private multiplier
235 static gcry_mpi_t my_mu;
238 * Service's own private exponent
240 static gcry_mpi_t my_lambda;
243 * Service's offset for values that could possibly be negative but are plaintext for encryption.
245 static gcry_mpi_t my_offset;
248 * Head of our double linked list for client-requests sent to us.
249 * for all of these elements we calculate a scalar product with a remote peer
250 * split between service->service and client->service for simplicity
252 static struct ServiceSession * from_client_head;
254 * Tail of our double linked list for client-requests sent to us.
255 * for all of these elements we calculate a scalar product with a remote peer
256 * split between service->service and client->service for simplicity
258 static struct ServiceSession * from_client_tail;
261 * Head of our double linked list for service-requests sent to us.
262 * for all of these elements we help the requesting service in calculating a scalar product
263 * split between service->service and client->service for simplicity
265 static struct ServiceSession * from_service_head;
268 * Tail of our double linked list for service-requests sent to us.
269 * for all of these elements we help the requesting service in calculating a scalar product
270 * split between service->service and client->service for simplicity
272 static struct ServiceSession * from_service_tail;
275 * Certain events (callbacks for server & mesh operations) must not be queued after shutdown.
277 static int do_shutdown;
279 ///////////////////////////////////////////////////////////////////////////////
281 ///////////////////////////////////////////////////////////////////////////////
285 * Generates an Paillier private/public keyset and extracts the values using libgrcypt only
290 gcry_sexp_t gen_params;
292 gcry_sexp_t tmp_sexp;
301 // we can still use the RSA keygen for generating p,q,n, but using e is pointless.
302 GNUNET_assert (0 == gcry_sexp_build (&gen_params, &erroff,
303 "(genkey(rsa(nbits %d)(rsa-use-e 3:257)))",
306 GNUNET_assert (0 == gcry_pk_genkey (&key, gen_params));
307 gcry_sexp_release (gen_params);
309 // get n and d of our publickey as MPI
310 tmp_sexp = gcry_sexp_find_token (key, "n", 0);
311 GNUNET_assert (tmp_sexp);
312 my_n = gcry_sexp_nth_mpi (tmp_sexp, 1, GCRYMPI_FMT_USG);
313 gcry_sexp_release (tmp_sexp);
314 tmp_sexp = gcry_sexp_find_token (key, "p", 0);
315 GNUNET_assert (tmp_sexp);
316 p = gcry_sexp_nth_mpi (tmp_sexp, 1, GCRYMPI_FMT_USG);
317 gcry_sexp_release (tmp_sexp);
318 tmp_sexp = gcry_sexp_find_token (key, "q", 0);
319 GNUNET_assert (tmp_sexp);
320 q = gcry_sexp_nth_mpi (tmp_sexp, 1, GCRYMPI_FMT_USG);
321 gcry_sexp_release (key);
323 tmp1 = gcry_mpi_new (0);
324 tmp2 = gcry_mpi_new (0);
325 gcd = gcry_mpi_new (0);
326 my_g = gcry_mpi_new (0);
327 my_mu = gcry_mpi_new (0);
328 my_nsquare = gcry_mpi_new (0);
329 my_lambda = gcry_mpi_new (0);
332 // lambda = \frac{(p-1)*(q-1)}{gcd(p-1,q-1)}
333 gcry_mpi_sub_ui (tmp1, p, 1);
334 gcry_mpi_sub_ui (tmp2, q, 1);
335 gcry_mpi_gcd (gcd, tmp1, tmp2);
336 gcry_mpi_set (my_lambda, tmp1);
337 gcry_mpi_mul (my_lambda, my_lambda, tmp2);
338 gcry_mpi_div (my_lambda, NULL, my_lambda, gcd, 0);
341 gcry_mpi_mul (my_nsquare, my_n, my_n);
347 gcry_mpi_randomize (my_g, KEYBITS * 2, GCRY_WEAK_RANDOM);
348 // g must be smaller than n^2
349 if (0 >= gcry_mpi_cmp (my_g, my_nsquare))
352 // g must have gcd == 1 with n^2
353 gcry_mpi_gcd (gcd, my_g, my_nsquare);
355 while (gcry_mpi_cmp_ui (gcd, 1));
357 // is this a valid g?
358 // if so, gcd(((g^lambda mod n^2)-1 )/n, n) = 1
359 gcry_mpi_powm (tmp1, my_g, my_lambda, my_nsquare);
360 gcry_mpi_sub_ui (tmp1, tmp1, 1);
361 gcry_mpi_div (tmp1, NULL, tmp1, my_n, 0);
362 gcry_mpi_gcd (gcd, tmp1, my_n);
364 while (gcry_mpi_cmp_ui (gcd, 1));
366 // calculate our mu based on g and n.
367 // mu = (((g^lambda mod n^2)-1 )/n)^-1 mod n
368 gcry_mpi_invm (my_mu, tmp1, my_n);
370 GNUNET_assert (0 == gcry_sexp_build (&key, &erroff,
371 "(public-key (paillier (n %M)(g %M)))",
374 // get the length of this sexpression
375 my_pubkey_external_length = gcry_sexp_sprint (key,
380 GNUNET_assert (my_pubkey_external_length > 0);
381 my_pubkey_external = GNUNET_malloc (my_pubkey_external_length);
383 // convert the sexpression to canonical format
384 gcry_sexp_sprint (key,
387 my_pubkey_external_length);
389 gcry_sexp_release (key);
391 // offset has to be sufficiently small to allow computation of:
392 // m1+m2 mod n == (S + a) + (S + b) mod n,
393 // if we have more complex operations, this factor needs to be lowered
394 my_offset = gcry_mpi_new (KEYBITS / 3);
395 gcry_mpi_set_bit (my_offset, KEYBITS / 3);
397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _ ("Generated key set with key length %d bits.\n"), KEYBITS);
402 * If target != size, move target bytes to the
403 * end of the size-sized buffer and zero out the
404 * first target-size bytes.
406 * @param buf original buffer
407 * @param size number of bytes in the buffer
408 * @param target target size of the buffer
411 adjust (unsigned char *buf, size_t size, size_t target)
415 memmove (&buf[target - size], buf, size);
416 memset (buf, 0, target - size);
422 * encrypts an element using the paillier crypto system
424 * @param c ciphertext (output)
426 * @param g the public base
427 * @param n the module from which which r is chosen (Z*_n)
428 * @param n_square the module for encryption, for performance reasons.
431 encrypt_element (gcry_mpi_t c, gcry_mpi_t m, gcry_mpi_t g, gcry_mpi_t n, gcry_mpi_t n_square)
435 GNUNET_assert (tmp = gcry_mpi_new (0));
437 while (0 >= gcry_mpi_cmp_ui (tmp, 1))
439 gcry_mpi_randomize (tmp, KEYBITS / 3, GCRY_WEAK_RANDOM);
440 // r must be 1 < r < n
443 gcry_mpi_powm (c, g, m, n_square);
444 gcry_mpi_powm (tmp, tmp, n, n_square);
445 gcry_mpi_mulm (c, tmp, c, n_square);
447 gcry_mpi_release (tmp);
452 * decrypts an element using the paillier crypto system
454 * @param m plaintext (output)
455 * @param c the ciphertext
456 * @param mu the modifier to correct encryption
457 * @param lambda the private exponent
458 * @param n the outer module for decryption
459 * @param n_square the inner module for decryption
462 decrypt_element (gcry_mpi_t m, gcry_mpi_t c, gcry_mpi_t mu, gcry_mpi_t lambda, gcry_mpi_t n, gcry_mpi_t n_square)
464 gcry_mpi_powm (m, c, lambda, n_square);
465 gcry_mpi_sub_ui (m, m, 1);
466 gcry_mpi_div (m, NULL, m, n, 0);
467 gcry_mpi_mulm (m, m, mu, n);
472 * computes the square sum over a vector of a given length.
474 * @param vector the vector to encrypt
475 * @param length the length of the vector
476 * @return an MPI value containing the calculated sum, never NULL
479 compute_square_sum (gcry_mpi_t * vector, uint32_t length)
485 GNUNET_assert (sum = gcry_mpi_new (0));
486 GNUNET_assert (elem = gcry_mpi_new (0));
488 // calculare E(sum (ai ^ 2), publickey)
489 for (i = 0; i < length; i++)
491 gcry_mpi_mul (elem, vector[i], vector[i]);
492 gcry_mpi_add (sum, sum, elem);
494 gcry_mpi_release (elem);
501 * Primitive callback for copying over a message, as they
502 * usually are too complex to be handled in the callback itself.
503 * clears a session-callback, if a session was handed over and the transmit handle was stored
505 * @param cls the message object
506 * @param size the size of the buffer we got
507 * @param buf the buffer to copy the message to
508 * @return 0 if we couldn't copy, else the size copied over
511 do_send_message (void *cls, size_t size, void *buf)
513 struct ServiceSession * session = cls;
518 if (ntohs (session->msg->size) == size)
520 memcpy (buf, session->msg, size);
524 switch (ntohs (session->msg->type))
526 case GNUNET_MESSAGE_TYPE_SCALARPRODUCT_SERVICE_TO_CLIENT:
527 session->state = FINALIZED;
528 session->client_transmit_handle = NULL;
530 case GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_TO_BOB:
531 case GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_TO_BOB_MULTIPART:
533 session->service_transmit_handle = NULL;
534 // reset flags for sending
535 if ((session->state != WAITING_FOR_MULTIPART_TRANSMISSION) && (session->used_element_count != session->transferred_element_count))
536 prepare_service_request_multipart(session);
537 //TODO we have sent a message and now need to trigger trigger the next multipart message sending
539 case GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_TO_ALICE:
540 case GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_TO_ALICE_MULTIPART:
542 session->service_transmit_handle = NULL;
543 if ((session->state != WAITING_FOR_MULTIPART_TRANSMISSION) && (session->used_element_count != session->transferred_element_count))
544 prepare_service_response_multipart(session);
547 session->service_transmit_handle = NULL;
550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
551 "Sent a message of type %hu.\n",
552 ntohs (session->msg->type));
553 GNUNET_free (session->msg);
561 * initializes a new vector with fresh MPI values (=0) of a given length
563 * @param length of the vector to create
564 * @return the initialized vector, never NULL
567 initialize_mpi_vector (uint32_t length)
570 gcry_mpi_t * output = GNUNET_malloc (sizeof (gcry_mpi_t) * length);
572 for (i = 0; i < length; i++)
573 GNUNET_assert (NULL != (output[i] = gcry_mpi_new (0)));
579 * permutes an MPI vector according to the given permutation vector
581 * @param vector the vector to permuted
582 * @param perm the permutation to use
583 * @param length the length of the vectors
584 * @return the permuted vector (same as input), never NULL
587 permute_vector (gcry_mpi_t * vector,
591 gcry_mpi_t tmp[length];
594 GNUNET_assert (length > 0);
597 memcpy (tmp, vector, length * sizeof (gcry_mpi_t));
599 // permute vector according to given
600 for (i = 0; i < length; i++)
601 vector[i] = tmp[perm[i]];
608 * Populate a vector with random integer values and convert them to
610 * @param length the length of the vector we must generate
611 * @return an array of MPI values with random values
614 generate_random_vector (uint32_t length)
616 gcry_mpi_t * random_vector;
620 random_vector = initialize_mpi_vector (length);
621 for (i = 0; i < length; i++)
623 value = (int32_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
625 // long to gcry_mpi_t
627 gcry_mpi_sub_ui (random_vector[i],
631 random_vector[i] = gcry_mpi_set_ui (random_vector[i], value);
634 return random_vector;
639 * Finds a not terminated client/service session in the
640 * given DLL based on session key, element count and state.
642 * @param tail - the tail of the DLL
643 * @param my - the session to compare it to
644 * @return a pointer to a matching session,
647 static struct ServiceSession *
648 find_matching_session (struct ServiceSession * tail,
649 const struct GNUNET_HashCode * key,
650 uint32_t element_count,
651 enum SessionState * state,
652 const struct GNUNET_PeerIdentity * peerid)
654 struct ServiceSession * curr;
656 for (curr = tail; NULL != curr; curr = curr->prev)
658 // if the key matches, and the element_count is same
659 if ((!memcmp (&curr->key, key, sizeof (struct GNUNET_HashCode)))
660 && (curr->element_count == element_count))
662 // if incoming state is NULL OR is same as state of the queued request
663 if ((NULL == state) || (curr->state == *state))
665 // if peerid is NULL OR same as the peer Id in the queued request
667 || (!memcmp (&curr->peer, peerid, sizeof (struct GNUNET_PeerIdentity))))
668 // matches and is not an already terminated session
679 free_session (struct ServiceSession * session)
685 for (i = 0; i < session->used_element_count; i++)
686 gcry_mpi_release (session->a[i]);
688 GNUNET_free (session->a);
690 if (session->product)
691 gcry_mpi_release (session->product);
693 if (session->remote_pubkey)
694 gcry_sexp_release (session->remote_pubkey);
696 GNUNET_free_non_null (session->vector);
697 GNUNET_free (session);
699 ///////////////////////////////////////////////////////////////////////////////
700 // Event and Message Handlers
701 ///////////////////////////////////////////////////////////////////////////////
705 * A client disconnected.
707 * Remove the associated session(s), release datastructures
708 * and cancel pending outgoing transmissions to the client.
709 * if the session has not yet completed, we also cancel Alice's request to Bob.
711 * @param cls closure, NULL
712 * @param client identification of the client
715 handle_client_disconnect (void *cls,
716 struct GNUNET_SERVER_Client *client)
718 struct ServiceSession *session;
722 session = GNUNET_SERVER_client_get_user_context (client, struct ServiceSession);
725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
726 _ ("Client (%p) disconnected from us.\n"), client);
727 GNUNET_CONTAINER_DLL_remove (from_client_head, from_client_tail, session);
729 if (!(session->role == BOB && session->state == FINALIZED))
731 //we MUST terminate any client message underway
732 if (session->service_transmit_handle && session->tunnel)
733 GNUNET_MESH_notify_transmit_ready_cancel (session->service_transmit_handle);
734 if (session->tunnel && session->state == WAITING_FOR_SERVICE_RESPONSE)
735 GNUNET_MESH_tunnel_destroy (session->tunnel);
737 if (GNUNET_SCHEDULER_NO_TASK != session->client_notification_task)
739 GNUNET_SCHEDULER_cancel (session->client_notification_task);
740 session->client_notification_task = GNUNET_SCHEDULER_NO_TASK;
742 if (GNUNET_SCHEDULER_NO_TASK != session->service_request_task)
744 GNUNET_SCHEDULER_cancel (session->service_request_task);
745 session->service_request_task = GNUNET_SCHEDULER_NO_TASK;
747 if (NULL != session->client_transmit_handle)
749 GNUNET_SERVER_notify_transmit_ready_cancel (session->client_transmit_handle);
750 session->client_transmit_handle = NULL;
752 free_session (session);
757 * Notify the client that the session has succeeded or failed completely.
758 * This message gets sent to
759 * * alice's client if bob disconnected or to
760 * * bob's client if the operation completed or alice disconnected
762 * @param client_session the associated client session
763 * @return GNUNET_NO, if we could not notify the client
764 * GNUNET_YES if we notified it.
767 prepare_client_end_notification (void * cls,
768 const struct GNUNET_SCHEDULER_TaskContext * tc)
770 struct ServiceSession * session = cls;
771 struct GNUNET_SCALARPRODUCT_client_response * msg;
773 session->client_notification_task = GNUNET_SCHEDULER_NO_TASK;
775 msg = GNUNET_new (struct GNUNET_SCALARPRODUCT_client_response);
776 msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_SERVICE_TO_CLIENT);
777 memcpy (&msg->key, &session->key, sizeof (struct GNUNET_HashCode));
778 memcpy (&msg->peer, &session->peer, sizeof ( struct GNUNET_PeerIdentity));
779 msg->header.size = htons (sizeof (struct GNUNET_SCALARPRODUCT_client_response));
780 // signal error if not signalized, positive result-range field but zero length.
781 msg->product_length = htonl (0);
782 msg->range = (session->state == FINALIZED) ? 0 : -1;
784 session->msg = &msg->header;
786 //transmit this message to our client
787 session->client_transmit_handle =
788 GNUNET_SERVER_notify_transmit_ready (session->client,
789 sizeof (struct GNUNET_SCALARPRODUCT_client_response),
790 GNUNET_TIME_UNIT_FOREVER_REL,
794 // if we could not even queue our request, something is wrong
795 if (NULL == session->client_transmit_handle)
797 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Could not send message to client (%p)!\n"), session->client);
798 // usually gets freed by do_send_message
803 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Sending session-end notification to client (%p) for session %s\n"), &session->client, GNUNET_h2s (&session->key));
810 * generates the response message to be sent to alice after computing
811 * the values (1), (2), S and S'
812 * (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)})$
813 * (2)[]: $E_A(a_{pi'(i)}) times E_A(- r_{pi'(i)}) &= E_A(a_{pi'(i)} - r_{pi'(i)})$
814 * S: $S := E_A(sum (r_i + b_i)^2)$
815 * S': $S' := E_A(sum r_i^2)$
817 * @param r (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)})$
818 * @param r_prime (2)[]: $E_A(a_{pi'(i)}) times E_A(- r_{pi'(i)}) &= E_A(a_{pi'(i)} - r_{pi'(i)})$
819 * @param s S: $S := E_A(sum (r_i + b_i)^2)$
820 * @param s_prime S': $S' := E_A(sum r_i^2)$
821 * @param request the associated requesting session with alice
822 * @param response the associated responder session with bob's client
823 * @return GNUNET_SYSERR if the function was called with NULL parameters or if there was an error
824 * GNUNET_NO if we could not send our message
825 * GNUNET_OK if the operation succeeded
828 prepare_service_response (gcry_mpi_t s,
830 struct ServiceSession * request,
831 struct ServiceSession * response)
833 struct GNUNET_SCALARPRODUCT_service_response * msg;
834 uint32_t msg_length = 0;
835 unsigned char * current = NULL;
836 unsigned char * element_exported = NULL;
837 size_t element_length = 0;
840 msg_length = sizeof (struct GNUNET_SCALARPRODUCT_service_response)
841 + 2 * PAILLIER_ELEMENT_LENGTH; // s, stick
843 if (GNUNET_SERVER_MAX_MESSAGE_SIZE > msg_length + 2 * request->used_element_count * PAILLIER_ELEMENT_LENGTH){ //kp, kq
844 msg_length += + 2 * request->used_element_count * PAILLIER_ELEMENT_LENGTH;
845 request->transferred_element_count = request->used_element_count;
848 request->transferred_element_count = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - msg_length) / (PAILLIER_ELEMENT_LENGTH * 2);
851 msg = GNUNET_malloc (msg_length);
853 msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_TO_ALICE);
854 msg->header.size = htons (msg_length);
855 msg->total_element_count = htonl (request->element_count);
856 msg->contained_element_count = htonl (request->used_element_count);
857 msg->contained_element_count = htonl (request->transferred_element_count);
858 memcpy (&msg->key, &request->key, sizeof (struct GNUNET_HashCode));
859 current = (unsigned char *) &msg[1];
861 element_exported = GNUNET_malloc (PAILLIER_ELEMENT_LENGTH);
862 // 4 times the same logics with slight variations.
863 // doesn't really justify having 2 functions for that
864 // so i put it into blocks to enhance readability
866 memset (element_exported, 0, PAILLIER_ELEMENT_LENGTH);
867 GNUNET_assert (0 == gcry_mpi_print (GCRYMPI_FMT_USG,
868 element_exported, PAILLIER_ELEMENT_LENGTH,
871 adjust (element_exported, element_length, PAILLIER_ELEMENT_LENGTH);
872 memcpy (current, element_exported, PAILLIER_ELEMENT_LENGTH);
873 current += PAILLIER_ELEMENT_LENGTH;
876 memset (element_exported, 0, PAILLIER_ELEMENT_LENGTH);
877 GNUNET_assert (0 == gcry_mpi_print (GCRYMPI_FMT_USG,
878 element_exported, PAILLIER_ELEMENT_LENGTH,
881 adjust (element_exported, element_length, PAILLIER_ELEMENT_LENGTH);
882 memcpy (current, element_exported, PAILLIER_ELEMENT_LENGTH);
883 current += PAILLIER_ELEMENT_LENGTH;
886 for (i = 0; i < request->used_element_count; i++)
888 if (request->transferred_element_count <= i)
889 break; //reached end of this message, can't include more
892 memset (element_exported, 0, PAILLIER_ELEMENT_LENGTH);
893 GNUNET_assert (0 == gcry_mpi_print (GCRYMPI_FMT_USG,
894 element_exported, PAILLIER_ELEMENT_LENGTH,
897 adjust (element_exported, element_length, PAILLIER_ELEMENT_LENGTH);
898 memcpy (current, element_exported, PAILLIER_ELEMENT_LENGTH);
899 current += PAILLIER_ELEMENT_LENGTH;
901 memset (element_exported, 0, PAILLIER_ELEMENT_LENGTH);
902 GNUNET_assert (0 == gcry_mpi_print (GCRYMPI_FMT_USG,
903 element_exported, PAILLIER_ELEMENT_LENGTH,
905 request->r_prime[i]));
906 adjust (element_exported, element_length, PAILLIER_ELEMENT_LENGTH);
907 memcpy (current, element_exported, PAILLIER_ELEMENT_LENGTH);
908 current += PAILLIER_ELEMENT_LENGTH;
911 GNUNET_free (element_exported);
912 for (i = 0; i < request->transferred_element_count; i++)
914 gcry_mpi_release (request->r_prime[i]);
915 gcry_mpi_release (request->r[i]);
917 gcry_mpi_release (s);
918 gcry_mpi_release (s_prime);
920 request->msg = (struct GNUNET_MessageHeader *) msg;
921 request->service_transmit_handle =
922 GNUNET_MESH_notify_transmit_ready (request->tunnel,
924 GNUNET_TIME_UNIT_FOREVER_REL,
928 //disconnect our client
929 if (NULL == request->service_transmit_handle)
931 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Could not send service-response message via mesh!)\n"));
932 request->state = FINALIZED;
934 response->client_notification_task =
935 GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
939 if (request->transferred_element_count != request->used_element_count)
941 request->state = WAITING_FOR_MULTIPART_TRANSMISSION;
944 request->state = FINALIZED;
953 * (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)})$
954 * (2)[]: $E_A(a_{\pi'(i)}) \otimes E_A(- r_{\pi'(i)}) &= E_A(a_{\pi'(i)} - r_{\pi'(i)})$
955 * S: $S := E_A(\sum (r_i + b_i)^2)$
956 * S': $S' := E_A(\sum r_i^2)$
958 * @param request the requesting session + bob's requesting peer
959 * @param response the responding session + bob's client handle
960 * @return GNUNET_SYSERR if the computation failed
961 * GNUNET_OK if everything went well.
964 compute_service_response (struct ServiceSession * request,
965 struct ServiceSession * response)
969 int ret = GNUNET_SYSERR;
973 gcry_mpi_t * rand = NULL;
974 gcry_mpi_t * r = NULL;
975 gcry_mpi_t * r_prime = NULL;
978 gcry_mpi_t * a_pi_prime;
980 gcry_mpi_t * rand_pi;
981 gcry_mpi_t * rand_pi_prime;
983 gcry_mpi_t s_prime = NULL;
984 gcry_mpi_t remote_n = NULL;
985 gcry_mpi_t remote_nsquare;
986 gcry_mpi_t remote_g = NULL;
990 count = request->used_element_count;
992 b = GNUNET_malloc (sizeof (gcry_mpi_t) * count);
993 a_pi = GNUNET_malloc (sizeof (gcry_mpi_t) * count);
994 b_pi = GNUNET_malloc (sizeof (gcry_mpi_t) * count);
995 a_pi_prime = GNUNET_malloc (sizeof (gcry_mpi_t) * count);
996 rand_pi = GNUNET_malloc (sizeof (gcry_mpi_t) * count);
997 rand_pi_prime = GNUNET_malloc (sizeof (gcry_mpi_t) * count);
999 // convert responder session to from long to mpi
1000 for (i = 0, j = 0; i < response->element_count && j < count; i++)
1002 if (request->mask[i / 8] & (1 << (i % 8)))
1004 value = response->vector[i] >= 0 ? response->vector[i] : -response->vector[i];
1005 // long to gcry_mpi_t
1006 if (0 > response->vector[i])
1008 b[j] = gcry_mpi_new (0);
1009 gcry_mpi_sub_ui (b[j], b[j], value);
1013 b[j] = gcry_mpi_set_ui (NULL, value);
1018 GNUNET_free (response->vector);
1019 response->vector = NULL;
1021 tmp_exp = gcry_sexp_find_token (request->remote_pubkey, "n", 0);
1024 GNUNET_break_op (0);
1025 gcry_sexp_release (request->remote_pubkey);
1026 request->remote_pubkey = NULL;
1029 remote_n = gcry_sexp_nth_mpi (tmp_exp, 1, GCRYMPI_FMT_USG);
1033 gcry_sexp_release (tmp_exp);
1036 remote_nsquare = gcry_mpi_new (KEYBITS + 1);
1037 gcry_mpi_mul (remote_nsquare, remote_n, remote_n);
1038 gcry_sexp_release (tmp_exp);
1039 tmp_exp = gcry_sexp_find_token (request->remote_pubkey, "g", 0);
1040 gcry_sexp_release (request->remote_pubkey);
1041 request->remote_pubkey = NULL;
1044 GNUNET_break_op (0);
1045 gcry_mpi_release (remote_n);
1048 remote_g = gcry_sexp_nth_mpi (tmp_exp, 1, GCRYMPI_FMT_USG);
1052 gcry_mpi_release (remote_n);
1053 gcry_sexp_release (tmp_exp);
1056 gcry_sexp_release (tmp_exp);
1058 // generate r, p and q
1059 rand = generate_random_vector (count);
1060 p = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, count);
1061 q = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, count);
1062 //initialize the result vectors
1063 r = initialize_mpi_vector (count);
1064 r_prime = initialize_mpi_vector (count);
1066 // copy the REFERNCES of a, b and r into aq and bq. we will not change
1067 // those values, thus we can work with the references
1068 memcpy (a_pi, request->a, sizeof (gcry_mpi_t) * count);
1069 memcpy (a_pi_prime, request->a, sizeof (gcry_mpi_t) * count);
1070 memcpy (b_pi, b, sizeof (gcry_mpi_t) * count);
1071 memcpy (rand_pi, rand, sizeof (gcry_mpi_t) * count);
1072 memcpy (rand_pi_prime, rand, sizeof (gcry_mpi_t) * count);
1074 // generate p and q permutations for a, b and r
1075 GNUNET_assert (permute_vector (a_pi, p, count));
1076 GNUNET_assert (permute_vector (b_pi, p, count));
1077 GNUNET_assert (permute_vector (rand_pi, p, count));
1078 GNUNET_assert (permute_vector (a_pi_prime, q, count));
1079 GNUNET_assert (permute_vector (rand_pi_prime, q, count));
1081 // encrypt the element
1082 // for the sake of readability I decided to have dedicated permutation
1083 // vectors, which get rid of all the lookups in p/q.
1084 // however, ap/aq are not absolutely necessary but are just abstraction
1085 // Calculate Kp = E(S + a_pi) (+) E(S - r_pi - b_pi)
1086 for (i = 0; i < count; i++)
1088 // E(S - r_pi - b_pi)
1089 gcry_mpi_sub (r[i], my_offset, rand_pi[i]);
1090 gcry_mpi_sub (r[i], r[i], b_pi[i]);
1091 encrypt_element (r[i], r[i], remote_g, remote_n, remote_nsquare);
1093 // E(S - r_pi - b_pi) * E(S + a_pi) == E(2*S + a - r - b)
1094 gcry_mpi_mulm (r[i], r[i], a_pi[i], remote_nsquare);
1098 GNUNET_free (rand_pi);
1100 // Calculate Kq = E(S + a_qi) (+) E(S - r_qi)
1101 for (i = 0; i < count; i++)
1104 gcry_mpi_sub (r_prime[i], my_offset, rand_pi_prime[i]);
1105 encrypt_element (r_prime[i], r_prime[i], remote_g, remote_n, remote_nsquare);
1107 // E(S - r_qi) * E(S + a_qi) == E(2*S + a_qi - r_qi)
1108 gcry_mpi_mulm (r_prime[i], r_prime[i], a_pi_prime[i], remote_nsquare);
1110 GNUNET_free (a_pi_prime);
1111 GNUNET_free (rand_pi_prime);
1114 request->r_prime = r_prime;
1116 // Calculate S' = E(SUM( r_i^2 ))
1117 s_prime = compute_square_sum (rand, count);
1118 encrypt_element (s_prime, s_prime, remote_g, remote_n, remote_nsquare);
1120 // Calculate S = E(SUM( (r_i + b_i)^2 ))
1121 for (i = 0; i < count; i++)
1123 gcry_mpi_add (rand[i], rand[i], b[i]);
1125 s = compute_square_sum (rand, count);
1126 encrypt_element (s, s, remote_g, remote_n, remote_nsquare);
1127 gcry_mpi_release (remote_n);
1128 gcry_mpi_release (remote_g);
1129 gcry_mpi_release (remote_nsquare);
1131 // release r and tmp
1132 for (i = 0; i < count; i++)
1133 // rp, rq, aq, ap, bp, bq are released along with a, r, b respectively, (a and b are handled at except:)
1134 gcry_mpi_release (rand[i]);
1136 // copy the r[], r_prime[], S and Stick into a new message, prepare_service_response frees these
1137 if (GNUNET_YES != prepare_service_response (s, s_prime, request, response))
1138 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Failed to communicate with `%s', scalar product calculation aborted.\n"),
1139 GNUNET_i2s (&request->peer));
1144 for (i = 0; i < count; i++)
1146 gcry_mpi_release (b[i]);
1147 gcry_mpi_release (request->a[i]);
1151 GNUNET_free (request->a);
1159 * Executed by Alice, fills in a service-request message and sends it to the given peer
1161 * @param session the session associated with this request, then also holds the CORE-handle
1162 * @return #GNUNET_SYSERR if we could not send the message
1163 * #GNUNET_NO if the message was too large
1164 * #GNUNET_OK if we sent it
1167 prepare_service_request (void *cls,
1168 const struct GNUNET_SCHEDULER_TaskContext *tc)
1170 struct ServiceSession * session = cls;
1171 unsigned char * current;
1172 unsigned char * element_exported;
1173 struct GNUNET_SCALARPRODUCT_service_request * msg;
1176 uint32_t msg_length;
1177 size_t element_length = 0; // initialized by gcry_mpi_print, but the compiler doesn't know that
1181 session->service_request_task = GNUNET_SCHEDULER_NO_TASK;
1183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _ ("Successfully created new tunnel to peer (%s)!\n"), GNUNET_i2s (&session->peer));
1185 msg_length = sizeof (struct GNUNET_SCALARPRODUCT_service_request)
1186 + session->mask_length
1187 + my_pubkey_external_length;
1189 if (GNUNET_SERVER_MAX_MESSAGE_SIZE > msg_length + session->used_element_count * PAILLIER_ELEMENT_LENGTH){
1190 msg_length += session->used_element_count * PAILLIER_ELEMENT_LENGTH;
1191 session->transferred_element_count = session->used_element_count;
1194 //create a multipart msg, first we calculate a new msg size for the head msg
1195 session->transferred_element_count = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - msg_length) / PAILLIER_ELEMENT_LENGTH;
1198 msg = GNUNET_malloc (msg_length);
1199 msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_TO_BOB);
1200 msg->total_element_count = htonl(session->used_element_count);
1201 msg->contained_element_count = htonl (session->transferred_element_count);
1202 memcpy (&msg->key, &session->key, sizeof (struct GNUNET_HashCode));
1203 msg->mask_length = htonl (session->mask_length);
1204 msg->pk_length = htonl (my_pubkey_external_length);
1205 msg->element_count = htonl (session->element_count);
1206 msg->header.size = htons (msg_length);
1208 // fill in the payload
1209 current = (unsigned char *) &msg[1];
1210 // copy over the mask
1211 memcpy (current, session->mask, session->mask_length);
1212 // copy over our public key
1213 current += session->mask_length;
1214 memcpy (current, my_pubkey_external, my_pubkey_external_length);
1215 current += my_pubkey_external_length;
1217 // now copy over the element vector
1218 element_exported = GNUNET_malloc (PAILLIER_ELEMENT_LENGTH);
1219 session->a = GNUNET_malloc (sizeof (gcry_mpi_t) * session->used_element_count);
1220 a = gcry_mpi_new (KEYBITS * 2);
1221 // encrypt our vector and generate string representations
1222 for (i = 0, j = 0; i < session->element_count; i++)
1224 // if this is a used element...
1225 if (session->mask[i / 8] & 1 << (i % 8))
1227 if (session->transferred_element_count <= j)
1228 break; //reached end of this message, can't include more
1230 memset(element_exported, 0, PAILLIER_ELEMENT_LENGTH);
1231 value = session->vector[i] >= 0 ? session->vector[i] : -session->vector[i];
1233 a = gcry_mpi_set_ui (a, 0);
1234 // long to gcry_mpi_t
1235 if (session->vector[i] < 0)
1236 gcry_mpi_sub_ui (a, a, value);
1238 gcry_mpi_add_ui (a, a, value);
1240 session->a[j++] = gcry_mpi_set (NULL, a);
1241 gcry_mpi_add (a, a, my_offset);
1242 encrypt_element (a, a, my_g, my_n, my_nsquare);
1244 // get representation as string
1245 // we always supply some value, so gcry_mpi_print fails only if it can't reserve memory
1246 GNUNET_assert (!gcry_mpi_print (GCRYMPI_FMT_USG,
1247 element_exported, PAILLIER_ELEMENT_LENGTH,
1251 // move buffer content to the end of the buffer so it can easily be read by libgcrypt. also this now has fixed size
1252 adjust (element_exported, element_length, PAILLIER_ELEMENT_LENGTH);
1254 // copy over to the message
1255 memcpy (current, element_exported, PAILLIER_ELEMENT_LENGTH);
1256 current += PAILLIER_ELEMENT_LENGTH;
1259 gcry_mpi_release (a);
1260 GNUNET_free(element_exported);
1262 session->msg = (struct GNUNET_MessageHeader *) msg;
1263 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Transmitting service request.\n"));
1265 //transmit via mesh messaging
1266 session->service_transmit_handle = GNUNET_MESH_notify_transmit_ready (session->tunnel, GNUNET_YES,
1267 GNUNET_TIME_UNIT_FOREVER_REL,
1271 if (!session->service_transmit_handle)
1273 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Could not send mutlicast message to tunnel!\n"));
1275 session->msg = NULL;
1276 session->client_notification_task =
1277 GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
1281 if (session->transferred_element_count != session->used_element_count)
1282 session->state = WAITING_FOR_MULTIPART_TRANSMISSION;
1284 //singlepart message
1285 session->state = WAITING_FOR_SERVICE_RESPONSE;
1290 * Handler for a client request message.
1291 * Can either be type A or B
1292 * A: request-initiation to compute a scalar product with a peer
1293 * B: response role, keep the values + session and wait for a matching session or process a waiting request
1295 * @param cls closure
1296 * @param client identification of the client
1297 * @param message the actual message
1300 handle_client_request (void *cls,
1301 struct GNUNET_SERVER_Client *client,
1302 const struct GNUNET_MessageHeader *message)
1304 const struct GNUNET_SCALARPRODUCT_client_request * msg = (const struct GNUNET_SCALARPRODUCT_client_request *) message;
1305 struct ServiceSession * session;
1306 uint32_t element_count;
1307 uint32_t mask_length;
1312 // only one concurrent session per client connection allowed, simplifies logics a lot...
1313 session = GNUNET_SERVER_client_get_user_context (client, struct ServiceSession);
1314 if ((NULL != session) && (session->state != FINALIZED))
1316 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1319 else if (NULL != session)
1321 // old session is already completed, clean it up
1322 GNUNET_CONTAINER_DLL_remove (from_client_head, from_client_tail, session);
1323 free_session (session);
1326 //we need at least a peer and one message id to compare
1327 if (sizeof (struct GNUNET_SCALARPRODUCT_client_request) > ntohs (msg->header.size))
1329 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1330 _ ("Too short message received from client!\n"));
1331 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1335 msg_type = ntohs (msg->header.type);
1336 element_count = ntohl (msg->element_count);
1337 mask_length = ntohl (msg->mask_length);
1339 //sanity check: is the message as long as the message_count fields suggests?
1340 if ((ntohs (msg->header.size) != (sizeof (struct GNUNET_SCALARPRODUCT_client_request) +element_count * sizeof (int32_t) + mask_length))
1341 || (0 == element_count))
1343 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1344 _ ("Invalid message received from client, session information incorrect!\n"));
1345 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1349 // do we have a duplicate session here already?
1350 if (NULL != find_matching_session (from_client_tail,
1355 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1356 _ ("Duplicate session information received, cannot create new session with key `%s'\n"),
1357 GNUNET_h2s (&msg->key));
1358 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1362 session = GNUNET_new (struct ServiceSession);
1363 session->service_request_task = GNUNET_SCHEDULER_NO_TASK;
1364 session->client_notification_task = GNUNET_SCHEDULER_NO_TASK;
1365 session->client = client;
1366 session->element_count = element_count;
1367 session->mask_length = mask_length;
1368 // get our transaction key
1369 memcpy (&session->key, &msg->key, sizeof (struct GNUNET_HashCode));
1370 //allocate memory for vector and encrypted vector
1371 session->vector = GNUNET_malloc (sizeof (int32_t) * element_count);
1372 vector = (int32_t *) & msg[1];
1374 if (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE == msg_type)
1376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1377 _ ("Got client-request-session with key %s, preparing tunnel to remote service.\n"),
1378 GNUNET_h2s (&session->key));
1380 session->role = ALICE;
1382 session->mask = GNUNET_malloc (mask_length);
1383 memcpy (session->mask, &vector[element_count], mask_length);
1385 // copy over the elements
1386 session->used_element_count = 0;
1387 for (i = 0; i < element_count; i++)
1389 session->vector[i] = ntohl (vector[i]);
1390 if (session->vector[i] == 0)
1391 session->mask[i / 8] &= ~(1 << (i % 8));
1392 if (session->mask[i / 8] & (1 << (i % 8)))
1393 session->used_element_count++;
1396 if (0 == session->used_element_count)
1398 GNUNET_break_op (0);
1399 GNUNET_free (session->vector);
1400 GNUNET_free (session);
1401 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1404 //session with ourself makes no sense!
1405 if (!memcmp (&msg->peer, &me, sizeof (struct GNUNET_PeerIdentity)))
1408 GNUNET_free (session->vector);
1409 GNUNET_free (session);
1410 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1414 memcpy (&session->peer, &msg->peer, sizeof (struct GNUNET_PeerIdentity));
1415 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1416 _ ("Creating new tunnel to for session with key %s.\n"),
1417 GNUNET_h2s (&session->key));
1418 session->tunnel = GNUNET_MESH_tunnel_create (my_mesh, session,
1420 GNUNET_APPLICATION_TYPE_SCALARPRODUCT,
1423 //prepare_service_request, tunnel_peer_disconnect_handler,
1424 if (!session->tunnel)
1427 GNUNET_free (session->vector);
1428 GNUNET_free (session);
1429 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1432 GNUNET_SERVER_client_set_user_context (client, session);
1433 GNUNET_CONTAINER_DLL_insert (from_client_head, from_client_tail, session);
1435 session->state = CLIENT_REQUEST_RECEIVED;
1436 session->service_request_task =
1437 GNUNET_SCHEDULER_add_now (&prepare_service_request,
1443 struct ServiceSession * requesting_session;
1444 enum SessionState needed_state = SERVICE_REQUEST_RECEIVED;
1446 session->role = BOB;
1447 session->mask = NULL;
1448 // copy over the elements
1449 session->used_element_count = element_count;
1450 for (i = 0; i < element_count; i++)
1451 session->vector[i] = ntohl (vector[i]);
1452 session->state = CLIENT_RESPONSE_RECEIVED;
1454 GNUNET_SERVER_client_set_user_context (client, session);
1455 GNUNET_CONTAINER_DLL_insert (from_client_head, from_client_tail, session);
1457 //check if service queue contains a matching request
1458 requesting_session = find_matching_session (from_service_tail,
1460 session->element_count,
1461 &needed_state, NULL);
1462 if (NULL != requesting_session)
1464 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Got client-responder-session with key %s and a matching service-request-session set, processing.\n"), GNUNET_h2s (&session->key));
1465 if (GNUNET_OK != compute_service_response (requesting_session, session))
1466 session->client_notification_task =
1467 GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
1473 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Got client-responder-session with key %s but NO matching service-request-session set, queuing element for later use.\n"), GNUNET_h2s (&session->key));
1474 // no matching session exists yet, store the response
1475 // for later processing by handle_service_request()
1478 GNUNET_SERVER_receive_done (client, GNUNET_YES);
1483 * Function called for inbound tunnels.
1485 * @param cls closure
1486 * @param tunnel new handle to the tunnel
1487 * @param initiator peer that started the tunnel
1488 * @param atsi performance information for the tunnel
1489 * @return initial tunnel context for the tunnel
1490 * (can be NULL -- that's not an error)
1493 tunnel_incoming_handler (void *cls,
1494 struct GNUNET_MESH_Tunnel *tunnel,
1495 const struct GNUNET_PeerIdentity *initiator,
1498 struct ServiceSession * c = GNUNET_new (struct ServiceSession);
1500 c->peer = *initiator;
1503 c->state = WAITING_FOR_SERVICE_REQUEST;
1509 * Function called whenever a tunnel is destroyed. Should clean up
1510 * any associated state.
1512 * It must NOT call GNUNET_MESH_tunnel_destroy on the tunnel.
1514 * @param cls closure (set from GNUNET_MESH_connect)
1515 * @param tunnel connection to the other end (henceforth invalid)
1516 * @param tunnel_ctx place where local state associated
1517 * with the tunnel is stored
1520 tunnel_destruction_handler (void *cls,
1521 const struct GNUNET_MESH_Tunnel *tunnel,
1524 struct ServiceSession * session = tunnel_ctx;
1525 struct ServiceSession * client_session;
1526 struct ServiceSession * curr;
1528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1529 _ ("Peer disconnected, terminating session %s with peer (%s)\n"),
1530 GNUNET_h2s (&session->key),
1531 GNUNET_i2s (&session->peer));
1532 if (ALICE == session->role)
1534 // as we have only one peer connected in each session, just remove the session
1536 if ((SERVICE_RESPONSE_RECEIVED > session->state) && (!do_shutdown))
1538 session->tunnel = NULL;
1539 // if this happened before we received the answer, we must terminate the session
1540 session->client_notification_task =
1541 GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
1546 { //(BOB == session->role) service session
1547 // remove the session, unless it has already been dequeued, but somehow still active
1548 // this could bug without the IF in case the queue is empty and the service session was the only one know to the service
1549 // scenario: disconnect before alice can send her message to bob.
1550 for (curr = from_service_head; NULL != curr; curr = curr->next)
1551 if (curr == session)
1553 GNUNET_CONTAINER_DLL_remove (from_service_head, from_service_tail, curr);
1556 // there is a client waiting for this service session, terminate it, too!
1557 // i assume the tupel of key and element count is unique. if it was not the rest of the code would not work either.
1558 client_session = find_matching_session (from_client_tail,
1560 session->element_count,
1562 free_session (session);
1564 // the client has to check if it was waiting for a result
1565 // or if it was a responder, no point in adding more statefulness
1566 if (client_session && (!do_shutdown))
1568 client_session->state = FINALIZED;
1569 client_session->client_notification_task =
1570 GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
1578 * Compute our scalar product, done by Alice
1580 * @param session - the session associated with this computation
1581 * @param kp - (1) from the protocol definition:
1582 * $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)})$
1583 * @param kq - (2) from the protocol definition:
1584 * $E_A(a_{\pi'(i)}) \otimes E_A(- r_{\pi'(i)}) &= E_A(a_{\pi'(i)} - r_{\pi'(i)})$
1585 * @param s - S from the protocol definition:
1586 * $S := E_A(\sum (r_i + b_i)^2)$
1587 * @param stick - S' from the protocol definition:
1588 * $S' := E_A(\sum r_i^2)$
1589 * @return product as MPI, never NULL
1592 compute_scalar_product (struct ServiceSession * session,
1593 gcry_mpi_t * r, gcry_mpi_t * r_prime, gcry_mpi_t s, gcry_mpi_t s_prime)
1604 count = session->used_element_count;
1605 tmp = gcry_mpi_new (KEYBITS);
1606 // due to the introduced static offset S, we now also have to remove this
1607 // from the E(a_pi)(+)E(-b_pi-r_pi) and E(a_qi)(+)E(-r_qi) twice each,
1608 // the result is E((S + a_pi) + (S -b_pi-r_pi)) and E(S + a_qi + S - r_qi)
1609 for (i = 0; i < count; i++)
1611 decrypt_element (r[i], r[i], my_mu, my_lambda, my_n, my_nsquare);
1612 gcry_mpi_sub (r[i], r[i], my_offset);
1613 gcry_mpi_sub (r[i], r[i], my_offset);
1614 decrypt_element (r_prime[i], r_prime[i], my_mu, my_lambda, my_n, my_nsquare);
1615 gcry_mpi_sub (r_prime[i], r_prime[i], my_offset);
1616 gcry_mpi_sub (r_prime[i], r_prime[i], my_offset);
1619 // calculate t = sum(ai)
1620 t = compute_square_sum (session->a, count);
1623 u = gcry_mpi_new (0);
1624 tmp = compute_square_sum (r, count);
1625 gcry_mpi_sub (u, u, tmp);
1626 gcry_mpi_release (tmp);
1629 utick = gcry_mpi_new (0);
1630 tmp = compute_square_sum (r_prime, count);
1631 gcry_mpi_sub (utick, utick, tmp);
1633 GNUNET_assert (p = gcry_mpi_new (0));
1634 GNUNET_assert (ptick = gcry_mpi_new (0));
1637 decrypt_element (s, s, my_mu, my_lambda, my_n, my_nsquare);
1638 decrypt_element (s_prime, s_prime, my_mu, my_lambda, my_n, my_nsquare);
1641 gcry_mpi_add (p, s, t);
1642 gcry_mpi_add (p, p, u);
1645 gcry_mpi_add (ptick, s_prime, t);
1646 gcry_mpi_add (ptick, ptick, utick);
1648 gcry_mpi_release (t);
1649 gcry_mpi_release (u);
1650 gcry_mpi_release (utick);
1653 gcry_mpi_sub (p, p, ptick);
1654 gcry_mpi_release (ptick);
1655 tmp = gcry_mpi_set_ui (tmp, 2);
1656 gcry_mpi_div (p, NULL, p, tmp, 0);
1658 gcry_mpi_release (tmp);
1659 for (i = 0; i < count; i++)
1660 gcry_mpi_release (session->a[i]);
1661 GNUNET_free (session->a);
1669 * prepare the response we will send to alice or bobs' clients.
1670 * in Bobs case the product will be NULL.
1672 * @param session the session associated with our client.
1675 prepare_client_response (void *cls,
1676 const struct GNUNET_SCHEDULER_TaskContext *tc)
1678 struct ServiceSession * session = cls;
1679 struct GNUNET_SCALARPRODUCT_client_response * msg;
1680 unsigned char * product_exported = NULL;
1681 size_t product_length = 0;
1682 uint32_t msg_length = 0;
1687 session->client_notification_task = GNUNET_SCHEDULER_NO_TASK;
1689 if (session->product)
1691 gcry_mpi_t value = gcry_mpi_new (0);
1693 sign = gcry_mpi_cmp_ui (session->product, 0);
1694 // libgcrypt can not handle a print of a negative number
1695 // if (a->sign) return gcry_error (GPG_ERR_INTERNAL); /* Can't handle it yet. */
1698 gcry_mpi_sub (value, value, session->product);
1703 gcry_mpi_add (value, value, session->product);
1708 gcry_mpi_release (session->product);
1709 session->product = NULL;
1711 // get representation as string
1713 && (0 != (rc = gcry_mpi_aprint (GCRYMPI_FMT_STD,
1718 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
1720 range = -1; // signal error with product-length = 0 and range = -1
1722 gcry_mpi_release (value);
1725 msg_length = sizeof (struct GNUNET_SCALARPRODUCT_client_response) +product_length;
1726 msg = GNUNET_malloc (msg_length);
1727 memcpy (&msg->key, &session->key, sizeof (struct GNUNET_HashCode));
1728 memcpy (&msg->peer, &session->peer, sizeof ( struct GNUNET_PeerIdentity));
1729 if (product_exported != NULL)
1731 memcpy (&msg[1], product_exported, product_length);
1732 GNUNET_free (product_exported);
1734 msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_SERVICE_TO_CLIENT);
1735 msg->header.size = htons (msg_length);
1737 msg->product_length = htonl (product_length);
1739 session->msg = (struct GNUNET_MessageHeader *) msg;
1740 //transmit this message to our client
1741 session->client_transmit_handle =
1742 GNUNET_SERVER_notify_transmit_ready (session->client,
1744 GNUNET_TIME_UNIT_FOREVER_REL,
1747 if (NULL == session->client_transmit_handle)
1749 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1750 _ ("Could not send message to client (%p)!\n"),
1752 session->client = NULL;
1753 // callback was not called!
1755 session->msg = NULL;
1758 // gracefully sent message, just terminate session structure
1759 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1760 _ ("Sent result to client (%p), this session (%s) has ended!\n"),
1762 GNUNET_h2s (&session->key));
1767 * Handle a request from another service to calculate a scalarproduct with us.
1769 * @param cls closure (set from #GNUNET_MESH_connect)
1770 * @param tunnel connection to the other end
1771 * @param tunnel_ctx place to store local state associated with the tunnel
1772 * @param sender who sent the message
1773 * @param message the actual message
1774 * @param atsi performance data for the connection
1775 * @return #GNUNET_OK to keep the connection open,
1776 * #GNUNET_SYSERR to close it (signal serious error)
1779 handle_service_request (void *cls,
1780 struct GNUNET_MESH_Tunnel * tunnel,
1782 const struct GNUNET_MessageHeader * message)
1784 struct ServiceSession * session;
1785 const struct GNUNET_SCALARPRODUCT_service_request * msg = (const struct GNUNET_SCALARPRODUCT_service_request *) message;
1786 uint32_t mask_length;
1788 uint32_t used_elements;
1789 uint32_t element_count;
1790 uint32_t msg_length;
1791 unsigned char * current;
1792 struct ServiceSession * responder_session;
1794 enum SessionState needed_state;
1796 session = (struct ServiceSession *) * tunnel_ctx;
1797 if (BOB != session->role)
1799 GNUNET_break_op (0);
1800 return GNUNET_SYSERR;
1802 // is this tunnel already in use?
1803 if ((session->next) || (from_service_head == session))
1805 GNUNET_break_op (0);
1806 return GNUNET_SYSERR;
1808 // Check if message was sent by me, which would be bad!
1809 if (!memcmp (&session->peer, &me, sizeof (struct GNUNET_PeerIdentity)))
1811 GNUNET_free (session);
1813 return GNUNET_SYSERR;
1816 //we need at least a peer and one message id to compare
1817 if (ntohs (msg->header.size) < sizeof (struct GNUNET_SCALARPRODUCT_service_request))
1819 GNUNET_free (session);
1820 GNUNET_break_op (0);
1821 return GNUNET_SYSERR;
1823 mask_length = ntohl (msg->mask_length);
1824 pk_length = ntohl (msg->pk_length);
1825 used_elements = ntohl (msg->contained_element_count);
1826 element_count = ntohl (msg->element_count);
1827 msg_length = sizeof (struct GNUNET_SCALARPRODUCT_service_request)
1828 +mask_length + pk_length + used_elements * PAILLIER_ELEMENT_LENGTH;
1830 //sanity check: is the message as long as the message_count fields suggests?
1831 if ((ntohs (msg->header.size) != msg_length) || (element_count < used_elements)
1832 || (used_elements == 0) || (mask_length != (element_count / 8 + (element_count % 8 ? 1 : 0)))
1835 GNUNET_free (session);
1836 GNUNET_break_op (0);
1837 return GNUNET_SYSERR;
1839 if (find_matching_session (from_service_tail,
1845 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Got message with duplicate session key (`%s'), ignoring service request.\n"), (const char *) &(msg->key));
1846 GNUNET_free (session);
1847 return GNUNET_SYSERR;
1850 memcpy (&session->peer, &session->peer, sizeof (struct GNUNET_PeerIdentity));
1851 session->state = SERVICE_REQUEST_RECEIVED;
1852 session->element_count = ntohl (msg->element_count);
1853 session->used_element_count = used_elements;
1854 session->tunnel = tunnel;
1857 memcpy (&session->key, &msg->key, sizeof (struct GNUNET_HashCode));
1858 current = (unsigned char *) &msg[1];
1859 //preserve the mask, we will need that later on
1860 session->mask = GNUNET_malloc (mask_length);
1861 memcpy (session->mask, current, mask_length);
1863 current += mask_length;
1865 //convert the publickey to sexp
1866 if (gcry_sexp_new (&session->remote_pubkey, current, pk_length, 1))
1868 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Could not translate remote public key to sexpression!\n"));
1869 GNUNET_free (session->mask);
1870 GNUNET_free (session);
1871 return GNUNET_SYSERR;
1874 current += pk_length;
1876 //check if service queue contains a matching request
1877 needed_state = CLIENT_RESPONSE_RECEIVED;
1878 responder_session = find_matching_session (from_client_tail,
1880 session->element_count,
1881 &needed_state, NULL);
1883 session->a = GNUNET_malloc (sizeof (gcry_mpi_t) * used_elements);
1885 if (GNUNET_SERVER_MAX_MESSAGE_SIZE >= sizeof (struct GNUNET_SCALARPRODUCT_service_request)
1888 + used_elements * PAILLIER_ELEMENT_LENGTH)
1890 gcry_error_t ret = 0;
1891 session->a = GNUNET_malloc (sizeof (gcry_mpi_t) * used_elements);
1892 // Convert each vector element to MPI_value
1893 for (i = 0; i < used_elements; i++)
1897 ret = gcry_mpi_scan (&session->a[i],
1899 ¤t[i * PAILLIER_ELEMENT_LENGTH],
1900 PAILLIER_ELEMENT_LENGTH,
1904 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Could not translate E[a%d] to MPI!\n%s/%s\n"),
1905 i, gcry_strsource (ret), gcry_strerror (ret));
1909 GNUNET_CONTAINER_DLL_insert (from_service_head, from_service_tail, session);
1910 if (responder_session)
1912 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Got session with key %s and a matching element set, processing.\n"), GNUNET_h2s (&session->key));
1913 if (GNUNET_OK != compute_service_response (session, responder_session))
1915 //something went wrong, remove it again...
1916 GNUNET_CONTAINER_DLL_remove (from_service_head, from_service_tail, session);
1921 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Got session with key %s without a matching element set, queueing.\n"), GNUNET_h2s (&session->key));
1927 // TODO FEATURE: fallback to fragmentation, in case the message is too long
1928 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Message too large, fragmentation is currently not supported!\n"));
1932 for (i = 0; i < used_elements; i++)
1934 gcry_mpi_release (session->a[i]);
1935 gcry_sexp_release (session->remote_pubkey);
1936 session->remote_pubkey = NULL;
1937 GNUNET_free_non_null (session->a);
1939 free_session (session);
1940 // and notify our client-session that we could not complete the session
1941 if (responder_session)
1942 // we just found the responder session in this queue
1943 responder_session->client_notification_task =
1944 GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
1946 return GNUNET_SYSERR;
1951 * Handle a response we got from another service we wanted to calculate a scalarproduct with.
1953 * @param cls closure (set from #GNUNET_MESH_connect)
1954 * @param tunnel connection to the other end
1955 * @param tunnel_ctx place to store local state associated with the tunnel
1956 * @param sender who sent the message
1957 * @param message the actual message
1958 * @param atsi performance data for the connection
1959 * @return #GNUNET_OK to keep the connection open,
1960 * #GNUNET_SYSERR to close it (we are done)
1963 handle_service_response (void *cls,
1964 struct GNUNET_MESH_Tunnel * tunnel,
1966 const struct GNUNET_MessageHeader * message)
1968 struct ServiceSession * session;
1969 const struct GNUNET_SCALARPRODUCT_service_response * msg = (const struct GNUNET_SCALARPRODUCT_service_response *) message;
1970 unsigned char * current;
1972 gcry_mpi_t s = NULL;
1973 gcry_mpi_t s_prime = NULL;
1976 uint32_t contained_element_count;
1978 gcry_mpi_t * r = NULL;
1979 gcry_mpi_t * r_prime = NULL;
1982 GNUNET_assert (NULL != message);
1983 session = (struct ServiceSession *) * tunnel_ctx;
1984 if (ALICE != session->role)
1986 GNUNET_break_op (0);
1987 return GNUNET_SYSERR;
1990 count = session->used_element_count;
1991 session->product = NULL;
1992 session->state = SERVICE_RESPONSE_RECEIVED;
1994 //we need at least a peer and one message id to compare
1995 if (sizeof (struct GNUNET_SCALARPRODUCT_service_response) > ntohs (msg->header.size))
1997 GNUNET_break_op (0);
2000 contained_element_count = ntohl (msg->contained_element_count);
2001 msg_size = sizeof (struct GNUNET_SCALARPRODUCT_service_response)
2002 + 2 * contained_element_count * PAILLIER_ELEMENT_LENGTH
2003 + 2 * PAILLIER_ELEMENT_LENGTH;
2004 //sanity check: is the message as long as the message_count fields suggests?
2005 if ((ntohs (msg->header.size) != msg_size) || (count != contained_element_count))
2007 GNUNET_break_op (0);
2012 current = (unsigned char *) &msg[1];
2013 if (0 != (rc = gcry_mpi_scan (&s, GCRYMPI_FMT_USG, current,
2014 PAILLIER_ELEMENT_LENGTH, &read)))
2016 LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc);
2017 GNUNET_break_op (0);
2020 current += PAILLIER_ELEMENT_LENGTH;
2022 if (0 != (rc = gcry_mpi_scan (&s_prime, GCRYMPI_FMT_USG, current,
2023 PAILLIER_ELEMENT_LENGTH, &read)))
2025 LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc);
2026 GNUNET_break_op (0);
2029 current += PAILLIER_ELEMENT_LENGTH;
2031 r = GNUNET_malloc (sizeof (gcry_mpi_t) * count);
2032 // Convert each kp[] to its MPI_value
2033 for (i = 0; i < count; i++)
2035 if (0 != (rc = gcry_mpi_scan (&r[i], GCRYMPI_FMT_USG, current,
2036 PAILLIER_ELEMENT_LENGTH, &read)))
2038 LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc);
2039 GNUNET_break_op (0);
2042 current += PAILLIER_ELEMENT_LENGTH;
2046 r_prime = GNUNET_malloc (sizeof (gcry_mpi_t) * count);
2047 // Convert each kq[] to its MPI_value
2048 for (i = 0; i < count; i++)
2050 if (0 != (rc = gcry_mpi_scan (&r_prime[i], GCRYMPI_FMT_USG, current,
2051 PAILLIER_ELEMENT_LENGTH, &read)))
2053 LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc);
2054 GNUNET_break_op (0);
2057 current += PAILLIER_ELEMENT_LENGTH;
2059 session->product = compute_scalar_product (session, r, r_prime, s, s_prime);
2063 gcry_mpi_release (s);
2065 gcry_mpi_release (s_prime);
2066 for (i = 0; r && i < count; i++)
2067 if (r[i]) gcry_mpi_release (r[i]);
2068 for (i = 0; r_prime && i < count; i++)
2069 if (r_prime[i]) gcry_mpi_release (r_prime[i]);
2070 GNUNET_free_non_null (r);
2071 GNUNET_free_non_null (r_prime);
2073 session->tunnel = NULL;
2074 // send message with product to client
2075 session->client_notification_task =
2076 GNUNET_SCHEDULER_add_now (&prepare_client_response,
2078 // the tunnel has done its job, terminate our connection and the tunnel
2079 // the peer will be notified that the tunnel was destroyed via tunnel_destruction_handler
2080 // just close the connection, as recommended by Christian
2081 return GNUNET_SYSERR;
2086 * Task run during shutdown.
2092 shutdown_task (void *cls,
2093 const struct GNUNET_SCHEDULER_TaskContext *tc)
2095 struct ServiceSession * session;
2096 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Shutting down, initiating cleanup.\n"));
2098 do_shutdown = GNUNET_YES;
2100 // terminate all owned open tunnels.
2101 for (session = from_client_head; NULL != session; session = session->next)
2103 if ((FINALIZED != session->state) && (NULL != session->tunnel)){
2104 GNUNET_MESH_tunnel_destroy (session->tunnel);
2105 session->tunnel = NULL;
2107 if (GNUNET_SCHEDULER_NO_TASK != session->client_notification_task)
2109 GNUNET_SCHEDULER_cancel (session->client_notification_task);
2110 session->client_notification_task = GNUNET_SCHEDULER_NO_TASK;
2112 if (GNUNET_SCHEDULER_NO_TASK != session->service_request_task)
2114 GNUNET_SCHEDULER_cancel (session->service_request_task);
2115 session->service_request_task = GNUNET_SCHEDULER_NO_TASK;
2117 if (NULL != session->client)
2119 GNUNET_SERVER_client_disconnect (session->client);
2120 session->client = NULL;
2123 for (session = from_service_head; NULL != session; session = session->next)
2124 if (NULL != session->tunnel){
2125 GNUNET_MESH_tunnel_destroy (session->tunnel);
2126 session->tunnel = NULL;
2131 GNUNET_MESH_disconnect (my_mesh);
2138 * Initialization of the program and message handlers
2140 * @param cls closure
2141 * @param server the initialized server
2142 * @param c configuration to use
2146 struct GNUNET_SERVER_Handle *server,
2147 const struct GNUNET_CONFIGURATION_Handle *c)
2149 static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
2150 {&handle_client_request, NULL, GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE, 0},
2151 {&handle_client_request, NULL, GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_BOB, 0},
2154 static const struct GNUNET_MESH_MessageHandler mesh_handlers[] = {
2155 { &handle_service_request, GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_TO_BOB, 0},
2156 { &handle_service_request_multipart, GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_TO_BOB_MULTIPART, 0},
2157 { &handle_service_response, GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_TO_ALICE, 0},
2158 { &handle_service_response_multipart, GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_TO_ALICE_MULTIPART, 0},
2161 static const uint32_t ports[] = {
2162 GNUNET_APPLICATION_TYPE_SCALARPRODUCT,
2165 //generate private/public key set
2166 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Generating Paillier-Keyset.\n"));
2168 // register server callbacks and disconnect handler
2169 GNUNET_SERVER_add_handlers (server, server_handlers);
2170 GNUNET_SERVER_disconnect_notify (server,
2171 &handle_client_disconnect,
2173 GNUNET_break (GNUNET_OK ==
2174 GNUNET_CRYPTO_get_peer_identity (c,
2176 my_mesh = GNUNET_MESH_connect (c, NULL,
2177 &tunnel_incoming_handler,
2178 &tunnel_destruction_handler,
2179 mesh_handlers, ports);
2182 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Connect to MESH failed\n"));
2183 GNUNET_SCHEDULER_shutdown ();
2186 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Mesh initialized\n"));
2187 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2194 * The main function for the scalarproduct service.
2196 * @param argc number of arguments from the command line
2197 * @param argv command line arguments
2198 * @return 0 ok, 1 on error
2201 main (int argc, char *const *argv)
2203 return (GNUNET_OK ==
2204 GNUNET_SERVICE_run (argc, argv,
2206 GNUNET_SERVICE_OPTION_NONE,
2207 &run, NULL)) ? 0 : 1;
2210 /* end of gnunet-service-ext.c */