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

Thoughts on Rust Cryptography

tarcieri
December 19, 2014

Thoughts on Rust Cryptography

Bay Area Rust Meetup - Mozilla SF (2014)

Video: https://www.youtube.com/watch?v=fO_ox-DGDqw

tarcieri

December 19, 2014
Tweet

More Decks by tarcieri

Other Decks in Programming

Transcript

  1. Thoughts on
    Rust Cryptography
    Tony Arcieri
    Mozilla SF
    December 19th, 2014

    View Slide

  2. View Slide

  3. View Slide

  4. View Slide

  5. This Talk
    • 2014: This year in cryptography
    • Is Rust a good language for crypto?
    • Survey of the Rust crypto landscape
    • A Rust common crypto library
    • Measuring timing variability in Rust

    View Slide

  6. 2014
    Year In Review

    View Slide

  7. • Java: Bleichenbacher OOB MitM (JCE)
    • Apple: “goto fail” MitM (SecureTransport)
    • GNUTLS: “goto cleanup” MitM
    • OpenSSL: “Heartbleed” Memory Exposure
    • TLS: Triple-Handshake MitM
    • NSS: “BERserk” MitM (Firefox/Chrome)
    • SSLv3: “POODLE” ciphertext recovery
    • Microsoft: “Winshock” RCE (SChannel)

    View Slide

  8. • Java: Bleichenbacher OOB MitM (JCE)
    • Apple: “goto fail” MitM (SecureTransport)
    • GNUTLS: “goto cleanup” MitM
    • OpenSSL: “Heartbleed” Memory Exposure
    • TLS: Triple-Handshake MitM
    • NSS: “BERserk” MitM (Firefox/Chrome)
    • SSLv3 (and TLS): “POODLE” ciphertext recovery
    • Microsoft: “Winshock” RCE (SChannel) BITES AGAIN!!!

    View Slide

  9. Themes?

    View Slide

  10. • Java: Bleichenbacher OOB MitM (JCE)
    • Apple: “goto fail” MitM (SecureTransport)
    • GNUTLS: “goto cleanup” MitM
    • OpenSSL: “Heartbleed” Memory Exposure
    • TLS: Triple-Handshake MitM
    • NSS: “BERserk” MitM (Firefox/Chrome)
    • SSLv3 (and TLS): “POODLE” ciphertext recovery
    • Microsoft: “Winshock” RCE (SChannel)
    SSL/TLS Design Flaws

    View Slide

  11. • Java: Bleichenbacher OOB MitM (JCE)
    • Apple: “goto fail” MitM (SecureTransport)
    • GNUTLS: “goto cleanup” MitM
    • OpenSSL: “Heartbleed” Memory Exposure
    • TLS: Triple-Handshake MitM
    • NSS: “BERserk” MitM (Firefox/Chrome)
    • SSLv3 (and TLS): “POODLE” ciphertext recovery
    • Microsoft: “Winshock” RCE (SChannel)
    SSL/TLS Design Flaws

    View Slide

  12. https://xkcd.com/1354/

    View Slide

  13. • Java: Bleichenbacher OOB MitM (JCE)
    • Apple: “goto fail” MitM (SecureTransport)
    • GNUTLS: “goto cleanup” MitM
    • OpenSSL: “Heartbleed” Memory Exposure
    • TLS: Triple-Handshake MitM
    • NSS: “BERserk” MitM (Firefox/Chrome)
    • SSLv3 (and TLS): “POODLE” ciphertext recovery
    • Microsoft: “Winshock” RCE (SChannel)
    SSL/TLS Design Flaws

    View Slide

  14. • Java: Bleichenbacher OOB MitM (JCE)
    • Apple: “goto fail” MitM (SecureTransport)
    • GNUTLS: “goto cleanup” MitM
    • OpenSSL: “Heartbleed” Memory Exposure
    • TLS: Triple-Handshake MitM
    • NSS: “BERserk” MitM (Firefox/Chrome)
    • SSLv3 (and TLS): “POODLE” ciphertext recovery
    • Microsoft: “Winshock” RCE (SChannel)
    C is a crappy language

    View Slide

  15. “goto fail”
    Apple SecureTransport

    View Slide

  16. http://tinyurl.com/tlsundersiege

    View Slide

  17. “goto fail”

    View Slide

  18. “goto fail”
    oops

    View Slide

  19. “goto fail”
    dead code

    View Slide

  20. “goto fail”
    mandatory
    braces?

    View Slide

  21. “goto fail”
    automatic
    cleanup?

    View Slide

  22. “goto fail”
    goto
    considered
    harmful
    maybe?

    View Slide

  23. “goto cleanup”
    GNUTLS
    http://www.cigital.com/justice-league-blog/2014/03/07/understanding-gnutls-certificate-verification-bug/

    View Slide

  24. GOTO BAD!!!

    View Slide

  25. Actually, it’s about representing
    booleans as integers

    View Slide

  26. _gnutls_verify_certificate2

    View Slide

  27. _gnutls_verify_certificate2
    zero == NOT CA

    View Slide

  28. check_if_ca
    0: false
    1: true

    View Slide

  29. check_if_ca

    View Slide

  30. check_if_ca
    less-than-zero == ERROR

    View Slide

  31. check_if_ca
    less-than-zero == ERROR

    View Slide

  32. check_if_ca
    less-than-zero == ERROR

    View Slide

  33. _gnutls_verify_certificate2
    less-than-zero != zero

    View Slide

  34. _gnutls_verify_certificate2
    less-than-zero == IT’S A CA!!!

    View Slide

  35. How do we fix this?

    View Slide

  36. “Should’ve used != 1”

    View Slide

  37. true and false

    View Slide

  38. true and false
    and maybe get rid of goto…

    View Slide

  39. Is zero false or true?

    View Slide

  40. What’s going on?
    https://www.openssl.org/docs/ssl/SSL_shutdown.html

    View Slide

  41. What’s going on?
    https://www.openssl.org/docs/ssl/SSL_shutdown.html
    when in doubt, call twice

    View Slide

  42. OpenSSL

    View Slide

  43. View Slide

  44. Which was the worst?

    View Slide

  45. “Winshock”
    Microsoft SChannel Remote Code Execution

    View Slide

  46. Remote Code Execution

    View Slide

  47. How do we fix this?

    View Slide

  48. Memory Safety

    View Slide

  49. Things that might help
    • Dead code detection
    • Mandatory braces
    • Automatic resource management
    • No goto statement
    • true/false (and better ways of handling errors)
    • Memory safety

    View Slide

  50. Is Rust a good
    language for crypto?

    View Slide

  51. Things Rust has
    • Dead code detection
    • Mandatory braces
    • Automatic resource management
    • No goto statement
    • true/false (and better ways of handling errors)
    • Memory safety

    View Slide

  52. What problems do we
    need to solve to create
    robust cryptography?

    View Slide

  53. Timing side-channels

    View Slide

  54. http://cr.yp.to/antiforgery/cachetiming-20050414.pdf

    View Slide

  55. http://cr.yp.to/antiforgery/cachetiming-20050414.pdf

    View Slide

  56. http://tonyarcieri.com/cream-the-scary-ssl-attack-youve-probably-never-heard-of

    View Slide

  57. Bad: OpenSSL

    View Slide

  58. Good: djb

    View Slide

  59. Lucky 13
    http://www.ieee-security.org/TC/SP2013/papers/4977a526.pdf

    View Slide

  60. — Nadhem J. AlFardan and Kenneth G. Paterson
    "In this sense, the attacks do not pose a
    significant danger to ordinary users of TLS in
    their current form. However, it is a truism that
    attacks only get better with time, and we
    cannot anticipate what improvements to our
    attacks, or entirely new attacks, may yet be
    discovered."

    View Slide

  61. http://www.ieee-security.org/TC/SP2013/papers/4977a526.pdf

    View Slide

  62. https://github.com/dmayer/time_trial

    View Slide

  63. What rules must we follow to
    create robust cryptographic
    implementations?

    View Slide

  64. Cryptocoding.net Rules
    • Compare secret strings in constant time
    • Avoid branchings controlled by secret data
    • Avoid table look-ups indexed by secret data
    • Avoid secret-dependent loop bounds
    • Prevent compiler interference with security-critical operations
    • Prevent confusion between secure and insecure APIs
    • Avoid mixing security and abstraction levels of cryptographic primitives in the same API layer
    • Use unsigned bytes to represent binary data
    • Use separate types for secret and non-secret information
    • Use separate types for different types of information
    • Clean memory of secret data
    • Use strong randomness

    View Slide

  65. Constant Time Operation
    • Compare secret strings in constant time
    • Avoid branchings controlled by secret data
    • Avoid table look-ups indexed by secret data
    • Avoid secret-dependent loop bounds

    View Slide

  66. Avoid “Optimizations”
    • Prevent compiler interference with security-
    critical operations

    View Slide

  67. Clear Abstractions
    • Prevent confusion between secure and insecure
    APIs
    • Avoid mixing security and abstraction levels of
    cryptographic primitives in the same API layer

    View Slide

  68. Types!
    • Use unsigned bytes to represent binary data
    • Use separate types for secret and non-secret
    information
    • Use separate types for different types of
    information

    View Slide

  69. Clean Up Secrets
    Zero out memory when done

    View Slide

  70. Use Strong
    Randomess

    View Slide

  71. People are working on
    solutions to all of these
    problems already

    View Slide

  72. No changes to Rust
    needed!

    View Slide

  73. View Slide

  74. View Slide

  75. Crypto in the standard
    library limits agility

    View Slide

  76. We need all the agility
    we can get

    View Slide

  77. Crypto libraries should be
    developed and released
    by crypto experts

    View Slide

  78. Survey of the Rust
    crypto landscape

    View Slide

  79. What Rust crypto libraries
    should I use today?

    View Slide

  80. rust-openssl
    https://github.com/sfackler/rust-openssl

    View Slide

  81. rust-openssl
    https://github.com/sfackler/rust-openssl

    View Slide

  82. rust-openssl
    https://github.com/sfackler/rust-openssl

    View Slide

  83. http://tinyurl.com/tlsundersiege

    View Slide

  84. sodiumoxide
    https://github.com/dnaq/sodiumoxide

    View Slide

  85. View Slide

  86. Is it safe?

    View Slide

  87. libsodium
    • Portable repackaging of the Networking and
    Cryptography Library (NaCl a.k.a. “salt”)
    • Includes Ed25519 and ChaCha20
    • Includes the scrypt password hashing function
    • Includes the Blake2 hash function
    • Includes SipHash
    • Some optional libsodium-specific utility functions

    View Slide

  88. Pure Rust Crypto
    WARNING: Proceed with caution!

    View Slide

  89. View Slide

  90. rust-crypto
    https://github.com/DaGenix/rust-crypto

    View Slide

  91. rust-crypto
    • Promising start!
    • Some good implementations
    • Some questionable implementations
    • Sometimes they’re the same!

    View Slide

  92. AES-NI
    “Advanced Encryption Standard New Instructions”
    Hardware implementation of AES from Intel

    View Slide

  93. https://github.com/DaGenix/rust-crypto/blob/master/src/rust-crypto/aesni.rs

    View Slide

  94. https://github.com/DaGenix/rust-crypto/blob/master/src/rust-crypto/aesni.rs
    o_O “aseni? Did you mean “aesni"?

    View Slide

  95. https://github.com/DaGenix/rust-crypto/blob/master/src/rust-crypto/aesni.rs

    View Slide

  96. asm!

    View Slide

  97. Avoid “Optimizations”
    • Prevent compiler interference with security-
    critical operations

    View Slide

  98. https://github.com/DaGenix/rust-crypto/blob/master/src/rust-crypto/aesni.rs

    View Slide

  99. https://github.com/DaGenix/rust-crypto/blob/master/src/rust-crypto/aesni.rs

    View Slide

  100. Fast and safe!

    View Slide

  101. rust-crypto tl;dr
    • Good effort!
    • No immediate security problems on cursory
    inspection
    • Clear signs the code has not been well-reviewed
    • Needs more expert scrutiny to be trusted

    View Slide

  102. TARS
    Protected memory buffers for Rust
    https://github.com/seb-m/tars

    View Slide

  103. Protect Keys!
    • mprotect! PROT_NONE guard pages
    • mlock! prevents keys from being swapped out
    • volatile zero on drop

    View Slide

  104. rust-consttime
    WARNING: DO NOT USE THIS!!!!!!!!!!

    View Slide

  105. View Slide

  106. Warning: While this library tries to avoid
    branches or input-dependent memory
    operations, the code optimizer may
    reintroduce them and you should carefully
    verify the assembly in order to use this.

    View Slide

  107. you should carefully
    verify the assembly in
    order to use this.

    View Slide

  108. View Slide

  109. How do we escape from
    LLVM’s optimizations?

    View Slide

  110. asm!

    View Slide

  111. ASM Problems
    • Architecture specific
    • Hard to write
    • Error-prone
    • Difficult to verify

    View Slide

  112. Is it possible to generate
    constant-time assembly
    from Rust source code?

    View Slide

  113. nadeko
    Constant-time syntax extension for a limited subset of Rust
    https://github.com/klutzy/nadeko

    View Slide

  114. BLACK
    F&@KING
    MAGIC!
    WARNING:

    View Slide

  115. View Slide

  116. Is it constant time?

    View Slide

  117. NOPE!

    View Slide

  118. NOPE!
    THANKS LLVM!

    View Slide

  119. With nadeko
    No branches!

    View Slide

  120. ROT13
    Super-secure constant time version

    View Slide

  121. View Slide

  122. View Slide

  123. nadeko
    • Constant-time arithmetic and bitwise ops
    • Constant-time basic “if”
    • No support for “for” yet

    View Slide

  124. suruga
    Ultra-modern (i.e. practically useless) TLS stack
    https://github.com/klutzy/suruga

    View Slide

  125. View Slide

  126. Towards a Rust
    common crypto library

    View Slide

  127. What should a common
    crypto library provide?

    View Slide

  128. Isn’t that just

    rust-crypto?

    View Slide

  129. Shared Unsafe Code
    rust-openssl sodiumoxide rust-crypto
    Encapsulate and reuse
    unsafe primitives

    View Slide

  130. Rust Common Crypto
    • Handle zeroing buffers on drop
    • Protected buffers for keys (ala TARS)
    • Traits! Traits! Traits!
    • Constant-time primitives (ala Nadeko)

    View Slide

  131. sodiumoxide example

    View Slide

  132. sodiumoxide example

    View Slide

  133. sodiumoxide example

    View Slide

  134. Can we do better?

    View Slide

  135. Common crypto library
    • Common foundation for all crypto libraries
    • Solve unsafe concerns in one place
    • Solve constant-time operation in one place
    • Promote interoperability

    View Slide

  136. Measuring timing
    variability in Rust

    View Slide

  137. Verifying constant time
    operation requires
    counting CPU cycles

    View Slide

  138. Modern CPU
    • Example clock speed: 2.5 GHz
    • 2,500,000,000 CPU cycles per second
    • 0.4 nanoseconds per cycle
    • 400 picoseconds per cycle

    View Slide

  139. Mind Your Nanoseconds!

    View Slide

  140. How do we measure
    CPU cycles?

    View Slide

  141. Timestamp Counter
    (TSC)

    View Slide

  142. RDTSC in Rust
    TSC: 85211194993141998

    View Slide

  143. Cyclometer
    https://github.com/cryptosphere/cyclometer

    View Slide

  144. Cyclometer
    • Automated testing for data-dependent timing
    variability in Rust cryptographic libraries
    • Use RDTSC to measure timing
    • Collect a large number of samples and apply
    statistical test (e.g. Box Test) to determine if
    timing variability is distinguishable

    View Slide

  145. Lucky 13
    http://www.ieee-security.org/TC/SP2013/papers/4977a526.pdf

    View Slide

  146. Automatically detect
    data-dependent timing

    View Slide

  147. Vaporware
    Pull requests accepted!

    View Slide

  148. Final thoughts
    • Rust has immense potential for cryptography but
    we must tread carefully
    • Rust code without any branches isn’t necessarily
    constant time. Beware LLVM!
    • Stick with wrappers to mainstream crypto
    libraries until pure Rust crypto is more mature

    View Slide

  149. Join the Rust crypto
    IRC channel!
    irc.mozilla.org:6697
    #rust-crypto

    View Slide

  150. That’s all folks!

    View Slide

  151. Twitter
    @bascule
    Blog
    tonyarcieri.com
    IRC
    Mozilla: bascule
    Freenode: tarcieri

    View Slide