Skip to content

feat(algorithms): ML-DSA-44/65/87 and the AKP key type via OpenSSL 3.5 (RFC 9964) (#214) - #223

Merged
Spomky merged 5 commits into
4.9.xfrom
feat/issue-214-ml-dsa
Sep 12, 2026
Merged

Spomky merged 5 commits into
4.9.xfrom
feat/issue-214-ml-dsa

Conversation

@Spomky

@Spomky Spomky commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

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

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

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 (kty 7). Everything is additive: new classes, new constants, one new match arm in Key::createFromData() and one in Thumbprint::canonicalForm(), one new OID family in PublicKeyLoader. No existing public signature changes, no new dependency: the crypto is OpenSSL's, through ext-openssl.

The algorithms — Cose\Algorithm\Signature\MLDSA

MLDSA (abstract) and MLDSA44, MLDSA65, MLDSA87, implementing Signature and KeyRestrictionAware like every signature algorithm, with Algorithms::COSE_ALGORITHM_ML_DSA_44/65/87.

  • Pure ML-DSA only (FIPS 204 algorithm 2, empty ctx): openssl_sign() / openssl_verify() with digest 0. The provider refuses any digest ("invalid digest"), which is what keeps HashML-DSA out (§7.2); a non-empty ctx cannot be expressed through PHP at all (§5).
  • Platform gate, both halves at runtime: PHP ≥ 8.4 (the digest-less openssl_sign()) and an OpenSSL runtime with ML-DSA. OPENSSL_VERSION_TEXT reports 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 a RuntimeException naming the missing piece (PHP version or OpenSSL library), like Ed448 and the Brainpool ESB*.
  • Signature length checked before OpenSSL (2420 / 3309 / 4627): any other length is 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\AkpKey

kty 7 (Key::TYPE_AKP, name AKP, dispatched by Key::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().

  • alg is 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-integer alg is refused at construction.
  • ML-DSA sizes checked at construction (§4, §5, §7.3): priv MUST be 32 bytes — the expanded private key (2560/4032/4896) is refused, as the issue asked — and pub MUST be 1312 / 1952 / 2592 for the alg carried. 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 what openssl genpkey … -provparam ml-dsa.output_formats=seed-only and openssl pkey -pubout write: a PrivateKeyInfo with the seed [0] OCTET STRING choice, a SubjectPublicKeyInfo with the encoded public key.

What the algorithm checks, point by point of the issue

  • AKP key without alg → InvalidArgumentException on both operations.
  • alg mismatch → refused whether or not the key restrictions are enforced (for this type, alg is what crv is to an EC2 key); with enforcement on, the RFC 9052 §7.1 message names the restriction. key_ops enforced as everywhere else.
  • §7.4 mismatched parameters: when both halves are present, pub is recomputed from the seed and compared with hash_equals(); refused on sign() and on verify().
  • A generic Key of kty 7 is turned into an AkpKey, 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 without alg throws. The kid of the RFC's own examples is reproduced: the issue expected it could not be, because the figure of §3 truncates pub, but Appendix A.2 prints the whole key, and the three kid values (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 an AkpKey carrying the alg the OID names; AkpKey refuses a subjectPublicKey whose 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

ManagerFactory holds 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:

if (MLDSA44::isSupported()) {
    $manager->add(MLDSA44::create(), MLDSA65::create(), MLDSA87::create());
}

Acceptance criteria of #214

  • Sign / verify round-trip for the three parameter sets — MLDSATest::aSignatureMadeWithAFreshKeyPairVerifies, VerificationContractTest (malformed, truncated, over-long, flipped, all-zero, random signatures, no PHP diagnostic), CertificateSignatureVerifierTest with the ML-DSA-44 certificate, the cose-wg harness on the RFC's COSE_Sign1 messages (Rfc9964FixtureTest: Sig_structure rebuilt byte for byte, verified, signed again).
  • OpenSSL CLI vectors committed with their seed — tests/fixtures/rfc9964/openssl-cli/ (generate.sh, OpenSSL 3.5.5): the seed expands to the committed pub, the signature verifies, the PEM forms are OpenSSL's byte for byte.
  • NIST ACVP vectors — tests/fixtures/nist-acvp/ml-dsa/ (release v1.1.0.43, SHA-256 of the source files in the README): the keyGen seed → public key cases (10 per set) and every sigGen case of the external interface in pure mode with an empty context (8, deterministic and hedged). The sigVer file of that release has no pure-mode case with an empty context — every one carries a random ctx, which openssl_verify() cannot be given — so it is not used; the README says so.
  • priv ≠ 32 bytes, pub of the wrong length, pub not matching the seed rejected before any OpenSSL call — AkpKeyTest::aMalformedKeyIsRejectedWithTheDocumentedException (the constructor never calls OpenSSL), MLDSATest::aPublicKeyThatDoesNotMatchTheSeedIsRefused.
  • A 2419-byte or tampered signature fails verification — MLDSATest::aSignatureOfAnotherLengthIsInvalid (one byte short, one byte long, empty, all-zero), theAcvpSignatureVerifies (one bit flipped, one byte appended), the six *-sig-fail-01 fixtures.
  • AKP thumbprint — ThumbprintTest::theKidOfTheRfc9964ExampleIsItsThumbprint (the three kid values reproduced), theThumbprintOfAnAkpKeyDependsOnKtyAlgAndPubAlone (kid, key_ops, priv, the spelling of kty and alg make no difference; another alg is another key), anAkpKeyWithoutAlgorithmHasNoThumbprint, the canonical-form test extended to a negative integer value.
  • On PHP < 8.4 or OpenSSL < 3.5, isSupported() is false, create() throws, the rest of the suite is unaffected — MLDSATest::theAlgorithmCannotBeBuiltWhereThePlatformLacksIt checks the message names PHP or OpenSSL; verified on the phpqa:8.3 image (PHP gate) and on php: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 by MLDSATest::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 pinned ubuntu-24.04 runner (OpenSSL 3.0.13) with shivammathur/setup-php@2.37.2 and the PHPUnit 11 PHAR, for the OpenSSL side. The workflow audit passes.

Fixtures

  • tests/fixtures/rfc9964/ — extract.py parses Appendix A of the RFC text into appendix-a.json (the six examples, re-joined across the line wrapping) and into ml-dsa-examples/ (the three COSE_Sign1 in the cose-wg schema, plus a fail twin with the last signature byte flipped); openssl-cli/ as above, plus ml-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.
  • The cose-wg harness learns the AKP key type (pub, priv, and alg resolved 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.json keywords, examples/16-ml-dsa.php (the RFC example reproduced, thumbprint included; a fresh ML-DSA-65 key signing a COSE_Sign1; each refusal; the gate on a platform without ML-DSA).

Checks

Rebased on 4.9.x after #222 (the example is 16-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 unset COSE_ML_DSA_EXPECTED are the new ones); 2855 / 155 on the 8.3 image with COSE_ML_DSA_EXPECTED=no; the ML-DSA, key, thumbprint, loader, harness and example tests pass on php:8.4-cli-bookworm (OpenSSL 3.0) with COSE_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 in ManagerFactory.

@Spomky Spomky added this to the 4.9.0 milestone Sep 12, 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 keys COSE_Key types, parameters and thumbprints documentation README, doc/ and examples/ tests Test suite, fixtures and interoperability vectors labels Sep 12, 2026
@Spomky Spomky self-assigned this Sep 12, 2026
@Spomky
Spomky force-pushed the feat/issue-214-ml-dsa branch from 7b9a7fc to 9641c37 Compare September 12, 2026 14:02
…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
Spomky force-pushed the feat/issue-214-ml-dsa branch from 9641c37 to 959c2ea Compare September 12, 2026 17:55
@Spomky
Spomky merged commit ea9b29d into 4.9.x Sep 12, 2026
18 checks passed
@Spomky
Spomky deleted the feat/issue-214-ml-dsa branch September 12, 2026 18:02
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.
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 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(algorithms): ML-DSA-44/65/87 and the AKP key type via OpenSSL 3.5 (RFC 9964)

1 participant