Skip to content

Commit 9bb79c1

Browse files
committed
cleanup: Remove a couple of unnecessary misc_tools dependencies
These programs link against libsodium, which already provides bin-to-hex and hex-to-bin conversion functions. Removing the misc_tools dependency shaves ~30KiB (in some cases 10%) off Windows static binaries using it.
1 parent 19475ad commit 9bb79c1

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

other/fun/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,9 @@ target_link_sodium(create_savedata)
3535
target_link_toxcore(create_savedata)
3636

3737
add_executable(sign sign.c)
38-
target_link_libraries(sign PRIVATE misc_tools)
3938
target_link_sodium(sign)
4039

4140
add_executable(cracker_simple cracker_simple.c)
42-
target_link_libraries(cracker_simple PRIVATE misc_tools)
4341
target_link_sodium(cracker_simple)
4442

4543
find_package(OpenMP)

other/fun/cracker_simple.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@
1313
#include <stdio.h>
1414
#include <string.h>
1515

16-
/* Sodium includes*/
17-
#include <sodium/crypto_scalarmult_curve25519.h>
18-
#include <sodium/randombytes.h>
16+
#include <sodium.h>
1917

20-
#include "../../testing/misc_tools.h"
2118
#include "../../toxcore/ccompat.h"
2219

2320
// Secret key and public key length
@@ -40,7 +37,13 @@ int main(int argc, char *argv[])
4037
long long unsigned int num_tries = 0;
4138

4239
size_t len = strlen(argv[1]) / 2;
43-
unsigned char *key = hex_string_to_bin(argv[1]);
40+
unsigned char *key = (unsigned char *)malloc(len);
41+
const char *hex_end = nullptr;
42+
if (sodium_hex2bin(key, len, argv[1], strlen(argv[1]), nullptr, nullptr, &hex_end) != 0
43+
|| hex_end != argv[1] + strlen(argv[1])) {
44+
printf("Invalid key provided\n");
45+
return 1;
46+
}
4447
uint8_t pub_key[KEY_LEN], priv_key[KEY_LEN], c_key[KEY_LEN];
4548

4649
if (len > KEY_LEN) {

other/fun/sign.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include <sodium.h>
1919
#include <string.h>
2020

21-
#include "../../testing/misc_tools.h" // hex_string_to_bin
2221
#include "../../toxcore/ccompat.h"
2322

2423
static int load_file(const char *filename, unsigned char **result)
@@ -70,7 +69,12 @@ int main(int argc, char *argv[])
7069
}
7170

7271
if (argc == 5 && argv[1][0] == 's') {
73-
unsigned char *secret_key = hex_string_to_bin(argv[2]);
72+
const char *hex_end = nullptr;
73+
if (sodium_hex2bin(sk, sizeof(sk), argv[2], strlen(argv[2]), nullptr, nullptr, &hex_end) != 0
74+
|| hex_end != argv[2] + strlen(argv[2])) {
75+
printf("Invalid secret key provided.\n");
76+
goto fail;
77+
}
7478
unsigned char *data = nullptr;
7579
int size = load_file(argv[3], &data);
7680

@@ -80,9 +84,8 @@ int main(int argc, char *argv[])
8084

8185
unsigned long long smlen;
8286
unsigned char *sm = (unsigned char *)malloc(size + crypto_sign_ed25519_BYTES * 2);
83-
crypto_sign_ed25519(sm, &smlen, data, size, secret_key);
87+
crypto_sign_ed25519(sm, &smlen, data, size, sk);
8488
free(data);
85-
free(secret_key);
8689

8790
if (smlen - size != crypto_sign_ed25519_BYTES) {
8891
free(sm);
@@ -110,8 +113,13 @@ int main(int argc, char *argv[])
110113
}
111114

112115
if (argc == 4 && argv[1][0] == 'c') {
113-
unsigned char *public_key = hex_string_to_bin(argv[2]);
114-
unsigned char *data;
116+
const char *hex_end = nullptr;
117+
if (sodium_hex2bin(pk, sizeof(pk), argv[2], strlen(argv[2]), nullptr, nullptr, &hex_end) != 0
118+
|| hex_end != argv[2] + strlen(argv[2])) {
119+
printf("Invalid public key provided.\n");
120+
goto fail;
121+
}
122+
unsigned char *data = nullptr;
115123
int size = load_file(argv[3], &data);
116124

117125
if (size < 0) {
@@ -127,7 +135,7 @@ int main(int argc, char *argv[])
127135
unsigned char *m = (unsigned char *)malloc(size);
128136
unsigned long long mlen;
129137

130-
if (crypto_sign_ed25519_open(m, &mlen, signe, size, public_key) == -1) {
138+
if (crypto_sign_ed25519_open(m, &mlen, signe, size, pk) == -1) {
131139
printf("Failed checking sig.\n");
132140
free(m);
133141
free(signe);

testing/misc_tools.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#ifndef C_TOXCORE_TESTING_MISC_TOOLS_H
22
#define C_TOXCORE_TESTING_MISC_TOOLS_H
33

4-
#include "../toxcore/tox.h"
4+
#include <stdbool.h>
5+
#include <stddef.h>
6+
#include <stdint.h>
57

68
#ifdef __cplusplus
79
extern "C" {
@@ -13,7 +15,6 @@ extern "C" {
1315
void c_sleep(uint32_t x);
1416

1517
uint8_t *hex_string_to_bin(const char *hex_string);
16-
char *id_toa(const uint8_t *id);
1718
void to_hex(char *out, uint8_t *in, int size);
1819
int tox_strncasecmp(const char *s1, const char *s2, size_t n);
1920
int cmdline_parsefor_ipv46(int argc, char **argv, bool *ipv6enabled);

0 commit comments

Comments
 (0)