Slide 1

Slide 1 text

Intro to PHP Encryption php[world] 2018

Slide 2

Slide 2 text

Why Crypto? ● Your customers’ privacy ● Your business’ privacy ● Regulatory compliance ● The right thing to do ● It’s easy and fun

Slide 3

Slide 3 text

PHP 7.2 Enter Libsodium ● Fork of NaCl ● Many implementations ● Originally Pecl ● Core PHP extension ● MODERN CRYPTO

Slide 4

Slide 4 text

Symmetric Encryption

Slide 5

Slide 5 text

Symmetric Encryption Image by SSL2BUY

Slide 6

Slide 6 text

Symmetric Encryption // Generating your encryption key $key = random_bytes(SODIUM_CRYPTO_SECRETBOX_KEYBYTES); // Using your key to encrypt information $nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); $ciphertext = sodium_crypto_secretbox($plaintext, $nonce, $key); // Encode $encoded = sodium_bin2hex($ciphertext);

Slide 7

Slide 7 text

Symmetric Encryption // Load the same secret key $key = ''; // Decode and split $decoded = sodium_hex2bin($encoded); $nonce = substr($encoded, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); $ciphertext = substr($encoded, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); // Using your key to decrypt information $plaintext = sodium_crypto_secretbox_open($ciphertext, $nonce, $key); // Check for errors if ($plaintext === false) throw new Exception('Bad ciphertext');

Slide 8

Slide 8 text

Asymmetric Encryption

Slide 9

Slide 9 text

Asymmetric Encryption Image by SSL2BUY

Slide 10

Slide 10 text

Asymmetric Encryption // On Alice's computer: $alice_kp = sodium_crypto_box_keypair(); // Split the key into public/private components $alice_secretkey = sodium_crypto_box_secretkey($alice_kp); $alice_publickey = sodium_crypto_box_publickey($alice_kp); // On Bob's computer: $bob_kp = sodium_crypto_box_keypair(); $bob_secretkey = sodium_crypto_box_secretkey($bob_kp); $bob_publickey = sodium_crypto_box_publickey($bob_kp);

Slide 11

Slide 11 text

Asymmetric Encryption // From Alice to Bob $alice_to_bob_kp = sodium_crypto_box_keypair_from_secretkey_and_publickey( $alice_secretkey, $bob_publickey ); $message_nonce = random_bytes(SODIUM_CRYPTO_BOX_NONCEBYTES); $ciphertext = sodium_crypto_box( $message, $nonce, $alice_to_bob_kp );

Slide 12

Slide 12 text

Asymmetric Encryption // Encoding - From Alice to Bob $encoded = sodium_bin2hex($nonce . $ciphertext); // Decoding - From Bob to Alice $decoded = sodium_hex2bin($encoded); $nonce = substr($decoded, 0, SODIUM_CRYPTO_BOX_NONCEBYTES); $ciphertext = substr($decoded, SODIUM_CRYPTO_BOX_NONCEBYTES);

Slide 13

Slide 13 text

Asymmetric Encryption // On Bob's end $bob_to_alice_kp = sodium_crypto_box_keypair_from_secretkey_and_publickey( $bob_secretkey, $alice_publickey ); $plaintext = sodium_crypto_box_open( $ciphertext, $nonce, $bob_to_alice_kp ); if ($plaintext === false) throw new Exception('Bad message or MAC');

Slide 14

Slide 14 text

Cryptographic Signatures

Slide 15

Slide 15 text

Cryptographic Signatures Image from Crypto StackExchange

Slide 16

Slide 16 text

Cryptographic Signatures $message = 'php[world] is the best community event of the year!'; // Generate a keypair $sign_kp = sodium_crypto_sign_keypair(); $sign_secretkey = sodium_crypto_sign_secretkey($sign_kp); $sign_publickey = sodium_crypto_sign_publickey($sign_kp); // Sign the message $signed_msg = sodium_crypto_sign( $message, $alice_sign_secretkey );

Slide 17

Slide 17 text

Cryptographic Signatures - Verification $original_msg = sodium_crypto_sign_open( $signed_msg, $sign_publickey ); // Verify the message if ($original_msg === false) { throw new Exception('Invalid signature'); } else { echo $original_msg; // Displays the original message. }

Slide 18

Slide 18 text

Password Hashing

Slide 19

Slide 19 text

Password Hashing ● MD5 ● SHA ● Blowfish ● Blake2

Slide 20

Slide 20 text

Password Hashing

Slide 21

Slide 21 text

Key Derivation

Slide 22

Slide 22 text

Key Derivation // Create a high-entropy master key $master_key = sodium_crypto_kdf_keygen(); // Derive any number of sub-keys $s1 = sodium_crypto_kdf_derive_from_key(32, 1, 'one', $master_key); $s2 = sodium_crypto_kdf_derive_from_key(32, 2, 'two', $master_key); $sN = sodium_crypto_kdf_derive_from_key(32, 999, 'N', $master_key);

Slide 23

Slide 23 text

Key Derivation ● The Libsodium API can derive up to 2^64 subkeys ● 18,446,744,000,000,000,000 possible subkeys ● Subkeys can have an arbitrary length ○ Between 128 (16 bytes) and 512 bits (64 bytes).

Slide 24

Slide 24 text

Secure Remote Passwords

Slide 25

Slide 25 text

Secure Remote Passwords

Slide 26

Slide 26 text

Secure Remote Passwords

Slide 27

Slide 27 text

Cryptography at Rest

Slide 28

Slide 28 text

Cryptography in Transit

Slide 29

Slide 29 text

Questions?

Slide 30

Slide 30 text

Thank You! https://joind.in/talk/95173