Network: De-duplicate getting random bytes with fallback

This commit is contained in:
Jonathan G Rennison
2023-06-14 22:18:21 +01:00
parent b18d3c9603
commit 76a9e280c2
3 changed files with 18 additions and 14 deletions

View File

@@ -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<byte *>(buf)[i] = (byte)InteractiveRandom();
}
}
}
#ifdef __EMSCRIPTEN__
extern "C" {