a95e2ea85652f42ccbe017acdd151b2718dae22d
[rust-lightning] / lightning / src / onion_message / utils.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 //! Onion message utility methods live here.
11
12 use bitcoin::hashes::{Hash, HashEngine};
13 use bitcoin::hashes::hmac::{Hmac, HmacEngine};
14 use bitcoin::hashes::sha256::Hash as Sha256;
15 use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey, Scalar};
16 use bitcoin::secp256k1::ecdh::SharedSecret;
17
18 use crate::ln::onion_utils;
19 use super::blinded_route::BlindedPath;
20 use super::messenger::Destination;
21
22 use crate::prelude::*;
23
24 // TODO: DRY with onion_utils::construct_onion_keys_callback
25 #[inline]
26 pub(super) fn construct_keys_callback<T: secp256k1::Signing + secp256k1::Verification,
27         FType: FnMut(PublicKey, SharedSecret, PublicKey, [u8; 32], Option<PublicKey>, Option<Vec<u8>>)>(
28         secp_ctx: &Secp256k1<T>, unblinded_path: &[PublicKey], destination: Option<Destination>,
29         session_priv: &SecretKey, mut callback: FType
30 ) -> Result<(), secp256k1::Error> {
31         let mut msg_blinding_point_priv = session_priv.clone();
32         let mut msg_blinding_point = PublicKey::from_secret_key(secp_ctx, &msg_blinding_point_priv);
33         let mut onion_packet_pubkey_priv = msg_blinding_point_priv.clone();
34         let mut onion_packet_pubkey = msg_blinding_point.clone();
35
36         macro_rules! build_keys {
37                 ($pk: expr, $blinded: expr, $encrypted_payload: expr) => {{
38                         let encrypted_data_ss = SharedSecret::new(&$pk, &msg_blinding_point_priv);
39
40                         let blinded_hop_pk = if $blinded { $pk } else {
41                                 let hop_pk_blinding_factor = {
42                                         let mut hmac = HmacEngine::<Sha256>::new(b"blinded_node_id");
43                                         hmac.input(encrypted_data_ss.as_ref());
44                                         Hmac::from_engine(hmac).into_inner()
45                                 };
46                                 $pk.mul_tweak(secp_ctx, &Scalar::from_be_bytes(hop_pk_blinding_factor).unwrap())?
47                         };
48                         let onion_packet_ss = SharedSecret::new(&blinded_hop_pk, &onion_packet_pubkey_priv);
49
50                         let rho = onion_utils::gen_rho_from_shared_secret(encrypted_data_ss.as_ref());
51                         let unblinded_pk_opt = if $blinded { None } else { Some($pk) };
52                         callback(blinded_hop_pk, onion_packet_ss, onion_packet_pubkey, rho, unblinded_pk_opt, $encrypted_payload);
53                         (encrypted_data_ss, onion_packet_ss)
54                 }}
55         }
56
57         macro_rules! build_keys_in_loop {
58                 ($pk: expr, $blinded: expr, $encrypted_payload: expr) => {
59                         let (encrypted_data_ss, onion_packet_ss) = build_keys!($pk, $blinded, $encrypted_payload);
60
61                         let msg_blinding_point_blinding_factor = {
62                                 let mut sha = Sha256::engine();
63                                 sha.input(&msg_blinding_point.serialize()[..]);
64                                 sha.input(encrypted_data_ss.as_ref());
65                                 Sha256::from_engine(sha).into_inner()
66                         };
67
68                         msg_blinding_point_priv = msg_blinding_point_priv.mul_tweak(&Scalar::from_be_bytes(msg_blinding_point_blinding_factor).unwrap())?;
69                         msg_blinding_point = PublicKey::from_secret_key(secp_ctx, &msg_blinding_point_priv);
70
71                         let onion_packet_pubkey_blinding_factor = {
72                                 let mut sha = Sha256::engine();
73                                 sha.input(&onion_packet_pubkey.serialize()[..]);
74                                 sha.input(onion_packet_ss.as_ref());
75                                 Sha256::from_engine(sha).into_inner()
76                         };
77                         onion_packet_pubkey_priv = onion_packet_pubkey_priv.mul_tweak(&Scalar::from_be_bytes(onion_packet_pubkey_blinding_factor).unwrap())?;
78                         onion_packet_pubkey = PublicKey::from_secret_key(secp_ctx, &onion_packet_pubkey_priv);
79                 };
80         }
81
82         for pk in unblinded_path {
83                 build_keys_in_loop!(*pk, false, None);
84         }
85         if let Some(dest) = destination {
86                 match dest {
87                         Destination::Node(pk) => {
88                                 build_keys!(pk, false, None);
89                         },
90                         Destination::BlindedPath(BlindedPath { blinded_hops, .. }) => {
91                                 for hop in blinded_hops {
92                                         build_keys_in_loop!(hop.blinded_node_id, true, Some(hop.encrypted_payload));
93                                 }
94                         },
95                 }
96         }
97         Ok(())
98 }