Date: Tue, 03 Sep 2019 14:07:33 -0000 From: Conrad Meyer <cem@FreeBSD.org> To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r346420 - in head: lib/libc/gen share/man/man9 sys/conf sys/libkern sys/sys Message-ID: <201904192208.x3JM8HWM079280@repo.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: cem Date: Fri Apr 19 22:08:17 2019 New Revision: 346420 URL: https://svnweb.freebsd.org/changeset/base/346420 Log: Revert r346410 and r346411 libkern in .PATH has too many filename conflicts with libc and my -DNO_CLEAN tinderbox didn't catch that ahead of time. Mea culpa. Added: head/lib/libc/gen/arc4random_uniform.c - copied unchanged from r346409, head/lib/libc/gen/arc4random_uniform.c Deleted: head/sys/libkern/arc4random_uniform.c Modified: head/lib/libc/gen/Makefile.inc head/share/man/man9/Makefile head/share/man/man9/random.9 head/sys/conf/files head/sys/sys/libkern.h head/sys/sys/param.h Modified: head/lib/libc/gen/Makefile.inc ============================================================================== --- head/lib/libc/gen/Makefile.inc Fri Apr 19 21:58:51 2019 (r346419) +++ head/lib/libc/gen/Makefile.inc Fri Apr 19 22:08:17 2019 (r346420) @@ -3,7 +3,6 @@ # machine-independent gen sources .PATH: ${LIBC_SRCTOP}/${LIBC_ARCH}/gen ${LIBC_SRCTOP}/gen -.PATH: ${SRCTOP}/sys/libkern CONFS= shells Copied: head/lib/libc/gen/arc4random_uniform.c (from r346409, head/lib/libc/gen/arc4random_uniform.c) ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/lib/libc/gen/arc4random_uniform.c Fri Apr 19 22:08:17 2019 (r346420, copy of r346409, head/lib/libc/gen/arc4random_uniform.c) @@ -0,0 +1,58 @@ +/* $OpenBSD: arc4random_uniform.c,v 1.2 2015/09/13 08:31:47 guenther Exp $ */ + +/* + * Copyright (c) 2008, Damien Miller <djm@openbsd.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * $FreeBSD$ + */ + +#include <sys/types.h> +#include <stdlib.h> + +/* + * Calculate a uniformly distributed random number less than upper_bound + * avoiding "modulo bias". + * + * Uniformity is achieved by generating new random numbers until the one + * returned is outside the range [0, 2**32 % upper_bound). This + * guarantees the selected random number will be inside + * [2**32 % upper_bound, 2**32) which maps back to [0, upper_bound) + * after reduction modulo upper_bound. + */ +uint32_t +arc4random_uniform(uint32_t upper_bound) +{ + uint32_t r, min; + + if (upper_bound < 2) + return 0; + + /* 2**32 % x == (2**32 - x) % x */ + min = -upper_bound % upper_bound; + + /* + * This could theoretically loop forever but each retry has + * p > 0.5 (worst case, usually far better) of selecting a + * number inside the range we need, so it should rarely need + * to re-roll. + */ + for (;;) { + r = arc4random(); + if (r >= min) + break; + } + + return r % upper_bound; +} Modified: head/share/man/man9/Makefile ============================================================================== --- head/share/man/man9/Makefile Fri Apr 19 21:58:51 2019 (r346419) +++ head/share/man/man9/Makefile Fri Apr 19 22:08:17 2019 (r346420) @@ -1668,8 +1668,6 @@ MLINKS+=psignal.9 gsignal.9 \ psignal.9 tdsignal.9 MLINKS+=random.9 arc4rand.9 \ random.9 arc4random.9 \ - random.9 arc4random_buf.9 \ - random.9 arc4random_uniform.9 \ random.9 is_random_seeded.9 \ random.9 read_random.9 \ random.9 read_random_uio.9 \ Modified: head/share/man/man9/random.9 ============================================================================== --- head/share/man/man9/random.9 Fri Apr 19 21:58:51 2019 (r346419) +++ head/share/man/man9/random.9 Fri Apr 19 22:08:17 2019 (r346420) @@ -26,7 +26,7 @@ .\" .\" $FreeBSD$ .\" " -.Dd April 19, 2019 +.Dd April 16, 2019 .Dt RANDOM 9 .Os .Sh NAME @@ -45,8 +45,6 @@ .Fn arc4random "void" .Ft void .Fn arc4random_buf "void *ptr" "size_t len" -.Ft uint32_t -.Fn arc4random_uniform "uint32_t upper_bound" .Ft void .Fn arc4rand "void *ptr" "u_int length" "int reseed" .Pp @@ -81,15 +79,6 @@ fills with .Fa len bytes of random data. -.Pp -.Fn arc4random_uniform -will return a single 32-bit value, uniformly distributed but less than -.Fa upper_bound . -This is recommended over constructions like -.Dq Li arc4random() % upper_bound -as it avoids "modulo bias" when the upper bound is not a power of two. -In the worst case, this function may consume multiple iterations -to ensure uniformity. .Pp The .Fn arc4rand Modified: head/sys/conf/files ============================================================================== --- head/sys/conf/files Fri Apr 19 21:58:51 2019 (r346419) +++ head/sys/conf/files Fri Apr 19 22:08:17 2019 (r346420) @@ -3985,7 +3985,6 @@ kgssapi/gsstest.c optional kgssapi_debug # the file should be moved to conf/files.<arch> from here. # libkern/arc4random.c standard -libkern/arc4random_uniform.c standard crypto/chacha20/chacha.c standard libkern/asprintf.c standard libkern/bcd.c standard Modified: head/sys/sys/libkern.h ============================================================================== --- head/sys/sys/libkern.h Fri Apr 19 21:58:51 2019 (r346419) +++ head/sys/sys/libkern.h Fri Apr 19 22:08:17 2019 (r346420) @@ -128,7 +128,6 @@ struct malloc_type; uint32_t arc4random(void); void arc4random_buf(void *, size_t); void arc4rand(void *, u_int, int); -uint32_t arc4random_uniform(uint32_t); int timingsafe_bcmp(const void *, const void *, size_t); void *bsearch(const void *, const void *, size_t, size_t, int (*)(const void *, const void *)); Modified: head/sys/sys/param.h ============================================================================== --- head/sys/sys/param.h Fri Apr 19 21:58:51 2019 (r346419) +++ head/sys/sys/param.h Fri Apr 19 22:08:17 2019 (r346420) @@ -60,7 +60,7 @@ * in the range 5 to 9. */ #undef __FreeBSD_version -#define __FreeBSD_version 1300021 /* Master, propagated to newvers */ +#define __FreeBSD_version 1300020 /* Master, propagated to newvers */ /* * __FreeBSD_kernel__ indicates that this system uses the kernel of FreeBSD,
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201904192208.x3JM8HWM079280>