HKDF: Difference between revisions
→Mechanism: clarified a bit |
m →Uses |
||
Line 21: | Line 21: | ||
HKDF has two primary and potentially independent uses: |
HKDF has two primary and potentially independent uses: |
||
1. To "extract" (condense/blend) entropy from a larger random source to provide a more uniformly unbiased and higher entropy but smaller output |
1. To "extract" (condense/blend) entropy from a larger random source to provide a more uniformly unbiased and higher entropy but smaller output (e.g. an encryption key). This is done by utilising the diffusion properties of cryptographic MACs. |
||
2. To "expand" the generated output of an already reasonably random input such as an existing shared key into a larger cryptographically independent output, thereby producing multiple keys deterministically from that initial shared key, so that the same process may produce those same secret keys safely on multiple devices, as long as the same inputs are utilised. |
2. To "expand" the generated output of an already reasonably random input such as an existing shared key into a larger cryptographically independent output, thereby producing multiple keys deterministically from that initial shared key, so that the same process may produce those same secret keys safely on multiple devices, as long as the same inputs are utilised. |
||
These two functions may also be combined and used to form a [[ |
These two functions may also be combined and used to form a [[pseudorandom number generator|PRNG]] to improve a random number generator's potentially-biased output, as well as protect it from analysis and help defend the random number generation from malicious inputs. |
||
== Example: Python implementation == |
== Example: Python implementation == |
Revision as of 23:52, 25 March 2018
This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these messages)
|
HKDF is a simple key derivation function (KDF) based on a hash-based message authentication code (HMAC).[1] It was initially proposed by its authors as a building block in various protocols and applications, as well as to discourage the proliferation of multiple KDF mechanisms.[1] The main approach HKDF follows is the "extract-then-expand" paradigm, where the KDF logically consists of two modules: the first stage takes the input keying material and "extracts" from it a fixed-length pseudorandom key, and then the second stage "expands" this key into several additional pseudorandom keys (the output of the KDF).[1]
It can be used, for example, to convert shared secrets exchanged via Diffie–Hellman into key material suitable for use in encryption, integrity checking or authentication.[2]
It is formally described in the RFC 5869.[1] One of its authors also described the algorithm in a companion paper in 2010.[2]
Mechanism
HKDF extracts a pseudorandom key (PRK) using an HMAC hash function (e.g. HMAC-SHA256) on an optional salt (acting as a key) and any potentially weak input key material (IKM) (acting as data). It then generates similarly cryptographically strong output key material (OKM) of any desired length by repeatedly generating PRK-keyed hash-blocks and then appending them into the output key material, finally truncating to the desired length.
For added security, the PRK-keyed HMAC-hashed blocks are chained during their generation by prepending the previous hash block to an incrementing 8-bit counter with an optional context string in the middle before being hashed by HMAC to generate the current hash block.
Note: HKDF does not amplify entropy but does allow a large source of weaker entropy to be utilised more evenly and effectively.
Uses
HKDF has two primary and potentially independent uses:
1. To "extract" (condense/blend) entropy from a larger random source to provide a more uniformly unbiased and higher entropy but smaller output (e.g. an encryption key). This is done by utilising the diffusion properties of cryptographic MACs.
2. To "expand" the generated output of an already reasonably random input such as an existing shared key into a larger cryptographically independent output, thereby producing multiple keys deterministically from that initial shared key, so that the same process may produce those same secret keys safely on multiple devices, as long as the same inputs are utilised.
These two functions may also be combined and used to form a PRNG to improve a random number generator's potentially-biased output, as well as protect it from analysis and help defend the random number generation from malicious inputs.
Example: Python implementation
#!/usr/bin/env python3
import hashlib
import hmac
from math import ceil
hash_len = 32
def hmac_sha256(key, data):
return hmac.new(key, data, hashlib.sha256).digest()
def hkdf(length, ikm, salt=b"", info=b""):
prk = hmac_sha256(salt, ikm)
t = b""
okm = b""
for i in range(ceil(length / hash_len)):
t = hmac_sha256(prk, t + info + bytes([1+i]))
okm += t
return okm[:length]
References
- ^ a b c d Krawczyk, H.; Eronen, P. (May 2010). "RFC 5869". Internet Engineering Task Force.
- ^ a b Krawczyk, Hugo (2010). "Cryptographic Extraction and Key Derivation: The HKDF Scheme" (PDF). Cryptology ePrint Archive. International Association for Cryptologic Research.