Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

In linux /dev/random is based of "environmental variables" and /dev/urandom is based off an algorithm and the time. My questions are:

Where does /dev/random gather these variables from.

And if you have two machines set to the same time (down to the millisecond-if possible) and have the same speed, OS, etc...and take a hexdump of /dev/urandom with X number of bytes, will they have the same exact value (because the time was the same)?

share|improve this question
2  
-1 because /dev/urandom into Google turns up quite a lot of what is in the answers as the first result. –  pnuts Jan 13 '13 at 19:58

3 Answers 3

up vote 5 down vote accepted

Where does /dev/urandom gather these variables from.

Everywhere it can, primarily interrupt timing. For example, if you have a network card, the precise time a packet arrives will be affected by the offset between the crystal timebase on the network card and the crystal timebase that clocks the CPU. The offset between two quartz crystal oscillators is affected by microscopic zone temperature variations that are believed to be truly physically random.

If you have a rotating disk, the time reads will complete is dependent on the speed the disk rotates. This is affected by turbulent shear force between the disk surface and the air inside the disk assembly. While this is not believed to be true physical randomness, it is entirely unpredictable or repeatable by any known mechanism.

And if you have two machines set to the same time (down to the millisecond-if possible) and have the same speed, OS, etc...and take a hexdump of /dev/random with X number of bytes, will they have the same exact value (because the time was the same)?

Well, millisecond wouldn't be nearly good enough. You'd need to be at the same level the CPU measures time -- billionths of a second. But, of course, that is how it must be. Otherwise, if you saw the state of one machine, you could know with 100% certainty that the other machine could not have that same state. And since the state is supposed to be random, you should not be able to look at any other machine and know anything about the state with 100% certainty. So long as the odds this could be accomplished are sufficiently low (say, less than one in 2^100), then it's perfectly fine.

Even if you roll two dice, each with a billion sides, they may come up the same. Provided they only do so once every billion times, then that's as it should be.

share|improve this answer
    
Do you have a reference for the network card example? I don't see it in the kernel sources, and that method isn't documented in drivers/char/random.c. The hard disk example seems to be the add_disk_randomness call in block/blk-core.c AFAICT. –  Daniel Beck Jan 13 '13 at 20:14
    
It's built into the interrupt handler, through handle_IRQ_event. –  David Schwartz Jan 13 '13 at 20:31

Take a look at urandom's manual, specifically the description:

When read, the /dev/random device will only return random bytes within the estimated number of bits of noise in the entropy pool. /dev/random should be suitable for uses that need very high quality randomness such as one-time pad or key generation. When the entropy pool is empty, reads from /dev/random will block until additional environmental noise is gathered.

A read from the /dev/urandom device will not block waiting for more entropy. As a result, if there is not sufficient entropy in the entropy pool, the returned values are theoretically vulnerable to a cryptographic attack on the algorithms used by the driver. Knowledge of how to do this is not available in the current unclassified literature, but it is theoretically possible that such an attack may exist. If this is a concern in your application, use /dev/random instead.

Unfortunately I can't really answer your second question, I'm use someone else can, though my guess would be that it'd be very hard to get the exact same on both machines.

share|improve this answer

In linux /dev/urandom is based of "environmental variables" and /dev/random is based off an algorithm and the time.

Both assumptions are false.

According to random(4) - Linux man page1, /dev/random gathers environmental noise from device drivers and other sources into an entropy pool.

An example of such a device is the mouse.2 Try executing

head -c 1024 /dev/random | base64

to clear the entropy pool (abort after a couple of seconds) and then

head -c 40 /dev/random | base64

to read 40 bytes from /dev/random. Do nothing and observe that head stalls a log time, since there's no new entropy in the pool.

Abort, then execute

head -c 40 /dev/random | base64

again. This time, mouse your mouse quickly from left to right. head will finish its task quickly.

Again, according to random(4) - Linux man page, /dev/urandom uses the same entropy pool, but it does not block. If the entropy pool isn't big enough, it falls back to a pseudorandom number generator (PRNG) to create more (pseudo-)random output.


1 Actual behavior might vary from distro to distro.

2 Example applies to Ubuntu 12.04. Other distros might differ.

share|improve this answer
    
The mouse move does this probably via the add_input_randomness call in drivers/input/input.c from what I can see. –  Daniel Beck Jan 13 '13 at 20:08
    
oops i flipped them –  agz Jan 13 '13 at 20:49
    
@user1950278: Still not true. Both use the same entropy pool. random uses it directly, urandom feeds it to a PRNG. –  Dennis Jan 13 '13 at 22:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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