]> git.bitcoin.ninja Git - rust-lightning/blob - fuzz/src/peer_crypt.rs
Merge pull request #1693 from valentinewallace/2022-09-readme-updates
[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
12 use bitcoin::secp256k1::{Secp256k1, PublicKey, SecretKey};
13
14 use utils::test_logger;
15
16 #[inline]
17 fn slice_to_be16(v: &[u8]) -> u16 {
18         ((v[0] as u16) << 8*1) |
19         ((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                         {
28                                 let slice_len = $len as usize;
29                                 if data.len() < read_pos + slice_len {
30                                         return;
31                                 }
32                                 read_pos += slice_len;
33                                 &data[read_pos - slice_len..read_pos]
34                         }
35                 }
36         }
37
38         let secp_ctx = Secp256k1::signing_only();
39
40         let our_network_key = match SecretKey::from_slice(get_slice!(32)) {
41                 Ok(key) => key,
42                 Err(_) => return,
43         };
44         let ephemeral_key = match SecretKey::from_slice(get_slice!(32)) {
45                 Ok(key) => key,
46                 Err(_) => return,
47         };
48
49         let mut crypter = if get_slice!(1)[0] != 0 {
50                 let their_pubkey = match PublicKey::from_slice(get_slice!(33)) {
51                         Ok(key) => key,
52                         Err(_) => return,
53                 };
54                 let mut crypter = PeerChannelEncryptor::new_outbound(their_pubkey, ephemeral_key);
55                 crypter.get_act_one(&secp_ctx);
56                 match crypter.process_act_two(get_slice!(50), &our_network_key, &secp_ctx) {
57                         Ok(_) => {},
58                         Err(_) => return,
59                 }
60                 assert!(crypter.is_ready_for_encryption());
61                 crypter
62         } else {
63                 let mut crypter = PeerChannelEncryptor::new_inbound(&our_network_key, &secp_ctx);
64                 match crypter.process_act_one_with_keys(get_slice!(50), &our_network_key, ephemeral_key, &secp_ctx) {
65                         Ok(_) => {},
66                         Err(_) => return,
67                 }
68                 match crypter.process_act_three(get_slice!(66)) {
69                         Ok(_) => {},
70                         Err(_) => return,
71                 }
72                 assert!(crypter.is_ready_for_encryption());
73                 crypter
74         };
75         loop {
76                 if get_slice!(1)[0] == 0 {
77                         crypter.encrypt_buffer(get_slice!(slice_to_be16(get_slice!(2))));
78                 } else {
79                         let len = match crypter.decrypt_length_header(get_slice!(16+2)) {
80                                 Ok(len) => len,
81                                 Err(_) => return,
82                         };
83                         match crypter.decrypt_message(get_slice!(len as usize + 16)) {
84                                 Ok(_) => {},
85                                 Err(_) => return,
86                         }
87                 }
88         }
89 }
90
91 pub fn peer_crypt_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
92         do_test(data);
93 }
94
95 #[no_mangle]
96 pub extern "C" fn peer_crypt_run(data: *const u8, datalen: usize) {
97         do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
98 }