diff --git a/src/rust/bitbox-noise/src/noise_xx.rs b/src/rust/bitbox-noise/src/noise_xx.rs index 6be6cd67df..d9c9e71804 100644 --- a/src/rust/bitbox-noise/src/noise_xx.rs +++ b/src/rust/bitbox-noise/src/noise_xx.rs @@ -133,9 +133,13 @@ impl State { }; return Ok(HandshakeResult::Done); } - Ok(HandshakeResult::Response( - handshake_state.write_message_vec(&payload)?, - )) + match handshake_state.write_message_vec(&payload) { + Ok(response) => Ok(HandshakeResult::Response(response)), + Err(err) => { + self.reset(); + Err(err.into()) + } + } } _ => Err(Error::WrongState), } @@ -227,9 +231,9 @@ impl State { #[cfg(test)] mod tests { use super::*; - use crate::testing::{MockRandom, make_mock_host}; + use crate::testing::{MockRandom, make_mock_host, non_contributory_pubkeys}; use hex_lit::hex; - use noise_protocol::U8Array; + use noise_protocol::{DH, U8Array}; impl HandshakeResult { fn response(self) -> Result, ()> { @@ -246,6 +250,81 @@ mod tests { } } + struct MaliciousX25519; + + impl DH for MaliciousX25519 { + type Key = Sensitive; + type Pubkey = PublicKey; + type Output = [u8; 32]; + + fn name() -> &'static str { + X25519::name() + } + + fn genkey() -> Self::Key { + unreachable!() + } + + // Low-order values stand in for public keys with no corresponding private key. + fn pubkey(k: &Self::Key) -> Self::Pubkey { + if non_contributory_pubkeys().any(|pubkey| pubkey == **k) { + **k + } else { + X25519::pubkey(k) + } + } + + fn dh(k: &Self::Key, pk: &Self::Pubkey) -> Result { + if non_contributory_pubkeys().any(|pubkey| pubkey == **k) { + Ok([0; 32]) + } else { + X25519::dh(k, pk) + } + } + } + + #[test] + fn test_handshake_non_contributory_ephemeral() { + for pubkey in non_contributory_pubkeys() { + let mut bb02 = State::Nothing; + bb02.init(genkey(&mut MockRandom), &mut MockRandom); + assert!( + matches!(bb02.handshake(&pubkey), Err(Error::Noise)), + "{pubkey:02x?}" + ); + assert!(matches!(bb02, State::Nothing)); + } + } + + #[test] + fn test_handshake_non_contributory_static() { + for pubkey in non_contributory_pubkeys() { + let mut host = + noise_protocol::HandshakeState::::new( + noise_protocol::patterns::noise_xx(), + true, + b"Noise_XX_25519_ChaChaPoly_SHA256", + Some(Sensitive::from(pubkey.into())), + Some(genkey(&mut MockRandom)), + None, + None, + ); + let mut bb02 = State::Nothing; + bb02.init(genkey(&mut MockRandom), &mut MockRandom); + + let msg = host.write_message_vec(b"").unwrap(); + let response = bb02.handshake(&msg).unwrap().response().unwrap(); + host.read_message_vec(&response).unwrap(); + let msg = host.write_message_vec(b"").unwrap(); + assert!(host.completed()); + assert!( + matches!(bb02.handshake(&msg), Err(Error::Noise)), + "{pubkey:02x?}" + ); + assert!(matches!(bb02, State::Nothing)); + } + } + #[test] pub fn test_full() { let mut bb02_random = MockRandom; @@ -268,6 +347,19 @@ mod tests { }; bb02.handshake(&host_handshake_2).unwrap().done().unwrap(); + assert_eq!( + bb02.remote_static_pubkey().unwrap(), + X25519::pubkey(&genkey(&mut MockRandom)) + ); + assert_eq!(bb02.get_handshake_hash().unwrap(), host.get_hash()); + assert!(matches!( + bb02.encrypt(b"message", &mut Vec::new()), + Err(Error::PairingVerificationRequired) + )); + assert!(matches!( + bb02.decrypt(&[0; 16]), + Err(Error::PairingVerificationRequired) + )); bb02.set_pairing_verified().unwrap(); let (mut host_send, mut host_recv) = host.get_ciphers(); diff --git a/src/rust/bitbox-noise/src/testing.rs b/src/rust/bitbox-noise/src/testing.rs index 9c8f4d2dc9..dd8583cc6d 100644 --- a/src/rust/bitbox-noise/src/testing.rs +++ b/src/rust/bitbox-noise/src/testing.rs @@ -35,3 +35,28 @@ pub fn make_mock_host() -> TestHandshakeState { let mut random = MockRandom; make_host(&mut random) } + +/// Low-order X25519 public keys and equivalent encodings that yield an all-zero shared secret for +/// every private key. Such keys are non-contributory: the DH result is predictable without knowing +/// either party's private key. +#[cfg(test)] +pub(crate) fn non_contributory_pubkeys() -> impl Iterator { + use hex_lit::hex; + + [ + hex!("0000000000000000000000000000000000000000000000000000000000000000"), + hex!("0100000000000000000000000000000000000000000000000000000000000000"), + hex!("e0eb7a7c3b41b8ae1656e3faf19fc46ada098deb9c32b1fd866205165f49b800"), + hex!("5f9c95bca3508c24b1d0b1559c83ef5b04445cc4581c8e86d8224eddd09f1157"), + hex!("ecffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f"), + hex!("edffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f"), + hex!("eeffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f"), + ] + .into_iter() + .flat_map(|pubkey| { + // X25519 ignores the high bit of the public key. + let mut alias = pubkey; + alias[31] |= 0x80; + [pubkey, alias] + }) +} diff --git a/src/rust/bitbox-noise/src/x25519.rs b/src/rust/bitbox-noise/src/x25519.rs index 723dd42ca9..d595be1c59 100644 --- a/src/rust/bitbox-noise/src/x25519.rs +++ b/src/rust/bitbox-noise/src/x25519.rs @@ -54,6 +54,25 @@ impl noise_protocol::DH for X25519 { fn dh(k: &Self::Key, pk: &Self::Pubkey) -> Result { let k = x25519_dalek::StaticSecret::from(*k.deref()); let pk = x25519_dalek::PublicKey::from(*pk); - Ok(*k.diffie_hellman(&pk).as_bytes()) + let shared = k.diffie_hellman(&pk); + if !shared.was_contributory() { + return Err(()); + } + Ok(*shared.as_bytes()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::testing::{MockRandom, non_contributory_pubkeys}; + use noise_protocol::DH; + + #[test] + fn test_dh_non_contributory() { + let private_key = genkey(&mut MockRandom); + for pubkey in non_contributory_pubkeys() { + assert_eq!(X25519::dh(&private_key, &pubkey), Err(()), "{pubkey:02x?}"); + } } }