diff options
author | irungentoo <irungentoo@gmail.com> | 2014-02-26 12:18:47 -0500 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2014-02-26 12:18:47 -0500 |
commit | cebae58ddc492869b627eb7b69c7565084a74a34 (patch) | |
tree | 8e2db2e5b60a306ebe61c55497687a9892f8c5fe /toxcore/onion.c | |
parent | b01c19ce56ae73c1c9b6303cf2fabde6373db95b (diff) |
Major speed/cpu usage/bandwidth improvements to onion.
Diffstat (limited to 'toxcore/onion.c')
-rw-r--r-- | toxcore/onion.c | 79 |
1 files changed, 56 insertions, 23 deletions
diff --git a/toxcore/onion.c b/toxcore/onion.c index 578621cc..57001784 100644 --- a/toxcore/onion.c +++ b/toxcore/onion.c | |||
@@ -47,48 +47,81 @@ static void change_symmetric_key(Onion *onion) | |||
47 | } | 47 | } |
48 | } | 48 | } |
49 | 49 | ||
50 | /* Create a new onion path. | ||
51 | * | ||
52 | * Create a new onion path out of nodes (nodes is a list of 3 nodes) | ||
53 | * | ||
54 | * new_path must be an empty memory location of atleast Onion_Path size. | ||
55 | * | ||
56 | * return -1 on failure. | ||
57 | * return 0 on success. | ||
58 | */ | ||
59 | int create_onion_path(DHT *dht, Onion_Path *new_path, Node_format *nodes) | ||
60 | { | ||
61 | if (!new_path || !nodes) | ||
62 | return -1; | ||
63 | |||
64 | encrypt_precompute(nodes[0].client_id, dht->self_secret_key, new_path->shared_key1); | ||
65 | memcpy(new_path->public_key1, dht->self_public_key, crypto_box_PUBLICKEYBYTES); | ||
66 | |||
67 | uint8_t random_public_key[crypto_box_PUBLICKEYBYTES]; | ||
68 | uint8_t random_secret_key[crypto_box_SECRETKEYBYTES]; | ||
69 | |||
70 | crypto_box_keypair(random_public_key, random_secret_key); | ||
71 | encrypt_precompute(nodes[1].client_id, random_secret_key, new_path->shared_key2); | ||
72 | memcpy(new_path->public_key2, random_public_key, crypto_box_PUBLICKEYBYTES); | ||
73 | |||
74 | crypto_box_keypair(random_public_key, random_secret_key); | ||
75 | encrypt_precompute(nodes[2].client_id, random_secret_key, new_path->shared_key3); | ||
76 | memcpy(new_path->public_key3, random_public_key, crypto_box_PUBLICKEYBYTES); | ||
77 | |||
78 | new_path->ip_port1 = nodes[0].ip_port; | ||
79 | new_path->ip_port2 = nodes[1].ip_port; | ||
80 | new_path->ip_port3 = nodes[2].ip_port; | ||
81 | |||
82 | /* to_net_family(&new_path->ip_port1.ip); */ | ||
83 | to_net_family(&new_path->ip_port2.ip); | ||
84 | to_net_family(&new_path->ip_port3.ip); | ||
85 | |||
86 | return 0; | ||
87 | } | ||
88 | |||
50 | /* Create and send a onion packet. | 89 | /* Create and send a onion packet. |
51 | * | 90 | * |
52 | * nodes is a list of 4 nodes, the packet will route through nodes 0, 1, 2 and the data | 91 | * Use Onion_Path path to send data of length to dest. |
53 | * with length length will arrive at 3. | ||
54 | * | 92 | * |
55 | * return -1 on failure. | 93 | * return -1 on failure. |
56 | * return 0 on success. | 94 | * return 0 on success. |
57 | */ | 95 | */ |
58 | int send_onion_packet(DHT *dht, Node_format *nodes, uint8_t *data, uint32_t length) | 96 | int send_onion_packet(Networking_Core *net, Onion_Path *path, IP_Port dest, uint8_t *data, uint32_t length) |
59 | { | 97 | { |
60 | if (1 + length + SEND_1 > MAX_ONION_SIZE || length == 0) | 98 | if (1 + length + SEND_1 > MAX_ONION_SIZE || length == 0) |
61 | return -1; | 99 | return -1; |
62 | 100 | ||
101 | to_net_family(&dest.ip); | ||
63 | uint8_t step1[sizeof(IP_Port) + length]; | 102 | uint8_t step1[sizeof(IP_Port) + length]; |
64 | to_net_family(&nodes[3].ip_port.ip); | 103 | |
65 | memcpy(step1, &nodes[3].ip_port, sizeof(IP_Port)); | 104 | memcpy(step1, &dest, sizeof(IP_Port)); |
66 | memcpy(step1 + sizeof(IP_Port), data, length); | 105 | memcpy(step1 + sizeof(IP_Port), data, length); |
67 | 106 | ||
68 | uint8_t nonce[crypto_box_NONCEBYTES]; | 107 | uint8_t nonce[crypto_box_NONCEBYTES]; |
69 | random_nonce(nonce); | 108 | random_nonce(nonce); |
70 | uint8_t random_public_key[crypto_box_PUBLICKEYBYTES]; | ||
71 | uint8_t random_secret_key[crypto_box_SECRETKEYBYTES]; | ||
72 | crypto_box_keypair(random_public_key, random_secret_key); | ||
73 | 109 | ||
74 | uint8_t step2[sizeof(IP_Port) + SEND_BASE + length]; | 110 | uint8_t step2[sizeof(IP_Port) + SEND_BASE + length]; |
75 | to_net_family(&nodes[2].ip_port.ip); | 111 | memcpy(step2, &path->ip_port3, sizeof(IP_Port)); |
76 | memcpy(step2, &nodes[2].ip_port, sizeof(IP_Port)); | 112 | memcpy(step2 + sizeof(IP_Port), path->public_key3, crypto_box_PUBLICKEYBYTES); |
77 | memcpy(step2 + sizeof(IP_Port), random_public_key, crypto_box_PUBLICKEYBYTES); | ||
78 | 113 | ||
79 | int len = encrypt_data(nodes[2].client_id, random_secret_key, nonce, | 114 | int len = encrypt_data_fast(path->shared_key3, nonce, step1, sizeof(step1), |
80 | step1, sizeof(step1), step2 + sizeof(IP_Port) + crypto_box_PUBLICKEYBYTES); | 115 | step2 + sizeof(IP_Port) + crypto_box_PUBLICKEYBYTES); |
81 | 116 | ||
82 | if ((uint32_t)len != sizeof(IP_Port) + length + crypto_box_MACBYTES) | 117 | if ((uint32_t)len != sizeof(IP_Port) + length + crypto_box_MACBYTES) |
83 | return -1; | 118 | return -1; |
84 | 119 | ||
85 | crypto_box_keypair(random_public_key, random_secret_key); | ||
86 | uint8_t step3[sizeof(IP_Port) + SEND_BASE * 2 + length]; | 120 | uint8_t step3[sizeof(IP_Port) + SEND_BASE * 2 + length]; |
87 | to_net_family(&nodes[1].ip_port.ip); | 121 | memcpy(step3, &path->ip_port2, sizeof(IP_Port)); |
88 | memcpy(step3, &nodes[1].ip_port, sizeof(IP_Port)); | 122 | memcpy(step3 + sizeof(IP_Port), path->public_key2, crypto_box_PUBLICKEYBYTES); |
89 | memcpy(step3 + sizeof(IP_Port), random_public_key, crypto_box_PUBLICKEYBYTES); | 123 | len = encrypt_data_fast(path->shared_key2, nonce, step2, sizeof(step2), |
90 | len = encrypt_data(nodes[1].client_id, random_secret_key, nonce, | 124 | step3 + sizeof(IP_Port) + crypto_box_PUBLICKEYBYTES); |
91 | step2, sizeof(step2), step3 + sizeof(IP_Port) + crypto_box_PUBLICKEYBYTES); | ||
92 | 125 | ||
93 | if ((uint32_t)len != sizeof(IP_Port) + SEND_BASE + length + crypto_box_MACBYTES) | 126 | if ((uint32_t)len != sizeof(IP_Port) + SEND_BASE + length + crypto_box_MACBYTES) |
94 | return -1; | 127 | return -1; |
@@ -96,15 +129,15 @@ int send_onion_packet(DHT *dht, Node_format *nodes, uint8_t *data, uint32_t leng | |||
96 | uint8_t packet[1 + length + SEND_1]; | 129 | uint8_t packet[1 + length + SEND_1]; |
97 | packet[0] = NET_PACKET_ONION_SEND_INITIAL; | 130 | packet[0] = NET_PACKET_ONION_SEND_INITIAL; |
98 | memcpy(packet + 1, nonce, crypto_box_NONCEBYTES); | 131 | memcpy(packet + 1, nonce, crypto_box_NONCEBYTES); |
99 | memcpy(packet + 1 + crypto_box_NONCEBYTES, dht->self_public_key, crypto_box_PUBLICKEYBYTES); | 132 | memcpy(packet + 1 + crypto_box_NONCEBYTES, path->public_key1, crypto_box_PUBLICKEYBYTES); |
100 | 133 | ||
101 | len = encrypt_data(nodes[0].client_id, dht->self_secret_key, nonce, | 134 | len = encrypt_data_fast(path->shared_key1, nonce, step3, sizeof(step3), |
102 | step3, sizeof(step3), packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES); | 135 | packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES); |
103 | 136 | ||
104 | if ((uint32_t)len != sizeof(IP_Port) + SEND_BASE * 2 + length + crypto_box_MACBYTES) | 137 | if ((uint32_t)len != sizeof(IP_Port) + SEND_BASE * 2 + length + crypto_box_MACBYTES) |
105 | return -1; | 138 | return -1; |
106 | 139 | ||
107 | if ((uint32_t)sendpacket(dht->c->lossless_udp->net, nodes[0].ip_port, packet, sizeof(packet)) != sizeof(packet)) | 140 | if ((uint32_t)sendpacket(net, path->ip_port1, packet, sizeof(packet)) != sizeof(packet)) |
108 | return -1; | 141 | return -1; |
109 | 142 | ||
110 | return 0; | 143 | return 0; |