diff options
Diffstat (limited to 'core/net_crypto.c')
-rw-r--r-- | core/net_crypto.c | 222 |
1 files changed, 111 insertions, 111 deletions
diff --git a/core/net_crypto.c b/core/net_crypto.c index fe03b5ee..270c969a 100644 --- a/core/net_crypto.c +++ b/core/net_crypto.c | |||
@@ -28,23 +28,23 @@ | |||
28 | #include "net_crypto.h" | 28 | #include "net_crypto.h" |
29 | 29 | ||
30 | 30 | ||
31 | //Our public and secret keys. | 31 | /* Our public and secret keys. */ |
32 | uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; | 32 | uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; |
33 | uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; | 33 | uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; |
34 | 34 | ||
35 | 35 | ||
36 | typedef struct | 36 | typedef struct |
37 | { | 37 | { |
38 | uint8_t public_key[crypto_box_PUBLICKEYBYTES];//the real public key of the peer. | 38 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; /* the real public key of the peer. */ |
39 | uint8_t recv_nonce[crypto_box_NONCEBYTES];//nonce of received packets | 39 | uint8_t recv_nonce[crypto_box_NONCEBYTES]; /* nonce of received packets */ |
40 | uint8_t sent_nonce[crypto_box_NONCEBYTES];//nonce of sent packets. | 40 | uint8_t sent_nonce[crypto_box_NONCEBYTES]; /* nonce of sent packets. */ |
41 | uint8_t sessionpublic_key[crypto_box_PUBLICKEYBYTES];//our public key for this session. | 41 | uint8_t sessionpublic_key[crypto_box_PUBLICKEYBYTES]; /* our public key for this session. */ |
42 | uint8_t sessionsecret_key[crypto_box_SECRETKEYBYTES];//our private key for this session. | 42 | uint8_t sessionsecret_key[crypto_box_SECRETKEYBYTES]; /* our private key for this session. */ |
43 | uint8_t peersessionpublic_key[crypto_box_PUBLICKEYBYTES];//The public key of the peer. | 43 | uint8_t peersessionpublic_key[crypto_box_PUBLICKEYBYTES]; /* The public key of the peer. */ |
44 | uint8_t status;//0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet | 44 | uint8_t status; /* 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet |
45 | //(we have received a handshake but no empty data packet), 3 if the connection is established. | 45 | (we have received a handshake but no empty data packet), 3 if the connection is established. |
46 | //4 if the connection is timed out. | 46 | 4 if the connection is timed out. */ |
47 | uint16_t number; //Lossless_UDP connection number corresponding to this connection. | 47 | uint16_t number; /* Lossless_UDP connection number corresponding to this connection. */ |
48 | 48 | ||
49 | }Crypto_Connection; | 49 | }Crypto_Connection; |
50 | 50 | ||
@@ -54,18 +54,18 @@ static Crypto_Connection crypto_connections[MAX_CRYPTO_CONNECTIONS]; | |||
54 | 54 | ||
55 | #define MAX_FRIEND_REQUESTS 32 | 55 | #define MAX_FRIEND_REQUESTS 32 |
56 | 56 | ||
57 | //keeps track of the connection numbers for friends request so we can check later if they were sent | 57 | /* keeps track of the connection numbers for friends request so we can check later if they were sent */ |
58 | static int outbound_friendrequests[MAX_FRIEND_REQUESTS]; | 58 | static int outbound_friendrequests[MAX_FRIEND_REQUESTS]; |
59 | 59 | ||
60 | #define MAX_INCOMING 64 | 60 | #define MAX_INCOMING 64 |
61 | 61 | ||
62 | //keeps track of the connection numbers for friends request so we can check later if they were sent | 62 | /* keeps track of the connection numbers for friends request so we can check later if they were sent */ |
63 | static int incoming_connections[MAX_INCOMING]; | 63 | static int incoming_connections[MAX_INCOMING]; |
64 | 64 | ||
65 | //encrypts plain of length length to encrypted of length + 16 using the | 65 | /* encrypts plain of length length to encrypted of length + 16 using the |
66 | //public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce | 66 | public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce |
67 | //return -1 if there was a problem. | 67 | return -1 if there was a problem. |
68 | //return length of encrypted data if everything was fine. | 68 | return length of encrypted data if everything was fine. */ |
69 | int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, | 69 | int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, |
70 | uint8_t * plain, uint32_t length, uint8_t * encrypted) | 70 | uint8_t * plain, uint32_t length, uint8_t * encrypted) |
71 | { | 71 | { |
@@ -78,24 +78,24 @@ int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, | |||
78 | uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_ZEROBYTES]; | 78 | uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_ZEROBYTES]; |
79 | uint8_t zeroes[crypto_box_BOXZEROBYTES] = {0}; | 79 | uint8_t zeroes[crypto_box_BOXZEROBYTES] = {0}; |
80 | 80 | ||
81 | memcpy(temp_plain + crypto_box_ZEROBYTES, plain, length);//pad the message with 32 0 bytes. | 81 | memcpy(temp_plain + crypto_box_ZEROBYTES, plain, length); /* pad the message with 32 0 bytes. */ |
82 | 82 | ||
83 | crypto_box(temp_encrypted, temp_plain, length + crypto_box_ZEROBYTES, nonce, public_key, secret_key); | 83 | crypto_box(temp_encrypted, temp_plain, length + crypto_box_ZEROBYTES, nonce, public_key, secret_key); |
84 | 84 | ||
85 | //if encryption is successful the first crypto_box_BOXZEROBYTES of the message will be zero | 85 | /* if encryption is successful the first crypto_box_BOXZEROBYTES of the message will be zero */ |
86 | if(memcmp(temp_encrypted, zeroes, crypto_box_BOXZEROBYTES) != 0) | 86 | if(memcmp(temp_encrypted, zeroes, crypto_box_BOXZEROBYTES) != 0) |
87 | { | 87 | { |
88 | return -1; | 88 | return -1; |
89 | } | 89 | } |
90 | //unpad the encrypted message | 90 | /* unpad the encrypted message */ |
91 | memcpy(encrypted, temp_encrypted + crypto_box_BOXZEROBYTES, length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES); | 91 | memcpy(encrypted, temp_encrypted + crypto_box_BOXZEROBYTES, length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES); |
92 | return length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES; | 92 | return length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES; |
93 | } | 93 | } |
94 | 94 | ||
95 | //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 |
96 | //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 |
97 | //return -1 if there was a problem(decryption failed) | 97 | return -1 if there was a problem(decryption failed) |
98 | //return length of plain data if everything was fine. | 98 | return length of plain data if everything was fine. */ |
99 | int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, | 99 | int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, |
100 | uint8_t * encrypted, uint32_t length, uint8_t * plain) | 100 | uint8_t * encrypted, uint32_t length, uint8_t * plain) |
101 | { | 101 | { |
@@ -107,24 +107,24 @@ int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, | |||
107 | uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_ZEROBYTES] = {0}; | 107 | uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_ZEROBYTES] = {0}; |
108 | uint8_t zeroes[crypto_box_ZEROBYTES] = {0}; | 108 | uint8_t zeroes[crypto_box_ZEROBYTES] = {0}; |
109 | 109 | ||
110 | memcpy(temp_encrypted + crypto_box_BOXZEROBYTES, encrypted, length);//pad the message with 16 0 bytes. | 110 | memcpy(temp_encrypted + crypto_box_BOXZEROBYTES, encrypted, length); /* pad the message with 16 0 bytes. */ |
111 | 111 | ||
112 | if(crypto_box_open(temp_plain, temp_encrypted, length + crypto_box_BOXZEROBYTES, | 112 | if(crypto_box_open(temp_plain, temp_encrypted, length + crypto_box_BOXZEROBYTES, |
113 | nonce, public_key, secret_key) == -1) | 113 | nonce, public_key, secret_key) == -1) |
114 | { | 114 | { |
115 | return -1; | 115 | return -1; |
116 | } | 116 | } |
117 | //if decryption is successful the first crypto_box_ZEROBYTES of the message will be zero | 117 | /* if decryption is successful the first crypto_box_ZEROBYTES of the message will be zero */ |
118 | if(memcmp(temp_plain, zeroes, crypto_box_ZEROBYTES) != 0) | 118 | if(memcmp(temp_plain, zeroes, crypto_box_ZEROBYTES) != 0) |
119 | { | 119 | { |
120 | return -1; | 120 | return -1; |
121 | } | 121 | } |
122 | //unpad the plain message | 122 | /* unpad the plain message */ |
123 | memcpy(plain, temp_plain + crypto_box_ZEROBYTES, length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES); | 123 | memcpy(plain, temp_plain + crypto_box_ZEROBYTES, length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES); |
124 | return length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES; | 124 | return length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES; |
125 | } | 125 | } |
126 | 126 | ||
127 | //increment the given nonce by 1 | 127 | /* increment the given nonce by 1 */ |
128 | void increment_nonce(uint8_t * nonce) | 128 | void increment_nonce(uint8_t * nonce) |
129 | { | 129 | { |
130 | uint32_t i; | 130 | uint32_t i; |
@@ -138,8 +138,8 @@ void increment_nonce(uint8_t * nonce) | |||
138 | } | 138 | } |
139 | } | 139 | } |
140 | 140 | ||
141 | //fill the given nonce with random bytes. | 141 | /* fill the given nonce with random bytes. |
142 | //TODO: make this more optimized | 142 | TODO: make this more optimized */ |
143 | void random_nonce(uint8_t * nonce) | 143 | void random_nonce(uint8_t * nonce) |
144 | { | 144 | { |
145 | uint32_t i; | 145 | uint32_t i; |
@@ -149,9 +149,9 @@ void random_nonce(uint8_t * nonce) | |||
149 | } | 149 | } |
150 | } | 150 | } |
151 | 151 | ||
152 | //return 0 if there is no received data in the buffer | 152 | /* return 0 if there is no received data in the buffer |
153 | //return -1 if the packet was discarded. | 153 | return -1 if the packet was discarded. |
154 | //return length of received data if successful | 154 | return length of received data if successful */ |
155 | int read_cryptpacket(int crypt_connection_id, uint8_t * data) | 155 | int read_cryptpacket(int crypt_connection_id, uint8_t * data) |
156 | { | 156 | { |
157 | if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) | 157 | if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) |
@@ -184,8 +184,8 @@ int read_cryptpacket(int crypt_connection_id, uint8_t * data) | |||
184 | } | 184 | } |
185 | 185 | ||
186 | 186 | ||
187 | //return 0 if data could not be put in packet queue | 187 | /* return 0 if data could not be put in packet queue |
188 | //return 1 if data was put into the queue | 188 | return 1 if data was put into the queue */ |
189 | int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length) | 189 | int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length) |
190 | { | 190 | { |
191 | if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) | 191 | if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) |
@@ -217,10 +217,10 @@ int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length) | |||
217 | return 1; | 217 | return 1; |
218 | } | 218 | } |
219 | 219 | ||
220 | //send a friend request to peer with public_key and ip_port. | 220 | /* send a friend request to peer with public_key and ip_port. |
221 | //Data represents the data we send with the friends request. | 221 | Data represents the data we send with the friends request. |
222 | //returns -1 on failure | 222 | returns -1 on failure |
223 | //returns a positive friend request id that can be used later to see if it was sent correctly on success. | 223 | returns a positive friend request id that can be used later to see if it was sent correctly on success. */ |
224 | int send_friendrequest(uint8_t * public_key, IP_Port ip_port, uint8_t * data, uint32_t length) | 224 | int send_friendrequest(uint8_t * public_key, IP_Port ip_port, uint8_t * data, uint32_t length) |
225 | { | 225 | { |
226 | if(length > MAX_DATA_SIZE - 1 - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES) | 226 | if(length > MAX_DATA_SIZE - 1 - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES) |
@@ -264,10 +264,10 @@ int send_friendrequest(uint8_t * public_key, IP_Port ip_port, uint8_t * data, ui | |||
264 | return -1; | 264 | return -1; |
265 | } | 265 | } |
266 | 266 | ||
267 | //return -1 if failure | 267 | /* return -1 if failure |
268 | //return 0 if connection is still trying to send the request. | 268 | return 0 if connection is still trying to send the request. |
269 | //return 1 if sent correctly | 269 | return 1 if sent correctly |
270 | //return 2 if connection timed out | 270 | return 2 if connection timed out */ |
271 | int check_friendrequest(int friend_request) | 271 | int check_friendrequest(int friend_request) |
272 | { | 272 | { |
273 | if(friend_request < 0 || friend_request > MAX_FRIEND_REQUESTS) | 273 | if(friend_request < 0 || friend_request > MAX_FRIEND_REQUESTS) |
@@ -299,9 +299,9 @@ int check_friendrequest(int friend_request) | |||
299 | return 0; | 299 | return 0; |
300 | } | 300 | } |
301 | 301 | ||
302 | //Send a crypto handshake packet containing an encrypted secret nonce and session public key | 302 | /* Send a crypto handshake packet containing an encrypted secret nonce and session public key |
303 | //to peer with connection_id and public_key | 303 | to peer with connection_id and public_key |
304 | //the packet is encrypted with a random nonce which is sent in plain text with the packet | 304 | the packet is encrypted with a random nonce which is sent in plain text with the packet */ |
305 | int send_cryptohandshake(int connection_id, uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key) | 305 | int send_cryptohandshake(int connection_id, uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key) |
306 | { | 306 | { |
307 | uint8_t temp_data[MAX_DATA_SIZE]; | 307 | uint8_t temp_data[MAX_DATA_SIZE]; |
@@ -324,9 +324,9 @@ int send_cryptohandshake(int connection_id, uint8_t * public_key, uint8_t * secr | |||
324 | return write_packet(connection_id, temp_data, len + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES); | 324 | return write_packet(connection_id, temp_data, len + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES); |
325 | } | 325 | } |
326 | 326 | ||
327 | //Extract secret nonce, session public key and public_key from a packet(data) with length length | 327 | /* Extract secret nonce, session public key and public_key from a packet(data) with length length |
328 | //return 1 if successful | 328 | return 1 if successful |
329 | //return 0 if failure | 329 | return 0 if failure */ |
330 | int handle_cryptohandshake(uint8_t * public_key, uint8_t * secret_nonce, | 330 | int handle_cryptohandshake(uint8_t * public_key, uint8_t * secret_nonce, |
331 | uint8_t * session_key, uint8_t * data, uint16_t length) | 331 | uint8_t * session_key, uint8_t * data, uint16_t length) |
332 | { | 332 | { |
@@ -359,9 +359,9 @@ int handle_cryptohandshake(uint8_t * public_key, uint8_t * secret_nonce, | |||
359 | } | 359 | } |
360 | 360 | ||
361 | 361 | ||
362 | //puts the public key of the friend if public_key, the data from the request | 362 | /* puts the public key of the friend if public_key, the data from the request |
363 | //in data if a friend request was sent to us and returns the length of the data. | 363 | in data if a friend request was sent to us and returns the length of the data. |
364 | //return -1 if no valid friend requests. | 364 | return -1 if no valid friend requests. */ |
365 | int handle_friendrequest(uint8_t * public_key, uint8_t * data) | 365 | int handle_friendrequest(uint8_t * public_key, uint8_t * data) |
366 | { | 366 | { |
367 | uint32_t i; | 367 | uint32_t i; |
@@ -384,12 +384,12 @@ int handle_friendrequest(uint8_t * public_key, uint8_t * data) | |||
384 | if(len1 != -1) | 384 | if(len1 != -1) |
385 | { | 385 | { |
386 | kill_connection(incoming_connections[i]); | 386 | kill_connection(incoming_connections[i]); |
387 | //kill_connection_in(incoming_connections[i], 1); //conection is useless now, kill it in 1 seconds | 387 | /* kill_connection_in(incoming_connections[i], 1); //conection is useless now, kill it in 1 seconds */ |
388 | incoming_connections[i] = -1; | 388 | incoming_connections[i] = -1; |
389 | return len1; | 389 | return len1; |
390 | } | 390 | } |
391 | } | 391 | } |
392 | kill_connection(incoming_connections[i]); //conection is useless now, kill it. | 392 | kill_connection(incoming_connections[i]); /* conection is useless now, kill it. */ |
393 | incoming_connections[i] = -1; | 393 | incoming_connections[i] = -1; |
394 | } | 394 | } |
395 | } | 395 | } |
@@ -397,9 +397,9 @@ int handle_friendrequest(uint8_t * public_key, uint8_t * data) | |||
397 | return -1; | 397 | return -1; |
398 | } | 398 | } |
399 | 399 | ||
400 | //get crypto connection id from public key of peer | 400 | /* get crypto connection id from public key of peer |
401 | //return -1 if there are no connections like we are looking for | 401 | return -1 if there are no connections like we are looking for |
402 | //return id if it found it | 402 | return id if it found it */ |
403 | int getcryptconnection_id(uint8_t * public_key) | 403 | int getcryptconnection_id(uint8_t * public_key) |
404 | { | 404 | { |
405 | uint32_t i; | 405 | uint32_t i; |
@@ -417,9 +417,9 @@ int getcryptconnection_id(uint8_t * public_key) | |||
417 | } | 417 | } |
418 | 418 | ||
419 | 419 | ||
420 | //Start a secure connection with other peer who has public_key and ip_port | 420 | /* Start a secure connection with other peer who has public_key and ip_port |
421 | //returns -1 if failure | 421 | returns -1 if failure |
422 | //returns crypt_connection_id of the initialized connection if everything went well. | 422 | returns crypt_connection_id of the initialized connection if everything went well. */ |
423 | int crypto_connect(uint8_t * public_key, IP_Port ip_port) | 423 | int crypto_connect(uint8_t * public_key, IP_Port ip_port) |
424 | { | 424 | { |
425 | uint32_t i; | 425 | uint32_t i; |
@@ -453,19 +453,19 @@ int crypto_connect(uint8_t * public_key, IP_Port ip_port) | |||
453 | increment_nonce(crypto_connections[i].recv_nonce); | 453 | increment_nonce(crypto_connections[i].recv_nonce); |
454 | return i; | 454 | return i; |
455 | } | 455 | } |
456 | return -1;//this should never happen. | 456 | return -1; /* this should never happen. */ |
457 | } | 457 | } |
458 | } | 458 | } |
459 | return -1; | 459 | return -1; |
460 | } | 460 | } |
461 | 461 | ||
462 | //handle an incoming connection | 462 | /* handle an incoming connection |
463 | //return -1 if no crypto inbound connection | 463 | return -1 if no crypto inbound connection |
464 | //return incoming connection id (Lossless_UDP one) if there is an incoming crypto connection | 464 | return incoming connection id (Lossless_UDP one) if there is an incoming crypto connection |
465 | //Put the public key of the peer in public_key, the secret_nonce from the handshake into secret_nonce | 465 | Put the public key of the peer in public_key, the secret_nonce from the handshake into secret_nonce |
466 | //and the session public key for the connection in session_key | 466 | and the session public key for the connection in session_key |
467 | //to accept it see: accept_crypto_inbound(...) | 467 | to accept it see: accept_crypto_inbound(...) |
468 | //to refuse it just call kill_connection(...) on the connection id | 468 | to refuse it just call kill_connection(...) on the connection id */ |
469 | int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key) | 469 | int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key) |
470 | { | 470 | { |
471 | uint32_t i; | 471 | uint32_t i; |
@@ -486,7 +486,7 @@ int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * sessi | |||
486 | if(handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) | 486 | if(handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) |
487 | { | 487 | { |
488 | int connection_id = incoming_connections[i]; | 488 | int connection_id = incoming_connections[i]; |
489 | incoming_connections[i] = -1;//remove this connection from the incoming connection list. | 489 | incoming_connections[i] = -1; /* remove this connection from the incoming connection list. */ |
490 | return connection_id; | 490 | return connection_id; |
491 | } | 491 | } |
492 | } | 492 | } |
@@ -495,9 +495,9 @@ int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * sessi | |||
495 | return -1; | 495 | return -1; |
496 | } | 496 | } |
497 | 497 | ||
498 | //kill a crypto connection | 498 | /* kill a crypto connection |
499 | //return 0 if killed successfully | 499 | return 0 if killed successfully |
500 | //return 1 if there was a problem. | 500 | return 1 if there was a problem. */ |
501 | int crypto_kill(int crypt_connection_id) | 501 | int crypto_kill(int crypt_connection_id) |
502 | { | 502 | { |
503 | if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) | 503 | if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) |
@@ -515,9 +515,9 @@ int crypto_kill(int crypt_connection_id) | |||
515 | } | 515 | } |
516 | 516 | ||
517 | 517 | ||
518 | //accept an incoming connection using the parameters provided by crypto_inbound | 518 | /* accept an incoming connection using the parameters provided by crypto_inbound |
519 | //return -1 if not successful | 519 | return -1 if not successful |
520 | //returns the crypt_connection_id if successful | 520 | returns the crypt_connection_id if successful */ |
521 | int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key) | 521 | int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key) |
522 | { | 522 | { |
523 | uint32_t i; | 523 | uint32_t i; |
@@ -549,20 +549,20 @@ int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * sec | |||
549 | { | 549 | { |
550 | increment_nonce(crypto_connections[i].recv_nonce); | 550 | increment_nonce(crypto_connections[i].recv_nonce); |
551 | uint32_t zero = 0; | 551 | uint32_t zero = 0; |
552 | crypto_connections[i].status = 3;//connection status needs to be 3 for write_cryptpacket() to work | 552 | crypto_connections[i].status = 3; /* connection status needs to be 3 for write_cryptpacket() to work */ |
553 | write_cryptpacket(i, ((uint8_t *)&zero), sizeof(zero)); | 553 | write_cryptpacket(i, ((uint8_t *)&zero), sizeof(zero)); |
554 | crypto_connections[i].status = 2;//set it to its proper value right after. | 554 | crypto_connections[i].status = 2; /* set it to its proper value right after. */ |
555 | return i; | 555 | return i; |
556 | } | 556 | } |
557 | return -1;//this should never happen. | 557 | return -1; /* this should never happen. */ |
558 | } | 558 | } |
559 | } | 559 | } |
560 | return -1; | 560 | return -1; |
561 | } | 561 | } |
562 | 562 | ||
563 | //return 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet | 563 | /* return 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet |
564 | //(we have received a handshake but no empty data packet), 3 if the connection is established. | 564 | (we have received a handshake but no empty data packet), 3 if the connection is established. |
565 | //4 if the connection is timed out and waiting to be killed | 565 | 4 if the connection is timed out and waiting to be killed */ |
566 | int is_cryptoconnected(int crypt_connection_id) | 566 | int is_cryptoconnected(int crypt_connection_id) |
567 | { | 567 | { |
568 | if(crypt_connection_id >= 0 && crypt_connection_id < MAX_CRYPTO_CONNECTIONS) | 568 | if(crypt_connection_id >= 0 && crypt_connection_id < MAX_CRYPTO_CONNECTIONS) |
@@ -573,33 +573,33 @@ int is_cryptoconnected(int crypt_connection_id) | |||
573 | } | 573 | } |
574 | 574 | ||
575 | 575 | ||
576 | //Generate our public and private keys | 576 | /* Generate our public and private keys |
577 | //Only call this function the first time the program starts. | 577 | Only call this function the first time the program starts. */ |
578 | void new_keys() | 578 | void new_keys() |
579 | { | 579 | { |
580 | crypto_box_keypair(self_public_key,self_secret_key); | 580 | crypto_box_keypair(self_public_key,self_secret_key); |
581 | } | 581 | } |
582 | 582 | ||
583 | //save the public and private keys to the keys array | 583 | /* save the public and private keys to the keys array |
584 | //Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES | 584 | Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */ |
585 | void save_keys(uint8_t * keys) | 585 | void save_keys(uint8_t * keys) |
586 | { | 586 | { |
587 | memcpy(keys, self_public_key, crypto_box_PUBLICKEYBYTES); | 587 | memcpy(keys, self_public_key, crypto_box_PUBLICKEYBYTES); |
588 | memcpy(keys + crypto_box_PUBLICKEYBYTES, self_secret_key, crypto_box_SECRETKEYBYTES); | 588 | memcpy(keys + crypto_box_PUBLICKEYBYTES, self_secret_key, crypto_box_SECRETKEYBYTES); |
589 | } | 589 | } |
590 | 590 | ||
591 | //load the public and private keys from the keys array | 591 | /* load the public and private keys from the keys array |
592 | //Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES | 592 | Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */ |
593 | void load_keys(uint8_t * keys) | 593 | void load_keys(uint8_t * keys) |
594 | { | 594 | { |
595 | memcpy(self_public_key, keys, crypto_box_PUBLICKEYBYTES); | 595 | memcpy(self_public_key, keys, crypto_box_PUBLICKEYBYTES); |
596 | memcpy(self_secret_key, keys + crypto_box_PUBLICKEYBYTES, crypto_box_SECRETKEYBYTES); | 596 | memcpy(self_secret_key, keys + crypto_box_PUBLICKEYBYTES, crypto_box_SECRETKEYBYTES); |
597 | } | 597 | } |
598 | 598 | ||
599 | //TODO: optimize this | 599 | /* TODO: optimize this |
600 | //adds an incoming connection to the incoming_connection list. | 600 | adds an incoming connection to the incoming_connection list. |
601 | //returns 0 if successful | 601 | returns 0 if successful |
602 | //returns 1 if failure | 602 | returns 1 if failure */ |
603 | int new_incoming(int id) | 603 | int new_incoming(int id) |
604 | { | 604 | { |
605 | uint32_t i; | 605 | uint32_t i; |
@@ -614,8 +614,8 @@ int new_incoming(int id) | |||
614 | return 1; | 614 | return 1; |
615 | } | 615 | } |
616 | 616 | ||
617 | //TODO: optimize this | 617 | /* TODO: optimize this |
618 | //handle all new incoming connections. | 618 | handle all new incoming connections. */ |
619 | static void handle_incomings() | 619 | static void handle_incomings() |
620 | { | 620 | { |
621 | int income; | 621 | int income; |
@@ -629,7 +629,7 @@ static void handle_incomings() | |||
629 | } | 629 | } |
630 | } | 630 | } |
631 | 631 | ||
632 | //handle received packets for not yet established crypto connections. | 632 | /* handle received packets for not yet established crypto connections. */ |
633 | static void receive_crypto() | 633 | static void receive_crypto() |
634 | { | 634 | { |
635 | uint32_t i; | 635 | uint32_t i; |
@@ -643,12 +643,12 @@ static void receive_crypto() | |||
643 | uint8_t session_key[crypto_box_PUBLICKEYBYTES]; | 643 | uint8_t session_key[crypto_box_PUBLICKEYBYTES]; |
644 | uint16_t len; | 644 | uint16_t len; |
645 | if(id_packet(crypto_connections[i].number) == 1) | 645 | if(id_packet(crypto_connections[i].number) == 1) |
646 | //if the packet is a friend request drop it (because we are already friends) | 646 | /* if the packet is a friend request drop it (because we are already friends) */ |
647 | { | 647 | { |
648 | len = read_packet(crypto_connections[i].number, temp_data); | 648 | len = read_packet(crypto_connections[i].number, temp_data); |
649 | 649 | ||
650 | } | 650 | } |
651 | if(id_packet(crypto_connections[i].number) == 2)//handle handshake packet. | 651 | if(id_packet(crypto_connections[i].number) == 2) /* handle handshake packet. */ |
652 | { | 652 | { |
653 | len = read_packet(crypto_connections[i].number, temp_data); | 653 | len = read_packet(crypto_connections[i].number, temp_data); |
654 | if(handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) | 654 | if(handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) |
@@ -659,16 +659,16 @@ static void receive_crypto() | |||
659 | memcpy(crypto_connections[i].peersessionpublic_key, session_key, crypto_box_PUBLICKEYBYTES); | 659 | memcpy(crypto_connections[i].peersessionpublic_key, session_key, crypto_box_PUBLICKEYBYTES); |
660 | increment_nonce(crypto_connections[i].sent_nonce); | 660 | increment_nonce(crypto_connections[i].sent_nonce); |
661 | uint32_t zero = 0; | 661 | uint32_t zero = 0; |
662 | crypto_connections[i].status = 3;//connection status needs to be 3 for write_cryptpacket() to work | 662 | crypto_connections[i].status = 3; /* connection status needs to be 3 for write_cryptpacket() to work */ |
663 | write_cryptpacket(i, ((uint8_t *)&zero), sizeof(zero)); | 663 | write_cryptpacket(i, ((uint8_t *)&zero), sizeof(zero)); |
664 | crypto_connections[i].status = 2;//set it to its proper value right after. | 664 | crypto_connections[i].status = 2; /* set it to its proper value right after. */ |
665 | } | 665 | } |
666 | } | 666 | } |
667 | } | 667 | } |
668 | else if(id_packet(crypto_connections[i].number) != -1) | 668 | else if(id_packet(crypto_connections[i].number) != -1) |
669 | { | 669 | { |
670 | //This should not happen | 670 | /* This should not happen |
671 | //kill the connection if it does | 671 | kill the connection if it does */ |
672 | crypto_kill(crypto_connections[i].number); | 672 | crypto_kill(crypto_connections[i].number); |
673 | } | 673 | } |
674 | 674 | ||
@@ -689,28 +689,28 @@ static void receive_crypto() | |||
689 | increment_nonce(crypto_connections[i].recv_nonce); | 689 | increment_nonce(crypto_connections[i].recv_nonce); |
690 | crypto_connections[i].status = 3; | 690 | crypto_connections[i].status = 3; |
691 | 691 | ||
692 | //connection is accepted so we disable the auto kill by setting it to about 1 month from now. | 692 | /* connection is accepted so we disable the auto kill by setting it to about 1 month from now. */ |
693 | kill_connection_in(crypto_connections[i].number, 3000000); | 693 | kill_connection_in(crypto_connections[i].number, 3000000); |
694 | } | 694 | } |
695 | else | 695 | else |
696 | { | 696 | { |
697 | //This should not happen | 697 | /* This should not happen |
698 | //kill the connection if it does | 698 | kill the connection if it does */ |
699 | crypto_kill(crypto_connections[i].number); | 699 | crypto_kill(crypto_connections[i].number); |
700 | } | 700 | } |
701 | } | 701 | } |
702 | else if(id_packet(crypto_connections[i].number) != -1) | 702 | else if(id_packet(crypto_connections[i].number) != -1) |
703 | { | 703 | { |
704 | //This should not happen | 704 | /* This should not happen |
705 | //kill the connection if it does | 705 | kill the connection if it does */ |
706 | crypto_kill(crypto_connections[i].number); | 706 | crypto_kill(crypto_connections[i].number); |
707 | } | 707 | } |
708 | } | 708 | } |
709 | } | 709 | } |
710 | } | 710 | } |
711 | 711 | ||
712 | //run this to (re)initialize net_crypto | 712 | /* run this to (re)initialize net_crypto |
713 | //sets all the global connection variables to their default values. | 713 | sets all the global connection variables to their default values. */ |
714 | void initNetCrypto() | 714 | void initNetCrypto() |
715 | { | 715 | { |
716 | memset(crypto_connections, 0 ,sizeof(crypto_connections)); | 716 | memset(crypto_connections, 0 ,sizeof(crypto_connections)); |
@@ -740,12 +740,12 @@ static void killTimedout() | |||
740 | } | 740 | } |
741 | } | 741 | } |
742 | 742 | ||
743 | //main loop | 743 | /* main loop */ |
744 | void doNetCrypto() | 744 | void doNetCrypto() |
745 | { | 745 | { |
746 | //TODO:check if friend requests were sent correctly | 746 | /* TODO:check if friend requests were sent correctly |
747 | //handle new incoming connections | 747 | handle new incoming connections |
748 | //handle friend requests | 748 | handle friend requests */ |
749 | handle_incomings(); | 749 | handle_incomings(); |
750 | receive_crypto(); | 750 | receive_crypto(); |
751 | killTimedout(); | 751 | killTimedout(); |