Line | Count | Source |
1 | | /* SPDX-License-Identifier: GPL-3.0-or-later |
2 | | * Copyright © 2016-2018 The TokTok team. |
3 | | * Copyright © 2013 Tox project. |
4 | | */ |
5 | | |
6 | | /** |
7 | | * Implementation of the onion part of docs/Prevent_Tracking.txt |
8 | | */ |
9 | | #ifndef C_TOXCORE_TOXCORE_ONION_H |
10 | | #define C_TOXCORE_TOXCORE_ONION_H |
11 | | |
12 | | #include "DHT.h" |
13 | | #include "logger.h" |
14 | | #include "mono_time.h" |
15 | | #include "shared_key_cache.h" |
16 | | |
17 | | typedef int onion_recv_1_cb(void *object, const IP_Port *dest, const uint8_t *data, uint16_t length); |
18 | | |
19 | | typedef struct Onion { |
20 | | const Logger *log; |
21 | | const Mono_Time *mono_time; |
22 | | const Random *rng; |
23 | | const Memory *mem; |
24 | | DHT *dht; |
25 | | Networking_Core *net; |
26 | | uint8_t secret_symmetric_key[CRYPTO_SYMMETRIC_KEY_SIZE]; |
27 | | uint64_t timestamp; |
28 | | |
29 | | Shared_Key_Cache *shared_keys_1; |
30 | | Shared_Key_Cache *shared_keys_2; |
31 | | Shared_Key_Cache *shared_keys_3; |
32 | | |
33 | | onion_recv_1_cb *recv_1_function; |
34 | | void *callback_object; |
35 | | } Onion; |
36 | | |
37 | 594k | #define ONION_MAX_PACKET_SIZE 1400 |
38 | | |
39 | 2.86M | #define ONION_RETURN_1 (CRYPTO_NONCE_SIZE + SIZE_IPPORT + CRYPTO_MAC_SIZE) |
40 | 1.90M | #define ONION_RETURN_2 (CRYPTO_NONCE_SIZE + SIZE_IPPORT + CRYPTO_MAC_SIZE + ONION_RETURN_1) |
41 | 896k | #define ONION_RETURN_3 (CRYPTO_NONCE_SIZE + SIZE_IPPORT + CRYPTO_MAC_SIZE + ONION_RETURN_2) |
42 | | |
43 | 712k | #define ONION_SEND_BASE (CRYPTO_PUBLIC_KEY_SIZE + SIZE_IPPORT + CRYPTO_MAC_SIZE) |
44 | 68.9k | #define ONION_SEND_3 (CRYPTO_NONCE_SIZE + ONION_SEND_BASE + ONION_RETURN_2) |
45 | 70.9k | #define ONION_SEND_2 (CRYPTO_NONCE_SIZE + ONION_SEND_BASE*2 + ONION_RETURN_1) |
46 | 188k | #define ONION_SEND_1 (CRYPTO_NONCE_SIZE + ONION_SEND_BASE*3) |
47 | | |
48 | 38.9k | #define ONION_MAX_DATA_SIZE (ONION_MAX_PACKET_SIZE - (ONION_SEND_1 + 1)) |
49 | 133k | #define ONION_RESPONSE_MAX_DATA_SIZE (ONION_MAX_PACKET_SIZE - (1 + ONION_RETURN_3)) |
50 | | |
51 | 452k | #define ONION_PATH_LENGTH 3 |
52 | | |
53 | | typedef struct Onion_Path { |
54 | | uint8_t shared_key1[CRYPTO_SHARED_KEY_SIZE]; |
55 | | uint8_t shared_key2[CRYPTO_SHARED_KEY_SIZE]; |
56 | | uint8_t shared_key3[CRYPTO_SHARED_KEY_SIZE]; |
57 | | |
58 | | uint8_t public_key1[CRYPTO_PUBLIC_KEY_SIZE]; |
59 | | uint8_t public_key2[CRYPTO_PUBLIC_KEY_SIZE]; |
60 | | uint8_t public_key3[CRYPTO_PUBLIC_KEY_SIZE]; |
61 | | |
62 | | IP_Port ip_port1; |
63 | | uint8_t node_public_key1[CRYPTO_PUBLIC_KEY_SIZE]; |
64 | | |
65 | | IP_Port ip_port2; |
66 | | uint8_t node_public_key2[CRYPTO_PUBLIC_KEY_SIZE]; |
67 | | |
68 | | IP_Port ip_port3; |
69 | | uint8_t node_public_key3[CRYPTO_PUBLIC_KEY_SIZE]; |
70 | | |
71 | | uint32_t path_num; |
72 | | } Onion_Path; |
73 | | |
74 | | /** @brief Create a new onion path. |
75 | | * |
76 | | * Create a new onion path out of nodes (nodes is a list of ONION_PATH_LENGTH nodes) |
77 | | * |
78 | | * new_path must be an empty memory location of at least Onion_Path size. |
79 | | * |
80 | | * return -1 on failure. |
81 | | * return 0 on success. |
82 | | */ |
83 | | non_null() |
84 | | int create_onion_path(const Random *rng, const DHT *dht, Onion_Path *new_path, const Node_format *nodes); |
85 | | |
86 | | /** @brief Dump nodes in onion path to nodes of length num_nodes. |
87 | | * |
88 | | * return -1 on failure. |
89 | | * return 0 on success. |
90 | | */ |
91 | | non_null() |
92 | | int onion_path_to_nodes(Node_format *nodes, unsigned int num_nodes, const Onion_Path *path); |
93 | | |
94 | | /** @brief Create a onion packet. |
95 | | * |
96 | | * Use Onion_Path path to create packet for data of length to dest. |
97 | | * Maximum length of data is ONION_MAX_DATA_SIZE. |
98 | | * packet should be at least ONION_MAX_PACKET_SIZE big. |
99 | | * |
100 | | * return -1 on failure. |
101 | | * return length of created packet on success. |
102 | | */ |
103 | | non_null() |
104 | | int create_onion_packet(const Random *rng, uint8_t *packet, uint16_t max_packet_length, |
105 | | const Onion_Path *path, const IP_Port *dest, |
106 | | const uint8_t *data, uint16_t length); |
107 | | |
108 | | |
109 | | /** @brief Create a onion packet to be sent over tcp. |
110 | | * |
111 | | * Use Onion_Path path to create packet for data of length to dest. |
112 | | * Maximum length of data is ONION_MAX_DATA_SIZE. |
113 | | * packet should be at least ONION_MAX_PACKET_SIZE big. |
114 | | * |
115 | | * return -1 on failure. |
116 | | * return length of created packet on success. |
117 | | */ |
118 | | non_null() |
119 | | int create_onion_packet_tcp(const Random *rng, uint8_t *packet, uint16_t max_packet_length, |
120 | | const Onion_Path *path, const IP_Port *dest, |
121 | | const uint8_t *data, uint16_t length); |
122 | | |
123 | | /** @brief Create and send a onion response sent initially to dest with. |
124 | | * Maximum length of data is ONION_RESPONSE_MAX_DATA_SIZE. |
125 | | * |
126 | | * return -1 on failure. |
127 | | * return 0 on success. |
128 | | */ |
129 | | non_null() |
130 | | int send_onion_response(const Logger *log, const Networking_Core *net, |
131 | | const IP_Port *dest, const uint8_t *data, uint16_t length, |
132 | | const uint8_t *ret); |
133 | | |
134 | | /** @brief Function to handle/send received decrypted versions of the packet created by create_onion_packet. |
135 | | * |
136 | | * return 0 on success. |
137 | | * return 1 on failure. |
138 | | * |
139 | | * Used to handle these packets that are received in a non traditional way (by TCP for example). |
140 | | * |
141 | | * Source family must be set to something else than TOX_AF_INET6 or TOX_AF_INET so that the callback gets called |
142 | | * when the response is received. |
143 | | */ |
144 | | non_null() |
145 | | int onion_send_1(const Onion *onion, const uint8_t *plain, uint16_t len, const IP_Port *source, const uint8_t *nonce); |
146 | | |
147 | | /** Set the callback to be called when the dest ip_port doesn't have TOX_AF_INET6 or TOX_AF_INET as the family. */ |
148 | | non_null(1) nullable(2, 3) |
149 | | void set_callback_handle_recv_1(Onion *onion, onion_recv_1_cb *function, void *object); |
150 | | |
151 | | non_null() |
152 | | Onion *new_onion(const Logger *log, const Memory *mem, const Mono_Time *mono_time, const Random *rng, DHT *dht); |
153 | | |
154 | | nullable(1) |
155 | | void kill_onion(Onion *onion); |
156 | | |
157 | | |
158 | | #endif /* C_TOXCORE_TOXCORE_ONION_H */ |