Skip to content

Commit

Permalink
refactor: Use structs for extended public/secret keys.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Feb 11, 2024
1 parent 5dd9ee3 commit e602d68
Show file tree
Hide file tree
Showing 16 changed files with 261 additions and 139 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ set(toxcore_SOURCES
toxcore/ccompat.h
toxcore/crypto_core.c
toxcore/crypto_core.h
toxcore/crypto_core_pack.c
toxcore/crypto_core_pack.h
toxcore/DHT.c
toxcore/DHT.h
toxcore/events/conference_connected.c
Expand Down
14 changes: 14 additions & 0 deletions toxcore/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,19 @@ cc_library(
],
)

cc_library(
name = "crypto_core_pack",
srcs = ["crypto_core_pack.c"],
hdrs = ["crypto_core_pack.h"],
visibility = ["//c-toxcore:__subpackages__"],
deps = [
":attributes",
":bin_pack",
":bin_unpack",
":crypto_core",
],
)

cc_library(
name = "crypto_core_test_util",
testonly = True,
Expand Down Expand Up @@ -920,6 +933,7 @@ cc_library(
":bin_unpack",
":ccompat",
":crypto_core",
":crypto_core_pack",
":forwarding",
":friend_connection",
":friend_requests",
Expand Down
2 changes: 2 additions & 0 deletions toxcore/Makefile.inc
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ libtoxcore_la_SOURCES = ../third_party/cmp/cmp.c \
../toxcore/network.c \
../toxcore/crypto_core.h \
../toxcore/crypto_core.c \
../toxcore/crypto_core_pack.h \
../toxcore/crypto_core_pack.c \
../toxcore/timed_auth.h \
../toxcore/timed_auth.c \
../toxcore/ping_array.h \
Expand Down
6 changes: 3 additions & 3 deletions toxcore/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ bool m_create_group_connection(Messenger *m, GC_Chat *chat)
const int onion_friend_number = friend_conn_get_onion_friendnum(connection);
Onion_Friend *onion_friend = onion_get_friend(m->onion_c, (uint16_t)onion_friend_number);

onion_friend_set_gc_public_key(onion_friend, get_chat_id(chat->chat_public_key));
onion_friend_set_gc_public_key(onion_friend, get_chat_id(&chat->chat_public_key));
onion_friend_set_gc_data(onion_friend, nullptr, 0);

return true;
Expand Down Expand Up @@ -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_PUBLIC_KEY_SIZE);
memcpy(announce.chat_public_key, get_chat_id(chat->chat_public_key), ENC_PUBLIC_KEY_SIZE);
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);

