{"id":487,"date":"2026-11-13T13:39:23","date_gmt":"2026-11-13T05:39:23","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-chacha20-expands-a-key-into-a-keystream-block\/"},"modified":"2026-11-13T21:58:51","modified_gmt":"2026-11-13T13:58:51","slug":"how-chacha20-expands-a-key-into-a-keystream-block","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-chacha20-expands-a-key-into-a-keystream-block\/","title":{"rendered":"How ChaCha20 expands a key into a keystream block"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>ChaCha20<\/strong> turns a 32-byte key, a 12-byte nonce, and a 32-bit block counter into a 64-byte keystream block. The state is sixteen little-endian words: the constant <code>expand 32-byte k<\/code>, the key, the counter, and the nonce. Ten double-rounds mix it. Each double-round is four column quarter-rounds and four diagonal quarter-rounds. The keystream block is the mixed state plus the original state. The plaintext is xored with that block.<\/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 stream cipher has to stretch a short secret and a nonce into a long pad that decryption can rebuild. ChaCha20 does that with an add-rotate-xor permutation, not with a block cipher. The quarter-round rotates by 16, 12, 8, and 7. Those distances are the ChaCha quarter-round. Salsa20 uses 7, 9, 13, and 18. Some libraries pack the counter and the 12-byte nonce into one 16-byte value. This layout keeps the counter as its own word, which is the RFC 8439 layout.<\/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\">Each triple of key, nonce, and counter produces one 64-byte block. The same triple reproduces the block, which is what decryption needs. A repeated counter under the same key and nonce repeats the keystream, so the xor of the two ciphertexts is the xor of the two plaintexts. The construction does not authenticate the ciphertext.<\/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>Load the constant, the key, the counter, and the nonce as sixteen little-endian words.<\/li>\n<li>Ten times, run the column quarter-rounds and then the diagonal quarter-rounds.<\/li>\n<li>Add each mixed word to the original word.<\/li>\n<li>Xor the resulting 64 bytes with the next 64 plaintext bytes and increment the counter.<\/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 when a nonce is reused under one key<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Reuse a nonce and restart the counter<\/td>\n<td>The keystream repeats. The xor of the ciphertexts cancels the pad.<\/td>\n<\/tr>\n<tr>\n<td>Use the Salsa20 rotation distances in this state<\/td>\n<td>ChaCha20 is defined with rotations of 16, 12, 8, and 7. The other distances are a different function.<\/td>\n<\/tr>\n<tr>\n<td>Skip the final addition of the original state<\/td>\n<td>The published keystream is the sum. The mixed state alone does not match the RFC block.<\/td>\n<\/tr>\n<tr>\n<td>Treat the keystream xor as an authenticated encryption<\/td>\n<td>A flipped ciphertext bit flips the matching plaintext bit. Nothing in the xor rejects it.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">Poly1305 is the one-time authenticator paired with this keystream in ChaCha20-Poly1305. The fragment is the keystream.<\/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 expands the RFC 8439 key, nonce, and counter into the first keystream block. It prints <code>10f1e7e4d13b5915500fdd1fa32071c4c7d1f4c733c068030422aa9ac3d46c4ed2826446079faa0914c2d705d98b02a2b5129cd1de164eb9cbd083e8a2503c4e<\/code>.<\/p>\n\n<pre><code class=\"language-python\">import struct\n\ndef _rol32(x: int, n: int) -&gt; int:\n    return ((x &lt;&lt; n) | (x &gt;&gt; (32 - n))) &amp; 0xFFFFFFFF\n\ndef _quarter(a: int, b: int, c: int, d: int) -&gt; tuple[int, int, int, int]:\n    a = (a + b) &amp; 0xFFFFFFFF\n    d = _rol32(d ^ a, 16)\n    c = (c + d) &amp; 0xFFFFFFFF\n    b = _rol32(b ^ c, 12)\n    a = (a + b) &amp; 0xFFFFFFFF\n    d = _rol32(d ^ a, 8)\n    c = (c + d) &amp; 0xFFFFFFFF\n    b = _rol32(b ^ c, 7)\n    return a, b, c, d\n\ndef chacha20_block(key: bytes, counter: int, nonce: bytes) -&gt; bytes:\n    state = list(struct.unpack(&quot;&lt;4I&quot;, b&quot;expand 32-byte k&quot;))\n    state += list(struct.unpack(&quot;&lt;8I&quot;, key))\n    state.append(counter &amp; 0xFFFFFFFF)\n    state += list(struct.unpack(&quot;&lt;3I&quot;, nonce))\n    w = state[:]\n    for _ in range(10):\n        w[0], w[4], w[8], w[12] = _quarter(w[0], w[4], w[8], w[12])\n        w[1], w[5], w[9], w[13] = _quarter(w[1], w[5], w[9], w[13])\n        w[2], w[6], w[10], w[14] = _quarter(w[2], w[6], w[10], w[14])\n        w[3], w[7], w[11], w[15] = _quarter(w[3], w[7], w[11], w[15])\n        w[0], w[5], w[10], w[15] = _quarter(w[0], w[5], w[10], w[15])\n        w[1], w[6], w[11], w[12] = _quarter(w[1], w[6], w[11], w[12])\n        w[2], w[7], w[8], w[13] = _quarter(w[2], w[7], w[8], w[13])\n        w[3], w[4], w[9], w[14] = _quarter(w[3], w[4], w[9], w[14])\n    out = [(w[i] + state[i]) &amp; 0xFFFFFFFF for i in range(16)]\n    return struct.pack(&quot;&lt;16I&quot;, *out)\n\ndef chacha20_xor(key: bytes, counter: int, nonce: bytes, data: bytes) -&gt; bytes:\n    out = bytearray()\n    for off in range(0, len(data), 64):\n        block = chacha20_block(key, counter, nonce)\n        chunk = data[off : off + 64]\n        out.extend(x ^ y for x, y in zip(chunk, block))\n        counter += 1\n    return bytes(out)\n\nkey = bytes(range(32))\nnonce = bytes.fromhex(&quot;000000090000004a00000000&quot;)\nstream = chacha20_block(key, 1, nonce)\nassert chacha20_xor(key, 1, nonce, bytes(64)) == stream\nassert stream.hex() == &quot;10f1e7e4d13b5915500fdd1fa32071c4c7d1f4c733c068030422aa9ac3d46c4ed2826446079faa0914c2d705d98b02a2b5129cd1de164eb9cbd083e8a2503c4e&quot;\nprint(stream.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\"><strong>WireGuard<\/strong>, OpenSSH, and TLS cipher suites use ChaCha20, almost always as ChaCha20-Poly1305. Google adopted it for phones without fast AES hardware. A repeated nonce under one key repeats the keystream.<\/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 ChaCha20 where the same nonce can occur twice under one key, and drop the bare xor where a modified ciphertext must be rejected. Keep the keystream where each nonce is used once and a one-time authenticator covers the ciphertext.<\/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 ChaCha20 returns the quarter-round and the RFC 8439 keystream block. <strong>ChaCha20 mixes a constant, a 32-byte key, a counter, and a 12-byte nonce for twenty rounds, then adds the original state to make a 64-byte keystream block.<\/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: ChaCha20 turns a 32-byte key, a 12-byte nonce, and a 32-bit block counter into a 64-byte keystream block. The state is sixteen little-endian words: the constant expand 32-byte k, the key, the counter, and the nonce. Ten double-rounds mix it. Each double-round is four column quarter-rounds and&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-487","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\/487","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=487"}],"version-history":[{"count":1,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/487\/revisions"}],"predecessor-version":[{"id":699,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/487\/revisions\/699"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=487"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=487"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}