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