Skip to content

feat(encryption): AEAD content encryption algorithms — AES-GCM, AES-CCM, ChaCha20/Poly1305 (RFC 9053 §4) (#199) - #209

Merged
Spomky merged 1 commit into
4.9.xfrom
feat/issue-199-aead-content-encryption
Sep 11, 2026
Merged

Spomky merged 1 commit into
4.9.xfrom
feat/issue-199-aead-content-encryption

Conversation

@Spomky

@Spomky Spomky commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Target branch: 4.9.x
Resolves issue #199
Part of #202

  • It is a Bug fix
  • It is a New feature
  • Breaks BC
  • Includes Deprecations

What

The content encryption algorithms of RFC 9053 §4, which reverses the "bring your own AEAD" position of examples/04. Everything is additive; no existing signature changes.

Cose\Algorithm\ContentEncryption

Class IANA name Id Key Nonce Tag
A128GCM / A192GCM / A256GCM A128GCM / A192GCM / A256GCM 1 / 2 / 3 128 / 192 / 256 12 B 128
A128CCM_16_64 / A256CCM_16_64 AES-CCM-16-64-128 / -256 10 / 11 128 / 256 13 B 64
A128CCM_64_64 / A256CCM_64_64 AES-CCM-64-64-128 / -256 12 / 13 128 / 256 7 B 64
ChaCha20Poly1305 ChaCha20/Poly1305 24 256 12 B 128
A128CCM_16_128 / A256CCM_16_128 AES-CCM-16-128-128 / -256 30 / 31 128 / 256 13 B 128
A128CCM_64_128 / A256CCM_64_128 AES-CCM-64-128-128 / -256 32 / 33 128 / 256 7 B 128
  • ContentEncryption extends Algorithm: encrypt(SymmetricKey, string $plaintext, string $nonce, string $aad): string (ciphertext ‖ tag), decrypt(...), plus keyLength(), nonceLength(), tagLength().
  • Aead is the shared base: key length check, nonce length check before any OpenSSL call (OpenSSL takes any 7–13 byte nonce for CCM and derives another L from it), tag verification left to the primitive, and one exception/message for every failure to authenticate (Aead::DECRYPTION_FAILED) — a wrong key, a wrong nonce, a wrong AAD, a tampered, foreign or truncated tag are indistinguishable and reported alike.
  • AES-GCM and AES-CCM through openssl_encrypt('aes-*-gcm|ccm', …, $tag, $aad, $tagLength); ChaCha20/Poly1305 through sodium_crypto_aead_chacha20poly1305_ietf_* when ext-sodium is loaded, chacha20-poly1305 otherwise (both paths tested, the OpenSSL one by disabling the sodium functions). AesGcm::isSupported(), AesCcm::isSupported(), ChaCha20Poly1305::isSupported() answer for the platform.
  • The AES-CCM class names follow the JOSE convention of web-token (A<key>CCM_<L>_<tag>); the IANA name orders the numbers as AES-CCM-<L>-<tag>-<key>. Both are in the docs.

Structures and nonce

  • Encrypt0Structure / EncryptStructure gain encrypt(ContentEncryption, SymmetricKey, string $plaintext, string $nonce) and decrypt(...), which hand the Enc_structure (RFC 9052 §5.3) to the algorithm as AAD.
  • New Cose\Encryption\InitializationVector: resolve(CoseHeaders, int $length, ?Key $key) returns the IV (5) as is, or completes the Partial IV (6) with the Base IV of the key per RFC 9052 §3.1 (left-pad the Partial IV, XOR with the Base IV as a prefix — the reading that reproduces h'89F52F65A1C5809300000061A7' of Appendix C.4.2); a layer carrying both parameters is rejected, as is an IV of the wrong length. fromPartialIv() is the sender side.

Key restrictions

  • Per the cross-cutting rule of [Tracking] RFC coverage for the next minor release #202, the key checks go through KeyRestrictionEnforcement. These algorithms enforce alg / key_ops by default: RFC 9053 §4.1–4.3 make it a MUST and the classes are new, so there is no key to keep working. withKeyRestrictionsEnforced(false) turns it off; the signature and MAC algorithms keep their opt-in default. Flip the one line in Aead::__construct() if you would rather keep it uniform.
  • RFC 9053 §4 says "encrypt or wrap key when encrypting", "decrypt or unwrap key when decrypting". New Key::assertUsableWithAny(int $alg, int $operation, int ...$alternatives); assertUsableWith() now delegates to it (same messages as before for a single operation). The trait's private checkKeyRestrictions() takes the alternatives.

cose-wg/Examples harness (#192)

  • The decryption path is in: encrypted-tests/, enveloped-tests/, aes-gcm-examples/, aes-ccm-examples/, chacha-poly-examples/ and the RFC 8152 C.3/C.4 encrypted appendices decrypt to their plaintext, re-encrypt to the ciphertext of the fixture and — headers and recipients kept as decoded — to output.cbor byte for byte; every *-fail-* fixture is rejected. The Enc_structure is compared with the recorded AAD_hex first, so a failure names what diverged (structure vs. primitive), as for signatures and MACs.
  • The two Partial IV fixtures (aes-gcm-05, RFC8152/Appendix_C_4_2) resolve the nonce through InitializationVector and compare it with the unsent IV the generator recorded.
  • chacha-poly-examples/chacha-poly-enc-01 records its AAD_hex under the context "Encrypt1" (upstream typo, still present at HEAD). The ciphertext was computed over "Encrypt0" and decrypts with it, so the fixture is listed in a new KNOWN_ERRATA table: the intermediate comparison is skipped, the message is verified in full, and CoseWgHarnessTest::aKnownErratumStillVerifies() fails the day upstream fixes the file. The fixture itself is untouched.
  • Remaining skips (194) all name a key management algorithm (feat(key management): direct, AES-KW, HKDF, ECDH-ES/SS recipient algorithms (RFC 9053 §5–6) #201).

Docs and examples

  • examples/04-encrypt0.php: A128GCM through Encrypt0Structure, the IV, then the Partial IV + Base IV variant. examples/05-encrypt-recipients.php: content layer through EncryptStructure; the recipient layer still wraps the CEK by hand — AES Key Wrap (RFC 3394, checked against the §4.1 vector at run time) under alg -3, honestly labelled as interim until feat(key management): direct, AES-KW, HKDF, ECDH-ES/SS recipient algorithms (RFC 9053 §5–6) #201 (the previous version wrapped with AES-GCM under the A128KW identifier, which no recipient could have unwrapped).
  • README, doc/Usage.md (new Encryption Operations, The Nonce: IV and Partial IV, Content Encryption Algorithms sections; key restrictions updated), examples/README.md, RELEASES.md; the "content encryption is not implemented" paragraphs are gone. Each algorithm's doc states the nonce-reuse hazard (GCM/ChaCha leak the authentication key, CCM the XOR of plaintexts) and recommends random_bytes() per message or a monotonic Partial IV; the 7-byte-nonce CCM variants are pointed at a counter.

Acceptance criteria of #199

  • encrypted-tests/, aes-gcm-examples/, aes-ccm-examples/, chacha-poly-examples/ decrypt to the expected plaintext, and encrypting with the fixture's nonce and key reproduces output.cbor byte for byte (CoseWgFixtureTest)
  • A truncated, tampered or foreign tag fails with the same exception type and message as a wrong key (AeadTest::aContentThatDoesNotAuthenticateIsRefusedTheSameWayWhateverTheReason, 9 cases × 12 algorithms)
  • A 12-byte nonce given to AES-CCM-16-64-128 is rejected before any OpenSSL call (AeadTest::aTwelveByteNonceIsRejectedByAesCcm16)
  • IV and Partial IV present together are rejected (InitializationVectorTest::anIvAndAPartialIvInTheSameLayerAreRejected)

Also covered: the published vectors of the primitives (GCM spec test cases 4 and 16, RFC 3610 packet vector #1, RFC 8439 §2.8.2), key_ops in both spellings and both names, the manager-wide enforcement switch, empty plaintext / empty AAD, PHP 8.2–8.5.

Checks

castor phpstan (level max, no baseline change), ecs, rector, deptrac, parallel-lint, composer validate --strict, dump-autoload --strict-psr: clean. PHPUnit: 2166 tests, 194 skipped (all key-management fixtures), on the 8.2, 8.4 and 8.5 images.

Not in this PR

Key management (#201), the spomky-labs/aes-key-wrap dependency. Nothing here gets a deprecation notice or an acknowledgement flag: no identifier of RFC 9053 §4 is Deprecated or Filter Only at IANA.

Rebased on 4.9.x after #204–#208 landed: the algorithm tables follow the Reference column convention of #193 and RfcReferencesTest knows the twelve new identifiers.

@Spomky Spomky added this to the 4.9.0 milestone Sep 11, 2026
@Spomky Spomky added enhancement New feature or request compliance Issue related to the compliance of the project with the specs algorithms Signature, MAC, encryption and key-management algorithms documentation README, doc/ and examples/ tests Test suite, fixtures and interoperability vectors labels Sep 11, 2026
@Spomky Spomky self-assigned this Sep 11, 2026
)

Add the content encryption algorithms of RFC 9053 section 4 under
Cose\Algorithm\ContentEncryption: A128GCM, A192GCM, A256GCM (§4.1), the
eight AES-CCM variants A128CCM_16_64 … A256CCM_64_128 (§4.2) and
ChaCha20Poly1305 (§4.3, sodium when loaded, OpenSSL otherwise), behind the
ContentEncryption interface. The ciphertext is laid out as COSE carries it,
content then tag; the key length and the nonce length fixed by each
identifier are checked before the primitive runs, so that OpenSSL never
derives another L for AES-CCM; and every decryption that does not
authenticate is reported with one InvalidArgumentException and one message.

Encrypt0Structure and EncryptStructure gain encrypt() and decrypt(), which
hand the Enc_structure of RFC 9052 section 5.3 to the algorithm as AAD.
Cose\Encryption\InitializationVector resolves the nonce from the "IV" or
the "Partial IV" header parameter and the "Base IV" of the key (RFC 9052
section 3.1), rejecting a layer that carries both.

The alg and key_ops restrictions of the key are enforced by default for
these algorithms, RFC 9053 section 4 making the checks a MUST and the
classes having no caller to keep compatible. Key::assertUsableWithAny()
accepts an operation under either of the names the RFC allows ("encrypt"
or "wrap key", "decrypt" or "unwrap key").

The cose-wg/Examples harness gains its decryption path: the
encrypted-tests, enveloped-tests, aes-gcm, aes-ccm and chacha-poly fixtures
decrypt to their plaintext, encrypt back to output.cbor byte for byte, and
the fail fixtures are rejected. chacha-poly-enc-01 records its AAD under
the context "Encrypt1"; the ciphertext decrypts under "Encrypt0", so the
intermediate is listed as a known erratum and the message is verified.

examples/04 and 05 use the new classes instead of calling OpenSSL by hand;
README, doc/Usage.md and RELEASES.md document the algorithms, the nonce
rules and the nonce-reuse hazard.
@Spomky
Spomky force-pushed the feat/issue-199-aead-content-encryption branch from 6d7ba94 to fcab67b Compare September 11, 2026 20:05
@Spomky
Spomky merged commit 9e8ab10 into 4.9.x Sep 11, 2026
17 checks passed
@Spomky
Spomky deleted the feat/issue-199-aead-content-encryption branch September 11, 2026 20:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

algorithms Signature, MAC, encryption and key-management algorithms compliance Issue related to the compliance of the project with the specs documentation README, doc/ and examples/ enhancement New feature or request tests Test suite, fixtures and interoperability vectors

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(encryption): AEAD content encryption algorithms — AES-GCM, AES-CCM, ChaCha20/Poly1305 (RFC 9053 §4)

1 participant