1.\" $NetBSD: rnd.9,v 1.11 2002/02/13 08:18:50 ross 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.\" 3. All advertising materials mentioning features or use of this software 18.\" must display the following acknowledgement: 19.\" This product includes software developed by the NetBSD 20.\" Foundation, Inc. and its contributors. 21.\" 4. Neither the name of The NetBSD Foundation nor the names of its 22.\" contributors may be used to endorse or promote products derived 23.\" from this software without specific prior written permission. 24.\" 25.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 26.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 29.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35.\" POSSIBILITY OF SUCH DAMAGE. 36.\" 37.Dd October 20, 1997 38.Dt RND 9 39.Os 40.Sh NAME 41.Nm RND , 42.Nm rnd_attach_source , 43.Nm rnd_detach_source , 44.Nm rnd_add_data , 45.Nm rnd_add_uint32 46.Nd functions to make a device available for entropy collection 47.Sh SYNOPSIS 48.Fd #include \*[Lt]sys/rnd.h\*[Gt] 49.Ft void 50.Fn rnd_attach_source "rndsource_element_t *rnd_source" "char *devname" "u_int32_t source_type" "u_int32_t flags" 51.Ft void 52.Fn rnd_detach_source "rndsource_element_t *rnd_source" 53.Ft void 54.Fn rnd_add_data "rndsource_element_t *rnd_source" "void *data" "u_int32_t len" "u_int32_t entropy" 55.Ft void 56.Fn rnd_add_uint32 "rndsource_element_t *rnd_source" "u_int32_t datum" 57.Sh DESCRIPTION 58These 59.Nm 60functions make a device available for entropy collection for 61.Pa /dev/random . 62.Pp 63Ideally the first argument 64.Fa rnd_source 65of these functions gets included in the devices' entity struct, 66but any means to permanently (static) attach one such argument 67to one incarnation of the device is ok. Do not share 68.Fa rnd_source 69structures between two devices. 70.Pp 71.Bl -tag -width 8n 72.It Fn rnd_attach_source "rndsource_element_t *rnd_source" "char *devname" "u_int32_t source_type" "u_int32_t flags" 73This function announces the availability of a device for entropy collection. 74It must be called before the source struct pointed to by 75.Fa rnd_source 76is used in any of the following functions. 77.Pp 78.Fa devname 79is the name of the device. It is used to print a message (if the kernel is 80compiled with ``options RND_VERBOSE'') and also for status information printed 81with 82.Xr rndctl 8 . 83.Pp 84.Fa source_type 85is 86.Dv RND_TYPE_NET 87for network devices, 88.Dv RND_TYPE_DISK 89for physical disks, 90.Dv RND_TYPE_TAPE 91for a tape drive, and 92.Dv RND_TYPE_TTY 93for a tty. 94.Dv RND_TYPE_UNKNOWN 95is not to be used as a type. It is used internally to the rnd system. 96.Pp 97.Fa flags 98are the logical OR of 99.Dv RND_FLAGS_NO_COLLECT (don't collect or estimate) 100.Dv RND_FLAGS_NO_ESTIMATE (don't estimate) 101to control the default setting for collection and estimation. Note that 102devices of type 103.Dv RND_TYPE_NET 104default to 105.Dv RND_FLAGS_NO_ESTIMATE . 106.Pp 107.It Fn rnd_detach_source "rndsource_element_t *rnd_source" 108This function disconnects the device from entropy collection. 109.It Fn rnd_add_uint32 "rndsource_element_t *rnd_source" "u_int32_t datum" 110This function adds the value of 111.Va datum 112to the entropy pool. No entropy is assumed to be collected from this value, 113it merely helps stir the entropy pool. All entropy is gathered from 114jitter between the timing of events. 115.Pp 116Note that using a constant for 117.Va datum 118does not weaken security, but it does 119not help. Try to use something that can change, such as an interrupt status 120register which might have a bit set for receive ready or transmit ready, 121or other device status information. 122.Pp 123To allow the system to gather the timing information accurately, this call 124should be placed within the actual hardware interrupt service routine. Care 125must be taken to ensure that the interrupt was actually serviced by the 126interrupt handler, since on some systems interrupts can be shared. 127.Pp 128This function loses nearly all usefulness if it is called from a scheduled 129software interrupt. If that is the only way to add the device as an entropy 130source, don't. 131.Pp 132If it is desired to mix in the 133.Va datum 134and to add in a timestamp, but not to actually estimate entropy from a source 135of randomness, passing 136.Dv NULL 137for 138.Va rnd_source 139is permitted, and the device does not need to be attached. 140.It Fn rnd_add_data "rndsource_element_t *rnd_source" "void *data" "u_int32_t len" "u_int32_t entropy" 141adds (hopefully) random 142.Fa data 143to the entropy pool. 144.Fa len 145is the number of bytes in 146.Fa data 147and 148.Fa entropy 149is an "entropy quality" measurement. 150If every bit of 151.Fa data 152is known to be random, 153.Fa entropy 154is the number of bits in 155.Fa data . 156.Pp 157Timing information is also used to add entropy into the system, using 158inter-event timings. 159.Pp 160If it is desired to mix in the 161.Va data 162and to add in a timestamp, but not to actually estimate entropy from a source 163of randomness, passing 164.Dv NULL 165for 166.Va rnd_source 167is permitted, and the device does not need to be attached. 168.El 169.\" .Sh ERRORS 170.Sh FILES 171These functions are declared in src/sys/sys/rnd.h and defined in 172src/sys/dev/rnd.c. 173.Sh SEE ALSO 174.Xr rnd 4 , 175.Xr rndctl 8 176.Sh HISTORY 177The random device was introduced in 178.Nx 1.3 . 179.Sh AUTHORS 180This implementation was written by Michael Graff \*[Lt]explorer@flame.org\*[Gt] 181using ideas and algorithms gathered from many sources, including 182the driver written by Ted Ts'o. 183.Sh BUGS 184The only good sources of randomness are quantum mechanical, and most 185computers avidly avoid having true sources of randomness included. 186Don't expect to surpass "pretty good". 187