Key Derivation Function (KDF)

Turning a weak input, such as a passphrase, into a strong key through deliberate, slow hashing.

Basically

A key derivation function turns a password or weak secret into a strong key. It makes guessing take more work, which protects encryption and authentication.

Key Takeaways

  • A key derivation function turns a password or weak secret into a strong cryptographic key through deliberate computational work, making brute-force attacks impractical. The process is called key stretching.
  • Bitcoin wallets use PBKDF2 to turn a seed phrase into a master seed. BIP-39 specifies 2048 rounds of HMAC-SHA512, producing the 512-bit seed that feeds HD wallet derivation.
  • Modern KDFs like scrypt and Argon2 add memory hardness on top of iteration count. They defend against GPU and ASIC-based attacks that exploit the speed of simple hash functions.

What Is a Key Derivation Function?

A key derivation function (KDF) is a cryptographic algorithm that takes a secret input, such as a password, passphrase or shared secret, and produces one or more cryptographic keys for encryption or authentication. Human-chosen passwords carry far too little entropy to serve directly as cryptographic keys. A typical password contains roughly 6 to 7 bits of entropy per character, making it trivially guessable when used as a raw key.

KDFs address this by applying a pseudorandom function repeatedly, forcing any attacker to spend significant time and resources on each guess. A modern GPU can compute a plain SHA-256 hash billions of times per second, but a properly configured KDF makes each password guess deliberately expensive through hundreds of milliseconds of CPU time, megabytes or gigabytes of RAM, or both.

Two broad categories of KDFs exist. Password-based KDFs, including PBKDF2, scrypt and Argon2, are intentionally slow to protect weak human-chosen secrets. Non-password-based KDFs, including HKDF, are designed for efficiency and derive multiple keys from already-strong cryptographic material like Diffie-Hellman shared secrets or master keys.

How It Works

Every password-based KDF follows the same fundamental pattern. It takes the password, combines it with a salt and applies a pseudorandom function many times to produce the output key.

The salt is critical because an attacker could otherwise precompute a table mapping common passwords to their derived keys. A 128-bit salt makes each password produce a different output for each salt value, making precomputation infeasible.

PBKDF2

PBKDF2 (Password-Based Key Derivation Function 2) was specified in RFC 2898 in 2000 and updated by RFC 8018 in 2017. It repeatedly applies a pseudorandom function (PRF), typically HMAC, to the password and salt combination.

PBKDF2 has configurable parameters. They control how the function derives the key from the password and salt.

PBKDF2 is the most widely deployed KDF because of its simplicity and FIPS-140 compliance. Its main weakness is that it requires only CPU time, not memory, making it vulnerable to massively parallel GPU attacks. OWASP currently recommends a minimum of 600,000 iterations for PBKDF2-SHA-256 and 210,000 for PBKDF2-SHA-512.

scrypt

Colin Percival designed scrypt in 2009, and it was standardised in RFC 7914 in 2016. It introduced memory hardness to key derivation by forcing each computation to allocate a large block of RAM, unlike PBKDF2, which only consumes CPU cycles.

scrypt takes three core parameters. These parameters set the resources required for each derivation.

The memory requirement is approximately 128 × N × r bytes. With N=131072 and r=8, scrypt consumes roughly 128 MB of RAM per derivation, making GPU and ASIC attacks far more expensive. Percival estimated in 2009 that a hardware brute-force attack against scrypt costs roughly 4,000 times more than against bcrypt and 20,000 times more than against PBKDF2.

Argon2

Argon2 won the Password Hashing Competition in July 2015 and was standardised in RFC 9106 in 2021. Alex Biryukov, Daniel Dinu and Dmitry Khovratovich designed it at the University of Luxembourg. Argon2 comes in three variants: Argon2d, Argon2i and Argon2id.

Argon2 exposes three tunable parameters. They let you adjust the resources used during key derivation.

RFC 9106 recommends Argon2id with 64 MiB of memory or more, 3 iterations and 4 threads for general use. Where resources allow, 2 GiB of memory with a single iteration provides stronger protection at equivalent latency.

HKDF

HKDF (HMAC-based Extract-and-Expand Key Derivation Function), specified in RFC 5869 in 2010, serves a different purpose from password-based KDFs. It derives multiple keys from an already-strong secret like a Diffie-Hellman shared secret instead of stretching weak passwords.

