]> git.bitcoin.ninja Git - rust-lightning/blob - fuzz/src/peer_crypt.rs
TESTING
[rust-lightning] / fuzz / src / peer_crypt.rs
1 use lightning::ln::peer_channel_encryptor::PeerChannelEncryptor;
2
3 use bitcoin::secp256k1::key::{PublicKey,SecretKey};
4
5 use utils::test_logger;
6
7 #[inline]
8 fn slice_to_be16(v: &[u8]) -> u16 {
9         ((v[0] as u16) << 8*1) |
10         ((v[1] as u16) << 8*0)
11 }
12
13 #[inline]
14 pub fn do_test(data: &[u8]) {
15         let mut read_pos = 0;
16         macro_rules! get_slice {
17                 ($len: expr) => {
18                         {
19                                 let slice_len = $len as usize;
20                                 if data.len() < read_pos + slice_len {
21                                         return;
22                                 }
23                                 read_pos += slice_len;
24                                 &data[read_pos - slice_len..read_pos]
25                         }
26                 }
27         }
28
29         let our_network_key = match SecretKey::from_slice(get_slice!(32)) {
30                 Ok(key) => key,
31                 Err(_) => return,
32         };
33         let ephemeral_key = match SecretKey::from_slice(get_slice!(32)) {
34                 Ok(key) => key,
35                 Err(_) => return,
36         };
37
38         let mut crypter = if get_slice!(1)[0] != 0 {
39                 let their_pubkey = match PublicKey::from_slice(get_slice!(33)) {
40                         Ok(key) => key,
41                         Err(_) => return,
42                 };
43                 let mut crypter = PeerChannelEncryptor::new_outbound(their_pubkey, ephemeral_key);
44                 crypter.get_act_one();
45                 match crypter.process_act_two(get_slice!(50), &our_network_key) {
46                         Ok(_) => {},
47                         Err(_) => return,
48                 }
49                 assert!(crypter.is_ready_for_encryption());
50                 crypter
51         } else {
52                 let mut crypter = PeerChannelEncryptor::new_inbound(&our_network_key);
53                 match crypter.process_act_one_with_keys(get_slice!(50), &our_network_key, ephemeral_key) {
54                         Ok(_) => {},
55                         Err(_) => return,
56                 }
57                 match crypter.process_act_three(get_slice!(66)) {
58                         Ok(_) => {},
59                         Err(_) => return,
60                 }
61                 assert!(crypter.is_ready_for_encryption());
62                 crypter
63         };
64         loop {
65                 if get_slice!(1)[0] == 0 {
66                         crypter.encrypt_message(get_slice!(slice_to_be16(get_slice!(2))));
67                 } else {
68                         let len = match crypter.decrypt_length_header(get_slice!(16+2)) {
69                                 Ok(len) => len,
70                                 Err(_) => return,
71                         };
72                         match crypter.decrypt_message(get_slice!(len as usize + 16)) {
73                                 Ok(_) => {},
74                                 Err(_) => return,
75                         }
76                 }
77         }
78 }
79
80 pub fn peer_crypt_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
81         do_test(data);
82 }
83
84 #[no_mangle]
85 pub extern "C" fn peer_crypt_run(data: *const u8, datalen: usize) {
86         do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
87 }