Skip to content

feat(algorithms): hash algorithms of RFC 9054 with a type-level Filter Only distinction (#195) - #210

Merged
Spomky merged 1 commit into
4.9.xfrom
feature/195-hash-algorithms
Sep 11, 2026
Merged

Spomky merged 1 commit into
4.9.xfrom
feature/195-hash-algorithms

Conversation

@Spomky

@Spomky Spomky commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

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

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

What

The hash algorithms of RFC 9054, which #196 (x5t), #197 (COSE Key Thumbprint) and later RFC 9995 need. Everything is additive; no existing public signature changes.

Cose\Algorithm\Hash

Class IANA name Id Digest IANA recommendation Implements
SHA1 SHA-1 -14 20 B Filter Only FilterOnlyHash
SHA256_64 SHA-256/64 -15 8 B Filter Only FilterOnlyHash
SHA256 SHA-256 -16 32 B Yes Hash
SHA512_256 SHA-512/256 -17 32 B Yes Hash
SHAKE128 SHAKE128 -18 32 B Yes Hash
SHA384 SHA-384 -43 48 B Yes Hash
SHA512 SHA-512 -44 64 B Yes Hash
SHAKE256 SHAKE256 -45 64 B Yes Hash
  • FilterOnlyHash extends Algorithm: hash(string $data): string, length(): int (the digest length in bytes — not in the issue, added because RSASSA-PSS needs hLen and x5t/thumbprint validation will need it). Hash extends FilterOnlyHash with nothing added: the general-purpose one.
  • Filter Only is a type, not a flag, as the issue asks: a parameter typed Hash refuses SHA1 and SHA256_64, a parameter typed FilterOnlyHash takes all eight. FilterOnlyTypeTest runs PHPStan (level max) on tests/fixtures/phpstan/filter-only-hash.php and expects exactly the two marked calls to be rejected and the six others accepted; it looks for phpstan in PHPSTAN_BINARY, vendor/bin and on the PATH (the QA image has it) and skips otherwise. HashAlgorithmsTest::onlyTheRecommendedHashesAreGeneralPurpose covers the hierarchy without PHPStan.
  • SHA-2 through hash(); SHA-512/256 through the distinct sha512/256 primitive, with a test that it is not the first 32 bytes of SHA-512; SHA-256/64 is substr(sha256, 0, 8), per RFC 9054 §3.2.
  • SHAKE128 / SHAKE256: pure PHP Keccak-p[1600, 24] sponge (Keccak, @internal): 64-bit lanes as PHP integers, rate 168 / 136 bytes, suffix 0x1F, pad10*1, outputs fixed at the 256 / 512 bits of the registry. SHAKE128::isSupported() / SHAKE256::isSupported() report PHP_INT_SIZE >= 8; hash() throws a RuntimeException on a 32-bit build. The sponge takes ~0.2 ms for an empty message and ~3 ms for 2 kB, which is what a certificate or a key costs.
  • Algorithms::COSE_ALGORITHM_SHA_1, _SHA_256_64, _SHA_256, _SHA_512_256, _SHAKE128, _SHA_384, _SHA_512, _SHAKE256. A Manager holds the classes like any other algorithm and withKeyRestrictionsEnforced() carries them over unchanged (a hash takes no key).

The fate of Cose\Hash

Removed. It was @internal, served PSSRSA alone (getLength() and hash(); name() and the DigestInfo prefix t() had no caller), and two "Hash" concepts were ruled out by the issue. PS256/PS384/PS512::getHashAlgorithm() now return Cose\Algorithm\Hash\SHA256/SHA384/SHA512 and PSSRSA takes the new Hash interface; the RSASSA-PSS vectors of the suite (cose-wg rsa-pss-examples, PSSRSATest) are unchanged. RELEASES.md says what to replace for anyone who used the class despite the marker.

One deviation from the issue text

The issue says "constants + COSE_ALGORITHM_MAP". The two maps of Algorithms describe the digest a signature identifier hands to openssl_sign()/openssl_verify() (OPENSSL_ALGO_*) or to hash(); SHA-256/64 and the two SHAKE functions have no such value, and SHA-1 in COSE_HASH_MAP would either bypass or have to extend the RS1 acknowledgement policy for a use that is not a signature. The maps therefore stay signature-only (documented on the constants) and a hash identifier resolves through the Manager, which is what x5t needs. Easy to add later if you want the maps to know them anyway.

No deprecation notice and no acknowledgement flag anywhere: per the cross-cutting rule of #202 and the issue, the Filter Only mark is the type.

Tests

  • HashAlgorithmsTest: identifiers vs. constants vs. ID; FIPS 180-4 "abc" and empty-message vectors for SHA-1 / SHA-2 (the CAVP Len = 24 and Len = 0 anchors); NIST FIPS 202 examples SHAKE128_Msg0, SHAKE256_Msg0, SHAKE128_Msg1600, SHAKE256_Msg1600 cut to the registry lengths (Msg1600 spans two blocks at either rate); SHA-256/64 as the truncation of the SHA-256 vector; length() vs strlen(hash()); the hierarchy; a Manager round trip; the x5t of cose-wg x509-examples/signed-05 ([-16, h'11FA…']) reproduced from alice.der — a thumbprint made by another implementation.
  • KeccakTest: the sponge vs. PHP's own sha3-256 (same permutation, other rate and suffix) for every message length from 0 to 280 — so the padding is exercised one byte short of a block, on a block and one past; FIPS 202 Appendix A SHA3-256("abc"); 200 bytes of SHAKE128 Msg0 and 150 bytes of SHAKE256 Msg1600, past one block of output (the public classes never squeeze twice); prefix consistency; parameter rejection.
  • FilterOnlyTypeTest: see above.
  • RfcReferencesTest knows RFC 9054 and the eight rows; ExamplesTest runs examples/11-hash-algorithms.php.

Docs and examples

README (This library implements, Features, Documentation, new Hash Algorithms table with the IANA recommendation column and the Filter Only explanation), doc/Usage.md (new Hash Algorithms section: the table, Filter Only, as a type, SHAKE128 and SHAKE256, TOC and References), RELEASES.md, composer.json keyword RFC9054, examples/11-hash-algorithms.php + index.

Acceptance criteria of #195

  • Test vectors for the eight algorithms (NIST/FIPS for SHA-2 and SHAKE at the registry output lengths; SHA-256/64 as the truncation of a known SHA-256 vector)
  • SHA1 and SHA256_64 do not implement Hash; a PHPStan run over a snippet passing them to a Hash parameter fails (FilterOnlyTypeTest, executed in the QA image)
  • Cose\Hash no longer exists

Checks

phpstan (level max, no baseline change), ecs, rector, deptrac, parallel-lint, composer validate --strict, dump-autoload --strict-psr: clean. PHPUnit: 2236 tests on the 8.5 image (194 skipped, all key-management fixtures of #201, unchanged), 2231 on 8.2.

Not in this PR

RFC 9861 (TurboSHAKE / KangarooTwelve — Keccak::sponge() takes the rate and the suffix, the round count is the only thing to parameterise), RFC 9995, x5t (#196) and the thumbprint (#197), which this unblocks.

…r Only distinction (#195)

Add the eight hash algorithms of RFC 9054 under Cose\Algorithm\Hash: SHA1
(-14), SHA256_64 (-15), SHA256 (-16), SHA512_256 (-17), SHAKE128 (-18),
SHA384 (-43), SHA512 (-44) and SHAKE256 (-45), each with hash() and
length(), registered by the COSE_ALGORITHM_SHA_* and COSE_ALGORITHM_SHAKE*
constants of Algorithms and held by a Manager like any other algorithm.

The IANA recommendation "Filter Only" of SHA-1 and SHA-256/64 is a type
rather than a runtime flag: every class implements FilterOnlyHash, only the
six IANA recommends implement Hash, so a parameter typed Hash refuses the
two under PHPStan and Psalm and with a TypeError at the call. A test runs
PHPStan on a fixture and expects exactly those two rejections.

SHAKE128 and SHAKE256 have no PHP primitive; a pure PHP Keccak-p[1600, 24]
sponge (Keccak, internal, 64-bit lanes) computes them at the 256 and 512
bits RFC 9054 stores. It is checked against sha3-256 for every message
length around the block boundaries, against the NIST Msg0 and Msg1600
examples, and past one block of output. SHA-512/256 goes through the
distinct sha512/256 primitive, not a truncation of SHA-512.

Cose\Hash, the @internal helper of the RSASSA-PSS code, is removed: PS256,
PS384 and PS512 take their digest from the new SHA256, SHA384 and SHA512
classes, and the signatures are unchanged.

README, doc/Usage.md and RELEASES.md document the algorithms and the type;
examples/11-hash-algorithms.php reproduces the x5t thumbprint of the
cose-wg x509-examples/signed-05 fixture; RfcReferencesTest knows RFC 9054.
@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
@Spomky
Spomky merged commit fcc388b into 4.9.x Sep 11, 2026
17 checks passed
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(algorithms): hash algorithms of RFC 9054 with a type-level Filter Only distinction

1 participant