diff options
Diffstat (limited to 'toxcore/net_crypto.c')
-rw-r--r-- | toxcore/net_crypto.c | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index b6f08fe4..3f866f74 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c | |||
@@ -145,14 +145,26 @@ static void increment_nonce(uint8_t *nonce) | |||
145 | /* Fill the given nonce with random bytes. */ | 145 | /* Fill the given nonce with random bytes. */ |
146 | void random_nonce(uint8_t *nonce) | 146 | void random_nonce(uint8_t *nonce) |
147 | { | 147 | { |
148 | uint32_t i, temp; | 148 | randombytes(nonce, crypto_box_NONCEBYTES); |
149 | } | ||
150 | |||
151 | |||
152 | static uint8_t base_nonce[crypto_box_NONCEBYTES]; | ||
153 | static uint8_t nonce_set = 0; | ||
149 | 154 | ||
150 | for (i = 0; i < crypto_box_NONCEBYTES / 4; ++i) { | 155 | /*Gives a nonce guaranteed to be different from previous ones.*/ |
151 | temp = random_int(); | 156 | void new_nonce(uint8_t *nonce) |
152 | memcpy(nonce + 4 * i, &temp, 4); | 157 | { |
158 | if (nonce_set == 0) { | ||
159 | random_nonce(base_nonce); | ||
160 | nonce_set = 1; | ||
153 | } | 161 | } |
162 | |||
163 | increment_nonce(base_nonce); | ||
164 | memcpy(nonce, base_nonce, crypto_box_NONCEBYTES); | ||
154 | } | 165 | } |
155 | 166 | ||
167 | |||
156 | /* return 0 if there is no received data in the buffer. | 168 | /* return 0 if there is no received data in the buffer. |
157 | * return -1 if the packet was discarded. | 169 | * return -1 if the packet was discarded. |
158 | * return length of received data if successful. | 170 | * return length of received data if successful. |
@@ -237,7 +249,7 @@ int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t * | |||
237 | uint8_t temp[MAX_DATA_SIZE]; | 249 | uint8_t temp[MAX_DATA_SIZE]; |
238 | memcpy(temp + 1, data, length); | 250 | memcpy(temp + 1, data, length); |
239 | temp[0] = request_id; | 251 | temp[0] = request_id; |
240 | random_nonce(nonce); | 252 | new_nonce(nonce); |
241 | int len = encrypt_data(recv_public_key, send_secret_key, nonce, temp, length + 1, | 253 | int len = encrypt_data(recv_public_key, send_secret_key, nonce, temp, length + 1, |
242 | 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + packet); | 254 | 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + packet); |
243 | 255 | ||
@@ -336,7 +348,7 @@ static int send_cryptohandshake(Net_Crypto *c, int connection_id, uint8_t *publi | |||
336 | uint8_t temp[crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES]; | 348 | uint8_t temp[crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES]; |
337 | uint8_t nonce[crypto_box_NONCEBYTES]; | 349 | uint8_t nonce[crypto_box_NONCEBYTES]; |
338 | 350 | ||
339 | random_nonce(nonce); | 351 | new_nonce(nonce); |
340 | memcpy(temp, secret_nonce, crypto_box_NONCEBYTES); | 352 | memcpy(temp, secret_nonce, crypto_box_NONCEBYTES); |
341 | memcpy(temp + crypto_box_NONCEBYTES, session_key, crypto_box_PUBLICKEYBYTES); | 353 | memcpy(temp + crypto_box_NONCEBYTES, session_key, crypto_box_PUBLICKEYBYTES); |
342 | 354 | ||