Danger
This is a “Hazardous Materials” module. You should ONLY use it if you’re 100% absolutely sure that you know what you’re doing because this module is full of land mines, dragons, and dinosaurs with laser guns.
ML-DSA signing
ML-DSA is a post-quantum digital signature algorithm based on module lattices, standardized in FIPS 204.
Signing & Verification
>>> from cryptography.hazmat.primitives.asymmetric.mldsa import MLDSA65PrivateKey
>>> private_key = MLDSA65PrivateKey.generate()
>>> signature = private_key.sign(b"my authenticated message")
>>> public_key = private_key.public_key()
>>> public_key.verify(signature, b"my authenticated message")
Context-based Signing & Verification
ML-DSA supports context strings to bind additional information to signatures. The context can be up to 255 bytes and is used to differentiate signatures in different contexts or protocols.
>>> from cryptography.hazmat.primitives.asymmetric.mldsa import MLDSA65PrivateKey
>>> private_key = MLDSA65PrivateKey.generate()
>>> context = b"email-signature-v1"
>>> signature = private_key.sign(b"my authenticated message", context)
>>> public_key = private_key.public_key()
>>> # Verification requires the same context
>>> public_key.verify(signature, b"my authenticated message", context)
Key interfaces
- class cryptography.hazmat.primitives.asymmetric.mldsa.MLDSA44PrivateKey[source]
Added in version 47.0.
- classmethod generate()[source]
Generate an ML-DSA-44 private key.
- Returns:
- Raises:
cryptography.exceptions.UnsupportedAlgorithm – If ML-DSA-44 is not supported by the backend
cryptographyis using.
- classmethod from_seed_bytes(data)[source]
Load an ML-DSA-44 private key from seed bytes.
- Parameters:
data (bytes-like) – 32 byte seed.
- Returns:
- Raises:
ValueError – If the seed is not 32 bytes.
cryptography.exceptions.UnsupportedAlgorithm – If ML-DSA-44 is not supported by the backend
cryptographyis using.
>>> from cryptography.hazmat.primitives.asymmetric import mldsa >>> private_key = mldsa.MLDSA44PrivateKey.generate() >>> seed = private_key.private_bytes_raw() >>> same_key = mldsa.MLDSA44PrivateKey.from_seed_bytes(seed)
- sign(data, context=None)[source]
Sign the data using ML-DSA-44. An optional context string can be provided.
- Parameters:
data (bytes-like) – The data to sign.
context (bytes-like or
None) – An optional context string (up to 255 bytes).
- Returns bytes:
The signature (2420 bytes).
- Raises:
ValueError – If the context is longer than 255 bytes.
- private_bytes(encoding, format, encryption_algorithm)[source]
Allows serialization of the key to bytes. Encoding (
PEM,DER, orRaw) and format (PKCS8orRaw) are chosen to define the exact serialization.This method only returns the serialization of the seed form of the private key, never the expanded one.
- Parameters:
encoding – A value from the
Encodingenum.format – A value from the
PrivateFormatenum. If theencodingisRawthenformatmust beRaw, otherwise it must bePKCS8.encryption_algorithm – An instance of an object conforming to the
KeySerializationEncryptioninterface.
- Return bytes:
Serialized key.
- private_bytes_raw()[source]
Allows serialization of the key to raw bytes. This method is a convenience shortcut for calling
private_bytes()withRawencoding,Rawformat, andNoEncryption.This method only returns the seed form of the private key (32 bytes).
- Return bytes:
Raw key (32-byte seed).
- class cryptography.hazmat.primitives.asymmetric.mldsa.MLDSA44PublicKey[source]
Added in version 47.0.
- classmethod from_public_bytes(data)[source]
- Parameters:
data (bytes) – 1312 byte public key.
- Returns:
- Raises:
ValueError – If the public key is not 1312 bytes.
cryptography.exceptions.UnsupportedAlgorithm – If ML-DSA-44 is not supported by the backend
cryptographyis using.
>>> from cryptography.hazmat.primitives import serialization >>> from cryptography.hazmat.primitives.asymmetric import mldsa >>> private_key = mldsa.MLDSA44PrivateKey.generate() >>> public_key = private_key.public_key() >>> public_bytes = public_key.public_bytes( ... encoding=serialization.Encoding.Raw, ... format=serialization.PublicFormat.Raw ... ) >>> loaded_public_key = mldsa.MLDSA44PublicKey.from_public_bytes(public_bytes)
- public_bytes(encoding, format)[source]
Allows serialization of the key to bytes. Encoding (
PEM,DER, orRaw) and format (SubjectPublicKeyInfoorRaw) are chosen to define the exact serialization.- Parameters:
encoding – A value from the
Encodingenum.format – A value from the
PublicFormatenum. If theencodingisRawthenformatmust beRaw, otherwise it must beSubjectPublicKeyInfo.
- Returns bytes:
The public key bytes.
- public_bytes_raw()[source]
Allows serialization of the key to raw bytes. This method is a convenience shortcut for calling
public_bytes()withRawencoding andRawformat.- Return bytes:
1312-byte raw public key.
- verify(signature, data, context=None)[source]
Verify a signature using ML-DSA-44. If a context string was used during signing, the same context must be provided for verification to succeed.
- Parameters:
signature (bytes-like) – The signature to verify.
data (bytes-like) – The data to verify.
context (bytes-like or
None) – An optional context string (up to 255 bytes) that was used during signing.
- Returns:
None
- Raises:
cryptography.exceptions.InvalidSignature – Raised when the signature cannot be verified.
ValueError – If the context is longer than 255 bytes.
- class cryptography.hazmat.primitives.asymmetric.mldsa.MLDSA65PrivateKey[source]
Added in version 47.0.
- classmethod generate()[source]
Generate an ML-DSA-65 private key.
- Returns:
- Raises:
cryptography.exceptions.UnsupportedAlgorithm – If ML-DSA-65 is not supported by the backend
cryptographyis using.
- classmethod from_seed_bytes(data)[source]
Load an ML-DSA-65 private key from seed bytes.
- Parameters:
data (bytes-like) – 32 byte seed.
- Returns:
- Raises:
ValueError – If the seed is not 32 bytes.
cryptography.exceptions.UnsupportedAlgorithm – If ML-DSA-65 is not supported by the backend
cryptographyis using.
>>> from cryptography.hazmat.primitives.asymmetric import mldsa >>> private_key = mldsa.MLDSA65PrivateKey.generate() >>> seed = private_key.private_bytes_raw() >>> same_key = mldsa.MLDSA65PrivateKey.from_seed_bytes(seed)
- sign(data, context=None)[source]
Sign the data using ML-DSA-65. An optional context string can be provided.
- Parameters:
data (bytes-like) – The data to sign.
context (bytes-like or
None) – An optional context string (up to 255 bytes).
- Returns bytes:
The signature (3309 bytes).
- Raises:
ValueError – If the context is longer than 255 bytes.
- private_bytes(encoding, format, encryption_algorithm)[source]
Allows serialization of the key to bytes. Encoding (
PEM,DER, orRaw) and format (PKCS8orRaw) are chosen to define the exact serialization.This method only returns the serialization of the seed form of the private key, never the expanded one.
- Parameters:
encoding – A value from the
Encodingenum.format – A value from the
PrivateFormatenum. If theencodingisRawthenformatmust beRaw, otherwise it must bePKCS8.encryption_algorithm – An instance of an object conforming to the
KeySerializationEncryptioninterface.
- Return bytes:
Serialized key.
- private_bytes_raw()[source]
Allows serialization of the key to raw bytes. This method is a convenience shortcut for calling
private_bytes()withRawencoding,Rawformat, andNoEncryption.This method only returns the seed form of the private key (32 bytes).
- Return bytes:
Raw key (32-byte seed).
- class cryptography.hazmat.primitives.asymmetric.mldsa.MLDSA65PublicKey[source]
Added in version 47.0.
- classmethod from_public_bytes(data)[source]
- Parameters:
data (bytes) – 1952 byte public key.
- Returns:
- Raises:
ValueError – If the public key is not 1952 bytes.
cryptography.exceptions.UnsupportedAlgorithm – If ML-DSA-65 is not supported by the backend
cryptographyis using.
>>> from cryptography.hazmat.primitives import serialization >>> from cryptography.hazmat.primitives.asymmetric import mldsa >>> private_key = mldsa.MLDSA65PrivateKey.generate() >>> public_key = private_key.public_key() >>> public_bytes = public_key.public_bytes( ... encoding=serialization.Encoding.Raw, ... format=serialization.PublicFormat.Raw ... ) >>> loaded_public_key = mldsa.MLDSA65PublicKey.from_public_bytes(public_bytes)
- public_bytes(encoding, format)[source]
Allows serialization of the key to bytes. Encoding (
PEM,DER, orRaw) and format (SubjectPublicKeyInfoorRaw) are chosen to define the exact serialization.- Parameters:
encoding – A value from the
Encodingenum.format – A value from the
PublicFormatenum. If theencodingisRawthenformatmust beRaw, otherwise it must beSubjectPublicKeyInfo.
- Returns bytes:
The public key bytes.
- public_bytes_raw()[source]
Allows serialization of the key to raw bytes. This method is a convenience shortcut for calling
public_bytes()withRawencoding andRawformat.- Return bytes:
1952-byte raw public key.
- verify(signature, data, context=None)[source]
Verify a signature using ML-DSA-65. If a context string was used during signing, the same context must be provided for verification to succeed.
- Parameters:
signature (bytes-like) – The signature to verify.
data (bytes-like) – The data to verify.
context (bytes-like or
None) – An optional context string (up to 255 bytes) that was used during signing.
- Returns:
None
- Raises:
cryptography.exceptions.InvalidSignature – Raised when the signature cannot be verified.
ValueError – If the context is longer than 255 bytes.
- class cryptography.hazmat.primitives.asymmetric.mldsa.MLDSA87PrivateKey[source]
Added in version 47.0.
- classmethod generate()[source]
Generate an ML-DSA-87 private key.
- Returns:
- Raises:
cryptography.exceptions.UnsupportedAlgorithm – If ML-DSA-87 is not supported by the backend
cryptographyis using.
- classmethod from_seed_bytes(data)[source]
Load an ML-DSA-87 private key from seed bytes.
- Parameters:
data (bytes-like) – 32 byte seed.
- Returns:
- Raises:
ValueError – If the seed is not 32 bytes.
cryptography.exceptions.UnsupportedAlgorithm – If ML-DSA-87 is not supported by the backend
cryptographyis using.
>>> from cryptography.hazmat.primitives.asymmetric import mldsa >>> private_key = mldsa.MLDSA87PrivateKey.generate() >>> seed = private_key.private_bytes_raw() >>> same_key = mldsa.MLDSA87PrivateKey.from_seed_bytes(seed)
- sign(data, context=None)[source]
Sign the data using ML-DSA-87. An optional context string can be provided.
- Parameters:
data (bytes-like) – The data to sign.
context (bytes-like or
None) – An optional context string (up to 255 bytes).
- Returns bytes:
The signature (4627 bytes).
- Raises:
ValueError – If the context is longer than 255 bytes.
- private_bytes(encoding, format, encryption_algorithm)[source]
Allows serialization of the key to bytes. Encoding (
PEM,DER, orRaw) and format (PKCS8orRaw) are chosen to define the exact serialization.This method only returns the serialization of the seed form of the private key, never the expanded one.
- Parameters:
encoding – A value from the
Encodingenum.format – A value from the
PrivateFormatenum. If theencodingisRawthenformatmust beRaw, otherwise it must bePKCS8.encryption_algorithm – An instance of an object conforming to the
KeySerializationEncryptioninterface.
- Return bytes:
Serialized key.
- private_bytes_raw()[source]
Allows serialization of the key to raw bytes. This method is a convenience shortcut for calling
private_bytes()withRawencoding,Rawformat, andNoEncryption.This method only returns the seed form of the private key (32 bytes).
- Return bytes:
Raw key (32-byte seed).
- class cryptography.hazmat.primitives.asymmetric.mldsa.MLDSA87PublicKey[source]
Added in version 47.0.
- classmethod from_public_bytes(data)[source]
- Parameters:
data (bytes) – 2592 byte public key.
- Returns:
- Raises:
ValueError – If the public key is not 2592 bytes.
cryptography.exceptions.UnsupportedAlgorithm – If ML-DSA-87 is not supported by the backend
cryptographyis using.
>>> from cryptography.hazmat.primitives import serialization >>> from cryptography.hazmat.primitives.asymmetric import mldsa >>> private_key = mldsa.MLDSA87PrivateKey.generate() >>> public_key = private_key.public_key() >>> public_bytes = public_key.public_bytes( ... encoding=serialization.Encoding.Raw, ... format=serialization.PublicFormat.Raw ... ) >>> loaded_public_key = mldsa.MLDSA87PublicKey.from_public_bytes(public_bytes)
- public_bytes(encoding, format)[source]
Allows serialization of the key to bytes. Encoding (
PEM,DER, orRaw) and format (SubjectPublicKeyInfoorRaw) are chosen to define the exact serialization.- Parameters:
encoding – A value from the
Encodingenum.format – A value from the
PublicFormatenum. If theencodingisRawthenformatmust beRaw, otherwise it must beSubjectPublicKeyInfo.
- Returns bytes:
The public key bytes.
- public_bytes_raw()[source]
Allows serialization of the key to raw bytes. This method is a convenience shortcut for calling
public_bytes()withRawencoding andRawformat.- Return bytes:
2592-byte raw public key.
- verify(signature, data, context=None)[source]
Verify a signature using ML-DSA-87. If a context string was used during signing, the same context must be provided for verification to succeed.
- Parameters:
signature (bytes-like) – The signature to verify.
data (bytes-like) – The data to verify.
context (bytes-like or
None) – An optional context string (up to 255 bytes) that was used during signing.
- Returns:
None
- Raises:
cryptography.exceptions.InvalidSignature – Raised when the signature cannot be verified.
ValueError – If the context is longer than 255 bytes.