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