1.\" $OpenBSD: arc4random.9,v 1.12 2015/11/23 17:53:57 jmc Exp $ 2.\" 3.\" Copyright (c) 1996,2000 Michael Shalayeff 4.\" All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 15.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25.\" 26.Dd $Mdocdate: November 23 2015 $ 27.Dt ARC4RANDOM 9 28.Os 29.Sh NAME 30.Nm arc4random , 31.Nm arc4random_buf , 32.Nm arc4random_uniform , 33.Nm add_true_randomness , 34.Nm add_timer_randomness , 35.Nm add_mouse_randomness , 36.Nm add_tty_randomness , 37.Nm add_net_randomness , 38.Nm add_disk_randomness , 39.Nm add_audio_randomness 40.Nd kernel random subsystem 41.Sh SYNOPSIS 42.In sys/systm.h 43.Ft u_int32_t 44.Fn arc4random "void" 45.Ft void 46.Fn arc4random_buf "void *buf" "size_t nbytes" 47.Ft u_int32_t 48.Fn arc4random_uniform "u_int32_t upper_bound" 49.In dev/rndvar.h 50.Ft void 51.Fn add_true_randomness "int" 52.Ft void 53.Fn add_timer_randomness "int" 54.Ft void 55.Fn add_mouse_randomness "int" 56.Ft void 57.Fn add_tty_randomness "int" 58.Ft void 59.Fn add_net_randomness "int" 60.Ft void 61.Fn add_disk_randomness "int" 62.Ft void 63.Fn add_audio_randomness "int" 64.Sh DESCRIPTION 65The 66.Fn add_random 67functions below mix input into the system entropy pool, which is then used 68to create a key for the ChaCha stream cipher used in the 69.Fn arc4random 70series of functions. 71.Pp 72The 73.Fn add_mouse_randomness , 74.Fn add_tty_randomness , 75.Fn add_net_randomness , 76.Fn add_disk_randomness 77and 78.Fn add_audio_randomness 79routines are used to 80supply data for the random data source device for further processing. 81The processing involves calculating inter-event timedelta and inserting 82it into a pool which is cryptographically mixed multiple times. 83.Pp 84.Fn add_true_randomness 85does not involve the usual timing calculations, 86and causes the supplied data argument to be added to the entropy pool, 87increasing the entropy counter by 32 bits. 88.Pp 89.Fn add_timer_randomness 90will not cause the entropy counter to rise. 91It is used to change the state of the pool periodically, 92mostly by means of timing the random driver's operations. 93.Pp 94.Fn arc4random 95and 96.Fn arc4random_buf 97provide random numbers and are intended to be called in any 98circumstance where random numbers are required. 99.Pp 100.Fn arc4random_uniform 101will return a uniformly distributed random number less than 102.Fa upper_bound , 103avoiding "modulo bias" when the upper bound is not a power of two. 104In the worst case, this function may consume multiple iterations 105to ensure uniformity; see the source code to understand the problem 106and solution. 107.Pp 108.Fn arc4random , 109.Fn arc4random_buf , 110and 111.Fn arc4random_uniform 112may be called in nearly any kernel context. 113.Sh SEE ALSO 114.Xr arc4random 3 , 115.Xr pchb 4 , 116.Xr random 4 117