urandom
是通過設備驅動程序提供的,內核與驅動程序做的第一件事情是致電init
調用。
如果你到這裏看看:http://lxr.free-electrons.com/source/drivers/char/random.c#L1401
* Note that setup_arch() may call add_device_randomness()
* long before we get here. This allows seeding of the pools
* with some platform dependent data very early in the boot
* process. But it limits our options here. We must use
* statically allocated structures that already have all
* initializations complete at compile time. We should also
* take care not to overwrite the precious per platform data
* we were given.
*/
static int rand_initialize(void)
{
init_std_data(&input_pool);
init_std_data(&blocking_pool);
init_std_data(&nonblocking_pool);
return 0;
}
early_initcall(rand_initialize);
所以,init
功能該驅動程序是rand_initialize
。但請注意,評論說setup_arch
可能會在此設備初始化之前調用add_device randomness()
。然而,調用該功能並不會添加任何實際的熵(它提供像MAC地址的東西池,所以如果你有兩個完全相同的虛擬機,你很好)。從評論:
* add_device_randomness() is for adding data to the random pool that
* is likely to differ between two devices (or possibly even per boot).
* This would be things like MAC addresses or serial numbers, or the
* read-out of the RTC. This does *not* add any actual entropy to the
* pool, but it initializes the pool to different values for devices
* that might otherwise be identical and have very little entropy
* available to them (particularly common in the embedded world).
另外,需要注意的是熵池存儲在關機並通過初始化腳本恢復開機時間(在我的Ubuntu 14.04,它在/etc/init.d/urandom
),所以你可能需要從調用腳本腳本前
53 (
54 date +%s.%N
55
56 # Load and then save $POOLBYTES bytes,
57 # which is the size of the entropy pool
58 if [ -f "$SAVEDFILE" ]
59 then
60 cat "$SAVEDFILE"
61 fi
62 # Redirect output of subshell (not individual commands)
63 # to cope with a misfeature in the FreeBSD (not Linux)
64 # /dev/random, where every superuser write/close causes
65 # an explicit reseed of the yarrow.
66 ) >/dev/urandom
或類似的呼叫。
修改'urandom'設備驅動程序在啓動時將其第一個值保存在某處,然後編寫一個簡單的'ioctl'來檢索它。 – Barmar
嗨@Barmar,這聽起來像個好主意,而且我還沒有在我的搜索中遇到過這個想法,但是你能指點我到哪個地方去解釋怎麼去做?我和我的團隊對內核開發相對不熟悉,雖然我們有足夠的能力進行必要的修改,但我們只需要知道從哪裏開始。內核是很容易理解的,但是源代碼本身是一個搜索周圍的巨大地方。 – user3703603
不知道,但我猜想整個'urandom'設備驅動程序只是一個源文件。 – Barmar