1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-01 17:38:41 +00:00

Use secure randomness to seed internal RNG

This commit is contained in:
sfan5 2025-03-30 16:42:26 +02:00
parent 785c042f1f
commit 1281173e50
4 changed files with 18 additions and 18 deletions

View file

@ -16,14 +16,13 @@
#include "porting.h"
#include "util/container.h"
#include "util/thread.h"
#include "util/numeric.h"
#include "version.h"
#include "settings.h"
#include "noise.h"
static std::mutex g_httpfetch_mutex;
static std::unordered_map<u64, std::queue<HTTPFetchResult>>
g_httpfetch_results;
static PcgRandom g_callerid_randomness;
static std::string default_user_agent()
{
@ -78,18 +77,18 @@ u64 httpfetch_caller_alloc_secure()
// Generate random caller IDs and make sure they're not
// already used or reserved.
// Give up after 100 tries to prevent infinite loop
size_t tries = 100;
int tries = 100;
u64 caller;
do {
caller = (((u64) g_callerid_randomness.next()) << 32) |
g_callerid_randomness.next();
// Global RNG is seeded securely, so we can use it.
myrand_bytes(&caller, sizeof(caller));
if (--tries < 1) {
FATAL_ERROR("httpfetch_caller_alloc_secure: ran out of caller IDs");
return HTTPFETCH_DISCARD;
}
} while (caller >= HTTPFETCH_CID_START &&
} while (caller < HTTPFETCH_CID_START ||
g_httpfetch_results.find(caller) != g_httpfetch_results.end());
verbosestream << "httpfetch_caller_alloc_secure: allocating "
@ -702,11 +701,6 @@ void httpfetch_init(int parallel_limit)
FATAL_ERROR_IF(res != CURLE_OK, "cURL init failed");
g_httpfetch_thread = std::make_unique<CurlFetchThread>(parallel_limit);
// Initialize g_callerid_randomness for httpfetch_caller_alloc_secure
u64 randbuf[2];
porting::secure_rand_fill_buf(randbuf, sizeof(u64) * 2);
g_callerid_randomness = PcgRandom(randbuf[0], randbuf[1]);
}
void httpfetch_cleanup()