### read_random_uio (9)

#### Leading comments

Copyright (c) 2015 Mark R V Murray Copyright (c) 2000 The Regents of the University of California. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list ...

#### NAME

**arc4rand**

**arc4random**

**random**

**read_random**

**read_random_uio**

**srandom**- supply pseudo-random numbers

#### SYNOPSIS

In sys/libkern.h Ft void Fn srandom u_long seed Ft u_long Fn random void Ft void Fn arc4rand void *ptr u_int length int reseed Ft uint32_t Fn arc4random voidIn sys/random.h Ft int Fn read_random void *buffer int count Ft int Fn read_random_uio struct uio *uio bool nonblock

#### DESCRIPTION

The Fn random function will by default produce a sequence of numbers that can be duplicated by calling Fn srandom with some constant as the Fa seed . The Fn srandom function may be called with any arbitrary Fa seed value to get slightly more unpredictable numbers. It is important to remember that the Fn random function is entirely predictable, and is therefore not of use where knowledge of the sequence of numbers may be of benefit to an attacker.The Fn arc4rand function will return very good quality random numbers, better suited for security-related purposes. The random numbers from Fn arc4rand are seeded from the entropy device if it is available. Automatic reseeds happen after a certain timeinterval and after a certain number of bytes have been delivered. A forced reseed can be forced by passing a non-zero value in the Fa reseed argument.

The Fn read_random function is used to return entropy directly from the entropy device if it has been loaded. If the entropy device is not loaded, then the Fa buffer is ignored and zero is returned. The Fa buffer is filled with no more than Fa count bytes. It is strongly advised that Fn read_random is not used; instead use Fn arc4rand unless it is necessary to know that no entropy has been returned.

The Fn read_random_uio function behaves identically to read(2) on /dev/random The Fa uio argument points to a buffer where random data should be stored. This function only returns data if the random device is seeded. It blocks if unseeded, except when the Fa nonblock argument is true.

All the bits returned by
Fn random ,
Fn arc4rand ,
Fn read_random ,
and
Fn read_random_uio
are usable.
For example,
`**random()&01**
'
will produce a random binary value.

The Fn arc4random is a convenience function which calls Fn arc4rand to return a 32 bit pseudo-random integer.

#### RETURN VALUES

The Fn random function uses a non-linear additive feedback random number generator employing a default table of size 31 containing long integers to return successive pseudo-random numbers in the range from 0 to (2**31)-1. The period of this random number generator is very large, approximately 16*((2**31)-1).The Fn arc4rand function uses the RC4 algorithm to generate successive pseudo-random bytes. The Fn arc4random function uses Fn arc4rand to generate pseudo-random numbers in the range from 0 to (2**32)-1.

The Fn read_random function returns the number of bytes placed in Fa buffer .

Fn read_random_uio returns zero when successful, otherwise an error code is returned.

#### ERRORS

Fn read_random_uio may fail if:**Bq**Er EFAULT- Fa uio points to an invalid memory region.
**Bq**Er EWOULDBLOCK- The random device is unseeded and Fa nonblock is true.