使用RNDADDENTROPY将熵添加到/ dev/random

Ben*_*ber 7 c linux ioctl embedded-linux

我有一个设备会产生一些噪音,我想将它添加到嵌入式Linux系统中/ dev/random设备的熵池中.

我正在阅读/ dev/random上手册页,我真的不明白你传递给RNDADDENTROPY ioctl调用的结构.

   RNDADDENTROPY
          Add some additional entropy to the input pool, incrementing
          the entropy count.  This differs from writing to /dev/random
          or /dev/urandom, which only adds some data but does not
          increment the entropy count.  The following structure is used:

              struct rand_pool_info {
                  int    entropy_count;
                  int    buf_size;
                  __u32  buf[0];
              };

          Here entropy_count is the value added to (or subtracted from)
          the entropy count, and buf is the buffer of size buf_size
          which gets added to the entropy pool.
Run Code Online (Sandbox Code Playgroud)

就是entropy_count在这种结构中,我加入的位数?为什么不总是这样buf_size * 8(假设buf_size是以字节为单位)?

另外为什么是buf零尺寸阵列?我该如何为它分配一个值?

感谢您的帮助!

小智 4

我正在使用硬件 RNG 来存储我的熵池。我的结构是静态大小,看起来像这样(我的内核有一个稍微不同的 random.h;只需复制您在您的结构中找到的内容并将数组大小增加到您想要的大小):

#define BUFSIZE 256
/* WARNING - this struct must match random.h's struct rand_pool_info */
typedef struct {
    int bit_count;               /* number of bits of entropy in data */
    int byte_count;              /* number of bytes of data in array */
    unsigned char buf[BUFSIZ];
} entropy_t;
Run Code Online (Sandbox Code Playgroud)

无论你在 buf 中传递什么,都将被散列并搅动熵池。如果您使用 /dev/urandom,那么您为 bit_count 传递什么并不重要,因为 /dev/urandom 会忽略它等于 0 并继续运行。

bit_count 的作用是推出 /dev/random 将阻塞的点,并等待某些东西从物理 RNG 源添加更多熵。因此,可以猜测 bit_count。如果你猜测得低,最糟糕的情况是 /dev/random 会比其他情况更快阻塞。如果你猜得高,/dev/random 将像 /dev/urandom 一样运行,比它在阻塞之前的运行时间长一点。

您可以根据熵源的“质量”进行猜测。如果它很低,比如人类输入的字符,您可以将其设置为每字节 1 或 2。如果它很高,例如从专用硬件 RNG 读取的值,您可以将其设置为每字节 8 位。