-
Notifications
You must be signed in to change notification settings - Fork 1.7k
MLDSA65 support for AWS-LC #14404
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DarkaMaul
wants to merge
11
commits into
pyca:main
Choose a base branch
from
trail-of-forks:dm/mldsa65-aws-lc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,235
−0
Open
MLDSA65 support for AWS-LC #14404
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
176e34b
MLDSA65 support for AWS-LC
DarkaMaul 65ab941
Improve coverage
DarkaMaul 4fab32f
Initial review
DarkaMaul 2450b41
First round of review
DarkaMaul ef4345f
Clean tests
DarkaMaul 2f6b313
Revert spurious formatting
DarkaMaul 4fe06c7
Incorporate review
DarkaMaul ed6d9de
Rename from mldsa65 to mldsa
DarkaMaul b60d88b
Improve serialization/deserialization
DarkaMaul 39c9db3
Fix coverage
DarkaMaul db6d98d
Use ASN1 struct to parse the private key
DarkaMaul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| ML-DSA vector creation | ||
| ====================== | ||
|
|
||
| This page documents the code that was used to generate the ML-DSA test | ||
| vectors. These vectors are used to verify: | ||
|
|
||
| * Unsupported ML-DSA variants (i.e. variants other than ML-DSA-65) are | ||
| correctly rejected when loading keys. | ||
| * ML-DSA-65 private keys without a seed are correctly rejected. | ||
|
|
||
| The following Python script was run to generate the vector files. | ||
|
|
||
| .. literalinclude:: /development/custom-vectors/mldsa/generate_mldsa.py | ||
|
|
||
| Download link: :download:`generate_mldsa.py | ||
| </development/custom-vectors/mldsa/generate_mldsa.py>` | ||
|
|
||
| ML-DSA-44 public key | ||
| -------------------- | ||
|
|
||
| The public key was derived from the private key using the OpenSSL CLI | ||
| (requires OpenSSL 3.5+): | ||
|
|
||
| .. code-block:: console | ||
|
|
||
| $ openssl pkey -in mldsa44_priv.der -inform DER -pubout -outform DER -out mldsa44_pub.der |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| # This file is dual licensed under the terms of the Apache License, Version | ||
| # 2.0, and the BSD License. See the LICENSE file in the root of this repository | ||
| # for complete details. | ||
|
|
||
| import os | ||
|
|
||
| from cryptography import x509 | ||
| from cryptography.hazmat import asn1 | ||
|
|
||
|
|
||
| @asn1.sequence | ||
| class AlgorithmIdentifier: | ||
| algorithm: x509.ObjectIdentifier | ||
|
|
||
|
|
||
| @asn1.sequence | ||
| class OneAsymmetricKey: | ||
| version: int | ||
| algorithm: AlgorithmIdentifier | ||
| private_key: bytes | ||
|
|
||
|
|
||
| # ML-DSA-PrivateKey ::= CHOICE { | ||
| # seed [0] IMPLICIT OCTET STRING (SIZE (32)), | ||
| # expandedKey OCTET STRING, | ||
| # both SEQUENCE { seed, expandedKey } | ||
| # } | ||
| MLDSA_SEED_BYTES = 32 | ||
|
|
||
|
|
||
| def generate_mldsa44_unsupported_variant(output_dir: str) -> None: | ||
| seed = b"\x2a" * MLDSA_SEED_BYTES | ||
| # [0] IMPLICIT OCTET STRING: tag 0x80, length 0x20 | ||
| seed_only_privkey = b"\x80\x20" + seed | ||
|
|
||
| # ML-DSA-44 OID: 2.16.840.1.101.3.4.3.17 | ||
| obj = OneAsymmetricKey( | ||
| version=0, | ||
| algorithm=AlgorithmIdentifier( | ||
| algorithm=x509.ObjectIdentifier("2.16.840.1.101.3.4.3.17"), | ||
| ), | ||
| private_key=seed_only_privkey, | ||
| ) | ||
| with open(os.path.join(output_dir, "mldsa44_priv.der"), "wb") as f: | ||
| f.write(asn1.encode_der(obj)) | ||
|
|
||
|
|
||
| def generate_mldsa65_noseed(output_dir: str) -> None: | ||
| # ML-DSA-65 OID: 2.16.840.1.101.3.4.3.18 | ||
| # Generate an ML-DSA-65 PKCS#8 key whose inner privateKey is an | ||
| # empty SEQUENCE (0x30 0x00) — i.e. the "both" SEQUENCE form with | ||
| # no seed present. This exercises the InvalidKey error path in the | ||
| # Rust parser when seed is None. | ||
| obj = OneAsymmetricKey( | ||
| version=0, | ||
| algorithm=AlgorithmIdentifier( | ||
| algorithm=x509.ObjectIdentifier("2.16.840.1.101.3.4.3.18"), | ||
| ), | ||
| private_key=b"\x30\x00", | ||
| ) | ||
| with open(os.path.join(output_dir, "mldsa65_noseed_priv.der"), "wb") as f: | ||
| f.write(asn1.encode_der(obj)) | ||
|
|
||
|
|
||
| def main(): | ||
| output_dir = os.path.join( | ||
| "vectors", "cryptography_vectors", "asymmetric", "MLDSA" | ||
| ) | ||
| generate_mldsa44_unsupported_variant(output_dir) | ||
| generate_mldsa65_noseed(output_dir) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # This file is dual licensed under the terms of the Apache License, Version | ||
| # 2.0, and the BSD License. See the LICENSE file in the root of this repository | ||
| # for complete details. | ||
|
|
||
| from cryptography.hazmat.primitives.asymmetric import mldsa | ||
| from cryptography.utils import Buffer | ||
|
|
||
| class MlDsa65PrivateKey: ... | ||
| class MlDsa65PublicKey: ... | ||
|
|
||
| def generate_key() -> mldsa.MlDsa65PrivateKey: ... | ||
| def from_public_bytes(data: bytes) -> mldsa.MlDsa65PublicKey: ... | ||
| def from_seed_bytes(data: Buffer) -> mldsa.MlDsa65PrivateKey: ... |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| # This file is dual licensed under the terms of the Apache License, Version | ||
| # 2.0, and the BSD License. See the LICENSE file in the root of this repository | ||
| # for complete details. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import abc | ||
|
|
||
| from cryptography.exceptions import UnsupportedAlgorithm, _Reasons | ||
| from cryptography.hazmat.bindings._rust import openssl as rust_openssl | ||
| from cryptography.hazmat.primitives import _serialization | ||
| from cryptography.utils import Buffer | ||
|
|
||
|
|
||
| class MlDsa65PublicKey(metaclass=abc.ABCMeta): | ||
| @classmethod | ||
| def from_public_bytes(cls, data: bytes) -> MlDsa65PublicKey: | ||
| from cryptography.hazmat.backends.openssl.backend import backend | ||
|
|
||
| if not backend.mldsa_supported(): | ||
| raise UnsupportedAlgorithm( | ||
| "ML-DSA-65 is not supported by this backend.", | ||
| _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM, | ||
| ) | ||
|
|
||
| return rust_openssl.mldsa.from_public_bytes(data) | ||
|
|
||
| @abc.abstractmethod | ||
| def public_bytes( | ||
| self, | ||
| encoding: _serialization.Encoding, | ||
| format: _serialization.PublicFormat, | ||
| ) -> bytes: | ||
| """ | ||
| The serialized bytes of the public key. | ||
| """ | ||
|
|
||
| @abc.abstractmethod | ||
| def public_bytes_raw(self) -> bytes: | ||
| """ | ||
| The raw bytes of the public key. | ||
| Equivalent to public_bytes(Raw, Raw). | ||
|
|
||
| The public key is 1,952 bytes for MLDSA-65. | ||
| """ | ||
|
|
||
| @abc.abstractmethod | ||
| def verify( | ||
| self, | ||
| signature: Buffer, | ||
| data: Buffer, | ||
| context: Buffer | None = None, | ||
| ) -> None: | ||
| """ | ||
alex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Verify the signature. | ||
| """ | ||
|
|
||
| @abc.abstractmethod | ||
| def __eq__(self, other: object) -> bool: | ||
| """ | ||
| Checks equality. | ||
| """ | ||
|
|
||
| @abc.abstractmethod | ||
| def __copy__(self) -> MlDsa65PublicKey: | ||
| """ | ||
| Returns a copy. | ||
| """ | ||
|
|
||
| @abc.abstractmethod | ||
| def __deepcopy__(self, memo: dict) -> MlDsa65PublicKey: | ||
| """ | ||
| Returns a deep copy. | ||
| """ | ||
|
|
||
|
|
||
| if hasattr(rust_openssl, "mldsa"): | ||
| MlDsa65PublicKey.register(rust_openssl.mldsa.MlDsa65PublicKey) | ||
|
|
||
|
|
||
| class MlDsa65PrivateKey(metaclass=abc.ABCMeta): | ||
| @classmethod | ||
| def generate(cls) -> MlDsa65PrivateKey: | ||
| from cryptography.hazmat.backends.openssl.backend import backend | ||
|
|
||
| if not backend.mldsa_supported(): | ||
| raise UnsupportedAlgorithm( | ||
| "ML-DSA-65 is not supported by this backend.", | ||
| _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM, | ||
| ) | ||
|
|
||
| return rust_openssl.mldsa.generate_key() | ||
|
|
||
| @classmethod | ||
| def from_seed_bytes(cls, data: Buffer) -> MlDsa65PrivateKey: | ||
| from cryptography.hazmat.backends.openssl.backend import backend | ||
|
|
||
| if not backend.mldsa_supported(): | ||
| raise UnsupportedAlgorithm( | ||
| "ML-DSA-65 is not supported by this backend.", | ||
| _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM, | ||
| ) | ||
|
|
||
| return rust_openssl.mldsa.from_seed_bytes(data) | ||
|
|
||
| @abc.abstractmethod | ||
| def public_key(self) -> MlDsa65PublicKey: | ||
| """ | ||
| The MlDsa65PublicKey derived from the private key. | ||
| """ | ||
|
|
||
| @abc.abstractmethod | ||
| def private_bytes( | ||
DarkaMaul marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self, | ||
| encoding: _serialization.Encoding, | ||
| format: _serialization.PrivateFormat, | ||
| encryption_algorithm: (_serialization.KeySerializationEncryption), | ||
| ) -> bytes: | ||
| """ | ||
| The serialized bytes of the private key. | ||
|
|
||
| This method only returns the serialization of the seed form of the | ||
| private key, never the expanded one. | ||
| """ | ||
|
|
||
| @abc.abstractmethod | ||
| def private_bytes_raw(self) -> bytes: | ||
| """ | ||
| The raw bytes of the private key. | ||
| Equivalent to private_bytes(Raw, Raw, NoEncryption()). | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same Q here about seed. |
||
|
|
||
| This method only returns the seed form of the private key (32 bytes). | ||
| """ | ||
|
|
||
| @abc.abstractmethod | ||
| def sign(self, data: Buffer, context: Buffer | None = None) -> bytes: | ||
| """ | ||
| Signs the data. | ||
| """ | ||
|
|
||
| @abc.abstractmethod | ||
| def __copy__(self) -> MlDsa65PrivateKey: | ||
| """ | ||
| Returns a copy. | ||
| """ | ||
|
|
||
| @abc.abstractmethod | ||
| def __deepcopy__(self, memo: dict) -> MlDsa65PrivateKey: | ||
| """ | ||
| Returns a deep copy. | ||
| """ | ||
|
|
||
|
|
||
| if hasattr(rust_openssl, "mldsa"): | ||
| MlDsa65PrivateKey.register(rust_openssl.mldsa.MlDsa65PrivateKey) | ||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Raw bytes here means seed, I assume? We should probably say that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No - for the public key, it's always going to be the expanded version
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation needs to be much clearer about which of these we mean.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've tried updating the docstring - let me know what you think.
IIUC:
So for the public key, we have no choice, we must return the full bytes.