RANDOM(9)

✨ RANDOM(9) ✨ β€” the kernel dev manual page, but make it cute

okay bestie, so this is RANDOM(9) from the Kernel Developer's Manual πŸ’– and it's literally the whole tea on how the kernel hands out pseudo-random numbers. sit down, hydrate, let's go 🫢


πŸŽ€ NAME πŸŽ€

arc4rand, arc4random, random, read_random, read_random_uio, srandom β€” supply pseudo-random numbers ✨


🌸 SYNOPSIS 🌸

#include <sys/libkern.h>

void
srandom(u_long seed);

u_long
random(void);

void
arc4rand(void *ptr, u_int length, int reseed);

uint32_t
arc4random(void);

#include <sys/random.h>

int
read_random(void *buffer, int count);

int
read_random_uio(struct uio *uio, bool nonblock);

πŸ’… DESCRIPTION πŸ’…

so random() is the basic girlie. by default it spits out a sequence of numbers that you can fully duplicate by calling srandom() with some constant as the seed. like, same seed, same vibes, every time.

srandom() can be called with any arbitrary seed value if you want slightly more unpredictable numbers. but listen β€” it is important to remember that random() is entirely predictable, so it is not of use where knowledge of the sequence of numbers may be of benefit to an attacker. it's giving 🚩 predictability 🚩 and that's on purpose, bestie.

then we have arc4rand(), the main character πŸ‘‘. it returns very good quality random numbers, way better suited for security-related purposes. the numbers from 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 πŸ“¦. and if you want to force it? a forced reseed can be forced by passing a non-zero value in the reseed argument. yes, "forced can be forced", we said what we said.

read_random() is the niche one. it returns entropy directly from the entropy device if it has been loaded. if the entropy device is not loaded, then the buffer is ignored and zero is returned 😭. the buffer is filled with no more than count bytes.

⚠️ it is strongly advised that read_random() is not used. instead use arc4rand() β€” unless it is necessary to know that no entropy has been returned. iykyk.

read_random_uio() behaves identically to read(2) on /dev/random. the 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 nonblock argument is true.

and here's a lil fun fact that ate πŸ’… β€” all the bits returned by random(), arc4rand(), read_random(), and read_random_uio() are usable. for example, β€˜random()&01’ will produce a random binary value. slay.

finally, arc4random() is just a convenience function which calls arc4rand() to return a 32 bit pseudo-random integer. it's the little sister function, doing the most with the least ✨


πŸ”₯ RETURN VALUES πŸ”₯

random() 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). huge. astronomical. iconic.

arc4rand() uses the RC4 algorithm to generate successive pseudo-random bytes. arc4random() uses arc4rand() to generate pseudo-random numbers in the range from 0 to (2**32)βˆ’1 πŸŽ€

read_random() returns the number of bytes placed in buffer.

read_random_uio() returns zero when successful βœ…, otherwise an error code is returned πŸ’€


πŸ’€ ERRORS πŸ’€

read_random_uio() may fail if:

  • [EFAULT] β€” uio points to an invalid memory region. not it πŸ™ˆ
  • [EWOULDBLOCK] β€” the random device is unseeded and nonblock is true. bestie it literally said "not right now" 🫠

πŸ‘‘ AUTHORS πŸ‘‘

  • Dan Moschuk wrote arc4random().
  • Mark R V Murray wrote read_random().

absolute legends, both of them 🌈


πŸ’œ and that's the whole page, bestie. no cap, every flag, every default, every range, every error β€” all accounted for. now go forth and arc4rand() responsibly. periodt. πŸ’…

this came from the Debian edition of the manual, dated July 16, 2015 πŸŽ€ mwah, bye πŸ¦‹πŸŒΈ

πŸ“¦ where this page comes from