From f053b98b6416a5566bed2b1f143f5055a873644c Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 18 Jan 2017 20:00:25 +0100 Subject: [PATCH] Use getrandom() on dietlibc -- via Felix von Leitner --- .../randombytes/salsa20/randombytes_salsa20_random.c | 10 +++++++++- .../randombytes/sysrandom/randombytes_sysrandom.c | 10 +++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c b/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c index a84c107a..dbd89716 100644 --- a/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c +++ b/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c @@ -6,7 +6,11 @@ # include #endif #ifdef __linux__ +#ifdef __dietlibc__ +#define _LINUX_SOURCE +#else # include +#endif # include #endif @@ -207,7 +211,7 @@ randombytes_salsa20_random_random_dev_open(void) } # endif -# if defined(SYS_getrandom) && defined(__NR_getrandom) +# if defined(__dietlibc__) || (defined(SYS_getrandom) && defined(__NR_getrandom)) static int _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); do { +#ifdef __dietlibc__ + readnb = getrandom(buf, size, 0); +#else readnb = syscall(SYS_getrandom, buf, (int) size, 0); +#endif } while (readnb < 0 && (errno == EINTR || errno == EAGAIN)); return (readnb == (int) size) - 1; diff --git a/src/libsodium/randombytes/sysrandom/randombytes_sysrandom.c b/src/libsodium/randombytes/sysrandom/randombytes_sysrandom.c index 83c8a39f..e7c902c8 100644 --- a/src/libsodium/randombytes/sysrandom/randombytes_sysrandom.c +++ b/src/libsodium/randombytes/sysrandom/randombytes_sysrandom.c @@ -6,7 +6,11 @@ # include #endif #ifdef __linux__ +#ifdef __dietlibc__ +#define _LINUX_SOURCE +#else # include +#endif # include #endif @@ -189,7 +193,7 @@ randombytes_sysrandom_random_dev_open(void) /* LCOV_EXCL_STOP */ } -# if defined(SYS_getrandom) && defined(__NR_getrandom) +# if defined(__dietlibc__) || (defined(SYS_getrandom) && defined(__NR_getrandom)) static int _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); do { +#ifdef __dietlibc__ + readnb = getrandom( buf, size, 0); +#else readnb = syscall(SYS_getrandom, buf, (int) size, 0); +#endif } while (readnb < 0 && (errno == EINTR || errno == EAGAIN)); return (readnb == (int) size) - 1;