Skip to content

Commit c818251

Browse files
author
deraadt
committed
update documentation for these functions. They now state the case regarding
pthreads and chroot, and safe initialization without /dev/random and such junk. If you are in control of an arc4random in another library, please look at these pages and get caught up. ok various
1 parent e1968c9 commit c818251

File tree

2 files changed

+67
-70
lines changed

2 files changed

+67
-70
lines changed

lib/libc/crypt/arc4random.3

Lines changed: 45 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.\" $OpenBSD: arc4random.3,v 1.29 2013/06/05 03:39:22 tedu Exp $
1+
.\" $OpenBSD: arc4random.3,v 1.30 2013/10/21 20:35:09 deraadt Exp $
22
.\"
33
.\" Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
44
.\" All rights reserved.
@@ -30,16 +30,14 @@
3030
.\"
3131
.\" Manual page, using -mandoc macros
3232
.\"
33-
.Dd $Mdocdate: June 5 2013 $
33+
.Dd $Mdocdate: October 21 2013 $
3434
.Dt ARC4RANDOM 3
3535
.Os
3636
.Sh NAME
3737
.Nm arc4random ,
3838
.Nm arc4random_buf ,
39-
.Nm arc4random_uniform ,
40-
.Nm arc4random_stir ,
41-
.Nm arc4random_addrandom
42-
.Nd arc4 random number generator
39+
.Nm arc4random_uniform
40+
.Nd random number generator
4341
.Sh SYNOPSIS
4442
.In stdlib.h
4543
.Ft u_int32_t
@@ -48,65 +46,54 @@
4846
.Fn arc4random_buf "void *buf" "size_t nbytes"
4947
.Ft u_int32_t
5048
.Fn arc4random_uniform "u_int32_t upper_bound"
51-
.Ft void
52-
.Fn arc4random_stir "void"
53-
.Ft void
54-
.Fn arc4random_addrandom "u_char *dat" "int datlen"
5549
.Sh DESCRIPTION
56-
The
57-
.Fn arc4random
58-
function provides a high quality 32-bit pseudo-random
59-
number very quickly.
60-
.Fn arc4random
61-
seeds itself on a regular basis from the kernel strong random number
62-
subsystem described in
63-
.Xr random 4 .
64-
On each call, an ARC4 generator is used to generate a new result.
65-
The
66-
.Fn arc4random
67-
function uses the ARC4 cipher key stream generator,
68-
which uses 8*8 8-bit S-Boxes.
69-
The S-Boxes can be in about (2**1700) states.
70-
.Pp
71-
.Fn arc4random
72-
provides a cryptographically secure pseudo-random number generator,
73-
as opposed to the fast but poor quality interfaces described in
50+
This family of functions provides higher quality data than those
51+
described in
7452
.Xr rand 3 ,
7553
.Xr random 3 ,
7654
and
7755
.Xr drand48 3 .
7856
.Pp
57+
Use of these functions is encouraged for almost all random number
58+
consumption because the other interfaces are deficient in either
59+
quality, portability, standardization, or availability.
60+
These functions can be called in almost all coding environments,
61+
including
62+
.Xr pthreads 3
63+
and
64+
.Xr chroot 2 .
65+
.Pp
66+
High quality 32-bit pseudo-random numbers are generated very quickly.
67+
On each call, a cryptographic pseudo-random number generator is used
68+
to generate a new result.
69+
One data pool is used for all consumers in a process, so that consumption
70+
under program flow can act as additional stirring.
71+
The subsystem is re-seeded from the kernel random number subsystem using
72+
.Xr sysctl 3
73+
on a regular basis, and also upon
74+
.Xr fork 2 .
75+
.Pp
76+
The
77+
.Fn arc4random
78+
function returns a single 32-bit value.
79+
.Pp
7980
.Fn arc4random_buf
8081
fills the region
8182
.Fa buf
8283
of length
8384
.Fa nbytes
84-
with ARC4-derived random data.
85+
with random data.
8586
.Pp
8687
.Fn arc4random_uniform
87-
will return a uniformly distributed random number less than
88+
will return a single 32-bit value, uniformly distributed but less than
8889
.Fa upper_bound .
89-
.Fn arc4random_uniform
90-
is recommended over constructions like
90+
This is recommended over constructions like
9191
.Dq Li arc4random() % upper_bound
9292
as it avoids "modulo bias" when the upper bound is not a power of two.
93+
In the worst case, this function may consume multiple iterations
94+
to ensure uniformity; see the source code to understand the problem
95+
and solution.
9396
.Pp
94-
The
95-
.Fn arc4random_stir
96-
function reads data using
97-
.Xr sysctl 3
98-
from
99-
.Va kern.arandom
100-
and uses it to permute the S-Boxes via
101-
.Fn arc4random_addrandom .
102-
.Pp
103-
There is no need to call
104-
.Fn arc4random_stir
105-
before using
106-
.Fn arc4random ,
107-
since
108-
.Fn arc4random
109-
automatically initializes itself.
11097
.Sh RETURN VALUES
11198
These functions are always successful, and no return value is
11299
reserved to indicate an error.
@@ -115,21 +102,16 @@ reserved to indicate an error.
115102
.Xr rand48 3 ,
116103
.Xr random 3
117104
.Sh HISTORY
118-
An algorithm called
119-
.Pa RC4
120-
was designed by RSA Data Security, Inc.
121-
It was considered a trade secret.
122-
Because it was a trade secret, it obviously could not be patented.
123-
A clone of this was posted anonymously to USENET and confirmed to
124-
be equivalent by several sources who had access to the original cipher.
125-
Because of the trade secret situation, RSA Data Security, Inc. could
126-
do nothing about the release of the
127-
.Ql Alleged RC4
128-
algorithm.
129-
Since
130-
.Pa RC4
131-
was trademarked, the cipher is now referred to as
132-
.Pa ARC4 .
133105
.Pp
134106
These functions first appeared in
135107
.Ox 2.1 .
108+
.Pp
109+
The original version of this random number generator used the
110+
RC4 (also known as ARC4) algorithm.
111+
In
112+
.Ox 5.5 ,
113+
it was replaced with the ChaCha20 cipher, and it may be replaced
114+
again in the future as cryptographic techniques advance.
115+
techniques advance.
116+
A good mnemonic is
117+
.Dq A Replacement Call for Random .

