我贴下原文,D_i 代表第 i 轮,每一轮的计算为:[上次计算的结果 + data + salt],直到 digest 之后的结果符合key长度要求
1 2 3 4 5
KEY DERIVATION ALGORITHM The key and IV is derived by concatenating D_1, D_2, etc until enough data is available for the key and IV. D_i is defined as:
D_i = HASH^count(D_(i-1) || data || salt) where || denotes concatentaion, D_0 is empty, HASH is the digest algorithm in use, HASH^1(data) is simply HASH(data), HASH^2(data) is HASH(HASH(data)) and so on.
// https://datatracker.ietf.org/doc/html/rfc5869 A key derivation function (KDF) is a basic and essential component of cryptographic systems. Its goal is to take some source of initial keying material and derive from it one or more cryptographically strong secret keys.
KDF,即密钥派生算法,旨在通过一些初始的密码材料,派生出一个至多个密码学意义上,足够健壮的 密钥
HKDF 包含两个阶段,extract, expand
extract(salt, user_weak_password) => HKDF_struct
// If you don’t have any info to pass, use an empty slice.
HKDF_struct.expand(info) => out_key
In many applications, the input keying material is not necessarily distributed uniformly, and the attacker may have some partial knowledge about it (for example, a Diffie-Hellman value computed by a key exchange protocol) or even partial control of it (as in some entropy-gathering applications). Thus, the goal of the "extract" stage is to "concentrate" the possibly dispersed entropy of the input keying material into a short, but cryptographically strong, pseudorandom key. In some applications, the input may already be a good pseudorandom key; in these cases, the "extract" stage is not necessary, and the "expand" part can be used alone.
The second stage "expands" the pseudorandom key to the desired length; the number and lengths of the output keys depend on the specific cryptographic algorithms for which the keys are needed.
2.2. Step 1: Extract
HKDF-Extract(salt, IKM) -> PRK
Options: Hash a hash function; HashLen denotes the length of the hash function output in octets
Inputs: salt optional salt value (a non-secret random value); if not provided, it is set to a string of HashLen zeros. IKM input keying material
Output: PRK a pseudorandom key (of HashLen octets)
The output PRK is calculated as follows:
PRK = HMAC-Hash(salt, IKM)
2.3. Step 2: Expand
HKDF-Expand(PRK, info, L) -> OKM
Options: Hash a hash function; HashLen denotes the length of the hash function output in octets
Krawczyk & Eronen Informational [Page 3]
RFC 5869 Extract-and-Expand HKDF May 2010
Inputs: PRK a pseudorandom key of at least HashLen octets (usually, the output from the extract step) info optional context and application specific information (can be a zero-length string) L length of output keying material in octets (<= 255*HashLen)
Output: OKM output keying material (of L octets)
The output OKM is calculated as follows:
N = ceil(L/HashLen) T = T(1) | T(2) | T(3) | ... | T(N) OKM = first L octets of T