Skip to content

Commit

Permalink
Merge moderation portion of new groupchats codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
JFreegman committed Mar 30, 2022
1 parent 09575dc commit 633ceb8
Show file tree
Hide file tree
Showing 16 changed files with 1,783 additions and 36 deletions.
17 changes: 12 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -260,19 +260,25 @@ set(toxcore_SOURCES ${toxcore_SOURCES}
toxcore/friend_requests.c
toxcore/friend_requests.h)

# LAYER 6: Tox messenger
# LAYER 6: DHT based group chats
# ----------------------------------------
set(toxcore_SOURCES ${toxcore_SOURCES}
toxcore/group_moderation.c
toxcore/group_moderation.h)

# LAYER 7: Tox messenger
# ----------------------
set(toxcore_SOURCES ${toxcore_SOURCES}
toxcore/Messenger.c
toxcore/Messenger.h)

# LAYER 7: Group chats
# LAYER 8: Conferences
# --------------------
set(toxcore_SOURCES ${toxcore_SOURCES}
toxcore/group.c
toxcore/group.h)

# LAYER 8: Public API
# LAYER 9: Public API
# -------------------
set(toxcore_SOURCES ${toxcore_SOURCES}
toxcore/tox_api.c
Expand All @@ -281,7 +287,7 @@ set(toxcore_SOURCES ${toxcore_SOURCES}
toxcore/tox_private.h)
set(toxcore_API_HEADERS ${toxcore_API_HEADERS} ${toxcore_SOURCE_DIR}/toxcore/tox.h^tox)

# LAYER 9: New async events API
# LAYER 10: New async events API
# -------------------
set(toxcore_SOURCES ${toxcore_SOURCES}
third_party/cmp/cmp.c
Expand Down Expand Up @@ -319,7 +325,7 @@ set(toxcore_SOURCES ${toxcore_SOURCES}
toxcore/tox_unpack.h)
set(toxcore_API_HEADERS ${toxcore_API_HEADERS} ${toxcore_SOURCE_DIR}/toxcore/tox_events.h^tox)

# LAYER 10: Dispatch recorded events to callbacks.
# LAYER 11: Dispatch recorded events to callbacks.
# -------------------
set(toxcore_SOURCES ${toxcore_SOURCES}
toxcore/tox_dispatch.c
Expand Down Expand Up @@ -438,6 +444,7 @@ unit_test(toxav rtp)
unit_test(toxcore DHT)
unit_test(toxcore bin_pack)
unit_test(toxcore crypto_core)
unit_test(toxcore group_moderation)
unit_test(toxcore mono_time)
unit_test(toxcore ping_array)
unit_test(toxcore tox)
Expand Down
1 change: 1 addition & 0 deletions other/docker/coverage/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ RUN source .github/scripts/flags-coverage.sh \
-DSTRICT_ABI=ON \
-DAUTOTEST=ON \
-DPROXY_TEST=ON \
-DUSE_IPV6=OFF \
&& cmake --build _build --parallel 8 --target install

WORKDIR /work/_build
Expand Down
1 change: 1 addition & 0 deletions testing/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ sh_test(
args = ["$(locations %s)" % f for f in CIMPLE_FILES] + [
"-Wno-boolean-return",
"-Wno-callback-names",
"-Wno-callgraph",
"-Wno-enum-names",
"-Wno-type-check",
"+RTS",
Expand Down
42 changes: 42 additions & 0 deletions toxcore/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ cc_test(
flaky = True,
deps = [
":crypto_core",
":util",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
Expand Down Expand Up @@ -163,6 +164,7 @@ cc_library(
],
deps = [
":attributes",
":ccompat",
":crypto_core",
"@pthread",
],
Expand Down Expand Up @@ -487,6 +489,46 @@ cc_library(
],
)

cc_library(
name = "group_moderation",
srcs = ["group_moderation.c"],
hdrs = ["group_moderation.h"],
deps = [
":DHT",
":ccompat",
":crypto_core",
":logger",
":mono_time",
":network",
":util",
"@libsodium",
],
)

cc_test(
name = "group_moderation_test",
size = "small",
srcs = ["group_moderation_test.cc"],
deps = [
":crypto_core",
":group_moderation",
":logger",
":util",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
)

cc_fuzz_test(
name = "group_moderation_fuzz_test",
srcs = ["group_moderation_fuzz_test.cc"],
corpus = ["//tools/toktok-fuzzer/corpus:group_moderation_fuzz_test"],
deps = [
":group_moderation",
"//c-toxcore/testing/fuzzing:fuzz_support",
],
)

cc_library(
name = "Messenger",
srcs = ["Messenger.c"],
Expand Down
12 changes: 7 additions & 5 deletions toxcore/DHT.c
Original file line number Diff line number Diff line change
Expand Up @@ -1800,7 +1800,7 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co
}

if (num_nodes > 0 && (mono_time_is_timeout(dht->mono_time, *lastgetnode, GET_NODE_INTERVAL)
|| *bootstrap_times < MAX_BOOTSTRAP_TIMES)) {
|| *bootstrap_times < MAX_BOOTSTRAP_TIMES)) {
uint32_t rand_node = random_range_u32(num_nodes);

if ((num_nodes - 1) != rand_node) {
Expand Down Expand Up @@ -1855,8 +1855,8 @@ static void do_Close(DHT *dht)
dht->num_to_bootstrap = 0;

const uint8_t not_killed = do_ping_and_sendnode_requests(
dht, &dht->close_lastgetnodes, dht->self_public_key, dht->close_clientlist, LCLIENT_LIST, &dht->close_bootstrap_times,
false);
dht, &dht->close_lastgetnodes, dht->self_public_key, dht->close_clientlist, LCLIENT_LIST, &dht->close_bootstrap_times,
false);

if (not_killed != 0) {
return;
Expand Down Expand Up @@ -2568,7 +2568,8 @@ static int handle_LANdiscovery(void *object, const IP_Port *source, const uint8_

/*----------------------------------------------------------------------------------*/

DHT *new_dht(const Logger *log, Mono_Time *mono_time, Networking_Core *net, bool hole_punching_enabled, bool lan_discovery_enabled)
DHT *new_dht(const Logger *log, Mono_Time *mono_time, Networking_Core *net, bool hole_punching_enabled,
bool lan_discovery_enabled)
{
if (net == nullptr) {
return nullptr;
Expand Down Expand Up @@ -2761,7 +2762,8 @@ void dht_save(const DHT *dht, uint8_t *data)
}
}

state_write_section_header(old_data, DHT_STATE_COOKIE_TYPE, pack_nodes(dht->log, data, sizeof(Node_format) * num, clients, num), DHT_STATE_TYPE_NODES);
state_write_section_header(old_data, DHT_STATE_COOKIE_TYPE, pack_nodes(dht->log, data, sizeof(Node_format) * num,
clients, num), DHT_STATE_TYPE_NODES);

free(clients);
}
Expand Down
6 changes: 5 additions & 1 deletion toxcore/DHT.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
extern "C" {
#endif

/* Maximum size of a signature (may be smaller) */
#define SIGNATURE_SIZE CRYPTO_SIGNATURE_SIZE
/** Maximum number of clients stored per friend. */
#define MAX_FRIEND_CLIENTS 8

Expand Down Expand Up @@ -64,6 +66,7 @@ extern "C" {
#define CRYPTO_PACKET_DHTPK 156
#define CRYPTO_PACKET_NAT_PING 254 // NAT ping crypto packet ID.

/* Max size of a packed node for IPV4 and IPV6 respectively */
#define PACKED_NODE_SIZE_IP4 (1 + SIZE_IP4 + sizeof(uint16_t) + CRYPTO_PUBLIC_KEY_SIZE)
#define PACKED_NODE_SIZE_IP6 (1 + SIZE_IP6 + sizeof(uint16_t) + CRYPTO_PUBLIC_KEY_SIZE)

Expand Down Expand Up @@ -467,7 +470,8 @@ int dht_load(DHT *dht, const uint8_t *data, uint32_t length);

/** Initialize DHT. */
non_null()
DHT *new_dht(const Logger *log, Mono_Time *mono_time, Networking_Core *net, bool hole_punching_enabled, bool lan_discovery_enabled);
DHT *new_dht(const Logger *log, Mono_Time *mono_time, Networking_Core *net, bool hole_punching_enabled,
bool lan_discovery_enabled);

non_null()
void kill_dht(DHT *dht);
Expand Down
23 changes: 3 additions & 20 deletions toxcore/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,6 @@ int getfriendcon_id(const Messenger *m, int32_t friendnumber)
return m->friendlist[friendnumber].friendcon_id;
}

/**
* @return a uint16_t that represents the checksum of address of length len.
*/
non_null()
static uint16_t address_checksum(const uint8_t *address, uint32_t len)
{
uint8_t checksum[2] = {0};
uint16_t check;

for (uint32_t i = 0; i < len; ++i) {
checksum[i % 2] ^= address[i];
}

memcpy(&check, checksum, sizeof(check));
return check;
}

/**
* Format: `[real_pk (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)]`
*
Expand All @@ -119,7 +102,7 @@ void getaddress(const Messenger *m, uint8_t *address)
pk_copy(address, nc_get_self_public_key(m->net_crypto));
uint32_t nospam = get_nospam(m->fr);
memcpy(address + CRYPTO_PUBLIC_KEY_SIZE, &nospam, sizeof(nospam));
uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum));
uint16_t checksum = data_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum));
memcpy(address + CRYPTO_PUBLIC_KEY_SIZE + sizeof(nospam), &checksum, sizeof(checksum));
}

Expand Down Expand Up @@ -236,7 +219,7 @@ int32_t m_addfriend(Messenger *m, const uint8_t *address, const uint8_t *data, u
}

uint16_t check;
const uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum));
const uint16_t checksum = data_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum));
memcpy(&check, address + CRYPTO_PUBLIC_KEY_SIZE + sizeof(uint32_t), sizeof(check));

if (check != checksum) {
Expand Down Expand Up @@ -2887,7 +2870,7 @@ static State_Load_Status friends_list_load(Messenger *m, const uint8_t *data, ui
uint8_t address[FRIEND_ADDRESS_SIZE];
pk_copy(address, temp.real_pk);
memcpy(address + CRYPTO_PUBLIC_KEY_SIZE, &temp.friendrequest_nospam, sizeof(uint32_t));
uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum));
uint16_t checksum = data_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum));
memcpy(address + CRYPTO_PUBLIC_KEY_SIZE + sizeof(uint32_t), &checksum, sizeof(checksum));
m_addfriend(m, address, temp.info, net_ntohs(temp.info_size));
}
Expand Down
51 changes: 51 additions & 0 deletions toxcore/crypto_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
#define crypto_box_MACBYTES (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES)
#endif

#ifndef VANILLA_NACL
// Need dht because of ENC_SECRET_KEY_SIZE and ENC_PUBLIC_KEY_SIZE
#define ENC_PUBLIC_KEY_SIZE CRYPTO_PUBLIC_KEY_SIZE
#define ENC_SECRET_KEY_SIZE CRYPTO_SECRET_KEY_SIZE
#endif

//!TOKSTYLE-
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
#include "../testing/fuzzing/fuzz_adapter.h"
Expand All @@ -58,6 +64,31 @@ static_assert(CRYPTO_SHA512_SIZE == crypto_hash_sha512_BYTES,
static_assert(CRYPTO_PUBLIC_KEY_SIZE == 32,
"CRYPTO_PUBLIC_KEY_SIZE is required to be 32 bytes for public_key_eq to work");

#ifndef VANILLA_NACL
static_assert(CRYPTO_SIGNATURE_SIZE == crypto_sign_BYTES,
"CRYPTO_SIGNATURE_SIZE should be equal to crypto_sign_BYTES");
static_assert(CRYPTO_SIGN_PUBLIC_KEY_SIZE == crypto_sign_PUBLICKEYBYTES,
"CRYPTO_SIGN_PUBLIC_KEY_SIZE should be equal to crypto_sign_PUBLICKEYBYTES");
static_assert(CRYPTO_SIGN_SECRET_KEY_SIZE == crypto_sign_SECRETKEYBYTES,
"CRYPTO_SIGN_SECRET_KEY_SIZE should be equal to crypto_sign_SECRETKEYBYTES");
#endif /* VANILLA_NACL */

#ifndef VANILLA_NACL
bool create_extended_keypair(uint8_t *pk, uint8_t *sk)
{
/* create signature key pair */
crypto_sign_keypair(pk + ENC_PUBLIC_KEY_SIZE, sk + ENC_SECRET_KEY_SIZE);

/* convert public signature key to public encryption key */
const int res1 = crypto_sign_ed25519_pk_to_curve25519(pk, pk + ENC_PUBLIC_KEY_SIZE);

/* convert secret signature key to secret encryption key */
const int res2 = crypto_sign_ed25519_sk_to_curve25519(sk, sk + ENC_SECRET_KEY_SIZE);

return res1 == 0 && res2 == 0;
}
#endif

#if !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
static uint8_t *crypto_malloc(size_t bytes)
{
Expand Down Expand Up @@ -177,6 +208,26 @@ uint32_t random_range_u32(uint32_t upper_bound)
#endif // VANILLA_NACL
}

bool crypto_signature_create(uint8_t *signature, const uint8_t *message, uint64_t message_length,
const uint8_t *secret_key)
{
#ifdef VANILLA_NACL
return false;
#else
return crypto_sign_detached(signature, nullptr, message, message_length, secret_key) == 0;
#endif // VANILLA_NACL
}

bool crypto_signature_verify(const uint8_t *signature, const uint8_t *message, uint64_t message_length,
const uint8_t *public_key)
{
#ifdef VANILLA_NACL
return false;
#else
return crypto_sign_verify_detached(signature, message, message_length, public_key) == 0;
#endif
}

bool public_key_valid(const uint8_t *public_key)
{
if (public_key[31] >= 128) { /* Last bit of key is always zero. */
Expand Down
Loading

0 comments on commit 633ceb8

Please sign in to comment.