33

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?

6
  • 5
    I don't know for sure so I won't put this as an answer, but the Wikipedia article (en.wikipedia.org/wiki//dev/random) is pretty clear in that regard: /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 Apr 12, 2011 at 12:41
  • 1
    @Alexander Gessier So I should just take Wikipedia's word for it?
    – seriousdev
    Apr 12, 2011 at 12:47
  • 2
    You could also use the references at the bottom of the wikipedia article to get: kernel.org/doc/man-pages/online/pages/man4/random.4.html Apr 12, 2011 at 12:51
  • Wikipedia is just quoting the man page.
    – Keith
    Apr 12, 2011 at 13:01
  • 1
    There is a link on the wikipedia article to a PDF that reports on a study of /dev/random.
    – Keith
    Apr 12, 2011 at 13:06

3 Answers 3

30

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.)

3
  • 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.
    – jwilkins
    May 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
  • 2
    To 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
20

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.

Wiki

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.

20
  • 1
    well depending on how you define random, even that is not truly random.
    – Pacerier
    Apr 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.
    – Andrey
    Apr 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)).
    – jfs
    Jul 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.
    – Andrey
    Aug 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.
    – jfs
    Aug 3, 2015 at 12:27
2

/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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.