Skip to content

Commit 0b40b0c

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 b3dd229 commit 0b40b0c

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
@@ -906,22 +906,24 @@ impl<ChanSigner: ChannelKeys, M: Deref> ChannelManager<ChanSigner, M> where M::T
906906
}
907907

908908
let mut chacha = ChaCha20::new(&rho, &[0u8; 8]);
909-
let next_hop_data = {
909+
let (next_hop_data, next_hop_hmac) = {
910910
let mut decoded = [0; 65];
911911
chacha.process(&msg.onion_routing_packet.hop_data[0..65], &mut decoded);
912-
match msgs::OnionHopData::read(&mut Cursor::new(&decoded[..])) {
912+
let mut hmac = [0; 32];
913+
hmac.copy_from_slice(&decoded[33..]);
914+
match msgs::OnionHopData::read(&mut Cursor::new(&decoded[..33])) {
913915
Err(err) => {
914916
let error_code = match err {
915917
msgs::DecodeError::UnknownVersion => 0x4000 | 1, // unknown realm byte
916918
_ => 0x2000 | 2, // Should never happen
917919
};
918920
return_err!("Unable to decode our hop data", error_code, &[0;0]);
919921
},
920-
Ok(msg) => msg
922+
Ok(msg) => (msg, hmac)
921923
}
922924
};
923925

924-
let pending_forward_info = if next_hop_data.hmac == [0; 32] {
926+
let pending_forward_info = if next_hop_hmac == [0; 32] {
925927
#[cfg(test)]
926928
{
927929
// 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
986988
version: 0,
987989
public_key,
988990
hop_data: new_packet_data,
989-
hmac: next_hop_data.hmac.clone(),
991+
hmac: next_hop_hmac.clone(),
990992
};
991993

992994
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)