1 .\" $OpenBSD: arc4random.3,v 1.35 2014/11/25 16:45:24 millert Exp $
3 .\" Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
4 .\" All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 3. All advertising materials mentioning features or use of this software
15 .\" must display the following acknowledgement:
16 .\" This product includes software developed by Niels Provos.
17 .\" 4. The name of the author may not be used to endorse or promote products
18 .\" derived from this software without specific prior written permission.
20 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 .\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 .\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 .\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 .\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 .\" Manual page, using -mandoc macros
40 .Nm arc4random_uniform
41 .Nd random number generator
49 .Fn arc4random_buf "void *buf" "size_t nbytes"
51 .Fn arc4random_uniform "uint32_t upper_bound"
53 This family of functions provides higher quality data than those
60 Use of these functions is encouraged for almost all random number
61 consumption because the other interfaces are deficient in either
62 quality, portability, standardization, or availability.
63 These functions can be called in almost all coding environments,
69 High quality 32-bit pseudo-random numbers are generated very quickly.
70 On each call, a cryptographic pseudo-random number generator is used
71 to generate a new result.
72 One data pool is used for all consumers in a process, so that consumption
73 under program flow can act as additional stirring.
74 The subsystem is re-seeded from the kernel random number subsystem using
76 on a regular basis, and also upon
81 function returns a single 32-bit value.
84 function returns pseudo-random numbers in the range of 0 to
85 .if t 2\u\s731\s10\d\(mi1,
87 and therefore has twice the range of
99 .Fn arc4random_uniform
100 will return a single 32-bit value, uniformly distributed but less than
102 This is recommended over constructions like
103 .Dq Li arc4random() % upper_bound
104 as it avoids "modulo bias" when the upper bound is not a power of two.
105 In the worst case, this function may consume multiple iterations
106 to ensure uniformity; see the source code to understand the problem
109 These functions are always successful, and no return value is
110 reserved to indicate an error.
112 The following produces a drop-in replacement for the traditional
119 .Dl "#define foo4random() (arc4random() % ((unsigned)RAND_MAX + 1))"
125 These functions first appeared in
128 The original version of this random number generator used the
129 RC4 (also known as ARC4) algorithm.
132 it was replaced with the ChaCha20 cipher, and it may be replaced
133 again in the future as cryptographic techniques advance.
135 .Dq A Replacement Call for Random .
139 random number generator was first introduced in
141 The ChaCha20 based implementation was introduced in
143 with obsolete stir and addrandom interfaces removed at the same time.