How Password Hashing Algorithms Work and Why You Never Ever Write Your Own

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.ツ?

How does password hashing work?ツ?

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:ツ?

  • Deterministic: hashing the same input should always render the same output.ツ?
  • One-way: generating an input message based on a given output should be infeasible.ツ?
  • Collision-resistant: finding two input messages that hash to the same output should also be infeasible.ツ?
  • Highly randomized: a small change in input should lead to a significant and uncorrelated change in output (a.k.a. “the avalanche effect”). Without this property, applying cryptoanalysis methods will allow making predictions about the input based on the output.ツ?

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.ツ?

Why shouldn’t you write your own password hashing algorithm?ツ?

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:ツ?

  • Ensuring??ッpre-image resistance??ッto prevent calculating the input based on the hash output.ツ?
  • Ensuring??ッhigh collision resistance??ッto prevent finding two inputs that hash to the same output.ツ?
  • Randomization and the??ッavalanche effect??ッto make sure attackers can’t easily find hashing patterns and correlations between the input and the output.ツ?
  • Resilience to a wide array ofツ?side-channel attacks??ッ(that is, attacks based on algorithm implementation details and examining the physical effects caused by invoking the implementation), such as timing attacks and cache attacks.ツ?
  • Minimizing any efficiency gains attainable by attackers through the use of??ッcracking-optimized hardware??ッsuch as ASIC, FPGA, and GPUs.ツ?

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:ツ?

  • Hiding vulnerabilities prevents revealing and repairing them as part of an openツ?discussion andツ?increases the probability of exploits.ツ?
  • If your password database ever leaks, there’s a good chance that the source code of your application will leak along with it, and as soon as your untested algorithm becomes known to the attacker, they’ll have an easy time cracking it.ツ?

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 headsup;ツ?you’ll only know when sensitive data of your users isツ?compromised,ツ?and your business is in serious trouble.ツ?

But what if you??ッreally??ッwant to level up your cryptography and learn by doing?ツ?

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