Shashank V
Shashank V

Reputation: 11233

openssl RAND_load_file always returns 0

I'm trying to read from /dev/random/ on linux using.

int bytes = RAND_load_file("/dev/random", 16);
printf("%d bytes read", bytes);

This always outputs 0 bytes read.

/dev/random is being fed by a software entropy source. I made sure /dev/random does have enough data. According to docs RAND_load_file should return no. of bytes read but this is not happening.

Upvotes: 2

Views: 486

Answers (1)

jww
jww

Reputation: 102376

I'm trying to read from /dev/random/ on linux using...

This always outputs 0 bytes read.

According to the urandom(4) man pages:

The character special files /dev/random and /dev/urandom (present since Linux 1.3.30) provide an interface to the kernel's random number generator.

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.

Some OSes block, like OpenBSD. Other OSes don't block, like Debian and Ubuntu. For those that don't block on a short read, they return the number of bytes actually returned (which may be less than requested). So your first problem could be entropy depletion. You should check errno to get additional information.

I've also seen problems where integer math is used when estimating entropy during the addition of entropy to the device. The problem flows through the system and shows its head when you try to random numbers from the device. Something like:

int bytes = 128;
int estimate = bytes / 256;

The problem is you need a float, not an int. Otherwise, your estimate of entropy is 0.

int bytes = 128;
float estimate = (float)bytes / 256;

/dev/random is being fed by a software entropy source. I made sure /dev/random does have enough data.

That's somewhat of a red flag to me... One thing you should not be doing is discussed on LWN.net: Don't play dice with random numbers. Don't link dev/random to /dev/urandom. You should probably wander over to Information Security Stack Exchange and discuss your needs and methods.

There's lots of good reading returned from linux random number generator site:lwn.net. I see the patches for mobile OSes is discussed, which might help you with your bigger engineering problem.


Also, here's from the same man page:

If your system does not have /dev/random and /dev/urandom created already, they can be created with the following commands:

mknod -m 644 /dev/random c 1 8
mknod -m 644 /dev/urandom c 1 9
chown root:root /dev/random /dev/urandom

When a Linux system starts up without much operator interaction, the entropy pool may be in a fairly predictable state. This reduces the actual amount of noise in the entropy pool below the estimate. In order to counteract this effect, it helps to carry entropy pool information across shut-downs and start-ups. To do this, add the following lines to an appropriate script which is run during the Linux system start-up sequence:

echo "Initializing random number generator..."
random_seed=/var/run/random-seed
# Carry a random seed from start-up to start-up
# Load and then save the whole entropy pool
if [ -f $random_seed ]; then
    cat $random_seed >/dev/urandom
else
    touch $random_seed
fi
chmod 600 $random_seed
poolfile=/proc/sys/kernel/random/poolsize
[ -r $poolfile ] && bytes=`cat $poolfile` || bytes=512
dd if=/dev/urandom of=$random_seed count=1 bs=$bytes

Also, add the following lines in an appropriate script which is run during the Linux system shutdown:

# Carry a random seed from shut-down to start-up
# Save the whole entropy pool
echo "Saving random seed..."
random_seed=/var/run/random-seed
touch $random_seed
chmod 600 $random_seed
poolfile=/proc/sys/kernel/random/poolsize
[ -r $poolfile ] && bytes=`cat $poolfile` || bytes=512
dd if=/dev/urandom of=$random_seed count=1 bs=$bytes

Upvotes: 0

Related Questions