summaryrefslogtreecommitdiff
path: root/util/libreboot-utils/lib/rand.c
diff options
context:
space:
mode:
authorLeah Rowe <leah@libreboot.org>2026-03-25 11:23:49 +0000
committerLeah Rowe <leah@libreboot.org>2026-03-25 11:28:44 +0000
commit6db9514c956c74afa171cca165d207a3ec502af0 (patch)
tree3524a4c281bdb72ae6bb1c8fa467c271065d9824 /util/libreboot-utils/lib/rand.c
parent49cc239884a5a73b43311a8b6f15b7b1709e46a7 (diff)
libreboot-utils: tidy up the rand code
Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'util/libreboot-utils/lib/rand.c')
-rw-r--r--util/libreboot-utils/lib/rand.c33
1 files changed, 14 insertions, 19 deletions
diff --git a/util/libreboot-utils/lib/rand.c b/util/libreboot-utils/lib/rand.c
index 2f88b420..0b156e51 100644
--- a/util/libreboot-utils/lib/rand.c
+++ b/util/libreboot-utils/lib/rand.c
@@ -59,6 +59,7 @@ rlong(void)
{
size_t rval;
int saved_errno = errno;
+ errno = 0;
#if (defined(__OpenBSD__) && (OpenBSD) >= 201) || \
defined(__FreeBSD__) || \
@@ -71,30 +72,23 @@ rlong(void)
size_t off = 0;
size_t len = sizeof(rval);
-
ssize_t rc;
- if (!len)
- goto err;
-
- while (off < len) {
-
- rc = (ssize_t)syscall(SYS_getrandom,
- (char *)&rval + off, len - off, 0);
-
- if (rc < 0) {
- if (errno == EINTR || errno == EAGAIN)
- continue;
+retry_rand:
+ rc = (ssize_t)syscall(SYS_getrandom,
+ (char *)&rval + off, len - off, 0);
- goto err; /* possibly unsupported by kernel */
- }
+ if (rc < 0) {
+ if (errno == EINTR || errno == EAGAIN)
+ goto retry_rand;
- off += (size_t)rc;
+ goto err; /* possibly unsupported by kernel */
}
- goto out;
+ if ((off += (size_t)rc) < len)
+ goto retry_rand;
- return rval;
+ goto out;
err:
/*
* getrandom can return with error, butt arc4random
@@ -103,8 +97,9 @@ err:
* BSD. So a rand failure is to be interpreted as
* a major systems failure, and we act accordingly.
*/
- err_no_cleanup(1, ECANCELED, "Randomisation failure");
- exit(1);
+ err_no_cleanup(1, ECANCELED,
+ "Randomisation failure, possibly unsupported in your kernel.");
+ exit(EXIT_FAILURE);
#else
#error Unsupported operating system (possibly unsecure randomisation)