]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/man/man9/random.9
disk(9): Fix a few mandoc related errors
[FreeBSD/FreeBSD.git] / share / man / man9 / random.9
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
62 The
63 .Fn arc4random
64 and
65 .Fn arc4random_buf
66 functions will return very good quality random numbers, suited for
67 security-related purposes.
68 Both are wrappers around the underlying
69 .Fn arc4rand
70 interface.
71 .Fn arc4random
72 returns a 32-bit random value, while
73 .Fn arc4random_buf
74 fills
75 .Fa ptr
76 with
77 .Fa len
78 bytes of random data.
79 .Pp
80 The
81 .Fn arc4rand
82 CSPRNG
83 is seeded from the
84 .Xr random 4
85 kernel abstract entropy device.
86 Automatic reseeding happens at unspecified time and bytes (of output)
87 intervals.
88 A reseed can be forced by passing a non-zero
89 .Fa reseed
90 value.
91 .Pp
92 The
93 .Fn read_random
94 function is used to read entropy directly from the kernel abstract entropy
95 device.
96 .Fn read_random
97 blocks if and until the entropy device is seeded.
98 The provided
99 .Fa buffer
100 is filled with no more than
101 .Fa count
102 bytes.
103 It is strongly advised that
104 .Fn read_random
105 is not used directly;
106 instead, use the
107 .Fn arc4rand
108 family of functions.
109 .Pp
110 The
111 .Fn is_random_seeded
112 function can be used to check in advance if
113 .Fn read_random
114 will block.
115 (If random is seeded, it will not block.)
116 .Pp
117 The
118 .Fn read_random_uio
119 function behaves identically to
120 .Xr read 2
121 on
122 .Pa /dev/random .
123 The
124 .Fa uio
125 argument points to a buffer where random data should be stored.
126 If
127 .Fa nonblock
128 is true and the random device is not seeded, this function does not return any
129 data.
130 Otherwise, this function may block interruptibly until the random device is seeded.
131 If the function is interrupted before the random device is seeded, no data is
132 returned.
133 .Pp
134 The deprecated
135 .Xr random 9
136 function will produce a sequence of pseudorandom numbers using a similar weak
137 linear congruential generator as
138 .Xr rand 3
139 (the 1988 Park-Miller LCG).
140 It is obsolete and scheduled to be removed in
141 .Fx 13.0 .
142 It is strongly advised that the
143 .Xr random 9
144 function not be used to generate random numbers.
145 See
146 .Sx SECURITY CONSIDERATIONS .
147 .Sh RETURN VALUES
148 The
149 .Fn arc4rand
150 function uses the Chacha20 algorithm to generate a pseudo-random sequence of
151 bytes.
152 The
153 .Fn arc4random
154 function uses
155 .Fn arc4rand
156 to generate pseudo-random numbers
157 in the range from 0 to
158 .if t 2\u\s732\s10\d\(mi1.
159 .if n (2**32)\(mi1.
160 .Pp
161 The
162 .Fn read_random
163 function returns
164 the number of bytes placed in
165 .Fa buffer .
166 .Pp
167 .Fn read_random_uio
168 returns zero when successful,
169 otherwise an error code is returned.
170 .Sh ERRORS
171 .Fn read_random_uio
172 may fail if:
173 .Bl -tag -width Er
174 .It Bq Er EFAULT
175 .Fa uio
176 points to an invalid memory region.
177 .It Bq Er EWOULDBLOCK
178 The random device is unseeded and
179 .Fa nonblock
180 is true.
181 .El
182 .Sh AUTHORS
183 .An Dan Moschuk
184 wrote
185 .Fn arc4random .
186 .An Mark R V Murray
187 wrote
188 .Fn read_random .
189 .Sh SECURITY CONSIDERATIONS
190 Do not use
191 .Fn random
192 in new code.
193 .Pp
194 It is important to remember that the
195 .Fn random
196 function is entirely predictable.
197 It is easy for attackers to predict future output of
198 .Fn random
199 by recording some generated values.
200 We cannot emphasize strongly enough that
201 .Fn random
202 must not be used to generate values that are intended to be unpredictable.