Repository navigation
feat(algorithms): ML-DSA-44/65/87 and the AKP key type via OpenSSL 3.5 (RFC 9964) (#214) - #223
Merged
Merged
Conversation
15 tasks done
Spomky
force-pushed
the
feat/issue-214-ml-dsa
branch
from
September 12, 2026 14:02
7b9a7fc to
9641c37
Compare
…5 (RFC 9964)
Cose\Algorithm\Signature\MLDSA\{MLDSA44, MLDSA65, MLDSA87} (-48, -49,
-50) implement Signature over the AKP key type: pure ML-DSA (FIPS 204
algorithm 2) with the empty context string, which is all RFC 9964
allows; no HashML-DSA, no non-empty ctx. The computation is OpenSSL's,
through the digest-less openssl_sign() of PHP 8.4; isSupported() gates
on the PHP version and on a runtime probe (the all-zero-seed key of the
RFC is loaded once per process), because OPENSSL_VERSION_TEXT names the
headers PHP was built against, not the library it loaded. create()
throws naming the missing piece, like Ed448 and the Brainpool ESB*.
Cose\Key\AkpKey is the Algorithm Key Pair type of RFC 9964 section 3:
kty 7 (Key::TYPE_AKP, name "AKP", dispatched by Key::createFromData()),
pub (-1), priv (-2). For ML-DSA, priv is the 32-byte seed and nothing
else (section 4 excludes the expanded private key) and pub the encoded
public key of the parameter set; both sizes are checked against alg
when the key is built (section 7.3). asPEM() writes the RFC 9881 forms:
a seed-only PrivateKeyInfo, a SubjectPublicKeyInfo.
The algorithm refuses an AKP key without alg - REQUIRED by section 3,
since the type says nothing about the algorithm - and one of another
parameter set, whether or not the key restrictions are enforced; when
the key carries both halves, pub is recomputed from the seed and
compared with hash_equals() (section 7.4). A signature of another
length is invalid before OpenSSL is called. keyPairFromSeed() expands a
seed into the key pair.
Thumbprint::canonicalForm() gains the AKP case of section 6 - kty, alg,
pub - and PublicKeyLoader reads the ML-DSA OIDs of RFC 9881 into an
AkpKey carrying the alg they name.
…s for ML-DSA tests/fixtures/rfc9964: the six examples of RFC 9964 Appendix A as the RFC prints them (appendix-a.json), the three COSE_Sign1 examples in the cose-wg/Examples schema with a broken twin each (ml-dsa-examples/), and vectors produced with the OpenSSL 3.5 command line from fixed seeds (openssl-cli/), plus an X.509 certificate holding the ML-DSA-44 key, issued by a throw-away P-256 CA. tests/fixtures/nist-acvp/ml-dsa: the ML-DSA-keyGen-FIPS204 seed-to-public-key cases and the pure-mode, empty-context cases of ML-DSA-sigGen-FIPS204 (release v1.1.0.43). Each directory has its extractor and a README recording the provenance.
…oth sides MLDSATest runs the three parameter sets against the RFC 9964 examples (the seed expands to the public key the RFC prints, the signature the RFC prints verifies, the kid is the thumbprint), the OpenSSL CLI vectors and the NIST ACVP cases; every key check (no alg, another parameter set, another key type, a mismatched pair, wrong sizes, a public key signing); a signature of another length; the key restrictions. On a platform without ML-DSA it asserts isSupported() false and the create() message naming the missing piece, and COSE_ML_DSA_EXPECTED lets the CI say which side it expects. AkpKeyTest covers the type, the ML-DSA size checks, the PEM forms byte for byte against OpenSSL's. ThumbprintTest, PublicKeyLoaderTest, CertificateSignatureVerifierTest, VerificationContractTest, AlgorithmsTest and RfcReferencesTest gain the AKP and ML-DSA cases. The cose-wg harness learns the AKP key type and the ML-DSA names, and Rfc9964FixtureTest puts the RFC's COSE_Sign1 messages through it.
The phpqa images ship OpenSSL 3.5, so the matrix already covers the PHP side of the gate (closed on 8.2 and 8.3, open on 8.4 and 8.5); each job now says which through COSE_ML_DSA_EXPECTED. A new job runs PHP 8.4 on the OpenSSL 3.0 of a pinned ubuntu-24.04 runner for the OpenSSL side.
README and doc/Usage.md gain an ML-DSA section (the three identifiers, the key, what is checked before OpenSSL is called, the platform gate), the AKP row of the key type and thumbprint tables, and RFC 9964 in every list of implemented RFCs; RELEASES.md describes the feature; the composer keywords name the RFC. examples/14-ml-dsa.php reproduces the COSE example of the RFC, thumbprint included, signs a COSE_Sign1 with a fresh ML-DSA-65 key and shows what is refused, and where.
Spomky
force-pushed
the
feat/issue-214-ml-dsa
branch
from
September 12, 2026 17:55
9641c37 to
959c2ea
Compare
Spomky
added a commit
that referenced
this pull request
Sep 13, 2026
doc/Usage.md (2600 lines) becomes fourteen chapters under doc/, with doc/README.md as the index: Installation, Messages, Signing, Countersignatures, Mac, Encryption, KeyManagement, Cwt, X509, HashEnvelope, Receipts, Algorithms, Keys and Upgrading. README.md keeps the introduction, the list of RFCs, the chapter table and the points to know before relying on the library; the algorithm and key tables live in doc/Algorithms.md and doc/Keys.md only, and RfcReferencesTest now checks them there — and checks that no other document carries a copy. The chapters cover everything merged since the split was started: RFC 9995 (hash envelope, #220), RFC 9338 (countersignatures, #222), RFC 9964 (ML-DSA and AKP keys, #223) and RFC 9942 (receipts, #221), each on its own page or in the Algorithms and Keys chapters, with the header table, the structures table, the doc index and the platform-gate table updated accordingly. RELEASES.md links point at the new chapters and its support matrix lists the 4.9.x line only.
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 #214 (Closes #214)
Part of #202
What
ML-DSA-44, ML-DSA-65 and ML-DSA-87 (-48, -49, -50) of RFC 9964, the first post-quantum signatures of the COSE registry, over the new AKP key type (
kty7). Everything is additive: new classes, new constants, one newmatcharm inKey::createFromData()and one inThumbprint::canonicalForm(), one new OID family inPublicKeyLoader. No existing public signature changes, no new dependency: the crypto is OpenSSL's, throughext-openssl.The algorithms —
Cose\Algorithm\Signature\MLDSAMLDSA(abstract) andMLDSA44,MLDSA65,MLDSA87, implementingSignatureandKeyRestrictionAwarelike every signature algorithm, withAlgorithms::COSE_ALGORITHM_ML_DSA_44/65/87.ctx):openssl_sign()/openssl_verify()with digest0. The provider refuses any digest ("invalid digest"), which is what keeps HashML-DSA out (§7.2); a non-emptyctxcannot be expressed through PHP at all (§5).openssl_sign()) and an OpenSSL runtime with ML-DSA.OPENSSL_VERSION_TEXTreports the headers PHP was compiled against (3.0.13 on a machine running 3.5.5), so the probe loads the all-zero-seed ML-DSA-44 PrivateKeyInfo of the RFC — 52 bytes — once per process.isSupported()is shared by the three classes;create()throws aRuntimeExceptionnaming the missing piece (PHP version or OpenSSL library), likeEd448and the BrainpoolESB*.false(FIPS 204 algorithm 3, step 1).keyPairFromSeed(string $seed): AkpKey— the seed is the only private key representation §4 allows, so this is how a key is generated (random_bytes(32)) and how a stored seed is rebuilt; OpenSSL does the expansion.The key type —
Cose\Key\AkpKeykty7 (Key::TYPE_AKP, nameAKP, dispatched byKey::createFromData()as the integer, the'7'string cbor-php produces, or the name),pub(-1,DATA_PUB),priv(-2,DATA_PRIV);pub(),priv(),isPrivate(),toPublic(),asPEM().algis required by the type (§3): the class accepts its absence, so that a map read from the wire can be inspected, and every consumer — the algorithms,Thumbprint::of(),asPEM()— refuses the key. A non-integeralgis refused at construction.privMUST be 32 bytes — the expanded private key (2560/4032/4896) is refused, as the issue asked — andpubMUST be 1312 / 1952 / 2592 for thealgcarried. An AKP key naming another algorithm is built without size checks (§3: the type is "for use with algorithms not limited to those registered in this document").asPEM()writes the RFC 9881 forms, byte for byte whatopenssl genpkey … -provparam ml-dsa.output_formats=seed-onlyandopenssl pkey -puboutwrite: a PrivateKeyInfo with theseed [0] OCTET STRINGchoice, a SubjectPublicKeyInfo with the encoded public key.What the algorithm checks, point by point of the issue
alg→InvalidArgumentExceptionon both operations.algmismatch → refused whether or not the key restrictions are enforced (for this type,algis whatcrvis to an EC2 key); with enforcement on, the RFC 9052 §7.1 message names the restriction.key_opsenforced as everywhere else.pubis recomputed from the seed and compared withhash_equals(); refused onsign()and onverify().Keyofkty7 is turned into anAkpKey, so every check above applies to a stored credential too; a key of another type is refused by name.Thumbprint and loader
Thumbprint::canonicalForm()gains the AKP case of §6:kty(1),alg(3),pub(-1) —alg, which no other key type includes. An AKP key withoutalgthrows. Thekidof the RFC's own examples is reproduced: the issue expected it could not be, because the figure of §3 truncatespub, but Appendix A.2 prints the whole key, and the threekidvalues (ML-DSA-44/65/87) are the thumbprints, byte for byte — so are the JWK thumbprints of A.1.PublicKeyLoader::fromSubjectPublicKeyInfo()/fromCertificate()read the three ML-DSA OIDs of RFC 9881 into anAkpKeycarrying thealgthe OID names;AkpKeyrefuses asubjectPublicKeywhose length does not match the OID. A certificate holding an ML-DSA key and issued by a classical CA is read; a certificate signed with ML-DSA is not — spomky-labs/pki-framework 1.6 throws "Unsupported signature algorithm 2.16.840.1.101.3.4.3.17" (documented in the README, the guide and the release notes; that is a pki-framework feature, not this library's).Registration
ManagerFactoryholds no built-in list (nothing to register there); the documented pattern, used by the cose-wg harness, example 14 and the guide, is the conditional one of the Brainpool curves:Acceptance criteria of #214
MLDSATest::aSignatureMadeWithAFreshKeyPairVerifies,VerificationContractTest(malformed, truncated, over-long, flipped, all-zero, random signatures, no PHP diagnostic),CertificateSignatureVerifierTestwith the ML-DSA-44 certificate, the cose-wg harness on the RFC'sCOSE_Sign1messages (Rfc9964FixtureTest: Sig_structure rebuilt byte for byte, verified, signed again).tests/fixtures/rfc9964/openssl-cli/(generate.sh, OpenSSL 3.5.5): the seed expands to the committedpub, the signature verifies, the PEM forms are OpenSSL's byte for byte.tests/fixtures/nist-acvp/ml-dsa/(release v1.1.0.43, SHA-256 of the source files in the README): thekeyGenseed → public key cases (10 per set) and everysigGencase of the external interface in pure mode with an empty context (8, deterministic and hedged). ThesigVerfile of that release has no pure-mode case with an empty context — every one carries a randomctx, whichopenssl_verify()cannot be given — so it is not used; the README says so.priv≠ 32 bytes,pubof the wrong length,pubnot matching the seed rejected before any OpenSSL call —AkpKeyTest::aMalformedKeyIsRejectedWithTheDocumentedException(the constructor never calls OpenSSL),MLDSATest::aPublicKeyThatDoesNotMatchTheSeedIsRefused.MLDSATest::aSignatureOfAnotherLengthIsInvalid(one byte short, one byte long, empty, all-zero),theAcvpSignatureVerifies(one bit flipped, one byte appended), the six*-sig-fail-01fixtures.ThumbprintTest::theKidOfTheRfc9964ExampleIsItsThumbprint(the threekidvalues reproduced),theThumbprintOfAnAkpKeyDependsOnKtyAlgAndPubAlone(kid,key_ops,priv, the spelling ofktyandalgmake no difference; anotheralgis another key),anAkpKeyWithoutAlgorithmHasNoThumbprint, the canonical-form test extended to a negative integer value.isSupported()is false,create()throws, the rest of the suite is unaffected —MLDSATest::theAlgorithmCannotBeBuiltWhereThePlatformLacksItchecks the message names PHP or OpenSSL; verified on thephpqa:8.3image (PHP gate) and onphp:8.4-cli-bookworm, OpenSSL 3.0.20 (OpenSSL gate).CI
The phpqa images ship OpenSSL 3.5.6, so the existing matrix covers the PHP side of the gate: closed on 8.2 and 8.3, open on 8.4 and 8.5. Each job now says which through
COSE_ML_DSA_EXPECTED, asserted byMLDSATest::thePlatformIsTheOneTheBuildExpects, so that a platform drifting to the other side fails instead of silently losing coverage. A new job, PHP 8.4 – OpenSSL 3.0, no ML-DSA, runs on a pinnedubuntu-24.04runner (OpenSSL 3.0.13) withshivammathur/setup-php@2.37.2and the PHPUnit 11 PHAR, for the OpenSSL side. The workflow audit passes.Fixtures
tests/fixtures/rfc9964/—extract.pyparses Appendix A of the RFC text intoappendix-a.json(the six examples, re-joined across the line wrapping) and intoml-dsa-examples/(the threeCOSE_Sign1in the cose-wg schema, plus a fail twin with the last signature byte flipped);openssl-cli/as above, plusml-dsa-44-certificate.pem.tests/fixtures/nist-acvp/ml-dsa/—extract.py,keygen.json,siggen.json, README with the release, commit and checksums. NIST data is public domain; acknowledged in the README.pub,priv, andalgresolved through the names table) and the three ML-DSA names.Docs
README (This library implements, Features, new ML-DSA section, the AKP row of the key type and thumbprint tables, Testing, Requirements, Documentation),
doc/Usage.md(intro, TOC, new ML-DSA section — keys, signing, what is checked before OpenSSL is called, the platform gate — key types, thumbprints, examples, references),RELEASES.md,composer.jsonkeywords,examples/16-ml-dsa.php(the RFC example reproduced, thumbprint included; a fresh ML-DSA-65 key signing aCOSE_Sign1; each refusal; the gate on a platform without ML-DSA).Checks
Rebased on
4.9.xafter #222 (the example is16-ml-dsa.php, 14 and 15 being taken by #220 and #222).phpstan(level max, no baseline change: the new classes are clean),ecs,rector,deptrac, workflow audit: clean. PHPUnit: 2871 tests, 60 skipped on the 8.5 image (the 3 fail-fixture table skips and the unsetCOSE_ML_DSA_EXPECTEDare the new ones); 2855 / 155 on the 8.3 image withCOSE_ML_DSA_EXPECTED=no; the ML-DSA, key, thumbprint, loader, harness and example tests pass onphp:8.4-cli-bookworm(OpenSSL 3.0) withCOSE_ML_DSA_EXPECTED=no.Not in this PR
HashML-DSA and a non-empty
ctx(RFC 9964 §5, §7.2), the expanded private key form (§4), ML-KEM / HPKE and SLH-DSA (no code point), reading a certificate signed with ML-DSA (pki-framework), and any built-in registry inManagerFactory.