1*7a42da02Sgutteridge.\" $NetBSD: entropy.7,v 1.10 2023/07/20 04:16:14 gutteridge Exp $ 2cba96d16Sriastradh.\" 3cba96d16Sriastradh.\" Copyright (c) 2021 The NetBSD Foundation, Inc. 4cba96d16Sriastradh.\" All rights reserved. 5cba96d16Sriastradh.\" 6cba96d16Sriastradh.\" Redistribution and use in source and binary forms, with or without 7cba96d16Sriastradh.\" modification, are permitted provided that the following conditions 8cba96d16Sriastradh.\" are met: 9cba96d16Sriastradh.\" 1. Redistributions of source code must retain the above copyright 10cba96d16Sriastradh.\" notice, this list of conditions and the following disclaimer. 11cba96d16Sriastradh.\" 2. Redistributions in binary form must reproduce the above copyright 12cba96d16Sriastradh.\" notice, this list of conditions and the following disclaimer in the 13cba96d16Sriastradh.\" documentation and/or other materials provided with the distribution. 14cba96d16Sriastradh.\" 15cba96d16Sriastradh.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 16cba96d16Sriastradh.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17cba96d16Sriastradh.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18cba96d16Sriastradh.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 19cba96d16Sriastradh.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20cba96d16Sriastradh.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21cba96d16Sriastradh.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22cba96d16Sriastradh.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23cba96d16Sriastradh.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24cba96d16Sriastradh.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25cba96d16Sriastradh.\" POSSIBILITY OF SUCH DAMAGE. 26cba96d16Sriastradh.\" 27*7a42da02Sgutteridge.Dd June 30, 2023 28cba96d16Sriastradh.Dt ENTROPY 7 29cba96d16Sriastradh.Os 30cba96d16Sriastradh.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 31cba96d16Sriastradh.Sh NAME 32cba96d16Sriastradh.Nm entropy 33cba96d16Sriastradh.Nd random unpredictable secrets needed for security 34cba96d16Sriastradh.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 35cba96d16Sriastradh.Sh DESCRIPTION 36cba96d16SriastradhComputers need random unpredictable secrets for the security of 37cba96d16Sriastradhsoftware such as web browsers and 38cba96d16Sriastradh.Xr ssh 1 . 39cba96d16Sriastradh.Pp 40cba96d16SriastradhComputers are designed to behave in highly predictable ways, so they 41cba96d16Sriastradhrely on observations of random physical phenomena around them, called 42cba96d16Sriastradh.Nm entropy sources , 43cba96d16Sriastradhto derive unpredictable secrets for cryptography. 44cba96d16Sriastradh.Pp 45cba96d16SriastradhWhile some computers have reliable entropy sources such as hardware 46cba96d16Sriastradhrandom number generators based on thermal noise in silicon circuits, 47cba96d16Sriastradhothers may require operator intervention for security. 48cba96d16Sriastradh.\"""""""""""""""""""""""""""""""""""""" 49cba96d16Sriastradh.Ss Threats 50cba96d16Sriastradh.Bl -bullet 51cba96d16Sriastradh.It 52cba96d16SriastradhWeb browsers and programs such as 53cba96d16Sriastradh.Xr ssh 1 54cba96d16Sriastradhrely on unpredictable secrets in cryptography to prevent eavesdropping 55cba96d16Sriastradhand detect tampering of sessions over the network. 56cba96d16Sriastradh.It 57cba96d16Sriastradh.Xr ssh-keygen 1 58cba96d16Sriastradhrelies on unpredictable secrets to create keys that allow you to log in 59cba96d16Sriastradhbut keep out malicious adversaries; if an adversary could guess the key 60cba96d16Sriastradhthen they could impersonate you. 61cba96d16Sriastradh.It 62cba96d16Sriastradh.Nx 63cba96d16Sriastradhrelies on unpredictable secrets to make sure that private user data 64cba96d16Sriastradhstored on nonvolatile media when memory is scarce 652dba4c83Suwe.Po 662dba4c83Suwe.Xr swapctl 8 , 672dba4c83Suweusing 682dba4c83Suwe.Ql vm.swap_encrypt=1 ; 692dba4c83Suwesee 702dba4c83Suwe.Xr sysctl 7 712dba4c83Suwe.Pc 72cba96d16Sriastradhcannot be recovered by forensic tools after shutdown. 73cba96d16Sriastradh.El 74cba96d16Sriastradh.\"""""""""""""""""""""""""""""""""""""" 756646954aSuwe.Ss Entropy in NetBSD 76cba96d16Sriastradh.Nx 77cba96d16Sriastradhgathers samples from various kinds of entropy sources, including: 78cba96d16Sriastradh.Bl -bullet -compact 79cba96d16Sriastradh.It 80cba96d16Sriastradhhardware random number generators 81cba96d16Sriastradh.It 82cba96d16Sriastradhnetwork traffic timing 83cba96d16Sriastradh.It 84cba96d16Sriastradhuser input (keystrokes, mouse movements, etc.) 85cba96d16Sriastradh.It 86cba96d16Sriastradhdisk I/O latency 87cba96d16Sriastradh.It 88cba96d16Sriastradhenvironment sensors 89cba96d16Sriastradh.Pq Xr envsys 4 90cba96d16Sriastradh.El 91cba96d16SriastradhThe samples are mixed together with cryptography to yield unpredictable 92cba96d16Sriastradhsecrets through 93cba96d16Sriastradh.Pa /dev/urandom 94cba96d16Sriastradh.Pq see Xr rnd 4 95cba96d16Sriastradhand related interfaces used by programs like 96cba96d16Sriastradh.Xr ssh 1 , 97cba96d16SriastradhFirefox, and so on. 98cba96d16Sriastradh.Pp 99cba96d16Sriastradh.Nx 100cba96d16Sriastradhalso stores a random seed at 101cba96d16Sriastradh.Pa /var/db/entropy-file 102cba96d16Sriastradhto carry unpredictable secrets over from one boot to the next, as long 103cba96d16Sriastradhas the medium remains secret and can be updated on boot. 104cba96d16SriastradhThe seed is maintained automatically by 105cba96d16Sriastradh.Pa /etc/rc.d/random_seed 106cba96d16Sriastradh.Pq see Xr rc.conf 5 . 107cba96d16Sriastradh.\"""""""""""""""""""""""""""""""""""""" 108cba96d16Sriastradh.Ss Ensuring enough entropy 109cba96d16SriastradhEntropy is measured in bits, and only 256 bits of entropy are needed 110cba96d16Sriastradhfor security, thanks to modern cryptography. 111cba96d16Sriastradh.Pp 112cba96d16SriastradhTo detect potentially insecure systems, 113cba96d16Sriastradh.Nx 11496b2c7deSriastradhtakes measures to alert the operator if there isn't definitely enough 11596b2c7deSriastradhfor security: 116cba96d16Sriastradh.Bl -bullet 117cba96d16Sriastradh.It 11883f1fd31Sriastradh.Nx 11983f1fd31Sriastradhissues warnings on the console if there's not enough entropy when 12083f1fd31Sriastradhprograms need it; see 12183f1fd31Sriastradh.Xr rnd 4 . 12283f1fd31Sriastradh.It 12396b2c7deSriastradhThe 12496b2c7deSriastradh.Xr motd 5 125*7a42da02Sgutteridgehas a warning if there was not enough entropy when network daemons such as 12696b2c7deSriastradh.Xr sshd 8 12796b2c7deSriastradhfirst generated keys. 12896b2c7deSriastradh.It 12996b2c7deSriastradhThe daily security report includes an alert if there's still not enough 13083f1fd31Sriastradhentropy; see 13183f1fd31Sriastradh.Xr security.conf 5 . 132cba96d16Sriastradh.El 133cba96d16Sriastradh.Pp 13496b2c7deSriastradhSince it is hard to know how unpredictable most physical systems are, 13596b2c7deSriastradhonly devices specifically designed to be hardware random number 13696b2c7deSriastradhgenerators, or a seed file stored on disk, count toward these alerts. 13796b2c7deSriastradh.Pp 13896b2c7deSriastradhAt boot, 13996b2c7deSriastradh.Nx 14096b2c7deSriastradhwill wait, when 14196b2c7deSriastradh.Ql entropy=wait 14296b2c7deSriastradhis set in 14396b2c7deSriastradh.Xr rc.conf 5 , 14496b2c7deSriastradhor fail to single-user mode, when 14596b2c7deSriastradh.Ql entropy=check 14696b2c7deSriastradhis set, if there is not enough entropy from 14796b2c7deSriastradh.Em any 14896b2c7deSriastradhsources, including devices not designed to be unpredictable, such as 14996b2c7deSriastradhthe CPU cycle counter sampled by a periodic timer, provided the samples 15096b2c7deSriastradhpass a simple filter called the 15196b2c7deSriastradh.Sq entropy estimator , 15296b2c7deSriastradhlike other operating systems. 15396b2c7deSriastradhSources known to be predictable, which could give a false sense of 15496b2c7deSriastradhsecurity, can be disabled from unblocking boot by setting 15596b2c7deSriastradh.Li rndctl_flags 15696b2c7deSriastradhin 15796b2c7deSriastradh.Xr rc.conf 5 . 158cba96d16Sriastradh.Pp 159cba96d16SriastradhMany new computers have hardware random number generators, such as 160cba96d16SriastradhRDRAND/RDSEED in Intel/AMD CPUs, or ARMv8.5-RNDRRS; 161cba96d16Sriastradh.Xr virtio 4 Ns -based 162cba96d16Sriastradhvirtualization platforms such as QEMU can expose entropy from the host 163cba96d16Sriastradhwith 164cba96d16Sriastradh.Xr viornd 4 ; 165cba96d16Sriastradhbootloader firmware such as UEFI may also expose an underlying 166cba96d16Sriastradhplatform's random number generator. 167cba96d16Sriastradh.Pp 168cba96d16SriastradhHowever, many older computers have no reliable entropy sources. 169cba96d16SriastradhSome have the hardware, but have it off by default, such as a disabled 170cba96d16Sriastradh.Xr tpm 4 . 171cba96d16SriastradhOn computers with no built-in reliable entropy source, you may wish to 172cba96d16Sriastradhtransfer a seed from another computer with 173cba96d16Sriastradh.Xr rndctl 8 , 174cba96d16Sriastradhor manually enter samples into 175cba96d16Sriastradh.Pa /dev/urandom 176cba96d16Sriastradh\(em see below. 177cba96d16Sriastradh.\"""""""""""""""""""""""""""""""""""""" 178cba96d16Sriastradh.Ss Adding entropy 179cba96d16Sriastradh.Pp 180cba96d16SriastradhYou can manually save and load seeds with the 181cba96d16Sriastradh.Xr rndctl 8 182cba96d16Sriastradhtool. 183cba96d16SriastradhFor example, you might use 184cba96d16Sriastradh.Dl rndctl -S seed 185dd68197bSriastradhto save a seed from one machine, transfer it \(em over a medium where 186dd68197bSriastradhyou are confident there are no eavesdroppers \(em to another machine, 187dd68197bSriastradhand load it with 188cba96d16Sriastradh.Dl rndctl -L seed 189cba96d16Sriastradhon the target machine; then run 190cba96d16Sriastradh.Dl /etc/rc.d/random_seed stop 191cba96d16Sriastradhon the target machine to ensure that the entropy will be saved for next 192dd68197bSriastradhboot, even if the system later crashes or otherwise shuts down 193dd68197bSriastradhuncleanly. 194cba96d16Sriastradh.Ic rndctl -S 195cba96d16Sriastradhrecords the number of bits of entropy in the seed so that 196cba96d16Sriastradh.Ic rndctl -L 197cba96d16Sriastradhcan count it. 198cba96d16Sriastradh.Pp 199cba96d16SriastradhUsers can write data to 200cba96d16Sriastradh.Pa /dev/urandom 201cba96d16Sriastradhto be mixed together with all other samples. 202cba96d16SriastradhFor example, no matter what entropy sources are built into a computer, 203cba96d16Sriastradhyou can ensure it has enough entropy (as long as there are no 204cba96d16Sriastradhsurveillance cameras watching you) by flipping a coin 256 times and 205cba96d16Sriastradhrunning: 206cba96d16Sriastradh.Dl echo thttthhhhttththtttht... > /dev/urandom 207cba96d16SriastradhThen run 208cba96d16Sriastradh.Dl /etc/rc.d/random_seed stop 209cba96d16Sriastradhto ensure that the effort will be saved for next boot. 210cba96d16Sriastradh.Pp 211cba96d16SriastradhInputs from the superuser (uid 0) to 212cba96d16Sriastradh.Pa /dev/urandom 213cba96d16Sriastradhcount toward the system's entropy estimate, at the maximum rate of one 214cba96d16Sriastradhbit of entropy per bit of data; inputs from unprivileged users will 215cba96d16Sriastradhaffect subsequent outputs but will be counted as having zero entropy. 216cba96d16Sriastradh.Pp 217cba96d16SriastradhAfter adding entropy, 218cba96d16Sriastradh.Sy make sure to regenerate any long-term keys 219cba96d16Sriastradhthat might be predictable because they were previously generated with 220cba96d16Sriastradhtoo little entropy. 221cba96d16SriastradhFor example, if 22256afebefSuwe.Ql sshd=YES 223cba96d16Sriastradhis enabled in 224cba96d16Sriastradh.Pa /etc/rc.conf , 225cba96d16Sriastradhthen 226cba96d16Sriastradh.Nx 227cba96d16Sriastradhwill automatically generate ssh host keys on boot; if they were 228cba96d16Sriastradhgenerated with too little entropy, then you may wish to delete them and 229cba96d16Sriastradhcreate new ones before allowing anyone to log in via 230cba96d16Sriastradh.Xr ssh 1 . 231cba96d16Sriastradh.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 232dd68197bSriastradh.Sh DIAGNOSTICS 233dd68197bSriastradh.Nx 234dd68197bSriastradhmay print the following warnings to the console: 235a8a9d812Sriastradh.Bl -diag 236dd68197bSriastradh.It WARNING: system needs entropy for security; see entropy(7) 237dd68197bSriastradhSome process tried to draw use entropy from 238dd68197bSriastradh.Nx , 239dd68197bSriastradhe.g. to generate a key for cryptography, before enough inputs from 240dd68197bSriastradhreliable entropy sources have been obtained. 241dd68197bSriastradhThe entropy may be low enough that an adversary could guess keys by 242dd68197bSriastradhbrute force. 243dd68197bSriastradh.Pp 244dd68197bSriastradhThis message is rate-limited, so if you have added entropy and want to 245dd68197bSriastradhverify that the problem is resolved, you should consult the 246dd68197bSriastradh.Dv kern.entropy.needed 247dd68197bSriastradh.Xr sysctl 7 248dd68197bSriastradhvariable to confirm it is zero, rather than just look for the absence 249dd68197bSriastradhof this message; see 250dd68197bSriastradh.Xr rnd 4 251dd68197bSriastradhfor details. 252dd68197bSriastradh.El 253dd68197bSriastradh.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 254cba96d16Sriastradh.Sh SEE ALSO 255cba96d16Sriastradh.Xr getrandom 2 , 256cba96d16Sriastradh.Xr arc4random 3 , 257cba96d16Sriastradh.Xr rnd 4 , 258cba96d16Sriastradh.Xr rc.conf 5 , 259cba96d16Sriastradh.Xr rc 8 , 260cba96d16Sriastradh.Xr rndctl 8 261cba96d16Sriastradh.Rs 262cba96d16Sriastradh.%A Nadia Heninger 263cba96d16Sriastradh.%A Zakir Durumeric 264cba96d16Sriastradh.%A Eric Wustrow 265cba96d16Sriastradh.%A J. Alex Halderman 266cba96d16Sriastradh.%T Mining Your Ps and Qs: Detection of Widespread Weak Keys in Network Devices 267cba96d16Sriastradh.%B Proceedings of the 21st USENIX Security Symposium 268cba96d16Sriastradh.%I USENIX 269cba96d16Sriastradh.%D August 2012 270cba96d16Sriastradh.%P 205-220 271cba96d16Sriastradh.%U https://www.usenix.org/conference/usenixsecurity12/technical-sessions/presentation/heninger 272cba96d16Sriastradh.%U https://factorable.net/ 273cba96d16Sriastradh.Re 274cba96d16Sriastradh.Rs 275cba96d16Sriastradh.%T openssl \(em predictable random number generator 276cba96d16Sriastradh.%I Debian Security Advisory 277cba96d16Sriastradh.%O DSA-1571-1 278cba96d16Sriastradh.%D 2008-05-13 27903120d56Sriastradh.%U https://www.debian.org/security/2008/dsa-1571.html 280cba96d16Sriastradh.Re 281cba96d16Sriastradh.Rs 282cba96d16Sriastradh.%T Features/VirtIORNG 283cba96d16Sriastradh.%I QEMU Wiki 284cba96d16Sriastradh.%U https://wiki.qemu.org/Features/VirtIORNG 285cba96d16Sriastradh.%D 2016-10-17 286cba96d16Sriastradh.Re 287