1 .\" $NetBSD: arc4random.3,v 1.19 2014/11/17 18:41:29 riastradh Exp $
3 .\" Copyright (c) 2014 The NetBSD Foundation, Inc.
4 .\" All rights reserved.
6 .\" This code is derived from software contributed to The NetBSD Foundation
7 .\" by Taylor R. Campbell.
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
12 .\" 1. Redistributions of source code must retain the above copyright
13 .\" notice, this list of conditions and the following disclaimer.
14 .\" 2. Redistributions in binary form must reproduce the above copyright
15 .\" notice, this list of conditions and the following disclaimer in the
16 .\" documentation and/or other materials provided with the distribution.
18 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 .\" POSSIBILITY OF SUCH DAMAGE.
35 .Nm arc4random_uniform ,
38 .Nm arc4random_addrandom
39 .Nd random number generator
47 .Fn arc4random_uniform "uint32_t bound"
49 .Fn arc4random_buf "void *buf" "size_t len"
51 .Fn arc4random_stir "void"
53 .Fn arc4random_addrandom "unsigned char *buf" "int len"
57 family of functions provides a cryptographic pseudorandom number
58 generator automatically seeded from the system entropy pool and safe to
59 use from multiple threads.
61 is designed to prevent an adversary from guessing outputs,
66 and is faster and more convenient than reading from
71 returns an integer in [0, 2^32) chosen independently with uniform
74 .Fn arc4random_uniform
75 returns an integer in [0,
77 chosen independently with uniform distribution.
82 bytes into the memory pointed to by
84 each byte chosen independently from [0, 256) with uniform
88 draws entropy from the operating system and incorporates it into the
89 library's PRNG state to influence future outputs.
91 .Fn arc4random_addrandom
94 bytes, which must be nonnegative, from the buffer
96 into the library's PRNG state to influence future outputs.
98 It is not necessary for an application to call
101 .Fn arc4random_addrandom
105 The first call to any
107 function will initialize the PRNG state unpredictably from the system
112 functions provide the following security properties against three
113 different classes of attackers, assuming enough entropy is provided by
114 the operating system:
115 .Bl -bullet -offset abcd
117 An attacker who has seen some outputs of any of the
119 functions cannot predict past or future unseen outputs.
121 An attacker who has seen the library's PRNG state in memory cannot
122 predict past outputs.
124 An attacker who has seen one process's PRNG state cannot predict past
125 or future outputs in other processes, particularly its parent or
131 means the result of any single request to an
133 function, no matter how short it is.
134 .Sh IMPLEMENTATION NOTES
137 functions are currently implemented using the ChaCha20 pseudorandom
139 For any 32-byte string
142 is a function from 16-byte strings to 64-byte strings.
143 It is conjectured that if
145 is chosen with uniform distribution, then the distribution on
147 is indistinguishable to a computationally bounded adversary from a
148 uniform distribution on all functions from 16-byte strings to 64-byte
151 The PRNG state is a 32-byte ChaCha20 key
157 .Bl -bullet -offset abcd -compact
159 computes the 64-byte quantity
162 .Pf ChaCha20_ Fa s Ns (0),
166 into two 32-byte quantities
183 yields the first four bytes of
187 either yields up to 32 bytes of
189 as output directly, or, for longer
192 as a ChaCha20 key and yields the concatenation
193 .Pf ChaCha20_ Fa k Ns (0)
195 .Pf ChaCha20_ Fa k Ns (1)
197 .Fn arc4random_uniform
200 until it obtains an integer in [2^32 %
202 2^32), and reduces that modulo
205 The PRNG state is per-thread, unless memory allocation fails inside the
206 library, in which case some threads may share global PRNG state with a
208 The global PRNG state is zeroed on fork in the parent via
209 .Xr pthread_atfork 3 ,
210 and the per-thread PRNG state is zeroed on fork in the child via
213 .Dv MAP_INHERIT_ZERO ,
214 so that the child cannot reuse or see the parent's PRNG state.
215 The PRNG state is reseeded automatically from the system entropy pool
216 on the first use of an
218 function after zeroing.
222 function may abort the process in the highly unlikely event that
223 library initialization necessary to implement the security model fails.
227 .Fn arc4random_addrandom
228 may abort the process in the highly unlikely event that the operating
229 system fails to provide entropy.
236 .%A Daniel J. Bernstein
237 .%T ChaCha, a variant of Salsa20
239 .%O Document ID: 4027b5256e17b9796842e6d0f68b0b5e
240 .%U http://cr.yp.to/papers.html#chacha
243 There is no way to get deterministic, reproducible results out of
245 for testing purposes.
249 was chosen for hysterical raisins -- it was originally implemented
250 using the RC4 stream cipher, which has been known since shortly after
251 it was published in 1994 to have observable biases in the output, and
252 is now known to be broken badly enough to admit practical attacks in
254 .\" Bob Jenkins, sci.crypt post dated 1994-09-16, message-id
255 .\" <359qjg$55v$1@mhadg.production.compuserve.com>,
256 .\" https://groups.google.com/d/msg/sci.crypt/JsO3xEATGFA/-wO4ttv7BCYJ
258 .\" Andrew Roos, `A Class of Weak Keys in the RC4 Stream Cipher',
259 .\" sci.crypt posts dated 1995-09-22, message-ids
260 .\" <43u1eh$1j3@hermes.is.co.za> and <44ebge$llf@hermes.is.co.za>.
262 .\" Paul Crowley, `Small bias in RC4 experimentally verified', March
263 .\" 1998, http://www.ciphergoth.org/crypto/rc4/
264 Unfortunately, the library found widespread adoption and the name stuck
265 before anyone recognized that it was silly.
268 .Fn arc4random_addrandom
270 There is no reason to require casts or accept negative lengths:
276 But it's too late to change that now.
278 .Fn arc4random_uniform
279 does not help to choose integers in [0,
281 uniformly at random when
285 The security model of
287 is stronger than many applications need, and stronger than other
288 operating systems provide.
289 For example, applications encrypting messages with random, but not
290 secret, initialization vectors need only prevent an adversary from
291 guessing future outputs, since past outputs will have been published
296 could be marginally faster if it were not necessary to prevent an
297 adversary who sees the state from predicting past outputs.
298 On the other hand, there are applications in the wild that use
300 to generate key material, such as OpenSSH, so for the sake of
302 users it would be imprudent to weaken the security model.
303 On the third hand, relying on the security model of
307 may lead you to an unpleasant surprise on another operating system
308 whose implementation of
310 has a weaker security model.
312 One may be tempted to create new APIs to accommodate different
313 security models and performance constraints without unpleasant
314 surprises on different operating systems.
315 This should not be done lightly, though, because there are already too
316 many different choices, and too many opportunities for programmers to
317 reach for one and pick the wrong one.