diff options
Diffstat (limited to 'core/net_crypto.h')
-rw-r--r-- | core/net_crypto.h | 194 |
1 files changed, 97 insertions, 97 deletions
diff --git a/core/net_crypto.h b/core/net_crypto.h index d4fe1313..0eb2ad6d 100644 --- a/core/net_crypto.h +++ b/core/net_crypto.h | |||
@@ -1,5 +1,5 @@ | |||
1 | /* net_crypto.h | 1 | /* net_crypto.h |
2 | * | 2 | * |
3 | * Functions for the core network crypto. | 3 | * Functions for the core network crypto. |
4 | * | 4 | * |
5 | * Copyright (C) 2013 Tox project All Rights Reserved. | 5 | * Copyright (C) 2013 Tox project All Rights Reserved. |
@@ -18,11 +18,11 @@ | |||
18 | * | 18 | * |
19 | * You should have received a copy of the GNU General Public License | 19 | * You should have received a copy of the GNU General Public License |
20 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | 20 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. |
21 | * | 21 | * |
22 | */ | 22 | */ |
23 | 23 | ||
24 | #ifndef NET_CRYPTO_H | 24 | #ifndef NET_CRYPTO_H |
25 | #define NET_CRYPTO_H | 25 | #define NET_CRYPTO_H |
26 | 26 | ||
27 | #include "Lossless_UDP.h" | 27 | #include "Lossless_UDP.h" |
28 | 28 | ||
@@ -30,102 +30,102 @@ | |||
30 | extern "C" { | 30 | extern "C" { |
31 | #endif | 31 | #endif |
32 | 32 | ||
33 | /* Our public key. */ | 33 | /* Our public key. */ |
34 | extern uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; | 34 | extern uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; |
35 | extern uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; | 35 | extern uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; |
36 | 36 | ||
37 | #define ENCRYPTION_PADDING (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES) | 37 | #define ENCRYPTION_PADDING (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES) |
38 | 38 | ||
39 | /* encrypts plain of length length to encrypted of length + 16 using the | 39 | /* encrypts plain of length length to encrypted of length + 16 using the |
40 | public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce | 40 | public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce |
41 | return -1 if there was a problem. | 41 | return -1 if there was a problem. |
42 | return length of encrypted data if everything was fine. */ | 42 | return length of encrypted data if everything was fine. */ |
43 | int encrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce, | 43 | int encrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce, |
44 | uint8_t *plain, uint32_t length, uint8_t *encrypted); | 44 | uint8_t *plain, uint32_t length, uint8_t *encrypted); |
45 | 45 | ||
46 | 46 | ||
47 | /* decrypts encrypted of length length to plain of length length - 16 using the | 47 | /* decrypts encrypted of length length to plain of length length - 16 using the |
48 | public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce | 48 | public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce |
49 | return -1 if there was a problem(decryption failed) | 49 | return -1 if there was a problem(decryption failed) |
50 | return length of plain data if everything was fine. */ | 50 | return length of plain data if everything was fine. */ |
51 | int decrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce, | 51 | int decrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce, |
52 | uint8_t *encrypted, uint32_t length, uint8_t *plain); | 52 | uint8_t *encrypted, uint32_t length, uint8_t *plain); |
53 | 53 | ||
54 | 54 | ||
55 | /* fill the given nonce with random bytes. */ | 55 | /* fill the given nonce with random bytes. */ |
56 | void random_nonce(uint8_t *nonce); | 56 | void random_nonce(uint8_t *nonce); |
57 | 57 | ||
58 | /* return 0 if there is no received data in the buffer | 58 | /* return 0 if there is no received data in the buffer |
59 | return -1 if the packet was discarded. | 59 | return -1 if the packet was discarded. |
60 | return length of received data if successful */ | 60 | return length of received data if successful */ |
61 | int read_cryptpacket(int crypt_connection_id, uint8_t *data); | 61 | int read_cryptpacket(int crypt_connection_id, uint8_t *data); |
62 | 62 | ||
63 | /* return 0 if data could not be put in packet queue | 63 | /* return 0 if data could not be put in packet queue |
64 | return 1 if data was put into the queue */ | 64 | return 1 if data was put into the queue */ |
65 | int write_cryptpacket(int crypt_connection_id, uint8_t *data, uint32_t length); | 65 | int write_cryptpacket(int crypt_connection_id, uint8_t *data, uint32_t length); |
66 | 66 | ||
67 | /* create a request to peer with public_key. | 67 | /* create a request to peer with public_key. |
68 | packet must be an array of MAX_DATA_SIZE big. | 68 | packet must be an array of MAX_DATA_SIZE big. |
69 | Data represents the data we send with the request with length being the length of the data. | 69 | Data represents the data we send with the request with length being the length of the data. |
70 | request_id is the id of the request (32 = friend request, 254 = ping request) | 70 | request_id is the id of the request (32 = friend request, 254 = ping request) |
71 | returns -1 on failure | 71 | returns -1 on failure |
72 | returns the length of the created packet on success */ | 72 | returns the length of the created packet on success */ |
73 | int create_request(uint8_t *packet, uint8_t * public_key, uint8_t *data, uint32_t length, uint8_t request_id); | 73 | int create_request(uint8_t *packet, uint8_t * public_key, uint8_t *data, uint32_t length, uint8_t request_id); |
74 | 74 | ||
75 | /* puts the senders public key in the request in public_key, the data from the request | 75 | /* puts the senders public key in the request in public_key, the data from the request |
76 | in data if a friend or ping request was sent to us and returns the length of the data. | 76 | in data if a friend or ping request was sent to us and returns the length of the data. |
77 | packet is the request packet and length is its length | 77 | packet is the request packet and length is its length |
78 | return -1 if not valid request. */ | 78 | return -1 if not valid request. */ |
79 | int handle_request(uint8_t *public_key, uint8_t *data, uint8_t *packet, uint16_t length); | 79 | int handle_request(uint8_t *public_key, uint8_t *data, uint8_t *packet, uint16_t length); |
80 | 80 | ||
81 | /* Start a secure connection with other peer who has public_key and ip_port | 81 | /* Start a secure connection with other peer who has public_key and ip_port |
82 | returns -1 if failure | 82 | returns -1 if failure |
83 | returns crypt_connection_id of the initialized connection if everything went well. */ | 83 | returns crypt_connection_id of the initialized connection if everything went well. */ |
84 | int crypto_connect(uint8_t *public_key, IP_Port ip_port); | 84 | int crypto_connect(uint8_t *public_key, IP_Port ip_port); |
85 | 85 | ||
86 | /* kill a crypto connection | 86 | /* kill a crypto connection |
87 | return 0 if killed successfully | 87 | return 0 if killed successfully |
88 | return 1 if there was a problem. */ | 88 | return 1 if there was a problem. */ |
89 | int crypto_kill(int crypt_connection_id); | 89 | int crypto_kill(int crypt_connection_id); |
90 | 90 | ||
91 | /* handle an incoming connection | 91 | /* handle an incoming connection |
92 | return -1 if no crypto inbound connection | 92 | return -1 if no crypto inbound connection |
93 | return incoming connection id (Lossless_UDP one) if there is an incoming crypto connection | 93 | return incoming connection id (Lossless_UDP one) if there is an incoming crypto connection |
94 | Put the public key of the peer in public_key, the secret_nonce from the handshake into secret_nonce | 94 | Put the public key of the peer in public_key, the secret_nonce from the handshake into secret_nonce |
95 | and the session public key for the connection in session_key | 95 | and the session public key for the connection in session_key |
96 | to accept it see: accept_crypto_inbound(...) | 96 | to accept it see: accept_crypto_inbound(...) |
97 | to refuse it just call kill_connection(...) on the connection id */ | 97 | to refuse it just call kill_connection(...) on the connection id */ |
98 | int crypto_inbound(uint8_t *public_key, uint8_t * secret_nonce, uint8_t *session_key); | 98 | int crypto_inbound(uint8_t *public_key, uint8_t * secret_nonce, uint8_t *session_key); |
99 | 99 | ||
100 | /* accept an incoming connection using the parameters provided by crypto_inbound | 100 | /* accept an incoming connection using the parameters provided by crypto_inbound |
101 | return -1 if not successful | 101 | return -1 if not successful |
102 | returns the crypt_connection_id if successful */ | 102 | returns the crypt_connection_id if successful */ |
103 | int accept_crypto_inbound(int connection_id, uint8_t *public_key, uint8_t * secret_nonce, uint8_t *session_key); | 103 | int accept_crypto_inbound(int connection_id, uint8_t *public_key, uint8_t * secret_nonce, uint8_t *session_key); |
104 | 104 | ||
105 | /* return 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet | 105 | /* return 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet |
106 | (we have received a handshake but no empty data packet), 3 if the connection is established. | 106 | (we have received a handshake but no empty data packet), 3 if the connection is established. |
107 | 4 if the connection is timed out and waiting to be killed */ | 107 | 4 if the connection is timed out and waiting to be killed */ |
108 | int is_cryptoconnected(int crypt_connection_id); | 108 | int is_cryptoconnected(int crypt_connection_id); |
109 | 109 | ||
110 | 110 | ||
111 | /* Generate our public and private keys | 111 | /* Generate our public and private keys |
112 | Only call this function the first time the program starts. */ | 112 | Only call this function the first time the program starts. */ |
113 | void new_keys(); | 113 | void new_keys(); |
114 | 114 | ||
115 | /* save the public and private keys to the keys array | 115 | /* save the public and private keys to the keys array |
116 | Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */ | 116 | Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */ |
117 | void save_keys(uint8_t * keys); | 117 | void save_keys(uint8_t * keys); |
118 | 118 | ||
119 | /* load the public and private keys from the keys array | 119 | /* load the public and private keys from the keys array |
120 | Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */ | 120 | Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */ |
121 | void load_keys(uint8_t * keys); | 121 | void load_keys(uint8_t * keys); |
122 | 122 | ||
123 | /* run this to (re)initialize net_crypto | 123 | /* run this to (re)initialize net_crypto |
124 | sets all the global connection variables to their default values. */ | 124 | sets all the global connection variables to their default values. */ |
125 | void initNetCrypto(); | 125 | void initNetCrypto(); |
126 | 126 | ||
127 | /* main loop */ | 127 | /* main loop */ |
128 | void doNetCrypto(); | 128 | void doNetCrypto(); |
129 | 129 | ||
130 | #ifdef __cplusplus | 130 | #ifdef __cplusplus |
131 | } | 131 | } |