summaryrefslogtreecommitdiff
path: root/core/net_crypto.h
diff options
context:
space:
mode:
Diffstat (limited to 'core/net_crypto.h')
-rw-r--r--core/net_crypto.h99
1 files changed, 73 insertions, 26 deletions
diff --git a/core/net_crypto.h b/core/net_crypto.h
index 742d9fdc..46bcf250 100644
--- a/core/net_crypto.h
+++ b/core/net_crypto.h
@@ -25,15 +25,54 @@
25#define NET_CRYPTO_H 25#define NET_CRYPTO_H
26 26
27#include "Lossless_UDP.h" 27#include "Lossless_UDP.h"
28#include "DHT.h"
29 28
30#ifdef __cplusplus 29#ifdef __cplusplus
31extern "C" { 30extern "C" {
32#endif 31#endif
33 32
34/* Our public key. */ 33#define MAX_INCOMING 64
35extern uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; 34
36extern uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; 35typedef struct {
36 uint8_t public_key[crypto_box_PUBLICKEYBYTES]; /* the real public key of the peer. */
37 uint8_t recv_nonce[crypto_box_NONCEBYTES]; /* nonce of received packets */
38 uint8_t sent_nonce[crypto_box_NONCEBYTES]; /* nonce of sent packets. */
39 uint8_t sessionpublic_key[crypto_box_PUBLICKEYBYTES]; /* our public key for this session. */
40 uint8_t sessionsecret_key[crypto_box_SECRETKEYBYTES]; /* our private key for this session. */
41 uint8_t peersessionpublic_key[crypto_box_PUBLICKEYBYTES]; /* The public key of the peer. */
42 uint8_t shared_key[crypto_box_BEFORENMBYTES]; /* the precomputed shared key from encrypt_precompute */
43 uint8_t status; /* 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet
44 (we have received a handshake but no empty data packet), 3 if the connection is established.
45 4 if the connection is timed out. */
46 uint16_t number; /* Lossless_UDP connection number corresponding to this connection. */
47
48} Crypto_Connection;
49
50typedef int (*cryptopacket_handler_callback)(void *object, IP_Port ip_port, uint8_t *source_pubkey, uint8_t *data,
51 uint32_t len);
52
53typedef struct {
54 cryptopacket_handler_callback function;
55 void *object;
56} Cryptopacket_Handles;
57
58typedef struct {
59 Lossless_UDP *lossless_udp;
60
61 Crypto_Connection *crypto_connections;
62
63 uint32_t crypto_connections_length; /* Length of connections array */
64
65 /* Our public and secret keys. */
66 uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
67 uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
68
69 /* keeps track of the connection numbers for friends request so we can check later if they were sent */
70 int incoming_connections[MAX_INCOMING];
71
72 Cryptopacket_Handles cryptopackethandlers[256];
73} Net_Crypto;
74
75#include "DHT.h"
37 76
38#define ENCRYPTION_PADDING (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES) 77#define ENCRYPTION_PADDING (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES)
39 78
@@ -75,34 +114,36 @@ void random_nonce(uint8_t *nonce);
75/* return 0 if there is no received data in the buffer 114/* return 0 if there is no received data in the buffer
76 return -1 if the packet was discarded. 115 return -1 if the packet was discarded.
77 return length of received data if successful */ 116 return length of received data if successful */
78int read_cryptpacket(int crypt_connection_id, uint8_t *data); 117int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data);
79 118
80/* return 0 if data could not be put in packet queue 119/* return 0 if data could not be put in packet queue
81 return 1 if data was put into the queue */ 120 return 1 if data was put into the queue */
82int write_cryptpacket(int crypt_connection_id, uint8_t *data, uint32_t length); 121int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length);
83 122
84/* create a request to peer with public_key. 123/* create a request to peer.
85 packet must be an array of MAX_DATA_SIZE big. 124 send_public_key and send_secret_key are the pub/secret keys of the sender
86 Data represents the data we send with the request with length being the length of the data. 125 recv_public_key is public key of reciever
87 request_id is the id of the request (32 = friend request, 254 = ping request) 126 packet must be an array of MAX_DATA_SIZE big.
88 returns -1 on failure 127 Data represents the data we send with the request with length being the length of the data.
89 returns the length of the created packet on success */ 128 request_id is the id of the request (32 = friend request, 254 = ping request)
90int create_request(uint8_t *packet, uint8_t *public_key, uint8_t *data, uint32_t length, uint8_t request_id); 129 returns -1 on failure
130 returns the length of the created packet on success */
131int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *packet, uint8_t *recv_public_key,
132 uint8_t *data, uint32_t length, uint8_t request_id);
91 133
92 134
93typedef int (*cryptopacket_handler_callback)(IP_Port ip_port, uint8_t *source_pubkey, uint8_t *data, uint32_t len);
94/* Function to call when request beginning with byte is received */ 135/* Function to call when request beginning with byte is received */
95void cryptopacket_registerhandler(uint8_t byte, cryptopacket_handler_callback cb); 136void cryptopacket_registerhandler(Net_Crypto *c, uint8_t byte, cryptopacket_handler_callback cb, void *object);
96 137
97/* Start a secure connection with other peer who has public_key and ip_port 138/* Start a secure connection with other peer who has public_key and ip_port
98 returns -1 if failure 139 returns -1 if failure
99 returns crypt_connection_id of the initialized connection if everything went well. */ 140 returns crypt_connection_id of the initialized connection if everything went well. */
100int crypto_connect(uint8_t *public_key, IP_Port ip_port); 141int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port);
101 142
102/* kill a crypto connection 143/* kill a crypto connection
103 return 0 if killed successfully 144 return 0 if killed successfully
104 return 1 if there was a problem. */ 145 return 1 if there was a problem. */
105int crypto_kill(int crypt_connection_id); 146int crypto_kill(Net_Crypto *c, int crypt_connection_id);
106 147
107/* handle an incoming connection 148/* handle an incoming connection
108 return -1 if no crypto inbound connection 149 return -1 if no crypto inbound connection
@@ -111,37 +152,43 @@ int crypto_kill(int crypt_connection_id);
111 and the session public key for the connection in session_key 152 and the session public key for the connection in session_key
112 to accept it see: accept_crypto_inbound(...) 153 to accept it see: accept_crypto_inbound(...)
113 to refuse it just call kill_connection(...) on the connection id */ 154 to refuse it just call kill_connection(...) on the connection id */
114int crypto_inbound(uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key); 155int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key);
115 156
116/* accept an incoming connection using the parameters provided by crypto_inbound 157/* accept an incoming connection using the parameters provided by crypto_inbound
117 return -1 if not successful 158 return -1 if not successful
118 returns the crypt_connection_id if successful */ 159 returns the crypt_connection_id if successful */
119int accept_crypto_inbound(int connection_id, uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key); 160int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, uint8_t *secret_nonce,
161 uint8_t *session_key);
120 162
121/* return 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet 163/* return 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet
122 (we have received a handshake but no empty data packet), 3 if the connection is established. 164 (we have received a handshake but no empty data packet), 3 if the connection is established.
123 4 if the connection is timed out and waiting to be killed */ 165 4 if the connection is timed out and waiting to be killed */
124int is_cryptoconnected(int crypt_connection_id); 166int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id);
125 167
126 168
127/* Generate our public and private keys 169/* Generate our public and private keys
128 Only call this function the first time the program starts. */ 170 Only call this function the first time the program starts. */
129void new_keys(void); 171void new_keys(Net_Crypto *c);
130 172
131/* save the public and private keys to the keys array 173/* save the public and private keys to the keys array
132 Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */ 174 Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */
133void save_keys(uint8_t *keys); 175void save_keys(Net_Crypto *c, uint8_t *keys);
134 176
135/* load the public and private keys from the keys array 177/* load the public and private keys from the keys array
136 Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */ 178 Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */
137void load_keys(uint8_t *keys); 179void load_keys(Net_Crypto *c, uint8_t *keys);
138 180
139/* run this to (re)initialize net_crypto 181/* create new instance of Net_Crypto
140 sets all the global connection variables to their default values. */ 182 sets all the global connection variables to their default values. */
141void initNetCrypto(void); 183Net_Crypto *new_net_crypto(Networking_Core *net);
142 184
143/* main loop */ 185/* main loop */
144void doNetCrypto(void); 186void do_net_crypto(Net_Crypto *c);
187
188void kill_net_crypto(Net_Crypto *c);
189
190/* Init the cryptopacket handling */
191void init_cryptopackets(void *dht);
145 192
146#ifdef __cplusplus 193#ifdef __cplusplus
147} 194}