Skip to content

Commit df2211e

Browse files
committed
refactor: Use tox memory allocator for temporary buffers in crypto.
1 parent ac81287 commit df2211e

28 files changed

+232
-187
lines changed

auto_tests/TCP_test.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ static void test_basic(void)
102102
random_nonce(rng, handshake + CRYPTO_PUBLIC_KEY_SIZE);
103103

104104
// Encrypting handshake
105-
int ret = encrypt_data(self_public_key, f_secret_key, handshake + CRYPTO_PUBLIC_KEY_SIZE, handshake_plain,
105+
int ret = encrypt_data(mem, self_public_key, f_secret_key, handshake + CRYPTO_PUBLIC_KEY_SIZE, handshake_plain,
106106
TCP_HANDSHAKE_PLAIN_SIZE, handshake + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE);
107107
ck_assert_msg(ret == TCP_CLIENT_HANDSHAKE_SIZE - (CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE),
108108
"encrypt_data() call failed.");
@@ -128,7 +128,7 @@ static void test_basic(void)
128128
uint8_t response_plain[TCP_HANDSHAKE_PLAIN_SIZE];
129129
ck_assert_msg(net_recv(ns, logger, sock, response, TCP_SERVER_HANDSHAKE_SIZE, &localhost) == TCP_SERVER_HANDSHAKE_SIZE,
130130
"Could/did not receive a server response to the initial handshake.");
131-
ret = decrypt_data(self_public_key, f_secret_key, response, response + CRYPTO_NONCE_SIZE,
131+
ret = decrypt_data(mem, self_public_key, f_secret_key, response, response + CRYPTO_NONCE_SIZE,
132132
TCP_SERVER_HANDSHAKE_SIZE - CRYPTO_NONCE_SIZE, response_plain);
133133
ck_assert_msg(ret == TCP_HANDSHAKE_PLAIN_SIZE, "Failed to decrypt handshake response.");
134134
uint8_t f_nonce_r[CRYPTO_NONCE_SIZE];
@@ -143,7 +143,7 @@ static void test_basic(void)
143143
uint8_t r_req[2 + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE];
144144
uint16_t size = 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE;
145145
size = net_htons(size);
146-
encrypt_data_symmetric(f_shared_key, f_nonce, r_req_p, 1 + CRYPTO_PUBLIC_KEY_SIZE, r_req + 2);
146+
encrypt_data_symmetric(mem, f_shared_key, f_nonce, r_req_p, 1 + CRYPTO_PUBLIC_KEY_SIZE, r_req + 2);
147147
increment_nonce(f_nonce);
148148
memcpy(r_req, &size, 2);
149149

@@ -174,7 +174,7 @@ static void test_basic(void)
174174
"Wrong packet size for request response.");
175175

176176
uint8_t packet_resp_plain[4096];
177-
ret = decrypt_data_symmetric(f_shared_key, f_nonce_r, packet_resp + 2, recv_data_len - 2, packet_resp_plain);
177+
ret = decrypt_data_symmetric(mem, f_shared_key, f_nonce_r, packet_resp + 2, recv_data_len - 2, packet_resp_plain);
178178
ck_assert_msg(ret != -1, "Failed to decrypt the TCP server's response.");
179179
increment_nonce(f_nonce_r);
180180

@@ -228,7 +228,7 @@ static struct sec_TCP_con *new_tcp_con(const Logger *logger, const Memory *mem,
228228
memcpy(handshake, sec_c->public_key, CRYPTO_PUBLIC_KEY_SIZE);
229229
random_nonce(rng, handshake + CRYPTO_PUBLIC_KEY_SIZE);
230230

231-
int ret = encrypt_data(tcp_server_public_key(tcp_s), f_secret_key, handshake + CRYPTO_PUBLIC_KEY_SIZE, handshake_plain,
231+
int ret = encrypt_data(mem, tcp_server_public_key(tcp_s), f_secret_key, handshake + CRYPTO_PUBLIC_KEY_SIZE, handshake_plain,
232232
TCP_HANDSHAKE_PLAIN_SIZE, handshake + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE);
233233
ck_assert_msg(ret == TCP_CLIENT_HANDSHAKE_SIZE - (CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE),
234234
"Failed to encrypt the outgoing handshake.");
@@ -248,7 +248,7 @@ static struct sec_TCP_con *new_tcp_con(const Logger *logger, const Memory *mem,
248248
uint8_t response_plain[TCP_HANDSHAKE_PLAIN_SIZE];
249249
ck_assert_msg(net_recv(sec_c->ns, logger, sock, response, TCP_SERVER_HANDSHAKE_SIZE, &localhost) == TCP_SERVER_HANDSHAKE_SIZE,
250250
"Failed to receive server handshake response.");
251-
ret = decrypt_data(tcp_server_public_key(tcp_s), f_secret_key, response, response + CRYPTO_NONCE_SIZE,
251+
ret = decrypt_data(mem, tcp_server_public_key(tcp_s), f_secret_key, response, response + CRYPTO_NONCE_SIZE,
252252
TCP_SERVER_HANDSHAKE_SIZE - CRYPTO_NONCE_SIZE, response_plain);
253253
ck_assert_msg(ret == TCP_HANDSHAKE_PLAIN_SIZE, "Failed to decrypt server handshake response.");
254254
encrypt_precompute(response_plain, t_secret_key, sec_c->shared_key);
@@ -271,7 +271,7 @@ static int write_packet_tcp_test_connection(const Logger *logger, struct sec_TCP
271271

272272
uint16_t c_length = net_htons(length + CRYPTO_MAC_SIZE);
273273
memcpy(packet, &c_length, sizeof(uint16_t));
274-
int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t));
274+
int len = encrypt_data_symmetric(con->mem, con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t));
275275

276276
if ((unsigned int)len != (packet_size - sizeof(uint16_t))) {
277277
return -1;
@@ -296,7 +296,7 @@ static int read_packet_sec_tcp(const Logger *logger, struct sec_TCP_con *con, ui
296296

297297
int rlen = net_recv(con->ns, logger, con->sock, data, length, &localhost);
298298
ck_assert_msg(rlen == length, "Did not receive packet of correct length. Wanted %i, instead got %i", length, rlen);
299-
rlen = decrypt_data_symmetric(con->shared_key, con->recv_nonce, data + 2, length - 2, data);
299+
rlen = decrypt_data_symmetric(con->mem, con->shared_key, con->recv_nonce, data + 2, length - 2, data);
300300
ck_assert_msg(rlen != -1, "Failed to decrypt a received packet from the Relay server.");
301301
increment_nonce(con->recv_nonce);
302302
return rlen;

auto_tests/crypto_test.c

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ static const uint8_t test_c[147] = {
8080

8181
static void test_known(void)
8282
{
83+
const Memory *mem = os_memory();
84+
ck_assert(mem != nullptr);
85+
8386
uint8_t c[147];
8487
uint8_t m[131];
8588

@@ -88,19 +91,22 @@ static void test_known(void)
8891
ck_assert_msg(sizeof(test_c) == sizeof(c), "sanity check failed");
8992
ck_assert_msg(sizeof(test_m) == sizeof(m), "sanity check failed");
9093

91-
const uint16_t clen = encrypt_data(bobpk, alicesk, test_nonce, test_m, sizeof(test_m) / sizeof(uint8_t), c);
94+
const uint16_t clen = encrypt_data(mem, bobpk, alicesk, test_nonce, test_m, sizeof(test_m) / sizeof(uint8_t), c);
9295

9396
ck_assert_msg(memcmp(test_c, c, sizeof(c)) == 0, "cyphertext doesn't match test vector");
9497
ck_assert_msg(clen == sizeof(c) / sizeof(uint8_t), "wrong ciphertext length");
9598

96-
const uint16_t mlen = decrypt_data(bobpk, alicesk, test_nonce, test_c, sizeof(test_c) / sizeof(uint8_t), m);
99+
const uint16_t mlen = decrypt_data(mem, bobpk, alicesk, test_nonce, test_c, sizeof(test_c) / sizeof(uint8_t), m);
97100

98101
ck_assert_msg(memcmp(test_m, m, sizeof(m)) == 0, "decrypted text doesn't match test vector");
99102
ck_assert_msg(mlen == sizeof(m) / sizeof(uint8_t), "wrong plaintext length");
100103
}
101104

102105
static void test_fast_known(void)
103106
{
107+
const Memory *mem = os_memory();
108+
ck_assert(mem != nullptr);
109+
104110
uint8_t k[CRYPTO_SHARED_KEY_SIZE];
105111
uint8_t c[147];
106112
uint8_t m[131];
@@ -112,19 +118,21 @@ static void test_fast_known(void)
112118
ck_assert_msg(sizeof(test_c) == sizeof(c), "sanity check failed");
113119
ck_assert_msg(sizeof(test_m) == sizeof(m), "sanity check failed");
114120

115-
const uint16_t clen = encrypt_data_symmetric(k, test_nonce, test_m, sizeof(test_m) / sizeof(uint8_t), c);
121+
const uint16_t clen = encrypt_data_symmetric(mem, k, test_nonce, test_m, sizeof(test_m) / sizeof(uint8_t), c);
116122

117123
ck_assert_msg(memcmp(test_c, c, sizeof(c)) == 0, "cyphertext doesn't match test vector");
118124
ck_assert_msg(clen == sizeof(c) / sizeof(uint8_t), "wrong ciphertext length");
119125

120-
const uint16_t mlen = decrypt_data_symmetric(k, test_nonce, test_c, sizeof(test_c) / sizeof(uint8_t), m);
126+
const uint16_t mlen = decrypt_data_symmetric(mem, k, test_nonce, test_c, sizeof(test_c) / sizeof(uint8_t), m);
121127

122128
ck_assert_msg(memcmp(test_m, m, sizeof(m)) == 0, "decrypted text doesn't match test vector");
123129
ck_assert_msg(mlen == sizeof(m) / sizeof(uint8_t), "wrong plaintext length");
124130
}
125131

126132
static void test_endtoend(void)
127133
{
134+
const Memory *mem = os_memory();
135+
ck_assert(mem != nullptr);
128136
const Random *rng = os_random();
129137
ck_assert(rng != nullptr);
130138

@@ -166,21 +174,21 @@ static void test_endtoend(void)
166174
ck_assert_msg(memcmp(k1, k2, CRYPTO_SHARED_KEY_SIZE) == 0, "encrypt_precompute: bad");
167175

168176
//Encrypt all four ways
169-
const uint16_t c1len = encrypt_data(pk2, sk1, n, m, mlen, c1);
170-
const uint16_t c2len = encrypt_data(pk1, sk2, n, m, mlen, c2);
171-
const uint16_t c3len = encrypt_data_symmetric(k1, n, m, mlen, c3);
172-
const uint16_t c4len = encrypt_data_symmetric(k2, n, m, mlen, c4);
177+
const uint16_t c1len = encrypt_data(mem, pk2, sk1, n, m, mlen, c1);
178+
const uint16_t c2len = encrypt_data(mem, pk1, sk2, n, m, mlen, c2);
179+
const uint16_t c3len = encrypt_data_symmetric(mem, k1, n, m, mlen, c3);
180+
const uint16_t c4len = encrypt_data_symmetric(mem, k2, n, m, mlen, c4);
173181

174182
ck_assert_msg(c1len == c2len && c1len == c3len && c1len == c4len, "cyphertext lengths differ");
175183
ck_assert_msg(c1len == mlen + (uint16_t)CRYPTO_MAC_SIZE, "wrong cyphertext length");
176184
ck_assert_msg(memcmp(c1, c2, c1len) == 0 && memcmp(c1, c3, c1len) == 0
177185
&& memcmp(c1, c4, c1len) == 0, "crypertexts differ");
178186

179187
//Decrypt all four ways
180-
const uint16_t m1len = decrypt_data(pk2, sk1, n, c1, c1len, m1);
181-
const uint16_t m2len = decrypt_data(pk1, sk2, n, c1, c1len, m2);
182-
const uint16_t m3len = decrypt_data_symmetric(k1, n, c1, c1len, m3);
183-
const uint16_t m4len = decrypt_data_symmetric(k2, n, c1, c1len, m4);
188+
const uint16_t m1len = decrypt_data(mem, pk2, sk1, n, c1, c1len, m1);
189+
const uint16_t m2len = decrypt_data(mem, pk1, sk2, n, c1, c1len, m2);
190+
const uint16_t m3len = decrypt_data_symmetric(mem, k1, n, c1, c1len, m3);
191+
const uint16_t m4len = decrypt_data_symmetric(mem, k2, n, c1, c1len, m4);
184192

185193
ck_assert_msg(m1len == m2len && m1len == m3len && m1len == m4len, "decrypted text lengths differ");
186194
ck_assert_msg(m1len == mlen, "wrong decrypted text length");
@@ -192,6 +200,8 @@ static void test_endtoend(void)
192200

193201
static void test_large_data(void)
194202
{
203+
const Memory *mem = os_memory();
204+
ck_assert(mem != nullptr);
195205
const Random *rng = os_random();
196206
ck_assert(rng != nullptr);
197207
uint8_t k[CRYPTO_SHARED_KEY_SIZE];
@@ -216,13 +226,13 @@ static void test_large_data(void)
216226
//Generate key
217227
rand_bytes(rng, k, CRYPTO_SHARED_KEY_SIZE);
218228

219-
const uint16_t c1len = encrypt_data_symmetric(k, n, m1, m1_size, c1);
220-
const uint16_t c2len = encrypt_data_symmetric(k, n, m2, m2_size, c2);
229+
const uint16_t c1len = encrypt_data_symmetric(mem, k, n, m1, m1_size, c1);
230+
const uint16_t c2len = encrypt_data_symmetric(mem, k, n, m2, m2_size, c2);
221231

222232
ck_assert_msg(c1len == m1_size + CRYPTO_MAC_SIZE, "could not encrypt");
223233
ck_assert_msg(c2len == m2_size + CRYPTO_MAC_SIZE, "could not encrypt");
224234

225-
const uint16_t m1plen = decrypt_data_symmetric(k, n, c1, c1len, m1prime);
235+
const uint16_t m1plen = decrypt_data_symmetric(mem, k, n, c1, c1len, m1prime);
226236

227237
ck_assert_msg(m1plen == m1_size, "decrypted text lengths differ");
228238
ck_assert_msg(memcmp(m1prime, m1, m1_size) == 0, "decrypted texts differ");
@@ -236,6 +246,8 @@ static void test_large_data(void)
236246

237247
static void test_large_data_symmetric(void)
238248
{
249+
const Memory *mem = os_memory();
250+
ck_assert(mem != nullptr);
239251
const Random *rng = os_random();
240252
ck_assert(rng != nullptr);
241253
uint8_t k[CRYPTO_SYMMETRIC_KEY_SIZE];
@@ -256,10 +268,10 @@ static void test_large_data_symmetric(void)
256268
//Generate key
257269
new_symmetric_key(rng, k);
258270

259-
const uint16_t c1len = encrypt_data_symmetric(k, n, m1, m1_size, c1);
271+
const uint16_t c1len = encrypt_data_symmetric(mem, k, n, m1, m1_size, c1);
260272
ck_assert_msg(c1len == m1_size + CRYPTO_MAC_SIZE, "could not encrypt data");
261273

262-
const uint16_t m1plen = decrypt_data_symmetric(k, n, c1, c1len, m1prime);
274+
const uint16_t m1plen = decrypt_data_symmetric(mem, k, n, c1, c1len, m1prime);
263275

264276
ck_assert_msg(m1plen == m1_size, "decrypted text lengths differ");
265277
ck_assert_msg(memcmp(m1prime, m1, m1_size) == 0, "decrypted texts differ");
@@ -271,6 +283,8 @@ static void test_large_data_symmetric(void)
271283

272284
static void test_very_large_data(void)
273285
{
286+
const Memory *mem = os_memory();
287+
ck_assert(mem != nullptr);
274288
const Random *rng = os_random();
275289
ck_assert(rng != nullptr);
276290

@@ -287,7 +301,7 @@ static void test_very_large_data(void)
287301
ck_assert(plain != nullptr);
288302
ck_assert(encrypted != nullptr);
289303

290-
encrypt_data(pk, sk, nonce, plain, plain_size, encrypted);
304+
encrypt_data(mem, pk, sk, nonce, plain, plain_size, encrypted);
291305

292306
free(encrypted);
293307
free(plain);

auto_tests/onion_test.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ static int handle_test_3(void *object, const IP_Port *source, const uint8_t *pac
109109
#if 0
110110
print_client_id(packet, length);
111111
#endif
112-
int len = decrypt_data(test_3_pub_key, dht_get_self_secret_key(onion->dht),
112+
int len = decrypt_data(onion->mem, test_3_pub_key, dht_get_self_secret_key(onion->dht),
113113
packet + 1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH,
114114
packet + 1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + CRYPTO_NONCE_SIZE,
115115
2 + CRYPTO_SHA256_SIZE + CRYPTO_MAC_SIZE, plain);
@@ -144,7 +144,7 @@ static int handle_test_3_old(void *object, const IP_Port *source, const uint8_t
144144
#if 0
145145
print_client_id(packet, length);
146146
#endif
147-
int len = decrypt_data(test_3_pub_key, dht_get_self_secret_key(onion->dht),
147+
int len = decrypt_data(onion->mem, test_3_pub_key, dht_get_self_secret_key(onion->dht),
148148
packet + 1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH,
149149
packet + 1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + CRYPTO_NONCE_SIZE,
150150
1 + CRYPTO_SHA256_SIZE + CRYPTO_MAC_SIZE, plain);
@@ -182,7 +182,7 @@ static int handle_test_4(void *object, const IP_Port *source, const uint8_t *pac
182182
return 1;
183183
}
184184

185-
int len = decrypt_data(packet + 1 + CRYPTO_NONCE_SIZE, dht_get_self_secret_key(onion->dht), packet + 1,
185+
int len = decrypt_data(onion->mem, packet + 1 + CRYPTO_NONCE_SIZE, dht_get_self_secret_key(onion->dht), packet + 1,
186186
packet + 1 + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE, sizeof("Install gentoo") + CRYPTO_MAC_SIZE, plain);
187187

188188
if (len == -1) {
@@ -202,10 +202,10 @@ static int handle_test_4(void *object, const IP_Port *source, const uint8_t *pac
202202
* Use Onion_Path path to send data of length to dest.
203203
* Maximum length of data is ONION_MAX_DATA_SIZE.
204204
*/
205-
static void send_onion_packet(const Networking_Core *net, const Random *rng, const Onion_Path *path, const IP_Port *dest, const uint8_t *data, uint16_t length)
205+
static void send_onion_packet(const Networking_Core *net, const Memory *mem, const Random *rng, const Onion_Path *path, const IP_Port *dest, const uint8_t *data, uint16_t length)
206206
{
207207
uint8_t packet[ONION_MAX_PACKET_SIZE];
208-
const int len = create_onion_packet(rng, packet, sizeof(packet), path, dest, data, length);
208+
const int len = create_onion_packet(mem, rng, packet, sizeof(packet), path, dest, data, length);
209209
ck_assert_msg(len != -1, "failed to create onion packet");
210210
ck_assert_msg(sendpacket(net, &path->ip_port1, packet, len) == len, "failed to send onion packet");
211211
}
@@ -264,7 +264,7 @@ static void test_basic(void)
264264
nodes[3] = n2;
265265
Onion_Path path;
266266
create_onion_path(rng, onion1->dht, &path, nodes);
267-
send_onion_packet(onion1->net, rng, &path, &nodes[3].ip_port, req_packet, sizeof(req_packet));
267+
send_onion_packet(onion1->net, onion1->mem, rng, &path, &nodes[3].ip_port, req_packet, sizeof(req_packet));
268268

269269
handled_test_1 = 0;
270270

@@ -291,7 +291,7 @@ static void test_basic(void)
291291
uint64_t s;
292292
memcpy(&s, sb_data, sizeof(uint64_t));
293293
memcpy(test_3_pub_key, nodes[3].public_key, CRYPTO_PUBLIC_KEY_SIZE);
294-
int ret = send_announce_request(log1, onion1->net, rng, &path, &nodes[3],
294+
int ret = send_announce_request(log1, onion1->mem, onion1->net, rng, &path, &nodes[3],
295295
dht_get_self_public_key(onion1->dht),
296296
dht_get_self_secret_key(onion1->dht),
297297
zeroes,
@@ -313,7 +313,7 @@ static void test_basic(void)
313313
memcpy(onion_announce_entry_public_key(onion2_a, 1), dht_get_self_public_key(onion2->dht), CRYPTO_PUBLIC_KEY_SIZE);
314314
onion_announce_entry_set_time(onion2_a, 1, mono_time_get(mono_time2));
315315
networking_registerhandler(onion1->net, NET_PACKET_ONION_DATA_RESPONSE, &handle_test_4, onion1);
316-
send_announce_request(log1, onion1->net, rng, &path, &nodes[3],
316+
send_announce_request(log1, onion1->mem, onion1->net, rng, &path, &nodes[3],
317317
dht_get_self_public_key(onion1->dht),
318318
dht_get_self_secret_key(onion1->dht),
319319
test_3_ping_id,
@@ -338,7 +338,7 @@ static void test_basic(void)
338338
ck_assert_msg((onion3 != nullptr), "Onion failed initializing.");
339339

340340
random_nonce(rng, nonce);
341-
ret = send_data_request(log3, onion3->net, rng, &path, &nodes[3].ip_port,
341+
ret = send_data_request(log3, onion3->mem, onion3->net, rng, &path, &nodes[3].ip_port,
342342
dht_get_self_public_key(onion1->dht),
343343
dht_get_self_public_key(onion1->dht),
344344
nonce, (const uint8_t *)"Install gentoo", sizeof("Install gentoo"));

toxcore/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ cc_library(
171171
deps = [
172172
":attributes",
173173
":ccompat",
174+
":mem",
174175
":util",
175176
"@libsodium",
176177
],
@@ -209,6 +210,7 @@ cc_test(
209210
deps = [
210211
":crypto_core",
211212
":crypto_core_test_util",
213+
":mem_test_util",
212214
":util",
213215
"@com_google_googletest//:gtest",
214216
"@com_google_googletest//:gtest_main",
@@ -480,10 +482,12 @@ cc_test(
480482
cc_fuzz_test(
481483
name = "DHT_fuzz_test",
482484
size = "small",
485+
testonly = True,
483486
srcs = ["DHT_fuzz_test.cc"],
484487
corpus = ["//tools/toktok-fuzzer/corpus:DHT_fuzz_test"],
485488
deps = [
486489
":DHT",
490+
":mem_test_util",
487491
"//c-toxcore/testing/fuzzing:fuzz_support",
488492
],
489493
)
@@ -779,6 +783,7 @@ cc_library(
779783
":crypto_core",
780784
":group_announce",
781785
":logger",
786+
":mem",
782787
":mono_time",
783788
":network",
784789
":onion_announce",

0 commit comments

Comments
 (0)