Early Preview
This is currently very much a preview. Please feel free to try things out,
but don't be upset if anything is not yet working. Feedback is welcome over on our
GitHub Dicussions page.
class System.​Security.​Cryptography.​MLDsa
Assembly: System.Security.Cryptography
Inheritance: object → MLDsa
Implemented Interfaces
Represents an ML-DSA key.
Properties
public
MLDsaAlgorithm
Algorithm
Gets the specific ML-DSA algorithm for this key.
public static
bool
IsSupported
Gets a value indicating whether the current platform supports ML-DSA.
Methods
public
void
Dispose​()
Releases all resources used by the <see cref="T:System.Security.Cryptography.MLDsa" /> class.
protected
void
Dispose​(bool disposing)
<para>Called by the <c>Dispose()</c> and <c>Finalize()</c> methods to release the managed and unmanaged resources used by the current instance of the <see cref="T:System.Security.Cryptography.MLDsa" /> class.</para>
disposing
<para>
<see langword="true" /> to release managed and unmanaged resources; <see langword="false" /> to release only unmanaged resources.</para>
public
byte[]
ExportEncryptedPkcs8PrivateKey​(ReadOnlySpan<​byte> passwordBytes,
PbeParameters pbeParameters)
Exports the current key in the PKCS#8 EncryptedPrivateKeyInfo format with a byte-based password.
Returns A byte array containing the PKCS#8 EncryptedPrivateKeyInfo representation of the this key.
passwordBytes
The bytes to use as a password when encrypting the key material.
pbeParameters
The password-based encryption (PBE) parameters to use when encrypting the key material.
public
byte[]
ExportEncryptedPkcs8PrivateKey​(ReadOnlySpan<​char> password,
PbeParameters pbeParameters)
Exports the current key in the PKCS#8 EncryptedPrivateKeyInfo format with a char-based password.
Returns A byte array containing the PKCS#8 EncryptedPrivateKeyInfo representation of the this key.
password
The password to use when encrypting the key material.
pbeParameters
The password-based encryption (PBE) parameters to use when encrypting the key material.
public
byte[]
ExportEncryptedPkcs8PrivateKey​(string password,
PbeParameters pbeParameters)
Exports the current key in the PKCS#8 EncryptedPrivateKeyInfo format with a char-based password.
Returns A byte array containing the PKCS#8 EncryptedPrivateKeyInfo representation of the this key.
password
The password to use when encrypting the key material.
pbeParameters
The password-based encryption (PBE) parameters to use when encrypting the key material.
public
string
ExportEncryptedPkcs8PrivateKeyPem​(ReadOnlySpan<​byte> passwordBytes,
PbeParameters pbeParameters)
<para>Exports the current key in a PEM-encoded representation of the PKCS#8 EncryptedPrivateKeyInfo representation of this key, using a byte-based password.</para>
Returns A string containing the PEM-encoded PKCS#8 EncryptedPrivateKeyInfo.
passwordBytes
The bytes to use as a password when encrypting the key material.
pbeParameters
The password-based encryption (PBE) parameters to use when encrypting the key material.
public
string
ExportEncryptedPkcs8PrivateKeyPem​(ReadOnlySpan<​char> password,
PbeParameters pbeParameters)
<para>Exports the current key in a PEM-encoded representation of the PKCS#8 EncryptedPrivateKeyInfo representation of this key, using a char-based password.</para>
Returns A string containing the PEM-encoded PKCS#8 EncryptedPrivateKeyInfo.
password
The password to use when encrypting the key material.
pbeParameters
The password-based encryption (PBE) parameters to use when encrypting the key material.
public
string
ExportEncryptedPkcs8PrivateKeyPem​(string password,
PbeParameters pbeParameters)
<p>Exports the current key in a PEM-encoded representation of the PKCS#8 EncryptedPrivateKeyInfo representation of this key, using a char-based password.</p>
Returns A string containing the PEM-encoded PKCS#8 EncryptedPrivateKeyInfo.
password
The password to use when encrypting the key material.
pbeParameters
The password-based encryption (PBE) parameters to use when encrypting the key material.
public
byte[]
ExportMLDsaPrivateKey​()
Exports the current key in the FIPS 204 private key format.
Returns The FIPS 204 private key.
public
void
ExportMLDsaPrivateKey​(Span<​byte> destination)
Exports the current key in the FIPS 204 private key format.
destination
<para>The buffer to receive the private key. Its length must be exactly <see cref="P:System.Security.Cryptography.MLDsaAlgorithm.PrivateKeySizeInBytes" />.</para>
protected
void
ExportMLDsaPrivateKeyCore​(Span<​byte> destination)
When overridden in a derived class, exports the FIPS 204 private key to the specified buffer.
destination
The buffer to receive the private key.
public
byte[]
ExportMLDsaPrivateSeed​()
Exports the private seed in the FIPS 204 private seed format.
Returns The FIPS 204 private seed.
public
void
ExportMLDsaPrivateSeed​(Span<​byte> destination)
Exports the private seed of the current key.
destination
<para>The buffer to receive the private seed. Its length must be exactly <see cref="P:System.Security.Cryptography.MLDsaAlgorithm.PrivateSeedSizeInBytes" />.</para>
protected
void
ExportMLDsaPrivateSeedCore​(Span<​byte> destination)
When overridden in a derived class, exports the private seed to the specified buffer.
destination
The buffer to receive the private seed.
public
byte[]
ExportMLDsaPublicKey​()
Exports the public-key portion of the current key in the FIPS 204 public key format.
Returns The FIPS 204 public key.
public
void
ExportMLDsaPublicKey​(Span<​byte> destination)
Exports the public-key portion of the current key in the FIPS 204 public key format.
destination
<para>The buffer to receive the public key. Its length must be exactly <see cref="P:System.Security.Cryptography.MLDsaAlgorithm.PublicKeySizeInBytes" />.</para>
protected
void
ExportMLDsaPublicKeyCore​(Span<​byte> destination)
When overridden in a derived class, exports the FIPS 204 public key to the specified buffer.
destination
The buffer to receive the public key.
public
byte[]
ExportPkcs8PrivateKey​()
Exports the current key in the PKCS#8 PrivateKeyInfo format.
Returns A byte array containing the PKCS#8 PrivateKeyInfo representation of the this key.
public
string
ExportPkcs8PrivateKeyPem​()
Exports the current key in a PEM-encoded representation of the PKCS#8 PrivateKeyInfo format.
Returns <para>A string containing the PEM-encoded representation of the PKCS#8 PrivateKeyInfo representation of the public-key portion of this key.</para>
public
byte[]
ExportSubjectPublicKeyInfo​()
Exports the public-key portion of the current key in the X.509 SubjectPublicKeyInfo format.
Returns A byte array containing the X.509 SubjectPublicKeyInfo representation of the public-key portion of this key.
public
string
ExportSubjectPublicKeyInfoPem​()
<para>Exports the public-key portion of the current key in a PEM-encoded representation of the X.509 SubjectPublicKeyInfo format.</para>
Returns <para>A string containing the PEM-encoded representation of the X.509 SubjectPublicKeyInfo representation of the public-key portion of this key.</para>
public static
MLDsa
GenerateKey​(MLDsaAlgorithm algorithm)
Generates a new ML-DSA key.
Returns The generated key.
algorithm
An algorithm identifying what kind of ML-DSA key to generate.
public static
MLDsa
ImportEncryptedPkcs8PrivateKey​(ReadOnlySpan<​byte> passwordBytes,
ReadOnlySpan<​byte> source)
Imports an ML-DSA private key from a PKCS#8 EncryptedPrivateKeyInfo structure.
Returns The imported key.
passwordBytes
The bytes to use as a password when decrypting the key material.
source
The bytes of a PKCS#8 EncryptedPrivateKeyInfo structure in the ASN.1-BER encoding.
public static
MLDsa
ImportEncryptedPkcs8PrivateKey​(ReadOnlySpan<​char> password,
ReadOnlySpan<​byte> source)
Imports an ML-DSA private key from a PKCS#8 EncryptedPrivateKeyInfo structure.
Returns The imported key.
password
The password to use when decrypting the key material.
source
The bytes of a PKCS#8 EncryptedPrivateKeyInfo structure in the ASN.1-BER encoding.
public static
MLDsa
ImportEncryptedPkcs8PrivateKey​(string password,
byte[] source)
Imports an ML-DSA private key from a PKCS#8 EncryptedPrivateKeyInfo structure.
Returns The imported key.
password
The password to use when decrypting the key material.
source
The bytes of a PKCS#8 EncryptedPrivateKeyInfo structure in the ASN.1-BER encoding.
public static
MLDsa
ImportFromEncryptedPem​(ReadOnlySpan<​char> source,
ReadOnlySpan<​byte> passwordBytes)
Imports an ML-DSA key from an encrypted RFC 7468 PEM-encoded string.
source
The PEM text of the encrypted key to import.
passwordBytes
The bytes to use as a password when decrypting the key material.
public static
MLDsa
ImportFromEncryptedPem​(ReadOnlySpan<​char> source,
ReadOnlySpan<​char> password)
Imports an ML-DSA key from an encrypted RFC 7468 PEM-encoded string.
source
The PEM text of the encrypted key to import.
password
The password to use for decrypting the key material.
public static
MLDsa
ImportFromEncryptedPem​(string source,
byte[] passwordBytes)
Imports an ML-DSA key from an encrypted RFC 7468 PEM-encoded string.
source
The PEM text of the encrypted key to import.
passwordBytes
The bytes to use as a password when decrypting the key material.
public static
MLDsa
ImportFromEncryptedPem​(string source,
string password)
Imports an ML-DSA key from an encrypted RFC 7468 PEM-encoded string.
source
The PEM text of the encrypted key to import.
password
The password to use for decrypting the key material.
public static
MLDsa
ImportFromPem​(ReadOnlySpan<​char> source)
Imports an ML-DSA key from an RFC 7468 PEM-encoded string.
Returns The imported ML-DSA key.
source
The text of the PEM key to import.
public static
MLDsa
ImportFromPem​(string source)
Imports an ML-DSA key from an RFC 7468 PEM-encoded string.
Returns The imported ML-DSA key.
source
The text of the PEM key to import.
public static
MLDsa
ImportMLDsaPrivateKey​(MLDsaAlgorithm algorithm,
byte[] source)
Imports an ML-DSA private key in the FIPS 204 private key format.
Returns The imported key.
algorithm
The specific ML-DSA algorithm for this key.
source
The bytes of a FIPS 204 private key.
public static
MLDsa
ImportMLDsaPrivateKey​(MLDsaAlgorithm algorithm,
ReadOnlySpan<​byte> source)
Imports an ML-DSA private key in the FIPS 204 private key format.
Returns The imported key.
algorithm
The specific ML-DSA algorithm for this key.
source
The bytes of a FIPS 204 private key.
public static
MLDsa
ImportMLDsaPrivateSeed​(MLDsaAlgorithm algorithm,
byte[] source)
Imports an ML-DSA private key from its private seed value.
Returns The imported key.
algorithm
The specific ML-DSA algorithm for this key.
source
The bytes the key seed.
public static
MLDsa
ImportMLDsaPrivateSeed​(MLDsaAlgorithm algorithm,
ReadOnlySpan<​byte> source)
Imports an ML-DSA private key from its private seed value.
Returns The imported key.
algorithm
The specific ML-DSA algorithm for this key.
source
The bytes the key seed.
public static
MLDsa
ImportMLDsaPublicKey​(MLDsaAlgorithm algorithm,
byte[] source)
Imports an ML-DSA public key in the FIPS 204 public key format.
Returns The imported key.
algorithm
The specific ML-DSA algorithm for this key.
source
The bytes of a FIPS 204 public key.
public static
MLDsa
ImportMLDsaPublicKey​(MLDsaAlgorithm algorithm,
ReadOnlySpan<​byte> source)
Imports an ML-DSA public key in the FIPS 204 public key format.
Returns The imported key.
algorithm
The specific ML-DSA algorithm for this key.
source
The bytes of a FIPS 204 public key.
public static
MLDsa
ImportPkcs8PrivateKey​(byte[] source)
Imports an ML-DSA private key from a PKCS#8 PrivateKeyInfo structure.
Returns The imported key.
source
The bytes of a PKCS#8 PrivateKeyInfo structure in the ASN.1-BER encoding.
public static
MLDsa
ImportPkcs8PrivateKey​(ReadOnlySpan<​byte> source)
Imports an ML-DSA private key from a PKCS#8 PrivateKeyInfo structure.
Returns The imported key.
source
The bytes of a PKCS#8 PrivateKeyInfo structure in the ASN.1-BER encoding.
public static
MLDsa
ImportSubjectPublicKeyInfo​(byte[] source)
Imports an ML-DSA public key from an X.509 SubjectPublicKeyInfo structure.
Returns The imported key.
source
The bytes of an X.509 SubjectPublicKeyInfo structure in the ASN.1-DER encoding.
public static
MLDsa
ImportSubjectPublicKeyInfo​(ReadOnlySpan<​byte> source)
Imports an ML-DSA public key from an X.509 SubjectPublicKeyInfo structure.
Returns The imported key.
source
The bytes of an X.509 SubjectPublicKeyInfo structure in the ASN.1-DER encoding.
public
byte[]
SignData​(byte[] data,
byte[] context = null)
Signs the specified data.
Returns ML-DSA signature for the specified data.
data
The data to sign.
context
<para>An optional context-specific value to limit the scope of the signature.</para> <para>The default value is <see langword="null" />.</para>
public
void
SignData​(ReadOnlySpan<​byte> data,
Span<​byte> destination,
ReadOnlySpan<​byte> context = null)
Signs the specified data, writing the signature into the provided buffer.
data
The data to sign.
destination
<para>The buffer to receive the signature. Its length must be exactly <see cref="P:System.Security.Cryptography.MLDsaAlgorithm.SignatureSizeInBytes" />.</para>
context
<para>An optional context-specific value to limit the scope of the signature.</para> <para>The default value is an empty buffer.</para>
protected
void
SignDataCore​(ReadOnlySpan<​byte> data,
ReadOnlySpan<​byte> context,
Span<​byte> destination)
<para>When overridden in a derived class, computes the signature of the specified data and context, writing it into the provided buffer.</para>
data
The data to sign.
context
The signature context.
destination
The buffer to receive the signature, which will always be the exactly correct size for the algorithm.
public
byte[]
SignMu​(byte[] externalMu)
Signs the specified externally computed signature mu (μ) value.
Returns ML-DSA signature for the specified mu value.
externalMu
The signature mu value to sign.
public
byte[]
SignMu​(ReadOnlySpan<​byte> externalMu)
Signs the specified externally computed signature mu (μ) value.
Returns ML-DSA signature for the specified mu value.
externalMu
The signature mu value to sign.
public
void
SignMu​(ReadOnlySpan<​byte> externalMu,
Span<​byte> destination)
<para>Signs the specified externally computed signature mu (μ) value, writing the signature into the provided buffer.</para>
externalMu
The signature mu value to sign.
destination
<para>The buffer to receive the signature. Its length must be exactly <see cref="P:System.Security.Cryptography.MLDsaAlgorithm.SignatureSizeInBytes" />.</para>
protected
void
SignMuCore​(ReadOnlySpan<​byte> externalMu,
Span<​byte> destination)
<para>When overridden in a derived class, computes the remainder of the signature from the precomputed mu (μ) value, writing it into the provided buffer.</para>
externalMu
The signature mu value to sign.
destination
The buffer to receive the signature, which will always be the exactly correct size for the algorithm.
public
byte[]
SignPreHash​(byte[] hash,
string hashAlgorithmOid,
byte[] context = null)
Signs the specified hash using the FIPS 204 pre-hash signing algorithm.
hash
The hash to sign.
hashAlgorithmOid
The OID of the hash algorithm used to create the hash.
context
<para>An optional context-specific value to limit the scope of the signature.</para> <para>The default value is <see langword="null" />.</para>
public
void
SignPreHash​(ReadOnlySpan<​byte> hash,
Span<​byte> destination,
string hashAlgorithmOid,
ReadOnlySpan<​byte> context = null)
Signs the specified hash using the FIPS 204 pre-hash signing algorithm, writing the signature into the provided buffer.
hash
The hash to sign.
destination
<para>The buffer to receive the signature. Its length must be exactly <see cref="P:System.Security.Cryptography.MLDsaAlgorithm.SignatureSizeInBytes" />.</para>
hashAlgorithmOid
The OID of the hash algorithm used to create the hash.
context
<para>An optional context-specific value to limit the scope of the signature.</para> <para>The default value is an empty buffer.</para>
protected
void
SignPreHashCore​(ReadOnlySpan<​byte> hash,
ReadOnlySpan<​byte> context,
string hashAlgorithmOid,
Span<​byte> destination)
<para>When overridden in a derived class, computes the pre-hash signature of the specified hash and context, writing it into the provided buffer.</para>
hash
The hash to sign.
context
The signature context.
hashAlgorithmOid
The OID of the hash algorithm used to create the hash.
destination
The buffer to receive the signature, which will always be the exactly correct size for the algorithm.
public
bool
TryExportEncryptedPkcs8PrivateKey​(ReadOnlySpan<​byte> passwordBytes,
PbeParameters pbeParameters,
Span<​byte> destination,
Int32& bytesWritten)
public
bool
TryExportEncryptedPkcs8PrivateKey​(ReadOnlySpan<​char> password,
PbeParameters pbeParameters,
Span<​byte> destination,
Int32& bytesWritten)
public
bool
TryExportEncryptedPkcs8PrivateKey​(string password,
PbeParameters pbeParameters,
Span<​byte> destination,
Int32& bytesWritten)
public
bool
TryExportPkcs8PrivateKey​(Span<​byte> destination,
Int32& bytesWritten)
protected
bool
TryExportPkcs8PrivateKeyCore​(Span<​byte> destination,
Int32& bytesWritten)
public
bool
TryExportSubjectPublicKeyInfo​(Span<​byte> destination,
Int32& bytesWritten)
public
bool
VerifyData​(byte[] data,
byte[] signature,
byte[] context = null)
Verifies that the specified signature is valid for this key and the provided data.
Returns <see langword="true" /> if the signature validates the data; otherwise, <see langword="false" /> .
data
The data to verify.
signature
The signature to verify.
context
<para>The context value which was provided during signing.</para> <para>The default value is <see langword="null" />.</para>
public
bool
VerifyData​(ReadOnlySpan<​byte> data,
ReadOnlySpan<​byte> signature,
ReadOnlySpan<​byte> context = null)
Verifies that the specified signature is valid for this key and the provided data.
Returns <see langword="true" /> if the signature validates the data; otherwise, <see langword="false" /> .
data
The data to verify.
signature
The signature to verify.
context
<para>The context value which was provided during signing.</para> <para>The default value is an empty buffer.</para>
protected
bool
VerifyDataCore​(ReadOnlySpan<​byte> data,
ReadOnlySpan<​byte> context,
ReadOnlySpan<​byte> signature)
When overridden in a derived class, verifies the signature of the specified data and context.
Returns <see langword="true" /> if the signature validates the data; otherwise, <see langword="false" /> .
data
The data to verify.
context
The signature context.
signature
The signature to verify.
public
bool
VerifyMu​(byte[] externalMu,
byte[] signature)
Verifies that a digital signature is valid for the provided externally computed signature mu (μ) value.
Returns <p>
<code data-dev-comment-type="langword">true</code> if the digital signature is valid for the provided mu value; otherwise, <code data-dev-comment-type="langword">false</code>.</p>
externalMu
The signature mu value.
signature
The signature to verify.
public
bool
VerifyMu​(ReadOnlySpan<​byte> externalMu,
ReadOnlySpan<​byte> signature)
Verifies that a digital signature is valid for the provided externally computed signature mu (μ) value.
Returns <para>
<see langword="true" /> if the digital signature is valid for the provided mu value; otherwise, <see langword="false" />.</para>
externalMu
The signature mu value.
signature
The signature to verify.
protected
bool
VerifyMuCore​(ReadOnlySpan<​byte> externalMu,
ReadOnlySpan<​byte> signature)
<para>When overridden in a derived class, verifies that a digital signature is valid for the provided externally computed signature mu (μ) value.</para>
Returns <see langword="true" /> if the mu value is valid; otherwise, <see langword="false" /> .
externalMu
The signature mu value.
signature
The signature to verify.
public
bool
VerifyPreHash​(byte[] hash,
byte[] signature,
string hashAlgorithmOid,
byte[] context = null)
Verifies that the specified FIPS 204 pre-hash signature is valid for this key and the provided hash.
Returns <see langword="true" /> if the signature validates the hash; otherwise, <see langword="false" /> .
hash
The hash to verify.
signature
The signature to verify.
hashAlgorithmOid
The OID of the hash algorithm used to create the hash.
context
<para>The context value which was provided during signing.</para> <para>The default value is <see langword="null" />.</para>
public
bool
VerifyPreHash​(ReadOnlySpan<​byte> hash,
ReadOnlySpan<​byte> signature,
string hashAlgorithmOid,
ReadOnlySpan<​byte> context = null)
Verifies that the specified FIPS 204 pre-hash signature is valid for this key and the provided hash.
Returns <see langword="true" /> if the signature validates the hash; otherwise, <see langword="false" /> .
hash
The hash to verify.
signature
The signature to verify.
hashAlgorithmOid
The OID of the hash algorithm used to create the hash.
context
<para>The context value which was provided during signing.</para> <para>The default value is an empty buffer.</para>
protected
bool
VerifyPreHashCore​(ReadOnlySpan<​byte> hash,
ReadOnlySpan<​byte> context,
string hashAlgorithmOid,
ReadOnlySpan<​byte> signature)
When overridden in a derived class, verifies the pre-hash signature of the specified hash and context.
Returns <see langword="true" /> if the signature validates the hash; otherwise, <see langword="false" /> .
hash
The data to verify.
context
The signature context.
hashAlgorithmOid
The OID of the hash algorithm used to create the hash.
signature
The signature to verify.
public
bool
Equals​(object obj)
Inherited from object
protected
void
Finalize​()
Inherited from object
public
int
GetHashCode​()
Inherited from object
protected
object
MemberwiseClone​()
Inherited from object
public
string
ToString​()
Inherited from object