{"id":496,"date":"2026-11-13T13:39:39","date_gmt":"2026-11-13T05:39:39","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-hkdf-extracts-and-expands-a-shared-secret\/"},"modified":"2026-11-13T21:59:02","modified_gmt":"2026-11-13T13:59:02","slug":"how-hkdf-extracts-and-expands-a-shared-secret","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-hkdf-extracts-and-expands-a-shared-secret\/","title":{"rendered":"How HKDF extracts and expands a shared secret"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>HKDF-SHA-256<\/strong> turns a shared secret into key bytes of a chosen length. Extract sets the pseudorandom key to HMAC of the salt and the input keying material. Expand appends HMAC of that key, the previous block, the info string, and a counter, starting from an empty previous block and a counter of 1. An empty salt is replaced by 32 zero bytes.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">A Diffie-Hellman result is a group element, not yet a set of symmetric keys. HKDF extracts a key from it and expands that key into the lengths the next algorithms ask for. The info string separates two uses of the same shared secret, such as a sending key and a receiving key. HKDF does not stretch a password. The input is assumed to have the entropy of a fresh shared secret. A password goes through PBKDF2 or scrypt first.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">The same input, salt, info, and length reproduce the same bytes. A different info string produces different bytes from the same extract step. The RFC 5869 case uses 22 bytes of <code>0x0b<\/code>, a 13-byte salt, a 10-byte info string, and a 42-byte output. The fragment matches that case. The extract step is one HMAC. The expand step is one HMAC per 32 bytes of output.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>If the salt is empty, use 32 zero bytes.<\/li>\n<li>Set the pseudorandom key to HMAC-SHA-256 of the salt and the input.<\/li>\n<li>Set the previous block to empty and the counter to 1.<\/li>\n<li>Append HMAC of the pseudorandom key and <code>previous || info || counter<\/code>, then increment the counter, until the requested length is reached.<\/li>\n<\/ol>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails as a password hash<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Feed a password straight into HKDF<\/td>\n<td>Extract-and-expand does not add an iteration cost or a memory cost. A password needs that cost.<\/td>\n<\/tr>\n<tr>\n<td>Use one info string for two different keys<\/td>\n<td>The info string is the label that separates the outputs. The same label repeats the output.<\/td>\n<\/tr>\n<tr>\n<td>Skip extract and expand the raw shared secret<\/td>\n<td>The two steps are the construction. Expanding the raw secret is a different function.<\/td>\n<\/tr>\n<tr>\n<td>Treat the output as an authenticator of a message<\/td>\n<td>HKDF derives keys. HMAC authenticates a message.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">PBKDF2 stretches a password. HKDF derives keys from a secret that already has entropy.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment runs the RFC 5869 HKDF-SHA-256 test case. It prints <code>3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865<\/code>.<\/p>\n\n<pre><code class=\"language-python\">import struct\n\ndef _primes(k: int) -&gt; list[int]:\n    out: list[int] = []\n    n = 2\n    while len(out) &lt; k:\n        if all(n % p for p in out if p * p &lt;= n):\n            out.append(n)\n        n += 1\n    return out\n\ndef _isqrt(n: int) -&gt; int:\n    x = int(n ** 0.5)\n    while x * x &gt; n:\n        x -= 1\n    while (x + 1) * (x + 1) &lt;= n:\n        x += 1\n    return x\n\ndef _icbrt(n: int) -&gt; int:\n    x = int(round(n ** (1 \/ 3)))\n    while x * x * x &gt; n:\n        x -= 1\n    while (x + 1) ** 3 &lt;= n:\n        x += 1\n    return x\n\n_PS = _primes(64)\n_H256 = [_isqrt(p &lt;&lt; 64) &amp; 0xFFFFFFFF for p in _PS[:8]]\n_K256 = [_icbrt(p &lt;&lt; 96) &amp; 0xFFFFFFFF for p in _PS]\n\ndef _rotr(x: int, n: int) -&gt; int:\n    return ((x &gt;&gt; n) | (x &lt;&lt; (32 - n))) &amp; 0xFFFFFFFF\n\ndef sha256(data: bytes) -&gt; bytes:\n    h = _H256[:]\n    bit_len = len(data) * 8\n    msg = data + b&quot;\\x80&quot;\n    msg += b&quot;\\x00&quot; * ((56 - len(msg) % 64) % 64)\n    msg += struct.pack(&quot;&gt;Q&quot;, bit_len)\n    for off in range(0, len(msg), 64):\n        w = list(struct.unpack(&quot;&gt;16I&quot;, msg[off : off + 64]))\n        for i in range(16, 64):\n            s0 = _rotr(w[i - 15], 7) ^ _rotr(w[i - 15], 18) ^ (w[i - 15] &gt;&gt; 3)\n            s1 = _rotr(w[i - 2], 17) ^ _rotr(w[i - 2], 19) ^ (w[i - 2] &gt;&gt; 10)\n            w.append((w[i - 16] + s0 + w[i - 7] + s1) &amp; 0xFFFFFFFF)\n        a, b, c, d, e, f, g, hh = h\n        for i in range(64):\n            s1 = _rotr(e, 6) ^ _rotr(e, 11) ^ _rotr(e, 25)\n            ch = (e &amp; f) ^ (~e &amp; g)\n            t1 = (hh + s1 + ch + _K256[i] + w[i]) &amp; 0xFFFFFFFF\n            s0 = _rotr(a, 2) ^ _rotr(a, 13) ^ _rotr(a, 22)\n            maj = (a &amp; b) ^ (a &amp; c) ^ (b &amp; c)\n            t2 = (s0 + maj) &amp; 0xFFFFFFFF\n            hh, g, f, e, d, c, b, a = g, f, e, (d + t1) &amp; 0xFFFFFFFF, c, b, a, (t1 + t2) &amp; 0xFFFFFFFF\n        h = [(x + y) &amp; 0xFFFFFFFF for x, y in zip(h, (a, b, c, d, e, f, g, hh))]\n    return struct.pack(&quot;&gt;8I&quot;, *h)\n\ndef hmac_sha256(key: bytes, msg: bytes) -&gt; bytes:\n    if len(key) &gt; 64:\n        key = sha256(key)\n    key = key.ljust(64, b&quot;\\x00&quot;)\n    outer = bytes(b ^ 0x5C for b in key)\n    inner = bytes(b ^ 0x36 for b in key)\n    return sha256(outer + sha256(inner + msg))\n\ndef hkdf_sha256(ikm: bytes, salt: bytes, info: bytes, length: int) -&gt; bytes:\n    if not salt:\n        salt = b&quot;\\x00&quot; * 32\n    prk = hmac_sha256(salt, ikm)\n    out = b&quot;&quot;\n    block = b&quot;&quot;\n    counter = 1\n    while len(out) &lt; length:\n        block = hmac_sha256(prk, block + info + bytes([counter]))\n        out += block\n        counter += 1\n    return out[:length]\n\nikm = bytes([0x0B]) * 22\nsalt = bytes(range(13))\ninfo = bytes(range(0xF0, 0xFA))\nderived = hkdf_sha256(ikm, salt, info, 42)\nassert derived.hex() == &quot;3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865&quot;\nprint(derived.hex())\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">The <strong>TLS 1.3<\/strong> key schedule and the Signal protocol derive session keys with HKDF. Extract turns the handshake secret into a pseudorandom key; expand labels each output, such as a traffic key. A password does not go straight into HKDF.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop HKDF as a password store. Keep it where a shared secret, typically a Diffie-Hellman result, has to become one or more symmetric keys and the info string labels each use.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for HKDF returns the extract step, the expand step, and the RFC 5869 output. <strong>HKDF-SHA-256 extracts a pseudorandom key with HMAC and expands it into labeled key bytes one counter block at a time.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: HKDF-SHA-256 turns a shared secret into key bytes of a chosen length. Extract sets the pseudorandom key to HMAC of the salt and the input keying material. Expand appends HMAC of that key, the previous block, the info string, and a counter, starting from an empty previous&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,49],"tags":[],"class_list":["post-496","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-cryptography"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/496","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=496"}],"version-history":[{"count":1,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/496\/revisions"}],"predecessor-version":[{"id":708,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/496\/revisions\/708"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=496"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=496"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}