Coverage Report

Created: 2026-10-24 08:16

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