Skip to content

Commit 4bf95b0

Browse files
committed
Refactor: Introduce ReceiveAuthKey
1 parent cfa4877 commit 4bf95b0

File tree

5 files changed

+28
-12
lines changed

5 files changed

+28
-12
lines changed

lightning/src/blinded_path/message.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::ln::onion_utils;
2525
use crate::offers::nonce::Nonce;
2626
use crate::onion_message::packet::ControlTlvs;
2727
use crate::routing::gossip::{NodeId, ReadOnlyNetworkGraph};
28-
use crate::sign::{EntropySource, NodeSigner, Recipient};
28+
use crate::sign::{EntropySource, NodeSigner, ReceiveAuthKey, Recipient};
2929
use crate::types::payment::PaymentHash;
3030
use crate::util::scid_utils;
3131
use crate::util::ser::{FixedLengthReader, LengthReadableArgs, Readable, Writeable, Writer};
@@ -92,7 +92,7 @@ impl BlindedMessagePath {
9292
recipient_node_id,
9393
context,
9494
&blinding_secret,
95-
[41; 32], // TODO: Pass this in
95+
ReceiveAuthKey { inner: [41; 32] }, // TODO: Pass this in
9696
)
9797
.map_err(|_| ())?,
9898
}))
@@ -515,7 +515,7 @@ pub(crate) const MESSAGE_PADDING_ROUND_OFF: usize = 100;
515515
pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
516516
secp_ctx: &Secp256k1<T>, intermediate_nodes: &[MessageForwardNode],
517517
recipient_node_id: PublicKey, context: MessageContext, session_priv: &SecretKey,
518-
local_node_receive_key: [u8; 32],
518+
local_node_receive_key: ReceiveAuthKey,
519519
) -> Result<Vec<BlindedHop>, secp256k1::Error> {
520520
let pks = intermediate_nodes
521521
.iter()

lightning/src/blinded_path/utils.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use crate::crypto::streams::chachapoly_encrypt_with_swapped_aad;
2222
use crate::io;
2323
use crate::ln::onion_utils;
2424
use crate::onion_message::messenger::Destination;
25+
use crate::sign::ReceiveAuthKey;
2526
use crate::util::ser::{Writeable, Writer};
2627

2728
use core::borrow::Borrow;
@@ -157,7 +158,7 @@ where
157158

158159
struct PublicKeyWithTlvs<W: Writeable> {
159160
pubkey: PublicKey,
160-
hop_recv_key: Option<[u8; 32]>,
161+
hop_recv_key: Option<ReceiveAuthKey>,
161162
tlvs: W,
162163
}
163164

@@ -172,7 +173,7 @@ pub(crate) fn construct_blinded_hops<'a, T, I, W>(
172173
) -> Result<Vec<BlindedHop>, secp256k1::Error>
173174
where
174175
T: secp256k1::Signing + secp256k1::Verification,
175-
I: Iterator<Item = ((PublicKey, Option<[u8; 32]>), W)>,
176+
I: Iterator<Item = ((PublicKey, Option<ReceiveAuthKey>), W)>,
176177
W: Writeable,
177178
{
178179
let mut blinded_hops = Vec::with_capacity(unblinded_path.size_hint().0);
@@ -201,11 +202,11 @@ where
201202

202203
/// Encrypt TLV payload to be used as a [`crate::blinded_path::BlindedHop::encrypted_payload`].
203204
fn encrypt_payload<P: Writeable>(
204-
payload: P, encrypted_tlvs_rho: [u8; 32], hop_recv_key: Option<[u8; 32]>,
205+
payload: P, encrypted_tlvs_rho: [u8; 32], hop_recv_key: Option<ReceiveAuthKey>,
205206
) -> Vec<u8> {
206207
let mut payload_data = payload.encode();
207208
if let Some(hop_recv_key) = hop_recv_key {
208-
chachapoly_encrypt_with_swapped_aad(payload_data, encrypted_tlvs_rho, hop_recv_key)
209+
chachapoly_encrypt_with_swapped_aad(payload_data, encrypted_tlvs_rho, hop_recv_key.inner)
209210
} else {
210211
let mut chacha = ChaCha20Poly1305RFC::new(&encrypted_tlvs_rho, &[0; 12], &[]);
211212
let mut tag = [0; 16];

lightning/src/onion_message/messenger.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use crate::ln::msgs::{
4040
};
4141
use crate::ln::onion_utils;
4242
use crate::routing::gossip::{NetworkGraph, NodeId, ReadOnlyNetworkGraph};
43-
use crate::sign::{EntropySource, NodeSigner, Recipient};
43+
use crate::sign::{EntropySource, NodeSigner, ReceiveAuthKey, Recipient};
4444
use crate::types::features::{InitFeatures, NodeFeatures};
4545
use crate::util::async_poll::{MultiResultFuturePoller, ResultFuture};
4646
use crate::util::logger::{Logger, WithContext};
@@ -1068,7 +1068,7 @@ where
10681068
},
10691069
}
10701070
};
1071-
let receiving_context_auth_key = [41; 32]; // TODO: pass this in
1071+
let receiving_context_auth_key = ReceiveAuthKey { inner: [41; 32] }; // TODO: pass this in
10721072
let next_hop = onion_utils::decode_next_untagged_hop(
10731073
onion_decode_ss,
10741074
&msg.onion_routing_packet.hop_data[..],

lightning/src/onion_message/packet.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use crate::blinded_path::message::{BlindedMessagePath, ForwardTlvs, NextMessageH
2121
use crate::crypto::streams::{ChaChaDualPolyReadAdapter, ChaChaPolyWriteAdapter};
2222
use crate::ln::msgs::DecodeError;
2323
use crate::ln::onion_utils;
24+
use crate::sign::ReceiveAuthKey;
2425
use crate::util::logger::Logger;
2526
use crate::util::ser::{
2627
BigSize, FixedLengthReader, LengthLimitedRead, LengthReadable, LengthReadableArgs, Readable,
@@ -262,11 +263,11 @@ impl<T: OnionMessageContents> Writeable for (Payload<T>, [u8; 32]) {
262263

263264
// Uses the provided secret to simultaneously decode and decrypt the control TLVs and data TLV.
264265
impl<H: CustomOnionMessageHandler + ?Sized, L: Logger + ?Sized>
265-
ReadableArgs<(SharedSecret, &H, [u8; 32], &L)>
266+
ReadableArgs<(SharedSecret, &H, ReceiveAuthKey, &L)>
266267
for Payload<ParsedOnionMessageContents<<H as CustomOnionMessageHandler>::CustomMessage>>
267268
{
268269
fn read<R: Read>(
269-
r: &mut R, args: (SharedSecret, &H, [u8; 32], &L),
270+
r: &mut R, args: (SharedSecret, &H, ReceiveAuthKey, &L),
270271
) -> Result<Self, DecodeError> {
271272
let (encrypted_tlvs_ss, handler, receive_tlvs_key, logger) = args;
272273

@@ -279,7 +280,7 @@ impl<H: CustomOnionMessageHandler + ?Sized, L: Logger + ?Sized>
279280
let mut message = None;
280281
decode_tlv_stream_with_custom_tlv_decode!(&mut rd, {
281282
(2, reply_path, option),
282-
(4, read_adapter, (option: LengthReadableArgs, (rho, receive_tlvs_key))),
283+
(4, read_adapter, (option: LengthReadableArgs, (rho, receive_tlvs_key.inner))),
283284
}, |msg_type, msg_reader| {
284285
if msg_type < 64 { return Ok(false) }
285286
// Don't allow reading more than one data TLV from an onion message.

lightning/src/sign/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,20 @@ pub struct PeerStorageKey {
803803
pub inner: [u8; 32],
804804
}
805805

806+
/// A secret key used to authenticate message contexts in received [`BlindedMessagePath`]s.
807+
///
808+
/// This key ensures that a node only accepts incoming messages delivered through
809+
/// blinded paths that it constructed itself.
810+
///
811+
/// [`BlindedMessagePath`]: crate::blinded_path::message::BlindedMessagePath
812+
#[derive(Clone, Copy, PartialEq, Eq)]
813+
pub struct ReceiveAuthKey {
814+
/// Represents the key used to authenticate incoming [`BlindedMessagePath`]s.
815+
///
816+
/// [`BlindedMessagePath`]: crate::blinded_path::message::BlindedMessagePath
817+
pub inner: [u8; 32],
818+
}
819+
806820
/// Specifies the recipient of an invoice.
807821
///
808822
/// This indicates to [`NodeSigner::sign_invoice`] what node secret key should be used to sign

0 commit comments

Comments
 (0)