Skip to content

[Feature request] Allow to import/export a X25519 private key #4386

Closed

Description

@simo5

X25519 private keys can be generated but cannot currently be exported.
This prevents passing them between process or saving to disk for whatever reason.

Activity

  1. changed the title [-][Feature request] Allow to export a X25519 private key[/-] [+][Feature request] Allow to import/export a X25519 private key[/+] on Aug 7, 2018
  2. simo5 commented on Aug 7, 2018

    @simo5
    ContributorAuthor

    Also allow to import it [edited subject to reflect]

  3. evilaliv3 commented on Aug 28, 2018

    @evilaliv3
    Contributor

    This would be really useful also in relation to ECIES encryption

    Many libraries like libsodium and opennpgp.js enable this, and many software like signal/whatapp already uses constructions where x25519 is adopted for encryption with preference over NIST curves.

    It would be really important for many interoperability use cases to expose:

    • private_bytes()
    • encoding in PKCS8
  4. ronf commented on Aug 29, 2018

    @ronf

    Curve25519 is also used in SSH, for both an ECDH key exchange and as an asymmetric encryption/signing mechanism via Ed25519. I haven’t had a chance to look closely at the implementation in Cryptography yet, but here are the primitives I'm currently using from the libnacl/libsodium implementation to implement Curve25519 DH:

    crypto_scalarmult_curve25519_bytes: Return the size of a Curve25519 public key (point value)
    crypto_scalarmult_curve25519_scalarbytes: Return the size of a Curve25519 private key (scalar value)
    crypto_scalarmult_curve25519: Compute a shared key from a private key and a peer public key
    crypto_scalarmult_curve25519_base: Compute the public key from a private key

    The shim I put around this starts by generating a random private key and has a get_public() call to return the public key to share with the peer and a get_shared(peer_public) call that takes a peer public key and returns a shared key. You can see the code for this at https://github.com/ronf/asyncssh/blob/master/asyncssh/crypto/curve25519.py.

    In addition, I use the following from libnacl/libsodium to implement Ed25519:

    crypto_sign_keypair: Generate a new key pair
    crypto_sign: Sign a message with a private key
    crypto_sign_open: Verify a message signature with a public key

    Here, crypto_sign_keypair() is used to generate a new key pair, crypto_sign(msg, private) is used in the sign operation, and crypto_sign_open(sig + data, public) is used in the verify operation.

    This is a bit more involved since there's key serialization to deal with in various formats in addition to the sign & verify operations. This code can be found at https://github.com/ronf/asyncssh/blob/master/asyncssh/ed25519.py.

    Since I wanted to interoperate with OpenSSH, I'm using its serialization format and currently don't support PKCS8 DER & PEM formats, but that's something I may add in the future if other tools generate such keys. To perform my own serialization into OpenSSH format, I would need a way to get both the private and public bytes if I wanted to use the cryptography implementation of this, and I would also need it to support sign & verify operations similar to what libnacl/libsodium does.

  5. reaperhulk commented on Oct 8, 2018

    @reaperhulk
    Member

    Re: PKCS8 support. I looked at implementing this today and there are two tricky bits:

    1. What do we do with the existing public_bytes method. For backwards compatibility we can allow it to take None for both encoding and format and output the current 32 byte little endian byte string, but that needs to be deprecated. However, we probably don't want to add Raw to PublicFormat since it would only have meaning with Ed25519 and X25519 and we'd need to add cases to prohibit it on all other key types. Do we want to add a raw_public_bytes method? Two bytes methods seems a bit odd.

    On the private key serialization side things are a bit simpler since private_bytes is a new method, but we need to decide what to do about making the from_private_bytes classmethod. For RSA/DSA/EC we've separated DER/PEM by having load_pem_private_key and load_der_private_key, but do we want to have one method just take bytes and Do The Right Thing here? If so we'd need to have it handle raw bytes, DER PKCS8, and PEM PKCS8. At the moment we have code that does all three, but due to the types of exceptions we raise it would be difficult to reuse the PEM/DER PKCS8 parsing code.

  6. simo5 commented on Oct 16, 2018

    @simo5
    ContributorAuthor

    I hve some code somewhere that I started workning on for private bytes, I kept the separation of tiypes and it seemed simple enough.
    For public bytes I think we should should not add raw public bytes, it is easy enough to get "raw" ones if you really need to from current oublic_bytes, I think.

  7. reaperhulk commented on Oct 16, 2018

    @reaperhulk
    Member

    The current public_bytes on X25519PublicKey already exposes raw (indeed it's the only serialization it allows), so we're not adding it, we're retaining compatibility. We could choose to add an additional utility function that takes a PKCS8 key and outputs the raw bytes form rather than restructuring our API. We would still need None to return the raw value during the deprecation period though. Not sure how I feel about that. It's definitely way less work than #4497

  8. reaperhulk commented on Jan 15, 2019

    @reaperhulk
    Member

    This is now possible in master

  9. locked as resolved and limited conversation to collaborators on Jul 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions