c - openssl RAND_load_file 总是返回 0

标签 c ssl random openssl entropy

我正在尝试在 linux 上使用 /dev/random/ 进行读取。

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

这总是输出 0 字节读取。

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

最佳答案

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。对于那些不阻塞短读的,它们返回实际返回的字节数(可能少于请求的字节数)。所以你的第一个问题可能是熵耗尽。您应该检查 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 .不要将 dev/random 链接到 /dev/urandom。您可能应该转至 Information Security Stack Exchange并讨论您的需求和方法。

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

关于c - openssl RAND_load_file 总是返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34605725/

相关文章:

linux - 使用 Luasec 产生 "module ' ssl.core' not found”

c++ - rand() 函数 C++

haskell - 在 Haskell 中生成随机数

c - 不明白为什么结果是一

c - 如何实现模幂运算,最多需要两倍于要在 C 中进行模幂运算的数字的字节大小?

c - sscanf 替换以前的值

c++ - 为什么 gRPC C++ 客户端在没有显式服务器的 SSL 证书的情况下无法工作,就像示例中那样?

c - 如何让 gcc 将包含函数调用的表达式识别为常量?

delphi - 使用 Indy 和 Delphi 10 连接 SSL 时出错

Java - 最高效的随机访问多线程列表