Skip to content

add Ed25519Key.generate() and drop requirement of nacl - #2103

Open
boyxuper wants to merge 1 commit into
paramiko:mainfrom
boyxuper:main
Open

boyxuper wants to merge 1 commit into
paramiko:mainfrom
boyxuper:main

Conversation

@boyxuper

@boyxuper boyxuper commented Aug 22, 2022 •

Copy link
Copy Markdown

note: cryptography>=2.6 has ed25519 support, so maybe we can boost the version requirement to 2.6 for merge test?

@boyxuper

Copy link
Copy Markdown
Author

@bitprophet maybe someone can review this pr?

@bskinn

bskinn commented Sep 27, 2022

Copy link
Copy Markdown
Contributor

The next unit of work for paramiko is planned to be the drop of Python 2 and older 3.x, after which will be the major key/auth overhaul indicated by #387.

Please correct me if I'm wrong, but it looks like this PR would be under the umbrella of the auth overhaul? If so, we'll flag this as something to consider as part of that effort.

Either way, thank you for the contribution! We'll get it worked in as soon as possible...no firm idea of what that timeline will be, though.

@utkonos

utkonos commented Dec 27, 2022 •

Copy link
Copy Markdown

I figured I'd add the following over here too in case someone is searching for a way to go ahead and generate ed25519 keys and use the result with paramiko right now and not wait.

The following code snippet works for ed25519 key generation using cryptography and then using io.StringIO as an interface to get the generated key into paramiko. From there, you're off to the races.

import io

from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives import asymmetric
import paramiko

c_ed25519key = asymmetric.ed25519.Ed25519PrivateKey.generate()
privpem = c_ed25519key.private_bytes(encoding=serialization.Encoding.PEM,
                                     format=serialization.PrivateFormat.OpenSSH,
                                     encryption_algorithm=serialization.NoEncryption())
print(privpem.decode())

priv_obj = io.StringIO(privpem.decode())
p_ed25519key = paramiko.ed25519key.Ed25519Key.from_private_key(priv_obj)
print(p_ed25519key.get_base64())

pub = c_ed25519key.public_key()
openssh_pub = pub.public_bytes(encoding=serialization.Encoding.OpenSSH,
                               format=serialization.PublicFormat.OpenSSH)
print(openssh_pub)

This and other info is over in
#1136

And just in case it's not crystal clear what is happening in that code, the p_ed25519key variable is a paramiko Ed25519Key object. These screenshots with the variable names changed and a type() should make it clear what's there:

image

image

So, you would use it as expected:

ssh_client.connect(hostname=ip, username='ubuntu', pkey=pkey)

@bskinn

bskinn commented Dec 27, 2022

Copy link
Copy Markdown
Contributor

Thanks very much for the research and implementation here, @utkonos! I expect they'll be helpful once the key/auth overhaul (#387) gets underway.

@utkonos

utkonos commented Dec 27, 2022

Copy link
Copy Markdown

I don't think much needs to be done to change Ed25519Key to add a generate() method or allow key material to be a direct input parameter to that class. The former is probably 3-4 lines of code maximum, and the latter is just as few. Right now the class object can accept key material in PEM format and once in there the key material is stored exactly as it is stored in the object that cryptography emits. I'm sure there are bureaucratic reasons to wait, but I don't see any technical reasons to wait.

No worries tho. Proceed as you see fit.

@bskinn

bskinn commented Dec 27, 2022

Copy link
Copy Markdown
Contributor

Nod, it does seem like a fairly small, straightforward change. But, I'm not deeply familiar with the codebase---I'm just helping triage---so I hesitate to say anything authoritative.

In any event, it'll be bitprophet's call how he wants to tackle it. If the key handling system is going to be substantially different from the current version, I could see it making more sense only taking the time to implement and release it the new way.

@utkonos

utkonos commented Dec 27, 2022 •

Copy link
Copy Markdown

