Skip to content

Commit dd18ec3

Browse files
committed
refactor: Use structs for public key and secret key in NGC.
1 parent ed2b60c commit dd18ec3

20 files changed

+386
-387
lines changed

toxcore/Messenger.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -2594,8 +2594,8 @@ static bool self_announce_group(const Messenger *m, GC_Chat *chat, Onion_Friend
25942594
memcpy(&announce.base_announce.ip_port, &chat->self_ip_port, sizeof(IP_Port));
25952595
}
25962596

2597-
memcpy(announce.base_announce.peer_public_key, chat->self_public_key.enc, ENC_PUBLIC_KEY_SIZE);
2598-
memcpy(announce.chat_public_key, get_chat_id(&chat->chat_public_key), ENC_PUBLIC_KEY_SIZE);
2597+
announce.base_announce.peer_public_key = chat->self_public_key.enc;
2598+
announce.chat_public_key = chat->chat_public_key.enc;
25992599

26002600
uint8_t gc_data[GCA_MAX_DATA_LENGTH];
26012601
const int length = gca_pack_public_announce(m->log, gc_data, GCA_MAX_DATA_LENGTH, &announce);

toxcore/crypto_core.c

+12-12
Original file line numberDiff line numberDiff line change
@@ -50,41 +50,41 @@ bool create_extended_keypair(Extended_Public_Key *pk, Extended_Secret_Key *sk, c
5050
/* create signature key pair */
5151
uint8_t seed[crypto_sign_SEEDBYTES];
5252
random_bytes(rng, seed, crypto_sign_SEEDBYTES);
53-
crypto_sign_seed_keypair(pk->sig, sk->sig, seed);
53+
crypto_sign_seed_keypair(pk->sig.data, sk->sig.data, seed);
5454
crypto_memzero(seed, crypto_sign_SEEDBYTES);
5555

5656
/* convert public signature key to public encryption key */
57-
const int res1 = crypto_sign_ed25519_pk_to_curve25519(pk->enc, pk->sig);
57+
const int res1 = crypto_sign_ed25519_pk_to_curve25519(pk->enc.data, pk->sig.data);
5858

5959
/* convert secret signature key to secret encryption key */
60-
const int res2 = crypto_sign_ed25519_sk_to_curve25519(sk->enc, sk->sig);
60+
const int res2 = crypto_sign_ed25519_sk_to_curve25519(sk->enc.data, sk->sig.data);
6161

6262
return res1 == 0 && res2 == 0;
6363
}
6464

6565
const uint8_t *get_enc_key(const Extended_Public_Key *key)
6666
{
67-
return key->enc;
67+
return key->enc.data;
6868
}
6969

7070
const uint8_t *get_sig_pk(const Extended_Public_Key *key)
7171
{
72-
return key->sig;
72+
return key->sig.data;
7373
}
7474

7575
void set_sig_pk(Extended_Public_Key *key, const uint8_t *sig_pk)
7676
{
77-
memcpy(key->sig, sig_pk, SIG_PUBLIC_KEY_SIZE);
77+
memcpy(key->sig.data, sig_pk, SIG_PUBLIC_KEY_SIZE);
7878
}
7979

8080
const uint8_t *get_sig_sk(const Extended_Secret_Key *key)
8181
{
82-
return key->sig;
82+
return key->sig.data;
8383
}
8484

8585
const uint8_t *get_chat_id(const Extended_Public_Key *key)
8686
{
87-
return key->sig;
87+
return key->sig.data;
8888
}
8989

9090
#if !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
@@ -210,16 +210,16 @@ uint32_t random_range_u32(const Random *rng, uint32_t upper_bound)
210210

211211
bool crypto_signature_create(uint8_t signature[CRYPTO_SIGNATURE_SIZE],
212212
const uint8_t *message, uint64_t message_length,
213-
const uint8_t secret_key[SIG_SECRET_KEY_SIZE])
213+
const Sign_Secret_Key *secret_key)
214214
{
215-
return crypto_sign_detached(signature, nullptr, message, message_length, secret_key) == 0;
215+
return crypto_sign_detached(signature, nullptr, message, message_length, secret_key->data) == 0;
216216
}
217217

218218
bool crypto_signature_verify(const uint8_t signature[CRYPTO_SIGNATURE_SIZE],
219219
const uint8_t *message, uint64_t message_length,
220-
const uint8_t public_key[SIG_PUBLIC_KEY_SIZE])
220+
const Sign_Public_Key *public_key)
221221
{
222-
return crypto_sign_verify_detached(signature, message, message_length, public_key) == 0;
222+
return crypto_sign_verify_detached(signature, message, message_length, public_key->data) == 0;
223223
}
224224