uint8_t gc_data[GCA_MAX_DATA_LENGTH];
const int length = gca_pack_public_announce(m->log, gc_data, GCA_MAX_DATA_LENGTH, &announce);
Expand Down
4 changes: 3 additions & 1 deletion toxcore/bin_pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ static bool null_skipper(cmp_ctx_t *ctx, size_t count)
non_null()
static size_t buf_writer(cmp_ctx_t *ctx, const void *data, size_t count)
{
const uint8_t *bytes = (const uint8_t *)data;

Bin_Pack *bp = (Bin_Pack *)ctx->buf;
assert(bp != nullptr);
const uint32_t new_pos = bp->bytes_pos + count;
Expand All @@ -48,7 +50,7 @@ static size_t buf_writer(cmp_ctx_t *ctx, const void *data, size_t count)
// Buffer too small.
return 0;
}
memcpy(&bp->bytes[bp->bytes_pos], data, count);
memcpy(&bp->bytes[bp->bytes_pos], bytes, count);
}
bp->bytes_pos += count;
return count;
Expand Down
4 changes: 3 additions & 1 deletion toxcore/bin_unpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ struct Bin_Unpack {
non_null()
static bool buf_reader(cmp_ctx_t *ctx, void *data, size_t limit)
{
uint8_t *bytes = (uint8_t *)data;

Bin_Unpack *reader = (Bin_Unpack *)ctx->buf;
assert(reader != nullptr && reader->bytes != nullptr);
if (limit > reader->bytes_size) {
return false;
}
memcpy(data, reader->bytes, limit);
memcpy(bytes, reader->bytes, limit);
reader->bytes += limit;
reader->bytes_size -= limit;
return true;
Expand Down
28 changes: 14 additions & 14 deletions toxcore/crypto_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,43 +45,43 @@ static_assert(CRYPTO_SIGN_PUBLIC_KEY_SIZE == crypto_sign_PUBLICKEYBYTES,
static_assert(CRYPTO_SIGN_SECRET_KEY_SIZE == crypto_sign_SECRETKEYBYTES,
"CRYPTO_SIGN_SECRET_KEY_SIZE should be equal to crypto_sign_SECRETKEYBYTES");

bool create_extended_keypair(uint8_t pk[EXT_PUBLIC_KEY_SIZE], uint8_t sk[EXT_SECRET_KEY_SIZE])
bool create_extended_keypair(Extended_Public_Key *pk, Extended_Secret_Key *sk)
{
/* create signature key pair */
crypto_sign_keypair(pk + ENC_PUBLIC_KEY_SIZE, sk + ENC_SECRET_KEY_SIZE);
crypto_sign_keypair(pk->sig, sk->sig);

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

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

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

const uint8_t *get_enc_key(const uint8_t *key)
const uint8_t *get_enc_key(const Extended_Public_Key *key)
{
return key;
return key->enc;
}

const uint8_t *get_sig_pk(const uint8_t *key)
const uint8_t *get_sig_pk(const Extended_Public_Key *key)
{
return key + ENC_PUBLIC_KEY_SIZE;
return key->sig;
}

void set_sig_pk(uint8_t *key, const uint8_t *sig_pk)
void set_sig_pk(Extended_Public_Key *key, const uint8_t *sig_pk)
{
memcpy(key + ENC_PUBLIC_KEY_SIZE, sig_pk, SIG_PUBLIC_KEY_SIZE);
memcpy(key->sig, sig_pk, SIG_PUBLIC_KEY_SIZE);
}

const uint8_t *get_sig_sk(const uint8_t *key)
const uint8_t *get_sig_sk(const Extended_Secret_Key *key)
{
return key + ENC_SECRET_KEY_SIZE;
return key->sig;
}

const uint8_t *get_chat_id(const uint8_t *key)
const uint8_t *get_chat_id(const Extended_Public_Key *key)
{
return key + ENC_PUBLIC_KEY_SIZE;
return key->sig;
}

#if !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
Expand Down
22 changes: 16 additions & 6 deletions toxcore/crypto_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,16 @@ 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.
Expand All @@ -337,14 +347,14 @@ bool public_key_valid(const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]);
* @retval true on success.
*/
non_null()
bool create_extended_keypair(uint8_t pk[EXT_PUBLIC_KEY_SIZE], uint8_t sk[EXT_SECRET_KEY_SIZE]);
bool create_extended_keypair(Extended_Public_Key *pk, Extended_Secret_Key *sk);

/** Functions for groupchat extended keys */
non_null() const uint8_t *get_enc_key(const uint8_t *key);
non_null() const uint8_t *get_sig_pk(const uint8_t *key);
non_null() void set_sig_pk(uint8_t *key, const uint8_t *sig_pk);
non_null() const uint8_t *get_sig_sk(const uint8_t *key);
non_null() const uint8_t *get_chat_id(const uint8_t *key);
non_null() const uint8_t *get_enc_key(const Extended_Public_Key *key);
non_null() const uint8_t *get_sig_pk(const Extended_Public_Key *key);
non_null() void set_sig_pk(Extended_Public_Key *key, const uint8_t *sig_pk);
non_null() const uint8_t *get_sig_sk(const Extended_Secret_Key *key);
non_null() const uint8_t *get_chat_id(const Extended_Public_Key *key);

/**
* @brief Generate a new random keypair.
Expand Down
58 changes: 58 additions & 0 deletions toxcore/crypto_core_pack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2016-2024 The TokTok team.
* Copyright © 2013 Tox project.
*/

#include "crypto_core_pack.h"

#include <string.h>

#include "bin_pack.h"
#include "bin_unpack.h"
#include "crypto_core.h"

bool pack_extended_public_key(const Extended_Public_Key *key, Bin_Pack *bp)
{
uint8_t ext_key[EXT_PUBLIC_KEY_SIZE];
memcpy(ext_key, key->enc, CRYPTO_PUBLIC_KEY_SIZE);
memcpy(&ext_key[CRYPTO_PUBLIC_KEY_SIZE], key->sig, CRYPTO_SIGN_PUBLIC_KEY_SIZE);

return bin_pack_bin(bp, ext_key, EXT_PUBLIC_KEY_SIZE);
}

bool pack_extended_secret_key(const Extended_Secret_Key *key, Bin_Pack *bp)
{
uint8_t ext_key[EXT_SECRET_KEY_SIZE];
memcpy(ext_key, key->enc, CRYPTO_SECRET_KEY_SIZE);
memcpy(&ext_key[CRYPTO_SECRET_KEY_SIZE], key->sig, CRYPTO_SIGN_SECRET_KEY_SIZE);

return bin_pack_bin(bp, ext_key, EXT_SECRET_KEY_SIZE);
}

bool unpack_extended_public_key(Extended_Public_Key *key, Bin_Unpack *bu)
{
uint8_t ext_key[EXT_PUBLIC_KEY_SIZE];

if (!bin_unpack_bin_fixed(bu, ext_key, EXT_PUBLIC_KEY_SIZE)) {
return false;
}

memcpy(key->enc, ext_key, CRYPTO_PUBLIC_KEY_SIZE);
memcpy(key->sig, &ext_key[CRYPTO_PUBLIC_KEY_SIZE], CRYPTO_SIGN_PUBLIC_KEY_SIZE);

return true;
}

bool unpack_extended_secret_key(Extended_Secret_Key *key, Bin_Unpack *bu)
{
uint8_t ext_key[EXT_SECRET_KEY_SIZE];

if (!bin_unpack_bin_fixed(bu, ext_key, EXT_SECRET_KEY_SIZE)) {
return false;
}

memcpy(key->enc, ext_key, CRYPTO_SECRET_KEY_SIZE);
memcpy(key->sig, &ext_key[CRYPTO_SECRET_KEY_SIZE], CRYPTO_SIGN_SECRET_KEY_SIZE);

return true;
}
31 changes: 31 additions & 0 deletions toxcore/crypto_core_pack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2016-2024 The TokTok team.
* Copyright © 2013 Tox project.
*/

#ifndef C_TOXCORE_TOXCORE_CRYPTO_CORE_PACK_H
#define C_TOXCORE_TOXCORE_CRYPTO_CORE_PACK_H

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

#include "attributes.h"
#include "bin_pack.h"
#include "bin_unpack.h"
#include "crypto_core.h"

#ifdef __cplusplus
extern "C" {
#endif

non_null() bool pack_extended_public_key(const Extended_Public_Key *key, Bin_Pack *bp);
non_null() bool pack_extended_secret_key(const Extended_Secret_Key *key, Bin_Pack *bp);
non_null() bool unpack_extended_public_key(Extended_Public_Key *key, Bin_Unpack *bu);
non_null() bool unpack_extended_secret_key(Extended_Secret_Key *key, Bin_Unpack *bu);

#ifdef __cplusplus
} /* extern "C" */
#endif

#endif /* C_TOXCORE_TOXCORE_CRYPTO_CORE_PACK_H */
12 changes: 5 additions & 7 deletions toxcore/crypto_core_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ namespace {
using HmacKey = std::array<uint8_t, CRYPTO_HMAC_KEY_SIZE>;
using Hmac = std::array<uint8_t, CRYPTO_HMAC_SIZE>;
using SecretKey = std::array<uint8_t, CRYPTO_SECRET_KEY_SIZE>;
using ExtPublicKey = std::array<uint8_t, EXT_PUBLIC_KEY_SIZE>;
using ExtSecretKey = std::array<uint8_t, EXT_SECRET_KEY_SIZE>;
using Signature = std::array<uint8_t, CRYPTO_SIGNATURE_SIZE>;
using Nonce = std::array<uint8_t, CRYPTO_NONCE_SIZE>;

Expand Down Expand Up @@ -72,10 +70,10 @@ TEST(CryptoCore, Signatures)
{
Test_Random rng;

ExtPublicKey pk;
ExtSecretKey sk;
Extended_Public_Key pk;
Extended_Secret_Key sk;

EXPECT_TRUE(create_extended_keypair(pk.data(), sk.data()));
EXPECT_TRUE(create_extended_keypair(&pk, &sk));

std::vector<uint8_t> message{0};
message.clear();
Expand All @@ -84,9 +82,9 @@ TEST(CryptoCore, Signatures)
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.data())));
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.data())));
signature.data(), message.data(), message.size(), get_sig_pk(&pk)));

message.push_back(random_u08(rng));
}
Expand Down
Loading

0 comments on commit e602d68

Please sign in to comment.