summaryrefslogtreecommitdiff
path: root/util/libreboot-utils/lib
diff options
context:
space:
mode:
authorLeah Rowe <leah@libreboot.org>2026-03-26 09:32:11 +0000
committerLeah Rowe <leah@libreboot.org>2026-03-26 10:33:16 +0000
commit1a9ac7c696766f2b65613d9755983498736a864c (patch)
tree8e26ccd89b4e20a2e2c8e70a8d9783ed7f0ad518 /util/libreboot-utils/lib
parent8e8f7bced44c5f61e390e81d8a84a93099358453 (diff)
libreboot-utils: improved randomness test
and the module bias handling is fully correct Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'util/libreboot-utils/lib')
-rw-r--r--util/libreboot-utils/lib/rand.c47
1 files changed, 33 insertions, 14 deletions
diff --git a/util/libreboot-utils/lib/rand.c b/util/libreboot-utils/lib/rand.c
index 63cb3fcd..5aa280e2 100644
--- a/util/libreboot-utils/lib/rand.c
+++ b/util/libreboot-utils/lib/rand.c
@@ -79,42 +79,61 @@
#endif
int
-win_lottery(char **buf) /* are u lucky? */
+win_lottery(void) /* are u lucky? */
{
- size_t size = 0;
- int rval;
+ size_t size = rsize();
+ size_t size2 = rsize();
+ int rval = 0;
- char *s1 = rmalloc(&size);
- char *s2 = rmalloc(&size);
+ char *s = NULL;
- if (scmp(s1, s2, BUFSIZ + 1, &rval) >= 0 &&
- rval == 0)
+ if (size != size2)
+ return 0;
+
+ s = mkrbuf(size << 1);
+
+ if (!memcmp(s, s + size, size))
rval = 1; /* winner! */
else
rval = 0;
- (void) scat(s1, s2, BUFSIZ << 1, buf);
+ free_if_null(&s);
+ return rval;
+}
- free_if_null(&s1);
- free_if_null(&s2);
+size_t
+rsize(void)
+{
+ size_t rval = 0;
+ size_t size = 67108864;
- return rval;
+ /* clamp rand to prevent modulo bias */
+ size_t limit = SIZE_MAX - (SIZE_MAX % size);
+
+ do {
+ rset(&rval, sizeof(rval));
+ } while (rval >= limit);
+
+ return rval % size;
}
+
void *
rmalloc(size_t *rval)
{
+ size_t size = 16777216;
+
/* clamp rand to prevent modulo bias */
- size_t limit = SIZE_MAX - (SIZE_MAX % BUFSIZ);
+ size_t limit = SIZE_MAX - (SIZE_MAX % size);
if (if_err(rval == NULL, EFAULT))
return NULL;
do {
rset(rval, sizeof(*rval));
- } while (*rval >= limit);
+ } while (*rval >= limit || *rval == 0);
- return mkrstr(*rval %= BUFSIZ);
+ return mkrstr(*rval %= size);
}
char *