2022-08-03 02:03:05 +02:00
|
|
|
.\" $OpenBSD: arc4random.3,v 1.37 2019/09/29 16:30:35 jmc Exp $
|
2015-09-23 19:39:47 +02:00
|
|
|
.\"
|
2005-12-20 21:29:47 +01:00
|
|
|
.\" Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
|
|
|
|
.\" All rights reserved.
|
|
|
|
.\"
|
|
|
|
.\" Redistribution and use in source and binary forms, with or without
|
|
|
|
.\" modification, are permitted provided that the following conditions
|
|
|
|
.\" are met:
|
|
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer.
|
|
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer in the
|
|
|
|
.\" documentation and/or other materials provided with the distribution.
|
|
|
|
.\" 3. All advertising materials mentioning features or use of this software
|
|
|
|
.\" must display the following acknowledgement:
|
|
|
|
.\" This product includes software developed by Niels Provos.
|
|
|
|
.\" 4. The name of the author may not be used to endorse or promote products
|
|
|
|
.\" derived from this software without specific prior written permission.
|
|
|
|
.\"
|
|
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
.\"
|
|
|
|
.\" Manual page, using -mandoc macros
|
|
|
|
.\"
|
2022-08-03 02:03:05 +02:00
|
|
|
.Dd $Mdocdate: September 29 2019 $
|
2022-08-03 02:21:16 +02:00
|
|
|
.Dt arc4random 3bsd
|
2005-12-20 21:29:47 +01:00
|
|
|
.Os
|
|
|
|
.Sh NAME
|
|
|
|
.Nm arc4random ,
|
2010-01-10 00:57:07 +01:00
|
|
|
.Nm arc4random_buf ,
|
|
|
|
.Nm arc4random_uniform ,
|
2005-12-20 21:29:47 +01:00
|
|
|
.Nm arc4random_stir ,
|
|
|
|
.Nm arc4random_addrandom
|
2022-08-03 02:03:05 +02:00
|
|
|
.Nd random number generator
|
2005-12-20 21:29:47 +01:00
|
|
|
.Sh LIBRARY
|
2009-12-11 21:31:48 +01:00
|
|
|
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
|
2019-06-13 23:36:09 +02:00
|
|
|
.ds doc-str-Lb-libbsd \*[str-Lb-libbsd]
|
2009-12-11 21:31:48 +01:00
|
|
|
.Lb libbsd
|
2005-12-20 21:29:47 +01:00
|
|
|
.Sh SYNOPSIS
|
2017-06-05 06:33:47 +02:00
|
|
|
.In stdlib.h
|
|
|
|
(See
|
|
|
|
.Xr libbsd 7
|
|
|
|
for include usage.)
|
2014-08-12 12:32:34 +02:00
|
|
|
.Ft uint32_t
|
2005-12-20 21:29:47 +01:00
|
|
|
.Fn arc4random "void"
|
|
|
|
.Ft void
|
2010-01-10 00:57:07 +01:00
|
|
|
.Fn arc4random_buf "void *buf" "size_t nbytes"
|
2014-08-12 12:32:34 +02:00
|
|
|
.Ft uint32_t
|
|
|
|
.Fn arc4random_uniform "uint32_t upper_bound"
|
2010-01-10 00:57:07 +01:00
|
|
|
.Ft void
|
2005-12-20 21:29:47 +01:00
|
|
|
.Fn arc4random_stir "void"
|
|
|
|
.Ft void
|
|
|
|
.Fn arc4random_addrandom "unsigned char *dat" "int datlen"
|
|
|
|
.Sh DESCRIPTION
|
2015-09-23 19:39:47 +02:00
|
|
|
This family of functions provides higher quality data than those
|
|
|
|
described in
|
|
|
|
.Xr rand 3 ,
|
|
|
|
.Xr random 3 ,
|
|
|
|
and
|
|
|
|
.Xr rand48 3 .
|
|
|
|
.Pp
|
|
|
|
Use of these functions is encouraged for almost all random number
|
|
|
|
consumption because the other interfaces are deficient in either
|
|
|
|
quality, portability, standardization, or availability.
|
|
|
|
These functions can be called in almost all coding environments,
|
|
|
|
including
|
|
|
|
.Xr pthreads 3
|
|
|
|
and
|
|
|
|
.Xr chroot 2 .
|
|
|
|
.Pp
|
|
|
|
High quality 32-bit pseudo-random numbers are generated very quickly.
|
|
|
|
On each call, a cryptographic pseudo-random number generator is used
|
|
|
|
to generate a new result.
|
|
|
|
One data pool is used for all consumers in a process, so that consumption
|
|
|
|
under program flow can act as additional stirring.
|
|
|
|
The subsystem is re-seeded from the kernel random number subsystem using
|
|
|
|
.Xr getentropy 2
|
|
|
|
on a regular basis, and also upon
|
|
|
|
.Xr fork 2 .
|
|
|
|
.Pp
|
2005-12-20 21:29:47 +01:00
|
|
|
The
|
|
|
|
.Fn arc4random
|
2015-09-23 19:39:47 +02:00
|
|
|
function returns a single 32-bit value.
|
2005-12-20 21:29:47 +01:00
|
|
|
.Pp
|
2015-09-23 19:39:47 +02:00
|
|
|
The
|
2010-01-10 00:57:07 +01:00
|
|
|
.Fn arc4random_buf
|
|
|
|
function fills the region
|
|
|
|
.Fa buf
|
|
|
|
of length
|
|
|
|
.Fa nbytes
|
2015-09-23 19:39:47 +02:00
|
|
|
with random data.
|
2010-01-10 00:57:07 +01:00
|
|
|
.Pp
|
|
|
|
.Fn arc4random_uniform
|
2015-09-23 19:39:47 +02:00
|
|
|
will return a single 32-bit value, uniformly distributed but less than
|
2010-01-10 00:57:07 +01:00
|
|
|
.Fa upper_bound .
|
2015-09-23 19:39:47 +02:00
|
|
|
This is recommended over constructions like
|
2010-01-10 00:57:07 +01:00
|
|
|
.Dq Li arc4random() % upper_bound
|
|
|
|
as it avoids "modulo bias" when the upper bound is not a power of two.
|
2015-09-23 19:39:47 +02:00
|
|
|
In the worst case, this function may consume multiple iterations
|
|
|
|
to ensure uniformity; see the source code to understand the problem
|
|
|
|
and solution.
|
2010-01-10 00:57:07 +01:00
|
|
|
.Pp
|
2005-12-20 21:29:47 +01:00
|
|
|
The
|
|
|
|
.Fn arc4random_stir
|
|
|
|
function reads data from
|
2015-09-23 19:39:47 +02:00
|
|
|
.Xr getentropy 2
|
|
|
|
and uses it to re-seed the subsystem via
|
2005-12-20 21:29:47 +01:00
|
|
|
.Fn arc4random_addrandom .
|
|
|
|
.Pp
|
|
|
|
There is no need to call
|
|
|
|
.Fn arc4random_stir
|
|
|
|
before using
|
|
|
|
.Fn arc4random
|
2010-01-10 00:57:07 +01:00
|
|
|
functions family, since
|
|
|
|
they automatically initialize themselves.
|
2015-09-23 19:39:47 +02:00
|
|
|
.Sh RETURN VALUES
|
|
|
|
These functions are always successful, and no return value is
|
|
|
|
reserved to indicate an error.
|
2005-12-20 21:29:47 +01:00
|
|
|
.Sh SEE ALSO
|
|
|
|
.Xr rand 3 ,
|
2015-09-23 19:39:47 +02:00
|
|
|
.Xr rand48 3 ,
|
|
|
|
.Xr random 3
|
2005-12-20 21:29:47 +01:00
|
|
|
.Sh HISTORY
|
2015-09-23 19:39:47 +02:00
|
|
|
These functions first appeared in
|
2017-08-05 13:42:56 +02:00
|
|
|
.Ox 2.1 ,
|
|
|
|
.Fx 3.0 ,
|
|
|
|
.Nx 1.6 ,
|
|
|
|
and
|
|
|
|
.Dx 1.0 .
|
2015-09-23 19:39:47 +02:00
|
|
|
.Pp
|
|
|
|
The original version of this random number generator used the
|
|
|
|
RC4 (also known as ARC4) algorithm.
|
|
|
|
In
|
|
|
|
.Ox 5.5
|
|
|
|
it was replaced with the ChaCha20 cipher, and it may be replaced
|
|
|
|
again in the future as cryptographic techniques advance.
|
|
|
|
A good mnemonic is
|
|
|
|
.Dq A Replacement Call for Random .
|