Skip to content

Commit 5d7b7a7

Browse files
committed
refactor: Use tox rng to seed the keypair generation.
1 parent 961891d commit 5d7b7a7

File tree

6 files changed

+14
-10
lines changed

6 files changed

+14
-10
lines changed

other/docker/goblint/sodium.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include <string.h>
44

5-
int crypto_sign_keypair(unsigned char *pk, unsigned char *sk)
5+
int crypto_sign_seed_keypair(unsigned char *pk, unsigned char *sk, const unsigned char *seed)
66
{
77
memset(pk, 0, 32);
88
memset(sk, 0, 32);

toxcore/crypto_core.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@ static_assert(CRYPTO_SIGN_PUBLIC_KEY_SIZE == crypto_sign_PUBLICKEYBYTES,
4545
static_assert(CRYPTO_SIGN_SECRET_KEY_SIZE == crypto_sign_SECRETKEYBYTES,
4646
"CRYPTO_SIGN_SECRET_KEY_SIZE should be equal to crypto_sign_SECRETKEYBYTES");
4747

48-
bool create_extended_keypair(uint8_t pk[EXT_PUBLIC_KEY_SIZE], uint8_t sk[EXT_SECRET_KEY_SIZE])
48+
bool create_extended_keypair(uint8_t pk[EXT_PUBLIC_KEY_SIZE], uint8_t sk[EXT_SECRET_KEY_SIZE], const Random *rng)
4949
{
5050
/* create signature key pair */
51-
crypto_sign_keypair(pk + ENC_PUBLIC_KEY_SIZE, sk + ENC_SECRET_KEY_SIZE);
51+
uint8_t seed[crypto_sign_SEEDBYTES];
52+
random_bytes(rng, seed, crypto_sign_SEEDBYTES);
53+
crypto_sign_seed_keypair(pk + ENC_PUBLIC_KEY_SIZE, sk + ENC_SECRET_KEY_SIZE, seed);
5254

5355
/* convert public signature key to public encryption key */
5456
const int res1 = crypto_sign_ed25519_pk_to_curve25519(pk, pk + ENC_PUBLIC_KEY_SIZE);

toxcore/crypto_core.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,11 +333,12 @@ bool public_key_valid(const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]);
333333
*
334334
* @param[out] pk The buffer where the public key will be stored. Must have room for EXT_PUBLIC_KEY_SIZE bytes.
335335
* @param[out] sk The buffer where the secret key will be stored. Must have room for EXT_SECRET_KEY_SIZE bytes.
336+
* @param rng The random number generator to use for the key generator seed.
336337
*
337338
* @retval true on success.
338339
*/
339340
non_null()
340-
bool create_extended_keypair(uint8_t pk[EXT_PUBLIC_KEY_SIZE], uint8_t sk[EXT_SECRET_KEY_SIZE]);
341+
bool create_extended_keypair(uint8_t pk[EXT_PUBLIC_KEY_SIZE], uint8_t sk[EXT_SECRET_KEY_SIZE], const Random *rng);
341342

342343
/** Functions for groupchat extended keys */
343344
non_null() const uint8_t *get_enc_key(const uint8_t *key);

toxcore/crypto_core_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ TEST(CryptoCore, Signatures)
7575
ExtPublicKey pk;
7676
ExtSecretKey sk;
7777

78-
EXPECT_TRUE(create_extended_keypair(pk.data(), sk.data()));
78+
EXPECT_TRUE(create_extended_keypair(pk.data(), sk.data(), rng));
7979

8080
std::vector<uint8_t> message{0};
8181
message.clear();

toxcore/group_chats.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7656,8 +7656,8 @@ int gc_group_load(GC_Session *c, Bin_Unpack *bu)
76567656
return group_number;
76577657
}
76587658

7659-
int gc_group_add(GC_Session *c, Group_Privacy_State privacy_state, const uint8_t *group_name,
7660-
uint16_t group_name_length,
7659+
int gc_group_add(GC_Session *c, Group_Privacy_State privacy_state,
7660+
const uint8_t *group_name, uint16_t group_name_length,
76617661
const uint8_t *nick, size_t nick_length)
76627662
{
76637663
if (group_name_length > MAX_GC_GROUP_NAME_SIZE) {
@@ -7690,7 +7690,7 @@ int gc_group_add(GC_Session *c, Group_Privacy_State privacy_state, const uint8_t
76907690

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

7693-
create_extended_keypair(chat->chat_public_key, chat->chat_secret_key);
7693+
create_extended_keypair(chat->chat_public_key, chat->chat_secret_key, chat->rng);
76947694

76957695
if (!init_gc_shared_state_founder(chat, privacy_state, group_name, group_name_length)) {
76967696
group_delete(c, chat);
@@ -8439,7 +8439,7 @@ static bool create_new_chat_ext_keypair(GC_Chat *chat)
84398439
{
84408440
crypto_memlock(chat->self_secret_key, sizeof(chat->self_secret_key));
84418441

8442-
if (!create_extended_keypair(chat->self_public_key, chat->self_secret_key)) {
8442+
if (!create_extended_keypair(chat->self_public_key, chat->self_secret_key, chat->rng)) {
84438443
crypto_memunlock(chat->self_secret_key, sizeof(chat->self_secret_key));
84448444
return false;
84458445
}

toxcore/group_moderation_test.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ struct SanctionsListMod : ::testing::Test {
194194
ExtPublicKey pk;
195195
ExtSecretKey sk;
196196
Logger *log = logger_new();
197+
Test_Random rng;
197198
Test_Memory mem;
198199
Moderation mod{mem};
199200

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

204205
void SetUp() override
205206
{
206-
ASSERT_TRUE(create_extended_keypair(pk.data(), sk.data()));
207+
ASSERT_TRUE(create_extended_keypair(pk.data(), sk.data(), rng));
207208

208209
mod.log = log;
209210

0 commit comments

Comments
 (0)