Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide fallback implementation for getentropy() #2000

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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"])

Expand Down
16 changes: 15 additions & 1 deletion src/SimpleRandomizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned int*>(buffer);
auto dis = std::uniform_int_distribution<unsigned int>();
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) {
Expand All @@ -128,7 +142,7 @@ void SimpleRandomizer::getRandomBytes(unsigned char* buf, size_t len)
assert(0);
abort();
}
#endif // ! __MINGW32__
#endif // !__MINGW32__ && !__APPLE__
}

} // namespace aria2