@@ -13,9 +13,8 @@ use bitcoin::hashes::cmp::fixed_time_eq;
1313use bitcoin:: hashes:: hmac:: { Hmac , HmacEngine } ;
1414use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
1515use bitcoin:: hashes:: { Hash , HashEngine } ;
16- use chacha20_poly1305:: chacha20:: { ChaCha20 , Key , Nonce } ;
1716
18- use crate :: crypto:: utils:: hkdf_extract_expand_8x;
17+ use crate :: crypto:: utils:: { apply_chacha20 , hkdf_extract_expand_8x} ;
1918use crate :: ln:: msgs;
2019use crate :: ln:: msgs:: MAX_VALUE_MSAT ;
2120use crate :: offers:: nonce:: Nonce as LocalNonce ;
@@ -101,12 +100,7 @@ impl ExpandedKey {
101100 /// Encrypts or decrypts the given `bytes`. Used for data included in an offer message's
102101 /// metadata (e.g., payment id).
103102 pub ( crate ) fn crypt_for_offer ( & self , mut bytes : [ u8 ; 32 ] , nonce : LocalNonce ) -> [ u8 ; 32 ] {
104- ChaCha20 :: new_from_block (
105- Key :: new ( self . offers_encryption_key ) ,
106- Nonce :: new ( nonce. 0 [ 4 ..] . try_into ( ) . unwrap ( ) ) ,
107- u32:: from_le_bytes ( nonce. 0 [ ..4 ] . try_into ( ) . unwrap ( ) ) ,
108- )
109- . apply_keystream ( & mut bytes) ;
103+ apply_chacha20 ( self . offers_encryption_key , nonce. 0 , & mut bytes) ;
110104 bytes
111105 }
112106}
@@ -181,12 +175,7 @@ pub fn create<ES: EntropySource>(
181175 iv_bytes. copy_from_slice ( & rand_bytes[ ..IV_LEN ] ) ;
182176
183177 if let Some ( metadata) = payment_metadata. as_mut ( ) {
184- ChaCha20 :: new_from_block (
185- Key :: new ( keys. metadata_enc_key ) ,
186- Nonce :: new ( iv_bytes[ 4 ..] . try_into ( ) . unwrap ( ) ) ,
187- u32:: from_le_bytes ( iv_bytes[ ..4 ] . try_into ( ) . unwrap ( ) ) ,
188- )
189- . apply_keystream ( metadata. as_mut_slice ( ) ) ;
178+ apply_chacha20 ( keys. metadata_enc_key , iv_bytes, metadata. as_mut_slice ( ) ) ;
190179 }
191180
192181 let mut hmac = HmacEngine :: < Sha256 > :: new ( & keys. ldk_pmt_hash_key ) ;
@@ -238,12 +227,7 @@ pub fn create_from_hash<ES: EntropySource>(
238227 let rand_bytes = entropy_source. get_secure_random_bytes ( ) ;
239228 iv_bytes. copy_from_slice ( & rand_bytes[ ..IV_LEN ] ) ;
240229
241- ChaCha20 :: new_from_block (
242- Key :: new ( keys. metadata_enc_key ) ,
243- Nonce :: new ( iv_bytes[ 4 ..16 ] . try_into ( ) . unwrap ( ) ) ,
244- u32:: from_le_bytes ( iv_bytes[ ..4 ] . try_into ( ) . unwrap ( ) ) ,
245- )
246- . apply_keystream ( metadata. as_mut_slice ( ) ) ;
230+ apply_chacha20 ( keys. metadata_enc_key , iv_bytes, metadata. as_mut_slice ( ) ) ;
247231 metadata. extend_from_slice ( & iv_bytes) ;
248232 }
249233
@@ -349,12 +333,7 @@ fn construct_payment_secret(
349333 iv_slice. copy_from_slice ( iv_bytes) ;
350334
351335 encrypted_info_slice. copy_from_slice ( info_bytes) ;
352- ChaCha20 :: new_from_block (
353- Key :: new ( * info_key) ,
354- Nonce :: new ( iv_bytes[ 4 ..] . try_into ( ) . unwrap ( ) ) ,
355- u32:: from_le_bytes ( iv_bytes[ ..4 ] . try_into ( ) . unwrap ( ) ) ,
356- )
357- . apply_keystream ( encrypted_info_slice) ;
336+ apply_chacha20 ( * info_key, * iv_bytes, encrypted_info_slice) ;
358337
359338 PaymentSecret ( payment_secret_bytes)
360339}
@@ -442,13 +421,9 @@ pub(super) fn verify<L: Logger>(
442421 }
443422 let new_len = metadata. len ( ) - IV_LEN ;
444423 let ( metadata_enc, metadata_iv) = metadata. split_at_mut ( new_len) ;
424+ let metadata_iv: [ u8 ; IV_LEN ] = metadata_iv. try_into ( ) . expect ( "len checked" ) ;
445425
446- ChaCha20 :: new_from_block (
447- Key :: new ( keys. metadata_enc_key ) ,
448- Nonce :: new ( metadata_iv[ 4 ..16 ] . try_into ( ) . unwrap ( ) ) ,
449- u32:: from_le_bytes ( metadata_iv[ ..4 ] . try_into ( ) . unwrap ( ) ) ,
450- )
451- . apply_keystream ( metadata_enc) ;
426+ apply_chacha20 ( keys. metadata_enc_key , metadata_iv, metadata_enc) ;
452427 metadata. truncate ( new_len) ;
453428 }
454429 } ,
@@ -473,12 +448,7 @@ pub(super) fn verify<L: Logger>(
473448 }
474449
475450 if let Some ( metadata) = payment_metadata {
476- ChaCha20 :: new_from_block (
477- Key :: new ( keys. metadata_enc_key ) ,
478- Nonce :: new ( iv_bytes[ 4 ..] . try_into ( ) . unwrap ( ) ) ,
479- u32:: from_le_bytes ( iv_bytes[ ..4 ] . try_into ( ) . unwrap ( ) ) ,
480- )
481- . apply_keystream ( metadata) ;
451+ apply_chacha20 ( keys. metadata_enc_key , iv_bytes, metadata) ;
482452 }
483453 } ,
484454 Ok ( Method :: SpontaneousPayment ) => {
@@ -557,12 +527,7 @@ pub(super) fn get_payment_preimage(
557527 } ) ?;
558528
559529 if let Some ( metadata) = payment_metadata {
560- ChaCha20 :: new_from_block (
561- Key :: new ( keys. metadata_enc_key ) ,
562- Nonce :: new ( iv_bytes[ 4 ..] . try_into ( ) . unwrap ( ) ) ,
563- u32:: from_le_bytes ( iv_bytes[ ..4 ] . try_into ( ) . unwrap ( ) ) ,
564- )
565- . apply_keystream ( metadata) ;
530+ apply_chacha20 ( keys. metadata_enc_key , iv_bytes, metadata) ;
566531 }
567532 Ok ( preimage)
568533 } ,
@@ -590,12 +555,7 @@ fn decrypt_info(
590555
591556 let mut info_bytes: [ u8 ; INFO_LEN ] = [ 0 ; INFO_LEN ] ;
592557 info_bytes. copy_from_slice ( encrypted_info_bytes) ;
593- ChaCha20 :: new_from_block (
594- Key :: new ( keys. info_key ) ,
595- Nonce :: new ( iv_bytes[ 4 ..] . try_into ( ) . unwrap ( ) ) ,
596- u32:: from_le_bytes ( iv_bytes[ ..4 ] . try_into ( ) . unwrap ( ) ) ,
597- )
598- . apply_keystream ( & mut info_bytes) ;
558+ apply_chacha20 ( keys. info_key , iv_bytes, & mut info_bytes) ;
599559
600560 ( iv_bytes, info_bytes)
601561}
0 commit comments