share/man/man9/arc4random.9

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.\" $OpenBSD: arc4random.9,v 1.6 2013/06/04 19:27:03 schwarze Exp $
1+
.\" $OpenBSD: arc4random.9,v 1.7 2013/10/21 20:35:09 deraadt Exp $
22
.\"
33
.\" Copyright (c) 1996,2000 Michael Shalayeff
44
.\" All rights reserved.
@@ -23,7 +23,7 @@
2323
.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2424
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2525
.\"
26-
.Dd $Mdocdate: June 4 2013 $
26+
.Dd $Mdocdate: October 21 2013 $
2727
.Dt ARC4RANDOM 9
2828
.Os
2929
.Sh NAME
@@ -53,6 +53,13 @@
5353
.Fn arc4random_uniform "u_int32_t upper_bound"
5454
.Sh DESCRIPTION
5555
The
56+
.Fn add_random
57+
functions below insert random input into a confusing mesh of
58+
hash functions to create a pool to be used by the
59+
.Pa arc4random
60+
series of functions.
61+
.Pp
62+
The
5663
.Fn add_mouse_randomness ,
5764
.Fn add_tty_randomness ,
5865
.Fn add_net_randomness ,
@@ -61,9 +68,8 @@ and
6168
.Fn add_audio_randomness
6269
routines are used to
6370
supply data for the random data source device for further processing.
64-
The processing involves calculating inter-event timedelta and measuring
65-
entropy contained in the resulting delta-T; the argument data supplied
66-
is just mixed into the pool and does not increase the entropy counter.
71+
The processing involves calculating inter-event timedelta and inserting
72+
it into a pool which is cryptographically mixed multiple times.
6773
.Pp
6874
.Fn add_true_randomness
6975
does not involve the usual timing calculations,
@@ -78,13 +84,22 @@ mostly by means of timing the random driver's operations.
7884
.Fn arc4random
7985
and
8086
.Fn arc4random_buf
81-
provide random numbers.
82-
They may be called safely in any kernel context.
87+
provide random numbers and are intended to be called in any
88+
circumstance where random numbers are required.
8389
.Pp
8490
.Fn arc4random_uniform
8591
will return a uniformly distributed random number less than
8692
.Fa upper_bound ,
8793
avoiding "modulo bias" when the upper bound is not a power of two.
94+
In the worst case, this function may consume multiple iterations
95+
to ensure uniformity; see the source code to understand the problem
96+
and solution.
97+
.Pp
98+
.Fn arc4random ,
99+
.Fn arc4random_buf ,
100+
and
101+
.Fn arc4random_uniform
102+
may be called in nearly any kernel context.
88103
.Sh SEE ALSO
89104
.Xr arc4random 3 ,
90105
.Xr pchb 4 ,

0 commit comments

Comments
 (0)