mirror of
https://codeberg.org/libreboot/lbmk.git
synced 2026-07-11 05:52:36 +02:00
libreboot-utils: improved randomness test
and the module bias handling is fully correct Signed-off-by: Leah Rowe <leah@libreboot.org>
This commit is contained in:
@@ -391,7 +391,8 @@ void *rmalloc(size_t *size); /* don't ever use this */
|
|||||||
void rset(void *buf, size_t n);
|
void rset(void *buf, size_t n);
|
||||||
void *mkrbuf(size_t n);
|
void *mkrbuf(size_t n);
|
||||||
char *mkrstr(size_t n);
|
char *mkrstr(size_t n);
|
||||||
int win_lottery(char **buf);
|
int win_lottery(void);
|
||||||
|
size_t rsize(void);
|
||||||
|
|
||||||
/* Helper functions for command: dump
|
/* Helper functions for command: dump
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -72,49 +72,62 @@
|
|||||||
* or your program dies.
|
* or your program dies.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef BUFSIZ
|
#define MAX_ALLOC (2 << 16)
|
||||||
#define BUFSIZ 8192 /* reasonably on modern 64-bit systems */
|
|
||||||
#elif (BUFSIZ <= 0)
|
|
||||||
#error defined buffer size BUFSIZ below or equal to zero
|
|
||||||
#endif
|
|
||||||
|
|
||||||
int
|
int
|
||||||
win_lottery(char **buf) /* are u lucky? */
|
win_lottery(void) /* are u lucky? */
|
||||||
{
|
{
|
||||||
size_t size = 0;
|
size_t size = rsize();
|
||||||
int rval;
|
size_t size2 = rsize();
|
||||||
|
char *s = NULL;
|
||||||
|
|
||||||
char *s1 = rmalloc(&size);
|
if (size &&
|
||||||
char *s2 = rmalloc(&size);
|
size == size2 &&
|
||||||
|
size <= MAX_ALLOC << 1) {
|
||||||
|
|
||||||
if (scmp(s1, s2, BUFSIZ + 1, &rval) >= 0 &&
|
if (!memcmp(s = mkrbuf(size << 1),
|
||||||
rval == 0)
|
s + size, size))
|
||||||
rval = 1; /* winner! */
|
size2 = 1; /* winner! */
|
||||||
else
|
else
|
||||||
rval = 0;
|
size2 = 0;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
(void) scat(s1, s2, BUFSIZ << 1, buf);
|
free_if_null(&s);
|
||||||
|
return (int)size2;
|
||||||
free_if_null(&s1);
|
|
||||||
free_if_null(&s2);
|
|
||||||
|
|
||||||
return rval;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t
|
||||||
|
rsize(void)
|
||||||
|
{
|
||||||
|
size_t rval = 0;
|
||||||
|
|
||||||
|
/* clamp rand to prevent modulo bias */
|
||||||
|
size_t limit = SIZE_MAX - (SIZE_MAX % MAX_ALLOC);
|
||||||
|
|
||||||
|
do {
|
||||||
|
rset(&rval, sizeof(rval));
|
||||||
|
} while (rval >= limit);
|
||||||
|
|
||||||
|
return rval % MAX_ALLOC;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void *
|
void *
|
||||||
rmalloc(size_t *rval)
|
rmalloc(size_t *rval)
|
||||||
{
|
{
|
||||||
/* clamp rand to prevent modulo bias */
|
/* clamp rand to prevent modulo bias */
|
||||||
size_t limit = SIZE_MAX - (SIZE_MAX % BUFSIZ);
|
size_t limit = SIZE_MAX - (SIZE_MAX % MAX_ALLOC);
|
||||||
|
|
||||||
if (if_err(rval == NULL, EFAULT))
|
if (if_err(rval == NULL, EFAULT))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
rset(rval, sizeof(*rval));
|
rset(rval, sizeof(*rval));
|
||||||
} while (*rval >= limit);
|
} while (*rval >= limit || *rval == 0);
|
||||||
|
|
||||||
return mkrstr(*rval %= BUFSIZ);
|
return mkrstr(*rval %= MAX_ALLOC);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *
|
char *
|
||||||
|
|||||||
@@ -7,15 +7,12 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
|
||||||
#include "include/common.h"
|
#include "include/common.h"
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
char *s1 = NULL;
|
int lucky;
|
||||||
int rval = 0;
|
|
||||||
|
|
||||||
#if defined(__OpenBSD__) && defined(OpenBSD)
|
#if defined(__OpenBSD__) && defined(OpenBSD)
|
||||||
#if (OpenBSD) >= 509
|
#if (OpenBSD) >= 509
|
||||||
if (pledge("stdio", NULL) == -1)
|
if (pledge("stdio", NULL) == -1)
|
||||||
@@ -24,16 +21,10 @@ main(int argc, char *argv[])
|
|||||||
#endif
|
#endif
|
||||||
setvbuf(stdout, NULL, _IONBF, 0);
|
setvbuf(stdout, NULL, _IONBF, 0);
|
||||||
|
|
||||||
if (win_lottery(&s1))
|
lucky = win_lottery();
|
||||||
rval = 1;
|
|
||||||
|
|
||||||
if (s1 != NULL) {
|
printf("%s\n", lucky ? "You won!" : "You lose! Sorry!");
|
||||||
printf("%s\n\n", s1);
|
return lucky ? EXIT_SUCCESS : EXIT_FAILURE;
|
||||||
free(s1);
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("%s\n", rval ? "You won!" : "You lose! Sorry!");
|
|
||||||
return rval? EXIT_SUCCESS : EXIT_FAILURE;
|
|
||||||
}/*
|
}/*
|
||||||
|
|
||||||
( >:3 )
|
( >:3 )
|
||||||
|
|||||||
Reference in New Issue
Block a user