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 14, 2024
1 parent a1e999f commit 021db70
Show file tree
Hide file tree
Showing 14 changed files with 265 additions and 137 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
15 changes: 15 additions & 0 deletions toxcore/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,20 @@ 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",
":ccompat",
":crypto_core",
],
)

cc_library(
name = "crypto_core_test_util",
testonly = True,
Expand Down Expand Up @@ -920,6 +934,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
29 changes: 15 additions & 14 deletions toxcore/crypto_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,45 +45,46 @@ 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], const Random *rng)
bool create_extended_keypair(Extended_Public_Key *pk, Extended_Secret_Key *sk, const Random *rng)
{
/* create signature key pair */
uint8_t seed[crypto_sign_SEEDBYTES];
random_bytes(rng, seed, crypto_sign_SEEDBYTES);
crypto_sign_seed_keypair(pk + ENC_PUBLIC_KEY_SIZE, sk + ENC_SECRET_KEY_SIZE, seed);
crypto_sign_seed_keypair(pk->sig, sk->sig, 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, 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 @@ -338,14 +348,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], const Random *rng);
bool create_extended_keypair(Extended_Public_Key *pk, Extended_Secret_Key *sk, const Random *rng);

/** 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
66 changes: 66 additions & 0 deletions toxcore/crypto_core_pack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* 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 "ccompat.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];
static_assert(sizeof(ext_key) == sizeof(key->enc) + sizeof(key->sig),
"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));

return bin_pack_bin(bp, ext_key, sizeof(ext_key));
}

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),
"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));

const bool result = bin_pack_bin(bp, ext_key, sizeof(ext_key));
crypto_memzero(ext_key, sizeof(ext_key));
return result;
}

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, sizeof(ext_key))) {
return false;
}

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

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, sizeof(ext_key))) {
return false;
}

memcpy(key->enc, ext_key, sizeof(key->enc));
memcpy(key->sig, &ext_key[sizeof(key->enc)], sizeof(key->sig));
crypto_memzero(ext_key, sizeof(ext_key));

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(), rng));
EXPECT_TRUE(create_extended_keypair(&pk, &sk, rng));

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 021db70

Please sign in to comment.