diff options
-rw-r--r-- | core/net_crypto.c | 98 | ||||
-rw-r--r-- | core/net_crypto.h | 17 | ||||
-rw-r--r-- | docs/Crypto.txt | 10 | ||||
-rw-r--r-- | testing/DHT_cryptosendfiletest.c | 7 |
4 files changed, 88 insertions, 44 deletions
diff --git a/core/net_crypto.c b/core/net_crypto.c index 38a72205..02a95e6b 100644 --- a/core/net_crypto.c +++ b/core/net_crypto.c | |||
@@ -18,9 +18,12 @@ uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; | |||
18 | 18 | ||
19 | typedef struct | 19 | typedef struct |
20 | { | 20 | { |
21 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | 21 | uint8_t public_key[crypto_box_PUBLICKEYBYTES];//the real public key of the peer. |
22 | uint8_t recv_nonce[crypto_box_NONCEBYTES];//nonce of recieved packets | 22 | uint8_t recv_nonce[crypto_box_NONCEBYTES];//nonce of recieved packets |
23 | uint8_t sent_nonce[crypto_box_NONCEBYTES];//nonce of sent packets. | 23 | uint8_t sent_nonce[crypto_box_NONCEBYTES];//nonce of sent packets. |
24 | uint8_t sessionpublic_key[crypto_box_PUBLICKEYBYTES];//our public key for this session. | ||
25 | uint8_t sessionsecret_key[crypto_box_SECRETKEYBYTES];//our private key for this session. | ||
26 | uint8_t peersessionpublic_key[crypto_box_PUBLICKEYBYTES];//The public key of the peer. | ||
24 | uint8_t status;//0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet | 27 | uint8_t status;//0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet |
25 | //(we have recieved a hanshake but no empty data packet), 3 if the connection is established. | 28 | //(we have recieved a hanshake but no empty data packet), 3 if the connection is established. |
26 | //4 if the connection is timed out. | 29 | //4 if the connection is timed out. |
@@ -43,10 +46,11 @@ int outbound_friendrequests[MAX_FRIEND_REQUESTS]; | |||
43 | int incoming_connections[MAX_INCOMING]; | 46 | int incoming_connections[MAX_INCOMING]; |
44 | 47 | ||
45 | //encrypts plain of length length to encrypted of length + 16 using the | 48 | //encrypts plain of length length to encrypted of length + 16 using the |
46 | //public key(32 bytes) of the reciever and a 24 byte nonce | 49 | //public key(32 bytes) of the reciever and the secret key of the sender and a 24 byte nonce |
47 | //return -1 if there was a problem. | 50 | //return -1 if there was a problem. |
48 | //return length of encrypted data if everything was fine. | 51 | //return length of encrypted data if everything was fine. |
49 | int encrypt_data(uint8_t * public_key, uint8_t * nonce, uint8_t * plain, uint32_t length, uint8_t * encrypted) | 52 | int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, |
53 | uint8_t * plain, uint32_t length, uint8_t * encrypted) | ||
50 | { | 54 | { |
51 | if(length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE || length == 0) | 55 | if(length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE || length == 0) |
52 | { | 56 | { |
@@ -59,7 +63,7 @@ int encrypt_data(uint8_t * public_key, uint8_t * nonce, uint8_t * plain, uint32_ | |||
59 | 63 | ||
60 | memcpy(temp_plain + crypto_box_ZEROBYTES, plain, length);//pad the message with 32 0 bytes. | 64 | memcpy(temp_plain + crypto_box_ZEROBYTES, plain, length);//pad the message with 32 0 bytes. |
61 | 65 | ||
62 | crypto_box(temp_encrypted, temp_plain, length + crypto_box_ZEROBYTES, nonce, public_key, self_secret_key); | 66 | crypto_box(temp_encrypted, temp_plain, length + crypto_box_ZEROBYTES, nonce, public_key, secret_key); |
63 | 67 | ||
64 | //if encryption is successful the first crypto_box_BOXZEROBYTES of the message will be zero | 68 | //if encryption is successful the first crypto_box_BOXZEROBYTES of the message will be zero |
65 | if(memcmp(temp_encrypted, zeroes, crypto_box_BOXZEROBYTES) != 0) | 69 | if(memcmp(temp_encrypted, zeroes, crypto_box_BOXZEROBYTES) != 0) |
@@ -72,10 +76,11 @@ int encrypt_data(uint8_t * public_key, uint8_t * nonce, uint8_t * plain, uint32_ | |||
72 | } | 76 | } |
73 | 77 | ||
74 | //decrypts encrypted of length length to plain of length length - 16 using the | 78 | //decrypts encrypted of length length to plain of length length - 16 using the |
75 | //public key(32 bytes) of the sender and a 24 byte nonce | 79 | //public key(32 bytes) of the sender, the secret key of the reciever and a 24 byte nonce |
76 | //return -1 if there was a problem(decryption failed) | 80 | //return -1 if there was a problem(decryption failed) |
77 | //return length of plain data if everything was fine. | 81 | //return length of plain data if everything was fine. |
78 | int decrypt_data(uint8_t * public_key, uint8_t * nonce, uint8_t * encrypted, uint32_t length, uint8_t * plain) | 82 | int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, |
83 | uint8_t * encrypted, uint32_t length, uint8_t * plain) | ||
79 | { | 84 | { |
80 | if(length > MAX_DATA_SIZE || length <= crypto_box_BOXZEROBYTES) | 85 | if(length > MAX_DATA_SIZE || length <= crypto_box_BOXZEROBYTES) |
81 | { | 86 | { |
@@ -88,7 +93,7 @@ int decrypt_data(uint8_t * public_key, uint8_t * nonce, uint8_t * encrypted, uin | |||
88 | memcpy(temp_encrypted + crypto_box_BOXZEROBYTES, encrypted, length);//pad the message with 16 0 bytes. | 93 | memcpy(temp_encrypted + crypto_box_BOXZEROBYTES, encrypted, length);//pad the message with 16 0 bytes. |
89 | 94 | ||
90 | if(crypto_box_open(temp_plain, temp_encrypted, length + crypto_box_BOXZEROBYTES, | 95 | if(crypto_box_open(temp_plain, temp_encrypted, length + crypto_box_BOXZEROBYTES, |
91 | nonce, public_key, self_secret_key) == -1) | 96 | nonce, public_key, secret_key) == -1) |
92 | { | 97 | { |
93 | return -1; | 98 | return -1; |
94 | } | 99 | } |
@@ -146,8 +151,9 @@ int read_cryptpacket(int crypt_connection_id, uint8_t * data) | |||
146 | { | 151 | { |
147 | return -1; | 152 | return -1; |
148 | } | 153 | } |
149 | int len = decrypt_data(crypto_connections[crypt_connection_id].public_key, | 154 | int len = decrypt_data(crypto_connections[crypt_connection_id].peersessionpublic_key, |
150 | crypto_connections[crypt_connection_id].recv_nonce, temp_data + 1, length - 1, data); | 155 | crypto_connections[crypt_connection_id].sessionsecret_key, |
156 | crypto_connections[crypt_connection_id].recv_nonce, temp_data + 1, length - 1, data); | ||
151 | if(len != -1) | 157 | if(len != -1) |
152 | { | 158 | { |
153 | increment_nonce(crypto_connections[crypt_connection_id].recv_nonce); | 159 | increment_nonce(crypto_connections[crypt_connection_id].recv_nonce); |
@@ -170,8 +176,9 @@ int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length) | |||
170 | return 0; | 176 | return 0; |
171 | } | 177 | } |
172 | uint8_t temp_data[MAX_DATA_SIZE]; | 178 | uint8_t temp_data[MAX_DATA_SIZE]; |
173 | int len = encrypt_data(crypto_connections[crypt_connection_id].public_key, | 179 | int len = encrypt_data(crypto_connections[crypt_connection_id].peersessionpublic_key, |
174 | crypto_connections[crypt_connection_id].sent_nonce, data, length, temp_data + 1); | 180 | crypto_connections[crypt_connection_id].sessionsecret_key, |
181 | crypto_connections[crypt_connection_id].sent_nonce, data, length, temp_data + 1); | ||
175 | if(len == -1) | 182 | if(len == -1) |
176 | { | 183 | { |
177 | return 0; | 184 | return 0; |
@@ -210,7 +217,8 @@ int send_friendrequest(uint8_t * public_key, IP_Port ip_port, uint8_t * data, ui | |||
210 | uint8_t temp_data[MAX_DATA_SIZE]; | 217 | uint8_t temp_data[MAX_DATA_SIZE]; |
211 | uint8_t nonce[crypto_box_NONCEBYTES]; | 218 | uint8_t nonce[crypto_box_NONCEBYTES]; |
212 | random_nonce(nonce); | 219 | random_nonce(nonce); |
213 | int len = encrypt_data(public_key, nonce, data, length, 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + temp_data); | 220 | int len = encrypt_data(public_key, self_secret_key, nonce, data, length, |
221 | 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + temp_data); | ||
214 | if(len == -1) | 222 | if(len == -1) |
215 | { | 223 | { |
216 | return -1; | 224 | return -1; |
@@ -266,14 +274,20 @@ int check_friendrequest(int friend_request) | |||
266 | return 0; | 274 | return 0; |
267 | } | 275 | } |
268 | 276 | ||
269 | //Send a crypto handshake packet containing an encrypted secret nonce to peer with connection_id and public_key | 277 | //Send a crypto handshake packet containing an encrypted secret nonce and session public key |
278 | //to peer with connection_id and public_key | ||
270 | //the packet is encrypted with a random nonce which is sent in plain text with the packet | 279 | //the packet is encrypted with a random nonce which is sent in plain text with the packet |
271 | int send_cryptohandshake(int connection_id, uint8_t * public_key, uint8_t * secret_nonce) | 280 | int send_cryptohandshake(int connection_id, uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key) |
272 | { | 281 | { |
273 | uint8_t temp_data[MAX_DATA_SIZE]; | 282 | uint8_t temp_data[MAX_DATA_SIZE]; |
283 | uint8_t temp[crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES]; | ||
274 | uint8_t nonce[crypto_box_NONCEBYTES]; | 284 | uint8_t nonce[crypto_box_NONCEBYTES]; |
285 | |||
275 | random_nonce(nonce); | 286 | random_nonce(nonce); |
276 | int len = encrypt_data(public_key, nonce, secret_nonce, crypto_box_NONCEBYTES, | 287 | memcpy(temp, secret_nonce, crypto_box_NONCEBYTES); |
288 | memcpy(temp + crypto_box_NONCEBYTES, session_key, crypto_box_PUBLICKEYBYTES); | ||
289 | |||
290 | int len = encrypt_data(public_key, self_secret_key, nonce, temp, crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES, | ||
277 | 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + temp_data); | 291 | 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + temp_data); |
278 | if(len == -1) | 292 | if(len == -1) |
279 | { | 293 | { |
@@ -285,13 +299,15 @@ int send_cryptohandshake(int connection_id, uint8_t * public_key, uint8_t * secr | |||
285 | return write_packet(connection_id, temp_data, len + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES); | 299 | return write_packet(connection_id, temp_data, len + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES); |
286 | } | 300 | } |
287 | 301 | ||
288 | //Extract secret nonce and public_key from a packet(data) with length length | 302 | //Extract secret nonce, session public key and public_key from a packet(data) with length length |
289 | //return 1 if successful | 303 | //return 1 if successful |
290 | //return 0 if failure | 304 | //return 0 if failure |
291 | int handle_cryptohandshake(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * data, uint16_t length) | 305 | int handle_cryptohandshake(uint8_t * public_key, uint8_t * secret_nonce, |
306 | uint8_t * session_key, uint8_t * data, uint16_t length) | ||
292 | { | 307 | { |
293 | int pad = (- crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES); | 308 | int pad = (- crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES); |
294 | if(length != 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + crypto_box_NONCEBYTES + pad) | 309 | if(length != 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES |
310 | + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + pad) | ||
295 | { | 311 | { |
296 | return 0; | 312 | return 0; |
297 | } | 313 | } |
@@ -299,13 +315,21 @@ int handle_cryptohandshake(uint8_t * public_key, uint8_t * secret_nonce, uint8_t | |||
299 | { | 315 | { |
300 | return 0; | 316 | return 0; |
301 | } | 317 | } |
318 | uint8_t temp[crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES]; | ||
319 | |||
302 | memcpy(public_key, data + 1, crypto_box_PUBLICKEYBYTES); | 320 | memcpy(public_key, data + 1, crypto_box_PUBLICKEYBYTES); |
303 | int len = decrypt_data(public_key, data + 1 + crypto_box_PUBLICKEYBYTES, | 321 | |
304 | data + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES, crypto_box_NONCEBYTES + pad, secret_nonce); | 322 | int len = decrypt_data(public_key, self_secret_key, data + 1 + crypto_box_PUBLICKEYBYTES, |
305 | if(len != crypto_box_NONCEBYTES) | 323 | data + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES, |
324 | crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + pad, temp); | ||
325 | |||
326 | if(len != crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES) | ||
306 | { | 327 | { |
307 | return 0; | 328 | return 0; |
308 | } | 329 | } |
330 | |||
331 | memcpy(secret_nonce, temp, crypto_box_NONCEBYTES); | ||
332 | memcpy(session_key, temp + crypto_box_NONCEBYTES, crypto_box_PUBLICKEYBYTES); | ||
309 | return 1; | 333 | return 1; |
310 | } | 334 | } |
311 | 335 | ||
@@ -330,7 +354,7 @@ int handle_friendrequest(uint8_t * public_key, uint8_t * data) | |||
330 | memcpy(public_key, temp_data + 1, crypto_box_PUBLICKEYBYTES); | 354 | memcpy(public_key, temp_data + 1, crypto_box_PUBLICKEYBYTES); |
331 | uint8_t nonce[crypto_box_NONCEBYTES]; | 355 | uint8_t nonce[crypto_box_NONCEBYTES]; |
332 | memcpy(nonce, temp_data + 1 + crypto_box_PUBLICKEYBYTES, crypto_box_NONCEBYTES); | 356 | memcpy(nonce, temp_data + 1 + crypto_box_PUBLICKEYBYTES, crypto_box_NONCEBYTES); |
333 | int len1 = decrypt_data(public_key, nonce, temp_data + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES, | 357 | int len1 = decrypt_data(public_key, self_secret_key, nonce, temp_data + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES, |
334 | len - (crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + 1), data); | 358 | len - (crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + 1), data); |
335 | if(len1 != -1) | 359 | if(len1 != -1) |
336 | { | 360 | { |
@@ -371,7 +395,10 @@ int crypto_connect(uint8_t * public_key, IP_Port ip_port) | |||
371 | crypto_connections[i].status = 1; | 395 | crypto_connections[i].status = 1; |
372 | random_nonce(crypto_connections[i].recv_nonce); | 396 | random_nonce(crypto_connections[i].recv_nonce); |
373 | memcpy(crypto_connections[i].public_key, public_key, crypto_box_PUBLICKEYBYTES); | 397 | memcpy(crypto_connections[i].public_key, public_key, crypto_box_PUBLICKEYBYTES); |
374 | if(send_cryptohandshake(id, public_key, crypto_connections[i].recv_nonce) == 1) | 398 | crypto_box_keypair(crypto_connections[i].sessionpublic_key, crypto_connections[i].sessionsecret_key); |
399 | |||
400 | if(send_cryptohandshake(id, public_key, crypto_connections[i].recv_nonce, | ||
401 | crypto_connections[i].sessionpublic_key) == 1) | ||
375 | { | 402 | { |
376 | increment_nonce(crypto_connections[i].recv_nonce); | 403 | increment_nonce(crypto_connections[i].recv_nonce); |
377 | return i; | 404 | return i; |
@@ -385,10 +412,11 @@ int crypto_connect(uint8_t * public_key, IP_Port ip_port) | |||
385 | //handle an incoming connection | 412 | //handle an incoming connection |
386 | //return -1 if no crypto inbound connection | 413 | //return -1 if no crypto inbound connection |
387 | //return incomming connection id (Lossless_UDP one) if there is an incomming crypto connection | 414 | //return incomming connection id (Lossless_UDP one) if there is an incomming crypto connection |
388 | //Put the public key of the peer in public_key and the secret_nonce from the handshake into secret_nonce | 415 | //Put the public key of the peer in public_key, the secret_nonce from the handshake into secret_nonce |
416 | //and the session public key for the connection in session_key | ||
389 | //to accept it see: accept_crypto_inbound(...) | 417 | //to accept it see: accept_crypto_inbound(...) |
390 | //to refuse it just call kill_connection(...) on the connection id | 418 | //to refuse it just call kill_connection(...) on the connection id |
391 | int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce) | 419 | int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key) |
392 | { | 420 | { |
393 | uint32_t i; | 421 | uint32_t i; |
394 | for(i = 0; i < MAX_INCOMING; i++) | 422 | for(i = 0; i < MAX_INCOMING; i++) |
@@ -399,7 +427,7 @@ int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce) | |||
399 | { | 427 | { |
400 | uint8_t temp_data[MAX_DATA_SIZE]; | 428 | uint8_t temp_data[MAX_DATA_SIZE]; |
401 | uint16_t len = read_packet(incoming_connections[i], temp_data); | 429 | uint16_t len = read_packet(incoming_connections[i], temp_data); |
402 | if(handle_cryptohandshake(public_key, secret_nonce, temp_data, len)) | 430 | if(handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) |
403 | { | 431 | { |
404 | int connection_id = incoming_connections[i]; | 432 | int connection_id = incoming_connections[i]; |
405 | incoming_connections[i] = -1;//remove this connection from the incoming connection list. | 433 | incoming_connections[i] = -1;//remove this connection from the incoming connection list. |
@@ -433,7 +461,7 @@ int crypto_kill(int crypt_connection_id) | |||
433 | //accept an incoming connection using the parameters provided by crypto_inbound | 461 | //accept an incoming connection using the parameters provided by crypto_inbound |
434 | //return -1 if not successful | 462 | //return -1 if not successful |
435 | //returns the crypt_connection_id if successful | 463 | //returns the crypt_connection_id if successful |
436 | int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * secret_nonce) | 464 | int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key) |
437 | { | 465 | { |
438 | uint32_t i; | 466 | uint32_t i; |
439 | if(connection_id == -1) | 467 | if(connection_id == -1) |
@@ -448,9 +476,14 @@ int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * sec | |||
448 | crypto_connections[i].status = 2; | 476 | crypto_connections[i].status = 2; |
449 | random_nonce(crypto_connections[i].recv_nonce); | 477 | random_nonce(crypto_connections[i].recv_nonce); |
450 | memcpy(crypto_connections[i].sent_nonce, secret_nonce, crypto_box_NONCEBYTES); | 478 | memcpy(crypto_connections[i].sent_nonce, secret_nonce, crypto_box_NONCEBYTES); |
479 | memcpy(crypto_connections[i].peersessionpublic_key, session_key, crypto_box_PUBLICKEYBYTES); | ||
451 | increment_nonce(crypto_connections[i].sent_nonce); | 480 | increment_nonce(crypto_connections[i].sent_nonce); |
452 | memcpy(crypto_connections[i].public_key, public_key, crypto_box_PUBLICKEYBYTES); | 481 | memcpy(crypto_connections[i].public_key, public_key, crypto_box_PUBLICKEYBYTES); |
453 | if(send_cryptohandshake(connection_id, public_key, crypto_connections[i].recv_nonce) == 1) | 482 | |
483 | crypto_box_keypair(crypto_connections[i].sessionpublic_key, crypto_connections[i].sessionsecret_key); | ||
484 | |||
485 | if(send_cryptohandshake(connection_id, public_key, crypto_connections[i].recv_nonce, | ||
486 | crypto_connections[i].sessionpublic_key) == 1) | ||
454 | { | 487 | { |
455 | increment_nonce(crypto_connections[i].recv_nonce); | 488 | increment_nonce(crypto_connections[i].recv_nonce); |
456 | uint32_t zero = 0; | 489 | uint32_t zero = 0; |
@@ -535,12 +568,14 @@ void recieve_crypto() | |||
535 | uint8_t temp_data[MAX_DATA_SIZE]; | 568 | uint8_t temp_data[MAX_DATA_SIZE]; |
536 | uint8_t secret_nonce[crypto_box_NONCEBYTES]; | 569 | uint8_t secret_nonce[crypto_box_NONCEBYTES]; |
537 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | 570 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; |
571 | uint8_t session_key[crypto_box_PUBLICKEYBYTES]; | ||
538 | uint16_t len = read_packet(crypto_connections[i].number, temp_data); | 572 | uint16_t len = read_packet(crypto_connections[i].number, temp_data); |
539 | if(handle_cryptohandshake(public_key, secret_nonce, temp_data, len)) | 573 | if(handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) |
540 | { | 574 | { |
541 | if(memcmp(public_key, crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) | 575 | if(memcmp(public_key, crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) |
542 | { | 576 | { |
543 | memcpy(crypto_connections[i].sent_nonce, secret_nonce, crypto_box_NONCEBYTES); | 577 | memcpy(crypto_connections[i].sent_nonce, secret_nonce, crypto_box_NONCEBYTES); |
578 | memcpy(crypto_connections[i].peersessionpublic_key, session_key, crypto_box_PUBLICKEYBYTES); | ||
544 | increment_nonce(crypto_connections[i].sent_nonce); | 579 | increment_nonce(crypto_connections[i].sent_nonce); |
545 | uint32_t zero = 0; | 580 | uint32_t zero = 0; |
546 | crypto_connections[i].status = 3;//connection status needs to be 3 for write_cryptpacket() to work | 581 | crypto_connections[i].status = 3;//connection status needs to be 3 for write_cryptpacket() to work |
@@ -564,8 +599,9 @@ void recieve_crypto() | |||
564 | uint8_t temp_data[MAX_DATA_SIZE]; | 599 | uint8_t temp_data[MAX_DATA_SIZE]; |
565 | uint8_t data[MAX_DATA_SIZE]; | 600 | uint8_t data[MAX_DATA_SIZE]; |
566 | int length = read_packet(crypto_connections[i].number, temp_data); | 601 | int length = read_packet(crypto_connections[i].number, temp_data); |
567 | int len = decrypt_data(crypto_connections[i].public_key, | 602 | int len = decrypt_data(crypto_connections[i].peersessionpublic_key, |
568 | crypto_connections[i].recv_nonce, temp_data + 1, length - 1, data); | 603 | crypto_connections[i].sessionsecret_key, |
604 | crypto_connections[i].recv_nonce, temp_data + 1, length - 1, data); | ||
569 | uint32_t zero = 0; | 605 | uint32_t zero = 0; |
570 | if(len == sizeof(uint32_t) && memcmp(((uint8_t *)&zero), data, sizeof(uint32_t)) == 0) | 606 | if(len == sizeof(uint32_t) && memcmp(((uint8_t *)&zero), data, sizeof(uint32_t)) == 0) |
571 | { | 607 | { |
diff --git a/core/net_crypto.h b/core/net_crypto.h index 850bcd13..3de0eb2f 100644 --- a/core/net_crypto.h +++ b/core/net_crypto.h | |||
@@ -19,17 +19,19 @@ extern uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; | |||
19 | 19 | ||
20 | 20 | ||
21 | //encrypts plain of length length to encrypted of length + 16 using the | 21 | //encrypts plain of length length to encrypted of length + 16 using the |
22 | //public key(32 bytes) of the reciever and a 24 byte nonce | 22 | //public key(32 bytes) of the reciever and the secret key of the sender and a 24 byte nonce |
23 | //return -1 if there was a problem. | 23 | //return -1 if there was a problem. |
24 | //return length of encrypted data if everything was fine. | 24 | //return length of encrypted data if everything was fine. |
25 | int encrypt_data(uint8_t * public_key, uint8_t * nonce, uint8_t * plain, uint32_t length, uint8_t * encrypted); | 25 | int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, |
26 | uint8_t * plain, uint32_t length, uint8_t * encrypted); | ||
26 | 27 | ||
27 | 28 | ||
28 | //decrypts encrypted of length length to plain of length length - 16 using the | 29 | //decrypts encrypted of length length to plain of length length - 16 using the |
29 | //public key(32 bytes) of the sender and a 24 byte nonce | 30 | //public key(32 bytes) of the sender, the secret key of the reciever and a 24 byte nonce |
30 | //return -1 if there was a problem(decryption failed) | 31 | //return -1 if there was a problem(decryption failed) |
31 | //return length of plain data if everything was fine. | 32 | //return length of plain data if everything was fine. |
32 | int decrypt_data(uint8_t * public_key, uint8_t * nonce, uint8_t * encrypted, uint32_t length, uint8_t * plain); | 33 | int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, |
34 | uint8_t * encrypted, uint32_t length, uint8_t * plain); | ||
33 | 35 | ||
34 | 36 | ||
35 | //return 0 if there is no received data in the buffer | 37 | //return 0 if there is no received data in the buffer |
@@ -76,16 +78,17 @@ int crypto_kill(int crypt_connection_id); | |||
76 | //handle an incoming connection | 78 | //handle an incoming connection |
77 | //return -1 if no crypto inbound connection | 79 | //return -1 if no crypto inbound connection |
78 | //return incomming connection id (Lossless_UDP one) if there is an incomming crypto connection | 80 | //return incomming connection id (Lossless_UDP one) if there is an incomming crypto connection |
79 | //Put the public key of the peer in public_key and the secret_nonce from the handshake into secret_nonce | 81 | //Put the public key of the peer in public_key, the secret_nonce from the handshake into secret_nonce |
82 | //and the session public key for the connection in session_key | ||
80 | //to accept it see: accept_crypto_inbound(...) | 83 | //to accept it see: accept_crypto_inbound(...) |
81 | //to refuse it just call kill_connection(...) on the connection id | 84 | //to refuse it just call kill_connection(...) on the connection id |
82 | int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce); | 85 | int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key); |
83 | 86 | ||
84 | 87 | ||
85 | //accept an incoming connection using the parameters provided by crypto_inbound | 88 | //accept an incoming connection using the parameters provided by crypto_inbound |
86 | //return -1 if not successful | 89 | //return -1 if not successful |
87 | //returns the crypt_connection_id if successful | 90 | //returns the crypt_connection_id if successful |
88 | int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * secret_nonce); | 91 | int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key); |
89 | 92 | ||
90 | //return 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet | 93 | //return 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet |
91 | //(we have recieved a hanshake but no empty data packet), 3 if the connection is established. | 94 | //(we have recieved a hanshake but no empty data packet), 3 if the connection is established. |
diff --git a/docs/Crypto.txt b/docs/Crypto.txt index 0f7d42da..817c1472 100644 --- a/docs/Crypto.txt +++ b/docs/Crypto.txt | |||
@@ -40,14 +40,18 @@ only crypto_box() is used for encryption and only crypto_box_open() for decrypti | |||
40 | 40 | ||
41 | Connecting to an already added friend: | 41 | Connecting to an already added friend: |
42 | Alice and Bob are friends. | 42 | Alice and Bob are friends. |
43 | As soon as they connect they each send themselves the following packet (the crypto handshake) (encrypted part encrypted with the public nonce in the packet | 43 | As soon as they connect they each generate a new keypair which will only be used for the current connection (The session keys). |
44 | They then send themselves the following packet (the crypto handshake) (encrypted part encrypted with the public nonce in the packet | ||
44 | the public key of the receiver and private key of the sender) | 45 | the public key of the receiver and private key of the sender) |
45 | [char with a value of 02][Senders Public key (client_id) (32 bytes)][Random nonce (24 bytes)][Encrypted message containing a random 24 bytes base nonce] | 46 | [char with a value of 02][Senders Public key (client_id) (32 bytes)][Random nonce (24 bytes)][Encrypted message containing a random 24 bytes base nonce and the session public key of the peer] |
47 | |||
46 | If the packet is decrypted successfully: | 48 | If the packet is decrypted successfully: |
47 | Each start using the secret nonce provided by the other to encrypt data packets (adding to it + 1 for each packet.) | 49 | Each start using the secret nonce, the public key provided by the other and their own session private key to encrypt data packets (adding to it + 1 for each packet.) |
48 | Each node sends themselves an empty data packet (data packet with 4 encrypted zero bytes) | 50 | Each node sends themselves an empty data packet (data packet with 4 encrypted zero bytes) |
49 | Data packet: | 51 | Data packet: |
50 | [char with a value of 03][Encrypted data] | 52 | [char with a value of 03][Encrypted data] |
51 | Each data packet received it is decrypted using the secret nonce sent to the other(with +1 added for the first packet +2 for the second, etc...) | 53 | Each data packet received it is decrypted using the secret nonce sent to the other(with +1 added for the first packet +2 for the second, etc...) |
54 | along with the private session key of the reciever. | ||
52 | Every data packet sent is encrypted using the secret nonce we received (with +1 added for the first packet +2 for the second, etc...) | 55 | Every data packet sent is encrypted using the secret nonce we received (with +1 added for the first packet +2 for the second, etc...) |
56 | the session public key of the reciever and the session private key of the sender. | ||
53 | The encrypted connection is only deemed successful when a data packet is received and decrypted successfully. | 57 | The encrypted connection is only deemed successful when a data packet is received and decrypted successfully. |
diff --git a/testing/DHT_cryptosendfiletest.c b/testing/DHT_cryptosendfiletest.c index 787e279e..84d777cc 100644 --- a/testing/DHT_cryptosendfiletest.c +++ b/testing/DHT_cryptosendfiletest.c | |||
@@ -8,7 +8,7 @@ | |||
8 | * NOTE: this program simulates 33% packet loss. | 8 | * NOTE: this program simulates 33% packet loss. |
9 | * | 9 | * |
10 | * This is how I compile it: gcc -O2 -Wall -o test ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/DHT.c ../nacl/build/Linux/lib/amd64/* DHT_cryptosendfiletest.c | 10 | * This is how I compile it: gcc -O2 -Wall -o test ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/DHT.c ../nacl/build/Linux/lib/amd64/* DHT_cryptosendfiletest.c |
11 | * | 11 | * |
12 | * | 12 | * |
13 | * Command line arguments are the ip and port of a node (for bootstrapping). | 13 | * Command line arguments are the ip and port of a node (for bootstrapping). |
14 | * | 14 | * |
@@ -167,8 +167,9 @@ int main(int argc, char *argv[]) | |||
167 | { | 167 | { |
168 | uint8_t secret_nonce[crypto_box_NONCEBYTES]; | 168 | uint8_t secret_nonce[crypto_box_NONCEBYTES]; |
169 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | 169 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; |
170 | inconnection = crypto_inbound(public_key, secret_nonce); | 170 | uint8_t session_key[crypto_box_PUBLICKEYBYTES]; |
171 | inconnection = accept_crypto_inbound(inconnection, acceptedfriend_public_key, secret_nonce); | 171 | inconnection = crypto_inbound(public_key, secret_nonce, session_key); |
172 | inconnection = accept_crypto_inbound(inconnection, acceptedfriend_public_key, secret_nonce, session_key); | ||
172 | //inconnection = incoming_connection(); | 173 | //inconnection = incoming_connection(); |
173 | if(inconnection != -1) | 174 | if(inconnection != -1) |
174 | { | 175 | { |