diff options
Diffstat (limited to 'openpgp')
-rw-r--r-- | openpgp/src/crypto/asymmetric.rs | 5 | ||||
-rw-r--r-- | openpgp/src/crypto/ecdh.rs | 27 | ||||
-rw-r--r-- | openpgp/src/packet/key.rs | 13 | ||||
-rw-r--r-- | openpgp/src/packet/pkesk.rs | 5 |
4 files changed, 27 insertions, 23 deletions
diff --git a/openpgp/src/crypto/asymmetric.rs b/openpgp/src/crypto/asymmetric.rs index 539a9dc5..6857d231 100644 --- a/openpgp/src/crypto/asymmetric.rs +++ b/openpgp/src/crypto/asymmetric.rs @@ -235,9 +235,8 @@ impl Decryptor for KeyPair { (PublicKey::ECDH{ .. }, mpis::SecretKey::ECDH { .. }, mpis::Ciphertext::ECDH { .. }) => - ::crypto::ecdh::unwrap_session_key(&self.public, - &self.secret, - ciphertext)?, + ::crypto::ecdh::decrypt(&self.public, &self.secret, + ciphertext)?, (public, secret, ciphertext) => return Err(Error::InvalidOperation(format!( diff --git a/openpgp/src/crypto/ecdh.rs b/openpgp/src/crypto/ecdh.rs index 2c92608e..70ad1b02 100644 --- a/openpgp/src/crypto/ecdh.rs +++ b/openpgp/src/crypto/ecdh.rs @@ -18,8 +18,7 @@ use nettle::{cipher, curve25519, mode, Mode, ecc, ecdh, Yarrow}; /// Wraps a session key using Elliptic Curve Diffie-Hellman. #[allow(non_snake_case)] -pub fn wrap_session_key(recipient: &Key, session_key: &[u8]) - -> Result<Ciphertext> +pub fn encrypt(recipient: &Key, session_key: &[u8]) -> Result<Ciphertext> { let mut rng = Yarrow::default(); @@ -47,7 +46,7 @@ pub fn wrap_session_key(recipient: &Key, session_key: &[u8]) curve25519::mul(&mut S, &v, R) .expect("buffers are of the wrong size"); - wrap_session_key_deterministic(recipient, session_key, VB, &S) + encrypt_shared(recipient, session_key, VB, &S) } Curve::NistP256 | Curve::NistP384 | Curve::NistP521 => { // Obtain the authenticated recipient public key R and @@ -91,7 +90,7 @@ pub fn wrap_session_key(recipient: &Key, session_key: &[u8]) let S = ecdh::point_mul(&v, &R)?; let (Sx,_) = S.as_bytes(); - wrap_session_key_deterministic(recipient, session_key, VB, &Sx) + encrypt_shared(recipient, session_key, VB, &Sx) } // Not implemented in Nettle @@ -107,11 +106,17 @@ pub fn wrap_session_key(recipient: &Key, session_key: &[u8]) } } -// VB: Ephemeral public key (with 0x40 prefix), -// S: Shared DH secret. +/// Wraps a session key. +/// +/// After using Elliptic Curve Diffie-Hellman to compute a shared +/// secret, this function deterministically encrypts the given session +/// key. +/// +/// `VB` is the ephemeral public key (with 0x40 prefix), `S` is the +/// shared Diffie-Hellman secret. #[allow(non_snake_case)] -pub(crate) fn wrap_session_key_deterministic(recipient: &Key, session_key: &[u8], - VB: MPI, S: &[u8]) -> Result<Ciphertext> +pub fn encrypt_shared(recipient: &Key, session_key: &[u8], VB: MPI, S: &[u8]) + -> Result<Ciphertext> { match recipient.mpis() { &PublicKey::ECDH{ ref curve, ref hash, ref sym,.. } => { @@ -148,9 +153,9 @@ pub(crate) fn wrap_session_key_deterministic(recipient: &Key, session_key: &[u8] /// Unwraps a session key using Elliptic Curve Diffie-Hellman. #[allow(non_snake_case)] -pub fn unwrap_session_key(recipient: &Key, recipient_sec: &SecretKey, - ciphertext: &Ciphertext) - -> Result<Box<[u8]>> { +pub fn decrypt(recipient: &Key, recipient_sec: &SecretKey, + ciphertext: &Ciphertext) + -> Result<Box<[u8]>> { use memsec; match (recipient.mpis(), recipient_sec, ciphertext) { diff --git a/openpgp/src/packet/key.rs b/openpgp/src/packet/key.rs index 65d74fbe..33117085 100644 --- a/openpgp/src/packet/key.rs +++ b/openpgp/src/packet/key.rs @@ -875,10 +875,11 @@ mod tests { let dek = b"\x09\x0D\xDC\x40\xC5\x71\x51\x88\xAC\xBD\x45\x56\xD4\x2A\xDF\x77\xCD\xF4\x82\xA2\x1B\x8F\x2E\x48\x3B\xCA\xBF\xD3\xE8\x6D\x0A\x7C\xDF\x10\xe6"; let sk = SessionKey::from(Vec::from(&dek[..])); - // Expected - let got_enc = ecdh::wrap_session_key_deterministic(&key, &sk, eph_pubkey, shared_sec).unwrap(); + // Expected + let got_enc = ecdh::encrypt_shared(&key, &sk, eph_pubkey, shared_sec) + .unwrap(); - assert_eq!(ciphertext, got_enc); + assert_eq!(ciphertext, got_enc); } #[test] @@ -914,10 +915,10 @@ mod tests { Some(SecretKey::Unencrypted{ ref mpis }) => mpis, _ => unreachable!(), }; - // Expected - let got_dek = ecdh::unwrap_session_key(&key, sec, &ciphertext).unwrap(); + // Expected + let got_dek = ecdh::decrypt(&key, sec, &ciphertext).unwrap(); - assert_eq!(&dek[..], &got_dek[..]); + assert_eq!(&dek[..], &got_dek[..]); } #[test] diff --git a/openpgp/src/packet/pkesk.rs b/openpgp/src/packet/pkesk.rs index 1f638da6..40332c45 100644 --- a/openpgp/src/packet/pkesk.rs +++ b/openpgp/src/packet/pkesk.rs @@ -98,9 +98,8 @@ impl PKESK3 { } }, - ECDH => { - ecdh::wrap_session_key(recipient, &psk)? - } + ECDH => ecdh::encrypt(recipient, &psk)?, + algo => return Err(Error::UnsupportedPublicKeyAlgorithm(algo).into()), }; |