From dd18ec32b57296661ce141b4af74c474e4de124e Mon Sep 17 00:00:00 2001 From: iphydf Date: Sat, 10 Feb 2024 19:21:58 +0000 Subject: [PATCH] refactor: Use structs for public key and secret key in NGC. --- toxcore/Messenger.c | 4 +- toxcore/crypto_core.c | 24 +-- toxcore/crypto_core.h | 40 ++-- toxcore/crypto_core_pack.c | 20 +- toxcore/crypto_core_test.cc | 8 +- toxcore/group_announce.c | 24 +-- toxcore/group_announce.h | 10 +- toxcore/group_announce_fuzz_test.cc | 19 +- toxcore/group_announce_test.cc | 38 ++-- toxcore/group_chats.c | 324 ++++++++++++++-------------- toxcore/group_chats.h | 6 +- toxcore/group_common.h | 6 +- toxcore/group_connection.c | 14 +- toxcore/group_moderation.c | 103 ++++----- toxcore/group_moderation.h | 28 +-- toxcore/group_moderation_test.cc | 76 +++---- toxcore/group_onion_announce.c | 4 +- toxcore/group_pack.c | 6 +- toxcore/util.c | 15 -- toxcore/util.h | 4 - 20 files changed, 386 insertions(+), 387 deletions(-) diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 947edef7e1..fe8b6f5389 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -2594,8 +2594,8 @@ static bool self_announce_group(const Messenger *m, GC_Chat *chat, Onion_Friend memcpy(&announce.base_announce.ip_port, &chat->self_ip_port, sizeof(IP_Port)); } - memcpy(announce.base_announce.peer_public_key, chat->self_public_key.enc, ENC_PUBLIC_KEY_SIZE); - memcpy(announce.chat_public_key, get_chat_id(&chat->chat_public_key), ENC_PUBLIC_KEY_SIZE); + announce.base_announce.peer_public_key = chat->self_public_key.enc; + announce.chat_public_key = chat->chat_public_key.enc; uint8_t gc_data[GCA_MAX_DATA_LENGTH]; const int length = gca_pack_public_announce(m->log, gc_data, GCA_MAX_DATA_LENGTH, &announce); diff --git a/toxcore/crypto_core.c b/toxcore/crypto_core.c index 7bb5bb929f..d7bed47248 100644 --- a/toxcore/crypto_core.c +++ b/toxcore/crypto_core.c @@ -50,41 +50,41 @@ bool create_extended_keypair(Extended_Public_Key *pk, Extended_Secret_Key *sk, c /* create signature key pair */ uint8_t seed[crypto_sign_SEEDBYTES]; random_bytes(rng, seed, crypto_sign_SEEDBYTES); - crypto_sign_seed_keypair(pk->sig, sk->sig, seed); + crypto_sign_seed_keypair(pk->sig.data, sk->sig.data, seed); crypto_memzero(seed, crypto_sign_SEEDBYTES); /* convert public signature key to public encryption key */ - const int res1 = crypto_sign_ed25519_pk_to_curve25519(pk->enc, pk->sig); + const int res1 = crypto_sign_ed25519_pk_to_curve25519(pk->enc.data, pk->sig.data); /* convert secret signature key to secret encryption key */ - const int res2 = crypto_sign_ed25519_sk_to_curve25519(sk->enc, sk->sig); + const int res2 = crypto_sign_ed25519_sk_to_curve25519(sk->enc.data, sk->sig.data); return res1 == 0 && res2 == 0; } const uint8_t *get_enc_key(const Extended_Public_Key *key) { - return key->enc; + return key->enc.data; } const uint8_t *get_sig_pk(const Extended_Public_Key *key) { - return key->sig; + return key->sig.data; } void set_sig_pk(Extended_Public_Key *key, const uint8_t *sig_pk) { - memcpy(key->sig, sig_pk, SIG_PUBLIC_KEY_SIZE); + memcpy(key->sig.data, sig_pk, SIG_PUBLIC_KEY_SIZE); } const uint8_t *get_sig_sk(const Extended_Secret_Key *key) { - return key->sig; + return key->sig.data; } const uint8_t *get_chat_id(const Extended_Public_Key *key) { - return key->sig; + return key->sig.data; } #if !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) @@ -210,16 +210,16 @@ uint32_t random_range_u32(const Random *rng, uint32_t upper_bound) bool crypto_signature_create(uint8_t signature[CRYPTO_SIGNATURE_SIZE], const uint8_t *message, uint64_t message_length, - const uint8_t secret_key[SIG_SECRET_KEY_SIZE]) + const Sign_Secret_Key *secret_key) { - return crypto_sign_detached(signature, nullptr, message, message_length, secret_key) == 0; + return crypto_sign_detached(signature, nullptr, message, message_length, secret_key->data) == 0; } bool crypto_signature_verify(const uint8_t signature[CRYPTO_SIGNATURE_SIZE], const uint8_t *message, uint64_t message_length, - const uint8_t public_key[SIG_PUBLIC_KEY_SIZE]) + const Sign_Public_Key *public_key) { - return crypto_sign_verify_detached(signature, message, message_length, public_key) == 0; + return crypto_sign_verify_detached(signature, message, message_length, public_key->data) == 0; } bool public_key_valid(const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]) diff --git a/toxcore/crypto_core.h b/toxcore/crypto_core.h index 979791bd97..40195ccf97 100644 --- a/toxcore/crypto_core.h +++ b/toxcore/crypto_core.h @@ -160,6 +160,32 @@ const Random *os_random(void); */ #define CRYPTO_HMAC_KEY_SIZE 32 +typedef struct Public_Key { + uint8_t data[CRYPTO_PUBLIC_KEY_SIZE]; +} Public_Key; + +typedef struct Secret_Key { + uint8_t data[CRYPTO_SECRET_KEY_SIZE]; +} Secret_Key; + +typedef struct Sign_Public_Key { + uint8_t data[CRYPTO_SIGN_PUBLIC_KEY_SIZE]; +} Sign_Public_Key; + +typedef struct Sign_Secret_Key { + uint8_t data[CRYPTO_SIGN_SECRET_KEY_SIZE]; +} Sign_Secret_Key; + +typedef struct Extended_Public_Key { + Public_Key enc; + Sign_Public_Key sig; +} Extended_Public_Key; + +typedef struct Extended_Secret_Key { + Secret_Key enc; + Sign_Secret_Key sig; +} Extended_Secret_Key; + /** * @brief A `bzero`-like function which won't be optimised away by the compiler. * @@ -285,7 +311,7 @@ uint32_t random_range_u32(const Random *rng, uint32_t upper_bound); non_null() bool crypto_signature_create(uint8_t signature[CRYPTO_SIGNATURE_SIZE], const uint8_t *message, uint64_t message_length, - const uint8_t secret_key[SIG_SECRET_KEY_SIZE]); + const Sign_Secret_Key *secret_key); /** @brief Verifies that the given signature was produced by a given message and public key. * @@ -300,7 +326,7 @@ bool crypto_signature_create(uint8_t signature[CRYPTO_SIGNATURE_SIZE], non_null() bool crypto_signature_verify(const uint8_t signature[CRYPTO_SIGNATURE_SIZE], const uint8_t *message, uint64_t message_length, - const uint8_t public_key[SIG_PUBLIC_KEY_SIZE]); + const Sign_Public_Key *public_key); /** * @brief Fill the given nonce with random bytes. @@ -324,16 +350,6 @@ void random_bytes(const Random *rng, uint8_t *bytes, size_t length); non_null() bool public_key_valid(const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]); -typedef struct Extended_Public_Key { - uint8_t enc[CRYPTO_PUBLIC_KEY_SIZE]; - uint8_t sig[CRYPTO_SIGN_PUBLIC_KEY_SIZE]; -} Extended_Public_Key; - -typedef struct Extended_Secret_Key { - uint8_t enc[CRYPTO_SECRET_KEY_SIZE]; - uint8_t sig[CRYPTO_SIGN_SECRET_KEY_SIZE]; -} Extended_Secret_Key; - /** * @brief Creates an extended keypair: curve25519 and ed25519 for encryption and signing * respectively. The Encryption keys are derived from the signature keys. diff --git a/toxcore/crypto_core_pack.c b/toxcore/crypto_core_pack.c index a173d39d8d..83c68752e7 100644 --- a/toxcore/crypto_core_pack.c +++ b/toxcore/crypto_core_pack.c @@ -15,10 +15,10 @@ bool pack_extended_public_key(const Extended_Public_Key *key, Bin_Pack *bp) { uint8_t ext_key[EXT_PUBLIC_KEY_SIZE]; - static_assert(sizeof(ext_key) == sizeof(key->enc) + sizeof(key->sig), + static_assert(sizeof(ext_key) == sizeof(key->enc.data) + sizeof(key->sig.data), "extended secret key size is not the sum of the encryption and sign secret key sizes"); - memcpy(ext_key, key->enc, sizeof(key->enc)); - memcpy(&ext_key[sizeof(key->enc)], key->sig, sizeof(key->sig)); + memcpy(ext_key, key->enc.data, sizeof(key->enc.data)); + memcpy(&ext_key[sizeof(key->enc.data)], key->sig.data, sizeof(key->sig.data)); return bin_pack_bin(bp, ext_key, sizeof(ext_key)); } @@ -26,10 +26,10 @@ bool pack_extended_public_key(const Extended_Public_Key *key, Bin_Pack *bp) bool pack_extended_secret_key(const Extended_Secret_Key *key, Bin_Pack *bp) { uint8_t ext_key[EXT_SECRET_KEY_SIZE]; - static_assert(sizeof(ext_key) == sizeof(key->enc) + sizeof(key->sig), + static_assert(sizeof(ext_key) == sizeof(key->enc.data) + sizeof(key->sig.data), "extended secret key size is not the sum of the encryption and sign secret key sizes"); - memcpy(ext_key, key->enc, sizeof(key->enc)); - memcpy(&ext_key[sizeof(key->enc)], key->sig, sizeof(key->sig)); + memcpy(ext_key, key->enc.data, sizeof(key->enc.data)); + memcpy(&ext_key[sizeof(key->enc.data)], key->sig.data, sizeof(key->sig.data)); const bool result = bin_pack_bin(bp, ext_key, sizeof(ext_key)); crypto_memzero(ext_key, sizeof(ext_key)); @@ -44,8 +44,8 @@ bool unpack_extended_public_key(Extended_Public_Key *key, Bin_Unpack *bu) return false; } - memcpy(key->enc, ext_key, sizeof(key->enc)); - memcpy(key->sig, &ext_key[sizeof(key->enc)], sizeof(key->sig)); + memcpy(key->enc.data, ext_key, sizeof(key->enc.data)); + memcpy(key->sig.data, &ext_key[sizeof(key->enc.data)], sizeof(key->sig.data)); return true; } @@ -58,8 +58,8 @@ bool unpack_extended_secret_key(Extended_Secret_Key *key, Bin_Unpack *bu) return false; } - memcpy(key->enc, ext_key, sizeof(key->enc)); - memcpy(key->sig, &ext_key[sizeof(key->enc)], sizeof(key->sig)); + memcpy(key->enc.data, ext_key, sizeof(key->enc.data)); + memcpy(key->sig.data, &ext_key[sizeof(key->enc.data)], sizeof(key->sig.data)); crypto_memzero(ext_key, sizeof(ext_key)); return true; diff --git a/toxcore/crypto_core_test.cc b/toxcore/crypto_core_test.cc index d18ae2daab..15807579af 100644 --- a/toxcore/crypto_core_test.cc +++ b/toxcore/crypto_core_test.cc @@ -81,10 +81,10 @@ TEST(CryptoCore, Signatures) // Try a few different sizes, including empty 0 length message. for (uint8_t i = 0; i < 100; ++i) { Signature signature; - EXPECT_TRUE(crypto_signature_create( - signature.data(), message.data(), message.size(), get_sig_sk(&sk))); - EXPECT_TRUE(crypto_signature_verify( - signature.data(), message.data(), message.size(), get_sig_pk(&pk))); + EXPECT_TRUE( + crypto_signature_create(signature.data(), message.data(), message.size(), &sk.sig)); + EXPECT_TRUE( + crypto_signature_verify(signature.data(), message.data(), message.size(), &pk.sig)); message.push_back(random_u08(rng)); } diff --git a/toxcore/group_announce.c b/toxcore/group_announce.c index ee083198c1..754ac44de0 100644 --- a/toxcore/group_announce.c +++ b/toxcore/group_announce.c @@ -44,12 +44,12 @@ static void remove_announces(GC_Announces_List *gc_announces_list, GC_Announces * Returns null if no announce is found. */ non_null() -static GC_Announces *get_announces_by_chat_id(const GC_Announces_List *gc_announces_list, const uint8_t *chat_id) +static GC_Announces *get_announces_by_chat_id(const GC_Announces_List *gc_announces_list, const Public_Key *chat_id) { GC_Announces *announces = gc_announces_list->root_announces; while (announces != nullptr) { - if (memcmp(announces->chat_id, chat_id, CHAT_ID_SIZE) == 0) { + if (memcmp(&announces->chat_id, chat_id, CHAT_ID_SIZE) == 0) { return announces; } @@ -60,7 +60,7 @@ static GC_Announces *get_announces_by_chat_id(const GC_Announces_List *gc_announ } int gca_get_announces(const GC_Announces_List *gc_announces_list, GC_Announce *gc_announces, uint8_t max_nodes, - const uint8_t *chat_id, const uint8_t *except_public_key) + const Public_Key *chat_id, const Public_Key *except_public_key) { if (gc_announces == nullptr || gc_announces_list == nullptr || chat_id == nullptr || max_nodes == 0 || except_public_key == nullptr) { @@ -78,7 +78,7 @@ int gca_get_announces(const GC_Announces_List *gc_announces_list, GC_Announce *g for (size_t i = 0; i < announces->index && i < GCA_MAX_SAVED_ANNOUNCES_PER_GC && added_count < max_nodes; ++i) { const size_t index = i % GCA_MAX_SAVED_ANNOUNCES_PER_GC; - if (memcmp(except_public_key, announces->peer_announces[index].base_announce.peer_public_key, + if (memcmp(except_public_key, &announces->peer_announces[index].base_announce.peer_public_key, ENC_PUBLIC_KEY_SIZE) == 0) { continue; } @@ -86,7 +86,7 @@ int gca_get_announces(const GC_Announces_List *gc_announces_list, GC_Announce *g bool already_added = false; for (size_t j = 0; j < added_count; ++j) { - if (memcmp(gc_announces[j].peer_public_key, announces->peer_announces[index].base_announce.peer_public_key, + if (memcmp(&gc_announces[j].peer_public_key, &announces->peer_announces[index].base_announce.peer_public_key, ENC_PUBLIC_KEY_SIZE) == 0) { already_added = true; break; @@ -125,7 +125,7 @@ int gca_pack_announce(const Logger *log, uint8_t *data, uint16_t length, const G } uint16_t offset = 0; - memcpy(data + offset, announce->peer_public_key, ENC_PUBLIC_KEY_SIZE); + memcpy(data + offset, announce->peer_public_key.data, ENC_PUBLIC_KEY_SIZE); offset += ENC_PUBLIC_KEY_SIZE; data[offset] = announce->ip_port_is_set ? 1 : 0; @@ -186,7 +186,7 @@ static int gca_unpack_announce(const Logger *log, const uint8_t *data, uint16_t } uint16_t offset = 0; - memcpy(announce->peer_public_key, data + offset, ENC_PUBLIC_KEY_SIZE); + memcpy(announce->peer_public_key.data, data + offset, ENC_PUBLIC_KEY_SIZE); offset += ENC_PUBLIC_KEY_SIZE; net_unpack_bool(&data[offset], &announce->ip_port_is_set); @@ -233,7 +233,7 @@ int gca_pack_public_announce(const Logger *log, uint8_t *data, uint16_t length, return -1; } - memcpy(data, public_announce->chat_public_key, CHAT_ID_SIZE); + memcpy(data, public_announce->chat_public_key.data, CHAT_ID_SIZE); const int packed_size = gca_pack_announce(log, data + CHAT_ID_SIZE, length - CHAT_ID_SIZE, &public_announce->base_announce); @@ -264,7 +264,7 @@ int gca_unpack_public_announce(const Logger *log, const uint8_t *data, uint16_t return -1; } - memcpy(public_announce->chat_public_key, data, CHAT_ID_SIZE); + memcpy(public_announce->chat_public_key.data, data, CHAT_ID_SIZE); const int base_announce_size = gca_unpack_announce(log, data + ENC_PUBLIC_KEY_SIZE, length - ENC_PUBLIC_KEY_SIZE, &public_announce->base_announce); @@ -361,7 +361,7 @@ static GC_Announces *gca_new_announces( announces->next_announce = gc_announces_list->root_announces; gc_announces_list->root_announces = announces; - memcpy(announces->chat_id, public_announce->chat_public_key, CHAT_ID_SIZE); + announces->chat_id = public_announce->chat_public_key; return announces; } @@ -373,7 +373,7 @@ GC_Peer_Announce *gca_add_announce(const Mono_Time *mono_time, GC_Announces_List return nullptr; } - GC_Announces *announces = get_announces_by_chat_id(gc_announces_list, public_announce->chat_public_key); + GC_Announces *announces = get_announces_by_chat_id(gc_announces_list, &public_announce->chat_public_key); // No entry for this chat_id exists so we create one if (announces == nullptr) { @@ -464,7 +464,7 @@ void do_gca(const Mono_Time *mono_time, GC_Announces_List *gc_announces_list) } } -void cleanup_gca(GC_Announces_List *gc_announces_list, const uint8_t *chat_id) +void cleanup_gca(GC_Announces_List *gc_announces_list, const Public_Key *chat_id) { if (gc_announces_list == nullptr || chat_id == nullptr) { return; diff --git a/toxcore/group_announce.h b/toxcore/group_announce.h index 72f2cfc1b8..2c4848129c 100644 --- a/toxcore/group_announce.h +++ b/toxcore/group_announce.h @@ -50,7 +50,7 @@ struct GC_Announce { uint8_t tcp_relays_count; bool ip_port_is_set; IP_Port ip_port; - uint8_t peer_public_key[ENC_PUBLIC_KEY_SIZE]; + Public_Key peer_public_key; }; /* Peer announce for specific group. */ @@ -62,12 +62,12 @@ struct GC_Peer_Announce { /* Used for announces in public groups. */ struct GC_Public_Announce { GC_Announce base_announce; - uint8_t chat_public_key[ENC_PUBLIC_KEY_SIZE]; + Public_Key chat_public_key; }; /* A linked list that holds all announces for a particular group. */ struct GC_Announces { - uint8_t chat_id[CHAT_ID_SIZE]; + Public_Key chat_id; uint64_t index; uint64_t last_announce_received_timestamp; @@ -109,7 +109,7 @@ void do_gca(const Mono_Time *mono_time, GC_Announces_List *gc_announces_list); * @param chat_id The chat ID that designates the entry we want to remove. */ non_null() -void cleanup_gca(GC_Announces_List *gc_announces_list, const uint8_t *chat_id); +void cleanup_gca(GC_Announces_List *gc_announces_list, const Public_Key *chat_id); /** @brief Puts a set of announces from the announces list in supplied list. * @@ -124,7 +124,7 @@ void cleanup_gca(GC_Announces_List *gc_announces_list, const uint8_t *chat_id); */ non_null() int gca_get_announces(const GC_Announces_List *gc_announces_list, GC_Announce *gc_announces, uint8_t max_nodes, - const uint8_t *chat_id, const uint8_t *except_public_key); + const Public_Key *chat_id, const Public_Key *except_public_key); /** @brief Adds a public_announce to list of announces. * diff --git a/toxcore/group_announce_fuzz_test.cc b/toxcore/group_announce_fuzz_test.cc index eb0dfb6290..41da0171fb 100644 --- a/toxcore/group_announce_fuzz_test.cc +++ b/toxcore/group_announce_fuzz_test.cc @@ -87,16 +87,25 @@ void TestDoGca(Fuzz_Data &input) CONSUME1_OR_RETURN(const uint8_t, max_nodes, input); // Always allocate at least something to avoid passing nullptr to functions below. std::vector gc_announces(max_nodes + 1); - CONSUME_OR_RETURN(const uint8_t *chat_id, input, CHAT_ID_SIZE); - CONSUME_OR_RETURN(const uint8_t *except_public_key, input, ENC_PUBLIC_KEY_SIZE); + + CONSUME_OR_RETURN(const uint8_t *chat_id_data, input, CHAT_ID_SIZE); + Public_Key chat_id; + memcpy(chat_id.data, chat_id_data, CHAT_ID_SIZE); + + CONSUME_OR_RETURN(const uint8_t *except_public_key_data, input, ENC_PUBLIC_KEY_SIZE); + Public_Key except_public_key; + memcpy(except_public_key.data, except_public_key_data, ENC_PUBLIC_KEY_SIZE); + gca_get_announces( - gca.get(), gc_announces.data(), max_nodes, chat_id, except_public_key); + gca.get(), gc_announces.data(), max_nodes, &chat_id, &except_public_key); break; } case 3: { // Remove a chat. - CONSUME_OR_RETURN(const uint8_t *chat_id, input, CHAT_ID_SIZE); - cleanup_gca(gca.get(), chat_id); + CONSUME_OR_RETURN(const uint8_t *chat_id_data, input, CHAT_ID_SIZE); + Public_Key chat_id; + memcpy(chat_id.data, chat_id_data, CHAT_ID_SIZE); + cleanup_gca(gca.get(), &chat_id); break; } } diff --git a/toxcore/group_announce_test.cc b/toxcore/group_announce_test.cc index 2918dc76c6..414d6c1a7b 100644 --- a/toxcore/group_announce_test.cc +++ b/toxcore/group_announce_test.cc @@ -53,7 +53,7 @@ TEST_F(Announces, KillGcaOnNullptrIsNoop) TEST_F(Announces, CanBeCreatedAndDeleted) { GC_Public_Announce ann{}; - ann.chat_public_key[0] = 0x88; + ann.chat_public_key.data[0] = 0x88; ASSERT_NE(gca_add_announce(mono_time_, gca_, &ann), nullptr); #ifndef _DEBUG ASSERT_EQ(gca_add_announce(mono_time_, gca_, nullptr), nullptr); @@ -66,10 +66,10 @@ TEST_F(Announces, AnnouncesCanTimeOut) advance_clock(100); ASSERT_EQ(gca_->root_announces, nullptr); GC_Public_Announce ann{}; - ann.chat_public_key[0] = 0xae; + ann.chat_public_key.data[0] = 0xae; ASSERT_NE(gca_add_announce(mono_time_, gca_, &ann), nullptr); ASSERT_NE(gca_->root_announces, nullptr); - ASSERT_EQ(gca_->root_announces->chat_id[0], 0xae); + ASSERT_EQ(gca_->root_announces->chat_id.data[0], 0xae); // One iteration without having any time passed => announce is still here. do_gca(mono_time_, gca_); @@ -90,28 +90,28 @@ TEST_F(Announces, AnnouncesGetAndCleanup) { GC_Public_Announce ann1{}; GC_Public_Announce ann2{}; - ann1.chat_public_key[0] = 0x91; - ann1.base_announce.peer_public_key[0] = 0x7f; - ann2.chat_public_key[0] = 0x92; - ann2.base_announce.peer_public_key[0] = 0x7c; + ann1.chat_public_key.data[0] = 0x91; + ann1.base_announce.peer_public_key.data[0] = 0x7f; + ann2.chat_public_key.data[0] = 0x92; + ann2.base_announce.peer_public_key.data[0] = 0x7c; ASSERT_NE(gca_add_announce(mono_time_, gca_, &ann1), nullptr); ASSERT_NE(gca_add_announce(mono_time_, gca_, &ann2), nullptr); ASSERT_NE(gca_add_announce(mono_time_, gca_, &ann2), nullptr); - uint8_t empty_pk[ENC_PUBLIC_KEY_SIZE] = {0}; + Public_Key empty_pk = {{0}}; GC_Announce announces; - ASSERT_EQ(gca_get_announces(gca_, &announces, 1, ann1.chat_public_key, empty_pk), 1); - ASSERT_EQ(gca_get_announces(gca_, &announces, 1, ann2.chat_public_key, empty_pk), 1); + ASSERT_EQ(gca_get_announces(gca_, &announces, 1, &ann1.chat_public_key, &empty_pk), 1); + ASSERT_EQ(gca_get_announces(gca_, &announces, 1, &ann2.chat_public_key, &empty_pk), 1); - cleanup_gca(gca_, ann1.chat_public_key); - ASSERT_EQ(gca_get_announces(gca_, &announces, 1, ann1.chat_public_key, empty_pk), 0); + cleanup_gca(gca_, &ann1.chat_public_key); + ASSERT_EQ(gca_get_announces(gca_, &announces, 1, &ann1.chat_public_key, &empty_pk), 0); - cleanup_gca(gca_, ann2.chat_public_key); - ASSERT_EQ(gca_get_announces(gca_, &announces, 1, ann2.chat_public_key, empty_pk), 0); + cleanup_gca(gca_, &ann2.chat_public_key); + ASSERT_EQ(gca_get_announces(gca_, &announces, 1, &ann2.chat_public_key, &empty_pk), 0); #ifndef _DEBUG - ASSERT_EQ(gca_get_announces(gca_, nullptr, 1, ann2.chat_public_key, empty_pk), -1); + ASSERT_EQ(gca_get_announces(gca_, nullptr, 1, &ann2.chat_public_key, &empty_pk), -1); #endif } @@ -129,7 +129,7 @@ struct AnnouncesPack : ::testing::Test { announces_.emplace_back(); auto &ann1 = announces_.back(); - ann1.peer_public_key[0] = 0xae; + ann1.peer_public_key.data[0] = 0xae; ann1.ip_port.ip.family = net_family_ipv4(); ann1.ip_port.ip.ip.v4.uint8[0] = 0x7f; // 127.0.0.1 ann1.ip_port.ip.ip.v4.uint8[3] = 0x1; @@ -139,7 +139,7 @@ struct AnnouncesPack : ::testing::Test { announces_.emplace_back(); auto &ann2 = announces_.back(); - ann2.peer_public_key[0] = 0xaf; // different key + ann2.peer_public_key.data[0] = 0xaf; // different key ann2.ip_port.ip.family = net_family_ipv4(); ann2.ip_port.ip.ip.v4.uint8[0] = 0x7f; // 127.0.0.2 ann2.ip_port.ip.ip.v4.uint8[3] = 0x2; @@ -156,7 +156,7 @@ struct AnnouncesPack : ::testing::Test { TEST_F(AnnouncesPack, PublicAnnounceCanBePackedAndUnpacked) { GC_Public_Announce ann{}; - ann.chat_public_key[0] = 0x88; + ann.chat_public_key.data[0] = 0x88; ann.base_announce = announces_[0]; std::vector packed(GCA_PUBLIC_ANNOUNCE_MAX_SIZE); @@ -196,7 +196,7 @@ TEST_F(AnnouncesPack, PublicAnnouncePackNull) std::vector packed(GCA_PUBLIC_ANNOUNCE_MAX_SIZE); EXPECT_EQ(gca_pack_public_announce(logger_, packed.data(), packed.size(), &ann), -1); - ann.chat_public_key[0] = 0x88; + ann.chat_public_key.data[0] = 0x88; ann.base_announce = announces_[0]; std::vector packedTooSmall(GCA_PUBLIC_ANNOUNCE_MAX_SIZE - 1); diff --git a/toxcore/group_chats.c b/toxcore/group_chats.c index 4197191c3b..463f03fd16 100644 --- a/toxcore/group_chats.c +++ b/toxcore/group_chats.c @@ -386,7 +386,7 @@ static bool self_gc_is_founder(const GC_Chat *chat) void gc_get_self_public_key(const GC_Chat *chat, uint8_t *public_key) { if (public_key != nullptr) { - memcpy(public_key, chat->self_public_key.enc, ENC_PUBLIC_KEY_SIZE); + memcpy(public_key, chat->self_public_key.enc.data, ENC_PUBLIC_KEY_SIZE); } } @@ -471,7 +471,7 @@ int pack_gc_saved_peers(const GC_Chat *chat, uint8_t *data, uint16_t length, uin } if (packed_tcp_len > 0 || packed_ipp_len > 0) { - memcpy(data + packed_len, chat->saved_peers[i].public_key, ENC_PUBLIC_KEY_SIZE); + memcpy(data + packed_len, chat->saved_peers[i].public_key.data, ENC_PUBLIC_KEY_SIZE); packed_len += ENC_PUBLIC_KEY_SIZE; ++count; } else { @@ -520,7 +520,7 @@ int unpack_gc_saved_peers(GC_Chat *chat, const uint8_t *data, uint16_t length) } if (tcp_len > 0 || ipp_len > 0) { - memcpy(saved_peer->public_key, data + unpacked_len, ENC_PUBLIC_KEY_SIZE); + memcpy(saved_peer->public_key.data, data + unpacked_len, ENC_PUBLIC_KEY_SIZE); unpacked_len += ENC_PUBLIC_KEY_SIZE; ++count; } else { @@ -566,7 +566,7 @@ static bool validate_password(const GC_Chat *chat, const uint8_t *password, uint * `id` must be at least ENC_PUBLIC_KEY_SIZE bytes in length. */ non_null() -static GC_Chat *get_chat_by_id(const GC_Session *c, const uint8_t *id) +static GC_Chat *get_chat_by_id(const GC_Session *c, const Public_Key *id) { if (c == nullptr) { return nullptr; @@ -579,7 +579,7 @@ static GC_Chat *get_chat_by_id(const GC_Session *c, const uint8_t *id) continue; } - if (memcmp(id, chat->self_public_key.enc, ENC_PUBLIC_KEY_SIZE) == 0) { + if (memcmp(id, &chat->self_public_key.enc, ENC_PUBLIC_KEY_SIZE) == 0) { return chat; } @@ -592,9 +592,9 @@ static GC_Chat *get_chat_by_id(const GC_Session *c, const uint8_t *id) } /** @brief Returns the jenkins hash of a 32 byte public encryption key. */ -uint32_t gc_get_pk_jenkins_hash(const uint8_t *public_key) +uint32_t gc_get_pk_jenkins_hash(const Public_Key *public_key) { - return jenkins_one_at_a_time_hash(public_key, ENC_PUBLIC_KEY_SIZE); + return jenkins_one_at_a_time_hash(public_key->data, ENC_PUBLIC_KEY_SIZE); } /** @brief Sets the sum of the public_key_hash of all confirmed peers. @@ -626,7 +626,7 @@ static uint16_t get_gc_topic_checksum(const GC_TopicInfo *topic_info) return data_checksum(topic_info->topic, topic_info->length); } -int get_peer_number_of_enc_pk(const GC_Chat *chat, const uint8_t *public_enc_key, bool confirmed) +int get_peer_number_of_enc_pk(const GC_Chat *chat, const Public_Key *public_enc_key, bool confirmed) { for (uint32_t i = 0; i < chat->numpeers; ++i) { const GC_Connection *gconn = get_gc_connection(chat, i); @@ -641,7 +641,7 @@ int get_peer_number_of_enc_pk(const GC_Chat *chat, const uint8_t *public_enc_key continue; } - if (memcmp(gconn->addr.public_key.enc, public_enc_key, ENC_PUBLIC_KEY_SIZE) == 0) { + if (memcmp(&gconn->addr.public_key.enc, public_enc_key, ENC_PUBLIC_KEY_SIZE) == 0) { return i; } } @@ -655,14 +655,14 @@ int get_peer_number_of_enc_pk(const GC_Chat *chat, const uint8_t *public_enc_key * Returns -1 if peer is not in the peer list. */ non_null() -static int get_peer_number_of_sig_pk(const GC_Chat *chat, const uint8_t *public_sig_key) +static int get_peer_number_of_sig_pk(const GC_Chat *chat, const Sign_Public_Key *public_sig_key) { for (uint32_t i = 0; i < chat->numpeers; ++i) { const GC_Connection *gconn = get_gc_connection(chat, i); assert(gconn != nullptr); - if (memcmp(get_sig_pk(&gconn->addr.public_key), public_sig_key, SIG_PUBLIC_KEY_SIZE) == 0) { + if (memcmp(&gconn->addr.public_key.sig, public_sig_key, SIG_PUBLIC_KEY_SIZE) == 0) { return i; } } @@ -671,7 +671,7 @@ static int get_peer_number_of_sig_pk(const GC_Chat *chat, const uint8_t *public_ } non_null() -static bool gc_get_enc_pk_from_sig_pk(const GC_Chat *chat, uint8_t *public_key, const uint8_t *public_sig_key) +static bool gc_get_enc_pk_from_sig_pk(const GC_Chat *chat, Public_Key *public_key, const Sign_Public_Key *public_sig_key) { for (uint32_t i = 0; i < chat->numpeers; ++i) { const GC_Connection *gconn = get_gc_connection(chat, i); @@ -680,8 +680,8 @@ static bool gc_get_enc_pk_from_sig_pk(const GC_Chat *chat, uint8_t *public_key, const Extended_Public_Key *full_pk = &gconn->addr.public_key; - if (memcmp(public_sig_key, get_sig_pk(full_pk), SIG_PUBLIC_KEY_SIZE) == 0) { - memcpy(public_key, get_enc_key(full_pk), ENC_PUBLIC_KEY_SIZE); + if (memcmp(public_sig_key, &full_pk->sig, SIG_PUBLIC_KEY_SIZE) == 0) { + *public_key = full_pk->enc; return true; } } @@ -793,8 +793,8 @@ static bool expand_chat_id(Extended_Public_Key *dest, const uint8_t *chat_id) { assert(dest != nullptr); - const int ret = crypto_sign_ed25519_pk_to_curve25519(dest->enc, chat_id); - memcpy(dest->sig, chat_id, SIG_PUBLIC_KEY_SIZE); + const int ret = crypto_sign_ed25519_pk_to_curve25519(dest->enc.data, chat_id); + memcpy(dest->sig.data, chat_id, SIG_PUBLIC_KEY_SIZE); return ret != -1; } @@ -810,7 +810,7 @@ static void copy_gc_saved_peer(const Random *rng, const GC_Connection *gconn, GC } addr->ip_port = gconn->addr.ip_port; - memcpy(addr->public_key, gconn->addr.public_key.enc, ENC_PUBLIC_KEY_SIZE); + addr->public_key = gconn->addr.public_key.enc; } /** Return true if `saved_peer` has either a valid IP_Port or a valid TCP relay. */ @@ -824,12 +824,12 @@ static bool saved_peer_is_valid(const GC_SavedPeerInfo *saved_peer) * Returns -1 if key is not found. */ non_null() -static int saved_peer_index(const GC_Chat *chat, const uint8_t *public_key) +static int saved_peer_index(const GC_Chat *chat, const Public_Key *public_key) { for (uint16_t i = 0; i < GC_MAX_SAVED_PEERS; ++i) { const GC_SavedPeerInfo *saved_peer = &chat->saved_peers[i]; - if (memcmp(saved_peer->public_key, public_key, ENC_PUBLIC_KEY_SIZE) == 0) { + if (memcmp(&saved_peer->public_key, public_key, ENC_PUBLIC_KEY_SIZE) == 0) { return i; } } @@ -849,7 +849,7 @@ static int saved_peer_index(const GC_Chat *chat, const uint8_t *public_key) * Returns -1 if there are no vacant indices. */ non_null(1) nullable(2) -static int saved_peers_get_new_index(const GC_Chat *chat, const uint8_t *public_key) +static int saved_peers_get_new_index(const GC_Chat *chat, const Public_Key *public_key) { if (public_key != nullptr) { const int idx = saved_peer_index(chat, public_key); @@ -872,7 +872,7 @@ static int saved_peers_get_new_index(const GC_Chat *chat, const uint8_t *public_ for (uint16_t i = 0; i < GC_MAX_SAVED_PEERS; ++i) { const GC_SavedPeerInfo *saved_peer = &chat->saved_peers[i]; - const int peernumber = get_peer_number_of_enc_pk(chat, saved_peer->public_key, true); + const int peernumber = get_peer_number_of_enc_pk(chat, &saved_peer->public_key, true); if (peernumber < 0) { return i; @@ -894,7 +894,7 @@ static int saved_peers_get_new_index(const GC_Chat *chat, const uint8_t *public_ non_null() static void add_gc_saved_peers(GC_Chat *chat, const GC_Connection *gconn) { - const int idx = saved_peers_get_new_index(chat, gconn->addr.public_key.enc); + const int idx = saved_peers_get_new_index(chat, &gconn->addr.public_key.enc); if (idx == -1) { return; @@ -929,7 +929,7 @@ static void refresh_gc_saved_peers(GC_Chat *chat) continue; } - if (saved_peer_index(chat, gconn->addr.public_key.enc) == -1) { + if (saved_peer_index(chat, &gconn->addr.public_key.enc) == -1) { GC_SavedPeerInfo *saved_peer = &chat->saved_peers[idx]; copy_gc_saved_peer(chat->rng, gconn, saved_peer); return; @@ -959,8 +959,8 @@ static uint16_t get_gc_confirmed_numpeers(const GC_Chat *chat) non_null() static bool sign_gc_shared_state(GC_Chat *chat); non_null() static bool broadcast_gc_mod_list(const GC_Chat *chat); non_null() static bool broadcast_gc_shared_state(const GC_Chat *chat); -non_null() static bool update_gc_sanctions_list(GC_Chat *chat, const uint8_t *public_sig_key); -non_null() static bool update_gc_topic(GC_Chat *chat, const uint8_t *public_sig_key); +non_null() static bool update_gc_sanctions_list(GC_Chat *chat, const Sign_Public_Key *public_sig_key); +non_null() static bool update_gc_topic(GC_Chat *chat, const Sign_Public_Key *public_sig_key); non_null() static bool send_gc_set_observer(const GC_Chat *chat, const Extended_Public_Key *target_ext_pk, const uint8_t *sanction_data, uint16_t length, bool add_obs); @@ -974,7 +974,7 @@ static bool peer_is_observer(const GC_Chat *chat, uint32_t peer_number) return false; } - return sanctions_list_is_observer(&chat->moderation, get_enc_key(&gconn->addr.public_key)); + return sanctions_list_is_observer(&chat->moderation, &gconn->addr.public_key.enc); } /** Returns true if peer designated by `peer_number` is the group founder. */ @@ -988,7 +988,7 @@ static bool peer_is_founder(const GC_Chat *chat, uint32_t peer_number) return false; } - return memcmp(chat->shared_state.founder_public_key.enc, gconn->addr.public_key.enc, ENC_PUBLIC_KEY_SIZE) == 0; + return memcmp(&chat->shared_state.founder_public_key.enc, &gconn->addr.public_key.enc, ENC_PUBLIC_KEY_SIZE) == 0; } /** Returns true if peer designated by `peer_number` is in the moderator list or is the founder. */ @@ -1005,7 +1005,7 @@ static bool peer_is_moderator(const GC_Chat *chat, uint32_t peer_number) return false; } - return mod_list_verify_sig_pk(&chat->moderation, get_sig_pk(&gconn->addr.public_key)); + return mod_list_verify_sig_pk(&chat->moderation, &gconn->addr.public_key.sig); } /** @brief Iterates through the peerlist and updates group roles according to the @@ -1034,7 +1034,7 @@ static void update_gc_peer_roles(GC_Chat *chat) continue; } - const uint8_t first_byte = gconn->addr.public_key.enc[0]; + const uint8_t first_byte = gconn->addr.public_key.enc.data[0]; const bool is_founder = peer_is_founder(chat, i); if (is_founder) { @@ -1091,12 +1091,12 @@ static bool prune_gc_mod_list(GC_Chat *chat) return true; } - uint8_t public_sig_key[SIG_PUBLIC_KEY_SIZE]; + Sign_Public_Key public_sig_key; bool pruned_mod = false; for (uint16_t i = 0; i < chat->moderation.num_mods; ++i) { - if (get_peer_number_of_sig_pk(chat, chat->moderation.mod_list[i]) == -1) { - memcpy(public_sig_key, chat->moderation.mod_list[i], SIG_PUBLIC_KEY_SIZE); + if (get_peer_number_of_sig_pk(chat, &chat->moderation.mod_list[i]) == -1) { + public_sig_key = chat->moderation.mod_list[i]; if (!mod_list_remove_index(&chat->moderation, i)) { continue; @@ -1112,8 +1112,8 @@ static bool prune_gc_mod_list(GC_Chat *chat) && sign_gc_shared_state(chat) && broadcast_gc_shared_state(chat) && broadcast_gc_mod_list(chat) - && update_gc_sanctions_list(chat, public_sig_key) - && update_gc_topic(chat, public_sig_key); + && update_gc_sanctions_list(chat, &public_sig_key) + && update_gc_topic(chat, &public_sig_key); } non_null() @@ -1121,7 +1121,7 @@ static bool prune_gc_sanctions_list_inner( GC_Chat *chat, const Mod_Sanction *sanction, const Extended_Public_Key *target_ext_pk) { - if (!sanctions_list_remove_observer(&chat->moderation, sanction->target_public_enc_key, nullptr)) { + if (!sanctions_list_remove_observer(&chat->moderation, &sanction->target_public_enc_key, nullptr)) { LOGGER_WARNING(chat->log, "Failed to remove entry from observer list"); return false; } @@ -1155,13 +1155,14 @@ static bool prune_gc_sanctions_list(GC_Chat *chat) } for (uint16_t i = 0; i < chat->moderation.num_sanctions; ++i) { - const int peer_number = get_peer_number_of_enc_pk(chat, chat->moderation.sanctions[i].target_public_enc_key, true); + const int peer_number = get_peer_number_of_enc_pk(chat, &chat->moderation.sanctions[i].target_public_enc_key, true); if (peer_number == -1) { const Mod_Sanction *sanction = &chat->moderation.sanctions[i]; - Extended_Public_Key target_ext_pk; - memcpy(target_ext_pk.enc, sanction->target_public_enc_key, ENC_PUBLIC_KEY_SIZE); - memcpy(target_ext_pk.sig, sanction->setter_public_sig_key, SIG_PUBLIC_KEY_SIZE); + const Extended_Public_Key target_ext_pk = { + sanction->target_public_enc_key, + sanction->setter_public_sig_key, + }; return prune_gc_sanctions_list_inner(chat, sanction, &target_ext_pk); } } @@ -1245,9 +1246,9 @@ static uint16_t pack_gc_shared_state(uint8_t *data, uint16_t length, const GC_Sh net_pack_u32(data + packed_len, shared_state->version); packed_len += sizeof(uint32_t); - memcpy(data + packed_len, shared_state->founder_public_key.enc, ENC_PUBLIC_KEY_SIZE); + memcpy(data + packed_len, shared_state->founder_public_key.enc.data, ENC_PUBLIC_KEY_SIZE); packed_len += ENC_PUBLIC_KEY_SIZE; - memcpy(data + packed_len, shared_state->founder_public_key.sig, SIG_PUBLIC_KEY_SIZE); + memcpy(data + packed_len, shared_state->founder_public_key.sig.data, SIG_PUBLIC_KEY_SIZE); packed_len += SIG_PUBLIC_KEY_SIZE; net_pack_u16(data + packed_len, shared_state->maxpeers); packed_len += sizeof(uint16_t); @@ -1290,9 +1291,9 @@ static uint16_t unpack_gc_shared_state(GC_SharedState *shared_state, const uint8 net_unpack_u32(data + len_processed, &shared_state->version); len_processed += sizeof(uint32_t); - memcpy(shared_state->founder_public_key.enc, data + len_processed, ENC_PUBLIC_KEY_SIZE); + memcpy(shared_state->founder_public_key.enc.data, data + len_processed, ENC_PUBLIC_KEY_SIZE); len_processed += ENC_PUBLIC_KEY_SIZE; - memcpy(shared_state->founder_public_key.sig, data + len_processed, SIG_PUBLIC_KEY_SIZE); + memcpy(shared_state->founder_public_key.sig.data, data + len_processed, SIG_PUBLIC_KEY_SIZE); len_processed += SIG_PUBLIC_KEY_SIZE; net_unpack_u16(data + len_processed, &shared_state->maxpeers); len_processed += sizeof(uint16_t); @@ -1348,7 +1349,7 @@ static uint16_t pack_gc_topic_info(uint8_t *data, uint16_t length, const GC_Topi packed_len += sizeof(uint16_t); memcpy(data + packed_len, topic_info->topic, topic_info->length); packed_len += topic_info->length; - memcpy(data + packed_len, topic_info->public_sig_key, SIG_PUBLIC_KEY_SIZE); + memcpy(data + packed_len, topic_info->public_sig_key.data, SIG_PUBLIC_KEY_SIZE); packed_len += SIG_PUBLIC_KEY_SIZE; return packed_len; @@ -1388,7 +1389,7 @@ static int unpack_gc_topic_info(GC_TopicInfo *topic_info, const uint8_t *data, u len_processed += topic_info->length; } - memcpy(topic_info->public_sig_key, data + len_processed, SIG_PUBLIC_KEY_SIZE); + memcpy(topic_info->public_sig_key.data, data + len_processed, SIG_PUBLIC_KEY_SIZE); len_processed += SIG_PUBLIC_KEY_SIZE; return len_processed; @@ -1533,7 +1534,7 @@ static int group_packet_unwrap(const Logger *log, const GC_Connection *gconn, ui } int group_packet_wrap( - const Logger *log, const Random *rng, const uint8_t *self_pk, const uint8_t *shared_key, uint8_t *packet, + const Logger *log, const Random *rng, const Public_Key *self_pk, const uint8_t *shared_key, uint8_t *packet, uint16_t packet_size, const uint8_t *data, uint16_t length, uint64_t message_id, uint8_t gp_packet_type, Net_Packet_Type net_packet_type) { @@ -1599,7 +1600,7 @@ int group_packet_wrap( } packet[0] = net_packet_type; - memcpy(packet + 1, self_pk, ENC_PUBLIC_KEY_SIZE); + memcpy(packet + 1, self_pk->data, ENC_PUBLIC_KEY_SIZE); memcpy(packet + 1 + ENC_PUBLIC_KEY_SIZE, nonce, CRYPTO_NONCE_SIZE); memcpy(packet + 1 + ENC_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE, encrypt, enc_len); @@ -1634,7 +1635,7 @@ static bool send_lossy_group_packet(const GC_Chat *chat, const GC_Connection *gc } const int len = group_packet_wrap( - chat->log, chat->rng, chat->self_public_key.enc, gconn->session_shared_key, packet, + chat->log, chat->rng, &chat->self_public_key.enc, gconn->session_shared_key, packet, packet_size, data, length, 0, packet_type, NET_PACKET_GC_LOSSY); if (len < 0) { @@ -1734,7 +1735,7 @@ static bool unpack_gc_sync_announce(GC_Chat *chat, const uint8_t *data, const ui return false; } - if (memcmp(announce.peer_public_key, chat->self_public_key.enc, ENC_PUBLIC_KEY_SIZE) == 0) { + if (memcmp(&announce.peer_public_key, &chat->self_public_key.enc, ENC_PUBLIC_KEY_SIZE) == 0) { LOGGER_WARNING(chat->log, "Attempted to unpack our own announce"); return true; } @@ -1745,7 +1746,7 @@ static bool unpack_gc_sync_announce(GC_Chat *chat, const uint8_t *data, const ui } const IP_Port *ip_port = announce.ip_port_is_set ? &announce.ip_port : nullptr; - const int new_peer_number = peer_add(chat, ip_port, announce.peer_public_key); + const int new_peer_number = peer_add(chat, ip_port, &announce.peer_public_key); if (new_peer_number == -1) { LOGGER_ERROR(chat->log, "peer_add() failed"); @@ -1838,7 +1839,7 @@ static int handle_gc_sync_response(const GC_Session *c, GC_Chat *chat, uint32_t return 0; } -non_null() static int get_gc_peer_public_key(const GC_Chat *chat, uint32_t peer_number, uint8_t *public_key); +non_null() static int get_gc_peer_public_key(const GC_Chat *chat, uint32_t peer_number, Public_Key *public_key); non_null() static bool send_peer_shared_state(const GC_Chat *chat, GC_Connection *gconn); non_null() static bool send_peer_mod_list(const GC_Chat *chat, GC_Connection *gconn); non_null() static bool send_peer_sanctions_list(const GC_Chat *chat, GC_Connection *gconn); @@ -1863,7 +1864,7 @@ static bool create_sync_announce(const GC_Chat *chat, const GC_Connection *gconn } } - get_gc_peer_public_key(chat, peer_number, announce->peer_public_key); + get_gc_peer_public_key(chat, peer_number, &announce->peer_public_key); if (gcc_ip_port_is_set(gconn)) { announce->ip_port = gconn->addr.ip_port; @@ -2872,7 +2873,7 @@ static void do_privacy_state_change(const GC_Session *c, GC_Chat *chat, void *us } } else { kill_group_friend_connection(c, chat); - cleanup_gca(c->announces_list, get_chat_id(&chat->chat_public_key)); + cleanup_gca(c->announces_list, &chat->chat_public_key.enc); chat->join_type = HJ_PRIVATE; } @@ -3024,8 +3025,7 @@ static int handle_gc_shared_state(const GC_Session *c, GC_Chat *chat, GC_Connect } if (chat->shared_state.version == 0) { // init founder public sig key in moderation object - memcpy(chat->moderation.founder_public_sig_key, - get_sig_pk(&new_shared_state.founder_public_key), SIG_PUBLIC_KEY_SIZE); + chat->moderation.founder_public_sig_key = new_shared_state.founder_public_key.sig; } chat->shared_state = new_shared_state; @@ -3386,7 +3386,7 @@ static bool broadcast_gc_sanctions_list(const GC_Chat *chat) * Returns true on success. */ non_null() -static bool update_gc_sanctions_list(GC_Chat *chat, const uint8_t *public_sig_key) +static bool update_gc_sanctions_list(GC_Chat *chat, const Sign_Public_Key *public_sig_key) { const uint16_t num_replaced = sanctions_list_replace_sig(&chat->moderation, public_sig_key); @@ -3549,7 +3549,7 @@ static int handle_gc_nick(const GC_Session *c, GC_Chat *chat, GC_Peer *peer, con * Returns -2 if `public_key` is null. */ non_null() -static int get_gc_peer_public_key(const GC_Chat *chat, uint32_t peer_number, uint8_t *public_key) +static int get_gc_peer_public_key(const GC_Chat *chat, uint32_t peer_number, Public_Key *public_key) { const GC_Connection *gconn = get_gc_connection(chat, peer_number); @@ -3561,7 +3561,7 @@ static int get_gc_peer_public_key(const GC_Chat *chat, uint32_t peer_number, uin return -2; } - memcpy(public_key, gconn->addr.public_key.enc, ENC_PUBLIC_KEY_SIZE); + *public_key = gconn->addr.public_key.enc; return 0; } @@ -3580,7 +3580,7 @@ int gc_get_peer_public_key_by_peer_id(const GC_Chat *chat, GC_Peer_Id peer_id, u return -2; } - memcpy(public_key, gconn->addr.public_key.enc, ENC_PUBLIC_KEY_SIZE); + memcpy(public_key, gconn->addr.public_key.enc.data, ENC_PUBLIC_KEY_SIZE); return 0; } @@ -3827,7 +3827,7 @@ int gc_set_topic(GC_Chat *chat, const uint8_t *topic, uint16_t length) memzero(chat->topic_info.topic, sizeof(chat->topic_info.topic)); } - memcpy(chat->topic_info.public_sig_key, get_sig_pk(&chat->self_public_key), SIG_PUBLIC_KEY_SIZE); + chat->topic_info.public_sig_key = chat->self_public_key.sig; chat->topic_info.checksum = get_gc_topic_checksum(&chat->topic_info); @@ -3888,9 +3888,9 @@ uint16_t gc_get_topic_size(const GC_Chat *chat) * Returns true on success */ non_null() -static bool update_gc_topic(GC_Chat *chat, const uint8_t *public_sig_key) +static bool update_gc_topic(GC_Chat *chat, const Sign_Public_Key *public_sig_key) { - if (memcmp(public_sig_key, chat->topic_info.public_sig_key, SIG_PUBLIC_KEY_SIZE) != 0) { + if (memcmp(public_sig_key, &chat->topic_info.public_sig_key, SIG_PUBLIC_KEY_SIZE) != 0) { return true; } @@ -3912,7 +3912,7 @@ static bool handle_gc_topic_validate(const GC_Chat *chat, const GC_Peer *peer, c } if (topic_lock_enabled) { - if (!mod_list_verify_sig_pk(&chat->moderation, topic_info->public_sig_key)) { + if (!mod_list_verify_sig_pk(&chat->moderation, &topic_info->public_sig_key)) { LOGGER_DEBUG(chat->log, "Invalid topic signature (bad credentials)"); return false; } @@ -3921,10 +3921,10 @@ static bool handle_gc_topic_validate(const GC_Chat *chat, const GC_Peer *peer, c return false; } } else { - uint8_t public_enc_key[ENC_PUBLIC_KEY_SIZE]; + Public_Key public_enc_key; - if (gc_get_enc_pk_from_sig_pk(chat, public_enc_key, topic_info->public_sig_key)) { - if (sanctions_list_is_observer(&chat->moderation, public_enc_key)) { + if (gc_get_enc_pk_from_sig_pk(chat, &public_enc_key, &topic_info->public_sig_key)) { + if (sanctions_list_is_observer(&chat->moderation, &public_enc_key)) { LOGGER_DEBUG(chat->log, "Invalid topic signature (sanctioned peer attempted to change topic)"); return false; } @@ -3982,7 +3982,7 @@ static int handle_gc_topic(const GC_Session *c, GC_Chat *chat, const GC_Peer *pe const uint8_t *signature = data; if (crypto_sign_verify_detached(signature, data + SIGNATURE_SIZE, length - SIGNATURE_SIZE, - topic_info.public_sig_key) == -1) { + topic_info.public_sig_key.data) == -1) { LOGGER_WARNING(chat->log, "failed to verify topic signature"); return 0; } @@ -4003,7 +4003,7 @@ static int handle_gc_topic(const GC_Session *c, GC_Chat *chat, const GC_Peer *pe memcpy(chat->topic_sig, signature, SIGNATURE_SIZE); if (!skip_callback && chat->connection_state == CS_CONNECTED && c->topic_change != nullptr) { - const int setter_peer_number = get_peer_number_of_sig_pk(chat, topic_info.public_sig_key); + const int setter_peer_number = get_peer_number_of_sig_pk(chat, &topic_info.public_sig_key); const GC_Peer_Id peer_id = setter_peer_number >= 0 ? chat->group[setter_peer_number].peer_id : gc_unknown_peer_id(); c->topic_change(c->messenger, chat->group_number, peer_id, topic_info.topic, topic_info.length, userdata); @@ -4155,15 +4155,15 @@ static int validate_unpack_gc_set_mod(GC_Chat *chat, uint32_t peer_number, const bool add_mod) { int target_peer_number; - uint8_t mod_data[MOD_LIST_ENTRY_SIZE]; + Sign_Public_Key mod_data; if (add_mod) { if (length < 1 + MOD_LIST_ENTRY_SIZE) { return -1; } - memcpy(mod_data, data + 1, MOD_MODERATION_HASH_SIZE); - target_peer_number = get_peer_number_of_sig_pk(chat, mod_data); + memcpy(mod_data.data, data + 1, MOD_MODERATION_HASH_SIZE); + target_peer_number = get_peer_number_of_sig_pk(chat, &mod_data); if (!gc_peer_number_is_valid(chat, target_peer_number)) { return -2; @@ -4175,12 +4175,12 @@ static int validate_unpack_gc_set_mod(GC_Chat *chat, uint32_t peer_number, const return -3; } - if (!mod_list_add_entry(&chat->moderation, mod_data)) { + if (!mod_list_add_entry(&chat->moderation, &mod_data)) { return -4; } } else { - memcpy(mod_data, data + 1, SIG_PUBLIC_KEY_SIZE); - target_peer_number = get_peer_number_of_sig_pk(chat, mod_data); + memcpy(mod_data.data, data + 1, SIG_PUBLIC_KEY_SIZE); + target_peer_number = get_peer_number_of_sig_pk(chat, &mod_data); if (!gc_peer_number_is_valid(chat, target_peer_number)) { return -2; @@ -4192,7 +4192,7 @@ static int validate_unpack_gc_set_mod(GC_Chat *chat, uint32_t peer_number, const return -3; } - if (!mod_list_remove_entry(&chat->moderation, mod_data)) { + if (!mod_list_remove_entry(&chat->moderation, &mod_data)) { return -4; } } @@ -4266,7 +4266,7 @@ static bool send_gc_set_mod(const GC_Chat *chat, const GC_Connection *gconn, boo net_pack_bool(&data[0], add_mod); - memcpy(data + 1, get_sig_pk(&gconn->addr.public_key), SIG_PUBLIC_KEY_SIZE); + memcpy(data + 1, gconn->addr.public_key.sig.data, SIG_PUBLIC_KEY_SIZE); if (!send_gc_broadcast_message(chat, data, length, GM_SET_MOD)) { free(data); @@ -4298,16 +4298,16 @@ static bool founder_gc_set_moderator(GC_Chat *chat, const GC_Connection *gconn, } } - if (!mod_list_add_entry(&chat->moderation, get_sig_pk(&gconn->addr.public_key))) { + if (!mod_list_add_entry(&chat->moderation, &gconn->addr.public_key.sig)) { return false; } } else { - if (!mod_list_remove_entry(&chat->moderation, get_sig_pk(&gconn->addr.public_key))) { + if (!mod_list_remove_entry(&chat->moderation, &gconn->addr.public_key.sig)) { return false; } - if (!update_gc_sanctions_list(chat, get_sig_pk(&gconn->addr.public_key)) - || !update_gc_topic(chat, get_sig_pk(&gconn->addr.public_key))) { + if (!update_gc_sanctions_list(chat, &gconn->addr.public_key.sig) + || !update_gc_topic(chat, &gconn->addr.public_key.sig)) { return false; } } @@ -4338,7 +4338,7 @@ static bool founder_gc_set_moderator(GC_Chat *chat, const GC_Connection *gconn, */ non_null() static int validate_unpack_observer_entry(GC_Chat *chat, const uint8_t *data, uint16_t length, - const uint8_t *public_key, bool add_obs) + const Public_Key *public_key, bool add_obs) { Mod_Sanction_Creds creds; @@ -4411,9 +4411,10 @@ static int handle_gc_set_observer(const GC_Session *c, GC_Chat *chat, uint32_t p bool add_obs; net_unpack_bool(&data[0], &add_obs); - const uint8_t *public_key = data + 1; + Public_Key public_key; + memcpy(public_key.data, data + 1, ENC_PUBLIC_KEY_SIZE); - const int target_peer_number = get_peer_number_of_enc_pk(chat, public_key, false); + const int target_peer_number = get_peer_number_of_enc_pk(chat, &public_key, false); if (target_peer_number >= 0 && (uint32_t)target_peer_number == peer_number) { return -2; @@ -4430,7 +4431,7 @@ static int handle_gc_set_observer(const GC_Session *c, GC_Chat *chat, uint32_t p const int ret = validate_unpack_observer_entry(chat, data + 1 + EXT_PUBLIC_KEY_SIZE, length - 1 - EXT_PUBLIC_KEY_SIZE, - public_key, add_obs); + &public_key, add_obs); if (ret == -1) { return -2; @@ -4469,8 +4470,8 @@ static bool send_gc_set_observer(const GC_Chat *chat, const Extended_Public_Key net_pack_bool(&packet[0], add_obs); - memcpy(packet + 1, target_ext_pk->enc, ENC_PUBLIC_KEY_SIZE); - memcpy(packet + 1 + ENC_PUBLIC_KEY_SIZE, target_ext_pk->sig, SIG_PUBLIC_KEY_SIZE); + memcpy(packet + 1, target_ext_pk->enc.data, ENC_PUBLIC_KEY_SIZE); + memcpy(packet + 1 + ENC_PUBLIC_KEY_SIZE, target_ext_pk->sig.data, SIG_PUBLIC_KEY_SIZE); memcpy(packet + 1 + ENC_PUBLIC_KEY_SIZE + SIG_PUBLIC_KEY_SIZE, sanction_data, length); if (!send_gc_broadcast_message(chat, packet, packet_len, GM_SET_OBSERVER)) { @@ -4513,7 +4514,7 @@ static bool mod_gc_set_observer(GC_Chat *chat, uint32_t peer_number, bool add_ob // if sanctioned peer set the topic we need to overwrite his signature and redistribute // topic info - const int setter_peer_number = get_peer_number_of_sig_pk(chat, chat->topic_info.public_sig_key); + const int setter_peer_number = get_peer_number_of_sig_pk(chat, &chat->topic_info.public_sig_key); if (setter_peer_number >= 0 && (uint32_t)setter_peer_number == peer_number) { if (gc_set_topic(chat, chat->topic_info.topic, chat->topic_info.length) != 0) { @@ -4523,7 +4524,7 @@ static bool mod_gc_set_observer(GC_Chat *chat, uint32_t peer_number, bool add_ob Mod_Sanction sanction; - if (!sanctions_list_make_entry(&chat->moderation, gconn->addr.public_key.enc, &sanction, SA_OBSERVER)) { + if (!sanctions_list_make_entry(&chat->moderation, &gconn->addr.public_key.enc, &sanction, SA_OBSERVER)) { LOGGER_WARNING(chat->log, "sanctions_list_make_entry failed in mod_gc_set_observer"); return false; } @@ -4537,7 +4538,7 @@ static bool mod_gc_set_observer(GC_Chat *chat, uint32_t peer_number, bool add_ob length += packed_len; } else { - if (!sanctions_list_remove_observer(&chat->moderation, gconn->addr.public_key.enc, nullptr)) { + if (!sanctions_list_remove_observer(&chat->moderation, &gconn->addr.public_key.enc, nullptr)) { LOGGER_WARNING(chat->log, "failed to remove sanction"); return false; } @@ -4821,7 +4822,7 @@ int gc_founder_set_privacy_state(const Messenger *m, int group_number, Group_Pri } if (new_privacy_state == GI_PRIVATE) { - cleanup_gca(c->announces_list, get_chat_id(&chat->chat_public_key)); + cleanup_gca(c->announces_list, &chat->chat_public_key.enc); kill_group_friend_connection(c, chat); chat->join_type = HJ_PRIVATE; } else { @@ -5180,9 +5181,10 @@ static int handle_gc_kick_peer(const GC_Session *c, GC_Chat *chat, const GC_Peer return 0; } - const uint8_t *target_pk = data; + Public_Key target_pk; + memcpy(target_pk.data, data, ENC_PUBLIC_KEY_SIZE); - const int target_peer_number = get_peer_number_of_enc_pk(chat, target_pk, false); + const int target_peer_number = get_peer_number_of_enc_pk(chat, &target_pk, false); GC_Peer *target_peer = get_gc_peer(chat, target_peer_number); if (target_peer != nullptr) { @@ -5232,7 +5234,7 @@ non_null() static bool send_gc_kick_peer(const GC_Chat *chat, const GC_Connection *gconn) { uint8_t packet[ENC_PUBLIC_KEY_SIZE]; - memcpy(packet, gconn->addr.public_key.enc, ENC_PUBLIC_KEY_SIZE); + memcpy(packet, gconn->addr.public_key.enc.data, ENC_PUBLIC_KEY_SIZE); return send_gc_broadcast_message(chat, packet, ENC_PUBLIC_KEY_SIZE, GM_KICK_PEER); } @@ -5509,7 +5511,7 @@ static int handle_gc_broadcast(const GC_Session *c, GC_Chat *chat, uint32_t peer * Return -2 if decryption fails. */ non_null() -static int unwrap_group_handshake_packet(const Logger *log, const uint8_t *self_sk, const uint8_t *sender_pk, +static int unwrap_group_handshake_packet(const Logger *log, const Secret_Key *self_sk, const Public_Key *sender_pk, uint8_t *plain, size_t plain_size, const uint8_t *packet, uint16_t length) { if (length <= CRYPTO_NONCE_SIZE) { @@ -5517,7 +5519,7 @@ static int unwrap_group_handshake_packet(const Logger *log, const uint8_t *self_ return -1; } - const int plain_len = decrypt_data(sender_pk, self_sk, packet, packet + CRYPTO_NONCE_SIZE, + const int plain_len = decrypt_data(sender_pk->data, self_sk->data, packet, packet + CRYPTO_NONCE_SIZE, length - CRYPTO_NONCE_SIZE, plain); if (plain_len < 0 || (uint32_t)plain_len != plain_size) { @@ -5540,8 +5542,8 @@ static int unwrap_group_handshake_packet(const Logger *log, const uint8_t *self_ */ non_null() static int wrap_group_handshake_packet( - const Logger *log, const Random *rng, const uint8_t *self_pk, const uint8_t *self_sk, - const uint8_t *target_pk, uint8_t *packet, uint32_t packet_size, + const Logger *log, const Random *rng, const Public_Key *self_pk, const Secret_Key *self_sk, + const Public_Key *target_pk, uint8_t *packet, uint32_t packet_size, const uint8_t *data, uint16_t length) { if (packet_size != GC_MIN_ENCRYPTED_HS_PAYLOAD_SIZE + sizeof(Node_format)) { @@ -5559,7 +5561,7 @@ static int wrap_group_handshake_packet( return -2; } - const int enc_len = encrypt_data(target_pk, self_sk, nonce, data, length, encrypt); + const int enc_len = encrypt_data(target_pk->data, self_sk->data, nonce, data, length, encrypt); if (enc_len < 0 || (size_t)enc_len != encrypt_buf_size) { LOGGER_ERROR(log, "Failed to encrypt group handshake packet (len: %d)", enc_len); @@ -5568,8 +5570,8 @@ static int wrap_group_handshake_packet( } packet[0] = NET_PACKET_GC_HANDSHAKE; - memcpy(packet + 1, self_pk, ENC_PUBLIC_KEY_SIZE); - memcpy(packet + 1 + ENC_PUBLIC_KEY_SIZE, target_pk, ENC_PUBLIC_KEY_SIZE); + memcpy(packet + 1, self_pk->data, ENC_PUBLIC_KEY_SIZE); + memcpy(packet + 1 + ENC_PUBLIC_KEY_SIZE, target_pk->data, ENC_PUBLIC_KEY_SIZE); memcpy(packet + 1 + ENC_PUBLIC_KEY_SIZE + ENC_PUBLIC_KEY_SIZE, nonce, CRYPTO_NONCE_SIZE); memcpy(packet + 1 + ENC_PUBLIC_KEY_SIZE + ENC_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE, encrypt, enc_len); @@ -5607,7 +5609,7 @@ static int make_gc_handshake_packet(const GC_Chat *chat, const GC_Connection *gc data[0] = handshake_type; memcpy(data + length, gconn->session_public_key, ENC_PUBLIC_KEY_SIZE); length += ENC_PUBLIC_KEY_SIZE; - memcpy(data + length, get_sig_pk(&chat->self_public_key), SIG_PUBLIC_KEY_SIZE); + memcpy(data + length, chat->self_public_key.sig.data, SIG_PUBLIC_KEY_SIZE); length += SIG_PUBLIC_KEY_SIZE; memcpy(data + length, &request_type, sizeof(uint8_t)); length += sizeof(uint8_t); @@ -5623,8 +5625,8 @@ static int make_gc_handshake_packet(const GC_Chat *chat, const GC_Connection *gc } const int enc_len = wrap_group_handshake_packet( - chat->log, chat->rng, chat->self_public_key.enc, chat->self_secret_key.enc, - gconn->addr.public_key.enc, packet, (uint16_t)packet_size, data, length); + chat->log, chat->rng, &chat->self_public_key.enc, &chat->self_secret_key.enc, + &gconn->addr.public_key.enc, packet, (uint16_t)packet_size, data, length); if (enc_len != GC_MIN_ENCRYPTED_HS_PAYLOAD_SIZE + nodes_size) { LOGGER_WARNING(chat->log, "Failed to wrap handshake packet: %d", enc_len); @@ -5719,7 +5721,7 @@ static bool send_gc_oob_handshake_request(const GC_Chat *chat, const GC_Connecti return false; } - return tcp_send_oob_packet_using_relay(chat->tcp_conn, gconn->oob_relay_pk, gconn->addr.public_key.enc, + return tcp_send_oob_packet_using_relay(chat->tcp_conn, gconn->oob_relay_pk, gconn->addr.public_key.enc.data, packet, (uint16_t)length) == 0; } @@ -5731,7 +5733,7 @@ static bool send_gc_oob_handshake_request(const GC_Chat *chat, const GC_Connecti * Returns -1 on failure. */ non_null() -static int handle_gc_handshake_response(const GC_Chat *chat, const uint8_t *sender_pk, const uint8_t *data, +static int handle_gc_handshake_response(const GC_Chat *chat, const Public_Key *sender_pk, const uint8_t *data, uint16_t length) { // this should be checked at lower level; this is a redundant defense check. Ideally we should @@ -5813,7 +5815,7 @@ static bool send_gc_handshake_response(const GC_Chat *chat, GC_Connection *gconn */ #define GC_NEW_PEER_CONNECTION_LIMIT 10 non_null(1, 3, 4) nullable(2) -static int handle_gc_handshake_request(GC_Chat *chat, const IP_Port *ipp, const uint8_t *sender_pk, +static int handle_gc_handshake_request(GC_Chat *chat, const IP_Port *ipp, const Public_Key *sender_pk, const uint8_t *data, uint16_t length) { // this should be checked at lower level; this is a redundant defense check. Ideally we should @@ -5938,7 +5940,7 @@ static int handle_gc_handshake_request(GC_Chat *chat, const IP_Port *ipp, const * Returns -1 on failure. */ non_null(1, 2, 4) nullable(3, 7) -static int handle_gc_handshake_packet(GC_Chat *chat, const uint8_t *sender_pk, const IP_Port *ipp, +static int handle_gc_handshake_packet(GC_Chat *chat, const Public_Key *sender_pk, const IP_Port *ipp, const uint8_t *packet, uint16_t length, bool direct_conn, void *userdata) { if (length < GC_MIN_HS_PACKET_PAYLOAD_SIZE + CRYPTO_MAC_SIZE + CRYPTO_NONCE_SIZE) { @@ -5952,7 +5954,7 @@ static int handle_gc_handshake_packet(GC_Chat *chat, const uint8_t *sender_pk, c return -1; } - const int plain_len = unwrap_group_handshake_packet(chat->log, chat->self_secret_key.enc, sender_pk, data, + const int plain_len = unwrap_group_handshake_packet(chat->log, &chat->self_secret_key.enc, sender_pk, data, data_buf_size, packet, length); if (plain_len < GC_MIN_HS_PACKET_PAYLOAD_SIZE) { @@ -6153,7 +6155,7 @@ static bool handle_gc_packet_fragment(const GC_Session *c, GC_Chat *chat, uint32 * Returns true if packet is successfully handled. */ non_null(1, 2, 3, 4) nullable(7) -static bool handle_gc_lossless_packet(const GC_Session *c, GC_Chat *chat, const uint8_t *sender_pk, +static bool handle_gc_lossless_packet(const GC_Session *c, GC_Chat *chat, const Public_Key *sender_pk, const uint8_t *packet, uint16_t length, bool direct_conn, void *userdata) { if (length < GC_MIN_LOSSLESS_PAYLOAD_SIZE) { @@ -6304,7 +6306,7 @@ static int handle_gc_lossy_packet_decoded( * Return true if packet is handled successfully. */ non_null(1, 2, 3, 4) nullable(7) -static bool handle_gc_lossy_packet(const GC_Session *c, GC_Chat *chat, const uint8_t *sender_pk, +static bool handle_gc_lossy_packet(const GC_Session *c, GC_Chat *chat, const Public_Key *sender_pk, const uint8_t *packet, uint16_t length, bool direct_conn, void *userdata) { if (length < GC_MIN_LOSSY_PAYLOAD_SIZE) { @@ -6403,15 +6405,18 @@ static int handle_gc_tcp_packet(void *object, int crypt_connection_id, const uin const uint8_t packet_type = packet[0]; - const uint8_t *sender_pk = packet + 1; + Public_Key sender_pk; + memcpy(sender_pk.data, packet + 1, ENC_PUBLIC_KEY_SIZE); const GC_Session *c = m->group_handler; GC_Chat *chat = nullptr; if (packet_type == NET_PACKET_GC_HANDSHAKE) { - chat = get_chat_by_id(c, packet + 1 + ENC_PUBLIC_KEY_SIZE); + Public_Key chat_id; + memcpy(chat_id.data, packet + 1 + ENC_PUBLIC_KEY_SIZE, ENC_PUBLIC_KEY_SIZE); + chat = get_chat_by_id(c, &chat_id); } else { - chat = get_chat_by_id(c, sender_pk); + chat = get_chat_by_id(c, &sender_pk); } if (chat == nullptr) { @@ -6427,7 +6432,7 @@ static int handle_gc_tcp_packet(void *object, int crypt_connection_id, const uin switch (packet_type) { case NET_PACKET_GC_LOSSLESS: { - if (!handle_gc_lossless_packet(c, chat, sender_pk, payload, payload_len, false, userdata)) { + if (!handle_gc_lossless_packet(c, chat, &sender_pk, payload, payload_len, false, userdata)) { return -1; } @@ -6435,7 +6440,7 @@ static int handle_gc_tcp_packet(void *object, int crypt_connection_id, const uin } case NET_PACKET_GC_LOSSY: { - if (!handle_gc_lossy_packet(c, chat, sender_pk, payload, payload_len, false, userdata)) { + if (!handle_gc_lossy_packet(c, chat, &sender_pk, payload, payload_len, false, userdata)) { return -1; } @@ -6451,7 +6456,7 @@ static int handle_gc_tcp_packet(void *object, int crypt_connection_id, const uin payload_len = payload_len - ENC_PUBLIC_KEY_SIZE; payload = payload + ENC_PUBLIC_KEY_SIZE; - return handle_gc_handshake_packet(chat, sender_pk, nullptr, payload, payload_len, false, userdata); + return handle_gc_handshake_packet(chat, &sender_pk, nullptr, payload, payload_len, false, userdata); } default: { @@ -6483,7 +6488,9 @@ static int handle_gc_tcp_oob_packet(void *object, const uint8_t *public_key, uns } const GC_Session *c = m->group_handler; - GC_Chat *chat = get_chat_by_id(c, packet + 1 + ENC_PUBLIC_KEY_SIZE); + Public_Key chat_id; + memcpy(chat_id.data, packet + 1 + ENC_PUBLIC_KEY_SIZE, ENC_PUBLIC_KEY_SIZE); + GC_Chat *chat = get_chat_by_id(c, &chat_id); if (chat == nullptr) { return -1; @@ -6499,7 +6506,8 @@ static int handle_gc_tcp_oob_packet(void *object, const uint8_t *public_key, uns return -1; } - const uint8_t *sender_pk = packet + 1; + Public_Key sender_pk; + memcpy(sender_pk.data, packet + 1, ENC_PUBLIC_KEY_SIZE); const uint8_t *payload = packet + 1 + ENC_PUBLIC_KEY_SIZE + ENC_PUBLIC_KEY_SIZE; const uint16_t payload_len = length - 1 - ENC_PUBLIC_KEY_SIZE - ENC_PUBLIC_KEY_SIZE; @@ -6508,7 +6516,7 @@ static int handle_gc_tcp_oob_packet(void *object, const uint8_t *public_key, uns return -1; } - if (handle_gc_handshake_packet(chat, sender_pk, nullptr, payload, payload_len, false, userdata) == -1) { + if (handle_gc_handshake_packet(chat, &sender_pk, nullptr, payload, payload_len, false, userdata) == -1) { return -1; } @@ -6539,15 +6547,18 @@ static int handle_gc_udp_packet(void *object, const IP_Port *source, const uint8 } const uint8_t packet_type = packet[0]; - const uint8_t *sender_pk = packet + 1; + Public_Key sender_pk; + memcpy(sender_pk.data, packet + 1, ENC_PUBLIC_KEY_SIZE); const GC_Session *c = m->group_handler; GC_Chat *chat = nullptr; if (packet_type == NET_PACKET_GC_HANDSHAKE) { - chat = get_chat_by_id(c, packet + 1 + ENC_PUBLIC_KEY_SIZE); + Public_Key chat_id; + memcpy(chat_id.data, packet + 1 + ENC_PUBLIC_KEY_SIZE, ENC_PUBLIC_KEY_SIZE); + chat = get_chat_by_id(c, &chat_id); } else { - chat = get_chat_by_id(c, sender_pk); + chat = get_chat_by_id(c, &sender_pk); } if (chat == nullptr) { @@ -6564,12 +6575,12 @@ static int handle_gc_udp_packet(void *object, const IP_Port *source, const uint8 switch (packet_type) { case NET_PACKET_GC_LOSSLESS: { - ret = handle_gc_lossless_packet(c, chat, sender_pk, payload, payload_len, true, userdata); + ret = handle_gc_lossless_packet(c, chat, &sender_pk, payload, payload_len, true, userdata); break; } case NET_PACKET_GC_LOSSY: { - ret = handle_gc_lossy_packet(c, chat, sender_pk, payload, payload_len, true, userdata); + ret = handle_gc_lossy_packet(c, chat, &sender_pk, payload, payload_len, true, userdata); break; } @@ -6582,7 +6593,7 @@ static int handle_gc_udp_packet(void *object, const IP_Port *source, const uint8 payload_len = payload_len - ENC_PUBLIC_KEY_SIZE; payload = payload + ENC_PUBLIC_KEY_SIZE; - ret = handle_gc_handshake_packet(chat, sender_pk, source, payload, payload_len, true, userdata) != -1; + ret = handle_gc_handshake_packet(chat, &sender_pk, source, payload, payload_len, true, userdata) != -1; break; } @@ -6785,7 +6796,7 @@ static int peer_update(const GC_Chat *chat, const GC_Peer *peer, uint32_t peer_n return peer_number; } -int peer_add(GC_Chat *chat, const IP_Port *ipp, const uint8_t *public_key) +int peer_add(GC_Chat *chat, const IP_Port *ipp, const Public_Key *public_key) { if (get_peer_number_of_enc_pk(chat, public_key, false) != -1) { return -2; @@ -6802,7 +6813,7 @@ int peer_add(GC_Chat *chat, const IP_Port *ipp, const uint8_t *public_key) int tcp_connection_num = -1; if (peer_number > 0) { // we don't need a connection to ourself - tcp_connection_num = new_tcp_connection_to(chat->tcp_conn, public_key, 0); + tcp_connection_num = new_tcp_connection_to(chat->tcp_conn, public_key->data, 0); if (tcp_connection_num == -1) { LOGGER_WARNING(chat->log, "Failed to init tcp connection for peer %d", peer_number); @@ -6860,7 +6871,7 @@ int peer_add(GC_Chat *chat, const IP_Port *ipp, const uint8_t *public_key) create_gc_session_keypair(chat->log, chat->rng, gconn->session_public_key, gconn->session_secret_key); if (peer_number > 0) { - memcpy(gconn->addr.public_key.enc, public_key, ENC_PUBLIC_KEY_SIZE); // we get the sig key in the handshake + gconn->addr.public_key.enc = *public_key; // we get the sig key in the handshake } else { gconn->addr.public_key = chat->self_public_key; } @@ -7257,13 +7268,9 @@ static void do_timed_out_reconn(GC_Chat *chat) } if (mono_time_is_timeout(chat->mono_time, timeout->last_seen, GC_TIMED_OUT_STALE_TIMEOUT) - || get_peer_number_of_enc_pk(chat, timeout->addr.public_key, true) != -1) { - *timeout = (GC_TimedOutPeer) { - {{ - 0 - } - } - }; + || get_peer_number_of_enc_pk(chat, &timeout->addr.public_key, true) != -1) { + const GC_TimedOutPeer empty = {{{{0}}}}; + *timeout = empty; continue; } @@ -7399,7 +7406,7 @@ static bool init_gc_tcp_connection(const GC_Session *c, GC_Chat *chat) { const Messenger *m = c->messenger; - chat->tcp_conn = new_tcp_connections(chat->log, chat->mem, chat->rng, m->ns, chat->mono_time, chat->self_secret_key.enc, + chat->tcp_conn = new_tcp_connections(chat->log, chat->mem, chat->rng, m->ns, chat->mono_time, chat->self_secret_key.enc.data, &m->options.proxy_info); if (chat->tcp_conn == nullptr) { @@ -7448,10 +7455,9 @@ static bool init_gc_shared_state_founder(GC_Chat *chat, Group_Privacy_State priv non_null() static void init_gc_moderation(GC_Chat *chat) { - memcpy(chat->moderation.founder_public_sig_key, - get_sig_pk(&chat->shared_state.founder_public_key), SIG_PUBLIC_KEY_SIZE); - memcpy(chat->moderation.self_public_sig_key, get_sig_pk(&chat->self_public_key), SIG_PUBLIC_KEY_SIZE); - memcpy(chat->moderation.self_secret_sig_key, get_sig_sk(&chat->self_secret_key), SIG_SECRET_KEY_SIZE); + chat->moderation.founder_public_sig_key = chat->shared_state.founder_public_key.sig; + chat->moderation.self_public_sig_key = chat->self_public_key.sig; + chat->moderation.self_secret_sig_key = chat->self_secret_key.sig; chat->moderation.shared_state_version = chat->shared_state.version; chat->moderation.log = chat->log; chat->moderation.mem = chat->mem; @@ -7509,7 +7515,7 @@ static int create_new_group(GC_Session *c, const uint8_t *nick, size_t nick_leng return -1; } - if (peer_add(chat, nullptr, chat->self_public_key.enc) != 0) { /* you are always peer_number/index 0 */ + if (peer_add(chat, nullptr, &chat->self_public_key.enc) != 0) { /* you are always peer_number/index 0 */ group_delete(c, chat); return -1; } @@ -7558,7 +7564,7 @@ static size_t load_gc_peers(GC_Chat *chat, const GC_SavedPeerInfo *addrs, uint16 const bool ip_port_is_set = ipport_isset(&addrs[i].ip_port); const IP_Port *ip_port = ip_port_is_set ? &addrs[i].ip_port : nullptr; - const int peer_number = peer_add(chat, ip_port, addrs[i].public_key); + const int peer_number = peer_add(chat, ip_port, &addrs[i].public_key); GC_Connection *gconn = get_gc_connection(chat, peer_number); @@ -7873,7 +7879,7 @@ int gc_invite_friend(const GC_Session *c, GC_Chat *chat, int32_t friend_number, memcpy(packet + 2, get_chat_id(&chat->chat_public_key), CHAT_ID_SIZE); uint16_t length = 2 + CHAT_ID_SIZE; - memcpy(packet + length, chat->self_public_key.enc, ENC_PUBLIC_KEY_SIZE); + memcpy(packet + length, chat->self_public_key.enc.data, ENC_PUBLIC_KEY_SIZE); length += ENC_PUBLIC_KEY_SIZE; memcpy(packet + length, chat->shared_state.group_name, group_name_length); @@ -7919,7 +7925,7 @@ static int send_gc_invite_accepted_packet(const Messenger *m, const GC_Chat *cha memcpy(packet + 2, get_chat_id(&chat->chat_public_key), CHAT_ID_SIZE); uint16_t length = 2 + CHAT_ID_SIZE; - memcpy(packet + length, chat->self_public_key.enc, ENC_PUBLIC_KEY_SIZE); + memcpy(packet + length, chat->self_public_key.enc.data, ENC_PUBLIC_KEY_SIZE); length += ENC_PUBLIC_KEY_SIZE; if (!send_group_invite_packet(m, friend_number, packet, length)) { @@ -8037,19 +8043,19 @@ int handle_gc_invite_confirmed_packet(const GC_Session *c, int friend_number, co return -4; } - uint8_t chat_id[CHAT_ID_SIZE]; - uint8_t invite_chat_pk[ENC_PUBLIC_KEY_SIZE]; + Public_Key chat_id; + Public_Key invite_chat_pk; - memcpy(chat_id, data, CHAT_ID_SIZE); - memcpy(invite_chat_pk, data + CHAT_ID_SIZE, ENC_PUBLIC_KEY_SIZE); + memcpy(&chat_id.data[0], data, CHAT_ID_SIZE); + memcpy(&invite_chat_pk.data[0], data + CHAT_ID_SIZE, ENC_PUBLIC_KEY_SIZE); - const GC_Chat *chat = gc_get_group_by_public_key(c, chat_id); + const GC_Chat *chat = gc_get_group_by_public_key(c, chat_id.data); if (chat == nullptr) { return -2; } - const int peer_number = get_peer_number_of_enc_pk(chat, invite_chat_pk, false); + const int peer_number = get_peer_number_of_enc_pk(chat, &invite_chat_pk, false); GC_Connection *gconn = get_gc_connection(chat, peer_number); @@ -8112,9 +8118,10 @@ bool handle_gc_invite_accepted_packet(const GC_Session *c, int friend_number, co return false; } - const uint8_t *invite_chat_pk = data + CHAT_ID_SIZE; + Public_Key invite_chat_pk; + memcpy(invite_chat_pk.data, data + CHAT_ID_SIZE, ENC_PUBLIC_KEY_SIZE); - const int peer_number = peer_add(chat, nullptr, invite_chat_pk); + const int peer_number = peer_add(chat, nullptr, &invite_chat_pk); if (!friend_was_invited(m, chat, friend_number)) { return false; @@ -8139,7 +8146,7 @@ bool handle_gc_invite_accepted_packet(const GC_Session *c, int friend_number, co uint8_t out_data[GC_JOIN_DATA_LENGTH + (GCC_MAX_TCP_SHARED_RELAYS * PACKED_NODE_SIZE_IP6)]; memcpy(out_data, chat_id, CHAT_ID_SIZE); - memcpy(out_data + CHAT_ID_SIZE, chat->self_public_key.enc, ENC_PUBLIC_KEY_SIZE); + memcpy(out_data + CHAT_ID_SIZE, chat->self_public_key.enc.data, ENC_PUBLIC_KEY_SIZE); if (num_tcp_relays > 0) { const uint32_t tcp_relays_added = add_gc_tcp_relays(chat, gconn, tcp_relays, num_tcp_relays); @@ -8182,7 +8189,8 @@ int gc_accept_invite(GC_Session *c, int32_t friend_number, const uint8_t *data, } const uint8_t *chat_id = data; - const uint8_t *invite_chat_pk = data + CHAT_ID_SIZE; + Public_Key invite_chat_pk; + memcpy(invite_chat_pk.data, data + CHAT_ID_SIZE, ENC_PUBLIC_KEY_SIZE); const int group_number = create_new_group(c, nick, nick_length, false, GI_PUBLIC); @@ -8208,7 +8216,7 @@ int gc_accept_invite(GC_Session *c, int32_t friend_number, const uint8_t *data, } } - const int peer_id = peer_add(chat, nullptr, invite_chat_pk); + const int peer_id = peer_add(chat, nullptr, &invite_chat_pk); if (peer_id < 0) { return -2; @@ -8398,7 +8406,7 @@ GC_Chat *gc_get_group_by_public_key(const GC_Session *c, const uint8_t *public_k continue; } - if (memcmp(public_key, get_chat_id(&chat->chat_public_key), CHAT_ID_SIZE) == 0) { + if (memcmp(public_key, chat->chat_public_key.enc.data, CHAT_ID_SIZE) == 0) { return chat; } } @@ -8416,7 +8424,7 @@ static bool group_exists(const GC_Session *c, const uint8_t *chat_id) continue; } - if (memcmp(get_chat_id(&chat->chat_public_key), chat_id, CHAT_ID_SIZE) == 0) { + if (memcmp(chat->chat_public_key.enc.data, chat_id, CHAT_ID_SIZE) == 0) { return true; } } @@ -8542,7 +8550,7 @@ int gc_add_peers_from_announces(GC_Chat *chat, const GC_Announce *announces, uin const bool ip_port_set = announce->ip_port_is_set; const IP_Port *ip_port = ip_port_set ? &announce->ip_port : nullptr; - const int peer_number = peer_add(chat, ip_port, announce->peer_public_key); + const int peer_number = peer_add(chat, ip_port, &announce->peer_public_key); GC_Connection *gconn = get_gc_connection(chat, peer_number); diff --git a/toxcore/group_chats.h b/toxcore/group_chats.h index 32a7323dc4..70e3e2c40c 100644 --- a/toxcore/group_chats.h +++ b/toxcore/group_chats.h @@ -119,7 +119,7 @@ GC_Connection *get_gc_connection(const GC_Chat *chat, int peer_number); /** @brief Returns the jenkins hash of a 32 byte public encryption key. */ non_null() -uint32_t gc_get_pk_jenkins_hash(const uint8_t *public_key); +uint32_t gc_get_pk_jenkins_hash(const Public_Key *public_key); /** @brief Check if peer with the public encryption key is in peer list. * @@ -129,7 +129,7 @@ uint32_t gc_get_pk_jenkins_hash(const uint8_t *public_key); * If `confirmed` is true the peer number will only be returned if the peer is confirmed. */ non_null() -int get_peer_number_of_enc_pk(const GC_Chat *chat, const uint8_t *public_enc_key, bool confirmed); +int get_peer_number_of_enc_pk(const GC_Chat *chat, const Public_Key *public_enc_key, bool confirmed); /** @brief Encrypts `data` of size `length` using the peer's shared key and a new nonce. * @@ -143,7 +143,7 @@ int get_peer_number_of_enc_pk(const GC_Chat *chat, const uint8_t *public_enc_key */ non_null(1, 2, 3, 4, 5) nullable(7) int group_packet_wrap( - const Logger *log, const Random *rng, const uint8_t *self_pk, const uint8_t *shared_key, uint8_t *packet, + const Logger *log, const Random *rng, const Public_Key *self_pk, const uint8_t *shared_key, uint8_t *packet, uint16_t packet_size, const uint8_t *data, uint16_t length, uint64_t message_id, uint8_t gp_packet_type, Net_Packet_Type net_packet_type); diff --git a/toxcore/group_common.h b/toxcore/group_common.h index bb1e6f9a64..9a73dfb149 100644 --- a/toxcore/group_common.h +++ b/toxcore/group_common.h @@ -205,7 +205,7 @@ typedef enum Group_Handshake_Join_Type { } Group_Handshake_Join_Type; typedef struct GC_SavedPeerInfo { - uint8_t public_key[ENC_PUBLIC_KEY_SIZE]; + Public_Key public_key; Node_format tcp_relay; IP_Port ip_port; } GC_SavedPeerInfo; @@ -259,7 +259,7 @@ typedef struct GC_TopicInfo { uint16_t length; uint16_t checksum; // used for syncing problems. the checksum with the highest value gets priority. uint8_t topic[MAX_GC_TOPIC_SIZE]; - uint8_t public_sig_key[SIG_PUBLIC_KEY_SIZE]; // Public signature key of the topic setter + Sign_Public_Key public_sig_key; // Public signature key of the topic setter } GC_TopicInfo; typedef struct GC_Chat { @@ -407,7 +407,7 @@ typedef struct GC_Session { * Return -2 if a peer with public_key is already in our peerlist. */ non_null(1, 3) nullable(2) -int peer_add(GC_Chat *chat, const IP_Port *ipp, const uint8_t *public_key); +int peer_add(GC_Chat *chat, const IP_Port *ipp, const Public_Key *public_key); /** @brief Unpacks saved peers from `data` of size `length` into `chat`. * diff --git a/toxcore/group_connection.c b/toxcore/group_connection.c index 1c2d1ec3da..ac62836216 100644 --- a/toxcore/group_connection.c +++ b/toxcore/group_connection.c @@ -438,8 +438,7 @@ int gcc_handle_packet_fragment(const GC_Session *c, GC_Chat *chat, uint32_t peer return 1; } - uint8_t sender_pk[ENC_PUBLIC_KEY_SIZE]; - memcpy(sender_pk, get_enc_key(&gconn->addr.public_key), ENC_PUBLIC_KEY_SIZE); + const Public_Key sender_pk = gconn->addr.public_key.enc; uint8_t *payload = nullptr; const uint16_t processed_len = reassemble_packet(chat->log, gconn, &payload, message_id); @@ -455,8 +454,8 @@ int gcc_handle_packet_fragment(const GC_Session *c, GC_Chat *chat, uint32_t peer } /* peer number can change from peer add operations in packet handlers */ - peer_number = get_peer_number_of_enc_pk(chat, sender_pk, false); - gconn = get_gc_connection(chat, peer_number); + const uint32_t updated_peer_number = get_peer_number_of_enc_pk(chat, &sender_pk, false); + gconn = get_gc_connection(chat, updated_peer_number); if (gconn == nullptr) { free(payload); @@ -512,14 +511,13 @@ non_null(1, 2, 3, 5) nullable(6) static bool process_recv_array_entry(const GC_Session *c, GC_Chat *chat, GC_Connection *gconn, uint32_t peer_number, GC_Message_Array_Entry *const array_entry, void *userdata) { - uint8_t sender_pk[ENC_PUBLIC_KEY_SIZE]; - memcpy(sender_pk, get_enc_key(&gconn->addr.public_key), ENC_PUBLIC_KEY_SIZE); + const Public_Key sender_pk = gconn->addr.public_key.enc; const bool ret = handle_gc_lossless_helper(c, chat, peer_number, array_entry->data, array_entry->data_length, array_entry->packet_type, userdata); /* peer number can change from peer add operations in packet handlers */ - peer_number = get_peer_number_of_enc_pk(chat, sender_pk, false); + peer_number = get_peer_number_of_enc_pk(chat, &sender_pk, false); gconn = get_gc_connection(chat, peer_number); clear_array_entry(array_entry); @@ -629,7 +627,7 @@ int gcc_encrypt_and_send_lossless_packet(const GC_Chat *chat, const GC_Connectio } const int enc_len = group_packet_wrap( - chat->log, chat->rng, chat->self_public_key.enc, gconn->session_shared_key, packet, + chat->log, chat->rng, &chat->self_public_key.enc, gconn->session_shared_key, packet, packet_size, data, length, message_id, packet_type, NET_PACKET_GC_LOSSLESS); if (enc_len < 0) { diff --git a/toxcore/group_moderation.c b/toxcore/group_moderation.c index 3cb69e9403..b4f95becf1 100644 --- a/toxcore/group_moderation.c +++ b/toxcore/group_moderation.c @@ -51,7 +51,7 @@ int mod_list_unpack(Moderation *moderation, const uint8_t *data, uint16_t length return 0; } - uint8_t **tmp_list = (uint8_t **)calloc(num_mods, sizeof(uint8_t *)); + Sign_Public_Key *tmp_list = (Sign_Public_Key *)calloc(num_mods, sizeof(Sign_Public_Key)); if (tmp_list == nullptr) { return -1; @@ -60,14 +60,9 @@ int mod_list_unpack(Moderation *moderation, const uint8_t *data, uint16_t length uint16_t unpacked_len = 0; for (uint16_t i = 0; i < num_mods; ++i) { - uint8_t *entry = (uint8_t *)malloc(MOD_LIST_ENTRY_SIZE); + Sign_Public_Key entry; - if (entry == nullptr) { - free_uint8_t_pointer_array(moderation->mem, tmp_list, i); - return -1; - } - - memcpy(entry, &data[i * MOD_LIST_ENTRY_SIZE], MOD_LIST_ENTRY_SIZE); + memcpy(&entry.data[0], &data[i * MOD_LIST_ENTRY_SIZE], MOD_LIST_ENTRY_SIZE); tmp_list[i] = entry; unpacked_len += MOD_LIST_ENTRY_SIZE; @@ -82,7 +77,7 @@ int mod_list_unpack(Moderation *moderation, const uint8_t *data, uint16_t length void mod_list_pack(const Moderation *moderation, uint8_t *data) { for (uint16_t i = 0; i < moderation->num_mods; ++i) { - memcpy(&data[i * MOD_LIST_ENTRY_SIZE], moderation->mod_list[i], MOD_LIST_ENTRY_SIZE); + memcpy(&data[i * MOD_LIST_ENTRY_SIZE], moderation->mod_list[i].data, MOD_LIST_ENTRY_SIZE); } } @@ -122,10 +117,10 @@ bool mod_list_make_hash(const Moderation *moderation, uint8_t *hash) * Returns -1 if key is not in the list. */ non_null() -static int mod_list_index_of_sig_pk(const Moderation *moderation, const uint8_t *public_sig_key) +static int mod_list_index_of_sig_pk(const Moderation *moderation, const Sign_Public_Key *public_sig_key) { for (uint16_t i = 0; i < moderation->num_mods; ++i) { - if (memcmp(moderation->mod_list[i], public_sig_key, SIG_PUBLIC_KEY_SIZE) == 0) { + if (memcmp(&moderation->mod_list[i], public_sig_key, SIG_PUBLIC_KEY_SIZE) == 0) { return i; } } @@ -133,14 +128,14 @@ static int mod_list_index_of_sig_pk(const Moderation *moderation, const uint8_t return -1; } -bool mod_list_verify_sig_pk(const Moderation *moderation, const uint8_t *sig_pk) +bool mod_list_verify_sig_pk(const Moderation *moderation, const Sign_Public_Key *sig_pk) { - if (memcmp(moderation->founder_public_sig_key, sig_pk, SIG_PUBLIC_KEY_SIZE) == 0) { + if (memcmp(&moderation->founder_public_sig_key, sig_pk, SIG_PUBLIC_KEY_SIZE) == 0) { return true; } for (uint16_t i = 0; i < moderation->num_mods; ++i) { - if (memcmp(moderation->mod_list[i], sig_pk, SIG_PUBLIC_KEY_SIZE) == 0) { + if (memcmp(&moderation->mod_list[i], sig_pk, SIG_PUBLIC_KEY_SIZE) == 0) { return true; } } @@ -162,14 +157,10 @@ bool mod_list_remove_index(Moderation *moderation, uint16_t index) --moderation->num_mods; if (index != moderation->num_mods) { - memcpy(moderation->mod_list[index], moderation->mod_list[moderation->num_mods], - MOD_LIST_ENTRY_SIZE); + moderation->mod_list[index] = moderation->mod_list[moderation->num_mods]; } - free(moderation->mod_list[moderation->num_mods]); - moderation->mod_list[moderation->num_mods] = nullptr; - - uint8_t **tmp_list = (uint8_t **)realloc(moderation->mod_list, moderation->num_mods * sizeof(uint8_t *)); + Sign_Public_Key *tmp_list = (Sign_Public_Key *)realloc(moderation->mod_list, moderation->num_mods * sizeof(Sign_Public_Key)); if (tmp_list == nullptr) { return false; @@ -180,7 +171,7 @@ bool mod_list_remove_index(Moderation *moderation, uint16_t index) return true; } -bool mod_list_remove_entry(Moderation *moderation, const uint8_t *public_sig_key) +bool mod_list_remove_entry(Moderation *moderation, const Sign_Public_Key *public_sig_key) { if (moderation->num_mods == 0) { return false; @@ -197,13 +188,13 @@ bool mod_list_remove_entry(Moderation *moderation, const uint8_t *public_sig_key return mod_list_remove_index(moderation, (uint16_t)idx); } -bool mod_list_add_entry(Moderation *moderation, const uint8_t *mod_data) +bool mod_list_add_entry(Moderation *moderation, const Sign_Public_Key *mod_data) { if (moderation->num_mods >= MOD_MAX_NUM_MODERATORS) { return false; } - uint8_t **tmp_list = (uint8_t **)realloc(moderation->mod_list, (moderation->num_mods + 1) * sizeof(uint8_t *)); + Sign_Public_Key *tmp_list = (Sign_Public_Key *)realloc(moderation->mod_list, (moderation->num_mods + 1) * sizeof(Sign_Public_Key)); if (tmp_list == nullptr) { return false; @@ -211,15 +202,7 @@ bool mod_list_add_entry(Moderation *moderation, const uint8_t *mod_data) moderation->mod_list = tmp_list; - uint8_t *entry = (uint8_t *)malloc(MOD_LIST_ENTRY_SIZE); - - if (entry == nullptr) { - return false; - } - - memcpy(entry, mod_data, MOD_LIST_ENTRY_SIZE); - - tmp_list[moderation->num_mods] = entry; + tmp_list[moderation->num_mods] = *mod_data; ++moderation->num_mods; return true; @@ -227,7 +210,7 @@ bool mod_list_add_entry(Moderation *moderation, const uint8_t *mod_data) void mod_list_cleanup(Moderation *moderation) { - free_uint8_t_pointer_array(moderation->mem, moderation->mod_list, moderation->num_mods); + free(moderation->mod_list); moderation->num_mods = 0; moderation->mod_list = nullptr; } @@ -242,7 +225,7 @@ uint16_t sanctions_creds_pack(const Mod_Sanction_Creds *creds, uint8_t *data) packed_len += MOD_SANCTION_HASH_SIZE; net_pack_u16(&data[packed_len], creds->checksum); packed_len += sizeof(uint16_t); - memcpy(&data[packed_len], creds->sig_pk, SIG_PUBLIC_KEY_SIZE); + memcpy(&data[packed_len], creds->sig_pk.data, SIG_PUBLIC_KEY_SIZE); packed_len += SIG_PUBLIC_KEY_SIZE; memcpy(&data[packed_len], creds->sig, SIGNATURE_SIZE); packed_len += SIGNATURE_SIZE; @@ -270,7 +253,7 @@ int sanctions_list_pack(uint8_t *data, uint16_t length, const Mod_Sanction *sanc memcpy(&data[packed_len], &sanctions[i].type, sizeof(uint8_t)); packed_len += sizeof(uint8_t); - memcpy(&data[packed_len], sanctions[i].setter_public_sig_key, SIG_PUBLIC_KEY_SIZE); + memcpy(&data[packed_len], sanctions[i].setter_public_sig_key.data, SIG_PUBLIC_KEY_SIZE); packed_len += SIG_PUBLIC_KEY_SIZE; net_pack_u64(&data[packed_len], sanctions[i].time_set); packed_len += TIME_STAMP_SIZE; @@ -282,7 +265,7 @@ int sanctions_list_pack(uint8_t *data, uint16_t length, const Mod_Sanction *sanc return -1; } - memcpy(&data[packed_len], sanctions[i].target_public_enc_key, ENC_PUBLIC_KEY_SIZE); + memcpy(&data[packed_len], sanctions[i].target_public_enc_key.data, ENC_PUBLIC_KEY_SIZE); packed_len += ENC_PUBLIC_KEY_SIZE; } else { return -1; @@ -324,7 +307,7 @@ uint16_t sanctions_creds_unpack(Mod_Sanction_Creds *creds, const uint8_t *data) len_processed += MOD_SANCTION_HASH_SIZE; net_unpack_u16(&data[len_processed], &creds->checksum); len_processed += sizeof(uint16_t); - memcpy(creds->sig_pk, &data[len_processed], SIG_PUBLIC_KEY_SIZE); + memcpy(creds->sig_pk.data, &data[len_processed], SIG_PUBLIC_KEY_SIZE); len_processed += SIG_PUBLIC_KEY_SIZE; memcpy(creds->sig, &data[len_processed], SIGNATURE_SIZE); len_processed += SIGNATURE_SIZE; @@ -345,7 +328,7 @@ int sanctions_list_unpack(Mod_Sanction *sanctions, Mod_Sanction_Creds *creds, ui memcpy(&sanctions[num].type, &data[len_processed], sizeof(uint8_t)); len_processed += sizeof(uint8_t); - memcpy(sanctions[num].setter_public_sig_key, &data[len_processed], SIG_PUBLIC_KEY_SIZE); + memcpy(sanctions[num].setter_public_sig_key.data, &data[len_processed], SIG_PUBLIC_KEY_SIZE); len_processed += SIG_PUBLIC_KEY_SIZE; net_unpack_u64(&data[len_processed], &sanctions[num].time_set); len_processed += TIME_STAMP_SIZE; @@ -355,7 +338,7 @@ int sanctions_list_unpack(Mod_Sanction *sanctions, Mod_Sanction_Creds *creds, ui return -1; } - memcpy(sanctions[num].target_public_enc_key, &data[len_processed], ENC_PUBLIC_KEY_SIZE); + memcpy(sanctions[num].target_public_enc_key.data, &data[len_processed], ENC_PUBLIC_KEY_SIZE); len_processed += ENC_PUBLIC_KEY_SIZE; } else { return -1; @@ -447,7 +430,7 @@ static bool sanctions_list_make_hash(const Mod_Sanction *sanctions, uint32_t new non_null() static bool sanctions_list_validate_entry(const Moderation *moderation, const Mod_Sanction *sanction) { - if (!mod_list_verify_sig_pk(moderation, sanction->setter_public_sig_key)) { + if (!mod_list_verify_sig_pk(moderation, &sanction->setter_public_sig_key)) { return false; } @@ -467,7 +450,7 @@ static bool sanctions_list_validate_entry(const Moderation *moderation, const Mo } return crypto_signature_verify(sanction->signature, packed_data, packed_len - SIGNATURE_SIZE, - sanction->setter_public_sig_key); + &sanction->setter_public_sig_key); } non_null() @@ -488,7 +471,7 @@ bool sanctions_list_make_creds(Moderation *moderation) ++moderation->sanctions_creds.version; - memcpy(moderation->sanctions_creds.sig_pk, moderation->self_public_sig_key, SIG_PUBLIC_KEY_SIZE); + moderation->sanctions_creds.sig_pk = moderation->self_public_sig_key; uint8_t hash[MOD_SANCTION_HASH_SIZE]; @@ -503,7 +486,7 @@ bool sanctions_list_make_creds(Moderation *moderation) sanctions_creds_set_checksum(&moderation->sanctions_creds); if (!crypto_signature_create(moderation->sanctions_creds.sig, moderation->sanctions_creds.hash, - MOD_SANCTION_HASH_SIZE, moderation->self_secret_sig_key)) { + MOD_SANCTION_HASH_SIZE, &moderation->self_secret_sig_key)) { moderation->sanctions_creds = old_creds; return false; } @@ -526,7 +509,7 @@ non_null(1, 3) nullable(2) static bool sanctions_creds_validate(const Moderation *moderation, const Mod_Sanction *sanctions, const Mod_Sanction_Creds *creds, uint16_t num_sanctions) { - if (!mod_list_verify_sig_pk(moderation, creds->sig_pk)) { + if (!mod_list_verify_sig_pk(moderation, &creds->sig_pk)) { LOGGER_WARNING(moderation->log, "Invalid credentials signature pk"); return false; } @@ -555,7 +538,7 @@ static bool sanctions_creds_validate(const Moderation *moderation, const Mod_San } } - if (!crypto_signature_verify(creds->sig, hash, MOD_SANCTION_HASH_SIZE, creds->sig_pk)) { + if (!crypto_signature_verify(creds->sig, hash, MOD_SANCTION_HASH_SIZE, &creds->sig_pk)) { LOGGER_WARNING(moderation->log, "Invalid signature"); return false; } @@ -680,7 +663,7 @@ static bool sanctions_list_remove_index(Moderation *moderation, uint16_t index, return true; } -bool sanctions_list_remove_observer(Moderation *moderation, const uint8_t *public_key, +bool sanctions_list_remove_observer(Moderation *moderation, const Public_Key *public_key, const Mod_Sanction_Creds *creds) { for (uint16_t i = 0; i < moderation->num_sanctions; ++i) { @@ -690,7 +673,7 @@ bool sanctions_list_remove_observer(Moderation *moderation, const uint8_t *publi continue; } - if (memcmp(public_key, curr_sanction->target_public_enc_key, ENC_PUBLIC_KEY_SIZE) == 0) { + if (memcmp(public_key, &curr_sanction->target_public_enc_key, ENC_PUBLIC_KEY_SIZE) == 0) { if (!sanctions_list_remove_index(moderation, i, creds)) { return false; } @@ -706,7 +689,7 @@ bool sanctions_list_remove_observer(Moderation *moderation, const uint8_t *publi return false; } -bool sanctions_list_is_observer(const Moderation *moderation, const uint8_t *public_key) +bool sanctions_list_is_observer(const Moderation *moderation, const Public_Key *public_key) { for (uint16_t i = 0; i < moderation->num_sanctions; ++i) { const Mod_Sanction *curr_sanction = &moderation->sanctions[i]; @@ -715,7 +698,7 @@ bool sanctions_list_is_observer(const Moderation *moderation, const uint8_t *pub continue; } - if (memcmp(curr_sanction->target_public_enc_key, public_key, ENC_PUBLIC_KEY_SIZE) == 0) { + if (memcmp(&curr_sanction->target_public_enc_key, public_key, ENC_PUBLIC_KEY_SIZE) == 0) { return true; } } @@ -726,7 +709,7 @@ bool sanctions_list_is_observer(const Moderation *moderation, const uint8_t *pub bool sanctions_list_entry_exists(const Moderation *moderation, const Mod_Sanction *sanction) { if (sanction->type == SA_OBSERVER) { - return sanctions_list_is_observer(moderation, sanction->target_public_enc_key); + return sanctions_list_is_observer(moderation, &sanction->target_public_enc_key); } return false; @@ -796,24 +779,28 @@ static bool sanctions_list_sign_entry(const Moderation *moderation, Mod_Sanction } return crypto_signature_create(sanction->signature, packed_data, packed_len - SIGNATURE_SIZE, - moderation->self_secret_sig_key); + &moderation->self_secret_sig_key); } -bool sanctions_list_make_entry(Moderation *moderation, const uint8_t *public_key, Mod_Sanction *sanction, +bool sanctions_list_make_entry(Moderation *moderation, const Public_Key *public_key, Mod_Sanction *sanction, uint8_t type) { *sanction = (Mod_Sanction) { - 0 + { + { + 0 + } + } }; if (type == SA_OBSERVER) { - memcpy(sanction->target_public_enc_key, public_key, ENC_PUBLIC_KEY_SIZE); + sanction->target_public_enc_key = *public_key; } else { LOGGER_ERROR(moderation->log, "Tried to create sanction with invalid type: %u", type); return false; } - memcpy(sanction->setter_public_sig_key, moderation->self_public_sig_key, SIG_PUBLIC_KEY_SIZE); + sanction->setter_public_sig_key = moderation->self_public_sig_key; sanction->time_set = (uint64_t)time(nullptr); sanction->type = type; @@ -834,16 +821,16 @@ bool sanctions_list_make_entry(Moderation *moderation, const uint8_t *public_key return true; } -uint16_t sanctions_list_replace_sig(Moderation *moderation, const uint8_t *public_sig_key) +uint16_t sanctions_list_replace_sig(Moderation *moderation, const Sign_Public_Key *public_sig_key) { uint16_t count = 0; for (uint16_t i = 0; i < moderation->num_sanctions; ++i) { - if (memcmp(moderation->sanctions[i].setter_public_sig_key, public_sig_key, SIG_PUBLIC_KEY_SIZE) != 0) { + if (memcmp(&moderation->sanctions[i].setter_public_sig_key, public_sig_key, SIG_PUBLIC_KEY_SIZE) != 0) { continue; } - memcpy(moderation->sanctions[i].setter_public_sig_key, moderation->self_public_sig_key, SIG_PUBLIC_KEY_SIZE); + moderation->sanctions[i].setter_public_sig_key = moderation->self_public_sig_key; if (!sanctions_list_sign_entry(moderation, &moderation->sanctions[i])) { LOGGER_ERROR(moderation->log, "Failed to sign sanction"); diff --git a/toxcore/group_moderation.h b/toxcore/group_moderation.h index eeab32fc0a..47a7a7b6d9 100644 --- a/toxcore/group_moderation.h +++ b/toxcore/group_moderation.h @@ -60,13 +60,13 @@ typedef struct Mod_Sanction_Creds { uint32_t version; uint8_t hash[MOD_SANCTION_HASH_SIZE]; // hash of all sanctions list signatures + version uint16_t checksum; // a sum of the hash - uint8_t sig_pk[SIG_PUBLIC_KEY_SIZE]; // Last mod to have modified the sanctions list + Sign_Public_Key sig_pk; // Last mod to have modified the sanctions list uint8_t sig[SIGNATURE_SIZE]; // signature of hash, signed by sig_pk } Mod_Sanction_Creds; /** Holds data pertaining to a peer who has been sanctioned. */ typedef struct Mod_Sanction { - uint8_t setter_public_sig_key[SIG_PUBLIC_KEY_SIZE]; + Sign_Public_Key setter_public_sig_key; // TODO(Jfreegman): This timestamp can potentially be used to track a user across // different group chats if they're a moderator and set many sanctions across the @@ -74,7 +74,7 @@ typedef struct Mod_Sanction { uint64_t time_set; uint8_t type; - uint8_t target_public_enc_key[ENC_PUBLIC_KEY_SIZE]; + Public_Key target_public_enc_key; /* Signature of all above packed data signed by the owner of public_sig_key */ uint8_t signature[SIGNATURE_SIZE]; @@ -89,13 +89,13 @@ typedef struct Moderation { Mod_Sanction_Creds sanctions_creds; - uint8_t **mod_list; // array of public signature keys of all the mods + Sign_Public_Key *mod_list; // array of public signature keys of all the mods uint16_t num_mods; // copies from parent/sibling chat/shared state objects - uint8_t founder_public_sig_key[SIG_PUBLIC_KEY_SIZE]; - uint8_t self_public_sig_key[SIG_PUBLIC_KEY_SIZE]; - uint8_t self_secret_sig_key[SIG_SECRET_KEY_SIZE]; + Sign_Public_Key founder_public_sig_key; + Sign_Public_Key self_public_sig_key; + Sign_Secret_Key self_secret_sig_key; uint32_t shared_state_version; } Moderation; @@ -149,7 +149,7 @@ bool mod_list_remove_index(Moderation *moderation, uint16_t index); * Returns true on success. */ non_null() -bool mod_list_remove_entry(Moderation *moderation, const uint8_t *public_sig_key); +bool mod_list_remove_entry(Moderation *moderation, const Sign_Public_Key *public_sig_key); /** @brief Adds a mod to the moderator list. * @@ -158,11 +158,11 @@ bool mod_list_remove_entry(Moderation *moderation, const uint8_t *public_sig_key * Returns true on success. */ non_null() -bool mod_list_add_entry(Moderation *moderation, const uint8_t *mod_data); +bool mod_list_add_entry(Moderation *moderation, const Sign_Public_Key *mod_data); /** @return true if the public signature key belongs to a moderator or the founder */ non_null() -bool mod_list_verify_sig_pk(const Moderation *moderation, const uint8_t *sig_pk); +bool mod_list_verify_sig_pk(const Moderation *moderation, const Sign_Public_Key *sig_pk); /** @brief Frees all memory associated with the moderator list and sets num_mods to 0. */ nullable(1) @@ -260,12 +260,12 @@ bool sanctions_list_add_entry(Moderation *moderation, const Mod_Sanction *sancti * Returns true on success. */ non_null() -bool sanctions_list_make_entry(Moderation *moderation, const uint8_t *public_key, Mod_Sanction *sanction, +bool sanctions_list_make_entry(Moderation *moderation, const Public_Key *public_key, Mod_Sanction *sanction, uint8_t type); /** @return true if public key is in the observer list. */ non_null() -bool sanctions_list_is_observer(const Moderation *moderation, const uint8_t *public_key); +bool sanctions_list_is_observer(const Moderation *moderation, const Public_Key *public_key); /** @return true if sanction already exists in the sanctions list. */ non_null() @@ -278,7 +278,7 @@ bool sanctions_list_entry_exists(const Moderation *moderation, const Mod_Sanctio * Returns false on failure or if entry was not found. */ non_null(1, 2) nullable(3) -bool sanctions_list_remove_observer(Moderation *moderation, const uint8_t *public_key, +bool sanctions_list_remove_observer(Moderation *moderation, const Public_Key *public_key, const Mod_Sanction_Creds *creds); /** @brief Replaces all sanctions list signatures made by public_sig_key with the caller's. @@ -288,7 +288,7 @@ bool sanctions_list_remove_observer(Moderation *moderation, const uint8_t *publi * Returns the number of entries re-signed. */ non_null() -uint16_t sanctions_list_replace_sig(Moderation *moderation, const uint8_t *public_sig_key); +uint16_t sanctions_list_replace_sig(Moderation *moderation, const Sign_Public_Key *public_sig_key); non_null() void sanctions_list_cleanup(Moderation *moderation); diff --git a/toxcore/group_moderation_test.cc b/toxcore/group_moderation_test.cc index ea72023e93..4a73ef00b7 100644 --- a/toxcore/group_moderation_test.cc +++ b/toxcore/group_moderation_test.cc @@ -40,44 +40,44 @@ TEST(ModList, AddRemoveMultipleMods) { Test_Memory mem; Moderation mods{mem}; - uint8_t sig_pk1[32] = {1}; - uint8_t sig_pk2[32] = {2}; - EXPECT_TRUE(mod_list_add_entry(&mods, sig_pk1)); - EXPECT_TRUE(mod_list_add_entry(&mods, sig_pk2)); - EXPECT_TRUE(mod_list_remove_entry(&mods, sig_pk1)); - EXPECT_TRUE(mod_list_remove_entry(&mods, sig_pk2)); + Sign_Public_Key sig_pk1 = {{1}}; + Sign_Public_Key sig_pk2 = {{2}}; + EXPECT_TRUE(mod_list_add_entry(&mods, &sig_pk1)); + EXPECT_TRUE(mod_list_add_entry(&mods, &sig_pk2)); + EXPECT_TRUE(mod_list_remove_entry(&mods, &sig_pk1)); + EXPECT_TRUE(mod_list_remove_entry(&mods, &sig_pk2)); } TEST(ModList, PackingAndUnpackingList) { - using ModListEntry = std::array; Test_Memory mem; Moderation mods{mem}; - EXPECT_TRUE(mod_list_add_entry(&mods, ModListEntry{}.data())); + Sign_Public_Key entry{}; + EXPECT_TRUE(mod_list_add_entry(&mods, &entry)); std::vector packed(mod_list_packed_size(&mods)); mod_list_pack(&mods, packed.data()); - EXPECT_TRUE(mod_list_remove_entry(&mods, ModListEntry{}.data())); + EXPECT_TRUE(mod_list_remove_entry(&mods, &entry)); Moderation mods2{mem}; EXPECT_EQ(mod_list_unpack(&mods2, packed.data(), packed.size(), 1), packed.size()); - EXPECT_TRUE(mod_list_remove_entry(&mods2, ModListEntry{}.data())); + EXPECT_TRUE(mod_list_remove_entry(&mods2, &entry)); } TEST(ModList, UnpackingTooManyModsFails) { - using ModListEntry = std::array; Test_Memory mem; Moderation mods{mem}; - EXPECT_TRUE(mod_list_add_entry(&mods, ModListEntry{}.data())); + Sign_Public_Key entry{}; + EXPECT_TRUE(mod_list_add_entry(&mods, &entry)); std::vector packed(mod_list_packed_size(&mods)); mod_list_pack(&mods, packed.data()); Moderation mods2{mem}; EXPECT_EQ(mod_list_unpack(&mods2, packed.data(), packed.size(), 2), -1); - EXPECT_TRUE(mod_list_remove_entry(&mods, ModListEntry{}.data())); + EXPECT_TRUE(mod_list_remove_entry(&mods, &entry)); } TEST(ModList, UnpackingFromEmptyBufferFails) @@ -115,16 +115,16 @@ TEST(ModList, RemoveEntryFromEmptyModListFails) { Test_Memory mem; Moderation mods{mem}; - uint8_t sig_pk[32] = {0}; - EXPECT_FALSE(mod_list_remove_entry(&mods, sig_pk)); + Sign_Public_Key sig_pk = {{0}}; + EXPECT_FALSE(mod_list_remove_entry(&mods, &sig_pk)); } TEST(ModList, ModListRemoveIndex) { Test_Memory mem; Moderation mods{mem}; - uint8_t sig_pk[32] = {1}; - EXPECT_TRUE(mod_list_add_entry(&mods, sig_pk)); + Sign_Public_Key sig_pk = {{1}}; + EXPECT_TRUE(mod_list_add_entry(&mods, &sig_pk)); EXPECT_TRUE(mod_list_remove_index(&mods, 0)); } @@ -139,31 +139,31 @@ TEST(ModList, EmptyModListCannotVerifyAnySigPk) { Test_Memory mem; Moderation mods{mem}; - uint8_t sig_pk[32] = {1}; - EXPECT_FALSE(mod_list_verify_sig_pk(&mods, sig_pk)); + Sign_Public_Key sig_pk = {{1}}; + EXPECT_FALSE(mod_list_verify_sig_pk(&mods, &sig_pk)); } TEST(ModList, ModListAddVerifyRemoveSigPK) { Test_Memory mem; Moderation mods{mem}; - uint8_t sig_pk[32] = {1}; - EXPECT_TRUE(mod_list_add_entry(&mods, sig_pk)); - EXPECT_TRUE(mod_list_verify_sig_pk(&mods, sig_pk)); - EXPECT_TRUE(mod_list_remove_entry(&mods, sig_pk)); - EXPECT_FALSE(mod_list_verify_sig_pk(&mods, sig_pk)); + Sign_Public_Key sig_pk = {{1}}; + EXPECT_TRUE(mod_list_add_entry(&mods, &sig_pk)); + EXPECT_TRUE(mod_list_verify_sig_pk(&mods, &sig_pk)); + EXPECT_TRUE(mod_list_remove_entry(&mods, &sig_pk)); + EXPECT_FALSE(mod_list_verify_sig_pk(&mods, &sig_pk)); } TEST(ModList, ModListHashCheck) { Test_Memory mem; Moderation mods1{mem}; - uint8_t sig_pk1[32] = {1}; + Sign_Public_Key sig_pk1 = {{1}}; std::array hash1; - EXPECT_TRUE(mod_list_add_entry(&mods1, sig_pk1)); + EXPECT_TRUE(mod_list_add_entry(&mods1, &sig_pk1)); EXPECT_TRUE(mod_list_make_hash(&mods1, hash1.data())); - EXPECT_TRUE(mod_list_remove_entry(&mods1, sig_pk1)); + EXPECT_TRUE(mod_list_remove_entry(&mods1, &sig_pk1)); } TEST(SanctionsList, PackingIntoUndersizedBufferFails) @@ -197,8 +197,8 @@ struct SanctionsListMod : ::testing::Test { Moderation mod{mem}; Mod_Sanction sanctions[2] = {}; - const uint8_t sanctioned_pk1[32] = {1}; - const uint8_t sanctioned_pk2[32] = {2}; + const Public_Key sanctioned_pk1 = {{1}}; + const Public_Key sanctioned_pk2 = {{2}}; void SetUp() override { @@ -206,31 +206,31 @@ struct SanctionsListMod : ::testing::Test { mod.log = log; - memcpy(mod.self_public_sig_key, get_sig_pk(&pk), SIG_PUBLIC_KEY_SIZE); - memcpy(mod.self_secret_sig_key, get_sig_sk(&sk), SIG_SECRET_KEY_SIZE); + mod.self_public_sig_key = pk.sig; + mod.self_secret_sig_key = sk.sig; - ASSERT_TRUE(mod_list_add_entry(&mod, get_sig_pk(&pk))); + ASSERT_TRUE(mod_list_add_entry(&mod, &pk.sig)); EXPECT_FALSE(sanctions_list_check_integrity(&mod, &mod.sanctions_creds, &sanctions[0], 0)); EXPECT_FALSE(sanctions_list_check_integrity(&mod, &mod.sanctions_creds, &sanctions[0], 1)); EXPECT_FALSE( sanctions_list_check_integrity(&mod, &mod.sanctions_creds, &sanctions[0], UINT16_MAX)); - EXPECT_TRUE(sanctions_list_make_entry(&mod, sanctioned_pk1, &sanctions[0], SA_OBSERVER)); + EXPECT_TRUE(sanctions_list_make_entry(&mod, &sanctioned_pk1, &sanctions[0], SA_OBSERVER)); EXPECT_TRUE(sanctions_list_check_integrity( &mod, &mod.sanctions_creds, sanctions, mod.num_sanctions)); - EXPECT_TRUE(sanctions_list_make_entry(&mod, sanctioned_pk2, &sanctions[1], SA_OBSERVER)); + EXPECT_TRUE(sanctions_list_make_entry(&mod, &sanctioned_pk2, &sanctions[1], SA_OBSERVER)); EXPECT_TRUE(sanctions_list_check_integrity( &mod, &mod.sanctions_creds, sanctions, mod.num_sanctions)); } ~SanctionsListMod() override { - EXPECT_TRUE(sanctions_list_remove_observer(&mod, sanctioned_pk1, nullptr)); - EXPECT_TRUE(sanctions_list_remove_observer(&mod, sanctioned_pk2, nullptr)); + EXPECT_TRUE(sanctions_list_remove_observer(&mod, &sanctioned_pk1, nullptr)); + EXPECT_TRUE(sanctions_list_remove_observer(&mod, &sanctioned_pk2, nullptr)); EXPECT_FALSE(sanctions_list_entry_exists(&mod, &sanctions[0])); EXPECT_FALSE(sanctions_list_entry_exists(&mod, &sanctions[1])); - EXPECT_TRUE(mod_list_remove_entry(&mod, get_sig_pk(&pk))); + EXPECT_TRUE(mod_list_remove_entry(&mod, &pk.sig)); logger_kill(log); } @@ -260,7 +260,7 @@ TEST_F(SanctionsListMod, PackUnpackSanction) TEST_F(SanctionsListMod, ReplaceSanctionSignatures) { - EXPECT_EQ(sanctions_list_replace_sig(&mod, mod.self_public_sig_key), mod.num_sanctions); + EXPECT_EQ(sanctions_list_replace_sig(&mod, &mod.self_public_sig_key), mod.num_sanctions); EXPECT_TRUE( sanctions_list_check_integrity(&mod, &mod.sanctions_creds, sanctions, mod.num_sanctions)); } diff --git a/toxcore/group_onion_announce.c b/toxcore/group_onion_announce.c index d05db09a07..6a1002253e 100644 --- a/toxcore/group_onion_announce.c +++ b/toxcore/group_onion_announce.c @@ -48,8 +48,8 @@ static int pack_group_announces(void *object, const Logger *logger, const Mono_T const int num_ann = gca_get_announces(gc_announces_list, gc_announces, GCA_MAX_SENT_ANNOUNCES, - public_announce.chat_public_key, - new_announce->base_announce.peer_public_key); + &public_announce.chat_public_key, + &new_announce->base_announce.peer_public_key); if (num_ann < 0) { LOGGER_ERROR(logger, "failed to get group announce"); diff --git a/toxcore/group_pack.c b/toxcore/group_pack.c index e3af82c67a..bbb2ab8018 100644 --- a/toxcore/group_pack.c +++ b/toxcore/group_pack.c @@ -147,7 +147,7 @@ static bool load_unpack_topic_info(GC_Chat *chat, Bin_Unpack *bu) && bin_unpack_u16(bu, &chat->topic_info.length) && bin_unpack_u16(bu, &chat->topic_info.checksum) && bin_unpack_bin_max(bu, chat->topic_info.topic, &chat->topic_info.length, sizeof(chat->topic_info.topic)) - && bin_unpack_bin_fixed(bu, chat->topic_info.public_sig_key, SIG_PUBLIC_KEY_SIZE) + && bin_unpack_bin_fixed(bu, chat->topic_info.public_sig_key.data, SIG_PUBLIC_KEY_SIZE) && bin_unpack_bin_fixed(bu, chat->topic_sig, SIGNATURE_SIZE))) { LOGGER_ERROR(chat->log, "Failed to unpack topic info"); return false; @@ -256,7 +256,7 @@ static bool load_unpack_self_info(GC_Chat *chat, Bin_Unpack *bu) } // we have to add ourself before setting self info - if (peer_add(chat, nullptr, chat->self_public_key.enc) != 0) { + if (peer_add(chat, nullptr, &chat->self_public_key.enc) != 0) { LOGGER_ERROR(chat->log, "Failed to add self to peer list"); return false; } @@ -373,7 +373,7 @@ static void save_pack_topic_info(const GC_Chat *chat, Bin_Pack *bp) bin_pack_u16(bp, chat->topic_info.length); // 2 bin_pack_u16(bp, chat->topic_info.checksum); // 3 bin_pack_bin(bp, chat->topic_info.topic, chat->topic_info.length); // 4 - bin_pack_bin(bp, chat->topic_info.public_sig_key, SIG_PUBLIC_KEY_SIZE); // 5 + bin_pack_bin(bp, chat->topic_info.public_sig_key.data, SIG_PUBLIC_KEY_SIZE); // 5 bin_pack_bin(bp, chat->topic_sig, SIGNATURE_SIZE); // 6 } diff --git a/toxcore/util.c b/toxcore/util.c index 1851e58a08..3fd71ec6a4 100644 --- a/toxcore/util.c +++ b/toxcore/util.c @@ -24,21 +24,6 @@ bool is_power_of_2(uint64_t x) return x != 0 && (x & (~x + 1)) == x; } -void free_uint8_t_pointer_array(const Memory *mem, uint8_t **ary, size_t n_items) -{ - if (ary == nullptr) { - return; - } - - for (size_t i = 0; i < n_items; ++i) { - if (ary[i] != nullptr) { - mem_delete(mem, ary[i]); - } - } - - mem_delete(mem, ary); -} - uint16_t data_checksum(const uint8_t *data, uint32_t length) { uint8_t checksum[2] = {0}; diff --git a/toxcore/util.h b/toxcore/util.h index 5be74a8d86..531e5b63ea 100644 --- a/toxcore/util.h +++ b/toxcore/util.h @@ -24,10 +24,6 @@ extern "C" { bool is_power_of_2(uint64_t x); -/** @brief Frees all pointers in a uint8_t pointer array, as well as the array itself. */ -non_null(1) nullable(2) -void free_uint8_t_pointer_array(const Memory *mem, uint8_t **ary, size_t n_items); - /** Returns -1 if failed or 0 if success */ non_null() int create_recursive_mutex(pthread_mutex_t *mutex);