nvmutil: clamp rand (rejection sampling)

clamp rand to eliminate modulo sampling; high
values on the randomisation will bias the result.

not really critical for mac addresses, but there's
no reason not to have this. this patches reduces
the chance that two libreboot users will generate
the same mac addresses!

Signed-off-by: Leah Rowe <leah@libreboot.org>
This commit is contained in:
Leah Rowe
2026-03-28 03:10:46 +00:00
parent f4f1670909
commit cec9a25c2a
2 changed files with 3 additions and 4 deletions
+2 -4
View File
@@ -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;
}
+1
View File
@@ -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;