diff options
Diffstat (limited to 'toxcore/net_crypto.c')
-rw-r--r-- | toxcore/net_crypto.c | 58 |
1 files changed, 52 insertions, 6 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 3c7c114e..d58f4c27 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c | |||
@@ -77,7 +77,7 @@ int encrypt_data_fast(uint8_t *enc_key, uint8_t *nonce, | |||
77 | 77 | ||
78 | /* Unpad the encrypted message. */ | 78 | /* Unpad the encrypted message. */ |
79 | memcpy(encrypted, temp_encrypted + crypto_box_BOXZEROBYTES, length + crypto_box_MACBYTES); | 79 | memcpy(encrypted, temp_encrypted + crypto_box_BOXZEROBYTES, length + crypto_box_MACBYTES); |
80 | return length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES; | 80 | return length + crypto_box_MACBYTES; |
81 | } | 81 | } |
82 | 82 | ||
83 | /* Fast decrypt. Depends on enc_ley from encrypt_precompute. */ | 83 | /* Fast decrypt. Depends on enc_ley from encrypt_precompute. */ |
@@ -104,7 +104,7 @@ int decrypt_data_fast(uint8_t *enc_key, uint8_t *nonce, | |||
104 | 104 | ||
105 | /* Unpad the plain message. */ | 105 | /* Unpad the plain message. */ |
106 | memcpy(plain, temp_plain + crypto_box_ZEROBYTES, length - crypto_box_MACBYTES); | 106 | memcpy(plain, temp_plain + crypto_box_ZEROBYTES, length - crypto_box_MACBYTES); |
107 | return length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES; | 107 | return length - crypto_box_MACBYTES; |
108 | } | 108 | } |
109 | 109 | ||
110 | int encrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce, | 110 | int encrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce, |
@@ -123,6 +123,39 @@ int decrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce, | |||
123 | return decrypt_data_fast(k, nonce, encrypted, length, plain); | 123 | return decrypt_data_fast(k, nonce, encrypted, length, plain); |
124 | } | 124 | } |
125 | 125 | ||
126 | int encrypt_data_symmetric(uint8_t *secret_key, uint8_t *nonce, uint8_t *plain, uint32_t length, uint8_t *encrypted) | ||
127 | { | ||
128 | if (length + crypto_secretbox_MACBYTES > MAX_DATA_SIZE || length == 0) | ||
129 | return -1; | ||
130 | |||
131 | uint8_t temp_plain[MAX_DATA_SIZE + crypto_secretbox_ZEROBYTES] = {0}; | ||
132 | uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_secretbox_BOXZEROBYTES]; | ||
133 | |||
134 | memcpy(temp_plain + crypto_secretbox_ZEROBYTES, plain, length); // Pad the message with 32 0 bytes. | ||
135 | |||
136 | crypto_secretbox(temp_encrypted, temp_plain, length + crypto_secretbox_ZEROBYTES, nonce, secret_key); | ||
137 | /* Unpad the encrypted message. */ | ||
138 | memcpy(encrypted, temp_encrypted + crypto_secretbox_BOXZEROBYTES, length + crypto_secretbox_MACBYTES); | ||
139 | return length + crypto_secretbox_MACBYTES; | ||
140 | } | ||
141 | |||
142 | int decrypt_data_symmetric(uint8_t *secret_key, uint8_t *nonce, uint8_t *encrypted, uint32_t length, uint8_t *plain) | ||
143 | { | ||
144 | if (length > MAX_DATA_SIZE || length <= crypto_secretbox_BOXZEROBYTES) | ||
145 | return -1; | ||
146 | |||
147 | uint8_t temp_plain[MAX_DATA_SIZE + crypto_secretbox_ZEROBYTES]; | ||
148 | uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_secretbox_BOXZEROBYTES] = {0}; | ||
149 | |||
150 | memcpy(temp_encrypted + crypto_secretbox_BOXZEROBYTES, encrypted, length); // Pad the message with 16 0 bytes. | ||
151 | |||
152 | if (crypto_secretbox_open(temp_plain, temp_encrypted, length + crypto_secretbox_BOXZEROBYTES, nonce, secret_key) == -1) | ||
153 | return -1; | ||
154 | |||
155 | memcpy(plain, temp_plain + crypto_secretbox_ZEROBYTES, length - crypto_secretbox_MACBYTES); | ||
156 | return length - crypto_secretbox_MACBYTES; | ||
157 | } | ||
158 | |||
126 | /* Increment the given nonce by 1. */ | 159 | /* Increment the given nonce by 1. */ |
127 | static void increment_nonce(uint8_t *nonce) | 160 | static void increment_nonce(uint8_t *nonce) |
128 | { | 161 | { |
@@ -136,16 +169,29 @@ static void increment_nonce(uint8_t *nonce) | |||
136 | } | 169 | } |
137 | } | 170 | } |
138 | 171 | ||
172 | #if crypto_box_NONCEBYTES != crypto_secretbox_NONCEBYTES | ||
173 | /*if they no longer equal each other, this function must be slit into two.*/ | ||
174 | #error random_nonce(): crypto_box_NONCEBYTES must equal crypto_secretbox_NONCEBYTES. | ||
175 | #endif | ||
139 | /* Fill the given nonce with random bytes. */ | 176 | /* Fill the given nonce with random bytes. */ |
140 | void random_nonce(uint8_t *nonce) | 177 | void random_nonce(uint8_t *nonce) |
141 | { | 178 | { |
142 | randombytes(nonce, crypto_box_NONCEBYTES); | 179 | randombytes(nonce, crypto_box_NONCEBYTES); |
143 | } | 180 | } |
144 | 181 | ||
182 | /* Fill a key crypto_secretbox_KEYBYTES big with random bytes */ | ||
183 | void new_symmetric_key(uint8_t *key) | ||
184 | { | ||
185 | randombytes(key, crypto_secretbox_KEYBYTES); | ||
186 | } | ||
145 | 187 | ||
146 | static uint8_t base_nonce[crypto_box_NONCEBYTES]; | 188 | static uint8_t base_nonce[crypto_box_NONCEBYTES]; |
147 | static uint8_t nonce_set = 0; | 189 | static uint8_t nonce_set = 0; |
148 | 190 | ||
191 | #if crypto_box_NONCEBYTES != crypto_secretbox_NONCEBYTES | ||
192 | /*if they no longer equal each other, this function must be slit into two.*/ | ||
193 | #error new_nonce(): crypto_box_NONCEBYTES must equal crypto_secretbox_NONCEBYTES. | ||
194 | #endif | ||
149 | /* Gives a nonce guaranteed to be different from previous ones.*/ | 195 | /* Gives a nonce guaranteed to be different from previous ones.*/ |
150 | void new_nonce(uint8_t *nonce) | 196 | void new_nonce(uint8_t *nonce) |
151 | { | 197 | { |
@@ -247,7 +293,7 @@ int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uin | |||
247 | int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *packet, uint8_t *recv_public_key, | 293 | int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *packet, uint8_t *recv_public_key, |
248 | uint8_t *data, uint32_t length, uint8_t request_id) | 294 | uint8_t *data, uint32_t length, uint8_t request_id) |
249 | { | 295 | { |
250 | if (MAX_DATA_SIZE < length + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING) | 296 | if (MAX_DATA_SIZE < length + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + crypto_box_MACBYTES) |
251 | return -1; | 297 | return -1; |
252 | 298 | ||
253 | uint8_t nonce[crypto_box_NONCEBYTES]; | 299 | uint8_t nonce[crypto_box_NONCEBYTES]; |
@@ -278,7 +324,7 @@ int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t * | |||
278 | int handle_request(uint8_t *self_public_key, uint8_t *self_secret_key, uint8_t *public_key, uint8_t *data, | 324 | int handle_request(uint8_t *self_public_key, uint8_t *self_secret_key, uint8_t *public_key, uint8_t *data, |
279 | uint8_t *request_id, uint8_t *packet, uint16_t length) | 325 | uint8_t *request_id, uint8_t *packet, uint16_t length) |
280 | { | 326 | { |
281 | if (length > crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING && | 327 | if (length > crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + crypto_box_MACBYTES && |
282 | length <= MAX_DATA_SIZE) { | 328 | length <= MAX_DATA_SIZE) { |
283 | if (memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) { | 329 | if (memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) { |
284 | memcpy(public_key, packet + 1 + crypto_box_PUBLICKEYBYTES, crypto_box_PUBLICKEYBYTES); | 330 | memcpy(public_key, packet + 1 + crypto_box_PUBLICKEYBYTES, crypto_box_PUBLICKEYBYTES); |
@@ -313,8 +359,8 @@ static int cryptopacket_handle(void *object, IP_Port source, uint8_t *packet, ui | |||
313 | DHT *dht = object; | 359 | DHT *dht = object; |
314 | 360 | ||
315 | if (packet[0] == NET_PACKET_CRYPTO) { | 361 | if (packet[0] == NET_PACKET_CRYPTO) { |
316 | if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING || | 362 | if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + crypto_box_MACBYTES || |
317 | length > MAX_DATA_SIZE + ENCRYPTION_PADDING) | 363 | length > MAX_DATA_SIZE + crypto_box_MACBYTES) |
318 | return 1; | 364 | return 1; |
319 | 365 | ||
320 | if (memcmp(packet + 1, dht->c->self_public_key, crypto_box_PUBLICKEYBYTES) == 0) { // Check if request is for us. | 366 | if (memcmp(packet + 1, dht->c->self_public_key, crypto_box_PUBLICKEYBYTES) == 0) { // Check if request is for us. |