Repository navigation
#12520 Add support for mlkem768x25519-sha256 for server-side connections using cryptography. - #12730
Conversation
…y supported kex algorithm check
gudnimg
left a comment
There was a problem hiding this comment.
Looks good to me. Just minor nitpicks about type annotation :)
| def test_CURVE25519_SHA256(self): | ||
| """ | ||
| The curve25519-sha256 key exchange algorithm is compatible with OpenSSH. | ||
| """ | ||
| return self.assertExecuteWithKexAlgorithm("curve25519-sha256") | ||
|
|
||
| def test_MLKEM768X25519_SHA256(self): | ||
| """ | ||
| The mlkem768x25519-sha256 key exchange algorithm is compatible with OpenSSH. | ||
| """ | ||
| return self.assertExecuteWithKexAlgorithm("mlkem768x25519-sha256") |
There was a problem hiding this comment.
Consider adding type annotation for new code. assertExecuteWithKexAlgorithm returns Deferred[Any] as far as I can tell.
| def test_CURVE25519_SHA256(self): | |
| """ | |
| The curve25519-sha256 key exchange algorithm is compatible with OpenSSH. | |
| """ | |
| return self.assertExecuteWithKexAlgorithm("curve25519-sha256") | |
| def test_MLKEM768X25519_SHA256(self): | |
| """ | |
| The mlkem768x25519-sha256 key exchange algorithm is compatible with OpenSSH. | |
| """ | |
| return self.assertExecuteWithKexAlgorithm("mlkem768x25519-sha256") | |
| def test_CURVE25519_SHA256(self) -> Deferred[Any]: | |
| """ | |
| The curve25519-sha256 key exchange algorithm is compatible with OpenSSH. | |
| """ | |
| return self.assertExecuteWithKexAlgorithm("curve25519-sha256") | |
| def test_MLKEM768X25519_SHA256(self) -> Deferred[Any]: | |
| """ | |
| The mlkem768x25519-sha256 key exchange algorithm is compatible with OpenSSH. | |
| """ | |
| return self.assertExecuteWithKexAlgorithm("mlkem768x25519-sha256") |
There was a problem hiding this comment.
Very much second this. If we're not going to have any type annotations we should at least have a brief review of why it's too much work.
| """ | ||
|
|
||
| # These variables don't comply with Twisted naming convention. | ||
| # The names are kept to match the RFC. |
There was a problem hiding this comment.
Maybe a dumb comment, but would it be useful to have a specific number for this RFC in the comment? Just as a source of reference if anyone needs to look it up.
There was a problem hiding this comment.
There is a RFC link somewhere else in the docs.
I didn't want to add a specific RFC reference here...since the current version is still draft... and I was trying to avoid having this reference invalid in the future.
The type-annotation comments are valid... unless mypy complains about it, I most probably will forget to add them again in the future.
I don't use type annotation for Python on my day to day job, but I agree that type annotation are helpful to read the Twisted code.
| """ | ||
|
|
||
| def checkDisconnected(self, kind=None): | ||
| def checkDisconnected(self, kind=None, description=None): |
There was a problem hiding this comment.
Would be nice to add type annotation here. But not a blocker imo.
There was a problem hiding this comment.
same - is this hard to type check?
|
Thanks for the review. I hope @FinnRG will have some time to take a look at the changes. I would like to get this merge, then implement client-side support and then do a release |
glyph
left a comment
There was a problem hiding this comment.
There are some cleanups here, but the only blockers are:
except Exception- type annotations themselves aren't required, but we should at least have some demonstrative error output to show that it's actually difficult to try.
I am going with a "request changes" because we seem to have had a few issues with accidental auto-merges going through after a "make some changes and land" approve goes through? (I don't have any examples to hand, forgive me if those weren't actually on Twisted, I get way too much email right now.) But the changes required should be pretty trivial.
Maybe a feature to add to the bot, to allow it to do an approve after a self-review for PRs in that state?
| import cryptography.hazmat.primitives.hpke # noqa: F401 | ||
| except ImportError: | ||
| # Most probably we have cryptography older than 48.0 | ||
| is_ml_kem_supported = False |
There was a problem hiding this comment.
| is_ml_kem_supported = False | |
| cryptographyHasMLKEM = False |
There was a problem hiding this comment.
I know it's a private namespace, but let's stick with coding-standard compliant names.
There was a problem hiding this comment.
np. it's ok. I don't use this convention for my other project, so I kind of forget to use the Twisted coding convention.
| from cryptography.hazmat.primitives.asymmetric.mlkem import MLKEM768PrivateKey | ||
|
|
||
|
|
||
| def skipWithoutMLKEM(f): |
There was a problem hiding this comment.
docstirng & type annotation please? I don't want to implicitly lose type coverage for every test this decorates
There was a problem hiding this comment.
I tried to add anotation... not sure if this is the right way... I went a bit down a small rabbit hole :P
| """ | ||
|
|
||
| def checkDisconnected(self, kind=None): | ||
| def checkDisconnected(self, kind=None, description=None): |
There was a problem hiding this comment.
same - is this hard to type check?
| kexAlgorithm | ||
| for kexAlgorithm in t.supportedKeyExchanges | ||
| if _kex.isFixedGroup(kexAlgorithm) or _kex.isEllipticCurve(kexAlgorithm) | ||
| if not _kex.isNonFixedGroup(kexAlgorithm) |
There was a problem hiding this comment.
Glad to have contributors qualified to understand if this is valid or not because I do not understand the math :)
There was a problem hiding this comment.
:) I tried to rename it. thanks for the feedback.
| try: | ||
| peerKey = x25519.X25519PublicKey.from_public_bytes(c_pk1) | ||
| traditionalExchange = ourKey.exchange(peerKey) | ||
| except Exception: | ||
| self.sendDisconnect( | ||
| DISCONNECT_KEY_EXCHANGE_FAILED, "Invalid peer X25519 public key" | ||
| ) | ||
| return |
There was a problem hiding this comment.
Yikes! Never catch Exception and then return without any kind of report. Please catch a specific exception type here, or use failuresHandled if this is a place a traceback should be reported. (I think that this is actually just a specific exception type issue, unless I'm misunderstanding; I don't think exchange is potentially arbitrary application code)
There was a problem hiding this comment.
I will catch the specific exception and guard only the required code.
This was lazy coding :)
I don't think that we need to log this.
The reporting is done via the SSH process.
Here the peer sends us junk/invalid data.
The remote SSH peer might be just send us invalid data hoping for a security error.
| try: | ||
| # encapsulate returns (ciphertext, shared_secret) | ||
| peerMLKEM = mlkem.MLKEM768PublicKey.from_public_bytes(c_pk2) | ||
| primitiveSharedSecret, ciperText = peerMLKEM.encapsulate() | ||
| except Exception: | ||
| self.sendDisconnect( | ||
| DISCONNECT_KEY_EXCHANGE_FAILED, "Invalid peer ML-KEM public key" | ||
| ) | ||
| return |
There was a problem hiding this comment.
Once again, please catch specific exceptions here.
|
|
||
| isClient = True | ||
|
|
||
| # FIXME https://github.com/twisted/twisted/issues/12624 |
There was a problem hiding this comment.
Thanks for filing this as a separate issue.
| def test_CURVE25519_SHA256(self): | ||
| """ | ||
| The curve25519-sha256 key exchange algorithm is compatible with OpenSSH. | ||
| """ | ||
| return self.assertExecuteWithKexAlgorithm("curve25519-sha256") | ||
|
|
||
| def test_MLKEM768X25519_SHA256(self): | ||
| """ | ||
| The mlkem768x25519-sha256 key exchange algorithm is compatible with OpenSSH. | ||
| """ | ||
| return self.assertExecuteWithKexAlgorithm("mlkem768x25519-sha256") |
There was a problem hiding this comment.
Very much second this. If we're not going to have any type annotations we should at least have a brief review of why it's too much work.
|
Thanks for the great review. I hope that I have addressed all the comments. This should be ready for another review. Some tests are failding due to some Ubuntu package mirrors |
glyph
left a comment
There was a problem hiding this comment.
If we can address the coverage gap I think it's ready to go.
| kexAlgorithms = [] | ||
| openSSHPath = which("ssh") | ||
| if not openSSHPath: | ||
| raise SkipTest("ssh client not found") |
There was a problem hiding this comment.
coverage gap: this test can be done at import time and use the @skipIf decorator to avoid having this branch actually in the code.
| except BaseException: | ||
|
|
||
| kexAlgorithms = output.decode("utf-8").split() | ||
| except Exception: |
There was a problem hiding this comment.
it looks like this just … never fails? can we just remove the whole try/except, then? no point in having this if it's not actually a supported environment
| skip: str | None = zi.Attribute( | ||
| "The reason why this test case should be skipped, or C{None} to not skip." | ||
| ) | ||
|
|
There was a problem hiding this comment.
interesting drive-by typing, but glad to have it! :)
| """ | ||
|
|
||
|
|
||
| ITestCaseVar = TypeVar("ITestCaseVar", bound=Union[ITestCase, type[ITestCase]]) |
There was a problem hiding this comment.
Could we keep this private just to avoid exposing soon-to-be-obsolete TypeVar objects?
| ITestCaseVar = TypeVar("ITestCaseVar", bound=Union[ITestCase, type[ITestCase]]) | |
| _ITestCaseVar = TypeVar("_ITestCaseVar", bound=Union[ITestCase, type[ITestCase]]) |
There was a problem hiding this comment.
Thanks for the review.
Agree.. this, together with the "skip" update in the interface were cause by adding type annotation to the skipif decorator. I will make it private.
I will refactor and send back for review
|
Thanks for there review. COverage is done without any exceptions. I used skipf |
Scope and purpose
Fixes #12520
This is based on the initial code from @FinnRG from #12621
99% is the code from Finn. I only converted to cryptography, updated the naming conventions, enabled a few more tests, add strict checks in new tests.
I did some refactoring while validating the implementation against the RFC and coverted variable names to Twisted standard
I have updated the code to use the Python cryptography library, rather than a separate library.
This will need cryptography 48.0 or newer and Twisted should just don't advertise support for MLKEM on older cryptography
I have also enabled the end to end KEX tests with OpenSSH on Ubuntu 26.04
Contributor Checklist:
This process applies to all pull requests - no matter how small.
Have a look at our developer documentation before submitting your Pull Request.
Below is a non-exhaustive list (as a reminder):
please review.Our bot will trigger the review process, by applying the pending review label
and requesting a review from the Twisted dev team.