Skip to content

Commit 5407db9

Browse files
Receiving/forwarding onion messages -- internal api changes
This commit covers the internal refactors needed for receiving and forwarding onion messages, and docs updates. Note that we support receiving custom TLVs, just not sending them.
1 parent 6b0196d commit 5407db9

File tree

4 files changed

+51
-16
lines changed

4 files changed

+51
-16
lines changed

lightning/src/ln/channelmanager.rs

+1-13
Original file line numberDiff line numberDiff line change
@@ -2097,21 +2097,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
20972097
},
20982098
onion_utils::Hop::Forward { next_hop_data, next_hop_hmac, new_packet_bytes } => {
20992099
let mut new_pubkey = msg.onion_routing_packet.public_key.unwrap();
2100-
2101-
let blinding_factor = {
2102-
let mut sha = Sha256::engine();
2103-
sha.input(&new_pubkey.serialize()[..]);
2104-
sha.input(&shared_secret);
2105-
Sha256::from_engine(sha).into_inner()
2106-
};
2107-
2108-
let public_key = if let Err(e) = new_pubkey.mul_assign(&self.secp_ctx, &blinding_factor[..]) {
2109-
Err(e)
2110-
} else { Ok(new_pubkey) };
2111-
21122100
let outgoing_packet = msgs::OnionPacket {
21132101
version: 0,
2114-
public_key,
2102+
public_key: onion_utils::next_hop_packet_pubkey(&new_pubkey, &shared_secret),
21152103
hop_data: new_packet_bytes,
21162104
hmac: next_hop_hmac.clone(),
21172105
};

lightning/src/ln/onion_message.rs

+12
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ impl Writeable for Payload {
3636
}
3737
}
3838

39+
/// Reads of `Payload`s are parameterized by the `rho` of a `SharedSecret`, which is used to decrypt
40+
/// the onion message payload's `encrypted_data` field.
41+
impl ReadableArgs<SharedSecret> for Payload {
42+
fn read<R: Read>(mut r: &mut R, encrypted_data_ss: SharedSecret) -> Result<Self, DecodeError> {
43+
// calls:
44+
// * ChaCha20Poly1305RFC::decrypt_in_place
45+
}
46+
}
47+
3948
// Coming soon:
4049
// enum Message {
4150
// InvoiceRequest(InvoiceRequest),
@@ -171,6 +180,9 @@ impl<Signer: Sign, K: Deref> OnionMessenger<Signer, K>
171180

172181
impl OnionMessageHandler for OnionMessenger {
173182
fn handle_onion_message(&self, peer_node_id: &PublicKey, msg: &msgs::OnionMessage) {
183+
// calls:
184+
// * onion_utils::decode_next_message_hop
185+
// * onion_utils::next_hop_packet_pubkey
174186
}
175187
}
176188

lightning/src/ln/onion_utils.rs

+34-3
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ pub(super) fn gen_ammag_from_shared_secret(shared_secret: &[u8]) -> [u8; 32] {
7373
Hmac::from_engine(hmac).into_inner()
7474
}
7575

76+
pub(super) fn next_hop_packet_pubkey(packet_pubkey: &PublicKey, packet_shared_secret: &SharedSecret) -> Result<PublicKey, secp256k1::Error> {}
77+
7678
// can only fail if an intermediary hop has an invalid public key or session_priv is invalid
7779
#[inline]
7880
pub(super) fn construct_onion_keys_callback<T: secp256k1::Signing, FType: FnMut(SharedSecret, [u8; 32], PublicKey, &RouteHop, usize)> (secp_ctx: &Secp256k1<T>, path: &Vec<RouteHop>, session_priv: &SecretKey, mut callback: FType) -> Result<(), secp256k1::Error> {
@@ -519,8 +521,33 @@ pub(super) fn process_onion_failure<T: secp256k1::Signing, L: Deref>(secp_ctx: &
519521
} else { unreachable!(); }
520522
}
521523

522-
/// Data decrypted from the onion payload.
523-
pub(crate) enum Hop {
524+
/// Used in the decoding of inbound payments' and onion messages' routing packets. This enum allows
525+
/// us to use `decode_next_hop` to return the payloads and next hop packet bytes of both payments
526+
/// and onion messages.
527+
enum Payload {
528+
/// This payload was for an incoming payment.
529+
Payment(PaymentPayload),
530+
/// This payload was for an incoming onion message.
531+
Message(MessagePayload),
532+
}
533+
534+
/// Data decrypted from the onion message's onion payload.
535+
pub(crate) enum MessagePayload {
536+
/// This onion payload was for us, not for forwarding to a next-hop.
537+
Receive(onion_message::Payload),
538+
/// This onion payload needs to be forwarded to a next-hop.
539+
Forward {
540+
/// Onion payload data used in forwarding the onion message.
541+
next_hop_data: onion_message::Payload,
542+
/// HMAC of the next hop's onion packet.
543+
next_hop_hmac: [u8; 32],
544+
/// Bytes of the onion packet we're forwarding.
545+
new_packet_bytes: [u8; 20*65],
546+
},
547+
}
548+
549+
/// Data decrypted from the payment's onion payload.
550+
pub(crate) enum PaymentPayload {
524551
/// This onion payload was for us, not for forwarding to a next-hop. Contains information for
525552
/// verifying the incoming payment.
526553
Receive(msgs::OnionHopData),
@@ -549,7 +576,11 @@ pub(crate) enum OnionDecodeErr {
549576
},
550577
}
551578

552-
pub(crate) fn decode_next_hop(shared_secret: [u8; 32], hop_data: &[u8], hmac_bytes: [u8; 32], payment_hash: PaymentHash) -> Result<Hop, OnionDecodeErr> {
579+
pub(crate) fn decode_next_payment_hop(shared_secret: [u8; 32], hop_data: &[u8], hmac_bytes: [u8; 32], payment_hash: PaymentHash) -> Result<PaymentPayload, OnionDecodeErr> {}
580+
581+
pub(crate) fn decode_next_message_hop(shared_secret: [u8; 32], hop_data: &[u8], hmac_bytes: [u8; 32], encrypted_tlvs_ss: SharedSecret) -> Result<MessagePayload, OnionDecodeErr> {}
582+
583+
fn decode_next_hop(shared_secret: [u8; 32], hop_data: &[u8], hmac_bytes: [u8; 32], payment_hash: Option<PaymentHash>, encrypted_tlvs_ss: Option<SharedSecret>) -> Result<(Payload, Option<([u8; 32], [u8; 20*65])>), OnionDecodeErr> {
553584
let (rho, mu) = gen_rho_mu_from_shared_secret(&shared_secret);
554585
let mut hmac = HmacEngine::<Sha256>::new(&mu);
555586
hmac.input(hop_data);

lightning/src/util/chacha20poly1305rfc.rs

+4
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ mod real_chachapoly {
9696
false
9797
}
9898
}
99+
100+
pub fn decrypt_in_place(&mut self, input_output: &mut [u8], tag: &[u8]) -> bool {}
101+
102+
fn decrypt_inner(&mut self, input: &mut [u8], output: Option<&mut [u8]>, tag: &[u8]) -> bool {}
99103
}
100104
}
101105
#[cfg(not(fuzzing))]

0 commit comments

Comments
 (0)