1 use lightning::ln::peer_channel_encryptor::PeerChannelEncryptor;
3 use bitcoin::secp256k1::key::{PublicKey,SecretKey};
5 use utils::test_logger;
8 fn slice_to_be16(v: &[u8]) -> u16 {
9 ((v[0] as u16) << 8*1) |
10 ((v[1] as u16) << 8*0)
14 pub fn do_test(data: &[u8]) {
16 macro_rules! get_slice {
19 let slice_len = $len as usize;
20 if data.len() < read_pos + slice_len {
23 read_pos += slice_len;
24 &data[read_pos - slice_len..read_pos]
29 let our_network_key = match SecretKey::from_slice(get_slice!(32)) {
33 let ephemeral_key = match SecretKey::from_slice(get_slice!(32)) {
38 let mut crypter = if get_slice!(1)[0] != 0 {
39 let their_pubkey = match PublicKey::from_slice(get_slice!(33)) {
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) {
49 assert!(crypter.is_ready_for_encryption());
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) {
57 match crypter.process_act_three(get_slice!(66)) {
61 assert!(crypter.is_ready_for_encryption());
65 if get_slice!(1)[0] == 0 {
66 crypter.encrypt_message(get_slice!(slice_to_be16(get_slice!(2))));
68 let len = match crypter.decrypt_length_header(get_slice!(16+2)) {
72 match crypter.decrypt_message(get_slice!(len as usize + 16)) {
80 pub fn peer_crypt_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
85 pub extern "C" fn peer_crypt_run(data: *const u8, datalen: usize) {
86 do_test(unsafe { std::slice::from_raw_parts(data, datalen) });