-
Notifications
You must be signed in to change notification settings - Fork 456
Expand file tree
/
Copy pathpeer_crypt.rs
More file actions
106 lines (96 loc) · 2.84 KB
/
peer_crypt.rs
File metadata and controls
106 lines (96 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// This file is Copyright its original authors, visible in version control
// history.
//
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
// You may not use this file except in accordance with one or both of these
// licenses.
use lightning::ln::peer_channel_encryptor::{MessageBuf, PeerChannelEncryptor};
use lightning::util::test_utils::TestNodeSigner;
use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
use crate::utils::test_logger;
#[inline]
fn slice_to_be16(v: &[u8]) -> u16 {
((v[0] as u16) << 8 * 1) | ((v[1] as u16) << 8 * 0)
}
#[inline]
pub fn do_test(data: &[u8]) {
let mut read_pos = 0;
macro_rules! get_slice {
($len: expr) => {{
let slice_len = $len as usize;
if data.len() < read_pos + slice_len {
return;
}
read_pos += slice_len;
&data[read_pos - slice_len..read_pos]
}};
}
let secp_ctx = Secp256k1::signing_only();
let our_network_key = match SecretKey::from_slice(get_slice!(32)) {
Ok(key) => key,
Err(_) => return,
};
let node_signer = TestNodeSigner::new(our_network_key);
let ephemeral_key = match SecretKey::from_slice(get_slice!(32)) {
Ok(key) => key,
Err(_) => return,
};
let mut crypter = if get_slice!(1)[0] != 0 {
let their_pubkey = match PublicKey::from_slice(get_slice!(33)) {
Ok(key) => key,
Err(_) => return,
};
let mut crypter = PeerChannelEncryptor::new_outbound(their_pubkey, ephemeral_key);
crypter.get_act_one(&secp_ctx);
match crypter.process_act_two(get_slice!(50), &&node_signer) {
Ok(_) => {},
Err(_) => return,
}
assert!(crypter.is_ready_for_encryption());
crypter
} else {
let mut crypter = PeerChannelEncryptor::new_inbound(&&node_signer);
match crypter.process_act_one_with_keys(
get_slice!(50),
&&node_signer,
ephemeral_key,
&secp_ctx,
) {
Ok(_) => {},
Err(_) => return,
}
match crypter.process_act_three(get_slice!(66)) {
Ok(_) => {},
Err(_) => return,
}
assert!(crypter.is_ready_for_encryption());
crypter
};
let mut buf = [0; 65536 + 16];
loop {
if get_slice!(1)[0] == 0 {
crypter.encrypt_buffer(MessageBuf::from_encoded(&get_slice!(slice_to_be16(
get_slice!(2)
))), false);
} else {
let len = match crypter.decrypt_length_header(get_slice!(16 + 2)) {
Ok(len) => len,
Err(_) => return,
};
buf[..len as usize + 16].copy_from_slice(&get_slice!(len as usize + 16));
match crypter.decrypt_message(&mut buf[..len as usize + 16]) {
Ok(_) => {},
Err(_) => return,
}
}
}
}
pub fn peer_crypt_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
do_test(data);
}
#[no_mangle]
pub extern "C" fn peer_crypt_run(data: *const u8, datalen: usize) {
do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
}