diff --git a/src/network/network.cpp b/src/network/network.cpp index 1530017dd2..a49b73e59f 100644 --- a/src/network/network.cpp +++ b/src/network/network.cpp @@ -1315,15 +1315,9 @@ static void NetworkGenerateServerId() std::string NetworkGenerateRandomKeyString(uint bytes) { uint8 *key = AllocaM(uint8, bytes); - char *hex_output = AllocaM(char, bytes * 2); + NetworkRandomBytesWithFallback(key, bytes); - if (randombytes(key, bytes) < 0) { - /* Fallback poor-quality random */ - DEBUG(misc, 0, "High quality random source unavailable"); - for (uint i = 0; i < bytes; i++) { - key[i] = (uint8)InteractiveRandom(); - } - } + char *hex_output = AllocaM(char, bytes * 2); char txt[3]; for (uint i = 0; i < bytes; ++i) { @@ -1401,6 +1395,17 @@ void NetworkShutDown() NetworkCoreShutdown(); } +void NetworkRandomBytesWithFallback(void *buf, size_t bytes) +{ + if (randombytes(buf, bytes) < 0) { + /* Fallback poor-quality random */ + DEBUG(net, 0, "High quality random source unavailable"); + for (uint i = 0; i < bytes; i++) { + reinterpret_cast(buf)[i] = (byte)InteractiveRandom(); + } + } +} + #ifdef __EMSCRIPTEN__ extern "C" { diff --git a/src/network/network_internal.h b/src/network/network_internal.h index 1b37aa6966..b64e045b8a 100644 --- a/src/network/network_internal.h +++ b/src/network/network_internal.h @@ -156,4 +156,6 @@ std::string NormalizeConnectionString(const std::string &connection_string, uint void ClientNetworkEmergencySave(); +void NetworkRandomBytesWithFallback(void *buf, size_t n); + #endif /* NETWORK_INTERNAL_H */ diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp index 4b93d21457..c47c457be8 100644 --- a/src/network/network_server.cpp +++ b/src/network/network_server.cpp @@ -214,12 +214,9 @@ ServerNetworkGameSocketHandler::ServerNetworkGameSocketHandler(SOCKET s) : Netwo this->receive_limit = _settings_client.network.bytes_per_frame_burst; uint64 seeds[3]; - if (randombytes(&seeds, sizeof(uint64) * lengthof(seeds)) < 0) { - /* Can't get random data, use InteractiveRandom */ - for (uint64 &seed : seeds) { - seed = (uint64)(InteractiveRandom()) | (((uint64)(InteractiveRandom())) << 32); - } - } + static_assert(sizeof(seeds) == 24); + NetworkRandomBytesWithFallback(seeds, sizeof(seeds)); + this->server_hash_bits = seeds[0]; this->rcon_hash_bits = seeds[1]; this->settings_hash_bits = seeds[2];