Skip to content

fix(encryption): check the size of an AES key wrapped key before unwrapping it - #799

Merged
Spomky merged 2 commits into
4.3.xfrom
fix/aes-key-wrap-encrypted-key-length
Nov 8, 2026
Merged

Spomky merged 2 commits into
4.3.xfrom
fix/aes-key-wrap-encrypted-key-length

Conversation

@Spomky

@Spomky Spomky commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

The defect

A128KW, A192KW, A256KW, PBES2-HS*+AKW, ECDH-ES+AKW and ECDH-SS+A*KW pass the whole JWE Encrypted Key to spomky-labs/aes-key-wrap. Its unwrap() splits the key into 64-bit blocks and runs six AES decryptions per block, from PHP, before it checks the integrity value. AES Key Wrap adds exactly 64 bits to the key it wraps, so the only size that can succeed is the CEK size plus 8 bytes. JWEDecrypter knows the CEK size, but compared it with the result only after the unwrapping, in checkCekSize().

A token with an oversized encrypted key was therefore rejected only after work and memory proportional to that size, and again for every key tried. The PBES2 key derivation and the ECDH key agreement also ran before the size was looked at. Measured with phpqa:8.4 for a 1 MiB encrypted key and one key (PHP 8.5.12 gives the same order of magnitude):

base branch this branch
A128KW 5.9 s, 17 MB 7 ms, 0.1 MB
PBES2-HS256+A128KW 7.8 s, 17 MB < 1 ms, 0 MB
ECDH-ES+A128KW 7.4 s, 17 MB < 1 ms, 0 MB
ECDH-SS+A128KW 5.9 s, 17 MB < 1 ms, 0 MB

Under memory_limit=128M, a 9 MiB encrypted key ends in a fatal error in AESKW.php on the base branch. This branch rejects it in 2 ms.

The fix

JWEDecrypter::decryptCEK() compares the size of the encrypted key with the CEK size plus 64 bits before it calls an algorithm built on AES Key Wrap (AESKW, PBES2AESKW and AbstractECDHAESKW, so their subclasses too). The check therefore comes before the key derivation, the key agreement and the unwrapping. A mismatch throws the same InvalidArgumentException class as the unwrapping and checkCekSize(), inside the loop over the keys, so decrypt() still returns a failure.

The issue suggested checking in the four algorithms. The check sits in the decrypter instead, for two reasons:

  • unwrapAgreementKey() of ECDH-ES+AKW and ECDH-SS+AKW has always ignored its $encryption_key_length argument, and callers pass sizes that do not match the key they unwrap: the library's own ECDHESKeyAgreementTest and ECDHSSKeyAgreementTest pass 128 or 192 with a 256-bit CEK. A check there would turn those working calls into exceptions.
  • unwrapKey() of A*KW and PBES2 only gets the CEK size as the undeclared fourth argument passed by the decrypter.

The decrypter is the one place that knows the size for all four and already rejected every mismatch. Calling the algorithms directly behaves as before. Once #653 (in #717) declares the size on KeyWrapping::unwrapKey() in 5.0, the check could move into the algorithms.

There is no general bound on the encrypted key in JWEDecrypter. With this check, the other key management modes are already bounded: dir, ECDH-ES and ECDH-SS require an empty encrypted key, RSA-OAEP and RSA1_5 a single block, and A128GCMKW to A256GCMKW decrypt it in one OpenSSL call (1 MiB: 11 ms, and 1 MB for the decrypted key itself).

Behaviour changes

No token changes outcome: every token rejected now was rejected before, and every token decrypted before is still decrypted, with the same key. For the tokens that are rejected:

  • The failure passed to the $onError callable of decrypt() (or the sixth argument of decryptUsingKeySet()) is now InvalidArgumentException: Invalid encrypted key size. So is the failure the loaders chain as the previous exception of "Unable to load and decrypt the token.". It used to be Integrity check failed!, Bad data, Unable to decrypt the data or Invalid CEK size, with the same class.
  • Because the size is checked before each key is used, a key that cannot serve the algorithm (another key type, a short KEK) now also reports the size failure for such a token, where it reported an InvalidKeyException. The same goes for an invalid PBES2 or ECDH header.
  • An empty or missing encrypted key no longer triggers the PHP warning "Undefined array key 0" in aes-key-wrap.
  • The 4.3.0 deprecation of AES-KW keys longer than the algorithm key size is no longer triggered for a token whose key would unwrap to the wrong size, because that key is not unwrapped any more.

