HKDF
   HOME

TheInfoList



OR:

HKDF is a simple key derivation function (KDF) based on HMAC message authentication code. 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. 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). 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. It is formally described in RFC 5869. One of its authors also described the algorithm in a companion paper in 2010. NIST SP800-56Cr2 specifies a parameterizable extract-then-expand scheme, noting that RFC5869 HKDF is a version of it and citing its paper for the rationale for the recommendations' extract-and-expand mechanisms. There are implementations of HKDF for C#, Go,
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mos ...
,
JavaScript JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of websites use JavaScript on the client side for webpage behavior, of ...
,
Perl Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offic ...
,
PHP PHP is a general-purpose scripting language geared toward web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementation is now produced by The PHP Group. ...
,
Python Python may refer to: Snakes * Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia ** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia * Python (mythology), a mythical serpent Computing * Python (pro ...
,
Ruby A ruby is a pinkish red to blood-red colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called ...
, and other
programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
s.


Mechanism

HKDF extracts a
pseudorandom A pseudorandom sequence of numbers is one that appears to be statistically random, despite having been produced by a completely deterministic and repeatable process. Background The generation of random numbers has many uses, such as for rand ...
key (PRK) using an HMAC hash function (e.g. HMAC- SHA256) on an optional
salt Salt is a mineral composed primarily of sodium chloride (NaCl), a chemical compound belonging to the larger class of salts; salt in the form of a natural crystalline mineral is known as rock salt or halite. Salt is present in vast quant ...
(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 appending the previous hash block with an incrementing 8-bit counter with an optional info string providing application-specific context before being hashed by HMAC to generate the current hash block. An important property of HKDF is that it 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: # To "extract" (condense/blend)
entropy Entropy is a scientific concept, as well as a measurable physical property, that is most commonly associated with a state of disorder, randomness, or uncertainty. The term and the concept are used in diverse fields, from classical thermodynam ...
from a larger random source to provide a more uniformly unbiased and higher entropy but smaller output (e.g., an
encryption key A key in cryptography is a piece of information, usually a string of numbers or letters that are stored in a file, which, when processed through a cryptographic algorithm, can encode or decode cryptographic data. Based on the used method, the key ...
). This is done by utilising the diffusion properties of cryptographic MACs. # 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 to 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: int, ikm, salt: bytes = b"", info: bytes = b"") -> bytes: """Key derivation function""" if len(salt)

0: salt = bytes( * hash_len) 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) okm += t return okm length okm = hkdf(length=42, ikm=bytes.fromhex('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b'), salt=bytes.fromhex('000102030405060708090a0b0c'), info=bytes.fromhex('f0f1f2f3f4f5f6f7f8f9')) assert okm

bytes.fromhex( '3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865')


References

{{Reflist Cryptography Key derivation functions