Skip to content

Commit 2b9bcfb

Browse files
committed
Test cases for message encryption/decryption size limits
1 parent 247e267 commit 2b9bcfb

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

lightning/src/ln/peer_channel_encryptor.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,8 @@ impl PeerChannelEncryptor {
468468

469469
#[cfg(test)]
470470
mod tests {
471+
use super::LN_MAX_MSG_LEN;
472+
471473
use bitcoin::secp256k1::key::{PublicKey,SecretKey};
472474

473475
use hex;
@@ -714,4 +716,38 @@ mod tests {
714716
}
715717
}
716718
}
719+
720+
#[test]
721+
#[should_panic(expected = "Attempted to encrypt message longer than 65535 bytes!")]
722+
fn max_message_len_encryption() {
723+
let mut outbound_peer = get_outbound_peer_for_initiator_test_vectors();
724+
let msg = [4u8; LN_MAX_MSG_LEN + 1];
725+
outbound_peer.encrypt_message(&msg);
726+
}
727+
728+
#[test]
729+
#[should_panic(expected = "Attempted to decrypt message longer than 65535 + 16 bytes!")]
730+
fn max_message_len_decryption() {
731+
let mut inbound_peer;
732+
733+
{
734+
// transport-responder successful handshake
735+
let our_node_id = SecretKey::from_slice(&hex::decode("2121212121212121212121212121212121212121212121212121212121212121").unwrap()[..]).unwrap();
736+
let our_ephemeral = SecretKey::from_slice(&hex::decode("2222222222222222222222222222222222222222222222222222222222222222").unwrap()[..]).unwrap();
737+
738+
inbound_peer = PeerChannelEncryptor::new_inbound(&our_node_id);
739+
740+
let act_one = hex::decode("00036360e856310ce5d294e8be33fc807077dc56ac80d95d9cd4ddbd21325eff73f70df6086551151f58b8afe6c195782c6a").unwrap().to_vec();
741+
assert_eq!(inbound_peer.process_act_one_with_keys(&act_one[..], &our_node_id, our_ephemeral.clone()).unwrap()[..], hex::decode("0002466d7fcae563e5cb09a0d1870bb580344804617879a14949cf22285f1bae3f276e2470b93aac583c9ef6eafca3f730ae").unwrap()[..]);
742+
743+
let act_three = hex::decode("00b9e3a702e93e3a9948c2ed6e5fd7590a6e1c3a0344cfc9d5b57357049aa22355361aa02e55a8fc28fef5bd6d71ad0c38228dc68b1c466263b47fdf31e560e139ba").unwrap().to_vec();
744+
// test vector doesn't specify the initiator static key, but it's the same as the one
745+
// from transport-initiator successful handshake
746+
assert_eq!(inbound_peer.process_act_three(&act_three[..]).unwrap().serialize()[..], hex::decode("034f355bdcb7cc0af728ef3cceb9615d90684bb5b2ca5f859ab0f0b704075871aa").unwrap()[..]);
747+
}
748+
749+
// MSG should not exceed LN_MAX_MSG_LEN + 16
750+
let msg = [4u8; LN_MAX_MSG_LEN + 17];
751+
inbound_peer.decrypt_message(&msg).unwrap();
752+
}
717753
}

lightning/src/ln/wire.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use util::ser::{Readable, Writeable, Writer};
2121
/// "The maximum size of any Lightning message MUST NOT exceed 65535 bytes.
2222
/// A maximum size of 65535 simplifies testing, makes memory management easier,
2323
/// and helps mitigate memory-exhaustion attacks."
24-
pub const LN_MAX_MSG_LEN: usize = 65535;
24+
pub const LN_MAX_MSG_LEN: usize = std::u16::MAX as usize; // Must be equal to 65535
2525

2626
/// A Lightning message returned by [`read`] when decoding bytes received over the wire. Each
2727
/// variant contains a message from [`ln::msgs`] or otherwise the message type if unknown.
@@ -318,6 +318,12 @@ mod tests {
318318
// Big-endian wire encoding of Pong message (type = 19, byteslen = 2).
319319
const ENCODED_PONG: [u8; 6] = [0u8, 19u8, 0u8, 2u8, 0u8, 0u8];
320320

321+
#[test]
322+
fn max_msg_len() {
323+
assert_eq!(LN_MAX_MSG_LEN, 65535);
324+
assert_eq!(LN_MAX_MSG_LEN, std::u16::MAX as usize);
325+
}
326+
321327
#[test]
322328
fn read_empty_buffer() {
323329
let buffer = [];

0 commit comments

Comments
 (0)