xref: /freebsd/lib/libsys/getrandom.2 (revision 8269e7673cf033aba67dab8264fe719920c70f87)
1*8269e767SBrooks Davis.\" Copyright 2020, 2018 Conrad Meyer <cem@FreeBSD.org>.  All rights reserved.
2*8269e767SBrooks Davis.\"
3*8269e767SBrooks Davis.\" Redistribution and use in source and binary forms, with or without
4*8269e767SBrooks Davis.\" modification, are permitted provided that the following conditions
5*8269e767SBrooks Davis.\" are met:
6*8269e767SBrooks Davis.\" 1. Redistributions of source code must retain the above copyright
7*8269e767SBrooks Davis.\"    notice, this list of conditions and the following disclaimer.
8*8269e767SBrooks Davis.\" 2. Redistributions in binary form must reproduce the above copyright
9*8269e767SBrooks Davis.\"    notice, this list of conditions and the following disclaimer in the
10*8269e767SBrooks Davis.\"    documentation and/or other materials provided with the distribution.
11*8269e767SBrooks Davis.\"
12*8269e767SBrooks Davis.\" THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
13*8269e767SBrooks Davis.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14*8269e767SBrooks Davis.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15*8269e767SBrooks Davis.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
16*8269e767SBrooks Davis.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17*8269e767SBrooks Davis.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
18*8269e767SBrooks Davis.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
19*8269e767SBrooks Davis.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20*8269e767SBrooks Davis.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
21*8269e767SBrooks Davis.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
22*8269e767SBrooks Davis.\" SUCH DAMAGE.
23*8269e767SBrooks Davis.\"
24*8269e767SBrooks Davis.Dd January 12, 2020
25*8269e767SBrooks Davis.Dt GETRANDOM 2
26*8269e767SBrooks Davis.Os
27*8269e767SBrooks Davis.Sh NAME
28*8269e767SBrooks Davis.Nm getrandom
29*8269e767SBrooks Davis.Nd get random data
30*8269e767SBrooks Davis.Sh LIBRARY
31*8269e767SBrooks Davis.Lb libc
32*8269e767SBrooks Davis.Sh SYNOPSIS
33*8269e767SBrooks Davis.In sys/random.h
34*8269e767SBrooks Davis.Ft ssize_t
35*8269e767SBrooks Davis.Fn getrandom "void *buf" "size_t buflen" "unsigned int flags"
36*8269e767SBrooks Davis.Sh DESCRIPTION
37*8269e767SBrooks Davis.Fn getrandom
38*8269e767SBrooks Davisfills
39*8269e767SBrooks Davis.Fa buf
40*8269e767SBrooks Daviswith up to
41*8269e767SBrooks Davis.Fa buflen
42*8269e767SBrooks Davisbytes of random data.
43*8269e767SBrooks Davis.Pp
44*8269e767SBrooks DavisThe
45*8269e767SBrooks Davis.Fa flags
46*8269e767SBrooks Davisargument may include zero or more of the following:
47*8269e767SBrooks Davis.Bl -tag -width _GRND_NONBLOCK_
48*8269e767SBrooks Davis.It Ql GRND_NONBLOCK
49*8269e767SBrooks DavisReturn
50*8269e767SBrooks Davis.Er EAGAIN
51*8269e767SBrooks Davisinstead of blocking, if the
52*8269e767SBrooks Davis.Xr random 4
53*8269e767SBrooks Davisdevice has not yet been seeded.
54*8269e767SBrooks DavisBy default,
55*8269e767SBrooks Davis.Fn getrandom
56*8269e767SBrooks Daviswill block until the device is seeded.
57*8269e767SBrooks Davis.It Ql GRND_RANDOM
58*8269e767SBrooks DavisThis flag does nothing on
59*8269e767SBrooks Davis.Fx .
60*8269e767SBrooks Davis.Pa /dev/random
61*8269e767SBrooks Davisand
62*8269e767SBrooks Davis.Pa /dev/urandom
63*8269e767SBrooks Davisare identical.
64*8269e767SBrooks Davis.It Ql GRND_INSECURE
65*8269e767SBrooks DavisThis flag is treated as an alternative name for
66*8269e767SBrooks Davis.Dv GRND_NONBLOCK .
67*8269e767SBrooks DavisIt is provided solely for API compatibility with Linux.
68*8269e767SBrooks Davis.El
69*8269e767SBrooks Davis.Pp
70*8269e767SBrooks DavisIf the
71*8269e767SBrooks Davis.Xr random 4
72*8269e767SBrooks Davisdevice has been seeded, reads of up to 256 bytes will always return as many
73*8269e767SBrooks Davisbytes as requested and will not be interrupted by signals.
74*8269e767SBrooks Davis.Sh RETURN VALUES
75*8269e767SBrooks DavisUpon successful completion, the number of bytes which were actually read is
76*8269e767SBrooks Davisreturned.
77*8269e767SBrooks DavisFor requests larger than 256 bytes, this can be fewer bytes than were
78*8269e767SBrooks Davisrequested.
79*8269e767SBrooks DavisOtherwise, -1 is returned and the global variable
80*8269e767SBrooks Davis.Va errno
81*8269e767SBrooks Davisis set to indicate the error.
82*8269e767SBrooks Davis.Sh ERRORS
83*8269e767SBrooks DavisThe
84*8269e767SBrooks Davis.Fn getrandom
85*8269e767SBrooks Davisoperation returns the following errors:
86*8269e767SBrooks Davis.Bl -tag -width Er
87*8269e767SBrooks Davis.It Bq Er EAGAIN
88*8269e767SBrooks DavisThe
89*8269e767SBrooks Davis.Ql GRND_NONBLOCK
90*8269e767SBrooks Davis(or
91*8269e767SBrooks Davis.Ql GRND_INSECURE )
92*8269e767SBrooks Davisflag was set and the
93*8269e767SBrooks Davis.Xr random 4
94*8269e767SBrooks Davisdevice was not yet seeded.
95*8269e767SBrooks Davis.It Bq Er EFAULT
96*8269e767SBrooks DavisThe
97*8269e767SBrooks Davis.Fa buf
98*8269e767SBrooks Davisparameter points to an invalid address.
99*8269e767SBrooks Davis.It Bq Er EINTR
100*8269e767SBrooks DavisThe sleep was interrupted by a signal.
101*8269e767SBrooks Davis.It Bq Er EINVAL
102*8269e767SBrooks DavisAn invalid
103*8269e767SBrooks Davis.Fa flags
104*8269e767SBrooks Daviswas specified.
105*8269e767SBrooks Davis.It Bq Er EINVAL
106*8269e767SBrooks DavisThe requested
107*8269e767SBrooks Davis.Fa buflen
108*8269e767SBrooks Daviswas larger than
109*8269e767SBrooks Davis.Dv IOSIZE_MAX .
110*8269e767SBrooks Davis.El
111*8269e767SBrooks Davis.Sh SEE ALSO
112*8269e767SBrooks Davis.Xr arc4random 3 ,
113*8269e767SBrooks Davis.Xr getentropy 3 ,
114*8269e767SBrooks Davis.Xr random 4
115*8269e767SBrooks Davis.Sh STANDARDS
116*8269e767SBrooks Davis.Fn getrandom
117*8269e767SBrooks Davisis non-standard.
118*8269e767SBrooks DavisIt is present in Linux.
119*8269e767SBrooks Davis.Sh HISTORY
120*8269e767SBrooks DavisThe
121*8269e767SBrooks Davis.Fn getrandom
122*8269e767SBrooks Davissystem call first appeared in
123*8269e767SBrooks Davis.Fx 12.0 .
124*8269e767SBrooks Davis.Sh CAVEATS
125*8269e767SBrooks DavisUnlike Linux, the
126*8269e767SBrooks Davis.Dv GRND_INSECURE
127*8269e767SBrooks Davisflag on
128*8269e767SBrooks Davis.Fx
129*8269e767SBrooks Davisdoes not produce any output before the
130*8269e767SBrooks Davis.Xr random 4
131*8269e767SBrooks Davisdevice is seeded.
132