From 1387c8f15032cab1af1c6444621b62af8d3a5494 Mon Sep 17 00:00:00 2001 From: Diadlo Date: Sun, 29 Jan 2017 22:36:22 +0300 Subject: [PATCH] Remove network dependency from toxcrypto --- CMakeLists.txt | 3 --- toxcore/crypto_core.c | 19 ++++++++++++++++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 244c13d1..201358ef 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -205,9 +205,6 @@ include(CheckFunctionExists) check_function_exists(explicit_bzero HAVE_EXPLICIT_BZERO) check_function_exists(memset_s HAVE_MEMSET_S) target_link_modules(toxcrypto ${LIBSODIUM_LIBRARIES}) -if(WIN32) - target_link_modules(toxcrypto ws2_32) # for htonl -endif() # LAYER 2: Basic networking # ------------------------- diff --git a/toxcore/crypto_core.c b/toxcore/crypto_core.c index ebbb1314..8e34b587 100644 --- a/toxcore/crypto_core.c +++ b/toxcore/crypto_core.c @@ -27,10 +27,9 @@ #include "config.h" #endif +#include "ccompat.h" #include "crypto_core.h" -#include "network.h" - #include #ifndef VANILLA_NACL @@ -210,6 +209,20 @@ void increment_nonce(uint8_t *nonce) carry >>= 8; } } + +static uint32_t host_to_network(uint32_t x) +{ +#if BYTE_ORDER == LITTLE_ENDIAN + return + ((x >> 24) & 0x000000FF) | // move byte 3 to byte 0 + ((x >> 8) & 0x0000FF00) | // move byte 2 to byte 1 + ((x << 8) & 0x00FF0000) | // move byte 1 to byte 2 + ((x << 24) & 0xFF000000); // move byte 0 to byte 3 +#else + return x; +#endif +} + /* increment the given nonce by num */ void increment_nonce_number(uint8_t *nonce, uint32_t host_order_num) { @@ -218,7 +231,7 @@ void increment_nonce_number(uint8_t *nonce, uint32_t host_order_num) * that loop bounds and their potential underflow or overflow * are independent of user-controlled input (you may have heard of the Heartbleed bug). */ - const uint32_t big_endian_num = net_htonl(host_order_num); + const uint32_t big_endian_num = host_to_network(host_order_num); const uint8_t *const num_vec = (const uint8_t *) &big_endian_num; uint8_t num_as_nonce[crypto_box_NONCEBYTES] = {0}; num_as_nonce[crypto_box_NONCEBYTES - 4] = num_vec[0];