From ecb1f1e92ab449023d1427f9db26c5bac09c4901 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Sat, 24 Oct 2026 01:50:25 +0200 Subject: Use new entropy fascility and add tests for it --- Makefile | 17 +++++++++-------- Makefile.arc4random | 3 --- opentracker.c | 15 +++------------ ot_udp.c | 22 ++++------------------ proxy.c | 9 +++------ tests/runner.c | 1 + tests/test.h | 1 + trackerlogic.c | 7 ++++--- trackerlogic.h | 7 ------- 9 files changed, 25 insertions(+), 57 deletions(-) delete mode 100644 Makefile.arc4random diff --git a/Makefile b/Makefile index 8f967f7..60a61cd 100644 --- a/Makefile +++ b/Makefile @@ -51,9 +51,10 @@ FEATURES+=-DWANT_FULLSCRAPE # full scrape transfer, if you rely on an older versions, enable this flag #FEATURES+=-DWANT_NO_AUTO_FREE -# Is enabled on BSD systems by default in trackerlogic.h -# on Linux systems the include Makefile adds -lbsd -#include Makefile.arc4random +# opentracker takes its randomness from arc4random, which is part of libc on +# the BSDs, on macOS and on glibc 2.36 and newer. On an older glibc or on musl +# it comes from libbsd instead, so uncomment the next include to link it. +#LDFLAGS+=-lbsd #FEATURES+=-D_DEBUG_HTTPERROR #FEATURES+=-D_DEBUG_RANDOMTORRENTS @@ -67,9 +68,9 @@ CFLAGS+=-I$(LIBOWFAT_HEADERS) -DGIT_VERSION=$(GIT_VERSION) -Wall -pipe -pthread LDFLAGS+=-L$(LIBOWFAT_LIBRARY) -lowfat -pthread BINARY =opentracker -HEADERS=trackerlogic.h scan_urlencoded_query.h ot_mutex.h ot_stats.h ot_vector.h ot_clean.h ot_udp.h ot_iovec.h ot_fullscrape.h ot_accesslist.h ot_http.h ot_livesync.h ot_rijndael.h -SOURCES=opentracker.c trackerlogic.c scan_urlencoded_query.c ot_mutex.c ot_stats.c ot_vector.c ot_clean.c ot_udp.c ot_iovec.c ot_fullscrape.c ot_accesslist.c ot_http.c ot_livesync.c ot_rijndael.c -SOURCES_proxy=proxy.c ot_vector.c ot_mutex.c +HEADERS=trackerlogic.h scan_urlencoded_query.h ot_mutex.h ot_random.h ot_stats.h ot_vector.h ot_clean.h ot_udp.h ot_iovec.h ot_fullscrape.h ot_accesslist.h ot_http.h ot_livesync.h ot_rijndael.h +SOURCES=opentracker.c trackerlogic.c scan_urlencoded_query.c ot_mutex.c ot_random.c ot_stats.c ot_vector.c ot_clean.c ot_udp.c ot_iovec.c ot_fullscrape.c ot_accesslist.c ot_http.c ot_livesync.c ot_rijndael.c +SOURCES_proxy=proxy.c ot_vector.c ot_mutex.c ot_random.c OBJECTS = $(SOURCES:%.c=%.o) OBJECTS_debug = $(SOURCES:%.c=%.debug.o) @@ -104,8 +105,8 @@ proxy.debug: $(OBJECTS_proxy_debug) $(HEADERS) # has to. tests/stubs.c supplies the few symbols those modules reference but # the tests never reach. TESTBIN=tests/runner -TEST_SOURCES=tests/runner.c tests/test.c tests/stubs.c tests/t_scan_urlencoded_query.c tests/t_iovec.c tests/t_vector.c -TEST_UNITS=scan_urlencoded_query.c ot_iovec.c ot_vector.c +TEST_SOURCES=tests/runner.c tests/test.c tests/stubs.c tests/t_scan_urlencoded_query.c tests/t_iovec.c tests/t_vector.c tests/t_random.c +TEST_UNITS=scan_urlencoded_query.c ot_iovec.c ot_vector.c ot_random.c TEST_HEADERS=tests/test.h $(HEADERS) CFLAGS_test = $(CFLAGS) -I. -g -O1 $(FEATURES) diff --git a/Makefile.arc4random b/Makefile.arc4random deleted file mode 100644 index 1488408..0000000 --- a/Makefile.arc4random +++ /dev/null @@ -1,3 +0,0 @@ -FEATURES+=-DWANT_ARC4RANDOM -LDFLAGS+=-lbsd - diff --git a/opentracker.c b/opentracker.c index 89bfd3e..4d44950 100644 --- a/opentracker.c +++ b/opentracker.c @@ -31,6 +31,7 @@ #include "ot_http.h" #include "ot_livesync.h" #include "ot_mutex.h" +#include "ot_random.h" #include "ot_stats.h" #include "ot_udp.h" #include "trackerlogic.h" @@ -294,13 +295,7 @@ static void *server_mainloop(void *args) { if (!ws.inbuf || !ws.outbuf) panic("Initializing worker failed"); -#ifdef WANT_ARC4RANDOM - arc4random_buf(&ws.rand48_state[0], 3 * sizeof(uint16_t)); -#else - ws.rand48_state[0] = (uint16_t)random(); - ws.rand48_state[1] = (uint16_t)random(); - ws.rand48_state[2] = (uint16_t)random(); -#endif + random_bytes(ws.rand48_state, sizeof(ws.rand48_state)); for (;;) { int64 sock; @@ -743,11 +738,7 @@ int main(int argc, char **argv) { serverip[10] = serverip[11] = -1; #endif -#ifdef WANT_DEV_RANDOM - srandomdev(); -#else - srandom(time(NULL)); -#endif + random_init(); while (scanon) { switch (getopt(argc, argv, diff --git a/ot_udp.c b/ot_udp.c index fae2c87..82aa8bd 100644 --- a/ot_udp.c +++ b/ot_udp.c @@ -16,6 +16,7 @@ #include "socket.h" /* Opentracker */ +#include "ot_random.h" #include "ot_rijndael.h" #include "ot_stats.h" #include "ot_udp.h" @@ -34,21 +35,10 @@ static int g_opentracker_started = 0; static void udp_generate_rijndael_round_key(void) { uint32_t key[16]; -#ifdef WANT_ARC4RANDOM - arc4random_buf(&key[0], sizeof(key)); -#else - key[0] = random(); - key[1] = random(); - key[2] = random(); - key[3] = random(); -#endif + random_bytes(key, sizeof(key)); rijndaelKeySetupEnc128(g_rijndael_round_key, (uint8_t *)key); -#ifdef WANT_ARC4RANDOM - g_key_of_the_hour[0] = arc4random(); -#else - g_key_of_the_hour[0] = random(); -#endif + g_key_of_the_hour[0] = random_uint32(); g_hour_of_the_key = g_now_minutes; } @@ -60,11 +50,7 @@ static void udp_make_connectionid(uint32_t connid[2], const ot_ip6 remoteip, int if (g_now_minutes - g_hour_of_the_key >= 60) { uint32_t old_key_of_the_hour = g_key_of_the_hour[0]; -#ifdef WANT_ARC4RANDOM - uint32_t new_key_of_the_hour = arc4random(); -#else - uint32_t new_key_of_the_hour = random(); -#endif + uint32_t new_key_of_the_hour = random_uint32(); /* If in the meantime another thread has performed key rotation, do not overwrite their results */ if (g_now_minutes - g_hour_of_the_key >= 60) { diff --git a/proxy.c b/proxy.c index cec985e..d728e5b 100644 --- a/proxy.c +++ b/proxy.c @@ -25,6 +25,7 @@ /* Opentracker */ #include "ot_mutex.h" +#include "ot_random.h" #include "ot_stats.h" #include "ot_vector.h" #include "trackerlogic.h" @@ -563,12 +564,8 @@ int main(int argc, char **argv) { uint16_t tmpport; int scanon = 1, lbound = 0, sbound = 0; - srandom(time(NULL)); -#ifdef WANT_ARC4RANDOM - g_tracker_id = arc4random(); -#else - g_tracker_id = random(); -#endif + random_init(); + g_tracker_id = random_uint32(); while (scanon) { switch (getopt(argc, argv, ":l:c:L:h")) { diff --git a/tests/runner.c b/tests/runner.c index d73c516..1b6deb1 100644 --- a/tests/runner.c +++ b/tests/runner.c @@ -17,6 +17,7 @@ struct module { static const struct module modules[] = {{"scan_urlencoded_query", test_scan_urlencoded_query}, {"ot_iovec", test_iovec}, {"ot_vector", test_vector}, + {"ot_random", test_random}, {NULL, NULL}}; /* Run every module, or only those whose name contains argv[1] */ diff --git a/tests/test.h b/tests/test.h index 941e058..19acb2f 100644 --- a/tests/test.h +++ b/tests/test.h @@ -79,5 +79,6 @@ extern unsigned long test_failures; void test_scan_urlencoded_query(void); void test_iovec(void); void test_vector(void); +void test_random(void); #endif diff --git a/trackerlogic.c b/trackerlogic.c index 59e9643..d8a2f9b 100644 --- a/trackerlogic.c +++ b/trackerlogic.c @@ -22,6 +22,7 @@ #include "ot_http.h" #include "ot_livesync.h" #include "ot_mutex.h" +#include "ot_random.h" #include "ot_stats.h" #include "ot_vector.h" #include "ot_udp.h" @@ -536,8 +537,8 @@ void trackerlogic_add_random_torrents(size_t amount) { ws.hash = (ot_hash *)ws.inbuf; while (amount--) { - arc4random_buf(ws.hash, sizeof(ot_hash)); - arc4random_buf(&ws.peer, sizeof(ws.peer)); + random_bytes(ws.hash, sizeof(ot_hash)); + random_bytes(&ws.peer, sizeof(ws.peer)); OT_PEERFLAG(ws.peer) &= PEER_FLAG_SEEDING | PEER_FLAG_COMPLETED | PEER_FLAG_STOPPED; @@ -555,7 +556,7 @@ void exerr(char *message) { } void trackerlogic_init(void) { - g_tracker_id = random(); + g_tracker_id = random_uint32(); if (!g_stats_path) g_stats_path = "stats"; diff --git a/trackerlogic.h b/trackerlogic.h index 88d9a11..aa10a54 100644 --- a/trackerlogic.h +++ b/trackerlogic.h @@ -12,13 +12,6 @@ #include #include -#if defined(__linux__) && defined(WANT_ARC4RANDOM) -#include -#endif -#ifdef __FreeBSD__ -#define WANT_ARC4RANDOM -#endif - typedef uint8_t ot_hash[20]; typedef time_t ot_time; typedef char ot_ip6[16]; -- cgit v1.3