-
Notifications
You must be signed in to change notification settings - Fork 55
Add ML-DSA support #1124
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
Draft
jku
wants to merge
6
commits into
secure-systems-lab:main
Choose a base branch
from
jku:mldsa
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.
Draft
Add ML-DSA support #1124
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c7d7de7
Add ML-DSA support
jku 59656ef
Add ML-DSA support to GCPSigner
jku c5bded4
Refactor to make linter happier
jku 9f4ca0b
More refactor to keep linter happy
jku 95266f1
Linter fixes, add a linter skip
jku 9182115
CryptoSigner: Simplify MLDSA signing prefix
jku 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
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 |
|---|---|---|
|
|
@@ -23,6 +23,11 @@ | |
| from cryptography.hazmat.primitives.asymmetric.ed25519 import ( | ||
| Ed25519PrivateKey, | ||
| ) | ||
| from cryptography.hazmat.primitives.asymmetric.mldsa import ( | ||
| MLDSA44PrivateKey, | ||
| MLDSA65PrivateKey, | ||
| MLDSA87PrivateKey, | ||
| ) | ||
| from cryptography.hazmat.primitives.asymmetric.padding import ( | ||
| MGF1, | ||
| PSS, | ||
|
|
@@ -38,6 +43,8 @@ | |
| from cryptography.hazmat.primitives.asymmetric.types import PrivateKeyTypes | ||
| from cryptography.hazmat.primitives.hashes import ( | ||
| SHA256, | ||
| SHA512, | ||
| Hash, | ||
| HashAlgorithm, | ||
| ) | ||
| from cryptography.hazmat.primitives.serialization import ( | ||
|
|
@@ -71,6 +78,14 @@ class _NoSignArgs: | |
| pass | ||
|
|
||
|
|
||
| @dataclass | ||
| class _MLDSASignArgs: | ||
| prefix: bytes | ||
|
|
||
| def __init__(self, version: int) -> None: | ||
| self.prefix = b"tuf" + bytes([version]) | ||
|
|
||
|
|
||
| # for backwards compat: use when spec-deprecated keytype ecdsa-sha2-nistp256 | ||
| # should be accepted in addition to "ecdsa" | ||
| _ECDSA_KEYTYPES = ["ecdsa", "ecdsa-sha2-nistp256"] | ||
|
|
@@ -117,14 +132,20 @@ def __init__( | |
| private_key: "PrivateKeyTypes", | ||
| public_key: SSlibKey | None = None, | ||
| ): | ||
| def assert_type( | ||
| name: str, key: PrivateKeyTypes, typ: type[PrivateKeyTypes] | ||
| ) -> None: | ||
| if not isinstance(key, typ): | ||
| raise ValueError(f"invalid {name} key: {type(key)}") | ||
|
|
||
| if CRYPTO_IMPORT_ERROR: | ||
| raise UnsupportedLibraryError(CRYPTO_IMPORT_ERROR) | ||
|
|
||
| if public_key is None: | ||
| public_key = SSlibKey.from_crypto(private_key.public_key()) | ||
|
|
||
| self._private_key: PrivateKeyTypes | ||
| self._sign_args: _RSASignArgs | _ECDSASignArgs | _NoSignArgs | ||
| self._sign_args: _RSASignArgs | _ECDSASignArgs | _NoSignArgs | _MLDSASignArgs | ||
|
|
||
| if public_key.keytype == "rsa" and public_key.scheme in [ | ||
| "rsassa-pss-sha224", | ||
|
|
@@ -136,8 +157,7 @@ def __init__( | |
| "rsa-pkcs1v15-sha384", | ||
| "rsa-pkcs1v15-sha512", | ||
| ]: | ||
| if not isinstance(private_key, RSAPrivateKey): | ||
| raise ValueError(f"invalid rsa key: {type(private_key)}") | ||
| assert_type("rsa", private_key, RSAPrivateKey) | ||
|
Collaborator
Author
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. all these assert tweaks are just to keep linter happy ("too many branches"): the functionality should not change |
||
|
|
||
| hash_name = public_key.get_hash_algorithm_name() | ||
| hash_algo = get_hash_algorithm(hash_name) | ||
|
|
@@ -146,31 +166,36 @@ def __init__( | |
| padding = _get_rsa_padding(padding_name, hash_algo) | ||
|
|
||
| self._sign_args = _RSASignArgs(padding, hash_algo) | ||
| self._private_key = private_key | ||
|
|
||
| elif ( | ||
| public_key.keytype in _ECDSA_KEYTYPES | ||
| and public_key.scheme == "ecdsa-sha2-nistp256" | ||
| ): | ||
| if not isinstance(private_key, EllipticCurvePrivateKey): | ||
| raise ValueError(f"invalid ecdsa key: {type(private_key)}") | ||
|
|
||
| signature_algorithm = ECDSA(SHA256()) | ||
| self._sign_args = _ECDSASignArgs(signature_algorithm) | ||
| self._private_key = private_key | ||
| assert_type("ecdsa", private_key, EllipticCurvePrivateKey) | ||
| self._sign_args = _ECDSASignArgs(ECDSA(SHA256())) | ||
|
|
||
| elif public_key.keytype == "ed25519" and public_key.scheme == "ed25519": | ||
| if not isinstance(private_key, Ed25519PrivateKey): | ||
| raise ValueError(f"invalid ed25519 key: {type(private_key)}") | ||
|
|
||
| assert_type("ed25519", private_key, Ed25519PrivateKey) | ||
| self._sign_args = _NoSignArgs() | ||
| self._private_key = private_key | ||
|
|
||
| elif public_key.keytype == "ml-dsa" and public_key.scheme == "ml-dsa-44/1": | ||
| assert_type("ml-dsa-44", private_key, MLDSA44PrivateKey) | ||
| self._sign_args = _MLDSASignArgs(1) | ||
|
|
||
| elif public_key.keytype == "ml-dsa" and public_key.scheme == "ml-dsa-65/1": | ||
| assert_type("ml-dsa-65", private_key, MLDSA65PrivateKey) | ||
| self._sign_args = _MLDSASignArgs(1) | ||
|
|
||
| elif public_key.keytype == "ml-dsa" and public_key.scheme == "ml-dsa-87/1": | ||
| assert_type("ml-dsa-87", private_key, MLDSA87PrivateKey) | ||
| self._sign_args = _MLDSASignArgs(1) | ||
|
|
||
| else: | ||
| raise ValueError( | ||
| f"unsupported public key {public_key.keytype}/{public_key.scheme}" | ||
| ) | ||
|
|
||
| self._private_key = private_key | ||
| self._public_key = public_key | ||
|
|
||
| @property | ||
|
|
@@ -321,5 +346,17 @@ def generate_ecdsa( | |
| return CryptoSigner(private_key, public_key) | ||
|
|
||
| def sign(self, payload: bytes) -> Signature: | ||
| sig = self._private_key.sign(payload, *astuple(self._sign_args)) # type: ignore | ||
| if isinstance( | ||
| self._private_key, (MLDSA44PrivateKey, MLDSA65PrivateKey, MLDSA87PrivateKey) | ||
| ): | ||
| if not isinstance(self._sign_args, _MLDSASignArgs): | ||
| raise AssertionError("Unexpected MLDSA signer state") | ||
|
|
||
| digest = Hash(SHA512()) | ||
| digest.update(payload) | ||
|
|
||
| sig = self._private_key.sign(self._sign_args.prefix + digest.finalize()) | ||
| else: | ||
| sig = self._private_key.sign(payload, *astuple(self._sign_args)) # type: ignore | ||
|
|
||
| return Signature(self.public_key.keyid, sig.hex()) | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.