aboutsummaryrefslogtreecommitdiff
path: root/src/scalarproduct
diff options
context:
space:
mode:
authorChristian Fuchs <christian.fuchs@cfuchs.net>2013-10-14 12:24:15 +0000
committerChristian Fuchs <christian.fuchs@cfuchs.net>2013-10-14 12:24:15 +0000
commita806156660a4212ad2a0df4bf9cb9982e23b798a (patch)
treeff8772a0d8b3937c95a339710688effe0d89732b /src/scalarproduct
parent7b73cd5a99f6a432fca0d9d0c6e8f20630e2c302 (diff)
downloadgnunet-a806156660a4212ad2a0df4bf9cb9982e23b798a.tar.gz
gnunet-a806156660a4212ad2a0df4bf9cb9982e23b798a.zip
finished multipart message reworking
couple of enhancements to formatting updated half the function comments inlined/removed generate_random_vector, as it was only called from one source
Diffstat (limited to 'src/scalarproduct')
-rw-r--r--src/scalarproduct/gnunet-service-scalarproduct.c158
1 files changed, 90 insertions, 68 deletions
diff --git a/src/scalarproduct/gnunet-service-scalarproduct.c b/src/scalarproduct/gnunet-service-scalarproduct.c
index f85667874..c767ffb86 100644
--- a/src/scalarproduct/gnunet-service-scalarproduct.c
+++ b/src/scalarproduct/gnunet-service-scalarproduct.c
@@ -216,6 +216,32 @@ struct ServiceSession
216}; 216};
217 217
218/////////////////////////////////////////////////////////////////////////////// 218///////////////////////////////////////////////////////////////////////////////
219// Forward Delcarations
220///////////////////////////////////////////////////////////////////////////////
221
222/**
223 * Send a multi part chunk of a service request from alice to bob.
224 * This element only contains a part of the elements-vector (session->a[]),
225 * mask and public key set have to be contained within the first message
226 *
227 * This allows a ~32kbit key length while using 32000 elements or 62000 elements per request.
228 *
229 * @param cls the associated service session
230 */
231static void
232prepare_service_request_multipart (void *cls);
233
234/**
235 * Send a multi part chunk of a service response from bob to alice.
236 * This element only contains the two permutations of R, R'.
237 *
238 * @param cls the associated service session
239 */
240static void
241prepare_service_response_multipart (void *cls);
242
243
244///////////////////////////////////////////////////////////////////////////////
219// Global Variables 245// Global Variables
220/////////////////////////////////////////////////////////////////////////////// 246///////////////////////////////////////////////////////////////////////////////
221 247
@@ -306,6 +332,7 @@ static int do_shutdown;
306// Helper Functions 332// Helper Functions
307/////////////////////////////////////////////////////////////////////////////// 333///////////////////////////////////////////////////////////////////////////////
308 334
335
309/** 336/**
310 * Generates an Paillier private/public keyset and extracts the values using libgrcypt only 337 * Generates an Paillier private/public keyset and extracts the values using libgrcypt only
311 */ 338 */
@@ -420,6 +447,7 @@ generate_keyset ()
420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _ ("Generated key set with key length %d bits.\n"), KEYBITS); 447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _ ("Generated key set with key length %d bits.\n"), KEYBITS);
421} 448}
422 449
450
423/** 451/**
424 * If target != size, move target bytes to the 452 * If target != size, move target bytes to the
425 * end of the size-sized buffer and zero out the 453 * end of the size-sized buffer and zero out the
@@ -438,8 +466,9 @@ adjust (unsigned char *buf, size_t size, size_t target)
438 } 466 }
439} 467}
440 468
469
441/** 470/**
442 * encrypts an element using the paillier crypto system 471 * Encrypts an element using the paillier crypto system
443 * 472 *
444 * @param c ciphertext (output) 473 * @param c ciphertext (output)
445 * @param m plaintext 474 * @param m plaintext
@@ -466,6 +495,7 @@ encrypt_element (gcry_mpi_t c, gcry_mpi_t m, gcry_mpi_t g, gcry_mpi_t n, gcry_mp
466 gcry_mpi_release (tmp); 495 gcry_mpi_release (tmp);
467} 496}
468 497
498
469/** 499/**
470 * decrypts an element using the paillier crypto system 500 * decrypts an element using the paillier crypto system
471 * 501 *
@@ -485,6 +515,7 @@ decrypt_element (gcry_mpi_t m, gcry_mpi_t c, gcry_mpi_t mu, gcry_mpi_t lambda, g
485 gcry_mpi_mulm (m, m, mu, n); 515 gcry_mpi_mulm (m, m, mu, n);
486} 516}
487 517
518
488/** 519/**
489 * computes the square sum over a vector of a given length. 520 * computes the square sum over a vector of a given length.
490 * 521 *
@@ -512,15 +543,6 @@ compute_square_sum (gcry_mpi_t * vector, uint32_t length)
512 return sum; 543 return sum;
513} 544}
514 545
515
516static void
517prepare_service_request_multipart (void *cls,
518 const struct GNUNET_SCHEDULER_TaskContext *tc);
519static void
520prepare_service_response_multipart (void *cls,
521 const struct GNUNET_SCHEDULER_TaskContext *tc);
522
523
524/** 546/**
525 * Primitive callback for copying over a message, as they 547 * Primitive callback for copying over a message, as they
526 * usually are too complex to be handled in the callback itself. 548 * usually are too complex to be handled in the callback itself.
@@ -559,24 +581,23 @@ do_send_message (void *cls, size_t size, void *buf)
559 session->state = FINALIZED; 581 session->state = FINALIZED;
560 session->client_transmit_handle = NULL; 582 session->client_transmit_handle = NULL;
561 break; 583 break;
584
562 case GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_TO_BOB: 585 case GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_TO_BOB:
563 case GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_TO_BOB_MULTIPART: 586 case GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_TO_BOB_MULTIPART:
564 //else
565 session->service_transmit_handle = NULL; 587 session->service_transmit_handle = NULL;
566 // reset flags for sending 588 if (session->state == WAITING_FOR_MULTIPART_TRANSMISSION)
567 if ((session->state != WAITING_FOR_MULTIPART_TRANSMISSION) && (session->used != session->transferred)) 589 prepare_service_request_multipart (session);
568 prepare_service_request_multipart (session, NULL);
569 //TODO we have sent a message and now need to trigger trigger the next multipart message sending
570 break; 590 break;
591
571 case GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_TO_ALICE: 592 case GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_TO_ALICE:
572 case GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_TO_ALICE_MULTIPART: 593 case GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_TO_ALICE_MULTIPART:
573 //else
574 session->service_transmit_handle = NULL; 594 session->service_transmit_handle = NULL;
575 if ((session->state != WAITING_FOR_MULTIPART_TRANSMISSION) && (session->used != session->transferred)) 595 if (session->state == WAITING_FOR_MULTIPART_TRANSMISSION)
576 prepare_service_response_multipart (session, NULL); 596 prepare_service_response_multipart (session);
577 break; 597 break;
598
578 default: 599 default:
579 session->service_transmit_handle = NULL; 600 GNUNET_assert(0);
580 } 601 }
581 602
582 return size; 603 return size;
@@ -627,43 +648,17 @@ permute_vector (gcry_mpi_t * vector,
627 return vector; 648 return vector;
628} 649}
629 650
630/**
631 * Populate a vector with random integer values and convert them to
632 *
633 * @param length the length of the vector we must generate
634 * @return an array of MPI values with random values
635 */
636static gcry_mpi_t *
637generate_random_vector (uint32_t length)
638{
639 gcry_mpi_t * random_vector;
640 int32_t value;
641 uint32_t i;
642
643 random_vector = initialize_mpi_vector (length);
644 for (i = 0; i < length; i++) {
645 value = (int32_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
646
647 // long to gcry_mpi_t
648 if (value < 0)
649 gcry_mpi_sub_ui (random_vector[i],
650 random_vector[i],
651 -value);
652 else
653 random_vector[i] = gcry_mpi_set_ui (random_vector[i], value);
654 }
655
656 return random_vector;
657}
658 651
659/** 652/**
660 * Finds a not terminated client/service session in the 653 * Finds a not terminated client/service session in the
661 * given DLL based on session key, element count and state. 654 * given DLL based on session key, element count and state.
662 * 655 *
663 * @param tail - the tail of the DLL 656 * @param tail - the tail of the DLL
664 * @param my - the session to compare it to 657 * @param key - the key we want to search for
665 * @return a pointer to a matching session, 658 * @param element_count - the total element count of the dataset (session->total)
666 * else NULL 659 * @param state - a pointer to the state the session should be in, NULL to ignore
660 * @param peerid - a pointer to the peer ID of the associated peer, NULL to ignore
661 * @return a pointer to a matching session, or NULL
667 */ 662 */
668static struct ServiceSession * 663static struct ServiceSession *
669find_matching_session (struct ServiceSession * tail, 664find_matching_session (struct ServiceSession * tail,
@@ -692,6 +687,12 @@ find_matching_session (struct ServiceSession * tail,
692 return NULL; 687 return NULL;
693} 688}
694 689
690
691/**
692 * Safely frees ALL memory areas referenced by a session.
693 *
694 * @param session - the session to free elements from
695 */
695static void 696static void
696free_session_variables (struct ServiceSession * session) 697free_session_variables (struct ServiceSession * session)
697{ 698{
@@ -732,7 +733,7 @@ free_session_variables (struct ServiceSession * session)
732/** 733/**
733 * A client disconnected. 734 * A client disconnected.
734 * 735 *
735 * Remove the associated session(s), release datastructures 736 * Remove the associated session(s), release data structures
736 * and cancel pending outgoing transmissions to the client. 737 * and cancel pending outgoing transmissions to the client.
737 * if the session has not yet completed, we also cancel Alice's request to Bob. 738 * if the session has not yet completed, we also cancel Alice's request to Bob.
738 * 739 *
@@ -777,15 +778,15 @@ handle_client_disconnect (void *cls,
777 GNUNET_free (session); 778 GNUNET_free (session);
778} 779}
779 780
781
780/** 782/**
781 * Notify the client that the session has succeeded or failed completely. 783 * Notify the client that the session has succeeded or failed completely.
782 * This message gets sent to 784 * This message gets sent to
783 * * alice's client if bob disconnected or to 785 * * alice's client if bob disconnected or to
784 * * bob's client if the operation completed or alice disconnected 786 * * bob's client if the operation completed or alice disconnected
785 * 787 *
786 * @param client_session the associated client session 788 * @param cls the associated client session
787 * @return GNUNET_NO, if we could not notify the client 789 * @param tc the task context handed to us by the scheduler, unused
788 * GNUNET_YES if we notified it.
789 */ 790 */
790static void 791static void
791prepare_client_end_notification (void * cls, 792prepare_client_end_notification (void * cls,
@@ -827,9 +828,15 @@ prepare_client_end_notification (void * cls,
827 828
828} 829}
829 830
831
832/**
833 * Send a multi part chunk of a service response from bob to alice.
834 * This element only contains the two permutations of R, R'.
835 *
836 * @param cls the associated service session
837 */
830static void 838static void
831prepare_service_response_multipart (void *cls, 839prepare_service_response_multipart (void *cls)
832 const struct GNUNET_SCHEDULER_TaskContext *tc)
833{ 840{
834 struct ServiceSession * session = cls; 841 struct ServiceSession * session = cls;
835 unsigned char * current; 842 unsigned char * current;
@@ -1139,7 +1146,18 @@ compute_service_response (struct ServiceSession * request,
1139 gcry_sexp_release (tmp_exp); 1146 gcry_sexp_release (tmp_exp);
1140 1147
1141 // generate r, p and q 1148 // generate r, p and q
1142 rand = generate_random_vector (count); 1149 rand = initialize_mpi_vector (count);
1150 for (i = 0; i < count; i++) {
1151 value = (int32_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
1152
1153 // long to gcry_mpi_t
1154 if (value < 0)
1155 gcry_mpi_sub_ui (rand[i],
1156 rand[i],
1157 -value);
1158 else
1159 rand[i] = gcry_mpi_set_ui (rand[i], value);
1160 }
1143 p = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, count); 1161 p = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, count);
1144 q = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, count); 1162 q = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK, count);
1145 //initialize the result vectors 1163 //initialize the result vectors
@@ -1234,9 +1252,18 @@ except:
1234 return ret; 1252 return ret;
1235} 1253}
1236 1254
1255
1256/**
1257 * Send a multi part chunk of a service request from alice to bob.
1258 * This element only contains a part of the elements-vector (session->a[]),
1259 * mask and public key set have to be contained within the first message
1260 *
1261 * This allows a ~32kbit key length while using 32000 elements or 62000 elements per request.
1262 *
1263 * @param cls the associated service session
1264 */
1237static void 1265static void
1238prepare_service_request_multipart (void *cls, 1266prepare_service_request_multipart (void *cls)
1239 const struct GNUNET_SCHEDULER_TaskContext *tc)
1240{ 1267{
1241 struct ServiceSession * session = cls; 1268 struct ServiceSession * session = cls;
1242 unsigned char * current; 1269 unsigned char * current;
@@ -1641,6 +1668,7 @@ handle_client_request (void *cls,
1641 GNUNET_SERVER_receive_done (client, GNUNET_YES); 1668 GNUNET_SERVER_receive_done (client, GNUNET_YES);
1642} 1669}
1643 1670
1671
1644/** 1672/**
1645 * Function called for inbound tunnels. 1673 * Function called for inbound tunnels.
1646 * 1674 *
@@ -1666,6 +1694,7 @@ tunnel_incoming_handler (void *cls,
1666 return c; 1694 return c;
1667} 1695}
1668 1696
1697
1669/** 1698/**
1670 * Function called whenever a tunnel is destroyed. Should clean up 1699 * Function called whenever a tunnel is destroyed. Should clean up
1671 * any associated state. 1700 * any associated state.
@@ -1730,18 +1759,11 @@ tunnel_destruction_handler (void *cls,
1730 } 1759 }
1731} 1760}
1732 1761
1762
1733/** 1763/**
1734 * Compute our scalar product, done by Alice 1764 * Compute our scalar product, done by Alice
1735 * 1765 *
1736 * @param session - the session associated with this computation 1766 * @param session - the session associated with this computation
1737 * @param kp - (1) from the protocol definition:
1738 * $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)})$
1739 * @param kq - (2) from the protocol definition:
1740 * $E_A(a_{\pi'(i)}) \otimes E_A(- r_{\pi'(i)}) &= E_A(a_{\pi'(i)} - r_{\pi'(i)})$
1741 * @param s - S from the protocol definition:
1742 * $S := E_A(\sum (r_i + b_i)^2)$
1743 * @param stick - S' from the protocol definition:
1744 * $S' := E_A(\sum r_i^2)$
1745 * @return product as MPI, never NULL 1767 * @return product as MPI, never NULL
1746 */ 1768 */
1747static gcry_mpi_t 1769static gcry_mpi_t