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 $
2
2
.\"
3
3
.\" Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
4
4
.\" All rights reserved.
30
30
.\"
31
31
.\" Manual page, using -mandoc macros
32
32
.\"
33
- .Dd $Mdocdate : June 5 2013 $
33
+ .Dd $Mdocdate : October 21 2013 $
34
34
.Dt ARC4RANDOM 3
35
35
.Os
36
36
.Sh NAME
37
37
.Nm arc4random ,
38
38
.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
43
41
.Sh SYNOPSIS
44
42
.In stdlib.h
45
43
.Ft u_int32_t
48
46
.Fn arc4random_buf " void *buf" " size_t nbytes"
49
47
.Ft u_int32_t
50
48
.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"
55
49
.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
74
52
.Xr rand 3 ,
75
53
.Xr random 3 ,
76
54
and
77
55
.Xr drand48 3 .
78
56
.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
79
80
.Fn arc4random_buf
80
81
fills the region
81
82
.Fa buf
82
83
of length
83
84
.Fa nbytes
84
- with ARC4-derived random data.
85
+ with random data.
85
86
.Pp
86
87
.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
88
89
.Fa upper_bound .
89
- .Fn arc4random_uniform
90
- is recommended over constructions like
90
+ This is recommended over constructions like
91
91
.Dq Li arc4random() % upper_bound
92
92
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.
93
96
.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.
110
97
.Sh RETURN VALUES
111
98
These functions are always successful, and no return value is
112
99
reserved to indicate an error.
@@ -115,21 +102,16 @@ reserved to indicate an error.
115
102
.Xr rand48 3 ,
116
103
.Xr random 3
117
104
.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 .
133
105
.Pp
134
106
These functions first appeared in
135
107
.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 .
0 commit comments