Slide 1

Slide 1 text

CryptoRuby 101 by [email protected] from A Professional Mobile Device Management Company

Slide 2

Slide 2 text

CryptoRuby 101 very basic stuff in general cryptography (encypt/decript) featuring Ruby Standard Library Extension (OpenSSL) what to expect from today's

Slide 3

Slide 3 text

(en|de)crypt symmetric encrypt with secret key decrypt with secret key asymmetric ecrypt with public key decrypt with private key

Slide 4

Slide 4 text

• an open-source library, written in C • implements basic cryptographic functions and SSL and TLS protocols • founded in 1998, used by 2/3 of all webservers • https://www.openssl.org

Slide 5

Slide 5 text

require 'openssl' • Ruby Standard Library Extension:
 /ext/openssl/* • http://ruby-doc.org/stdlib-2.2.2/libdoc/openssl/rdoc/index.html

Slide 6

Slide 6 text

symmetric-key cryptography 
 a cipher (or cypher) is an algorithm for 
 encryption or decryption OpenSSL::Cipher Chiper Block Chaining mode encryption

Slide 7

Slide 7 text

CBC = Chiper Block Chaining CBC mode encryption

Slide 8

Slide 8 text

CBC = Chiper Block Chaining CBC mode encryption

Slide 9

Slide 9 text

source: http://en.wikipedia.org/wiki/Block_cipher_mode_of_operation CBC mode encryption

Slide 10

Slide 10 text

OpenSSL::Cipher Code Snippet

Slide 11

Slide 11 text

msg = 'hello secret world' cipher = OpenSSL::Cipher.new('AES-256-CBC').encrypt iv = cipher.random_iv key = cipher.random_key encrypted = cipher.update(msg) + cipher.final # safe to share publicly: encrypted, alg, iv decipher = OpenSSL::Cipher.new('AES-256-CBC').decrypt dechiper.iv, dechiper.key = iv, key decrypted = decipher.update(encrypted) + decipher.final puts msg == decrypted #=> true

Slide 12

Slide 12 text

ActiveSupport::MessageEncryptor 
 a simple way to encrypt values which get stored somewhere you don't trust

Slide 13

Slide 13 text

ActiveSupport::MessageEncryptor #encrypt_and_sign #decrypt_and_verify • implemented using OpenSSL::Cipher • https://github.com/rails/rails/blob/master/activesupport/ lib/active_support/message_encryptor.rb#L100 • default cipher algorythm is 'AES-256-CBC'

Slide 14

Slide 14 text

ActiveSupport::MessageEncryptor class EncryptedCookieJar def initialize(parent_jar, key_generator, options = {}) @parent_jar = parent_jar @options = options secret = key_generator.generate_key(@options[:encrypted_cookie_salt]) sign_secret = key_generator.generate_key(@options[:encrypted_signed_cookie_salt]) @encryptor = ActiveSupport::MessageEncryptor.new(secret, sign_secret, digest: digest, ...) end # etc... used as @encryptor in ActionDispatch::EncryptedCookieJar https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/ middleware/cookies.rb

Slide 15

Slide 15 text

ActiveSupport::MessageEncryptor Code Snippet

Slide 16

Slide 16 text

cookie = "user_id:1" key = Rails.application.secrets[:secret_key_base] chiper = ActiveSupport::MessageEncryptor.new(key) encrypted_cookie = chiper.encrypt_and_sign(cookie)
 # cookie: "#{base64_encrypted_data}--#{base_64_iv}" # read encrypted_cookie decrypted = chiper.decrypt_and_verify(encrypted_cookie) cookie == decrypted #=> true

Slide 17

Slide 17 text

cookie = "user_id:1" salt = SecureRandom.random_bytes(64)
 pass = 'password'
 key = ActiveSupport::KeyGenerator.new(pass).generate_key(salt) chiper = ActiveSupport::MessageEncryptor.new(key) encrypted_cookie = chiper.encrypt_and_sign(cookie)
 # cookie: "#{base64_encrypted_data}--#{base_64_iv}" # read encrypted_cookie decrypted = chiper.decrypt_and_verify(encrypted_cookie) cookie == decrypted #=> true

Slide 18

Slide 18 text

Re-cap • explore OpenSSL namespace
 http://ruby-doc.org/stdlib-2.2.2/libdoc/openssl/rdoc/index.html • start with simple OpenSSL::Cipher
 http://ruby-doc.org/stdlib-2.2.2/libdoc/openssl/rdoc/OpenSSL/Cipher.html • dive into Asymmetric Public Key Algorithms: OpenSSL::PKey
 http://ruby-doc.org/stdlib-2.2.2/libdoc/openssl/rdoc/OpenSSL/PKey.html 
 http://ruby-doc.org/stdlib-2.2.2/libdoc/openssl/rdoc/OpenSSL/PKey/RSA.html

Slide 19

Slide 19 text

Gems • ActiveSupport • SymmetricEncryption
 provides encryption of data for Ruby and Rails:
 https://github.com/reidmorrison/symmetric-encryption • Strongbox
 provides Public Key Encryption for ActiveRecord:
 https://github.com/spikex/strongbox • etc: https://www.ruby-toolbox.com/categories/encryption

Slide 20

Slide 20 text

cryptofails.com “Be skeptical of everything you read and hear about crypto”