SlideShare a Scribd company logo
Crypto failures every
developer should avoid
FILIP ŠEBESTA - OWASP CZECH CHAPTER MEETING - PRAGUE
DECEMBER 7 2015
What the conventional wisdom tells us
about cryptography
Cryptography is dangerous because you get no feedback when you mess it up
Never implement your own crypto – what does that really mean?
Most of the code using cryptography is not written/reviewed by
cryptographers or qualified architects.
Crypto built into products is oftentimes broken.
Broken crypto might be more dangerous than no crypto as it gives you a false
sense of security.
Veracode’s State of Software Security (June 2015)
The crux of the problem
Many programmers think that they can just link to a crypto library and they're
done, but cryptography is hard to implement robustly if you don't understand
the finer aspects of it.
You can easily develop an insecure crypto scheme using only “secure” algo.
Longer keys don't always mean more security etc.
Penetration testers rarely scrutinize crypto as it is a complex and demanding
task.
Crypto libraries are too complex – since they were designed by and for
cryptographers.
Area of confusion
What level of knowledge is requred to deal with crypto as a programmer?
Do developers need to be cryptographers in order to use crypto correctly?
Do we even have a choice?
Rather then arguing who’s fault it is shouldn’t we work towards fixing this
issue?
The way out of it
1.train developers to use it securely
2.Use high level crypto libraries
What is this presentation about?
The goal of this presentation is to show the
disconnect between what is expected from
developers leveraging cryptography and the
reality.
It will show some of the most typical crypto
failures so that developers can learn from
them.
It will also emphasize the importance of
abstraction of crypto primitives to not allow
developers to screw things so easily.
What kind of crypto pitfalls will we cover?
In his essay „Security Pitfalls in Cryptography” (1998) Bruce Schneider divided cryptography
attacks into following categories:
- Attacks Against Cryptographic Designs
- Attacks Against Implementations
- Attacks Against Passwords
- Attacks Against Hardware
- Attacks Against Trust Models
- Attacks on the Users
- Attacks Against Failure Recovery
- Attacks Against the Cryptography
Failure #1 – Improper password storage
LinkedIn leaked out approximately 6.5
million passwords in 2012.
What went wrong?
The social networking website LinkedIn was hacked on 5 June 2012, and passwords for nearly
6.5 million user accounts were stolen by Russian cybercriminals
 LinkedIn failed to use a salt when hashing the passwords
