5. JOHN CONGDON
• PHP Since 2003
• SDPHP Organizer
• Developer for
Networx Online
6. JOHN CONGDON
• PHP Since 2003
• SDPHP Organizer
• Developer for
Networx Online
• PhoneBurner.com
7. JOHN CONGDON
• PHP Since 2003
• SDPHP Organizer
• Developer for
Networx Online
• PhoneBurner.com
• MeetingBurner.com
8. JOHN CONGDON
• PHP Since 2003
• SDPHP Organizer
• Developer for
Networx Online
• PhoneBurner.com
• MeetingBurner.com
• FaxBurner.com
9. JOHN CONGDON
• PHP Since 2003
• SDPHP Organizer
• Developer for
Networx Online
• PhoneBurner.com
• MeetingBurner.com
• FaxBurner.com
• I am not a
cryptographer
16. CRYPTOGRAPHIC HASHING
Wikipedia Definition:A cryptographic hash function is a hash function; that is, an algorithm that takes an arbitrary block
of data and returns a fixed-size bitstring, the (cryptographic) hash value, such that any (accidental or intentional) change
to the data will (with very high probability) change the hash value.The data to be encoded are often called the "message,"
and the hash value is sometimes called the message digest or simply the digest.
17. CRYPTOGRAPHIC HASHING
Wikipedia Definition:A cryptographic hash function is a hash function; that is, an algorithm that takes an arbitrary block
of data and returns a fixed-size bitstring, the (cryptographic) hash value, such that any (accidental or intentional) change
to the data will (with very high probability) change the hash value.The data to be encoded are often called the "message,"
and the hash value is sometimes called the message digest or simply the digest.
HASH
18. CRYPTOGRAPHIC HASHING
Wikipedia Definition:A cryptographic hash function is a hash function; that is, an algorithm that takes an arbitrary block
of data and returns a fixed-size bitstring, the (cryptographic) hash value, such that any (accidental or intentional) change
to the data will (with very high probability) change the hash value.The data to be encoded are often called the "message,"
and the hash value is sometimes called the message digest or simply the digest.
HASHMessage
19. CRYPTOGRAPHIC HASHING
Wikipedia Definition:A cryptographic hash function is a hash function; that is, an algorithm that takes an arbitrary block
of data and returns a fixed-size bitstring, the (cryptographic) hash value, such that any (accidental or intentional) change
to the data will (with very high probability) change the hash value.The data to be encoded are often called the "message,"
and the hash value is sometimes called the message digest or simply the digest.
HASH DigestMessage
20. CRYPTOGRAPHIC HASHING
Wikipedia Definition:A cryptographic hash function is a hash function; that is, an algorithm that takes an arbitrary block
of data and returns a fixed-size bitstring, the (cryptographic) hash value, such that any (accidental or intentional) change
to the data will (with very high probability) change the hash value.The data to be encoded are often called the "message,"
and the hash value is sometimes called the message digest or simply the digest.
HASH
DigestMessage
21. CRYPTOGRAPHIC HASHING
Wikipedia Definition:A cryptographic hash function is a hash function; that is, an algorithm that takes an arbitrary block
of data and returns a fixed-size bitstring, the (cryptographic) hash value, such that any (accidental or intentional) change
to the data will (with very high probability) change the hash value.The data to be encoded are often called the "message,"
and the hash value is sometimes called the message digest or simply the digest.
HASH
DigestMessage
1abcb33beeb811dca15f0ac3e47b88d9unicorn
22. CRYPTOGRAPHIC HASHING
Wikipedia Definition:A cryptographic hash function is a hash function; that is, an algorithm that takes an arbitrary block
of data and returns a fixed-size bitstring, the (cryptographic) hash value, such that any (accidental or intentional) change
to the data will (with very high probability) change the hash value.The data to be encoded are often called the "message,"
and the hash value is sometimes called the message digest or simply the digest.
HASH
DigestMessage
1abcb33beeb811dca15f0ac3e47b88d9unicorn
23. MD5 EXAMPLE
$username = $_POST['username'];
$password = $_POST['password'];
$user = getUserByUsername($username);
$authenticated = false;
if ($user->password == md5($password)) {
$authenticated = true;
}
*example only: not meant to be used
24. MD5 EXAMPLE
$username = $_POST['username'];
$password = $_POST['password'];
$user = getUserByUsername($username);
$authenticated = false;
if ($user->password == md5($password)) {
$authenticated = true;
}
*example only: not meant to be used
32. ADDING SALT
In cryptography, a salt is random data that is used as an additional input to a one-way function that hashes a password or
passphrase.[1]The primary function of salts is to defend against dictionary attacks versus a list of password hashes and
against pre-computed rainbow table attacks.
33. ADDING SALT
In cryptography, a salt is random data that is used as an additional input to a one-way function that hashes a password or
passphrase.[1]The primary function of salts is to defend against dictionary attacks versus a list of password hashes and
against pre-computed rainbow table attacks.
$hash = md5('RAND_SALT' . $password);
34. ADDING SALT
In cryptography, a salt is random data that is used as an additional input to a one-way function that hashes a password or
passphrase.[1]The primary function of salts is to defend against dictionary attacks versus a list of password hashes and
against pre-computed rainbow table attacks.
$hash = md5('RAND_SALT' . $password);
RAND_SALT must come from a cryptographically secure
source.
Do not use (rand, mt_rand, uniqid)
Do use (/dev/urandom, mcrypt, openssl)
35. $username = $_POST['username'];
$password = $_POST['password'];
$user = getUserByUsername($username);
$authenticated = false;
if ($user->password == md5($user->salt . $password))
{
$authenticated = true;
}
*example only: not meant to be used
MD5+SALT EXAMPLE
40. USE TODAY'S STANDARDS
Currently: BCrypt
• Slower by design
• Configurable to help withstand the test of time
• Should be configured to take 0.25 to 0.50 seconds
• Start with a cost of 10, use higher if possible
https://github.com/johncongdon/bcrypt-cost-finder
41. PHP 5.5 Password Hashing API
http://www.php.net/manual/en/ref.password.php
48. I Lied: Available in PHP >= 5.3.7
https://github.com/ircmaxell/password_compat
A forward compatible password API implementation that
will work until you are ready to upgrade to 5.5. This will
work for all versions of PHP that has the $2y fix.
Upgrading to 5.5 will not break your current code if you
use this library.
49. Want More? Get Statistics Here
http://blog.ircmaxell.com/2013/01/password-storage-talk-at-php-benelux-13.html
54. AVOID ENCRYPTION AT ALL COSTS!
Clarification:
Avoid storing any data that you need to encrypt.
55. AVOID ENCRYPTION AT ALL COSTS!
Clarification:
Avoid storing any data that you need to encrypt.
Before deciding to collect and store this information,
ask yourself why you need it.
56. AVOID ENCRYPTION AT ALL COSTS!
Clarification:
Avoid storing any data that you need to encrypt.
Before deciding to collect and store this information,
ask yourself why you need it.
Is the risk of potentially leaking this information worth the reward?
57. AVOID ENCRYPTION AT ALL COSTS!
Clarification:
Avoid storing any data that you need to encrypt.
Before deciding to collect and store this information,
ask yourself why you need it.
Is the risk of potentially leaking this information worth the reward?
Are there any alternative solutions available to you?
58. AVOID ENCRYPTION AT ALL COSTS!
Clarification:
Avoid storing any data that you need to encrypt.
Before deciding to collect and store this information,
ask yourself why you need it.
Is the risk of potentially leaking this information worth the reward?
Are there any alternative solutions available to you?
Example: Credit card companies usually offer a token solution
61. SYMMETRIC VS ASYMMETRIC
Symmetric
Only one shared key
Same key encrypts and decrypts
Easiest to understand
Asymmetric
Two keys (Public and Private)
Encryption/Decryption
Public key encrypts
Private key decrypts
Signing/Verifying
Private key signs
Public key verifies
65. KEYS, CIPHERS, MODES, AND IV OH MY!
Keys should be easy enough (Keep it secret)
Ciphers
66. KEYS, CIPHERS, MODES, AND IV OH MY!
Keys should be easy enough (Keep it secret)
Ciphers
Deterministic algorithm (Ex: 3DES, Blowfish, TwoFish)
67. KEYS, CIPHERS, MODES, AND IV OH MY!
Keys should be easy enough (Keep it secret)
Ciphers
Deterministic algorithm (Ex: 3DES, Blowfish, TwoFish)
Modes
68. KEYS, CIPHERS, MODES, AND IV OH MY!
Keys should be easy enough (Keep it secret)
Ciphers
Deterministic algorithm (Ex: 3DES, Blowfish, TwoFish)
Modes
Determines how the key stream is used (never cross them)
69. KEYS, CIPHERS, MODES, AND IV OH MY!
Keys should be easy enough (Keep it secret)
Ciphers
Deterministic algorithm (Ex: 3DES, Blowfish, TwoFish)
Modes
Determines how the key stream is used (never cross them)
Avoid ECB (Electronic Code Book)
70. KEYS, CIPHERS, MODES, AND IV OH MY!
Keys should be easy enough (Keep it secret)
Ciphers
Deterministic algorithm (Ex: 3DES, Blowfish, TwoFish)
Modes
Determines how the key stream is used (never cross them)
Avoid ECB (Electronic Code Book)
Use CBC or CFB, Cipher Block Chaining / Cipher FeedBack)
71. KEYS, CIPHERS, MODES, AND IV OH MY!
Keys should be easy enough (Keep it secret)
Ciphers
Deterministic algorithm (Ex: 3DES, Blowfish, TwoFish)
Modes
Determines how the key stream is used (never cross them)
Avoid ECB (Electronic Code Book)
Use CBC or CFB, Cipher Block Chaining / Cipher FeedBack)
Initialization Vectors
72. KEYS, CIPHERS, MODES, AND IV OH MY!
Keys should be easy enough (Keep it secret)
Ciphers
Deterministic algorithm (Ex: 3DES, Blowfish, TwoFish)
Modes
Determines how the key stream is used (never cross them)
Avoid ECB (Electronic Code Book)
Use CBC or CFB, Cipher Block Chaining / Cipher FeedBack)
Initialization Vectors
Similar to SALT in hashing (It's not a secret)
73. KEYS, CIPHERS, MODES, AND IV OH MY!
Keys should be easy enough (Keep it secret)
Ciphers
Deterministic algorithm (Ex: 3DES, Blowfish, TwoFish)
Modes
Determines how the key stream is used (never cross them)
Avoid ECB (Electronic Code Book)
Use CBC or CFB, Cipher Block Chaining / Cipher FeedBack)
Initialization Vectors
Similar to SALT in hashing (It's not a secret)
Must be random per encrypted text
74. EXAMPLE: ENCRYPT USING CRYPT
$crypt_key = 'MySecretKey';
$message = "Do not tell my boss, but I did xyz";
$iv_size = mcrypt_get_iv_size(
MCRYPT_BLOWFISH,
MCRYPT_MODE_CBC
);
$iv = mcrypt_create_iv($iv_size, MCRYPT_DEV_URANDOM);
$cipher = mcrypt_encrypt(
MCRYPT_BLOWFISH,
$crypt_key,
$message,
MCRYPT_MODE_CBC,
$iv
);
76. HMAC: HASH-BASED MESSAGE AUTHENTICATION CODE
Using a separate key, this will give us a signature of the
encryption. We can use this to ensure that the data has
not been tampered with.
77. HMAC: HASH-BASED MESSAGE AUTHENTICATION CODE
Using a separate key, this will give us a signature of the
encryption. We can use this to ensure that the data has
not been tampered with.
When encrypting:
78. HMAC: HASH-BASED MESSAGE AUTHENTICATION CODE
Using a separate key, this will give us a signature of the
encryption. We can use this to ensure that the data has
not been tampered with.
When encrypting:
Always encrypt first, and then get the signature of
the Cipher Text.
79. HMAC: HASH-BASED MESSAGE AUTHENTICATION CODE
Using a separate key, this will give us a signature of the
encryption. We can use this to ensure that the data has
not been tampered with.
When encrypting:
Always encrypt first, and then get the signature of
the Cipher Text.
Store the signature with your IV and Cipher Text.
80. HMAC: HASH-BASED MESSAGE AUTHENTICATION CODE
Using a separate key, this will give us a signature of the
encryption. We can use this to ensure that the data has
not been tampered with.
When encrypting:
Always encrypt first, and then get the signature of
the Cipher Text.
Store the signature with your IV and Cipher Text.
When Decrypting:
81. HMAC: HASH-BASED MESSAGE AUTHENTICATION CODE
Using a separate key, this will give us a signature of the
encryption. We can use this to ensure that the data has
not been tampered with.
When encrypting:
Always encrypt first, and then get the signature of
the Cipher Text.
Store the signature with your IV and Cipher Text.
When Decrypting:
Always verify the signature first, and then decrypt if
successful.
82. EXAMPLE: USING HMAC
$crypt_key = 'MySecretKey';
$hmac_key = 'HashingKey';
$hmac = hash_hmac('sha512', $cipher, $hmac_key);
//Store it with your encrypted data
$encoded_data = base64_encode($iv . $cipher . $hmac);
83. $decoded_data = base64_decode($encoded_data);
$iv = substr($decoded_data, 0, $iv_size);
$hmac = substr($decoded_data, -128);
$cipher = substr($decoded_data, $iv_size, -128);
if ($hmac != hash_hmac('sha512', $cipher, $hmac_key))
{
throw new Exception('HMAC does not match');
}
$message = mcrypt_decrypt(
MCRYPT_BLOWFISH,
$crypt_key,
$cipher,
MCRYPT_MODE_CBC,
$iv
);
EXAMPLE: DECRYPTING USING HMAC
95. ENCRYPTION !== PROTECTION
Data obtained through SQL Injection attacks
should be relatively secure.
For us to encrypt/decrypt, we must have
access to the key. Therefore, any breach of
the system will disclose the key to the
attacker, leaving ALL encryption useless.
96. ENCRYPTION !== PROTECTION
Data obtained through SQL Injection attacks
should be relatively secure.
For us to encrypt/decrypt, we must have
access to the key. Therefore, any breach of
the system will disclose the key to the
attacker, leaving ALL encryption useless.
Apache environment variable, memory,
config files, password entered during
system start, etc... do not keep the key
private.
99. OTHER THINGS TO CONSIDER
• Encrypt / decrypt on a separate server
100. OTHER THINGS TO CONSIDER
• Encrypt / decrypt on a separate server
• More overhead and complexity
101. OTHER THINGS TO CONSIDER
• Encrypt / decrypt on a separate server
• More overhead and complexity
• Any server breach can still decrypt
data
102. OTHER THINGS TO CONSIDER
• Encrypt / decrypt on a separate server
• More overhead and complexity
• Any server breach can still decrypt
data
• With enough thought and monitoring,
you can kill the decryption server to
limit the damage done
103. OTHER THINGS TO CONSIDER
• Encrypt / decrypt on a separate server
• More overhead and complexity
• Any server breach can still decrypt
data
• With enough thought and monitoring,
you can kill the decryption server to
limit the damage done
• Think about restricting requests per
second
104. OTHER THINGS TO CONSIDER
• Encrypt / decrypt on a separate server
• More overhead and complexity
• Any server breach can still decrypt
data
• With enough thought and monitoring,
you can kill the decryption server to
limit the damage done
• Think about restricting requests per
second
Paranoid about password safety? Consider encrypting the
hash. Renders SQL Injection and rainbow tables/brute force
mostly useless without the key.
106. OTHER THINGS TO CONSIDER
Do you need access to the user's information without
them on the system?
107. OTHER THINGS TO CONSIDER
Do you need access to the user's information without
them on the system?
If your user must be present, then consider making
them partially responsible for the security. Have them
use a second password or passphrase that you can add
to your key to use in the encryption.
108. FINAL WORDS...
I've learned a ton while preparing this presentation.
Thanks especially to Anthony Ferrara (@ircmaxell)
http://blog.ircmaxell.com