X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Fonion_utils.rs;h=953313955a50220ed9b7d9693897facc35fbf7e6;hb=6ca49948c169a297144d5d1474a2cef3827237e1;hp=598edcf0367952b011e290bfe48938ec92364ecb;hpb=f4f1093edc506314484c8d52a40dd155e692394b;p=rust-lightning diff --git a/lightning/src/ln/onion_utils.rs b/lightning/src/ln/onion_utils.rs index 598edcf0..95331395 100644 --- a/lightning/src/ln/onion_utils.rs +++ b/lightning/src/ln/onion_utils.rs @@ -589,31 +589,6 @@ pub(super) fn process_onion_failure(secp_ctx: & } else { unreachable!(); } } -/// An input used when decoding an onion packet. -pub(crate) trait DecodeInput { - type Arg; - /// If Some, this is the input when checking the hmac of the onion packet. - fn payment_hash(&self) -> Option<&PaymentHash>; - /// Read argument when decrypting our hop payload. - fn read_arg(self) -> Self::Arg; -} - -impl DecodeInput for PaymentHash { - type Arg = (); - fn payment_hash(&self) -> Option<&PaymentHash> { - Some(self) - } - fn read_arg(self) -> Self::Arg { () } -} - -impl DecodeInput for SharedSecret { - type Arg = SharedSecret; - fn payment_hash(&self) -> Option<&PaymentHash> { - None - } - fn read_arg(self) -> Self::Arg { self } -} - /// Allows `decode_next_hop` to return the next hop packet bytes for either payments or onion /// message forwards. pub(crate) trait NextPacketBytes: AsMut<[u8]> { @@ -664,7 +639,7 @@ pub(crate) enum OnionDecodeErr { } pub(crate) fn decode_next_payment_hop(shared_secret: [u8; 32], hop_data: &[u8], hmac_bytes: [u8; 32], payment_hash: PaymentHash) -> Result { - match decode_next_hop(shared_secret, hop_data, hmac_bytes, payment_hash) { + match decode_next_hop(shared_secret, hop_data, hmac_bytes, Some(payment_hash), ()) { Ok((next_hop_data, None)) => Ok(Hop::Receive(next_hop_data)), Ok((next_hop_data, Some((next_hop_hmac, FixedSizeOnionPacket(new_packet_bytes))))) => { Ok(Hop::Forward { @@ -677,12 +652,16 @@ pub(crate) fn decode_next_payment_hop(shared_secret: [u8; 32], hop_data: &[u8], } } -pub(crate) fn decode_next_hop, N: NextPacketBytes>(shared_secret: [u8; 32], hop_data: &[u8], hmac_bytes: [u8; 32], decode_input: D) -> Result<(R, Option<([u8; 32], N)>), OnionDecodeErr> { +pub(crate) fn decode_next_untagged_hop, N: NextPacketBytes>(shared_secret: [u8; 32], hop_data: &[u8], hmac_bytes: [u8; 32], read_args: T) -> Result<(R, Option<([u8; 32], N)>), OnionDecodeErr> { + decode_next_hop(shared_secret, hop_data, hmac_bytes, None, read_args) +} + +fn decode_next_hop, N: NextPacketBytes>(shared_secret: [u8; 32], hop_data: &[u8], hmac_bytes: [u8; 32], payment_hash: Option, read_args: T) -> Result<(R, Option<([u8; 32], N)>), OnionDecodeErr> { let (rho, mu) = gen_rho_mu_from_shared_secret(&shared_secret); let mut hmac = HmacEngine::::new(&mu); hmac.input(hop_data); - if let Some(payment_hash) = decode_input.payment_hash() { - hmac.input(&payment_hash.0[..]); + if let Some(tag) = payment_hash { + hmac.input(&tag.0[..]); } if !fixed_time_eq(&Hmac::from_engine(hmac).into_inner(), &hmac_bytes) { return Err(OnionDecodeErr::Malformed { @@ -693,7 +672,7 @@ pub(crate) fn decode_next_hop, N: NextPa let mut chacha = ChaCha20::new(&rho, &[0u8; 8]); let mut chacha_stream = ChaChaReader { chacha: &mut chacha, read: Cursor::new(&hop_data[..]) }; - match R::read(&mut chacha_stream, decode_input.read_arg()) { + match R::read(&mut chacha_stream, read_args) { Err(err) => { let error_code = match err { msgs::DecodeError::UnknownVersion => 0x4000 | 1, // unknown realm byte