summaryrefslogtreecommitdiff
path: root/toxcore/net_crypto.h
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/net_crypto.h')
-rw-r--r--toxcore/net_crypto.h147
1 files changed, 83 insertions, 64 deletions
diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h
index 81670993..1fde0297 100644
--- a/toxcore/net_crypto.h
+++ b/toxcore/net_crypto.h
@@ -32,20 +32,21 @@ extern "C" {
32 32
33#define MAX_INCOMING 64 33#define MAX_INCOMING 64
34 34
35#define CRYPTO_PACKET_FRIEND_REQ 32 /* Friend request crypto packet ID */ 35#define CRYPTO_PACKET_FRIEND_REQ 32 /* Friend request crypto packet ID. */
36#define CRYPTO_PACKET_NAT_PING 254 /* NAT ping crypto packet ID */ 36#define CRYPTO_PACKET_NAT_PING 254 /* NAT ping crypto packet ID. */
37 37
38typedef struct { 38typedef struct {
39 uint8_t public_key[crypto_box_PUBLICKEYBYTES]; /* the real public key of the peer. */ 39 uint8_t public_key[crypto_box_PUBLICKEYBYTES]; /* The real public key of the peer. */
40 uint8_t recv_nonce[crypto_box_NONCEBYTES]; /* nonce of received packets */ 40 uint8_t recv_nonce[crypto_box_NONCEBYTES]; /* Nonce of received packets. */
41 uint8_t sent_nonce[crypto_box_NONCEBYTES]; /* nonce of sent packets. */ 41 uint8_t sent_nonce[crypto_box_NONCEBYTES]; /* Nonce of sent packets. */
42 uint8_t sessionpublic_key[crypto_box_PUBLICKEYBYTES]; /* our public key for this session. */ 42 uint8_t sessionpublic_key[crypto_box_PUBLICKEYBYTES]; /* Our public key for this session. */
43 uint8_t sessionsecret_key[crypto_box_SECRETKEYBYTES]; /* our private key for this session. */ 43 uint8_t sessionsecret_key[crypto_box_SECRETKEYBYTES]; /* Our private key for this session. */
44 uint8_t peersessionpublic_key[crypto_box_PUBLICKEYBYTES]; /* The public key of the peer. */ 44 uint8_t peersessionpublic_key[crypto_box_PUBLICKEYBYTES]; /* The public key of the peer. */
45 uint8_t shared_key[crypto_box_BEFORENMBYTES]; /* the precomputed shared key from encrypt_precompute */ 45 uint8_t shared_key[crypto_box_BEFORENMBYTES]; /* The precomputed shared key from encrypt_precompute. */
46 uint8_t status; /* 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet 46 uint8_t status; /* 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet
47 (we have received a handshake but no empty data packet), 3 if the connection is established. 47 * (we have received a handshake but no empty data packet), 3 if the connection is established.
48 4 if the connection is timed out. */ 48 * 4 if the connection is timed out.
49 */
49 uint16_t number; /* Lossless_UDP connection number corresponding to this connection. */ 50 uint16_t number; /* Lossless_UDP connection number corresponding to this connection. */
50 51
51} Crypto_Connection; 52} Crypto_Connection;
@@ -63,13 +64,13 @@ typedef struct {
63 64
64 Crypto_Connection *crypto_connections; 65 Crypto_Connection *crypto_connections;
65 66
66 uint32_t crypto_connections_length; /* Length of connections array */ 67 uint32_t crypto_connections_length; /* Length of connections array. */
67 68
68 /* Our public and secret keys. */ 69 /* Our public and secret keys. */
69 uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; 70 uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
70 uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; 71 uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
71 72
72 /* keeps track of the connection numbers for friends request so we can check later if they were sent */ 73 /* keeps track of the connection numbers for friends request so we can check later if they were sent. */
73 int incoming_connections[MAX_INCOMING]; 74 int incoming_connections[MAX_INCOMING];
74 75
75 Cryptopacket_Handles cryptopackethandlers[256]; 76 Cryptopacket_Handles cryptopackethandlers[256];
@@ -79,21 +80,23 @@ typedef struct {
79 80
80#define ENCRYPTION_PADDING (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES) 81#define ENCRYPTION_PADDING (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES)
81 82
82/* returns zero if the buffer contains only zeros */ 83/* return zero if the buffer contains only zeros. */
83uint8_t crypto_iszero(uint8_t *buffer, uint32_t blen); 84uint8_t crypto_iszero(uint8_t *buffer, uint32_t blen);
84 85
85/* encrypts plain of length length to encrypted of length + 16 using the 86/* Encrypts plain of length length to encrypted of length + 16 using the
86 public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce 87 * public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce.
87 return -1 if there was a problem. 88 * return -1 if there was a problem.
88 return length of encrypted data if everything was fine. */ 89 * return length of encrypted data if everything was fine.
90 */
89int encrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce, 91int encrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce,
90 uint8_t *plain, uint32_t length, uint8_t *encrypted); 92 uint8_t *plain, uint32_t length, uint8_t *encrypted);
91 93
92 94
93/* decrypts encrypted of length length to plain of length length - 16 using the 95/* Decrypts encrypted of length length to plain of length length - 16 using the
94 public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce 96 * public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce.
95 return -1 if there was a problem(decryption failed) 97 * return -1 if there was a problem (decryption failed).
96 return length of plain data if everything was fine. */ 98 * return length of plain data if everything was fine.
99 */
97int decrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce, 100int decrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce,
98 uint8_t *encrypted, uint32_t length, uint8_t *plain); 101 uint8_t *encrypted, uint32_t length, uint8_t *plain);
99 102
@@ -111,86 +114,102 @@ int decrypt_data_fast(uint8_t *enc_key, uint8_t *nonce,
111 uint8_t *encrypted, uint32_t length, uint8_t *plain); 114 uint8_t *encrypted, uint32_t length, uint8_t *plain);
112 115
113 116
114/* fill the given nonce with random bytes. */ 117/* Fill the given nonce with random bytes. */
115void random_nonce(uint8_t *nonce); 118void random_nonce(uint8_t *nonce);
116 119
117/* return 0 if there is no received data in the buffer 120/* return 0 if there is no received data in the buffer.
118 return -1 if the packet was discarded. 121 * return -1 if the packet was discarded.
119 return length of received data if successful */ 122 * return length of received data if successful.
123 */
120int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data); 124int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data);
121 125
122/* return 0 if data could not be put in packet queue 126/* return 0 if data could not be put in packet queue
123 return 1 if data was put into the queue */ 127 * return 1 if data was put into the queue
128 */
124int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length); 129int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length);
125 130
126/* create a request to peer. 131/* Create a request to peer.
127 send_public_key and send_secret_key are the pub/secret keys of the sender 132 * send_public_key and send_secret_key are the pub/secret keys of the sender.
128 recv_public_key is public key of reciever 133 * recv_public_key is public key of reciever.
129 packet must be an array of MAX_DATA_SIZE big. 134 * packet must be an array of MAX_DATA_SIZE big.
130 Data represents the data we send with the request with length being the length of the data. 135 * Data represents the data we send with the request with length being the length of the data.
131 request_id is the id of the request (32 = friend request, 254 = ping request) 136 * request_id is the id of the request (32 = friend request, 254 = ping request).
132 returns -1 on failure 137 *
133 returns the length of the created packet on success */ 138 * returns -1 on failure.
139 * returns the length of the created packet on success.
140 */
134int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *packet, uint8_t *recv_public_key, 141int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *packet, uint8_t *recv_public_key,
135 uint8_t *data, uint32_t length, uint8_t request_id); 142 uint8_t *data, uint32_t length, uint8_t request_id);
136 143
137 144
138/* Function to call when request beginning with byte is received */ 145/* Function to call when request beginning with byte is received. */
139void cryptopacket_registerhandler(Net_Crypto *c, uint8_t byte, cryptopacket_handler_callback cb, void *object); 146void cryptopacket_registerhandler(Net_Crypto *c, uint8_t byte, cryptopacket_handler_callback cb, void *object);
140 147
141/* Start a secure connection with other peer who has public_key and ip_port 148/* Start a secure connection with other peer who has public_key and ip_port.
142 returns -1 if failure 149 * returns -1 if failure.
143 returns crypt_connection_id of the initialized connection if everything went well. */ 150 * returns crypt_connection_id of the initialized connection if everything went well.
151 */
144int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port); 152int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port);
145 153
146/* kill a crypto connection 154/* Kill a crypto connection.
147 return 0 if killed successfully 155 * return 0 if killed successfully.
148 return 1 if there was a problem. */ 156 * return 1 if there was a problem.
157 */
149int crypto_kill(Net_Crypto *c, int crypt_connection_id); 158int crypto_kill(Net_Crypto *c, int crypt_connection_id);
150 159
151/* handle an incoming connection 160/* Handle an incoming connection.
152 return -1 if no crypto inbound connection 161 * return -1 if no crypto inbound connection.
153 return incoming connection id (Lossless_UDP one) if there is an incoming crypto connection 162 * return incoming connection id (Lossless_UDP one) if there is an incoming crypto connection.
154 Put the public key of the peer in public_key, the secret_nonce from the handshake into secret_nonce 163 *
155 and the session public key for the connection in session_key 164 * Put the public key of the peer in public_key, the secret_nonce from the handshake into secret_nonce
156 to accept it see: accept_crypto_inbound(...) 165 * and the session public key for the connection in session_key.
157 to refuse it just call kill_connection(...) on the connection id */ 166 * to accept it see: accept_crypto_inbound(...).
167 * to refuse it just call kill_connection(...) on the connection id.
168 */
158int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key); 169int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key);
159 170
160/* accept an incoming connection using the parameters provided by crypto_inbound 171/* Accept an incoming connection using the parameters provided by crypto_inbound.
161 return -1 if not successful 172 * return -1 if not successful.
162 returns the crypt_connection_id if successful */ 173 * returns the crypt_connection_id if successful.
174 */
163int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, uint8_t *secret_nonce, 175int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, uint8_t *secret_nonce,
164 uint8_t *session_key); 176 uint8_t *session_key);
165 177
166/* return 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet 178/* return 0 if no connection.
167 (we have received a handshake but no empty data packet), 3 if the connection is established. 179 * return 1 we have sent a handshake
168 4 if the connection is timed out and waiting to be killed */ 180 * return 2 if connexion is not confirmed yet (we have received a handshake but no empty data packet).
181 * return 3 if the connection is established.
182 * return 4 if the connection is timed out and waiting to be killed.
183 */
169int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id); 184int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id);
170 185
171 186
172/* Generate our public and private keys 187/* Generate our public and private keys.
173 Only call this function the first time the program starts. */ 188 * Only call this function the first time the program starts.
189 */
174void new_keys(Net_Crypto *c); 190void new_keys(Net_Crypto *c);
175 191
176/* save the public and private keys to the keys array 192/* Save the public and private keys to the keys array.
177 Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */ 193 * Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES.
194 */
178void save_keys(Net_Crypto *c, uint8_t *keys); 195void save_keys(Net_Crypto *c, uint8_t *keys);
179 196
180/* load the public and private keys from the keys array 197/* Load the public and private keys from the keys array.
181 Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */ 198 * Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES.
199 */
182void load_keys(Net_Crypto *c, uint8_t *keys); 200void load_keys(Net_Crypto *c, uint8_t *keys);
183 201
184/* create new instance of Net_Crypto 202/* Create new instance of Net_Crypto.
185 sets all the global connection variables to their default values. */ 203 * Sets all the global connection variables to their default values.
204 */
186Net_Crypto *new_net_crypto(Networking_Core *net); 205Net_Crypto *new_net_crypto(Networking_Core *net);
187 206
188/* main loop */ 207/* Main loop. */
189void do_net_crypto(Net_Crypto *c); 208void do_net_crypto(Net_Crypto *c);
190 209
191void kill_net_crypto(Net_Crypto *c); 210void kill_net_crypto(Net_Crypto *c);
192 211
193/* Init the cryptopacket handling */ 212/* Initialize the cryptopacket handling. */
194void init_cryptopackets(void *dht); 213void init_cryptopackets(void *dht);
195 214
196#ifdef __cplusplus 215#ifdef __cplusplus