views:

297

answers:

6

Hello,

What is the (best) way to create a secure random numbers in Linux (C/ C++ code), more random than the general rand() results, and not pseudo as OpenSSL BN_rand?

In Windows I found CryptGenRandom() as a good option. Is there any equivalent in Linux?

Thank you in advance.

+14  A: 

you can read from /dev/random which is populated with an entropy pool. There is some good info on the wikipedia site on it: http://en.wikipedia.org/wiki//dev/random

Martijn
Conclusion: Wikipedia has an article with `//` and `/` as part of its url. Just saying.
Kobi
Can't open thre damn URL on my iPhone. 404
Time Machine
you could try http://en.m.wikipedia.org/wiki?search=/dev/random
Martijn
+4  A: 

"Random" numbers generated by a computer without any external data are pseudo-random. It means that they are generated with a mathematical formula. These algorithms are reliable and should be okay for almost all purposes.

To have a "true" random number, you need an intervention from outside. There are some solutions implemented in various programs (I remember of several ones that used mouse movements or atmospheric noise).

As Martijin just pointed, there is also /dev/random on Linux and OSX. It uses the noise collected by the device drivers.

There is also a web service that I just found : http://www.random.org/clients/http/

Marc Demierre
You can collect random data on a computer, or use random data from elsewhere (there are several websites which make it available for download; just an example, not saying that is good for security ;). While the rest of your answer is good, your "by definition" isn't.
Roger Pate
Yes you're right, the random elements coming from outside are a base for the computer and it generates numbers with it. I'll edit.
Marc Demierre
Sometimes the "computer" already has some "external" sources of entropy. For instance, it may contains several clocks (CPU clock + Sound card/GPU clock), which don't exactly coincide.
valdo
A: 

Take a look at boost::random_device.

usta
A: 

read /dev/urandom

tumbak
+2  A: 

1st CryptGenRandom is not "truly" random device by they are enough random to be cryptographically safe.

Similar under Linux (and most unixes) is reading from /dev/urandom.

If you want to get real random numbers you may read /dev/random but you may get blocked waiting for system to collect them if entropy pool is too small.

Artyom
A: 

/dev/urandom generates some random numbers based on the actions you perform(moving of the mouse,typing,etc!)

Stefan George