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

CryptoSwift: Crypto You Can Do

CryptoSwift: Crypto You Can Do

Marcin Krzyzanowski

March 22, 2015
Tweet

More Decks by Marcin Krzyzanowski

Other Decks in Programming

Transcript

  1. CryptoSwift
    Crypto You Can Do
    Marcin Krzyżanowski
    SwiftSummit, London 2015

    View Slide

  2. Cryptography
    and
    Swift

    View Slide

  3. Enigma

    View Slide

  4. View Slide

  5. Now we have
    computers

    View Slide

  6. Calculations are
    performed
    by “the magic box”

    View Slide

  7. Frameworks

    View Slide

  8. Cryptography and Swift
    • CommonCrypto

    View Slide

  9. Cryptography and Swift
    • CommonCrypto
    • OpenSSL

    View Slide

  10. Cryptography and Swift
    • CommonCrypto
    • OpenSSL
    • NaCl

    View Slide

  11. Cryptography and Swift
    • CommonCrypto
    • OpenSSL
    • NaCl
    • CryptoSwift

    View Slide

  12. Cryptography and Swift
    • CommonCrypto
    • OpenSSL
    • NaCl
    • CryptoSwift
    • CryptoJS….

    View Slide

  13. CommonCrypto

    View Slide

  14. CommonCrypto
    • The C library
    • Part of the system (iOS and OS X)
    • Can be used with Swift (thanks to C
    interoperability)

    View Slide

  15. CommonCrypto
    • The C library
    • Part of the system (iOS and OS X)
    • Can be used with Swift (thanks to C
    interoperability)
    ‣ Unsafe pointers

    View Slide

  16. CommonCrypto
    • The C library
    • Part of the system (iOS and OS X)
    • Can be used with Swift (thanks to C
    interoperability)
    ‣ Unsafe pointers
    • Sources available at opensource.apple.com

    View Slide

  17. CommonCrypto
    CCCrypt(
    UInt32(kCCEncrypt),
    UInt32(kCCAlgorithmAES128),
    UInt32(kCCOptionPKCS7Padding),
    keyBytes, // UnsafePointer(keyData.bytes)
    key.count,
    ivBytes, // UnsafePointer(ivData.bytes)
    dataBytes, // UnsafePointer(data.bytes)
    dataLength,
    cryptPointer, // UnsafeMutablePointer(cryptData!.mutableBytes)
    cryptLength,
    &numBytesEncrypted
    )

    View Slide

  18. CryptoSwift
    http://github.com/krzyzanowskim/CryptoSwift

    View Slide

  19. Why?

    View Slide

  20. Because I can

    View Slide

  21. I was curious

    View Slide

  22. To learn

    View Slide

  23. I’m an engineer
    to challenge myself

    View Slide

  24. CryptoSwift

    View Slide

  25. CryptoSwift
    Raise your hand if you already know it

    View Slide

  26. CryptoSwift
    • Swift framework

    View Slide

  27. CryptoSwift
    • Swift framework
    • iOS and OS X

    View Slide

  28. CryptoSwift
    • Swift framework
    • iOS and OS X
    • Pure Swift implementation

    View Slide

  29. CryptoSwift
    • Swift framework
    • iOS and OS X
    • Pure Swift implementation
    • Constantly improved

    View Slide

  30. A cryptographic hash function
    allows one to easily verify that
    some input data matches a
    stored hash value, but makes it
    hard to construct any data that
    would hash to the same value or
    find any two unique data pieces
    that hash to the same value.
    CryptoSwift - hash

    View Slide

  31. CryptoSwift - hash
    • MD5, SHA1, SHA2, CRC32
    import CryptoSwift
    "SwiftSummit".md5()
    "SwiftSummit".sha1()
    "SwiftSummit".sha512()
    "SwiftSummit".crc32()

    View Slide

  32. “In cryptography, a cipher (or cypher)
    is an algorithm for performing
    encryption or decryption—a series of
    well-defined steps that can be
    followed as a procedure.”
    CryptoSwift - ciphers

    View Slide

  33. CryptoSwift - ciphers
    Symmetric ciphers
    plaintext encryption ciphertext
    key
    ciphertext decryption plaintext

    View Slide

  34. CryptoSwift - ciphers
    • AES
    • The Advanced Encryption Standard, the
    symmetric block cipher ratified as a standard by
    National Institute of Standards and Technology
    of the United States (NIST)
    • Hardware acceleration (not for Swift)
    ‣ Advanced Encryption Standard Instruction Set
    (AES-NI)

    View Slide

  35. import CryptoSwift
    let key = "1234567890123456" // key
    let iv = "1234567890123456" // random
    if let aes = AES(key: key, iv: iv, blockMode: .CBC) {
    if let encrypted = aes.encrypt([1,2], padding: PKCS7())
    {
    let data = NSData.withBytes(encrypted)
    }
    }
    CryptoSwift - ciphers

    View Slide

  36. CryptoSwift - ciphers
    • ChaCha20
    ‣ The stream cipher by D. J. Bernstein, built on a
    pseudorandom function based on add-rotate-xor
    operations
    ‣ Adopted by Apple with HomeKit, by Google with
    Chrome (replace RC4)
    ‣ Lack of official support in OpenSSL. Patches are
    waiting to be merged.

    View Slide

  37. import CryptoSwift
    let key = "1234567890123456" // key
    let iv = "1234567890123456" // random
    if let chacha = ChaCha20(key: key, iv: iv) {
    if let encrypted = chacha.encrypt([1,2])
    {
    let data = NSData.withBytes(encrypted)
    }
    }
    CryptoSwift - ciphers

    View Slide

  38. enum Cipher {
    case ChaCha20(key: [UInt8], iv: [UInt8])
    case AES(key: [UInt8], iv: [UInt8], blockMode: CipherBlockMode)
    func encrypt(bytes: [UInt8]) -> [UInt8]?
    func decrypt(bytes: [UInt8]) -> [UInt8]?
    static func randomIV(blockSize: Int) -> [UInt8]
    }
    CryptoSwift - ciphers

    View Slide

  39. The block cipher operation is an
    algorithm that uses a block cipher
    to encrypt a large message
    CryptoSwift - block mode

    View Slide

  40. • ECB (Electronic Codebook)
    • CBC (Cipher Block Chaining)
    • CFB (Cipher Feedback)
    • CTR (Counter)
    CryptoSwift - block mode

    View Slide

  41. • Electronic CodeBook ECB - Don’t use!
    ‣ Sequence of encrypted blocks, every block with
    the same key.
    CryptoSwift - block mode

    View Slide

  42. CryptoSwift - block mode
    • Cipher-block Chaining CBC
    ‣ Sequence of encrypted blocks, every following
    block uses encrypted data as a key to the cipher.
    decryption is parallelizable

    View Slide

  43. Authenticators
    The message authentication code
    is a short piece of information used
    to authenticate a message and to
    provide integrity and authenticity
    assurances on the message

    View Slide

  44. CryptoSwift - authenticators
    • Poly1305 - a one-time authenticator
    ‣ takes a 32-byte one-time key and a message
    and produces the 16-byte tag.
    • HMAC - Keyed-Hashing for Message
    Authentication
    ‣ takes a key and message and produces a tag
    with one of the hash functions (MD5, SHA)

    View Slide

  45. CryptoSwift
    enum Authenticator {
    case Poly1305(key: [UInt8])
    case HMAC(key: [UInt8], variant: HMAC.Variant)
    func authenticate(message: [UInt8]) -> [UInt8]?
    }

    View Slide

  46. CryptoSwift
    extension NSData { … }
    extension String { … }
    “message”.md5()
    “message”.sha512()
    “plaintext”.encrypt(Cipher.AES(…))

    View Slide

  47. Performance

    View Slide

  48. Performance
    • CryptoSwift implementation is significantly slower
    than CommonCrypto
    • It’s better with the new version of Swift
    • NSMutableData is slow
    • memory allocation is slow if the “unsafe pointer” is
    not used.
    • Array enumeration is significantly visible.

    View Slide

  49. CryptoSwift
    Crypto You Can Do

    View Slide

  50. CryptoSwift
    Crypto You Can Do
    HOW?

    View Slide

  51. read http://cr.yp.to
    by J. Bernstein

    View Slide

  52. …understand nothing
    at first

    View Slide

  53. re-read

    View Slide

  54. write code, try

    View Slide

  55. do tests

    View Slide

  56. share and ask for
    feedback

    View Slide

  57. fix & improve

    View Slide

  58. contribute to
    CryptoSwift
    http://github.com/krzyzanowskim/CryptoSwift

    View Slide

  59. Thank you
    [email protected]
    http://blog.krzyzanowskim.com

    View Slide