diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2023-05-12 14:14:31 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2023-05-22 12:00:10 +0200 |
commit | 3e84692aa4cbf047ca8fb16ca424deed8d9c3769 (patch) | |
tree | a757a823b9fc1ea184ed48dc270c7c457017ee0e | |
parent | 2245b12bb056a390a5bb3541ce72ef66835297e6 (diff) |
openpgp: Deduplicate Key::import_secret_ed25519.
-rw-r--r-- | openpgp/src/crypto/backend/botan/asymmetric.rs | 24 | ||||
-rw-r--r-- | openpgp/src/crypto/backend/cng/asymmetric.rs | 34 | ||||
-rw-r--r-- | openpgp/src/crypto/backend/nettle/asymmetric.rs | 24 | ||||
-rw-r--r-- | openpgp/src/crypto/backend/openssl/asymmetric.rs | 27 | ||||
-rw-r--r-- | openpgp/src/crypto/backend/rust/asymmetric.rs | 34 | ||||
-rw-r--r-- | openpgp/src/packet/key.rs | 26 |
6 files changed, 26 insertions, 143 deletions
diff --git a/openpgp/src/crypto/backend/botan/asymmetric.rs b/openpgp/src/crypto/backend/botan/asymmetric.rs index 77943875..265b83f7 100644 --- a/openpgp/src/crypto/backend/botan/asymmetric.rs +++ b/openpgp/src/crypto/backend/botan/asymmetric.rs @@ -462,30 +462,6 @@ impl<P: key::KeyParts, R: key::KeyRole> Key<P, R> { impl<R> Key4<SecretParts, R> where R: key::KeyRole, { - /// Creates a new OpenPGP secret key packet for an existing Ed25519 key. - /// - /// The ECDH key will use hash algorithm `hash` and symmetric - /// algorithm `sym`. If one or both are `None` secure defaults - /// will be used. The key will have it's creation date set to - /// `ctime` or the current time if `None` is given. - pub fn import_secret_ed25519<T>(private_key: &[u8], ctime: T) - -> Result<Self> where T: Into<Option<SystemTime>> - { - let secret = Privkey::load_ed25519(private_key)?; - let (public, secret) = secret.get_ed25519_key()?; - - Self::with_secret( - ctime.into().unwrap_or_else(crate::now), - PublicKeyAlgorithm::EdDSA, - mpi::PublicKey::EdDSA { - curve: Curve::Ed25519, - q: MPI::new_compressed_point(&public), - }, - mpi::SecretKeyMaterial::EdDSA { - scalar: secret.into(), - }.into()) - } - /// Creates a new OpenPGP public key packet for an existing RSA key. /// /// The RSA key will use public exponent `e` and modulo `n`. The key will diff --git a/openpgp/src/crypto/backend/cng/asymmetric.rs b/openpgp/src/crypto/backend/cng/asymmetric.rs index b2299726..de49587d 100644 --- a/openpgp/src/crypto/backend/cng/asymmetric.rs +++ b/openpgp/src/crypto/backend/cng/asymmetric.rs @@ -803,40 +803,6 @@ impl<R> Key4<SecretParts, R> where R: key::KeyRole, { - /// Creates a new OpenPGP secret key packet for an existing Ed25519 key. - /// - /// The key will have it's creation date set to `ctime` or the current time - /// if `None` is given. - pub fn import_secret_ed25519<T>(private_key: &[u8], ctime: T) -> Result<Self> - where - T: Into<Option<SystemTime>>, - { - // CNG doesn't support EdDSA, use ed25519-dalek instead - use ed25519_dalek::{PublicKey, SecretKey}; - - let private = SecretKey::from_bytes(private_key).map_err(|e| { - Error::InvalidKey(e.to_string()) - })?; - - // Mark MPI as compressed point with 0x40 prefix. See - // https://tools.ietf.org/html/draft-ietf-openpgp-rfc4880bis-07#section-13.2. - let mut public = [0u8; 1 + CURVE25519_SIZE]; - public[0] = 0x40; - public[1..].copy_from_slice(Into::<PublicKey>::into(&private).as_bytes()); - - Self::with_secret( - ctime.into().unwrap_or_else(crate::now), - PublicKeyAlgorithm::EdDSA, - mpi::PublicKey::EdDSA { - curve: Curve::Ed25519, - q: mpi::MPI::new(&public) - }, - mpi::SecretKeyMaterial::EdDSA { - scalar: private_key.into(), - }.into() - ) - } - /// Creates a new OpenPGP public key packet for an existing RSA key. /// /// The RSA key will use public exponent `e` and modulo `n`. The key will diff --git a/openpgp/src/crypto/backend/nettle/asymmetric.rs b/openpgp/src/crypto/backend/nettle/asymmetric.rs index a83c5c1f..debb8a5b 100644 --- a/openpgp/src/crypto/backend/nettle/asymmetric.rs +++ b/openpgp/src/crypto/backend/nettle/asymmetric.rs @@ -393,30 +393,6 @@ use crate::types::PublicKeyAlgorithm; impl<R> Key4<SecretParts, R> where R: key::KeyRole, { - /// Creates a new OpenPGP secret key packet for an existing Ed25519 key. - /// - /// The ECDH key will use hash algorithm `hash` and symmetric - /// algorithm `sym`. If one or both are `None` secure defaults - /// will be used. The key will have it's creation date set to - /// `ctime` or the current time if `None` is given. - pub fn import_secret_ed25519<T>(private_key: &[u8], ctime: T) - -> Result<Self> where T: Into<Option<SystemTime>> - { - let mut public_key = [0; ed25519::ED25519_KEY_SIZE]; - ed25519::public_key(&mut public_key, private_key).unwrap(); - - Self::with_secret( - ctime.into().unwrap_or_else(crate::now), - PublicKeyAlgorithm::EdDSA, - mpi::PublicKey::EdDSA { - curve: Curve::Ed25519, - q: MPI::new_compressed_point(&public_key), - }, - mpi::SecretKeyMaterial::EdDSA { - scalar: private_key.into(), - }.into()) - } - /// Creates a new OpenPGP public key packet for an existing RSA key. /// /// The RSA key will use public exponent `e` and modulo `n`. The key will diff --git a/openpgp/src/crypto/backend/openssl/asymmetric.rs b/openpgp/src/crypto/backend/openssl/asymmetric.rs index cee5ebba..d45443fd 100644 --- a/openpgp/src/crypto/backend/openssl/asymmetric.rs +++ b/openpgp/src/crypto/backend/openssl/asymmetric.rs @@ -456,33 +456,6 @@ impl<R> Key4<SecretParts, R> where R: key::KeyRole, { - /// Creates a new OpenPGP secret key packet for an existing Ed25519 key. - /// - /// The ECDH key will use hash algorithm `hash` and symmetric - /// algorithm `sym`. If one or both are `None` secure defaults - /// will be used. The key will have it's creation date set to - /// `ctime` or the current time if `None` is given. - pub fn import_secret_ed25519<T>(private_key: &[u8], ctime: T) -> Result<Self> - where - T: Into<Option<SystemTime>>, - { - let key = PKey::private_key_from_raw_bytes(private_key, openssl::pkey::Id::ED25519)?; - let public_key = key.raw_public_key()?; - - Self::with_secret( - ctime.into().unwrap_or_else(crate::now), - PublicKeyAlgorithm::EdDSA, - mpi::PublicKey::EdDSA { - curve: Curve::Ed25519, - q: public_key.into(), - }, - mpi::SecretKeyMaterial::EdDSA { - scalar: private_key.into(), - } - .into(), - ) - } - /// Creates a new OpenPGP public key packet for an existing RSA key. /// /// The RSA key will use public exponent `e` and modulo `n`. The key will diff --git a/openpgp/src/crypto/backend/rust/asymmetric.rs b/openpgp/src/crypto/backend/rust/asymmetric.rs index 13049578..d52b66b9 100644 --- a/openpgp/src/crypto/backend/rust/asymmetric.rs +++ b/openpgp/src/crypto/backend/rust/asymmetric.rs @@ -465,40 +465,6 @@ impl<P: key::KeyParts, R: key::KeyRole> Key<P, R> { impl<R> Key4<SecretParts, R> where R: key::KeyRole, { - /// Creates a new OpenPGP secret key packet for an existing Ed25519 key. - /// - /// The ECDH key will use hash algorithm `hash` and symmetric - /// algorithm `sym`. If one or both are `None` secure defaults - /// will be used. The key will have it's creation date set to - /// `ctime` or the current time if `None` is given. - pub fn import_secret_ed25519<T>(private_key: &[u8], ctime: T) - -> Result<Self> where T: Into<Option<SystemTime>> - { - use ed25519_dalek::{PublicKey, SecretKey}; - - let private = SecretKey::from_bytes(private_key).map_err(|e| { - Error::InvalidKey(e.to_string()) - })?; - - // Mark MPI as compressed point with 0x40 prefix. See - // https://tools.ietf.org/html/draft-ietf-openpgp-rfc4880bis-07#section-13.2. - let mut public = [0u8; 1 + CURVE25519_SIZE]; - public[0] = 0x40; - public[1..].copy_from_slice(Into::<PublicKey>::into(&private).as_bytes()); - - Self::with_secret( - ctime.into().unwrap_or_else(crate::now), - PublicKeyAlgorithm::EdDSA, - mpi::PublicKey::EdDSA { - curve: Curve::Ed25519, - q: mpi::MPI::new(&public) - }, - mpi::SecretKeyMaterial::EdDSA { - scalar: private_key.into(), - }.into() - ) - } - /// Creates a new OpenPGP public key packet for an existing RSA key. /// /// The RSA key will use public exponent `e` and modulo `n`. The key will diff --git a/openpgp/src/packet/key.rs b/openpgp/src/packet/key.rs index 2750e7a3..4f9b496a 100644 --- a/openpgp/src/packet/key.rs +++ b/openpgp/src/packet/key.rs @@ -1089,6 +1089,32 @@ impl<R> Key4<SecretParts, R> scalar: private_key.into(), }.into()) } + + /// Creates a new OpenPGP secret key packet for an existing Ed25519 key. + /// + /// The ECDH key will use hash algorithm `hash` and symmetric + /// algorithm `sym`. If one or both are `None` secure defaults + /// will be used. The key will have it's creation date set to + /// `ctime` or the current time if `None` is given. + pub fn import_secret_ed25519<T>(private_key: &[u8], ctime: T) + -> Result<Self> where T: Into<Option<time::SystemTime>> + { + use crate::crypto::backend::{Backend, interface::Asymmetric}; + + let private_key = Protected::from(private_key); + let public_key = Backend::ed25519_derive_public(&private_key)?; + + Self::with_secret( + ctime.into().unwrap_or_else(crate::now), + PublicKeyAlgorithm::EdDSA, + mpi::PublicKey::EdDSA { + curve: Curve::Ed25519, + q: mpi::MPI::new_compressed_point(&public_key), + }, + mpi::SecretKeyMaterial::EdDSA { + scalar: private_key.into(), + }.into()) + } } impl<P, R> Key4<P, R> |