Skip to content

feat(keys): COSE Key Thumbprint (RFC 9679) and compressed EC2 points (#197) - #211

Merged
Spomky merged 1 commit into
4.9.xfrom
feat/issue-197-key-thumbprint
Sep 11, 2026
Merged

Spomky merged 1 commit into
4.9.xfrom
feat/issue-197-key-thumbprint

Conversation

@Spomky

@Spomky Spomky commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Target branch: 4.9.x
Resolves issue #197 (Closes #197)
Part of #202

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

What

The COSE Key Thumbprint of RFC 9679, on top of the hash registry of #195 (PR #210), and the compressed EC2 point form it needs. Everything is additive; no existing public signature changes and a key carrying y as a byte string is handled exactly as before.

Cose\Key\Thumbprint

The issue offered a class or a Key::thumbprint() method; this is the class, as a small value object, with nothing added to Key.

Member What
Thumbprint::of(Key $key, Hash $hash = new SHA256()) The thumbprint, per §3
Thumbprint::canonicalForm(Key $key): string The CBOR bytes the digest is computed over (steps 1–2 of §3), for cross-checks
value(): string The raw digest: a kid, or the value of a ckt (§5.6)
hash(): Hash The algorithm it was computed with
equals(string $thumbprint): bool hash_equals()
toUri(): string urn:ietf:params:oauth:ckt:<hash>:<base64url, unpadded> (§5.7)
Thumbprint::URI_PREFIX urn:ietf:params:oauth:ckt
  • The canonical form is built from scratch, never re-encoded from the source map: a map of the required members of §4 — OKP kty/crv/x, EC2 kty/crv/x/y, RSA kty/n/e, Symmetric kty/k — written by a private 40-line encoder (integers and lengths in shortest form, keys sorted bytewise on their encodings, RFC 8949 §4.2.1). No runtime dependency on cbor-php, which stays require-dev; the test suite cross-checks the encoder against CBOR\MapObject for every key type, including a Brainpool crv (two-byte argument) and an RSA modulus (two-byte length).
  • kty and crv are always the registry integers, whatever the key names them under ('EC', 'EC2', '2', 'P-256', …). The y of an EC2 key is always the uncompressed coordinate (§4.2). The x of an OKP private key that carries none is the recomputed one, so a d-only Ed25519 key has the thumbprint of its public key.
  • The hash parameter is typed Hash, not FilterOnlyHash: SHA-1 and SHA-256/64 are refused at the type level, as the issue asks. Default SHA-256 (§3: "MUST be supported").
  • URI hash names come from the IANA Named Information Hash Algorithm Registry, the only source §5.7 allows: sha-256, sha-384, sha-512. SHA-512/256, SHAKE128 and SHAKE256 have no entry there, so toUri() throws for them; value() works for all six.
  • A generic Key of another type (HSS-LMS 5, AKP 7) is refused with the type in the message; §4.6 defers those to their own specifications, and RFC 8778 is out of scope of the milestone.
  • The §7 warning on symmetric keys (a thumbprint is a digest of the secret; never for passwords or low-entropy secrets) is on the class, in the README, in doc/Usage.md and in the example.

Compressed EC2 points

RFC 9053 §7.1.1 lets a public EC2 key carry y as the sign bit of the point, a CBOR boolean; Ec2Key used to reject it as an "invalid type", and RFC 9679 §4.2 requires the thumbprint to be computed over the uncompressed point anyway. So:

  • Ec2Key accepts y as bool and decompresses on load: y = (x³ + ax + b)^((p+1)/4) mod p, a single modPow through Cose\BigInteger / brick/math, valid because all eight supported curves have p ≡ 3 (mod 4). The candidate is squared and compared with the right-hand side, so an x that lies on no point is rejected (Invalid EC2 key. The compressed point is not on the curve); an x ≥ p is rejected too (SEC 1 §2.3.4 step 2.2); the β = 0 corner case (whose only other root is 0) is handled rather than returning p. The sign bit is the parity of y (SEC 1 §2.3.3: true = odd).
  • y(), getUncompressedCoordinates() and asPEM() return the coordinate whatever form was supplied — so signing and verifying with a compressed key just works; getData() keeps the boolean, so the map round-trips unchanged.
  • The curve parameters (p, a, b) of the eight curves are constants on Ec2Key, from SEC 2, FIPS 186-4 and RFC 5639; I checked every one of them against openssl ecparam -param_enc explicit, and the test suite checks them the practical way (below).
  • PublicKeyLoader reads a compressed subjectPublicKey (0x02 / 0x03, RFC 5480 §2.2), which it used to refuse by name, and hands back a key carrying the uncompressed point. The hybrid form (0x06 / 0x07) is still refused.
  • Cose\BigInteger (internal) gains shiftRight(), isOdd() and isZero().

Cost: ~0.1–4 ms per compressed key on the QA image (GMP); brick/math's pure-PHP calculator is slower but this is a one-time cost at load.

Acceptance criteria of #197

  • The worked example of RFC 9679 §6 passes byte for byte, including the intermediate canonical CBOR (ThumbprintTest::theExampleOfRfc9679IsReproduced, examples/12-key-thumbprint.php); the §5.7 URI urn:ietf:params:oauth:ckt:sha-256:SWvYr63zB-WwjGSwQhv53AFSijRKQ72oj63RZp2iU-w too
  • Two encodings differing in member order, in extra members (kid, alg, key_ops, d), in kty/crv as text vs int vs numeric string, or in y as bool vs bstr produce the same thumbprint (everyRepresentationOfAKeyHasTheSameThumbprint, 8 representations)
  • A compressed y that does not yield a point on the curve is rejected on load (anXThatIsOnNoPointOfTheCurveIsRejected: the smallest off-curve x of each of the 8 curves, both sign bits)
  • Private and public variants of the same key give the same thumbprint (aPrivateKeyHasTheThumbprintOfItsPublicKey: P-256, secp256k1, brainpoolP256r1, Ed25519, Ed448, RSA; plus the d-only OKP case)
  • README, doc/Usage.md, examples/ updated

Tests

  • tests/Key/ThumbprintTest.php (11 methods, 30 cases): the RFC vector; the representations; the canonical form vs cbor-php for OKP / EC2 / EC2-Brainpool / RSA / Symmetric; private = public; d-only OKP; SHA-384 / SHA-512 with their URI names; SHA-512/256 / SHAKE refused a URI; SHA-1 refused as a TypeError; equals(); unknown kty; two keys sharing x differ.
  • tests/Key/Ec2KeyCompressedPointTest.php (11 methods, 51 cases): for each of the 8 curves, a fresh OpenSSL key loaded from x + parity equals the OpenSSL key (y(), DER, PEM, getData() keeps the bool) — which is what validates the curve constants; the opposite sign bit gives the other root; a named curve; a compressed private key signs with ES256 and its public half verifies; off-curve x per curve; x ≥ p; wrong x length; y as int/float/array still refused; compressed SPKI loaded for each curve and equal to the uncompressed load; off-curve SPKI refused; hybrid SPKI refused. Curves this OpenSSL build lacks skip.
  • RfcReferencesTest knows RFC 9679; ExamplesTest runs examples/12-key-thumbprint.php.

Docs

README (This library implements, Features, Documentation, Key Parameter Forms for the sign bit, new Key Thumbprints section with the §4 table), doc/Usage.md (same, plus TOC and References; the examples table also gains the missing rows for examples 10 and 11), RELEASES.md (4.8.x → 4.9.x), composer.json keyword RFC9679, examples/12-key-thumbprint.php + index. One stale PHPStan baseline entry (Ec2Key::y() returning mixed) removed since y() now returns a typed property.

Checks

phpstan (level max), ecs, rector, deptrac, parallel-lint, composer validate --strict, dump-autoload --strict-psr, workflow audit: clean. PHPUnit: 2318 tests on the 8.4 image (194 skipped, unchanged: the key-management fixtures of #201), 2313 on 8.2 (215 skipped, unchanged); none of the 81 new tests skips on either.

Not in this PR

HSS-LMS (kty 5, §4.5) and AKP (kty 7) thumbprints, which need their key classes (RFC 8778 and RFC 9964, out of scope of #202); parsing a thumbprint URI back into its parts; x5t (#196), which #201 needs next to this.

…197)

Cose\Key\Thumbprint computes the RFC 9679 thumbprint over a COSE_Key rebuilt
from the required parameters of the key type in the deterministic encoding of
RFC 8949 section 4.2.1, with the section 5.7 URI for the hashes the IANA Named
Information registry names. Ec2Key accepts y as the sign bit of a compressed
point and decompresses it on load for all eight curves; PublicKeyLoader reads
a compressed subjectPublicKey the same way.
@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 keys COSE_Key types, parameters and thumbprints documentation README, doc/ and examples/ tests Test suite, fixtures and interoperability vectors labels Sep 11, 2026
@Spomky Spomky self-assigned this Sep 11, 2026
@Spomky
Spomky merged commit 1cd3905 into 4.9.x Sep 11, 2026
17 checks passed
@Spomky
Spomky deleted the feat/issue-197-key-thumbprint branch September 11, 2026 20:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compliance Issue related to the compliance of the project with the specs documentation README, doc/ and examples/ enhancement New feature or request keys COSE_Key types, parameters and thumbprints tests Test suite, fixtures and interoperability vectors

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(keys): COSE Key Thumbprint (RFC 9679) and compressed EC2 points

1 participant