Skip to content

Commit 392bb8b

Browse files
committed
Refactor: Introduce ReceiveAuthKey
1 parent ef643df commit 392bb8b

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
@@ -26,7 +26,7 @@ use crate::offers::nonce::Nonce;
2626
use crate::offers::offer::OfferId;
2727
use crate::onion_message::packet::ControlTlvs;
2828
use crate::routing::gossip::{NodeId, ReadOnlyNetworkGraph};
29-
use crate::sign::{EntropySource, NodeSigner, Recipient};
29+
use crate::sign::{EntropySource, NodeSigner, ReceiveAuthKey, Recipient};
3030
use crate::types::payment::PaymentHash;
3131
use crate::util::scid_utils;
3232
use crate::util::ser::{FixedLengthReader, LengthReadableArgs, Readable, Writeable, Writer};
@@ -93,7 +93,7 @@ impl BlindedMessagePath {
9393
recipient_node_id,
9494
context,
9595
&blinding_secret,
96-
[41; 32], // TODO: Pass this in
96+
ReceiveAuthKey { inner: [41; 32] }, // TODO: Pass this in
9797
)
9898
.map_err(|_| ())?,
9999
}))
@@ -557,7 +557,7 @@ pub(crate) const MESSAGE_PADDING_ROUND_OFF: usize = 100;
557557
pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
558558
secp_ctx: &Secp256k1<T>, intermediate_nodes: &[MessageForwardNode],
559559
recipient_node_id: PublicKey, context: MessageContext, session_priv: &SecretKey,
560-
local_node_receive_key: [u8; 32],
560+
local_node_receive_key: ReceiveAuthKey,
561561
) -> Result<Vec<BlindedHop>, secp256k1::Error> {
562562
let pks = intermediate_nodes
563563
.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};
@@ -1074,7 +1074,7 @@ where
10741074
},
10751075
}
10761076
};
1077-
let receiving_context_auth_key = [41; 32]; // TODO: pass this in
1077+
let receiving_context_auth_key = ReceiveAuthKey { inner: [41; 32] }; // TODO: pass this in
10781078
let next_hop = onion_utils::decode_next_untagged_hop(
10791079
onion_decode_ss,
10801080
&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
@@ -804,6 +804,20 @@ pub struct PeerStorageKey {
804804
pub inner: [u8; 32],
805805
}
806806

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

0 commit comments

Comments
 (0)