You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Feature request] Allow to import/export a X25519 private key #4386
X25519 private keys can be generated but cannot currently be exported.
This prevents passing them between process or saving to disk for whatever reason.
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
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:
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
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.
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.
Re: PKCS8 support. I looked at implementing this today and there are two tricky bits:
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.
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.
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
Activity
[-][Feature request] Allow to export a X25519 private key[/-][+][Feature request] Allow to import/export a X25519 private key[/+]simo5 commentedon Aug 7, 2018
Also allow to import it [edited subject to reflect]
evilaliv3 commentedon Aug 28, 2018
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:
ronf commentedon Aug 29, 2018
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.
reaperhulk commentedon Oct 8, 2018
Re: PKCS8 support. I looked at implementing this today and there are two tricky bits:
public_bytesmethod. For backwards compatibility we can allow it to takeNonefor bothencodingandformatand output the current 32 byte little endian byte string, but that needs to be deprecated. However, we probably don't want to addRawtoPublicFormatsince 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 araw_public_bytesmethod? Two bytes methods seems a bit odd.On the private key serialization side things are a bit simpler since
private_bytesis a new method, but we need to decide what to do about making thefrom_private_bytesclassmethod. For RSA/DSA/EC we've separated DER/PEM by havingload_pem_private_keyandload_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.simo5 commentedon Oct 16, 2018
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.
reaperhulk commentedon Oct 16, 2018
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
Noneto return the raw value during the deprecation period though. Not sure how I feel about that. It's definitely way less work than #4497reaperhulk commentedon Jan 15, 2019
This is now possible in master