diff --git a/CMakeLists.txt b/CMakeLists.txt index 7e5e5d25..7be074b1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -161,7 +161,6 @@ set(toxcore_PKGCONFIG_REQUIRES) # LAYER 1: Crypto core # -------------------- -apidsl(toxcore/crypto_core.api.h) set(toxcore_SOURCES ${toxcore_SOURCES} toxcore/ccompat.h toxcore/crypto_core.c @@ -189,9 +188,6 @@ set(toxcore_SOURCES ${toxcore_SOURCES} # LAYER 3: Distributed Hash Table # ------------------------------- -apidsl(toxcore/LAN_discovery.api.h) -apidsl(toxcore/ping.api.h) -apidsl(toxcore/ping_array.api.h) set(toxcore_SOURCES ${toxcore_SOURCES} toxcore/DHT.c toxcore/DHT.h diff --git a/auto_tests/BUILD.bazel b/auto_tests/BUILD.bazel index 9e75a1c5..c7a40c8c 100644 --- a/auto_tests/BUILD.bazel +++ b/auto_tests/BUILD.bazel @@ -15,6 +15,8 @@ cc_library( flaky_tests = { "crypto_core_test": True, "lan_discovery_test": True, + "save_load_test": True, + "tox_many_tcp_test": True, } [cc_test( diff --git a/toxcore/DHT.h b/toxcore/DHT.h index 6040df7d..c16d7325 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h @@ -211,7 +211,6 @@ typedef struct Shared_Keys { typedef int cryptopacket_handler_cb(void *object, IP_Port ip_port, const uint8_t *source_pubkey, const uint8_t *data, uint16_t len, void *userdata); -#define DHT_DEFINED typedef struct DHT DHT; const uint8_t *dht_get_self_public_key(const DHT *dht); diff --git a/toxcore/LAN_discovery.api.h b/toxcore/LAN_discovery.api.h deleted file mode 100644 index ad3d60ed..00000000 --- a/toxcore/LAN_discovery.api.h +++ /dev/null @@ -1,57 +0,0 @@ -%{ -/* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. - * Copyright © 2013 Tox project. - */ - -/* - * LAN discovery implementation. - */ -#ifndef C_TOXCORE_TOXCORE_LAN_DISCOVERY_H -#define C_TOXCORE_TOXCORE_LAN_DISCOVERY_H - -#include "DHT.h" -%} - -class dHT { struct this; } -class iP { struct this; } - -namespace lan_discovery { - -/** - * Interval in seconds between LAN discovery packet sending. - */ -#define LAN_DISCOVERY_INTERVAL 10 - -/** - * Send a LAN discovery pcaket to the broadcast address with port port. - */ -static int32_t send(uint16_t port, dHT::this *dht); - -/** - * Sets up packet handlers. - */ -static void init(dHT::this *dht); - -/** - * Clear packet handlers. - */ -static void kill(dHT::this *dht); - -} - -/** - * Is IP a local ip or not. - */ -static bool ip_is_local(iP::this ip); - -/** - * Checks if a given IP isn't routable. - * - * @return true if ip is a LAN ip, false if it is not. - */ -static bool ip_is_lan(iP::this ip); - -%{ -#endif // C_TOXCORE_TOXCORE_LAN_DISCOVERY_H -%} diff --git a/toxcore/LAN_discovery.h b/toxcore/LAN_discovery.h index 6b55e319..dcb9e532 100644 --- a/toxcore/LAN_discovery.h +++ b/toxcore/LAN_discovery.h @@ -11,16 +11,6 @@ #include "DHT.h" -#ifndef DHT_DEFINED -#define DHT_DEFINED -typedef struct DHT DHT; -#endif /* DHT_DEFINED */ - -#ifndef IP_DEFINED -#define IP_DEFINED -typedef struct IP IP; -#endif /* IP_DEFINED */ - /** * Interval in seconds between LAN discovery packet sending. */ diff --git a/toxcore/crypto_core.api.h b/toxcore/crypto_core.api.h deleted file mode 100644 index 84e7eff2..00000000 --- a/toxcore/crypto_core.api.h +++ /dev/null @@ -1,253 +0,0 @@ -%{ -/* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. - * Copyright © 2013 Tox project. - */ - -/* - * Functions for the core crypto. - */ -#ifndef C_TOXCORE_TOXCORE_CRYPTO_CORE_H -#define C_TOXCORE_TOXCORE_CRYPTO_CORE_H - -#include -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif -%} - -/** - * The number of bytes in a Tox public key. - */ -const CRYPTO_PUBLIC_KEY_SIZE = 32; - -/** - * The number of bytes in a Tox secret key. - */ -const CRYPTO_SECRET_KEY_SIZE = 32; - -/** - * The number of bytes in a shared key computed from public and secret keys. - */ -const CRYPTO_SHARED_KEY_SIZE = 32; - -/** - * The number of bytes in a symmetric key. - */ -const CRYPTO_SYMMETRIC_KEY_SIZE = CRYPTO_SHARED_KEY_SIZE; - -/** - * The number of bytes needed for the MAC (message authentication code) in an - * encrypted message. - */ -const CRYPTO_MAC_SIZE = 16; - -/** - * The number of bytes in a nonce used for encryption/decryption. - */ -const CRYPTO_NONCE_SIZE = 24; - -/** - * The number of bytes in a SHA256 hash. - */ -const CRYPTO_SHA256_SIZE = 32; - -/** - * The number of bytes in a SHA512 hash. - */ -const CRYPTO_SHA512_SIZE = 64; - -/** - * A `memcmp`-like function whose running time does not depend on the input - * bytes, only on the input length. Useful to compare sensitive data where - * timing attacks could reveal that data. - * - * This means for instance that comparing "aaaa" and "aaaa" takes 4 time, and - * "aaaa" and "baaa" also takes 4 time. With a regular `memcmp`, the latter may - * take 1 time, because it immediately knows that the two strings are not equal. - */ -static int32_t crypto_memcmp(const uint8_t *p1, const uint8_t *p2, size_t length); - -/** - * A `bzero`-like function which won't be optimised away by the compiler. Some - * compilers will inline `bzero` or `memset` if they can prove that there will - * be no reads to the written data. Use this function if you want to be sure the - * memory is indeed zeroed. - */ -static void crypto_memzero(void *data, size_t length); - -/** - * Compute a SHA256 hash (32 bytes). - */ -static void crypto_sha256(uint8_t[CRYPTO_SHA256_SIZE] hash, const uint8_t[length] data); - -/** - * Compute a SHA512 hash (64 bytes). - */ -static void crypto_sha512(uint8_t[CRYPTO_SHA512_SIZE] hash, const uint8_t[length] data); - -/** - * Compare 2 public keys of length CRYPTO_PUBLIC_KEY_SIZE, not vulnerable to - * timing attacks. - * - * @return 0 if both mem locations of length are equal, -1 if they are not. - */ -static int32_t public_key_cmp( - const uint8_t[CRYPTO_PUBLIC_KEY_SIZE] pk1, - const uint8_t[CRYPTO_PUBLIC_KEY_SIZE] pk2); - -namespace random { - -/** - * Return a random 8 bit integer. - */ -static uint8_t u08(); - -/** - * Return a random 16 bit integer. - */ -static uint16_t u16(); - -/** - * Return a random 32 bit integer. - */ -static uint32_t u32(); - -/** - * Return a random 64 bit integer. - */ -static uint64_t u64(); - -/** - * Fill the given nonce with random bytes. - */ -static void nonce(uint8_t[CRYPTO_NONCE_SIZE] nonce); - -/** - * Fill an array of bytes with random values. - */ -static void bytes(uint8_t[length] bytes); - -} - -/** - * Check if a Tox public key CRYPTO_PUBLIC_KEY_SIZE is valid or not. This - * should only be used for input validation. - * - * @return false if it isn't, true if it is. - */ -static bool public_key_valid(const uint8_t[CRYPTO_PUBLIC_KEY_SIZE] public_key); - -/** - * Generate a new random keypair. Every call to this function is likely to - * generate a different keypair. - */ -static int32_t crypto_new_keypair( - uint8_t[CRYPTO_PUBLIC_KEY_SIZE] public_key, - uint8_t[CRYPTO_SECRET_KEY_SIZE] secret_key); - -/** - * Derive the public key from a given secret key. - */ -static void crypto_derive_public_key( - uint8_t[CRYPTO_PUBLIC_KEY_SIZE] public_key, - const uint8_t[CRYPTO_SECRET_KEY_SIZE] secret_key); - -/** - * Encrypt plain text of the given length to encrypted of length + - * $CRYPTO_MAC_SIZE using the public key ($CRYPTO_PUBLIC_KEY_SIZE bytes) of the - * receiver and the secret key of the sender and a $CRYPTO_NONCE_SIZE byte - * nonce. - * - * @return -1 if there was a problem, length of encrypted data if everything - * was fine. - */ -static int32_t encrypt_data( - const uint8_t[CRYPTO_PUBLIC_KEY_SIZE] public_key, - const uint8_t[CRYPTO_SECRET_KEY_SIZE] secret_key, - const uint8_t[CRYPTO_NONCE_SIZE] nonce, - const uint8_t[length] plain, - uint8_t *encrypted); - - -/** - * Decrypt encrypted text of the given length to plain text of the given length - * - $CRYPTO_MAC_SIZE using the public key ($CRYPTO_PUBLIC_KEY_SIZE bytes) of - * the sender, the secret key of the receiver and a $CRYPTO_NONCE_SIZE byte - * nonce. - * - * @return -1 if there was a problem (decryption failed), length of plain text - * data if everything was fine. - */ -static int32_t decrypt_data( - const uint8_t[CRYPTO_PUBLIC_KEY_SIZE] public_key, - const uint8_t[CRYPTO_SECRET_KEY_SIZE] secret_key, - const uint8_t[CRYPTO_NONCE_SIZE] nonce, - const uint8_t[length] encrypted, - uint8_t *plain); - -/** - * Fast encrypt/decrypt operations. Use if this is not a one-time communication. - * $encrypt_precompute does the shared-key generation once so it does not have - * to be performed on every encrypt/decrypt. - */ -static int32_t encrypt_precompute( - const uint8_t[CRYPTO_PUBLIC_KEY_SIZE] public_key, - const uint8_t[CRYPTO_SECRET_KEY_SIZE] secret_key, - uint8_t[CRYPTO_SHARED_KEY_SIZE] shared_key); - -/** - * Encrypts plain of length length to encrypted of length + $CRYPTO_MAC_SIZE - * using a shared key $CRYPTO_SYMMETRIC_KEY_SIZE big and a $CRYPTO_NONCE_SIZE - * byte nonce. - * - * @return -1 if there was a problem, length of encrypted data if everything - * was fine. - */ -static int32_t encrypt_data_symmetric( - const uint8_t[CRYPTO_SHARED_KEY_SIZE] shared_key, - const uint8_t[CRYPTO_NONCE_SIZE] nonce, - const uint8_t[length] plain, - uint8_t *encrypted); - -/** - * Decrypts encrypted of length length to plain of length length - - * $CRYPTO_MAC_SIZE using a shared key CRYPTO_SHARED_KEY_SIZE big and a - * $CRYPTO_NONCE_SIZE byte nonce. - * - * @return -1 if there was a problem (decryption failed), length of plain data - * if everything was fine. - */ -static int32_t decrypt_data_symmetric( - const uint8_t[CRYPTO_SHARED_KEY_SIZE] shared_key, - const uint8_t[CRYPTO_NONCE_SIZE] nonce, - const uint8_t[length] encrypted, - uint8_t *plain); - -/** - * Increment the given nonce by 1 in big endian (rightmost byte incremented - * first). - */ -static void increment_nonce(uint8_t[CRYPTO_NONCE_SIZE] nonce); - -/** - * Increment the given nonce by a given number. The number should be in host - * byte order. - */ -static void increment_nonce_number(uint8_t[CRYPTO_NONCE_SIZE] nonce, uint32_t increment); - -/** - * Fill a key CRYPTO_SYMMETRIC_KEY_SIZE big with random bytes. - */ -static void new_symmetric_key(uint8_t[CRYPTO_SYMMETRIC_KEY_SIZE] key); - -%{ -#ifdef __cplusplus -} // extern "C" -#endif - -#endif // C_TOXCORE_TOXCORE_CRYPTO_CORE_H -%} diff --git a/toxcore/crypto_core.h b/toxcore/crypto_core.h index 2c828a8c..8160fefa 100644 --- a/toxcore/crypto_core.h +++ b/toxcore/crypto_core.h @@ -22,58 +22,42 @@ extern "C" { */ #define CRYPTO_PUBLIC_KEY_SIZE 32 -uint32_t crypto_public_key_size(void); - /** * The number of bytes in a Tox secret key. */ #define CRYPTO_SECRET_KEY_SIZE 32 -uint32_t crypto_secret_key_size(void); - /** * The number of bytes in a shared key computed from public and secret keys. */ #define CRYPTO_SHARED_KEY_SIZE 32 -uint32_t crypto_shared_key_size(void); - /** * The number of bytes in a symmetric key. */ #define CRYPTO_SYMMETRIC_KEY_SIZE CRYPTO_SHARED_KEY_SIZE -uint32_t crypto_symmetric_key_size(void); - /** * The number of bytes needed for the MAC (message authentication code) in an * encrypted message. */ #define CRYPTO_MAC_SIZE 16 -uint32_t crypto_mac_size(void); - /** * The number of bytes in a nonce used for encryption/decryption. */ #define CRYPTO_NONCE_SIZE 24 -uint32_t crypto_nonce_size(void); - /** * The number of bytes in a SHA256 hash. */ #define CRYPTO_SHA256_SIZE 32 -uint32_t crypto_sha256_size(void); - /** * The number of bytes in a SHA512 hash. */ #define CRYPTO_SHA512_SIZE 64 -uint32_t crypto_sha512_size(void); - /** * A `memcmp`-like function whose running time does not depend on the input * bytes, only on the input length. Useful to compare sensitive data where diff --git a/toxcore/mono_time.h b/toxcore/mono_time.h index 0951fc74..d3d1624d 100644 --- a/toxcore/mono_time.h +++ b/toxcore/mono_time.h @@ -12,8 +12,6 @@ extern "C" { #endif -#ifndef MONO_TIME_DEFINED -#define MONO_TIME_DEFINED /** * The timer portion of the toxcore event loop. * @@ -44,7 +42,6 @@ extern "C" { * implementation should at least theoretically match the specification. */ typedef struct Mono_Time Mono_Time; -#endif /* MONO_TIME_DEFINED */ Mono_Time *mono_time_new(void); void mono_time_free(Mono_Time *mono_time); diff --git a/toxcore/network.h b/toxcore/network.h index 5ad3e7ea..39658958 100644 --- a/toxcore/network.h +++ b/toxcore/network.h @@ -162,13 +162,11 @@ typedef union IP_Union { IP6 v6; } IP_Union; -#define IP_DEFINED typedef struct IP { Family family; IP_Union ip; } IP; -#define IP_PORT_DEFINED typedef struct IP_Port { IP ip; uint16_t port; diff --git a/toxcore/ping.api.h b/toxcore/ping.api.h deleted file mode 100644 index d4d071b1..00000000 --- a/toxcore/ping.api.h +++ /dev/null @@ -1,50 +0,0 @@ -%{ -/* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. - * Copyright © 2013 Tox project. - * Copyright © 2013 plutooo - */ - -/* - * Buffered pinging using cyclic arrays. - */ -#ifndef C_TOXCORE_TOXCORE_PING_H -#define C_TOXCORE_TOXCORE_PING_H - -#include "DHT.h" -#include "network.h" - -#include -%} - -class iP_Port { struct this; } -class dHT { struct this; } -class mono_Time { struct this; } - -class ping { - -struct this; - -static this new(const mono_Time::this *mono_time, dHT::this *dht); -void kill(); - -/** Add nodes to the to_ping list. - * All nodes in this list are pinged every TIME_TOPING seconds - * and are then removed from the list. - * If the list is full the nodes farthest from our public_key are replaced. - * The purpose of this list is to enable quick integration of new nodes into the - * network while preventing amplification attacks. - * - * return 0 if node was added. - * return -1 if node was not added. - */ -int32_t add(const uint8_t *public_key, iP_Port::this ip_port); -void iterate(); - -int32_t send_request(iP_Port::this ipp, const uint8_t *public_key); - -} - -%{ -#endif // C_TOXCORE_TOXCORE_PING_H -%} diff --git a/toxcore/ping.h b/toxcore/ping.h index 06878677..c088c5a1 100644 --- a/toxcore/ping.h +++ b/toxcore/ping.h @@ -15,25 +15,7 @@ #include -#ifndef IP_PORT_DEFINED -#define IP_PORT_DEFINED -typedef struct IP_Port IP_Port; -#endif /* IP_PORT_DEFINED */ - -#ifndef DHT_DEFINED -#define DHT_DEFINED -typedef struct DHT DHT; -#endif /* DHT_DEFINED */ - -#ifndef MONO_TIME_DEFINED -#define MONO_TIME_DEFINED -typedef struct Mono_Time Mono_Time; -#endif /* MONO_TIME_DEFINED */ - -#ifndef PING_DEFINED -#define PING_DEFINED typedef struct Ping Ping; -#endif /* PING_DEFINED */ Ping *ping_new(const struct Mono_Time *mono_time, DHT *dht); diff --git a/toxcore/ping_array.api.h b/toxcore/ping_array.api.h deleted file mode 100644 index d36e56dc..00000000 --- a/toxcore/ping_array.api.h +++ /dev/null @@ -1,66 +0,0 @@ -%{ -/* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. - * Copyright © 2013 Tox project. - */ - -/* - * Implementation of an efficient array to store that we pinged something. - */ -#ifndef C_TOXCORE_TOXCORE_PING_ARRAY_H -#define C_TOXCORE_TOXCORE_PING_ARRAY_H - -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif -%} - -class mono_Time { struct this; } - -class ping { class array { - -struct this; - -/** - * Initialize a Ping_Array. - * - * @param size represents the total size of the array and should be a power of 2. - * @param timeout represents the maximum timeout in seconds for the entry. - * - * @return 0 on success, -1 on failure. - */ -static this new(uint32_t size, uint32_t timeout); - -/** - * Free all the allocated memory in a Ping_Array. - */ -void kill(); - -/** - * Add a data with length to the Ping_Array list and return a ping_id. - * - * @return ping_id on success, 0 on failure. - */ -uint64_t add(const mono_Time::this *mono_time, const uint8_t *data, uint32_t length); - -/** - * Check if ping_id is valid and not timed out. - * - * On success, copies the data into data of length, - * - * @return length of data copied on success, -1 on failure. - */ -int32_t check(const mono_Time::this *mono_time, uint8_t[length] data, uint64_t ping_id); - -} } - -%{ -#ifdef __cplusplus -} // extern "C" -#endif - -#endif // C_TOXCORE_TOXCORE_PING_ARRAY_H -%} diff --git a/toxcore/ping_array.h b/toxcore/ping_array.h index 70c517de..3212272e 100644 --- a/toxcore/ping_array.h +++ b/toxcore/ping_array.h @@ -9,6 +9,8 @@ #ifndef C_TOXCORE_TOXCORE_PING_ARRAY_H #define C_TOXCORE_TOXCORE_PING_ARRAY_H +#include "mono_time.h" + #include #include @@ -16,15 +18,7 @@ extern "C" { #endif -#ifndef MONO_TIME_DEFINED -#define MONO_TIME_DEFINED -typedef struct Mono_Time Mono_Time; -#endif /* MONO_TIME_DEFINED */ - -#ifndef PING_ARRAY_DEFINED -#define PING_ARRAY_DEFINED typedef struct Ping_Array Ping_Array; -#endif /* PING_ARRAY_DEFINED */ /** * Initialize a Ping_Array.