Skip to content

Commit 100d4c3

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 acaea4a commit 100d4c3

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
@@ -19,6 +19,15 @@ impl Writeable for Payload {
1919
}
2020
}
2121

22+
/// Reads of `Payload`s are parameterized by the `rho` of a `SharedSecret`, which is used to decrypt
23+
/// the onion message payload's `encrypted_data` field.
24+
impl ReadableArgs<SharedSecret> for Payload {
25+
fn read<R: Read>(mut r: &mut R, encrypted_data_ss: SharedSecret) -> Result<Self, DecodeError> {
26+
// calls:
27+
// * ChaCha20Poly1305RFC::decrypt_in_place
28+
}
29+
}
30+
2231
// Coming soon:
2332
// enum Message {
2433
// InvoiceRequest(InvoiceRequest),
@@ -154,6 +163,9 @@ impl<Signer: Sign, K: Deref> OnionMessager<Signer, K>
154163

155164
impl OnionMessageHandler for OnionMessager {
156165
fn handle_onion_message(&self, peer_node_id: &PublicKey, msg: &msgs::OnionMessage) {
166+
// calls:
167+
// * onion_utils::decode_next_message_hop
168+
// * onion_utils::next_hop_packet_pubkey
157169
}
158170
}
159171

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> {
@@ -515,8 +517,33 @@ pub(super) fn process_onion_failure<T: secp256k1::Signing, L: Deref>(secp_ctx: &
515517
} else { unreachable!(); }
516518
}
517519

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

548-
pub(crate) fn decode_next_hop(shared_secret: [u8; 32], hop_data: &[u8], hmac_bytes: [u8; 32], payment_hash: PaymentHash) -> Result<Hop, OnionDecodeErr> {
575+
pub(crate) fn decode_next_payment_hop(shared_secret: [u8; 32], hop_data: &[u8], hmac_bytes: [u8; 32], payment_hash: PaymentHash) -> Result<PaymentPayload, OnionDecodeErr> {}
576+
577+
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> {}
578+
579+
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> {
549580
let (rho, mu) = gen_rho_mu_from_shared_secret(&shared_secret);
550581
let mut hmac = HmacEngine::<Sha256>::new(&mu);
551582
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)