2020-07-15 16:47:40 +03:00
|
|
|
.\" $NetBSD: rnd.9,v 1.25.18.1 2020/07/15 13:47:40 martin Exp $
|
1997-11-01 13:00:36 +03:00
|
|
|
.\"
|
|
|
|
.\" Copyright (c) 1997 The NetBSD Foundation, Inc.
|
|
|
|
.\" All rights reserved.
|
|
|
|
.\"
|
|
|
|
.\" This documentation is derived from text contributed to The NetBSD
|
|
|
|
.\" Foundation by S.P.Zeidler (aka stargazer).
|
|
|
|
.\"
|
|
|
|
.\" 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.
|
|
|
|
.\"
|
2014-08-10 21:17:04 +04:00
|
|
|
.Dd August 10, 2014
|
1997-11-01 13:00:36 +03:00
|
|
|
.Dt RND 9
|
1999-03-16 03:40:46 +03:00
|
|
|
.Os
|
1997-11-01 13:00:36 +03:00
|
|
|
.Sh NAME
|
1997-11-12 03:47:32 +03:00
|
|
|
.Nm RND ,
|
|
|
|
.Nm rnd_attach_source ,
|
|
|
|
.Nm rnd_detach_source ,
|
|
|
|
.Nm rnd_add_data ,
|
2020-07-15 16:47:40 +03:00
|
|
|
.Nm rnd_add_data_sync ,
|
1997-11-12 03:47:32 +03:00
|
|
|
.Nm rnd_add_uint32
|
1997-11-01 13:00:36 +03:00
|
|
|
.Nd functions to make a device available for entropy collection
|
|
|
|
.Sh SYNOPSIS
|
2015-04-14 01:23:54 +03:00
|
|
|
.In sys/rndsource.h
|
1997-11-01 13:00:36 +03:00
|
|
|
.Ft void
|
2020-07-15 16:47:40 +03:00
|
|
|
.Fn rndsource_setcb "krndsource_t *rnd_source" "void (*callback)(size_t, void *)" "void *cookie"
|
|
|
|
.Ft void
|
2012-07-12 04:07:36 +04:00
|
|
|
.Fn rnd_attach_source "krndsource_t *rnd_source" "char *devname" "uint32_t source_type" "uint32_t flags"
|
1997-11-01 13:00:36 +03:00
|
|
|
.Ft void
|
2012-07-12 04:07:36 +04:00
|
|
|
.Fn rnd_detach_source "krndsource_t *rnd_source"
|
1997-11-01 13:00:36 +03:00
|
|
|
.Ft void
|
2012-07-12 04:07:36 +04:00
|
|
|
.Fn rnd_add_data "krndsource_t *rnd_source" "void *data" "uint32_t len" "uint32_t entropy"
|
1997-11-01 13:00:36 +03:00
|
|
|
.Ft void
|
2020-07-15 16:47:40 +03:00
|
|
|
.Fn rnd_add_data_sync "krndsource_t *rnd_source" "void *data" "uint32_t len" "uint32_t entropy"
|
|
|
|
.Ft void
|
2012-07-12 04:07:36 +04:00
|
|
|
.Fn rnd_add_uint32 "krndsource_t *rnd_source" "uint32_t datum"
|
1997-11-01 13:00:36 +03:00
|
|
|
.Sh DESCRIPTION
|
|
|
|
These
|
2000-11-07 09:43:24 +03:00
|
|
|
.Nm
|
|
|
|
functions make a device available for entropy collection for
|
2011-12-18 00:05:38 +04:00
|
|
|
the kernel entropy pool, which provides key material for the
|
|
|
|
.Xr cprng 9
|
|
|
|
and
|
|
|
|
.Xr rnd 4
|
2011-12-18 01:25:12 +04:00
|
|
|
.Pa ( /dev/random )
|
|
|
|
interfaces.
|
2000-11-07 09:43:24 +03:00
|
|
|
.Pp
|
2020-07-15 16:47:40 +03:00
|
|
|
The caller must zero an
|
|
|
|
.Fa rnd_source
|
|
|
|
object before using it.
|
1997-11-01 13:00:36 +03:00
|
|
|
Ideally the first argument
|
|
|
|
.Fa rnd_source
|
2001-09-04 06:51:15 +04:00
|
|
|
of these functions gets included in the devices' entity struct,
|
2011-12-18 00:05:38 +04:00
|
|
|
but any means to permanently (statically) attach one such argument
|
2002-10-14 17:43:14 +04:00
|
|
|
to one incarnation of the device is ok.
|
|
|
|
Do not share
|
2001-09-04 06:51:15 +04:00
|
|
|
.Fa rnd_source
|
2020-07-15 16:47:40 +03:00
|
|
|
structures between two devices, and make sure to serialize all access
|
|
|
|
to each
|
|
|
|
.Fa rnd_source ,
|
|
|
|
for example with
|
|
|
|
.Xr mutex 9 .
|
2000-11-07 09:43:24 +03:00
|
|
|
.Bl -tag -width 8n
|
2020-07-15 16:47:40 +03:00
|
|
|
.It Fn rndsource_setcb "krndsource_t *rnd_source" "void (*callback)(size_t, void *)" "void *cookie"
|
|
|
|
This function sets a callback to be invoked when the kernel entropy
|
|
|
|
pool is hungry.
|
|
|
|
It is optional; if used, it must be used
|
|
|
|
.Em before
|
|
|
|
.Fn rnd_attach_source ,
|
|
|
|
and the caller must pass
|
|
|
|
.Dv RND_FLAG_HASCB
|
|
|
|
to
|
|
|
|
.Fn rnd_attach_source
|
|
|
|
in order for the callback to be used.
|
|
|
|
The callback is invoked as
|
|
|
|
.Fa callback ( Fa nbytes , Fa cookie ) ,
|
|
|
|
where
|
|
|
|
.Fa nbytes
|
|
|
|
is the number of bytes requested for the entropy pool, and
|
|
|
|
.Fa cookie
|
|
|
|
is the cookie that was passed to
|
|
|
|
.Fn rndsource_setcb .
|
|
|
|
The callback normally does one of two things:
|
|
|
|
.Bl -dash
|
|
|
|
.It
|
|
|
|
Sends a request to a hardware device for entropy and returns.
|
|
|
|
The hardware will later return data asynchronously by an interrupt, and
|
|
|
|
the callback will use
|
|
|
|
.Fn rnd_add_data
|
|
|
|
or
|
|
|
|
.Fn rnd_add_uint32
|
|
|
|
to add the data to the pool.
|
|
|
|
.It
|
|
|
|
Synchronously gathers entropy from hardware \(em for example, by a CPU
|
|
|
|
instruction like Intel RDSEED.
|
|
|
|
In this case, in order to add data to the pool
|
|
|
|
.Em before
|
|
|
|
returning, the callback
|
|
|
|
.Em must
|
|
|
|
use
|
|
|
|
.Fn rnd_add_data_sync ,
|
|
|
|
not
|
|
|
|
.Fn rnd_add_data
|
|
|
|
or
|
|
|
|
.Fn rnd_add_uint32 .
|
|
|
|
.El
|
2012-07-12 04:07:36 +04:00
|
|
|
.It Fn rnd_attach_source "krndsource_t *rnd_source" "char *devname" "uint32_t source_type" "uint32_t flags"
|
1997-11-01 13:00:36 +03:00
|
|
|
This function announces the availability of a device for entropy collection.
|
|
|
|
It must be called before the source struct pointed to by
|
|
|
|
.Fa rnd_source
|
|
|
|
is used in any of the following functions.
|
2000-11-07 09:43:24 +03:00
|
|
|
.Pp
|
2001-09-04 06:51:15 +04:00
|
|
|
.Fa devname
|
2002-10-14 17:43:14 +04:00
|
|
|
is the name of the device.
|
|
|
|
It is used to print a message (if the kernel is compiled with
|
|
|
|
``options RND_VERBOSE'') and also for status information printed with
|
1997-11-01 13:00:36 +03:00
|
|
|
.Xr rndctl 8 .
|
2000-11-07 09:43:24 +03:00
|
|
|
.Pp
|
1999-02-28 20:08:05 +03:00
|
|
|
.Fa source_type
|
2001-09-04 06:51:15 +04:00
|
|
|
is
|
|
|
|
.Dv RND_TYPE_NET
|
|
|
|
for network devices,
|
|
|
|
.Dv RND_TYPE_DISK
|
1997-11-01 13:00:36 +03:00
|
|
|
for physical disks,
|
2001-09-04 06:51:15 +04:00
|
|
|
.Dv RND_TYPE_TAPE
|
2008-09-17 03:29:49 +04:00
|
|
|
for a tape drive,
|
2001-09-04 06:51:15 +04:00
|
|
|
.Dv RND_TYPE_TTY
|
2012-07-12 04:07:36 +04:00
|
|
|
for a tty,
|
2008-09-17 03:29:49 +04:00
|
|
|
.Dv RND_TYPE_RNG
|
2012-07-12 04:07:36 +04:00
|
|
|
for a random number generator, and
|
|
|
|
.Dv RND_TYPE_ENV
|
|
|
|
for an environment sensor.
|
2001-09-04 06:51:15 +04:00
|
|
|
.Dv RND_TYPE_UNKNOWN
|
2002-10-14 17:43:14 +04:00
|
|
|
is not to be used as a type.
|
|
|
|
It is used internally to the rnd system.
|
2000-11-07 09:43:24 +03:00
|
|
|
.Pp
|
1999-02-28 20:08:05 +03:00
|
|
|
.Fa flags
|
|
|
|
are the logical OR of
|
2014-08-10 20:44:32 +04:00
|
|
|
.Dv RND_FLAG_COLLECT_VALUE
|
|
|
|
(mix data provided by this source into the pool)
|
|
|
|
.Dv RND_FLAG_COLLECT_TIME
|
|
|
|
(mix timestamps from this source into the pool)
|
|
|
|
.Dv RND_FLAG_ESTIMATE_VALUE
|
|
|
|
(use a delta estimator to count bits of entropy from this source's data towards
|
|
|
|
the pool estimate)
|
|
|
|
.Dv RND_FLAG_ESTIMATE_TIME
|
|
|
|
(use a delta estimator to count bits of entropy from this source's timestamps
|
2020-07-15 16:47:40 +03:00
|
|
|
towards the pool estimate)
|
|
|
|
.Dv RND_FLAG_HASCB
|
|
|
|
(caller specified a callback with
|
|
|
|
.Fn rndsource_setcb ) .
|
2014-08-10 20:44:32 +04:00
|
|
|
For many devices,
|
|
|
|
.Dv RND_FLAG_DEFAULT
|
2014-08-10 21:17:04 +04:00
|
|
|
.Dv ( RND_FLAG_COLLECT_VALUE | RND_FLAG_COLLECT_TIME | RND_FLAG_ESTIMATE_TIME )
|
|
|
|
is the best choice.
|
2002-10-14 17:43:14 +04:00
|
|
|
Note that devices of type
|
1999-02-28 20:08:05 +03:00
|
|
|
.Dv RND_TYPE_NET
|
|
|
|
default to
|
2014-08-10 21:17:04 +04:00
|
|
|
.Dv RND_FLAG_COLLECT_VALUE | RND_FLAG_COLLECT_TIME
|
2014-08-10 20:44:32 +04:00
|
|
|
(no entropy counted).
|
2012-07-12 04:07:36 +04:00
|
|
|
.It Fn rnd_detach_source "krndsource_t *rnd_source"
|
1997-11-01 13:00:36 +03:00
|
|
|
This function disconnects the device from entropy collection.
|
2012-07-12 04:07:36 +04:00
|
|
|
.It Fn rnd_add_uint32 "krndsource_t *rnd_source" "uint32_t datum"
|
1997-11-01 13:00:36 +03:00
|
|
|
This function adds the value of
|
|
|
|
.Va datum
|
2002-10-14 17:43:14 +04:00
|
|
|
to the entropy pool.
|
|
|
|
No entropy is assumed to be collected from this value, it merely helps
|
|
|
|
stir the entropy pool.
|
|
|
|
All entropy is gathered from jitter between the timing of events.
|
2000-11-07 09:43:24 +03:00
|
|
|
.Pp
|
1997-11-01 13:00:36 +03:00
|
|
|
Note that using a constant for
|
|
|
|
.Va datum
|
|
|
|
does not weaken security, but it does
|
2002-10-14 17:43:14 +04:00
|
|
|
not help.
|
|
|
|
Try to use something that can change, such as an interrupt status register
|
|
|
|
which might have a bit set for receive ready or transmit ready, or other
|
|
|
|
device status information.
|
2000-11-07 09:43:24 +03:00
|
|
|
.Pp
|
1997-11-01 13:00:36 +03:00
|
|
|
To allow the system to gather the timing information accurately, this call
|
2002-10-14 17:43:14 +04:00
|
|
|
should be placed within the actual hardware interrupt service routine.
|
|
|
|
Care must be taken to ensure that the interrupt was actually serviced by
|
|
|
|
the interrupt handler, since on some systems interrupts can be shared.
|
2000-11-07 09:43:24 +03:00
|
|
|
.Pp
|
1997-11-01 13:00:36 +03:00
|
|
|
This function loses nearly all usefulness if it is called from a scheduled
|
2002-10-14 17:43:14 +04:00
|
|
|
software interrupt.
|
|
|
|
If that is the only way to add the device as an entropy source, don't.
|
2000-11-07 09:43:24 +03:00
|
|
|
.Pp
|
1997-11-01 13:00:36 +03:00
|
|
|
If it is desired to mix in the
|
|
|
|
.Va datum
|
|
|
|
and to add in a timestamp, but not to actually estimate entropy from a source
|
2001-09-04 06:51:15 +04:00
|
|
|
of randomness, passing
|
|
|
|
.Dv NULL
|
1997-11-01 13:00:36 +03:00
|
|
|
for
|
|
|
|
.Va rnd_source
|
|
|
|
is permitted, and the device does not need to be attached.
|
2020-07-15 16:47:40 +03:00
|
|
|
.Pp
|
|
|
|
.Fn rnd_add_uint32
|
|
|
|
.Em must not
|
|
|
|
be used during a callback as set with
|
|
|
|
.Fn rndsource_setcb ;
|
|
|
|
use
|
|
|
|
.Fn rnd_add_data_sync
|
|
|
|
instead.
|
2012-07-12 04:07:36 +04:00
|
|
|
.It Fn rnd_add_data "krndsource_t *rnd_source" "void *data" "uint32_t len" "uint32_t entropy"
|
2001-09-04 06:51:15 +04:00
|
|
|
adds (hopefully) random
|
|
|
|
.Fa data
|
1997-11-01 13:00:36 +03:00
|
|
|
to the entropy pool.
|
|
|
|
.Fa len
|
|
|
|
is the number of bytes in
|
|
|
|
.Fa data
|
|
|
|
and
|
|
|
|
.Fa entropy
|
1999-03-07 01:09:29 +03:00
|
|
|
is an "entropy quality" measurement.
|
2001-09-04 06:51:15 +04:00
|
|
|
If every bit of
|
1997-11-01 13:00:36 +03:00
|
|
|
.Fa data
|
|
|
|
is known to be random,
|
|
|
|
.Fa entropy
|
|
|
|
is the number of bits in
|
|
|
|
.Fa data .
|
2000-11-07 09:43:24 +03:00
|
|
|
.Pp
|
1997-11-01 13:00:36 +03:00
|
|
|
Timing information is also used to add entropy into the system, using
|
|
|
|
inter-event timings.
|
2000-11-07 09:43:24 +03:00
|
|
|
.Pp
|
1997-11-01 13:00:36 +03:00
|
|
|
If it is desired to mix in the
|
|
|
|
.Va data
|
|
|
|
and to add in a timestamp, but not to actually estimate entropy from a source
|
2001-09-04 06:51:15 +04:00
|
|
|
of randomness, passing
|
|
|
|
.Dv NULL
|
1997-11-01 13:00:36 +03:00
|
|
|
for
|
|
|
|
.Va rnd_source
|
|
|
|
is permitted, and the device does not need to be attached.
|
2020-07-15 16:47:40 +03:00
|
|
|
.Pp
|
|
|
|
.Fn rnd_add_data
|
|
|
|
.Em must not
|
|
|
|
be used during a callback as set with
|
|
|
|
.Fn rndsource_setcb ;
|
|
|
|
use
|
|
|
|
.Fn rnd_add_data_sync
|
|
|
|
instead.
|
1997-11-01 13:00:36 +03:00
|
|
|
.El
|
2011-12-18 00:05:38 +04:00
|
|
|
.Sh INTERNAL ENTROPY POOL MANAGEMENT
|
|
|
|
When a hardware event occurs (such as completion of a hard drive
|
|
|
|
transfer or an interrupt from a network device) a timestamp is
|
|
|
|
generated.
|
|
|
|
This timestamp is compared to the previous timestamp
|
|
|
|
recorded for the device, and the first, second, and third order
|
|
|
|
differentials are calculated.
|
|
|
|
.Pp
|
|
|
|
If any of these differentials is zero, no entropy is assumed to
|
|
|
|
have been gathered.
|
|
|
|
If all are non-zero, one bit is assumed.
|
|
|
|
Next, data is mixed into the entropy pool using an LFSR (linear
|
|
|
|
feedback shift register).
|
|
|
|
.Pp
|
|
|
|
To extract data from the entropy pool, a cryptographically strong hash
|
|
|
|
function is used.
|
|
|
|
The output of this hash is mixed back into the pool using the LFSR,
|
|
|
|
and then folded in half before being returned to the caller.
|
|
|
|
.Pp
|
|
|
|
Mixing the actual hash into the pool causes the next extraction to
|
|
|
|
return a different value, even if no timing events were added to the
|
|
|
|
pool.
|
|
|
|
Folding the data in half prevents the caller to derive the
|
|
|
|
actual hash of the pool, preventing some attacks.
|
|
|
|
.Pp
|
2011-12-18 01:25:12 +04:00
|
|
|
In the
|
|
|
|
.Nx
|
|
|
|
kernel, values should be extracted from the entropy pool
|
2011-12-18 00:05:38 +04:00
|
|
|
.Em only
|
|
|
|
via the
|
|
|
|
.Xr cprng 9
|
2011-12-18 01:25:12 +04:00
|
|
|
interface.
|
|
|
|
Direct access to the entropy pool is unsupported and may be dangerous.
|
|
|
|
There is no supported API for direct access to the output of the entropy pool.
|
1997-11-01 13:00:36 +03:00
|
|
|
.\" .Sh ERRORS
|
2001-12-26 04:07:45 +03:00
|
|
|
.Sh FILES
|
2015-04-14 01:23:54 +03:00
|
|
|
These functions are declared in src/sys/sys/rndsource.h and defined in
|
2012-07-11 22:17:50 +04:00
|
|
|
src/sys/kern/kern_rndq.c.
|
2001-12-26 04:07:45 +03:00
|
|
|
.Sh SEE ALSO
|
|
|
|
.Xr rnd 4 ,
|
2011-11-29 00:19:25 +04:00
|
|
|
.Xr rndctl 8 ,
|
|
|
|
.Xr cprng 9
|
1997-11-01 13:00:36 +03:00
|
|
|
.Sh HISTORY
|
1999-02-28 20:08:05 +03:00
|
|
|
The random device was introduced in
|
1997-11-01 13:00:36 +03:00
|
|
|
.Nx 1.3 .
|
2001-09-11 03:32:29 +04:00
|
|
|
.Sh AUTHORS
|
2014-08-10 21:17:04 +04:00
|
|
|
This implementation was written by
|
|
|
|
.An Michael Graff Aq Mt explorer@flame.org
|
1997-11-01 13:00:36 +03:00
|
|
|
using ideas and algorithms gathered from many sources, including
|
|
|
|
the driver written by Ted Ts'o.
|
|
|
|
.Sh BUGS
|
|
|
|
The only good sources of randomness are quantum mechanical, and most
|
|
|
|
computers avidly avoid having true sources of randomness included.
|
|
|
|
Don't expect to surpass "pretty good".
|