Skip to content

Commit 57a5cc1

Browse files
committed
refactor: Use structs for extended public/secret keys.
1 parent 5dd9ee3 commit 57a5cc1

16 files changed

+261
-139
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ set(toxcore_SOURCES
223223
toxcore/ccompat.h
224224
toxcore/crypto_core.c
225225
toxcore/crypto_core.h
226+
toxcore/crypto_core_pack.c
227+
toxcore/crypto_core_pack.h
226228
toxcore/DHT.c
227229
toxcore/DHT.h
228230
toxcore/events/conference_connected.c

toxcore/BUILD.bazel

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,19 @@ cc_library(
175175
],
176176
)
177177

178+
cc_library(
179+
name = "crypto_core_pack",
180+
srcs = ["crypto_core_pack.c"],
181+
hdrs = ["crypto_core_pack.h"],
182+
visibility = ["//c-toxcore:__subpackages__"],
183+
deps = [
184+
":attributes",
185+
":bin_pack",
186+
":bin_unpack",
187+
":crypto_core",
188+
],
189+
)
190+
178191
cc_library(
179192
name = "crypto_core_test_util",
180193
testonly = True,
@@ -920,6 +933,7 @@ cc_library(
920933
":bin_unpack",
921934
":ccompat",
922935
":crypto_core",
936+
":crypto_core_pack",
923937
":forwarding",
924938
":friend_connection",
925939
":friend_requests",

toxcore/Makefile.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ libtoxcore_la_SOURCES = ../third_party/cmp/cmp.c \
6767
../toxcore/network.c \
6868
../toxcore/crypto_core.h \
6969
../toxcore/crypto_core.c \
70+
../toxcore/crypto_core_pack.h \
71+
../toxcore/crypto_core_pack.c \
7072
../toxcore/timed_auth.h \
7173
../toxcore/timed_auth.c \
7274
../toxcore/ping_array.h \

toxcore/Messenger.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ bool m_create_group_connection(Messenger *m, GC_Chat *chat)
404404
const int onion_friend_number = friend_conn_get_onion_friendnum(connection);
405405
Onion_Friend *onion_friend = onion_get_friend(m->onion_c, (uint16_t)onion_friend_number);
406406

407-
onion_friend_set_gc_public_key(onion_friend, get_chat_id(chat->chat_public_key));
407+
onion_friend_set_gc_public_key(onion_friend, get_chat_id(&chat->chat_public_key));
408408
onion_friend_set_gc_data(onion_friend, nullptr, 0);
409409

410410
return true;
@@ -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_PUBLIC_KEY_SIZE);
2598-
memcpy(announce.chat_public_key, get_chat_id(chat->chat_public_key), ENC_PUBLIC_KEY_SIZE);
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);
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/bin_pack.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ static bool null_skipper(cmp_ctx_t *ctx, size_t count)
3636
non_null()
3737
static size_t buf_writer(cmp_ctx_t *ctx, const void *data, size_t count)
3838
{
39+
const uint8_t *bytes = (const uint8_t *)data;
40+
3941
Bin_Pack *bp = (Bin_Pack *)ctx->buf;
4042
assert(bp != nullptr);
4143
const uint32_t new_pos = bp->bytes_pos + count;
@@ -48,7 +50,7 @@ static size_t buf_writer(cmp_ctx_t *ctx, const void *data, size_t count)
4850
// Buffer too small.
4951
return 0;
5052
}
51-
memcpy(&bp->bytes[bp->bytes_pos], data, count);
53+
memcpy(&bp->bytes[bp->bytes_pos], bytes, count);
5254
}
5355
bp->bytes_pos += count;
5456
return count;

