From owner-svn-src-head@freebsd.org Tue Sep 3 14:07:32 2019 Return-Path: Delivered-To: svn-src-head@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 8E413DD627; Tue, 3 Sep 2019 14:07:11 +0000 (UTC) (envelope-from yuripv@freebsd.org) Received: from freefall.freebsd.org (freefall.freebsd.org [96.47.72.132]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "freefall.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 46N80H0wQBz4QL3; Tue, 3 Sep 2019 14:07:11 +0000 (UTC) (envelope-from yuripv@freebsd.org) Received: by freefall.freebsd.org (Postfix, from userid 1452) id 378DC1B217; Tue, 3 Sep 2019 14:06:31 +0000 (UTC) X-Original-To: yuripv@localmail.freebsd.org Delivered-To: yuripv@localmail.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (Client CN "mx1.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by freefall.freebsd.org (Postfix) with ESMTPS id EF4161A05; Fri, 19 Apr 2019 22:08:23 +0000 (UTC) (envelope-from owner-src-committers@freebsd.org) Received: from freefall.freebsd.org (freefall.freebsd.org [96.47.72.132]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "freefall.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 3BE246DAEF; Fri, 19 Apr 2019 22:08:23 +0000 (UTC) (envelope-from owner-src-committers@freebsd.org) Received: by freefall.freebsd.org (Postfix, from userid 538) id 115F5199C; Fri, 19 Apr 2019 22:08:23 +0000 (UTC) Delivered-To: src-committers@localmail.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [96.47.72.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (Client CN "mx1.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by freefall.freebsd.org (Postfix) with ESMTPS id BAB24199A for ; Fri, 19 Apr 2019 22:08:19 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 6B7096DAE6; Fri, 19 Apr 2019 22:08:19 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 22E3E4C84; Fri, 19 Apr 2019 22:08:19 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id x3JM8JST079289; Fri, 19 Apr 2019 22:08:19 GMT (envelope-from cem@FreeBSD.org) Received: (from cem@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id x3JM8HWM079280; Fri, 19 Apr 2019 22:08:17 GMT (envelope-from cem@FreeBSD.org) Message-Id: <201904192208.x3JM8HWM079280@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: cem set sender to cem@FreeBSD.org using -f From: Conrad Meyer 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 X-SVN-Group: head X-SVN-Commit-Author: cem X-SVN-Commit-Paths: in head: lib/libc/gen share/man/man9 sys/conf sys/libkern sys/sys X-SVN-Commit-Revision: 346420 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk X-Loop: FreeBSD.org Sender: owner-src-committers@freebsd.org X-Rspamd-Queue-Id: 3BE246DAEF X-Spamd-Bar: -- Authentication-Results: mx1.freebsd.org X-Spamd-Result: default: False [-2.98 / 15.00]; local_wl_from(0.00)[freebsd.org]; NEURAL_HAM_MEDIUM(-1.00)[-0.999,0]; NEURAL_HAM_SHORT(-0.98)[-0.980,0]; ASN(0.00)[asn:11403, ipnet:96.47.64.0/20, country:US]; NEURAL_HAM_LONG(-1.00)[-1.000,0] Status: O X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.29 List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Date: Tue, 03 Sep 2019 14:07:33 -0000 X-Original-Date: Fri, 19 Apr 2019 22:08:17 +0000 (UTC) X-List-Received-Date: Tue, 03 Sep 2019 14:07:33 -0000 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 + * + * 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 +#include + +/* + * 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. 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,