From: Matt Corallo Date: Thu, 26 Dec 2019 18:43:43 +0000 (-0500) Subject: Pull hmac out of OnionHopData. X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=0b40b0cea4bea3fd47f0123131192600c7a2e757;p=rust-lightning Pull hmac out of OnionHopData. Its a bit awkward to have an hmac field covering the struct that its in, and there is little difference in removing it, so just pull it out and use a [u8; 32] where we care about the hmac. --- diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 6c1f1abbb..e42325746 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -906,10 +906,12 @@ impl ChannelManager where M::T } let mut chacha = ChaCha20::new(&rho, &[0u8; 8]); - let next_hop_data = { + let (next_hop_data, next_hop_hmac) = { let mut decoded = [0; 65]; chacha.process(&msg.onion_routing_packet.hop_data[0..65], &mut decoded); - match msgs::OnionHopData::read(&mut Cursor::new(&decoded[..])) { + let mut hmac = [0; 32]; + hmac.copy_from_slice(&decoded[33..]); + match msgs::OnionHopData::read(&mut Cursor::new(&decoded[..33])) { Err(err) => { let error_code = match err { msgs::DecodeError::UnknownVersion => 0x4000 | 1, // unknown realm byte @@ -917,11 +919,11 @@ impl ChannelManager where M::T }; return_err!("Unable to decode our hop data", error_code, &[0;0]); }, - Ok(msg) => msg + Ok(msg) => (msg, hmac) } }; - let pending_forward_info = if next_hop_data.hmac == [0; 32] { + let pending_forward_info = if next_hop_hmac == [0; 32] { #[cfg(test)] { // In tests, make sure that the initial onion pcket data is, at least, non-0. @@ -986,7 +988,7 @@ impl ChannelManager where M::T version: 0, public_key, hop_data: new_packet_data, - hmac: next_hop_data.hmac.clone(), + hmac: next_hop_hmac.clone(), }; PendingHTLCStatus::Forward(PendingForwardHTLCInfo { diff --git a/lightning/src/ln/msgs.rs b/lightning/src/ln/msgs.rs index b48819972..b42efcd5f 100644 --- a/lightning/src/ln/msgs.rs +++ b/lightning/src/ln/msgs.rs @@ -625,7 +625,6 @@ mod fuzzy_internal_msgs { pub(crate) amt_to_forward: u64, pub(crate) outgoing_cltv_value: u32, // 12 bytes of 0-padding - pub(crate) hmac: [u8; 32], } pub struct DecodedOnionErrorPacket { @@ -964,7 +963,7 @@ impl_writeable!(UpdateAddHTLC, 32+8+8+32+4+1366, { impl Writeable for OnionHopData { fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { - w.size_hint(65); + w.size_hint(33); match self.format { OnionHopDataFormat::Legacy => 0u8.write(w)?, #[cfg(test)] @@ -974,7 +973,6 @@ impl Writeable for OnionHopData { self.amt_to_forward.write(w)?; self.outgoing_cltv_value.write(w)?; w.write_all(&[0;12])?; - self.hmac.write(w)?; Ok(()) } } @@ -996,7 +994,6 @@ impl Readable for OnionHopData { r.read_exact(&mut [0; 12])?; v }, - hmac: Readable::read(r)?, }) } } diff --git a/lightning/src/ln/onion_utils.rs b/lightning/src/ln/onion_utils.rs index 1c1105cfc..fb9684886 100644 --- a/lightning/src/ln/onion_utils.rs +++ b/lightning/src/ln/onion_utils.rs @@ -125,7 +125,6 @@ pub(super) fn build_onion_payloads(route: &Route, starting_htlc_offset: u32) -> short_channel_id: last_short_channel_id, amt_to_forward: value_msat, outgoing_cltv_value: cltv, - hmac: [0; 32], }); cur_value_msat += hop.fee_msat; if cur_value_msat >= 21000000 * 100000000 * 1000 { @@ -193,8 +192,8 @@ fn construct_onion_packet_with_init_noise(mut payloads: Vec, for (i, (payload, keys)) in payloads.iter_mut().zip(onion_keys.iter()).rev().enumerate() { shift_arr_right(&mut packet_data); - payload.hmac = hmac_res; - packet_data[0..65].copy_from_slice(&payload.encode()[..]); + packet_data[0..33].copy_from_slice(&payload.encode()[..]); + packet_data[33..65].copy_from_slice(&hmac_res); let mut chacha = ChaCha20::new(&keys.rho, &[0u8; 8]); chacha.process(&packet_data, &mut buf[0..20*65]); @@ -518,35 +517,30 @@ mod tests { short_channel_id: 0, amt_to_forward: 0, outgoing_cltv_value: 0, - hmac: [0; 32], }, msgs::OnionHopData { format: msgs::OnionHopDataFormat::Legacy, short_channel_id: 0x0101010101010101, amt_to_forward: 0x0100000001, outgoing_cltv_value: 0, - hmac: [0; 32], }, msgs::OnionHopData { format: msgs::OnionHopDataFormat::Legacy, short_channel_id: 0x0202020202020202, amt_to_forward: 0x0200000002, outgoing_cltv_value: 0, - hmac: [0; 32], }, msgs::OnionHopData { format: msgs::OnionHopDataFormat::Legacy, short_channel_id: 0x0303030303030303, amt_to_forward: 0x0300000003, outgoing_cltv_value: 0, - hmac: [0; 32], }, msgs::OnionHopData { format: msgs::OnionHopDataFormat::Legacy, short_channel_id: 0x0404040404040404, amt_to_forward: 0x0400000004, outgoing_cltv_value: 0, - hmac: [0; 32], }, );