From: Alejandro Colomar Date: Wed, 19 Jun 2024 17:54:16 +0000 (+0200) Subject: lib/csrand.c: Fix the lower part of the domain of csrand_uniform() X-Git-Tag: 4.17.0-rc1~230 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=47edcd3045fd2865cbcdcb002f52195a960b3dda;p=thirdparty%2Fshadow.git lib/csrand.c: Fix the lower part of the domain of csrand_uniform() I accidentally broke this code during an un-optimization. We need to start from a random value of the width of the limit, that is, 32 bits. Thanks to Jason for pointing to his similar code in the kernel, which made me see my mistake. Fixes: 2a61122b5e8f ("Unoptimize the higher part of the domain of csrand_uniform()") Closes: Reported-by: Michael Brunnbauer Link: Cc: "Jason A. Donenfeld" Link: Link: Link: Tested-by: Michael Brunnbauer Reviewed-by: Michael Brunnbauer Signed-off-by: Alejandro Colomar --- diff --git a/lib/csrand.c b/lib/csrand.c index 9d6f1503d..8ded343a5 100644 --- a/lib/csrand.c +++ b/lib/csrand.c @@ -23,6 +23,7 @@ #include "sizeof.h" +static uint32_t csrand32(void); static uint32_t csrand_uniform32(uint32_t n); static unsigned long csrand_uniform_slow(unsigned long n); @@ -97,6 +98,13 @@ csrand_interval(unsigned long min, unsigned long max) } +static uint32_t +csrand32(void) +{ + return csrand(); +} + + /* * Fast Random Integer Generation in an Interval * ACM Transactions on Modeling and Computer Simulation 29 (1), 2019 @@ -109,12 +117,12 @@ csrand_uniform32(uint32_t n) uint64_t r, mult; if (n == 0) - return csrand(); + return csrand32(); bound = -n % n; // analogous to `2^32 % n`, since `x % y == (x-y) % y` do { - r = csrand(); + r = csrand32(); mult = r * n; rem = mult; // analogous to `mult % 2^32` } while (rem < bound); // p = (2^32 % n) / 2^32; W.C.: n=2^31+1, p=0.5