(Very) Basic Intro to Key Derivation Functions (Argon2, Scrypt, etc)
Tags: cryptography, passwords, programming, security
Published: 2019-12-30 14:26:46

---
title: (Very) Basic Intro to Key Derivation Functions (Argon2, Scrypt, etc)
published: true
date: 2019-12-30 14:23:26 UTC
tags: Cryptography,Passwords,Programming,Security
canonical_url: https://qvault.io/2019/12/30/very-basic-intro-to-key-derivation-functions-argon2-scrypt-etc/?utm_source=rss&utm_medium=rss&utm_campaign=very-basic-intro-to-key-derivation-functions-argon2-scrypt-etc
---

![](https://qvault.io/wp-content/uploads/2019/12/photo-1553386323-60698d6f7325-100x67.png)

![](https://qvault.io/wp-content/uploads/2019/12/photo-1553386323-60698d6f7325-1024x683.png)

A [key derivation function](https://en.wikipedia.org/wiki/Key_derivation_function), or KDF, derives one or many secret keys from a secret value. If you’ve ever needed to store a password in a database or create a private key from a password, you may have used a KDF.

Examples of popular KDFs:

- [Argon2](https://en.wikipedia.org/wiki/Argon2)
- [Scrypt](https://en.wikipedia.org/wiki/Scrypt)
- [PBKDF2](https://en.wikipedia.org/wiki/PBKDF2)

## Are KDFs just hash functions?

No, but there is overlap. In order to understand KDFs, let’s first go through a quick refresher on [hash functions.](https://blog.goodaudience.com/very-basic-intro-to-hash-functions-sha-256-md-5-etc-ed721622ff8)

Examples of hash functions include:

- Sha256
- MD5
- Bcrypt

A hash function takes an input and creates an output. In most password hashing scenarios it looks something like this:

```
sha256("password123") -> ef92b778bafe771e89245b89ecbc08a44a4e166c06659911881f383d4473e94f
```

The function must have the following properties:

- Data is scrambled deterministically (Same input, same output)
- No matter the input, the output of a hash function always has the same size
- The input can not be retrieved from the output (one-way function)

## So what is the difference?

As it turns out, all key derivation functions are secure hash functions, but not all hash functions are secure KDFs.

![](https://qvault.io/wp-content/uploads/2019/12/Capture-1.png)

In addition to the properties of a hash function, KDFs can serve the [following purposes](https://crypto.stackexchange.com/questions/6564/why-is-bcrypt-called-a-key-derivation-function):

- Key Stretching
- Key Whitening
- Key Separation
- Key Strengthening

Let’s look at each case separately, with the following definition of our general KDF in mind:

```
derivedKey = keyDerivationFunction(originalKey, salt, difficulty)
```

**Salt** is random data used to protect against pre-computation attacks or rainbow tables.

**Difficulty** can be used to make the KDF slower via intense computation, memory, or parallelism requirements. This protects against brute force attacks because it will take an attacker longer per guess.

## Key Stretching

Key stretching is the most common use case for the average developer. The idea is to take a key with low-entropy (security or randomness) and stretch it into a longer key that is more secure. Passwords are a great example. Many websites use Bcrypt to stretch keys:

```
passwordForDB = bcrypt(password, salt, difficulty)
```

## Key Whitening

Some encryption schemes require specific key lengths, for example, 256 bits. Let’s say we have a system that already has keys generated, but the keys are 512 bits long. We can use a KDF to get the keys into the correct format and size.

## Key Separation 

KDFs allow child keys to be created from a master key. This can be used in applications like Bitcoin where child keys can control sections of a wallet, and only the master has full control. This is done by use of different salts. For example:

```
childOne = kdf(masterKey, saltOne, difficulty)
childTwo = kdf(masterKey, saltTwo, difficulty)
childThree = kdf(masterKey, saltThree, difficulty)
```

## Key Strengthening

Strengthing extends a key with a random salt, but then deletes the salt so it can’t be used again. This makes the resulting key stronger without adding significant vulnerabilities to the system.

## Should I Use KDFs?

Yes. Most often when storing passwords in databases, but also if any of these other use cases fall into the domain of your code. Tweet at me if you have comments or questions.

Lane on Twitter: [@wagslane](https://twitter.com/wagslane)

Lane on Dev.to: [wagslane](https://dev.to/wagslane)

Download Qvault: [https://qvault.io](https://qvault.io/)

Star our Github: [https://github.com/q-vault/qvault](https://github.com/q-vault/qvault)

The post [(Very) Basic Intro to Key Derivation Functions (Argon2, Scrypt, etc)](https://qvault.io/2019/12/30/very-basic-intro-to-key-derivation-functions-argon2-scrypt-etc/) appeared first on [Qvault](https://qvault.io).