diff options
author | Wiktor Kwapisiewicz <wiktor@metacode.biz> | 2023-03-23 09:59:46 +0100 |
---|---|---|
committer | Wiktor Kwapisiewicz <wiktor@metacode.biz> | 2023-03-23 14:22:59 +0100 |
commit | eab194fceb8acbe436ad6ab60f89a957f2e61bd2 (patch) | |
tree | 9430f02db8e9d9be07a8a9ba5ac9d0a9eae5837a /openpgp/src/crypto/backend/botan/asymmetric.rs | |
parent | b1d8527d1fb56e2d97c8a285d2ff1dc1e931d4c6 (diff) |
openpgp: Move `Key4::import_secret_cv25519` into common code.
- Most of the logic in this function is the same across backends.
- Introduce `Key4::derive_cv25519_public_key` that does
backend-specific derivation.
- Fixes https://gitlab.com/sequoia-pgp/sequoia/-/issues/958
Diffstat (limited to 'openpgp/src/crypto/backend/botan/asymmetric.rs')
-rw-r--r-- | openpgp/src/crypto/backend/botan/asymmetric.rs | 34 |
1 files changed, 2 insertions, 32 deletions
diff --git a/openpgp/src/crypto/backend/botan/asymmetric.rs b/openpgp/src/crypto/backend/botan/asymmetric.rs index bf9b53c6..51ed2edc 100644 --- a/openpgp/src/crypto/backend/botan/asymmetric.rs +++ b/openpgp/src/crypto/backend/botan/asymmetric.rs @@ -29,7 +29,6 @@ use crate::{ Curve, HashAlgorithm, PublicKeyAlgorithm, - SymmetricAlgorithm, }, }; @@ -392,40 +391,11 @@ 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 X25519 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_cv25519<H, S, T>(private_key: &[u8], - hash: H, sym: S, ctime: T) - -> Result<Self> where H: Into<Option<HashAlgorithm>>, - S: Into<Option<SymmetricAlgorithm>>, - T: Into<Option<SystemTime>> + pub(crate) fn derive_cv25519_public_key(private_key: &Protected) -> Result<[u8; 32]> { let secret = Privkey::load_x25519(private_key)?; - let public = secret.pubkey()?.get_x25519_key()?; - let mut secret = secret.get_x25519_key()?; - - // OpenPGP stores the secret in reverse order. - secret.reverse(); - use crate::crypto::ecdh; - Self::with_secret( - ctime.into().unwrap_or_else(crate::now), - PublicKeyAlgorithm::ECDH, - mpi::PublicKey::ECDH { - curve: Curve::Cv25519, - hash: hash.into().unwrap_or_else( - || ecdh::default_ecdh_kdf_hash(&Curve::Cv25519)), - sym: sym.into().unwrap_or_else( - || ecdh::default_ecdh_kek_cipher(&Curve::Cv25519)), - q: MPI::new_compressed_point(&public), - }, - mpi::SecretKeyMaterial::ECDH { - scalar: secret.into(), - }.into()) + Ok(<[u8; 32]>::try_from(&secret.pubkey()?.get_x25519_key()?[..])?) } /// Creates a new OpenPGP secret key packet for an existing Ed25519 key. |