summaryrefslogtreecommitdiff
path: root/toxcore/net_crypto.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-09-02 09:55:37 -0400
committerirungentoo <irungentoo@gmail.com>2013-09-02 09:55:37 -0400
commit52336565612207fdb2d2068989da936b5b83ceeb (patch)
treec1c3c9ceb9c09d6d4a42d13aba294e53453d5b30 /toxcore/net_crypto.c
parent36a3b02f63aedeeb09105832ba75252bc06b8598 (diff)
Properly fixed signed/unsigned comparisons.
Diffstat (limited to 'toxcore/net_crypto.c')
-rw-r--r--toxcore/net_crypto.c16
1 files changed, 10 insertions, 6 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index f55f40a1..b168fc76 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -32,7 +32,10 @@
32#define CONN_ESTABLISHED 3 32#define CONN_ESTABLISHED 3
33#define CONN_TIMED_OUT 4 33#define CONN_TIMED_OUT 4
34 34
35static uint8_t crypt_id_valid(int crypt_connection_id, Net_Crypto *c) { return crypt_connection_id < 0 || (uint32_t)crypt_connection_id >= c->crypto_connections_length; } 35static uint8_t crypt_connection_id_not_valid(Net_Crypto *c, int crypt_connection_id)
36{
37 return (uint32_t)crypt_connection_id >= c->crypto_connections_length;
38}
36 39
37/* Use this instead of memcmp; not vulnerable to timing attacks. */ 40/* Use this instead of memcmp; not vulnerable to timing attacks. */
38uint8_t crypto_iszero(uint8_t *mem, uint32_t length) 41uint8_t crypto_iszero(uint8_t *mem, uint32_t length)
@@ -152,7 +155,7 @@ void random_nonce(uint8_t *nonce)
152 */ 155 */
153int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data) 156int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data)
154{ 157{
155 if (crypt_id_valid(crypt_connection_id,c)) 158 if (crypt_connection_id_not_valid(c, crypt_connection_id))
156 return 0; 159 return 0;
157 160
158 if (c->crypto_connections[crypt_connection_id].status != CONN_ESTABLISHED) 161 if (c->crypto_connections[crypt_connection_id].status != CONN_ESTABLISHED)
@@ -184,7 +187,7 @@ int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data)
184 */ 187 */
185int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length) 188int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length)
186{ 189{
187 if (crypt_id_valid(crypt_connection_id,c)) 190 if (crypt_connection_id_not_valid(c, crypt_connection_id))
188 return 0; 191 return 0;
189 192
190 if (length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE - 1) 193 if (length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE - 1)
@@ -307,7 +310,8 @@ static int cryptopacket_handle(void *object, IP_Port source, uint8_t *packet, ui
307 310
308 } else { /* If request is not for us, try routing it. */ 311 } else { /* If request is not for us, try routing it. */
309 int retval = route_packet(dht, packet + 1, packet, length); 312 int retval = route_packet(dht, packet + 1, packet, length);
310 if (retval < 0 || (uint32_t)retval == length) 313
314 if ((unsigned int)retval == length)
311 return 0; 315 return 0;
312 } 316 }
313 } 317 }
@@ -508,7 +512,7 @@ int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, ui
508 */ 512 */
509int crypto_kill(Net_Crypto *c, int crypt_connection_id) 513int crypto_kill(Net_Crypto *c, int crypt_connection_id)
510{ 514{
511 if (crypt_id_valid(crypt_connection_id,c)) 515 if (crypt_connection_id_not_valid(c, crypt_connection_id))
512 return 1; 516 return 1;
513 517
514 if (c->crypto_connections[crypt_connection_id].status != CONN_NO_CONNECTION) { 518 if (c->crypto_connections[crypt_connection_id].status != CONN_NO_CONNECTION) {
@@ -599,7 +603,7 @@ int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key,
599 */ 603 */
600int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id) 604int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id)
601{ 605{
602 if (crypt_connection_id >= 0 && (uint32_t)crypt_connection_id < c->crypto_connections_length) 606 if ((unsigned int)crypt_connection_id < c->crypto_connections_length)
603 return c->crypto_connections[crypt_connection_id].status; 607 return c->crypto_connections[crypt_connection_id].status;
604 608
605 return CONN_NO_CONNECTION; 609 return CONN_NO_CONNECTION;