Skip to content

Commit

Permalink
refactor: Use tox rng to seed the keypair generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Feb 9, 2024
1 parent 442213b commit 704898e
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 9 deletions.
7 changes: 5 additions & 2 deletions toxcore/crypto_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,13 @@ 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(uint8_t pk[EXT_PUBLIC_KEY_SIZE], uint8_t sk[EXT_SECRET_KEY_SIZE], const Random *rng)
{
/* create signature key pair */
crypto_sign_keypair(pk + ENC_PUBLIC_KEY_SIZE, sk + ENC_SECRET_KEY_SIZE);
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);

/* convert public signature key to public encryption key */
const int res1 = crypto_sign_ed25519_pk_to_curve25519(pk, pk + ENC_PUBLIC_KEY_SIZE);
Expand Down
2 changes: 1 addition & 1 deletion toxcore/crypto_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ 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(uint8_t pk[EXT_PUBLIC_KEY_SIZE], uint8_t sk[EXT_SECRET_KEY_SIZE], const Random *rng);

/** Functions for groupchat extended keys */
non_null() const uint8_t *get_enc_key(const uint8_t *key);
Expand Down
2 changes: 1 addition & 1 deletion toxcore/crypto_core_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ TEST(CryptoCore, Signatures)
ExtPublicKey pk;
ExtSecretKey sk;

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

std::vector<uint8_t> message{0};
message.clear();
Expand Down
8 changes: 4 additions & 4 deletions toxcore/group_chats.c
Original file line number Diff line number Diff line change
Expand Up @@ -7626,8 +7626,8 @@ int gc_group_load(GC_Session *c, Bin_Unpack *bu)
return group_number;
}

int gc_group_add(GC_Session *c, Group_Privacy_State privacy_state, const uint8_t *group_name,
uint16_t group_name_length,
int gc_group_add(GC_Session *c, Group_Privacy_State privacy_state,
const uint8_t *group_name, uint16_t group_name_length,
const uint8_t *nick, size_t nick_length)
{
if (group_name_length > MAX_GC_GROUP_NAME_SIZE) {
Expand Down Expand Up @@ -7660,7 +7660,7 @@ int gc_group_add(GC_Session *c, Group_Privacy_State privacy_state, const uint8_t

crypto_memlock(chat->chat_secret_key, sizeof(chat->chat_secret_key));

create_extended_keypair(chat->chat_public_key, chat->chat_secret_key);
create_extended_keypair(chat->chat_public_key, chat->chat_secret_key, chat->rng);

if (!init_gc_shared_state_founder(chat, privacy_state, group_name, group_name_length)) {
group_delete(c, chat);
Expand Down Expand Up @@ -8409,7 +8409,7 @@ static bool create_new_chat_ext_keypair(GC_Chat *chat)
{
crypto_memlock(chat->self_secret_key, sizeof(chat->self_secret_key));

if (!create_extended_keypair(chat->self_public_key, chat->self_secret_key)) {
if (!create_extended_keypair(chat->self_public_key, chat->self_secret_key, chat->rng)) {
crypto_memunlock(chat->self_secret_key, sizeof(chat->self_secret_key));
return false;
}
Expand Down
3 changes: 2 additions & 1 deletion toxcore/group_moderation_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ struct SanctionsListMod : ::testing::Test {
ExtPublicKey pk;
ExtSecretKey sk;
Logger *log = logger_new();
Test_Random rng;
Test_Memory mem;
Moderation mod{mem};

Expand All @@ -203,7 +204,7 @@ struct SanctionsListMod : ::testing::Test {

void SetUp() override
{
ASSERT_TRUE(create_extended_keypair(pk.data(), sk.data()));
ASSERT_TRUE(create_extended_keypair(pk.data(), sk.data(), rng));

mod.log = log;

Expand Down

0 comments on commit 704898e

Please sign in to comment.