Totally understand. I can see that the PR that we're taking under touches quite a few things. I would propose a much more narrow PR that simply adds a generate() method and nothing else. Right now, one has to manually get the key material into the class through a complex pathway, but once the key material is in there, everything else is the same. paramiko is and has been setup to use this key type for a while, there should be no other code changes required. The removal of nacl and any other code changes can wait for an overhaul.

But again, no worries. There is always monkey patching. Which is what I'm looking at next.

@utkonos

utkonos commented Dec 27, 2022

Copy link
Copy Markdown

I am incorrect about the above. All the nacl code does need to be removed for this all to work correctly. The contents of pkey.signing_key is a nacl object. Nothing will work unless nacl is removed completely and cryptography is coded in place of it. However, I was able to do a quick monkeypatch that just adds the bare minimum code using nacl to get the job done.

image

@bskinn

bskinn commented Jan 3, 2023

Copy link
Copy Markdown
Contributor

Thanks again for all your work here, @utkonos -- it'll be a big help.

@bitprophet

bitprophet commented Jan 9, 2023 •

Copy link
Copy Markdown
Member

Curious how this and #1806 overlap 🤔 I may try folding this or that, into the 3.0 release after all (it'd let us remove a dependency outright!), but per Brian's notes, this may instead wait for a later feature-oriented release.

@boyxuper

boyxuper commented Jan 4, 2026

Copy link
Copy Markdown
Author

@bitprophet just rebased on v4.0. can we review this after years? :D

