Skip to content

Commit b8b1556

Browse files
authored
Merge pull request #39 from bfabiszewski/gnu
Adds support for GNU/Hurd and GNU/kFreeBSD
2 parents d132926 + 9fcd3f9 commit b8b1556

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

randombytes.c

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
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

@@ -18,7 +18,12 @@
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.
@@ -33,10 +38,10 @@
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>
@@ -47,7 +52,7 @@
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__))
@@ -56,6 +61,10 @@
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__)
130140
static int randombytes_linux_read_entropy_ioctl(int device, int *entropy)
131141
{
132142
return ioctl(device, RNDGETENTCNT, entropy);
@@ -174,6 +184,9 @@ static int randombytes_linux_wait_for_entropy(int device)
174184
strategy = PROC;
175185
// Open the entropy count file
176186
proc_file = fopen("/proc/sys/kernel/random/entropy_avail", "r");
187+
if (proc_file == NULL) {
188+
return -1;
189+
}
177190
} else if (retcode != 0) {
178191
// Unrecoverable ioctl error
179192
return -1;
@@ -232,6 +245,7 @@ static int randombytes_linux_wait_for_entropy(int device)
232245
}
233246
return retcode;
234247
}
248+
# endif /* defined(__linux__) */
235249

236250

237251
static int randombytes_linux_randombytes_urandom(void *buf, size_t n)
@@ -243,7 +257,9 @@ static int randombytes_linux_randombytes_urandom(void *buf, size_t n)
243257
fd = open("/dev/urandom", O_RDONLY);
244258
} while (fd == -1 && errno == EINTR);
245259
if (fd == -1) return -1;
260+
# if defined(__linux__)
246261
if (randombytes_linux_wait_for_entropy(fd) == -1) return -1;
262+
# endif
247263

248264
while (n > 0) {
249265
count = n <= SSIZE_MAX ? n : SSIZE_MAX;
@@ -307,7 +323,7 @@ int randombytes(void *buf, size_t n)
307323
#if defined(__EMSCRIPTEN__)
308324
# pragma message("Using crypto api from NodeJS")
309325
return randombytes_js_randombytes_nodejs(buf, n);
310-
#elif defined(__linux__)
326+
#elif defined(__linux__) || defined(__GNU__) || defined(GNU_KFREEBSD)
311327
# if defined(USE_GLIBC)
312328
# pragma message("Using getrandom function call")
313329
/* Use getrandom system call */

0 commit comments

Comments
 (0)