Pull hmac out of OnionHopData.
authorMatt Corallo <git@bluematt.me>
Thu, 26 Dec 2019 18:43:43 +0000 (13:43 -0500)
committerMatt Corallo <git@bluematt.me>
Tue, 11 Feb 2020 18:48:56 +0000 (13:48 -0500)
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.

lightning/src/ln/channelmanager.rs
lightning/src/ln/msgs.rs
lightning/src/ln/onion_utils.rs

index ca6562b93d4af70e5deac2a3538190fb3fc6c396..34329aea0009267394bb083ede614baf21c5a51e 100644 (file)
@@ -906,10 +906,12 @@ impl<ChanSigner: ChannelKeys, M: Deref> ChannelManager<ChanSigner, M> 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<ChanSigner: ChannelKeys, M: Deref> ChannelManager<ChanSigner, M> 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<ChanSigner: ChannelKeys, M: Deref> ChannelManager<ChanSigner, M> 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 {
index 9d1563d644711b9e61a67e0797d75f3183485b93..4518dd72203825b0addd36f9daee2d2d18e5729e 100644 (file)
@@ -627,7 +627,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 {
@@ -966,7 +965,7 @@ impl_writeable!(UpdateAddHTLC, 32+8+8+32+4+1366, {
 
 impl Writeable for OnionHopData {
        fn write<W: Writer>(&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)]
@@ -976,7 +975,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(())
        }
 }
@@ -998,7 +996,6 @@ impl<R: Read> Readable<R> for OnionHopData {
                                r.read_exact(&mut [0; 12])?;
                                v
                        },
-                       hmac: Readable::read(r)?,
                })
        }
 }
index 1c1105cfcae76dcc0634ad531d2aa73ffb012365..fb9684886e6d5e92b36e62b5154fba61baab6802 100644 (file)
@@ -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<msgs::OnionHopData>,
 
        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],
                        },
                );