225225
bool public_key_valid(const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE])

toxcore/crypto_core.h

+28-12
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,32 @@ const Random *os_random(void);
160160
*/
161161
#define CRYPTO_HMAC_KEY_SIZE 32
162162

163+
typedef struct Public_Key {
164+
uint8_t data[CRYPTO_PUBLIC_KEY_SIZE];
165+
} Public_Key;
166+
167+
typedef struct Secret_Key {
168+
uint8_t data[CRYPTO_SECRET_KEY_SIZE];
169+
} Secret_Key;
170+
171+
typedef struct Sign_Public_Key {
172+
uint8_t data[CRYPTO_SIGN_PUBLIC_KEY_SIZE];
173+
} Sign_Public_Key;
174+
175+
typedef struct Sign_Secret_Key {
176+
uint8_t data[CRYPTO_SIGN_SECRET_KEY_SIZE];
177+
} Sign_Secret_Key;
178+
179+
typedef struct Extended_Public_Key {
180+
Public_Key enc;
181+
Sign_Public_Key sig;
182+
} Extended_Public_Key;
183+
184+
typedef struct Extended_Secret_Key {
185+
Secret_Key enc;
186+
Sign_Secret_Key sig;
187+
} Extended_Secret_Key;
188+
163189
/**
164190
* @brief A `bzero`-like function which won't be optimised away by the compiler.
165191
*
@@ -285,7 +311,7 @@ uint32_t random_range_u32(const Random *rng, uint32_t upper_bound);
285311
non_null()
286312
bool crypto_signature_create(uint8_t signature[CRYPTO_SIGNATURE_SIZE],
287313
const uint8_t *message, uint64_t message_length,
288-
const uint8_t secret_key[SIG_SECRET_KEY_SIZE]);
314+
const Sign_Secret_Key *secret_key);
289315

290316
/** @brief Verifies that the given signature was produced by a given message and public key.
291317
*
@@ -300,7 +326,7 @@ bool crypto_signature_create(uint8_t signature[CRYPTO_SIGNATURE_SIZE],
300326
non_null()
301327
bool crypto_signature_verify(const uint8_t signature[CRYPTO_SIGNATURE_SIZE],
302328
const uint8_t *message, uint64_t message_length,
303-
const uint8_t public_key[SIG_PUBLIC_KEY_SIZE]);
329+
const Sign_Public_Key *public_key);
304330

305331
/**
306332
* @brief Fill the given nonce with random bytes.
@@ -324,16 +350,6 @@ void random_bytes(const Random *rng, uint8_t *bytes, size_t length);
324350
non_null()
325351
bool public_key_valid(const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]);
326352

327-
typedef struct Extended_Public_Key {
328-
uint8_t enc[CRYPTO_PUBLIC_KEY_SIZE];
329-
uint8_t sig[CRYPTO_SIGN_PUBLIC_KEY_SIZE];
330-
} Extended_Public_Key;
331-
332-
typedef struct Extended_Secret_Key {
333-
uint8_t enc[CRYPTO_SECRET_KEY_SIZE];
334-
uint8_t sig[CRYPTO_SIGN_SECRET_KEY_SIZE];
335-
} Extended_Secret_Key;
336-
337353
/**
338354
* @brief Creates an extended keypair: curve25519 and ed25519 for encryption and signing
339355
* respectively. The Encryption keys are derived from the signature keys.

toxcore/crypto_core_pack.c

+10-10
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@
1515
bool pack_extended_public_key(const Extended_Public_Key *key, Bin_Pack *bp)
1616
{
1717
uint8_t ext_key[EXT_PUBLIC_KEY_SIZE];
18-
static_assert(sizeof(ext_key) == sizeof(key->enc) + sizeof(key->sig),
18+
static_assert(sizeof(ext_key) == sizeof(key->enc.data) + sizeof(key->sig.data),
1919
"extended secret key size is not the sum of the encryption and sign secret key sizes");
20-
memcpy(ext_key, key->enc, sizeof(key->enc));
21-
memcpy(&ext_key[sizeof(key->enc)], key->sig, sizeof(key->sig));
20+
memcpy(ext_key, key->enc.data, sizeof(key->enc.data));
21+
memcpy(&ext_key[sizeof(key->enc.data)], key->sig.data, sizeof(key->sig.data));
2222

2323
return bin_pack_bin(bp, ext_key, sizeof(ext_key));
2424
}
2525

2626
bool pack_extended_secret_key(const Extended_Secret_Key *key, Bin_Pack *bp)
2727
{
2828
uint8_t ext_key[EXT_SECRET_KEY_SIZE];
29-
static_assert(sizeof(ext_key) == sizeof(key->enc) + sizeof(key->sig),
29+
static_assert(sizeof(ext_key) == sizeof(key->enc.data) + sizeof(key->sig.data),
3030
"extended secret key size is not the sum of the encryption and sign secret key sizes");
31-
memcpy(ext_key, key->enc, sizeof(key->enc));
32-
memcpy(&ext_key[sizeof(key->enc)], key->sig, sizeof(key->sig));
31+
memcpy(ext_key, key->enc.data, sizeof(key->enc.data));
32+
memcpy(&ext_key[sizeof(key->enc.data)], key->sig.data, sizeof(key->sig.data));
3333

3434
const bool result = bin_pack_bin(bp, ext_key, sizeof(ext_key));
3535
crypto_memzero(ext_key, sizeof(ext_key));
@@ -44,8 +44,8 @@ bool unpack_extended_public_key(Extended_Public_Key *key, Bin_Unpack *bu)
4444
return false;
4545
}
4646

47-
memcpy(key->enc, ext_key, sizeof(key->enc));
48-
memcpy(key->sig, &ext_key[sizeof(key->enc)], sizeof(key->sig));
47+
memcpy(key->enc.data, ext_key, sizeof(key->enc.data));
48+
memcpy(key->sig.data, &ext_key[sizeof(key->enc.data)], sizeof(key->sig.data));
4949

5050
return true;
5151
}
@@ -58,8 +58,8 @@ bool unpack_extended_secret_key(Extended_Secret_Key *key, Bin_Unpack *bu)
5858
return false;
5959
}
6060

61-
memcpy(key->enc, ext_key, sizeof(key->enc));
62-
memcpy(key->sig, &ext_key[sizeof(key->enc)], sizeof(key->sig));
61+
memcpy(key->enc.data, ext_key, sizeof(key->enc.data));
62+
memcpy(key->sig.data, &ext_key[sizeof(key->enc.data)], sizeof(key->sig.data));
6363
crypto_memzero(ext_key, sizeof(ext_key));
6464

6565
return true;

toxcore/crypto_core_test.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ TEST(CryptoCore, Signatures)
8181
// Try a few different sizes, including empty 0 length message.
8282
for (uint8_t i = 0; i < 100; ++i) {
8383
Signature signature;
84-
EXPECT_TRUE(crypto_signature_create(
85-
signature.data(), message.data(), message.size(), get_sig_sk(&sk)));
86-
EXPECT_TRUE(crypto_signature_verify(
87-
signature.data(), message.data(), message.size(), get_sig_pk(&pk)));
84+
EXPECT_TRUE(
85+
crypto_signature_create(signature.data(), message.data(), message.size(), &sk.sig));
86+
EXPECT_TRUE(
87+
crypto_signature_verify(signature.data(), message.data(), message.size(), &pk.sig));
8888

8989
message.push_back(random_u08(rng));
9090
}

toxcore/group_announce.c

+12-12
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ static void remove_announces(GC_Announces_List *gc_announces_list, GC_Announces
4444
* Returns null if no announce is found.
4545
*/
4646
non_null()
47-
static GC_Announces *get_announces_by_chat_id(const GC_Announces_List *gc_announces_list, const uint8_t *chat_id)
47+
static GC_Announces *get_announces_by_chat_id(const GC_Announces_List *gc_announces_list, const Public_Key *chat_id)
4848
{
4949
GC_Announces *announces = gc_announces_list->root_announces;
5050

5151
while (announces != nullptr) {
52-
if (memcmp(announces->chat_id, chat_id, CHAT_ID_SIZE) == 0) {
52+
if (memcmp(&announces->chat_id, chat_id, CHAT_ID_SIZE) == 0) {
5353
return announces;
5454
}
5555

@@ -60,7 +60,7 @@ static GC_Announces *get_announces_by_chat_id(const GC_Announces_List *gc_announ
6060
}
6161

6262
int gca_get_announces(const GC_Announces_List *gc_announces_list, GC_Announce *gc_announces, uint8_t max_nodes,
63-
const uint8_t *chat_id, const uint8_t *except_public_key)
63+
const Public_Key *chat_id, const Public_Key *except_public_key)
6464
{
6565
if (gc_announces == nullptr || gc_announces_list == nullptr || chat_id == nullptr || max_nodes == 0
6666
|| except_public_key == nullptr) {
@@ -78,15 +78,15 @@ int gca_get_announces(const GC_Announces_List *gc_announces_list, GC_Announce *g
7878
for (size_t i = 0; i < announces->index && i < GCA_MAX_SAVED_ANNOUNCES_PER_GC && added_count < max_nodes; ++i) {
7979
const size_t index = i % GCA_MAX_SAVED_ANNOUNCES_PER_GC;
8080

81-
if (memcmp(except_public_key, announces->peer_announces[index].base_announce.peer_public_key,
81+
if (memcmp(except_public_key, &announces->peer_announces[index].base_announce.peer_public_key,
8282
ENC_PUBLIC_KEY_SIZE) == 0) {
8383
continue;
8484
}
8585

8686
bool already_added = false;
8787

8888
for (size_t j = 0; j < added_count; ++j) {
89-
if (memcmp(gc_announces[j].peer_public_key, announces->peer_announces[index].base_announce.peer_public_key,
89+
if (memcmp(&gc_announces[j].peer_public_key, &announces->peer_announces[index].base_announce.peer_public_key,
9090
ENC_PUBLIC_KEY_SIZE) == 0) {
9191
already_added = true;
9292
break;
@@ -125,7 +125,7 @@ int gca_pack_announce(const Logger *log, uint8_t *data, uint16_t length, const G
125125
}
126126

127127
uint16_t offset = 0;
128-
memcpy(data + offset, announce->peer_public_key, ENC_PUBLIC_KEY_SIZE);
128+
memcpy(data + offset, announce->peer_public_key.data, ENC_PUBLIC_KEY_SIZE);
129129
offset += ENC_PUBLIC_KEY_SIZE;
130130

131131
data[offset] = announce->ip_port_is_set ? 1 : 0;
@@ -186,7 +186,7 @@ static int gca_unpack_announce(const Logger *log, const uint8_t *data, uint16_t
186186
}
187187

188188
uint16_t offset = 0;
189-
memcpy(announce->peer_public_key, data + offset, ENC_PUBLIC_KEY_SIZE);
189+
memcpy(announce->peer_public_key.data, data + offset, ENC_PUBLIC_KEY_SIZE);
190190
offset += ENC_PUBLIC_KEY_SIZE;
191191

192192
net_unpack_bool(&data[offset], &announce->ip_port_is_set);
@@ -233,7 +233,7 @@ int gca_pack_public_announce(const Logger *log, uint8_t *data, uint16_t length,
233233
return -1;
234234
}
235235

236-
memcpy(data, public_announce->chat_public_key, CHAT_ID_SIZE);
236+
memcpy(data, public_announce->chat_public_key.data, CHAT_ID_SIZE);
237237

238238
const int packed_size = gca_pack_announce(log, data + CHAT_ID_SIZE, length - CHAT_ID_SIZE,
239239
&public_announce->base_announce);
@@ -264,7 +264,7 @@ int gca_unpack_public_announce(const Logger *log, const uint8_t *data, uint16_t
264264
return -1;
265265
}
266266

267-
memcpy(public_announce->chat_public_key, data, CHAT_ID_SIZE);
267+
memcpy(public_announce->chat_public_key.data, data, CHAT_ID_SIZE);
268268

269269
const int base_announce_size = gca_unpack_announce(log, data + ENC_PUBLIC_KEY_SIZE, length - ENC_PUBLIC_KEY_SIZE,
270270
&public_announce->base_announce);
@@ -361,7 +361,7 @@ static GC_Announces *gca_new_announces(
361361

362362
announces->next_announce = gc_announces_list->root_announces;
363363
gc_announces_list->root_announces = announces;
364-
memcpy(announces->chat_id, public_announce->chat_public_key, CHAT_ID_SIZE);
364+
announces->chat_id = public_announce->chat_public_key;
365365

366366
return announces;
367367
}
@@ -373,7 +373,7 @@ GC_Peer_Announce *gca_add_announce(const Mono_Time *mono_time, GC_Announces_List
373373
return nullptr;
374374
}
375375

376-
GC_Announces *announces = get_announces_by_chat_id(gc_announces_list, public_announce->chat_public_key);
376+
GC_Announces *announces = get_announces_by_chat_id(gc_announces_list, &public_announce->chat_public_key);
377377

378378
// No entry for this chat_id exists so we create one
379379
if (announces == nullptr) {
@@ -464,7 +464,7 @@ void do_gca(const Mono_Time *mono_time, GC_Announces_List *gc_announces_list)
464464
}
465465
}
466466

467-
void cleanup_gca(GC_Announces_List *gc_announces_list, const uint8_t *chat_id)
467+
void cleanup_gca(GC_Announces_List *gc_announces_list, const Public_Key *chat_id)
468468
{
469469
if (gc_announces_list == nullptr || chat_id == nullptr) {
470470
return;

toxcore/group_announce.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ struct GC_Announce {
5050
uint8_t tcp_relays_count;
5151
bool ip_port_is_set;
5252
IP_Port ip_port;
53-
uint8_t peer_public_key[ENC_PUBLIC_KEY_SIZE];
53+
Public_Key peer_public_key;
5454
};
5555

5656
/* Peer announce for specific group. */
@@ -62,12 +62,12 @@ struct GC_Peer_Announce {
6262
/* Used for announces in public groups. */
6363
struct GC_Public_Announce {
6464
GC_Announce base_announce;
65-
uint8_t chat_public_key[ENC_PUBLIC_KEY_SIZE];
65+
Public_Key chat_public_key;
6666
};
6767

6868
/* A linked list that holds all announces for a particular group. */
6969
struct GC_Announces {
70-
uint8_t chat_id[CHAT_ID_SIZE];
70+
Public_Key chat_id;
7171
uint64_t index;
7272
uint64_t last_announce_received_timestamp;
7373

@@ -109,7 +109,7 @@ void do_gca(const Mono_Time *mono_time, GC_Announces_List *gc_announces_list);
109109
* @param chat_id The chat ID that designates the entry we want to remove.
110110
*/
111111
non_null()
112-
void cleanup_gca(GC_Announces_List *gc_announces_list, const uint8_t *chat_id);
112+
void cleanup_gca(GC_Announces_List *gc_announces_list, const Public_Key *chat_id);
113113

114114
/** @brief Puts a set of announces from the announces list in supplied list.
115115
*
@@ -124,7 +124,7 @@ void cleanup_gca(GC_Announces_List *gc_announces_list, const uint8_t *chat_id);
124124
*/
125125
non_null()
126126
int gca_get_announces(const GC_Announces_List *gc_announces_list, GC_Announce *gc_announces, uint8_t max_nodes,
127-
const uint8_t *chat_id, const uint8_t *except_public_key);
127+
const Public_Key *chat_id, const Public_Key *except_public_key);
128128

129129
/** @brief Adds a public_announce to list of announces.
130130
*

toxcore/group_announce_fuzz_test.cc

+14-5
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,25 @@ void TestDoGca(Fuzz_Data &input)
8787
CONSUME1_OR_RETURN(const uint8_t, max_nodes, input);
8888
// Always allocate at least something to avoid passing nullptr to functions below.
8989
std::vector<GC_Announce> gc_announces(max_nodes + 1);
90-
CONSUME_OR_RETURN(const uint8_t *chat_id, input, CHAT_ID_SIZE);
91-
CONSUME_OR_RETURN(const uint8_t *except_public_key, input, ENC_PUBLIC_KEY_SIZE);
90+
91+
CONSUME_OR_RETURN(const uint8_t *chat_id_data, input, CHAT_ID_SIZE);
92+
Public_Key chat_id;
93+
memcpy(chat_id.data, chat_id_data, CHAT_ID_SIZE);
94+
95+
CONSUME_OR_RETURN(const uint8_t *except_public_key_data, input, ENC_PUBLIC_KEY_SIZE);
96+
Public_Key except_public_key;
97+
memcpy(except_public_key.data, except_public_key_data, ENC_PUBLIC_KEY_SIZE);
98+
9299
gca_get_announces(
93-
gca.get(), gc_announces.data(), max_nodes, chat_id, except_public_key);
100+
gca.get(), gc_announces.data(), max_nodes, &chat_id, &except_public_key);
94101
break;
95102
}
96103
case 3: {
97104
// Remove a chat.
98-
CONSUME_OR_RETURN(const uint8_t *chat_id, input, CHAT_ID_SIZE);
99-
cleanup_gca(gca.get(), chat_id);
105+
CONSUME_OR_RETURN(const uint8_t *chat_id_data, input, CHAT_ID_SIZE);
106+
Public_Key chat_id;
107+
memcpy(chat_id.data, chat_id_data, CHAT_ID_SIZE);
108+
cleanup_gca(gca.get(), &chat_id);
100109
break;
101110
}
102111
}

0 commit comments

Comments
 (0)