167297998SMark Murray.\" 2d1b06863SMark Murray.\" Copyright (c) 2015 3d1b06863SMark Murray.\" Mark R V Murray 467297998SMark Murray.\" Copyright (c) 2000 567297998SMark Murray.\" The Regents of the University of California. All rights reserved. 667297998SMark Murray.\" 767297998SMark Murray.\" Redistribution and use in source and binary forms, with or without 867297998SMark Murray.\" modification, are permitted provided that the following conditions 967297998SMark Murray.\" are met: 1067297998SMark Murray.\" 1. Redistributions of source code must retain the above copyright 1167297998SMark Murray.\" notice, this list of conditions and the following disclaimer. 1267297998SMark Murray.\" 2. Redistributions in binary form must reproduce the above copyright 1367297998SMark Murray.\" notice, this list of conditions and the following disclaimer in the 1467297998SMark Murray.\" documentation and/or other materials provided with the distribution. 1567297998SMark Murray.\" 1667297998SMark Murray.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR 1767297998SMark Murray.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1867297998SMark Murray.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1967297998SMark Murray.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT, 2067297998SMark Murray.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2167297998SMark Murray.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2267297998SMark Murray.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2367297998SMark Murray.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2467297998SMark Murray.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2567297998SMark Murray.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2667297998SMark Murray.\" " 27*f50322c1SEd Maste.Dd March 18, 2024 2867297998SMark Murray.Dt RANDOM 9 29aa12cea2SUlrich Spörlein.Os 3067297998SMark Murray.Sh NAME 31d962d52aSRuslan Ermilov.Nm arc4rand , 3267297998SMark Murray.Nm arc4random , 3313774e82SConrad Meyer.Nm arc4random_buf , 34f3d2512dSConrad Meyer.Nm is_random_seeded , 356cfaa4a7SSheldon Hearn.Nm random , 366cfaa4a7SSheldon Hearn.Nm read_random , 37f3bae413SConrad Meyer.Nm read_random_uio 38eb083802SRuslan Ermilov.Nd supply pseudo-random numbers 3967297998SMark Murray.Sh SYNOPSIS 4032eef9aeSRuslan Ermilov.In sys/libkern.h 4113774e82SConrad Meyer.Ft uint32_t 4213774e82SConrad Meyer.Fn arc4random "void" 4313774e82SConrad Meyer.Ft void 4413774e82SConrad Meyer.Fn arc4random_buf "void *ptr" "size_t len" 4513774e82SConrad Meyer.Ft void 4613774e82SConrad Meyer.Fn arc4rand "void *ptr" "u_int length" "int reseed" 4713774e82SConrad Meyer.Pp 4813774e82SConrad Meyer.In sys/random.h 49f3d2512dSConrad Meyer.Ft bool 50f3d2512dSConrad Meyer.Fn is_random_seeded "void" 5113774e82SConrad Meyer.Ft void 5213774e82SConrad Meyer.Fn read_random "void *buffer" "int count" 5313774e82SConrad Meyer.Ft int 5413774e82SConrad Meyer.Fn read_random_uio "struct uio *uio" "bool nonblock" 5513774e82SConrad Meyer.Ss LEGACY ROUTINES 5613774e82SConrad Meyer.In sys/libkern.h 5767297998SMark Murray.Ft u_long 5867297998SMark Murray.Fn random "void" 5967297998SMark Murray.Sh DESCRIPTION 6067297998SMark MurrayThe 6113774e82SConrad Meyer.Fn arc4random 6213774e82SConrad Meyerand 6313774e82SConrad Meyer.Fn arc4random_buf 6413774e82SConrad Meyerfunctions will return very good quality random numbers, suited for 6513774e82SConrad Meyersecurity-related purposes. 6613774e82SConrad MeyerBoth are wrappers around the underlying 6713774e82SConrad Meyer.Fn arc4rand 6813774e82SConrad Meyerinterface. 6913774e82SConrad Meyer.Fn arc4random 7013774e82SConrad Meyerreturns a 32-bit random value, while 7113774e82SConrad Meyer.Fn arc4random_buf 7213774e82SConrad Meyerfills 7313774e82SConrad Meyer.Fa ptr 7413774e82SConrad Meyerwith 7513774e82SConrad Meyer.Fa len 7613774e82SConrad Meyerbytes of random data. 7767297998SMark Murray.Pp 7867297998SMark MurrayThe 792c38619bSPoul-Henning Kamp.Fn arc4rand 8013774e82SConrad MeyerCSPRNG 8113774e82SConrad Meyeris seeded from the 8213774e82SConrad Meyer.Xr random 4 8313774e82SConrad Meyerkernel abstract entropy device. 8413774e82SConrad MeyerAutomatic reseeding happens at unspecified time and bytes (of output) 8513774e82SConrad Meyerintervals. 8613774e82SConrad MeyerA reseed can be forced by passing a non-zero 87d962d52aSRuslan Ermilov.Fa reseed 8813774e82SConrad Meyervalue. 8967297998SMark Murray.Pp 9067297998SMark MurrayThe 9167297998SMark Murray.Fn read_random 9213774e82SConrad Meyerfunction is used to read entropy directly from the kernel abstract entropy 9313774e82SConrad Meyerdevice. 9413774e82SConrad Meyer.Fn read_random 9513774e82SConrad Meyerblocks if and until the entropy device is seeded. 9613774e82SConrad MeyerThe provided 97d962d52aSRuslan Ermilov.Fa buffer 9867297998SMark Murrayis filled with no more than 99d962d52aSRuslan Ermilov.Fa count 1005203edcdSRuslan Ermilovbytes. 101d1b06863SMark MurrayIt is strongly advised that 10267297998SMark Murray.Fn read_random 10313774e82SConrad Meyeris not used directly; 10413774e82SConrad Meyerinstead, use the 1052c38619bSPoul-Henning Kamp.Fn arc4rand 10613774e82SConrad Meyerfamily of functions. 10767297998SMark Murray.Pp 108707d98feSEd SchoutenThe 109f3d2512dSConrad Meyer.Fn is_random_seeded 110f3d2512dSConrad Meyerfunction can be used to check in advance if 111f3d2512dSConrad Meyer.Fn read_random 112f3d2512dSConrad Meyerwill block. 113f3d2512dSConrad Meyer(If random is seeded, it will not block.) 114f3d2512dSConrad Meyer.Pp 115f3d2512dSConrad MeyerThe 116707d98feSEd Schouten.Fn read_random_uio 117707d98feSEd Schoutenfunction behaves identically to 118707d98feSEd Schouten.Xr read 2 119707d98feSEd Schoutenon 120707d98feSEd Schouten.Pa /dev/random . 121707d98feSEd SchoutenThe 122707d98feSEd Schouten.Fa uio 123707d98feSEd Schoutenargument points to a buffer where random data should be stored. 12413774e82SConrad MeyerIf 125707d98feSEd Schouten.Fa nonblock 12613774e82SConrad Meyeris true and the random device is not seeded, this function does not return any 12713774e82SConrad Meyerdata. 12813774e82SConrad MeyerOtherwise, this function may block interruptibly until the random device is seeded. 12913774e82SConrad MeyerIf the function is interrupted before the random device is seeded, no data is 13013774e82SConrad Meyerreturned. 131707d98feSEd Schouten.Pp 132f3bae413SConrad MeyerThe deprecated 133dbbf3e3fSLawrence Stewart.Fn random 134dbbf3e3fSLawrence Stewartfunction will return a 31-bit value. 135f3bae413SConrad MeyerIt is obsolete and scheduled to be removed in 136*f50322c1SEd Maste.Fx 15.0 . 137dbbf3e3fSLawrence StewartConsider 138dbbf3e3fSLawrence Stewart.Xr prng 9 139dbbf3e3fSLawrence Stewartinstead and see 14013774e82SConrad Meyer.Sx SECURITY CONSIDERATIONS . 14167297998SMark Murray.Sh RETURN VALUES 14267297998SMark MurrayThe 1432c38619bSPoul-Henning Kamp.Fn arc4rand 14413774e82SConrad Meyerfunction uses the Chacha20 algorithm to generate a pseudo-random sequence of 14513774e82SConrad Meyerbytes. 1462c38619bSPoul-Henning KampThe 14767297998SMark Murray.Fn arc4random 148d1b06863SMark Murrayfunction uses 1492c38619bSPoul-Henning Kamp.Fn arc4rand 150d1b06863SMark Murrayto generate pseudo-random numbers 151d1b06863SMark Murrayin the range from 0 to 15267297998SMark Murray.if t 2\u\s732\s10\d\(mi1. 15367297998SMark Murray.if n (2**32)\(mi1. 15467297998SMark Murray.Pp 15567297998SMark MurrayThe 15667297998SMark Murray.Fn read_random 157d1b06863SMark Murrayfunction returns 158d1b06863SMark Murraythe number of bytes placed in 159d962d52aSRuslan Ermilov.Fa buffer . 160707d98feSEd Schouten.Pp 161707d98feSEd Schouten.Fn read_random_uio 162707d98feSEd Schoutenreturns zero when successful, 163707d98feSEd Schoutenotherwise an error code is returned. 164dbbf3e3fSLawrence Stewart.Pp 165dbbf3e3fSLawrence Stewart.Fn random 166dbbf3e3fSLawrence Stewartreturns numbers 167dbbf3e3fSLawrence Stewartin the range from 0 to 168dbbf3e3fSLawrence Stewart.if t 2\u\s731\s10\d\(mi1. 169dbbf3e3fSLawrence Stewart.if n (2**31)\(mi1. 170dbbf3e3fSLawrence Stewart 171707d98feSEd Schouten.Sh ERRORS 172707d98feSEd Schouten.Fn read_random_uio 173707d98feSEd Schoutenmay fail if: 174707d98feSEd Schouten.Bl -tag -width Er 175707d98feSEd Schouten.It Bq Er EFAULT 176707d98feSEd Schouten.Fa uio 177707d98feSEd Schoutenpoints to an invalid memory region. 178707d98feSEd Schouten.It Bq Er EWOULDBLOCK 179707d98feSEd SchoutenThe random device is unseeded and 180707d98feSEd Schouten.Fa nonblock 181707d98feSEd Schoutenis true. 182707d98feSEd Schouten.El 18367297998SMark Murray.Sh AUTHORS 18467297998SMark Murray.An Dan Moschuk 18567297998SMark Murraywrote 18667297998SMark Murray.Fn arc4random . 18767297998SMark Murray.An Mark R V Murray 18867297998SMark Murraywrote 18967297998SMark Murray.Fn read_random . 19013774e82SConrad Meyer.Sh SECURITY CONSIDERATIONS 19113774e82SConrad MeyerDo not use 19213774e82SConrad Meyer.Fn random 19313774e82SConrad Meyerin new code. 19413774e82SConrad Meyer.Pp 19513774e82SConrad MeyerIt is important to remember that the 19613774e82SConrad Meyer.Fn random 19713774e82SConrad Meyerfunction is entirely predictable. 19813774e82SConrad MeyerIt is easy for attackers to predict future output of 19913774e82SConrad Meyer.Fn random 20013774e82SConrad Meyerby recording some generated values. 20113774e82SConrad MeyerWe cannot emphasize strongly enough that 20213774e82SConrad Meyer.Fn random 20313774e82SConrad Meyermust not be used to generate values that are intended to be unpredictable. 204