toabctl added a commit to toabctl/paramiko that referenced this pull request Aug 26, 2026
Ed25519 support arrived in 2.2 (paramiko#325 via paramiko#972) backed by PyNaCl,
because Cryptography had no Ed25519 primitive at the time; it landed in
Cryptography 2.6, roughly two years later. Our floor is already 3.3, so
the primitive has been available across the entire supported range, and
Ed25519Key was the last remaining user of PyNaCl.

Beyond dropping a C-extension dependency, this puts Ed25519 under the
same OpenSSL configuration that already governs every other key type,
cipher and KEX we use -- including any algorithm policy the host
OpenSSL enforces, which a separately bundled libsodium cannot
participate in. Compare KexCurve25519.is_available(), which already
defers to Cryptography for exactly that reason.

Implementation notes:

* Uses public_bytes(Encoding.Raw, PublicFormat.Raw) rather than the
  tidier public_bytes_raw(), which only exists as of Cryptography 40.
  The older spelling works across our whole supported range, including
  the 3.3.2 and 39.0.0 cells in CI.

* Ed25519Key._fields now holds the key's raw bytes rather than the key
  object itself. Cryptography's key objects compare by identity on
  older versions (3.3.2, 39.0.0) and are unhashable on newer ones, so
  neither is safe for the equality and hashing contract PKey relies on
  here; raw bytes are exact on every version. test_ed25519_compare
  gains a hash() assertion so it covers both failure modes.

* Cryptography's verify() takes (signature, data), the reverse of
  PyNaCl's verify(smessage, signature).

Wire format is unchanged. Ed25519 is deterministic, so the new
test_sign_and_verify_ed25519 pins the exact signature bytes the PyNaCl
implementation produced for the existing test key.

The one user-visible change is Ed25519Key.verifying_key, which now
returns an Ed25519PublicKey rather than a nacl.signing.VerifyKey.

Refs paramiko#1419, paramiko#2527. Builds on the earlier attempts by @adiroiban in
paramiko#1806 and @boyxuper in paramiko#2103.
toabctl added a commit to toabctl/paramiko that referenced this pull request Aug 26, 2026
Ed25519 support arrived in 2.2 (paramiko#325 via paramiko#972) backed by PyNaCl,
because Cryptography had no Ed25519 primitive at the time; it landed in
Cryptography 2.6, roughly two years later. Our floor is already 3.3, so
the primitive has been available across the entire supported range, and
Ed25519Key was the last remaining user of PyNaCl.

Beyond dropping a C-extension dependency, this puts Ed25519 under the
same OpenSSL configuration that already governs every other key type,
cipher and KEX we use -- including any algorithm policy the host
OpenSSL enforces, which a separately bundled libsodium cannot
participate in. Compare KexCurve25519.is_available(), which already
defers to Cryptography for exactly that reason.

Implementation notes:

* Uses public_bytes(Encoding.Raw, PublicFormat.Raw) rather than the
  tidier public_bytes_raw(), which only exists as of Cryptography 40.
  The older spelling works across our whole supported range, including
  the 3.3.2 and 39.0.0 cells in CI.

* Ed25519Key._fields now holds the key's raw bytes rather than the key
  object itself. Cryptography's key objects compare by identity on
  older versions (3.3.2, 39.0.0) and are unhashable on newer ones, so
  neither is safe for the equality and hashing contract PKey relies on
  here; raw bytes are exact on every version. test_ed25519_compare
  gains a hash() assertion so it covers both failure modes.

* Cryptography's verify() takes (signature, data), the reverse of
  PyNaCl's verify(smessage, signature).

Wire format is unchanged. Ed25519 is deterministic, so the new
test_sign_and_verify_ed25519 pins the exact signature bytes the PyNaCl
implementation produced for the existing test key.

The one user-visible change is Ed25519Key.verifying_key, which now
returns an Ed25519PublicKey rather than a nacl.signing.VerifyKey.

Refs paramiko#1419, paramiko#2527. Builds on the earlier attempts by @adiroiban in
paramiko#1806 and @boyxuper in paramiko#2103.
toabctl added a commit to toabctl/paramiko that referenced this pull request Aug 26, 2026
Ed25519Key was the last thing using PyNaCl, and only did so because
Cryptography had no Ed25519 primitive back when 2.2 shipped; it grew one
in 2.6 and our floor is already 3.3. Wire format is unchanged.

_fields now holds the key's raw bytes rather than the key object, since
Cryptography's key objects compare by identity on older versions and are
unhashable on newer ones. verifying_key returns an Ed25519PublicKey
rather than a nacl.signing.VerifyKey, which is the one API change.

Refs paramiko#1419, paramiko#2527. Same approach as paramiko#1806 and paramiko#2103.
toabctl added a commit to toabctl/paramiko that referenced this pull request Aug 31, 2026
Ed25519Key was the last thing using PyNaCl, and only did so because
Cryptography had no Ed25519 primitive back when 2.2 shipped; it grew one
in 2.6 and our floor is already 3.3. Wire format is unchanged.

_fields now holds the key's raw bytes rather than the key object, since
Cryptography's key objects compare by identity on older versions and are
unhashable on newer ones. verifying_key returns an Ed25519PublicKey
rather than a nacl.signing.VerifyKey, which is the one API change.

Refs paramiko#1419, paramiko#2527. Same approach as paramiko#1806 and paramiko#2103.
Comment thread paramiko/ed25519key.py
filename=None,
password=None,
file_obj=None,
vals=None,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

do we really need vals here and change the constructor signature?

Comment thread paramiko/ed25519key.py
from paramiko.util import b


def public_bytes_raw(verify_key):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

this is not required I think. it simply calls

self.public_bytes(
            _serialization.Encoding.Raw, _serialization.PublicFormat.Raw
        )

so we can either do the same or create our own helper method instead of calling a private method.

@toabctl

toabctl commented Sep 16, 2026

Copy link
Copy Markdown

@boyxuper could you rebase? @bitprophet is this something that would be accepted?

@toabctl

toabctl commented Sep 28, 2026

Copy link
Copy Markdown

friendly ping. it would be really cool to get rid of nacl and only include a single crypto lib.

@toabctl

toabctl commented Oct 9, 2026

Copy link
Copy Markdown

@boyxuper are you still interested in this change?

@toabctl

toabctl commented Nov 2, 2026

Copy link
Copy Markdown

@boyxuper thx for updating but CI fails now. could you fix it please? (eg. by running black paramiko/ed25519key.py)

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.

5 participants