Use getentropy() from POSIX 2024 if it is available.

This commit is contained in:
Sadie Powell 2024-07-13 13:21:50 +01:00
parent c3c97b917d
commit 610688e410
4 changed files with 35 additions and 1 deletions

1
configure vendored
View File

@ -183,6 +183,7 @@ my %compiler = get_compiler_info($config{CXX});
$config{HAS_ARC4RANDOM_BUF} = run_test 'arc4random_buf()', test_file($config{CXX}, 'arc4random_buf.cpp');
$config{HAS_CLOCK_GETTIME} = run_test 'clock_gettime()', test_file($config{CXX}, 'clock_gettime.cpp', $^O eq 'darwin' ? undef : '-lrt');
$config{HAS_GETENTROPY} = run_test 'getentropy()', test_file($config{CXX}, 'getentropy.cpp');
my @socketengines;
push @socketengines, 'epoll' if run_test 'epoll', test_header $config{CXX}, 'sys/epoll.h';

View File

@ -58,4 +58,7 @@
/** Whether the clock_gettime() function was available at compile time. */
%define HAS_CLOCK_GETTIME
/** Whether the getentropy() function was available at compile time. */
%define HAS_GETENTROPY
#endif

26
make/test/getentropy.cpp Normal file
View File

@ -0,0 +1,26 @@
/*
* InspIRCd -- Internet Relay Chat Daemon
*
* Copyright (C) 2024 Sadie Powell <sadie@witchery.services>
*
* This file is part of InspIRCd. InspIRCd is free software: you can
* redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, version 2.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <unistd.h>
int main() {
char buffer[100];
getentropy(buffer, sizeof(buffer));
return 0;
}

View File

@ -456,7 +456,11 @@ unsigned long InspIRCd::GenRandomInt(unsigned long max) const
// This is overridden by a higher-quality algorithm when TLS support is loaded
void InspIRCd::DefaultGenRandom(char* output, size_t max)
{
#if defined HAS_ARC4RANDOM_BUF
#ifdef HAS_GETENTROPY
if (getentropy(output, max) == 0)
return;
#endif
#ifdef HAS_ARC4RANDOM_BUF
arc4random_buf(output, max);
#else
static std::random_device device;