AsyncPaymentsMessageHandler trait for OnionMessenger
[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::{MessageBuf, PeerChannelEncryptor};
11 use lightning::util::test_utils::TestNodeSigner;
12
13 use bitcoin::secp256k1::{PublicKey, Secp256k1, 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) | ((v[1] as u16) << 8 * 0)
20 }
21
22 #[inline]
23 pub fn do_test(data: &[u8]) {
24         let mut read_pos = 0;
25         macro_rules! get_slice {
26                 ($len: expr) => {{
27                         let slice_len = $len as usize;
28                         if data.len() < read_pos + slice_len {
29                                 return;
30                         }
31                         read_pos += slice_len;
32                         &data[read_pos - slice_len..read_pos]
33                 }};
34         }
35
36         let secp_ctx = Secp256k1::signing_only();
37
38         let our_network_key = match SecretKey::from_slice(get_slice!(32)) {
39                 Ok(key) => key,
40                 Err(_) => return,
41         };
42         let node_signer = TestNodeSigner::new(our_network_key);
43         let ephemeral_key = match SecretKey::from_slice(get_slice!(32)) {
44                 Ok(key) => key,
45                 Err(_) => return,
46         };
47
48         let mut crypter = if get_slice!(1)[0] != 0 {
49                 let their_pubkey = match PublicKey::from_slice(get_slice!(33)) {
50                         Ok(key) => key,
51                         Err(_) => return,
52                 };
53                 let mut crypter = PeerChannelEncryptor::new_outbound(their_pubkey, ephemeral_key);
54                 crypter.get_act_one(&secp_ctx);
55                 match crypter.process_act_two(get_slice!(50), &&node_signer) {
56                         Ok(_) => {},
57                         Err(_) => return,
58                 }
59                 assert!(crypter.is_ready_for_encryption());
60                 crypter
61         } else {
62                 let mut crypter = PeerChannelEncryptor::new_inbound(&&node_signer);
63                 match crypter.process_act_one_with_keys(
64                         get_slice!(50),
65                         &&node_signer,
66                         ephemeral_key,
67                         &secp_ctx,
68                 ) {
69                         Ok(_) => {},
70                         Err(_) => return,
71                 }
72                 match crypter.process_act_three(get_slice!(66)) {
73                         Ok(_) => {},
74                         Err(_) => return,
75                 }
76                 assert!(crypter.is_ready_for_encryption());
77                 crypter
78         };
79         let mut buf = [0; 65536 + 16];
80         loop {
81                 if get_slice!(1)[0] == 0 {
82                         crypter.encrypt_buffer(MessageBuf::from_encoded(&get_slice!(slice_to_be16(
83                                 get_slice!(2)
84                         ))));
85                 } else {
86                         let len = match crypter.decrypt_length_header(get_slice!(16 + 2)) {
87                                 Ok(len) => len,
88                                 Err(_) => return,
89                         };
90                         buf[..len as usize + 16].copy_from_slice(&get_slice!(len as usize + 16));
91                         match crypter.decrypt_message(&mut buf[..len as usize + 16]) {
92                                 Ok(_) => {},
93                                 Err(_) => return,
94                         }
95                 }
96         }
97 }
98
99 pub fn peer_crypt_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
100         do_test(data);
101 }
102
103 #[no_mangle]
104 pub extern "C" fn peer_crypt_run(data: *const u8, datalen: usize) {
105         do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
106 }