11// In the case that are compiling on linux, we need to define _GNU_SOURCE
22// *before* randombytes.h is included. Otherwise SYS_getrandom will not be
33// declared.
4- #if defined(__linux__ )
4+ #if defined(__linux__ ) || defined( __GNU__ )
55# define _GNU_SOURCE
6- #endif /* defined(__linux__) */
6+ #endif /* defined(__linux__) || defined(__GNU__) */
77
88#include "randombytes.h"
99
1818#include <stdlib.h>
1919#endif
2020
21- #if defined(__linux__ )
21+ /* kFreeBSD */
22+ #if defined(__FreeBSD_kernel__ ) && defined(__GLIBC__ )
23+ # define GNU_KFREEBSD
24+ #endif
25+
26+ #if defined(__linux__ ) || defined(__GNU__ ) || defined(GNU_KFREEBSD )
2227/* Linux */
2328// We would need to include <linux/random.h>, but not every target has access
2429// to the linux headers. We only need RNDGETENTCNT, so we instead inline it.
3338# include <stdint.h>
3439# include <stdio.h>
3540# include <sys/ioctl.h>
36- # if defined(__linux__ ) && defined(__GLIBC__ ) && ((__GLIBC__ > 2 ) || (__GLIBC_MINOR__ > 24 ))
41+ # if ( defined(__linux__ ) || defined( __GNU__ ) ) && defined(__GLIBC__ ) && ((__GLIBC__ > 2 ) || (__GLIBC_MINOR__ > 24 ))
3742# define USE_GLIBC
3843# include <sys/random.h>
39- # endif /* defined(__linux__) && defined(__GLIBC__) && ((__GLIBC__ > 2) || (__GLIBC_MINOR__ > 24)) */
44+ # endif /* ( defined(__linux__) || defined(__GNU__) ) && defined(__GLIBC__) && ((__GLIBC__ > 2) || (__GLIBC_MINOR__ > 24)) */
4045# include <sys/stat.h>
4146# include <sys/syscall.h>
4247# include <sys/types.h>
4752# define SSIZE_MAX (SIZE_MAX / 2 - 1)
4853# endif /* defined(SSIZE_MAX) */
4954
50- #endif /* defined(__linux__) */
55+ #endif /* defined(__linux__) || defined(__GNU__) || defined(GNU_KFREEBSD) */
5156
5257
5358#if defined(__unix__ ) || (defined(__APPLE__ ) && defined(__MACH__ ))
5661# if defined(BSD )
5762# include <stdlib.h>
5863# endif
64+ /* GNU/Hurd defines BSD in sys/param.h which causes problems later */
65+ # if defined(__GNU__ )
66+ # undef BSD
67+ # endif
5968#endif
6069
6170#if defined(__EMSCRIPTEN__ )
@@ -93,7 +102,7 @@ static int randombytes_wasi_randombytes(void *buf, size_t n) {
93102}
94103#endif /* defined(__wasi__) */
95104
96- #if defined(__linux__ ) && (defined(USE_GLIBC ) || defined(SYS_getrandom ))
105+ #if ( defined(__linux__ ) || defined( __GNU__ ) ) && (defined(USE_GLIBC ) || defined(SYS_getrandom ))
97106# if defined(USE_GLIBC )
98107// getrandom is declared in glibc.
99108# elif defined(SYS_getrandom )
@@ -123,10 +132,11 @@ static int randombytes_linux_randombytes_getrandom(void *buf, size_t n)
123132 assert (n == 0 );
124133 return 0 ;
125134}
126- #endif // defined(__linux__) && (defined(USE_GLIBC) || defined(SYS_getrandom))
135+ #endif /* ( defined(__linux__) || defined(__GNU__)) && (defined(USE_GLIBC) || defined(SYS_getrandom)) */
127136
137+ #if (defined(__linux__ ) || defined(GNU_KFREEBSD )) && !defined(SYS_getrandom )
128138
129- #if defined(__linux__ ) && !defined( SYS_getrandom )
139+ # if defined(__linux__ )
130140static int randombytes_linux_read_entropy_ioctl (int device , int * entropy )
131141{
132142 return ioctl (device , RNDGETENTCNT , entropy );
@@ -235,6 +245,7 @@ static int randombytes_linux_wait_for_entropy(int device)
235245 }
236246 return retcode ;
237247}
248+ # endif /* defined(__linux__) */
238249
239250
240251static int randombytes_linux_randombytes_urandom (void * buf , size_t n )
@@ -246,7 +257,9 @@ static int randombytes_linux_randombytes_urandom(void *buf, size_t n)
246257 fd = open ("/dev/urandom" , O_RDONLY );
247258 } while (fd == -1 && errno == EINTR );
248259 if (fd == -1 ) return -1 ;
260+ # if defined(__linux__ )
249261 if (randombytes_linux_wait_for_entropy (fd ) == -1 ) return -1 ;
262+ # endif
250263
251264 while (n > 0 ) {
252265 count = n <= SSIZE_MAX ? n : SSIZE_MAX ;
@@ -310,7 +323,7 @@ int randombytes(void *buf, size_t n)
310323#if defined(__EMSCRIPTEN__ )
311324# pragma message("Using crypto api from NodeJS")
312325 return randombytes_js_randombytes_nodejs (buf , n );
313- #elif defined(__linux__ )
326+ #elif defined(__linux__ ) || defined( __GNU__ ) || defined( GNU_KFREEBSD )
314327# if defined(USE_GLIBC )
315328# pragma message("Using getrandom function call")
316329 /* Use getrandom system call */
0 commit comments