summaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorLeah Rowe <leah@libreboot.org>2026-04-01 12:44:38 +0100
committerLeah Rowe <leah@libreboot.org>2026-04-01 12:44:38 +0100
commit4e72555ac70ecc4fd28624433294d58b6350086a (patch)
tree868f34743120183624c45b807823c0e3b709d7f7 /util
parent44004191cbc1a2a4fc2a60aca215560b5d989b7c (diff)
lbutils: support using arc4random on linux
-DUSE_ARC4=1 use that Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'util')
-rw-r--r--util/libreboot-utils/Makefile1
-rw-r--r--util/libreboot-utils/lib/rand.c20
2 files changed, 16 insertions, 5 deletions
diff --git a/util/libreboot-utils/Makefile b/util/libreboot-utils/Makefile
index f42c4ab6..efa0aedc 100644
--- a/util/libreboot-utils/Makefile
+++ b/util/libreboot-utils/Makefile
@@ -7,7 +7,6 @@
CC = cc
HELLCC = clang
-OLDCC = $(CC) -DUSE_URANDOM=1 -DUSE_OPENAT
CFLAGS = -Os -Wall -Wextra -std=c99 -pedantic -Werror
LDFLAGS =
diff --git a/util/libreboot-utils/lib/rand.c b/util/libreboot-utils/lib/rand.c
index 8e50e110..1591a3b0 100644
--- a/util/libreboot-utils/lib/rand.c
+++ b/util/libreboot-utils/lib/rand.c
@@ -7,10 +7,17 @@
#ifndef RAND_H
#define RAND_H
-#if defined(USE_URANDOM) && \
+#if defined(USE_ARC4) && \
+ ((USE_ARC4) > 0)
+#define _DEFAULT_SOURCE 1 /* for arc4random on *linux* */
+ /* (not needed on bsd - on bsd,
+ it is used automatically unless
+ overridden with USE_URANDOM */
+#elif defined(USE_URANDOM) && \
((USE_URANDOM) > 0)
#include <fcntl.h> /* if not arc4random: /dev/urandom */
-#elif defined(__linux__)
+#elif defined(__linux__) && \
+ !(defined(USE_ARC4) && ((USE_ARC4) > 0))
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif
@@ -134,10 +141,15 @@ rset(void *buf, size_t n)
if (n == 0)
exitf("rset: zero-byte request");
-#if (defined(__OpenBSD__) || defined(__FreeBSD__) || \
+/* on linux, getrandom is recommended,
+ but you can pass -DUSE_ARC4=1 to use arc4random.
+ useful for portability testing from linux.
+ */
+#if (defined(USE_ARC4) && ((USE_ARC4) > 0)) || \
+ ((defined(__OpenBSD__) || defined(__FreeBSD__) || \
defined(__NetBSD__) || defined(__APPLE__) || \
defined(__DragonFly__)) && !(defined(USE_URANDOM) && \
- ((USE_URANDOM) > 0))
+ ((USE_URANDOM) > 0)))
arc4random_buf(buf, n);
#else