#include <linux/random.h> int ioctl(fd, RNDrequest, param);
乱数ジェネレーターはデバイスドライバやその他の源からの環境ノイズを エントロピープールへ集める。 また、ジェネレーターはエントロピープール内のノイズのビット数の推定値を 保持する。 このエントロピープールから乱数が生成される。
Linux 3.17 and later provides the simpler and safer getrandom(2) interface which requires no special files; see the getrandom(2) manual page for details.
When read, the /dev/urandom device returns random bytes using a pseudorandom number generator seeded from the entropy pool. Reads from this device do not block (i.e., the CPU is not yielded), but can incur an appreciable delay when requesting large amounts of data.
When read during early boot time, /dev/urandom may return data prior to the entropy pool being initialized. If this is of concern in your application, use getrandom(2) or /dev/random instead.
The /dev/random device is a legacy interface which dates back to a time where the cryptographic primitives used in the implementation of /dev/urandom were not widely trusted. It will return random bytes only within the estimated number of bits of fresh noise in the entropy pool, blocking if necessary. /dev/random is suitable for applications that need high quality randomness, and can afford indeterminate delays.
When the entropy pool is empty, reads from /dev/random will block until additional environmental noise is gathered. Since Linux 5.6, the O_NONBLOCK flag is ignored as /dev/random will no longer block except during early boot process. In earlier versions, if open(2) is called for /dev/random with the O_NONBLOCK flag, a subsequent read(2) will not block if the requested number of bytes is not available. Instead, the available bytes are returned. If no byte is available, read(2) will return -1 and errno will be set to EAGAIN.
The O_NONBLOCK flag has no effect when opening /dev/urandom. When calling read(2) for the device /dev/urandom, reads of up to 256 bytes will return as many bytes as are requested and will not be interrupted by a signal handler. Reads with a buffer over this limit may return less than the requested number of bytes or fail with the error EINTR, if interrupted by a signal handler.
Since Linux 3.16, a read(2) from /dev/urandom will return at most 32 MB. A read(2) from /dev/random will return at most 512 bytes (340 bytes before Linux 2.6.12).
/dev/random や /dev/urandom に書き込みを行うと、 書き込まれたデータでエントロピープールが更新される。 しかし、 エントロピーカウントが増えるわけではない。 つまり、 /dev/random と /dev/urandom の両方のファイルから読み出される内容に影響があるが、 /dev/random からの読み出しが早くなるわけではないということだ。
下記で推奨しているように再起動の前後で乱数種ファイルが保存される場合、起動シーケンスにおいて乱数種が 再ロードされた直後から、その出力はローカルのルートアクセスができない 攻撃者に対して暗号的に安全なものとなり、ネットワーク暗号化のセッションキーとして使うには完全に最適なものとなる。 (すべての主な Linux のディストリビューションは少なくとも 2000 年以降はリブートの前後で乱数種のファイルを保存するようになっている。) /dev/random からの読み出しは停止 (block) する可能性があるので、ユーザーは普通 このファイルを非停止 (nonblocking) モードで開こうとし (もしくはタイムアウトを指定して読み出しを実行し)、希望するレベルの エントロピーはすぐには利用できない場合には、何らかの通知を行うことだろう。
mknod -m 666 /dev/random c 1 8 mknod -m 666 /dev/urandom c 1 9 chown root:root /dev/random /dev/urandom
オペレータの操作なしに Linux システムが起動した直後は、 エントロピープールは意外性の乏しい均一な状態にあるだろう。 これにより、エントロピープールの実際のノイズ量は評価値より少なくなる。 この効果を打ち消すために、シャットダウンから (次の) 起動時まで持ち越した エントロピープールの情報が助けになる。 エントロピープールを持ち越すためには、 Linux システムの起動時に実行される適切なスクリプトに、 以下の行を追加すればよい:
echo "Initializing random number generator..."
random_seed=/var/run/random-seed
# 乱数種を今回のスタートアップから次回のスタートアップまで持ち越す。
# ロードを行い、その後、全てのエントロピープールを保存する。
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 ] && bits=$(cat $poolfile) || bits=4096
bytes=$(expr $bits / 8)
dd if=/dev/urandom of=$random_seed count=1 bs=$bytes
また、Linux システムのシャットダウン時に実行される適切なスクリプトに、 以下の行を追加すればよい:
# 乱数種を今回のシャットダウンから次回のスタートアップまで持ち越す。 # 全てのエントロピープールを保存する。 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 ] && bits=$(cat $poolfile) || bits=4096 bytes=$(expr $bits / 8) dd if=/dev/urandom of=$random_seed count=1 bs=$bytes
In the above examples, we assume Linux 2.6.0 or later, where /proc/sys/kernel/random/poolsize returns the size of the entropy pool in bits (see below).
struct rand_pool_info {
int entropy_count;
int buf_size;
__u32 buf[0];
};
RFC 1750, "Randomness Recommendations for Security"