@@ -427,16 +427,21 @@ impl PeerChannelEncryptor {
427
427
Ok ( self . their_node_id . unwrap ( ) . clone ( ) )
428
428
}
429
429
430
- /// Encrypts the given pre-serialized message, returning the encrypted version.
431
- /// panics if msg.len() > 65535 or Noise handshake has not finished.
432
- pub fn encrypt_buffer ( & mut self , msg : & [ u8 ] ) -> Vec < u8 > {
433
- if msg. len ( ) > LN_MAX_MSG_LEN {
430
+ /// Builds sendable bytes for a message.
431
+ ///
432
+ /// `msgbuf` must begin with 16 + 2 dummy/0 bytes, which will be filled with the encrypted
433
+ /// message length and its MAC. It should then be followed by the message bytes themselves
434
+ /// (including the two byte message type).
435
+ ///
436
+ /// For effeciency, the [`Vec::capacity`] should be at least 16 bytes larger than the
437
+ /// [`Vec::length`], to avoid reallocating for the message MAC, which will be appended to the
438
+ /// vec.
439
+ fn encrypt_message_with_header_0s ( & mut self , msgbuf : & mut Vec < u8 > ) {
440
+ let msg_len = msgbuf. len ( ) - 16 - 2 ;
441
+ if msg_len > LN_MAX_MSG_LEN {
434
442
panic ! ( "Attempted to encrypt message longer than 65535 bytes!" ) ;
435
443
}
436
444
437
- let mut res = Vec :: with_capacity ( msg. len ( ) + 16 * 2 + 2 ) ;
438
- res. resize ( msg. len ( ) + 16 * 2 + 2 , 0 ) ;
439
-
440
445
match self . noise_state {
441
446
NoiseState :: Finished { ref mut sk, ref mut sn, ref mut sck, rk : _, rn : _, rck : _ } => {
442
447
if * sn >= 1000 {
@@ -446,16 +451,21 @@ impl PeerChannelEncryptor {
446
451
* sn = 0 ;
447
452
}
448
453
449
- Self :: encrypt_with_ad ( & mut res [ 0 ..16 +2 ] , * sn, sk, & [ 0 ; 0 ] , & ( msg . len ( ) as u16 ) . to_be_bytes ( ) ) ;
454
+ Self :: encrypt_with_ad ( & mut msgbuf [ 0 ..16 +2 ] , * sn, sk, & [ 0 ; 0 ] , & ( msg_len as u16 ) . to_be_bytes ( ) ) ;
450
455
* sn += 1 ;
451
456
452
- Self :: encrypt_with_ad ( & mut res [ 16 +2 .. ] , * sn, sk, & [ 0 ; 0 ] , msg ) ;
457
+ Self :: encrypt_in_place_with_ad ( msgbuf , 16 +2 , * sn, sk, & [ 0 ; 0 ] ) ;
453
458
* sn += 1 ;
454
459
} ,
455
460
_ => panic ! ( "Tried to encrypt a message prior to noise handshake completion" ) ,
456
461
}
462
+ }
457
463
458
- res
464
+ /// Encrypts the given pre-serialized message, returning the encrypted version.
465
+ /// panics if msg.len() > 65535 or Noise handshake has not finished.
466
+ pub fn encrypt_buffer ( & mut self , mut msg : MessageBuf ) -> Vec < u8 > {
467
+ self . encrypt_message_with_header_0s ( & mut msg. 0 ) ;
468
+ msg. 0
459
469
}
460
470
461
471
/// Encrypts the given message, returning the encrypted version.
@@ -468,29 +478,7 @@ impl PeerChannelEncryptor {
468
478
res. 0 . resize ( 16 + 2 , 0 ) ;
469
479
wire:: write ( message, & mut res) . expect ( "In-memory messages must never fail to serialize" ) ;
470
480
471
- let msg_len = res. 0 . len ( ) - 16 - 2 ;
472
- if msg_len > LN_MAX_MSG_LEN {
473
- panic ! ( "Attempted to encrypt message longer than 65535 bytes!" ) ;
474
- }
475
-
476
- match self . noise_state {
477
- NoiseState :: Finished { ref mut sk, ref mut sn, ref mut sck, rk : _, rn : _, rck : _ } => {
478
- if * sn >= 1000 {
479
- let ( new_sck, new_sk) = hkdf_extract_expand_twice ( sck, sk) ;
480
- * sck = new_sck;
481
- * sk = new_sk;
482
- * sn = 0 ;
483
- }
484
-
485
- Self :: encrypt_with_ad ( & mut res. 0 [ 0 ..16 +2 ] , * sn, sk, & [ 0 ; 0 ] , & ( msg_len as u16 ) . to_be_bytes ( ) ) ;
486
- * sn += 1 ;
487
-
488
- Self :: encrypt_in_place_with_ad ( & mut res. 0 , 16 +2 , * sn, sk, & [ 0 ; 0 ] ) ;
489
- * sn += 1 ;
490
- } ,
491
- _ => panic ! ( "Tried to encrypt a message prior to noise handshake completion" ) ,
492
- }
493
-
481
+ self . encrypt_message_with_header_0s ( & mut res. 0 ) ;
494
482
res. 0
495
483
}
496
484
@@ -557,9 +545,30 @@ impl PeerChannelEncryptor {
557
545
}
558
546
}
559
547
548
+ /// A buffer which stores an encoded message (including the two message-type bytes) with some
549
+ /// padding to allow for future encryption/MACing.
550
+ pub struct MessageBuf ( Vec < u8 > ) ;
551
+ impl MessageBuf {
552
+ /// Creates a new buffer from an encoded message (i.e. the two message-type bytes followed by
553
+ /// the message contents).
554
+ ///
555
+ /// Panics if the message is longer than 2^16.
556
+ pub fn from_encoded ( encoded_msg : & [ u8 ] ) -> Self {
557
+ if encoded_msg. len ( ) > LN_MAX_MSG_LEN {
558
+ panic ! ( "Attempted to encrypt message longer than 65535 bytes!" ) ;
559
+ }
560
+ // In addition to the message (continaing the two message type bytes), we also have to add
561
+ // the message length header (and its MAC) and the message MAC.
562
+ let mut res = Vec :: with_capacity ( encoded_msg. len ( ) + 16 * 2 + 2 ) ;
563
+ res. resize ( encoded_msg. len ( ) + 16 + 2 , 0 ) ;
564
+ res[ 16 + 2 ..] . copy_from_slice ( & encoded_msg) ;
565
+ Self ( res)
566
+ }
567
+ }
568
+
560
569
#[ cfg( test) ]
561
570
mod tests {
562
- use super :: LN_MAX_MSG_LEN ;
571
+ use super :: { MessageBuf , LN_MAX_MSG_LEN } ;
563
572
564
573
use bitcoin:: secp256k1:: { PublicKey , SecretKey } ;
565
574
use bitcoin:: secp256k1:: Secp256k1 ;
@@ -775,7 +784,7 @@ mod tests {
775
784
776
785
for i in 0 ..1005 {
777
786
let msg = [ 0x68 , 0x65 , 0x6c , 0x6c , 0x6f ] ;
778
- let mut res = outbound_peer. encrypt_buffer ( & msg) ;
787
+ let mut res = outbound_peer. encrypt_buffer ( MessageBuf :: from_encoded ( & msg) ) ;
779
788
assert_eq ! ( res. len( ) , 5 + 2 * 16 + 2 ) ;
780
789
781
790
let len_header = res[ 0 ..2 +16 ] . to_vec ( ) ;
@@ -811,7 +820,7 @@ mod tests {
811
820
fn max_message_len_encryption ( ) {
812
821
let mut outbound_peer = get_outbound_peer_for_initiator_test_vectors ( ) ;
813
822
let msg = [ 4u8 ; LN_MAX_MSG_LEN + 1 ] ;
814
- outbound_peer. encrypt_buffer ( & msg) ;
823
+ outbound_peer. encrypt_buffer ( MessageBuf :: from_encoded ( & msg) ) ;
815
824
}
816
825
817
826
#[ test]
0 commit comments