1.\" $NetBSD: rnd.9,v 1.20 2011/12/17 21:25:12 wiz Exp $ 2.\" 3.\" Copyright (c) 1997 The NetBSD Foundation, Inc. 4.\" All rights reserved. 5.\" 6.\" This documentation is derived from text contributed to The NetBSD 7.\" Foundation by S.P.Zeidler (aka stargazer). 8.\" 9.\" Redistribution and use in source and binary forms, with or without 10.\" modification, are permitted provided that the following conditions 11.\" are met: 12.\" 1. Redistributions of source code must retain the above copyright 13.\" notice, this list of conditions and the following disclaimer. 14.\" 2. Redistributions in binary form must reproduce the above copyright 15.\" notice, this list of conditions and the following disclaimer in the 16.\" documentation and/or other materials provided with the distribution. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28.\" POSSIBILITY OF SUCH DAMAGE. 29.\" 30.Dd December 17, 2011 31.Dt RND 9 32.Os 33.Sh NAME 34.Nm RND , 35.Nm rnd_attach_source , 36.Nm rnd_detach_source , 37.Nm rnd_add_data , 38.Nm rnd_add_uint32 39.Nd functions to make a device available for entropy collection 40.Sh SYNOPSIS 41.In sys/rnd.h 42.Ft void 43.Fn rnd_attach_source "rndsource_element_t *rnd_source" "char *devname" "uint32_t source_type" "uint32_t flags" 44.Ft void 45.Fn rnd_detach_source "rndsource_element_t *rnd_source" 46.Ft void 47.Fn rnd_add_data "rndsource_element_t *rnd_source" "void *data" "uint32_t len" "uint32_t entropy" 48.Ft void 49.Fn rnd_add_uint32 "rndsource_element_t *rnd_source" "uint32_t datum" 50.Sh DESCRIPTION 51These 52.Nm 53functions make a device available for entropy collection for 54the kernel entropy pool, which provides key material for the 55.Xr cprng 9 56and 57.Xr rnd 4 58.Pa ( /dev/random ) 59interfaces. 60.Pp 61Ideally the first argument 62.Fa rnd_source 63of these functions gets included in the devices' entity struct, 64but any means to permanently (statically) attach one such argument 65to one incarnation of the device is ok. 66Do not share 67.Fa rnd_source 68structures between two devices. 69.Pp 70.Bl -tag -width 8n 71.It Fn rnd_attach_source "rndsource_element_t *rnd_source" "char *devname" "uint32_t source_type" "uint32_t flags" 72This function announces the availability of a device for entropy collection. 73It must be called before the source struct pointed to by 74.Fa rnd_source 75is used in any of the following functions. 76.Pp 77.Fa devname 78is the name of the device. 79It is used to print a message (if the kernel is compiled with 80``options RND_VERBOSE'') and also for status information printed with 81.Xr rndctl 8 . 82.Pp 83.Fa source_type 84is 85.Dv RND_TYPE_NET 86for network devices, 87.Dv RND_TYPE_DISK 88for physical disks, 89.Dv RND_TYPE_TAPE 90for a tape drive, 91.Dv RND_TYPE_TTY 92for a tty, and 93.Dv RND_TYPE_RNG 94for a random number generator. 95.Dv RND_TYPE_UNKNOWN 96is not to be used as a type. 97It is used internally to the rnd system. 98.Pp 99.Fa flags 100are the logical OR of 101.Dv RND_FLAG_NO_COLLECT 102(don't collect or estimate) 103.Dv RND_FLAG_NO_ESTIMATE 104(don't estimate) 105to control the default setting for collection and estimation. 106Note that devices of type 107.Dv RND_TYPE_NET 108default to 109.Dv RND_FLAG_NO_ESTIMATE . 110.Pp 111.It Fn rnd_detach_source "rndsource_element_t *rnd_source" 112This function disconnects the device from entropy collection. 113.It Fn rnd_add_uint32 "rndsource_element_t *rnd_source" "uint32_t datum" 114This function adds the value of 115.Va datum 116to the entropy pool. 117No entropy is assumed to be collected from this value, it merely helps 118stir the entropy pool. 119All entropy is gathered from jitter between the timing of events. 120.Pp 121Note that using a constant for 122.Va datum 123does not weaken security, but it does 124not help. 125Try to use something that can change, such as an interrupt status register 126which might have a bit set for receive ready or transmit ready, or other 127device status information. 128.Pp 129To allow the system to gather the timing information accurately, this call 130should be placed within the actual hardware interrupt service routine. 131Care must be taken to ensure that the interrupt was actually serviced by 132the interrupt handler, since on some systems interrupts can be shared. 133.Pp 134This function loses nearly all usefulness if it is called from a scheduled 135software interrupt. 136If that is the only way to add the device as an entropy source, don't. 137.Pp 138If it is desired to mix in the 139.Va datum 140and to add in a timestamp, but not to actually estimate entropy from a source 141of randomness, passing 142.Dv NULL 143for 144.Va rnd_source 145is permitted, and the device does not need to be attached. 146.It Fn rnd_add_data "rndsource_element_t *rnd_source" "void *data" "uint32_t len" "uint32_t entropy" 147adds (hopefully) random 148.Fa data 149to the entropy pool. 150.Fa len 151is the number of bytes in 152.Fa data 153and 154.Fa entropy 155is an "entropy quality" measurement. 156If every bit of 157.Fa data 158is known to be random, 159.Fa entropy 160is the number of bits in 161.Fa data . 162.Pp 163Timing information is also used to add entropy into the system, using 164inter-event timings. 165.Pp 166If it is desired to mix in the 167.Va data 168and to add in a timestamp, but not to actually estimate entropy from a source 169of randomness, passing 170.Dv NULL 171for 172.Va rnd_source 173is permitted, and the device does not need to be attached. 174.El 175.Sh INTERNAL ENTROPY POOL MANAGEMENT 176When a hardware event occurs (such as completion of a hard drive 177transfer or an interrupt from a network device) a timestamp is 178generated. 179This timestamp is compared to the previous timestamp 180recorded for the device, and the first, second, and third order 181differentials are calculated. 182.Pp 183If any of these differentials is zero, no entropy is assumed to 184have been gathered. 185If all are non-zero, one bit is assumed. 186Next, data is mixed into the entropy pool using an LFSR (linear 187feedback shift register). 188.Pp 189To extract data from the entropy pool, a cryptographically strong hash 190function is used. 191The output of this hash is mixed back into the pool using the LFSR, 192and then folded in half before being returned to the caller. 193.Pp 194Mixing the actual hash into the pool causes the next extraction to 195return a different value, even if no timing events were added to the 196pool. 197Folding the data in half prevents the caller to derive the 198actual hash of the pool, preventing some attacks. 199.Pp 200In the 201.Nx 202kernel, values should be extracted from the entropy pool 203.Em only 204via the 205.Xr cprng 9 206interface. 207Direct access to the entropy pool is unsupported and may be dangerous. 208There is no supported API for direct access to the output of the entropy pool. 209.\" .Sh ERRORS 210.Sh FILES 211These functions are declared in src/sys/sys/rnd.h and defined in 212src/sys/dev/rnd.c. 213.Sh SEE ALSO 214.Xr rnd 4 , 215.Xr rndctl 8 , 216.Xr cprng 9 217.Sh HISTORY 218The random device was introduced in 219.Nx 1.3 . 220.Sh AUTHORS 221This implementation was written by Michael Graff \*[Lt]explorer@flame.org\*[Gt] 222using ideas and algorithms gathered from many sources, including 223the driver written by Ted Ts'o. 224.Sh BUGS 225The only good sources of randomness are quantum mechanical, and most 226computers avidly avoid having true sources of randomness included. 227Don't expect to surpass "pretty good". 228