gpt4 book ai didi

c - openssl RAND_load_file 总是返回 0

转载 作者:太空宇宙 更新时间:2023-11-04 00:03:11 24 4
gpt4 key购买 nike

我正在尝试在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com