Use getrandom() on dietlibc -- via Felix von Leitner

This commit is contained in:
Frank Denis 2017-01-18 20:00:25 +01:00
parent a81cea2366
commit f053b98b64
2 changed files with 18 additions and 2 deletions

View File

@ -6,7 +6,11 @@
# include <sys/time.h> # include <sys/time.h>
#endif #endif
#ifdef __linux__ #ifdef __linux__
#ifdef __dietlibc__
#define _LINUX_SOURCE
#else
# include <sys/syscall.h> # include <sys/syscall.h>
#endif
# include <poll.h> # include <poll.h>
#endif #endif
@ -207,7 +211,7 @@ randombytes_salsa20_random_random_dev_open(void)
} }
# endif # endif
# if defined(SYS_getrandom) && defined(__NR_getrandom) # if defined(__dietlibc__) || (defined(SYS_getrandom) && defined(__NR_getrandom))
static int static int
_randombytes_linux_getrandom(void * const buf, const size_t size) _randombytes_linux_getrandom(void * const buf, const size_t size)
{ {
@ -215,7 +219,11 @@ _randombytes_linux_getrandom(void * const buf, const size_t size)
assert(size <= 256U); assert(size <= 256U);
do { do {
#ifdef __dietlibc__
readnb = getrandom(buf, size, 0);
#else
readnb = syscall(SYS_getrandom, buf, (int) size, 0); readnb = syscall(SYS_getrandom, buf, (int) size, 0);
#endif
} while (readnb < 0 && (errno == EINTR || errno == EAGAIN)); } while (readnb < 0 && (errno == EINTR || errno == EAGAIN));
return (readnb == (int) size) - 1; return (readnb == (int) size) - 1;

View File

@ -6,7 +6,11 @@
# include <sys/time.h> # include <sys/time.h>
#endif #endif
#ifdef __linux__ #ifdef __linux__
#ifdef __dietlibc__
#define _LINUX_SOURCE
#else
# include <sys/syscall.h> # include <sys/syscall.h>
#endif
# include <poll.h> # include <poll.h>
#endif #endif
@ -189,7 +193,7 @@ randombytes_sysrandom_random_dev_open(void)
/* LCOV_EXCL_STOP */ /* LCOV_EXCL_STOP */
} }
# if defined(SYS_getrandom) && defined(__NR_getrandom) # if defined(__dietlibc__) || (defined(SYS_getrandom) && defined(__NR_getrandom))
static int static int
_randombytes_linux_getrandom(void * const buf, const size_t size) _randombytes_linux_getrandom(void * const buf, const size_t size)
{ {
@ -197,7 +201,11 @@ _randombytes_linux_getrandom(void * const buf, const size_t size)
assert(size <= 256U); assert(size <= 256U);
do { do {
#ifdef __dietlibc__
readnb = getrandom( buf, size, 0);
#else
readnb = syscall(SYS_getrandom, buf, (int) size, 0); readnb = syscall(SYS_getrandom, buf, (int) size, 0);
#endif
} while (readnb < 0 && (errno == EINTR || errno == EAGAIN)); } while (readnb < 0 && (errno == EINTR || errno == EAGAIN));
return (readnb == (int) size) - 1; return (readnb == (int) size) - 1;