mirror of
https://codeberg.org/libreboot/lbmk.git
synced 2026-07-11 05:52:36 +02:00
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:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user