diff options
Diffstat (limited to 'core/net_crypto.h')
-rw-r--r-- | core/net_crypto.h | 200 |
1 files changed, 0 insertions, 200 deletions
diff --git a/core/net_crypto.h b/core/net_crypto.h deleted file mode 100644 index 81670993..00000000 --- a/core/net_crypto.h +++ /dev/null | |||
@@ -1,200 +0,0 @@ | |||
1 | /* net_crypto.h | ||
2 | * | ||
3 | * Functions for the core network crypto. | ||
4 | * | ||
5 | * Copyright (C) 2013 Tox project All Rights Reserved. | ||
6 | * | ||
7 | * This file is part of Tox. | ||
8 | * | ||
9 | * Tox is free software: you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation, either version 3 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * Tox is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 | * GNU General Public License for more details. | ||
18 | * | ||
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/>. | ||
21 | * | ||
22 | */ | ||
23 | |||
24 | #ifndef NET_CRYPTO_H | ||
25 | #define NET_CRYPTO_H | ||
26 | |||
27 | #include "Lossless_UDP.h" | ||
28 | |||
29 | #ifdef __cplusplus | ||
30 | extern "C" { | ||
31 | #endif | ||
32 | |||
33 | #define MAX_INCOMING 64 | ||
34 | |||
35 | #define CRYPTO_PACKET_FRIEND_REQ 32 /* Friend request crypto packet ID */ | ||
36 | #define CRYPTO_PACKET_NAT_PING 254 /* NAT ping crypto packet ID */ | ||
37 | |||
38 | typedef struct { | ||
39 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; /* the real public key of the peer. */ | ||
40 | uint8_t recv_nonce[crypto_box_NONCEBYTES]; /* nonce of received packets */ | ||
41 | uint8_t sent_nonce[crypto_box_NONCEBYTES]; /* nonce of sent packets. */ | ||
42 | uint8_t sessionpublic_key[crypto_box_PUBLICKEYBYTES]; /* our public key for this session. */ | ||
43 | uint8_t sessionsecret_key[crypto_box_SECRETKEYBYTES]; /* our private key for this session. */ | ||
44 | uint8_t peersessionpublic_key[crypto_box_PUBLICKEYBYTES]; /* The public key of the peer. */ | ||
45 | uint8_t shared_key[crypto_box_BEFORENMBYTES]; /* the precomputed shared key from encrypt_precompute */ | ||
46 | uint8_t status; /* 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet | ||
47 | (we have received a handshake but no empty data packet), 3 if the connection is established. | ||
48 | 4 if the connection is timed out. */ | ||
49 | uint16_t number; /* Lossless_UDP connection number corresponding to this connection. */ | ||
50 | |||
51 | } Crypto_Connection; | ||
52 | |||
53 | typedef int (*cryptopacket_handler_callback)(void *object, IP_Port ip_port, uint8_t *source_pubkey, uint8_t *data, | ||
54 | uint32_t len); | ||
55 | |||
56 | typedef struct { | ||
57 | cryptopacket_handler_callback function; | ||
58 | void *object; | ||
59 | } Cryptopacket_Handles; | ||
60 | |||
61 | typedef struct { | ||
62 | Lossless_UDP *lossless_udp; | ||
63 | |||
64 | Crypto_Connection *crypto_connections; | ||
65 | |||
66 | uint32_t crypto_connections_length; /* Length of connections array */ | ||
67 | |||
68 | /* Our public and secret keys. */ | ||
69 | uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; | ||
70 | uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; | ||
71 | |||
72 | /* keeps track of the connection numbers for friends request so we can check later if they were sent */ | ||
73 | int incoming_connections[MAX_INCOMING]; | ||
74 | |||
75 | Cryptopacket_Handles cryptopackethandlers[256]; | ||
76 | } Net_Crypto; | ||
77 | |||
78 | #include "DHT.h" | ||
79 | |||
80 | #define ENCRYPTION_PADDING (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES) | ||
81 | |||
82 | /* returns zero if the buffer contains only zeros */ | ||
83 | uint8_t crypto_iszero(uint8_t *buffer, uint32_t blen); | ||
84 | |||
85 | /* encrypts plain of length length to encrypted of length + 16 using the | ||
86 | public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce | ||
87 | return -1 if there was a problem. | ||
88 | return length of encrypted data if everything was fine. */ | ||
89 | int encrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce, | ||
90 | uint8_t *plain, uint32_t length, uint8_t *encrypted); | ||
91 | |||
92 | |||
93 | /* decrypts encrypted of length length to plain of length length - 16 using the | ||
94 | public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce | ||
95 | return -1 if there was a problem(decryption failed) | ||
96 | return length of plain data if everything was fine. */ | ||
97 | int decrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce, | ||
98 | uint8_t *encrypted, uint32_t length, uint8_t *plain); | ||
99 | |||
100 | /* Fast encrypt/decrypt operations. Use if this is not a one-time communication. | ||
101 | encrypt_precompute does the shared-key generation once so it does not have | ||
102 | to be preformed on every encrypt/decrypt. */ | ||
103 | void encrypt_precompute(uint8_t *public_key, uint8_t *secret_key, uint8_t *enc_key); | ||
104 | |||
105 | /* Fast encrypt. Depends on enc_key from encrypt_precompute. */ | ||
106 | int encrypt_data_fast(uint8_t *enc_key, uint8_t *nonce, | ||
107 | uint8_t *plain, uint32_t length, uint8_t *encrypted); | ||
108 | |||
109 | /* Fast decrypt. Depends on enc_ley from encrypt_precompute. */ | ||
110 | int decrypt_data_fast(uint8_t *enc_key, uint8_t *nonce, | ||
111 | uint8_t *encrypted, uint32_t length, uint8_t *plain); | ||
112 | |||
113 | |||
114 | /* fill the given nonce with random bytes. */ | ||
115 | void random_nonce(uint8_t *nonce); | ||
116 | |||
117 | /* return 0 if there is no received data in the buffer | ||
118 | return -1 if the packet was discarded. | ||
119 | return length of received data if successful */ | ||
120 | int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data); | ||
121 | |||
122 | /* return 0 if data could not be put in packet queue | ||
123 | return 1 if data was put into the queue */ | ||
124 | int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length); | ||
125 | |||
126 | /* create a request to peer. | ||
127 | send_public_key and send_secret_key are the pub/secret keys of the sender | ||
128 | recv_public_key is public key of reciever | ||
129 | packet must be an array of MAX_DATA_SIZE big. | ||
130 | Data represents the data we send with the request with length being the length of the data. | ||
131 | request_id is the id of the request (32 = friend request, 254 = ping request) | ||
132 | returns -1 on failure | ||
133 | returns the length of the created packet on success */ | ||
134 | int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *packet, uint8_t *recv_public_key, | ||
135 | uint8_t *data, uint32_t length, uint8_t request_id); | ||
136 | |||
137 | |||
138 | /* Function to call when request beginning with byte is received */ | ||
139 | void cryptopacket_registerhandler(Net_Crypto *c, uint8_t byte, cryptopacket_handler_callback cb, void *object); | ||
140 | |||
141 | /* Start a secure connection with other peer who has public_key and ip_port | ||
142 | returns -1 if failure | ||
143 | returns crypt_connection_id of the initialized connection if everything went well. */ | ||
144 | int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port); | ||
145 | |||
146 | /* kill a crypto connection | ||
147 | return 0 if killed successfully | ||
148 | return 1 if there was a problem. */ | ||
149 | int crypto_kill(Net_Crypto *c, int crypt_connection_id); | ||
150 | |||
151 | /* handle an incoming connection | ||
152 | return -1 if no crypto inbound connection | ||
153 | return incoming connection id (Lossless_UDP one) if there is an incoming crypto connection | ||
154 | Put the public key of the peer in public_key, the secret_nonce from the handshake into secret_nonce | ||
155 | and the session public key for the connection in session_key | ||
156 | to accept it see: accept_crypto_inbound(...) | ||
157 | to refuse it just call kill_connection(...) on the connection id */ | ||
158 | int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key); | ||
159 | |||
160 | /* accept an incoming connection using the parameters provided by crypto_inbound | ||
161 | return -1 if not successful | ||
162 | returns the crypt_connection_id if successful */ | ||
163 | int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, uint8_t *secret_nonce, | ||
164 | uint8_t *session_key); | ||
165 | |||
166 | /* return 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet | ||
167 | (we have received a handshake but no empty data packet), 3 if the connection is established. | ||
168 | 4 if the connection is timed out and waiting to be killed */ | ||
169 | int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id); | ||
170 | |||
171 | |||
172 | /* Generate our public and private keys | ||
173 | Only call this function the first time the program starts. */ | ||
174 | void new_keys(Net_Crypto *c); | ||
175 | |||
176 | /* save the public and private keys to the keys array | ||
177 | Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */ | ||
178 | void save_keys(Net_Crypto *c, uint8_t *keys); | ||
179 | |||
180 | /* load the public and private keys from the keys array | ||
181 | Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */ | ||
182 | void load_keys(Net_Crypto *c, uint8_t *keys); | ||
183 | |||
184 | /* create new instance of Net_Crypto | ||
185 | sets all the global connection variables to their default values. */ | ||
186 | Net_Crypto *new_net_crypto(Networking_Core *net); | ||
187 | |||
188 | /* main loop */ | ||
189 | void do_net_crypto(Net_Crypto *c); | ||
190 | |||
191 | void kill_net_crypto(Net_Crypto *c); | ||
192 | |||
193 | /* Init the cryptopacket handling */ | ||
194 | void init_cryptopackets(void *dht); | ||
195 | |||
196 | #ifdef __cplusplus | ||
197 | } | ||
198 | #endif | ||
199 | |||
200 | #endif | ||