Skip to content

Fix RFC 9881 ML-DSA private key encoding and decoding - #184

Open
yamian2 wants to merge 2 commits into
microsoft:mainfrom
yamian2:fix/mldsa-private-key-codec
Open

yamian2 wants to merge 2 commits into
microsoft:mainfrom
yamian2:fix/mldsa-private-key-codec

Conversation

@yamian2

@yamian2 yamian2 commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fix the ML-DSA PKCS#8 private-key codec so SCOSSL both emits and accepts the representations defined by RFC 9881.

SCOSSL previously wrapped every ML-DSA private-key representation in a universal ASN.1 OCTET STRING and its decoder only accepted that wrapper. A 32-byte seed was therefore emitted as 04 20 <seed> and standards-compliant seed-only keys encoded as 80 20 <seed> were rejected. This prevented private-key exchange with OpenSSL 3.5 and .NET and caused PKCS#12 imports to fail before the private key could be matched to its certificate.

Standards reference

RFC 9881, Section 6 defines:

ML-DSA-PrivateKey ::= CHOICE {
    seed        [0] IMPLICIT OCTET STRING (SIZE (32)),
    expandedKey OCTET STRING (...),
    both        SEQUENCE {
        seed        OCTET STRING (SIZE (32)),
        expandedKey OCTET STRING (...)
    }
}

The ASN.1 tag distinguishes the representation:

  • 0x80 ([0] IMPLICIT) for a 32-byte seed
  • 0x04 (universal OCTET STRING) for an expanded private key
  • 0x30 (SEQUENCE) for a seed plus expanded key

Changes

Encoder

  • Encode SYMCRYPT_MLDSAKEY_FORMAT_PRIVATE_SEED with context-specific primitive tag [0], producing 80 20 <seed>.
  • Validate that a seed has the required 32-byte length.
  • Preserve universal OCTET STRING encoding for expanded private keys.

Decoder

  • Dispatch on the RFC 9881 representation tag and accept seed, expanded-key, and seed-plus-expanded-key forms.
  • Require complete DER consumption and the exact 32-byte seed length.
  • For the both form, derive the expanded key from the seed and compare it in constant time with the supplied expanded key, rejecting inconsistent pairs.
  • Retain read compatibility with the legacy SCOSSL 04 20 <seed> encoding while no longer emitting it.

Interoperability impact

This fixes both directions of private-key exchange with RFC 9881 implementations, including OpenSSL 3.5 and .NET ML-DSA APIs. It also permits PKCS#12 files containing a standards-compliant seed-only ML-DSA PKCS#8 key to be imported and matched to their certificate.

Validation

  • Built the updated provider against OpenSSL 3.5.8 and SymCrypt.
  • Exported an ML-DSA-44 seed-backed key through symcryptprovider; verified the PKCS#8 payload is exactly 80 20 followed by the 32-byte seed.
  • Reloaded that key through OpenSSL 3.5's default provider and verified the derived public key.
  • Generated a standardized seed-only key through OpenSSL's default provider, loaded it through symcryptprovider, and verified the derived public key.
  • Generated ML-DSA-44 PKCS#8 through .NET, loaded it through symcryptprovider, and verified the public key matches .NET's SPKI.
  • Rebuilt sys-cert-store and certutil with the codec changes and successfully imported the real ml-dsa-44.pfx fixture that previously failed with PKCS#12 key has no matching certificate.

@yamian2 yamian2 changed the title Fix RFC 9881 ML-DSA seed private key encoding Fix RFC 9881 ML-DSA private key encoding and decoding Nov 10, 2026
@mamckee

Copy link
Copy Markdown
Collaborator

This is currently implemented in the scossl-1.11-validation-fixes feature branch as part of the next 1.11 release, which will be RI'd to main after tagging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants