-
Notifications
You must be signed in to change notification settings - Fork 140
Move BOLT11 JIT params to payment metadata #899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| // This file is Copyright its original authors, visible in version control history. | ||
| // | ||
| // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
| // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or | ||
| // http://opensource.org/licenses/MIT>, at your option. You may not use this file except in | ||
| // accordance with one or both of these licenses. | ||
|
|
||
| use bitcoin::hashes::{sha256, Hash, HashEngine, Hmac, HmacEngine}; | ||
| use chacha20_poly1305::{ChaCha20Poly1305, Key, Nonce}; | ||
| use lightning::util::ser::{Readable, Writeable}; | ||
| use lightning_types::payment::{PaymentHash, PaymentSecret}; | ||
|
|
||
| use crate::payment::store::LSPS2Parameters; | ||
|
|
||
| /// Metadata carried in invoice payment metadata fields. | ||
| #[derive(Clone, Debug, PartialEq, Eq)] | ||
| pub(crate) struct PaymentMetadata { | ||
| pub(crate) lsps2_parameters: Option<LSPS2Parameters>, | ||
| } | ||
|
|
||
| #[derive(Clone, Copy)] | ||
| pub(crate) struct PaymentMetadataKeys { | ||
| encryption_key: [u8; 32], | ||
| nonce_key: [u8; 32], | ||
| } | ||
|
|
||
| impl PaymentMetadataKeys { | ||
| pub(crate) fn new(base_secret: [u8; 32]) -> Self { | ||
| Self { | ||
| encryption_key: hmac_sha256(&base_secret, b"ldk_node_payment_metadata_encryption_key"), | ||
|
joostjager marked this conversation as resolved.
|
||
| nonce_key: hmac_sha256(&base_secret, b"ldk_node_payment_metadata_nonce_key"), | ||
| } | ||
| } | ||
|
|
||
| fn nonce(&self, payment_hash: &PaymentHash, payment_secret: &PaymentSecret) -> [u8; 12] { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it totally ruled out that payment hash and secret are never reused, also not in some manual flow for example? Perhaps defensively picking a random nonce and storing it in the metadata is safer?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, pretty much. If we reuse the payment hash for example we have worse issues than just privacy leakage at our hands. Happy to store the nonce if you insist, but note that we try to keep invoices as small as possible, in particular for QR encoding.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still I think it is better to not rely on that, but might be worth getting a 2nd opinion.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Related question: if size is important, is the current scheme minimal? Perhaps the double tlv wrapper and/or u64 can be shaved down too.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And with lightningdevkit/rust-lightning#4528, perhaps the tag isn't needed anymore for auth, because auth is already on the rust-lightning level?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
We want the wrapper for clean extensibility, and I'd rather not go down the road of using something besides
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not quite sure how this is supposed to work? As of lightningdevkit/rust-lightning#4528 we need the encrypted data first to calculate the payment secret, so we can't encrypt using the payment secret has a key. Not quite sure what the solution is, though, it does seem like we should be able to just encrypt without an extra IV. For auto-generated-payment_hashes, the payment secret includes a random IV, so we should be able to use that, it needs an API tweak though. For manually-provided payment_hashes I think using the payment_hash as the encryption key is fine - we're ultimately storing information about a specific payment in the invoice. If someone generates a duplicate bolt 11 invoice with the same payment hash and the same amount and expiry, that's the same payment - an attacker being able to swap the payment metadata between them isn't an interesting attack imo.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah, lol, I see your point. Not sure I missed that after reviewing #4528.
Well, if we don't want to randomize and store nonces, this brings up the question again I had raised on lightningdevkit/rust-lightning#4528 (comment): Should we just add the entire payment-metadata encryption wrapper upstream (maybe even as a simple |
||
| let mut engine = HmacEngine::<sha256::Hash>::new(&self.nonce_key); | ||
| engine.input(b"ldk_node_payment_metadata_nonce"); | ||
| engine.input(&payment_hash.0); | ||
| engine.input(&payment_secret.0); | ||
| let hmac = Hmac::<sha256::Hash>::from_engine(engine).to_byte_array(); | ||
|
|
||
| let mut nonce = [0u8; 12]; | ||
| nonce.copy_from_slice(&hmac[..12]); | ||
| nonce | ||
| } | ||
| } | ||
|
|
||
| const PAYMENT_METADATA_AAD: &[u8] = b"ldk_node_payment_metadata"; | ||
| const PAYMENT_METADATA_TAG_LEN: usize = 16; | ||
|
|
||
| /// Encrypted invoice payment metadata. | ||
| pub(crate) struct EncryptedPaymentMetadata { | ||
| pub(crate) raw: Vec<u8>, | ||
| } | ||
|
|
||
| impl PaymentMetadata { | ||
| pub(crate) fn encrypt( | ||
| &self, keys: &PaymentMetadataKeys, payment_hash: &PaymentHash, | ||
| payment_secret: &PaymentSecret, | ||
| ) -> EncryptedPaymentMetadata { | ||
| let nonce = keys.nonce(payment_hash, payment_secret); | ||
| let mut ciphertext = sealed::PaymentMetadataTlv::from(self.clone()).encode(); | ||
| let cipher = ChaCha20Poly1305::new(Key::new(keys.encryption_key), Nonce::new(nonce)); | ||
| let tag = cipher.encrypt(&mut ciphertext, Some(PAYMENT_METADATA_AAD)); | ||
|
joostjager marked this conversation as resolved.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we HMAC'ing this rather than just encrypting? Is that required anywhere we're storing this? AFAIU this ends up in the onion and every byte counts.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, see #899 (comment), Joost also brough up if we could save some bytes by dropping the tag and assuming it empty. Probably makes sense. |
||
|
|
||
| let mut raw = Vec::with_capacity(tag.len() + ciphertext.len()); | ||
| raw.extend_from_slice(&tag); | ||
| raw.extend_from_slice(&ciphertext); | ||
|
|
||
| EncryptedPaymentMetadata { raw } | ||
| } | ||
| } | ||
|
|
||
| impl EncryptedPaymentMetadata { | ||
| pub(crate) fn from_raw(raw: Vec<u8>) -> Self { | ||
| Self { raw } | ||
| } | ||
|
|
||
| pub(crate) fn decrypt( | ||
| &self, keys: &PaymentMetadataKeys, payment_hash: &PaymentHash, | ||
| payment_secret: &PaymentSecret, | ||
| ) -> Option<PaymentMetadata> { | ||
| if self.raw.len() < PAYMENT_METADATA_TAG_LEN { | ||
| return None; | ||
| } | ||
|
|
||
| let mut tag = [0u8; PAYMENT_METADATA_TAG_LEN]; | ||
| tag.copy_from_slice(&self.raw[..PAYMENT_METADATA_TAG_LEN]); | ||
|
|
||
| let mut plaintext = self.raw[PAYMENT_METADATA_TAG_LEN..].to_vec(); | ||
| let nonce = keys.nonce(payment_hash, payment_secret); | ||
| let cipher = ChaCha20Poly1305::new(Key::new(keys.encryption_key), Nonce::new(nonce)); | ||
| cipher.decrypt(&mut plaintext, tag, Some(PAYMENT_METADATA_AAD)).ok()?; | ||
|
|
||
| sealed::PaymentMetadataTlv::read(&mut &plaintext[..]).ok().map(Into::into) | ||
| } | ||
| } | ||
|
|
||
| fn hmac_sha256(key: &[u8], data: &[u8]) -> [u8; 32] { | ||
| let mut engine = HmacEngine::<sha256::Hash>::new(key); | ||
| engine.input(data); | ||
| Hmac::<sha256::Hash>::from_engine(engine).to_byte_array() | ||
| } | ||
|
|
||
| mod sealed { | ||
| use lightning::impl_writeable_tlv_based; | ||
|
|
||
| use crate::payment::metadata::PaymentMetadata; | ||
| use crate::payment::store::LSPS2Parameters; | ||
|
|
||
| pub(super) struct PaymentMetadataTlv { | ||
| pub(super) lsps2_parameters: Option<LSPS2Parameters>, | ||
| } | ||
|
|
||
| impl_writeable_tlv_based!(PaymentMetadataTlv, { | ||
| (0, lsps2_parameters, option), | ||
| }); | ||
|
|
||
| impl From<PaymentMetadata> for PaymentMetadataTlv { | ||
| fn from(metadata: PaymentMetadata) -> Self { | ||
| Self { lsps2_parameters: metadata.lsps2_parameters } | ||
| } | ||
| } | ||
|
|
||
| impl From<PaymentMetadataTlv> for PaymentMetadata { | ||
| fn from(metadata: PaymentMetadataTlv) -> Self { | ||
| Self { lsps2_parameters: metadata.lsps2_parameters } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn empty_metadata_encrypts_and_decrypts() { | ||
| let metadata = PaymentMetadata { lsps2_parameters: None }; | ||
| let keys = PaymentMetadataKeys::new([42; 32]); | ||
| let payment_hash = PaymentHash([7; 32]); | ||
| let payment_secret = PaymentSecret([8; 32]); | ||
|
|
||
| let encrypted = metadata.encrypt(&keys, &payment_hash, &payment_secret); | ||
| let decrypted = encrypted.decrypt(&keys, &payment_hash, &payment_secret).unwrap(); | ||
|
|
||
| assert_eq!(metadata, decrypted); | ||
| } | ||
|
|
||
| #[test] | ||
| fn lsps2_parameters_encrypt_and_decrypt() { | ||
| let lsps2_parameters = LSPS2Parameters { | ||
| max_total_opening_fee_msat: Some(42_000), | ||
| max_proportional_opening_fee_ppm_msat: Some(17_000), | ||
| }; | ||
| let metadata = PaymentMetadata { lsps2_parameters: Some(lsps2_parameters) }; | ||
| let keys = PaymentMetadataKeys::new([42; 32]); | ||
| let payment_hash = PaymentHash([7; 32]); | ||
| let payment_secret = PaymentSecret([8; 32]); | ||
|
|
||
| let encrypted = metadata.encrypt(&keys, &payment_hash, &payment_secret); | ||
| let decrypted = encrypted.decrypt(&keys, &payment_hash, &payment_secret).unwrap(); | ||
|
|
||
| assert_eq!(metadata, decrypted); | ||
| } | ||
|
|
||
| #[test] | ||
| fn encrypted_metadata_uses_deterministic_context_nonce() { | ||
| let metadata = PaymentMetadata { lsps2_parameters: None }; | ||
| let keys = PaymentMetadataKeys::new([42; 32]); | ||
| let payment_hash = PaymentHash([7; 32]); | ||
| let payment_secret = PaymentSecret([8; 32]); | ||
|
|
||
| let encrypted = metadata.encrypt(&keys, &payment_hash, &payment_secret); | ||
| let encrypted_again = metadata.encrypt(&keys, &payment_hash, &payment_secret); | ||
|
|
||
| assert_eq!(encrypted.raw, encrypted_again.raw); | ||
| assert_eq!(encrypted.decrypt(&keys, &payment_hash, &payment_secret), Some(metadata)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn encrypted_metadata_requires_matching_key_and_context() { | ||
| let metadata = PaymentMetadata { lsps2_parameters: None }; | ||
| let keys = PaymentMetadataKeys::new([42; 32]); | ||
| let wrong_keys = PaymentMetadataKeys::new([43; 32]); | ||
| let payment_hash = PaymentHash([7; 32]); | ||
| let wrong_payment_hash = PaymentHash([9; 32]); | ||
| let payment_secret = PaymentSecret([8; 32]); | ||
| let wrong_payment_secret = PaymentSecret([10; 32]); | ||
|
|
||
| let encrypted = metadata.encrypt(&keys, &payment_hash, &payment_secret); | ||
|
|
||
| assert_eq!(encrypted.decrypt(&wrong_keys, &payment_hash, &payment_secret), None); | ||
| assert_eq!(encrypted.decrypt(&keys, &wrong_payment_hash, &payment_secret), None); | ||
| assert_eq!(encrypted.decrypt(&keys, &payment_hash, &wrong_payment_secret), None); | ||
| assert_eq!( | ||
| EncryptedPaymentMetadata::from_raw(vec![0; PAYMENT_METADATA_TAG_LEN + 1]).decrypt( | ||
| &keys, | ||
| &payment_hash, | ||
| &payment_secret | ||
| ), | ||
| None | ||
| ); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this rename is strictly better. Parameters sounds broader than what it is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's intentional as we might add more fields in the BOLT12 context that are not 'fee limits'. Sorry, maybe should have given that rationale in the commit description.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which parameters are that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See https://github.com/lightningdevkit/rust-lightning/pull/4463/changes#diff-dd4e4cab42ecc909f892759965a0ef44080f865dcae8173f084d960f296406b5R32-R39