diff options
| author | Leah Rowe <leah@libreboot.org> | 2026-03-26 09:32:11 +0000 |
|---|---|---|
| committer | Leah Rowe <leah@libreboot.org> | 2026-03-26 09:40:55 +0000 |
| commit | 6daad03a8d53936fdc2370f917481cbbeee1a437 (patch) | |
| tree | 0a5c5ac59c9fa6089d5d392f8f3b1e8c72e7dca7 /util/libreboot-utils/lib | |
| parent | 8e8f7bced44c5f61e390e81d8a84a93099358453 (diff) | |
libreboot-utils: improved randomness test
now it's for real
Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'util/libreboot-utils/lib')
| -rw-r--r-- | util/libreboot-utils/lib/rand.c | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/util/libreboot-utils/lib/rand.c b/util/libreboot-utils/lib/rand.c index 63cb3fcd..c52fbd5e 100644 --- a/util/libreboot-utils/lib/rand.c +++ b/util/libreboot-utils/lib/rand.c @@ -79,21 +79,25 @@ #endif int -win_lottery(char **buf) /* are u lucky? */ +win_lottery(void) /* are u lucky? */ { size_t size = 0; + size_t size2 = 0; int rval; char *s1 = rmalloc(&size); - char *s2 = rmalloc(&size); + char *s2 = rmalloc(&size2); - if (scmp(s1, s2, BUFSIZ + 1, &rval) >= 0 && + if (size2 > size) + size = size2; + + if (scmp(s1, s2, size + 1, &rval) >= 0 && rval == 0) rval = 1; /* winner! */ else rval = 0; - (void) scat(s1, s2, BUFSIZ << 1, buf); + printf("%s%s\n\n", s1, s2); free_if_null(&s1); free_if_null(&s2); @@ -104,17 +108,19 @@ win_lottery(char **buf) /* are u lucky? */ 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 * |
