在重新启动/关闭时保存随机种子,并在系统启动期间恢复它

信息安全 密码学 linux 随机的
2021-08-26 17:06:43

在 Debian 上(我猜在其他 linux 发行版上也是如此),有一个 init script /etc/init.d/urandom,它的功能是:

"Save and restore random seed between restarts"

在我的例子中,这个文件被保存到/var/lib/urandom/random-seed.

我想知道将随机种子保存到磁盘上的文件是多么明智,因为密码学中随机种子的整个假设是保密的。

首先,为什么要这样做。我们是否需要在重启之间“保留”随机种子?如果我们不这样做会发生什么?电脑开机时会不会有0随机性?

3个回答

首先,为什么要这样做。我们是否需要在重启之间“保留”随机种子?如果我们不这样做会发生什么?电脑开机时会不会有0随机性?

事实证明,这是一个比你可能意识到的更深层次的问题。我会在不写教科书的情况下尽量做到公正。

基本上:计算机随机性很差;当你有一些真正的随机性(又名熵)时,坚持下去是个好主意。

假设您的计算机没有硬件随机数生成器。(现代 Intel 芯片具有rdrand接入硬件 rng 的指令,但出于政治原因,Linux 内核不使用它。)

在启动时,内核从哪里获得纯随机性?根据维基百科

Linux 内核从键盘计时、鼠标移动和 IDE 计时生成熵,并通过特殊文件 /dev/random 和 /dev/urandom 将随机字符数据提供给其他操作系统进程。此功能是在 Linux 版本 1.3.30 中引入的。

所以鼠标、键盘和硬盘 IO 事件的时序。假设您在操作系统启动期间需要熵(例如,您sshd需要在第一次启动时生成密钥),您尚未加载鼠标和键盘驱动程序,并且在启动周期的早期您不会许多磁盘 IO 调用——见鬼,在启动的早期,内核仍在 RAM FS 中运行,即使在那之后,您也可能在具有非常可预测的 IO 时间的 SSD 或闪存驱动器上。

所以回到你的问题:

如果我们不这样做会发生什么?电脑开机时会不会有0随机性?

这是可能的。


在小型嵌入式 Linux 设备上,例如从闪存启动的路由器(小型家用设备和为互联网供电的大型数据中心设备),这实际上是一个严重的问题。

有关此主题的精彩阅读,请参阅 2012 年的论文Mining Your Ps and Qs: Detection of Widespread Weak Keys in Network Devices有令人震惊的发现:

由于密钥生成过程中的熵不足,0.75% 的 TLS 证书 [on the internet] 共享密钥。

Short-Description在你引用的下面几行,/etc/init.d/urandom说明了一些关于保密的假设:

## Assumption 1:  We assume [/var/lib/urandom/random-seed] is a file (or a symlink
## to a file) that resides on a non-volatile medium that persists
## across reboots.
## Case 1a: Ideally, it is readable and writeable.  Its is unshared,
## i.e. its contents are unique to this machine.  It is protected so
## that its contents are not known to attackers.
## Case 1b: Less than ideally, it is read-only.  Its contents are
## unique to this machine and not known to attackers.

稍后在将种子写入磁盘的文件中,有一条注释:

# see documentation in linux/drivers/char/random.c

值得一读,但包括:

* When any operating system starts up, it will go through a sequence
* of actions that are fairly predictable by an adversary, especially
* if the start-up does not involve interaction with a human operator.
* This reduces the actual number of bits of unpredictability in the
* entropy pool below the value in entropy_count.  In order to
* counteract this effect, it helps to carry information in the
* entropy pool across shut-downs and start-ups.

... Even with
* complete knowledge of the start-up activities, predicting the state
* of the entropy pool requires knowledge of the previous history of
* the system.

在重新启动之间保存熵是解决启动时熵短缺的不完美解决方案。为什么不完美?首先,因为保存的熵是可发现的,如果潜在的攻击者能够获得保存的种子,他们也可以破坏所有以它为种子的随机数生成器。其次,因为当系统从备份中恢复或使用相同的已保存种子生成多个 VM 实例时,它们都将重用相同的已保存熵。

尽管如此,这样的灾难仍然比您的系统没有可用的启动时熵更可取。

请记住,如果您配置为保存熵,您的解决方案将无法获得认证,因为 FIPS 和几乎任何其他加密和信息安全相关标准都禁止这种做法。