Skip to content

Commit 953e93d

Browse files
committed
fix bug that was kindly caught by fuzz tests
1 parent 7f09475 commit 953e93d

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

lightning/src/ln/peers/chacha.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ pub fn decrypt(key: &[u8], nonce: u64, associated_data: &[u8], tagged_ciphertext
2121
nonce_bytes[4..].copy_from_slice(&byte_utils::le64_to_array(nonce));
2222

2323
let length = tagged_ciphertext.len();
24-
let ciphertext = &tagged_ciphertext[0..length - 16];
25-
let authentication_tag = &tagged_ciphertext[length - 16..length];
24+
if length < 16 {
25+
return Err("ciphertext cannot be shorter than tag length of 16 bytes".to_string());
26+
}
27+
let end_index = length - 16;
28+
let ciphertext = &tagged_ciphertext[0..end_index];
29+
let authentication_tag = &tagged_ciphertext[end_index..length];
2630

2731
let mut chacha = ChaCha20Poly1305RFC::new(key, &nonce_bytes, associated_data);
2832
let mut plaintext = vec![0u8; length - 16];

lightning/src/ln/peers/conduit.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl Conduit {
8686
length_bytes.copy_from_slice(length_vec.as_slice());
8787
let message_length = byte_utils::slice_to_be16(&length_bytes) as usize;
8888

89-
let message_end_index = message_length + 18; // todo: abort if too short
89+
let message_end_index = message_length + 18 + 16; // todo: abort if too short
9090
if buffer.len() < message_end_index {
9191
return (None, 0);
9292
}
@@ -154,6 +154,16 @@ mod tests {
154154
read_buffer: None,
155155
};
156156

157+
let mut remote_peer = Conduit {
158+
sending_key: receiving_key,
159+
receiving_key: sending_key,
160+
sending_chaining_key: chaining_key,
161+
receiving_chaining_key: chaining_key,
162+
sending_nonce: 0,
163+
receiving_nonce: 0,
164+
read_buffer: None,
165+
};
166+
157167
let message = hex::decode("68656c6c6f").unwrap();
158168
let mut encrypted_messages: Vec<Vec<u8>> = Vec::new();
159169

@@ -168,5 +178,13 @@ mod tests {
168178
assert_eq!(encrypted_messages[501], hex::decode("1b186c57d44eb6de4c057c49940d79bb838a145cb528d6e8fd26dbe50a60ca2c104b56b60e45bd").unwrap());
169179
assert_eq!(encrypted_messages[1000], hex::decode("4a2f3cc3b5e78ddb83dcb426d9863d9d9a723b0337c89dd0b005d89f8d3c05c52b76b29b740f09").unwrap());
170180
assert_eq!(encrypted_messages[1001], hex::decode("2ecd8c8a5629d0d02ab457a0fdd0f7b90a192cd46be5ecb6ca570bfc5e268338b1a16cf4ef2d36").unwrap());
181+
182+
for _ in 0..1002 {
183+
let encrypted_message = encrypted_messages.remove(0);
184+
let mut decrypted_messages = remote_peer.decrypt_message_stream(Some(&encrypted_message));
185+
assert_eq!(decrypted_messages.len(), 1);
186+
let decrypted_message = decrypted_messages.remove(0);
187+
assert_eq!(decrypted_message, hex::decode("68656c6c6f").unwrap());
188+
}
171189
}
172190
}

lightning/src/ln/peers/handshake/tests.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#![cfg(test)]
22

3+
use hex;
34
use secp256k1;
5+
46
use secp256k1::key::{PublicKey, SecretKey};
57

68
use ln::peers::handshake::PeerHandshake;
@@ -20,8 +22,18 @@ fn test_exchange() {
2022

2123
let remote_public_key = PublicKey::from_secret_key(&curve, &remote_private_key);
2224

23-
let act_1_message = local_handshake.initiate(&remote_public_key);
24-
let act_2_message = remote_handshake.process_act_one(act_1_message.unwrap());
25-
let act_3_message = local_handshake.process_act_two(act_2_message.unwrap());
26-
remote_handshake.process_act_three(act_3_message.unwrap().0).unwrap();
25+
let act_1 = local_handshake.initiate(&remote_public_key).unwrap();
26+
let act_1_hex = hex::encode(&act_1.0.to_vec());
27+
assert_eq!(act_1_hex, "00036360e856310ce5d294e8be33fc807077dc56ac80d95d9cd4ddbd21325eff73f70df6086551151f58b8afe6c195782c6a");
28+
29+
let act_2 = remote_handshake.process_act_one(act_1).unwrap();
30+
let act_2_hex = hex::encode(&act_2.0.to_vec());
31+
assert_eq!(act_2_hex, "0002466d7fcae563e5cb09a0d1870bb580344804617879a14949cf22285f1bae3f276e2470b93aac583c9ef6eafca3f730ae");
32+
33+
let act_2_result = local_handshake.process_act_two(act_2).unwrap();
34+
let act_3 = act_2_result.0;
35+
let act_3_hex = hex::encode(&act_3.0.to_vec());
36+
assert_eq!(act_3_hex, "00b9e3a702e93e3a9948c2ed6e5fd7590a6e1c3a0344cfc9d5b57357049aa22355361aa02e55a8fc28fef5bd6d71ad0c38228dc68b1c466263b47fdf31e560e139ba");
37+
38+
remote_handshake.process_act_three(act_3).unwrap();
2739
}

0 commit comments

Comments
 (0)