X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Finbound_payment.rs;h=eeae514fdf3ce4097f79487ed09ceace231cadd4;hb=282b52f7bd874190d3e4b311b8b2a5b870285331;hp=dda7cc2b29a125d214e022b18511e4cfc78fd031;hpb=513c2b4e4b0be0f2ef94dc8908b8cf21e963373a;p=rust-lightning diff --git a/lightning/src/ln/inbound_payment.rs b/lightning/src/ln/inbound_payment.rs index dda7cc2b..eeae514f 100644 --- a/lightning/src/ln/inbound_payment.rs +++ b/lightning/src/ln/inbound_payment.rs @@ -18,8 +18,8 @@ use crate::sign::{KeyMaterial, EntropySource}; use crate::ln::{PaymentHash, PaymentPreimage, PaymentSecret}; use crate::ln::msgs; use crate::ln::msgs::MAX_VALUE_MSAT; -use crate::util::chacha20::ChaCha20; -use crate::util::crypto::hkdf_extract_expand_4x; +use crate::crypto::chacha20::ChaCha20; +use crate::crypto::utils::hkdf_extract_expand_5x; use crate::util::errors::APIError; use crate::util::logger::Logger; @@ -50,6 +50,8 @@ pub struct ExpandedKey { user_pmt_hash_key: [u8; 32], /// The base key used to derive signing keys and authenticate messages for BOLT 12 Offers. offers_base_key: [u8; 32], + /// The key used to encrypt message metadata for BOLT 12 Offers. + offers_encryption_key: [u8; 32], } impl ExpandedKey { @@ -57,20 +59,25 @@ impl ExpandedKey { /// /// It is recommended to cache this value and not regenerate it for each new inbound payment. pub fn new(key_material: &KeyMaterial) -> ExpandedKey { - let (metadata_key, ldk_pmt_hash_key, user_pmt_hash_key, offers_base_key) = - hkdf_extract_expand_4x(b"LDK Inbound Payment Key Expansion", &key_material.0); + let ( + metadata_key, + ldk_pmt_hash_key, + user_pmt_hash_key, + offers_base_key, + offers_encryption_key, + ) = hkdf_extract_expand_5x(b"LDK Inbound Payment Key Expansion", &key_material.0); Self { metadata_key, ldk_pmt_hash_key, user_pmt_hash_key, offers_base_key, + offers_encryption_key, } } /// Returns an [`HmacEngine`] used to construct [`Offer::metadata`]. /// /// [`Offer::metadata`]: crate::offers::offer::Offer::metadata - #[allow(unused)] pub(crate) fn hmac_for_offer( &self, nonce: Nonce, iv_bytes: &[u8; IV_LEN] ) -> HmacEngine { @@ -79,6 +86,13 @@ impl ExpandedKey { hmac.input(&nonce.0); hmac } + + /// Encrypts or decrypts the given `bytes`. Used for data included in an offer message's + /// metadata (e.g., payment id). + pub(crate) fn crypt_for_offer(&self, mut bytes: [u8; 32], nonce: Nonce) -> [u8; 32] { + ChaCha20::encrypt_single_block_in_place(&self.offers_encryption_key, &nonce.0, &mut bytes); + bytes + } } /// A 128-bit number used only once. @@ -88,7 +102,6 @@ impl ExpandedKey { /// /// [`Offer::metadata`]: crate::offers::offer::Offer::metadata /// [`Offer::signing_pubkey`]: crate::offers::offer::Offer::signing_pubkey -#[allow(unused)] #[derive(Clone, Copy, Debug, PartialEq)] pub(crate) struct Nonce(pub(crate) [u8; Self::LENGTH]); @@ -186,9 +199,9 @@ pub fn create(keys: &ExpandedKey, min_value_msat: Option, let mut hmac = HmacEngine::::new(&keys.ldk_pmt_hash_key); hmac.input(&iv_bytes); hmac.input(&metadata_bytes); - let payment_preimage_bytes = Hmac::from_engine(hmac).into_inner(); + let payment_preimage_bytes = Hmac::from_engine(hmac).to_byte_array(); - let ldk_pmt_hash = PaymentHash(Sha256::hash(&payment_preimage_bytes).into_inner()); + let ldk_pmt_hash = PaymentHash(Sha256::hash(&payment_preimage_bytes).to_byte_array()); let payment_secret = construct_payment_secret(&iv_bytes, &metadata_bytes, &keys.metadata_key); Ok((ldk_pmt_hash, payment_secret)) } @@ -214,7 +227,7 @@ pub fn create_from_hash(keys: &ExpandedKey, min_value_msat: Option, payment let mut hmac = HmacEngine::::new(&keys.user_pmt_hash_key); hmac.input(&metadata_bytes); hmac.input(&payment_hash.0); - let hmac_bytes = Hmac::from_engine(hmac).into_inner(); + let hmac_bytes = Hmac::from_engine(hmac).to_byte_array(); let mut iv_bytes = [0 as u8; IV_LEN]; iv_bytes.copy_from_slice(&hmac_bytes[..IV_LEN]); @@ -271,10 +284,9 @@ fn construct_payment_secret(iv_bytes: &[u8; IV_LEN], metadata_bytes: &[u8; METAD let (iv_slice, encrypted_metadata_slice) = payment_secret_bytes.split_at_mut(IV_LEN); iv_slice.copy_from_slice(iv_bytes); - let chacha_block = ChaCha20::get_single_block(metadata_key, iv_bytes); - for i in 0..METADATA_LEN { - encrypted_metadata_slice[i] = chacha_block[i] ^ metadata_bytes[i]; - } + ChaCha20::encrypt_single_block( + metadata_key, iv_bytes, encrypted_metadata_slice, metadata_bytes + ); PaymentSecret(payment_secret_bytes) } @@ -338,7 +350,7 @@ pub(super) fn verify(payment_hash: PaymentHash, payment_data: &msgs::F let mut hmac = HmacEngine::::new(&keys.user_pmt_hash_key); hmac.input(&metadata_bytes[..]); hmac.input(&payment_hash.0); - if !fixed_time_eq(&iv_bytes, &Hmac::from_engine(hmac).into_inner().split_at_mut(IV_LEN).0) { + if !fixed_time_eq(&iv_bytes, &Hmac::from_engine(hmac).to_byte_array().split_at_mut(IV_LEN).0) { log_trace!(logger, "Failing HTLC with user-generated payment_hash {}: unexpected payment_secret", &payment_hash); return Err(()) } @@ -406,11 +418,10 @@ fn decrypt_metadata(payment_secret: PaymentSecret, keys: &ExpandedKey) -> ([u8; let (iv_slice, encrypted_metadata_bytes) = payment_secret.0.split_at(IV_LEN); iv_bytes.copy_from_slice(iv_slice); - let chacha_block = ChaCha20::get_single_block(&keys.metadata_key, &iv_bytes); let mut metadata_bytes: [u8; METADATA_LEN] = [0; METADATA_LEN]; - for i in 0..METADATA_LEN { - metadata_bytes[i] = chacha_block[i] ^ encrypted_metadata_bytes[i]; - } + ChaCha20::encrypt_single_block( + &keys.metadata_key, &iv_bytes, &mut metadata_bytes, encrypted_metadata_bytes + ); (iv_bytes, metadata_bytes) } @@ -421,8 +432,8 @@ fn derive_ldk_payment_preimage(payment_hash: PaymentHash, iv_bytes: &[u8; IV_LEN let mut hmac = HmacEngine::::new(&keys.ldk_pmt_hash_key); hmac.input(iv_bytes); hmac.input(metadata_bytes); - let decoded_payment_preimage = Hmac::from_engine(hmac).into_inner(); - if !fixed_time_eq(&payment_hash.0, &Sha256::hash(&decoded_payment_preimage).into_inner()) { + let decoded_payment_preimage = Hmac::from_engine(hmac).to_byte_array(); + if !fixed_time_eq(&payment_hash.0, &Sha256::hash(&decoded_payment_preimage).to_byte_array()) { return Err(decoded_payment_preimage); } return Ok(PaymentPreimage(decoded_payment_preimage))