Skip to content

Commit d35bf5b

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 0fce673 commit d35bf5b

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
@@ -883,22 +883,24 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
883883
}
884884

885885
let mut chacha = ChaCha20::new(&rho, &[0u8; 8]);
886-
let next_hop_data = {
886+
let (next_hop_data, next_hop_hmac) = {
887887
let mut decoded = [0; 65];
888888
chacha.process(&msg.onion_routing_packet.hop_data[0..65], &mut decoded);
889-
match msgs::OnionHopData::read(&mut Cursor::new(&decoded[..])) {
889+
let mut hmac = [0; 32];
890+
hmac.copy_from_slice(&decoded[33..]);
891+
match msgs::OnionHopData::read(&mut Cursor::new(&decoded[..33])) {
890892
Err(err) => {
891893
let error_code = match err {
892894
msgs::DecodeError::UnknownVersion => 0x4000 | 1, // unknown realm byte
893895
_ => 0x2000 | 2, // Should never happen
894896
};
895897
return_err!("Unable to decode our hop data", error_code, &[0;0]);
896898
},
897-
Ok(msg) => msg
899+
Ok(msg) => (msg, hmac)
898900
}
899901
};
900902

901-
let pending_forward_info = if next_hop_data.hmac == [0; 32] {
903+
let pending_forward_info = if next_hop_hmac == [0; 32] {
902904
#[cfg(test)]
903905
{
904906
// In tests, make sure that the initial onion pcket data is, at least, non-0.
@@ -963,7 +965,7 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
963965
version: 0,
964966
public_key,
965967
hop_data: new_packet_data,
966-
hmac: next_hop_data.hmac.clone(),
968+
hmac: next_hop_hmac.clone(),
967969
};
968970

969971
PendingHTLCStatus::Forward(PendingForwardHTLCInfo {

lightning/src/ln/msgs.rs

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

631630
pub struct DecodedOnionErrorPacket {
@@ -964,7 +963,7 @@ impl_writeable!(UpdateAddHTLC, 32+8+8+32+4+1366, {
964963

965964
impl Writeable for OnionHopData {
966965
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
967-
w.size_hint(65);
966+
w.size_hint(33);
968967
match self.format {
969968
OnionHopDataFormat::Legacy => 0u8.write(w)?,
970969
#[cfg(test)]
@@ -974,7 +973,6 @@ impl Writeable for OnionHopData {
974973
self.amt_to_forward.write(w)?;
975974
self.outgoing_cltv_value.write(w)?;
976975
w.write_all(&[0;12])?;
977-
self.hmac.write(w)?;
978976
Ok(())
979977
}
980978
}
@@ -996,7 +994,6 @@ impl<R: Read> Readable<R> for OnionHopData {
996994
r.read_exact(&mut [0; 12])?;
997995
v
998996
},
999-
hmac: Readable::read(r)?,
1000997
})
1001998
}
1002999
}

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)