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