Copyright (c) 2020 The NetBSD Foundation, Inc.
All rights reserved.
This code is derived from software contributed to The NetBSD Foundation
by Nia Alarie.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
.Dd August 28, 2024 .Dt GETENTROPY 3 .Os .Sh NAME .Nm getentropy .Nd generate uniform random seeds from system entropy for cryptography .Sh LIBRARY .Lb libc .Sh SYNOPSIS n unistd.h .Ft int .Fn getentropy "void *buf" "size_t buflen" n limits.h
p .Li #define GETENTROPY_MAX 256 .Sh DESCRIPTION The .Nm function fills .Fa buf with exactly .Fa buflen independent uniform random bytes derived from the system's entropy pool.
p The output of .Nm is meant to be unpredictable to an adversary and fit for use in cryptography. See .Sx CAVEATS below.
p .Nm is meant for seeding random number generators, not for direct use by applications; most applications should use .Xr arc4random 3 .
p .Fa buflen must be at most 256. .Sh RETURN VALUES .Rv -std getentropy .Sh ERRORS .Nm will succeed unless: l -tag -width Er t Bq Er EFAULT The .Fa buf argument points to an invalid memory address. t Bq Er EINVAL More than 256 bytes were requested. .El .Sh CAVEATS Security can only be guaranteed relative to whatever unpredictable physical processes or secret seed material are available to the system; see .Xr entropy 7 .
p On systems which have no hardware random number generator and which have not had secret seed material loaded, .Nx makes a reasonable effort to incorporate samples from various physical processes available to it that might be unpredictable from random jitter in timing.
p However, the .Nm interface alone can make no security guarantees without a physical system configuration that includes random number generation hardware or secret seed material from such hardware on another machine.
p .Nx attempts to reseed the system entropy pool when it has detected the system has been cloned as a guest in a virtual machine, so that subsequent calls to .Nm in the clones yield independent outputs. However, this relies on the virtual machine host to notify the guest, e.g. through the .Xr acpivmgenid 4 device, and even so there is an unavoidable small window of time between when the virtual machine is actually cloned and when the system is reseeded during which .Nm may yield identical outputs in the clones. .Sh SEE ALSO .Xr arc4random 3 , .Xr rnd 4 , .Xr entropy 7 .Sh STANDARDS The .Nm function conforms to .St -p1003.1-2024 . .Sh HISTORY The .Nm function first appeared in .Ox 5.6 , then in .Fx 12.0 , and in .Nx 10.0 .