1.\" $OpenBSD: arc4random.9,v 1.10 2014/01/23 08:15:04 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: January 23 2014 $ 27.Dt ARC4RANDOM 9 28.Os 29.Sh NAME 30.Nm arc4random 31.Nd kernel random subsystem 32.Sh SYNOPSIS 33.In dev/rndvar.h 34.Ft void 35.Fn add_true_randomness "int" 36.Ft void 37.Fn add_timer_randomness "int" 38.Ft void 39.Fn add_mouse_randomness "int" 40.Ft void 41.Fn add_tty_randomness "int" 42.Ft void 43.Fn add_net_randomness "int" 44.Ft void 45.Fn add_disk_randomness "int" 46.Ft void 47.Fn add_audio_randomness "int" 48.Ft u_int32_t 49.Fn arc4random "void" 50.Ft void 51.Fn arc4random_buf "void *buf" "size_t nbytes" 52.Ft u_int32_t 53.Fn arc4random_uniform "u_int32_t upper_bound" 54.Sh DESCRIPTION 55The 56.Fn add_random 57functions below mix input into the system entropy pool, which is then used 58to create a key for the ChaCha stream cipher used in the 59.Fn arc4random 60series of functions. 61.Pp 62The 63.Fn add_mouse_randomness , 64.Fn add_tty_randomness , 65.Fn add_net_randomness , 66.Fn add_disk_randomness 67and 68.Fn add_audio_randomness 69routines are used to 70supply data for the random data source device for further processing. 71The processing involves calculating inter-event timedelta and inserting 72it into a pool which is cryptographically mixed multiple times. 73.Pp 74.Fn add_true_randomness 75does not involve the usual timing calculations, 76and causes the supplied data argument to be added to the entropy pool, 77increasing the entropy counter by 32 bits. 78.Pp 79.Fn add_timer_randomness 80will not cause the entropy counter to rise. 81It is used to change the state of the pool periodically, 82mostly by means of timing the random driver's operations. 83.Pp 84.Fn arc4random 85and 86.Fn arc4random_buf 87provide random numbers and are intended to be called in any 88circumstance where random numbers are required. 89.Pp 90.Fn arc4random_uniform 91will return a uniformly distributed random number less than 92.Fa upper_bound , 93avoiding "modulo bias" when the upper bound is not a power of two. 94In the worst case, this function may consume multiple iterations 95to ensure uniformity; see the source code to understand the problem 96and solution. 97.Pp 98.Fn arc4random , 99.Fn arc4random_buf , 100and 101.Fn arc4random_uniform 102may be called in nearly any kernel context. 103.Sh SEE ALSO 104.Xr arc4random 3 , 105.Xr pchb 4 , 106.Xr random 4 107