From 71b1b4634fd5496a6ebdf2ec4ef38c420c56e202 Mon Sep 17 00:00:00 2001 From: Arkadiusz Bokowy Date: Sun, 6 Nov 2022 23:19:07 +0100 Subject: [PATCH 1/2] Provide fallback implementation for getentropy() Commit ba3396f changed getRandomBytes() implementation from c++ std uniform distribution to getentropy(). The getentropy() function first appeared in glibc 2.25. In order to provide backward compatibility for older glibc (e.g. Android SDK <= 19) restore previous getRandomBytes() implementation as a fallback. --- src/SimpleRandomizer.cc | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/SimpleRandomizer.cc b/src/SimpleRandomizer.cc index 8dbef82f..7748ba0d 100644 --- a/src/SimpleRandomizer.cc +++ b/src/SimpleRandomizer.cc @@ -106,6 +106,20 @@ void SimpleRandomizer::getRandomBytes(unsigned char* buf, size_t len) auto iter = len / blocklen; auto p = buf; +#if !HAVE_GETENTROPY + auto getentropy = [this](void *buffer, size_t length) { + auto buf = reinterpret_cast(buffer); + auto dis = std::uniform_int_distribution(); + for (size_t q = length / sizeof(unsigned int); q > 0; --q, ++buf) { + *buf = dis(gen_); + } + const size_t r = length % sizeof(unsigned int); + auto last = dis(gen_); + memcpy(buf, &last, r); + return 0; + }; +#endif // !HAVE_GETENTROPY + for (size_t i = 0; i < iter; ++i) { auto rv = getentropy(p, blocklen); if (rv != 0) { @@ -128,7 +142,7 @@ void SimpleRandomizer::getRandomBytes(unsigned char* buf, size_t len) assert(0); abort(); } -#endif // ! __MINGW32__ +#endif // !__MINGW32__ && !__APPLE__ } } // namespace aria2 From 977c00f48de369fb11d570bf11b6bd6a1779f19f Mon Sep 17 00:00:00 2001 From: anzz1 Date: Tue, 1 Aug 2023 18:11:13 +0300 Subject: [PATCH 2/2] configure.ac: add check for getentropy() (#1) getentropy() requires glibc >=2.25 defines HAVE_GETENTROPY if callable --- configure.ac | 3 +++ 1 file changed, 3 insertions(+) diff --git a/configure.ac b/configure.ac index 10acaba0..a105f3c6 100644 --- a/configure.ac +++ b/configure.ac @@ -911,6 +911,9 @@ AC_CHECK_FUNCS([gettimeofday], AC_CHECK_FUNCS([strptime], [AM_CONDITIONAL([HAVE_STRPTIME], true)], [AM_CONDITIONAL([HAVE_STRPTIME], false)]) +AC_CHECK_FUNCS([getentropy], + [AM_CONDITIONAL([HAVE_GETENTROPY], true)], + [AM_CONDITIONAL([HAVE_GETENTROPY], false)]) AC_CHECK_FUNCS([daemon], [have_daemon=yes]) AM_CONDITIONAL([HAVE_DAEMON], [test "x$have_daemon" = "xyes"])