Skip to content

#12520 Add support for mlkem768x25519-sha256 for server-side connections. - #12621

Closed
FinnRG wants to merge 15 commits into
twisted:trunkfrom
FinnRG:feat/mlkem768
Closed

FinnRG wants to merge 15 commits into
twisted:trunkfrom
FinnRG:feat/mlkem768

Conversation

@FinnRG

@FinnRG FinnRG commented Apr 13, 2026

Copy link
Copy Markdown
Contributor

Scope and purpose

Fixes #12520.

As discussed in #12520 (comment), I've created a small wrapper library around the RustCrypto/ml-kem crate until cryptography adds support for this algorithm.

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):

  • The title of the PR should describe the changes and starts with the associated issue number, like “#9782 Remove twisted.news. #1234 Brief description”.
  • A release notes news fragment file was create in src/twisted/newsfragments/ (see: Release notes fragments docs.)
  • The automated tests were updated.
  • Once all checks are green, request a review by leaving a comment that contains exactly the string please review.
    Our bot will trigger the review process, by applying the pending review label
    and requesting a review from the Twisted dev team.

@codspeed

codspeed Bot commented Apr 13, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 2.14%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 1 improved benchmark
✅ 35 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_http11_server_many_headers 1.2 ms 1.2 ms +2.14%

Comparing FinnRG:feat/mlkem768 (5a83de5) with trunk (b71fe03)

Open in CodSpeed

@FinnRG

FinnRG commented Apr 13, 2026 •

Copy link
Copy Markdown
Contributor Author

(The failing tests are due to missing PQ support on the client side, I'll see if I can resolve the tests without actually implementing it because I don't want to make the PR larger)

@FinnRG

FinnRG commented Apr 13, 2026

Copy link
Copy Markdown
Contributor Author

please review

@FinnRG
FinnRG marked this pull request as ready for review April 13, 2026 14:09
@chevah-robot
chevah-robot requested a review from a team April 13, 2026 14:09

@glyph glyph left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This all looks pretty plausible, although I am not an expert in the relevant domain. I do have some naming requests and deployment concerns though.

Comment thread pyproject.toml Resolved


@implementer(_IPQHybridKexAlgorithm)
class _MLKEM768X25519SHA256:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Truly this is a class name for the ages.

Comment thread src/twisted/conch/ssh/_kex.py Resolved
Comment thread src/twisted/conch/ssh/_kex.py Resolved
@FinnRG

FinnRG commented Apr 13, 2026 •

Copy link
Copy Markdown
Contributor Author

@glyph Thanks for the quick review. I have published a new version of ml-kem-rs with prebuilt wheels for the free-threaded Python versions 3.13t and 3.14t.

I tried adding the typing info, but this caused further mypy warnings (see for example this job) and should probably be done for all tests in that file in a separate PR.

@FinnRG
FinnRG requested a review from glyph April 13, 2026 22:45
@adiroiban adiroiban changed the title #12520 Add support for mlkem768x25519-sha256 #12520 Add support for mlkem768x25519-sha256 for server-side connections. Apr 17, 2026

@adiroiban adiroiban left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this.

I have created a separate ticket for client-side support, so that here we can focus on server-side support.

I did just a quick review and left a few minor comments

I have not read the specification for this new algorithm and I have not validated the algorithm agains the RFC.

I have not yet executed manual tests...
I plan to update my computer to Ubuntu 26.04 ... and I hope that with that I can get a newer OpenSSH version

I am not sure which RFC and revision was used for this implementation.


From OpenSSH website

OpenSSH has offered post-quantum key agreement (KexAlgorithms) by default since release 9.0 (April 2022), initially via the sntrup761x25519-sha512 algorithm. More recently, in OpenSSH 9.9, we have added a second post-quantum key agreement mlkem768x25519-sha256 and it was made the new default scheme in OpenSSH 10.0 (April 2025).

I think that we should also update the end to end / functional tests from here

class OpenSSHKeyExchangeTests(ConchServerSetupMixin, OpenSSHClientMixin, TestCase):
"""
Tests L{SSHTransportBase}'s key exchange algorithm compatibility with
OpenSSH.
"""

On my Ubuntu 24.04 I have

OpenSSH_9.6p1 Ubuntu-3ubuntu13.15, OpenSSL 3.0.13 30 Jan 2024

