Are you fascinated with cryptography? You’re not alone: a lot of engineers are. Occasionally, some of them decide to go as far as to write their own custom cryptographic hash functions and use them in real-world applications. While understandably enticing, doing so breaks the number 1 rule of the security community:??ッdon’t write your own crypto.ツ?
How do hashing algorithms work and what’s special about password hashing? What does it take for an algorithm to get ready for widespread production use? Is security through obscurity a good idea? Let’s see.ツ?
Before storing a user’s password in your application’s database, you’re supposed to apply a cryptographic hash function to it. (You’re not storing passwords in plain text, right? Good. Just asking.)ツ?
Any cryptographic hash function converts an arbitrary-length input (a.k.a. “message”) into a fixed-length output (a.k.a. “hash”, “message digest”). A??ッsecure cryptographic hash function??ッmust be:ツ?
Now, there’s general cryptographic hashing, and then there’s password hashing that is somewhat special.ツ?
Standard cryptographic hash functions are designed to be fast, and when you’re hashing passwords, it becomes a problem.??ッPassword hashing must be slow.??ッYou want to make it as hard as possible for the attacker to apply brute force attacks to passwords in your database should it ever leak. This is why you want to make passwords hashing computationally expensive. How expensive? Well, it’s a tradeoff between convenience for your legitimate users when they validate their passwords and making brute-force attacks hard for the attacker.ツ?
To make hashing computationally expensive, a special kind of functions is commonly used:??ッkey derivation functions??ッ(KDFs). Under the hood, KDFs invoke hashing functions, but they add a random salt before hashing, and then apply numerous (usually thousands or tens of thousands) iterations of hashing. Ideally, they make brute force attacks both CPU-intensive and memory-intensive.ツ?
A key derivation function produces a derived key from a base key and other parameters. In a password-based key derivation function, the base key is a password and the other parameters are a salt value and an iteration countツ?(RFC 2898: Password-Based Cryptography Specification Version 2.0).
In password hashing discussions, the terms “hash function” (such as MD5 or SHA-1) and “key derivation function” (such as PBKDF2 or Argon2) are often used interchangeably although they’re technically not the same.ツ?
Both writing a custom hashing algorithm and creating your own implementation of a well-known algorithm are bad ideas. Why?ツ?
You probably don’t have the skills. Let’s face it: cryptography is hard, and messing up an algorithm or implementation is easy, even for professionals. Should you go for creating your own password hashing, some of the things you’d need to take care of include:ツ?
This is a lot on your plate – even more so given that??ッyou won’t have access to qualified testers??ッfrom the cryptography community to help you find (inevitable) vulnerabilities.ツ?
You’ll likely want to depend on secrecy and obscurity??ッby keeping your algorithm private. Doing so breaks the fundamental doctrine of cryptography known as theツ?Kerckhoff’sツ?principle:??ッ“a cryptosystem should be secure even if everything about theツ?system, except the key, is public knowledge.”??ッSecurity by obscurity can provide a short-term advantage but relying on it long-term is a bad practice:ツ?
You’ll put sensitive user data at risk. Leaking sensitive user data is one of the worst things that can happen to a business. This is something that instantly undermines trust, turns customers away, and is very expensive to remediate. Some companies and lots of developers are prone to the Not Invented Here fallacy, but password hashing is probably the worst thing you can choose to re-implement.ツ?
Most importantly,??ッyou won’t know when your algorithm gets broken.ツ?
Established algorithms and implementations benefit from??ッyears of testing and polishing??ッby large communities of cryptography experts who help reveal and fix vulnerabilities without any malicious intent.ツ?
Since your own algorithm and/or implementation won’t be available to anyone with a good will, attackers will be the only category of people willing to crack it. Once they do that, they won’t give you a heads–up;ツ?you’ll only know when sensitive data of your users isツ?compromised,ツ?and your business is in serious trouble.ツ?
That’s great! Go forward and practice. Read reference implementations of existing algorithms, play with your own implementations, reach out to the community for advice, and have a great time learning something new and exciting!ツ?
Just don’t use whatever you’ve written in your production applications.ツ?
To learn more, read our vulnerability decoder on insecure crypto.ツ?
*** This is a Security Bloggers Network syndicated blog from Application Security Research, News, and Education Blog authored by fheisler@veracode.com (fheisler). Read the original post at: https://www.veracode.com/blog/secure-development/how-password-hashing-algorithms-work-and-why-you-never-ever-write-your-own
Organizations and businesses have had to turn to third-party cloud and managed security services to look for ways to bolster…
The pandemic and resulting remote work have revived concerns surrounding shadow IT Two of the biggest challenges for IT departments…
In episode 152 for December 21st 2020: A discussion about the SolarWinds Orion backdoor, third-party security, and the threat of…
The sudden transition to remote work brought on by the pandemic resulted in many companies ensuring employees had the necessary…
Many thanks to DEF CON and Conference Speakers for publishing their outstanding presentations; of which, originally appeared at the organization's…
via the respected information security capabilities of Robert M. Lee & the superlative illustration talents of Jeff Haas at Little…