diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/dht/gnunet-service-xdht_neighbours.c | 66 | ||||
-rw-r--r-- | src/include/gnunet_crypto_lib.h | 13 | ||||
-rw-r--r-- | src/util/crypto_ecc.c | 67 |
3 files changed, 102 insertions, 44 deletions
diff --git a/src/dht/gnunet-service-xdht_neighbours.c b/src/dht/gnunet-service-xdht_neighbours.c index fa7aeb981..020676a9b 100644 --- a/src/dht/gnunet-service-xdht_neighbours.c +++ b/src/dht/gnunet-service-xdht_neighbours.c | |||
@@ -485,7 +485,7 @@ static struct GNUNET_CORE_Handle *core_api; | |||
485 | /** | 485 | /** |
486 | * The current finger index that we have found trail to. | 486 | * The current finger index that we have found trail to. |
487 | */ | 487 | */ |
488 | static unsigned int current_finger_id; | 488 | static unsigned int current_finger_index; |
489 | 489 | ||
490 | 490 | ||
491 | /** | 491 | /** |
@@ -777,56 +777,29 @@ get_random_friend() | |||
777 | 777 | ||
778 | 778 | ||
779 | /** | 779 | /** |
780 | * TODO: Check the logic of using current_finger_id again. | 780 | * Find finger id to which we want to setup the trail |
781 | * This code is not correct. I need to check the pointers and | 781 | * @return finger id |
782 | * correct use of memcpy and all the data type. | ||
783 | * Use Chord formula finger[i]=(n+2^(i-1))mod m, | ||
784 | * where i = current finger map index - max. 256 bits | ||
785 | * n = own peer identity - 256 bits | ||
786 | * m = number of bits in peer id - 256 bits | ||
787 | * @return finger_peer_id for which we have to find the trail through network. | ||
788 | */ | 782 | */ |
789 | static | 783 | static |
790 | struct GNUNET_PeerIdentity * | 784 | struct GNUNET_PeerIdentity * |
791 | finger_id_to_search() | 785 | compute_finger_identity() |
792 | { | 786 | { |
793 | struct GNUNET_PeerIdentity *finger_peer_id; | 787 | struct GNUNET_PeerIdentity *finger_identity; |
794 | uint32_t peer_id; | ||
795 | uint32_t finger_id; | ||
796 | 788 | ||
797 | finger_peer_id = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity)); | 789 | finger_identity = GNUNET_CRYPTO_compute_finger(&my_identity,current_finger_index); |
798 | |||
799 | /* Copy unsigned char array into peer_id. */ | ||
800 | if (0 == memcpy(&peer_id,&my_identity.public_key.q_y,sizeof(uint32_t))) | ||
801 | return NULL; | ||
802 | |||
803 | /* We do all the arithmetic operation on peer_id to get finger_id*/ | ||
804 | finger_id = (uint32_t)(peer_id + pow(2,current_finger_id)) % MAX_FINGERS; | ||
805 | |||
806 | /* Copy the finger_id to finger_peer_id. */ | ||
807 | if (0 == memcpy(&finger_peer_id->public_key.q_y,&finger_id,sizeof(uint32_t))) | ||
808 | return NULL; | ||
809 | 790 | ||
810 | 791 | current_finger_index = (current_finger_index+1) % MAX_FINGERS; | |
811 | /* FIXME: Here I increment the index so that next time when we enter this | ||
812 | function, then we begin the search from current index. Is it possible | ||
813 | to set this value when we add the finger id to our finger table. No, because | ||
814 | even there is a call going on to find the finger, we can start another call | ||
815 | to search another peer. */ | ||
816 | current_finger_id = (current_finger_id+1) % MAX_FINGERS; | ||
817 | 792 | ||
818 | /* Check if you already have an entry in finger_peers for this finger_id. | 793 | /* Check if you already have an entry in finger_peers for this finger_id. |
819 | If yes then again look for a new finger_id. */ | 794 | If yes then again look for a new finger_id. |
820 | /*if(NULL != GNUNET_CONTAINER_multipeermap_get(finger_peers,finger_peer_id)) | 795 | FIXME: Should we return NULL here? |
796 | if(NULL != GNUNET_CONTAINER_multipeermap_get(finger_peers,finger_peer_id)) | ||
821 | { | 797 | { |
822 | 798 | finger_peer_id = compute_finger_identity(); | |
823 | finger_peer_id = finger_id_to_search(); | 799 | }*/ |
824 | } | 800 | return finger_identity; |
825 | */ | ||
826 | return finger_peer_id; | ||
827 | } | 801 | } |
828 | 802 | ||
829 | |||
830 | /** | 803 | /** |
831 | * TODO: Implement after testing friend/finger map. | 804 | * TODO: Implement after testing friend/finger map. |
832 | * TODO: Handle the case when we already have a trail to our predecessor in | 805 | * TODO: Handle the case when we already have a trail to our predecessor in |
@@ -838,7 +811,7 @@ finger_id_to_search() | |||
838 | * @return peer identity of immediate predecessor. | 811 | * @return peer identity of immediate predecessor. |
839 | */ | 812 | */ |
840 | static | 813 | static |
841 | struct GNUNET_PeerIdentity* | 814 | struct GNUNET_PeerIdentity * |
842 | find_immediate_predecessor() | 815 | find_immediate_predecessor() |
843 | { | 816 | { |
844 | /* Using your own peer identity, calculate your predecessor | 817 | /* Using your own peer identity, calculate your predecessor |
@@ -880,7 +853,7 @@ send_find_finger_trail_message (void *cls, | |||
880 | else | 853 | else |
881 | { | 854 | { |
882 | /* Find the finger_peer_id for which we want to setup the trail */ | 855 | /* Find the finger_peer_id for which we want to setup the trail */ |
883 | finger_peer_id = finger_id_to_search(); | 856 | finger_peer_id = compute_finger_identity(); |
884 | } | 857 | } |
885 | 858 | ||
886 | /* Choose a friend randomly from your friend_peers map. */ | 859 | /* Choose a friend randomly from your friend_peers map. */ |
@@ -897,7 +870,7 @@ send_find_finger_trail_message (void *cls, | |||
897 | DHT_MINIMUM_FIND_FINGER_TRAIL_INTERVAL.rel_value_us + | 870 | DHT_MINIMUM_FIND_FINGER_TRAIL_INTERVAL.rel_value_us + |
898 | GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, | 871 | GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, |
899 | DHT_MAXIMUM_FIND_FINGER_TRAIL_INTERVAL.rel_value_us / | 872 | DHT_MAXIMUM_FIND_FINGER_TRAIL_INTERVAL.rel_value_us / |
900 | (current_finger_id + 1)); | 873 | (current_finger_index + 1)); |
901 | 874 | ||
902 | find_finger_trail_task = | 875 | find_finger_trail_task = |
903 | GNUNET_SCHEDULER_add_delayed (next_send_time, &send_find_finger_trail_message, | 876 | GNUNET_SCHEDULER_add_delayed (next_send_time, &send_find_finger_trail_message, |
@@ -1059,6 +1032,11 @@ handle_dht_p2p_result (void *cls, const struct GNUNET_PeerIdentity *peer, | |||
1059 | * FIXME:1. Check if current_destination field is set correctly. | 1032 | * FIXME:1. Check if current_destination field is set correctly. |
1060 | * 2. Is it correct to use GNUNET_CMP_PEER_IDENTITY to find out the successor | 1033 | * 2. Is it correct to use GNUNET_CMP_PEER_IDENTITY to find out the successor |
1061 | * of a finger. | 1034 | * of a finger. |
1035 | * 3. We should check the interval of the keys for which a peer is responsible | ||
1036 | * when we are looking to find the correct peer to store a key. But | ||
1037 | * --> How do we maintain this interval? | ||
1038 | * --> Should we check this interval when we are looking for trail to a finger | ||
1039 | * as in this function? | ||
1062 | * The code flow seems to be very large. Could do better. | 1040 | * The code flow seems to be very large. Could do better. |
1063 | * @param destination peer id's predecessor we are looking for. | 1041 | * @param destination peer id's predecessor we are looking for. |
1064 | * @return | 1042 | * @return |
diff --git a/src/include/gnunet_crypto_lib.h b/src/include/gnunet_crypto_lib.h index 2e333bd17..3d9241b64 100644 --- a/src/include/gnunet_crypto_lib.h +++ b/src/include/gnunet_crypto_lib.h | |||
@@ -1268,6 +1268,19 @@ GNUNET_CRYPTO_ecdsa_private_key_derive (const struct GNUNET_CRYPTO_EcdsaPrivateK | |||
1268 | 1268 | ||
1269 | 1269 | ||
1270 | /** | 1270 | /** |
1271 | * Computes a new PeerIdentity using the Chord formula. | ||
1272 | * new_peer_identity = ((my_identity + pow(2,i)) mod (pow(2,m) | ||
1273 | * where m, size of struct GNUNET_PeerIdentity in bits. | ||
1274 | * i, 0 <= i <= m | ||
1275 | * @param my_identity original PeerIdentity | ||
1276 | * @param value of i. | ||
1277 | * @return finger_identity | ||
1278 | */ | ||
1279 | struct GNUNET_PeerIdentity * | ||
1280 | GNUNET_CRYPTO_compute_finger(struct GNUNET_PeerIdentity *my_identity,unsigned int index); | ||
1281 | |||
1282 | |||
1283 | /** | ||
1271 | * @ingroup crypto | 1284 | * @ingroup crypto |
1272 | * Derive a public key from a given public key and a label. | 1285 | * Derive a public key from a given public key and a label. |
1273 | * Essentially calculates a public key 'V = H(l,P) * P'. | 1286 | * Essentially calculates a public key 'V = H(l,P) * P'. |
diff --git a/src/util/crypto_ecc.c b/src/util/crypto_ecc.c index 23d6ade7e..2de97cb8c 100644 --- a/src/util/crypto_ecc.c +++ b/src/util/crypto_ecc.c | |||
@@ -1448,6 +1448,73 @@ GNUNET_CRYPTO_ecdsa_private_key_derive (const struct GNUNET_CRYPTO_EcdsaPrivateK | |||
1448 | 1448 | ||
1449 | 1449 | ||
1450 | /** | 1450 | /** |
1451 | * Computes a new PeerIdentity using the Chord formula. | ||
1452 | * new_peer_identity = ((my_identity + pow(2,i)) mod (pow(2,m) | ||
1453 | * where m, size of struct GNUNET_PeerIdentity in bits. | ||
1454 | * i, 0 <= i <= m | ||
1455 | * @param my_identity original PeerIdentity | ||
1456 | * @param value of i. | ||
1457 | * @return finger_identity | ||
1458 | */ | ||
1459 | struct GNUNET_PeerIdentity * | ||
1460 | GNUNET_CRYPTO_compute_finger(struct GNUNET_PeerIdentity *my_identity, unsigned int index) | ||
1461 | { | ||
1462 | gcry_mpi_t my_identity_mpi; | ||
1463 | gcry_mpi_t finger_identity_mpi; | ||
1464 | gcry_mpi_t add; | ||
1465 | gcry_mpi_t mod; | ||
1466 | gcry_error_t rc; | ||
1467 | struct GNUNET_PeerIdentity *finger_identity; | ||
1468 | size_t read = 0; | ||
1469 | size_t write = 0; | ||
1470 | |||
1471 | finger_identity = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity)); | ||
1472 | |||
1473 | /* Initialize my_identity_mpi. */ | ||
1474 | my_identity_mpi = gcry_mpi_new(8*sizeof(struct GNUNET_PeerIdentity)); | ||
1475 | |||
1476 | /* Copy my_identity into my_id */ | ||
1477 | if(0 != (rc = gcry_mpi_scan(&my_identity_mpi, GCRYMPI_FMT_USG, my_identity->public_key.q_y, | ||
1478 | sizeof(struct GNUNET_PeerIdentity), &read))) | ||
1479 | { | ||
1480 | LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc); | ||
1481 | GNUNET_free(finger_identity); | ||
1482 | return NULL; | ||
1483 | } | ||
1484 | |||
1485 | /* Initialize finger_identity_mpi */ | ||
1486 | finger_identity_mpi = gcry_mpi_new(8*sizeof(struct GNUNET_PeerIdentity)); | ||
1487 | |||
1488 | /* Initialize add */ | ||
1489 | add = gcry_mpi_new(8*sizeof(struct GNUNET_PeerIdentity)); | ||
1490 | |||
1491 | /* Set the index bit in add.*/ | ||
1492 | gcry_mpi_set_bit(add,index); | ||
1493 | |||
1494 | /* Initialize mod */ | ||
1495 | mod = gcry_mpi_new(8*sizeof(struct GNUNET_PeerIdentity) + 1); | ||
1496 | gcry_mpi_set_bit(mod,257); | ||
1497 | gcry_mpi_sub_ui(mod,mod,(unsigned long)1); | ||
1498 | |||
1499 | |||
1500 | /* finger_identity_mpi = (my_identity_mpi + add) % mod */ | ||
1501 | gcry_mpi_addm(finger_identity_mpi,my_identity_mpi,add,mod); | ||
1502 | |||
1503 | |||
1504 | /* Copy finger_identity_mpi to finger_identity */ | ||
1505 | if(0 != (rc = gcry_mpi_print(GCRYMPI_FMT_USG,finger_identity->public_key.q_y, | ||
1506 | 32,&write,finger_identity_mpi))) | ||
1507 | { | ||
1508 | LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_print", rc); | ||
1509 | GNUNET_free(finger_identity); | ||
1510 | return NULL; | ||
1511 | } | ||
1512 | |||
1513 | return finger_identity; | ||
1514 | } | ||
1515 | |||
1516 | |||
1517 | /** | ||
1451 | * Derive a public key from a given public key and a label. | 1518 | * Derive a public key from a given public key and a label. |
1452 | * Essentially calculates a public key 'V = H(l,P) * P'. | 1519 | * Essentially calculates a public key 'V = H(l,P) * P'. |
1453 | * | 1520 | * |