$ ssh -Q kex
... CLASSIC KEX....
[email protected]

and it looks like GitHub only has 24.04 available as newest Ubuntu ... so we will need to run theses test in a container ... or find a quick way to install OpenSSH 9.9 on Ubuntu 24.04 :(

Comment thread src/twisted/conch/ssh/transport.py Resolved
Comment thread src/twisted/conch/ssh/factory.py Resolved
Comment thread src/twisted/newsfragments/12520.feature Resolved
Comment thread pyproject.toml Resolved
Comment thread pyproject.toml Resolved
Comment thread src/twisted/conch/ssh/transport.py Resolved
@glyph
glyph removed their request for review April 21, 2026 23:23
@glyph

glyph commented Apr 21, 2026

Copy link
Copy Markdown
Member

not sure how the bot state got desynced here but this needs a response to @adiroiban's review

@FinnRG — when you're done dealing with those, just as a workflow thing, please use the magic "please review" phrase to have the bot request a review from the whole team rather than from individuals. adi and I are the most likely ones to review but we should really give others an opportunity to see it in their queue :)

@chevah-robot
chevah-robot requested a review from a team April 21, 2026 23:24
@FinnRG

FinnRG commented Apr 26, 2026

Copy link
Copy Markdown
Contributor Author

@adiroiban I agree that end to end tests would be nice. Since 26.04 released a few days ago it shouldn't take too long for Github to add support for it, do we want to wait for this to happen or do you suggest another approach?

The CI is currently failing (and codspeed shows a performance regression), but none of these seem to be related to my changes.

@FinnRG

FinnRG commented Apr 26, 2026

Copy link
Copy Markdown
Contributor Author

please review

@adiroiban

Copy link
Copy Markdown
Member

I think that I will only have time to look into this by next week.
I was planing to upgrade the Ubuntu version of my laptop ... but could not find the time yet.

In theory, we don't have to wait for GitHub Actions to add support for Ubuntu 26.04...we can run the tests in a container.

As an example, there is this PR #12585 which adds tests for Centos9... to get OpenSSL + FIPS

@adiroiban

adiroiban commented May 25, 2026 •

Copy link
Copy Markdown
Member

For manual test I am using Ubuntu 26.04 default OpenSSH

$ ssh -p 5022 -o KexAlgorithms=mlkem768x25519-sha256 -i ssh-keys/client_rsa  user@localhost

SSH server running as

$ python docs/conch/examples/sshsimpleserver.py 

On trunk I get this error which is expected

$ ssh -p 5022 -o KexAlgorithms=mlkem768x25519-sha256 -i ../twisted/ssh-keys/client_rsa  user@localhost 
Unable to negotiate with 127.0.0.1 port 5022: no matching key exchange method found.

On this branch, it works.


This needs a review, but the high level manual smoke test looks good.

I need to read the specs and check that it matches the implementation.

Thanks!

Comment thread tox.ini
mindeps: appdirs == 1.4.0
mindeps: bcrypt == 3.2.1
# Conch-hpq
mindeps: twisted[conch]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that this line is required.

The idea with mindeps is to pin the oldest version of the supported dependencies

S_REPLY (S_CT2 || S_PK1), and a valid signature over the exchange
hash H. It also sends SSH_MSG_NEWKEYS.
"""
import ml_kem_rs

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor comment.

To avoid local imports and repeating the imports the idea is to try to import the dependence at the module level, catch the import error and set a skip condition.

then mark the affected tests or test case based on that skip condition.

@alex

alex commented Jun 8, 2026

Copy link
Copy Markdown
Member

cryptography ahs support for ML-KEM at this point

@glyph

glyph commented Jun 10, 2026

Copy link
Copy Markdown
Member

Thanks for the update Alex!

@adiroiban

Copy link
Copy Markdown
Member

Thanks for the update.

I think that we can look to refactor this to use cryptography .

I see that ML-KEM was introduced in 48.0.0 - 2026-05-04

@adiroiban

Copy link
Copy Markdown
Member

I think that this can be closed since we now have #12730 which no longer depends on an extra deps.

It just uses a newer cryptography

@adiroiban adiroiban closed this Aug 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Post quantum key agreement algorithms (mlkem768x25519-sha256)

5 participants