{"id":495,"date":"2026-11-13T13:39:37","date_gmt":"2026-11-13T05:39:37","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-gcm-authenticates-ciphertext-with-ghash\/"},"modified":"2026-11-13T21:59:00","modified_gmt":"2026-11-13T13:59:00","slug":"how-gcm-authenticates-ciphertext-with-ghash","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-gcm-authenticates-ciphertext-with-ghash\/","title":{"rendered":"How GCM authenticates ciphertext with GHASH"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>GCM<\/strong> encrypts with counter mode and authenticates with GHASH. <code>H<\/code> is the AES encryption of the zero block. For a 12-byte nonce, <code>J0<\/code> is the nonce followed by <code>00000001<\/code>. Encryption starts at the next counter. GHASH multiplies each ciphertext block by <code>H<\/code> in GF(2^128). The last field element carries the bit lengths of the additional data and the ciphertext. The tag is GHASH xored with the encryption of <code>J0<\/code>.<\/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\">Counter mode hides a message and does not detect a change. GCM adds a tag so a receiver can reject a ciphertext that was not produced with the key. The additional authenticated data is covered by the tag and is not encrypted. The fragment uses an empty additional-data string and a 12-byte nonce, which is the usual nonce length. A nonce under one key is used once. Repeating it repeats the counter keystream and breaks the authenticator. The fragment does not show how to exploit that repeat. It shows the tag.<\/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\">A receiver who recomputes the tag and compares it in full accepts only a ciphertext whose GHASH matches. The comparison has to be the whole tag. Encryption is counter mode, so confidentiality still depends on unique counters. The field multiplication uses the GCM polynomial, with the reduction constant <code>0xe1<\/code> followed by 120 zero bits in the bit ordering the implementation shifts. An empty message still has a tag, because the length block is hashed.<\/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>Encrypt the zero block to obtain <code>H<\/code>.<\/li>\n<li>Form <code>J0<\/code> as the 12-byte nonce followed by <code>00000001<\/code>.<\/li>\n<li>Encrypt the successive counters, starting at <code>J0 + 1<\/code>, and xor them with the plaintext.<\/li>\n<li>Hash the additional data, the ciphertext, and the two length fields by multiplying by <code>H<\/code>.<\/li>\n<li>Xor the hash with the encryption of <code>J0<\/code> to obtain the tag.<\/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 repeated under one key<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Reuse a nonce for a second message<\/td>\n<td>The counter keystream repeats, and the authenticator is no longer binding. Each nonce is used once.<\/td>\n<\/tr>\n<tr>\n<td>Compare only the first few tag bytes<\/td>\n<td>The check is the whole tag. A shortened comparison is a different check.<\/td>\n<\/tr>\n<tr>\n<td>Hash the plaintext instead of the ciphertext<\/td>\n<td>GHASH covers the ciphertext and the additional data. Hashing the plaintext is a different authenticator.<\/td>\n<\/tr>\n<tr>\n<td>Build <code>J0<\/code> by appending the counter for a nonce that is not 12 bytes<\/td>\n<td>The fragment defines <code>J0<\/code> for a 12-byte nonce. Other lengths hash the nonce first.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">CBC and bare counter mode leave the ciphertext unauthenticated. GCM is the authenticated combination.<\/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 encrypts <code>hello<\/code> under the all-zero AES-128 key and a 12-byte zero nonce, then computes the GHASH tag. It prints <code>6bedb6a20f fc05f3475d29034caa1cc8fa3628c39f<\/code>.<\/p>\n\n<pre><code class=\"language-python\">def _xtime(a: int) -&gt; int:\n    return ((a &lt;&lt; 1) ^ 0x1B) &amp; 0xFF if a &amp; 0x80 else (a &lt;&lt; 1) &amp; 0xFF\n\ndef _gmul(a: int, b: int) -&gt; int:\n    p = 0\n    for _ in range(8):\n        if b &amp; 1:\n            p ^= a\n        a = _xtime(a)\n        b &gt;&gt;= 1\n    return p\n\ndef _sbox() -&gt; list[int]:\n    box = []\n    for x in range(256):\n        inv = 0 if x == 0 else next(i for i in range(1, 256) if _gmul(x, i) == 1)\n        y = 0\n        for i in range(8):\n            bit = (inv &gt;&gt; i) ^ (inv &gt;&gt; ((i + 4) % 8)) ^ (inv &gt;&gt; ((i + 5) % 8))\n            bit ^= (inv &gt;&gt; ((i + 6) % 8)) ^ (inv &gt;&gt; ((i + 7) % 8)) ^ (0x63 &gt;&gt; i)\n            y |= (bit &amp; 1) &lt;&lt; i\n        box.append(y)\n    return box\n\n_S = _sbox()\n\ndef aes128_encrypt(block: bytes, key: bytes) -&gt; bytes:\n    words = list(key)\n    rcon = 1\n    while len(words) &lt; 176:\n        t = words[-4:]\n        if len(words) % 16 == 0:\n            t = t[1:] + t[:1]\n            t = [_S[b] for b in t]\n            t[0] ^= rcon\n            rcon = _xtime(rcon)\n        earlier = words[-16:-12]\n        words.extend(earlier[i] ^ t[i] for i in range(4))\n    state = list(block)\n\n    def add(rnd: int) -&gt; None:\n        rk = words[rnd * 16 : (rnd + 1) * 16]\n        for i in range(16):\n            state[i] ^= rk[i]\n\n    def sub() -&gt; None:\n        for i in range(16):\n            state[i] = _S[state[i]]\n\n    def shift() -&gt; None:\n        for r in range(1, 4):\n            row = [state[r + 4 * c] for c in range(4)]\n            row = row[r:] + row[:r]\n            for c in range(4):\n                state[r + 4 * c] = row[c]\n\n    def mix() -&gt; None:\n        for c in range(4):\n            col = state[4 * c : 4 * c + 4]\n            state[4 * c] = _gmul(col[0], 2) ^ _gmul(col[1], 3) ^ col[2] ^ col[3]\n            state[4 * c + 1] = col[0] ^ _gmul(col[1], 2) ^ _gmul(col[2], 3) ^ col[3]\n            state[4 * c + 2] = col[0] ^ col[1] ^ _gmul(col[2], 2) ^ _gmul(col[3], 3)\n            state[4 * c + 3] = _gmul(col[0], 3) ^ col[1] ^ col[2] ^ _gmul(col[3], 2)\n\n    add(0)\n    for rnd in range(1, 10):\n        sub()\n        shift()\n        mix()\n        add(rnd)\n    sub()\n    shift()\n    add(10)\n    return bytes(state)\n\ndef _inc32(block: bytes) -&gt; bytes:\n    n = int.from_bytes(block[-4:], &quot;big&quot;)\n    return block[:-4] + ((n + 1) &amp; 0xFFFFFFFF).to_bytes(4, &quot;big&quot;)\n\ndef _ghash_mul(x: int, y: int) -&gt; int:\n    z = 0\n    for i in range(128):\n        if (x &gt;&gt; (127 - i)) &amp; 1:\n            z ^= y\n        if y &amp; 1:\n            y = (y &gt;&gt; 1) ^ 0xE1000000000000000000000000000000\n        else:\n            y &gt;&gt;= 1\n    return z\n\ndef aes_gcm(key: bytes, iv: bytes, plaintext: bytes, aad: bytes = b&quot;&quot;) -&gt; tuple[bytes, bytes]:\n    h = int.from_bytes(aes128_encrypt(b&quot;\\x00&quot; * 16, key), &quot;big&quot;)\n    if len(iv) == 12:\n        j0 = iv + b&quot;\\x00\\x00\\x00\\x01&quot;\n    else:\n        raise ValueError(&quot;this example uses a 96-bit nonce&quot;)\n    counter = _inc32(j0)\n    stream = b&quot;&quot;\n    while len(stream) &lt; len(plaintext):\n        stream += aes128_encrypt(counter, key)\n        counter = _inc32(counter)\n    ct = bytes(p ^ s for p, s in zip(plaintext, stream))\n\n    def blocks(buf: bytes) -&gt; list[int]:\n        if not buf:\n            return []\n        buf += b&quot;\\x00&quot; * ((16 - len(buf) % 16) % 16)\n        return [int.from_bytes(buf[i : i + 16], &quot;big&quot;) for i in range(0, len(buf), 16)]\n\n    y = 0\n    for block in blocks(aad) + blocks(ct):\n        y = _ghash_mul(y ^ block, h)\n    lengths = (len(aad) * 8 &lt;&lt; 64) | (len(ct) * 8)\n    y = _ghash_mul(y ^ lengths, h)\n    tag = (y ^ int.from_bytes(aes128_encrypt(j0, key), &quot;big&quot;)).to_bytes(16, &quot;big&quot;)\n    return ct, tag\n\nkey, iv, text = bytes(16), bytes(12), b&quot;hello&quot;\ncipher, tag = aes_gcm(key, iv, text)\nassert cipher.hex() == &quot;6bedb6a20f&quot; and tag.hex() == &quot;fc05f3475d29034caa1cc8fa3628c39f&quot;\nprint(cipher.hex(), tag.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>TLS 1.3<\/strong> and IPsec authenticate ciphertext with AES-GCM. HTTPS on a current browser is usually this mode: counter-mode encryption plus a GHASH tag. A nonce under one key is used once.<\/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 GCM where a nonce might be reused under one key. Keep it where the implementation stores enough state, or draws a large enough random nonce, that each nonce occurs once.<\/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 GCM returns GHASH, the 96-bit nonce layout, and the tag. <strong>GCM encrypts with a counter that starts just after the nonce block and tags the ciphertext by multiplying by the encryption of the zero 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: GCM encrypts with counter mode and authenticates with GHASH. H is the AES encryption of the zero block. For a 12-byte nonce, J0 is the nonce followed by 00000001. Encryption starts at the next counter. GHASH multiplies each ciphertext block by H in GF(2^128). The last field&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-495","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\/495","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=495"}],"version-history":[{"count":1,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/495\/revisions"}],"predecessor-version":[{"id":707,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/495\/revisions\/707"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=495"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}