Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The plain simple reality of entropy @ PacSec 2015

The plain simple reality of entropy @ PacSec 2015

Filippo Valsorda

November 12, 2015
Tweet

More Decks by Filippo Valsorda

Other Decks in Programming

Transcript

  1. The plain simple reality of entropy Or how I learned

    to stop worrying and love urandom Filippo Valsorda
  2. Filippo Valsorda @FiloSo<le Cryptography and systems engineer at Developer of

    the CloudFlare DNS server, built its DNSSEC implementaCon ODen implement, teach or break cryptography or security protocols
  3. Random bytes b9dbba0b7370260aa57f24dbacaf0ade3654eae237c42cadc29b730067d5 467c29d6201bc412440c81c91cab1d95dadd0411ea57b86a3973bd9032ec c7083a0c9e96c2e3b209bd2b0a1ee4cd0de345b32f2b848dba9312428aa7 d5a067d6be0b0a5dc90695946706e0940f0cf3821f056056e756df138f8c 29fb57acc4d21f382de3df24fbb6f3f145c3d9f194285d01ac05a44f4705 40a83c0aeeb1ed32320c072711074151c866fb8f66066bcead84edd84a49 e8dd7c02c76aeb1b15b573143d8caa49267d1a1c4b9a0fa0897595839046 29956205f91a5ce94951c8fad74370acda246f20d1b365e817a6d6ac9f31

    e3171e3a50007001eabc655f2d0485d5c6f8b515de7f31d3c65956301af1 1e71a2215824fb401d7e2c839505924e57ec304dc63f7e11de0673fbac3d a48526ba13edf61b50f20e110f9865c9692ea5c0981ac253262ebcde6242 a62701fb8cb3853557417811a63f7db94c5c050f2cd552b8eb99c99d5efe 3790f405544a46b713f7329d8f77e075053c7a6958798a99ddeb89f6381b 7a6676c612fc6cdc658584c29715c6739905b312a501cb5abb809f9d2714 319d92956f8b197119af6876091507c92eccf438dfca175211e1e6f060ba
  4. Random bytes • Keys
 ssh-keygen -b 2048
 SSL/TLS symmetric keys

    • Nonces, (EC)DSA signatures • TCP Sequence number, DNS Query ID • ASLR (Address space layout randomizaCon) • Passphrases, tokens, … • SimulaCons
  5. Unpredictable events ȡ Ý • Visible to the kernel •

    Carrying variable amounts of entropy • Not uniform and usually not enough
  6. SHA2-512( ) Example hash-based CSPRNG 1492bcf6062118a27098c426122651 805958a9b7149a8b4c534fb8721d81 d59029df69ddb0624fb07ecd55d4e0 6a74e0dcdf8b209576d2705e520eb5 9f1a3212b0e30d445cd08d06b3ccf8

    fedb56c946266cb56d0df18dd2c79f a09087f6a580f7f1dc8a1840de5483 75aeebc228421a1dadc091b9088b99 1b38b2f77e478cff4cc92ff99fa06d9029a2cf8a10f5cfee83ea2e7bd8a123f7 31ff26c51e048c5030cb3469349fe221835f7ffc70893c5b2674691b7dafc744
  7. Example hash-based CSPRNG 0fb23fc707f8764892f3fa613c6ea24b 27d159a6d29e6daa1a53297e96155022 17268915dd48c60864aca2de3f105266 4452b99f41e1f7221711529eb3191b9f SHA2-512( || 0

    ) f376eb8cee3b9c4c44c3b467a417ab7b f577ac352cf9705eda0e98b6e32daad7 318aa173e463e1f9cb1e93806fd702e3 c58946ff9320aae429385e22aa6ba271 Entropy pool 0fb23fc707f8764892f3fa613c6ea24b 27d159a6d29e6daa1a53297e96155022 17268915dd48c60864aca2de3f105266 4452b99f41e1f7221711529eb3191b9f SHA2-512( || 1 ) 23658111d9e6893ce67f8e4b5c37bc1f 7e05a327b0b152c60e62df48fb7ab38a 23658111d9e6893ce67f8e4b5c37bc1f 7e05a327b0b152c60e62df48fb7ab38a Random bytes 0fb23fc707f8764892f3fa613c6ea24b 27d159a6d29e6daa1a53297e96155022 17268915dd48c60864aca2de3f105266 4452b99f41e1f7221711529eb3191b9f SHA2-512( || 2 ) 358d7a4b03fe0f7c67b5a1d62d106e62 b4eb1d15c10e5ed3200acead3f5d8d16 75dfb256a5a270a96f69393f3dbc0ee2 77df4663cafcbdcfca8ae32aadd605a8 …
  8. /dev/urandom $ head -c 300 /dev/urandom | xxd 0000000: f60d

    4bda 67a1 83b4 d095 0db9 5366 0bb7 ..K.g.......Sf.. 0000010: bf20 7474 2b62 8a61 88f5 7938 52ed f77a . tt+b.a..y8R..z 0000020: c2e7 6fa9 3c66 2998 dc54 a6cb 8c59 caa6 ..o.<f)..T...Y.. 0000030: ac37 9640 81d5 1691 09ca 1d64 6d4f 7e9f [email protected]~. 0000040: 6749 8674 4df6 e6d3 85de 4e19 e979 63f2 gI.tM.....N..yc. 0000050: de44 09c5 d6c7 b26b 6407 35e9 5bd3 cbd6 .D.....kd.5.[... 0000060: 1a02 10b8 6111 9713 57a6 191c 5e27 601c ....a...W...^'`. 0000070: 6965 1fc2 5798 8faf 5f6b 104f 351c b2b5 ie..W..._k.O5... 0000080: 573f 9bb9 10bf 16f6 fe0d fdff 2e49 2d86 W?...........I-. 0000090: c183 1cc1 25f1 923e 54ec e235 7ff4 db05 ....%..>T..5.... 00000a0: 56bd 2b26 4e87 a7ad 6542 f01e 183c 718f V.+&N...eB...<q. 00000b0: 7437 6f31 4af6 17e7 7870 ccc9 61e3 dd94 t7o1J...xp..a... 00000c0: 72d1 1b46 bf17 c8ed 2b67 f440 3c34 c22e r..F....+g.@<4.. 00000d0: a21d eb8c 5a16 3a5e 8744 6920 2b16 6d81 ....Z.:^.Di +.m. 00000e0: 6ca8 8205 63e4 3b31 92ba 03ec 0b86 256d l...c.;1......%m 00000f0: 799f b699 3a2f 9699 bc31 72d6 c225 3021 y...:/...1r..%0!
  9. /dev/[u]random Keeps a pool of 4096 bit #define INPUT_POOL_SHIFT 12

    #define INPUT_POOL_WORDS (1 << (INPUT_POOL_SHIFT-5)) static __u32 input_pool_data[INPUT_POOL_WORDS]; static struct entropy_store input_pool = { .poolinfo = &poolinfo_table[0], .name = "input", .limit = 1, .lock = __SPIN_LOCK_UNLOCKED(input_pool.lock), .pool = input_pool_data }; hkps:/ /github.com/torvalds/linux/blob/85051e295/drivers/char/random.c
  10. /dev/[u]random The pool is mixed with unpredictable bytes from various

    sources using a fast CRC-like hash /* * This function adds bytes into the entropy “pool". * The pool is stirred with a primitive polynomial of the * appropriate degree, and then twisted. We twist by three * bits at a time because it's cheap to do so and helps * slightly in the expected case where the entropy is * concentrated in the low-order bits. */ static void _mix_pool_bytes(struct entropy_store *r, const void *in, int nbytes) hkps:/ /github.com/torvalds/linux/blob/85051e295/drivers/char/random.c
  11. /dev/[u]random Random numbers are generated by hashing the pool with

    SHA1 static void extract_buf(struct entropy_store *r, __u8 *out) { sha_init(hash.w); /* Generate a hash across the pool, 16 words (512 bits) at a time */ for (i = 0; i < r->poolinfo->poolwords; i += 16) sha_transform(hash.w, (__u8 *)(r->pool + i), workspace); __mix_pool_bytes(r, hash.w, sizeof(hash.w)); hash.w[0] ^= hash.w[3]; hash.w[1] ^= hash.w[4]; hash.w[2] ^= rol32(hash.w[2], 16); memcpy(out, &hash, EXTRACT_SIZE); } hkps:/ /github.com/torvalds/linux/blob/85051e295/drivers/char/random.c
  12. /dev/[u]random /dev/random and /dev/urandom use the same code, sizes and

    entropy sources static ssize_t random_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos) extract_entropy_user(&blocking_pool, buf, nbytes); static ssize_t urandom_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos) extract_entropy_user(&nonblocking_pool, buf, nbytes); hkps:/ /github.com/torvalds/linux/blob/85051e295/drivers/char/random.c
  13. /dev/[u]random Only difference: /dev/random • tries to guess how many

    bits of entropy were mixed in the pool • decreases that number when random bytes are read • blocks if number is low
  14. /dev/[u]random If CSPRNG leaked secret bits: • Stream ciphers wouldn’t

    work • CTR wouldn’t work • TLS wouldn’t work • PGP wouldn’t work Cryptography relies on this.
  15. /dev/[u]random /dev/urandom is safe for crypto • Google’s BoringSSL uses

    it • Python, Go, Ruby use it • Sandstorm replaces /dev/random with it • Cryptographers say so, too • other OS don’t have /dev/random
  16. /dev/[u]random ✦ hkps:/ /www.imperialviolet.org/2015/10/17/boringssl.html ✦ hkp:/ /sockpuppet.org/blog/2014/02/25/safely-generate-random-numbers/ ✦ hkp:/ /www.2uo.de/myths-about-urandom/

    ✦ hkp:/ /blog.cr.yp.to/20140205-entropy.html ✦ hkps:/ /developer.apple.com/library/mac/documentaCon/Darwin/Reference/ ManPages/man4/random.4.html ✦ hkps:/ /en.wikipedia.org/wiki/ /dev/random#FreeBSD ✦ hkps:/ /docs.sandstorm.io/en/latest/developing/security-pracCces/ ✦ hkp:/ /lists.randombit.net/pipermail/cryptography/2013-August/004983.html
  17. /dev/[u]random • You don’t need /dev/random • You don’t need

    to keep measuring your entropy pool • You don’t need to “refill” the pool (like haveged, etc.) • Random numbers “quality” does not decrease
  18. Unseeded CSPRNG Very early at boot, /dev/urandom might not be

    seeded yet, predictable. This is a Linux shortcoming. The OS must save the entropy pool at poweroff. Your distribuCon probably does already.
  19. Unseeded CSPRNG Very early at boot, /dev/urandom might not be

    seeded yet, predictable. This is a Linux shortcoming. The OS must save the entropy pool at poweroff. Embedded devices oDen don’t.
  20. Unseeded CSPRNG A beker API is now available: getrandom(2) It’s

    equivalent to OpenBSD getentropy(2) Behaves like urandom, but blocks at boot unCl the pool is iniCalized hkps:/ /lwn.net/ArCcles/606552/
  21. Unseeded CSPRNG Userspace CSPRNG are more dangerous: it’s easy to

    enCrely forget to seed them. It happened: hkp:/ /android-developers.blogspot.it/ 2013/08/some-securerandom-thoughts.html
  22. hkp:/ /android-developers.blogspot.it/ 2013/08/some-securerandom-thoughts.html Unseeded CSPRNG “ExploiCng ECDSA Failures in the

    Bitcoin Blockchain” hkps:/ /speakerdeck.com/filoso<le/exploiCng-ecdsa- failures-in-the-bitcoin-blockchain
  23. Broken CSPRNG Between 2006 and 2008, in Debian, the OpenSSL

    CSPRNG was crippled, seeding only with the PID. All outputs, keys, etc. of anything using it were easily predictable. hkps:/ /www.debian.org/security/2008/dsa-1571
  24. Broken CSPRNG --- openssl/trunk/rand/md_rand.c 2006/05/02 16:25:19 140 +++ openssl/trunk/rand/md_rand.c 2006/05/02

    16:34:53 141 @@ -271,7 +271,10 @@ static void ssleay_rand_add( else MD_Update(&m,&(state[st_idx]),j); +/* + * Don't add uninitialised data. MD_Update(&m,buf,j); +*/ MD_Update(&m,(unsigned char *)&(md_c[0]),sizeof(md_c)); MD_Final(&m,local_md); md_c[1]++;
  25. Non-CS PRNG Not all PRNG are Cryptographically Secure Normal PRNG

    are usually fast and uniform, but not unpredictable.
  26. Non-CS PRNG MT19937 core is a state of 624 numbers,

    and a funcCon like this def get_mt_random_number(): if index == 0: generate_numbers() y = STATE[index] y ^= y >> 11 y ^= (y << 7) & 0x9d2c5680 y ^= (y << 15) & 0xefc60000 y ^= y >> 18 index = (index + 1) % 624 return y
  27. Non-CS PRNG Given an output, it’s easy to reconstruct the

    state number from which it was generated def untemper(y): x = y for i in range(32 // 18): y ^= x >> (18 * (i + 1)) for i in range(32 // 15): y ^= (((y >> (i*15)) % (2**15)) << ((i+1)*15)) & 0xefc60000 for i in range(32 // 7): y ^= (((y >> (i*7)) % (2**7)) << ((i+1)*7)) & 0x9d2c5680 x = y for i in range(32 // 11): y ^= x >> (11 * (i + 1)) return y
  28. Non-CS PRNG ADer seeing just 624 outputs, we can predict

    all future outputs. def untemper(y): x = y for i in range(32 // 18): y ^= x >> (18 * (i + 1)) for i in range(32 // 15): y ^= (((y >> (i*15)) % (2**15)) << ((i+1)*15)) & 0xefc60000 for i in range(32 // 7): y ^= (((y >> (i*7)) % (2**7)) << ((i+1)*7)) & 0x9d2c5680 x = y for i in range(32 // 11): y ^= x >> (11 * (i + 1)) return y
  29. To sum up: Don’t use non-CS PRNG like MT. You

    don’t need /dev/random. Avoid userspace CSPRNG. Use /dev/urandom
  30. Thank you! Q/A Doubts? Unconvinced? Ask! Filippo Valsorda — @FiloSo<le

    filippo@cloudflare.com Slides: hkps:/ /filippo.io/entropy-talk