php[world] 2016 - Tales From the Crypto: A Cryptography Primer

93
@adam_englander Tales From the Crypto A Cryptography Primer

Transcript of php[world] 2016 - Tales From the Crypto: A Cryptography Primer

Page 1: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Tales From the CryptoA Cryptography Primer

Page 2: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Who Am I?

Page 3: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

We are going to talk about the common methods and terms used for

cryptography in application development

Page 4: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

What is Cryptography?

Page 5: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Cryptography…is the practice and study of techniques for secure communication in the presence of third parties called adversaries.

Wikipedia

Page 6: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Cryptography obscures data in such a way that it is costly to

duplicate or difficult to reverse.

Page 7: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Good cryptography makes it extremely difficult to identify patterns

in the obscured data.

Page 8: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Type of Cryptography We Will Cover

• Encryption/Decryption

• Digital Signatures

• Hashing

Page 9: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Encryption

Page 10: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Encrypting data ensures only certain parties can read it.

Page 11: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Encrypted data can be decrypted.

Page 12: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Encryption uses mathematical algorithms called ciphers.

Page 13: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

The ciphers use secrets called cipher keys.

Page 14: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Cipher keys can be symmetric (shared secrets) or asymmetric

(public key cryptography).

Page 15: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Symmetric Key Encryption

Page 16: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Symmetric key encryption applies ciphers against data producing a

cipher text.

Page 17: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

There are two types of symmetric key ciphers:

stream and block.

Page 18: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Stream ciphers use a cipher key with a cryptographically secure

pseudorandom cipher digit stream called a keystream to

product the cipher text.

Page 19: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Cryptographically secure pseudorandom values are

issued to be random enough not to generate distinguishable

patterns.

Page 20: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Block ciphers execute against a fixed length group of bits.

Page 21: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Cipher Block Execution Modes

Page 22: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Electronic Cookbook (ECB)

Page 23: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

DO NOT USE ECB!

Page 24: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Electronic cookbook encrypts each block separately.

Page 25: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Page 26: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

It is not secure as patterns are created from the same data resulting

in the same cipher text.

Page 27: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

If you manage to decrypt one block of the cipher text, you can now

decrypt all of the others.

Page 28: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Plain ECB CBC

Mode Comparison

Tux the Penguin, the Linux mascot. Created in 1996 by Larry Ewing with The GIMP.

Page 29: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

DO NOT USE ECB!

Page 30: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Block Chain and Feedback Modes: CBC, CFB, and OFB

Page 31: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

All use an initialization vector (IV) to provide the chain/feedback on the

first block.

Page 32: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

All base the cipher value of the current block on some portion of

the previous block.

Page 33: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Page 34: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Page 35: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Page 36: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Unless you have a specific use case, use CBC.

Page 37: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Use PKCS7 padding as it is secure and has the widest compatibility.

Page 38: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Asymmetric Key Encryption: Public Key Cryptography

Page 39: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Public Key Cryptography use key pairs, public/private.

Page 40: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Public keys can be disseminated to anyone.

Page 41: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Public keys can encrypt data but cannot decrypt the data it

encrypts.

Page 42: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Private keys are secret.

Page 43: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Public keys can encrypt and decrypt data.

Page 44: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Public Key Cryptography Implementations

Page 45: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

RSA is the only form available in PHP.

Page 46: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

RSA encryption is computationally expensive

using very large prime integers and exponential computation.

Page 47: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

RSA encryption is limited to the amount of data it can encrypt

based on the size of the private key.

Page 48: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

RSA encryption is often used to exchange secret keys for symmetric key encryption.

Page 49: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

The Diffie/Hellmen key exchange in SSL/TLS is a great example.

Page 50: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Diffie-Hellman Key Exchange

The “Common Paint” is a random number generated on the client and

encrypted with the public key from the server. It is transmitted to the server and decrypted using the private key. They

negotiate a shared secret and then utilize symmetric key encryption with that secret to communicate further.

Page 51: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Always use PKCS1 OAEP Padding. PKCS#1 v1.5 is the PHP default but

must not be used.

Page 52: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Hashing

Page 53: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Hashes can not be reversed. They can only be recreated and verified.

Page 54: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Hashing data is used to verify the integrity of data or store the

data obscured.

Page 55: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Electronic Signatures

Page 56: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Hashes are used in conjunction with secrets to create electronic

signatures.

Page 57: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Symmetric Key Signatures

Page 58: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Symmetric key signatures are known as a Hash-based Message

Authentication Code or HMAC

Page 59: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

HMACs use a hashing algorithm in combination with a shared secret to

generate a verifiable hash.

Page 60: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

The minimum hashing algorithm for an HMAC is SHA-1. SHA-256 or

better is preferred.

Page 61: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Key size determines the cryptographic strength of the

signature.

Page 62: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Asymmetric Key Signatures

Page 63: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

The private key is used to sign the data.

Page 64: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

The public key is used to verify the signature.

Page 65: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

RSA is the only asymmetric key signature available in PHP.

Page 66: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

The amount of data RSA can sign is based on the size of the private key.

Page 67: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

RSA uses hashing algorithms for data larger than the key allows.

Page 68: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

SHA1 is the “suggested” minimum hashing algorithm for RSA.

Page 69: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Password Hashing

Page 70: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Hashes are used for passwords or any value that will be

presented for verification.

Page 71: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Proper password hashing is done via a Key Derivation Function (KDF).

Page 72: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Never use a standard hashing algorithm for passwords EVER!

Page 73: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Key derivation functions use a “salt” to create differentiation for the same

password.

Page 74: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Key derivation functions apply the salted hash for a defined iteration

count.

Page 75: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Hashes must be cryptographically pseudorandom and large.

Page 76: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Iteration counts must be as large as can be tolerated.

Page 77: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

HASH UNTIL IT HURTS!

Page 78: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

PHP provides PBKDF2 and BCRYPT for password hashing.

Page 79: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

It also provides convenience functions: http://php.net/manual/en/

ref.password.php

Page 80: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Suggestions

Page 81: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Encrypt all data that is secret or private.

Page 82: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Sign all significant data in transit.

Page 83: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Use the strongest encryption you can support.

Page 84: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Do NOT use rand() or mt_rand() to generate keys or IVs. Use

Use random_bytes().

paragonie/random_compat is a PHP 5.x polyfill.

Page 85: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Use the OpenSSL extension for everything but password hashing

Page 86: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

If you are writing a library, you can use phpseclib/phpseclib as

an abstraction layer to OpenSSL, MCrypt, or no crypto

extensions installed.

Page 87: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Use OPENSSL_PKCS1_OAEP_PADDING

for RSA encryption and OPENSSL_ALGO_SHA256/384/512 for

signatures

Page 88: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Use aes-256-cbc for symmetric key encryption and

aes-256-cbc-hmac-sha256 for symmetric key signatures

Page 89: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Use built in password hashing functions to do it right.

Page 90: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Use a large number of iterations. Shoot for at least 500ms of hashing.

Page 91: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Further Reading

• http://php.net/manual/en/book.openssl.php

• http://php.net/manual/en/function.password-hash.php

• http://php.net/manual/en/book.csprng.php

• https://github.com/phpseclib/phpseclib

• Wikipedia

Page 92: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

Please Rate This Talk

https://joind.in/talk/6ef69

Page 93: php[world] 2016 - Tales From the Crypto: A Cryptography Primer

@adam_englander

20162016