|
9 | 9 |
|
10 | 10 | This library implements: |
11 | 11 | - **[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) |
14 | 14 | - **[RFC 8230](https://datatracker.ietf.org/doc/html/rfc8230)** - RSASSA-PSS (PS256, PS384, PS512) and the RSA key type |
15 | 15 | - **[RFC 8812](https://datatracker.ietf.org/doc/html/rfc8812)** - RSASSA-PKCS1-v1_5 (RS256, RS384, RS512, RS1) and |
16 | 16 | ECDSA with secp256k1 (ES256K) |
@@ -53,6 +53,9 @@ row, so that a shipped identifier can be traced to its specification without lea |
53 | 53 | - **Signatures**: ECDSA (ES256, ES384, ES512, ES256K), EdDSA (Ed25519, Ed448), RSA (RS256/384/512, PS256/384/512) |
54 | 54 | - **Fully-specified identifiers** ([RFC 9864](https://www.rfc-editor.org/rfc/rfc9864.html)): ESP256/384/512, ESB256/320/384/512, Ed25519, Ed448 |
55 | 55 | - **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) |
56 | 59 | - Compatible with WebAuthn, FIDO2, and digital COVID certificates |
57 | 60 |
|
58 | 61 | ✅ **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 |
402 | 405 | an `InvalidArgumentException` before OpenSSL is reached — RFC 9053 §3.2 ties the key length to the identifier, so it |
403 | 406 | is the wrong key rather than a weak one. `AesCbcMac::keyLength()` and `tagLength()` give the lengths in bytes. |
404 | 407 |
|
| 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 | +
|
405 | 473 | #### The HMAC Key |
406 | 474 |
|
407 | 475 | [RFC 9053, section 3.1](https://www.rfc-editor.org/rfc/rfc9053#section-3.1) requires implementations "creating and |
|
0 commit comments