lists.openwall.net | lists / announce owl-users owl-dev john-users john-dev passwdqc-users yescrypt popa3d-users / oss-security kernel-hardening musl sabotage tlsify passwords / crypt-dev xvendor / Bugtraq Full-Disclosure linux-kernel linux-netdev linux-ext4 linux-hardening PHC | |
Open Source and information security mailing list archives
| ||
|
Date: Fri, 21 Oct 2022 21:44:01 -0400 From: "Jason A. Donenfeld" <Jason@...c4.com> To: linux-kernel@...r.kernel.org Cc: "Jason A. Donenfeld" <Jason@...c4.com>, Kees Cook <keescook@...omium.org>, Greg Kroah-Hartman <gregkh@...uxfoundation.org>, Jakub Kicinski <kuba@...nel.org>, Russell King <linux@...linux.org.uk>, Catalin Marinas <catalin.marinas@....com>, Thomas Bogendoerfer <tsbogend@...ha.franken.de>, Heiko Carstens <hca@...ux.ibm.com>, Herbert Xu <herbert@...dor.apana.org.au>, Christoph Böhmwalder <christoph.boehmwalder@...bit.com>, Jani Nikula <jani.nikula@...ux.intel.com>, Jason Gunthorpe <jgg@...dia.com>, Sakari Ailus <sakari.ailus@...ux.intel.com>, "Martin K . Petersen" <martin.petersen@...cle.com>, Theodore Ts'o <tytso@....edu>, Andreas Dilger <adilger.kernel@...ger.ca>, Jaegeuk Kim <jaegeuk@...nel.org>, Richard Weinberger <richard@....at>, "Darrick J . Wong" <djwong@...nel.org>, SeongJae Park <sj@...nel.org>, Thomas Gleixner <tglx@...utronix.de>, Andrew Morton <akpm@...ux-foundation.org>, Michael Ellerman <mpe@...erman.id.au>, Helge Deller <deller@....de>, netdev@...r.kernel.org, linux-crypto@...r.kernel.org, linux-block@...r.kernel.org, linux-fsdevel@...r.kernel.org, linux-media@...r.kernel.org, linux-arm-kernel@...ts.infradead.org, loongarch@...ts.linux.dev, linux-mips@...r.kernel.org, linuxppc-dev@...ts.ozlabs.org, linux-mmc@...r.kernel.org, linux-parisc@...r.kernel.org Subject: [PATCH v1 3/5] random: add helpers for random numbers with given floor or range Now that we have get_random_u32_below(), it's trivial to make inline helpers to compute get_random_u32_above() and get_random_u32_between(), which will help clean up open coded loops and manual computations throughout the tree. Signed-off-by: Jason A. Donenfeld <Jason@...c4.com> --- include/linux/random.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/include/linux/random.h b/include/linux/random.h index 3a82c0a8bc46..92188a74e50e 100644 --- a/include/linux/random.h +++ b/include/linux/random.h @@ -91,6 +91,30 @@ static inline u32 get_random_u32_below(u32 ceil) } } +/* + * Returns a random integer in the interval (floor, U32_MAX], with uniform + * distribution, suitable for all uses. Fastest when floor is a constant, but + * still fast for variable floor as well. + */ +static inline u32 get_random_u32_above(u32 floor) +{ + BUILD_BUG_ON_MSG(__builtin_constant_p(floor) && floor == U32_MAX, + "get_random_u32_above() must take floor < U32_MAX"); + return floor + 1 + get_random_u32_below(U32_MAX - floor); +} + +/* + * Returns a random integer in the interval [floor, ceil), with uniform + * distribution, suitable for all uses. Fastest when floor and ceil are + * constant, but still fast for variable floor and ceil as well. + */ +static inline u32 get_random_u32_between(u32 floor, u32 ceil) +{ + BUILD_BUG_ON_MSG(__builtin_constant_p(floor) && __builtin_constant_p(ceil) && + floor >= ceil, "get_random_u32_above() must take floor < ceil"); + return floor + get_random_u32_below(ceil - floor); +} + /* * On 64-bit architectures, protect against non-terminated C string overflows * by zeroing out the first byte of the canary; this leaves 56 bits of entropy. -- 2.38.1
Powered by blists - more mailing lists