Merge pull request #3001 from optout21/splicing-feature-bit-with-any
[rust-lightning] / fuzz / src / peer_crypt.rs
index e0ff02f04ccc341a219cc9a90bf308a349224591..41d8c0936060868a97df54711f725e397ea3a35d 100644 (file)
@@ -1,6 +1,18 @@
-use lightning::ln::peer_channel_encryptor::PeerChannelEncryptor;
+// 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 secp256k1::key::{PublicKey,SecretKey};
+use lightning::ln::peer_channel_encryptor::{PeerChannelEncryptor, MessageBuf};
+use lightning::util::test_utils::TestNodeSigner;
+
+use bitcoin::secp256k1::{Secp256k1, PublicKey, SecretKey};
+
+use crate::utils::test_logger;
 
 #[inline]
 fn slice_to_be16(v: &[u8]) -> u16 {
@@ -24,10 +36,13 @@ pub fn do_test(data: &[u8]) {
                }
        }
 
+       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,
@@ -39,16 +54,16 @@ pub fn do_test(data: &[u8]) {
                        Err(_) => return,
                };
                let mut crypter = PeerChannelEncryptor::new_outbound(their_pubkey, ephemeral_key);
-               crypter.get_act_one();
-               match crypter.process_act_two(get_slice!(50), &our_network_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(&our_network_key);
-               match crypter.process_act_one_with_keys(get_slice!(50), &our_network_key, ephemeral_key) {
+               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,
                }
@@ -59,15 +74,17 @@ pub fn do_test(data: &[u8]) {
                assert!(crypter.is_ready_for_encryption());
                crypter
        };
+       let mut buf = [0; 65536 + 16];
        loop {
                if get_slice!(1)[0] == 0 {
-                       crypter.encrypt_message(get_slice!(slice_to_be16(get_slice!(2))));
+                       crypter.encrypt_buffer(MessageBuf::from_encoded(&get_slice!(slice_to_be16(get_slice!(2)))));
                } else {
                        let len = match crypter.decrypt_length_header(get_slice!(16+2)) {
                                Ok(len) => len,
                                Err(_) => return,
                        };
-                       match crypter.decrypt_message(get_slice!(len as usize + 16)) {
+                       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,
                        }
@@ -75,6 +92,10 @@ pub fn do_test(data: &[u8]) {
        }
 }
 
+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) });