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 9, 2024
1 parent d75796e commit d3cc534
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 137 deletions.
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
28 changes: 14 additions & 14 deletions toxcore/crypto_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,45 +46,45 @@ 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);

/* 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
27 changes: 21 additions & 6 deletions toxcore/crypto_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,21 @@ 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 uint8_t Public_Key[CRYPTO_PUBLIC_KEY_SIZE];
typedef uint8_t Secret_Key[CRYPTO_SECRET_KEY_SIZE];
typedef uint8_t Sign_Public_Key[CRYPTO_SIGN_PUBLIC_KEY_SIZE];
typedef uint8_t Sign_Secret_Key[CRYPTO_SIGN_SECRET_KEY_SIZE];

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 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 +353,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
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 d3cc534

Please sign in to comment.