diff options
author | irungentoo <irungentoo@gmail.com> | 2013-07-05 17:00:39 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-07-05 17:00:39 -0400 |
commit | a480c0195a78f56116b3bf58fe17d930bf4e64f4 (patch) | |
tree | c549b6347e28b8570ed27b5c53b768b8f7b11a61 /core/net_crypto.h | |
parent | 358f46f6483f0c24186272914952e44221c76871 (diff) |
Crypto done(still need to test it a bit more thought)
Replaced chars with uint8_t
Added a new test program.
Added some functions to Lossless UDP.
And some other stuff.
Diffstat (limited to 'core/net_crypto.h')
-rw-r--r-- | core/net_crypto.h | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/core/net_crypto.h b/core/net_crypto.h new file mode 100644 index 00000000..850bcd13 --- /dev/null +++ b/core/net_crypto.h | |||
@@ -0,0 +1,108 @@ | |||
1 | /* net_crypto.h | ||
2 | * | ||
3 | * Functions for the core network crypto. | ||
4 | * | ||
5 | */ | ||
6 | |||
7 | #ifndef NET_CRYPTO_H | ||
8 | #define NET_CRYPTO_H | ||
9 | |||
10 | #include "Lossless_UDP.h" | ||
11 | |||
12 | //TODO: move this to network.h | ||
13 | #ifndef WIN32 | ||
14 | #include "../nacl/build/Linux/include/amd64/crypto_box.h" | ||
15 | #endif | ||
16 | //Our public key. | ||
17 | extern uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; | ||
18 | |||
19 | |||
20 | |||
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 | ||
23 | //return -1 if there was a problem. | ||
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); | ||
26 | |||
27 | |||
28 | //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 | //return -1 if there was a problem(decryption failed) | ||
31 | //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 | |||
34 | |||
35 | //return 0 if there is no received data in the buffer | ||
36 | //return -1 if the packet was discarded. | ||
37 | //return length of recieved data if successful | ||
38 | int read_cryptpacket(int crypt_connection_id, uint8_t * data); | ||
39 | |||
40 | |||
41 | //return 0 if data could not be put in packet queue | ||
42 | //return 1 if data was put into the queue | ||
43 | int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length); | ||
44 | |||
45 | //send a friend request to peer with public_key and ip_port. | ||
46 | //Data represents the data we send with the friends request. | ||
47 | //returns -1 on failure | ||
48 | //returns a positive friend request id that can be used later to see if it was sent correctly on success. | ||
49 | int send_friendrequest(uint8_t * public_key, IP_Port ip_port, uint8_t * data, uint32_t length); | ||
50 | |||
51 | |||
52 | //return -1 if failure | ||
53 | //return 0 if connection is still trying to send the request. | ||
54 | //return 1 if sent correctly | ||
55 | //return 2 if connection timed out | ||
56 | int check_friendrequest(int friend_request); | ||
57 | |||
58 | |||
59 | //puts the public key of the friend if public_key, the data from the request | ||
60 | //in data if a friend request was sent to us and returns the length of the data. | ||
61 | //return -1 if no valid friend requests. | ||
62 | int handle_friendrequest(uint8_t * public_key, uint8_t * data); | ||
63 | |||
64 | |||
65 | //Start a secure connection with other peer who has public_key and ip_port | ||
66 | //returns -1 if failure | ||
67 | //returns crypt_connection_id of the initialized connection if everything went well. | ||
68 | int crypto_connect(uint8_t * public_key, IP_Port ip_port); | ||
69 | |||
70 | |||
71 | //kill a crypto connection | ||
72 | //return 0 if killed successfully | ||
73 | //return 1 if there was a problem. | ||
74 | int crypto_kill(int crypt_connection_id); | ||
75 | |||
76 | //handle an incoming connection | ||
77 | //return -1 if no crypto inbound connection | ||
78 | //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 | ||
80 | //to accept it see: accept_crypto_inbound(...) | ||
81 | //to refuse it just call kill_connection(...) on the connection id | ||
82 | int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce); | ||
83 | |||
84 | |||
85 | //accept an incoming connection using the parameters provided by crypto_inbound | ||
86 | //return -1 if not successful | ||
87 | //returns the crypt_connection_id if successful | ||
88 | int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * secret_nonce); | ||
89 | |||
90 | //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. | ||
92 | //4 if the connection is timed out and wating to be killed | ||
93 | int is_cryptoconnected(int crypt_connection_id); | ||
94 | |||
95 | |||
96 | //Generate our public and private keys | ||
97 | //Only call this function the first time the program starts. | ||
98 | void new_keys(); | ||
99 | |||
100 | //run this to (re)initialize net_crypto | ||
101 | //sets all the global connection variables to their default values. | ||
102 | void initNetCrypto(); | ||
103 | |||
104 | //main loop | ||
105 | void doNetCrypto(); | ||
106 | |||
107 | |||
108 | #endif | ||