Repository navigation
feat(algorithms): hash algorithms of RFC 9054 with a type-level Filter Only distinction (#195) - #210
Merged
Merged
Conversation
…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.
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 #195 (Closes #195)
Part of #202
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\HashSHA1FilterOnlyHashSHA256_64FilterOnlyHashSHA256HashSHA512_256HashSHAKE128HashSHA384HashSHA512HashSHAKE256HashFilterOnlyHash extends Algorithm:hash(string $data): string,length(): int(the digest length in bytes — not in the issue, added because RSASSA-PSS needs hLen andx5t/thumbprint validation will need it).Hash extends FilterOnlyHashwith nothing added: the general-purpose one.HashrefusesSHA1andSHA256_64, a parameter typedFilterOnlyHashtakes all eight.FilterOnlyTypeTestruns PHPStan (level max) ontests/fixtures/phpstan/filter-only-hash.phpand expects exactly the two marked calls to be rejected and the six others accepted; it looks forphpstaninPHPSTAN_BINARY,vendor/binand on the PATH (the QA image has it) and skips otherwise.HashAlgorithmsTest::onlyTheRecommendedHashesAreGeneralPurposecovers the hierarchy without PHPStan.hash(); SHA-512/256 through the distinctsha512/256primitive, with a test that it is not the first 32 bytes of SHA-512; SHA-256/64 issubstr(sha256, 0, 8), per RFC 9054 §3.2.Keccak,@internal): 64-bit lanes as PHP integers, rate 168 / 136 bytes, suffix0x1F, pad10*1, outputs fixed at the 256 / 512 bits of the registry.SHAKE128::isSupported()/SHAKE256::isSupported()reportPHP_INT_SIZE >= 8;hash()throws aRuntimeExceptionon 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. AManagerholds the classes like any other algorithm andwithKeyRestrictionsEnforced()carries them over unchanged (a hash takes no key).The fate of
Cose\HashRemoved. It was
@internal, servedPSSRSAalone (getLength()andhash();name()and theDigestInfoprefixt()had no caller), and two "Hash" concepts were ruled out by the issue.PS256/PS384/PS512::getHashAlgorithm()now returnCose\Algorithm\Hash\SHA256/SHA384/SHA512andPSSRSAtakes the newHashinterface; the RSASSA-PSS vectors of the suite (cose-wgrsa-pss-examples,PSSRSATest) are unchanged.RELEASES.mdsays 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 ofAlgorithmsdescribe the digest a signature identifier hands toopenssl_sign()/openssl_verify()(OPENSSL_ALGO_*) or tohash(); SHA-256/64 and the two SHAKE functions have no such value, and SHA-1 inCOSE_HASH_MAPwould 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 theManager, which is whatx5tneeds. 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 examplesSHAKE128_Msg0,SHAKE256_Msg0,SHAKE128_Msg1600,SHAKE256_Msg1600cut to the registry lengths (Msg1600 spans two blocks at either rate); SHA-256/64 as the truncation of the SHA-256 vector;length()vsstrlen(hash()); the hierarchy; aManagerround trip; thex5tof cose-wgx509-examples/signed-05([-16, h'11FA…']) reproduced fromalice.der— a thumbprint made by another implementation.KeccakTest: the sponge vs. PHP's ownsha3-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 ASHA3-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.RfcReferencesTestknows RFC 9054 and the eight rows;ExamplesTestrunsexamples/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.jsonkeywordRFC9054,examples/11-hash-algorithms.php+ index.Acceptance criteria of #195
SHA1andSHA256_64do not implementHash; a PHPStan run over a snippet passing them to aHashparameter fails (FilterOnlyTypeTest, executed in the QA image)Cose\Hashno longer existsChecks
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.