Skip to content

Commit 9a0a3b7

Browse files
committed
Explicit Message Padding for OnionRealm0HopData
1 parent f70058e commit 9a0a3b7

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

lightning/src/ln/msgs.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use std::error::Error;
2727
use std::{cmp, fmt};
2828
use std::io::Read;
2929
use std::result::Result;
30+
use std::marker::PhantomData;
3031

3132
use util::events;
3233
use util::ser::{Readable, Writeable, Writer};
@@ -610,7 +611,7 @@ pub(crate) struct OnionRealm0HopData {
610611
pub(crate) short_channel_id: u64,
611612
pub(crate) amt_to_forward: u64,
612613
pub(crate) outgoing_cltv_value: u32,
613-
// 12 bytes of 0-padding
614+
pub(crate) padding: PhantomData<[u8; 12]>,
614615
}
615616

616617
mod fuzzy_internal_msgs {
@@ -964,7 +965,7 @@ impl Writeable for OnionRealm0HopData {
964965
self.short_channel_id.write(w)?;
965966
self.amt_to_forward.write(w)?;
966967
self.outgoing_cltv_value.write(w)?;
967-
w.write_all(&[0;12])?;
968+
self.padding.write(w)?;
968969
Ok(())
969970
}
970971
}
@@ -974,11 +975,8 @@ impl<R: Read> Readable<R> for OnionRealm0HopData {
974975
Ok(OnionRealm0HopData {
975976
short_channel_id: Readable::read(r)?,
976977
amt_to_forward: Readable::read(r)?,
977-
outgoing_cltv_value: {
978-
let v: u32 = Readable::read(r)?;
979-
r.read_exact(&mut [0; 12])?;
980-
v
981-
}
978+
outgoing_cltv_value: Readable::read(r)?,
979+
padding: Readable::read(r)?,
982980
})
983981
}
984982
}

lightning/src/ln/onion_utils.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use secp256k1::key::{SecretKey,PublicKey};
1616
use secp256k1::Secp256k1;
1717
use secp256k1::ecdh::SharedSecret;
1818
use secp256k1;
19+
use std::marker::PhantomData;
1920

2021
use std::io::Cursor;
2122
use std::sync::Arc;
@@ -126,6 +127,7 @@ pub(super) fn build_onion_payloads(route: &Route, starting_htlc_offset: u32) ->
126127
short_channel_id: last_short_channel_id,
127128
amt_to_forward: value_msat,
128129
outgoing_cltv_value: cltv,
130+
padding: PhantomData,
129131
},
130132
hmac: [0; 32],
131133
});
@@ -429,6 +431,8 @@ mod tests {
429431
use ln::router::{Route, RouteHop};
430432
use ln::msgs;
431433
use util::ser::Writeable;
434+
435+
use std::marker::PhantomData;
432436

433437
use hex;
434438

@@ -521,6 +525,7 @@ mod tests {
521525
short_channel_id: 0,
522526
amt_to_forward: 0,
523527
outgoing_cltv_value: 0,
528+
padding: PhantomData,
524529
},
525530
hmac: [0; 32],
526531
},
@@ -530,6 +535,7 @@ mod tests {
530535
short_channel_id: 0x0101010101010101,
531536
amt_to_forward: 0x0100000001,
532537
outgoing_cltv_value: 0,
538+
padding: PhantomData,
533539
},
534540
hmac: [0; 32],
535541
},
@@ -539,6 +545,7 @@ mod tests {
539545
short_channel_id: 0x0202020202020202,
540546
amt_to_forward: 0x0200000002,
541547
outgoing_cltv_value: 0,
548+
padding: PhantomData,
542549
},
543550
hmac: [0; 32],
544551
},
@@ -548,6 +555,7 @@ mod tests {
548555
short_channel_id: 0x0303030303030303,
549556
amt_to_forward: 0x0300000003,
550557
outgoing_cltv_value: 0,
558+
padding: PhantomData,
551559
},
552560
hmac: [0; 32],
553561
},
@@ -557,6 +565,7 @@ mod tests {
557565
short_channel_id: 0x0404040404040404,
558566
amt_to_forward: 0x0400000004,
559567
outgoing_cltv_value: 0,
568+
padding: PhantomData,
560569
},
561570
hmac: [0; 32],
562571
},

lightning/src/util/ser.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::io::{Read, Write};
66
use std::collections::HashMap;
77
use std::hash::Hash;
88
use std::sync::Mutex;
9+
use std::marker::PhantomData;
910

1011
use secp256k1::Signature;
1112
use secp256k1::key::{PublicKey, SecretKey};
@@ -208,6 +209,7 @@ macro_rules! impl_array {
208209
impl_array!(3); // for rgb
209210
impl_array!(4); // for IPv4
210211
impl_array!(10); // for OnionV2
212+
impl_array!(12); // for OnionRealm0HopData
211213
impl_array!(16); // for IPv6
212214
impl_array!(32); // for channel id & hmac
213215
impl_array!(33); // for PublicKey
@@ -392,6 +394,20 @@ impl Writeable for PaymentHash {
392394
}
393395
}
394396

397+
impl<R: Read> Readable<R> for PhantomData<[u8; 12]> {
398+
fn read(r: &mut R) -> Result<Self, DecodeError> {
399+
let _buf: [u8; 12] = Readable::read(r)?;
400+
Ok(PhantomData)
401+
}
402+
}
403+
404+
impl Writeable for PhantomData<[u8; 12]> {
405+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
406+
w.write_all(&[0;12])?;
407+
Ok(())
408+
}
409+
}
410+
395411
impl<R: Read> Readable<R> for PaymentHash {
396412
fn read(r: &mut R) -> Result<Self, DecodeError> {
397413
let buf: [u8; 32] = Readable::read(r)?;

0 commit comments

Comments
 (0)