toxcore/bin_unpack.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ struct Bin_Unpack {
2121
non_null()
2222
static bool buf_reader(cmp_ctx_t *ctx, void *data, size_t limit)
2323
{
24+
uint8_t *bytes = (uint8_t *)data;
25+
2426
Bin_Unpack *reader = (Bin_Unpack *)ctx->buf;
2527
assert(reader != nullptr && reader->bytes != nullptr);
2628
if (limit > reader->bytes_size) {
2729
return false;
2830
}
29-
memcpy(data, reader->bytes, limit);
31+
memcpy(bytes, reader->bytes, limit);
3032
reader->bytes += limit;
3133
reader->bytes_size -= limit;
3234
return true;

toxcore/crypto_core.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,43 +45,43 @@ 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(Extended_Public_Key *pk, Extended_Secret_Key *sk)
4949
{
5050
/* create signature key pair */
51-
crypto_sign_keypair(pk + ENC_PUBLIC_KEY_SIZE, sk + ENC_SECRET_KEY_SIZE);
51+
crypto_sign_keypair(pk->sig, sk->sig);
5252

5353
/* convert public signature key to public encryption key */
54-
const int res1 = crypto_sign_ed25519_pk_to_curve25519(pk, pk + ENC_PUBLIC_KEY_SIZE);
54+
const int res1 = crypto_sign_ed25519_pk_to_curve25519(pk->enc, pk->sig);
5555

5656
/* convert secret signature key to secret encryption key */
57-
const int res2 = crypto_sign_ed25519_sk_to_curve25519(sk, sk + ENC_SECRET_KEY_SIZE);
57+
const int res2 = crypto_sign_ed25519_sk_to_curve25519(sk->enc, sk->sig);
5858

5959
return res1 == 0 && res2 == 0;
6060
}
6161

62-
const uint8_t *get_enc_key(const uint8_t *key)
62+
const uint8_t *get_enc_key(const Extended_Public_Key *key)
6363
{
64-
return key;
64+
return key->enc;
6565
}
6666

67-
const uint8_t *get_sig_pk(const uint8_t *key)
67+
const uint8_t *get_sig_pk(const Extended_Public_Key *key)
6868
{
69-
return key + ENC_PUBLIC_KEY_SIZE;
69+
return key->sig;
7070
}
7171

72-
void set_sig_pk(uint8_t *key, const uint8_t *sig_pk)
72+
void set_sig_pk(Extended_Public_Key *key, const uint8_t *sig_pk)
7373
{
74-
memcpy(key + ENC_PUBLIC_KEY_SIZE, sig_pk, SIG_PUBLIC_KEY_SIZE);
74+
memcpy(key->sig, sig_pk, SIG_PUBLIC_KEY_SIZE);
7575
}
7676

77-
const uint8_t *get_sig_sk(const uint8_t *key)
77+
const uint8_t *get_sig_sk(const Extended_Secret_Key *key)
7878
{
79-
return key + ENC_SECRET_KEY_SIZE;
79+
return key->sig;
8080
}
8181

82-
const uint8_t *get_chat_id(const uint8_t *key)
82+
const uint8_t *get_chat_id(const Extended_Public_Key *key)
8383
{
84-
return key + ENC_PUBLIC_KEY_SIZE;
84+
return key->sig;
8585
}
8686

8787
#if !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)

toxcore/crypto_core.h

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,16 @@ void random_bytes(const Random *rng, uint8_t *bytes, size_t length);
324324
non_null()
325325
bool public_key_valid(const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]);
326326

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+
327337
/**
328338
* @brief Creates an extended keypair: curve25519 and ed25519 for encryption and signing
329339
* respectively. The Encryption keys are derived from the signature keys.
@@ -337,14 +347,14 @@ bool public_key_valid(const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]);
337347
* @retval true on success.
338348
*/
339349
non_null()
340-
bool create_extended_keypair(uint8_t pk[EXT_PUBLIC_KEY_SIZE], uint8_t sk[EXT_SECRET_KEY_SIZE]);
350+
bool create_extended_keypair(Extended_Public_Key *pk, Extended_Secret_Key *sk);
341351

342352
/** Functions for groupchat extended keys */
343-
non_null() const uint8_t *get_enc_key(const uint8_t *key);
344-
non_null() const uint8_t *get_sig_pk(const uint8_t *key);
345-
non_null() void set_sig_pk(uint8_t *key, const uint8_t *sig_pk);
346-
non_null() const uint8_t *get_sig_sk(const uint8_t *key);
347-
non_null() const uint8_t *get_chat_id(const uint8_t *key);
353+
non_null() const uint8_t *get_enc_key(const Extended_Public_Key *key);
354+
non_null() const uint8_t *get_sig_pk(const Extended_Public_Key *key);
355+
non_null() void set_sig_pk(Extended_Public_Key *key, const uint8_t *sig_pk);
356+
non_null() const uint8_t *get_sig_sk(const Extended_Secret_Key *key);
357+
non_null() const uint8_t *get_chat_id(const Extended_Public_Key *key);
348358

349359
/**
350360
* @brief Generate a new random keypair.

toxcore/crypto_core_pack.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* SPDX-License-Identifier: GPL-3.0-or-later
2+
* Copyright © 2016-2024 The TokTok team.
3+
* Copyright © 2013 Tox project.
4+
*/
5+
6+
#include "crypto_core_pack.h"
7+
8+
#include <string.h>
9+
10+
#include "bin_pack.h"
11+
#include "bin_unpack.h"
12+
#include "crypto_core.h"
13+
14+
bool pack_extended_public_key(const Extended_Public_Key *key, Bin_Pack *bp)
15+
{
16+
uint8_t ext_key[EXT_PUBLIC_KEY_SIZE];
17+
memcpy(ext_key, key->enc, CRYPTO_PUBLIC_KEY_SIZE);
18+
memcpy(&ext_key[CRYPTO_PUBLIC_KEY_SIZE], key->sig, CRYPTO_SIGN_PUBLIC_KEY_SIZE);
19+
20+
return bin_pack_bin(bp, ext_key, EXT_PUBLIC_KEY_SIZE);
21+
}
22+
23+
bool pack_extended_secret_key(const Extended_Secret_Key *key, Bin_Pack *bp)
24+
{
25+
uint8_t ext_key[EXT_SECRET_KEY_SIZE];
26+
memcpy(ext_key, key->enc, CRYPTO_SECRET_KEY_SIZE);
27+
memcpy(&ext_key[CRYPTO_SECRET_KEY_SIZE], key->sig, CRYPTO_SIGN_SECRET_KEY_SIZE);
28+
29+
return bin_pack_bin(bp, ext_key, EXT_SECRET_KEY_SIZE);
30+
}
31+
32+
bool unpack_extended_public_key(Extended_Public_Key *key, Bin_Unpack *bu)
33+
{
34+
uint8_t ext_key[EXT_PUBLIC_KEY_SIZE];
35+
36+
if (!bin_unpack_bin_fixed(bu, ext_key, EXT_PUBLIC_KEY_SIZE)) {
37+
return false;
38+
}
39+
40+
memcpy(key->enc, ext_key, CRYPTO_PUBLIC_KEY_SIZE);
41+
memcpy(key->sig, &ext_key[CRYPTO_PUBLIC_KEY_SIZE], CRYPTO_SIGN_PUBLIC_KEY_SIZE);
42+
43+
return true;
44+
}
45+
46+
bool unpack_extended_secret_key(Extended_Secret_Key *key, Bin_Unpack *bu)
47+
{
48+
uint8_t ext_key[EXT_SECRET_KEY_SIZE];
49+
50+
if (!bin_unpack_bin_fixed(bu, ext_key, EXT_SECRET_KEY_SIZE)) {
51+
return false;
52+
}
53+
54+
memcpy(key->enc, ext_key, CRYPTO_SECRET_KEY_SIZE);
55+
memcpy(key->sig, &ext_key[CRYPTO_SECRET_KEY_SIZE], CRYPTO_SIGN_SECRET_KEY_SIZE);
56+
57+
return true;
58+
}

toxcore/crypto_core_pack.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* SPDX-License-Identifier: GPL-3.0-or-later
2+
* Copyright © 2016-2024 The TokTok team.
3+
* Copyright © 2013 Tox project.
4+
*/
5+
6+
#ifndef C_TOXCORE_TOXCORE_CRYPTO_CORE_PACK_H
7+
#define C_TOXCORE_TOXCORE_CRYPTO_CORE_PACK_H
8+
9+
#include <stdbool.h>
10+
#include <stddef.h>
11+
#include <stdint.h>
12+
13+
#include "attributes.h"
14+
#include "bin_pack.h"
15+
#include "bin_unpack.h"
16+
#include "crypto_core.h"
17+
18+
#ifdef __cplusplus
19+
extern "C" {
20+
#endif
21+
22+
non_null() bool pack_extended_public_key(const Extended_Public_Key *key, Bin_Pack *bp);
23+
non_null() bool pack_extended_secret_key(const Extended_Secret_Key *key, Bin_Pack *bp);
24+
non_null() bool unpack_extended_public_key(Extended_Public_Key *key, Bin_Unpack *bu);
25+
non_null() bool unpack_extended_secret_key(Extended_Secret_Key *key, Bin_Unpack *bu);
26+
27+
#ifdef __cplusplus
28+
} /* extern "C" */
29+
#endif
30+
31+
#endif /* C_TOXCORE_TOXCORE_CRYPTO_CORE_PACK_H */

toxcore/crypto_core_test.cc

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ namespace {
1414
using HmacKey = std::array<uint8_t, CRYPTO_HMAC_KEY_SIZE>;
1515
using Hmac = std::array<uint8_t, CRYPTO_HMAC_SIZE>;
1616
using SecretKey = std::array<uint8_t, CRYPTO_SECRET_KEY_SIZE>;
17-
using ExtPublicKey = std::array<uint8_t, EXT_PUBLIC_KEY_SIZE>;
18-
using ExtSecretKey = std::array<uint8_t, EXT_SECRET_KEY_SIZE>;
1917
using Signature = std::array<uint8_t, CRYPTO_SIGNATURE_SIZE>;
2018
using Nonce = std::array<uint8_t, CRYPTO_NONCE_SIZE>;
2119

@@ -72,10 +70,10 @@ TEST(CryptoCore, Signatures)
7270
{
7371
Test_Random rng;
7472

75-
ExtPublicKey pk;
76-
ExtSecretKey sk;
73+
Extended_Public_Key pk;
74+
Extended_Secret_Key sk;
7775

78-
EXPECT_TRUE(create_extended_keypair(pk.data(), sk.data()));
76+
EXPECT_TRUE(create_extended_keypair(&pk, &sk));
7977

8078
std::vector<uint8_t> message{0};
8179
message.clear();
@@ -84,9 +82,9 @@ TEST(CryptoCore, Signatures)
8482
for (uint8_t i = 0; i < 100; ++i) {
8583
Signature signature;
8684
EXPECT_TRUE(crypto_signature_create(
87-
signature.data(), message.data(), message.size(), get_sig_sk(sk.data())));
85+
signature.data(), message.data(), message.size(), get_sig_sk(&sk)));
8886
EXPECT_TRUE(crypto_signature_verify(
89-
signature.data(), message.data(), message.size(), get_sig_pk(pk.data())));
87+
signature.data(), message.data(), message.size(), get_sig_pk(&pk)));
9088

9189
message.push_back(random_u08(rng));
9290
}

0 commit comments

Comments
 (0)