Skip to content

Commit fcab67b

Browse files
committed
feat(encryption): AEAD content encryption algorithms (RFC 9053 §4) (#199)
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.
1 parent 5b9de06 commit fcab67b

41 files changed

Lines changed: 2969 additions & 179 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎README.md‎

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
This library implements:
1111
- **[RFC 9052](https://datatracker.ietf.org/doc/html/rfc9052)** - COSE: Structures and Process
12-
- **[RFC 9053](https://datatracker.ietf.org/doc/html/rfc9053)** - COSE: Initial Algorithms (signatures, HMAC, AES-CBC-MAC and the
13-
key types)
12+
- **[RFC 9053](https://datatracker.ietf.org/doc/html/rfc9053)** - COSE: Initial Algorithms (signatures, HMAC, AES-CBC-MAC, the
13+
AEAD content encryption algorithms and the key types)
1414
- **[RFC 8230](https://datatracker.ietf.org/doc/html/rfc8230)** - RSASSA-PSS (PS256, PS384, PS512) and the RSA key type
1515
- **[RFC 8812](https://datatracker.ietf.org/doc/html/rfc8812)** - RSASSA-PKCS1-v1_5 (RS256, RS384, RS512, RS1) and
1616
ECDSA with secp256k1 (ES256K)
@@ -53,6 +53,9 @@ row, so that a shipped identifier can be traced to its specification without lea
5353
- **Signatures**: ECDSA (ES256, ES384, ES512, ES256K), EdDSA (Ed25519, Ed448), RSA (RS256/384/512, PS256/384/512)
5454
- **Fully-specified identifiers** ([RFC 9864](https://www.rfc-editor.org/rfc/rfc9864.html)): ESP256/384/512, ESB256/320/384/512, Ed25519, Ed448
5555
- **MAC**: HMAC with SHA-256/384/512, AES-CBC-MAC with 128/256-bit keys and 64/128-bit tags
56+
- **Content encryption** ([RFC 9053 §4](https://www.rfc-editor.org/rfc/rfc9053.html#section-4)): AES-GCM (128/192/256),
57+
the eight AES-CCM variants, ChaCha20/Poly1305 — through the `Enc_structure`, with the `IV` / `Partial IV` resolution
58+
of [RFC 9052 §3.1](https://www.rfc-editor.org/rfc/rfc9052.html#section-3.1)
5659
- Compatible with WebAuthn, FIDO2, and digital COVID certificates
5760

5861
✅ **Key Restrictions** ([RFC 9052](https://www.rfc-editor.org/rfc/rfc9052.html#section-7.1) §7.1)
@@ -402,6 +405,71 @@ for AES-MAC 128/64 and 128/128, 32 bytes for AES-MAC 256/64 and 256/128. A key o
402405
an `InvalidArgumentException` before OpenSSL is reached — RFC 9053 §3.2 ties the key length to the identifier, so it
403406
is the wrong key rather than a weak one. `AesCbcMac::keyLength()` and `tagLength()` give the lengths in bytes.
404407

408+
### Content Encryption Algorithms
409+
410+
The AEAD algorithms of [RFC 9053 §4](https://www.rfc-editor.org/rfc/rfc9053.html#section-4), in
411+
`Cose\Algorithm\ContentEncryption`. The ciphertext is the encrypted content followed by the tag, as COSE carries it.
412+
413+
| Algorithm | Identifier | Class | Key | Nonce | Tag | Reference |
414+
|-----------|------------|-------|-----|-------|-----|-----------|
415+
| A128GCM | 1 | `A128GCM` | 128 bits | 12 bytes | 128 bits | [RFC 9053 §4.1](https://www.rfc-editor.org/rfc/rfc9053#section-4.1) |
416+
| A192GCM | 2 | `A192GCM` | 192 bits | 12 bytes | 128 bits | [RFC 9053 §4.1](https://www.rfc-editor.org/rfc/rfc9053#section-4.1) |
417+
| A256GCM | 3 | `A256GCM` | 256 bits | 12 bytes | 128 bits | [RFC 9053 §4.1](https://www.rfc-editor.org/rfc/rfc9053#section-4.1) |
418+
| AES-CCM-16-64-128 | 10 | `A128CCM_16_64` | 128 bits | 13 bytes | 64 bits | [RFC 9053 §4.2](https://www.rfc-editor.org/rfc/rfc9053#section-4.2) |
419+
| AES-CCM-16-64-256 | 11 | `A256CCM_16_64` | 256 bits | 13 bytes | 64 bits | [RFC 9053 §4.2](https://www.rfc-editor.org/rfc/rfc9053#section-4.2) |
420+
| AES-CCM-64-64-128 | 12 | `A128CCM_64_64` | 128 bits | 7 bytes | 64 bits | [RFC 9053 §4.2](https://www.rfc-editor.org/rfc/rfc9053#section-4.2) |
421+
| AES-CCM-64-64-256 | 13 | `A256CCM_64_64` | 256 bits | 7 bytes | 64 bits | [RFC 9053 §4.2](https://www.rfc-editor.org/rfc/rfc9053#section-4.2) |
422+
| ChaCha20/Poly1305 | 24 | `ChaCha20Poly1305` | 256 bits | 12 bytes | 128 bits | [RFC 9053 §4.3](https://www.rfc-editor.org/rfc/rfc9053#section-4.3) |
423+
| AES-CCM-16-128-128 | 30 | `A128CCM_16_128` | 128 bits | 13 bytes | 128 bits | [RFC 9053 §4.2](https://www.rfc-editor.org/rfc/rfc9053#section-4.2) |
424+
| AES-CCM-16-128-256 | 31 | `A256CCM_16_128` | 256 bits | 13 bytes | 128 bits | [RFC 9053 §4.2](https://www.rfc-editor.org/rfc/rfc9053#section-4.2) |
425+
| AES-CCM-64-128-128 | 32 | `A128CCM_64_128` | 128 bits | 7 bytes | 128 bits | [RFC 9053 §4.2](https://www.rfc-editor.org/rfc/rfc9053#section-4.2) |
426+
| AES-CCM-64-128-256 | 33 | `A256CCM_64_128` | 256 bits | 7 bytes | 128 bits | [RFC 9053 §4.2](https://www.rfc-editor.org/rfc/rfc9053#section-4.2) |
427+
428+
```php
429+
use CBOR\ByteStringObject;
430+
use CBOR\ListObject;
431+
use CBOR\MapItem;
432+
use CBOR\MapObject;
433+
use CBOR\Tag\CoseEncrypt0Tag;
434+
use CBOR\UnsignedIntegerObject;
435+
use Cose\Algorithm\ContentEncryption\A128GCM;
436+
use Cose\Encryption\Encrypt0Structure;
437+
use Cose\Encryption\InitializationVector;
438+
use Cose\Key\SymmetricKey;
439+
use Cose\Structure\CoseHeaders;
440+
use Cose\Structure\HeaderMapHelper;
441+
442+
$algorithm = A128GCM::create();
443+
$key = SymmetricKey::create([SymmetricKey::TYPE => SymmetricKey::TYPE_OCT, SymmetricKey::DATA_K => $sharedSecret]);
444+
$nonce = random_bytes($algorithm->nonceLength()); // unique per message under this key, RFC 9053 §4.1.1
445+
446+
$protectedHeader = HeaderMapHelper::encodeProtected(MapObject::create([
447+
MapItem::create(UnsignedIntegerObject::create(1), UnsignedIntegerObject::create($algorithm::identifier())),
448+
]));
449+
// The AEAD authenticates the Enc_structure ["Encrypt0", protected, external_aad], RFC 9052 §5.3
450+
$ciphertext = Encrypt0Structure::create($protectedHeader)->encrypt($algorithm, $key, $plaintext, $nonce);
451+
$message = CoseEncrypt0Tag::create(ListObject::create([
452+
$protectedHeader,
453+
MapObject::create([MapItem::create(UnsignedIntegerObject::create(InitializationVector::IV), ByteStringObject::create($nonce))]),
454+
ByteStringObject::create($ciphertext),
455+
]));
456+
457+
// Decrypting: the nonce comes from the "IV", or from a "Partial IV" and the Base IV of the key (RFC 9052 §3.1)
458+
$nonce = InitializationVector::resolve(CoseHeaders::fromMessage($message), $algorithm->nonceLength(), $key);
459+
$plaintext = Encrypt0Structure::create($message->getProtectedHeader())
460+
->decrypt($algorithm, $key, $message->getCiphertext()->getValue(), $nonce); // InvalidArgumentException if it does not authenticate
461+
```
462+
463+
Every algorithm checks the key length and the nonce length before the primitive runs, and enforces the `alg` and
464+
`key_ops` restrictions of the key by default (RFC 9053 §4.1–4.3). AES-CCM and ChaCha20/Poly1305 depend on the
465+
platform: `A128CCM_16_64::isSupported()` and `ChaCha20Poly1305::isSupported()` say. See
466+
[Encryption Operations](doc/Usage.md#encryption-operations) and [`examples/04-encrypt0.php`](examples/04-encrypt0.php).
467+
468+
> [!WARNING]
469+
> A nonce reused under the same key breaks every one of these algorithms: AES-GCM and ChaCha20/Poly1305 give up
470+
> their authentication key, AES-CCM the XOR of the plaintexts. Use `random_bytes()` per message, or a strictly
471+
> increasing counter sent as the `Partial IV`.
472+
405473
#### The HMAC Key
406474

407475
[RFC 9053, section 3.1](https://www.rfc-editor.org/rfc/rfc9053#section-3.1) requires implementations "creating and

‎RELEASES.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@ This matrix is the single source of truth for the branches under support; [SECUR
2626

2727
### 4.8.x to 4.9.x
2828

29+
**Content encryption is implemented.** The AEAD algorithms of RFC 9053 §4 ship in `Cose\Algorithm\ContentEncryption`:
30+
`A128GCM`, `A192GCM`, `A256GCM` (§4.1), the eight AES-CCM variants `A128CCM_16_64` … `A256CCM_64_128` (§4.2) and
31+
`ChaCha20Poly1305` (§4.3), behind the `ContentEncryption` interface. `Encrypt0Structure` and `EncryptStructure` gain
32+
`encrypt()` and `decrypt()`, which hand the `Enc_structure` of RFC 9052 §5.3 to the algorithm as additional
33+
authenticated data, and `Cose\Encryption\InitializationVector` resolves the nonce from the `IV` or the `Partial IV`
34+
header parameter and the `Base IV` of the key (RFC 9052 §3.1). `examples/04-encrypt0.php` and
35+
`examples/05-encrypt-recipients.php` no longer call OpenSSL by hand. Two points to know:
36+
37+
- **These algorithms enforce the `alg` and `key_ops` restrictions of the key by default.** RFC 9053 §4 makes the
38+
check a MUST and the classes are new, so there is no key to keep working. `withKeyRestrictionsEnforced(false)`
39+
turns it off; the signature and MAC algorithms keep their opt-in default.
40+
- **`Key::assertUsableWithAny()`** is the form of `assertUsableWith()` that accepts an operation under several names:
41+
RFC 9053 §4 lets a content encryption key carry `encrypt` or `wrap key`, `decrypt` or `unwrap key`.
42+
43+
The key management algorithms of RFC 9053 §5–6 are not part of this release; see issue #201.
44+
2945
**The interoperability fixtures of the IETF COSE working group are part of the test suite.**
3046
[cose-wg/Examples](https://github.com/cose-wg/Examples) is vendored under `tests/fixtures/cose-wg/`, with a harness
3147
(`tests/CoseWg/`) that verifies every fixture this library has an algorithm for and reports the others as skipped

‎composer.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"suggest": {
4545
"ext-bcmath": "Recommended: without GMP or BCMath, signing with RSASSA-PSS (PS256/PS384/PS512) blinds its private exponentiation in pure PHP",
4646
"ext-gmp": "Recommended: without GMP or BCMath, signing with RSASSA-PSS (PS256/PS384/PS512) blinds its private exponentiation in pure PHP",
47-
"ext-sodium": "Required by the EdDSA/Ed25519 signature algorithms (-8, -19, -260, -261) and to recompute an OKP public key from its private key",
47+
"ext-sodium": "Required by the EdDSA/Ed25519 signature algorithms (-8, -19, -260, -261) and to recompute an OKP public key from its private key; used by ChaCha20/Poly1305 (24) when present, OpenSSL otherwise",
4848
"spomky-labs/cbor-php": "Required by the RFC 9052 header reader and cryptographic structures. 3.4.0 or later: it ships the six COSE message classes (CBOR\\Tag\\CoseSign1Tag and its siblings) that replace the deprecated Cose\\...Tag classes, and its decoder is what enforces the RFC 9052 label uniqueness and nesting bounds this library relies on"
4949
},
5050
"autoload": {

0 commit comments

Comments
 (0)