From c0b19e2457c992478b79b1724aea16cd6ee4f5bf Mon Sep 17 00:00:00 2001 From: ryan Date: Wed, 17 Apr 2024 15:36:42 -0400 Subject: [PATCH] keyfork-shard: assert shared secrets are contributory --- crates/keyfork-shard/src/lib.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/keyfork-shard/src/lib.rs b/crates/keyfork-shard/src/lib.rs index a672951..cec7e9d 100644 --- a/crates/keyfork-shard/src/lib.rs +++ b/crates/keyfork-shard/src/lib.rs @@ -248,10 +248,9 @@ pub trait Format { // create our shared key let our_key = EphemeralSecret::random(); let our_pubkey_mnemonic = Mnemonic::from_bytes(PublicKey::from(&our_key).as_bytes())?; - let shared_secret = our_key - .diffie_hellman(&PublicKey::from(their_pubkey)) - .to_bytes(); - let hkdf = Hkdf::::new(None, &shared_secret); + let shared_secret = our_key.diffie_hellman(&PublicKey::from(their_pubkey)); + assert!(shared_secret.was_contributory(), bug!("shared secret might be insecure")); + let hkdf = Hkdf::::new(None, shared_secret.as_bytes()); let mut shared_key_data = [0u8; 256 / 8]; hkdf.expand(b"key", &mut shared_key_data)?; @@ -515,8 +514,9 @@ pub fn remote_decrypt(w: &mut impl Write) -> Result<(), Box::new(None, &shared_secret); + let shared_secret = our_key.diffie_hellman(&PublicKey::from(pubkey)); + assert!(shared_secret.was_contributory(), bug!("shared secret might be insecure")); + let hkdf = Hkdf::::new(None, shared_secret.as_bytes()); let mut shared_key_data = [0u8; 256 / 8]; hkdf.expand(b"key", &mut shared_key_data)?;