summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--util/libreboot-utils/lib/num.c6
-rw-r--r--util/libreboot-utils/lib/rand.c1
2 files changed, 3 insertions, 4 deletions
diff --git a/util/libreboot-utils/lib/num.c b/util/libreboot-utils/lib/num.c
index d297ab86..f53f0cee 100644
--- a/util/libreboot-utils/lib/num.c
+++ b/util/libreboot-utils/lib/num.c
@@ -41,10 +41,8 @@ hextonum(char ch_s)
if ((unsigned int)(ch - 'a') <= 5)
return ch - 'a' + 10;
- if (ch == '?' || ch == 'x') {
- rset(&rval, sizeof(rval));
- return rval & 0xf;
- }
+ if (ch == '?' || ch == 'x')
+ return rsize(16); /* <-- with rejection sampling! */
return 16;
}
diff --git a/util/libreboot-utils/lib/rand.c b/util/libreboot-utils/lib/rand.c
index 9304d83a..10831e44 100644
--- a/util/libreboot-utils/lib/rand.c
+++ b/util/libreboot-utils/lib/rand.c
@@ -86,6 +86,7 @@ rsize(size_t n)
if (!n)
err_no_cleanup(0, EFAULT, "rsize: division by zero");
+ /* rejection sampling (clamp rand to eliminate modulo bias) */
for (; rval >= SIZE_MAX - (SIZE_MAX % n); rset(&rval, sizeof(rval)));
return rval % n;