Commit c7f758ee authored by Philipp Schafft's avatar Philipp Schafft 🦁 Committed by Philipp Schafft
Browse files

Feature: Added support for getrandom(), and getentropy()

parent 4edda3b6
......@@ -80,7 +80,7 @@ AS_IF([test "$ice_found_std_headers" != "yes"], [
AC_MSG_ERROR([Unable to find the standard headers])
])
AC_CHECK_HEADERS([sys/select.h errno.h fcntl.h])
AC_CHECK_HEADERS([sys/select.h errno.h fcntl.h sys/random.h])
AC_CHECK_FUNCS([open close read write fopen fclose fread fwrite], [], [AC_MSG_ERROR([Required I/O functions missing])])
......@@ -90,6 +90,8 @@ AC_CHECK_FUNCS_ONCE([sysconf])
AC_CHECK_FUNCS_ONCE([uname getuid getpid getppid])
AC_CHECK_FUNCS_ONCE([getrandom getentropy])
AC_TYPE_SIZE_T
AC_TYPE_SSIZE_T
AC_TYPE_UINTPTR_T
......
......@@ -43,6 +43,10 @@
#include <fcntl.h>
#endif
#ifdef HAVE_SYS_RANDOM_H
#include <sys/random.h>
#endif
#include <igloo/prng.h>
#include <igloo/error.h>
#include <igloo/digest.h>
......@@ -300,28 +304,41 @@ static igloo_error_t digest_instance_extra_once(const void *instanceraw, size_t
static inline size_t igloo_prng_auto_reseed_unlocked_urandom_buffer(void *buffer, size_t bufferlen)
{
#ifdef PATH_URANDOM
#if defined(PATH_URANDOM) || defined(HAVE_GETRANDOM)
ssize_t ret;
#endif
#ifdef PATH_URANDOM
int fh;
#endif
#ifdef HAVE_GETRANDOM
ret = getrandom(buffer, bufferlen, 0);
if (ret > 0)
return ret;
#endif
#ifdef HAVE_GETENTROPY
if (getentropy(buffer, bufferlen) == 0) {
return bufferlen;
}
#endif
#ifdef PATH_URANDOM
#ifdef O_CLOEXEC
fh = open(PATH_URANDOM, O_RDONLY|O_CLOEXEC, 0);
#else
fh = open(PATH_URANDOM, O_RDONLY, 0);
#endif
if (fh < 0)
return 0;
ret = read(fh, buffer, bufferlen);
close(fh);
if (fh >= 0) {
ret = read(fh, buffer, bufferlen);
close(fh);
if (ret < 1)
return 0;
if (ret > 0)
return ret;
}
#endif
return ret;
#else
return 0;
#endif
}
igloo_error_t igloo_prng_auto_reseed_unlocked(igloo_prng_state_t *self, const void *instanceraw, size_t instancelen, igloo_ro_t instance)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment