Skip to content

Commit b5ed34b

Browse files
committed
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.
1 parent 0c9b601 commit b5ed34b

File tree

3 files changed

+10
-17
lines changed

3 files changed

+10
-17
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -925,22 +925,24 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
925925
}
926926

927927
let mut chacha = ChaCha20::new(&rho, &[0u8; 8]);
928-
let next_hop_data = {
928+
let (next_hop_data, next_hop_hmac) = {
929929
let mut decoded = [0; 65];
930930
chacha.process(&msg.onion_routing_packet.hop_data[0..65], &mut decoded);
931-
match msgs::OnionHopData::read(&mut Cursor::new(&decoded[..])) {
931+
let mut hmac = [0; 32];
932+
hmac.copy_from_slice(&decoded[33..]);
933+
match msgs::OnionHopData::read(&mut Cursor::new(&decoded[..33])) {
932934
Err(err) => {
933935
let error_code = match err {
934936
msgs::DecodeError::UnknownVersion => 0x4000 | 1, // unknown realm byte
935937
_ => 0x2000 | 2, // Should never happen
936938
};
937939
return_err!("Unable to decode our hop data", error_code, &[0;0]);
938940
},
939-
Ok(msg) => msg
941+
Ok(msg) => (msg, hmac)
940942
}
941943
};
942944

943-
let pending_forward_info = if next_hop_data.hmac == [0; 32] {
945+
let pending_forward_info = if next_hop_hmac == [0; 32] {
944946
#[cfg(test)]
945947
{
946948
// In tests, make sure that the initial onion pcket data is, at least, non-0.
@@ -1005,7 +1007,7 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
10051007
version: 0,
10061008
public_key,
10071009
hop_data: new_packet_data,
1008-
hmac: next_hop_data.hmac.clone(),
1010+
hmac: next_hop_hmac.clone(),
10091011
};
10101012

10111013
PendingHTLCStatus::Forward(PendingForwardHTLCInfo {

lightning/src/ln/msgs.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,6 @@ mod fuzzy_internal_msgs {
624624
pub(crate) amt_to_forward: u64,
625625
pub(crate) outgoing_cltv_value: u32,
626626
// 12 bytes of 0-padding
627-
pub(crate) hmac: [u8; 32],
628627
}
629628

630629
pub struct DecodedOnionErrorPacket {
@@ -963,7 +962,7 @@ impl_writeable!(UpdateAddHTLC, 32+8+8+32+4+1366, {
963962

964963
impl Writeable for OnionHopData {
965964
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
966-
w.size_hint(65);
965+
w.size_hint(33);
967966
match self.format {
968967
OnionHopDataFormat::Legacy => 0u8.write(w)?,
969968
#[cfg(test)]
@@ -973,7 +972,6 @@ impl Writeable for OnionHopData {
973972
self.amt_to_forward.write(w)?;
974973
self.outgoing_cltv_value.write(w)?;
975974
w.write_all(&[0;12])?;
976-
self.hmac.write(w)?;
977975
Ok(())
978976
}
979977
}
@@ -995,7 +993,6 @@ impl<R: Read> Readable<R> for OnionHopData {
995993
r.read_exact(&mut [0; 12])?;
996994
v
997995
},
998-
hmac: Readable::read(r)?,
999996
})
1000997
}
1001998
}

lightning/src/ln/onion_utils.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ pub(super) fn build_onion_payloads(route: &Route, starting_htlc_offset: u32) ->
125125
short_channel_id: last_short_channel_id,
126126
amt_to_forward: value_msat,
127127
outgoing_cltv_value: cltv,
128-
hmac: [0; 32],
129128
});
130129
cur_value_msat += hop.fee_msat;
131130
if cur_value_msat >= 21000000 * 100000000 * 1000 {
@@ -193,8 +192,8 @@ fn construct_onion_packet_with_init_noise(mut payloads: Vec<msgs::OnionHopData>,
193192

194193
for (i, (payload, keys)) in payloads.iter_mut().zip(onion_keys.iter()).rev().enumerate() {
195194
shift_arr_right(&mut packet_data);
196-
payload.hmac = hmac_res;
197-
packet_data[0..65].copy_from_slice(&payload.encode()[..]);
195+
packet_data[0..33].copy_from_slice(&payload.encode()[..]);
196+
packet_data[33..65].copy_from_slice(&hmac_res);
198197

199198
let mut chacha = ChaCha20::new(&keys.rho, &[0u8; 8]);
200199
chacha.process(&packet_data, &mut buf[0..20*65]);
@@ -518,35 +517,30 @@ mod tests {
518517
short_channel_id: 0,
519518
amt_to_forward: 0,
520519
outgoing_cltv_value: 0,
521-
hmac: [0; 32],
522520
},
523521
msgs::OnionHopData {
524522
format: msgs::OnionHopDataFormat::Legacy,
525523
short_channel_id: 0x0101010101010101,
526524
amt_to_forward: 0x0100000001,
527525
outgoing_cltv_value: 0,
528-
hmac: [0; 32],
529526
},
530527
msgs::OnionHopData {
531528
format: msgs::OnionHopDataFormat::Legacy,
532529
short_channel_id: 0x0202020202020202,
533530
amt_to_forward: 0x0200000002,
534531
outgoing_cltv_value: 0,
535-
hmac: [0; 32],
536532
},
537533
msgs::OnionHopData {
538534
format: msgs::OnionHopDataFormat::Legacy,
539535
short_channel_id: 0x0303030303030303,
540536
amt_to_forward: 0x0300000003,
541537
outgoing_cltv_value: 0,
542-
hmac: [0; 32],
543538
},
544539
msgs::OnionHopData {
545540
format: msgs::OnionHopDataFormat::Legacy,
546541
short_channel_id: 0x0404040404040404,
547542
amt_to_forward: 0x0400000004,
548543
outgoing_cltv_value: 0,
549-
hmac: [0; 32],
550544
},
551545
);
552546

0 commit comments

Comments
 (0)