Repository navigation
feat(encryption): AEAD content encryption algorithms — AES-GCM, AES-CCM, ChaCha20/Poly1305 (RFC 9053 §4) (#199) - #209
Merged
Conversation
) 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
force-pushed
the
feat/issue-199-aead-content-encryption
branch
from
September 11, 2026 20:05
6d7ba94 to
fcab67b
Compare
This was referenced Sep 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Target branch: 4.9.x
Resolves issue #199
Part of #202
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\ContentEncryptionA128GCM/A192GCM/A256GCMA128CCM_16_64/A256CCM_16_64A128CCM_64_64/A256CCM_64_64ChaCha20Poly1305A128CCM_16_128/A256CCM_16_128A128CCM_64_128/A256CCM_64_128ContentEncryption extends Algorithm:encrypt(SymmetricKey, string $plaintext, string $nonce, string $aad): string(ciphertext ‖ tag),decrypt(...), pluskeyLength(),nonceLength(),tagLength().Aeadis 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.openssl_encrypt('aes-*-gcm|ccm', …, $tag, $aad, $tagLength); ChaCha20/Poly1305 throughsodium_crypto_aead_chacha20poly1305_ietf_*whenext-sodiumis loaded,chacha20-poly1305otherwise (both paths tested, the OpenSSL one by disabling the sodium functions).AesGcm::isSupported(),AesCcm::isSupported(),ChaCha20Poly1305::isSupported()answer for the platform.A<key>CCM_<L>_<tag>); the IANA name orders the numbers asAES-CCM-<L>-<tag>-<key>. Both are in the docs.Structures and nonce
Encrypt0Structure/EncryptStructuregainencrypt(ContentEncryption, SymmetricKey, string $plaintext, string $nonce)anddecrypt(...), which hand theEnc_structure(RFC 9052 §5.3) to the algorithm as AAD.Cose\Encryption\InitializationVector:resolve(CoseHeaders, int $length, ?Key $key)returns theIV(5) as is, or completes thePartial IV(6) with theBase IVof the key per RFC 9052 §3.1 (left-pad the Partial IV, XOR with the Base IV as a prefix — the reading that reproducesh'89F52F65A1C5809300000061A7'of Appendix C.4.2); a layer carrying both parameters is rejected, as is anIVof the wrong length.fromPartialIv()is the sender side.Key restrictions
KeyRestrictionEnforcement. These algorithms enforcealg/key_opsby 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 inAead::__construct()if you would rather keep it uniform.encryptorwrap keywhen encrypting", "decryptorunwrap keywhen decrypting". NewKey::assertUsableWithAny(int $alg, int $operation, int ...$alternatives);assertUsableWith()now delegates to it (same messages as before for a single operation). The trait's privatecheckKeyRestrictions()takes the alternatives.cose-wg/Examples harness (#192)
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 — tooutput.cborbyte for byte; every*-fail-*fixture is rejected. TheEnc_structureis compared with the recordedAAD_hexfirst, so a failure names what diverged (structure vs. primitive), as for signatures and MACs.aes-gcm-05,RFC8152/Appendix_C_4_2) resolve the nonce throughInitializationVectorand compare it with the unsent IV the generator recorded.chacha-poly-examples/chacha-poly-enc-01records itsAAD_hexunder 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 newKNOWN_ERRATAtable: the intermediate comparison is skipped, the message is verified in full, andCoseWgHarnessTest::aKnownErratumStillVerifies()fails the day upstream fixes the file. The fixture itself is untouched.Docs and examples
examples/04-encrypt0.php: A128GCM throughEncrypt0Structure, theIV, then thePartial IV+Base IVvariant.examples/05-encrypt-recipients.php: content layer throughEncryptStructure; the recipient layer still wraps the CEK by hand — AES Key Wrap (RFC 3394, checked against the §4.1 vector at run time) underalg -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).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 recommendsrandom_bytes()per message or a monotonicPartial 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 reproducesoutput.cborbyte for byte (CoseWgFixtureTest)AeadTest::aContentThatDoesNotAuthenticateIsRefusedTheSameWayWhateverTheReason, 9 cases × 12 algorithms)AeadTest::aTwelveByteNonceIsRejectedByAesCcm16)IVandPartial IVpresent 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_opsin 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-wrapdependency. 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.xafter #204–#208 landed: the algorithm tables follow the Reference column convention of #193 andRfcReferencesTestknows the twelve new identifiers.