LinkedIn also stored the passwords using just SHA-1 algorithm
Due to these flaws attackers were able to unscramble the password hashes using standard
rainbow tables
Ashley Madison Leak
What happened?
User passwords were cryptographically protected using bcrypt, an algorithm so slow and
computationally demanding it would literally take centuries to crack all of them.
However, apart of storing user passwords with bcrypt, security researches identified two
instances where passwords were stored using MD5 for “automatic login”
$loginkey = md5(strtolower($username).'::'.strtolower($password));
md5(lc($username).”::”.lc($pass).”:”.lc($email).”:73@^bhhs&#@&^@8@*$”).
Crypto failures every developer should avoid
The right way to do it
Never store passwords in plaintext (obviously)
Never store hashed passwords without using a unique salt (ideally a random one sufficiently long)
Use a cryptographically secure & slow hash functions (PBKDF2, bcrypt, scrypt). Apply a sufficient number of iterations.
Slow hashing is desirable as it makes processing of a single password more expensive for the attacker
PBKDF2: good enough but can be optimized with GPU
bcrypt: less GPU friendly then PBKDF2 but vulnerable to brute-forcing using Field-Programmable Gate Arrays (FPGAs) with
relatively small SRAM
scrypt: requires not only exponential time but also exponential memory
however doesn’t have a cryptanalytic pedigree required to gain most cryptographers' trust
Do not allow users to use weak passwords. Even with the strongest crypto, weak passwords are easy to crack.
As soon as you receive a password, remove it from memory.
High level approach example
string password_hash ( string $password , integer $algo [, array $options ] )
password_hash() creates a new password hash using a strong one-way hashing algorithm.
Uses bcrypt or PASSWORD_DEFAULT (a constatnt which may change in time)
Options Cost and Salt are secure by default
http://php.net/manual/en/function.password-hash.php
Failure #2 Improper use of hash
functions
It’s 2015 and developers still use MD5
SHA- 1 has been considered insecure for long time, yet it still used widely.
Comparing hashes also causes problems – timing attacks
Using certain hash functions for what they are not might also cause issues e.g. length extension
attacks
Comparing Hashes
if ($hash1 === $hash2) {
//mac verification is OK echo "hashes are equal" }
else { //something bad happened }
For a given message, attempt to send it with a hash of all one
single character. Do this once for each ASCII char e.g. 'aaaa...',
'bbbb...' etc.
Measure the time each request takes to complete. Since string
equality takes a tiny bit longer to complete when the first char
matches, the message that takes the longest to return will
have the correct first character.
Length extension attacks
key = “secret attacker doesn’t know”
value =“send $100”
Signature = sha256(key + “:” + value)
Length extension attack allows us to append data and be able to compute a valid signature without
actually knowing the key.
value =“send $1000”
Signature = “valid signature”
https://blog.skullsecurity.org/2012/everything-you-need-to-know-about-hash-length-extension-
attacks
The right way to do it
Don’t ever use insecure algo such as MD5 or SHA-1. Use SHA-2 instead (SHA-256, Sha-384, SHA -512)
 For signed MAC use HMAC-SHA2
If you compare hashes, use only cryptographically secure functions e.g. :
.NET : RSACryptoServiceProvider.VerifyHash https://msdn.microsoft.com/en-
us/library/system.security.cryptography.rsacryptoserviceprovider.verifyhash(v=vs.110).aspx
Failure #3 Using encryption without
authentication
Encryption doesn’t automatically protect data against modification
Chosen cipher text attacks are a real threat
Bit flipping attacks
Padding oracle
Crypto failures every developer should avoid
Bit flipping
Padding oracle
Frequently present in web applications
Occurs when a web service reveals whether there was a padding or integrity error when
decryption (can be also base on timing)
If you can feed in ciphertexts and somehow find out whether or not they decrypt to something
with valid padding or not, then you can decrypt ANY given ciphertext.
Typical scenario : Using AES in CBC mode where have a ciphertext encrypted by a web service.
POODLE
How to do it right?
Never underestimate the importance of message authenticity even in situation without an
obvious attack vector.
There is rarely a reason for not doing authentication checks when decrypting data
Prefer authenticated modes of operation e.g. GCM, OCB (patented)
If forced using AES with CBC use HMAC to ensure message authenticity
Failure #4 Reusing nonces and IVs
Nonces should be used only once – that’s what the word stands for
The purpose of their use is to avoid replay attacks
IVs should usually be non-predictable
Length of IVs is also important (WEP)
Attacks on WEP
WEP uses 24b IV
For RC4 security is important that the
same key is never used twice
For every 24b IV there is a 50% chance
that the same IV will repeat after
approximately 5000 packets
The right way to do it
Never reuse nonces
Do not use static IVs
Always use random, unpredictable IVs with sufficient length
Pitfall #5 Dealing randomness
A good source of true randomness is an integral part of most cryptosystems. Common mistakes
include:
Using no or limited randomness when required
Using sources of “randomness” which are actually not random at all
Leaking internal state of PRNG
Random Number Bug in Debian
MD_Update(&m,buf,j);
[ .. ]
MD_Update(&m,buf,j); /* purify complains */
Removing this code had the side effect of crippling the seeding process for the OpenSSL PRNG.
Instead of mixing in random data for the initial seed, the only "random" value that was used
was the current process ID.
Crypto failures every developer should avoid
Modulo bias
int x;
int modulus = 4;
x= rand() % modulus; // random string 0-3
Assuming that rand() returns values (0,1,2,3,4,5)
Probability of 0: 33.33%
Probability of 1: 33.33%
Probability of 2: 16.66%
Probability of 3: 16.66%
Modulo bias #2
You will get modulo bias if max(rand()) + 1 is not evenly divisible by your modulus.
int excess = (RAND_MAX + 1) % modulus;
int limit = RAND_MAX - excess;
while (x = rand() > limit) {};
return x % modulus;
The right way to do it
.NET : https://msdn.microsoft.com/en-
us/library/system.security.cryptography.randomnumbergenerator
C++ (Windows): CryptGenRandom function in MS-CAPI https://msdn.microsoft.com/en-
us/library/windows/desktop/aa379942(v=vs.85).aspx
Linux: read from /dev/urandom
recommended reading: http://www.2uo.de/myths-about-urandom/
Failure #6 SSL/TLS Certificates
Using TLS in non-browser software is a challenging task
Using standard SSL/TLS libraries is not enough
Certificate validation is sometimes completely disabled
Certificate revocation check is oftentimes missing
Full chain check is sometimes not complete
Subject is sometimes not verified at all!
Crypto failures every developer should avoid
The right way to do it
Implementing proper certificate validation is not a trivial task. It requires a deep understanding
of the protocol.
Don’t assume that using SSL/TLS libraries ensures proper certificate validation.
Don’t disable SSL for testing purposes, it may backfire.
Implement unit testing (especially negative one) to verify that certificate validation is
implemented properly.
To mitigate the risks of rogue certificates consider certificate pinning.
www.badssl.com
Crypto failures every developer should avoid
Failure #7 Insecure key provisioning
A very common mistake is to spend lots of time designing an impenetrable crypto scheme and
then not protecting keys properly
Keys are often generated in an insecure environment, stored on developers machines,
hardcoded in the source code etc.
People also don’t think through the scenarios when keys get leaked
Crypto failures every developer should avoid
The right way to do it
Avoid using passwords as keys as they don’t have enough entropy and use truly random keys
instead.
If you have to use passwords as keys apply key derivation functions e.g. PBKDF2
Don’t reuse the same key on many devices.
Don’t reuse the same key for multiple purposes (encryption, authentication)
Generate keys in a secure environment
Store keys securely
Account for periodic key rotation and disaster recovery
Make a plan for creation, rotation and removal of encryption keys!
Takeaways
Never implement your own crypto primitives
Never leverage cryptography if you don’t understand it’s fundamentals
Learn the fundamentals!
Avoid using low level crypto libraries (OpenSSL, Bouncy Castle, Java JCE, PyCrypto) unless you
are a pro
 Beware of compositional weaknesses. Consult person with advanced knowledge of
cryptography.
Use high level crypto libraries (KeyCzar, NaCL, libsodium )
Keyczar
Keyczar’s goal is to make it easier for application developers to safely use cryptography.
Keyczar defaults to safe algorithms, key lengths, and modes, and prevents developers from
inadvertently exposing key material. It uses a simple, extensible key versioning system that
allows developers to easily rotate and retire keys.
Java, Python, and C++ implementations
Key rotation and versioning
https://github.com/google/keyczar
http://jay.tuley.name/keyczar-dotnet/
Example: encryption
Crypter crypter = new Crypter("/path/to/your/keys");
String ciphertext = crypter.encrypt("Secret message");
Recommended resources
Matasano crypto challenges: http://cryptopals.com/
Standford Cryptography
https://www.coursera.org/course/crypto
 Standford Cryptography II
https://www.coursera.org/course/crypto2
Schneier on Security https://www.schneier.com
Crypto failures every developer should avoid

More Related Content

PPTX
A Brief History of Cryptographic Failures
PPTX
Crypto failures every developer should avoid
PDF
NTXISSACSC4 - A Brief History of Cryptographic Failures
PDF
Offensive malware usage and defense
PDF
"Giving the bad guys no sleep"
PPTX
Corporate Espionage without the Hassle of Committing Felonies
PPTX
"There's a pot of Bitcoins behind the ransomware rainbow"
PDF
NTXISSACSC4 - Artifacts Are for Archaeologists: Why Hunting Malware Isn't Enough
A Brief History of Cryptographic Failures
Crypto failures every developer should avoid
NTXISSACSC4 - A Brief History of Cryptographic Failures
Offensive malware usage and defense
"Giving the bad guys no sleep"
Corporate Espionage without the Hassle of Committing Felonies
"There's a pot of Bitcoins behind the ransomware rainbow"
NTXISSACSC4 - Artifacts Are for Archaeologists: Why Hunting Malware Isn't Enough

What's hot (20)

PDF
NTXISSACSC4 - Detecting and Catching the Bad Guys Using Deception
PDF
Network Security and Cryptography.pdf
PDF
RSA Algoritmn
PPTX
Using Cryptography Properly in Applications
PPTX
ANALYZE'15 - Bulk Malware Analysis at Scale
PPTX
Defcon Crypto Village - OPSEC Concerns in Using Crypto
PDF
The 4horsemen of ics secapocalypse
PPTX
Cryptography and Encryptions,Network Security,Caesar Cipher
PPTX
Cryptographic tools
 
PPTX
GreyNoise - Lowering Signal To Noise
PDF
Inside Cybercrime Groups Harvesting Active Directory for Fun and Profit - Vit...
PDF
INTRODUCTION TO CRYPTOGRAPHY
PDF
Web Security.pdf
PPTX
HITCON 2015 - DGAs, DNS and Threat Intelligence
PDF
NTXISSACSC4 - Ransomware: History Analysis & Mitigation
PDF
Surreptitiously weakening cryptographic systems
PPT
fucking shit
PPT
Cryptography
PDF
IPv6 Security Talk mit Joe Klein
PPTX
Deploying a Shadow Threat Intel Capability at Thotcon on May 6, 2016
NTXISSACSC4 - Detecting and Catching the Bad Guys Using Deception
Network Security and Cryptography.pdf
RSA Algoritmn
Using Cryptography Properly in Applications
ANALYZE'15 - Bulk Malware Analysis at Scale
Defcon Crypto Village - OPSEC Concerns in Using Crypto
The 4horsemen of ics secapocalypse
Cryptography and Encryptions,Network Security,Caesar Cipher
Cryptographic tools
 
GreyNoise - Lowering Signal To Noise
Inside Cybercrime Groups Harvesting Active Directory for Fun and Profit - Vit...
INTRODUCTION TO CRYPTOGRAPHY
Web Security.pdf
HITCON 2015 - DGAs, DNS and Threat Intelligence
NTXISSACSC4 - Ransomware: History Analysis & Mitigation
Surreptitiously weakening cryptographic systems
fucking shit
Cryptography
IPv6 Security Talk mit Joe Klein
Deploying a Shadow Threat Intel Capability at Thotcon on May 6, 2016
Ad

Similar to Crypto failures every developer should avoid (20)

PDF
Crypto Strikes Back! (Google 2009)
PDF
When Crypto Attacks! (Yahoo 2009)
PDF
Cryptography
PPTX
Security Training: #4 Development: Typical Security Issues
PPTX
My Baby Done Bad Crypto - My Sweet Baby Done Me Wrong
PDF
Introduction to cryptography for software developers
PPTX
Crypto in the Real World - ISACA 10-Jan-2008
PDF
Applied cryptanalysis - everything else
PDF
"Crypto wallets security. For developers", Julia Potapenko
PDF
Security Theatre - Benelux
PDF
Security theatre (Scotland php)
PDF
Common crypto attacks and secure implementations
PDF
Crypto workshop part 3 - Don't do this yourself
ODP
Applying Security Algorithms Using openSSL crypto library
PPTX
Implementing a Secure and Effective PKI on Windows Server 2012 R2
ODP
Crypto in the Real World: or How to Scare an IT Auditor
PDF
Security Theatre - Confoo
PPTX
Technology, Process, and Strategy
PDF
Hash
PDF
What Every Software Engineer Should Know About Security and Encryption
Crypto Strikes Back! (Google 2009)
When Crypto Attacks! (Yahoo 2009)
Cryptography
Security Training: #4 Development: Typical Security Issues
My Baby Done Bad Crypto - My Sweet Baby Done Me Wrong
Introduction to cryptography for software developers
Crypto in the Real World - ISACA 10-Jan-2008
Applied cryptanalysis - everything else
"Crypto wallets security. For developers", Julia Potapenko
Security Theatre - Benelux
Security theatre (Scotland php)
Common crypto attacks and secure implementations
Crypto workshop part 3 - Don't do this yourself
Applying Security Algorithms Using openSSL crypto library
Implementing a Secure and Effective PKI on Windows Server 2012 R2
Crypto in the Real World: or How to Scare an IT Auditor
Security Theatre - Confoo
Technology, Process, and Strategy
Hash
What Every Software Engineer Should Know About Security and Encryption
Ad

Recently uploaded (20)

PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Big Data Technologies - Introduction.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Spectroscopy.pptx food analysis technology
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Approach and Philosophy of On baking technology
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
cuic standard and advanced reporting.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
20250228 LYD VKU AI Blended-Learning.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Advanced methodologies resolving dimensionality complications for autism neur...
Big Data Technologies - Introduction.pptx
MYSQL Presentation for SQL database connectivity
sap open course for s4hana steps from ECC to s4
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Spectroscopy.pptx food analysis technology
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Network Security Unit 5.pdf for BCA BBA.
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Chapter 3 Spatial Domain Image Processing.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Approach and Philosophy of On baking technology
Dropbox Q2 2025 Financial Results & Investor Presentation
cuic standard and advanced reporting.pdf

Crypto failures every developer should avoid

  • 1. Crypto failures every developer should avoid FILIP ŠEBESTA - OWASP CZECH CHAPTER MEETING - PRAGUE DECEMBER 7 2015
  • 2. What the conventional wisdom tells us about cryptography Cryptography is dangerous because you get no feedback when you mess it up Never implement your own crypto – what does that really mean? Most of the code using cryptography is not written/reviewed by cryptographers or qualified architects. Crypto built into products is oftentimes broken. Broken crypto might be more dangerous than no crypto as it gives you a false sense of security.
  • 3. Veracode’s State of Software Security (June 2015)
  • 4. The crux of the problem Many programmers think that they can just link to a crypto library and they're done, but cryptography is hard to implement robustly if you don't understand the finer aspects of it. You can easily develop an insecure crypto scheme using only “secure” algo. Longer keys don't always mean more security etc. Penetration testers rarely scrutinize crypto as it is a complex and demanding task. Crypto libraries are too complex – since they were designed by and for cryptographers.
  • 5. Area of confusion What level of knowledge is requred to deal with crypto as a programmer? Do developers need to be cryptographers in order to use crypto correctly? Do we even have a choice? Rather then arguing who’s fault it is shouldn’t we work towards fixing this issue?
  • 6. The way out of it 1.train developers to use it securely 2.Use high level crypto libraries
  • 7. What is this presentation about? The goal of this presentation is to show the disconnect between what is expected from developers leveraging cryptography and the reality. It will show some of the most typical crypto failures so that developers can learn from them. It will also emphasize the importance of abstraction of crypto primitives to not allow developers to screw things so easily.
  • 8. What kind of crypto pitfalls will we cover? In his essay „Security Pitfalls in Cryptography” (1998) Bruce Schneider divided cryptography attacks into following categories: - Attacks Against Cryptographic Designs - Attacks Against Implementations - Attacks Against Passwords - Attacks Against Hardware - Attacks Against Trust Models - Attacks on the Users - Attacks Against Failure Recovery - Attacks Against the Cryptography
  • 9. Failure #1 – Improper password storage
  • 10. LinkedIn leaked out approximately 6.5 million passwords in 2012.
  • 11. What went wrong? The social networking website LinkedIn was hacked on 5 June 2012, and passwords for nearly 6.5 million user accounts were stolen by Russian cybercriminals  LinkedIn failed to use a salt when hashing the passwords LinkedIn also stored the passwords using just SHA-1 algorithm Due to these flaws attackers were able to unscramble the password hashes using standard rainbow tables
  • 13. What happened? User passwords were cryptographically protected using bcrypt, an algorithm so slow and computationally demanding it would literally take centuries to crack all of them. However, apart of storing user passwords with bcrypt, security researches identified two instances where passwords were stored using MD5 for “automatic login” $loginkey = md5(strtolower($username).'::'.strtolower($password)); md5(lc($username).”::”.lc($pass).”:”.lc($email).”:73@^bhhs&#@&^@8@*$”).
  • 15. The right way to do it Never store passwords in plaintext (obviously) Never store hashed passwords without using a unique salt (ideally a random one sufficiently long) Use a cryptographically secure & slow hash functions (PBKDF2, bcrypt, scrypt). Apply a sufficient number of iterations. Slow hashing is desirable as it makes processing of a single password more expensive for the attacker PBKDF2: good enough but can be optimized with GPU bcrypt: less GPU friendly then PBKDF2 but vulnerable to brute-forcing using Field-Programmable Gate Arrays (FPGAs) with relatively small SRAM scrypt: requires not only exponential time but also exponential memory however doesn’t have a cryptanalytic pedigree required to gain most cryptographers' trust Do not allow users to use weak passwords. Even with the strongest crypto, weak passwords are easy to crack. As soon as you receive a password, remove it from memory.
  • 16. High level approach example string password_hash ( string $password , integer $algo [, array $options ] ) password_hash() creates a new password hash using a strong one-way hashing algorithm. Uses bcrypt or PASSWORD_DEFAULT (a constatnt which may change in time) Options Cost and Salt are secure by default http://php.net/manual/en/function.password-hash.php
  • 17. Failure #2 Improper use of hash functions It’s 2015 and developers still use MD5 SHA- 1 has been considered insecure for long time, yet it still used widely. Comparing hashes also causes problems – timing attacks Using certain hash functions for what they are not might also cause issues e.g. length extension attacks
  • 18. Comparing Hashes if ($hash1 === $hash2) { //mac verification is OK echo "hashes are equal" } else { //something bad happened } For a given message, attempt to send it with a hash of all one single character. Do this once for each ASCII char e.g. 'aaaa...', 'bbbb...' etc. Measure the time each request takes to complete. Since string equality takes a tiny bit longer to complete when the first char matches, the message that takes the longest to return will have the correct first character.
  • 19. Length extension attacks key = “secret attacker doesn’t know” value =“send $100” Signature = sha256(key + “:” + value) Length extension attack allows us to append data and be able to compute a valid signature without actually knowing the key. value =“send $1000” Signature = “valid signature” https://blog.skullsecurity.org/2012/everything-you-need-to-know-about-hash-length-extension- attacks
  • 20. The right way to do it Don’t ever use insecure algo such as MD5 or SHA-1. Use SHA-2 instead (SHA-256, Sha-384, SHA -512)  For signed MAC use HMAC-SHA2 If you compare hashes, use only cryptographically secure functions e.g. : .NET : RSACryptoServiceProvider.VerifyHash https://msdn.microsoft.com/en- us/library/system.security.cryptography.rsacryptoserviceprovider.verifyhash(v=vs.110).aspx
  • 21. Failure #3 Using encryption without authentication Encryption doesn’t automatically protect data against modification Chosen cipher text attacks are a real threat Bit flipping attacks Padding oracle
  • 24. Padding oracle Frequently present in web applications Occurs when a web service reveals whether there was a padding or integrity error when decryption (can be also base on timing) If you can feed in ciphertexts and somehow find out whether or not they decrypt to something with valid padding or not, then you can decrypt ANY given ciphertext. Typical scenario : Using AES in CBC mode where have a ciphertext encrypted by a web service. POODLE
  • 25. How to do it right? Never underestimate the importance of message authenticity even in situation without an obvious attack vector. There is rarely a reason for not doing authentication checks when decrypting data Prefer authenticated modes of operation e.g. GCM, OCB (patented) If forced using AES with CBC use HMAC to ensure message authenticity
  • 26. Failure #4 Reusing nonces and IVs Nonces should be used only once – that’s what the word stands for The purpose of their use is to avoid replay attacks IVs should usually be non-predictable Length of IVs is also important (WEP)
  • 27. Attacks on WEP WEP uses 24b IV For RC4 security is important that the same key is never used twice For every 24b IV there is a 50% chance that the same IV will repeat after approximately 5000 packets
  • 28. The right way to do it Never reuse nonces Do not use static IVs Always use random, unpredictable IVs with sufficient length
  • 29. Pitfall #5 Dealing randomness A good source of true randomness is an integral part of most cryptosystems. Common mistakes include: Using no or limited randomness when required Using sources of “randomness” which are actually not random at all Leaking internal state of PRNG
  • 30. Random Number Bug in Debian MD_Update(&m,buf,j); [ .. ] MD_Update(&m,buf,j); /* purify complains */ Removing this code had the side effect of crippling the seeding process for the OpenSSL PRNG. Instead of mixing in random data for the initial seed, the only "random" value that was used was the current process ID.
  • 32. Modulo bias int x; int modulus = 4; x= rand() % modulus; // random string 0-3 Assuming that rand() returns values (0,1,2,3,4,5) Probability of 0: 33.33% Probability of 1: 33.33% Probability of 2: 16.66% Probability of 3: 16.66%
  • 33. Modulo bias #2 You will get modulo bias if max(rand()) + 1 is not evenly divisible by your modulus. int excess = (RAND_MAX + 1) % modulus; int limit = RAND_MAX - excess; while (x = rand() > limit) {}; return x % modulus;
  • 34. The right way to do it .NET : https://msdn.microsoft.com/en- us/library/system.security.cryptography.randomnumbergenerator C++ (Windows): CryptGenRandom function in MS-CAPI https://msdn.microsoft.com/en- us/library/windows/desktop/aa379942(v=vs.85).aspx Linux: read from /dev/urandom recommended reading: http://www.2uo.de/myths-about-urandom/
  • 35. Failure #6 SSL/TLS Certificates Using TLS in non-browser software is a challenging task Using standard SSL/TLS libraries is not enough Certificate validation is sometimes completely disabled Certificate revocation check is oftentimes missing Full chain check is sometimes not complete Subject is sometimes not verified at all!
  • 37. The right way to do it Implementing proper certificate validation is not a trivial task. It requires a deep understanding of the protocol. Don’t assume that using SSL/TLS libraries ensures proper certificate validation. Don’t disable SSL for testing purposes, it may backfire. Implement unit testing (especially negative one) to verify that certificate validation is implemented properly. To mitigate the risks of rogue certificates consider certificate pinning.
  • 40. Failure #7 Insecure key provisioning A very common mistake is to spend lots of time designing an impenetrable crypto scheme and then not protecting keys properly Keys are often generated in an insecure environment, stored on developers machines, hardcoded in the source code etc. People also don’t think through the scenarios when keys get leaked
  • 42. The right way to do it Avoid using passwords as keys as they don’t have enough entropy and use truly random keys instead. If you have to use passwords as keys apply key derivation functions e.g. PBKDF2 Don’t reuse the same key on many devices. Don’t reuse the same key for multiple purposes (encryption, authentication) Generate keys in a secure environment Store keys securely Account for periodic key rotation and disaster recovery Make a plan for creation, rotation and removal of encryption keys!
  • 43. Takeaways Never implement your own crypto primitives Never leverage cryptography if you don’t understand it’s fundamentals Learn the fundamentals! Avoid using low level crypto libraries (OpenSSL, Bouncy Castle, Java JCE, PyCrypto) unless you are a pro  Beware of compositional weaknesses. Consult person with advanced knowledge of cryptography. Use high level crypto libraries (KeyCzar, NaCL, libsodium )
  • 44. Keyczar Keyczar’s goal is to make it easier for application developers to safely use cryptography. Keyczar defaults to safe algorithms, key lengths, and modes, and prevents developers from inadvertently exposing key material. It uses a simple, extensible key versioning system that allows developers to easily rotate and retire keys. Java, Python, and C++ implementations Key rotation and versioning https://github.com/google/keyczar http://jay.tuley.name/keyczar-dotnet/
  • 45. Example: encryption Crypter crypter = new Crypter("/path/to/your/keys"); String ciphertext = crypter.encrypt("Secret message");
  • 46. Recommended resources Matasano crypto challenges: http://cryptopals.com/ Standford Cryptography https://www.coursera.org/course/crypto  Standford Cryptography II https://www.coursera.org/course/crypto2 Schneier on Security https://www.schneier.com