Verification

  • AESKeyWrapEncryptedKeySizeTest has 21 cases, all of them failing on the base branch, which also emits the PHP warning:
    • A128KW, A192KW, A256KW, two PBES2 and two of each ECDH variant, with CEK sizes from 128 to 512 bits. The valid token decrypts. The token is rejected with the new failure when the encrypted key is one block longer, one block shorter, one byte longer, empty, or wraps a key meant for another content encryption algorithm (the case where the unwrapping succeeds). On the base branch, the failures are those of the unwrapping and of checkCekSize();
    • a 1 MiB encrypted key is rejected without allocating as much as its size. The base branch allocates 17.8 MB;
    • PBES2: no key derivation runs for such a token (CountingPBES2HS256A128KW);
    • ECDH-ES and ECDH-SS: no key agreement runs. With a recipient key on another curve, the size failure is reported instead of the agreement failure.
  • BC, checked against the base branch:
    • the public and protected API of JWEDecrypter, dumped by reflection, is identical. The new method is private, and no other file of src/ changes;
    • a probe decrypts 3,628 tokens with both versions, through decrypt(), decryptUsingKeySet() and JWELoader. It covers the 12 algorithms built on AES Key Wrap plus a third-party subclass of AESKW, and 9 others (dir, ECDH-ES, ECDH-SS, RSA-OAEP, RSA-OAEP-256, RSA1_5, A128GCMKW, A256GCMKW, and a third-party AES-KW algorithm that does not extend AESKW). Each comes with the six content encryption algorithms, the three serializations, the valid token and eight variants of its encrypted key, plus key sets with unusable keys and a token with two recipients. Outcomes, payloads and keys are identical. The only differences are the ones listed above. Nothing changes for the other algorithms, or for direct calls of unwrapKey() and unwrapAgreementKey().
  • Full suite on PHP 8.4: 2,529 tests green (the base branch has 2,508, with the same 4 skipped and 2 incomplete). ECS, Rector, PHPStan, Deptrac and the license check are clean with the current phpqa:8.4 image.
  • Mutation testing on the diff: 21 mutants, all killed (MSI 100%) with a 120 s timeout. With the 3 s timeout of infection.json.dist, the CI kills 3 and skips the other 18, because the slow encryption tests also cover these lines.
  • Merge-up: the commit applies to 5.0.x without conflict. Its tests pass there once their ECDH-SS cases use the key order that 5.0.x accepts today. The order they use (private key of the recipient, public key of the sender, 830a555) reaches 5.0.x with the next merge-up of 4.3.x.
  • Open pull requests on the same file: fix(encryption): deprecate "dir" JWEs without protected header and without "aad" #796 and fix(encryption): read "key_ops" according to the key management mode #797 merge with this one without conflict. The second commit moves the new method next to checkCekSize() for that reason, as fix(encryption): deprecate "dir" JWEs without protected header and without "aad" #796 adds its own method where this one was. The HPKE draft feat(encryption): HPKE for JWE — Integrated Encryption and Key Encryption (draft-ietf-jose-hpke-encrypt) #740 conflicts, as it already does with the current 4.3.x.

Fixes #771

Found by the Anthropic OSS Scanner (ANT-2026-GFP863VS).

…apping it

A128KW, A192KW, A256KW, PBES2-HS*+A*KW, ECDH-ES+A*KW and ECDH-SS+A*KW
hand the whole encrypted key to spomky-labs/aes-key-wrap, which unwraps
every 64-bit block before checking the integrity value. JWEDecrypter
compared the size of the unwrapped key with the CEK size only afterwards,
so a token was rejected after work and memory proportional to the size
of its encrypted key, for every key tried: several seconds and 17 MB per
megabyte, and a fatal error instead of an exception for a 9 MB encrypted
key under a 128 MB memory limit.

JWEDecrypter now compares the size of the encrypted key with the CEK size
plus 64 bits before calling these algorithms, that is before the key
derivation, the key agreement and the unwrapping. The tokens it rejects
are the ones it already rejected: only the failure passed to the
callable of decrypt() and chained by the loaders changes, and an empty
encrypted key no longer triggers a PHP warning in the key wrapping
library. The algorithms themselves are unchanged when called directly:
unwrapAgreementKey() has always ignored the size it is given, and
callers pass sizes that do not match the key they unwrap.

Reported by Anthropic's OSS Scanner (ANT-2026-GFP863VS).
@Spomky Spomky added this to the 4.3.0 milestone Nov 8, 2026
@Spomky
Spomky merged commit 05d942b into 4.3.x Nov 8, 2026
18 checks passed
@Spomky
Spomky deleted the fix/aes-key-wrap-encrypted-key-length branch November 8, 2026 18:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AES Key Wrap based algorithms unwrap an encrypted key of any length before comparing it with the CEK size

1 participant