HKDF operates in two stages: extract and expand. These stages turn the input secret into the required output keys.

HKDF is used in TLS 1.3, the Signal Protocol and the Lightning Network's BOLT 8 transport protocol. BOLT 8 uses it to derive session encryption keys from the Noise_XK handshake ECDH outputs.

KDFs vs. Simple Hash Functions

A common mistake is using a plain hash function like SHA-256 to derive a key from a password. Both KDFs and hash functions produce fixed-length outputs from arbitrary inputs, but they solve fundamentally different problems.

Hash functions are designed to be fast because their primary use is verifying data integrity through checksumming files, building Merkle trees or computing proof-of-work puzzles. That same speed is a liability for a human-chosen password because an attacker can try billions of guesses per second.

BIP-39 Seed Derivation

The most important KDF application in Bitcoin is BIP-39 mnemonic seed generation. When you create a wallet, BIP-39 derives the seed from your mnemonic and optional passphrase.

BIP-32 then processes this 64-byte seed with HMAC-SHA512 and the key "Bitcoin seed" to produce the master private key and chain code for hierarchical deterministic wallet derivation. Different derivation paths then generate the actual signing keys used for transactions.

Wallet Encryption

Desktop wallets use KDFs to encrypt the wallet file with a user-chosen password, deriving the AES encryption key when you enter your password. Without the KDF's computational overhead, an attacker with a copy of the encrypted wallet file could attempt billions of password guesses per second. A properly tuned KDF makes each guess take hundreds of milliseconds, reducing the practical attack surface from seconds to centuries.

Lightning Network Transport

The Lightning Network uses HKDF in its BOLT 8 encrypted transport protocol. During the three-act Noise_XK handshake, HKDF derives temporary encryption keys from each elliptic curve Diffie-Hellman exchange. After the handshake completes, HKDF produces the symmetric session keys used to encrypt all subsequent messages between Lightning peers.

Secure Key Storage

Cryptocurrency exchanges, custodians and hardware security modules use KDFs when encrypting key material at rest. The KDF ensures that even if an attacker gains access to the encrypted key store, the master password cannot be quickly recovered through brute force. Spark and similar Layer 2 protocols that manage private keys on behalf of users depend on strong KDF-protected storage for operator-side key material.

Comparing Major KDFs

The research article on hardware wallet attack vectors explains how these cryptographic primitives protect wallet infrastructure. You can also read the guide to wallet recovery methods.

Parameter Selection

Choosing the wrong parameters undermines the entire purpose of a KDF. Too few iterations or too little memory make the function trivially fast to brute-force, while too many cause unacceptable login delays for legitimate users. The right balance targets roughly 100 milliseconds of computation on the expected hardware, adjusted upward as attacker hardware improves over time.

BIP-39's Fixed Iteration Count

BIP-39 specifies exactly 2048 rounds of PBKDF2-HMAC-SHA512, a number chosen in 2013 that has not been updated since and is relatively low by modern standards. However, BIP-39 mnemonics already carry 128 to 256 bits of entropy, far exceeding a typical password. The PBKDF2 step serves less as a brute-force defence because the mnemonic is already high-entropy and more as a one-way transformation that separates the mnemonic from the derived seed, allowing the optional passphrase to act as a 25th word.

No Substitute for Strong Entropy

A KDF cannot create entropy that does not exist in the input. If you pick a 4-digit PIN as your password, even Argon2 with maximum parameters only slows an attacker because all 10,000 possible PINs will eventually be tried. KDFs work best when combined with strong entropy sources and proper key management practices.

Side-Channel Attacks

Data-dependent KDFs like Argon2d and scrypt are vulnerable to side-channel attacks where an attacker monitors memory access patterns or timing to extract information about the password. Argon2id, the hybrid variant, is recommended for most applications because it combines Argon2i's side-channel resistance with Argon2d's GPU resistance.

Quantum Resistance

KDFs based on symmetric cryptography, including HMAC and SHA-2, are relatively resilient against quantum computers. Grover's algorithm reduces the effective security of symmetric primitives by half, so 256-bit security becomes 128-bit equivalent, but this is far less catastrophic than Shor's algorithm's impact on elliptic curve and RSA cryptography. The post-quantum cryptography article offers a broader discussion.

The glossary entry is for informational purposes only and does not constitute financial or investment advice. Always do your own research before using any protocol or technology.

0

← All 1876 glossary terms