RANDOM

Section: Linux Programmer's Manual (4)
Updated: 2017-09-15
Index JM Home Page
 

名前

random, urandom - カーネル乱数ソースデバイス  

書式

#include <linux/random.h>

int ioctl(fd, RNDrequest, param);  

説明

(Linux 1.3.30 から提供されている) /dev/random/dev/urandom キャラクタースペシャルファイルは カーネル乱数ジェネレーターへのインターフェースを提供する。 /dev/random ファイルはメジャーデバイス番号 1 マイナーデバイス番号 8 である。 /dev/urandom ファイルはメジャーデバイス番号 1 マイナーデバイス番号 9 である。

乱数ジェネレーターはデバイスドライバやその他の源からの環境ノイズを エントロピープールへ集める。 また、ジェネレーターはエントロピープール内のノイズのビット数の推定値を 保持する。 このエントロピープールから乱数が生成される。

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.

エントロピープールが空の時は、/dev/random からの読み出しは、 更なる環境ノイズが得られるまで、ブロックされる。 open(2) が /dev/random に対して O_NONBLOCK フラグ付きで呼ばれると、 それ以降の read(2) は要求したバイト数のデータが利用可能になるまで停止しない。 その代わり、 利用可能なデータが返される。 利用可能なバイトが全くない場合、 read(2) は -1 を返し、 errnoEAGAIN が設定される。

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 on Linux kernels before version 2.6.12).

/dev/random/dev/urandom に書き込みを行うと、 書き込まれたデータでエントロピープールが更新される。 しかし、 エントロピーカウントが増えるわけではない。 つまり、 /dev/random/dev/urandom の両方のファイルから読み出される内容に影響があるが、 /dev/random からの読み出しが早くなるわけではないということだ。  

使い方

The /dev/random interface is considered a legacy interface, and /dev/urandom is preferred and sufficient in all use cases, with the exception of applications which require randomness during early boot time; for these applications, getrandom(2) must be used instead, because it will block until the entropy pool is initialized.

下記で推奨しているように再起動の前後で乱数種ファイルが保存される場合、起動シーケンスにおいて乱数種が 再ロードされた直後から、その出力はローカルのルートアクセスができない 攻撃者に対して暗号的に安全なものとなり、ネットワーク暗号化のセッションキーとして使うには完全に最適なものとなる。 (すべての主な Linux のディストリビューションは少なくとも 2000 年以降はリブートの前後で乱数種のファイルを保存するようになっている。) /dev/random からの読み出しは停止 (block) する可能性があるので、ユーザーは普通 このファイルを非停止 (nonblocking) モードで開こうとし (もしくはタイムアウトを指定して読み出しを実行し)、希望するレベルの エントロピーはすぐには利用できない場合には、何らかの通知を行うことだろう。  

Configuration

システムにあらかじめ作成された /dev/random/dev/urandom が存在しないなら、次のようなコマンドで作成できる。


 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).  

/proc インターフェース

ディレクトリ /proc/sys/kernel/random にあるファイル (2.3.16 以降で存在する) で、 /dev/random デバイスに関する追加の情報を参照できる。
entropy_avail
この読み込み専用のファイルは使用可能なエントロピーをビット単位で表示する。値は 0 から 4096 までの範囲の数字である。
poolsize
このファイルはエントロピープールのサイズを示す。 このファイルの意味はカーネルバージョンにより異なる。
Linux 2.4:
このファイルはエントロピープールのサイズを「バイト」単位で規定する。 通常、このファイルの値は 512 になるが、書き込み可能であり、 アルゴリズムで利用可能な任意の値に変更できる。 選択可能な値は 32, 64, 128, 256, 512, 1024, 2048 である。
Linux 2.6 以降:
このファイルは読み出し専用であり、 エントロピープールのサイズを「ビット」単位で規定する。 値は 4096 である。
read_wakeup_threshold
This file contains the number of bits of entropy required for waking up processes that sleep waiting for entropy from /dev/random. The default is 64.
write_wakeup_threshold
ファイル write_wakeup_threshold はエントロピーのビット数を保持しており、この値以下になったら /dev/random への書き込みアクセスのために select(2) または poll(2) を実行するプロセスを起こす。 この値はファイルに書き込みを行うことによって変更できる。
uuid and boot_id
これらの読み込み専用のファイルは 6fd5a44b-35f4-4ad4-a9b9-6b9be13e1fe9 のような ランダムな文字列を保持している。 前者は読み込みの度に新たに生成され、 後者は 1 度だけ生成される。
 

ioctl(2) インターフェース

以下の ioctl(2) 要求が /dev/random/dev/urandom に接続されたファイルディスクリプターに対して定義されている。 実行されたすべての要求は、 /dev/random/dev/urandom に影響を与える入力エントロピープールとやり取りを行う。 RNDGETENTCNT 以外のすべての要求には CAP_SYS_ADMIN ケーパビリティが必要である。
RNDGETENTCNT
入力エントロピープールのカウントを取得する。 取得される内容は proc の entropy_avail ファイルと同じである。 結果は引数が指す整数 (int) に格納される。
RNDADDTOENTCNT
入力エントロピープールのカウントを引数が指す値だけ加算または減算する。
RNDGETPOOL
Linux 2.6.9 で削除された。
RNDADDENTROPY
入力プールに追加のエントロピーを追加し、エントロピーカウントを増やす。 この要求は /dev/random/dev/urandom への書き込みとは異なる。 /dev/random/dev/urandom への書き込みでは、 何らかのデータが追加されるだけで、 エントロピーカウントは増やされない。 以下の構造体が使用される。

 struct rand_pool_info {
    int    entropy_count;
    int    buf_size;
    __u32  buf[0]; };
entropy_count はエントロピーカウントに加算 (または減算) する値である。 buf は大きさが buf_size のバッファーで、この内容がエントロピープールに追加される。
RNDZAPENTCNT, RNDCLEARPOOL
すべてのプールのエントロピーカウントを 0 にし、何らかのシステムデータ (現在の時刻など) をプールに追加する。
 

ファイル


 /dev/random

 /dev/urandom  

注意

For an overview and comparison of the various interfaces that can be used to obtain randomness, see random(7).  

バグ

During early boot time, reads from /dev/urandom may return data prior to the entropy pool being initialized.  

関連項目


 mknod(1), getrandom(2), random(7)

RFC 1750, "Randomness Recommendations for Security"  

この文書について

この man ページは Linux man-pages プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は https://www.kernel.org/doc/man-pages/ に書かれている。

関連キーワード

dev, エントロピー, urandom, プール, bytes, bits, 乱数, read, 追加, proc

Linux マニュアル 一覧

[man1] [man2] [man3] [man4] [man5] [man6] [man7] [man8]
[a] [b] [c] [d] [e] [f] [g] [h] [i] [j] [k] [l] [m] [n] [o] [p] [q] [r] [s] [t] [u] [v] [w] [x] [y] [z]

 

Index

名前
書式
説明
使い方
Configuration
/proc インターフェース
ioctl(2) インターフェース
ファイル
注意
バグ
関連項目
この文書について

This document was created by man2html, using the manual pages.
Time: 12:08:52 GMT, June 11, 2022