For instance, could it be used to generate a one-time pad key?
Also, what are its sources and how could it be used to generate a random number between x and y?
3 Answers
Strictly speaking, /dev/random
is not really completely random. /dev/random
feeds on hardware sources which are assumed to be unpredictible in some way; then it mixes such data using functions (hash functions, mostly) which are also assumed to be one-way. So the "true randomness" of /dev/random
is thus relative to the inherent security of the mixing functions, security which is no more guaranteed than that of any other cryptographic primitive, in particular the PRNG hidden in /dev/urandom
.
The difference between /dev/random
and /dev/urandom
is that the former will try to maintain an estimate (which means "a wild guess") of how much entropy it has gathered, and will refuse to output more bits than that. On the other hand, /dev/urandom
will happily produce megabytes of data from the entropy it has.
The security difference between the two approaches is meaningless unless you assume that "classical" cryptographic algorithms can be broken, and you use one of the very few information-theoretic algorithms (e.g. OTP or Shamir's secret sharing); and, even then, /dev/random
may be considered as more secure than /dev/urandom
only if the mixing functions are still considered to be one-way, which is not compatible with the idea that a classical cryptographic algorithm can be broken. So, in practice and even in theory, no difference whatsoever. You can use the output of /dev/urandom
for an OTP and it will not be broken because of any structure internal to /dev/urandom
-- actual management of the obtained stream will be the weak point (especially long-time storage). On the other hand, /dev/random
has very real practical issues, namely that it can block at untimely instants. It is really irksome when an automated OS install blocks (for hours !) because SSH server key generation insists on using /dev/random
and needlessly stalls for entropy.
There are many applications which read /dev/random
as a kind of ritual, as if it was "better" than /dev/urandom
, probably on a karmic level. This is plain wrong, especially when the alea is to be used with classical cryptographic algorithms (e.g. to generate a SSH server public key). Do not do that. Instead, use /dev/urandom
and you will live longer and happier. Even for one-time pad.
(Just for completeness, there is a quirk with /dev/urandom
as implemented on Linux: it will never block, even if it has not gathered any entropy at all since previous boot. Distributions avoid this problem by creating a "random seed" at installation time, with /dev/random
, and using that seed at each boot to initialize the PRNG used by /dev/urandom
; a new random seed is regenerated immediately, for next boot. This ensures that /dev/urandom
always works over a sufficiently big internal seed. The FreeBSD implementation of /dev/urandom
will block until a given entropy threshold is reached, which is safer.)
-
4@thomas-pornin: Not true. Look at slide 63 of slideshare.net/astamos/cloud-computing-security for a brief note. Basically, in a VM it's not unlikely for there to be insufficient entropy vs the image you cloned unless measures are taken.– jwilkinsMay 27, 2011 at 18:41
-
@jwilkins would the RNG output of the two VMs be effectively unrelated as soon as they received even a single bit of entropy input that was different? (In the same same sense that if I change any single bit of a file, the hash of that file should be totally unrelated to what it was before?) If so, is the VM problem limited to just the first few moments after boot/clone, before the RNG states diverge? Apr 28, 2015 at 23:11
-
2To ask the same question in a different way, suppose I have two CSPRNGs using the same algorithm. Generator A is seeded with (X, 0), where X is 256+ bits of entropy. Generator B is seeded with (X, 1), where X exactly the same as for Generator A. Is there any attack that would let me predict the output of B, given the output of A? Apr 28, 2015 at 23:18
The only thing in this universe that can be considered truly is one based on quantum effects. Common example is radioactive decay. For certain atoms you can be sure only about half-life, but you can't be sure which nucleus will break up next.
About /dev/random
- it depends on implementation. In Linux it uses as entropy sources:
The Linux kernel generates entropy from keyboard timings, mouse movements, and IDE timings and makes the random character data available to other operating system processes through the special files /dev/random and /dev/urandom.
It means that it is better than algorithmic random generators, but it is not perfect as well. The entropy may not be distributed randomly and can be biased.
This was philosophy. Practice is that on Linux /dev/random
is random enough for vast majority of tasks.
There are implementations of random generators that have more entropy sources, including noise on audio inputs, CPU temperature sensors etc. Anyway they are not true.
There is interesting site where you can get Genuine random numbers, generated by radioactive decay.
-
1well depending on how you define random, even that is not truly random.– PacerierApr 24, 2014 at 10:45
-
2@Pacerier is you define random as toast with peanut butter and jam then yes, it is not truly random. By any other meaningful definition it is.– AndreyApr 24, 2014 at 14:38
-
it is not correct. You don't need quantum effects for "true" randomness e.g., classical turbulence models are not less true than quantum theories (the former are less fundumental but it does not matter: we know that the current (quantum) theories are also not a "theory of everything" (there are limitations)).– jfsJul 30, 2015 at 18:11
-
1@J.F.Sebastian I may be wrong but as I understand it there is a difference between very random and truly random. Turbulence, 3 body problem and all those systems are highly chaotic, but they are not truly random.– AndreyAug 3, 2015 at 11:00
-
@Andrey: my point is that turbulence is not merely "very random", it is "truly random". For example, it has a nice property that no matter how much do you know about the system; you won't be able to predict the specific outcome. You can persuade me otherwise with math and/or measurements.– jfsAug 3, 2015 at 12:27
/dev/random
will block if there's not enough random data in the entropy pool whereas /dev/urandom
will not. Instead, /dev/urandom
will fall back to a PRNG (kernel docs). From the same docs:
The random number generator [entropy pool] gathers environmental noise from device drivers and other sources into an entropy pool.
So /dev/random
is not algorithmic, like a PRNG, but it may not be "truly random" either. Mouse movements and keystroke timings tend to follow patterns and can be used for exploits but you'll have to weigh the risk against your use case.
To get a random number between x
and y
using /dev/random
, assuming you're happy with a 32-bit integer, you could have a look at the way the Java java.util.Random class does it (nextInt()
), substituting in appropriate code to read from /dev/random
for the nextBytes()
method.
/dev/random
should be suitable for uses that need very high quality randomness such as one-time pad or key generation - whereas/dev/urandom
is not