xref: /freebsd/share/man/man9/random.9 (revision 3cbb4cc200f8a0ad7ed08233425ea54524a21f1c)
1.\"
2.\" Copyright (c) 2015
3.\"	Mark R V Murray
4.\" Copyright (c) 2000
5.\"	The Regents of the University of California.  All rights reserved.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
17.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
20.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26.\"
27.\" $FreeBSD$
28.\" "
29.Dd December 26, 2019
30.Dt RANDOM 9
31.Os
32.Sh NAME
33.Nm arc4rand ,
34.Nm arc4random ,
35.Nm arc4random_buf ,
36.Nm is_random_seeded ,
37.Nm random ,
38.Nm read_random ,
39.Nm read_random_uio
40.Nd supply pseudo-random numbers
41.Sh SYNOPSIS
42.In sys/libkern.h
43.Ft uint32_t
44.Fn arc4random "void"
45.Ft void
46.Fn arc4random_buf "void *ptr" "size_t len"
47.Ft void
48.Fn arc4rand "void *ptr" "u_int length" "int reseed"
49.Pp
50.In sys/random.h
51.Ft bool
52.Fn is_random_seeded "void"
53.Ft void
54.Fn read_random "void *buffer" "int count"
55.Ft int
56.Fn read_random_uio "struct uio *uio" "bool nonblock"
57.Ss LEGACY ROUTINES
58.In sys/libkern.h
59.Ft u_long
60.Fn random "void"
61.Sh DESCRIPTION
62The
63.Fn arc4random
64and
65.Fn arc4random_buf
66functions will return very good quality random numbers, suited for
67security-related purposes.
68Both are wrappers around the underlying
69.Fn arc4rand
70interface.
71.Fn arc4random
72returns a 32-bit random value, while
73.Fn arc4random_buf
74fills
75.Fa ptr
76with
77.Fa len
78bytes of random data.
79.Pp
80The
81.Fn arc4rand
82CSPRNG
83is seeded from the
84.Xr random 4
85kernel abstract entropy device.
86Automatic reseeding happens at unspecified time and bytes (of output)
87intervals.
88A reseed can be forced by passing a non-zero
89.Fa reseed
90value.
91.Pp
92The
93.Fn read_random
94function is used to read entropy directly from the kernel abstract entropy
95device.
96.Fn read_random
97blocks if and until the entropy device is seeded.
98The provided
99.Fa buffer
100is filled with no more than
101.Fa count
102bytes.
103It is strongly advised that
104.Fn read_random
105is not used directly;
106instead, use the
107.Fn arc4rand
108family of functions.
109.Pp
110The
111.Fn is_random_seeded
112function can be used to check in advance if
113.Fn read_random
114will block.
115(If random is seeded, it will not block.)
116.Pp
117The
118.Fn read_random_uio
119function behaves identically to
120.Xr read 2
121on
122.Pa /dev/random .
123The
124.Fa uio
125argument points to a buffer where random data should be stored.
126If
127.Fa nonblock
128is true and the random device is not seeded, this function does not return any
129data.
130Otherwise, this function may block interruptibly until the random device is seeded.
131If the function is interrupted before the random device is seeded, no data is
132returned.
133.Pp
134The deprecated
135.Xr random 9
136function will produce a sequence of pseudorandom numbers using a similar weak
137linear congruential generator as
138.Xr rand 3
139(the 1988 Park-Miller LCG).
140It is obsolete and scheduled to be removed in
141.Fx 13.0 .
142It is strongly advised that the
143.Xr random 9
144function not be used to generate random numbers.
145See
146.Sx SECURITY CONSIDERATIONS .
147.Sh RETURN VALUES
148The
149.Fn arc4rand
150function uses the Chacha20 algorithm to generate a pseudo-random sequence of
151bytes.
152The
153.Fn arc4random
154function uses
155.Fn arc4rand
156to generate pseudo-random numbers
157in the range from 0 to
158.if t 2\u\s732\s10\d\(mi1.
159.if n (2**32)\(mi1.
160.Pp
161The
162.Fn read_random
163function returns
164the number of bytes placed in
165.Fa buffer .
166.Pp
167.Fn read_random_uio
168returns zero when successful,
169otherwise an error code is returned.
170.Sh ERRORS
171.Fn read_random_uio
172may fail if:
173.Bl -tag -width Er
174.It Bq Er EFAULT
175.Fa uio
176points to an invalid memory region.
177.It Bq Er EWOULDBLOCK
178The random device is unseeded and
179.Fa nonblock
180is true.
181.El
182.Sh AUTHORS
183.An Dan Moschuk
184wrote
185.Fn arc4random .
186.An Mark R V Murray
187wrote
188.Fn read_random .
189.Sh SECURITY CONSIDERATIONS
190Do not use
191.Fn random
192in new code.
193.Pp
194It is important to remember that the
195.Fn random
196function is entirely predictable.
197It is easy for attackers to predict future output of
198.Fn random
199by recording some generated values.
200We cannot emphasize strongly enough that
201.Fn random
202must not be used to generate values that are intended to be unpredictable.
203