Skip to Content
oxcrypto

HKDF

Extract-and-expand key derivation (RFC 5869) over every fixed-output hash.

from oxcrypto.kdf.hkdf import HKDF

algorithm = HKDF.SHA256
key = algorithm.derive(shared_secret, salt=salt, info=b"context", length=32)

prk = algorithm.extract(shared_secret, salt=salt)
key = algorithm.expand(prk, info=b"context", length=32)

extract returns a pseudorandom key of the hash output size. When salt is omitted it is HashLen zero bytes, as RFC 5869 specifies. expand requires a prk of at least the hash output size and returns length bytes, from 0 up to 255 times the hash output size. derive runs both steps.

Every selector of oxcrypto.hash with a fixed output size is available, from HKDF.SHA256 and HKDF.SHA3_256 to HKDF.STREEBOG512 and HKDF.BLAKE3. Prefer SHA-2 or SHA-3 for interoperability. MD2, MD4, MD5 and SHA-1 exist only for legacy protocols.

StandardRFC 5869 (Informational), approved for key derivation in NIST SP 800-56C Rev. 2
Cratehkdf 0.13.0 with hmac 0.13.0
MaturityStable release, no published audit
MSRVRust 1.85, builds with the project MSRV 1.88
ExperimentalNo. HMAC over hashes other than SHA-1, SHA-2 and SHA-3 is not covered by NIST approval.
Zeroizationhkdf keeps the previous T(i) blocks, and hmac keeps the padded key block and the inner hash, without wiping them.