Skip to content

Commit 1e1cf1e

Browse files
committed
refactor: Use structs for extended public/secret keys.
1 parent 623e3ee commit 1e1cf1e

14 files changed

+266
-137
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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,20 @@ 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+
":ccompat",
188+
":crypto_core",
189+
],
190+
)
191+
178192
cc_library(
179193
name = "crypto_core_test_util",
180194
testonly = True,
@@ -920,6 +934,7 @@ cc_library(
920934
":bin_unpack",
921935
":ccompat",
922936
":crypto_core",
937+
":crypto_core_pack",
923938
":forwarding",
924939
":friend_connection",
925940
":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/crypto_core.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,45 +45,46 @@ 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], const Random *rng)
48+
bool create_extended_keypair(Extended_Public_Key *pk, Extended_Secret_Key *sk, const Random *rng)
4949
{
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 + ENC_PUBLIC_KEY_SIZE, sk + ENC_SECRET_KEY_SIZE, seed);
53+
crypto_sign_seed_keypair(pk->sig, sk->sig, seed);
54+
crypto_memzero(seed, crypto_sign_SEEDBYTES);
5455

5556
/* convert public signature key to public encryption key */
56-
const int res1 = crypto_sign_ed25519_pk_to_curve25519(pk, pk + ENC_PUBLIC_KEY_SIZE);
57+
const int res1 = crypto_sign_ed25519_pk_to_curve25519(pk->enc, pk->sig);
5758

5859
/* convert secret signature key to secret encryption key */
59-
const int res2 = crypto_sign_ed25519_sk_to_curve25519(sk, sk + ENC_SECRET_KEY_SIZE);
60+
const int res2 = crypto_sign_ed25519_sk_to_curve25519(sk->enc, sk->sig);
6061

6162
return res1 == 0 && res2 == 0;
6263
}
6364

64-
const uint8_t *get_enc_key(const uint8_t *key)
65+
const uint8_t *get_enc_key(const Extended_Public_Key *key)
6566
{
66-
return key;
67+
return key->enc;
6768
}
6869

69-
const uint8_t *get_sig_pk(const uint8_t *key)
70+
const uint8_t *get_sig_pk(const Extended_Public_Key *key)
7071
{
71-
return key + ENC_PUBLIC_KEY_SIZE;
72+
return key->sig;
7273
}
7374

74-
void set_sig_pk(uint8_t *key, const uint8_t *sig_pk)
75+
void set_sig_pk(Extended_Public_Key *key, const uint8_t *sig_pk)
7576
{
76-
memcpy(key + ENC_PUBLIC_KEY_SIZE, sig_pk, SIG_PUBLIC_KEY_SIZE);
77+
memcpy(key->sig, sig_pk, SIG_PUBLIC_KEY_SIZE);
7778
}
7879

79-
const uint8_t *get_sig_sk(const uint8_t *key)
80+
const uint8_t *get_sig_sk(const Extended_Secret_Key *key)
8081
{
81-
return key + ENC_SECRET_KEY_SIZE;
82+
return key->sig;
8283
}
8384

84-
const uint8_t *get_chat_id(const uint8_t *key)
85+
const uint8_t *get_chat_id(const Extended_Public_Key *key)
8586
{
86-
return key + ENC_PUBLIC_KEY_SIZE;
87+
return key->sig;
8788
}
8889

8990
#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.
@@ -338,14 +348,14 @@ bool public_key_valid(const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]);
338348
* @retval true on success.
339349
*/
340350
non_null()
341-
bool create_extended_keypair(uint8_t pk[EXT_PUBLIC_KEY_SIZE], uint8_t sk[EXT_SECRET_KEY_SIZE], const Random *rng);
351+
bool create_extended_keypair(Extended_Public_Key *pk, Extended_Secret_Key *sk, const Random *rng);
342352

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

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

toxcore/crypto_core_pack.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 "ccompat.h"
13+
#include "crypto_core.h"
14+
15+
static_assert(EXT_PUBLIC_KEY_SIZE == CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_SIGN_PUBLIC_KEY_SIZE,
16+
"extended public key size is not the sum of the encryption and sign public key sizes");
17+
static_assert(EXT_SECRET_KEY_SIZE == CRYPTO_SECRET_KEY_SIZE + CRYPTO_SIGN_SECRET_KEY_SIZE,
18+
"extended secret key size is not the sum of the encryption and sign secret key sizes");
19+
20+
bool pack_extended_public_key(const Extended_Public_Key *key, Bin_Pack *bp)
21+
{
22+
uint8_t ext_key[EXT_PUBLIC_KEY_SIZE];
23+
memcpy(ext_key, key->enc, CRYPTO_PUBLIC_KEY_SIZE);
24+
memcpy(&ext_key[CRYPTO_PUBLIC_KEY_SIZE], key->sig, CRYPTO_SIGN_PUBLIC_KEY_SIZE);
25+
26+
return bin_pack_bin(bp, ext_key, EXT_PUBLIC_KEY_SIZE);
27+
}
28+
29+
bool pack_extended_secret_key(const Extended_Secret_Key *key, Bin_Pack *bp)
30+
{
31+
uint8_t ext_key[EXT_SECRET_KEY_SIZE];
32+
memcpy(ext_key, key->enc, CRYPTO_SECRET_KEY_SIZE);
33+
memcpy(&ext_key[CRYPTO_SECRET_KEY_SIZE], key->sig, CRYPTO_SIGN_SECRET_KEY_SIZE);
34+
35+
const bool result = bin_pack_bin(bp, ext_key, EXT_SECRET_KEY_SIZE);
36+
crypto_memzero(ext_key, EXT_SECRET_KEY_SIZE);
37+
return result;
38+
}
39+
40+
bool unpack_extended_public_key(Extended_Public_Key *key, Bin_Unpack *bu)
41+
{
42+
uint8_t ext_key[EXT_PUBLIC_KEY_SIZE];
43+
44+
if (!bin_unpack_bin_fixed(bu, ext_key, EXT_PUBLIC_KEY_SIZE)) {
45+
return false;
46+
}
47+
48+
memcpy(key->enc, ext_key, CRYPTO_PUBLIC_KEY_SIZE);
49+
memcpy(key->sig, &ext_key[CRYPTO_PUBLIC_KEY_SIZE], CRYPTO_SIGN_PUBLIC_KEY_SIZE);
50+
51+
return true;
52+
}
53+
54+
bool unpack_extended_secret_key(Extended_Secret_Key *key, Bin_Unpack *bu)
55+
{
56+
uint8_t ext_key[EXT_SECRET_KEY_SIZE];
57+
58+
if (!bin_unpack_bin_fixed(bu, ext_key, EXT_SECRET_KEY_SIZE)) {
59+
return false;
60+
}
61+
62+
memcpy(key->enc, ext_key, CRYPTO_SECRET_KEY_SIZE);
63+
memcpy(key->sig, &ext_key[CRYPTO_SECRET_KEY_SIZE], CRYPTO_SIGN_SECRET_KEY_SIZE);
64+
crypto_memzero(ext_key, EXT_SECRET_KEY_SIZE);
65+
66+
return true;
67+
}

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(), rng));
76+
EXPECT_TRUE(create_extended_keypair(&pk, &sk, rng));
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)