openssl RAND_load_file 总是 returns 0

openssl RAND_load_file always returns 0

我正在尝试使用 linux 从 /dev/random/ 阅读。

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

这总是输出 0 个字节读取。

/dev/random 由软件熵源提供。我确定 /dev/random 确实有足够的数据。根据文档 RAND_load_file 应该 return 不。读取的字节数,但这并没有发生。

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

This always outputs 0 bytes read.

根据 urandom(4) 手册页:

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.

有些操作系统会阻塞,例如 OpenBSD。其他操作系统不会阻止,例如 Debian 和 Ubuntu。对于那些不阻塞短读的人,他们 return 实际 return 字节数(可能少于请求)。所以你的第一个问题可能是熵耗尽。您应该查看 errno 以获得更多信息。

我还看到在向设备添加熵期间估算熵时使用整数数学的问题。当您尝试从设备中随机数时,问题会贯穿整个系统并暴露出来。类似于:

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

问题是您需要 float,而不是 int。否则,您对熵的估计为 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.

这对我来说有点危险...不应该做的一件事在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并讨论你的需求和方法。

return 从 linux random number generator site:lwn.net 编辑了很多很好的读物。我看到讨论了移动操作系统的补丁,这可能会帮助您解决更大的工程问题。


此外,这是来自同一手册页的内容:

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